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>
66 /* Some compile options */
67 #undef SUSPEND_USES_PMU
69 #undef HACKED_PCI_SAVE
71 /* Misc minor number allocated for /dev/pmu */
74 /* How many iterations between battery polls */
75 #define BATTERY_POLLING_COUNT 2
77 static volatile unsigned char __iomem *via;
79 /* VIA registers - spaced 0x200 bytes apart */
80 #define RS 0x200 /* skip between registers */
81 #define B 0 /* B-side data */
82 #define A RS /* A-side data */
83 #define DIRB (2*RS) /* B-side direction (1=output) */
84 #define DIRA (3*RS) /* A-side direction (1=output) */
85 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
86 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
87 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
88 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
89 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
90 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
91 #define SR (10*RS) /* Shift register */
92 #define ACR (11*RS) /* Auxiliary control register */
93 #define PCR (12*RS) /* Peripheral control register */
94 #define IFR (13*RS) /* Interrupt flag register */
95 #define IER (14*RS) /* Interrupt enable register */
96 #define ANH (15*RS) /* A-side data, no handshake */
98 /* Bits in B data register: both active low */
99 #define TACK 0x08 /* Transfer acknowledge (input) */
100 #define TREQ 0x10 /* Transfer request (output) */
103 #define SR_CTRL 0x1c /* Shift register control bits */
104 #define SR_EXT 0x0c /* Shift on external clock */
105 #define SR_OUT 0x10 /* Shift out if 1 */
107 /* Bits in IFR and IER */
108 #define IER_SET 0x80 /* set bits in IER */
109 #define IER_CLR 0 /* clear bits in IER */
110 #define SR_INT 0x04 /* Shift register full/empty */
112 #define CB1_INT 0x10 /* transition on CB1 input */
114 static volatile enum pmu_state {
123 static volatile enum int_data_state {
128 } int_data_state[2] = { int_data_empty, int_data_empty };
130 static struct adb_request *current_req;
131 static struct adb_request *last_req;
132 static struct adb_request *req_awaiting_reply;
133 static unsigned char interrupt_data[2][32];
134 static int interrupt_data_len[2];
135 static int int_data_last;
136 static unsigned char *reply_ptr;
137 static int data_index;
139 static volatile int adb_int_pending;
140 static volatile int disable_poll;
141 static struct adb_request bright_req_1, bright_req_2;
142 static struct device_node *vias;
143 static int pmu_kind = PMU_UNKNOWN;
144 static int pmu_fully_inited = 0;
145 static int pmu_has_adb;
146 static unsigned char __iomem *gpio_reg = NULL;
147 static int gpio_irq = -1;
148 static int gpio_irq_enabled = -1;
149 static volatile int pmu_suspended = 0;
150 static spinlock_t pmu_lock;
151 static u8 pmu_intr_mask;
152 static int pmu_version;
153 static int drop_interrupts;
154 #ifdef CONFIG_PMAC_PBOOK
155 static int option_lid_wakeup = 1;
156 static int sleep_in_progress;
157 #endif /* CONFIG_PMAC_PBOOK */
158 static unsigned long async_req_locks;
159 static unsigned int pmu_irq_stats[11];
161 static struct proc_dir_entry *proc_pmu_root;
162 static struct proc_dir_entry *proc_pmu_info;
163 static struct proc_dir_entry *proc_pmu_irqstats;
164 static struct proc_dir_entry *proc_pmu_options;
165 static int option_server_mode;
167 #ifdef CONFIG_PMAC_PBOOK
168 int pmu_battery_count;
170 unsigned int pmu_power_flags;
171 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
172 static int query_batt_timer = BATTERY_POLLING_COUNT;
173 static struct adb_request batt_req;
174 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
175 #endif /* CONFIG_PMAC_PBOOK */
177 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
178 extern int disable_kernel_backlight;
179 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
183 struct notifier_block *sleep_notifier_list;
186 static int adb_dev_map = 0;
187 static int pmu_adb_flags;
189 static int pmu_probe(void);
190 static int pmu_init(void);
191 static int pmu_send_request(struct adb_request *req, int sync);
192 static int pmu_adb_autopoll(int devs);
193 static int pmu_adb_reset_bus(void);
194 #endif /* CONFIG_ADB */
196 static int init_pmu(void);
197 static int pmu_queue_request(struct adb_request *req);
198 static void pmu_start(void);
199 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
200 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
201 static int proc_get_info(char *page, char **start, off_t off,
202 int count, int *eof, void *data);
203 static int proc_get_irqstats(char *page, char **start, off_t off,
204 int count, int *eof, void *data);
205 #ifdef CONFIG_PMAC_BACKLIGHT
206 static int pmu_set_backlight_level(int level, void* data);
207 static int pmu_set_backlight_enable(int on, int level, void* data);
208 #endif /* CONFIG_PMAC_BACKLIGHT */
209 #ifdef CONFIG_PMAC_PBOOK
210 static void pmu_pass_intr(unsigned char *data, int len);
211 static int proc_get_batt(char *page, char **start, off_t off,
212 int count, int *eof, void *data);
213 #endif /* CONFIG_PMAC_PBOOK */
214 static int proc_read_options(char *page, char **start, off_t off,
215 int count, int *eof, void *data);
216 static int proc_write_options(struct file *file, const char __user *buffer,
217 unsigned long count, void *data);
220 struct adb_driver via_pmu_driver = {
229 #endif /* CONFIG_ADB */
231 extern void low_sleep_handler(void);
232 extern void enable_kernel_altivec(void);
233 extern void enable_kernel_fp(void);
236 int pmu_polled_request(struct adb_request *req);
237 int pmu_wink(struct adb_request *req);
241 * This table indicates for each PMU opcode:
242 * - the number of data bytes to be sent with the command, or -1
243 * if a length byte should be sent,
244 * - the number of response bytes which the PMU will return, or
245 * -1 if it will send a length byte.
247 static const s8 pmu_data_len[256][2] __openfirmwaredata = {
248 /* 0 1 2 3 4 5 6 7 */
249 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
250 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
251 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
253 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
255 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
257 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
259 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
260 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
261 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
263 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
264 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
265 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
267 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
268 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
269 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
270 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
272 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
273 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
274 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
275 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
276 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
277 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
278 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
279 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
280 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
283 static char *pbook_type[] = {
285 "PowerBook 2400/3400/3500(G3)",
286 "PowerBook G3 Series",
291 #ifdef CONFIG_PMAC_BACKLIGHT
292 static struct backlight_controller pmu_backlight_controller = {
293 pmu_set_backlight_enable,
294 pmu_set_backlight_level
296 #endif /* CONFIG_PMAC_BACKLIGHT */
303 vias = find_devices("via-pmu");
307 printk(KERN_WARNING "Warning: only using 1st via-pmu\n");
309 if (vias->n_addrs < 1 || vias->n_intrs < 1) {
310 printk(KERN_ERR "via-pmu: %d addresses, %d interrupts!\n",
311 vias->n_addrs, vias->n_intrs);
312 if (vias->n_addrs < 1 || vias->n_intrs < 1)
316 spin_lock_init(&pmu_lock);
320 pmu_intr_mask = PMU_INT_PCEJECT |
325 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
326 || device_is_compatible(vias->parent, "ohare")))
327 pmu_kind = PMU_OHARE_BASED;
328 else if (device_is_compatible(vias->parent, "paddington"))
329 pmu_kind = PMU_PADDINGTON_BASED;
330 else if (device_is_compatible(vias->parent, "heathrow"))
331 pmu_kind = PMU_HEATHROW_BASED;
332 else if (device_is_compatible(vias->parent, "Keylargo")
333 || device_is_compatible(vias->parent, "K2-Keylargo")) {
334 struct device_node *gpio, *gpiop;
336 pmu_kind = PMU_KEYLARGO_BASED;
337 pmu_has_adb = (find_type_devices("adb") != NULL);
338 pmu_intr_mask = PMU_INT_PCEJECT |
344 gpiop = find_devices("gpio");
345 if (gpiop && gpiop->n_addrs) {
346 gpio_reg = ioremap(gpiop->addrs->address, 0x10);
347 gpio = find_devices("extint-gpio1");
349 gpio = find_devices("pmu-interrupt");
350 if (gpio && gpio->parent == gpiop && gpio->n_intrs)
351 gpio_irq = gpio->intrs[0].line;
354 pmu_kind = PMU_UNKNOWN;
356 via = ioremap(vias->addrs->address, 0x2000);
358 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
359 out_8(&via[IFR], 0x7f); /* clear IFR */
368 printk(KERN_INFO "PMU driver %d initialized for %s, firmware: %02x\n",
369 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
371 sys_ctrler = SYS_CTRLER_PMU;
377 static int __openfirmware
380 return vias == NULL? -ENODEV: 0;
390 #endif /* CONFIG_ADB */
393 * We can't wait until pmu_init gets called, that happens too late.
394 * It happens after IDE and SCSI initialization, which can take a few
395 * seconds, and by that time the PMU could have given up on us and
397 * Thus this is called with arch_initcall rather than device_initcall.
399 static int __init via_pmu_start(void)
404 bright_req_1.complete = 1;
405 bright_req_2.complete = 1;
406 #ifdef CONFIG_PMAC_PBOOK
407 batt_req.complete = 1;
410 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
412 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
413 vias->intrs[0].line);
417 if (pmu_kind == PMU_KEYLARGO_BASED && gpio_irq != -1) {
418 if (request_irq(gpio_irq, gpio1_interrupt, 0, "GPIO1 ADB", (void *)0))
419 printk(KERN_ERR "pmu: can't get irq %d (GPIO1)\n", gpio_irq);
420 gpio_irq_enabled = 1;
423 /* Enable interrupts */
424 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
426 pmu_fully_inited = 1;
428 /* Make sure PMU settle down before continuing. This is _very_ important
429 * since the IDE probe may shut interrupts down for quite a bit of time. If
430 * a PMU communication is pending while this happens, the PMU may timeout
431 * Not that on Core99 machines, the PMU keeps sending us environement
432 * messages, we should find a way to either fix IDE or make it call
433 * pmu_suspend() before masking interrupts. This can also happens while
434 * scolling with some fbdevs.
438 } while (pmu_state != idle);
443 arch_initcall(via_pmu_start);
446 * This has to be done after pci_init, which is a subsys_initcall.
448 static int __init via_pmu_dev_init(void)
454 request_OF_resource(vias, 0, NULL);
456 #ifdef CONFIG_PMAC_BACKLIGHT
457 /* Enable backlight */
458 register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
459 #endif /* CONFIG_PMAC_BACKLIGHT */
461 #ifdef CONFIG_PMAC_PBOOK
462 if (machine_is_compatible("AAPL,3400/2400") ||
463 machine_is_compatible("AAPL,3500")) {
464 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
465 NULL, PMAC_MB_INFO_MODEL, 0);
466 pmu_battery_count = 1;
467 if (mb == PMAC_TYPE_COMET)
468 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
470 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
471 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
472 machine_is_compatible("PowerBook1,1")) {
473 pmu_battery_count = 2;
474 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
475 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
477 struct device_node* prim = find_devices("power-mgt");
478 u32 *prim_info = NULL;
480 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
482 /* Other stuffs here yet unknown */
483 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
484 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
485 if (pmu_battery_count > 1)
486 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
489 #endif /* CONFIG_PMAC_PBOOK */
490 /* Create /proc/pmu */
491 proc_pmu_root = proc_mkdir("pmu", NULL);
493 #ifdef CONFIG_PMAC_PBOOK
496 for (i=0; i<pmu_battery_count; i++) {
498 sprintf(title, "battery_%d", i);
499 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
500 proc_get_batt, (void *)i);
502 #endif /* CONFIG_PMAC_PBOOK */
504 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
505 proc_get_info, NULL);
506 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
507 proc_get_irqstats, NULL);
508 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
509 if (proc_pmu_options) {
510 proc_pmu_options->nlink = 1;
511 proc_pmu_options->read_proc = proc_read_options;
512 proc_pmu_options->write_proc = proc_write_options;
518 device_initcall(via_pmu_dev_init);
520 static int __openfirmware
524 struct adb_request req;
526 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
527 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
529 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
531 while (!req.complete) {
533 printk(KERN_ERR "init_pmu: no response from PMU\n");
540 /* ack all pending interrupts */
542 interrupt_data[0][0] = 1;
543 while (interrupt_data[0][0] || pmu_state != idle) {
545 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
548 if (pmu_state == idle)
550 via_pmu_interrupt(0, NULL, NULL);
554 /* Tell PMU we are ready. */
555 if (pmu_kind == PMU_KEYLARGO_BASED) {
556 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
557 while (!req.complete)
561 /* Read PMU version */
562 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
563 pmu_wait_complete(&req);
564 if (req.reply_len > 0)
565 pmu_version = req.reply[0];
567 /* Read server mode setting */
568 if (pmu_kind == PMU_KEYLARGO_BASED) {
569 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
570 PMU_PWR_GET_POWERUP_EVENTS);
571 pmu_wait_complete(&req);
572 if (req.reply_len == 2) {
573 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
574 option_server_mode = 1;
575 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
576 option_server_mode ? "enabled" : "disabled");
589 static inline void wakeup_decrementer(void)
591 set_dec(tb_ticks_per_jiffy);
592 /* No currently-supported powerbook has a 601,
593 * so use get_tbl, not native
595 last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
599 static void pmu_set_server_mode(int server_mode)
601 struct adb_request req;
603 if (pmu_kind != PMU_KEYLARGO_BASED)
606 option_server_mode = server_mode;
607 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
608 pmu_wait_complete(&req);
609 if (req.reply_len < 2)
612 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
613 PMU_PWR_SET_POWERUP_EVENTS,
614 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
616 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
617 PMU_PWR_CLR_POWERUP_EVENTS,
618 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
619 pmu_wait_complete(&req);
622 #ifdef CONFIG_PMAC_PBOOK
624 /* This new version of the code for 2400/3400/3500 powerbooks
625 * is inspired from the implementation in gkrellm-pmu
628 done_battery_state_ohare(struct adb_request* req)
632 * 0x01 : AC indicator
634 * 0x04 : battery exist
637 * 0x20 : full charged
638 * 0x40 : pcharge reset
639 * 0x80 : battery exist
641 * [1][2] : battery voltage
642 * [3] : CPU temperature
643 * [4] : battery temperature
648 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
649 long pcharge, charge, vb, vmax, lmax;
650 long vmax_charging, vmax_charged;
651 long amperage, voltage, time, max;
652 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
653 NULL, PMAC_MB_INFO_MODEL, 0);
655 if (req->reply[0] & 0x01)
656 pmu_power_flags |= PMU_PWR_AC_PRESENT;
658 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
660 if (mb == PMAC_TYPE_COMET) {
671 /* If battery installed */
672 if (req->reply[0] & 0x04) {
673 bat_flags |= PMU_BATT_PRESENT;
674 if (req->reply[0] & 0x02)
675 bat_flags |= PMU_BATT_CHARGING;
676 vb = (req->reply[1] << 8) | req->reply[2];
677 voltage = (vb * 265 + 72665) / 10;
678 amperage = req->reply[5];
679 if ((req->reply[0] & 0x01) == 0) {
681 vb += ((amperage - 200) * 15)/100;
682 } else if (req->reply[0] & 0x02) {
683 vb = (vb * 97) / 100;
684 vmax = vmax_charging;
686 charge = (100 * vb) / vmax;
687 if (req->reply[0] & 0x40) {
688 pcharge = (req->reply[6] << 8) + req->reply[7];
692 pcharge = 100 - pcharge / lmax;
693 if (pcharge < charge)
697 time = (charge * 16440) / amperage;
701 amperage = -amperage;
703 charge = max = amperage = voltage = time = 0;
705 pmu_batteries[pmu_cur_battery].flags = bat_flags;
706 pmu_batteries[pmu_cur_battery].charge = charge;
707 pmu_batteries[pmu_cur_battery].max_charge = max;
708 pmu_batteries[pmu_cur_battery].amperage = amperage;
709 pmu_batteries[pmu_cur_battery].voltage = voltage;
710 pmu_batteries[pmu_cur_battery].time_remaining = time;
712 clear_bit(0, &async_req_locks);
716 done_battery_state_smart(struct adb_request* req)
719 * [0] : format of this structure (known: 3,4,5)
732 * [4][5] : max charge
737 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
739 unsigned int capa, max, voltage;
741 if (req->reply[1] & 0x01)
742 pmu_power_flags |= PMU_PWR_AC_PRESENT;
744 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
747 capa = max = amperage = voltage = 0;
749 if (req->reply[1] & 0x04) {
750 bat_flags |= PMU_BATT_PRESENT;
751 switch(req->reply[0]) {
753 case 4: capa = req->reply[2];
755 amperage = *((signed char *)&req->reply[4]);
756 voltage = req->reply[5];
758 case 5: capa = (req->reply[2] << 8) | req->reply[3];
759 max = (req->reply[4] << 8) | req->reply[5];
760 amperage = *((signed short *)&req->reply[6]);
761 voltage = (req->reply[8] << 8) | req->reply[9];
764 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
765 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
770 if ((req->reply[1] & 0x01) && (amperage > 0))
771 bat_flags |= PMU_BATT_CHARGING;
773 pmu_batteries[pmu_cur_battery].flags = bat_flags;
774 pmu_batteries[pmu_cur_battery].charge = capa;
775 pmu_batteries[pmu_cur_battery].max_charge = max;
776 pmu_batteries[pmu_cur_battery].amperage = amperage;
777 pmu_batteries[pmu_cur_battery].voltage = voltage;
779 if ((req->reply[1] & 0x01) && (amperage > 0))
780 pmu_batteries[pmu_cur_battery].time_remaining
781 = ((max-capa) * 3600) / amperage;
783 pmu_batteries[pmu_cur_battery].time_remaining
784 = (capa * 3600) / (-amperage);
786 pmu_batteries[pmu_cur_battery].time_remaining = 0;
788 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
790 clear_bit(0, &async_req_locks);
794 query_battery_state(void)
796 if (test_and_set_bit(0, &async_req_locks))
798 if (pmu_kind == PMU_OHARE_BASED)
799 pmu_request(&batt_req, done_battery_state_ohare,
800 1, PMU_BATTERY_STATE);
802 pmu_request(&batt_req, done_battery_state_smart,
803 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
806 #endif /* CONFIG_PMAC_PBOOK */
809 proc_get_info(char *page, char **start, off_t off,
810 int count, int *eof, void *data)
814 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
815 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
816 #ifdef CONFIG_PMAC_PBOOK
817 p += sprintf(p, "AC Power : %d\n",
818 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0));
819 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
820 #endif /* CONFIG_PMAC_PBOOK */
826 proc_get_irqstats(char *page, char **start, off_t off,
827 int count, int *eof, void *data)
831 static const char *irq_names[] = {
832 "Total CB1 triggered events",
833 "Total GPIO1 triggered events",
834 "PC-Card eject button",
835 "Sound/Brightness button",
837 "Battery state change",
838 "Environment interrupt",
840 "Ghost interrupt (zero len)",
841 "Empty interrupt (empty mask)",
845 for (i=0; i<11; i++) {
846 p += sprintf(p, " %2u: %10u (%s)\n",
847 i, pmu_irq_stats[i], irq_names[i]);
852 #ifdef CONFIG_PMAC_PBOOK
854 proc_get_batt(char *page, char **start, off_t off,
855 int count, int *eof, void *data)
857 int batnum = (int)data;
860 p += sprintf(p, "\n");
861 p += sprintf(p, "flags : %08x\n",
862 pmu_batteries[batnum].flags);
863 p += sprintf(p, "charge : %d\n",
864 pmu_batteries[batnum].charge);
865 p += sprintf(p, "max_charge : %d\n",
866 pmu_batteries[batnum].max_charge);
867 p += sprintf(p, "current : %d\n",
868 pmu_batteries[batnum].amperage);
869 p += sprintf(p, "voltage : %d\n",
870 pmu_batteries[batnum].voltage);
871 p += sprintf(p, "time rem. : %d\n",
872 pmu_batteries[batnum].time_remaining);
876 #endif /* CONFIG_PMAC_PBOOK */
879 proc_read_options(char *page, char **start, off_t off,
880 int count, int *eof, void *data)
884 #ifdef CONFIG_PMAC_PBOOK
885 if (pmu_kind == PMU_KEYLARGO_BASED &&
886 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
887 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
888 #endif /* CONFIG_PMAC_PBOOK */
889 if (pmu_kind == PMU_KEYLARGO_BASED)
890 p += sprintf(p, "server_mode=%d\n", option_server_mode);
896 proc_write_options(struct file *file, const char __user *buffer,
897 unsigned long count, void *data)
901 unsigned long fcount = count;
907 if (copy_from_user(tmp, buffer, count))
915 while(*val && (*val != '=')) {
925 #ifdef CONFIG_PMAC_PBOOK
926 if (pmu_kind == PMU_KEYLARGO_BASED &&
927 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
928 if (!strcmp(label, "lid_wakeup"))
929 option_lid_wakeup = ((*val) == '1');
930 #endif /* CONFIG_PMAC_PBOOK */
931 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
933 new_value = ((*val) == '1');
934 if (new_value != option_server_mode)
935 pmu_set_server_mode(new_value);
941 /* Send an ADB command */
943 pmu_send_request(struct adb_request *req, int sync)
947 if ((vias == NULL) || (!pmu_fully_inited)) {
954 switch (req->data[0]) {
956 for (i = 0; i < req->nbytes - 1; ++i)
957 req->data[i] = req->data[i+1];
959 if (pmu_data_len[req->data[0]][1] != 0) {
960 req->reply[0] = ADB_RET_OK;
964 ret = pmu_queue_request(req);
967 switch (req->data[1]) {
969 if (req->nbytes != 2)
971 req->data[0] = PMU_READ_RTC;
974 req->reply[0] = CUDA_PACKET;
976 req->reply[2] = CUDA_GET_TIME;
977 ret = pmu_queue_request(req);
980 if (req->nbytes != 6)
982 req->data[0] = PMU_SET_RTC;
984 for (i = 1; i <= 4; ++i)
985 req->data[i] = req->data[i+1];
987 req->reply[0] = CUDA_PACKET;
989 req->reply[2] = CUDA_SET_TIME;
990 ret = pmu_queue_request(req);
997 for (i = req->nbytes - 1; i > 1; --i)
998 req->data[i+2] = req->data[i];
999 req->data[3] = req->nbytes - 2;
1000 req->data[2] = pmu_adb_flags;
1001 /*req->data[1] = req->data[1];*/
1002 req->data[0] = PMU_ADB_CMD;
1004 req->reply_expected = 1;
1006 ret = pmu_queue_request(req);
1015 while (!req->complete)
1021 /* Enable/disable autopolling */
1023 pmu_adb_autopoll(int devs)
1025 struct adb_request req;
1027 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1032 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1033 adb_dev_map >> 8, adb_dev_map);
1036 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1039 while (!req.complete)
1044 /* Reset the ADB bus */
1046 pmu_adb_reset_bus(void)
1048 struct adb_request req;
1049 int save_autopoll = adb_dev_map;
1051 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1054 /* anyone got a better idea?? */
1055 pmu_adb_autopoll(0);
1059 req.data[0] = PMU_ADB_CMD;
1061 req.data[2] = ADB_BUSRESET;
1065 req.reply_expected = 1;
1066 if (pmu_queue_request(&req) != 0) {
1067 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1070 pmu_wait_complete(&req);
1072 if (save_autopoll != 0)
1073 pmu_adb_autopoll(save_autopoll);
1077 #endif /* CONFIG_ADB */
1079 /* Construct and send a pmu request */
1081 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1090 if (nbytes < 0 || nbytes > 32) {
1091 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1095 req->nbytes = nbytes;
1097 va_start(list, nbytes);
1098 for (i = 0; i < nbytes; ++i)
1099 req->data[i] = va_arg(list, int);
1102 req->reply_expected = 0;
1103 return pmu_queue_request(req);
1107 pmu_queue_request(struct adb_request *req)
1109 unsigned long flags;
1116 if (req->nbytes <= 0) {
1120 nsend = pmu_data_len[req->data[0]][0];
1121 if (nsend >= 0 && req->nbytes != nsend + 1) {
1130 spin_lock_irqsave(&pmu_lock, flags);
1131 if (current_req != 0) {
1132 last_req->next = req;
1137 if (pmu_state == idle)
1140 spin_unlock_irqrestore(&pmu_lock, flags);
1148 /* Sightly increased the delay, I had one occurrence of the message
1152 while ((in_8(&via[B]) & TACK) == 0) {
1153 if (--timeout < 0) {
1154 printk(KERN_ERR "PMU not responding (!ack)\n");
1161 /* New PMU seems to be very sensitive to those timings, so we make sure
1162 * PCI is flushed immediately */
1166 volatile unsigned char __iomem *v = via;
1168 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1170 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1177 volatile unsigned char __iomem *v = via;
1179 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1180 in_8(&v[SR]); /* resets SR */
1181 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1186 pmu_done(struct adb_request *req)
1188 void (*done)(struct adb_request *) = req->done;
1191 /* Here, we assume that if the request has a done member, the
1192 * struct request will survive to setting req->complete to 1
1201 struct adb_request *req;
1203 /* assert pmu_state == idle */
1204 /* get the packet to send */
1206 if (req == 0 || pmu_state != idle
1207 || (/*req->reply_expected && */req_awaiting_reply))
1210 pmu_state = sending;
1212 data_len = pmu_data_len[req->data[0]][0];
1214 /* Sounds safer to make sure ACK is high before writing. This helped
1215 * kill a problem with ADB and some iBooks
1218 /* set the shift register to shift out and send a byte */
1219 send_byte(req->data[0]);
1229 via_pmu_interrupt(0, NULL, NULL);
1239 /* Kicks ADB read when PMU is suspended */
1240 adb_int_pending = 1;
1242 via_pmu_interrupt(0, NULL, NULL);
1243 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1244 || req_awaiting_reply));
1248 pmu_wait_complete(struct adb_request *req)
1252 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1253 via_pmu_interrupt(0, NULL, NULL);
1256 /* This function loops until the PMU is idle and prevents it from
1257 * anwsering to ADB interrupts. pmu_request can still be called.
1258 * This is done to avoid spurrious shutdowns when we know we'll have
1259 * interrupts switched off for a long time
1264 unsigned long flags;
1265 #ifdef SUSPEND_USES_PMU
1266 struct adb_request *req;
1271 spin_lock_irqsave(&pmu_lock, flags);
1273 if (pmu_suspended > 1) {
1274 spin_unlock_irqrestore(&pmu_lock, flags);
1279 spin_unlock_irqrestore(&pmu_lock, flags);
1280 if (req_awaiting_reply)
1281 adb_int_pending = 1;
1282 via_pmu_interrupt(0, NULL, NULL);
1283 spin_lock_irqsave(&pmu_lock, flags);
1284 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1285 #ifdef SUSPEND_USES_PMU
1286 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1287 spin_unlock_irqrestore(&pmu_lock, flags);
1288 while(!req.complete)
1290 #else /* SUSPEND_USES_PMU */
1292 disable_irq_nosync(gpio_irq);
1293 out_8(&via[IER], CB1_INT | IER_CLR);
1294 spin_unlock_irqrestore(&pmu_lock, flags);
1295 #endif /* SUSPEND_USES_PMU */
1304 unsigned long flags;
1306 if (!via || (pmu_suspended < 1))
1309 spin_lock_irqsave(&pmu_lock, flags);
1311 if (pmu_suspended > 0) {
1312 spin_unlock_irqrestore(&pmu_lock, flags);
1315 adb_int_pending = 1;
1316 #ifdef SUSPEND_USES_PMU
1317 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1318 spin_unlock_irqrestore(&pmu_lock, flags);
1319 while(!req.complete)
1321 #else /* SUSPEND_USES_PMU */
1323 enable_irq(gpio_irq);
1324 out_8(&via[IER], CB1_INT | IER_SET);
1325 spin_unlock_irqrestore(&pmu_lock, flags);
1327 #endif /* SUSPEND_USES_PMU */
1330 /* Interrupt data could be the result data from an ADB cmd */
1332 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1334 unsigned char ints, pirq;
1338 if (drop_interrupts || len < 1) {
1339 adb_int_pending = 0;
1344 /* Get PMU interrupt mask */
1347 /* Record zero interrupts for stats */
1351 /* Hack to deal with ADB autopoll flag */
1352 if (ints & PMU_INT_ADB)
1353 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1358 if (i > pmu_irq_stats[10])
1359 pmu_irq_stats[10] = i;
1363 for (pirq = 0; pirq < 8; pirq++)
1364 if (ints & (1 << pirq))
1366 pmu_irq_stats[pirq]++;
1368 ints &= ~(1 << pirq);
1370 /* Note: for some reason, we get an interrupt with len=1,
1371 * data[0]==0 after each normal ADB interrupt, at least
1372 * on the Pismo. Still investigating... --BenH
1374 if ((1 << pirq) & PMU_INT_ADB) {
1375 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1376 struct adb_request *req = req_awaiting_reply;
1378 printk(KERN_ERR "PMU: extra ADB reply\n");
1381 req_awaiting_reply = NULL;
1385 memcpy(req->reply, data + 1, len - 1);
1386 req->reply_len = len - 1;
1390 #if defined(CONFIG_XMON) && !defined(CONFIG_PPC64)
1391 if (len == 4 && data[1] == 0x2c) {
1392 extern int xmon_wants_key, xmon_adb_keycode;
1393 if (xmon_wants_key) {
1394 xmon_adb_keycode = data[2];
1398 #endif /* defined(CONFIG_XMON) && !defined(CONFIG_PPC64) */
1401 * XXX On the [23]400 the PMU gives us an up
1402 * event for keycodes 0x74 or 0x75 when the PC
1403 * card eject buttons are released, so we
1404 * ignore those events.
1406 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1407 && data[1] == 0x2c && data[3] == 0xff
1408 && (data[2] & ~1) == 0xf4))
1409 adb_input(data+1, len-1, regs, 1);
1410 #endif /* CONFIG_ADB */
1413 /* Sound/brightness button pressed */
1414 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1415 #ifdef CONFIG_PMAC_BACKLIGHT
1417 #ifdef CONFIG_INPUT_ADBHID
1418 if (!disable_kernel_backlight)
1419 #endif /* CONFIG_INPUT_ADBHID */
1420 set_backlight_level(data[1] >> 4);
1421 #endif /* CONFIG_PMAC_BACKLIGHT */
1423 /* Tick interrupt */
1424 else if ((1 << pirq) & PMU_INT_TICK) {
1425 #ifdef CONFIG_PMAC_PBOOK
1426 /* Environement or tick interrupt, query batteries */
1427 if (pmu_battery_count) {
1428 if ((--query_batt_timer) == 0) {
1429 query_battery_state();
1430 query_batt_timer = BATTERY_POLLING_COUNT;
1434 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1435 if (pmu_battery_count)
1436 query_battery_state();
1437 pmu_pass_intr(data, len);
1439 pmu_pass_intr(data, len);
1440 #endif /* CONFIG_PMAC_PBOOK */
1445 static struct adb_request* __pmac
1446 pmu_sr_intr(struct pt_regs *regs)
1448 struct adb_request *req;
1451 if (via[B] & TREQ) {
1452 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1453 out_8(&via[IFR], SR_INT);
1456 /* The ack may not yet be low when we get the interrupt */
1457 while ((in_8(&via[B]) & TACK) != 0)
1460 /* if reading grab the byte, and reset the interrupt */
1461 if (pmu_state == reading || pmu_state == reading_intr)
1462 bite = in_8(&via[SR]);
1464 /* reset TREQ and wait for TACK to go high */
1465 out_8(&via[B], in_8(&via[B]) | TREQ);
1468 switch (pmu_state) {
1472 data_len = req->nbytes - 1;
1473 send_byte(data_len);
1476 if (data_index <= data_len) {
1477 send_byte(req->data[data_index++]);
1481 data_len = pmu_data_len[req->data[0]][1];
1482 if (data_len == 0) {
1484 current_req = req->next;
1485 if (req->reply_expected)
1486 req_awaiting_reply = req;
1490 pmu_state = reading;
1492 reply_ptr = req->reply + req->reply_len;
1500 pmu_state = reading_intr;
1501 reply_ptr = interrupt_data[int_data_last];
1503 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1504 enable_irq(gpio_irq);
1505 gpio_irq_enabled = 1;
1511 if (data_len == -1) {
1514 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1515 } else if (data_index < 32) {
1516 reply_ptr[data_index++] = bite;
1518 if (data_index < data_len) {
1523 if (pmu_state == reading_intr) {
1525 int_data_state[int_data_last] = int_data_ready;
1526 interrupt_data_len[int_data_last] = data_len;
1530 * For PMU sleep and freq change requests, we lock the
1531 * PMU until it's explicitely unlocked. This avoids any
1532 * spurrious event polling getting in
1534 current_req = req->next;
1535 req->reply_len += data_index;
1536 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1545 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1551 static irqreturn_t __pmac
1552 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1554 unsigned long flags;
1558 struct adb_request *req = NULL;
1561 /* This is a bit brutal, we can probably do better */
1562 spin_lock_irqsave(&pmu_lock, flags);
1566 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1570 if (++nloop > 1000) {
1571 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1572 "intr=%x, ier=%x pmu_state=%d\n",
1573 intr, in_8(&via[IER]), pmu_state);
1576 out_8(&via[IFR], intr);
1577 if (intr & CB1_INT) {
1578 adb_int_pending = 1;
1581 if (intr & SR_INT) {
1582 req = pmu_sr_intr(regs);
1589 if (pmu_state == idle) {
1590 if (adb_int_pending) {
1591 if (int_data_state[0] == int_data_empty)
1593 else if (int_data_state[1] == int_data_empty)
1598 int_data_state[int_data_last] = int_data_fill;
1599 /* Sounds safer to make sure ACK is high before writing.
1600 * This helped kill a problem with ADB and some iBooks
1603 send_byte(PMU_INT_ACK);
1604 adb_int_pending = 0;
1605 } else if (current_req)
1609 /* Mark the oldest buffer for flushing */
1610 if (int_data_state[!int_data_last] == int_data_ready) {
1611 int_data_state[!int_data_last] = int_data_flush;
1612 int_data = !int_data_last;
1613 } else 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;
1618 spin_unlock_irqrestore(&pmu_lock, flags);
1620 /* Deal with completed PMU requests outside of the lock */
1626 /* Deal with interrupt datas outside of the lock */
1627 if (int_data >= 0) {
1628 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1629 spin_lock_irqsave(&pmu_lock, flags);
1631 int_data_state[int_data] = int_data_empty;
1636 return IRQ_RETVAL(handled);
1642 unsigned long flags;
1644 spin_lock_irqsave(&pmu_lock, flags);
1645 if (pmu_state == locked)
1647 adb_int_pending = 1;
1648 spin_unlock_irqrestore(&pmu_lock, flags);
1652 static irqreturn_t __pmac
1653 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1655 unsigned long flags;
1657 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1658 spin_lock_irqsave(&pmu_lock, flags);
1659 if (gpio_irq_enabled > 0) {
1660 disable_irq_nosync(gpio_irq);
1661 gpio_irq_enabled = 0;
1664 adb_int_pending = 1;
1665 spin_unlock_irqrestore(&pmu_lock, flags);
1666 via_pmu_interrupt(0, NULL, NULL);
1672 #ifdef CONFIG_PMAC_BACKLIGHT
1673 static int backlight_to_bright[] __pmacdata = {
1674 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1675 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1678 static int __openfirmware
1679 pmu_set_backlight_enable(int on, int level, void* data)
1681 struct adb_request req;
1687 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1688 backlight_to_bright[level]);
1689 pmu_wait_complete(&req);
1691 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1692 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1693 pmu_wait_complete(&req);
1698 static void __openfirmware
1699 pmu_bright_complete(struct adb_request *req)
1701 if (req == &bright_req_1)
1702 clear_bit(1, &async_req_locks);
1703 if (req == &bright_req_2)
1704 clear_bit(2, &async_req_locks);
1707 static int __openfirmware
1708 pmu_set_backlight_level(int level, void* data)
1713 if (test_and_set_bit(1, &async_req_locks))
1715 pmu_request(&bright_req_1, pmu_bright_complete, 2, PMU_BACKLIGHT_BRIGHT,
1716 backlight_to_bright[level]);
1717 if (test_and_set_bit(2, &async_req_locks))
1719 pmu_request(&bright_req_2, pmu_bright_complete, 2, PMU_POWER_CTRL,
1720 PMU_POW_BACKLIGHT | (level > BACKLIGHT_OFF ?
1721 PMU_POW_ON : PMU_POW_OFF));
1725 #endif /* CONFIG_PMAC_BACKLIGHT */
1728 pmu_enable_irled(int on)
1730 struct adb_request req;
1734 if (pmu_kind == PMU_KEYLARGO_BASED)
1737 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1738 (on ? PMU_POW_ON : PMU_POW_OFF));
1739 pmu_wait_complete(&req);
1745 struct adb_request req;
1750 local_irq_disable();
1752 drop_interrupts = 1;
1754 if (pmu_kind != PMU_KEYLARGO_BASED) {
1755 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1757 while(!req.complete)
1761 pmu_request(&req, NULL, 1, PMU_RESET);
1762 pmu_wait_complete(&req);
1770 struct adb_request req;
1775 local_irq_disable();
1777 drop_interrupts = 1;
1779 if (pmu_kind != PMU_KEYLARGO_BASED) {
1780 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1782 pmu_wait_complete(&req);
1784 /* Disable server mode on shutdown or we'll just
1787 pmu_set_server_mode(0);
1790 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1791 'M', 'A', 'T', 'T');
1792 pmu_wait_complete(&req);
1803 struct pmu_i2c_hdr {
1814 pmu_i2c_combined_read(int bus, int addr, int subaddr, u8* data, int len)
1816 struct adb_request req;
1817 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1821 for (retry=0; retry<16; retry++) {
1822 memset(&req, 0, sizeof(req));
1825 hdr->address = addr & 0xfe;
1826 hdr->mode = PMU_I2C_MODE_COMBINED;
1828 hdr->sub_addr = subaddr;
1829 hdr->comb_addr = addr | 1;
1832 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1833 req.reply_expected = 0;
1835 req.data[0] = PMU_I2C_CMD;
1836 req.reply[0] = 0xff;
1837 rc = pmu_queue_request(&req);
1840 while(!req.complete)
1842 if (req.reply[0] == PMU_I2C_STATUS_OK)
1846 if (req.reply[0] != PMU_I2C_STATUS_OK)
1849 for (retry=0; retry<16; retry++) {
1850 memset(&req, 0, sizeof(req));
1854 hdr->bus = PMU_I2C_BUS_STATUS;
1855 req.reply[0] = 0xff;
1858 req.reply_expected = 0;
1860 req.data[0] = PMU_I2C_CMD;
1861 rc = pmu_queue_request(&req);
1864 while(!req.complete)
1866 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1867 memcpy(data, &req.reply[1], req.reply_len - 1);
1868 return req.reply_len - 1;
1875 pmu_i2c_stdsub_write(int bus, int addr, int subaddr, u8* data, int len)
1877 struct adb_request req;
1878 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1882 for (retry=0; retry<16; retry++) {
1883 memset(&req, 0, sizeof(req));
1886 hdr->address = addr & 0xfe;
1887 hdr->mode = PMU_I2C_MODE_STDSUB;
1889 hdr->sub_addr = subaddr;
1890 hdr->comb_addr = addr & 0xfe;
1893 req.data[0] = PMU_I2C_CMD;
1894 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
1895 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
1896 req.reply_expected = 0;
1898 req.reply[0] = 0xff;
1899 rc = pmu_queue_request(&req);
1902 while(!req.complete)
1904 if (req.reply[0] == PMU_I2C_STATUS_OK)
1908 if (req.reply[0] != PMU_I2C_STATUS_OK)
1911 for (retry=0; retry<16; retry++) {
1912 memset(&req, 0, sizeof(req));
1916 hdr->bus = PMU_I2C_BUS_STATUS;
1917 req.reply[0] = 0xff;
1920 req.reply_expected = 0;
1922 req.data[0] = PMU_I2C_CMD;
1923 rc = pmu_queue_request(&req);
1926 while(!req.complete)
1928 if (req.reply[0] == PMU_I2C_STATUS_OK)
1935 pmu_i2c_simple_read(int bus, int addr, u8* data, int len)
1937 struct adb_request req;
1938 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1942 for (retry=0; retry<16; retry++) {
1943 memset(&req, 0, sizeof(req));
1946 hdr->address = addr | 1;
1947 hdr->mode = PMU_I2C_MODE_SIMPLE;
1953 req.data[0] = PMU_I2C_CMD;
1954 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1955 req.reply_expected = 0;
1957 req.reply[0] = 0xff;
1958 rc = pmu_queue_request(&req);
1961 while(!req.complete)
1963 if (req.reply[0] == PMU_I2C_STATUS_OK)
1967 if (req.reply[0] != PMU_I2C_STATUS_OK)
1970 for (retry=0; retry<16; retry++) {
1971 memset(&req, 0, sizeof(req));
1975 hdr->bus = PMU_I2C_BUS_STATUS;
1976 req.reply[0] = 0xff;
1979 req.reply_expected = 0;
1981 req.data[0] = PMU_I2C_CMD;
1982 rc = pmu_queue_request(&req);
1985 while(!req.complete)
1987 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1988 memcpy(data, &req.reply[1], req.reply_len - 1);
1989 return req.reply_len - 1;
1996 pmu_i2c_simple_write(int bus, int addr, u8* data, int len)
1998 struct adb_request req;
1999 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
2003 for (retry=0; retry<16; retry++) {
2004 memset(&req, 0, sizeof(req));
2007 hdr->address = addr & 0xfe;
2008 hdr->mode = PMU_I2C_MODE_SIMPLE;
2014 req.data[0] = PMU_I2C_CMD;
2015 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
2016 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
2017 req.reply_expected = 0;
2019 req.reply[0] = 0xff;
2020 rc = pmu_queue_request(&req);
2023 while(!req.complete)
2025 if (req.reply[0] == PMU_I2C_STATUS_OK)
2029 if (req.reply[0] != PMU_I2C_STATUS_OK)
2032 for (retry=0; retry<16; retry++) {
2033 memset(&req, 0, sizeof(req));
2037 hdr->bus = PMU_I2C_BUS_STATUS;
2038 req.reply[0] = 0xff;
2041 req.reply_expected = 0;
2043 req.data[0] = PMU_I2C_CMD;
2044 rc = pmu_queue_request(&req);
2047 while(!req.complete)
2049 if (req.reply[0] == PMU_I2C_STATUS_OK)
2055 #ifdef CONFIG_PMAC_PBOOK
2057 static LIST_HEAD(sleep_notifiers);
2060 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
2062 struct list_head *list;
2063 struct pmu_sleep_notifier *notifier;
2065 for (list = sleep_notifiers.next; list != &sleep_notifiers;
2066 list = list->next) {
2067 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2068 if (n->priority > notifier->priority)
2071 __list_add(&n->list, list->prev, list);
2076 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
2078 if (n->list.next == 0)
2081 n->list.next = NULL;
2085 /* Sleep is broadcast last-to-first */
2087 broadcast_sleep(int when, int fallback)
2089 int ret = PBOOK_SLEEP_OK;
2090 struct list_head *list;
2091 struct pmu_sleep_notifier *notifier;
2093 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
2094 list = list->prev) {
2095 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2096 ret = notifier->notifier_call(notifier, when);
2097 if (ret != PBOOK_SLEEP_OK) {
2098 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
2099 when, notifier, notifier->notifier_call);
2100 for (; list != &sleep_notifiers; list = list->next) {
2101 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2102 notifier->notifier_call(notifier, fallback);
2110 /* Wake is broadcast first-to-last */
2112 broadcast_wake(void)
2114 int ret = PBOOK_SLEEP_OK;
2115 struct list_head *list;
2116 struct pmu_sleep_notifier *notifier;
2118 for (list = sleep_notifiers.next; list != &sleep_notifiers;
2119 list = list->next) {
2120 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2121 notifier->notifier_call(notifier, PBOOK_WAKE);
2127 * This struct is used to store config register values for
2128 * PCI devices which may get powered off when we sleep.
2130 static struct pci_save {
2131 #ifndef HACKED_PCI_SAVE
2140 static int pbook_npci_saves;
2143 pbook_alloc_pci_save(void)
2146 struct pci_dev *pd = NULL;
2149 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2154 pbook_pci_saves = (struct pci_save *)
2155 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
2156 pbook_npci_saves = npci;
2160 pbook_free_pci_save(void)
2162 if (pbook_pci_saves == NULL)
2164 kfree(pbook_pci_saves);
2165 pbook_pci_saves = NULL;
2166 pbook_npci_saves = 0;
2170 pbook_pci_save(void)
2172 struct pci_save *ps = pbook_pci_saves;
2173 struct pci_dev *pd = NULL;
2174 int npci = pbook_npci_saves;
2179 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2182 #ifndef HACKED_PCI_SAVE
2183 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
2184 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
2185 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
2186 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
2190 pci_read_config_dword(pd, i<<4, &ps->config[i]);
2196 /* For this to work, we must take care of a few things: If gmac was enabled
2197 * during boot, it will be in the pci dev list. If it's disabled at this point
2198 * (and it will probably be), then you can't access it's config space.
2201 pbook_pci_restore(void)
2204 struct pci_save *ps = pbook_pci_saves - 1;
2205 struct pci_dev *pd = NULL;
2206 int npci = pbook_npci_saves;
2209 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2210 #ifdef HACKED_PCI_SAVE
2216 pci_write_config_dword(pd, i<<4, ps->config[i]);
2217 pci_write_config_dword(pd, 4, ps->config[1]);
2222 if (ps->command == 0)
2224 pci_read_config_word(pd, PCI_COMMAND, &cmd);
2225 if ((ps->command & ~cmd) == 0)
2227 switch (pd->hdr_type) {
2228 case PCI_HEADER_TYPE_NORMAL:
2229 for (j = 0; j < 6; ++j)
2230 pci_write_config_dword(pd,
2231 PCI_BASE_ADDRESS_0 + j*4,
2232 pd->resource[j].start);
2233 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
2235 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
2237 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
2239 pci_write_config_word(pd, PCI_COMMAND, ps->command);
2247 /* N.B. This doesn't work on the 3400 */
2251 struct adb_request req;
2253 memset(&req, 0, sizeof(req));
2255 for (; n > 0; --n) {
2262 req.reply[0] = ADB_RET_OK;
2264 req.reply_expected = 0;
2265 pmu_polled_request(&req);
2273 req.reply[0] = ADB_RET_OK;
2275 req.reply_expected = 0;
2276 pmu_polled_request(&req);
2284 * Put the powerbook to sleep.
2287 static u32 save_via[8] __pmacdata;
2290 save_via_state(void)
2292 save_via[0] = in_8(&via[ANH]);
2293 save_via[1] = in_8(&via[DIRA]);
2294 save_via[2] = in_8(&via[B]);
2295 save_via[3] = in_8(&via[DIRB]);
2296 save_via[4] = in_8(&via[PCR]);
2297 save_via[5] = in_8(&via[ACR]);
2298 save_via[6] = in_8(&via[T1CL]);
2299 save_via[7] = in_8(&via[T1CH]);
2302 restore_via_state(void)
2304 out_8(&via[ANH], save_via[0]);
2305 out_8(&via[DIRA], save_via[1]);
2306 out_8(&via[B], save_via[2]);
2307 out_8(&via[DIRB], save_via[3]);
2308 out_8(&via[PCR], save_via[4]);
2309 out_8(&via[ACR], save_via[5]);
2310 out_8(&via[T1CL], save_via[6]);
2311 out_8(&via[T1CH], save_via[7]);
2312 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
2313 out_8(&via[IFR], 0x7f); /* clear IFR */
2314 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2318 pmac_suspend_devices(void)
2322 pm_prepare_console();
2324 /* Notify old-style device drivers & userland */
2325 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2326 if (ret != PBOOK_SLEEP_OK) {
2327 printk(KERN_ERR "Sleep rejected by drivers\n");
2331 /* Sync the disks. */
2332 /* XXX It would be nice to have some way to ensure that
2333 * nobody is dirtying any new buffers while we wait. That
2334 * could be achieved using the refrigerator for processes
2339 /* Sleep can fail now. May not be very robust but useful for debugging */
2340 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2341 if (ret != PBOOK_SLEEP_OK) {
2342 printk(KERN_ERR "Driver sleep failed\n");
2346 /* Send suspend call to devices, hold the device core's dpm_sem */
2347 ret = device_suspend(PMSG_SUSPEND);
2350 printk(KERN_ERR "Driver sleep failed\n");
2354 /* Disable clock spreading on some machines */
2355 pmac_tweak_clock_spreading(0);
2357 /* Stop preemption */
2360 /* Make sure the decrementer won't interrupt us */
2361 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2362 /* Make sure any pending DEC interrupt occurring while we did
2363 * the above didn't re-enable the DEC */
2365 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2367 /* We can now disable MSR_EE. This code of course works properly only
2368 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2369 * stop the "other" CPUs way before we do all that stuff.
2371 local_irq_disable();
2373 /* Broadcast power down irq
2374 * This isn't that useful in most cases (only directly wired devices can
2375 * use this but still... This will take care of sysdev's as well, so
2376 * we exit from here with local irqs disabled and PIC off.
2378 ret = device_power_down(PMSG_SUSPEND);
2380 wakeup_decrementer();
2385 printk(KERN_ERR "Driver powerdown failed\n");
2389 /* Wait for completion of async backlight requests */
2390 while (!bright_req_1.complete || !bright_req_2.complete ||
2394 /* Giveup the lazy FPU & vec so we don't have to back them
2395 * up from the low level code
2399 #ifdef CONFIG_ALTIVEC
2400 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2401 enable_kernel_altivec();
2402 #endif /* CONFIG_ALTIVEC */
2408 pmac_wakeup_devices(void)
2412 /* Power back up system devices (including the PIC) */
2415 /* Force a poll of ADB interrupts */
2416 adb_int_pending = 1;
2417 via_pmu_interrupt(0, NULL, NULL);
2419 /* Restart jiffies & scheduling */
2420 wakeup_decrementer();
2422 /* Re-enable local CPU interrupts */
2427 /* Re-enable clock spreading on some machines */
2428 pmac_tweak_clock_spreading(1);
2430 /* Resume devices */
2433 /* Notify old style drivers */
2436 pm_restore_console();
2441 #define GRACKLE_PM (1<<7)
2442 #define GRACKLE_DOZE (1<<5)
2443 #define GRACKLE_NAP (1<<4)
2444 #define GRACKLE_SLEEP (1<<3)
2447 powerbook_sleep_grackle(void)
2449 unsigned long save_l2cr;
2450 unsigned short pmcr1;
2451 struct adb_request req;
2453 struct pci_dev *grackle;
2455 grackle = pci_find_slot(0, 0);
2459 ret = pmac_suspend_devices();
2461 printk(KERN_ERR "Sleep rejected by devices\n");
2465 /* Turn off various things. Darwin does some retry tests here... */
2466 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2467 pmu_wait_complete(&req);
2468 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2469 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2470 pmu_wait_complete(&req);
2472 /* For 750, save backside cache setting and disable it */
2473 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2475 if (!__fake_sleep) {
2476 /* Ask the PMU to put us to sleep */
2477 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2478 pmu_wait_complete(&req);
2481 /* The VIA is supposed not to be restored correctly*/
2483 /* We shut down some HW */
2484 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2486 pci_read_config_word(grackle, 0x70, &pmcr1);
2487 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2488 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2489 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2490 pci_write_config_word(grackle, 0x70, pmcr1);
2492 /* Call low-level ASM sleep handler */
2496 low_sleep_handler();
2498 /* We're awake again, stop grackle PM */
2499 pci_read_config_word(grackle, 0x70, &pmcr1);
2500 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2501 pci_write_config_word(grackle, 0x70, pmcr1);
2503 /* Make sure the PMU is idle */
2504 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2505 restore_via_state();
2507 /* Restore L2 cache */
2508 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2509 _set_L2CR(save_l2cr);
2511 /* Restore userland MMU context */
2512 set_context(current->active_mm->context, current->active_mm->pgd);
2514 /* Power things up */
2516 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2517 pmu_wait_complete(&req);
2518 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2519 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2520 pmu_wait_complete(&req);
2521 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2522 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2523 pmu_wait_complete(&req);
2525 pmac_wakeup_devices();
2531 powerbook_sleep_Core99(void)
2533 unsigned long save_l2cr;
2534 unsigned long save_l3cr;
2535 struct adb_request req;
2538 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2539 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2543 if (num_online_cpus() > 1 || cpu_is_offline(0))
2546 ret = pmac_suspend_devices();
2548 printk(KERN_ERR "Sleep rejected by devices\n");
2552 /* Stop environment and ADB interrupts */
2553 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2554 pmu_wait_complete(&req);
2556 /* Tell PMU what events will wake us up */
2557 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2559 pmu_wait_complete(&req);
2560 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2561 0, PMU_PWR_WAKEUP_KEY |
2562 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2563 pmu_wait_complete(&req);
2565 /* Save the state of the L2 and L3 caches */
2566 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2567 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2569 if (!__fake_sleep) {
2570 /* Ask the PMU to put us to sleep */
2571 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2572 pmu_wait_complete(&req);
2575 /* The VIA is supposed not to be restored correctly*/
2578 /* Shut down various ASICs. There's a chance that we can no longer
2579 * talk to the PMU after this, so I moved it to _after_ sending the
2580 * sleep command to it. Still need to be checked.
2582 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2584 /* Call low-level ASM sleep handler */
2588 low_sleep_handler();
2590 /* Restore Apple core ASICs state */
2591 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2594 restore_via_state();
2597 pmac_call_early_video_resume();
2599 /* Restore L2 cache */
2600 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2601 _set_L2CR(save_l2cr);
2602 /* Restore L3 cache */
2603 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2604 _set_L3CR(save_l3cr);
2606 /* Restore userland MMU context */
2607 set_context(current->active_mm->context, current->active_mm->pgd);
2609 /* Tell PMU we are ready */
2611 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2612 pmu_wait_complete(&req);
2613 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2614 pmu_wait_complete(&req);
2616 pmac_wakeup_devices();
2621 #define PB3400_MEM_CTRL 0xf8000000
2622 #define PB3400_MEM_CTRL_SLEEP 0x70
2625 powerbook_sleep_3400(void)
2630 struct adb_request sleep_req;
2631 void __iomem *mem_ctrl;
2632 unsigned int __iomem *mem_ctrl_sleep;
2634 /* first map in the memory controller registers */
2635 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2636 if (mem_ctrl == NULL) {
2637 printk("powerbook_sleep_3400: ioremap failed\n");
2640 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2642 /* Allocate room for PCI save */
2643 pbook_alloc_pci_save();
2645 ret = pmac_suspend_devices();
2647 pbook_free_pci_save();
2648 printk(KERN_ERR "Sleep rejected by devices\n");
2652 /* Save the state of PCI config space for some slots */
2655 /* Set the memory controller to keep the memory refreshed
2656 while we're asleep */
2657 for (i = 0x403f; i >= 0x4000; --i) {
2658 out_be32(mem_ctrl_sleep, i);
2660 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2666 /* Ask the PMU to put us to sleep */
2667 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2668 while (!sleep_req.complete)
2671 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2673 /* displacement-flush the L2 cache - necessary? */
2674 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2675 i = *(volatile int *)p;
2678 /* Put the CPU into sleep mode */
2679 asm volatile("mfspr %0,1008" : "=r" (hid0) :);
2680 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2681 asm volatile("mtspr 1008,%0" : : "r" (hid0));
2682 _nmask_and_or_msr(0, MSR_POW | MSR_EE);
2685 /* OK, we're awake again, start restoring things */
2686 out_be32(mem_ctrl_sleep, 0x3f);
2687 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2688 pbook_pci_restore();
2691 /* wait for the PMU interrupt sequence to complete */
2695 pmac_wakeup_devices();
2696 pbook_free_pci_save();
2703 * Support for /dev/pmu device
2705 #define RB_SIZE 0x10
2706 struct pmu_private {
2707 struct list_head list;
2712 unsigned char data[16];
2714 wait_queue_head_t wait;
2716 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2717 int backlight_locker;
2718 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2721 static LIST_HEAD(all_pmu_pvt);
2722 static DEFINE_SPINLOCK(all_pvt_lock __pmacdata);
2725 pmu_pass_intr(unsigned char *data, int len)
2727 struct pmu_private *pp;
2728 struct list_head *list;
2730 unsigned long flags;
2732 if (len > sizeof(pp->rb_buf[0].data))
2733 len = sizeof(pp->rb_buf[0].data);
2734 spin_lock_irqsave(&all_pvt_lock, flags);
2735 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2736 pp = list_entry(list, struct pmu_private, list);
2737 spin_lock(&pp->lock);
2741 if (i != pp->rb_get) {
2742 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2744 memcpy(rp->data, data, len);
2746 wake_up_interruptible(&pp->wait);
2748 spin_unlock(&pp->lock);
2750 spin_unlock_irqrestore(&all_pvt_lock, flags);
2754 pmu_open(struct inode *inode, struct file *file)
2756 struct pmu_private *pp;
2757 unsigned long flags;
2759 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2762 pp->rb_get = pp->rb_put = 0;
2763 spin_lock_init(&pp->lock);
2764 init_waitqueue_head(&pp->wait);
2765 spin_lock_irqsave(&all_pvt_lock, flags);
2766 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2767 pp->backlight_locker = 0;
2768 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2769 list_add(&pp->list, &all_pmu_pvt);
2770 spin_unlock_irqrestore(&all_pvt_lock, flags);
2771 file->private_data = pp;
2775 static ssize_t __pmac
2776 pmu_read(struct file *file, char __user *buf,
2777 size_t count, loff_t *ppos)
2779 struct pmu_private *pp = file->private_data;
2780 DECLARE_WAITQUEUE(wait, current);
2781 unsigned long flags;
2784 if (count < 1 || pp == 0)
2786 if (!access_ok(VERIFY_WRITE, buf, count))
2789 spin_lock_irqsave(&pp->lock, flags);
2790 add_wait_queue(&pp->wait, &wait);
2791 current->state = TASK_INTERRUPTIBLE;
2795 if (pp->rb_get != pp->rb_put) {
2797 struct rb_entry *rp = &pp->rb_buf[i];
2799 spin_unlock_irqrestore(&pp->lock, flags);
2802 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2806 spin_lock_irqsave(&pp->lock, flags);
2811 if (file->f_flags & O_NONBLOCK)
2814 if (signal_pending(current))
2816 spin_unlock_irqrestore(&pp->lock, flags);
2818 spin_lock_irqsave(&pp->lock, flags);
2820 current->state = TASK_RUNNING;
2821 remove_wait_queue(&pp->wait, &wait);
2822 spin_unlock_irqrestore(&pp->lock, flags);
2827 static ssize_t __pmac
2828 pmu_write(struct file *file, const char __user *buf,
2829 size_t count, loff_t *ppos)
2834 static unsigned int __pmac
2835 pmu_fpoll(struct file *filp, poll_table *wait)
2837 struct pmu_private *pp = filp->private_data;
2838 unsigned int mask = 0;
2839 unsigned long flags;
2843 poll_wait(filp, &pp->wait, wait);
2844 spin_lock_irqsave(&pp->lock, flags);
2845 if (pp->rb_get != pp->rb_put)
2847 spin_unlock_irqrestore(&pp->lock, flags);
2852 pmu_release(struct inode *inode, struct file *file)
2854 struct pmu_private *pp = file->private_data;
2855 unsigned long flags;
2859 file->private_data = NULL;
2860 spin_lock_irqsave(&all_pvt_lock, flags);
2861 list_del(&pp->list);
2862 spin_unlock_irqrestore(&all_pvt_lock, flags);
2863 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2864 if (pp->backlight_locker) {
2865 spin_lock_irqsave(&pmu_lock, flags);
2866 disable_kernel_backlight--;
2867 spin_unlock_irqrestore(&pmu_lock, flags);
2869 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2876 /* Note: removed __openfirmware here since it causes link errors */
2878 pmu_ioctl(struct inode * inode, struct file *filp,
2879 u_int cmd, u_long arg)
2881 struct pmu_private *pp = filp->private_data;
2882 __u32 __user *argp = (__u32 __user *)arg;
2887 if (!capable(CAP_SYS_ADMIN))
2889 if (sleep_in_progress)
2891 sleep_in_progress = 1;
2893 case PMU_OHARE_BASED:
2894 error = powerbook_sleep_3400();
2896 case PMU_HEATHROW_BASED:
2897 case PMU_PADDINGTON_BASED:
2898 error = powerbook_sleep_grackle();
2900 case PMU_KEYLARGO_BASED:
2901 error = powerbook_sleep_Core99();
2906 sleep_in_progress = 0;
2908 case PMU_IOC_CAN_SLEEP:
2909 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2910 return put_user(0, argp);
2912 return put_user(1, argp);
2914 #ifdef CONFIG_PMAC_BACKLIGHT
2915 /* Backlight should have its own device or go via
2918 case PMU_IOC_GET_BACKLIGHT:
2919 if (sleep_in_progress)
2921 error = get_backlight_level();
2924 return put_user(error, argp);
2925 case PMU_IOC_SET_BACKLIGHT:
2928 if (sleep_in_progress)
2930 error = get_user(value, argp);
2932 error = set_backlight_level(value);
2935 #ifdef CONFIG_INPUT_ADBHID
2936 case PMU_IOC_GRAB_BACKLIGHT: {
2937 unsigned long flags;
2938 if (pp->backlight_locker)
2940 pp->backlight_locker = 1;
2941 spin_lock_irqsave(&pmu_lock, flags);
2942 disable_kernel_backlight++;
2943 spin_unlock_irqrestore(&pmu_lock, flags);
2946 #endif /* CONFIG_INPUT_ADBHID */
2947 #endif /* CONFIG_PMAC_BACKLIGHT */
2948 case PMU_IOC_GET_MODEL:
2949 return put_user(pmu_kind, argp);
2950 case PMU_IOC_HAS_ADB:
2951 return put_user(pmu_has_adb, argp);
2956 static struct file_operations pmu_device_fops __pmacdata = {
2962 .release = pmu_release,
2965 static struct miscdevice pmu_device __pmacdata = {
2966 PMU_MINOR, "pmu", &pmu_device_fops
2969 void pmu_device_init(void)
2973 if (misc_register(&pmu_device) < 0)
2974 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2976 #endif /* CONFIG_PMAC_PBOOK */
2979 static inline void __pmac
2980 polled_handshake(volatile unsigned char __iomem *via)
2982 via[B] &= ~TREQ; eieio();
2983 while ((via[B] & TACK) != 0)
2985 via[B] |= TREQ; eieio();
2986 while ((via[B] & TACK) == 0)
2990 static inline void __pmac
2991 polled_send_byte(volatile unsigned char __iomem *via, int x)
2993 via[ACR] |= SR_OUT | SR_EXT; eieio();
2994 via[SR] = x; eieio();
2995 polled_handshake(via);
2998 static inline int __pmac
2999 polled_recv_byte(volatile unsigned char __iomem *via)
3003 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
3004 x = via[SR]; eieio();
3005 polled_handshake(via);
3006 x = via[SR]; eieio();
3011 pmu_polled_request(struct adb_request *req)
3013 unsigned long flags;
3015 volatile unsigned char __iomem *v = via;
3019 l = pmu_data_len[c][0];
3020 if (l >= 0 && req->nbytes != l + 1)
3023 local_irq_save(flags);
3024 while (pmu_state != idle)
3027 while ((via[B] & TACK) == 0)
3029 polled_send_byte(v, c);
3031 l = req->nbytes - 1;
3032 polled_send_byte(v, l);
3034 for (i = 1; i <= l; ++i)
3035 polled_send_byte(v, req->data[i]);
3037 l = pmu_data_len[c][1];
3039 l = polled_recv_byte(v);
3040 for (i = 0; i < l; ++i)
3041 req->reply[i + req->reply_len] = polled_recv_byte(v);
3046 local_irq_restore(flags);
3049 #endif /* DEBUG_SLEEP */
3052 /* FIXME: This is a temporary set of callbacks to enable us
3053 * to do suspend-to-disk.
3058 static int pmu_sys_suspended = 0;
3060 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
3062 if (state != PM_SUSPEND_DISK || pmu_sys_suspended)
3065 /* Suspend PMU event interrupts */
3068 pmu_sys_suspended = 1;
3072 static int pmu_sys_resume(struct sys_device *sysdev)
3074 struct adb_request req;
3076 if (!pmu_sys_suspended)
3079 /* Tell PMU we are ready */
3080 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
3081 pmu_wait_complete(&req);
3083 /* Resume PMU event interrupts */
3086 pmu_sys_suspended = 0;
3091 #endif /* CONFIG_PM */
3093 static struct sysdev_class pmu_sysclass = {
3094 set_kset_name("pmu"),
3097 static struct sys_device device_pmu = {
3099 .cls = &pmu_sysclass,
3102 static struct sysdev_driver driver_pmu = {
3104 .suspend = &pmu_sys_suspend,
3105 .resume = &pmu_sys_resume,
3106 #endif /* CONFIG_PM */
3109 static int __init init_pmu_sysfs(void)
3113 rc = sysdev_class_register(&pmu_sysclass);
3115 printk(KERN_ERR "Failed registering PMU sys class\n");
3118 rc = sysdev_register(&device_pmu);
3120 printk(KERN_ERR "Failed registering PMU sys device\n");
3123 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
3125 printk(KERN_ERR "Failed registering PMU sys driver\n");
3131 subsys_initcall(init_pmu_sysfs);
3133 EXPORT_SYMBOL(pmu_request);
3134 EXPORT_SYMBOL(pmu_poll);
3135 EXPORT_SYMBOL(pmu_poll_adb);
3136 EXPORT_SYMBOL(pmu_wait_complete);
3137 EXPORT_SYMBOL(pmu_suspend);
3138 EXPORT_SYMBOL(pmu_resume);
3139 EXPORT_SYMBOL(pmu_unlock);
3140 EXPORT_SYMBOL(pmu_i2c_combined_read);
3141 EXPORT_SYMBOL(pmu_i2c_stdsub_write);
3142 EXPORT_SYMBOL(pmu_i2c_simple_read);
3143 EXPORT_SYMBOL(pmu_i2c_simple_write);
3144 #ifdef CONFIG_PMAC_PBOOK
3145 EXPORT_SYMBOL(pmu_register_sleep_notifier);
3146 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
3147 EXPORT_SYMBOL(pmu_enable_irled);
3148 EXPORT_SYMBOL(pmu_battery_count);
3149 EXPORT_SYMBOL(pmu_batteries);
3150 EXPORT_SYMBOL(pmu_power_flags);
3151 #endif /* CONFIG_PMAC_PBOOK */