remove dead Radeon URL
[linux-2.6/linux-loongson.git] / drivers / macintosh / via-pmu.c
blob4a478eb0e27dabfa73b169072090abadfea2e143
1 /*
2 * Device driver for the via-pmu on Apple Powermacs.
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.
11 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
12 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
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
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/sysdev.h>
47 #include <linux/suspend.h>
48 #include <linux/syscalls.h>
49 #include <linux/cpu.h>
50 #include <asm/prom.h>
51 #include <asm/machdep.h>
52 #include <asm/io.h>
53 #include <asm/pgtable.h>
54 #include <asm/system.h>
55 #include <asm/sections.h>
56 #include <asm/irq.h>
57 #include <asm/pmac_feature.h>
58 #include <asm/pmac_pfunc.h>
59 #include <asm/pmac_low_i2c.h>
60 #include <asm/uaccess.h>
61 #include <asm/mmu_context.h>
62 #include <asm/cputable.h>
63 #include <asm/time.h>
64 #ifdef CONFIG_PMAC_BACKLIGHT
65 #include <asm/backlight.h>
66 #endif
68 #ifdef CONFIG_PPC32
69 #include <asm/open_pic.h>
70 #endif
72 /* Some compile options */
73 #undef SUSPEND_USES_PMU
74 #define DEBUG_SLEEP
75 #undef HACKED_PCI_SAVE
77 /* Misc minor number allocated for /dev/pmu */
78 #define PMU_MINOR 154
80 /* How many iterations between battery polls */
81 #define BATTERY_POLLING_COUNT 2
83 static volatile unsigned char __iomem *via;
85 /* VIA registers - spaced 0x200 bytes apart */
86 #define RS 0x200 /* skip between registers */
87 #define B 0 /* B-side data */
88 #define A RS /* A-side data */
89 #define DIRB (2*RS) /* B-side direction (1=output) */
90 #define DIRA (3*RS) /* A-side direction (1=output) */
91 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
92 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
93 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
94 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
95 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
96 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
97 #define SR (10*RS) /* Shift register */
98 #define ACR (11*RS) /* Auxiliary control register */
99 #define PCR (12*RS) /* Peripheral control register */
100 #define IFR (13*RS) /* Interrupt flag register */
101 #define IER (14*RS) /* Interrupt enable register */
102 #define ANH (15*RS) /* A-side data, no handshake */
104 /* Bits in B data register: both active low */
105 #define TACK 0x08 /* Transfer acknowledge (input) */
106 #define TREQ 0x10 /* Transfer request (output) */
108 /* Bits in ACR */
109 #define SR_CTRL 0x1c /* Shift register control bits */
110 #define SR_EXT 0x0c /* Shift on external clock */
111 #define SR_OUT 0x10 /* Shift out if 1 */
113 /* Bits in IFR and IER */
114 #define IER_SET 0x80 /* set bits in IER */
115 #define IER_CLR 0 /* clear bits in IER */
116 #define SR_INT 0x04 /* Shift register full/empty */
117 #define CB2_INT 0x08
118 #define CB1_INT 0x10 /* transition on CB1 input */
120 static volatile enum pmu_state {
121 idle,
122 sending,
123 intack,
124 reading,
125 reading_intr,
126 locked,
127 } pmu_state;
129 static volatile enum int_data_state {
130 int_data_empty,
131 int_data_fill,
132 int_data_ready,
133 int_data_flush
134 } int_data_state[2] = { int_data_empty, int_data_empty };
136 static struct adb_request *current_req;
137 static struct adb_request *last_req;
138 static struct adb_request *req_awaiting_reply;
139 static unsigned char interrupt_data[2][32];
140 static int interrupt_data_len[2];
141 static int int_data_last;
142 static unsigned char *reply_ptr;
143 static int data_index;
144 static int data_len;
145 static volatile int adb_int_pending;
146 static volatile int disable_poll;
147 static struct adb_request bright_req_1, bright_req_2;
148 static struct device_node *vias;
149 static int pmu_kind = PMU_UNKNOWN;
150 static int pmu_fully_inited = 0;
151 static int pmu_has_adb;
152 static struct device_node *gpio_node;
153 static unsigned char __iomem *gpio_reg = NULL;
154 static int gpio_irq = -1;
155 static int gpio_irq_enabled = -1;
156 static volatile int pmu_suspended = 0;
157 static spinlock_t pmu_lock;
158 static u8 pmu_intr_mask;
159 static int pmu_version;
160 static int drop_interrupts;
161 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
162 static int option_lid_wakeup = 1;
163 #endif /* CONFIG_PM && CONFIG_PPC32 */
164 static int sleep_in_progress;
165 static unsigned long async_req_locks;
166 static unsigned int pmu_irq_stats[11];
168 static struct proc_dir_entry *proc_pmu_root;
169 static struct proc_dir_entry *proc_pmu_info;
170 static struct proc_dir_entry *proc_pmu_irqstats;
171 static struct proc_dir_entry *proc_pmu_options;
172 static int option_server_mode;
174 int pmu_battery_count;
175 int pmu_cur_battery;
176 unsigned int pmu_power_flags;
177 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
178 static int query_batt_timer = BATTERY_POLLING_COUNT;
179 static struct adb_request batt_req;
180 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
182 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
183 extern int disable_kernel_backlight;
184 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
186 int __fake_sleep;
187 int asleep;
188 struct notifier_block *sleep_notifier_list;
190 #ifdef CONFIG_ADB
191 static int adb_dev_map = 0;
192 static int pmu_adb_flags;
194 static int pmu_probe(void);
195 static int pmu_init(void);
196 static int pmu_send_request(struct adb_request *req, int sync);
197 static int pmu_adb_autopoll(int devs);
198 static int pmu_adb_reset_bus(void);
199 #endif /* CONFIG_ADB */
201 static int init_pmu(void);
202 static void pmu_start(void);
203 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
204 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
205 static int proc_get_info(char *page, char **start, off_t off,
206 int count, int *eof, void *data);
207 static int proc_get_irqstats(char *page, char **start, off_t off,
208 int count, int *eof, void *data);
209 #ifdef CONFIG_PMAC_BACKLIGHT
210 static int pmu_set_backlight_level(int level, void* data);
211 static int pmu_set_backlight_enable(int on, int level, void* data);
212 #endif /* CONFIG_PMAC_BACKLIGHT */
213 static void pmu_pass_intr(unsigned char *data, int len);
214 static int proc_get_batt(char *page, char **start, off_t off,
215 int count, int *eof, void *data);
216 static int proc_read_options(char *page, char **start, off_t off,
217 int count, int *eof, void *data);
218 static int proc_write_options(struct file *file, const char __user *buffer,
219 unsigned long count, void *data);
221 #ifdef CONFIG_ADB
222 struct adb_driver via_pmu_driver = {
223 "PMU",
224 pmu_probe,
225 pmu_init,
226 pmu_send_request,
227 pmu_adb_autopoll,
228 pmu_poll_adb,
229 pmu_adb_reset_bus
231 #endif /* CONFIG_ADB */
233 extern void low_sleep_handler(void);
234 extern void enable_kernel_altivec(void);
235 extern void enable_kernel_fp(void);
237 #ifdef DEBUG_SLEEP
238 int pmu_polled_request(struct adb_request *req);
239 int pmu_wink(struct adb_request *req);
240 #endif
243 * This table indicates for each PMU opcode:
244 * - the number of data bytes to be sent with the command, or -1
245 * if a length byte should be sent,
246 * - the number of response bytes which the PMU will return, or
247 * -1 if it will send a length byte.
249 static const s8 pmu_data_len[256][2] = {
250 /* 0 1 2 3 4 5 6 7 */
251 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
253 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
255 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
257 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
259 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
260 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
261 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
262 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
263 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
264 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
265 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
267 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
268 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
269 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
270 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
272 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
273 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
274 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
275 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
276 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
277 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
278 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
279 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
280 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
281 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
282 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
285 static char *pbook_type[] = {
286 "Unknown PowerBook",
287 "PowerBook 2400/3400/3500(G3)",
288 "PowerBook G3 Series",
289 "1999 PowerBook G3",
290 "Core99"
293 #ifdef CONFIG_PMAC_BACKLIGHT
294 static struct backlight_controller pmu_backlight_controller = {
295 pmu_set_backlight_enable,
296 pmu_set_backlight_level
298 #endif /* CONFIG_PMAC_BACKLIGHT */
300 int __init find_via_pmu(void)
302 u64 taddr;
303 u32 *reg;
305 if (via != 0)
306 return 1;
307 vias = of_find_node_by_name(NULL, "via-pmu");
308 if (vias == NULL)
309 return 0;
311 reg = (u32 *)get_property(vias, "reg", NULL);
312 if (reg == NULL) {
313 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
314 goto fail;
316 taddr = of_translate_address(vias, reg);
317 if (taddr == OF_BAD_ADDR) {
318 printk(KERN_ERR "via-pmu: Can't translate address !\n");
319 goto fail;
322 spin_lock_init(&pmu_lock);
324 pmu_has_adb = 1;
326 pmu_intr_mask = PMU_INT_PCEJECT |
327 PMU_INT_SNDBRT |
328 PMU_INT_ADB |
329 PMU_INT_TICK;
331 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
332 || device_is_compatible(vias->parent, "ohare")))
333 pmu_kind = PMU_OHARE_BASED;
334 else if (device_is_compatible(vias->parent, "paddington"))
335 pmu_kind = PMU_PADDINGTON_BASED;
336 else if (device_is_compatible(vias->parent, "heathrow"))
337 pmu_kind = PMU_HEATHROW_BASED;
338 else if (device_is_compatible(vias->parent, "Keylargo")
339 || device_is_compatible(vias->parent, "K2-Keylargo")) {
340 struct device_node *gpiop;
341 u64 gaddr = OF_BAD_ADDR;
343 pmu_kind = PMU_KEYLARGO_BASED;
344 pmu_has_adb = (find_type_devices("adb") != NULL);
345 pmu_intr_mask = PMU_INT_PCEJECT |
346 PMU_INT_SNDBRT |
347 PMU_INT_ADB |
348 PMU_INT_TICK |
349 PMU_INT_ENVIRONMENT;
351 gpiop = of_find_node_by_name(NULL, "gpio");
352 if (gpiop) {
353 reg = (u32 *)get_property(gpiop, "reg", NULL);
354 if (reg)
355 gaddr = of_translate_address(gpiop, reg);
356 if (gaddr != OF_BAD_ADDR)
357 gpio_reg = ioremap(gaddr, 0x10);
359 if (gpio_reg == NULL)
360 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
361 } else
362 pmu_kind = PMU_UNKNOWN;
364 via = ioremap(taddr, 0x2000);
365 if (via == NULL) {
366 printk(KERN_ERR "via-pmu: Can't map address !\n");
367 goto fail;
370 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
371 out_8(&via[IFR], 0x7f); /* clear IFR */
373 pmu_state = idle;
375 if (!init_pmu()) {
376 via = NULL;
377 return 0;
380 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
381 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
383 sys_ctrler = SYS_CTRLER_PMU;
385 return 1;
386 fail:
387 of_node_put(vias);
388 vias = NULL;
389 return 0;
392 #ifdef CONFIG_ADB
393 static int pmu_probe(void)
395 return vias == NULL? -ENODEV: 0;
398 static int __init pmu_init(void)
400 if (vias == NULL)
401 return -ENODEV;
402 return 0;
404 #endif /* CONFIG_ADB */
407 * We can't wait until pmu_init gets called, that happens too late.
408 * It happens after IDE and SCSI initialization, which can take a few
409 * seconds, and by that time the PMU could have given up on us and
410 * turned us off.
411 * Thus this is called with arch_initcall rather than device_initcall.
413 static int __init via_pmu_start(void)
415 if (vias == NULL)
416 return -ENODEV;
418 bright_req_1.complete = 1;
419 bright_req_2.complete = 1;
420 batt_req.complete = 1;
422 #ifndef CONFIG_PPC_MERGE
423 if (pmu_kind == PMU_KEYLARGO_BASED)
424 openpic_set_irq_priority(vias->intrs[0].line,
425 OPENPIC_PRIORITY_DEFAULT + 1);
426 #endif
428 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
429 (void *)0)) {
430 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
431 vias->intrs[0].line);
432 return -EAGAIN;
435 if (pmu_kind == PMU_KEYLARGO_BASED) {
436 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
437 if (gpio_node == NULL)
438 gpio_node = of_find_node_by_name(NULL,
439 "pmu-interrupt");
440 if (gpio_node && gpio_node->n_intrs > 0)
441 gpio_irq = gpio_node->intrs[0].line;
443 if (gpio_irq != -1) {
444 if (request_irq(gpio_irq, gpio1_interrupt, 0,
445 "GPIO1 ADB", (void *)0))
446 printk(KERN_ERR "pmu: can't get irq %d"
447 " (GPIO1)\n", gpio_irq);
448 else
449 gpio_irq_enabled = 1;
453 /* Enable interrupts */
454 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
456 pmu_fully_inited = 1;
458 /* Make sure PMU settle down before continuing. This is _very_ important
459 * since the IDE probe may shut interrupts down for quite a bit of time. If
460 * a PMU communication is pending while this happens, the PMU may timeout
461 * Not that on Core99 machines, the PMU keeps sending us environement
462 * messages, we should find a way to either fix IDE or make it call
463 * pmu_suspend() before masking interrupts. This can also happens while
464 * scolling with some fbdevs.
466 do {
467 pmu_poll();
468 } while (pmu_state != idle);
470 return 0;
473 arch_initcall(via_pmu_start);
476 * This has to be done after pci_init, which is a subsys_initcall.
478 static int __init via_pmu_dev_init(void)
480 if (vias == NULL)
481 return -ENODEV;
483 #ifdef CONFIG_PMAC_BACKLIGHT
484 /* Enable backlight */
485 register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
486 #endif /* CONFIG_PMAC_BACKLIGHT */
488 #ifdef CONFIG_PPC32
489 if (machine_is_compatible("AAPL,3400/2400") ||
490 machine_is_compatible("AAPL,3500")) {
491 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
492 NULL, PMAC_MB_INFO_MODEL, 0);
493 pmu_battery_count = 1;
494 if (mb == PMAC_TYPE_COMET)
495 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
496 else
497 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
498 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
499 machine_is_compatible("PowerBook1,1")) {
500 pmu_battery_count = 2;
501 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
502 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
503 } else {
504 struct device_node* prim = find_devices("power-mgt");
505 u32 *prim_info = NULL;
506 if (prim)
507 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
508 if (prim_info) {
509 /* Other stuffs here yet unknown */
510 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
511 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
512 if (pmu_battery_count > 1)
513 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
516 #endif /* CONFIG_PPC32 */
518 /* Create /proc/pmu */
519 proc_pmu_root = proc_mkdir("pmu", NULL);
520 if (proc_pmu_root) {
521 long i;
523 for (i=0; i<pmu_battery_count; i++) {
524 char title[16];
525 sprintf(title, "battery_%ld", i);
526 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
527 proc_get_batt, (void *)i);
530 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
531 proc_get_info, NULL);
532 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
533 proc_get_irqstats, NULL);
534 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
535 if (proc_pmu_options) {
536 proc_pmu_options->nlink = 1;
537 proc_pmu_options->read_proc = proc_read_options;
538 proc_pmu_options->write_proc = proc_write_options;
541 return 0;
544 device_initcall(via_pmu_dev_init);
546 static int
547 init_pmu(void)
549 int timeout;
550 struct adb_request req;
552 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
553 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
555 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
556 timeout = 100000;
557 while (!req.complete) {
558 if (--timeout < 0) {
559 printk(KERN_ERR "init_pmu: no response from PMU\n");
560 return 0;
562 udelay(10);
563 pmu_poll();
566 /* ack all pending interrupts */
567 timeout = 100000;
568 interrupt_data[0][0] = 1;
569 while (interrupt_data[0][0] || pmu_state != idle) {
570 if (--timeout < 0) {
571 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
572 return 0;
574 if (pmu_state == idle)
575 adb_int_pending = 1;
576 via_pmu_interrupt(0, NULL, NULL);
577 udelay(10);
580 /* Tell PMU we are ready. */
581 if (pmu_kind == PMU_KEYLARGO_BASED) {
582 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
583 while (!req.complete)
584 pmu_poll();
587 /* Read PMU version */
588 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
589 pmu_wait_complete(&req);
590 if (req.reply_len > 0)
591 pmu_version = req.reply[0];
593 /* Read server mode setting */
594 if (pmu_kind == PMU_KEYLARGO_BASED) {
595 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
596 PMU_PWR_GET_POWERUP_EVENTS);
597 pmu_wait_complete(&req);
598 if (req.reply_len == 2) {
599 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
600 option_server_mode = 1;
601 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
602 option_server_mode ? "enabled" : "disabled");
605 return 1;
609 pmu_get_model(void)
611 return pmu_kind;
614 static void pmu_set_server_mode(int server_mode)
616 struct adb_request req;
618 if (pmu_kind != PMU_KEYLARGO_BASED)
619 return;
621 option_server_mode = server_mode;
622 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
623 pmu_wait_complete(&req);
624 if (req.reply_len < 2)
625 return;
626 if (server_mode)
627 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
628 PMU_PWR_SET_POWERUP_EVENTS,
629 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
630 else
631 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
632 PMU_PWR_CLR_POWERUP_EVENTS,
633 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
634 pmu_wait_complete(&req);
637 /* This new version of the code for 2400/3400/3500 powerbooks
638 * is inspired from the implementation in gkrellm-pmu
640 static void
641 done_battery_state_ohare(struct adb_request* req)
643 /* format:
644 * [0] : flags
645 * 0x01 : AC indicator
646 * 0x02 : charging
647 * 0x04 : battery exist
648 * 0x08 :
649 * 0x10 :
650 * 0x20 : full charged
651 * 0x40 : pcharge reset
652 * 0x80 : battery exist
654 * [1][2] : battery voltage
655 * [3] : CPU temperature
656 * [4] : battery temperature
657 * [5] : current
658 * [6][7] : pcharge
659 * --tkoba
661 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
662 long pcharge, charge, vb, vmax, lmax;
663 long vmax_charging, vmax_charged;
664 long amperage, voltage, time, max;
665 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
666 NULL, PMAC_MB_INFO_MODEL, 0);
668 if (req->reply[0] & 0x01)
669 pmu_power_flags |= PMU_PWR_AC_PRESENT;
670 else
671 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
673 if (mb == PMAC_TYPE_COMET) {
674 vmax_charged = 189;
675 vmax_charging = 213;
676 lmax = 6500;
677 } else {
678 vmax_charged = 330;
679 vmax_charging = 330;
680 lmax = 6500;
682 vmax = vmax_charged;
684 /* If battery installed */
685 if (req->reply[0] & 0x04) {
686 bat_flags |= PMU_BATT_PRESENT;
687 if (req->reply[0] & 0x02)
688 bat_flags |= PMU_BATT_CHARGING;
689 vb = (req->reply[1] << 8) | req->reply[2];
690 voltage = (vb * 265 + 72665) / 10;
691 amperage = req->reply[5];
692 if ((req->reply[0] & 0x01) == 0) {
693 if (amperage > 200)
694 vb += ((amperage - 200) * 15)/100;
695 } else if (req->reply[0] & 0x02) {
696 vb = (vb * 97) / 100;
697 vmax = vmax_charging;
699 charge = (100 * vb) / vmax;
700 if (req->reply[0] & 0x40) {
701 pcharge = (req->reply[6] << 8) + req->reply[7];
702 if (pcharge > lmax)
703 pcharge = lmax;
704 pcharge *= 100;
705 pcharge = 100 - pcharge / lmax;
706 if (pcharge < charge)
707 charge = pcharge;
709 if (amperage > 0)
710 time = (charge * 16440) / amperage;
711 else
712 time = 0;
713 max = 100;
714 amperage = -amperage;
715 } else
716 charge = max = amperage = voltage = time = 0;
718 pmu_batteries[pmu_cur_battery].flags = bat_flags;
719 pmu_batteries[pmu_cur_battery].charge = charge;
720 pmu_batteries[pmu_cur_battery].max_charge = max;
721 pmu_batteries[pmu_cur_battery].amperage = amperage;
722 pmu_batteries[pmu_cur_battery].voltage = voltage;
723 pmu_batteries[pmu_cur_battery].time_remaining = time;
725 clear_bit(0, &async_req_locks);
728 static void
729 done_battery_state_smart(struct adb_request* req)
731 /* format:
732 * [0] : format of this structure (known: 3,4,5)
733 * [1] : flags
735 * format 3 & 4:
737 * [2] : charge
738 * [3] : max charge
739 * [4] : current
740 * [5] : voltage
742 * format 5:
744 * [2][3] : charge
745 * [4][5] : max charge
746 * [6][7] : current
747 * [8][9] : voltage
750 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
751 int amperage;
752 unsigned int capa, max, voltage;
754 if (req->reply[1] & 0x01)
755 pmu_power_flags |= PMU_PWR_AC_PRESENT;
756 else
757 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
760 capa = max = amperage = voltage = 0;
762 if (req->reply[1] & 0x04) {
763 bat_flags |= PMU_BATT_PRESENT;
764 switch(req->reply[0]) {
765 case 3:
766 case 4: capa = req->reply[2];
767 max = req->reply[3];
768 amperage = *((signed char *)&req->reply[4]);
769 voltage = req->reply[5];
770 break;
771 case 5: capa = (req->reply[2] << 8) | req->reply[3];
772 max = (req->reply[4] << 8) | req->reply[5];
773 amperage = *((signed short *)&req->reply[6]);
774 voltage = (req->reply[8] << 8) | req->reply[9];
775 break;
776 default:
777 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
778 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
779 break;
783 if ((req->reply[1] & 0x01) && (amperage > 0))
784 bat_flags |= PMU_BATT_CHARGING;
786 pmu_batteries[pmu_cur_battery].flags = bat_flags;
787 pmu_batteries[pmu_cur_battery].charge = capa;
788 pmu_batteries[pmu_cur_battery].max_charge = max;
789 pmu_batteries[pmu_cur_battery].amperage = amperage;
790 pmu_batteries[pmu_cur_battery].voltage = voltage;
791 if (amperage) {
792 if ((req->reply[1] & 0x01) && (amperage > 0))
793 pmu_batteries[pmu_cur_battery].time_remaining
794 = ((max-capa) * 3600) / amperage;
795 else
796 pmu_batteries[pmu_cur_battery].time_remaining
797 = (capa * 3600) / (-amperage);
798 } else
799 pmu_batteries[pmu_cur_battery].time_remaining = 0;
801 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
803 clear_bit(0, &async_req_locks);
806 static void
807 query_battery_state(void)
809 if (test_and_set_bit(0, &async_req_locks))
810 return;
811 if (pmu_kind == PMU_OHARE_BASED)
812 pmu_request(&batt_req, done_battery_state_ohare,
813 1, PMU_BATTERY_STATE);
814 else
815 pmu_request(&batt_req, done_battery_state_smart,
816 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
819 static int
820 proc_get_info(char *page, char **start, off_t off,
821 int count, int *eof, void *data)
823 char* p = page;
825 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
826 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
827 p += sprintf(p, "AC Power : %d\n",
828 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
829 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
831 return p - page;
834 static int
835 proc_get_irqstats(char *page, char **start, off_t off,
836 int count, int *eof, void *data)
838 int i;
839 char* p = page;
840 static const char *irq_names[] = {
841 "Total CB1 triggered events",
842 "Total GPIO1 triggered events",
843 "PC-Card eject button",
844 "Sound/Brightness button",
845 "ADB message",
846 "Battery state change",
847 "Environment interrupt",
848 "Tick timer",
849 "Ghost interrupt (zero len)",
850 "Empty interrupt (empty mask)",
851 "Max irqs in a row"
854 for (i=0; i<11; i++) {
855 p += sprintf(p, " %2u: %10u (%s)\n",
856 i, pmu_irq_stats[i], irq_names[i]);
858 return p - page;
861 static int
862 proc_get_batt(char *page, char **start, off_t off,
863 int count, int *eof, void *data)
865 long batnum = (long)data;
866 char *p = page;
868 p += sprintf(p, "\n");
869 p += sprintf(p, "flags : %08x\n",
870 pmu_batteries[batnum].flags);
871 p += sprintf(p, "charge : %d\n",
872 pmu_batteries[batnum].charge);
873 p += sprintf(p, "max_charge : %d\n",
874 pmu_batteries[batnum].max_charge);
875 p += sprintf(p, "current : %d\n",
876 pmu_batteries[batnum].amperage);
877 p += sprintf(p, "voltage : %d\n",
878 pmu_batteries[batnum].voltage);
879 p += sprintf(p, "time rem. : %d\n",
880 pmu_batteries[batnum].time_remaining);
882 return p - page;
885 static int
886 proc_read_options(char *page, char **start, off_t off,
887 int count, int *eof, void *data)
889 char *p = page;
891 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
892 if (pmu_kind == PMU_KEYLARGO_BASED &&
893 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
894 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
895 #endif
896 if (pmu_kind == PMU_KEYLARGO_BASED)
897 p += sprintf(p, "server_mode=%d\n", option_server_mode);
899 return p - page;
902 static int
903 proc_write_options(struct file *file, const char __user *buffer,
904 unsigned long count, void *data)
906 char tmp[33];
907 char *label, *val;
908 unsigned long fcount = count;
910 if (!count)
911 return -EINVAL;
912 if (count > 32)
913 count = 32;
914 if (copy_from_user(tmp, buffer, count))
915 return -EFAULT;
916 tmp[count] = 0;
918 label = tmp;
919 while(*label == ' ')
920 label++;
921 val = label;
922 while(*val && (*val != '=')) {
923 if (*val == ' ')
924 *val = 0;
925 val++;
927 if ((*val) == 0)
928 return -EINVAL;
929 *(val++) = 0;
930 while(*val == ' ')
931 val++;
932 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
933 if (pmu_kind == PMU_KEYLARGO_BASED &&
934 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
935 if (!strcmp(label, "lid_wakeup"))
936 option_lid_wakeup = ((*val) == '1');
937 #endif
938 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
939 int new_value;
940 new_value = ((*val) == '1');
941 if (new_value != option_server_mode)
942 pmu_set_server_mode(new_value);
944 return fcount;
947 #ifdef CONFIG_ADB
948 /* Send an ADB command */
949 static int
950 pmu_send_request(struct adb_request *req, int sync)
952 int i, ret;
954 if ((vias == NULL) || (!pmu_fully_inited)) {
955 req->complete = 1;
956 return -ENXIO;
959 ret = -EINVAL;
961 switch (req->data[0]) {
962 case PMU_PACKET:
963 for (i = 0; i < req->nbytes - 1; ++i)
964 req->data[i] = req->data[i+1];
965 --req->nbytes;
966 if (pmu_data_len[req->data[0]][1] != 0) {
967 req->reply[0] = ADB_RET_OK;
968 req->reply_len = 1;
969 } else
970 req->reply_len = 0;
971 ret = pmu_queue_request(req);
972 break;
973 case CUDA_PACKET:
974 switch (req->data[1]) {
975 case CUDA_GET_TIME:
976 if (req->nbytes != 2)
977 break;
978 req->data[0] = PMU_READ_RTC;
979 req->nbytes = 1;
980 req->reply_len = 3;
981 req->reply[0] = CUDA_PACKET;
982 req->reply[1] = 0;
983 req->reply[2] = CUDA_GET_TIME;
984 ret = pmu_queue_request(req);
985 break;
986 case CUDA_SET_TIME:
987 if (req->nbytes != 6)
988 break;
989 req->data[0] = PMU_SET_RTC;
990 req->nbytes = 5;
991 for (i = 1; i <= 4; ++i)
992 req->data[i] = req->data[i+1];
993 req->reply_len = 3;
994 req->reply[0] = CUDA_PACKET;
995 req->reply[1] = 0;
996 req->reply[2] = CUDA_SET_TIME;
997 ret = pmu_queue_request(req);
998 break;
1000 break;
1001 case ADB_PACKET:
1002 if (!pmu_has_adb)
1003 return -ENXIO;
1004 for (i = req->nbytes - 1; i > 1; --i)
1005 req->data[i+2] = req->data[i];
1006 req->data[3] = req->nbytes - 2;
1007 req->data[2] = pmu_adb_flags;
1008 /*req->data[1] = req->data[1];*/
1009 req->data[0] = PMU_ADB_CMD;
1010 req->nbytes += 2;
1011 req->reply_expected = 1;
1012 req->reply_len = 0;
1013 ret = pmu_queue_request(req);
1014 break;
1016 if (ret) {
1017 req->complete = 1;
1018 return ret;
1021 if (sync)
1022 while (!req->complete)
1023 pmu_poll();
1025 return 0;
1028 /* Enable/disable autopolling */
1029 static int
1030 pmu_adb_autopoll(int devs)
1032 struct adb_request req;
1034 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1035 return -ENXIO;
1037 if (devs) {
1038 adb_dev_map = devs;
1039 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1040 adb_dev_map >> 8, adb_dev_map);
1041 pmu_adb_flags = 2;
1042 } else {
1043 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1044 pmu_adb_flags = 0;
1046 while (!req.complete)
1047 pmu_poll();
1048 return 0;
1051 /* Reset the ADB bus */
1052 static int
1053 pmu_adb_reset_bus(void)
1055 struct adb_request req;
1056 int save_autopoll = adb_dev_map;
1058 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1059 return -ENXIO;
1061 /* anyone got a better idea?? */
1062 pmu_adb_autopoll(0);
1064 req.nbytes = 5;
1065 req.done = NULL;
1066 req.data[0] = PMU_ADB_CMD;
1067 req.data[1] = 0;
1068 req.data[2] = ADB_BUSRESET;
1069 req.data[3] = 0;
1070 req.data[4] = 0;
1071 req.reply_len = 0;
1072 req.reply_expected = 1;
1073 if (pmu_queue_request(&req) != 0) {
1074 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1075 return -EIO;
1077 pmu_wait_complete(&req);
1079 if (save_autopoll != 0)
1080 pmu_adb_autopoll(save_autopoll);
1082 return 0;
1084 #endif /* CONFIG_ADB */
1086 /* Construct and send a pmu request */
1088 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1089 int nbytes, ...)
1091 va_list list;
1092 int i;
1094 if (vias == NULL)
1095 return -ENXIO;
1097 if (nbytes < 0 || nbytes > 32) {
1098 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1099 req->complete = 1;
1100 return -EINVAL;
1102 req->nbytes = nbytes;
1103 req->done = done;
1104 va_start(list, nbytes);
1105 for (i = 0; i < nbytes; ++i)
1106 req->data[i] = va_arg(list, int);
1107 va_end(list);
1108 req->reply_len = 0;
1109 req->reply_expected = 0;
1110 return pmu_queue_request(req);
1114 pmu_queue_request(struct adb_request *req)
1116 unsigned long flags;
1117 int nsend;
1119 if (via == NULL) {
1120 req->complete = 1;
1121 return -ENXIO;
1123 if (req->nbytes <= 0) {
1124 req->complete = 1;
1125 return 0;
1127 nsend = pmu_data_len[req->data[0]][0];
1128 if (nsend >= 0 && req->nbytes != nsend + 1) {
1129 req->complete = 1;
1130 return -EINVAL;
1133 req->next = NULL;
1134 req->sent = 0;
1135 req->complete = 0;
1137 spin_lock_irqsave(&pmu_lock, flags);
1138 if (current_req != 0) {
1139 last_req->next = req;
1140 last_req = req;
1141 } else {
1142 current_req = req;
1143 last_req = req;
1144 if (pmu_state == idle)
1145 pmu_start();
1147 spin_unlock_irqrestore(&pmu_lock, flags);
1149 return 0;
1152 static inline void
1153 wait_for_ack(void)
1155 /* Sightly increased the delay, I had one occurrence of the message
1156 * reported
1158 int timeout = 4000;
1159 while ((in_8(&via[B]) & TACK) == 0) {
1160 if (--timeout < 0) {
1161 printk(KERN_ERR "PMU not responding (!ack)\n");
1162 return;
1164 udelay(10);
1168 /* New PMU seems to be very sensitive to those timings, so we make sure
1169 * PCI is flushed immediately */
1170 static inline void
1171 send_byte(int x)
1173 volatile unsigned char __iomem *v = via;
1175 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1176 out_8(&v[SR], x);
1177 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1178 (void)in_8(&v[B]);
1181 static inline void
1182 recv_byte(void)
1184 volatile unsigned char __iomem *v = via;
1186 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1187 in_8(&v[SR]); /* resets SR */
1188 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1189 (void)in_8(&v[B]);
1192 static inline void
1193 pmu_done(struct adb_request *req)
1195 void (*done)(struct adb_request *) = req->done;
1196 mb();
1197 req->complete = 1;
1198 /* Here, we assume that if the request has a done member, the
1199 * struct request will survive to setting req->complete to 1
1201 if (done)
1202 (*done)(req);
1205 static void
1206 pmu_start(void)
1208 struct adb_request *req;
1210 /* assert pmu_state == idle */
1211 /* get the packet to send */
1212 req = current_req;
1213 if (req == 0 || pmu_state != idle
1214 || (/*req->reply_expected && */req_awaiting_reply))
1215 return;
1217 pmu_state = sending;
1218 data_index = 1;
1219 data_len = pmu_data_len[req->data[0]][0];
1221 /* Sounds safer to make sure ACK is high before writing. This helped
1222 * kill a problem with ADB and some iBooks
1224 wait_for_ack();
1225 /* set the shift register to shift out and send a byte */
1226 send_byte(req->data[0]);
1229 void
1230 pmu_poll(void)
1232 if (!via)
1233 return;
1234 if (disable_poll)
1235 return;
1236 via_pmu_interrupt(0, NULL, NULL);
1239 void
1240 pmu_poll_adb(void)
1242 if (!via)
1243 return;
1244 if (disable_poll)
1245 return;
1246 /* Kicks ADB read when PMU is suspended */
1247 adb_int_pending = 1;
1248 do {
1249 via_pmu_interrupt(0, NULL, NULL);
1250 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1251 || req_awaiting_reply));
1254 void
1255 pmu_wait_complete(struct adb_request *req)
1257 if (!via)
1258 return;
1259 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1260 via_pmu_interrupt(0, NULL, NULL);
1263 /* This function loops until the PMU is idle and prevents it from
1264 * anwsering to ADB interrupts. pmu_request can still be called.
1265 * This is done to avoid spurrious shutdowns when we know we'll have
1266 * interrupts switched off for a long time
1268 void
1269 pmu_suspend(void)
1271 unsigned long flags;
1272 #ifdef SUSPEND_USES_PMU
1273 struct adb_request *req;
1274 #endif
1275 if (!via)
1276 return;
1278 spin_lock_irqsave(&pmu_lock, flags);
1279 pmu_suspended++;
1280 if (pmu_suspended > 1) {
1281 spin_unlock_irqrestore(&pmu_lock, flags);
1282 return;
1285 do {
1286 spin_unlock_irqrestore(&pmu_lock, flags);
1287 if (req_awaiting_reply)
1288 adb_int_pending = 1;
1289 via_pmu_interrupt(0, NULL, NULL);
1290 spin_lock_irqsave(&pmu_lock, flags);
1291 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1292 #ifdef SUSPEND_USES_PMU
1293 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1294 spin_unlock_irqrestore(&pmu_lock, flags);
1295 while(!req.complete)
1296 pmu_poll();
1297 #else /* SUSPEND_USES_PMU */
1298 if (gpio_irq >= 0)
1299 disable_irq_nosync(gpio_irq);
1300 out_8(&via[IER], CB1_INT | IER_CLR);
1301 spin_unlock_irqrestore(&pmu_lock, flags);
1302 #endif /* SUSPEND_USES_PMU */
1303 break;
1305 } while (1);
1308 void
1309 pmu_resume(void)
1311 unsigned long flags;
1313 if (!via || (pmu_suspended < 1))
1314 return;
1316 spin_lock_irqsave(&pmu_lock, flags);
1317 pmu_suspended--;
1318 if (pmu_suspended > 0) {
1319 spin_unlock_irqrestore(&pmu_lock, flags);
1320 return;
1322 adb_int_pending = 1;
1323 #ifdef SUSPEND_USES_PMU
1324 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1325 spin_unlock_irqrestore(&pmu_lock, flags);
1326 while(!req.complete)
1327 pmu_poll();
1328 #else /* SUSPEND_USES_PMU */
1329 if (gpio_irq >= 0)
1330 enable_irq(gpio_irq);
1331 out_8(&via[IER], CB1_INT | IER_SET);
1332 spin_unlock_irqrestore(&pmu_lock, flags);
1333 pmu_poll();
1334 #endif /* SUSPEND_USES_PMU */
1337 /* Interrupt data could be the result data from an ADB cmd */
1338 static void
1339 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1341 unsigned char ints, pirq;
1342 int i = 0;
1344 asleep = 0;
1345 if (drop_interrupts || len < 1) {
1346 adb_int_pending = 0;
1347 pmu_irq_stats[8]++;
1348 return;
1351 /* Get PMU interrupt mask */
1352 ints = data[0];
1354 /* Record zero interrupts for stats */
1355 if (ints == 0)
1356 pmu_irq_stats[9]++;
1358 /* Hack to deal with ADB autopoll flag */
1359 if (ints & PMU_INT_ADB)
1360 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1362 next:
1364 if (ints == 0) {
1365 if (i > pmu_irq_stats[10])
1366 pmu_irq_stats[10] = i;
1367 return;
1370 for (pirq = 0; pirq < 8; pirq++)
1371 if (ints & (1 << pirq))
1372 break;
1373 pmu_irq_stats[pirq]++;
1374 i++;
1375 ints &= ~(1 << pirq);
1377 /* Note: for some reason, we get an interrupt with len=1,
1378 * data[0]==0 after each normal ADB interrupt, at least
1379 * on the Pismo. Still investigating... --BenH
1381 if ((1 << pirq) & PMU_INT_ADB) {
1382 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1383 struct adb_request *req = req_awaiting_reply;
1384 if (req == 0) {
1385 printk(KERN_ERR "PMU: extra ADB reply\n");
1386 return;
1388 req_awaiting_reply = NULL;
1389 if (len <= 2)
1390 req->reply_len = 0;
1391 else {
1392 memcpy(req->reply, data + 1, len - 1);
1393 req->reply_len = len - 1;
1395 pmu_done(req);
1396 } else {
1397 if (len == 4 && data[1] == 0x2c) {
1398 extern int xmon_wants_key, xmon_adb_keycode;
1399 if (xmon_wants_key) {
1400 xmon_adb_keycode = data[2];
1401 return;
1404 #ifdef CONFIG_ADB
1406 * XXX On the [23]400 the PMU gives us an up
1407 * event for keycodes 0x74 or 0x75 when the PC
1408 * card eject buttons are released, so we
1409 * ignore those events.
1411 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1412 && data[1] == 0x2c && data[3] == 0xff
1413 && (data[2] & ~1) == 0xf4))
1414 adb_input(data+1, len-1, regs, 1);
1415 #endif /* CONFIG_ADB */
1418 /* Sound/brightness button pressed */
1419 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1420 #ifdef CONFIG_PMAC_BACKLIGHT
1421 if (len == 3)
1422 #ifdef CONFIG_INPUT_ADBHID
1423 if (!disable_kernel_backlight)
1424 #endif /* CONFIG_INPUT_ADBHID */
1425 set_backlight_level(data[1] >> 4);
1426 #endif /* CONFIG_PMAC_BACKLIGHT */
1428 /* Tick interrupt */
1429 else if ((1 << pirq) & PMU_INT_TICK) {
1430 /* Environement or tick interrupt, query batteries */
1431 if (pmu_battery_count) {
1432 if ((--query_batt_timer) == 0) {
1433 query_battery_state();
1434 query_batt_timer = BATTERY_POLLING_COUNT;
1438 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1439 if (pmu_battery_count)
1440 query_battery_state();
1441 pmu_pass_intr(data, len);
1442 } else {
1443 pmu_pass_intr(data, len);
1445 goto next;
1448 static struct adb_request*
1449 pmu_sr_intr(struct pt_regs *regs)
1451 struct adb_request *req;
1452 int bite = 0;
1454 if (via[B] & TREQ) {
1455 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1456 out_8(&via[IFR], SR_INT);
1457 return NULL;
1459 /* The ack may not yet be low when we get the interrupt */
1460 while ((in_8(&via[B]) & TACK) != 0)
1463 /* if reading grab the byte, and reset the interrupt */
1464 if (pmu_state == reading || pmu_state == reading_intr)
1465 bite = in_8(&via[SR]);
1467 /* reset TREQ and wait for TACK to go high */
1468 out_8(&via[B], in_8(&via[B]) | TREQ);
1469 wait_for_ack();
1471 switch (pmu_state) {
1472 case sending:
1473 req = current_req;
1474 if (data_len < 0) {
1475 data_len = req->nbytes - 1;
1476 send_byte(data_len);
1477 break;
1479 if (data_index <= data_len) {
1480 send_byte(req->data[data_index++]);
1481 break;
1483 req->sent = 1;
1484 data_len = pmu_data_len[req->data[0]][1];
1485 if (data_len == 0) {
1486 pmu_state = idle;
1487 current_req = req->next;
1488 if (req->reply_expected)
1489 req_awaiting_reply = req;
1490 else
1491 return req;
1492 } else {
1493 pmu_state = reading;
1494 data_index = 0;
1495 reply_ptr = req->reply + req->reply_len;
1496 recv_byte();
1498 break;
1500 case intack:
1501 data_index = 0;
1502 data_len = -1;
1503 pmu_state = reading_intr;
1504 reply_ptr = interrupt_data[int_data_last];
1505 recv_byte();
1506 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1507 enable_irq(gpio_irq);
1508 gpio_irq_enabled = 1;
1510 break;
1512 case reading:
1513 case reading_intr:
1514 if (data_len == -1) {
1515 data_len = bite;
1516 if (bite > 32)
1517 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1518 } else if (data_index < 32) {
1519 reply_ptr[data_index++] = bite;
1521 if (data_index < data_len) {
1522 recv_byte();
1523 break;
1526 if (pmu_state == reading_intr) {
1527 pmu_state = idle;
1528 int_data_state[int_data_last] = int_data_ready;
1529 interrupt_data_len[int_data_last] = data_len;
1530 } else {
1531 req = current_req;
1533 * For PMU sleep and freq change requests, we lock the
1534 * PMU until it's explicitely unlocked. This avoids any
1535 * spurrious event polling getting in
1537 current_req = req->next;
1538 req->reply_len += data_index;
1539 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1540 pmu_state = locked;
1541 else
1542 pmu_state = idle;
1543 return req;
1545 break;
1547 default:
1548 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1549 pmu_state);
1551 return NULL;
1554 static irqreturn_t
1555 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1557 unsigned long flags;
1558 int intr;
1559 int nloop = 0;
1560 int int_data = -1;
1561 struct adb_request *req = NULL;
1562 int handled = 0;
1564 /* This is a bit brutal, we can probably do better */
1565 spin_lock_irqsave(&pmu_lock, flags);
1566 ++disable_poll;
1568 for (;;) {
1569 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1570 if (intr == 0)
1571 break;
1572 handled = 1;
1573 if (++nloop > 1000) {
1574 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1575 "intr=%x, ier=%x pmu_state=%d\n",
1576 intr, in_8(&via[IER]), pmu_state);
1577 break;
1579 out_8(&via[IFR], intr);
1580 if (intr & CB1_INT) {
1581 adb_int_pending = 1;
1582 pmu_irq_stats[0]++;
1584 if (intr & SR_INT) {
1585 req = pmu_sr_intr(regs);
1586 if (req)
1587 break;
1591 recheck:
1592 if (pmu_state == idle) {
1593 if (adb_int_pending) {
1594 if (int_data_state[0] == int_data_empty)
1595 int_data_last = 0;
1596 else if (int_data_state[1] == int_data_empty)
1597 int_data_last = 1;
1598 else
1599 goto no_free_slot;
1600 pmu_state = intack;
1601 int_data_state[int_data_last] = int_data_fill;
1602 /* Sounds safer to make sure ACK is high before writing.
1603 * This helped kill a problem with ADB and some iBooks
1605 wait_for_ack();
1606 send_byte(PMU_INT_ACK);
1607 adb_int_pending = 0;
1608 } else if (current_req)
1609 pmu_start();
1611 no_free_slot:
1612 /* Mark the oldest buffer for flushing */
1613 if (int_data_state[!int_data_last] == int_data_ready) {
1614 int_data_state[!int_data_last] = int_data_flush;
1615 int_data = !int_data_last;
1616 } else if (int_data_state[int_data_last] == int_data_ready) {
1617 int_data_state[int_data_last] = int_data_flush;
1618 int_data = int_data_last;
1620 --disable_poll;
1621 spin_unlock_irqrestore(&pmu_lock, flags);
1623 /* Deal with completed PMU requests outside of the lock */
1624 if (req) {
1625 pmu_done(req);
1626 req = NULL;
1629 /* Deal with interrupt datas outside of the lock */
1630 if (int_data >= 0) {
1631 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1632 spin_lock_irqsave(&pmu_lock, flags);
1633 ++disable_poll;
1634 int_data_state[int_data] = int_data_empty;
1635 int_data = -1;
1636 goto recheck;
1639 return IRQ_RETVAL(handled);
1642 void
1643 pmu_unlock(void)
1645 unsigned long flags;
1647 spin_lock_irqsave(&pmu_lock, flags);
1648 if (pmu_state == locked)
1649 pmu_state = idle;
1650 adb_int_pending = 1;
1651 spin_unlock_irqrestore(&pmu_lock, flags);
1655 static irqreturn_t
1656 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1658 unsigned long flags;
1660 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1661 spin_lock_irqsave(&pmu_lock, flags);
1662 if (gpio_irq_enabled > 0) {
1663 disable_irq_nosync(gpio_irq);
1664 gpio_irq_enabled = 0;
1666 pmu_irq_stats[1]++;
1667 adb_int_pending = 1;
1668 spin_unlock_irqrestore(&pmu_lock, flags);
1669 via_pmu_interrupt(0, NULL, NULL);
1670 return IRQ_HANDLED;
1672 return IRQ_NONE;
1675 #ifdef CONFIG_PMAC_BACKLIGHT
1676 static int backlight_to_bright[] = {
1677 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1678 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1681 static int
1682 pmu_set_backlight_enable(int on, int level, void* data)
1684 struct adb_request req;
1686 if (vias == NULL)
1687 return -ENODEV;
1689 if (on) {
1690 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1691 backlight_to_bright[level]);
1692 pmu_wait_complete(&req);
1694 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1695 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1696 pmu_wait_complete(&req);
1698 return 0;
1701 static void
1702 pmu_bright_complete(struct adb_request *req)
1704 if (req == &bright_req_1)
1705 clear_bit(1, &async_req_locks);
1706 if (req == &bright_req_2)
1707 clear_bit(2, &async_req_locks);
1710 static int
1711 pmu_set_backlight_level(int level, void* data)
1713 if (vias == NULL)
1714 return -ENODEV;
1716 if (test_and_set_bit(1, &async_req_locks))
1717 return -EAGAIN;
1718 pmu_request(&bright_req_1, pmu_bright_complete, 2, PMU_BACKLIGHT_BRIGHT,
1719 backlight_to_bright[level]);
1720 if (test_and_set_bit(2, &async_req_locks))
1721 return -EAGAIN;
1722 pmu_request(&bright_req_2, pmu_bright_complete, 2, PMU_POWER_CTRL,
1723 PMU_POW_BACKLIGHT | (level > BACKLIGHT_OFF ?
1724 PMU_POW_ON : PMU_POW_OFF));
1726 return 0;
1728 #endif /* CONFIG_PMAC_BACKLIGHT */
1730 void
1731 pmu_enable_irled(int on)
1733 struct adb_request req;
1735 if (vias == NULL)
1736 return ;
1737 if (pmu_kind == PMU_KEYLARGO_BASED)
1738 return ;
1740 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1741 (on ? PMU_POW_ON : PMU_POW_OFF));
1742 pmu_wait_complete(&req);
1745 void
1746 pmu_restart(void)
1748 struct adb_request req;
1750 if (via == NULL)
1751 return;
1753 local_irq_disable();
1755 drop_interrupts = 1;
1757 if (pmu_kind != PMU_KEYLARGO_BASED) {
1758 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1759 PMU_INT_TICK );
1760 while(!req.complete)
1761 pmu_poll();
1764 pmu_request(&req, NULL, 1, PMU_RESET);
1765 pmu_wait_complete(&req);
1766 for (;;)
1770 void
1771 pmu_shutdown(void)
1773 struct adb_request req;
1775 if (via == NULL)
1776 return;
1778 local_irq_disable();
1780 drop_interrupts = 1;
1782 if (pmu_kind != PMU_KEYLARGO_BASED) {
1783 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1784 PMU_INT_TICK );
1785 pmu_wait_complete(&req);
1786 } else {
1787 /* Disable server mode on shutdown or we'll just
1788 * wake up again
1790 pmu_set_server_mode(0);
1793 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1794 'M', 'A', 'T', 'T');
1795 pmu_wait_complete(&req);
1796 for (;;)
1801 pmu_present(void)
1803 return via != 0;
1806 #ifdef CONFIG_PM
1808 static LIST_HEAD(sleep_notifiers);
1811 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1813 struct list_head *list;
1814 struct pmu_sleep_notifier *notifier;
1816 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1817 list = list->next) {
1818 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1819 if (n->priority > notifier->priority)
1820 break;
1822 __list_add(&n->list, list->prev, list);
1823 return 0;
1825 EXPORT_SYMBOL(pmu_register_sleep_notifier);
1828 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1830 if (n->list.next == 0)
1831 return -ENOENT;
1832 list_del(&n->list);
1833 n->list.next = NULL;
1834 return 0;
1836 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
1837 #endif /* CONFIG_PM */
1839 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1841 /* Sleep is broadcast last-to-first */
1842 static int
1843 broadcast_sleep(int when, int fallback)
1845 int ret = PBOOK_SLEEP_OK;
1846 struct list_head *list;
1847 struct pmu_sleep_notifier *notifier;
1849 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1850 list = list->prev) {
1851 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1852 ret = notifier->notifier_call(notifier, when);
1853 if (ret != PBOOK_SLEEP_OK) {
1854 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
1855 when, notifier, notifier->notifier_call);
1856 for (; list != &sleep_notifiers; list = list->next) {
1857 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1858 notifier->notifier_call(notifier, fallback);
1860 return ret;
1863 return ret;
1866 /* Wake is broadcast first-to-last */
1867 static int
1868 broadcast_wake(void)
1870 int ret = PBOOK_SLEEP_OK;
1871 struct list_head *list;
1872 struct pmu_sleep_notifier *notifier;
1874 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1875 list = list->next) {
1876 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1877 notifier->notifier_call(notifier, PBOOK_WAKE);
1879 return ret;
1883 * This struct is used to store config register values for
1884 * PCI devices which may get powered off when we sleep.
1886 static struct pci_save {
1887 #ifndef HACKED_PCI_SAVE
1888 u16 command;
1889 u16 cache_lat;
1890 u16 intr;
1891 u32 rom_address;
1892 #else
1893 u32 config[16];
1894 #endif
1895 } *pbook_pci_saves;
1896 static int pbook_npci_saves;
1898 static void
1899 pbook_alloc_pci_save(void)
1901 int npci;
1902 struct pci_dev *pd = NULL;
1904 npci = 0;
1905 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1906 ++npci;
1908 if (npci == 0)
1909 return;
1910 pbook_pci_saves = (struct pci_save *)
1911 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
1912 pbook_npci_saves = npci;
1915 static void
1916 pbook_free_pci_save(void)
1918 if (pbook_pci_saves == NULL)
1919 return;
1920 kfree(pbook_pci_saves);
1921 pbook_pci_saves = NULL;
1922 pbook_npci_saves = 0;
1925 static void
1926 pbook_pci_save(void)
1928 struct pci_save *ps = pbook_pci_saves;
1929 struct pci_dev *pd = NULL;
1930 int npci = pbook_npci_saves;
1932 if (ps == NULL)
1933 return;
1935 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1936 if (npci-- == 0)
1937 return;
1938 #ifndef HACKED_PCI_SAVE
1939 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1940 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1941 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1942 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1943 #else
1944 int i;
1945 for (i=1;i<16;i++)
1946 pci_read_config_dword(pd, i<<4, &ps->config[i]);
1947 #endif
1948 ++ps;
1952 /* For this to work, we must take care of a few things: If gmac was enabled
1953 * during boot, it will be in the pci dev list. If it's disabled at this point
1954 * (and it will probably be), then you can't access it's config space.
1956 static void
1957 pbook_pci_restore(void)
1959 u16 cmd;
1960 struct pci_save *ps = pbook_pci_saves - 1;
1961 struct pci_dev *pd = NULL;
1962 int npci = pbook_npci_saves;
1963 int j;
1965 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1966 #ifdef HACKED_PCI_SAVE
1967 int i;
1968 if (npci-- == 0)
1969 return;
1970 ps++;
1971 for (i=2;i<16;i++)
1972 pci_write_config_dword(pd, i<<4, ps->config[i]);
1973 pci_write_config_dword(pd, 4, ps->config[1]);
1974 #else
1975 if (npci-- == 0)
1976 return;
1977 ps++;
1978 if (ps->command == 0)
1979 continue;
1980 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1981 if ((ps->command & ~cmd) == 0)
1982 continue;
1983 switch (pd->hdr_type) {
1984 case PCI_HEADER_TYPE_NORMAL:
1985 for (j = 0; j < 6; ++j)
1986 pci_write_config_dword(pd,
1987 PCI_BASE_ADDRESS_0 + j*4,
1988 pd->resource[j].start);
1989 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1990 ps->rom_address);
1991 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1992 ps->cache_lat);
1993 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1994 ps->intr);
1995 pci_write_config_word(pd, PCI_COMMAND, ps->command);
1996 break;
1998 #endif
2002 #ifdef DEBUG_SLEEP
2003 /* N.B. This doesn't work on the 3400 */
2004 void
2005 pmu_blink(int n)
2007 struct adb_request req;
2009 memset(&req, 0, sizeof(req));
2011 for (; n > 0; --n) {
2012 req.nbytes = 4;
2013 req.done = NULL;
2014 req.data[0] = 0xee;
2015 req.data[1] = 4;
2016 req.data[2] = 0;
2017 req.data[3] = 1;
2018 req.reply[0] = ADB_RET_OK;
2019 req.reply_len = 1;
2020 req.reply_expected = 0;
2021 pmu_polled_request(&req);
2022 mdelay(50);
2023 req.nbytes = 4;
2024 req.done = NULL;
2025 req.data[0] = 0xee;
2026 req.data[1] = 4;
2027 req.data[2] = 0;
2028 req.data[3] = 0;
2029 req.reply[0] = ADB_RET_OK;
2030 req.reply_len = 1;
2031 req.reply_expected = 0;
2032 pmu_polled_request(&req);
2033 mdelay(50);
2035 mdelay(50);
2037 #endif
2040 * Put the powerbook to sleep.
2043 static u32 save_via[8];
2045 static void
2046 save_via_state(void)
2048 save_via[0] = in_8(&via[ANH]);
2049 save_via[1] = in_8(&via[DIRA]);
2050 save_via[2] = in_8(&via[B]);
2051 save_via[3] = in_8(&via[DIRB]);
2052 save_via[4] = in_8(&via[PCR]);
2053 save_via[5] = in_8(&via[ACR]);
2054 save_via[6] = in_8(&via[T1CL]);
2055 save_via[7] = in_8(&via[T1CH]);
2057 static void
2058 restore_via_state(void)
2060 out_8(&via[ANH], save_via[0]);
2061 out_8(&via[DIRA], save_via[1]);
2062 out_8(&via[B], save_via[2]);
2063 out_8(&via[DIRB], save_via[3]);
2064 out_8(&via[PCR], save_via[4]);
2065 out_8(&via[ACR], save_via[5]);
2066 out_8(&via[T1CL], save_via[6]);
2067 out_8(&via[T1CH], save_via[7]);
2068 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
2069 out_8(&via[IFR], 0x7f); /* clear IFR */
2070 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2073 static int
2074 pmac_suspend_devices(void)
2076 int ret;
2078 pm_prepare_console();
2080 /* Notify old-style device drivers & userland */
2081 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2082 if (ret != PBOOK_SLEEP_OK) {
2083 printk(KERN_ERR "Sleep rejected by drivers\n");
2084 return -EBUSY;
2087 /* Sync the disks. */
2088 /* XXX It would be nice to have some way to ensure that
2089 * nobody is dirtying any new buffers while we wait. That
2090 * could be achieved using the refrigerator for processes
2091 * that swsusp uses
2093 sys_sync();
2095 /* Sleep can fail now. May not be very robust but useful for debugging */
2096 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2097 if (ret != PBOOK_SLEEP_OK) {
2098 printk(KERN_ERR "Driver sleep failed\n");
2099 return -EBUSY;
2102 /* Send suspend call to devices, hold the device core's dpm_sem */
2103 ret = device_suspend(PMSG_SUSPEND);
2104 if (ret) {
2105 broadcast_wake();
2106 printk(KERN_ERR "Driver sleep failed\n");
2107 return -EBUSY;
2110 /* Call platform functions marked "on sleep" */
2111 pmac_pfunc_i2c_suspend();
2112 pmac_pfunc_base_suspend();
2114 /* Stop preemption */
2115 preempt_disable();
2117 /* Make sure the decrementer won't interrupt us */
2118 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2119 /* Make sure any pending DEC interrupt occurring while we did
2120 * the above didn't re-enable the DEC */
2121 mb();
2122 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2124 /* We can now disable MSR_EE. This code of course works properly only
2125 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2126 * stop the "other" CPUs way before we do all that stuff.
2128 local_irq_disable();
2130 /* Broadcast power down irq
2131 * This isn't that useful in most cases (only directly wired devices can
2132 * use this but still... This will take care of sysdev's as well, so
2133 * we exit from here with local irqs disabled and PIC off.
2135 ret = device_power_down(PMSG_SUSPEND);
2136 if (ret) {
2137 wakeup_decrementer();
2138 local_irq_enable();
2139 preempt_enable();
2140 device_resume();
2141 broadcast_wake();
2142 printk(KERN_ERR "Driver powerdown failed\n");
2143 return -EBUSY;
2146 /* Wait for completion of async backlight requests */
2147 while (!bright_req_1.complete || !bright_req_2.complete ||
2148 !batt_req.complete)
2149 pmu_poll();
2151 /* Giveup the lazy FPU & vec so we don't have to back them
2152 * up from the low level code
2154 enable_kernel_fp();
2156 #ifdef CONFIG_ALTIVEC
2157 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2158 enable_kernel_altivec();
2159 #endif /* CONFIG_ALTIVEC */
2161 return 0;
2164 static int
2165 pmac_wakeup_devices(void)
2167 mdelay(100);
2169 /* Power back up system devices (including the PIC) */
2170 device_power_up();
2172 /* Force a poll of ADB interrupts */
2173 adb_int_pending = 1;
2174 via_pmu_interrupt(0, NULL, NULL);
2176 /* Restart jiffies & scheduling */
2177 wakeup_decrementer();
2179 /* Re-enable local CPU interrupts */
2180 local_irq_enable();
2181 mdelay(10);
2182 preempt_enable();
2184 /* Call platform functions marked "on wake" */
2185 pmac_pfunc_base_resume();
2186 pmac_pfunc_i2c_resume();
2188 /* Resume devices */
2189 device_resume();
2191 /* Notify old style drivers */
2192 broadcast_wake();
2194 pm_restore_console();
2196 return 0;
2199 #define GRACKLE_PM (1<<7)
2200 #define GRACKLE_DOZE (1<<5)
2201 #define GRACKLE_NAP (1<<4)
2202 #define GRACKLE_SLEEP (1<<3)
2205 powerbook_sleep_grackle(void)
2207 unsigned long save_l2cr;
2208 unsigned short pmcr1;
2209 struct adb_request req;
2210 int ret;
2211 struct pci_dev *grackle;
2213 grackle = pci_find_slot(0, 0);
2214 if (!grackle)
2215 return -ENODEV;
2217 ret = pmac_suspend_devices();
2218 if (ret) {
2219 printk(KERN_ERR "Sleep rejected by devices\n");
2220 return ret;
2223 /* Turn off various things. Darwin does some retry tests here... */
2224 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2225 pmu_wait_complete(&req);
2226 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2227 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2228 pmu_wait_complete(&req);
2230 /* For 750, save backside cache setting and disable it */
2231 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2233 if (!__fake_sleep) {
2234 /* Ask the PMU to put us to sleep */
2235 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2236 pmu_wait_complete(&req);
2239 /* The VIA is supposed not to be restored correctly*/
2240 save_via_state();
2241 /* We shut down some HW */
2242 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2244 pci_read_config_word(grackle, 0x70, &pmcr1);
2245 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2246 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2247 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2248 pci_write_config_word(grackle, 0x70, pmcr1);
2250 /* Call low-level ASM sleep handler */
2251 if (__fake_sleep)
2252 mdelay(5000);
2253 else
2254 low_sleep_handler();
2256 /* We're awake again, stop grackle PM */
2257 pci_read_config_word(grackle, 0x70, &pmcr1);
2258 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2259 pci_write_config_word(grackle, 0x70, pmcr1);
2261 /* Make sure the PMU is idle */
2262 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2263 restore_via_state();
2265 /* Restore L2 cache */
2266 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2267 _set_L2CR(save_l2cr);
2269 /* Restore userland MMU context */
2270 set_context(current->active_mm->context, current->active_mm->pgd);
2272 /* Power things up */
2273 pmu_unlock();
2274 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2275 pmu_wait_complete(&req);
2276 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2277 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2278 pmu_wait_complete(&req);
2279 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2280 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2281 pmu_wait_complete(&req);
2283 pmac_wakeup_devices();
2285 return 0;
2288 static int
2289 powerbook_sleep_Core99(void)
2291 unsigned long save_l2cr;
2292 unsigned long save_l3cr;
2293 struct adb_request req;
2294 int ret;
2296 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2297 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2298 return -ENOSYS;
2301 if (num_online_cpus() > 1 || cpu_is_offline(0))
2302 return -EAGAIN;
2304 ret = pmac_suspend_devices();
2305 if (ret) {
2306 printk(KERN_ERR "Sleep rejected by devices\n");
2307 return ret;
2310 /* Stop environment and ADB interrupts */
2311 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2312 pmu_wait_complete(&req);
2314 /* Tell PMU what events will wake us up */
2315 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2316 0xff, 0xff);
2317 pmu_wait_complete(&req);
2318 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2319 0, PMU_PWR_WAKEUP_KEY |
2320 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2321 pmu_wait_complete(&req);
2323 /* Save the state of the L2 and L3 caches */
2324 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2325 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2327 if (!__fake_sleep) {
2328 /* Ask the PMU to put us to sleep */
2329 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2330 pmu_wait_complete(&req);
2333 /* The VIA is supposed not to be restored correctly*/
2334 save_via_state();
2336 /* Shut down various ASICs. There's a chance that we can no longer
2337 * talk to the PMU after this, so I moved it to _after_ sending the
2338 * sleep command to it. Still need to be checked.
2340 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2342 /* Call low-level ASM sleep handler */
2343 if (__fake_sleep)
2344 mdelay(5000);
2345 else
2346 low_sleep_handler();
2348 /* Restore Apple core ASICs state */
2349 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2351 /* Restore VIA */
2352 restore_via_state();
2354 /* tweak LPJ before cpufreq is there */
2355 loops_per_jiffy *= 2;
2357 /* Restore video */
2358 pmac_call_early_video_resume();
2360 /* Restore L2 cache */
2361 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2362 _set_L2CR(save_l2cr);
2363 /* Restore L3 cache */
2364 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2365 _set_L3CR(save_l3cr);
2367 /* Restore userland MMU context */
2368 set_context(current->active_mm->context, current->active_mm->pgd);
2370 /* Tell PMU we are ready */
2371 pmu_unlock();
2372 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2373 pmu_wait_complete(&req);
2374 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2375 pmu_wait_complete(&req);
2377 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2378 loops_per_jiffy /= 2;
2380 pmac_wakeup_devices();
2382 return 0;
2385 #define PB3400_MEM_CTRL 0xf8000000
2386 #define PB3400_MEM_CTRL_SLEEP 0x70
2388 static int
2389 powerbook_sleep_3400(void)
2391 int ret, i, x;
2392 unsigned int hid0;
2393 unsigned long p;
2394 struct adb_request sleep_req;
2395 void __iomem *mem_ctrl;
2396 unsigned int __iomem *mem_ctrl_sleep;
2398 /* first map in the memory controller registers */
2399 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2400 if (mem_ctrl == NULL) {
2401 printk("powerbook_sleep_3400: ioremap failed\n");
2402 return -ENOMEM;
2404 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2406 /* Allocate room for PCI save */
2407 pbook_alloc_pci_save();
2409 ret = pmac_suspend_devices();
2410 if (ret) {
2411 pbook_free_pci_save();
2412 printk(KERN_ERR "Sleep rejected by devices\n");
2413 return ret;
2416 /* Save the state of PCI config space for some slots */
2417 pbook_pci_save();
2419 /* Set the memory controller to keep the memory refreshed
2420 while we're asleep */
2421 for (i = 0x403f; i >= 0x4000; --i) {
2422 out_be32(mem_ctrl_sleep, i);
2423 do {
2424 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2425 } while (x == 0);
2426 if (x >= 0x100)
2427 break;
2430 /* Ask the PMU to put us to sleep */
2431 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2432 while (!sleep_req.complete)
2433 mb();
2435 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2437 /* displacement-flush the L2 cache - necessary? */
2438 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2439 i = *(volatile int *)p;
2440 asleep = 1;
2442 /* Put the CPU into sleep mode */
2443 hid0 = mfspr(SPRN_HID0);
2444 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2445 mtspr(SPRN_HID0, hid0);
2446 mtmsr(mfmsr() | MSR_POW | MSR_EE);
2447 udelay(10);
2449 /* OK, we're awake again, start restoring things */
2450 out_be32(mem_ctrl_sleep, 0x3f);
2451 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2452 pbook_pci_restore();
2453 pmu_unlock();
2455 /* wait for the PMU interrupt sequence to complete */
2456 while (asleep)
2457 mb();
2459 pmac_wakeup_devices();
2460 pbook_free_pci_save();
2461 iounmap(mem_ctrl);
2463 return 0;
2466 #endif /* CONFIG_PM && CONFIG_PPC32 */
2469 * Support for /dev/pmu device
2471 #define RB_SIZE 0x10
2472 struct pmu_private {
2473 struct list_head list;
2474 int rb_get;
2475 int rb_put;
2476 struct rb_entry {
2477 unsigned short len;
2478 unsigned char data[16];
2479 } rb_buf[RB_SIZE];
2480 wait_queue_head_t wait;
2481 spinlock_t lock;
2482 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2483 int backlight_locker;
2484 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2487 static LIST_HEAD(all_pmu_pvt);
2488 static DEFINE_SPINLOCK(all_pvt_lock);
2490 static void
2491 pmu_pass_intr(unsigned char *data, int len)
2493 struct pmu_private *pp;
2494 struct list_head *list;
2495 int i;
2496 unsigned long flags;
2498 if (len > sizeof(pp->rb_buf[0].data))
2499 len = sizeof(pp->rb_buf[0].data);
2500 spin_lock_irqsave(&all_pvt_lock, flags);
2501 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2502 pp = list_entry(list, struct pmu_private, list);
2503 spin_lock(&pp->lock);
2504 i = pp->rb_put + 1;
2505 if (i >= RB_SIZE)
2506 i = 0;
2507 if (i != pp->rb_get) {
2508 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2509 rp->len = len;
2510 memcpy(rp->data, data, len);
2511 pp->rb_put = i;
2512 wake_up_interruptible(&pp->wait);
2514 spin_unlock(&pp->lock);
2516 spin_unlock_irqrestore(&all_pvt_lock, flags);
2519 static int
2520 pmu_open(struct inode *inode, struct file *file)
2522 struct pmu_private *pp;
2523 unsigned long flags;
2525 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2526 if (pp == 0)
2527 return -ENOMEM;
2528 pp->rb_get = pp->rb_put = 0;
2529 spin_lock_init(&pp->lock);
2530 init_waitqueue_head(&pp->wait);
2531 spin_lock_irqsave(&all_pvt_lock, flags);
2532 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2533 pp->backlight_locker = 0;
2534 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2535 list_add(&pp->list, &all_pmu_pvt);
2536 spin_unlock_irqrestore(&all_pvt_lock, flags);
2537 file->private_data = pp;
2538 return 0;
2541 static ssize_t
2542 pmu_read(struct file *file, char __user *buf,
2543 size_t count, loff_t *ppos)
2545 struct pmu_private *pp = file->private_data;
2546 DECLARE_WAITQUEUE(wait, current);
2547 unsigned long flags;
2548 int ret = 0;
2550 if (count < 1 || pp == 0)
2551 return -EINVAL;
2552 if (!access_ok(VERIFY_WRITE, buf, count))
2553 return -EFAULT;
2555 spin_lock_irqsave(&pp->lock, flags);
2556 add_wait_queue(&pp->wait, &wait);
2557 current->state = TASK_INTERRUPTIBLE;
2559 for (;;) {
2560 ret = -EAGAIN;
2561 if (pp->rb_get != pp->rb_put) {
2562 int i = pp->rb_get;
2563 struct rb_entry *rp = &pp->rb_buf[i];
2564 ret = rp->len;
2565 spin_unlock_irqrestore(&pp->lock, flags);
2566 if (ret > count)
2567 ret = count;
2568 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2569 ret = -EFAULT;
2570 if (++i >= RB_SIZE)
2571 i = 0;
2572 spin_lock_irqsave(&pp->lock, flags);
2573 pp->rb_get = i;
2575 if (ret >= 0)
2576 break;
2577 if (file->f_flags & O_NONBLOCK)
2578 break;
2579 ret = -ERESTARTSYS;
2580 if (signal_pending(current))
2581 break;
2582 spin_unlock_irqrestore(&pp->lock, flags);
2583 schedule();
2584 spin_lock_irqsave(&pp->lock, flags);
2586 current->state = TASK_RUNNING;
2587 remove_wait_queue(&pp->wait, &wait);
2588 spin_unlock_irqrestore(&pp->lock, flags);
2590 return ret;
2593 static ssize_t
2594 pmu_write(struct file *file, const char __user *buf,
2595 size_t count, loff_t *ppos)
2597 return 0;
2600 static unsigned int
2601 pmu_fpoll(struct file *filp, poll_table *wait)
2603 struct pmu_private *pp = filp->private_data;
2604 unsigned int mask = 0;
2605 unsigned long flags;
2607 if (pp == 0)
2608 return 0;
2609 poll_wait(filp, &pp->wait, wait);
2610 spin_lock_irqsave(&pp->lock, flags);
2611 if (pp->rb_get != pp->rb_put)
2612 mask |= POLLIN;
2613 spin_unlock_irqrestore(&pp->lock, flags);
2614 return mask;
2617 static int
2618 pmu_release(struct inode *inode, struct file *file)
2620 struct pmu_private *pp = file->private_data;
2621 unsigned long flags;
2623 lock_kernel();
2624 if (pp != 0) {
2625 file->private_data = NULL;
2626 spin_lock_irqsave(&all_pvt_lock, flags);
2627 list_del(&pp->list);
2628 spin_unlock_irqrestore(&all_pvt_lock, flags);
2629 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2630 if (pp->backlight_locker) {
2631 spin_lock_irqsave(&pmu_lock, flags);
2632 disable_kernel_backlight--;
2633 spin_unlock_irqrestore(&pmu_lock, flags);
2635 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2636 kfree(pp);
2638 unlock_kernel();
2639 return 0;
2642 static int
2643 pmu_ioctl(struct inode * inode, struct file *filp,
2644 u_int cmd, u_long arg)
2646 __u32 __user *argp = (__u32 __user *)arg;
2647 int error = -EINVAL;
2649 switch (cmd) {
2650 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2651 case PMU_IOC_SLEEP:
2652 if (!capable(CAP_SYS_ADMIN))
2653 return -EACCES;
2654 if (sleep_in_progress)
2655 return -EBUSY;
2656 sleep_in_progress = 1;
2657 switch (pmu_kind) {
2658 case PMU_OHARE_BASED:
2659 error = powerbook_sleep_3400();
2660 break;
2661 case PMU_HEATHROW_BASED:
2662 case PMU_PADDINGTON_BASED:
2663 error = powerbook_sleep_grackle();
2664 break;
2665 case PMU_KEYLARGO_BASED:
2666 error = powerbook_sleep_Core99();
2667 break;
2668 default:
2669 error = -ENOSYS;
2671 sleep_in_progress = 0;
2672 break;
2673 case PMU_IOC_CAN_SLEEP:
2674 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2675 return put_user(0, argp);
2676 else
2677 return put_user(1, argp);
2678 #endif /* CONFIG_PM && CONFIG_PPC32 */
2680 #ifdef CONFIG_PMAC_BACKLIGHT
2681 /* Backlight should have its own device or go via
2682 * the fbdev
2684 case PMU_IOC_GET_BACKLIGHT:
2685 if (sleep_in_progress)
2686 return -EBUSY;
2687 error = get_backlight_level();
2688 if (error < 0)
2689 return error;
2690 return put_user(error, argp);
2691 case PMU_IOC_SET_BACKLIGHT:
2693 __u32 value;
2694 if (sleep_in_progress)
2695 return -EBUSY;
2696 error = get_user(value, argp);
2697 if (!error)
2698 error = set_backlight_level(value);
2699 break;
2701 #ifdef CONFIG_INPUT_ADBHID
2702 case PMU_IOC_GRAB_BACKLIGHT: {
2703 struct pmu_private *pp = filp->private_data;
2704 unsigned long flags;
2706 if (pp->backlight_locker)
2707 return 0;
2708 pp->backlight_locker = 1;
2709 spin_lock_irqsave(&pmu_lock, flags);
2710 disable_kernel_backlight++;
2711 spin_unlock_irqrestore(&pmu_lock, flags);
2712 return 0;
2714 #endif /* CONFIG_INPUT_ADBHID */
2715 #endif /* CONFIG_PMAC_BACKLIGHT */
2716 case PMU_IOC_GET_MODEL:
2717 return put_user(pmu_kind, argp);
2718 case PMU_IOC_HAS_ADB:
2719 return put_user(pmu_has_adb, argp);
2721 return error;
2724 static struct file_operations pmu_device_fops = {
2725 .read = pmu_read,
2726 .write = pmu_write,
2727 .poll = pmu_fpoll,
2728 .ioctl = pmu_ioctl,
2729 .open = pmu_open,
2730 .release = pmu_release,
2733 static struct miscdevice pmu_device = {
2734 PMU_MINOR, "pmu", &pmu_device_fops
2737 static int pmu_device_init(void)
2739 if (!via)
2740 return 0;
2741 if (misc_register(&pmu_device) < 0)
2742 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2743 return 0;
2745 device_initcall(pmu_device_init);
2748 #ifdef DEBUG_SLEEP
2749 static inline void
2750 polled_handshake(volatile unsigned char __iomem *via)
2752 via[B] &= ~TREQ; eieio();
2753 while ((via[B] & TACK) != 0)
2755 via[B] |= TREQ; eieio();
2756 while ((via[B] & TACK) == 0)
2760 static inline void
2761 polled_send_byte(volatile unsigned char __iomem *via, int x)
2763 via[ACR] |= SR_OUT | SR_EXT; eieio();
2764 via[SR] = x; eieio();
2765 polled_handshake(via);
2768 static inline int
2769 polled_recv_byte(volatile unsigned char __iomem *via)
2771 int x;
2773 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2774 x = via[SR]; eieio();
2775 polled_handshake(via);
2776 x = via[SR]; eieio();
2777 return x;
2781 pmu_polled_request(struct adb_request *req)
2783 unsigned long flags;
2784 int i, l, c;
2785 volatile unsigned char __iomem *v = via;
2787 req->complete = 1;
2788 c = req->data[0];
2789 l = pmu_data_len[c][0];
2790 if (l >= 0 && req->nbytes != l + 1)
2791 return -EINVAL;
2793 local_irq_save(flags);
2794 while (pmu_state != idle)
2795 pmu_poll();
2797 while ((via[B] & TACK) == 0)
2799 polled_send_byte(v, c);
2800 if (l < 0) {
2801 l = req->nbytes - 1;
2802 polled_send_byte(v, l);
2804 for (i = 1; i <= l; ++i)
2805 polled_send_byte(v, req->data[i]);
2807 l = pmu_data_len[c][1];
2808 if (l < 0)
2809 l = polled_recv_byte(v);
2810 for (i = 0; i < l; ++i)
2811 req->reply[i + req->reply_len] = polled_recv_byte(v);
2813 if (req->done)
2814 (*req->done)(req);
2816 local_irq_restore(flags);
2817 return 0;
2819 #endif /* DEBUG_SLEEP */
2822 /* FIXME: This is a temporary set of callbacks to enable us
2823 * to do suspend-to-disk.
2826 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2828 static int pmu_sys_suspended = 0;
2830 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
2832 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
2833 return 0;
2835 /* Suspend PMU event interrupts */
2836 pmu_suspend();
2838 pmu_sys_suspended = 1;
2839 return 0;
2842 static int pmu_sys_resume(struct sys_device *sysdev)
2844 struct adb_request req;
2846 if (!pmu_sys_suspended)
2847 return 0;
2849 /* Tell PMU we are ready */
2850 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2851 pmu_wait_complete(&req);
2853 /* Resume PMU event interrupts */
2854 pmu_resume();
2856 pmu_sys_suspended = 0;
2858 return 0;
2861 #endif /* CONFIG_PM && CONFIG_PPC32 */
2863 static struct sysdev_class pmu_sysclass = {
2864 set_kset_name("pmu"),
2867 static struct sys_device device_pmu = {
2868 .id = 0,
2869 .cls = &pmu_sysclass,
2872 static struct sysdev_driver driver_pmu = {
2873 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2874 .suspend = &pmu_sys_suspend,
2875 .resume = &pmu_sys_resume,
2876 #endif /* CONFIG_PM && CONFIG_PPC32 */
2879 static int __init init_pmu_sysfs(void)
2881 int rc;
2883 rc = sysdev_class_register(&pmu_sysclass);
2884 if (rc) {
2885 printk(KERN_ERR "Failed registering PMU sys class\n");
2886 return -ENODEV;
2888 rc = sysdev_register(&device_pmu);
2889 if (rc) {
2890 printk(KERN_ERR "Failed registering PMU sys device\n");
2891 return -ENODEV;
2893 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2894 if (rc) {
2895 printk(KERN_ERR "Failed registering PMU sys driver\n");
2896 return -ENODEV;
2898 return 0;
2901 subsys_initcall(init_pmu_sysfs);
2903 EXPORT_SYMBOL(pmu_request);
2904 EXPORT_SYMBOL(pmu_queue_request);
2905 EXPORT_SYMBOL(pmu_poll);
2906 EXPORT_SYMBOL(pmu_poll_adb);
2907 EXPORT_SYMBOL(pmu_wait_complete);
2908 EXPORT_SYMBOL(pmu_suspend);
2909 EXPORT_SYMBOL(pmu_resume);
2910 EXPORT_SYMBOL(pmu_unlock);
2911 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2912 EXPORT_SYMBOL(pmu_enable_irled);
2913 EXPORT_SYMBOL(pmu_battery_count);
2914 EXPORT_SYMBOL(pmu_batteries);
2915 EXPORT_SYMBOL(pmu_power_flags);
2916 #endif /* CONFIG_PM && CONFIG_PPC32 */