~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/macintosh/via-pmu.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Device driver for the via-pmu on Apple Powermacs.
  3  *
  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 PowerBook 3400 and 2400.
  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.
 10  *
 11  * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
 12  * 
 13  * todo: - Check this driver for smp safety (new Core99 motherboards).
 14  *       - Cleanup synchro between VIA interrupt and GPIO-based PMU
 15  *         interrupt.
 16  *
 17  *
 18  */
 19 #include <stdarg.h>
 20 #include <linux/config.h>
 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/malloc.h>
 30 #include <linux/poll.h>
 31 #include <linux/adb.h>
 32 #include <linux/pmu.h>
 33 #include <linux/cuda.h>
 34 #include <linux/smp_lock.h>
 35 #include <linux/spinlock.h>
 36 #include <asm/prom.h>
 37 #include <asm/machdep.h>
 38 #include <asm/io.h>
 39 #include <asm/pgtable.h>
 40 #include <asm/system.h>
 41 #include <asm/init.h>
 42 #include <asm/irq.h>
 43 #include <asm/hardirq.h>
 44 #include <asm/feature.h>
 45 #include <asm/uaccess.h>
 46 #include <asm/mmu_context.h>
 47 #ifdef CONFIG_PMAC_BACKLIGHT
 48 #include <asm/backlight.h>
 49 #endif
 50 
 51 /* Some compile options */
 52 #undef SUSPEND_USES_PMU
 53 
 54 /* Misc minor number allocated for /dev/pmu */
 55 #define PMU_MINOR       154
 56 
 57 static volatile unsigned char *via;
 58 
 59 /* VIA registers - spaced 0x200 bytes apart */
 60 #define RS              0x200           /* skip between registers */
 61 #define B               0               /* B-side data */
 62 #define A               RS              /* A-side data */
 63 #define DIRB            (2*RS)          /* B-side direction (1=output) */
 64 #define DIRA            (3*RS)          /* A-side direction (1=output) */
 65 #define T1CL            (4*RS)          /* Timer 1 ctr/latch (low 8 bits) */
 66 #define T1CH            (5*RS)          /* Timer 1 counter (high 8 bits) */
 67 #define T1LL            (6*RS)          /* Timer 1 latch (low 8 bits) */
 68 #define T1LH            (7*RS)          /* Timer 1 latch (high 8 bits) */
 69 #define T2CL            (8*RS)          /* Timer 2 ctr/latch (low 8 bits) */
 70 #define T2CH            (9*RS)          /* Timer 2 counter (high 8 bits) */
 71 #define SR              (10*RS)         /* Shift register */
 72 #define ACR             (11*RS)         /* Auxiliary control register */
 73 #define PCR             (12*RS)         /* Peripheral control register */
 74 #define IFR             (13*RS)         /* Interrupt flag register */
 75 #define IER             (14*RS)         /* Interrupt enable register */
 76 #define ANH             (15*RS)         /* A-side data, no handshake */
 77 
 78 /* Bits in B data register: both active low */
 79 #define TACK            0x08            /* Transfer acknowledge (input) */
 80 #define TREQ            0x10            /* Transfer request (output) */
 81 
 82 /* Bits in ACR */
 83 #define SR_CTRL         0x1c            /* Shift register control bits */
 84 #define SR_EXT          0x0c            /* Shift on external clock */
 85 #define SR_OUT          0x10            /* Shift out if 1 */
 86 
 87 /* Bits in IFR and IER */
 88 #define IER_SET         0x80            /* set bits in IER */
 89 #define IER_CLR         0               /* clear bits in IER */
 90 #define SR_INT          0x04            /* Shift register full/empty */
 91 #define CB2_INT         0x08
 92 #define CB1_INT         0x10            /* transition on CB1 input */
 93 
 94 static volatile enum pmu_state {
 95         idle,
 96         sending,
 97         intack,
 98         reading,
 99         reading_intr,
100 } pmu_state;
101 
102 static struct adb_request *current_req;
103 static struct adb_request *last_req;
104 static struct adb_request *req_awaiting_reply;
105 static unsigned char interrupt_data[256]; /* Made bigger: I've been told that might happen */
106 static unsigned char *reply_ptr;
107 static int data_index;
108 static int data_len;
109 static volatile int adb_int_pending;
110 static int pmu_adb_flags;
111 static int adb_dev_map = 0;
112 static struct adb_request bright_req_1, bright_req_2, bright_req_3;
113 static struct device_node *vias;
114 static int pmu_kind = PMU_UNKNOWN;
115 static int pmu_fully_inited = 0;
116 static int pmu_has_adb;
117 static unsigned char *gpio_reg = NULL;
118 static int gpio_irq = -1;
119 static volatile int pmu_suspended = 0;
120 static spinlock_t pmu_lock;
121 
122 int asleep;
123 struct notifier_block *sleep_notifier_list;
124 
125 #ifdef CONFIG_ADB
126 static int pmu_probe(void);
127 static int pmu_init(void);
128 static int pmu_send_request(struct adb_request *req, int sync);
129 static int pmu_adb_autopoll(int devs);
130 static int pmu_adb_reset_bus(void);
131 #endif /* CONFIG_ADB */
132 
133 static int init_pmu(void);
134 static int pmu_queue_request(struct adb_request *req);
135 static void pmu_start(void);
136 static void via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
137 static void send_byte(int x);
138 static void recv_byte(void);
139 static void pmu_sr_intr(struct pt_regs *regs);
140 static void pmu_done(struct adb_request *req);
141 static void pmu_handle_data(unsigned char *data, int len,
142                             struct pt_regs *regs);
143 static void set_volume(int level);
144 static void gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
145 #ifdef CONFIG_PMAC_BACKLIGHT
146 static int pmu_set_backlight_level(int level, void* data);
147 static int pmu_set_backlight_enable(int on, int level, void* data);
148 #endif /* CONFIG_PMAC_BACKLIGHT */
149 #ifdef CONFIG_PMAC_PBOOK
150 static void pmu_pass_intr(unsigned char *data, int len);
151 #endif
152 
153 #ifdef CONFIG_ADB
154 struct adb_driver via_pmu_driver = {
155         "PMU",
156         pmu_probe,
157         pmu_init,
158         pmu_send_request,
159         pmu_adb_autopoll,
160         pmu_poll,
161         pmu_adb_reset_bus
162 };
163 #endif /* CONFIG_ADB */
164 
165 extern void low_sleep_handler(void);
166 extern void sleep_save_intrs(int);
167 extern void sleep_restore_intrs(void);
168 
169 extern int grackle_pcibios_read_config_word(unsigned char bus,
170         unsigned char dev_fn, unsigned char offset, unsigned short *val);
171 
172 extern int grackle_pcibios_write_config_word(unsigned char bus,
173         unsigned char dev_fn, unsigned char offset, unsigned short val);
174 
175 /*
176  * This table indicates for each PMU opcode:
177  * - the number of data bytes to be sent with the command, or -1
178  *   if a length byte should be sent,
179  * - the number of response bytes which the PMU will return, or
180  *   -1 if it will send a length byte.
181  */
182 static const s8 pmu_data_len[256][2] __openfirmwaredata = {
183 /*         0       1       2       3       4       5       6       7  */
184 /*00*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
185 /*08*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
186 /*10*/  { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
187 /*18*/  { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
188 /*20*/  {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
189 /*28*/  { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
190 /*30*/  { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
191 /*38*/  { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
192 /*40*/  { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
193 /*48*/  { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
194 /*50*/  { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
195 /*58*/  { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
196 /*60*/  { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
197 /*68*/  { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
198 /*70*/  { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
199 /*78*/  { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
200 /*80*/  { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
201 /*88*/  { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
202 /*90*/  { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
203 /*98*/  { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
204 /*a0*/  { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
205 /*a8*/  { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
206 /*b0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
207 /*b8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
208 /*c0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
209 /*c8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
210 /*d0*/  { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
211 /*d8*/  { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
212 /*e0*/  {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
213 /*e8*/  { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
214 /*f0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
215 /*f8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
216 };
217 
218 static char *pbook_type[] = {
219         "Unknown PowerBook",
220         "PowerBook 2400/3400/3500(G3)",
221         "PowerBook G3 Series",
222         "1999 PowerBook G3",
223         "Core99"
224 };
225 
226 #ifdef CONFIG_PMAC_BACKLIGHT
227 static struct backlight_controller pmu_backlight_controller = {
228         pmu_set_backlight_enable,
229         pmu_set_backlight_level
230 };
231 #endif /* CONFIG_PMAC_BACKLIGHT */
232 
233 int __openfirmware
234 find_via_pmu()
235 {
236         if (via != 0)
237                 return 1;
238         vias = find_devices("via-pmu");
239         if (vias == 0)
240                 return 0;
241         if (vias->next != 0)
242                 printk(KERN_WARNING "Warning: only using 1st via-pmu\n");
243 
244         if (vias->n_addrs < 1 || vias->n_intrs < 1) {
245                 printk(KERN_ERR "via-pmu: %d addresses, %d interrupts!\n",
246                        vias->n_addrs, vias->n_intrs);
247                 if (vias->n_addrs < 1 || vias->n_intrs < 1)
248                         return 0;
249         }
250 
251         spin_lock_init(&pmu_lock);
252 
253         pmu_has_adb = 1;
254 
255         if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
256             || device_is_compatible(vias->parent, "ohare")))
257                 pmu_kind = PMU_OHARE_BASED;
258         else if (device_is_compatible(vias->parent, "paddington"))
259                 pmu_kind = PMU_PADDINGTON_BASED;
260         else if (device_is_compatible(vias->parent, "heathrow"))
261                 pmu_kind = PMU_HEATHROW_BASED;
262         else if (device_is_compatible(vias->parent, "Keylargo")) {
263                 struct device_node *gpio, *gpiop;
264 
265                 pmu_kind = PMU_KEYLARGO_BASED;
266                 pmu_has_adb = (find_type_devices("adb") != NULL);
267 
268                 gpiop = find_devices("gpio");
269                 if (gpiop && gpiop->n_addrs) {
270                         gpio_reg = ioremap(gpiop->addrs->address, 0x10);
271                         gpio = find_devices("extint-gpio1");
272                         if (gpio && gpio->parent == gpiop && gpio->n_intrs)
273                                 gpio_irq = gpio->intrs[0].line;
274                 }
275         } else
276                 pmu_kind = PMU_UNKNOWN;
277 
278         via = (volatile unsigned char *) ioremap(vias->addrs->address, 0x2000);
279 
280         out_8(&via[IER], IER_CLR | 0x7f);       /* disable all intrs */
281         out_8(&via[IFR], 0x7f);                 /* clear IFR */
282 
283         pmu_state = idle;
284 
285         if (!init_pmu()) {
286                 via = NULL;
287                 return 0;
288         }
289 
290         printk(KERN_INFO "PMU driver initialized for %s\n",
291                pbook_type[pmu_kind]);
292                
293         sys_ctrler = SYS_CTRLER_PMU;
294         
295         return 1;
296 }
297 
298 #ifdef CONFIG_ADB
299 static int __openfirmware
300 pmu_probe()
301 {
302         return vias == NULL? -ENODEV: 0;
303 }
304 
305 static int __openfirmware
306 pmu_init(void)
307 {
308         if (vias == NULL)
309                 return -ENODEV;
310         return 0;
311 }
312 #endif /* CONFIG_ADB */
313 
314 /*
315  * We can't wait until pmu_init gets called, that happens too late.
316  * It happens after IDE and SCSI initialization, which can take a few
317  * seconds, and by that time the PMU could have given up on us and
318  * turned us off.
319  * This is called from arch/ppc/kernel/pmac_setup.c:pmac_init2().
320  */
321 int via_pmu_start(void)
322 {
323         if (vias == NULL)
324                 return -ENODEV;
325 
326         bright_req_1.complete = 1;
327         bright_req_2.complete = 1;
328         bright_req_3.complete = 1;
329 
330         if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
331                         (void *)0)) {
332                 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
333                        vias->intrs[0].line);
334                 return -EAGAIN;
335         }
336 
337         if (pmu_kind == PMU_KEYLARGO_BASED && gpio_irq != -1) {
338                 if (request_irq(gpio_irq, gpio1_interrupt, 0, "GPIO1/ADB", (void *)0))
339                         printk(KERN_ERR "pmu: can't get irq %d (GPIO1)\n", gpio_irq);
340         }
341 
342         /* Enable interrupts */
343         out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
344 
345         pmu_fully_inited = 1;
346 
347 #ifdef CONFIG_PMAC_BACKLIGHT
348         /* Enable backlight */
349         register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
350 #endif /* CONFIG_PMAC_BACKLIGHT */
351 
352         /* Make sure PMU settle down before continuing. This is _very_ important
353          * since the IDE probe may shut interrupts down for quite a bit of time. If
354          * a PMU communication is pending while this happens, the PMU may timeout
355          * Not that on Core99 machines, the PMU keeps sending us environement
356          * messages, we should find a way to either fix IDE or make it call
357          * pmu_suspend() before masking interrupts. This can also happens while
358          * scolling with some fbdevs.
359          */
360         do {
361                 pmu_poll();
362         } while (pmu_state != idle);
363 
364         return 0;
365 }
366 
367 static int __openfirmware
368 init_pmu()
369 {
370         int timeout;
371         struct adb_request req;
372 
373         out_8(&via[B], via[B] | TREQ);                  /* negate TREQ */
374         out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK);  /* TACK in, TREQ out */
375 
376         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0xfc);
377         timeout =  100000;
378         while (!req.complete) {
379                 if (--timeout < 0) {
380                         printk(KERN_ERR "init_pmu: no response from PMU\n");
381                         return 0;
382                 }
383                 udelay(10);
384                 pmu_poll();
385         }
386 
387         /* ack all pending interrupts */
388         timeout = 100000;
389         interrupt_data[0] = 1;
390         while (interrupt_data[0] || pmu_state != idle) {
391                 if (--timeout < 0) {
392                         printk(KERN_ERR "init_pmu: timed out acking intrs\n");
393                         return 0;
394                 }
395                 if (pmu_state == idle)
396                         adb_int_pending = 1;
397                 via_pmu_interrupt(0, 0, 0);
398                 udelay(10);
399         }
400 
401         /* Tell PMU we are ready. Which PMU support this ? */
402         if (pmu_kind == PMU_KEYLARGO_BASED) {
403                 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
404                 while (!req.complete)
405                         pmu_poll();
406         }
407                 
408         return 1;
409 }
410 
411 int
412 pmu_get_model(void)
413 {
414         return pmu_kind;
415 }
416 
417 #ifdef CONFIG_ADB
418 /* Send an ADB command */
419 static int __openfirmware
420 pmu_send_request(struct adb_request *req, int sync)
421 {
422         int i, ret;
423 
424         if ((vias == NULL) || (!pmu_fully_inited)) {
425                 req->complete = 1;
426                 return -ENXIO;
427         }
428 
429         ret = -EINVAL;
430 
431         switch (req->data[0]) {
432         case PMU_PACKET:
433                 for (i = 0; i < req->nbytes - 1; ++i)
434                         req->data[i] = req->data[i+1];
435                 --req->nbytes;
436                 if (pmu_data_len[req->data[0]][1] != 0) {
437                         req->reply[0] = ADB_RET_OK;
438                         req->reply_len = 1;
439                 } else
440                         req->reply_len = 0;
441                 ret = pmu_queue_request(req);
442                 break;
443         case CUDA_PACKET:
444                 switch (req->data[1]) {
445                 case CUDA_GET_TIME:
446                         if (req->nbytes != 2)
447                                 break;
448                         req->data[0] = PMU_READ_RTC;
449                         req->nbytes = 1;
450                         req->reply_len = 3;
451                         req->reply[0] = CUDA_PACKET;
452                         req->reply[1] = 0;
453                         req->reply[2] = CUDA_GET_TIME;
454                         ret = pmu_queue_request(req);
455                         break;
456                 case CUDA_SET_TIME:
457                         if (req->nbytes != 6)
458                                 break;
459                         req->data[0] = PMU_SET_RTC;
460                         req->nbytes = 5;
461                         for (i = 1; i <= 4; ++i)
462                                 req->data[i] = req->data[i+1];
463                         req->reply_len = 3;
464                         req->reply[0] = CUDA_PACKET;
465                         req->reply[1] = 0;
466                         req->reply[2] = CUDA_SET_TIME;
467                         ret = pmu_queue_request(req);
468                         break;
469                 }
470                 break;
471         case ADB_PACKET:
472                 if (!pmu_has_adb)
473                         return -ENXIO;
474                 for (i = req->nbytes - 1; i > 1; --i)
475                         req->data[i+2] = req->data[i];
476                 req->data[3] = req->nbytes - 2;
477                 req->data[2] = pmu_adb_flags;
478                 /*req->data[1] = req->data[1];*/
479                 req->data[0] = PMU_ADB_CMD;
480                 req->nbytes += 2;
481                 req->reply_expected = 1;
482                 req->reply_len = 0;
483                 ret = pmu_queue_request(req);
484                 break;
485         }
486         if (ret) {
487                 req->complete = 1;
488                 return ret;
489         }
490 
491         if (sync)
492                 while (!req->complete)
493                         pmu_poll();
494 
495         return 0;
496 }
497 
498 /* Enable/disable autopolling */
499 static int __openfirmware
500 pmu_adb_autopoll(int devs)
501 {
502         struct adb_request req;
503 
504         if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
505                 return -ENXIO;
506 
507         if (devs) {
508                 adb_dev_map = devs;
509                 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
510                             adb_dev_map >> 8, adb_dev_map);
511                 pmu_adb_flags = 2;
512         } else {
513                 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
514                 pmu_adb_flags = 0;
515         }
516         while (!req.complete)
517                 pmu_poll();
518         return 0;
519 }
520 
521 /* Reset the ADB bus */
522 static int __openfirmware
523 pmu_adb_reset_bus(void)
524 {
525         struct adb_request req;
526         int save_autopoll = adb_dev_map;
527 
528         if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
529                 return -ENXIO;
530 
531         /* anyone got a better idea?? */
532         pmu_adb_autopoll(0);
533 
534         req.nbytes = 5;
535         req.done = NULL;
536         req.data[0] = PMU_ADB_CMD;
537         req.data[1] = 0;
538         req.data[2] = ADB_BUSRESET; /* 3 ??? */
539         req.data[3] = 0;
540         req.data[4] = 0;
541         req.reply_len = 0;
542         req.reply_expected = 1;
543         if (pmu_queue_request(&req) != 0) {
544                 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
545                 return -EIO;
546         }
547         while (!req.complete)
548                 pmu_poll();
549 
550         if (save_autopoll != 0)
551                 pmu_adb_autopoll(save_autopoll);
552 
553         return 0;
554 }
555 #endif /* CONFIG_ADB */
556 
557 /* Construct and send a pmu request */
558 int __openfirmware
559 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
560             int nbytes, ...)
561 {
562         va_list list;
563         int i;
564 
565         if (vias == NULL)
566                 return -ENXIO;
567 
568         if (nbytes < 0 || nbytes > 32) {
569                 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
570                 req->complete = 1;
571                 return -EINVAL;
572         }
573         req->nbytes = nbytes;
574         req->done = done;
575         va_start(list, nbytes);
576         for (i = 0; i < nbytes; ++i)
577                 req->data[i] = va_arg(list, int);
578         va_end(list);
579         if (pmu_data_len[req->data[0]][1] != 0) {
580                 req->reply[0] = ADB_RET_OK;
581                 req->reply_len = 1;
582         } else
583                 req->reply_len = 0;
584         req->reply_expected = 0;
585         return pmu_queue_request(req);
586 }
587 
588 int __openfirmware
589 pmu_queue_request(struct adb_request *req)
590 {
591         unsigned long flags;
592         int nsend;
593 
594         if (via == NULL) {
595                 req->complete = 1;
596                 return -ENXIO;
597         }
598         if (req->nbytes <= 0) {
599                 req->complete = 1;
600                 return 0;
601         }
602         nsend = pmu_data_len[req->data[0]][0];
603         if (nsend >= 0 && req->nbytes != nsend + 1) {
604                 req->complete = 1;
605                 return -EINVAL;
606         }
607 
608         req->next = 0;
609         req->sent = 0;
610         req->complete = 0;
611 
612         spin_lock_irqsave(&pmu_lock, flags);
613         if (current_req != 0) {
614                 last_req->next = req;
615                 last_req = req;
616         } else {
617                 current_req = req;
618                 last_req = req;
619                 if (pmu_state == idle)
620                         pmu_start();
621         }
622         spin_unlock_irqrestore(&pmu_lock, flags);
623 
624         return 0;
625 }
626 
627 static void __openfirmware
628 wait_for_ack(void)
629 {
630         /* Sightly increased the delay, I had one occurence of the message
631          * reported
632          */
633         int timeout = 4000;
634         while ((in_8(&via[B]) & TACK) == 0) {
635                 if (--timeout < 0) {
636                         printk(KERN_ERR "PMU not responding (!ack)\n");
637                         return;
638                 }
639                 udelay(10);
640         }
641 }
642 
643 /* New PMU seems to be very sensitive to those timings, so we make sure
644  * PCI is flushed immediately */
645 static void __openfirmware
646 send_byte(int x)
647 {
648         volatile unsigned char *v = via;
649 
650         out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
651         out_8(&v[SR], x);
652         out_8(&v[B], in_8(&v[B]) & ~TREQ);              /* assert TREQ */
653         (void)in_8(&v[B]);
654 }
655 
656 static void __openfirmware
657 recv_byte()
658 {
659         volatile unsigned char *v = via;
660 
661         out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
662         in_8(&v[SR]);           /* resets SR */
663         out_8(&v[B], in_8(&v[B]) & ~TREQ);
664         (void)in_8(&v[B]);
665 }
666 
667 static volatile int disable_poll;
668 
669 static void __openfirmware
670 pmu_start()
671 {
672         struct adb_request *req;
673 
674         /* assert pmu_state == idle */
675         /* get the packet to send */
676         req = current_req;
677         if (req == 0 || pmu_state != idle
678             || (/*req->reply_expected && */req_awaiting_reply))
679                 return;
680 
681         pmu_state = sending;
682         data_index = 1;
683         data_len = pmu_data_len[req->data[0]][0];
684 
685         /* Sounds safer to make sure ACK is high before writing. This helped
686          * kill a problem with ADB and some iBooks
687          */
688         wait_for_ack();
689         /* set the shift register to shift out and send a byte */
690         send_byte(req->data[0]);
691 }
692 
693 void __openfirmware
694 pmu_poll()
695 {
696         if (!via)
697                 return;
698         if (disable_poll)
699                 return;
700         /* Kicks ADB read when PMU is suspended */
701         if (pmu_suspended)
702                 adb_int_pending = 1;
703         do {
704                 via_pmu_interrupt(0, 0, 0);
705         } while (pmu_suspended && (adb_int_pending || pmu_state != idle
706                 || req_awaiting_reply));
707 }
708 
709 /* This function loops until the PMU is idle and prevents it from
710  * anwsering to ADB interrupts. pmu_request can still be called.
711  * This is done to avoid spurrious shutdowns when we know we'll have
712  * interrupts switched off for a long time
713  */
714 void __openfirmware
715 pmu_suspend(void)
716 {
717         unsigned long flags;
718 #ifdef SUSPEND_USES_PMU
719         struct adb_request *req;
720 #endif
721         if (!via)
722                 return;
723         
724         spin_lock_irqsave(&pmu_lock, flags);
725         pmu_suspended++;
726         if (pmu_suspended > 1) {
727                 spin_unlock_irqrestore(&pmu_lock, flags);
728                 return;
729         }
730 
731         do {
732                 spin_unlock(&pmu_lock);
733                 via_pmu_interrupt(0, 0, 0);
734                 spin_lock(&pmu_lock);
735                 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
736 #ifdef SUSPEND_USES_PMU
737                         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
738                         spin_unlock_irqrestore(&pmu_lock, flags);
739                         while(!req.complete)
740                                 pmu_poll();
741 #else /* SUSPEND_USES_PMU */
742                         if (gpio_irq >= 0)
743                                 disable_irq(gpio_irq);
744                         out_8(&via[IER], CB1_INT | IER_CLR);
745                         spin_unlock_irqrestore(&pmu_lock, flags);
746 #endif /* SUSPEND_USES_PMU */
747                         break;
748                 }
749         } while (1);
750 }
751 
752 void __openfirmware
753 pmu_resume(void)
754 {
755         unsigned long flags;
756 
757         if (!via || (pmu_suspended < 1))
758                 return;
759 
760         spin_lock_irqsave(&pmu_lock, flags);
761         pmu_suspended--;
762         if (pmu_suspended > 0) {
763                 spin_unlock_irqrestore(&pmu_lock, flags);
764                 return;
765         }
766         adb_int_pending = 1;
767 #ifdef SUSPEND_USES_PMU
768         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0xfc);
769         spin_unlock_irqrestore(&pmu_lock, flags);
770         while(!req.complete)
771                 pmu_poll();
772 #else /* SUSPEND_USES_PMU */
773         if (gpio_irq >= 0)
774                 enable_irq(gpio_irq);
775         out_8(&via[IER], CB1_INT | IER_SET);
776         spin_unlock_irqrestore(&pmu_lock, flags);
777         pmu_poll();
778 #endif /* SUSPEND_USES_PMU */
779 }
780 
781 static void __openfirmware
782 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
783 {
784         unsigned long flags;
785         int intr;
786         int nloop = 0;
787 
788         /* This is a bit brutal, we can probably do better */
789         spin_lock_irqsave(&pmu_lock, flags);
790         ++disable_poll;
791                 
792         while ((intr = in_8(&via[IFR])) != 0) {
793                 if (++nloop > 1000) {
794                         printk(KERN_DEBUG "PMU: stuck in intr loop, "
795                                "intr=%x pmu_state=%d\n", intr, pmu_state);
796                         break;
797                 }
798                 if (intr & SR_INT)
799                         pmu_sr_intr(regs);
800                 else if (intr & CB1_INT) {
801                         adb_int_pending = 1;
802                         out_8(&via[IFR], CB1_INT);
803                 }
804                 intr &= ~(SR_INT | CB1_INT);
805                 if (intr != 0) {
806                         out_8(&via[IFR], intr);
807                 }
808         }
809         /* This is not necessary except if synchronous ADB requests are done
810          * with interrupts off, which should not happen. Since I'm not sure
811          * this "wiring" will remain, I'm commenting it out for now. Please do
812          * not remove. -- BenH.
813          */
814 #if 0
815         if (gpio_reg && !pmu_suspended && (in_8(gpio_reg + 0x9) & 0x02) == 0)
816                 adb_int_pending = 1;
817 #endif
818 
819         if (pmu_state == idle) {
820                 if (adb_int_pending) {
821                         pmu_state = intack;
822                         /* Sounds safer to make sure ACK is high before writing.
823                          * This helped kill a problem with ADB and some iBooks
824                          */
825                         wait_for_ack();
826                         send_byte(PMU_INT_ACK);
827                         adb_int_pending = 0;
828                 } else if (current_req) {
829                         pmu_start();
830                 }
831         }
832         
833         --disable_poll;
834         spin_unlock_irqrestore(&pmu_lock, flags);
835 }
836 
837 static void __openfirmware
838 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
839 {
840         adb_int_pending = 1;
841         via_pmu_interrupt(0, 0, 0);
842 }
843 
844 static void __openfirmware
845 pmu_sr_intr(struct pt_regs *regs)
846 {
847         struct adb_request *req;
848         int bite;
849 
850         if (via[B] & TREQ) {
851                 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
852                 out_8(&via[IFR], SR_INT);
853                 return;
854         }
855         /* This one seems to appear with PMU99. According to OF methods,
856          * the protocol didn't change...
857          */
858         if (via[B] & TACK) {
859                 while ((in_8(&via[B]) & TACK) != 0)
860                         ;
861         }
862 
863         /* reset TREQ and wait for TACK to go high */
864         out_8(&via[B], in_8(&via[B]) | TREQ);
865         wait_for_ack();
866 
867         /* if reading grab the byte, and reset the interrupt */
868         if (pmu_state == reading || pmu_state == reading_intr)
869                 bite = in_8(&via[SR]);
870 
871         out_8(&via[IFR], SR_INT);
872 
873         switch (pmu_state) {
874         case sending:
875                 req = current_req;
876                 if (data_len < 0) {
877                         data_len = req->nbytes - 1;
878                         send_byte(data_len);
879                         break;
880                 }
881                 if (data_index <= data_len) {
882                         send_byte(req->data[data_index++]);
883                         break;
884                 }
885                 req->sent = 1;
886                 data_len = pmu_data_len[req->data[0]][1];
887                 if (data_len == 0) {
888                         pmu_state = idle;
889                         current_req = req->next;
890                         if (req->reply_expected)
891                                 req_awaiting_reply = req;
892                         else {
893                                 spin_unlock(&pmu_lock);
894                                 pmu_done(req);
895                                 spin_lock(&pmu_lock);
896                         }
897                 } else {
898                         pmu_state = reading;
899                         data_index = 0;
900                         reply_ptr = req->reply + req->reply_len;
901                         recv_byte();
902                 }
903                 break;
904 
905         case intack:
906                 data_index = 0;
907                 data_len = -1;
908                 pmu_state = reading_intr;
909                 reply_ptr = interrupt_data;
910                 recv_byte();
911                 break;
912 
913         case reading:
914         case reading_intr:
915                 if (data_len == -1) {
916                         data_len = bite;
917                         if (bite > 32)
918                                 printk(KERN_ERR "PMU: bad reply len %d\n",
919                                        bite);
920                 } else {
921                         reply_ptr[data_index++] = bite;
922                 }
923                 if (data_index < data_len) {
924                         recv_byte();
925                         break;
926                 }
927 
928                 if (pmu_state == reading_intr) {
929                         spin_unlock(&pmu_lock);
930                         pmu_handle_data(interrupt_data, data_index, regs);
931                         spin_lock(&pmu_lock);
932                 } else {
933                         req = current_req;
934                         current_req = req->next;
935                         req->reply_len += data_index;
936                         spin_unlock(&pmu_lock);
937                         pmu_done(req);
938                         spin_lock(&pmu_lock);
939                 }
940                 pmu_state = idle;
941 
942                 break;
943 
944         default:
945                 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
946                        pmu_state);
947         }
948 }
949 
950 static void __openfirmware
951 pmu_done(struct adb_request *req)
952 {
953         req->complete = 1;
954         if (req->done)
955                 (*req->done)(req);
956 }
957 
958 /* Interrupt data could be the result data from an ADB cmd */
959 static void __openfirmware
960 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
961 {
962         asleep = 0;
963         if (len < 1) {
964 //              xmon_printk("empty ADB\n");
965                 adb_int_pending = 0;
966                 return;
967         }
968         if (data[0] & PMU_INT_ADB) {
969                 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
970                         struct adb_request *req = req_awaiting_reply;
971                         if (req == 0) {
972                                 printk(KERN_ERR "PMU: extra ADB reply\n");
973                                 return;
974                         }
975                         req_awaiting_reply = 0;
976                         if (len <= 2)
977                                 req->reply_len = 0;
978                         else {
979                                 memcpy(req->reply, data + 1, len - 1);
980                                 req->reply_len = len - 1;
981                         }
982                         pmu_done(req);
983                 } else {
984 #ifdef CONFIG_XMON
985                         if (len == 4 && data[1] == 0x2c) {
986                                 extern int xmon_wants_key, xmon_adb_keycode;
987                                 if (xmon_wants_key) {
988                                         xmon_adb_keycode = data[2];
989                                         return;
990                                 }
991                         }
992 #endif /* CONFIG_XMON */
993 #ifdef CONFIG_ADB
994                         /*
995                          * XXX On the [23]400 the PMU gives us an up
996                          * event for keycodes 0x74 or 0x75 when the PC
997                          * card eject buttons are released, so we
998                          * ignore those events.
999                          */
1000                         if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1001                               && data[1] == 0x2c && data[3] == 0xff
1002                               && (data[2] & ~1) == 0xf4))
1003                                 adb_input(data+1, len-1, regs, 1);
1004 #endif /* CONFIG_ADB */         
1005                 }
1006         } else if (data[0] == 0x08 && len == 3) {
1007                 /* sound/brightness buttons pressed */
1008 #ifdef CONFIG_PMAC_BACKLIGHT
1009                 set_backlight_level(data[1] >> 4);
1010 #endif
1011                 set_volume(data[2]);
1012         } else {
1013 #ifdef CONFIG_PMAC_PBOOK
1014                 pmu_pass_intr(data, len);
1015 #endif
1016         }
1017 }
1018 
1019 #ifdef CONFIG_PMAC_BACKLIGHT
1020 static int backlight_to_bright[] = {
1021         0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1022         0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1023 };
1024  
1025 static int __openfirmware
1026 pmu_set_backlight_enable(int on, int level, void* data)
1027 {
1028         struct adb_request req;
1029         
1030         if (vias == NULL)
1031                 return -ENODEV;
1032 
1033         if (on) {
1034                 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1035                             backlight_to_bright[level]);
1036                 while (!req.complete)
1037                         pmu_poll();
1038         }
1039         pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1040                     PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1041         while (!req.complete)
1042                 pmu_poll();
1043 
1044         return 0;
1045 }
1046 
1047 static int __openfirmware
1048 pmu_set_backlight_level(int level, void* data)
1049 {
1050         if (vias == NULL)
1051                 return -ENODEV;
1052 
1053         if (!bright_req_1.complete)
1054                 return -EAGAIN;
1055         pmu_request(&bright_req_1, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1056                 backlight_to_bright[level]);
1057         if (!bright_req_2.complete)
1058                 return -EAGAIN;
1059         pmu_request(&bright_req_2, NULL, 2, PMU_POWER_CTRL, PMU_POW_BACKLIGHT
1060                 | (level > BACKLIGHT_OFF ? PMU_POW_ON : PMU_POW_OFF));
1061 
1062         return 0;
1063 }
1064 #endif /* CONFIG_PMAC_BACKLIGHT */
1065 
1066 void __openfirmware
1067 pmu_enable_irled(int on)
1068 {
1069         struct adb_request req;
1070 
1071         if (vias == NULL)
1072                 return ;
1073         if (pmu_kind == PMU_KEYLARGO_BASED)
1074                 return ;
1075 
1076         pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1077             (on ? PMU_POW_ON : PMU_POW_OFF));
1078         while (!req.complete)
1079                 pmu_poll();
1080 }
1081 
1082 static void __openfirmware
1083 set_volume(int level)
1084 {
1085 }
1086 
1087 void __openfirmware
1088 pmu_restart(void)
1089 {
1090         struct adb_request req;
1091 
1092         cli();
1093 
1094         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1095                                         PMU_INT_TICK );
1096         while(!req.complete)
1097                 pmu_poll();
1098 
1099         pmu_request(&req, NULL, 1, PMU_RESET);
1100         while(!req.complete || (pmu_state != idle))
1101                 pmu_poll();
1102         for (;;)
1103                 ;
1104 }
1105 
1106 void __openfirmware
1107 pmu_shutdown(void)
1108 {
1109         struct adb_request req;
1110 
1111         cli();
1112 
1113         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1114                                         PMU_INT_TICK );
1115         while(!req.complete)
1116                 pmu_poll();
1117 
1118         pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1119                     'M', 'A', 'T', 'T');
1120         while(!req.complete || (pmu_state != idle))
1121                 pmu_poll();
1122         for (;;)
1123                 ;
1124 }
1125 
1126 int
1127 pmu_present(void)
1128 {
1129         return via != 0;
1130 }
1131 
1132 #ifdef CONFIG_PMAC_PBOOK
1133 
1134 static LIST_HEAD(sleep_notifiers);
1135 
1136 int
1137 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1138 {
1139         struct list_head *list;
1140         struct pmu_sleep_notifier *notifier;
1141 
1142         for (list = sleep_notifiers.next; list != &sleep_notifiers;
1143              list = list->next) {
1144                 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1145                 if (n->priority > notifier->priority)
1146                         break;
1147         }
1148         __list_add(&n->list, list->prev, list);
1149         return 0;
1150 }
1151 
1152 int
1153 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1154 {
1155         if (n->list.next == 0)
1156                 return -ENOENT;
1157         list_del(&n->list);
1158         n->list.next = 0;
1159         return 0;
1160 }
1161 
1162 /* Sleep is broadcast last-to-first */
1163 static int
1164 broadcast_sleep(int when, int fallback)
1165 {
1166         int ret = PBOOK_SLEEP_OK;
1167         struct list_head *list;
1168         struct pmu_sleep_notifier *notifier;
1169 
1170         for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1171              list = list->prev) {
1172                 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1173                 ret = notifier->notifier_call(notifier, when);
1174                 if (ret != PBOOK_SLEEP_OK) {
1175                         printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
1176                                when, notifier, notifier->notifier_call);
1177                         for (; list != &sleep_notifiers; list = list->next) {
1178                                 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1179                                 notifier->notifier_call(notifier, fallback);
1180                         }
1181                         return ret;
1182                 }
1183         }
1184         return ret;
1185 }
1186 
1187 /* Wake is broadcast first-to-last */
1188 static int
1189 broadcast_wake(void)
1190 {
1191         int ret = PBOOK_SLEEP_OK;
1192         struct list_head *list;
1193         struct pmu_sleep_notifier *notifier;
1194 
1195         for (list = sleep_notifiers.next; list != &sleep_notifiers;
1196              list = list->next) {
1197                 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1198                 notifier->notifier_call(notifier, PBOOK_WAKE);
1199         }
1200         return ret;
1201 }
1202 
1203 /*
1204  * This struct is used to store config register values for
1205  * PCI devices which may get powered off when we sleep.
1206  */
1207 static struct pci_save {
1208         u16     command;
1209         u16     cache_lat;
1210         u16     intr;
1211         u32     rom_address;
1212 } *pbook_pci_saves;
1213 static int n_pbook_pci_saves;
1214 
1215 static void __openfirmware
1216 pbook_pci_save(void)
1217 {
1218         int npci;
1219         struct pci_dev *pd;
1220         struct pci_save *ps;
1221 
1222         npci = 0;
1223         pci_for_each_dev(pd) {
1224                 ++npci;
1225         }
1226         n_pbook_pci_saves = npci;
1227         if (npci == 0)
1228                 return;
1229         ps = (struct pci_save *) kmalloc(npci * sizeof(*ps), GFP_KERNEL);
1230         pbook_pci_saves = ps;
1231         if (ps == NULL)
1232                 return;
1233 
1234         pci_for_each_dev(pd) {
1235                 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1236                 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1237                 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1238                 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1239                 ++ps;
1240         }
1241 }
1242 
1243 static void __openfirmware
1244 pbook_pci_restore(void)
1245 {
1246         u16 cmd;
1247         struct pci_save *ps = pbook_pci_saves - 1;
1248         struct pci_dev *pd;
1249         int j;
1250 
1251         pci_for_each_dev(pd) {
1252                 ps++;
1253                 if (ps->command == 0)
1254                         continue;
1255                 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1256                 if ((ps->command & ~cmd) == 0)
1257                         continue;
1258                 switch (pd->hdr_type) {
1259                 case PCI_HEADER_TYPE_NORMAL:
1260                         for (j = 0; j < 6; ++j)
1261                                 pci_write_config_dword(pd,
1262                                         PCI_BASE_ADDRESS_0 + j*4,
1263                                         pd->resource[j].start);
1264                         pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1265                                 ps->rom_address);
1266                         pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1267                                 ps->cache_lat);
1268                         pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1269                                 ps->intr);
1270                         pci_write_config_word(pd, PCI_COMMAND, ps->command);
1271                         break;
1272                         /* other header types not restored at present */
1273                 }
1274         }
1275 }
1276 
1277 #if 0
1278 /* N.B. This doesn't work on the 3400 */
1279 void pmu_blink(int n)
1280 {
1281         struct adb_request req;
1282 
1283         for (; n > 0; --n) {
1284                 pmu_request(&req, NULL, 4, 0xee, 4, 0, 1);
1285                 while (!req.complete) pmu_poll();
1286                 udelay(50000);
1287                 pmu_request(&req, NULL, 4, 0xee, 4, 0, 0);
1288                 while (!req.complete) pmu_poll();
1289                 udelay(50000);
1290         }
1291         udelay(150000);
1292 }
1293 #endif
1294 
1295 /*
1296  * Put the powerbook to sleep.
1297  */
1298  
1299 static u32 save_via[8];
1300 static void save_via_state(void)
1301 {
1302         save_via[0] = in_8(&via[ANH]);
1303         save_via[1] = in_8(&via[DIRA]);
1304         save_via[2] = in_8(&via[B]);
1305         save_via[3] = in_8(&via[DIRB]);
1306         save_via[4] = in_8(&via[PCR]);
1307         save_via[5] = in_8(&via[ACR]);
1308         save_via[6] = in_8(&via[T1CL]);
1309         save_via[7] = in_8(&via[T1CH]);
1310 }
1311 static void restore_via_state(void)
1312 {
1313         out_8(&via[ANH], save_via[0]);
1314         out_8(&via[DIRA], save_via[1]);
1315         out_8(&via[B], save_via[2]);
1316         out_8(&via[DIRB], save_via[3]);
1317         out_8(&via[PCR], save_via[4]);
1318         out_8(&via[ACR], save_via[5]);
1319         out_8(&via[T1CL], save_via[6]);
1320         out_8(&via[T1CH], save_via[7]);
1321         out_8(&via[IER], IER_CLR | 0x7f);       /* disable all intrs */
1322         out_8(&via[IFR], 0x7f);                         /* clear IFR */
1323         out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
1324 }
1325 
1326 #define FEATURE_CTRL(base)      ((unsigned int *)(base + 0x38))
1327 #define GRACKLE_PM      (1<<7)
1328 #define GRACKLE_DOZE    (1<<5)
1329 #define GRACKLE_NAP     (1<<4)
1330 #define GRACKLE_SLEEP   (1<<3)
1331 
1332 int __openfirmware powerbook_sleep_G3(void)
1333 {
1334         unsigned long save_l2cr;
1335         unsigned long wait;
1336         unsigned short pmcr1;
1337         struct adb_request req;
1338         int ret, timeout;
1339 
1340         /* Notify device drivers */
1341         ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
1342         if (ret != PBOOK_SLEEP_OK) {
1343                 printk("pmu: sleep rejected\n");
1344                 return -EBUSY;
1345         }
1346 
1347         /* Sync the disks. */
1348         /* XXX It would be nice to have some way to ensure that
1349          * nobody is dirtying any new buffers while we wait.
1350          * BenH: Moved to _after_ sleep request and changed video
1351          * drivers to vmalloc() during sleep request. This way, all
1352          * vmalloc's are done before actual sleep of block drivers */
1353         fsync_dev(0);
1354 
1355         /* Sleep can fail now. May not be very robust but useful for debugging */
1356         ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
1357         if (ret != PBOOK_SLEEP_OK) {
1358                 printk("pmu: sleep failed\n");
1359                 return -EBUSY;
1360         }
1361 
1362         /* Give the disks a little time to actually finish writing */
1363         for (wait = jiffies + (HZ/2); time_before(jiffies, wait); )
1364                 mb();
1365 
1366         /* Wait for completion of async backlight requests */
1367         while (!bright_req_1.complete || !bright_req_2.complete || !bright_req_3.complete)
1368                 pmu_poll();
1369         
1370         /* Turn off various things. Darwin does some retry tests here... */
1371         pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
1372         while (!req.complete)
1373                 pmu_poll();
1374         pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1375                 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
1376         while (!req.complete)
1377                 pmu_poll();
1378 
1379         /* Disable all interrupts except pmu */
1380         sleep_save_intrs(vias->intrs[0].line);
1381 
1382         /* Make sure the PMU is idle */
1383         while (pmu_state != idle)
1384                 pmu_poll();
1385 
1386         /* Make sure the decrementer won't interrupt us */
1387         asm volatile("mtdec %0" : : "r" (0x7fffffff));
1388         /* Make sure any pending DEC interrupt occuring while we did
1389          * the above didn't re-enable the DEC */
1390         mb();
1391         asm volatile("mtdec %0" : : "r" (0x7fffffff));
1392         
1393         /* Giveup the FPU */
1394         if (current->thread.regs && (current->thread.regs->msr & MSR_FP) != 0)
1395                 giveup_fpu(current);
1396 
1397         /* For 750, save backside cache setting and disable it */
1398         save_l2cr = _get_L2CR();        /* (returns 0 if not 750) */
1399         if (save_l2cr)
1400                 _set_L2CR(0);
1401 
1402         /* Ask the PMU to put us to sleep */
1403         pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
1404         while (!req.complete)
1405                 pmu_poll();
1406 
1407         /* The VIA is supposed not to be restored correctly*/
1408         save_via_state();
1409         /* We shut down some HW */
1410         feature_prepare_for_sleep();
1411 
1412         grackle_pcibios_read_config_word(0,0,0x70,&pmcr1);
1413         /* Apparently, MacOS uses NAP mode for Grackle ??? */
1414         pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP); 
1415         pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
1416         grackle_pcibios_write_config_word(0, 0, 0x70, pmcr1);
1417 
1418         /* Call low-level ASM sleep handler */
1419         low_sleep_handler();
1420 
1421         /* We're awake again, stop grackle PM */
1422         grackle_pcibios_read_config_word(0, 0, 0x70, &pmcr1);
1423         pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP); 
1424         grackle_pcibios_write_config_word(0, 0, 0x70, pmcr1);
1425         
1426         /* Restore things */
1427         feature_wake_up();
1428         restore_via_state();
1429         
1430         /* Restore L2 cache */
1431         if (save_l2cr)
1432                 _set_L2CR(save_l2cr);
1433         
1434         /* Restore userland MMU context */
1435         set_context(current->mm->context, current->mm->pgd);
1436 
1437         /* Re-enable DEC interrupts and kick DEC */
1438         asm volatile("mtdec %0" : : "r" (0x7fffffff));
1439         sti();
1440         asm volatile("mtdec %0" : : "r" (0x10000000));
1441 
1442         /* Power things up */
1443         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0xfc);
1444         while (!req.complete)
1445                 pmu_poll();
1446         pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
1447                         PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
1448         while (!req.complete)
1449                 pmu_poll();
1450         pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1451                         PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
1452         while (!req.complete)
1453                 pmu_poll();
1454 
1455         /* ack all pending interrupts */
1456         timeout = 100000;
1457         interrupt_data[0] = 1;
1458         while (interrupt_data[0] || pmu_state != idle) {
1459                 if (--timeout < 0)
1460                         break;
1461                 if (pmu_state == idle)
1462                         adb_int_pending = 1;
1463                 via_pmu_interrupt(0, 0, 0);
1464                 udelay(10);
1465         }
1466 
1467         /* reenable interrupt controller */
1468         sleep_restore_intrs();
1469 
1470         /* Leave some time for HW to settle down */
1471         mdelay(100);
1472 
1473         /* Notify drivers */
1474         mdelay(10);
1475         broadcast_wake();
1476 
1477         return 0;
1478 }
1479 
1480 /* Not finished yet */
1481 int __openfirmware powerbook_sleep_Core99(void)
1482 {
1483         int ret;
1484         unsigned long save_l2cr;
1485         unsigned long wait;
1486         struct adb_request req;
1487 
1488         /* Notify device drivers */
1489         ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
1490         if (ret != PBOOK_SLEEP_OK) {
1491                 printk("pmu: sleep rejected\n");
1492                 return -EBUSY;
1493         }
1494 
1495         /* Sync the disks. */
1496         /* XXX It would be nice to have some way to ensure that
1497          * nobody is dirtying any new buffers while we wait.
1498          * BenH: Moved to _after_ sleep request and changed video
1499          * drivers to vmalloc() during sleep request. This way, all
1500          * vmalloc's are done before actual sleep of block drivers */
1501         fsync_dev(0);
1502 
1503         /* Sleep can fail now. May not be very robust but useful for debugging */
1504         ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
1505         if (ret != PBOOK_SLEEP_OK) {
1506                 printk("pmu: sleep failed\n");
1507                 return -EBUSY;
1508         }
1509 
1510         /* Give the disks a little time to actually finish writing */
1511         for (wait = jiffies + (HZ/4); time_before(jiffies, wait); )
1512                 mb();
1513 
1514         /* Tell PMU what events will wake us up */
1515         pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
1516                 0xff, 0xff);
1517         while (!req.complete)
1518                 pmu_poll();
1519         pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
1520                 0, PMU_PWR_WAKEUP_KEY | PMU_PWR_WAKEUP_LID_OPEN);
1521         while (!req.complete)
1522                 pmu_poll();
1523                 
1524         /* Disable all interrupts except pmu */
1525         sleep_save_intrs(vias->intrs[0].line);
1526 
1527         /* Make sure the decrementer won't interrupt us */
1528         asm volatile("mtdec %0" : : "r" (0x7fffffff));
1529 
1530         /* Save the state of PCI config space for some slots */
1531         pbook_pci_save();
1532 
1533         feature_prepare_for_sleep();
1534 
1535         /* For 750, save backside cache setting and disable it */
1536         save_l2cr = _get_L2CR();        /* (returns 0 if not 750) */
1537         if (save_l2cr)
1538                 _set_L2CR(0);
1539 
1540         if (current->thread.regs && (current->thread.regs->msr & MSR_FP) != 0)
1541                 giveup_fpu(current);
1542 
1543         /* Ask the PMU to put us to sleep */
1544         pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
1545         while (!req.complete)
1546                 mb();
1547 
1548         cli();
1549         while (pmu_state != idle)
1550                 pmu_poll();
1551 
1552         /* Call low-level ASM sleep handler */
1553         low_sleep_handler();
1554 
1555         /* Make sure the PMU is idle */
1556         while (pmu_state != idle)
1557                 pmu_poll();
1558 
1559         sti();
1560 
1561         feature_wake_up();
1562         pbook_pci_restore();
1563 
1564         set_context(current->mm->context, current->mm->pgd);
1565 
1566         /* Restore L2 cache */
1567         if (save_l2cr)
1568                 _set_L2CR(save_l2cr | 0x200000); /* set invalidate bit */
1569 
1570         /* reenable interrupts */
1571         sleep_restore_intrs();
1572 
1573         /* Tell PMU we are ready */
1574         pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
1575         while (!req.complete)
1576                 pmu_poll();
1577                 
1578         /* Notify drivers */
1579         mdelay(10);
1580         broadcast_wake();
1581 
1582         return 0;
1583 }
1584 
1585 #define PB3400_MEM_CTRL         ((unsigned int *)0xf8000070)
1586 
1587 int __openfirmware powerbook_sleep_3400(void)
1588 {
1589         int ret, i, x;
1590         unsigned long msr;
1591         unsigned int hid0;
1592         unsigned long p, wait;
1593         struct adb_request sleep_req;
1594 
1595         /* Notify device drivers */
1596         ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
1597         if (ret != PBOOK_SLEEP_OK) {
1598                 printk("pmu: sleep rejected\n");
1599                 return -EBUSY;
1600         }
1601 
1602         /* Sync the disks. */
1603         /* XXX It would be nice to have some way to ensure that
1604          * nobody is dirtying any new buffers while we wait.
1605          * BenH: Moved to _after_ sleep request and changed video
1606          * drivers to vmalloc() during sleep request. This way, all
1607          * vmalloc's are done before actual sleep of block drivers */
1608         fsync_dev(0);
1609 
1610         /* Sleep can fail now. May not be very robust but useful for debugging */
1611         ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
1612         if (ret != PBOOK_SLEEP_OK) {
1613                 printk("pmu: sleep failed\n");
1614                 return -EBUSY;
1615         }
1616 
1617         /* Give the disks a little time to actually finish writing */
1618         for (wait = jiffies + (HZ/4); time_before(jiffies, wait); )
1619                 mb();
1620 
1621         /* Disable all interrupts except pmu */
1622         sleep_save_intrs(vias->intrs[0].line);
1623 
1624         /* Make sure the decrementer won't interrupt us */
1625         asm volatile("mtdec %0" : : "r" (0x7fffffff));
1626 
1627         /* Save the state of PCI config space for some slots */
1628         pbook_pci_save();
1629 
1630         /* Set the memory controller to keep the memory refreshed
1631            while we're asleep */
1632         for (i = 0x403f; i >= 0x4000; --i) {
1633                 out_be32(PB3400_MEM_CTRL, i);
1634                 do {
1635                         x = (in_be32(PB3400_MEM_CTRL) >> 16) & 0x3ff;
1636                 } while (x == 0);
1637                 if (x >= 0x100)
1638                         break;
1639         }
1640 
1641         /* Ask the PMU to put us to sleep */
1642         pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
1643         while (!sleep_req.complete)
1644                 mb();
1645 
1646         /* displacement-flush the L2 cache - necessary? */
1647         for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
1648                 i = *(volatile int *)p;
1649         asleep = 1;
1650 
1651         /* Put the CPU into sleep mode */
1652         asm volatile("mfspr %0,1008" : "=r" (hid0) :);
1653         hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
1654         asm volatile("mtspr 1008,%0" : : "r" (hid0));
1655         save_flags(msr);
1656         msr |= MSR_POW | MSR_EE;
1657         restore_flags(msr);
1658         udelay(10);
1659 
1660         /* OK, we're awake again, start restoring things */
1661         out_be32(PB3400_MEM_CTRL, 0x3f);
1662         pbook_pci_restore();
1663 
1664         /* wait for the PMU interrupt sequence to complete */
1665         while (asleep)
1666                 mb();
1667 
1668         /* reenable interrupts */
1669         sleep_restore_intrs();
1670 
1671         /* Notify drivers */
1672         broadcast_wake();
1673 
1674         return 0;
1675 }
1676 
1677 /*
1678  * Support for /dev/pmu device
1679  */
1680 #define RB_SIZE         10
1681 struct pmu_private {
1682         struct list_head list;
1683         int     rb_get;
1684         int     rb_put;
1685         struct rb_entry {
1686                 unsigned short len;
1687                 unsigned char data[16];
1688         }       rb_buf[RB_SIZE];
1689         wait_queue_head_t wait;
1690         spinlock_t lock;
1691 };
1692 
1693 static LIST_HEAD(all_pmu_pvt);
1694 static spinlock_t all_pvt_lock = SPIN_LOCK_UNLOCKED;
1695 
1696 static void pmu_pass_intr(unsigned char *data, int len)
1697 {
1698         struct pmu_private *pp;
1699         struct list_head *list;
1700         int i;
1701         unsigned long flags;
1702 
1703         if (len > sizeof(pp->rb_buf[0].data))
1704                 len = sizeof(pp->rb_buf[0].data);
1705         spin_lock_irqsave(&all_pvt_lock, flags);
1706         for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
1707                 pp = list_entry(list, struct pmu_private, list);
1708                 i = pp->rb_put + 1;
1709                 if (i >= RB_SIZE)
1710                         i = 0;
1711                 if (i != pp->rb_get) {
1712                         struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
1713                         rp->len = len;
1714                         memcpy(rp->data, data, len);
1715                         pp->rb_put = i;
1716                         wake_up_interruptible(&pp->wait);
1717                 }
1718         }
1719         spin_unlock_irqrestore(&all_pvt_lock, flags);
1720 }
1721 
1722 static int __openfirmware pmu_open(struct inode *inode, struct file *file)
1723 {
1724         struct pmu_private *pp;
1725         unsigned long flags;
1726 
1727         pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
1728         if (pp == 0)
1729                 return -ENOMEM;
1730         pp->rb_get = pp->rb_put = 0;
1731         spin_lock_init(&pp->lock);
1732         init_waitqueue_head(&pp->wait);
1733         spin_lock_irqsave(&all_pvt_lock, flags);
1734         list_add(&pp->list, &all_pmu_pvt);
1735         spin_unlock_irqrestore(&all_pvt_lock, flags);
1736         file->private_data = pp;
1737         return 0;
1738 }
1739 
1740 static ssize_t __openfirmware pmu_read(struct file *file, char *buf,
1741                         size_t count, loff_t *ppos)
1742 {
1743         struct pmu_private *pp = file->private_data;
1744         DECLARE_WAITQUEUE(wait, current);
1745         int ret;
1746 
1747         if (count < 1 || pp == 0)
1748                 return -EINVAL;
1749         ret = verify_area(VERIFY_WRITE, buf, count);
1750         if (ret)
1751                 return ret;
1752 
1753         add_wait_queue(&pp->wait, &wait);
1754         current->state = TASK_INTERRUPTIBLE;
1755 
1756         for (;;) {
1757                 ret = -EAGAIN;
1758                 spin_lock(&pp->lock);
1759                 if (pp->rb_get != pp->rb_put) {
1760                         int i = pp->rb_get;
1761                         struct rb_entry *rp = &pp->rb_buf[i];
1762                         ret = rp->len;
1763                         if (ret > count)
1764                                 ret = count;
1765                         if (ret > 0 && copy_to_user(buf, rp->data, ret))
1766                                 ret = -EFAULT;
1767                         if (++i >= RB_SIZE)
1768                                 i = 0;
1769                         pp->rb_get = i;
1770                 }
1771                 spin_unlock(&pp->lock);
1772                 if (ret >= 0)
1773                         break;
1774 
1775                 if (file->f_flags & O_NONBLOCK)
1776                         break;
1777                 ret = -ERESTARTSYS;
1778                 if (signal_pending(current))
1779                         break;
1780                 schedule();
1781         }
1782         current->state = TASK_RUNNING;
1783         remove_wait_queue(&pp->wait, &wait);
1784 
1785         return ret;
1786 }
1787 
1788 static ssize_t __openfirmware pmu_write(struct file *file, const char *buf,
1789                          size_t count, loff_t *ppos)
1790 {
1791         return 0;
1792 }
1793 
1794 static unsigned int pmu_fpoll(struct file *filp, poll_table *wait)
1795 {
1796         struct pmu_private *pp = filp->private_data;
1797         unsigned int mask = 0;
1798 
1799         if (pp == 0)
1800                 return 0;
1801         poll_wait(filp, &pp->wait, wait);
1802         spin_lock(&pp->lock);
1803         if (pp->rb_get != pp->rb_put)
1804                 mask |= POLLIN;
1805         spin_unlock(&pp->lock);
1806         return mask;
1807 }
1808 
1809 static int pmu_release(struct inode *inode, struct file *file)
1810 {
1811         struct pmu_private *pp = file->private_data;
1812         unsigned long flags;
1813 
1814         lock_kernel();
1815         if (pp != 0) {
1816                 file->private_data = 0;
1817                 spin_lock_irqsave(&all_pvt_lock, flags);
1818                 list_del(&pp->list);
1819                 spin_unlock_irqrestore(&all_pvt_lock, flags);
1820                 kfree(pp);
1821         }
1822         unlock_kernel();
1823         return 0;
1824 }
1825 
1826 /* Note: removed __openfirmware here since it causes link errors */
1827 static int pmu_ioctl(struct inode * inode, struct file *filp,
1828                      u_int cmd, u_long arg)
1829 {
1830         int error;
1831 
1832         switch (cmd) {
1833         case PMU_IOC_SLEEP:
1834                 switch (pmu_kind) {
1835                 case PMU_OHARE_BASED:
1836                         error = powerbook_sleep_3400();
1837                         break;
1838                 case PMU_HEATHROW_BASED:
1839                 case PMU_PADDINGTON_BASED:
1840                         error = powerbook_sleep_G3();
1841                         break;
1842 #if 0 /* Not ready yet */
1843                 case PMU_KEYLARGO_BASED:
1844                         error = powerbook_sleep_Core99();
1845                         break;
1846 #endif                  
1847                 default:
1848                         error = -ENOSYS;
1849                 }
1850                 return error;
1851 #ifdef CONFIG_PMAC_BACKLIGHT
1852         /* Backlight should have its own device or go via
1853          * the fbdev
1854          */
1855         case PMU_IOC_GET_BACKLIGHT:
1856                 error = get_backlight_level();
1857                 if (error < 0)
1858                         return error;
1859                 return put_user(error, (__u32 *)arg);
1860         case PMU_IOC_SET_BACKLIGHT:
1861         {
1862                 __u32 value;
1863                 error = get_user(value, (__u32 *)arg);
1864                 if (!error)
1865                         error = set_backlight_level(value);
1866                 return error;
1867         }
1868 #endif /* CONFIG_PMAC_BACKLIGHT */
1869         case PMU_IOC_GET_MODEL:
1870                 return put_user(pmu_kind, (__u32 *)arg);
1871         case PMU_IOC_HAS_ADB:
1872                 return put_user(pmu_has_adb, (__u32 *)arg);
1873         }
1874         return -EINVAL;
1875 }
1876 
1877 static struct file_operations pmu_device_fops = {
1878         read:           pmu_read,
1879         write:          pmu_write,
1880         poll:           pmu_fpoll,
1881         ioctl:          pmu_ioctl,
1882         open:           pmu_open,
1883         release:        pmu_release,
1884 };
1885 
1886 static struct miscdevice pmu_device = {
1887         PMU_MINOR, "pmu", &pmu_device_fops
1888 };
1889 
1890 void pmu_device_init(void)
1891 {
1892         if (via)
1893                 misc_register(&pmu_device);
1894 }
1895 #endif /* CONFIG_PMAC_PBOOK */
1896 
1897 #if 0
1898 static inline void polled_handshake(volatile unsigned char *via)
1899 {
1900         via[B] &= ~TREQ; eieio();
1901         while ((via[B] & TACK) != 0)
1902                 ;
1903         via[B] |= TREQ; eieio();
1904         while ((via[B] & TACK) == 0)
1905                 ;
1906 }
1907 
1908 static inline void polled_send_byte(volatile unsigned char *via, int x)
1909 {
1910         via[ACR] |= SR_OUT | SR_EXT; eieio();
1911         via[SR] = x; eieio();
1912         polled_handshake(via);
1913 }
1914 
1915 static inline int polled_recv_byte(volatile unsigned char *via)
1916 {
1917         int x;
1918 
1919         via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
1920         x = via[SR]; eieio();
1921         polled_handshake(via);
1922         x = via[SR]; eieio();
1923         return x;
1924 }
1925 
1926 int
1927 pmu_polled_request(struct adb_request *req)
1928 {
1929         unsigned long flags;
1930         int i, l, c;
1931         volatile unsigned char *v = via;
1932 
1933         req->complete = 1;
1934         c = req->data[0];
1935         l = pmu_data_len[c][0];
1936         if (l >= 0 && req->nbytes != l + 1)
1937                 return -EINVAL;
1938 
1939         save_flags(flags); cli();
1940         while (pmu_state != idle)
1941                 pmu_poll();
1942 
1943         polled_send_byte(v, c);
1944         if (l < 0) {
1945                 l = req->nbytes - 1;
1946                 polled_send_byte(v, l);
1947         }
1948         for (i = 1; i <= l; ++i)
1949                 polled_send_byte(v, req->data[i]);
1950 
1951         l = pmu_data_len[c][1];
1952         if (l < 0)
1953                 l = polled_recv_byte(v);
1954         for (i = 0; i < l; ++i)
1955                 req->reply[i + req->reply_len] = polled_recv_byte(v);
1956 
1957         if (req->done)
1958                 (*req->done)(req);
1959 
1960         restore_flags(flags);
1961         return 0;
1962 }
1963 #endif /* 0 */
1964 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.