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
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>
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>
51 #include <asm/machdep.h>
53 #include <asm/pgtable.h>
54 #include <asm/system.h>
55 #include <asm/sections.h>
57 #include <asm/pmac_feature.h>
58 #include <asm/uaccess.h>
59 #include <asm/mmu_context.h>
60 #include <asm/cputable.h>
62 #ifdef CONFIG_PMAC_BACKLIGHT
63 #include <asm/backlight.h>
67 #include <asm/open_pic.h>
70 /* Some compile options */
71 #undef SUSPEND_USES_PMU
73 #undef HACKED_PCI_SAVE
75 /* Misc minor number allocated for /dev/pmu */
78 /* How many iterations between battery polls */
79 #define BATTERY_POLLING_COUNT 2
81 static volatile unsigned char __iomem *via;
83 /* VIA registers - spaced 0x200 bytes apart */
84 #define RS 0x200 /* skip between registers */
85 #define B 0 /* B-side data */
86 #define A RS /* A-side data */
87 #define DIRB (2*RS) /* B-side direction (1=output) */
88 #define DIRA (3*RS) /* A-side direction (1=output) */
89 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
90 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
91 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
92 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
93 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
94 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
95 #define SR (10*RS) /* Shift register */
96 #define ACR (11*RS) /* Auxiliary control register */
97 #define PCR (12*RS) /* Peripheral control register */
98 #define IFR (13*RS) /* Interrupt flag register */
99 #define IER (14*RS) /* Interrupt enable register */
100 #define ANH (15*RS) /* A-side data, no handshake */
102 /* Bits in B data register: both active low */
103 #define TACK 0x08 /* Transfer acknowledge (input) */
104 #define TREQ 0x10 /* Transfer request (output) */
107 #define SR_CTRL 0x1c /* Shift register control bits */
108 #define SR_EXT 0x0c /* Shift on external clock */
109 #define SR_OUT 0x10 /* Shift out if 1 */
111 /* Bits in IFR and IER */
112 #define IER_SET 0x80 /* set bits in IER */
113 #define IER_CLR 0 /* clear bits in IER */
114 #define SR_INT 0x04 /* Shift register full/empty */
116 #define CB1_INT 0x10 /* transition on CB1 input */
118 static volatile enum pmu_state {
127 static volatile enum int_data_state {
132 } int_data_state[2] = { int_data_empty, int_data_empty };
134 static struct adb_request *current_req;
135 static struct adb_request *last_req;
136 static struct adb_request *req_awaiting_reply;
137 static unsigned char interrupt_data[2][32];
138 static int interrupt_data_len[2];
139 static int int_data_last;
140 static unsigned char *reply_ptr;
141 static int data_index;
143 static volatile int adb_int_pending;
144 static volatile int disable_poll;
145 static struct adb_request bright_req_1, bright_req_2;
146 static struct device_node *vias;
147 static int pmu_kind = PMU_UNKNOWN;
148 static int pmu_fully_inited = 0;
149 static int pmu_has_adb;
150 static unsigned char __iomem *gpio_reg = NULL;
151 static int gpio_irq = -1;
152 static int gpio_irq_enabled = -1;
153 static volatile int pmu_suspended = 0;
154 static spinlock_t pmu_lock;
155 static u8 pmu_intr_mask;
156 static int pmu_version;
157 static int drop_interrupts;
158 #ifdef CONFIG_PMAC_PBOOK
159 static int option_lid_wakeup = 1;
160 static int sleep_in_progress;
161 #endif /* CONFIG_PMAC_PBOOK */
162 static unsigned long async_req_locks;
163 static unsigned int pmu_irq_stats[11];
165 static struct proc_dir_entry *proc_pmu_root;
166 static struct proc_dir_entry *proc_pmu_info;
167 static struct proc_dir_entry *proc_pmu_irqstats;
168 static struct proc_dir_entry *proc_pmu_options;
169 static int option_server_mode;
171 #ifdef CONFIG_PMAC_PBOOK
172 int pmu_battery_count;
174 unsigned int pmu_power_flags;
175 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
176 static int query_batt_timer = BATTERY_POLLING_COUNT;
177 static struct adb_request batt_req;
178 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
179 #endif /* CONFIG_PMAC_PBOOK */
181 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
182 extern int disable_kernel_backlight;
183 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
187 struct notifier_block *sleep_notifier_list;
190 static int adb_dev_map = 0;
191 static int pmu_adb_flags;
193 static int pmu_probe(void);
194 static int pmu_init(void);
195 static int pmu_send_request(struct adb_request *req, int sync);
196 static int pmu_adb_autopoll(int devs);
197 static int pmu_adb_reset_bus(void);
198 #endif /* CONFIG_ADB */
200 static int init_pmu(void);
201 static int pmu_queue_request(struct adb_request *req);
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 #ifdef CONFIG_PMAC_PBOOK
214 static void pmu_pass_intr(unsigned char *data, int len);
215 static int proc_get_batt(char *page, char **start, off_t off,
216 int count, int *eof, void *data);
217 #endif /* CONFIG_PMAC_PBOOK */
218 static int proc_read_options(char *page, char **start, off_t off,
219 int count, int *eof, void *data);
220 static int proc_write_options(struct file *file, const char __user *buffer,
221 unsigned long count, void *data);
224 struct adb_driver via_pmu_driver = {
233 #endif /* CONFIG_ADB */
235 extern void low_sleep_handler(void);
236 extern void enable_kernel_altivec(void);
237 extern void enable_kernel_fp(void);
240 int pmu_polled_request(struct adb_request *req);
241 int pmu_wink(struct adb_request *req);
245 * This table indicates for each PMU opcode:
246 * - the number of data bytes to be sent with the command, or -1
247 * if a length byte should be sent,
248 * - the number of response bytes which the PMU will return, or
249 * -1 if it will send a length byte.
251 static const s8 pmu_data_len[256][2] __openfirmwaredata = {
252 /* 0 1 2 3 4 5 6 7 */
253 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
255 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
257 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
259 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
260 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
261 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
263 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
264 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
265 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
267 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
268 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
269 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
270 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
272 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
273 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
274 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
275 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
276 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
277 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
278 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
279 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
280 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
281 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
282 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
283 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
284 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
287 static char *pbook_type[] = {
289 "PowerBook 2400/3400/3500(G3)",
290 "PowerBook G3 Series",
295 #ifdef CONFIG_PMAC_BACKLIGHT
296 static struct backlight_controller pmu_backlight_controller = {
297 pmu_set_backlight_enable,
298 pmu_set_backlight_level
300 #endif /* CONFIG_PMAC_BACKLIGHT */
307 vias = find_devices("via-pmu");
311 printk(KERN_WARNING "Warning: only using 1st via-pmu\n");
313 if (vias->n_addrs < 1 || vias->n_intrs < 1) {
314 printk(KERN_ERR "via-pmu: %d addresses, %d interrupts!\n",
315 vias->n_addrs, vias->n_intrs);
316 if (vias->n_addrs < 1 || vias->n_intrs < 1)
320 spin_lock_init(&pmu_lock);
324 pmu_intr_mask = PMU_INT_PCEJECT |
329 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
330 || device_is_compatible(vias->parent, "ohare")))
331 pmu_kind = PMU_OHARE_BASED;
332 else if (device_is_compatible(vias->parent, "paddington"))
333 pmu_kind = PMU_PADDINGTON_BASED;
334 else if (device_is_compatible(vias->parent, "heathrow"))
335 pmu_kind = PMU_HEATHROW_BASED;
336 else if (device_is_compatible(vias->parent, "Keylargo")
337 || device_is_compatible(vias->parent, "K2-Keylargo")) {
338 struct device_node *gpio, *gpiop;
340 pmu_kind = PMU_KEYLARGO_BASED;
341 pmu_has_adb = (find_type_devices("adb") != NULL);
342 pmu_intr_mask = PMU_INT_PCEJECT |
348 gpiop = find_devices("gpio");
349 if (gpiop && gpiop->n_addrs) {
350 gpio_reg = ioremap(gpiop->addrs->address, 0x10);
351 gpio = find_devices("extint-gpio1");
353 gpio = find_devices("pmu-interrupt");
354 if (gpio && gpio->parent == gpiop && gpio->n_intrs)
355 gpio_irq = gpio->intrs[0].line;
358 pmu_kind = PMU_UNKNOWN;
360 via = ioremap(vias->addrs->address, 0x2000);
362 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
363 out_8(&via[IFR], 0x7f); /* clear IFR */
372 printk(KERN_INFO "PMU driver %d initialized for %s, firmware: %02x\n",
373 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
375 sys_ctrler = SYS_CTRLER_PMU;
381 static int __openfirmware
384 return vias == NULL? -ENODEV: 0;
394 #endif /* CONFIG_ADB */
397 * We can't wait until pmu_init gets called, that happens too late.
398 * It happens after IDE and SCSI initialization, which can take a few
399 * seconds, and by that time the PMU could have given up on us and
401 * Thus this is called with arch_initcall rather than device_initcall.
403 static int __init via_pmu_start(void)
408 bright_req_1.complete = 1;
409 bright_req_2.complete = 1;
410 #ifdef CONFIG_PMAC_PBOOK
411 batt_req.complete = 1;
415 if (pmu_kind == PMU_KEYLARGO_BASED)
416 openpic_set_irq_priority(vias->intrs[0].line,
417 OPENPIC_PRIORITY_DEFAULT + 1);
420 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
422 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
423 vias->intrs[0].line);
427 if (pmu_kind == PMU_KEYLARGO_BASED && gpio_irq != -1) {
428 if (request_irq(gpio_irq, gpio1_interrupt, 0, "GPIO1 ADB", (void *)0))
429 printk(KERN_ERR "pmu: can't get irq %d (GPIO1)\n", gpio_irq);
430 gpio_irq_enabled = 1;
433 /* Enable interrupts */
434 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
436 pmu_fully_inited = 1;
438 /* Make sure PMU settle down before continuing. This is _very_ important
439 * since the IDE probe may shut interrupts down for quite a bit of time. If
440 * a PMU communication is pending while this happens, the PMU may timeout
441 * Not that on Core99 machines, the PMU keeps sending us environement
442 * messages, we should find a way to either fix IDE or make it call
443 * pmu_suspend() before masking interrupts. This can also happens while
444 * scolling with some fbdevs.
448 } while (pmu_state != idle);
453 arch_initcall(via_pmu_start);
456 * This has to be done after pci_init, which is a subsys_initcall.
458 static int __init via_pmu_dev_init(void)
464 request_OF_resource(vias, 0, NULL);
466 #ifdef CONFIG_PMAC_BACKLIGHT
467 /* Enable backlight */
468 register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
469 #endif /* CONFIG_PMAC_BACKLIGHT */
471 #ifdef CONFIG_PMAC_PBOOK
472 if (machine_is_compatible("AAPL,3400/2400") ||
473 machine_is_compatible("AAPL,3500")) {
474 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
475 NULL, PMAC_MB_INFO_MODEL, 0);
476 pmu_battery_count = 1;
477 if (mb == PMAC_TYPE_COMET)
478 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
480 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
481 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
482 machine_is_compatible("PowerBook1,1")) {
483 pmu_battery_count = 2;
484 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
485 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
487 struct device_node* prim = find_devices("power-mgt");
488 u32 *prim_info = NULL;
490 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
492 /* Other stuffs here yet unknown */
493 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
494 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
495 if (pmu_battery_count > 1)
496 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
499 #endif /* CONFIG_PMAC_PBOOK */
500 /* Create /proc/pmu */
501 proc_pmu_root = proc_mkdir("pmu", NULL);
503 #ifdef CONFIG_PMAC_PBOOK
506 for (i=0; i<pmu_battery_count; i++) {
508 sprintf(title, "battery_%d", i);
509 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
510 proc_get_batt, (void *)i);
512 #endif /* CONFIG_PMAC_PBOOK */
514 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
515 proc_get_info, NULL);
516 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
517 proc_get_irqstats, NULL);
518 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
519 if (proc_pmu_options) {
520 proc_pmu_options->nlink = 1;
521 proc_pmu_options->read_proc = proc_read_options;
522 proc_pmu_options->write_proc = proc_write_options;
528 device_initcall(via_pmu_dev_init);
530 static int __openfirmware
534 struct adb_request req;
536 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
537 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
539 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
541 while (!req.complete) {
543 printk(KERN_ERR "init_pmu: no response from PMU\n");
550 /* ack all pending interrupts */
552 interrupt_data[0][0] = 1;
553 while (interrupt_data[0][0] || pmu_state != idle) {
555 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
558 if (pmu_state == idle)
560 via_pmu_interrupt(0, NULL, NULL);
564 /* Tell PMU we are ready. */
565 if (pmu_kind == PMU_KEYLARGO_BASED) {
566 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
567 while (!req.complete)
571 /* Read PMU version */
572 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
573 pmu_wait_complete(&req);
574 if (req.reply_len > 0)
575 pmu_version = req.reply[0];
577 /* Read server mode setting */
578 if (pmu_kind == PMU_KEYLARGO_BASED) {
579 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
580 PMU_PWR_GET_POWERUP_EVENTS);
581 pmu_wait_complete(&req);
582 if (req.reply_len == 2) {
583 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
584 option_server_mode = 1;
585 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
586 option_server_mode ? "enabled" : "disabled");
599 static inline void wakeup_decrementer(void)
601 set_dec(tb_ticks_per_jiffy);
602 /* No currently-supported powerbook has a 601,
603 * so use get_tbl, not native
605 last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
609 static void pmu_set_server_mode(int server_mode)
611 struct adb_request req;
613 if (pmu_kind != PMU_KEYLARGO_BASED)
616 option_server_mode = server_mode;
617 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
618 pmu_wait_complete(&req);
619 if (req.reply_len < 2)
622 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
623 PMU_PWR_SET_POWERUP_EVENTS,
624 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
626 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
627 PMU_PWR_CLR_POWERUP_EVENTS,
628 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
629 pmu_wait_complete(&req);
632 #ifdef CONFIG_PMAC_PBOOK
634 /* This new version of the code for 2400/3400/3500 powerbooks
635 * is inspired from the implementation in gkrellm-pmu
638 done_battery_state_ohare(struct adb_request* req)
642 * 0x01 : AC indicator
644 * 0x04 : battery exist
647 * 0x20 : full charged
648 * 0x40 : pcharge reset
649 * 0x80 : battery exist
651 * [1][2] : battery voltage
652 * [3] : CPU temperature
653 * [4] : battery temperature
658 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
659 long pcharge, charge, vb, vmax, lmax;
660 long vmax_charging, vmax_charged;
661 long amperage, voltage, time, max;
662 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
663 NULL, PMAC_MB_INFO_MODEL, 0);
665 if (req->reply[0] & 0x01)
666 pmu_power_flags |= PMU_PWR_AC_PRESENT;
668 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
670 if (mb == PMAC_TYPE_COMET) {
681 /* If battery installed */
682 if (req->reply[0] & 0x04) {
683 bat_flags |= PMU_BATT_PRESENT;
684 if (req->reply[0] & 0x02)
685 bat_flags |= PMU_BATT_CHARGING;
686 vb = (req->reply[1] << 8) | req->reply[2];
687 voltage = (vb * 265 + 72665) / 10;
688 amperage = req->reply[5];
689 if ((req->reply[0] & 0x01) == 0) {
691 vb += ((amperage - 200) * 15)/100;
692 } else if (req->reply[0] & 0x02) {
693 vb = (vb * 97) / 100;
694 vmax = vmax_charging;
696 charge = (100 * vb) / vmax;
697 if (req->reply[0] & 0x40) {
698 pcharge = (req->reply[6] << 8) + req->reply[7];
702 pcharge = 100 - pcharge / lmax;
703 if (pcharge < charge)
707 time = (charge * 16440) / amperage;
711 amperage = -amperage;
713 charge = max = amperage = voltage = time = 0;
715 pmu_batteries[pmu_cur_battery].flags = bat_flags;
716 pmu_batteries[pmu_cur_battery].charge = charge;
717 pmu_batteries[pmu_cur_battery].max_charge = max;
718 pmu_batteries[pmu_cur_battery].amperage = amperage;
719 pmu_batteries[pmu_cur_battery].voltage = voltage;
720 pmu_batteries[pmu_cur_battery].time_remaining = time;
722 clear_bit(0, &async_req_locks);
726 done_battery_state_smart(struct adb_request* req)
729 * [0] : format of this structure (known: 3,4,5)
742 * [4][5] : max charge
747 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
749 unsigned int capa, max, voltage;
751 if (req->reply[1] & 0x01)
752 pmu_power_flags |= PMU_PWR_AC_PRESENT;
754 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
757 capa = max = amperage = voltage = 0;
759 if (req->reply[1] & 0x04) {
760 bat_flags |= PMU_BATT_PRESENT;
761 switch(req->reply[0]) {
763 case 4: capa = req->reply[2];
765 amperage = *((signed char *)&req->reply[4]);
766 voltage = req->reply[5];
768 case 5: capa = (req->reply[2] << 8) | req->reply[3];
769 max = (req->reply[4] << 8) | req->reply[5];
770 amperage = *((signed short *)&req->reply[6]);
771 voltage = (req->reply[8] << 8) | req->reply[9];
774 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
775 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
780 if ((req->reply[1] & 0x01) && (amperage > 0))
781 bat_flags |= PMU_BATT_CHARGING;
783 pmu_batteries[pmu_cur_battery].flags = bat_flags;
784 pmu_batteries[pmu_cur_battery].charge = capa;
785 pmu_batteries[pmu_cur_battery].max_charge = max;
786 pmu_batteries[pmu_cur_battery].amperage = amperage;
787 pmu_batteries[pmu_cur_battery].voltage = voltage;
789 if ((req->reply[1] & 0x01) && (amperage > 0))
790 pmu_batteries[pmu_cur_battery].time_remaining
791 = ((max-capa) * 3600) / amperage;
793 pmu_batteries[pmu_cur_battery].time_remaining
794 = (capa * 3600) / (-amperage);
796 pmu_batteries[pmu_cur_battery].time_remaining = 0;
798 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
800 clear_bit(0, &async_req_locks);
804 query_battery_state(void)
806 if (test_and_set_bit(0, &async_req_locks))
808 if (pmu_kind == PMU_OHARE_BASED)
809 pmu_request(&batt_req, done_battery_state_ohare,
810 1, PMU_BATTERY_STATE);
812 pmu_request(&batt_req, done_battery_state_smart,
813 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
816 #endif /* CONFIG_PMAC_PBOOK */
819 proc_get_info(char *page, char **start, off_t off,
820 int count, int *eof, void *data)
824 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
825 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
826 #ifdef CONFIG_PMAC_PBOOK
827 p += sprintf(p, "AC Power : %d\n",
828 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0));
829 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
830 #endif /* CONFIG_PMAC_PBOOK */
836 proc_get_irqstats(char *page, char **start, off_t off,
837 int count, int *eof, void *data)
841 static const char *irq_names[] = {
842 "Total CB1 triggered events",
843 "Total GPIO1 triggered events",
844 "PC-Card eject button",
845 "Sound/Brightness button",
847 "Battery state change",
848 "Environment interrupt",
850 "Ghost interrupt (zero len)",
851 "Empty interrupt (empty mask)",
855 for (i=0; i<11; i++) {
856 p += sprintf(p, " %2u: %10u (%s)\n",
857 i, pmu_irq_stats[i], irq_names[i]);
862 #ifdef CONFIG_PMAC_PBOOK
864 proc_get_batt(char *page, char **start, off_t off,
865 int count, int *eof, void *data)
867 int batnum = (int)data;
870 p += sprintf(p, "\n");
871 p += sprintf(p, "flags : %08x\n",
872 pmu_batteries[batnum].flags);
873 p += sprintf(p, "charge : %d\n",
874 pmu_batteries[batnum].charge);
875 p += sprintf(p, "max_charge : %d\n",
876 pmu_batteries[batnum].max_charge);
877 p += sprintf(p, "current : %d\n",
878 pmu_batteries[batnum].amperage);
879 p += sprintf(p, "voltage : %d\n",
880 pmu_batteries[batnum].voltage);
881 p += sprintf(p, "time rem. : %d\n",
882 pmu_batteries[batnum].time_remaining);
886 #endif /* CONFIG_PMAC_PBOOK */
889 proc_read_options(char *page, char **start, off_t off,
890 int count, int *eof, void *data)
894 #ifdef CONFIG_PMAC_PBOOK
895 if (pmu_kind == PMU_KEYLARGO_BASED &&
896 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
897 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
898 #endif /* CONFIG_PMAC_PBOOK */
899 if (pmu_kind == PMU_KEYLARGO_BASED)
900 p += sprintf(p, "server_mode=%d\n", option_server_mode);
906 proc_write_options(struct file *file, const char __user *buffer,
907 unsigned long count, void *data)
911 unsigned long fcount = count;
917 if (copy_from_user(tmp, buffer, count))
925 while(*val && (*val != '=')) {
935 #ifdef CONFIG_PMAC_PBOOK
936 if (pmu_kind == PMU_KEYLARGO_BASED &&
937 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
938 if (!strcmp(label, "lid_wakeup"))
939 option_lid_wakeup = ((*val) == '1');
940 #endif /* CONFIG_PMAC_PBOOK */
941 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
943 new_value = ((*val) == '1');
944 if (new_value != option_server_mode)
945 pmu_set_server_mode(new_value);
951 /* Send an ADB command */
953 pmu_send_request(struct adb_request *req, int sync)
957 if ((vias == NULL) || (!pmu_fully_inited)) {
964 switch (req->data[0]) {
966 for (i = 0; i < req->nbytes - 1; ++i)
967 req->data[i] = req->data[i+1];
969 if (pmu_data_len[req->data[0]][1] != 0) {
970 req->reply[0] = ADB_RET_OK;
974 ret = pmu_queue_request(req);
977 switch (req->data[1]) {
979 if (req->nbytes != 2)
981 req->data[0] = PMU_READ_RTC;
984 req->reply[0] = CUDA_PACKET;
986 req->reply[2] = CUDA_GET_TIME;
987 ret = pmu_queue_request(req);
990 if (req->nbytes != 6)
992 req->data[0] = PMU_SET_RTC;
994 for (i = 1; i <= 4; ++i)
995 req->data[i] = req->data[i+1];
997 req->reply[0] = CUDA_PACKET;
999 req->reply[2] = CUDA_SET_TIME;
1000 ret = pmu_queue_request(req);
1007 for (i = req->nbytes - 1; i > 1; --i)
1008 req->data[i+2] = req->data[i];
1009 req->data[3] = req->nbytes - 2;
1010 req->data[2] = pmu_adb_flags;
1011 /*req->data[1] = req->data[1];*/
1012 req->data[0] = PMU_ADB_CMD;
1014 req->reply_expected = 1;
1016 ret = pmu_queue_request(req);
1025 while (!req->complete)
1031 /* Enable/disable autopolling */
1033 pmu_adb_autopoll(int devs)
1035 struct adb_request req;
1037 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1042 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1043 adb_dev_map >> 8, adb_dev_map);
1046 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1049 while (!req.complete)
1054 /* Reset the ADB bus */
1056 pmu_adb_reset_bus(void)
1058 struct adb_request req;
1059 int save_autopoll = adb_dev_map;
1061 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1064 /* anyone got a better idea?? */
1065 pmu_adb_autopoll(0);
1069 req.data[0] = PMU_ADB_CMD;
1071 req.data[2] = ADB_BUSRESET;
1075 req.reply_expected = 1;
1076 if (pmu_queue_request(&req) != 0) {
1077 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1080 pmu_wait_complete(&req);
1082 if (save_autopoll != 0)
1083 pmu_adb_autopoll(save_autopoll);
1087 #endif /* CONFIG_ADB */
1089 /* Construct and send a pmu request */
1091 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1100 if (nbytes < 0 || nbytes > 32) {
1101 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1105 req->nbytes = nbytes;
1107 va_start(list, nbytes);
1108 for (i = 0; i < nbytes; ++i)
1109 req->data[i] = va_arg(list, int);
1112 req->reply_expected = 0;
1113 return pmu_queue_request(req);
1117 pmu_queue_request(struct adb_request *req)
1119 unsigned long flags;
1126 if (req->nbytes <= 0) {
1130 nsend = pmu_data_len[req->data[0]][0];
1131 if (nsend >= 0 && req->nbytes != nsend + 1) {
1140 spin_lock_irqsave(&pmu_lock, flags);
1141 if (current_req != 0) {
1142 last_req->next = req;
1147 if (pmu_state == idle)
1150 spin_unlock_irqrestore(&pmu_lock, flags);
1158 /* Sightly increased the delay, I had one occurrence of the message
1162 while ((in_8(&via[B]) & TACK) == 0) {
1163 if (--timeout < 0) {
1164 printk(KERN_ERR "PMU not responding (!ack)\n");
1171 /* New PMU seems to be very sensitive to those timings, so we make sure
1172 * PCI is flushed immediately */
1176 volatile unsigned char __iomem *v = via;
1178 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1180 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1187 volatile unsigned char __iomem *v = via;
1189 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1190 in_8(&v[SR]); /* resets SR */
1191 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1196 pmu_done(struct adb_request *req)
1198 void (*done)(struct adb_request *) = req->done;
1201 /* Here, we assume that if the request has a done member, the
1202 * struct request will survive to setting req->complete to 1
1211 struct adb_request *req;
1213 /* assert pmu_state == idle */
1214 /* get the packet to send */
1216 if (req == 0 || pmu_state != idle
1217 || (/*req->reply_expected && */req_awaiting_reply))
1220 pmu_state = sending;
1222 data_len = pmu_data_len[req->data[0]][0];
1224 /* Sounds safer to make sure ACK is high before writing. This helped
1225 * kill a problem with ADB and some iBooks
1228 /* set the shift register to shift out and send a byte */
1229 send_byte(req->data[0]);
1239 via_pmu_interrupt(0, NULL, NULL);
1249 /* Kicks ADB read when PMU is suspended */
1250 adb_int_pending = 1;
1252 via_pmu_interrupt(0, NULL, NULL);
1253 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1254 || req_awaiting_reply));
1258 pmu_wait_complete(struct adb_request *req)
1262 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1263 via_pmu_interrupt(0, NULL, NULL);
1266 /* This function loops until the PMU is idle and prevents it from
1267 * anwsering to ADB interrupts. pmu_request can still be called.
1268 * This is done to avoid spurrious shutdowns when we know we'll have
1269 * interrupts switched off for a long time
1274 unsigned long flags;
1275 #ifdef SUSPEND_USES_PMU
1276 struct adb_request *req;
1281 spin_lock_irqsave(&pmu_lock, flags);
1283 if (pmu_suspended > 1) {
1284 spin_unlock_irqrestore(&pmu_lock, flags);
1289 spin_unlock_irqrestore(&pmu_lock, flags);
1290 if (req_awaiting_reply)
1291 adb_int_pending = 1;
1292 via_pmu_interrupt(0, NULL, NULL);
1293 spin_lock_irqsave(&pmu_lock, flags);
1294 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1295 #ifdef SUSPEND_USES_PMU
1296 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1297 spin_unlock_irqrestore(&pmu_lock, flags);
1298 while(!req.complete)
1300 #else /* SUSPEND_USES_PMU */
1302 disable_irq_nosync(gpio_irq);
1303 out_8(&via[IER], CB1_INT | IER_CLR);
1304 spin_unlock_irqrestore(&pmu_lock, flags);
1305 #endif /* SUSPEND_USES_PMU */
1314 unsigned long flags;
1316 if (!via || (pmu_suspended < 1))
1319 spin_lock_irqsave(&pmu_lock, flags);
1321 if (pmu_suspended > 0) {
1322 spin_unlock_irqrestore(&pmu_lock, flags);
1325 adb_int_pending = 1;
1326 #ifdef SUSPEND_USES_PMU
1327 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1328 spin_unlock_irqrestore(&pmu_lock, flags);
1329 while(!req.complete)
1331 #else /* SUSPEND_USES_PMU */
1333 enable_irq(gpio_irq);
1334 out_8(&via[IER], CB1_INT | IER_SET);
1335 spin_unlock_irqrestore(&pmu_lock, flags);
1337 #endif /* SUSPEND_USES_PMU */
1340 /* Interrupt data could be the result data from an ADB cmd */
1342 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1344 unsigned char ints, pirq;
1348 if (drop_interrupts || len < 1) {
1349 adb_int_pending = 0;
1354 /* Get PMU interrupt mask */
1357 /* Record zero interrupts for stats */
1361 /* Hack to deal with ADB autopoll flag */
1362 if (ints & PMU_INT_ADB)
1363 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1368 if (i > pmu_irq_stats[10])
1369 pmu_irq_stats[10] = i;
1373 for (pirq = 0; pirq < 8; pirq++)
1374 if (ints & (1 << pirq))
1376 pmu_irq_stats[pirq]++;
1378 ints &= ~(1 << pirq);
1380 /* Note: for some reason, we get an interrupt with len=1,
1381 * data[0]==0 after each normal ADB interrupt, at least
1382 * on the Pismo. Still investigating... --BenH
1384 if ((1 << pirq) & PMU_INT_ADB) {
1385 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1386 struct adb_request *req = req_awaiting_reply;
1388 printk(KERN_ERR "PMU: extra ADB reply\n");
1391 req_awaiting_reply = NULL;
1395 memcpy(req->reply, data + 1, len - 1);
1396 req->reply_len = len - 1;
1400 #if defined(CONFIG_XMON) && !defined(CONFIG_PPC64)
1401 if (len == 4 && data[1] == 0x2c) {
1402 extern int xmon_wants_key, xmon_adb_keycode;
1403 if (xmon_wants_key) {
1404 xmon_adb_keycode = data[2];
1408 #endif /* defined(CONFIG_XMON) && !defined(CONFIG_PPC64) */
1411 * XXX On the [23]400 the PMU gives us an up
1412 * event for keycodes 0x74 or 0x75 when the PC
1413 * card eject buttons are released, so we
1414 * ignore those events.
1416 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1417 && data[1] == 0x2c && data[3] == 0xff
1418 && (data[2] & ~1) == 0xf4))
1419 adb_input(data+1, len-1, regs, 1);
1420 #endif /* CONFIG_ADB */
1423 /* Sound/brightness button pressed */
1424 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1425 #ifdef CONFIG_PMAC_BACKLIGHT
1427 #ifdef CONFIG_INPUT_ADBHID
1428 if (!disable_kernel_backlight)
1429 #endif /* CONFIG_INPUT_ADBHID */
1430 set_backlight_level(data[1] >> 4);
1431 #endif /* CONFIG_PMAC_BACKLIGHT */
1433 /* Tick interrupt */
1434 else if ((1 << pirq) & PMU_INT_TICK) {
1435 #ifdef CONFIG_PMAC_PBOOK
1436 /* Environement or tick interrupt, query batteries */
1437 if (pmu_battery_count) {
1438 if ((--query_batt_timer) == 0) {
1439 query_battery_state();
1440 query_batt_timer = BATTERY_POLLING_COUNT;
1444 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1445 if (pmu_battery_count)
1446 query_battery_state();
1447 pmu_pass_intr(data, len);
1449 pmu_pass_intr(data, len);
1450 #endif /* CONFIG_PMAC_PBOOK */
1455 static struct adb_request* __pmac
1456 pmu_sr_intr(struct pt_regs *regs)
1458 struct adb_request *req;
1461 if (via[B] & TREQ) {
1462 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1463 out_8(&via[IFR], SR_INT);
1466 /* The ack may not yet be low when we get the interrupt */
1467 while ((in_8(&via[B]) & TACK) != 0)
1470 /* if reading grab the byte, and reset the interrupt */
1471 if (pmu_state == reading || pmu_state == reading_intr)
1472 bite = in_8(&via[SR]);
1474 /* reset TREQ and wait for TACK to go high */
1475 out_8(&via[B], in_8(&via[B]) | TREQ);
1478 switch (pmu_state) {
1482 data_len = req->nbytes - 1;
1483 send_byte(data_len);
1486 if (data_index <= data_len) {
1487 send_byte(req->data[data_index++]);
1491 data_len = pmu_data_len[req->data[0]][1];
1492 if (data_len == 0) {
1494 current_req = req->next;
1495 if (req->reply_expected)
1496 req_awaiting_reply = req;
1500 pmu_state = reading;
1502 reply_ptr = req->reply + req->reply_len;
1510 pmu_state = reading_intr;
1511 reply_ptr = interrupt_data[int_data_last];
1513 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1514 enable_irq(gpio_irq);
1515 gpio_irq_enabled = 1;
1521 if (data_len == -1) {
1524 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1525 } else if (data_index < 32) {
1526 reply_ptr[data_index++] = bite;
1528 if (data_index < data_len) {
1533 if (pmu_state == reading_intr) {
1535 int_data_state[int_data_last] = int_data_ready;
1536 interrupt_data_len[int_data_last] = data_len;
1540 * For PMU sleep and freq change requests, we lock the
1541 * PMU until it's explicitely unlocked. This avoids any
1542 * spurrious event polling getting in
1544 current_req = req->next;
1545 req->reply_len += data_index;
1546 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1555 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1561 static irqreturn_t __pmac
1562 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1564 unsigned long flags;
1568 struct adb_request *req = NULL;
1571 /* This is a bit brutal, we can probably do better */
1572 spin_lock_irqsave(&pmu_lock, flags);
1576 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1580 if (++nloop > 1000) {
1581 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1582 "intr=%x, ier=%x pmu_state=%d\n",
1583 intr, in_8(&via[IER]), pmu_state);
1586 out_8(&via[IFR], intr);
1587 if (intr & CB1_INT) {
1588 adb_int_pending = 1;
1591 if (intr & SR_INT) {
1592 req = pmu_sr_intr(regs);
1599 if (pmu_state == idle) {
1600 if (adb_int_pending) {
1601 if (int_data_state[0] == int_data_empty)
1603 else if (int_data_state[1] == int_data_empty)
1608 int_data_state[int_data_last] = int_data_fill;
1609 /* Sounds safer to make sure ACK is high before writing.
1610 * This helped kill a problem with ADB and some iBooks
1613 send_byte(PMU_INT_ACK);
1614 adb_int_pending = 0;
1615 } else if (current_req)
1619 /* Mark the oldest buffer for flushing */
1620 if (int_data_state[!int_data_last] == int_data_ready) {
1621 int_data_state[!int_data_last] = int_data_flush;
1622 int_data = !int_data_last;
1623 } else if (int_data_state[int_data_last] == int_data_ready) {
1624 int_data_state[int_data_last] = int_data_flush;
1625 int_data = int_data_last;
1628 spin_unlock_irqrestore(&pmu_lock, flags);
1630 /* Deal with completed PMU requests outside of the lock */
1636 /* Deal with interrupt datas outside of the lock */
1637 if (int_data >= 0) {
1638 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1639 spin_lock_irqsave(&pmu_lock, flags);
1641 int_data_state[int_data] = int_data_empty;
1646 return IRQ_RETVAL(handled);
1652 unsigned long flags;
1654 spin_lock_irqsave(&pmu_lock, flags);
1655 if (pmu_state == locked)
1657 adb_int_pending = 1;
1658 spin_unlock_irqrestore(&pmu_lock, flags);
1662 static irqreturn_t __pmac
1663 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1665 unsigned long flags;
1667 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1668 spin_lock_irqsave(&pmu_lock, flags);
1669 if (gpio_irq_enabled > 0) {
1670 disable_irq_nosync(gpio_irq);
1671 gpio_irq_enabled = 0;
1674 adb_int_pending = 1;
1675 spin_unlock_irqrestore(&pmu_lock, flags);
1676 via_pmu_interrupt(0, NULL, NULL);
1682 #ifdef CONFIG_PMAC_BACKLIGHT
1683 static int backlight_to_bright[] __pmacdata = {
1684 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1685 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1688 static int __openfirmware
1689 pmu_set_backlight_enable(int on, int level, void* data)
1691 struct adb_request req;
1697 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1698 backlight_to_bright[level]);
1699 pmu_wait_complete(&req);
1701 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1702 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1703 pmu_wait_complete(&req);
1708 static void __openfirmware
1709 pmu_bright_complete(struct adb_request *req)
1711 if (req == &bright_req_1)
1712 clear_bit(1, &async_req_locks);
1713 if (req == &bright_req_2)
1714 clear_bit(2, &async_req_locks);
1717 static int __openfirmware
1718 pmu_set_backlight_level(int level, void* data)
1723 if (test_and_set_bit(1, &async_req_locks))
1725 pmu_request(&bright_req_1, pmu_bright_complete, 2, PMU_BACKLIGHT_BRIGHT,
1726 backlight_to_bright[level]);
1727 if (test_and_set_bit(2, &async_req_locks))
1729 pmu_request(&bright_req_2, pmu_bright_complete, 2, PMU_POWER_CTRL,
1730 PMU_POW_BACKLIGHT | (level > BACKLIGHT_OFF ?
1731 PMU_POW_ON : PMU_POW_OFF));
1735 #endif /* CONFIG_PMAC_BACKLIGHT */
1738 pmu_enable_irled(int on)
1740 struct adb_request req;
1744 if (pmu_kind == PMU_KEYLARGO_BASED)
1747 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1748 (on ? PMU_POW_ON : PMU_POW_OFF));
1749 pmu_wait_complete(&req);
1755 struct adb_request req;
1760 local_irq_disable();
1762 drop_interrupts = 1;
1764 if (pmu_kind != PMU_KEYLARGO_BASED) {
1765 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1767 while(!req.complete)
1771 pmu_request(&req, NULL, 1, PMU_RESET);
1772 pmu_wait_complete(&req);
1780 struct adb_request req;
1785 local_irq_disable();
1787 drop_interrupts = 1;
1789 if (pmu_kind != PMU_KEYLARGO_BASED) {
1790 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1792 pmu_wait_complete(&req);
1794 /* Disable server mode on shutdown or we'll just
1797 pmu_set_server_mode(0);
1800 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1801 'M', 'A', 'T', 'T');
1802 pmu_wait_complete(&req);
1813 struct pmu_i2c_hdr {
1824 pmu_i2c_combined_read(int bus, int addr, int subaddr, u8* data, int len)
1826 struct adb_request req;
1827 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1831 for (retry=0; retry<16; retry++) {
1832 memset(&req, 0, sizeof(req));
1835 hdr->address = addr & 0xfe;
1836 hdr->mode = PMU_I2C_MODE_COMBINED;
1838 hdr->sub_addr = subaddr;
1839 hdr->comb_addr = addr | 1;
1842 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1843 req.reply_expected = 0;
1845 req.data[0] = PMU_I2C_CMD;
1846 req.reply[0] = 0xff;
1847 rc = pmu_queue_request(&req);
1850 while(!req.complete)
1852 if (req.reply[0] == PMU_I2C_STATUS_OK)
1856 if (req.reply[0] != PMU_I2C_STATUS_OK)
1859 for (retry=0; retry<16; retry++) {
1860 memset(&req, 0, sizeof(req));
1864 hdr->bus = PMU_I2C_BUS_STATUS;
1865 req.reply[0] = 0xff;
1868 req.reply_expected = 0;
1870 req.data[0] = PMU_I2C_CMD;
1871 rc = pmu_queue_request(&req);
1874 while(!req.complete)
1876 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1877 memcpy(data, &req.reply[1], req.reply_len - 1);
1878 return req.reply_len - 1;
1885 pmu_i2c_stdsub_write(int bus, int addr, int subaddr, u8* data, int len)
1887 struct adb_request req;
1888 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1892 for (retry=0; retry<16; retry++) {
1893 memset(&req, 0, sizeof(req));
1896 hdr->address = addr & 0xfe;
1897 hdr->mode = PMU_I2C_MODE_STDSUB;
1899 hdr->sub_addr = subaddr;
1900 hdr->comb_addr = addr & 0xfe;
1903 req.data[0] = PMU_I2C_CMD;
1904 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
1905 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
1906 req.reply_expected = 0;
1908 req.reply[0] = 0xff;
1909 rc = pmu_queue_request(&req);
1912 while(!req.complete)
1914 if (req.reply[0] == PMU_I2C_STATUS_OK)
1918 if (req.reply[0] != PMU_I2C_STATUS_OK)
1921 for (retry=0; retry<16; retry++) {
1922 memset(&req, 0, sizeof(req));
1926 hdr->bus = PMU_I2C_BUS_STATUS;
1927 req.reply[0] = 0xff;
1930 req.reply_expected = 0;
1932 req.data[0] = PMU_I2C_CMD;
1933 rc = pmu_queue_request(&req);
1936 while(!req.complete)
1938 if (req.reply[0] == PMU_I2C_STATUS_OK)
1945 pmu_i2c_simple_read(int bus, int addr, u8* data, int len)
1947 struct adb_request req;
1948 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1952 for (retry=0; retry<16; retry++) {
1953 memset(&req, 0, sizeof(req));
1956 hdr->address = addr | 1;
1957 hdr->mode = PMU_I2C_MODE_SIMPLE;
1963 req.data[0] = PMU_I2C_CMD;
1964 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1965 req.reply_expected = 0;
1967 req.reply[0] = 0xff;
1968 rc = pmu_queue_request(&req);
1971 while(!req.complete)
1973 if (req.reply[0] == PMU_I2C_STATUS_OK)
1977 if (req.reply[0] != PMU_I2C_STATUS_OK)
1980 for (retry=0; retry<16; retry++) {
1981 memset(&req, 0, sizeof(req));
1985 hdr->bus = PMU_I2C_BUS_STATUS;
1986 req.reply[0] = 0xff;
1989 req.reply_expected = 0;
1991 req.data[0] = PMU_I2C_CMD;
1992 rc = pmu_queue_request(&req);
1995 while(!req.complete)
1997 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1998 memcpy(data, &req.reply[1], req.reply_len - 1);
1999 return req.reply_len - 1;
2006 pmu_i2c_simple_write(int bus, int addr, u8* data, int len)
2008 struct adb_request req;
2009 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
2013 for (retry=0; retry<16; retry++) {
2014 memset(&req, 0, sizeof(req));
2017 hdr->address = addr & 0xfe;
2018 hdr->mode = PMU_I2C_MODE_SIMPLE;
2024 req.data[0] = PMU_I2C_CMD;
2025 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
2026 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
2027 req.reply_expected = 0;
2029 req.reply[0] = 0xff;
2030 rc = pmu_queue_request(&req);
2033 while(!req.complete)
2035 if (req.reply[0] == PMU_I2C_STATUS_OK)
2039 if (req.reply[0] != PMU_I2C_STATUS_OK)
2042 for (retry=0; retry<16; retry++) {
2043 memset(&req, 0, sizeof(req));
2047 hdr->bus = PMU_I2C_BUS_STATUS;
2048 req.reply[0] = 0xff;
2051 req.reply_expected = 0;
2053 req.data[0] = PMU_I2C_CMD;
2054 rc = pmu_queue_request(&req);
2057 while(!req.complete)
2059 if (req.reply[0] == PMU_I2C_STATUS_OK)
2065 #ifdef CONFIG_PMAC_PBOOK
2067 static LIST_HEAD(sleep_notifiers);
2070 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
2072 struct list_head *list;
2073 struct pmu_sleep_notifier *notifier;
2075 for (list = sleep_notifiers.next; list != &sleep_notifiers;
2076 list = list->next) {
2077 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2078 if (n->priority > notifier->priority)
2081 __list_add(&n->list, list->prev, list);
2086 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
2088 if (n->list.next == 0)
2091 n->list.next = NULL;
2095 /* Sleep is broadcast last-to-first */
2097 broadcast_sleep(int when, int fallback)
2099 int ret = PBOOK_SLEEP_OK;
2100 struct list_head *list;
2101 struct pmu_sleep_notifier *notifier;
2103 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
2104 list = list->prev) {
2105 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2106 ret = notifier->notifier_call(notifier, when);
2107 if (ret != PBOOK_SLEEP_OK) {
2108 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
2109 when, notifier, notifier->notifier_call);
2110 for (; list != &sleep_notifiers; list = list->next) {
2111 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2112 notifier->notifier_call(notifier, fallback);
2120 /* Wake is broadcast first-to-last */
2122 broadcast_wake(void)
2124 int ret = PBOOK_SLEEP_OK;
2125 struct list_head *list;
2126 struct pmu_sleep_notifier *notifier;
2128 for (list = sleep_notifiers.next; list != &sleep_notifiers;
2129 list = list->next) {
2130 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2131 notifier->notifier_call(notifier, PBOOK_WAKE);
2137 * This struct is used to store config register values for
2138 * PCI devices which may get powered off when we sleep.
2140 static struct pci_save {
2141 #ifndef HACKED_PCI_SAVE
2150 static int pbook_npci_saves;
2153 pbook_alloc_pci_save(void)
2156 struct pci_dev *pd = NULL;
2159 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2164 pbook_pci_saves = (struct pci_save *)
2165 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
2166 pbook_npci_saves = npci;
2170 pbook_free_pci_save(void)
2172 if (pbook_pci_saves == NULL)
2174 kfree(pbook_pci_saves);
2175 pbook_pci_saves = NULL;
2176 pbook_npci_saves = 0;
2180 pbook_pci_save(void)
2182 struct pci_save *ps = pbook_pci_saves;
2183 struct pci_dev *pd = NULL;
2184 int npci = pbook_npci_saves;
2189 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2192 #ifndef HACKED_PCI_SAVE
2193 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
2194 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
2195 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
2196 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
2200 pci_read_config_dword(pd, i<<4, &ps->config[i]);
2206 /* For this to work, we must take care of a few things: If gmac was enabled
2207 * during boot, it will be in the pci dev list. If it's disabled at this point
2208 * (and it will probably be), then you can't access it's config space.
2211 pbook_pci_restore(void)
2214 struct pci_save *ps = pbook_pci_saves - 1;
2215 struct pci_dev *pd = NULL;
2216 int npci = pbook_npci_saves;
2219 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2220 #ifdef HACKED_PCI_SAVE
2226 pci_write_config_dword(pd, i<<4, ps->config[i]);
2227 pci_write_config_dword(pd, 4, ps->config[1]);
2232 if (ps->command == 0)
2234 pci_read_config_word(pd, PCI_COMMAND, &cmd);
2235 if ((ps->command & ~cmd) == 0)
2237 switch (pd->hdr_type) {
2238 case PCI_HEADER_TYPE_NORMAL:
2239 for (j = 0; j < 6; ++j)
2240 pci_write_config_dword(pd,
2241 PCI_BASE_ADDRESS_0 + j*4,
2242 pd->resource[j].start);
2243 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
2245 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
2247 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
2249 pci_write_config_word(pd, PCI_COMMAND, ps->command);
2257 /* N.B. This doesn't work on the 3400 */
2261 struct adb_request req;
2263 memset(&req, 0, sizeof(req));
2265 for (; n > 0; --n) {
2272 req.reply[0] = ADB_RET_OK;
2274 req.reply_expected = 0;
2275 pmu_polled_request(&req);
2283 req.reply[0] = ADB_RET_OK;
2285 req.reply_expected = 0;
2286 pmu_polled_request(&req);
2294 * Put the powerbook to sleep.
2297 static u32 save_via[8] __pmacdata;
2300 save_via_state(void)
2302 save_via[0] = in_8(&via[ANH]);
2303 save_via[1] = in_8(&via[DIRA]);
2304 save_via[2] = in_8(&via[B]);
2305 save_via[3] = in_8(&via[DIRB]);
2306 save_via[4] = in_8(&via[PCR]);
2307 save_via[5] = in_8(&via[ACR]);
2308 save_via[6] = in_8(&via[T1CL]);
2309 save_via[7] = in_8(&via[T1CH]);
2312 restore_via_state(void)
2314 out_8(&via[ANH], save_via[0]);
2315 out_8(&via[DIRA], save_via[1]);
2316 out_8(&via[B], save_via[2]);
2317 out_8(&via[DIRB], save_via[3]);
2318 out_8(&via[PCR], save_via[4]);
2319 out_8(&via[ACR], save_via[5]);
2320 out_8(&via[T1CL], save_via[6]);
2321 out_8(&via[T1CH], save_via[7]);
2322 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
2323 out_8(&via[IFR], 0x7f); /* clear IFR */
2324 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2328 pmac_suspend_devices(void)
2332 pm_prepare_console();
2334 /* Notify old-style device drivers & userland */
2335 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2336 if (ret != PBOOK_SLEEP_OK) {
2337 printk(KERN_ERR "Sleep rejected by drivers\n");
2341 /* Sync the disks. */
2342 /* XXX It would be nice to have some way to ensure that
2343 * nobody is dirtying any new buffers while we wait. That
2344 * could be achieved using the refrigerator for processes
2349 /* Sleep can fail now. May not be very robust but useful for debugging */
2350 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2351 if (ret != PBOOK_SLEEP_OK) {
2352 printk(KERN_ERR "Driver sleep failed\n");
2356 /* Send suspend call to devices, hold the device core's dpm_sem */
2357 ret = device_suspend(PMSG_SUSPEND);
2360 printk(KERN_ERR "Driver sleep failed\n");
2364 /* Disable clock spreading on some machines */
2365 pmac_tweak_clock_spreading(0);
2367 /* Stop preemption */
2370 /* Make sure the decrementer won't interrupt us */
2371 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2372 /* Make sure any pending DEC interrupt occurring while we did
2373 * the above didn't re-enable the DEC */
2375 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2377 /* We can now disable MSR_EE. This code of course works properly only
2378 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2379 * stop the "other" CPUs way before we do all that stuff.
2381 local_irq_disable();
2383 /* Broadcast power down irq
2384 * This isn't that useful in most cases (only directly wired devices can
2385 * use this but still... This will take care of sysdev's as well, so
2386 * we exit from here with local irqs disabled and PIC off.
2388 ret = device_power_down(PMSG_SUSPEND);
2390 wakeup_decrementer();
2395 printk(KERN_ERR "Driver powerdown failed\n");
2399 /* Wait for completion of async backlight requests */
2400 while (!bright_req_1.complete || !bright_req_2.complete ||
2404 /* Giveup the lazy FPU & vec so we don't have to back them
2405 * up from the low level code
2409 #ifdef CONFIG_ALTIVEC
2410 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2411 enable_kernel_altivec();
2412 #endif /* CONFIG_ALTIVEC */
2418 pmac_wakeup_devices(void)
2422 /* Power back up system devices (including the PIC) */
2425 /* Force a poll of ADB interrupts */
2426 adb_int_pending = 1;
2427 via_pmu_interrupt(0, NULL, NULL);
2429 /* Restart jiffies & scheduling */
2430 wakeup_decrementer();
2432 /* Re-enable local CPU interrupts */
2437 /* Re-enable clock spreading on some machines */
2438 pmac_tweak_clock_spreading(1);
2440 /* Resume devices */
2443 /* Notify old style drivers */
2446 pm_restore_console();
2451 #define GRACKLE_PM (1<<7)
2452 #define GRACKLE_DOZE (1<<5)
2453 #define GRACKLE_NAP (1<<4)
2454 #define GRACKLE_SLEEP (1<<3)
2457 powerbook_sleep_grackle(void)
2459 unsigned long save_l2cr;
2460 unsigned short pmcr1;
2461 struct adb_request req;
2463 struct pci_dev *grackle;
2465 grackle = pci_find_slot(0, 0);
2469 ret = pmac_suspend_devices();
2471 printk(KERN_ERR "Sleep rejected by devices\n");
2475 /* Turn off various things. Darwin does some retry tests here... */
2476 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2477 pmu_wait_complete(&req);
2478 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2479 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2480 pmu_wait_complete(&req);
2482 /* For 750, save backside cache setting and disable it */
2483 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2485 if (!__fake_sleep) {
2486 /* Ask the PMU to put us to sleep */
2487 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2488 pmu_wait_complete(&req);
2491 /* The VIA is supposed not to be restored correctly*/
2493 /* We shut down some HW */
2494 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2496 pci_read_config_word(grackle, 0x70, &pmcr1);
2497 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2498 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2499 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2500 pci_write_config_word(grackle, 0x70, pmcr1);
2502 /* Call low-level ASM sleep handler */
2506 low_sleep_handler();
2508 /* We're awake again, stop grackle PM */
2509 pci_read_config_word(grackle, 0x70, &pmcr1);
2510 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2511 pci_write_config_word(grackle, 0x70, pmcr1);
2513 /* Make sure the PMU is idle */
2514 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2515 restore_via_state();
2517 /* Restore L2 cache */
2518 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2519 _set_L2CR(save_l2cr);
2521 /* Restore userland MMU context */
2522 set_context(current->active_mm->context, current->active_mm->pgd);
2524 /* Power things up */
2526 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2527 pmu_wait_complete(&req);
2528 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2529 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2530 pmu_wait_complete(&req);
2531 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2532 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2533 pmu_wait_complete(&req);
2535 pmac_wakeup_devices();
2541 powerbook_sleep_Core99(void)
2543 unsigned long save_l2cr;
2544 unsigned long save_l3cr;
2545 struct adb_request req;
2548 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2549 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2553 if (num_online_cpus() > 1 || cpu_is_offline(0))
2556 ret = pmac_suspend_devices();
2558 printk(KERN_ERR "Sleep rejected by devices\n");
2562 /* Stop environment and ADB interrupts */
2563 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2564 pmu_wait_complete(&req);
2566 /* Tell PMU what events will wake us up */
2567 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2569 pmu_wait_complete(&req);
2570 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2571 0, PMU_PWR_WAKEUP_KEY |
2572 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2573 pmu_wait_complete(&req);
2575 /* Save the state of the L2 and L3 caches */
2576 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2577 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2579 if (!__fake_sleep) {
2580 /* Ask the PMU to put us to sleep */
2581 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2582 pmu_wait_complete(&req);
2585 /* The VIA is supposed not to be restored correctly*/
2588 /* Shut down various ASICs. There's a chance that we can no longer
2589 * talk to the PMU after this, so I moved it to _after_ sending the
2590 * sleep command to it. Still need to be checked.
2592 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2594 /* Call low-level ASM sleep handler */
2598 low_sleep_handler();
2600 /* Restore Apple core ASICs state */
2601 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2604 restore_via_state();
2606 /* tweak LPJ before cpufreq is there */
2607 loops_per_jiffy *= 2;
2610 pmac_call_early_video_resume();
2612 /* Restore L2 cache */
2613 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2614 _set_L2CR(save_l2cr);
2615 /* Restore L3 cache */
2616 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2617 _set_L3CR(save_l3cr);
2619 /* Restore userland MMU context */
2620 set_context(current->active_mm->context, current->active_mm->pgd);
2622 /* Tell PMU we are ready */
2624 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2625 pmu_wait_complete(&req);
2626 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2627 pmu_wait_complete(&req);
2629 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2630 loops_per_jiffy /= 2;
2632 pmac_wakeup_devices();
2637 #define PB3400_MEM_CTRL 0xf8000000
2638 #define PB3400_MEM_CTRL_SLEEP 0x70
2641 powerbook_sleep_3400(void)
2646 struct adb_request sleep_req;
2647 void __iomem *mem_ctrl;
2648 unsigned int __iomem *mem_ctrl_sleep;
2650 /* first map in the memory controller registers */
2651 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2652 if (mem_ctrl == NULL) {
2653 printk("powerbook_sleep_3400: ioremap failed\n");
2656 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2658 /* Allocate room for PCI save */
2659 pbook_alloc_pci_save();
2661 ret = pmac_suspend_devices();
2663 pbook_free_pci_save();
2664 printk(KERN_ERR "Sleep rejected by devices\n");
2668 /* Save the state of PCI config space for some slots */
2671 /* Set the memory controller to keep the memory refreshed
2672 while we're asleep */
2673 for (i = 0x403f; i >= 0x4000; --i) {
2674 out_be32(mem_ctrl_sleep, i);
2676 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2682 /* Ask the PMU to put us to sleep */
2683 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2684 while (!sleep_req.complete)
2687 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2689 /* displacement-flush the L2 cache - necessary? */
2690 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2691 i = *(volatile int *)p;
2694 /* Put the CPU into sleep mode */
2695 asm volatile("mfspr %0,1008" : "=r" (hid0) :);
2696 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2697 asm volatile("mtspr 1008,%0" : : "r" (hid0));
2698 _nmask_and_or_msr(0, MSR_POW | MSR_EE);
2701 /* OK, we're awake again, start restoring things */
2702 out_be32(mem_ctrl_sleep, 0x3f);
2703 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2704 pbook_pci_restore();
2707 /* wait for the PMU interrupt sequence to complete */
2711 pmac_wakeup_devices();
2712 pbook_free_pci_save();
2719 * Support for /dev/pmu device
2721 #define RB_SIZE 0x10
2722 struct pmu_private {
2723 struct list_head list;
2728 unsigned char data[16];
2730 wait_queue_head_t wait;
2732 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2733 int backlight_locker;
2734 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2737 static LIST_HEAD(all_pmu_pvt);
2738 static DEFINE_SPINLOCK(all_pvt_lock __pmacdata);
2741 pmu_pass_intr(unsigned char *data, int len)
2743 struct pmu_private *pp;
2744 struct list_head *list;
2746 unsigned long flags;
2748 if (len > sizeof(pp->rb_buf[0].data))
2749 len = sizeof(pp->rb_buf[0].data);
2750 spin_lock_irqsave(&all_pvt_lock, flags);
2751 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2752 pp = list_entry(list, struct pmu_private, list);
2753 spin_lock(&pp->lock);
2757 if (i != pp->rb_get) {
2758 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2760 memcpy(rp->data, data, len);
2762 wake_up_interruptible(&pp->wait);
2764 spin_unlock(&pp->lock);
2766 spin_unlock_irqrestore(&all_pvt_lock, flags);
2770 pmu_open(struct inode *inode, struct file *file)
2772 struct pmu_private *pp;
2773 unsigned long flags;
2775 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2778 pp->rb_get = pp->rb_put = 0;
2779 spin_lock_init(&pp->lock);
2780 init_waitqueue_head(&pp->wait);
2781 spin_lock_irqsave(&all_pvt_lock, flags);
2782 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2783 pp->backlight_locker = 0;
2784 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2785 list_add(&pp->list, &all_pmu_pvt);
2786 spin_unlock_irqrestore(&all_pvt_lock, flags);
2787 file->private_data = pp;
2791 static ssize_t __pmac
2792 pmu_read(struct file *file, char __user *buf,
2793 size_t count, loff_t *ppos)
2795 struct pmu_private *pp = file->private_data;
2796 DECLARE_WAITQUEUE(wait, current);
2797 unsigned long flags;
2800 if (count < 1 || pp == 0)
2802 if (!access_ok(VERIFY_WRITE, buf, count))
2805 spin_lock_irqsave(&pp->lock, flags);
2806 add_wait_queue(&pp->wait, &wait);
2807 current->state = TASK_INTERRUPTIBLE;
2811 if (pp->rb_get != pp->rb_put) {
2813 struct rb_entry *rp = &pp->rb_buf[i];
2815 spin_unlock_irqrestore(&pp->lock, flags);
2818 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2822 spin_lock_irqsave(&pp->lock, flags);
2827 if (file->f_flags & O_NONBLOCK)
2830 if (signal_pending(current))
2832 spin_unlock_irqrestore(&pp->lock, flags);
2834 spin_lock_irqsave(&pp->lock, flags);
2836 current->state = TASK_RUNNING;
2837 remove_wait_queue(&pp->wait, &wait);
2838 spin_unlock_irqrestore(&pp->lock, flags);
2843 static ssize_t __pmac
2844 pmu_write(struct file *file, const char __user *buf,
2845 size_t count, loff_t *ppos)
2850 static unsigned int __pmac
2851 pmu_fpoll(struct file *filp, poll_table *wait)
2853 struct pmu_private *pp = filp->private_data;
2854 unsigned int mask = 0;
2855 unsigned long flags;
2859 poll_wait(filp, &pp->wait, wait);
2860 spin_lock_irqsave(&pp->lock, flags);
2861 if (pp->rb_get != pp->rb_put)
2863 spin_unlock_irqrestore(&pp->lock, flags);
2868 pmu_release(struct inode *inode, struct file *file)
2870 struct pmu_private *pp = file->private_data;
2871 unsigned long flags;
2875 file->private_data = NULL;
2876 spin_lock_irqsave(&all_pvt_lock, flags);
2877 list_del(&pp->list);
2878 spin_unlock_irqrestore(&all_pvt_lock, flags);
2879 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2880 if (pp->backlight_locker) {
2881 spin_lock_irqsave(&pmu_lock, flags);
2882 disable_kernel_backlight--;
2883 spin_unlock_irqrestore(&pmu_lock, flags);
2885 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2892 /* Note: removed __openfirmware here since it causes link errors */
2894 pmu_ioctl(struct inode * inode, struct file *filp,
2895 u_int cmd, u_long arg)
2897 struct pmu_private *pp = filp->private_data;
2898 __u32 __user *argp = (__u32 __user *)arg;
2903 if (!capable(CAP_SYS_ADMIN))
2905 if (sleep_in_progress)
2907 sleep_in_progress = 1;
2909 case PMU_OHARE_BASED:
2910 error = powerbook_sleep_3400();
2912 case PMU_HEATHROW_BASED:
2913 case PMU_PADDINGTON_BASED:
2914 error = powerbook_sleep_grackle();
2916 case PMU_KEYLARGO_BASED:
2917 error = powerbook_sleep_Core99();
2922 sleep_in_progress = 0;
2924 case PMU_IOC_CAN_SLEEP:
2925 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2926 return put_user(0, argp);
2928 return put_user(1, argp);
2930 #ifdef CONFIG_PMAC_BACKLIGHT
2931 /* Backlight should have its own device or go via
2934 case PMU_IOC_GET_BACKLIGHT:
2935 if (sleep_in_progress)
2937 error = get_backlight_level();
2940 return put_user(error, argp);
2941 case PMU_IOC_SET_BACKLIGHT:
2944 if (sleep_in_progress)
2946 error = get_user(value, argp);
2948 error = set_backlight_level(value);
2951 #ifdef CONFIG_INPUT_ADBHID
2952 case PMU_IOC_GRAB_BACKLIGHT: {
2953 unsigned long flags;
2954 if (pp->backlight_locker)
2956 pp->backlight_locker = 1;
2957 spin_lock_irqsave(&pmu_lock, flags);
2958 disable_kernel_backlight++;
2959 spin_unlock_irqrestore(&pmu_lock, flags);
2962 #endif /* CONFIG_INPUT_ADBHID */
2963 #endif /* CONFIG_PMAC_BACKLIGHT */
2964 case PMU_IOC_GET_MODEL:
2965 return put_user(pmu_kind, argp);
2966 case PMU_IOC_HAS_ADB:
2967 return put_user(pmu_has_adb, argp);
2972 static struct file_operations pmu_device_fops __pmacdata = {
2978 .release = pmu_release,
2981 static struct miscdevice pmu_device __pmacdata = {
2982 PMU_MINOR, "pmu", &pmu_device_fops
2985 void pmu_device_init(void)
2989 if (misc_register(&pmu_device) < 0)
2990 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2992 #endif /* CONFIG_PMAC_PBOOK */
2995 static inline void __pmac
2996 polled_handshake(volatile unsigned char __iomem *via)
2998 via[B] &= ~TREQ; eieio();
2999 while ((via[B] & TACK) != 0)
3001 via[B] |= TREQ; eieio();
3002 while ((via[B] & TACK) == 0)
3006 static inline void __pmac
3007 polled_send_byte(volatile unsigned char __iomem *via, int x)
3009 via[ACR] |= SR_OUT | SR_EXT; eieio();
3010 via[SR] = x; eieio();
3011 polled_handshake(via);
3014 static inline int __pmac
3015 polled_recv_byte(volatile unsigned char __iomem *via)
3019 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
3020 x = via[SR]; eieio();
3021 polled_handshake(via);
3022 x = via[SR]; eieio();
3027 pmu_polled_request(struct adb_request *req)
3029 unsigned long flags;
3031 volatile unsigned char __iomem *v = via;
3035 l = pmu_data_len[c][0];
3036 if (l >= 0 && req->nbytes != l + 1)
3039 local_irq_save(flags);
3040 while (pmu_state != idle)
3043 while ((via[B] & TACK) == 0)
3045 polled_send_byte(v, c);
3047 l = req->nbytes - 1;
3048 polled_send_byte(v, l);
3050 for (i = 1; i <= l; ++i)
3051 polled_send_byte(v, req->data[i]);
3053 l = pmu_data_len[c][1];
3055 l = polled_recv_byte(v);
3056 for (i = 0; i < l; ++i)
3057 req->reply[i + req->reply_len] = polled_recv_byte(v);
3062 local_irq_restore(flags);
3065 #endif /* DEBUG_SLEEP */
3068 /* FIXME: This is a temporary set of callbacks to enable us
3069 * to do suspend-to-disk.
3074 static int pmu_sys_suspended = 0;
3076 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
3078 if (state != PM_SUSPEND_DISK || pmu_sys_suspended)
3081 /* Suspend PMU event interrupts */
3084 pmu_sys_suspended = 1;
3088 static int pmu_sys_resume(struct sys_device *sysdev)
3090 struct adb_request req;
3092 if (!pmu_sys_suspended)
3095 /* Tell PMU we are ready */
3096 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
3097 pmu_wait_complete(&req);
3099 /* Resume PMU event interrupts */
3102 pmu_sys_suspended = 0;
3107 #endif /* CONFIG_PM */
3109 static struct sysdev_class pmu_sysclass = {
3110 set_kset_name("pmu"),
3113 static struct sys_device device_pmu = {
3115 .cls = &pmu_sysclass,
3118 static struct sysdev_driver driver_pmu = {
3120 .suspend = &pmu_sys_suspend,
3121 .resume = &pmu_sys_resume,
3122 #endif /* CONFIG_PM */
3125 static int __init init_pmu_sysfs(void)
3129 rc = sysdev_class_register(&pmu_sysclass);
3131 printk(KERN_ERR "Failed registering PMU sys class\n");
3134 rc = sysdev_register(&device_pmu);
3136 printk(KERN_ERR "Failed registering PMU sys device\n");
3139 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
3141 printk(KERN_ERR "Failed registering PMU sys driver\n");
3147 subsys_initcall(init_pmu_sysfs);
3149 EXPORT_SYMBOL(pmu_request);
3150 EXPORT_SYMBOL(pmu_poll);
3151 EXPORT_SYMBOL(pmu_poll_adb);
3152 EXPORT_SYMBOL(pmu_wait_complete);
3153 EXPORT_SYMBOL(pmu_suspend);
3154 EXPORT_SYMBOL(pmu_resume);
3155 EXPORT_SYMBOL(pmu_unlock);
3156 EXPORT_SYMBOL(pmu_i2c_combined_read);
3157 EXPORT_SYMBOL(pmu_i2c_stdsub_write);
3158 EXPORT_SYMBOL(pmu_i2c_simple_read);
3159 EXPORT_SYMBOL(pmu_i2c_simple_write);
3160 #ifdef CONFIG_PMAC_PBOOK
3161 EXPORT_SYMBOL(pmu_register_sleep_notifier);
3162 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
3163 EXPORT_SYMBOL(pmu_enable_irled);
3164 EXPORT_SYMBOL(pmu_battery_count);
3165 EXPORT_SYMBOL(pmu_batteries);
3166 EXPORT_SYMBOL(pmu_power_flags);
3167 #endif /* CONFIG_PMAC_PBOOK */