vserver 1.9.5.x5
[linux-2.6.git] / drivers / macintosh / via-pmu.c
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  * Copyright (C) 2001-2002 Benjamin Herrenschmidt
13  *
14  * THIS DRIVER IS BECOMING A TOTAL MESS !
15  *  - Cleanup atomically disabling reply to PMU events after
16  *    a sleep or a freq. switch
17  *  - Move sleep code out of here to pmac_pm, merge into new
18  *    common PM infrastructure
19  *  - Move backlight code out as well
20  *  - Save/Restore PCI space properly
21  *
22  */
23 #include <stdarg.h>
24 #include <linux/config.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/miscdevice.h>
31 #include <linux/blkdev.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/adb.h>
36 #include <linux/pmu.h>
37 #include <linux/cuda.h>
38 #include <linux/smp_lock.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/pm.h>
42 #include <linux/proc_fs.h>
43 #include <linux/init.h>
44 #include <linux/interrupt.h>
45 #include <linux/device.h>
46 #include <linux/suspend.h>
47 #include <linux/syscalls.h>
48 #include <linux/cpu.h>
49 #include <asm/prom.h>
50 #include <asm/machdep.h>
51 #include <asm/io.h>
52 #include <asm/pgtable.h>
53 #include <asm/system.h>
54 #include <asm/sections.h>
55 #include <asm/irq.h>
56 #include <asm/pmac_feature.h>
57 #include <asm/uaccess.h>
58 #include <asm/mmu_context.h>
59 #include <asm/cputable.h>
60 #include <asm/time.h>
61 #ifdef CONFIG_PMAC_BACKLIGHT
62 #include <asm/backlight.h>
63 #endif
64
65 /* Some compile options */
66 #undef SUSPEND_USES_PMU
67 #define DEBUG_SLEEP
68 #undef HACKED_PCI_SAVE
69
70 /* Misc minor number allocated for /dev/pmu */
71 #define PMU_MINOR               154
72
73 /* How many iterations between battery polls */
74 #define BATTERY_POLLING_COUNT   2
75
76 static volatile unsigned char __iomem *via;
77
78 /* VIA registers - spaced 0x200 bytes apart */
79 #define RS              0x200           /* skip between registers */
80 #define B               0               /* B-side data */
81 #define A               RS              /* A-side data */
82 #define DIRB            (2*RS)          /* B-side direction (1=output) */
83 #define DIRA            (3*RS)          /* A-side direction (1=output) */
84 #define T1CL            (4*RS)          /* Timer 1 ctr/latch (low 8 bits) */
85 #define T1CH            (5*RS)          /* Timer 1 counter (high 8 bits) */
86 #define T1LL            (6*RS)          /* Timer 1 latch (low 8 bits) */
87 #define T1LH            (7*RS)          /* Timer 1 latch (high 8 bits) */
88 #define T2CL            (8*RS)          /* Timer 2 ctr/latch (low 8 bits) */
89 #define T2CH            (9*RS)          /* Timer 2 counter (high 8 bits) */
90 #define SR              (10*RS)         /* Shift register */
91 #define ACR             (11*RS)         /* Auxiliary control register */
92 #define PCR             (12*RS)         /* Peripheral control register */
93 #define IFR             (13*RS)         /* Interrupt flag register */
94 #define IER             (14*RS)         /* Interrupt enable register */
95 #define ANH             (15*RS)         /* A-side data, no handshake */
96
97 /* Bits in B data register: both active low */
98 #define TACK            0x08            /* Transfer acknowledge (input) */
99 #define TREQ            0x10            /* Transfer request (output) */
100
101 /* Bits in ACR */
102 #define SR_CTRL         0x1c            /* Shift register control bits */
103 #define SR_EXT          0x0c            /* Shift on external clock */
104 #define SR_OUT          0x10            /* Shift out if 1 */
105
106 /* Bits in IFR and IER */
107 #define IER_SET         0x80            /* set bits in IER */
108 #define IER_CLR         0               /* clear bits in IER */
109 #define SR_INT          0x04            /* Shift register full/empty */
110 #define CB2_INT         0x08
111 #define CB1_INT         0x10            /* transition on CB1 input */
112
113 static volatile enum pmu_state {
114         idle,
115         sending,
116         intack,
117         reading,
118         reading_intr,
119         locked,
120 } pmu_state;
121
122 static volatile enum int_data_state {
123         int_data_empty,
124         int_data_fill,
125         int_data_ready,
126         int_data_flush
127 } int_data_state[2] = { int_data_empty, int_data_empty };
128
129 static struct adb_request *current_req;
130 static struct adb_request *last_req;
131 static struct adb_request *req_awaiting_reply;
132 static unsigned char interrupt_data[2][32];
133 static int interrupt_data_len[2];
134 static int int_data_last;
135 static unsigned char *reply_ptr;
136 static int data_index;
137 static int data_len;
138 static volatile int adb_int_pending;
139 static volatile int disable_poll;
140 static struct adb_request bright_req_1, bright_req_2;
141 static struct device_node *vias;
142 static int pmu_kind = PMU_UNKNOWN;
143 static int pmu_fully_inited = 0;
144 static int pmu_has_adb;
145 static unsigned char __iomem *gpio_reg = NULL;
146 static int gpio_irq = -1;
147 static int gpio_irq_enabled = -1;
148 static volatile int pmu_suspended = 0;
149 static spinlock_t pmu_lock;
150 static u8 pmu_intr_mask;
151 static int pmu_version;
152 static int drop_interrupts;
153 #ifdef CONFIG_PMAC_PBOOK
154 static int option_lid_wakeup = 1;
155 static int sleep_in_progress;
156 #endif /* CONFIG_PMAC_PBOOK */
157 static unsigned long async_req_locks;
158 static unsigned int pmu_irq_stats[11];
159
160 static struct proc_dir_entry *proc_pmu_root;
161 static struct proc_dir_entry *proc_pmu_info;
162 static struct proc_dir_entry *proc_pmu_irqstats;
163 static struct proc_dir_entry *proc_pmu_options;
164 static int option_server_mode;
165
166 #ifdef CONFIG_PMAC_PBOOK
167 int pmu_battery_count;
168 int pmu_cur_battery;
169 unsigned int pmu_power_flags;
170 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
171 static int query_batt_timer = BATTERY_POLLING_COUNT;
172 static struct adb_request batt_req;
173 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
174 #endif /* CONFIG_PMAC_PBOOK */
175
176 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
177 extern int disable_kernel_backlight;
178 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
179
180 int __fake_sleep;
181 int asleep;
182 struct notifier_block *sleep_notifier_list;
183
184 #ifdef CONFIG_ADB
185 static int adb_dev_map = 0;
186 static int pmu_adb_flags;
187
188 static int pmu_probe(void);
189 static int pmu_init(void);
190 static int pmu_send_request(struct adb_request *req, int sync);
191 static int pmu_adb_autopoll(int devs);
192 static int pmu_adb_reset_bus(void);
193 #endif /* CONFIG_ADB */
194
195 static int init_pmu(void);
196 static int pmu_queue_request(struct adb_request *req);
197 static void pmu_start(void);
198 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
199 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
200 static int proc_get_info(char *page, char **start, off_t off,
201                           int count, int *eof, void *data);
202 static int proc_get_irqstats(char *page, char **start, off_t off,
203                           int count, int *eof, void *data);
204 #ifdef CONFIG_PMAC_BACKLIGHT
205 static int pmu_set_backlight_level(int level, void* data);
206 static int pmu_set_backlight_enable(int on, int level, void* data);
207 #endif /* CONFIG_PMAC_BACKLIGHT */
208 #ifdef CONFIG_PMAC_PBOOK
209 static void pmu_pass_intr(unsigned char *data, int len);
210 static int proc_get_batt(char *page, char **start, off_t off,
211                         int count, int *eof, void *data);
212 #endif /* CONFIG_PMAC_PBOOK */
213 static int proc_read_options(char *page, char **start, off_t off,
214                         int count, int *eof, void *data);
215 static int proc_write_options(struct file *file, const char __user *buffer,
216                         unsigned long count, void *data);
217
218 #ifdef CONFIG_ADB
219 struct adb_driver via_pmu_driver = {
220         "PMU",
221         pmu_probe,
222         pmu_init,
223         pmu_send_request,
224         pmu_adb_autopoll,
225         pmu_poll_adb,
226         pmu_adb_reset_bus
227 };
228 #endif /* CONFIG_ADB */
229
230 extern void low_sleep_handler(void);
231 extern void enable_kernel_altivec(void);
232 extern void enable_kernel_fp(void);
233
234 #ifdef DEBUG_SLEEP
235 int pmu_polled_request(struct adb_request *req);
236 int pmu_wink(struct adb_request *req);
237 #endif
238
239 /*
240  * This table indicates for each PMU opcode:
241  * - the number of data bytes to be sent with the command, or -1
242  *   if a length byte should be sent,
243  * - the number of response bytes which the PMU will return, or
244  *   -1 if it will send a length byte.
245  */
246 static const s8 pmu_data_len[256][2] __openfirmwaredata = {
247 /*         0       1       2       3       4       5       6       7  */
248 /*00*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
249 /*08*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
250 /*10*/  { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
251 /*18*/  { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
252 /*20*/  {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
253 /*28*/  { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
254 /*30*/  { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
255 /*38*/  { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
256 /*40*/  { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
257 /*48*/  { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
258 /*50*/  { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
259 /*58*/  { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
260 /*60*/  { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
261 /*68*/  { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
262 /*70*/  { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
263 /*78*/  { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
264 /*80*/  { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
265 /*88*/  { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
266 /*90*/  { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
267 /*98*/  { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
268 /*a0*/  { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
269 /*a8*/  { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
270 /*b0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
271 /*b8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
272 /*c0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
273 /*c8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
274 /*d0*/  { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
275 /*d8*/  { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
276 /*e0*/  {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
277 /*e8*/  { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
278 /*f0*/  {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
279 /*f8*/  {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
280 };
281
282 static char *pbook_type[] = {
283         "Unknown PowerBook",
284         "PowerBook 2400/3400/3500(G3)",
285         "PowerBook G3 Series",
286         "1999 PowerBook G3",
287         "Core99"
288 };
289
290 #ifdef CONFIG_PMAC_BACKLIGHT
291 static struct backlight_controller pmu_backlight_controller = {
292         pmu_set_backlight_enable,
293         pmu_set_backlight_level
294 };
295 #endif /* CONFIG_PMAC_BACKLIGHT */
296
297 int __openfirmware
298 find_via_pmu(void)
299 {
300         if (via != 0)
301                 return 1;
302         vias = find_devices("via-pmu");
303         if (vias == 0)
304                 return 0;
305         if (vias->next != 0)
306                 printk(KERN_WARNING "Warning: only using 1st via-pmu\n");
307
308         if (vias->n_addrs < 1 || vias->n_intrs < 1) {
309                 printk(KERN_ERR "via-pmu: %d addresses, %d interrupts!\n",
310                        vias->n_addrs, vias->n_intrs);
311                 if (vias->n_addrs < 1 || vias->n_intrs < 1)
312                         return 0;
313         }
314
315         spin_lock_init(&pmu_lock);
316
317         pmu_has_adb = 1;
318
319         pmu_intr_mask = PMU_INT_PCEJECT |
320                         PMU_INT_SNDBRT |
321                         PMU_INT_ADB |
322                         PMU_INT_TICK;
323         
324         if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
325             || device_is_compatible(vias->parent, "ohare")))
326                 pmu_kind = PMU_OHARE_BASED;
327         else if (device_is_compatible(vias->parent, "paddington"))
328                 pmu_kind = PMU_PADDINGTON_BASED;
329         else if (device_is_compatible(vias->parent, "heathrow"))
330                 pmu_kind = PMU_HEATHROW_BASED;
331         else if (device_is_compatible(vias->parent, "Keylargo")
332                  || device_is_compatible(vias->parent, "K2-Keylargo")) {
333                 struct device_node *gpio, *gpiop;
334
335                 pmu_kind = PMU_KEYLARGO_BASED;
336                 pmu_has_adb = (find_type_devices("adb") != NULL);
337                 pmu_intr_mask = PMU_INT_PCEJECT |
338                                 PMU_INT_SNDBRT |
339                                 PMU_INT_ADB |
340                                 PMU_INT_TICK |
341                                 PMU_INT_ENVIRONMENT;
342                 
343                 gpiop = find_devices("gpio");
344                 if (gpiop && gpiop->n_addrs) {
345                         gpio_reg = ioremap(gpiop->addrs->address, 0x10);
346                         gpio = find_devices("extint-gpio1");
347                         if (gpio == NULL)
348                                 gpio = find_devices("pmu-interrupt");
349                         if (gpio && gpio->parent == gpiop && gpio->n_intrs)
350                                 gpio_irq = gpio->intrs[0].line;
351                 }
352         } else
353                 pmu_kind = PMU_UNKNOWN;
354
355         via = ioremap(vias->addrs->address, 0x2000);
356         
357         out_8(&via[IER], IER_CLR | 0x7f);       /* disable all intrs */
358         out_8(&via[IFR], 0x7f);                 /* clear IFR */
359
360         pmu_state = idle;
361
362         if (!init_pmu()) {
363                 via = NULL;
364                 return 0;
365         }
366
367         printk(KERN_INFO "PMU driver %d initialized for %s, firmware: %02x\n",
368                PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
369                
370 #ifndef CONFIG_PPC64
371         sys_ctrler = SYS_CTRLER_PMU;
372 #endif
373         
374         return 1;
375 }
376
377 #ifdef CONFIG_ADB
378 static int __openfirmware
379 pmu_probe(void)
380 {
381         return vias == NULL? -ENODEV: 0;
382 }
383
384 static int __init
385 pmu_init(void)
386 {
387         if (vias == NULL)
388                 return -ENODEV;
389         return 0;
390 }
391 #endif /* CONFIG_ADB */
392
393 /*
394  * We can't wait until pmu_init gets called, that happens too late.
395  * It happens after IDE and SCSI initialization, which can take a few
396  * seconds, and by that time the PMU could have given up on us and
397  * turned us off.
398  * Thus this is called with arch_initcall rather than device_initcall.
399  */
400 static int __init via_pmu_start(void)
401 {
402         if (vias == NULL)
403                 return -ENODEV;
404
405         bright_req_1.complete = 1;
406         bright_req_2.complete = 1;
407 #ifdef CONFIG_PMAC_PBOOK
408         batt_req.complete = 1;
409 #endif
410
411         if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
412                         (void *)0)) {
413                 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
414                        vias->intrs[0].line);
415                 return -EAGAIN;
416         }
417
418         if (pmu_kind == PMU_KEYLARGO_BASED && gpio_irq != -1) {
419                 if (request_irq(gpio_irq, gpio1_interrupt, 0, "GPIO1 ADB", (void *)0))
420                         printk(KERN_ERR "pmu: can't get irq %d (GPIO1)\n", gpio_irq);
421                 gpio_irq_enabled = 1;
422         }
423
424         /* Enable interrupts */
425         out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
426
427         pmu_fully_inited = 1;
428
429         /* Make sure PMU settle down before continuing. This is _very_ important
430          * since the IDE probe may shut interrupts down for quite a bit of time. If
431          * a PMU communication is pending while this happens, the PMU may timeout
432          * Not that on Core99 machines, the PMU keeps sending us environement
433          * messages, we should find a way to either fix IDE or make it call
434          * pmu_suspend() before masking interrupts. This can also happens while
435          * scolling with some fbdevs.
436          */
437         do {
438                 pmu_poll();
439         } while (pmu_state != idle);
440
441         return 0;
442 }
443
444 arch_initcall(via_pmu_start);
445
446 /*
447  * This has to be done after pci_init, which is a subsys_initcall.
448  */
449 static int __init via_pmu_dev_init(void)
450 {
451         if (vias == NULL)
452                 return -ENODEV;
453
454 #ifndef CONFIG_PPC64
455         request_OF_resource(vias, 0, NULL);
456 #endif
457 #ifdef CONFIG_PMAC_BACKLIGHT
458         /* Enable backlight */
459         register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
460 #endif /* CONFIG_PMAC_BACKLIGHT */
461
462 #ifdef CONFIG_PMAC_PBOOK
463         if (machine_is_compatible("AAPL,3400/2400") ||
464                 machine_is_compatible("AAPL,3500")) {
465                 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
466                         NULL, PMAC_MB_INFO_MODEL, 0);
467                 pmu_battery_count = 1;
468                 if (mb == PMAC_TYPE_COMET)
469                         pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
470                 else
471                         pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
472         } else if (machine_is_compatible("AAPL,PowerBook1998") ||
473                 machine_is_compatible("PowerBook1,1")) {
474                 pmu_battery_count = 2;
475                 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
476                 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
477         } else {
478                 struct device_node* prim = find_devices("power-mgt");
479                 u32 *prim_info = NULL;
480                 if (prim)
481                         prim_info = (u32 *)get_property(prim, "prim-info", NULL);
482                 if (prim_info) {
483                         /* Other stuffs here yet unknown */
484                         pmu_battery_count = (prim_info[6] >> 16) & 0xff;
485                         pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
486                         if (pmu_battery_count > 1)
487                                 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
488                 }
489         }
490 #endif /* CONFIG_PMAC_PBOOK */
491         /* Create /proc/pmu */
492         proc_pmu_root = proc_mkdir("pmu", NULL);
493         if (proc_pmu_root) {
494 #ifdef CONFIG_PMAC_PBOOK
495                 int i;
496
497                 for (i=0; i<pmu_battery_count; i++) {
498                         char title[16];
499                         sprintf(title, "battery_%d", i);
500                         proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
501                                                 proc_get_batt, (void *)i);
502                 }
503 #endif /* CONFIG_PMAC_PBOOK */
504
505                 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
506                                         proc_get_info, NULL);
507                 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
508                                         proc_get_irqstats, NULL);
509                 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
510                 if (proc_pmu_options) {
511                         proc_pmu_options->nlink = 1;
512                         proc_pmu_options->read_proc = proc_read_options;
513                         proc_pmu_options->write_proc = proc_write_options;
514                 }
515         }
516         return 0;
517 }
518
519 device_initcall(via_pmu_dev_init);
520
521 static int __openfirmware
522 init_pmu(void)
523 {
524         int timeout;
525         struct adb_request req;
526
527         out_8(&via[B], via[B] | TREQ);                  /* negate TREQ */
528         out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK);  /* TACK in, TREQ out */
529
530         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
531         timeout =  100000;
532         while (!req.complete) {
533                 if (--timeout < 0) {
534                         printk(KERN_ERR "init_pmu: no response from PMU\n");
535                         return 0;
536                 }
537                 udelay(10);
538                 pmu_poll();
539         }
540
541         /* ack all pending interrupts */
542         timeout = 100000;
543         interrupt_data[0][0] = 1;
544         while (interrupt_data[0][0] || pmu_state != idle) {
545                 if (--timeout < 0) {
546                         printk(KERN_ERR "init_pmu: timed out acking intrs\n");
547                         return 0;
548                 }
549                 if (pmu_state == idle)
550                         adb_int_pending = 1;
551                 via_pmu_interrupt(0, NULL, NULL);
552                 udelay(10);
553         }
554
555         /* Tell PMU we are ready.  */
556         if (pmu_kind == PMU_KEYLARGO_BASED) {
557                 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
558                 while (!req.complete)
559                         pmu_poll();
560         }
561
562         /* Read PMU version */
563         pmu_request(&req, NULL, 1, PMU_GET_VERSION);
564         pmu_wait_complete(&req);
565         if (req.reply_len > 0)
566                 pmu_version = req.reply[0];
567         
568         /* Read server mode setting */
569         if (pmu_kind == PMU_KEYLARGO_BASED) {
570                 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
571                             PMU_PWR_GET_POWERUP_EVENTS);
572                 pmu_wait_complete(&req);
573                 if (req.reply_len == 2) {
574                         if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
575                                 option_server_mode = 1;
576                         printk(KERN_INFO "via-pmu: Server Mode is %s\n",
577                                option_server_mode ? "enabled" : "disabled");
578                 }
579         }
580         return 1;
581 }
582
583 int
584 pmu_get_model(void)
585 {
586         return pmu_kind;
587 }
588
589 #ifndef CONFIG_PPC64
590 static inline void wakeup_decrementer(void)
591 {
592         set_dec(tb_ticks_per_jiffy);
593         /* No currently-supported powerbook has a 601,
594          * so use get_tbl, not native
595          */
596         last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
597 }
598 #endif
599
600 static void pmu_set_server_mode(int server_mode)
601 {
602         struct adb_request req;
603
604         if (pmu_kind != PMU_KEYLARGO_BASED)
605                 return;
606
607         option_server_mode = server_mode;
608         pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
609         pmu_wait_complete(&req);
610         if (req.reply_len < 2)
611                 return;
612         if (server_mode)
613                 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
614                             PMU_PWR_SET_POWERUP_EVENTS,
615                             req.reply[0], PMU_PWR_WAKEUP_AC_INSERT); 
616         else
617                 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
618                             PMU_PWR_CLR_POWERUP_EVENTS,
619                             req.reply[0], PMU_PWR_WAKEUP_AC_INSERT); 
620         pmu_wait_complete(&req);
621 }
622
623 #ifdef CONFIG_PMAC_PBOOK
624
625 /* This new version of the code for 2400/3400/3500 powerbooks
626  * is inspired from the implementation in gkrellm-pmu
627  */
628 static void __pmac
629 done_battery_state_ohare(struct adb_request* req)
630 {
631         /* format:
632          *  [0]    :  flags
633          *    0x01 :  AC indicator
634          *    0x02 :  charging
635          *    0x04 :  battery exist
636          *    0x08 :  
637          *    0x10 :  
638          *    0x20 :  full charged
639          *    0x40 :  pcharge reset
640          *    0x80 :  battery exist
641          *
642          *  [1][2] :  battery voltage
643          *  [3]    :  CPU temperature
644          *  [4]    :  battery temperature
645          *  [5]    :  current
646          *  [6][7] :  pcharge
647          *              --tkoba
648          */
649         unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
650         long pcharge, charge, vb, vmax, lmax;
651         long vmax_charging, vmax_charged;
652         long amperage, voltage, time, max;
653         int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
654                         NULL, PMAC_MB_INFO_MODEL, 0);
655
656         if (req->reply[0] & 0x01)
657                 pmu_power_flags |= PMU_PWR_AC_PRESENT;
658         else
659                 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
660         
661         if (mb == PMAC_TYPE_COMET) {
662                 vmax_charged = 189;
663                 vmax_charging = 213;
664                 lmax = 6500;
665         } else {
666                 vmax_charged = 330;
667                 vmax_charging = 330;
668                 lmax = 6500;
669         }
670         vmax = vmax_charged;
671
672         /* If battery installed */
673         if (req->reply[0] & 0x04) {
674                 bat_flags |= PMU_BATT_PRESENT;
675                 if (req->reply[0] & 0x02)
676                         bat_flags |= PMU_BATT_CHARGING;
677                 vb = (req->reply[1] << 8) | req->reply[2];
678                 voltage = (vb * 265 + 72665) / 10;
679                 amperage = req->reply[5];
680                 if ((req->reply[0] & 0x01) == 0) {
681                         if (amperage > 200)
682                                 vb += ((amperage - 200) * 15)/100;
683                 } else if (req->reply[0] & 0x02) {
684                         vb = (vb * 97) / 100;
685                         vmax = vmax_charging;
686                 }
687                 charge = (100 * vb) / vmax;
688                 if (req->reply[0] & 0x40) {
689                         pcharge = (req->reply[6] << 8) + req->reply[7];
690                         if (pcharge > lmax)
691                                 pcharge = lmax;
692                         pcharge *= 100;
693                         pcharge = 100 - pcharge / lmax;
694                         if (pcharge < charge)
695                                 charge = pcharge;
696                 }
697                 if (amperage > 0)
698                         time = (charge * 16440) / amperage;
699                 else
700                         time = 0;
701                 max = 100;
702                 amperage = -amperage;
703         } else
704                 charge = max = amperage = voltage = time = 0;
705
706         pmu_batteries[pmu_cur_battery].flags = bat_flags;
707         pmu_batteries[pmu_cur_battery].charge = charge;
708         pmu_batteries[pmu_cur_battery].max_charge = max;
709         pmu_batteries[pmu_cur_battery].amperage = amperage;
710         pmu_batteries[pmu_cur_battery].voltage = voltage;
711         pmu_batteries[pmu_cur_battery].time_remaining = time;
712
713         clear_bit(0, &async_req_locks);
714 }
715
716 static void __pmac
717 done_battery_state_smart(struct adb_request* req)
718 {
719         /* format:
720          *  [0] : format of this structure (known: 3,4,5)
721          *  [1] : flags
722          *  
723          *  format 3 & 4:
724          *  
725          *  [2] : charge
726          *  [3] : max charge
727          *  [4] : current
728          *  [5] : voltage
729          *  
730          *  format 5:
731          *  
732          *  [2][3] : charge
733          *  [4][5] : max charge
734          *  [6][7] : current
735          *  [8][9] : voltage
736          */
737          
738         unsigned int bat_flags = PMU_BATT_TYPE_SMART;
739         int amperage;
740         unsigned int capa, max, voltage;
741         
742         if (req->reply[1] & 0x01)
743                 pmu_power_flags |= PMU_PWR_AC_PRESENT;
744         else
745                 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
746
747
748         capa = max = amperage = voltage = 0;
749         
750         if (req->reply[1] & 0x04) {
751                 bat_flags |= PMU_BATT_PRESENT;
752                 switch(req->reply[0]) {
753                         case 3:
754                         case 4: capa = req->reply[2];
755                                 max = req->reply[3];
756                                 amperage = *((signed char *)&req->reply[4]);
757                                 voltage = req->reply[5];
758                                 break;
759                         case 5: capa = (req->reply[2] << 8) | req->reply[3];
760                                 max = (req->reply[4] << 8) | req->reply[5];
761                                 amperage = *((signed short *)&req->reply[6]);
762                                 voltage = (req->reply[8] << 8) | req->reply[9];
763                                 break;
764                         default:
765                                 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
766                                         req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
767                                 break;
768                 }
769         }
770
771         if ((req->reply[1] & 0x01) && (amperage > 0))
772                 bat_flags |= PMU_BATT_CHARGING;
773
774         pmu_batteries[pmu_cur_battery].flags = bat_flags;
775         pmu_batteries[pmu_cur_battery].charge = capa;
776         pmu_batteries[pmu_cur_battery].max_charge = max;
777         pmu_batteries[pmu_cur_battery].amperage = amperage;
778         pmu_batteries[pmu_cur_battery].voltage = voltage;
779         if (amperage) {
780                 if ((req->reply[1] & 0x01) && (amperage > 0))
781                         pmu_batteries[pmu_cur_battery].time_remaining
782                                 = ((max-capa) * 3600) / amperage;
783                 else
784                         pmu_batteries[pmu_cur_battery].time_remaining
785                                 = (capa * 3600) / (-amperage);
786         } else
787                 pmu_batteries[pmu_cur_battery].time_remaining = 0;
788
789         pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
790
791         clear_bit(0, &async_req_locks);
792 }
793
794 static void __pmac
795 query_battery_state(void)
796 {
797         if (test_and_set_bit(0, &async_req_locks))
798                 return;
799         if (pmu_kind == PMU_OHARE_BASED)
800                 pmu_request(&batt_req, done_battery_state_ohare,
801                         1, PMU_BATTERY_STATE);
802         else
803                 pmu_request(&batt_req, done_battery_state_smart,
804                         2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
805 }
806
807 #endif /* CONFIG_PMAC_PBOOK */
808
809 static int __pmac
810 proc_get_info(char *page, char **start, off_t off,
811                 int count, int *eof, void *data)
812 {
813         char* p = page;
814
815         p += sprintf(p, "PMU driver version     : %d\n", PMU_DRIVER_VERSION);
816         p += sprintf(p, "PMU firmware version   : %02x\n", pmu_version);
817 #ifdef CONFIG_PMAC_PBOOK
818         p += sprintf(p, "AC Power               : %d\n",
819                 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0));
820         p += sprintf(p, "Battery count          : %d\n", pmu_battery_count);
821 #endif /* CONFIG_PMAC_PBOOK */
822
823         return p - page;
824 }
825
826 static int __pmac
827 proc_get_irqstats(char *page, char **start, off_t off,
828                   int count, int *eof, void *data)
829 {
830         int i;
831         char* p = page;
832         static const char *irq_names[] = {
833                 "Total CB1 triggered events",
834                 "Total GPIO1 triggered events",
835                 "PC-Card eject button",
836                 "Sound/Brightness button",
837                 "ADB message",
838                 "Battery state change",
839                 "Environment interrupt",
840                 "Tick timer",
841                 "Ghost interrupt (zero len)",
842                 "Empty interrupt (empty mask)",
843                 "Max irqs in a row"
844         };
845
846         for (i=0; i<11; i++) {
847                 p += sprintf(p, " %2u: %10u (%s)\n",
848                              i, pmu_irq_stats[i], irq_names[i]);
849         }
850         return p - page;
851 }
852
853 #ifdef CONFIG_PMAC_PBOOK
854 static int __pmac
855 proc_get_batt(char *page, char **start, off_t off,
856                 int count, int *eof, void *data)
857 {
858         int batnum = (int)data;
859         char *p = page;
860         
861         p += sprintf(p, "\n");
862         p += sprintf(p, "flags      : %08x\n",
863                 pmu_batteries[batnum].flags);
864         p += sprintf(p, "charge     : %d\n",
865                 pmu_batteries[batnum].charge);
866         p += sprintf(p, "max_charge : %d\n",
867                 pmu_batteries[batnum].max_charge);
868         p += sprintf(p, "current    : %d\n",
869                 pmu_batteries[batnum].amperage);
870         p += sprintf(p, "voltage    : %d\n",
871                 pmu_batteries[batnum].voltage);
872         p += sprintf(p, "time rem.  : %d\n",
873                 pmu_batteries[batnum].time_remaining);
874
875         return p - page;
876 }
877 #endif /* CONFIG_PMAC_PBOOK */
878
879 static int __pmac
880 proc_read_options(char *page, char **start, off_t off,
881                         int count, int *eof, void *data)
882 {
883         char *p = page;
884
885 #ifdef CONFIG_PMAC_PBOOK
886         if (pmu_kind == PMU_KEYLARGO_BASED &&
887             pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
888                 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
889 #endif /* CONFIG_PMAC_PBOOK */
890         if (pmu_kind == PMU_KEYLARGO_BASED)
891                 p += sprintf(p, "server_mode=%d\n", option_server_mode);
892
893         return p - page;
894 }
895                         
896 static int __pmac
897 proc_write_options(struct file *file, const char __user *buffer,
898                         unsigned long count, void *data)
899 {
900         char tmp[33];
901         char *label, *val;
902         unsigned long fcount = count;
903         
904         if (!count)
905                 return -EINVAL;
906         if (count > 32)
907                 count = 32;
908         if (copy_from_user(tmp, buffer, count))
909                 return -EFAULT;
910         tmp[count] = 0;
911
912         label = tmp;
913         while(*label == ' ')
914                 label++;
915         val = label;
916         while(*val && (*val != '=')) {
917                 if (*val == ' ')
918                         *val = 0;
919                 val++;
920         }
921         if ((*val) == 0)
922                 return -EINVAL;
923         *(val++) = 0;
924         while(*val == ' ')
925                 val++;
926 #ifdef CONFIG_PMAC_PBOOK
927         if (pmu_kind == PMU_KEYLARGO_BASED &&
928             pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
929                 if (!strcmp(label, "lid_wakeup"))
930                         option_lid_wakeup = ((*val) == '1');
931 #endif /* CONFIG_PMAC_PBOOK */
932         if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
933                 int new_value;
934                 new_value = ((*val) == '1');
935                 if (new_value != option_server_mode)
936                         pmu_set_server_mode(new_value);
937         }
938         return fcount;
939 }
940
941 #ifdef CONFIG_ADB
942 /* Send an ADB command */
943 static int __pmac
944 pmu_send_request(struct adb_request *req, int sync)
945 {
946         int i, ret;
947
948         if ((vias == NULL) || (!pmu_fully_inited)) {
949                 req->complete = 1;
950                 return -ENXIO;
951         }
952
953         ret = -EINVAL;
954
955         switch (req->data[0]) {
956         case PMU_PACKET:
957                 for (i = 0; i < req->nbytes - 1; ++i)
958                         req->data[i] = req->data[i+1];
959                 --req->nbytes;
960                 if (pmu_data_len[req->data[0]][1] != 0) {
961                         req->reply[0] = ADB_RET_OK;
962                         req->reply_len = 1;
963                 } else
964                         req->reply_len = 0;
965                 ret = pmu_queue_request(req);
966                 break;
967         case CUDA_PACKET:
968                 switch (req->data[1]) {
969                 case CUDA_GET_TIME:
970                         if (req->nbytes != 2)
971                                 break;
972                         req->data[0] = PMU_READ_RTC;
973                         req->nbytes = 1;
974                         req->reply_len = 3;
975                         req->reply[0] = CUDA_PACKET;
976                         req->reply[1] = 0;
977                         req->reply[2] = CUDA_GET_TIME;
978                         ret = pmu_queue_request(req);
979                         break;
980                 case CUDA_SET_TIME:
981                         if (req->nbytes != 6)
982                                 break;
983                         req->data[0] = PMU_SET_RTC;
984                         req->nbytes = 5;
985                         for (i = 1; i <= 4; ++i)
986                                 req->data[i] = req->data[i+1];
987                         req->reply_len = 3;
988                         req->reply[0] = CUDA_PACKET;
989                         req->reply[1] = 0;
990                         req->reply[2] = CUDA_SET_TIME;
991                         ret = pmu_queue_request(req);
992                         break;
993                 }
994                 break;
995         case ADB_PACKET:
996                 if (!pmu_has_adb)
997                         return -ENXIO;
998                 for (i = req->nbytes - 1; i > 1; --i)
999                         req->data[i+2] = req->data[i];
1000                 req->data[3] = req->nbytes - 2;
1001                 req->data[2] = pmu_adb_flags;
1002                 /*req->data[1] = req->data[1];*/
1003                 req->data[0] = PMU_ADB_CMD;
1004                 req->nbytes += 2;
1005                 req->reply_expected = 1;
1006                 req->reply_len = 0;
1007                 ret = pmu_queue_request(req);
1008                 break;
1009         }
1010         if (ret) {
1011                 req->complete = 1;
1012                 return ret;
1013         }
1014
1015         if (sync)
1016                 while (!req->complete)
1017                         pmu_poll();
1018
1019         return 0;
1020 }
1021
1022 /* Enable/disable autopolling */
1023 static int __pmac
1024 pmu_adb_autopoll(int devs)
1025 {
1026         struct adb_request req;
1027
1028         if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1029                 return -ENXIO;
1030
1031         if (devs) {
1032                 adb_dev_map = devs;
1033                 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1034                             adb_dev_map >> 8, adb_dev_map);
1035                 pmu_adb_flags = 2;
1036         } else {
1037                 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1038                 pmu_adb_flags = 0;
1039         }
1040         while (!req.complete)
1041                 pmu_poll();
1042         return 0;
1043 }
1044
1045 /* Reset the ADB bus */
1046 static int __pmac
1047 pmu_adb_reset_bus(void)
1048 {
1049         struct adb_request req;
1050         int save_autopoll = adb_dev_map;
1051
1052         if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1053                 return -ENXIO;
1054
1055         /* anyone got a better idea?? */
1056         pmu_adb_autopoll(0);
1057
1058         req.nbytes = 5;
1059         req.done = NULL;
1060         req.data[0] = PMU_ADB_CMD;
1061         req.data[1] = 0;
1062         req.data[2] = ADB_BUSRESET;
1063         req.data[3] = 0;
1064         req.data[4] = 0;
1065         req.reply_len = 0;
1066         req.reply_expected = 1;
1067         if (pmu_queue_request(&req) != 0) {
1068                 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1069                 return -EIO;
1070         }
1071         pmu_wait_complete(&req);
1072
1073         if (save_autopoll != 0)
1074                 pmu_adb_autopoll(save_autopoll);
1075
1076         return 0;
1077 }
1078 #endif /* CONFIG_ADB */
1079
1080 /* Construct and send a pmu request */
1081 int __openfirmware
1082 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1083             int nbytes, ...)
1084 {
1085         va_list list;
1086         int i;
1087
1088         if (vias == NULL)
1089                 return -ENXIO;
1090
1091         if (nbytes < 0 || nbytes > 32) {
1092                 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1093                 req->complete = 1;
1094                 return -EINVAL;
1095         }
1096         req->nbytes = nbytes;
1097         req->done = done;
1098         va_start(list, nbytes);
1099         for (i = 0; i < nbytes; ++i)
1100                 req->data[i] = va_arg(list, int);
1101         va_end(list);
1102         req->reply_len = 0;
1103         req->reply_expected = 0;
1104         return pmu_queue_request(req);
1105 }
1106
1107 int __pmac
1108 pmu_queue_request(struct adb_request *req)
1109 {
1110         unsigned long flags;
1111         int nsend;
1112
1113         if (via == NULL) {
1114                 req->complete = 1;
1115                 return -ENXIO;
1116         }
1117         if (req->nbytes <= 0) {
1118                 req->complete = 1;
1119                 return 0;
1120         }
1121         nsend = pmu_data_len[req->data[0]][0];
1122         if (nsend >= 0 && req->nbytes != nsend + 1) {
1123                 req->complete = 1;
1124                 return -EINVAL;
1125         }
1126
1127         req->next = NULL;
1128         req->sent = 0;
1129         req->complete = 0;
1130
1131         spin_lock_irqsave(&pmu_lock, flags);
1132         if (current_req != 0) {
1133                 last_req->next = req;
1134                 last_req = req;
1135         } else {
1136                 current_req = req;
1137                 last_req = req;
1138                 if (pmu_state == idle)
1139                         pmu_start();
1140         }
1141         spin_unlock_irqrestore(&pmu_lock, flags);
1142
1143         return 0;
1144 }
1145
1146 static inline void
1147 wait_for_ack(void)
1148 {
1149         /* Sightly increased the delay, I had one occurrence of the message
1150          * reported
1151          */
1152         int timeout = 4000;
1153         while ((in_8(&via[B]) & TACK) == 0) {
1154                 if (--timeout < 0) {
1155                         printk(KERN_ERR "PMU not responding (!ack)\n");
1156                         return;
1157                 }
1158                 udelay(10);
1159         }
1160 }
1161
1162 /* New PMU seems to be very sensitive to those timings, so we make sure
1163  * PCI is flushed immediately */
1164 static inline void
1165 send_byte(int x)
1166 {
1167         volatile unsigned char __iomem *v = via;
1168
1169         out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1170         out_8(&v[SR], x);
1171         out_8(&v[B], in_8(&v[B]) & ~TREQ);              /* assert TREQ */
1172         (void)in_8(&v[B]);
1173 }
1174
1175 static inline void
1176 recv_byte(void)
1177 {
1178         volatile unsigned char __iomem *v = via;
1179
1180         out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1181         in_8(&v[SR]);           /* resets SR */
1182         out_8(&v[B], in_8(&v[B]) & ~TREQ);
1183         (void)in_8(&v[B]);
1184 }
1185
1186 static inline void
1187 pmu_done(struct adb_request *req)
1188 {
1189         void (*done)(struct adb_request *) = req->done;
1190         mb();
1191         req->complete = 1;
1192         /* Here, we assume that if the request has a done member, the
1193          * struct request will survive to setting req->complete to 1
1194          */
1195         if (done)
1196                 (*done)(req);
1197 }
1198
1199 static void __pmac
1200 pmu_start(void)
1201 {
1202         struct adb_request *req;
1203
1204         /* assert pmu_state == idle */
1205         /* get the packet to send */
1206         req = current_req;
1207         if (req == 0 || pmu_state != idle
1208             || (/*req->reply_expected && */req_awaiting_reply))
1209                 return;
1210
1211         pmu_state = sending;
1212         data_index = 1;
1213         data_len = pmu_data_len[req->data[0]][0];
1214
1215         /* Sounds safer to make sure ACK is high before writing. This helped
1216          * kill a problem with ADB and some iBooks
1217          */
1218         wait_for_ack();
1219         /* set the shift register to shift out and send a byte */
1220         send_byte(req->data[0]);
1221 }
1222
1223 void __openfirmware
1224 pmu_poll(void)
1225 {
1226         if (!via)
1227                 return;
1228         if (disable_poll)
1229                 return;
1230         via_pmu_interrupt(0, NULL, NULL);
1231 }
1232
1233 void __openfirmware
1234 pmu_poll_adb(void)
1235 {
1236         if (!via)
1237                 return;
1238         if (disable_poll)
1239                 return;
1240         /* Kicks ADB read when PMU is suspended */
1241         adb_int_pending = 1;
1242         do {
1243                 via_pmu_interrupt(0, NULL, NULL);
1244         } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1245                 || req_awaiting_reply));
1246 }
1247
1248 void __openfirmware
1249 pmu_wait_complete(struct adb_request *req)
1250 {
1251         if (!via)
1252                 return;
1253         while((pmu_state != idle && pmu_state != locked) || !req->complete)
1254                 via_pmu_interrupt(0, NULL, NULL);
1255 }
1256
1257 /* This function loops until the PMU is idle and prevents it from
1258  * anwsering to ADB interrupts. pmu_request can still be called.
1259  * This is done to avoid spurrious shutdowns when we know we'll have
1260  * interrupts switched off for a long time
1261  */
1262 void __openfirmware
1263 pmu_suspend(void)
1264 {
1265         unsigned long flags;
1266 #ifdef SUSPEND_USES_PMU
1267         struct adb_request *req;
1268 #endif
1269         if (!via)
1270                 return;
1271         
1272         spin_lock_irqsave(&pmu_lock, flags);
1273         pmu_suspended++;
1274         if (pmu_suspended > 1) {
1275                 spin_unlock_irqrestore(&pmu_lock, flags);
1276                 return;
1277         }
1278
1279         do {
1280                 spin_unlock_irqrestore(&pmu_lock, flags);
1281                 if (req_awaiting_reply)
1282                         adb_int_pending = 1;
1283                 via_pmu_interrupt(0, NULL, NULL);
1284                 spin_lock_irqsave(&pmu_lock, flags);
1285                 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1286 #ifdef SUSPEND_USES_PMU
1287                         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1288                         spin_unlock_irqrestore(&pmu_lock, flags);
1289                         while(!req.complete)
1290                                 pmu_poll();
1291 #else /* SUSPEND_USES_PMU */
1292                         if (gpio_irq >= 0)
1293                                 disable_irq_nosync(gpio_irq);
1294                         out_8(&via[IER], CB1_INT | IER_CLR);
1295                         spin_unlock_irqrestore(&pmu_lock, flags);
1296 #endif /* SUSPEND_USES_PMU */
1297                         break;
1298                 }
1299         } while (1);
1300 }
1301
1302 void __openfirmware
1303 pmu_resume(void)
1304 {
1305         unsigned long flags;
1306
1307         if (!via || (pmu_suspended < 1))
1308                 return;
1309
1310         spin_lock_irqsave(&pmu_lock, flags);
1311         pmu_suspended--;
1312         if (pmu_suspended > 0) {
1313                 spin_unlock_irqrestore(&pmu_lock, flags);
1314                 return;
1315         }
1316         adb_int_pending = 1;
1317 #ifdef SUSPEND_USES_PMU
1318         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1319         spin_unlock_irqrestore(&pmu_lock, flags);
1320         while(!req.complete)
1321                 pmu_poll();
1322 #else /* SUSPEND_USES_PMU */
1323         if (gpio_irq >= 0)
1324                 enable_irq(gpio_irq);
1325         out_8(&via[IER], CB1_INT | IER_SET);
1326         spin_unlock_irqrestore(&pmu_lock, flags);
1327         pmu_poll();
1328 #endif /* SUSPEND_USES_PMU */
1329 }
1330
1331 /* Interrupt data could be the result data from an ADB cmd */
1332 static void __pmac
1333 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1334 {
1335         unsigned char ints, pirq;
1336         int i = 0;
1337
1338         asleep = 0;
1339         if (drop_interrupts || len < 1) {
1340                 adb_int_pending = 0;
1341                 pmu_irq_stats[8]++;
1342                 return;
1343         }
1344
1345         /* Get PMU interrupt mask */
1346         ints = data[0];
1347
1348         /* Record zero interrupts for stats */
1349         if (ints == 0)
1350                 pmu_irq_stats[9]++;
1351
1352         /* Hack to deal with ADB autopoll flag */
1353         if (ints & PMU_INT_ADB)
1354                 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1355
1356 next:
1357
1358         if (ints == 0) {
1359                 if (i > pmu_irq_stats[10])
1360                         pmu_irq_stats[10] = i;
1361                 return;
1362         }
1363
1364         for (pirq = 0; pirq < 8; pirq++)
1365                 if (ints & (1 << pirq))
1366                         break;
1367         pmu_irq_stats[pirq]++;
1368         i++;
1369         ints &= ~(1 << pirq);
1370
1371         /* Note: for some reason, we get an interrupt with len=1,
1372          * data[0]==0 after each normal ADB interrupt, at least
1373          * on the Pismo. Still investigating...  --BenH
1374          */
1375         if ((1 << pirq) & PMU_INT_ADB) {
1376                 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1377                         struct adb_request *req = req_awaiting_reply;
1378                         if (req == 0) {
1379                                 printk(KERN_ERR "PMU: extra ADB reply\n");
1380                                 return;
1381                         }
1382                         req_awaiting_reply = NULL;
1383                         if (len <= 2)
1384                                 req->reply_len = 0;
1385                         else {
1386                                 memcpy(req->reply, data + 1, len - 1);
1387                                 req->reply_len = len - 1;
1388                         }
1389                         pmu_done(req);
1390                 } else {
1391 #if defined(CONFIG_XMON) && !defined(CONFIG_PPC64)
1392                         if (len == 4 && data[1] == 0x2c) {
1393                                 extern int xmon_wants_key, xmon_adb_keycode;
1394                                 if (xmon_wants_key) {
1395                                         xmon_adb_keycode = data[2];
1396                                         return;
1397                                 }
1398                         }
1399 #endif /* defined(CONFIG_XMON) && !defined(CONFIG_PPC64) */
1400 #ifdef CONFIG_ADB
1401                         /*
1402                          * XXX On the [23]400 the PMU gives us an up
1403                          * event for keycodes 0x74 or 0x75 when the PC
1404                          * card eject buttons are released, so we
1405                          * ignore those events.
1406                          */
1407                         if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1408                               && data[1] == 0x2c && data[3] == 0xff
1409                               && (data[2] & ~1) == 0xf4))
1410                                 adb_input(data+1, len-1, regs, 1);
1411 #endif /* CONFIG_ADB */         
1412                 }
1413         }
1414         /* Sound/brightness button pressed */
1415         else if ((1 << pirq) & PMU_INT_SNDBRT) {
1416 #ifdef CONFIG_PMAC_BACKLIGHT
1417                 if (len == 3)
1418 #ifdef CONFIG_INPUT_ADBHID
1419                         if (!disable_kernel_backlight)
1420 #endif /* CONFIG_INPUT_ADBHID */
1421                                 set_backlight_level(data[1] >> 4);
1422 #endif /* CONFIG_PMAC_BACKLIGHT */
1423         }
1424         /* Tick interrupt */
1425         else if ((1 << pirq) & PMU_INT_TICK) {
1426 #ifdef CONFIG_PMAC_PBOOK
1427                 /* Environement or tick interrupt, query batteries */
1428                 if (pmu_battery_count) {
1429                         if ((--query_batt_timer) == 0) {
1430                                 query_battery_state();
1431                                 query_batt_timer = BATTERY_POLLING_COUNT;
1432                         }
1433                 }
1434         }
1435         else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1436                 if (pmu_battery_count)
1437                         query_battery_state();
1438                 pmu_pass_intr(data, len);
1439         } else {
1440                pmu_pass_intr(data, len);
1441 #endif /* CONFIG_PMAC_PBOOK */
1442         }
1443         goto next;
1444 }
1445
1446 static struct adb_request* __pmac
1447 pmu_sr_intr(struct pt_regs *regs)
1448 {
1449         struct adb_request *req;
1450         int bite = 0;
1451
1452         if (via[B] & TREQ) {
1453                 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1454                 out_8(&via[IFR], SR_INT);
1455                 return NULL;
1456         }
1457         /* The ack may not yet be low when we get the interrupt */
1458         while ((in_8(&via[B]) & TACK) != 0)
1459                         ;
1460
1461         /* if reading grab the byte, and reset the interrupt */
1462         if (pmu_state == reading || pmu_state == reading_intr)
1463                 bite = in_8(&via[SR]);
1464
1465         /* reset TREQ and wait for TACK to go high */
1466         out_8(&via[B], in_8(&via[B]) | TREQ);
1467         wait_for_ack();
1468
1469         switch (pmu_state) {
1470         case sending:
1471                 req = current_req;
1472                 if (data_len < 0) {
1473                         data_len = req->nbytes - 1;
1474                         send_byte(data_len);
1475                         break;
1476                 }
1477                 if (data_index <= data_len) {
1478                         send_byte(req->data[data_index++]);
1479                         break;
1480                 }
1481                 req->sent = 1;
1482                 data_len = pmu_data_len[req->data[0]][1];
1483                 if (data_len == 0) {
1484                         pmu_state = idle;
1485                         current_req = req->next;
1486                         if (req->reply_expected)
1487                                 req_awaiting_reply = req;
1488                         else
1489                                 return req;
1490                 } else {
1491                         pmu_state = reading;
1492                         data_index = 0;
1493                         reply_ptr = req->reply + req->reply_len;
1494                         recv_byte();
1495                 }
1496                 break;
1497
1498         case intack:
1499                 data_index = 0;
1500                 data_len = -1;
1501                 pmu_state = reading_intr;
1502                 reply_ptr = interrupt_data[int_data_last];
1503                 recv_byte();
1504                 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1505                         enable_irq(gpio_irq);
1506                         gpio_irq_enabled = 1;
1507                 }
1508                 break;
1509
1510         case reading:
1511         case reading_intr:
1512                 if (data_len == -1) {
1513                         data_len = bite;
1514                         if (bite > 32)
1515                                 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1516                 } else if (data_index < 32) {
1517                         reply_ptr[data_index++] = bite;
1518                 }
1519                 if (data_index < data_len) {
1520                         recv_byte();
1521                         break;
1522                 }
1523
1524                 if (pmu_state == reading_intr) {
1525                         pmu_state = idle;
1526                         int_data_state[int_data_last] = int_data_ready;
1527                         interrupt_data_len[int_data_last] = data_len;
1528                 } else {
1529                         req = current_req;
1530                         /* 
1531                          * For PMU sleep and freq change requests, we lock the
1532                          * PMU until it's explicitely unlocked. This avoids any
1533                          * spurrious event polling getting in
1534                          */
1535                         current_req = req->next;
1536                         req->reply_len += data_index;
1537                         if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1538                                 pmu_state = locked;
1539                         else
1540                                 pmu_state = idle;
1541                         return req;
1542                 }
1543                 break;
1544
1545         default:
1546                 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1547                        pmu_state);
1548         }
1549         return NULL;
1550 }
1551
1552 static irqreturn_t __pmac
1553 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1554 {
1555         unsigned long flags;
1556         int intr;
1557         int nloop = 0;
1558         int int_data = -1;
1559         struct adb_request *req = NULL;
1560         int handled = 0;
1561
1562         /* This is a bit brutal, we can probably do better */
1563         spin_lock_irqsave(&pmu_lock, flags);
1564         ++disable_poll;
1565         
1566         for (;;) {
1567                 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1568                 if (intr == 0)
1569                         break;
1570                 handled = 1;
1571                 if (++nloop > 1000) {
1572                         printk(KERN_DEBUG "PMU: stuck in intr loop, "
1573                                "intr=%x, ier=%x pmu_state=%d\n",
1574                                intr, in_8(&via[IER]), pmu_state);
1575                         break;
1576                 }
1577                 out_8(&via[IFR], intr);
1578                 if (intr & CB1_INT) {
1579                         adb_int_pending = 1;
1580                         pmu_irq_stats[0]++;
1581                 }
1582                 if (intr & SR_INT) {
1583                         req = pmu_sr_intr(regs);
1584                         if (req)
1585                                 break;
1586                 }
1587         }
1588
1589 recheck:
1590         if (pmu_state == idle) {
1591                 if (adb_int_pending) {
1592                         if (int_data_state[0] == int_data_empty)
1593                                 int_data_last = 0;
1594                         else if (int_data_state[1] == int_data_empty)
1595                                 int_data_last = 1;
1596                         else
1597                                 goto no_free_slot;
1598                         pmu_state = intack;
1599                         int_data_state[int_data_last] = int_data_fill;
1600                         /* Sounds safer to make sure ACK is high before writing.
1601                          * This helped kill a problem with ADB and some iBooks
1602                          */
1603                         wait_for_ack();
1604                         send_byte(PMU_INT_ACK);
1605                         adb_int_pending = 0;
1606                 } else if (current_req)
1607                         pmu_start();
1608         }
1609 no_free_slot:                   
1610         /* Mark the oldest buffer for flushing */
1611         if (int_data_state[!int_data_last] == int_data_ready) {
1612                 int_data_state[!int_data_last] = int_data_flush;
1613                 int_data = !int_data_last;
1614         } else if (int_data_state[int_data_last] == int_data_ready) {
1615                 int_data_state[int_data_last] = int_data_flush;
1616                 int_data = int_data_last;
1617         }
1618         --disable_poll;
1619         spin_unlock_irqrestore(&pmu_lock, flags);
1620
1621         /* Deal with completed PMU requests outside of the lock */
1622         if (req) {
1623                 pmu_done(req);
1624                 req = NULL;
1625         }
1626                 
1627         /* Deal with interrupt datas outside of the lock */
1628         if (int_data >= 0) {
1629                 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1630                 spin_lock_irqsave(&pmu_lock, flags);
1631                 ++disable_poll;
1632                 int_data_state[int_data] = int_data_empty;
1633                 int_data = -1;
1634                 goto recheck;
1635         }
1636
1637         return IRQ_RETVAL(handled);
1638 }
1639
1640 void __pmac
1641 pmu_unlock(void)
1642 {
1643         unsigned long flags;
1644
1645         spin_lock_irqsave(&pmu_lock, flags);
1646         if (pmu_state == locked)
1647                 pmu_state = idle;
1648         adb_int_pending = 1;
1649         spin_unlock_irqrestore(&pmu_lock, flags);
1650 }
1651
1652
1653 static irqreturn_t __pmac
1654 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1655 {
1656         unsigned long flags;
1657
1658         if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1659                 spin_lock_irqsave(&pmu_lock, flags);
1660                 if (gpio_irq_enabled > 0) {
1661                         disable_irq_nosync(gpio_irq);
1662                         gpio_irq_enabled = 0;
1663                 }
1664                 pmu_irq_stats[1]++;
1665                 adb_int_pending = 1;
1666                 spin_unlock_irqrestore(&pmu_lock, flags);
1667                 via_pmu_interrupt(0, NULL, NULL);
1668                 return IRQ_HANDLED;
1669         }
1670         return IRQ_NONE;
1671 }
1672
1673 #ifdef CONFIG_PMAC_BACKLIGHT
1674 static int backlight_to_bright[] __pmacdata = {
1675         0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1676         0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1677 };
1678  
1679 static int __openfirmware
1680 pmu_set_backlight_enable(int on, int level, void* data)
1681 {
1682         struct adb_request req;
1683         
1684         if (vias == NULL)
1685                 return -ENODEV;
1686
1687         if (on) {
1688                 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1689                             backlight_to_bright[level]);
1690                 pmu_wait_complete(&req);
1691         }
1692         pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1693                     PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1694         pmu_wait_complete(&req);
1695
1696         return 0;
1697 }
1698
1699 static void __openfirmware
1700 pmu_bright_complete(struct adb_request *req)
1701 {
1702         if (req == &bright_req_1)
1703                 clear_bit(1, &async_req_locks);
1704         if (req == &bright_req_2)
1705                 clear_bit(2, &async_req_locks);
1706 }
1707
1708 static int __openfirmware
1709 pmu_set_backlight_level(int level, void* data)
1710 {
1711         if (vias == NULL)
1712                 return -ENODEV;
1713
1714         if (test_and_set_bit(1, &async_req_locks))
1715                 return -EAGAIN;
1716         pmu_request(&bright_req_1, pmu_bright_complete, 2, PMU_BACKLIGHT_BRIGHT,
1717                 backlight_to_bright[level]);
1718         if (test_and_set_bit(2, &async_req_locks))
1719                 return -EAGAIN;
1720         pmu_request(&bright_req_2, pmu_bright_complete, 2, PMU_POWER_CTRL,
1721                     PMU_POW_BACKLIGHT | (level > BACKLIGHT_OFF ?
1722                                          PMU_POW_ON : PMU_POW_OFF));
1723
1724         return 0;
1725 }
1726 #endif /* CONFIG_PMAC_BACKLIGHT */
1727
1728 void __pmac
1729 pmu_enable_irled(int on)
1730 {
1731         struct adb_request req;
1732
1733         if (vias == NULL)
1734                 return ;
1735         if (pmu_kind == PMU_KEYLARGO_BASED)
1736                 return ;
1737
1738         pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1739             (on ? PMU_POW_ON : PMU_POW_OFF));
1740         pmu_wait_complete(&req);
1741 }
1742
1743 void __pmac
1744 pmu_restart(void)
1745 {
1746         struct adb_request req;
1747
1748         local_irq_disable();
1749
1750         drop_interrupts = 1;
1751         
1752         if (pmu_kind != PMU_KEYLARGO_BASED) {
1753                 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1754                                                 PMU_INT_TICK );
1755                 while(!req.complete)
1756                         pmu_poll();
1757         }
1758
1759         pmu_request(&req, NULL, 1, PMU_RESET);
1760         pmu_wait_complete(&req);
1761         for (;;)
1762                 ;
1763 }
1764
1765 void __pmac
1766 pmu_shutdown(void)
1767 {
1768         struct adb_request req;
1769
1770         local_irq_disable();
1771
1772         drop_interrupts = 1;
1773
1774         if (pmu_kind != PMU_KEYLARGO_BASED) {
1775                 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1776                                                 PMU_INT_TICK );
1777                 pmu_wait_complete(&req);
1778         } else {
1779                 /* Disable server mode on shutdown or we'll just
1780                  * wake up again
1781                  */
1782                 pmu_set_server_mode(0);
1783         }
1784
1785         pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1786                     'M', 'A', 'T', 'T');
1787         pmu_wait_complete(&req);
1788         for (;;)
1789                 ;
1790 }
1791
1792 int
1793 pmu_present(void)
1794 {
1795         return via != 0;
1796 }
1797
1798 struct pmu_i2c_hdr {
1799         u8      bus;
1800         u8      mode;
1801         u8      bus2;
1802         u8      address;
1803         u8      sub_addr;
1804         u8      comb_addr;
1805         u8      count;
1806 };
1807
1808 int
1809 pmu_i2c_combined_read(int bus, int addr, int subaddr,  u8* data, int len)
1810 {
1811         struct adb_request      req;
1812         struct pmu_i2c_hdr      *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1813         int retry;
1814         int rc;
1815
1816         for (retry=0; retry<16; retry++) {
1817                 memset(&req, 0, sizeof(req));
1818
1819                 hdr->bus = bus;
1820                 hdr->address = addr & 0xfe;
1821                 hdr->mode = PMU_I2C_MODE_COMBINED;
1822                 hdr->bus2 = 0;
1823                 hdr->sub_addr = subaddr;
1824                 hdr->comb_addr = addr | 1;
1825                 hdr->count = len;
1826                 
1827                 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1828                 req.reply_expected = 0;
1829                 req.reply_len = 0;
1830                 req.data[0] = PMU_I2C_CMD;
1831                 req.reply[0] = 0xff;
1832                 rc = pmu_queue_request(&req);
1833                 if (rc)
1834                         return rc;
1835                 while(!req.complete)
1836                         pmu_poll();
1837                 if (req.reply[0] == PMU_I2C_STATUS_OK)
1838                         break;
1839                 mdelay(15);
1840         }
1841         if (req.reply[0] != PMU_I2C_STATUS_OK)
1842                 return -1;
1843
1844         for (retry=0; retry<16; retry++) {
1845                 memset(&req, 0, sizeof(req));
1846
1847                 mdelay(15);
1848
1849                 hdr->bus = PMU_I2C_BUS_STATUS;
1850                 req.reply[0] = 0xff;
1851                 
1852                 req.nbytes = 2;
1853                 req.reply_expected = 0;
1854                 req.reply_len = 0;
1855                 req.data[0] = PMU_I2C_CMD;
1856                 rc = pmu_queue_request(&req);
1857                 if (rc)
1858                         return rc;
1859                 while(!req.complete)
1860                         pmu_poll();
1861                 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1862                         memcpy(data, &req.reply[1], req.reply_len - 1);
1863                         return req.reply_len - 1;
1864                 }
1865         }
1866         return -1;
1867 }
1868
1869 int
1870 pmu_i2c_stdsub_write(int bus, int addr, int subaddr,  u8* data, int len)
1871 {
1872         struct adb_request      req;
1873         struct pmu_i2c_hdr      *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1874         int retry;
1875         int rc;
1876
1877         for (retry=0; retry<16; retry++) {
1878                 memset(&req, 0, sizeof(req));
1879
1880                 hdr->bus = bus;
1881                 hdr->address = addr & 0xfe;
1882                 hdr->mode = PMU_I2C_MODE_STDSUB;
1883                 hdr->bus2 = 0;
1884                 hdr->sub_addr = subaddr;
1885                 hdr->comb_addr = addr & 0xfe;
1886                 hdr->count = len;
1887
1888                 req.data[0] = PMU_I2C_CMD;
1889                 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
1890                 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
1891                 req.reply_expected = 0;
1892                 req.reply_len = 0;
1893                 req.reply[0] = 0xff;
1894                 rc = pmu_queue_request(&req);
1895                 if (rc)
1896                         return rc;
1897                 while(!req.complete)
1898                         pmu_poll();
1899                 if (req.reply[0] == PMU_I2C_STATUS_OK)
1900                         break;
1901                 mdelay(15);
1902         }
1903         if (req.reply[0] != PMU_I2C_STATUS_OK)
1904                 return -1;
1905
1906         for (retry=0; retry<16; retry++) {
1907                 memset(&req, 0, sizeof(req));
1908
1909                 mdelay(15);
1910
1911                 hdr->bus = PMU_I2C_BUS_STATUS;
1912                 req.reply[0] = 0xff;
1913                 
1914                 req.nbytes = 2;
1915                 req.reply_expected = 0;
1916                 req.reply_len = 0;
1917                 req.data[0] = PMU_I2C_CMD;
1918                 rc = pmu_queue_request(&req);
1919                 if (rc)
1920                         return rc;
1921                 while(!req.complete)
1922                         pmu_poll();
1923                 if (req.reply[0] == PMU_I2C_STATUS_OK)
1924                         return len;
1925         }
1926         return -1;
1927 }
1928
1929 int
1930 pmu_i2c_simple_read(int bus, int addr,  u8* data, int len)
1931 {
1932         struct adb_request      req;
1933         struct pmu_i2c_hdr      *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1934         int retry;
1935         int rc;
1936
1937         for (retry=0; retry<16; retry++) {
1938                 memset(&req, 0, sizeof(req));
1939
1940                 hdr->bus = bus;
1941                 hdr->address = addr | 1;
1942                 hdr->mode = PMU_I2C_MODE_SIMPLE;
1943                 hdr->bus2 = 0;
1944                 hdr->sub_addr = 0;
1945                 hdr->comb_addr = 0;
1946                 hdr->count = len;
1947
1948                 req.data[0] = PMU_I2C_CMD;
1949                 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1950                 req.reply_expected = 0;
1951                 req.reply_len = 0;
1952                 req.reply[0] = 0xff;
1953                 rc = pmu_queue_request(&req);
1954                 if (rc)
1955                         return rc;
1956                 while(!req.complete)
1957                         pmu_poll();
1958                 if (req.reply[0] == PMU_I2C_STATUS_OK)
1959                         break;
1960                 mdelay(15);
1961         }
1962         if (req.reply[0] != PMU_I2C_STATUS_OK)
1963                 return -1;
1964
1965         for (retry=0; retry<16; retry++) {
1966                 memset(&req, 0, sizeof(req));
1967
1968                 mdelay(15);
1969
1970                 hdr->bus = PMU_I2C_BUS_STATUS;
1971                 req.reply[0] = 0xff;
1972                 
1973                 req.nbytes = 2;
1974                 req.reply_expected = 0;
1975                 req.reply_len = 0;
1976                 req.data[0] = PMU_I2C_CMD;
1977                 rc = pmu_queue_request(&req);
1978                 if (rc)
1979                         return rc;
1980                 while(!req.complete)
1981                         pmu_poll();
1982                 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1983                         memcpy(data, &req.reply[1], req.reply_len - 1);
1984                         return req.reply_len - 1;
1985                 }
1986         }
1987         return -1;
1988 }
1989
1990 int
1991 pmu_i2c_simple_write(int bus, int addr,  u8* data, int len)
1992 {
1993         struct adb_request      req;
1994         struct pmu_i2c_hdr      *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1995         int retry;
1996         int rc;
1997
1998         for (retry=0; retry<16; retry++) {
1999                 memset(&req, 0, sizeof(req));
2000
2001                 hdr->bus = bus;
2002                 hdr->address = addr & 0xfe;
2003                 hdr->mode = PMU_I2C_MODE_SIMPLE;
2004                 hdr->bus2 = 0;
2005                 hdr->sub_addr = 0;
2006                 hdr->comb_addr = 0;
2007                 hdr->count = len;
2008
2009                 req.data[0] = PMU_I2C_CMD;
2010                 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
2011                 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
2012                 req.reply_expected = 0;
2013                 req.reply_len = 0;
2014                 req.reply[0] = 0xff;
2015                 rc = pmu_queue_request(&req);
2016                 if (rc)
2017                         return rc;
2018                 while(!req.complete)
2019                         pmu_poll();
2020                 if (req.reply[0] == PMU_I2C_STATUS_OK)
2021                         break;
2022                 mdelay(15);
2023         }
2024         if (req.reply[0] != PMU_I2C_STATUS_OK)
2025                 return -1;
2026
2027         for (retry=0; retry<16; retry++) {
2028                 memset(&req, 0, sizeof(req));
2029
2030                 mdelay(15);
2031
2032                 hdr->bus = PMU_I2C_BUS_STATUS;
2033                 req.reply[0] = 0xff;
2034                 
2035                 req.nbytes = 2;
2036                 req.reply_expected = 0;
2037                 req.reply_len = 0;
2038                 req.data[0] = PMU_I2C_CMD;
2039                 rc = pmu_queue_request(&req);
2040                 if (rc)
2041                         return rc;
2042                 while(!req.complete)
2043                         pmu_poll();
2044                 if (req.reply[0] == PMU_I2C_STATUS_OK)
2045                         return len;
2046         }
2047         return -1;
2048 }
2049
2050 #ifdef CONFIG_PMAC_PBOOK
2051
2052 static LIST_HEAD(sleep_notifiers);
2053
2054 int
2055 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
2056 {
2057         struct list_head *list;
2058         struct pmu_sleep_notifier *notifier;
2059
2060         for (list = sleep_notifiers.next; list != &sleep_notifiers;
2061              list = list->next) {
2062                 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2063                 if (n->priority > notifier->priority)
2064                         break;
2065         }
2066         __list_add(&n->list, list->prev, list);
2067         return 0;
2068 }
2069
2070 int
2071 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
2072 {
2073         if (n->list.next == 0)
2074                 return -ENOENT;
2075         list_del(&n->list);
2076         n->list.next = NULL;
2077         return 0;
2078 }
2079
2080 /* Sleep is broadcast last-to-first */
2081 static int __pmac
2082 broadcast_sleep(int when, int fallback)
2083 {
2084         int ret = PBOOK_SLEEP_OK;
2085         struct list_head *list;
2086         struct pmu_sleep_notifier *notifier;
2087
2088         for (list = sleep_notifiers.prev; list != &sleep_notifiers;
2089              list = list->prev) {
2090                 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2091                 ret = notifier->notifier_call(notifier, when);
2092                 if (ret != PBOOK_SLEEP_OK) {
2093                         printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
2094                                when, notifier, notifier->notifier_call);
2095                         for (; list != &sleep_notifiers; list = list->next) {
2096                                 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2097                                 notifier->notifier_call(notifier, fallback);
2098                         }
2099                         return ret;
2100                 }
2101         }
2102         return ret;
2103 }
2104
2105 /* Wake is broadcast first-to-last */
2106 static int __pmac
2107 broadcast_wake(void)
2108 {
2109         int ret = PBOOK_SLEEP_OK;
2110         struct list_head *list;
2111         struct pmu_sleep_notifier *notifier;
2112
2113         for (list = sleep_notifiers.next; list != &sleep_notifiers;
2114              list = list->next) {
2115                 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2116                 notifier->notifier_call(notifier, PBOOK_WAKE);
2117         }
2118         return ret;
2119 }
2120
2121 /*
2122  * This struct is used to store config register values for
2123  * PCI devices which may get powered off when we sleep.
2124  */
2125 static struct pci_save {
2126 #ifndef HACKED_PCI_SAVE
2127         u16     command;
2128         u16     cache_lat;
2129         u16     intr;
2130         u32     rom_address;
2131 #else
2132         u32     config[16];
2133 #endif  
2134 } *pbook_pci_saves;
2135 static int pbook_npci_saves;
2136
2137 static void __pmac
2138 pbook_alloc_pci_save(void)
2139 {
2140         int npci;
2141         struct pci_dev *pd = NULL;
2142
2143         npci = 0;
2144         while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2145                 ++npci;
2146         }
2147         if (npci == 0)
2148                 return;
2149         pbook_pci_saves = (struct pci_save *)
2150                 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
2151         pbook_npci_saves = npci;
2152 }
2153
2154 static void __pmac
2155 pbook_free_pci_save(void)
2156 {
2157         if (pbook_pci_saves == NULL)
2158                 return;
2159         kfree(pbook_pci_saves);
2160         pbook_pci_saves = NULL;
2161         pbook_npci_saves = 0;
2162 }
2163
2164 static void __pmac
2165 pbook_pci_save(void)
2166 {
2167         struct pci_save *ps = pbook_pci_saves;
2168         struct pci_dev *pd = NULL;
2169         int npci = pbook_npci_saves;
2170         
2171         if (ps == NULL)
2172                 return;
2173
2174         while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2175                 if (npci-- == 0)
2176                         return;
2177 #ifndef HACKED_PCI_SAVE
2178                 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
2179                 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
2180                 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
2181                 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
2182 #else
2183                 int i;
2184                 for (i=1;i<16;i++)
2185                         pci_read_config_dword(pd, i<<4, &ps->config[i]);
2186 #endif
2187                 ++ps;
2188         }
2189 }
2190
2191 /* For this to work, we must take care of a few things: If gmac was enabled
2192  * during boot, it will be in the pci dev list. If it's disabled at this point
2193  * (and it will probably be), then you can't access it's config space.
2194  */
2195 static void __pmac
2196 pbook_pci_restore(void)
2197 {
2198         u16 cmd;
2199         struct pci_save *ps = pbook_pci_saves - 1;
2200         struct pci_dev *pd = NULL;
2201         int npci = pbook_npci_saves;
2202         int j;
2203
2204         while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2205 #ifdef HACKED_PCI_SAVE
2206                 int i;
2207                 if (npci-- == 0)
2208                         return;
2209                 ps++;
2210                 for (i=2;i<16;i++)
2211                         pci_write_config_dword(pd, i<<4, ps->config[i]);
2212                 pci_write_config_dword(pd, 4, ps->config[1]);
2213 #else
2214                 if (npci-- == 0)
2215                         return;
2216                 ps++;
2217                 if (ps->command == 0)
2218                         continue;
2219                 pci_read_config_word(pd, PCI_COMMAND, &cmd);
2220                 if ((ps->command & ~cmd) == 0)
2221                         continue;
2222                 switch (pd->hdr_type) {
2223                 case PCI_HEADER_TYPE_NORMAL:
2224                         for (j = 0; j < 6; ++j)
2225                                 pci_write_config_dword(pd,
2226                                         PCI_BASE_ADDRESS_0 + j*4,
2227                                         pd->resource[j].start);
2228                         pci_write_config_dword(pd, PCI_ROM_ADDRESS,
2229                                 ps->rom_address);
2230                         pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
2231                                 ps->cache_lat);
2232                         pci_write_config_word(pd, PCI_INTERRUPT_LINE,
2233                                 ps->intr);
2234                         pci_write_config_word(pd, PCI_COMMAND, ps->command);
2235                         break;
2236                 }
2237 #endif  
2238         }
2239 }
2240
2241 #ifdef DEBUG_SLEEP
2242 /* N.B. This doesn't work on the 3400 */
2243 void  __pmac
2244 pmu_blink(int n)
2245 {
2246         struct adb_request req;
2247
2248         memset(&req, 0, sizeof(req));
2249
2250         for (; n > 0; --n) {
2251                 req.nbytes = 4;
2252                 req.done = NULL;
2253                 req.data[0] = 0xee;
2254                 req.data[1] = 4;
2255                 req.data[2] = 0;
2256                 req.data[3] = 1;
2257                 req.reply[0] = ADB_RET_OK;
2258                 req.reply_len = 1;
2259                 req.reply_expected = 0;
2260                 pmu_polled_request(&req);
2261                 mdelay(50);
2262                 req.nbytes = 4;
2263                 req.done = NULL;
2264                 req.data[0] = 0xee;
2265                 req.data[1] = 4;
2266                 req.data[2] = 0;
2267                 req.data[3] = 0;
2268                 req.reply[0] = ADB_RET_OK;
2269                 req.reply_len = 1;
2270                 req.reply_expected = 0;
2271                 pmu_polled_request(&req);
2272                 mdelay(50);
2273         }
2274         mdelay(50);
2275 }
2276 #endif
2277
2278 /*
2279  * Put the powerbook to sleep.
2280  */
2281  
2282 static u32 save_via[8] __pmacdata;
2283
2284 static void __pmac
2285 save_via_state(void)
2286 {
2287         save_via[0] = in_8(&via[ANH]);
2288         save_via[1] = in_8(&via[DIRA]);
2289         save_via[2] = in_8(&via[B]);
2290         save_via[3] = in_8(&via[DIRB]);
2291         save_via[4] = in_8(&via[PCR]);
2292         save_via[5] = in_8(&via[ACR]);
2293         save_via[6] = in_8(&via[T1CL]);
2294         save_via[7] = in_8(&via[T1CH]);
2295 }
2296 static void __pmac
2297 restore_via_state(void)
2298 {
2299         out_8(&via[ANH], save_via[0]);
2300         out_8(&via[DIRA], save_via[1]);
2301         out_8(&via[B], save_via[2]);
2302         out_8(&via[DIRB], save_via[3]);
2303         out_8(&via[PCR], save_via[4]);
2304         out_8(&via[ACR], save_via[5]);
2305         out_8(&via[T1CL], save_via[6]);
2306         out_8(&via[T1CH], save_via[7]);
2307         out_8(&via[IER], IER_CLR | 0x7f);       /* disable all intrs */
2308         out_8(&via[IFR], 0x7f);                         /* clear IFR */
2309         out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2310 }
2311
2312 static int __pmac
2313 pmac_suspend_devices(void)
2314 {
2315         int ret;
2316
2317         pm_prepare_console();
2318         
2319         /* Notify old-style device drivers & userland */
2320         ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2321         if (ret != PBOOK_SLEEP_OK) {
2322                 printk(KERN_ERR "Sleep rejected by drivers\n");
2323                 return -EBUSY;
2324         }
2325
2326         /* Sync the disks. */
2327         /* XXX It would be nice to have some way to ensure that
2328          * nobody is dirtying any new buffers while we wait. That
2329          * could be acheived using the refrigerator for processes
2330          * that swsusp uses
2331          */
2332         sys_sync();
2333
2334         /* Sleep can fail now. May not be very robust but useful for debugging */
2335         ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2336         if (ret != PBOOK_SLEEP_OK) {
2337                 printk(KERN_ERR "Driver sleep failed\n");
2338                 return -EBUSY;
2339         }
2340
2341         /* Send suspend call to devices, hold the device core's dpm_sem */
2342         ret = device_suspend(PM_SUSPEND_MEM);
2343         if (ret) {
2344                 broadcast_wake();
2345                 printk(KERN_ERR "Driver sleep failed\n");
2346                 return -EBUSY;
2347         }
2348
2349         preempt_disable();
2350
2351         /* Make sure the decrementer won't interrupt us */
2352         asm volatile("mtdec %0" : : "r" (0x7fffffff));
2353         /* Make sure any pending DEC interrupt occurring while we did
2354          * the above didn't re-enable the DEC */
2355         mb();
2356         asm volatile("mtdec %0" : : "r" (0x7fffffff));
2357
2358         /* We can now disable MSR_EE. This code of course works properly only
2359          * on UP machines... For SMP, if we ever implement sleep, we'll have to
2360          * stop the "other" CPUs way before we do all that stuff.
2361          */
2362         local_irq_disable();
2363
2364         /* Broadcast power down irq
2365          * This isn't that useful in most cases (only directly wired devices can
2366          * use this but still... This will take care of sysdev's as well, so
2367          * we exit from here with local irqs disabled and PIC off.
2368          */
2369         ret = device_power_down(PM_SUSPEND_MEM);
2370         if (ret) {
2371                 wakeup_decrementer();
2372                 local_irq_enable();
2373                 preempt_enable();
2374                 device_resume();
2375                 broadcast_wake();
2376                 printk(KERN_ERR "Driver powerdown failed\n");
2377                 return -EBUSY;
2378         }
2379
2380         /* Wait for completion of async backlight requests */
2381         while (!bright_req_1.complete || !bright_req_2.complete ||
2382
2383                         !batt_req.complete)
2384                 pmu_poll();
2385
2386         /* Giveup the lazy FPU & vec so we don't have to back them
2387          * up from the low level code
2388          */
2389         enable_kernel_fp();
2390
2391 #ifdef CONFIG_ALTIVEC
2392         if (cur_cpu_spec[0]->cpu_features & CPU_FTR_ALTIVEC)
2393                 enable_kernel_altivec();
2394 #endif /* CONFIG_ALTIVEC */
2395
2396         return 0;
2397 }
2398
2399 static int __pmac
2400 pmac_wakeup_devices(void)
2401 {
2402         mdelay(100);
2403
2404         /* Power back up system devices (including the PIC) */
2405         device_power_up();
2406
2407         /* Force a poll of ADB interrupts */
2408         adb_int_pending = 1;
2409         via_pmu_interrupt(0, NULL, NULL);
2410
2411         /* Restart jiffies & scheduling */
2412         wakeup_decrementer();
2413
2414         /* Re-enable local CPU interrupts */
2415         local_irq_enable();
2416
2417         mdelay(100);
2418
2419         preempt_enable();
2420
2421         /* Resume devices */
2422         device_resume();
2423
2424         /* Notify old style drivers */
2425         broadcast_wake();
2426
2427         pm_restore_console();
2428
2429         return 0;
2430 }
2431
2432 #define GRACKLE_PM      (1<<7)
2433 #define GRACKLE_DOZE    (1<<5)
2434 #define GRACKLE_NAP     (1<<4)
2435 #define GRACKLE_SLEEP   (1<<3)
2436
2437 int __pmac
2438 powerbook_sleep_grackle(void)
2439 {
2440         unsigned long save_l2cr;
2441         unsigned short pmcr1;
2442         struct adb_request req;
2443         int ret;
2444         struct pci_dev *grackle;
2445
2446         grackle = pci_find_slot(0, 0);
2447         if (!grackle)
2448                 return -ENODEV;
2449
2450         ret = pmac_suspend_devices();
2451         if (ret) {
2452                 printk(KERN_ERR "Sleep rejected by devices\n");
2453                 return ret;
2454         }
2455         
2456         /* Turn off various things. Darwin does some retry tests here... */
2457         pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2458         pmu_wait_complete(&req);
2459         pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2460                 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2461         pmu_wait_complete(&req);
2462
2463         /* For 750, save backside cache setting and disable it */
2464         save_l2cr = _get_L2CR();        /* (returns -1 if not available) */
2465
2466         if (!__fake_sleep) {
2467                 /* Ask the PMU to put us to sleep */
2468                 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2469                 pmu_wait_complete(&req);
2470         }
2471
2472         /* The VIA is supposed not to be restored correctly*/
2473         save_via_state();
2474         /* We shut down some HW */
2475         pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2476
2477         pci_read_config_word(grackle, 0x70, &pmcr1);
2478         /* Apparently, MacOS uses NAP mode for Grackle ??? */
2479         pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP); 
2480         pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2481         pci_write_config_word(grackle, 0x70, pmcr1);
2482
2483         /* Call low-level ASM sleep handler */
2484         if (__fake_sleep)
2485                 mdelay(5000);
2486         else
2487                 low_sleep_handler();
2488
2489         /* We're awake again, stop grackle PM */
2490         pci_read_config_word(grackle, 0x70, &pmcr1);
2491         pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP); 
2492         pci_write_config_word(grackle, 0x70, pmcr1);
2493
2494         /* Make sure the PMU is idle */
2495         pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2496         restore_via_state();
2497         
2498         /* Restore L2 cache */
2499         if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2500                 _set_L2CR(save_l2cr);
2501         
2502         /* Restore userland MMU context */
2503         set_context(current->active_mm->context, current->active_mm->pgd);
2504
2505         /* Power things up */
2506         pmu_unlock();
2507         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2508         pmu_wait_complete(&req);
2509         pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2510                         PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2511         pmu_wait_complete(&req);
2512         pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2513                         PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2514         pmu_wait_complete(&req);
2515
2516         pmac_wakeup_devices();
2517
2518         return 0;
2519 }
2520
2521 static int __pmac
2522 powerbook_sleep_Core99(void)
2523 {
2524         unsigned long save_l2cr;
2525         unsigned long save_l3cr;
2526         struct adb_request req;
2527         int ret;
2528         
2529         if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2530                 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2531                 return -ENOSYS;
2532         }
2533
2534         if (num_online_cpus() > 1 || cpu_is_offline(0))
2535                 return -EAGAIN;
2536
2537         ret = pmac_suspend_devices();
2538         if (ret) {
2539                 printk(KERN_ERR "Sleep rejected by devices\n");
2540                 return ret;
2541         }
2542
2543         printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
2544
2545         /* Tell PMU what events will wake us up */
2546         pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2547                 0xff, 0xff);
2548         pmu_wait_complete(&req);
2549         pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2550                 0, PMU_PWR_WAKEUP_KEY |
2551                 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2552         pmu_wait_complete(&req);
2553
2554         /* Save the state of the L2 and L3 caches */
2555         save_l3cr = _get_L3CR();        /* (returns -1 if not available) */
2556         save_l2cr = _get_L2CR();        /* (returns -1 if not available) */
2557
2558         if (!__fake_sleep) {
2559                 /* Ask the PMU to put us to sleep */
2560                 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2561                 pmu_wait_complete(&req);
2562         }
2563
2564         /* The VIA is supposed not to be restored correctly*/
2565         save_via_state();
2566
2567         /* Shut down various ASICs. There's a chance that we can no longer
2568          * talk to the PMU after this, so I moved it to _after_ sending the
2569          * sleep command to it. Still need to be checked.
2570          */
2571         pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2572
2573         /* Call low-level ASM sleep handler */
2574         if (__fake_sleep)
2575                 mdelay(5000);
2576         else
2577                 low_sleep_handler();
2578
2579         /* Restore Apple core ASICs state */
2580         pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2581
2582         /* Restore VIA */
2583         restore_via_state();
2584
2585         /* Restore video */
2586         pmac_call_early_video_resume();
2587
2588         /* Restore L2 cache */
2589         if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2590                 _set_L2CR(save_l2cr);
2591         /* Restore L3 cache */
2592         if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2593                 _set_L3CR(save_l3cr);
2594         
2595         /* Restore userland MMU context */
2596         set_context(current->active_mm->context, current->active_mm->pgd);
2597
2598         /* Tell PMU we are ready */
2599         pmu_unlock();
2600         pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2601         pmu_wait_complete(&req);
2602         pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2603         pmu_wait_complete(&req);
2604
2605         printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
2606
2607         pmac_wakeup_devices();
2608
2609         return 0;
2610 }
2611
2612 #define PB3400_MEM_CTRL         0xf8000000
2613 #define PB3400_MEM_CTRL_SLEEP   0x70
2614
2615 static int __pmac
2616 powerbook_sleep_3400(void)
2617 {
2618         int ret, i, x;
2619         unsigned int hid0;
2620         unsigned long p;
2621         struct adb_request sleep_req;
2622         void __iomem *mem_ctrl;
2623         unsigned int __iomem *mem_ctrl_sleep;
2624
2625         /* first map in the memory controller registers */
2626         mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2627         if (mem_ctrl == NULL) {
2628                 printk("powerbook_sleep_3400: ioremap failed\n");
2629                 return -ENOMEM;
2630         }
2631         mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2632
2633         /* Allocate room for PCI save */
2634         pbook_alloc_pci_save();
2635
2636         ret = pmac_suspend_devices();
2637         if (ret) {
2638                 pbook_free_pci_save();
2639                 printk(KERN_ERR "Sleep rejected by devices\n");
2640                 return ret;
2641         }
2642
2643         /* Save the state of PCI config space for some slots */
2644         pbook_pci_save();
2645
2646         /* Set the memory controller to keep the memory refreshed
2647            while we're asleep */
2648         for (i = 0x403f; i >= 0x4000; --i) {
2649                 out_be32(mem_ctrl_sleep, i);
2650                 do {
2651                         x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2652                 } while (x == 0);
2653                 if (x >= 0x100)
2654                         break;
2655         }
2656
2657         /* Ask the PMU to put us to sleep */
2658         pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2659         while (!sleep_req.complete)
2660                 mb();
2661
2662         pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2663
2664         /* displacement-flush the L2 cache - necessary? */
2665         for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2666                 i = *(volatile int *)p;
2667         asleep = 1;
2668
2669         /* Put the CPU into sleep mode */
2670         asm volatile("mfspr %0,1008" : "=r" (hid0) :);
2671         hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2672         asm volatile("mtspr 1008,%0" : : "r" (hid0));
2673         _nmask_and_or_msr(0, MSR_POW | MSR_EE);
2674         udelay(10);
2675
2676         /* OK, we're awake again, start restoring things */
2677         out_be32(mem_ctrl_sleep, 0x3f);
2678         pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2679         pbook_pci_restore();
2680         pmu_unlock();
2681
2682         /* wait for the PMU interrupt sequence to complete */
2683         while (asleep)
2684                 mb();
2685
2686         pmac_wakeup_devices();
2687         pbook_free_pci_save();
2688         iounmap(mem_ctrl);
2689
2690         return 0;
2691 }
2692
2693 /*
2694  * Support for /dev/pmu device
2695  */
2696 #define RB_SIZE         0x10
2697 struct pmu_private {
2698         struct list_head list;
2699         int     rb_get;
2700         int     rb_put;
2701         struct rb_entry {
2702                 unsigned short len;
2703                 unsigned char data[16];
2704         }       rb_buf[RB_SIZE];
2705         wait_queue_head_t wait;
2706         spinlock_t lock;
2707 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2708         int     backlight_locker;
2709 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */     
2710 };
2711
2712 static LIST_HEAD(all_pmu_pvt);
2713 static DEFINE_SPINLOCK(all_pvt_lock __pmacdata);
2714
2715 static void __pmac
2716 pmu_pass_intr(unsigned char *data, int len)
2717 {
2718         struct pmu_private *pp;
2719         struct list_head *list;
2720         int i;
2721         unsigned long flags;
2722
2723         if (len > sizeof(pp->rb_buf[0].data))
2724                 len = sizeof(pp->rb_buf[0].data);
2725         spin_lock_irqsave(&all_pvt_lock, flags);
2726         for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2727                 pp = list_entry(list, struct pmu_private, list);
2728                 spin_lock(&pp->lock);
2729                 i = pp->rb_put + 1;
2730                 if (i >= RB_SIZE)
2731                         i = 0;
2732                 if (i != pp->rb_get) {
2733                         struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2734                         rp->len = len;
2735                         memcpy(rp->data, data, len);
2736                         pp->rb_put = i;
2737                         wake_up_interruptible(&pp->wait);
2738                 }
2739                 spin_unlock(&pp->lock);
2740         }
2741         spin_unlock_irqrestore(&all_pvt_lock, flags);
2742 }
2743
2744 static int __pmac
2745 pmu_open(struct inode *inode, struct file *file)
2746 {
2747         struct pmu_private *pp;
2748         unsigned long flags;
2749
2750         pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2751         if (pp == 0)
2752                 return -ENOMEM;
2753         pp->rb_get = pp->rb_put = 0;
2754         spin_lock_init(&pp->lock);
2755         init_waitqueue_head(&pp->wait);
2756         spin_lock_irqsave(&all_pvt_lock, flags);
2757 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2758         pp->backlight_locker = 0;
2759 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */     
2760         list_add(&pp->list, &all_pmu_pvt);
2761         spin_unlock_irqrestore(&all_pvt_lock, flags);
2762         file->private_data = pp;
2763         return 0;
2764 }
2765
2766 static ssize_t  __pmac
2767 pmu_read(struct file *file, char __user *buf,
2768                         size_t count, loff_t *ppos)
2769 {
2770         struct pmu_private *pp = file->private_data;
2771         DECLARE_WAITQUEUE(wait, current);
2772         unsigned long flags;
2773         int ret;
2774
2775         if (count < 1 || pp == 0)
2776                 return -EINVAL;
2777         ret = verify_area(VERIFY_WRITE, buf, count);
2778         if (ret)
2779                 return ret;
2780
2781         spin_lock_irqsave(&pp->lock, flags);
2782         add_wait_queue(&pp->wait, &wait);
2783         current->state = TASK_INTERRUPTIBLE;
2784
2785         for (;;) {
2786                 ret = -EAGAIN;
2787                 if (pp->rb_get != pp->rb_put) {
2788                         int i = pp->rb_get;
2789                         struct rb_entry *rp = &pp->rb_buf[i];
2790                         ret = rp->len;
2791                         spin_unlock_irqrestore(&pp->lock, flags);
2792                         if (ret > count)
2793                                 ret = count;
2794                         if (ret > 0 && copy_to_user(buf, rp->data, ret))
2795                                 ret = -EFAULT;
2796                         if (++i >= RB_SIZE)
2797                                 i = 0;
2798                         spin_lock_irqsave(&pp->lock, flags);
2799                         pp->rb_get = i;
2800                 }
2801                 if (ret >= 0)
2802                         break;
2803                 if (file->f_flags & O_NONBLOCK)
2804                         break;
2805                 ret = -ERESTARTSYS;
2806                 if (signal_pending(current))
2807                         break;
2808                 spin_unlock_irqrestore(&pp->lock, flags);
2809                 schedule();
2810                 spin_lock_irqsave(&pp->lock, flags);
2811         }
2812         current->state = TASK_RUNNING;
2813         remove_wait_queue(&pp->wait, &wait);
2814         spin_unlock_irqrestore(&pp->lock, flags);
2815         
2816         return ret;
2817 }
2818
2819 static ssize_t __pmac
2820 pmu_write(struct file *file, const char __user *buf,
2821                          size_t count, loff_t *ppos)
2822 {
2823         return 0;
2824 }
2825
2826 static unsigned int __pmac
2827 pmu_fpoll(struct file *filp, poll_table *wait)
2828 {
2829         struct pmu_private *pp = filp->private_data;
2830         unsigned int mask = 0;
2831         unsigned long flags;
2832         
2833         if (pp == 0)
2834                 return 0;
2835         poll_wait(filp, &pp->wait, wait);
2836         spin_lock_irqsave(&pp->lock, flags);
2837         if (pp->rb_get != pp->rb_put)
2838                 mask |= POLLIN;
2839         spin_unlock_irqrestore(&pp->lock, flags);
2840         return mask;
2841 }
2842
2843 static int __pmac
2844 pmu_release(struct inode *inode, struct file *file)
2845 {
2846         struct pmu_private *pp = file->private_data;
2847         unsigned long flags;
2848
2849         lock_kernel();
2850         if (pp != 0) {
2851                 file->private_data = NULL;
2852                 spin_lock_irqsave(&all_pvt_lock, flags);
2853                 list_del(&pp->list);
2854                 spin_unlock_irqrestore(&all_pvt_lock, flags);
2855 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2856                 if (pp->backlight_locker) {
2857                         spin_lock_irqsave(&pmu_lock, flags);
2858                         disable_kernel_backlight--;
2859                         spin_unlock_irqrestore(&pmu_lock, flags);
2860                 }
2861 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2862                 kfree(pp);
2863         }
2864         unlock_kernel();
2865         return 0;
2866 }
2867
2868 /* Note: removed __openfirmware here since it causes link errors */
2869 static int __pmac
2870 pmu_ioctl(struct inode * inode, struct file *filp,
2871                      u_int cmd, u_long arg)
2872 {
2873         struct pmu_private *pp = filp->private_data;
2874         __u32 __user *argp = (__u32 __user *)arg;
2875         int error;
2876
2877         switch (cmd) {
2878         case PMU_IOC_SLEEP:
2879                 if (!capable(CAP_SYS_ADMIN))
2880                         return -EACCES;
2881                 if (sleep_in_progress)
2882                         return -EBUSY;
2883                 sleep_in_progress = 1;
2884                 switch (pmu_kind) {
2885                 case PMU_OHARE_BASED:
2886                         error = powerbook_sleep_3400();
2887                         break;
2888                 case PMU_HEATHROW_BASED:
2889                 case PMU_PADDINGTON_BASED:
2890                         error = powerbook_sleep_grackle();
2891                         break;
2892                 case PMU_KEYLARGO_BASED:
2893                         error = powerbook_sleep_Core99();
2894                         break;
2895                 default:
2896                         error = -ENOSYS;
2897                 }
2898                 sleep_in_progress = 0;
2899                 return error;
2900         case PMU_IOC_CAN_SLEEP:
2901                 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2902                         return put_user(0, argp);
2903                 else
2904                         return put_user(1, argp);
2905
2906 #ifdef CONFIG_PMAC_BACKLIGHT
2907         /* Backlight should have its own device or go via
2908          * the fbdev
2909          */
2910         case PMU_IOC_GET_BACKLIGHT:
2911                 if (sleep_in_progress)
2912                         return -EBUSY;
2913                 error = get_backlight_level();
2914                 if (error < 0)
2915                         return error;
2916                 return put_user(error, argp);
2917         case PMU_IOC_SET_BACKLIGHT:
2918         {
2919                 __u32 value;
2920                 if (sleep_in_progress)
2921                         return -EBUSY;
2922                 error = get_user(value, argp);
2923                 if (!error)
2924                         error = set_backlight_level(value);
2925                 return error;
2926         }
2927 #ifdef CONFIG_INPUT_ADBHID
2928         case PMU_IOC_GRAB_BACKLIGHT: {
2929                 unsigned long flags;
2930                 if (pp->backlight_locker)
2931                         return 0;
2932                 pp->backlight_locker = 1;
2933                 spin_lock_irqsave(&pmu_lock, flags);
2934                 disable_kernel_backlight++;
2935                 spin_unlock_irqrestore(&pmu_lock, flags);
2936                 return 0;
2937         }
2938 #endif /* CONFIG_INPUT_ADBHID */
2939 #endif /* CONFIG_PMAC_BACKLIGHT */
2940         case PMU_IOC_GET_MODEL:
2941                 return put_user(pmu_kind, argp);
2942         case PMU_IOC_HAS_ADB:
2943                 return put_user(pmu_has_adb, argp);
2944         }
2945         return -EINVAL;
2946 }
2947
2948 static struct file_operations pmu_device_fops __pmacdata = {
2949         .read           = pmu_read,
2950         .write          = pmu_write,
2951         .poll           = pmu_fpoll,
2952         .ioctl          = pmu_ioctl,
2953         .open           = pmu_open,
2954         .release        = pmu_release,
2955 };
2956
2957 static struct miscdevice pmu_device __pmacdata = {
2958         PMU_MINOR, "pmu", &pmu_device_fops
2959 };
2960
2961 void pmu_device_init(void)
2962 {
2963         if (!via)
2964                 return;
2965         if (misc_register(&pmu_device) < 0)
2966                 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2967 }
2968 #endif /* CONFIG_PMAC_PBOOK */
2969
2970 #ifdef DEBUG_SLEEP
2971 static inline void  __pmac
2972 polled_handshake(volatile unsigned char __iomem *via)
2973 {
2974         via[B] &= ~TREQ; eieio();
2975         while ((via[B] & TACK) != 0)
2976                 ;
2977         via[B] |= TREQ; eieio();
2978         while ((via[B] & TACK) == 0)
2979                 ;
2980 }
2981
2982 static inline void  __pmac
2983 polled_send_byte(volatile unsigned char __iomem *via, int x)
2984 {
2985         via[ACR] |= SR_OUT | SR_EXT; eieio();
2986         via[SR] = x; eieio();
2987         polled_handshake(via);
2988 }
2989
2990 static inline int __pmac
2991 polled_recv_byte(volatile unsigned char __iomem *via)
2992 {
2993         int x;
2994
2995         via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2996         x = via[SR]; eieio();
2997         polled_handshake(via);
2998         x = via[SR]; eieio();
2999         return x;
3000 }
3001
3002 int __pmac
3003 pmu_polled_request(struct adb_request *req)
3004 {
3005         unsigned long flags;
3006         int i, l, c;
3007         volatile unsigned char __iomem *v = via;
3008
3009         req->complete = 1;
3010         c = req->data[0];
3011         l = pmu_data_len[c][0];
3012         if (l >= 0 && req->nbytes != l + 1)
3013                 return -EINVAL;
3014
3015         local_irq_save(flags);
3016         while (pmu_state != idle)
3017                 pmu_poll();
3018
3019         while ((via[B] & TACK) == 0)
3020                 ;
3021         polled_send_byte(v, c);
3022         if (l < 0) {
3023                 l = req->nbytes - 1;
3024                 polled_send_byte(v, l);
3025         }
3026         for (i = 1; i <= l; ++i)
3027                 polled_send_byte(v, req->data[i]);
3028
3029         l = pmu_data_len[c][1];
3030         if (l < 0)
3031                 l = polled_recv_byte(v);
3032         for (i = 0; i < l; ++i)
3033                 req->reply[i + req->reply_len] = polled_recv_byte(v);
3034
3035         if (req->done)
3036                 (*req->done)(req);
3037
3038         local_irq_restore(flags);
3039         return 0;
3040 }
3041 #endif /* DEBUG_SLEEP */
3042
3043 EXPORT_SYMBOL(pmu_request);
3044 EXPORT_SYMBOL(pmu_poll);
3045 EXPORT_SYMBOL(pmu_poll_adb);
3046 EXPORT_SYMBOL(pmu_wait_complete);
3047 EXPORT_SYMBOL(pmu_suspend);
3048 EXPORT_SYMBOL(pmu_resume);
3049 EXPORT_SYMBOL(pmu_unlock);
3050 EXPORT_SYMBOL(pmu_i2c_combined_read);
3051 EXPORT_SYMBOL(pmu_i2c_stdsub_write);
3052 EXPORT_SYMBOL(pmu_i2c_simple_read);
3053 EXPORT_SYMBOL(pmu_i2c_simple_write);
3054 #ifdef CONFIG_PMAC_PBOOK
3055 EXPORT_SYMBOL(pmu_register_sleep_notifier);
3056 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
3057 EXPORT_SYMBOL(pmu_enable_irled);
3058 EXPORT_SYMBOL(pmu_battery_count);
3059 EXPORT_SYMBOL(pmu_batteries);
3060 EXPORT_SYMBOL(pmu_power_flags);
3061 #endif /* CONFIG_PMAC_PBOOK */
3062