2  * Device driver for the PMU on 68K-based Apple PowerBooks
 
   4  * The VIA (versatile interface adapter) interfaces to the PMU,
 
   5  * a 6805 microprocessor core whose primary function is to control
 
   6  * battery charging and system power on the PowerBooks.
 
   7  * The PMU also controls the ADB (Apple Desktop Bus) which connects
 
   8  * to the keyboard and mouse, as well as the non-volatile RAM
 
   9  * and the RTC (real time clock) chip.
 
  11  * Adapted for 68K PMU by Joshua M. Thompson
 
  13  * Based largely on the PowerMac PMU code by Paul Mackerras and
 
  16  * Also based on the PMU driver from MkLinux by Apple Computer, Inc.
 
  17  * and the Open Software Foundation, Inc.
 
  21 #include <linux/types.h>
 
  22 #include <linux/errno.h>
 
  23 #include <linux/kernel.h>
 
  24 #include <linux/delay.h>
 
  25 #include <linux/sched.h>
 
  26 #include <linux/miscdevice.h>
 
  27 #include <linux/blkdev.h>
 
  28 #include <linux/pci.h>
 
  29 #include <linux/slab.h>
 
  30 #include <linux/init.h>
 
  31 #include <linux/interrupt.h>
 
  33 #include <linux/adb.h>
 
  34 #include <linux/pmu.h>
 
  35 #include <linux/cuda.h>
 
  37 #include <asm/macintosh.h>
 
  38 #include <asm/macints.h>
 
  39 #include <asm/machw.h>
 
  40 #include <asm/mac_via.h>
 
  42 #include <asm/pgtable.h>
 
  43 #include <asm/system.h>
 
  45 #include <asm/uaccess.h>
 
  47 /* Misc minor number allocated for /dev/pmu */
 
  50 /* VIA registers - spaced 0x200 bytes apart */
 
  51 #define RS              0x200           /* skip between registers */
 
  52 #define B               0               /* B-side data */
 
  53 #define A               RS              /* A-side data */
 
  54 #define DIRB            (2*RS)          /* B-side direction (1=output) */
 
  55 #define DIRA            (3*RS)          /* A-side direction (1=output) */
 
  56 #define T1CL            (4*RS)          /* Timer 1 ctr/latch (low 8 bits) */
 
  57 #define T1CH            (5*RS)          /* Timer 1 counter (high 8 bits) */
 
  58 #define T1LL            (6*RS)          /* Timer 1 latch (low 8 bits) */
 
  59 #define T1LH            (7*RS)          /* Timer 1 latch (high 8 bits) */
 
  60 #define T2CL            (8*RS)          /* Timer 2 ctr/latch (low 8 bits) */
 
  61 #define T2CH            (9*RS)          /* Timer 2 counter (high 8 bits) */
 
  62 #define SR              (10*RS)         /* Shift register */
 
  63 #define ACR             (11*RS)         /* Auxiliary control register */
 
  64 #define PCR             (12*RS)         /* Peripheral control register */
 
  65 #define IFR             (13*RS)         /* Interrupt flag register */
 
  66 #define IER             (14*RS)         /* Interrupt enable register */
 
  67 #define ANH             (15*RS)         /* A-side data, no handshake */
 
  69 /* Bits in B data register: both active low */
 
  70 #define TACK            0x02            /* Transfer acknowledge (input) */
 
  71 #define TREQ            0x04            /* Transfer request (output) */
 
  74 #define SR_CTRL         0x1c            /* Shift register control bits */
 
  75 #define SR_EXT          0x0c            /* Shift on external clock */
 
  76 #define SR_OUT          0x10            /* Shift out if 1 */
 
  78 /* Bits in IFR and IER */
 
  79 #define SR_INT          0x04            /* Shift register full/empty */
 
  80 #define CB1_INT         0x10            /* transition on CB1 input */
 
  82 static enum pmu_state {
 
  90 static struct adb_request *current_req;
 
  91 static struct adb_request *last_req;
 
  92 static struct adb_request *req_awaiting_reply;
 
  93 static unsigned char interrupt_data[32];
 
  94 static unsigned char *reply_ptr;
 
  95 static int data_index;
 
  97 static int adb_int_pending;
 
  98 static int pmu_adb_flags;
 
  99 static int adb_dev_map = 0;
 
 100 static struct adb_request bright_req_1, bright_req_2, bright_req_3;
 
 101 static int pmu_kind = PMU_UNKNOWN;
 
 102 static int pmu_fully_inited = 0;
 
 105 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
 
 107 static int pmu_probe(void);
 
 108 static int pmu_init(void);
 
 109 static void pmu_start(void);
 
 110 static irqreturn_t pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
 
 111 static int pmu_send_request(struct adb_request *req, int sync);
 
 112 static int pmu_autopoll(int devs);
 
 114 static int pmu_reset_bus(void);
 
 115 static int pmu_queue_request(struct adb_request *req);
 
 117 static void pmu_start(void);
 
 118 static void send_byte(int x);
 
 119 static void recv_byte(void);
 
 120 static void pmu_done(struct adb_request *req);
 
 121 static void pmu_handle_data(unsigned char *data, int len,
 
 122                             struct pt_regs *regs);
 
 123 static void set_volume(int level);
 
 124 static void pmu_enable_backlight(int on);
 
 125 static void pmu_set_brightness(int level);
 
 127 struct adb_driver via_pmu_driver = {
 
 138  * This table indicates for each PMU opcode:
 
 139  * - the number of data bytes to be sent with the command, or -1
 
 140  *   if a length byte should be sent,
 
 141  * - the number of response bytes which the PMU will return, or
 
 142  *   -1 if it will send a length byte.
 
 144 static s8 pmu_data_len[256][2] = {
 
 145 /*         0       1       2       3       4       5       6       7  */
 
 146 /*00*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 147 /*08*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
 
 148 /*10*/  { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 149 /*18*/  { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
 
 150 /*20*/  {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 151 /*28*/  { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
 
 152 /*30*/  { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 153 /*38*/  { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
 
 154 /*40*/  { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 155 /*48*/  { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
 
 156 /*50*/  { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
 
 157 /*58*/  { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
 
 158 /*60*/  { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 159 /*68*/  { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
 
 160 /*70*/  { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 161 /*78*/  { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
 
 162 /*80*/  { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 163 /*88*/  { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
 
 164 /*90*/  { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 165 /*98*/  { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
 
 166 /*a0*/  { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
 
 167 /*a8*/  { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
 
 168 /*b0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 169 /*b8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
 
 170 /*c0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 171 /*c8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
 
 172 /*d0*/  { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 173 /*d8*/  { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
 
 174 /*e0*/  {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
 
 175 /*e8*/  { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
 
 176 /*f0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
 
 177 /*f8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
 
 182         if (macintosh_config->adb_type == MAC_ADB_PB1) {
 
 183                 pmu_kind = PMU_68K_V1;
 
 184         } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
 
 185                 pmu_kind = PMU_68K_V2;
 
 199         volatile struct adb_request req;
 
 201         via2[B] |= TREQ;                                /* negate TREQ */
 
 202         via2[DIRB] = (via2[DIRB] | TREQ) & ~TACK;       /* TACK in, TREQ out */
 
 204         pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB);
 
 206         while (!req.complete) {
 
 208                         printk(KERN_ERR "pmu_init: no response from PMU\n");
 
 215         /* ack all pending interrupts */
 
 217         interrupt_data[0] = 1;
 
 218         while (interrupt_data[0] || pmu_state != idle) {
 
 220                         printk(KERN_ERR "pmu_init: timed out acking intrs\n");
 
 223                 if (pmu_state == idle) {
 
 225                         pmu_interrupt(0, NULL, NULL);
 
 231         pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK,
 
 232                         PMU_INT_ADB_AUTO|PMU_INT_SNDBRT|PMU_INT_ADB);
 
 234         while (!req.complete) {
 
 236                         printk(KERN_ERR "pmu_init: no response from PMU\n");
 
 243         bright_req_1.complete = 1;
 
 244         bright_req_2.complete = 1;
 
 245         bright_req_3.complete = 1;
 
 247         if (request_irq(IRQ_MAC_ADB_SR, pmu_interrupt, 0, "pmu-shift",
 
 249                 printk(KERN_ERR "pmu_init: can't get irq %d\n",
 
 253         if (request_irq(IRQ_MAC_ADB_CL, pmu_interrupt, 0, "pmu-clock",
 
 255                 printk(KERN_ERR "pmu_init: can't get irq %d\n",
 
 257                 free_irq(IRQ_MAC_ADB_SR, pmu_interrupt);
 
 261         pmu_fully_inited = 1;
 
 263         /* Enable backlight */
 
 264         pmu_enable_backlight(1);
 
 266         printk("adb: PMU 68K driver v0.5 for Unified ADB.\n");
 
 277 /* Send an ADB command */
 
 279 pmu_send_request(struct adb_request *req, int sync)
 
 283     if (!pmu_fully_inited)
 
 291     switch (req->data[0]) {
 
 293                 for (i = 0; i < req->nbytes - 1; ++i)
 
 294                         req->data[i] = req->data[i+1];
 
 296                 if (pmu_data_len[req->data[0]][1] != 0) {
 
 297                         req->reply[0] = ADB_RET_OK;
 
 301                 ret = pmu_queue_request(req);
 
 304                 switch (req->data[1]) {
 
 306                         if (req->nbytes != 2)
 
 308                         req->data[0] = PMU_READ_RTC;
 
 311                         req->reply[0] = CUDA_PACKET;
 
 313                         req->reply[2] = CUDA_GET_TIME;
 
 314                         ret = pmu_queue_request(req);
 
 317                         if (req->nbytes != 6)
 
 319                         req->data[0] = PMU_SET_RTC;
 
 321                         for (i = 1; i <= 4; ++i)
 
 322                                 req->data[i] = req->data[i+1];
 
 324                         req->reply[0] = CUDA_PACKET;
 
 326                         req->reply[2] = CUDA_SET_TIME;
 
 327                         ret = pmu_queue_request(req);
 
 330                         if (req->nbytes != 4)
 
 332                         req->data[0] = PMU_READ_NVRAM;
 
 333                         req->data[1] = req->data[2];
 
 334                         req->data[2] = req->data[3];
 
 337                         req->reply[0] = CUDA_PACKET;
 
 339                         req->reply[2] = CUDA_GET_PRAM;
 
 340                         ret = pmu_queue_request(req);
 
 343                         if (req->nbytes != 5)
 
 345                         req->data[0] = PMU_WRITE_NVRAM;
 
 346                         req->data[1] = req->data[2];
 
 347                         req->data[2] = req->data[3];
 
 348                         req->data[3] = req->data[4];
 
 351                         req->reply[0] = CUDA_PACKET;
 
 353                         req->reply[2] = CUDA_SET_PRAM;
 
 354                         ret = pmu_queue_request(req);
 
 359                 for (i = req->nbytes - 1; i > 1; --i)
 
 360                         req->data[i+2] = req->data[i];
 
 361                 req->data[3] = req->nbytes - 2;
 
 362                 req->data[2] = pmu_adb_flags;
 
 363                 /*req->data[1] = req->data[1];*/
 
 364                 req->data[0] = PMU_ADB_CMD;
 
 366                 req->reply_expected = 1;
 
 368                 ret = pmu_queue_request(req);
 
 378         while (!req->complete)
 
 385 /* Enable/disable autopolling */
 
 387 pmu_autopoll(int devs)
 
 389         struct adb_request req;
 
 391         if (!pmu_fully_inited) return -ENXIO;
 
 395                 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
 
 396                             adb_dev_map >> 8, adb_dev_map);
 
 399                 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
 
 402         while (!req.complete)
 
 407 /* Reset the ADB bus */
 
 411         struct adb_request req;
 
 413         int save_autopoll = adb_dev_map;
 
 415         if (!pmu_fully_inited) return -ENXIO;
 
 417         /* anyone got a better idea?? */
 
 422         req.data[0] = PMU_ADB_CMD;
 
 424         req.data[2] = 3; /* ADB_BUSRESET ??? */
 
 428         req.reply_expected = 1;
 
 429         if (pmu_queue_request(&req) != 0)
 
 431                 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
 
 434         while (!req.complete)
 
 437         while (!req.complete) {
 
 439                         printk(KERN_ERR "pmu_adb_reset_bus (reset): no response from PMU\n");
 
 446         if (save_autopoll != 0)
 
 447                 pmu_autopoll(save_autopoll);
 
 452 /* Construct and send a pmu request */
 
 454 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
 
 460         if (nbytes < 0 || nbytes > 32) {
 
 461                 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
 
 465         req->nbytes = nbytes;
 
 467         va_start(list, nbytes);
 
 468         for (i = 0; i < nbytes; ++i)
 
 469                 req->data[i] = va_arg(list, int);
 
 471         if (pmu_data_len[req->data[0]][1] != 0) {
 
 472                 req->reply[0] = ADB_RET_OK;
 
 476         req->reply_expected = 0;
 
 477         return pmu_queue_request(req);
 
 481 pmu_queue_request(struct adb_request *req)
 
 486         if (req->nbytes <= 0) {
 
 490         nsend = pmu_data_len[req->data[0]][0];
 
 491         if (nsend >= 0 && req->nbytes != nsend + 1) {
 
 499         local_irq_save(flags);
 
 501         if (current_req != 0) {
 
 502                 last_req->next = req;
 
 507                 if (pmu_state == idle)
 
 511         local_irq_restore(flags);
 
 518         via1[ACR] |= SR_CTRL;
 
 520         via2[B] &= ~TREQ;               /* assert TREQ */
 
 528         via1[ACR] = (via1[ACR] | SR_EXT) & ~SR_OUT;
 
 529         c = via1[SR];           /* resets SR */
 
 537         struct adb_request *req;
 
 539         /* assert pmu_state == idle */
 
 540         /* get the packet to send */
 
 541         local_irq_save(flags);
 
 543         if (req == 0 || pmu_state != idle
 
 544             || (req->reply_expected && req_awaiting_reply))
 
 549         data_len = pmu_data_len[req->data[0]][0];
 
 551         /* set the shift register to shift out and send a byte */
 
 552         send_byte(req->data[0]);
 
 555         local_irq_restore(flags);
 
 563         local_irq_save(flags);
 
 564         if (via1[IFR] & SR_INT) {
 
 566                 pmu_interrupt(IRQ_MAC_ADB_SR, NULL, NULL);
 
 568         if (via1[IFR] & CB1_INT) {
 
 570                 pmu_interrupt(IRQ_MAC_ADB_CL, NULL, NULL);
 
 572         local_irq_restore(flags);
 
 576 pmu_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 
 578         struct adb_request *req;
 
 579         int timeout, bite = 0;  /* to prevent compiler warning */
 
 582         printk("pmu_interrupt: irq %d state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
 
 583                 irq, pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
 
 586         if (irq == IRQ_MAC_ADB_CL) {            /* CB1 interrupt */
 
 588         } else if (irq == IRQ_MAC_ADB_SR) {     /* SR interrupt  */
 
 589                 if (via2[B] & TACK) {
 
 590                         printk(KERN_DEBUG "PMU: SR_INT but ack still high! (%x)\n", via2[B]);
 
 593                 /* if reading grab the byte */
 
 594                 if ((via1[ACR] & SR_OUT) == 0) bite = via1[SR];
 
 596                 /* reset TREQ and wait for TACK to go high */
 
 599                 while (!(via2[B] & TACK)) {
 
 601                                 printk(KERN_ERR "PMU not responding (!ack)\n");
 
 611                                 data_len = req->nbytes - 1;
 
 615                         if (data_index <= data_len) {
 
 616                                 send_byte(req->data[data_index++]);
 
 620                         data_len = pmu_data_len[req->data[0]][1];
 
 623                                 current_req = req->next;
 
 624                                 if (req->reply_expected)
 
 625                                         req_awaiting_reply = req;
 
 631                                 reply_ptr = req->reply + req->reply_len;
 
 639                         pmu_state = reading_intr;
 
 640                         reply_ptr = interrupt_data;
 
 646                         if (data_len == -1) {
 
 649                                         printk(KERN_ERR "PMU: bad reply len %d\n",
 
 652                                 reply_ptr[data_index++] = bite;
 
 654                         if (data_index < data_len) {
 
 659                         if (pmu_state == reading_intr) {
 
 660                                 pmu_handle_data(interrupt_data, data_index, regs);
 
 663                                 current_req = req->next;
 
 664                                 req->reply_len += data_index;
 
 672                         printk(KERN_ERR "pmu_interrupt: unknown state %d?\n",
 
 677         if (pmu_state == idle) {
 
 678                 if (adb_int_pending) {
 
 680                         send_byte(PMU_INT_ACK);
 
 682                 } else if (current_req) {
 
 688         printk("pmu_interrupt: exit state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
 
 689                 pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
 
 695 pmu_done(struct adb_request *req)
 
 702 /* Interrupt data could be the result data from an ADB cmd */
 
 704 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
 
 706         static int show_pmu_ints = 1;
 
 713         if (data[0] & PMU_INT_ADB) {
 
 714                 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
 
 715                         struct adb_request *req = req_awaiting_reply;
 
 717                                 printk(KERN_ERR "PMU: extra ADB reply\n");
 
 720                         req_awaiting_reply = NULL;
 
 724                                 memcpy(req->reply, data + 1, len - 1);
 
 725                                 req->reply_len = len - 1;
 
 729                         adb_input(data+1, len-1, regs, 1);
 
 732                 if (data[0] == 0x08 && len == 3) {
 
 733                         /* sound/brightness buttons pressed */
 
 734                         pmu_set_brightness(data[1] >> 3);
 
 736                 } else if (show_pmu_ints
 
 737                            && !(data[0] == PMU_INT_TICK && len == 1)) {
 
 739                         printk(KERN_DEBUG "pmu intr");
 
 740                         for (i = 0; i < len; ++i)
 
 741                                 printk(" %.2x", data[i]);
 
 747 int backlight_level = -1;
 
 748 int backlight_enabled = 0;
 
 750 #define LEVEL_TO_BRIGHT(lev)    ((lev) < 1? 0x7f: 0x4a - ((lev) << 1))
 
 753 pmu_enable_backlight(int on)
 
 755         struct adb_request req;
 
 758             /* first call: get current backlight value */
 
 759             if (backlight_level < 0) {
 
 763                         pmu_request(&req, NULL, 3, PMU_READ_NVRAM, 0x14, 0xe);
 
 764                         while (!req.complete)
 
 766                         printk(KERN_DEBUG "pmu: nvram returned bright: %d\n", (int)req.reply[1]);
 
 767                         backlight_level = req.reply[1];
 
 770                         backlight_enabled = 0;
 
 774             pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
 
 775                 LEVEL_TO_BRIGHT(backlight_level));
 
 776             while (!req.complete)
 
 779         pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
 
 780             PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
 
 781         while (!req.complete)
 
 783         backlight_enabled = on;
 
 787 pmu_set_brightness(int level)
 
 791         backlight_level = level;
 
 792         bright = LEVEL_TO_BRIGHT(level);
 
 793         if (!backlight_enabled)
 
 795         if (bright_req_1.complete)
 
 796                 pmu_request(&bright_req_1, NULL, 2, PMU_BACKLIGHT_BRIGHT,
 
 798         if (bright_req_2.complete)
 
 799                 pmu_request(&bright_req_2, NULL, 2, PMU_POWER_CTRL,
 
 800                     PMU_POW_BACKLIGHT | (bright < 0x7f ? PMU_POW_ON : PMU_POW_OFF));
 
 804 pmu_enable_irled(int on)
 
 806         struct adb_request req;
 
 808         pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
 
 809             (on ? PMU_POW_ON : PMU_POW_OFF));
 
 810         while (!req.complete)
 
 815 set_volume(int level)
 
 822         return (pmu_kind != PMU_UNKNOWN);
 
 825 #if 0 /* needs some work for 68K */
 
 828  * This struct is used to store config register values for
 
 829  * PCI devices which may get powered off when we sleep.
 
 831 static struct pci_save {
 
 836 static int n_pbook_pci_saves;
 
 842         struct pci_dev *pd = NULL;
 
 846         while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL)
 
 848         n_pbook_pci_saves = npci;
 
 851         ps = (struct pci_save *) kmalloc(npci * sizeof(*ps), GFP_KERNEL);
 
 852         pbook_pci_saves = ps;
 
 857         while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
 
 858                 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
 
 859                 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
 
 860                 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
 
 867 pbook_pci_restore(void)
 
 870         struct pci_save *ps = pbook_pci_saves;
 
 871         struct pci_dev *pd = NULL;
 
 874         while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
 
 875                 if (ps->command == 0)
 
 877                 pci_read_config_word(pd, PCI_COMMAND, &cmd);
 
 878                 if ((ps->command & ~cmd) == 0)
 
 880                 switch (pd->hdr_type) {
 
 881                 case PCI_HEADER_TYPE_NORMAL:
 
 882                         for (j = 0; j < 6; ++j)
 
 883                                 pci_write_config_dword(pd,
 
 884                                         PCI_BASE_ADDRESS_0 + j*4,
 
 885                                         pd->resource[j].start);
 
 886                         pci_write_config_dword(pd, PCI_ROM_ADDRESS,
 
 887                                pd->resource[PCI_ROM_RESOURCE].start);
 
 888                         pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
 
 890                         pci_write_config_word(pd, PCI_INTERRUPT_LINE,
 
 892                         pci_write_config_word(pd, PCI_COMMAND, ps->command);
 
 894                         /* other header types not restored at present */
 
 900  * Put the powerbook to sleep.
 
 902 #define IRQ_ENABLE      ((unsigned int *)0xf3000024)
 
 903 #define MEM_CTRL        ((unsigned int *)0xf8000070)
 
 905 int powerbook_sleep(void)
 
 908         static int save_backlight;
 
 909         static unsigned int save_irqen;
 
 912         unsigned long p, wait;
 
 913         struct adb_request sleep_req;
 
 915         /* Notify device drivers */
 
 916         ret = blocking_notifier_call_chain(&sleep_notifier_list,
 
 918         if (ret & NOTIFY_STOP_MASK)
 
 921         /* Sync the disks. */
 
 922         /* XXX It would be nice to have some way to ensure that
 
 923          * nobody is dirtying any new buffers while we wait. */
 
 926         /* Turn off the display backlight */
 
 927         save_backlight = backlight_enabled;
 
 929                 pmu_enable_backlight(0);
 
 931         /* Give the disks a little time to actually finish writing */
 
 932         for (wait = jiffies + (HZ/4); time_before(jiffies, wait); )
 
 935         /* Disable all interrupts except pmu */
 
 936         save_irqen = in_le32(IRQ_ENABLE);
 
 937         for (i = 0; i < 32; ++i)
 
 938                 if (i != vias->intrs[0].line && (save_irqen & (1 << i)))
 
 940         asm volatile("mtdec %0" : : "r" (0x7fffffff));
 
 942         /* Save the state of PCI config space for some slots */
 
 945         /* Set the memory controller to keep the memory refreshed
 
 946            while we're asleep */
 
 947         for (i = 0x403f; i >= 0x4000; --i) {
 
 948                 out_be32(MEM_CTRL, i);
 
 950                         x = (in_be32(MEM_CTRL) >> 16) & 0x3ff;
 
 956         /* Ask the PMU to put us to sleep */
 
 957         pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
 
 958         while (!sleep_req.complete)
 
 960         /* displacement-flush the L2 cache - necessary? */
 
 961         for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
 
 962                 i = *(volatile int *)p;
 
 965         /* Put the CPU into sleep mode */
 
 966         asm volatile("mfspr %0,1008" : "=r" (hid0) :);
 
 967         hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
 
 968         asm volatile("mtspr 1008,%0" : : "r" (hid0));
 
 969         local_save_flags(msr);
 
 970         msr |= MSR_POW | MSR_EE;
 
 971         local_irq_restore(msr);
 
 974         /* OK, we're awake again, start restoring things */
 
 975         out_be32(MEM_CTRL, 0x3f);
 
 978         /* wait for the PMU interrupt sequence to complete */
 
 982         /* reenable interrupts */
 
 983         for (i = 0; i < 32; ++i)
 
 984                 if (i != vias->intrs[0].line && (save_irqen & (1 << i)))
 
 988         blocking_notifier_call_chain(&sleep_notifier_list, PBOOK_WAKE, NULL);
 
 990         /* reenable ADB autopoll */
 
 991         pmu_adb_autopoll(adb_dev_map);
 
 993         /* Turn on the screen backlight, if it was on before */
 
 995                 pmu_enable_backlight(1);
 
 997         /* Wait for the hard disk to spin up */
 
1003  * Support for /dev/pmu device
 
1005 static int pmu_open(struct inode *inode, struct file *file)
 
1010 static ssize_t pmu_read(struct file *file, char *buf,
 
1011                         size_t count, loff_t *ppos)
 
1016 static ssize_t pmu_write(struct file *file, const char *buf,
 
1017                          size_t count, loff_t *ppos)
 
1022 static int pmu_ioctl(struct inode * inode, struct file *filp,
 
1023                      u_int cmd, u_long arg)
 
1031             case PMU_IOC_GET_BACKLIGHT:
 
1032                 return put_user(backlight_level, (__u32 *)arg);
 
1033             case PMU_IOC_SET_BACKLIGHT:
 
1034                 error = get_user(value, (__u32 *)arg);
 
1036                         pmu_set_brightness(value);
 
1038             case PMU_IOC_GET_MODEL:
 
1039                 return put_user(pmu_kind, (__u32 *)arg);
 
1044 static struct file_operations pmu_device_fops = {
 
1051 static struct miscdevice pmu_device = {
 
1052         PMU_MINOR, "pmu", &pmu_device_fops
 
1055 void pmu_device_init(void)
 
1059         if (misc_register(&pmu_device) < 0)
 
1060                 printk(KERN_ERR "via-pmu68k: cannot register misc device.\n");
 
1062 #endif /* CONFIG_PMAC_PBOOK */