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 struct device_node *gpio_node;
151 static unsigned char __iomem *gpio_reg = NULL;
152 static int gpio_irq = -1;
153 static int gpio_irq_enabled = -1;
154 static volatile int pmu_suspended = 0;
155 static spinlock_t pmu_lock;
156 static u8 pmu_intr_mask;
157 static int pmu_version;
158 static int drop_interrupts;
159 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
160 static int option_lid_wakeup = 1;
161 static int sleep_in_progress;
162 #endif /* CONFIG_PM && CONFIG_PPC32 */
163 static unsigned long async_req_locks;
164 static unsigned int pmu_irq_stats[11];
166 static struct proc_dir_entry *proc_pmu_root;
167 static struct proc_dir_entry *proc_pmu_info;
168 static struct proc_dir_entry *proc_pmu_irqstats;
169 static struct proc_dir_entry *proc_pmu_options;
170 static int option_server_mode;
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];
180 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
181 extern int disable_kernel_backlight;
182 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
186 struct notifier_block *sleep_notifier_list;
189 static int adb_dev_map = 0;
190 static int pmu_adb_flags;
192 static int pmu_probe(void);
193 static int pmu_init(void);
194 static int pmu_send_request(struct adb_request *req, int sync);
195 static int pmu_adb_autopoll(int devs);
196 static int pmu_adb_reset_bus(void);
197 #endif /* CONFIG_ADB */
199 static int init_pmu(void);
200 static int pmu_queue_request(struct adb_request *req);
201 static void pmu_start(void);
202 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
203 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
204 static int proc_get_info(char *page, char **start, off_t off,
205 int count, int *eof, void *data);
206 static int proc_get_irqstats(char *page, char **start, off_t off,
207 int count, int *eof, void *data);
208 #ifdef CONFIG_PMAC_BACKLIGHT
209 static int pmu_set_backlight_level(int level, void* data);
210 static int pmu_set_backlight_enable(int on, int level, void* data);
211 #endif /* CONFIG_PMAC_BACKLIGHT */
212 static void pmu_pass_intr(unsigned char *data, int len);
213 static int proc_get_batt(char *page, char **start, off_t off,
214 int count, int *eof, void *data);
215 static int proc_read_options(char *page, char **start, off_t off,
216 int count, int *eof, void *data);
217 static int proc_write_options(struct file *file, const char __user *buffer,
218 unsigned long count, void *data);
221 struct adb_driver via_pmu_driver = {
230 #endif /* CONFIG_ADB */
232 extern void low_sleep_handler(void);
233 extern void enable_kernel_altivec(void);
234 extern void enable_kernel_fp(void);
237 int pmu_polled_request(struct adb_request *req);
238 int pmu_wink(struct adb_request *req);
242 * This table indicates for each PMU opcode:
243 * - the number of data bytes to be sent with the command, or -1
244 * if a length byte should be sent,
245 * - the number of response bytes which the PMU will return, or
246 * -1 if it will send a length byte.
248 static const s8 pmu_data_len[256][2] = {
249 /* 0 1 2 3 4 5 6 7 */
250 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
251 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
252 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
253 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
254 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
255 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
256 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
257 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
258 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
259 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
260 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
261 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
262 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
263 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
264 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
265 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
266 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
267 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
268 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
269 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
270 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
271 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
272 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
273 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
274 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
275 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
276 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
277 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
278 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
279 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
280 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
281 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
284 static char *pbook_type[] = {
286 "PowerBook 2400/3400/3500(G3)",
287 "PowerBook G3 Series",
292 #ifdef CONFIG_PMAC_BACKLIGHT
293 static struct backlight_controller pmu_backlight_controller = {
294 pmu_set_backlight_enable,
295 pmu_set_backlight_level
297 #endif /* CONFIG_PMAC_BACKLIGHT */
299 int __init find_via_pmu(void)
306 vias = of_find_node_by_name(NULL, "via-pmu");
310 reg = (u32 *)get_property(vias, "reg", NULL);
312 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
315 taddr = of_translate_address(vias, reg);
316 if (taddr == OF_BAD_ADDR) {
317 printk(KERN_ERR "via-pmu: Can't translate address !\n");
321 spin_lock_init(&pmu_lock);
325 pmu_intr_mask = PMU_INT_PCEJECT |
330 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
331 || device_is_compatible(vias->parent, "ohare")))
332 pmu_kind = PMU_OHARE_BASED;
333 else if (device_is_compatible(vias->parent, "paddington"))
334 pmu_kind = PMU_PADDINGTON_BASED;
335 else if (device_is_compatible(vias->parent, "heathrow"))
336 pmu_kind = PMU_HEATHROW_BASED;
337 else if (device_is_compatible(vias->parent, "Keylargo")
338 || device_is_compatible(vias->parent, "K2-Keylargo")) {
339 struct device_node *gpiop;
340 phys_addr_t gaddr = 0;
342 pmu_kind = PMU_KEYLARGO_BASED;
343 pmu_has_adb = (find_type_devices("adb") != NULL);
344 pmu_intr_mask = PMU_INT_PCEJECT |
350 gpiop = of_find_node_by_name(NULL, "gpio");
352 reg = (u32 *)get_property(gpiop, "reg", NULL);
354 gaddr = of_translate_address(gpiop, reg);
356 gpio_reg = ioremap(gaddr, 0x10);
358 if (gpio_reg == NULL)
359 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
361 pmu_kind = PMU_UNKNOWN;
363 via = ioremap(taddr, 0x2000);
365 printk(KERN_ERR "via-pmu: Can't map address !\n");
369 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
370 out_8(&via[IFR], 0x7f); /* clear IFR */
379 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
380 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
382 sys_ctrler = SYS_CTRLER_PMU;
392 static int pmu_probe(void)
394 return vias == NULL? -ENODEV: 0;
397 static int __init pmu_init(void)
403 #endif /* CONFIG_ADB */
406 * We can't wait until pmu_init gets called, that happens too late.
407 * It happens after IDE and SCSI initialization, which can take a few
408 * seconds, and by that time the PMU could have given up on us and
410 * Thus this is called with arch_initcall rather than device_initcall.
412 static int __init via_pmu_start(void)
417 bright_req_1.complete = 1;
418 bright_req_2.complete = 1;
419 batt_req.complete = 1;
421 #ifndef CONFIG_PPC_MERGE
422 if (pmu_kind == PMU_KEYLARGO_BASED)
423 openpic_set_irq_priority(vias->intrs[0].line,
424 OPENPIC_PRIORITY_DEFAULT + 1);
427 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
429 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
430 vias->intrs[0].line);
434 if (pmu_kind == PMU_KEYLARGO_BASED) {
435 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
436 if (gpio_node == NULL)
437 gpio_node = of_find_node_by_name(NULL,
439 if (gpio_node && gpio_node->n_intrs > 0)
440 gpio_irq = gpio_node->intrs[0].line;
442 if (gpio_irq != -1) {
443 if (request_irq(gpio_irq, gpio1_interrupt, 0,
444 "GPIO1 ADB", (void *)0))
445 printk(KERN_ERR "pmu: can't get irq %d"
446 " (GPIO1)\n", gpio_irq);
448 gpio_irq_enabled = 1;
452 /* Enable interrupts */
453 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
455 pmu_fully_inited = 1;
457 /* Make sure PMU settle down before continuing. This is _very_ important
458 * since the IDE probe may shut interrupts down for quite a bit of time. If
459 * a PMU communication is pending while this happens, the PMU may timeout
460 * Not that on Core99 machines, the PMU keeps sending us environement
461 * messages, we should find a way to either fix IDE or make it call
462 * pmu_suspend() before masking interrupts. This can also happens while
463 * scolling with some fbdevs.
467 } while (pmu_state != idle);
472 arch_initcall(via_pmu_start);
475 * This has to be done after pci_init, which is a subsys_initcall.
477 static int __init via_pmu_dev_init(void)
483 request_OF_resource(vias, 0, NULL);
485 #ifdef CONFIG_PMAC_BACKLIGHT
486 /* Enable backlight */
487 register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
488 #endif /* CONFIG_PMAC_BACKLIGHT */
491 if (machine_is_compatible("AAPL,3400/2400") ||
492 machine_is_compatible("AAPL,3500")) {
493 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
494 NULL, PMAC_MB_INFO_MODEL, 0);
495 pmu_battery_count = 1;
496 if (mb == PMAC_TYPE_COMET)
497 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
499 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
500 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
501 machine_is_compatible("PowerBook1,1")) {
502 pmu_battery_count = 2;
503 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
504 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
506 struct device_node* prim = find_devices("power-mgt");
507 u32 *prim_info = NULL;
509 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
511 /* Other stuffs here yet unknown */
512 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
513 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
514 if (pmu_battery_count > 1)
515 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
518 #endif /* CONFIG_PPC32 */
520 /* Create /proc/pmu */
521 proc_pmu_root = proc_mkdir("pmu", NULL);
525 for (i=0; i<pmu_battery_count; i++) {
527 sprintf(title, "battery_%ld", i);
528 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
529 proc_get_batt, (void *)i);
532 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
533 proc_get_info, NULL);
534 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
535 proc_get_irqstats, NULL);
536 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
537 if (proc_pmu_options) {
538 proc_pmu_options->nlink = 1;
539 proc_pmu_options->read_proc = proc_read_options;
540 proc_pmu_options->write_proc = proc_write_options;
546 device_initcall(via_pmu_dev_init);
552 struct adb_request req;
554 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
555 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
557 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
559 while (!req.complete) {
561 printk(KERN_ERR "init_pmu: no response from PMU\n");
568 /* ack all pending interrupts */
570 interrupt_data[0][0] = 1;
571 while (interrupt_data[0][0] || pmu_state != idle) {
573 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
576 if (pmu_state == idle)
578 via_pmu_interrupt(0, NULL, NULL);
582 /* Tell PMU we are ready. */
583 if (pmu_kind == PMU_KEYLARGO_BASED) {
584 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
585 while (!req.complete)
589 /* Read PMU version */
590 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
591 pmu_wait_complete(&req);
592 if (req.reply_len > 0)
593 pmu_version = req.reply[0];
595 /* Read server mode setting */
596 if (pmu_kind == PMU_KEYLARGO_BASED) {
597 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
598 PMU_PWR_GET_POWERUP_EVENTS);
599 pmu_wait_complete(&req);
600 if (req.reply_len == 2) {
601 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
602 option_server_mode = 1;
603 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
604 option_server_mode ? "enabled" : "disabled");
616 static void pmu_set_server_mode(int server_mode)
618 struct adb_request req;
620 if (pmu_kind != PMU_KEYLARGO_BASED)
623 option_server_mode = server_mode;
624 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
625 pmu_wait_complete(&req);
626 if (req.reply_len < 2)
629 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
630 PMU_PWR_SET_POWERUP_EVENTS,
631 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
633 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
634 PMU_PWR_CLR_POWERUP_EVENTS,
635 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
636 pmu_wait_complete(&req);
639 /* This new version of the code for 2400/3400/3500 powerbooks
640 * is inspired from the implementation in gkrellm-pmu
643 done_battery_state_ohare(struct adb_request* req)
647 * 0x01 : AC indicator
649 * 0x04 : battery exist
652 * 0x20 : full charged
653 * 0x40 : pcharge reset
654 * 0x80 : battery exist
656 * [1][2] : battery voltage
657 * [3] : CPU temperature
658 * [4] : battery temperature
663 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
664 long pcharge, charge, vb, vmax, lmax;
665 long vmax_charging, vmax_charged;
666 long amperage, voltage, time, max;
667 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
668 NULL, PMAC_MB_INFO_MODEL, 0);
670 if (req->reply[0] & 0x01)
671 pmu_power_flags |= PMU_PWR_AC_PRESENT;
673 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
675 if (mb == PMAC_TYPE_COMET) {
686 /* If battery installed */
687 if (req->reply[0] & 0x04) {
688 bat_flags |= PMU_BATT_PRESENT;
689 if (req->reply[0] & 0x02)
690 bat_flags |= PMU_BATT_CHARGING;
691 vb = (req->reply[1] << 8) | req->reply[2];
692 voltage = (vb * 265 + 72665) / 10;
693 amperage = req->reply[5];
694 if ((req->reply[0] & 0x01) == 0) {
696 vb += ((amperage - 200) * 15)/100;
697 } else if (req->reply[0] & 0x02) {
698 vb = (vb * 97) / 100;
699 vmax = vmax_charging;
701 charge = (100 * vb) / vmax;
702 if (req->reply[0] & 0x40) {
703 pcharge = (req->reply[6] << 8) + req->reply[7];
707 pcharge = 100 - pcharge / lmax;
708 if (pcharge < charge)
712 time = (charge * 16440) / amperage;
716 amperage = -amperage;
718 charge = max = amperage = voltage = time = 0;
720 pmu_batteries[pmu_cur_battery].flags = bat_flags;
721 pmu_batteries[pmu_cur_battery].charge = charge;
722 pmu_batteries[pmu_cur_battery].max_charge = max;
723 pmu_batteries[pmu_cur_battery].amperage = amperage;
724 pmu_batteries[pmu_cur_battery].voltage = voltage;
725 pmu_batteries[pmu_cur_battery].time_remaining = time;
727 clear_bit(0, &async_req_locks);
731 done_battery_state_smart(struct adb_request* req)
734 * [0] : format of this structure (known: 3,4,5)
747 * [4][5] : max charge
752 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
754 unsigned int capa, max, voltage;
756 if (req->reply[1] & 0x01)
757 pmu_power_flags |= PMU_PWR_AC_PRESENT;
759 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
762 capa = max = amperage = voltage = 0;
764 if (req->reply[1] & 0x04) {
765 bat_flags |= PMU_BATT_PRESENT;
766 switch(req->reply[0]) {
768 case 4: capa = req->reply[2];
770 amperage = *((signed char *)&req->reply[4]);
771 voltage = req->reply[5];
773 case 5: capa = (req->reply[2] << 8) | req->reply[3];
774 max = (req->reply[4] << 8) | req->reply[5];
775 amperage = *((signed short *)&req->reply[6]);
776 voltage = (req->reply[8] << 8) | req->reply[9];
779 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
780 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
785 if ((req->reply[1] & 0x01) && (amperage > 0))
786 bat_flags |= PMU_BATT_CHARGING;
788 pmu_batteries[pmu_cur_battery].flags = bat_flags;
789 pmu_batteries[pmu_cur_battery].charge = capa;
790 pmu_batteries[pmu_cur_battery].max_charge = max;
791 pmu_batteries[pmu_cur_battery].amperage = amperage;
792 pmu_batteries[pmu_cur_battery].voltage = voltage;
794 if ((req->reply[1] & 0x01) && (amperage > 0))
795 pmu_batteries[pmu_cur_battery].time_remaining
796 = ((max-capa) * 3600) / amperage;
798 pmu_batteries[pmu_cur_battery].time_remaining
799 = (capa * 3600) / (-amperage);
801 pmu_batteries[pmu_cur_battery].time_remaining = 0;
803 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
805 clear_bit(0, &async_req_locks);
809 query_battery_state(void)
811 if (test_and_set_bit(0, &async_req_locks))
813 if (pmu_kind == PMU_OHARE_BASED)
814 pmu_request(&batt_req, done_battery_state_ohare,
815 1, PMU_BATTERY_STATE);
817 pmu_request(&batt_req, done_battery_state_smart,
818 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
822 proc_get_info(char *page, char **start, off_t off,
823 int count, int *eof, void *data)
827 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
828 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
829 p += sprintf(p, "AC Power : %d\n",
830 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0));
831 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
837 proc_get_irqstats(char *page, char **start, off_t off,
838 int count, int *eof, void *data)
842 static const char *irq_names[] = {
843 "Total CB1 triggered events",
844 "Total GPIO1 triggered events",
845 "PC-Card eject button",
846 "Sound/Brightness button",
848 "Battery state change",
849 "Environment interrupt",
851 "Ghost interrupt (zero len)",
852 "Empty interrupt (empty mask)",
856 for (i=0; i<11; i++) {
857 p += sprintf(p, " %2u: %10u (%s)\n",
858 i, pmu_irq_stats[i], irq_names[i]);
864 proc_get_batt(char *page, char **start, off_t off,
865 int count, int *eof, void *data)
867 long batnum = (long)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);
888 proc_read_options(char *page, char **start, off_t off,
889 int count, int *eof, void *data)
893 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
894 if (pmu_kind == PMU_KEYLARGO_BASED &&
895 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
896 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
898 if (pmu_kind == PMU_KEYLARGO_BASED)
899 p += sprintf(p, "server_mode=%d\n", option_server_mode);
905 proc_write_options(struct file *file, const char __user *buffer,
906 unsigned long count, void *data)
910 unsigned long fcount = count;
916 if (copy_from_user(tmp, buffer, count))
924 while(*val && (*val != '=')) {
934 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
935 if (pmu_kind == PMU_KEYLARGO_BASED &&
936 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
937 if (!strcmp(label, "lid_wakeup"))
938 option_lid_wakeup = ((*val) == '1');
940 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
942 new_value = ((*val) == '1');
943 if (new_value != option_server_mode)
944 pmu_set_server_mode(new_value);
950 /* Send an ADB command */
952 pmu_send_request(struct adb_request *req, int sync)
956 if ((vias == NULL) || (!pmu_fully_inited)) {
963 switch (req->data[0]) {
965 for (i = 0; i < req->nbytes - 1; ++i)
966 req->data[i] = req->data[i+1];
968 if (pmu_data_len[req->data[0]][1] != 0) {
969 req->reply[0] = ADB_RET_OK;
973 ret = pmu_queue_request(req);
976 switch (req->data[1]) {
978 if (req->nbytes != 2)
980 req->data[0] = PMU_READ_RTC;
983 req->reply[0] = CUDA_PACKET;
985 req->reply[2] = CUDA_GET_TIME;
986 ret = pmu_queue_request(req);
989 if (req->nbytes != 6)
991 req->data[0] = PMU_SET_RTC;
993 for (i = 1; i <= 4; ++i)
994 req->data[i] = req->data[i+1];
996 req->reply[0] = CUDA_PACKET;
998 req->reply[2] = CUDA_SET_TIME;
999 ret = pmu_queue_request(req);
1006 for (i = req->nbytes - 1; i > 1; --i)
1007 req->data[i+2] = req->data[i];
1008 req->data[3] = req->nbytes - 2;
1009 req->data[2] = pmu_adb_flags;
1010 /*req->data[1] = req->data[1];*/
1011 req->data[0] = PMU_ADB_CMD;
1013 req->reply_expected = 1;
1015 ret = pmu_queue_request(req);
1024 while (!req->complete)
1030 /* Enable/disable autopolling */
1032 pmu_adb_autopoll(int devs)
1034 struct adb_request req;
1036 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1041 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1042 adb_dev_map >> 8, adb_dev_map);
1045 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1048 while (!req.complete)
1053 /* Reset the ADB bus */
1055 pmu_adb_reset_bus(void)
1057 struct adb_request req;
1058 int save_autopoll = adb_dev_map;
1060 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1063 /* anyone got a better idea?? */
1064 pmu_adb_autopoll(0);
1068 req.data[0] = PMU_ADB_CMD;
1070 req.data[2] = ADB_BUSRESET;
1074 req.reply_expected = 1;
1075 if (pmu_queue_request(&req) != 0) {
1076 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1079 pmu_wait_complete(&req);
1081 if (save_autopoll != 0)
1082 pmu_adb_autopoll(save_autopoll);
1086 #endif /* CONFIG_ADB */
1088 /* Construct and send a pmu request */
1090 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1099 if (nbytes < 0 || nbytes > 32) {
1100 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1104 req->nbytes = nbytes;
1106 va_start(list, nbytes);
1107 for (i = 0; i < nbytes; ++i)
1108 req->data[i] = va_arg(list, int);
1111 req->reply_expected = 0;
1112 return pmu_queue_request(req);
1116 pmu_queue_request(struct adb_request *req)
1118 unsigned long flags;
1125 if (req->nbytes <= 0) {
1129 nsend = pmu_data_len[req->data[0]][0];
1130 if (nsend >= 0 && req->nbytes != nsend + 1) {
1139 spin_lock_irqsave(&pmu_lock, flags);
1140 if (current_req != 0) {
1141 last_req->next = req;
1146 if (pmu_state == idle)
1149 spin_unlock_irqrestore(&pmu_lock, flags);
1157 /* Sightly increased the delay, I had one occurrence of the message
1161 while ((in_8(&via[B]) & TACK) == 0) {
1162 if (--timeout < 0) {
1163 printk(KERN_ERR "PMU not responding (!ack)\n");
1170 /* New PMU seems to be very sensitive to those timings, so we make sure
1171 * PCI is flushed immediately */
1175 volatile unsigned char __iomem *v = via;
1177 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1179 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1186 volatile unsigned char __iomem *v = via;
1188 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1189 in_8(&v[SR]); /* resets SR */
1190 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1195 pmu_done(struct adb_request *req)
1197 void (*done)(struct adb_request *) = req->done;
1200 /* Here, we assume that if the request has a done member, the
1201 * struct request will survive to setting req->complete to 1
1210 struct adb_request *req;
1212 /* assert pmu_state == idle */
1213 /* get the packet to send */
1215 if (req == 0 || pmu_state != idle
1216 || (/*req->reply_expected && */req_awaiting_reply))
1219 pmu_state = sending;
1221 data_len = pmu_data_len[req->data[0]][0];
1223 /* Sounds safer to make sure ACK is high before writing. This helped
1224 * kill a problem with ADB and some iBooks
1227 /* set the shift register to shift out and send a byte */
1228 send_byte(req->data[0]);
1238 via_pmu_interrupt(0, NULL, NULL);
1248 /* Kicks ADB read when PMU is suspended */
1249 adb_int_pending = 1;
1251 via_pmu_interrupt(0, NULL, NULL);
1252 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1253 || req_awaiting_reply));
1257 pmu_wait_complete(struct adb_request *req)
1261 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1262 via_pmu_interrupt(0, NULL, NULL);
1265 /* This function loops until the PMU is idle and prevents it from
1266 * anwsering to ADB interrupts. pmu_request can still be called.
1267 * This is done to avoid spurrious shutdowns when we know we'll have
1268 * interrupts switched off for a long time
1273 unsigned long flags;
1274 #ifdef SUSPEND_USES_PMU
1275 struct adb_request *req;
1280 spin_lock_irqsave(&pmu_lock, flags);
1282 if (pmu_suspended > 1) {
1283 spin_unlock_irqrestore(&pmu_lock, flags);
1288 spin_unlock_irqrestore(&pmu_lock, flags);
1289 if (req_awaiting_reply)
1290 adb_int_pending = 1;
1291 via_pmu_interrupt(0, NULL, NULL);
1292 spin_lock_irqsave(&pmu_lock, flags);
1293 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1294 #ifdef SUSPEND_USES_PMU
1295 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1296 spin_unlock_irqrestore(&pmu_lock, flags);
1297 while(!req.complete)
1299 #else /* SUSPEND_USES_PMU */
1301 disable_irq_nosync(gpio_irq);
1302 out_8(&via[IER], CB1_INT | IER_CLR);
1303 spin_unlock_irqrestore(&pmu_lock, flags);
1304 #endif /* SUSPEND_USES_PMU */
1313 unsigned long flags;
1315 if (!via || (pmu_suspended < 1))
1318 spin_lock_irqsave(&pmu_lock, flags);
1320 if (pmu_suspended > 0) {
1321 spin_unlock_irqrestore(&pmu_lock, flags);
1324 adb_int_pending = 1;
1325 #ifdef SUSPEND_USES_PMU
1326 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1327 spin_unlock_irqrestore(&pmu_lock, flags);
1328 while(!req.complete)
1330 #else /* SUSPEND_USES_PMU */
1332 enable_irq(gpio_irq);
1333 out_8(&via[IER], CB1_INT | IER_SET);
1334 spin_unlock_irqrestore(&pmu_lock, flags);
1336 #endif /* SUSPEND_USES_PMU */
1339 /* Interrupt data could be the result data from an ADB cmd */
1341 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1343 unsigned char ints, pirq;
1347 if (drop_interrupts || len < 1) {
1348 adb_int_pending = 0;
1353 /* Get PMU interrupt mask */
1356 /* Record zero interrupts for stats */
1360 /* Hack to deal with ADB autopoll flag */
1361 if (ints & PMU_INT_ADB)
1362 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1367 if (i > pmu_irq_stats[10])
1368 pmu_irq_stats[10] = i;
1372 for (pirq = 0; pirq < 8; pirq++)
1373 if (ints & (1 << pirq))
1375 pmu_irq_stats[pirq]++;
1377 ints &= ~(1 << pirq);
1379 /* Note: for some reason, we get an interrupt with len=1,
1380 * data[0]==0 after each normal ADB interrupt, at least
1381 * on the Pismo. Still investigating... --BenH
1383 if ((1 << pirq) & PMU_INT_ADB) {
1384 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1385 struct adb_request *req = req_awaiting_reply;
1387 printk(KERN_ERR "PMU: extra ADB reply\n");
1390 req_awaiting_reply = NULL;
1394 memcpy(req->reply, data + 1, len - 1);
1395 req->reply_len = len - 1;
1399 if (len == 4 && data[1] == 0x2c) {
1400 extern int xmon_wants_key, xmon_adb_keycode;
1401 if (xmon_wants_key) {
1402 xmon_adb_keycode = data[2];
1408 * XXX On the [23]400 the PMU gives us an up
1409 * event for keycodes 0x74 or 0x75 when the PC
1410 * card eject buttons are released, so we
1411 * ignore those events.
1413 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1414 && data[1] == 0x2c && data[3] == 0xff
1415 && (data[2] & ~1) == 0xf4))
1416 adb_input(data+1, len-1, regs, 1);
1417 #endif /* CONFIG_ADB */
1420 /* Sound/brightness button pressed */
1421 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1422 #ifdef CONFIG_PMAC_BACKLIGHT
1424 #ifdef CONFIG_INPUT_ADBHID
1425 if (!disable_kernel_backlight)
1426 #endif /* CONFIG_INPUT_ADBHID */
1427 set_backlight_level(data[1] >> 4);
1428 #endif /* CONFIG_PMAC_BACKLIGHT */
1430 /* Tick interrupt */
1431 else if ((1 << pirq) & PMU_INT_TICK) {
1432 /* Environement or tick interrupt, query batteries */
1433 if (pmu_battery_count) {
1434 if ((--query_batt_timer) == 0) {
1435 query_battery_state();
1436 query_batt_timer = BATTERY_POLLING_COUNT;
1440 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1441 if (pmu_battery_count)
1442 query_battery_state();
1443 pmu_pass_intr(data, len);
1445 pmu_pass_intr(data, len);
1450 static struct adb_request*
1451 pmu_sr_intr(struct pt_regs *regs)
1453 struct adb_request *req;
1456 if (via[B] & TREQ) {
1457 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1458 out_8(&via[IFR], SR_INT);
1461 /* The ack may not yet be low when we get the interrupt */
1462 while ((in_8(&via[B]) & TACK) != 0)
1465 /* if reading grab the byte, and reset the interrupt */
1466 if (pmu_state == reading || pmu_state == reading_intr)
1467 bite = in_8(&via[SR]);
1469 /* reset TREQ and wait for TACK to go high */
1470 out_8(&via[B], in_8(&via[B]) | TREQ);
1473 switch (pmu_state) {
1477 data_len = req->nbytes - 1;
1478 send_byte(data_len);
1481 if (data_index <= data_len) {
1482 send_byte(req->data[data_index++]);
1486 data_len = pmu_data_len[req->data[0]][1];
1487 if (data_len == 0) {
1489 current_req = req->next;
1490 if (req->reply_expected)
1491 req_awaiting_reply = req;
1495 pmu_state = reading;
1497 reply_ptr = req->reply + req->reply_len;
1505 pmu_state = reading_intr;
1506 reply_ptr = interrupt_data[int_data_last];
1508 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1509 enable_irq(gpio_irq);
1510 gpio_irq_enabled = 1;
1516 if (data_len == -1) {
1519 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1520 } else if (data_index < 32) {
1521 reply_ptr[data_index++] = bite;
1523 if (data_index < data_len) {
1528 if (pmu_state == reading_intr) {
1530 int_data_state[int_data_last] = int_data_ready;
1531 interrupt_data_len[int_data_last] = data_len;
1535 * For PMU sleep and freq change requests, we lock the
1536 * PMU until it's explicitely unlocked. This avoids any
1537 * spurrious event polling getting in
1539 current_req = req->next;
1540 req->reply_len += data_index;
1541 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1550 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1557 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1559 unsigned long flags;
1563 struct adb_request *req = NULL;
1566 /* This is a bit brutal, we can probably do better */
1567 spin_lock_irqsave(&pmu_lock, flags);
1571 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1575 if (++nloop > 1000) {
1576 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1577 "intr=%x, ier=%x pmu_state=%d\n",
1578 intr, in_8(&via[IER]), pmu_state);
1581 out_8(&via[IFR], intr);
1582 if (intr & CB1_INT) {
1583 adb_int_pending = 1;
1586 if (intr & SR_INT) {
1587 req = pmu_sr_intr(regs);
1594 if (pmu_state == idle) {
1595 if (adb_int_pending) {
1596 if (int_data_state[0] == int_data_empty)
1598 else if (int_data_state[1] == int_data_empty)
1603 int_data_state[int_data_last] = int_data_fill;
1604 /* Sounds safer to make sure ACK is high before writing.
1605 * This helped kill a problem with ADB and some iBooks
1608 send_byte(PMU_INT_ACK);
1609 adb_int_pending = 0;
1610 } else if (current_req)
1614 /* Mark the oldest buffer for flushing */
1615 if (int_data_state[!int_data_last] == int_data_ready) {
1616 int_data_state[!int_data_last] = int_data_flush;
1617 int_data = !int_data_last;
1618 } else if (int_data_state[int_data_last] == int_data_ready) {
1619 int_data_state[int_data_last] = int_data_flush;
1620 int_data = int_data_last;
1623 spin_unlock_irqrestore(&pmu_lock, flags);
1625 /* Deal with completed PMU requests outside of the lock */
1631 /* Deal with interrupt datas outside of the lock */
1632 if (int_data >= 0) {
1633 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1634 spin_lock_irqsave(&pmu_lock, flags);
1636 int_data_state[int_data] = int_data_empty;
1641 return IRQ_RETVAL(handled);
1647 unsigned long flags;
1649 spin_lock_irqsave(&pmu_lock, flags);
1650 if (pmu_state == locked)
1652 adb_int_pending = 1;
1653 spin_unlock_irqrestore(&pmu_lock, flags);
1658 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1660 unsigned long flags;
1662 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1663 spin_lock_irqsave(&pmu_lock, flags);
1664 if (gpio_irq_enabled > 0) {
1665 disable_irq_nosync(gpio_irq);
1666 gpio_irq_enabled = 0;
1669 adb_int_pending = 1;
1670 spin_unlock_irqrestore(&pmu_lock, flags);
1671 via_pmu_interrupt(0, NULL, NULL);
1677 #ifdef CONFIG_PMAC_BACKLIGHT
1678 static int backlight_to_bright[] = {
1679 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1680 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1684 pmu_set_backlight_enable(int on, int level, void* data)
1686 struct adb_request req;
1692 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1693 backlight_to_bright[level]);
1694 pmu_wait_complete(&req);
1696 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1697 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1698 pmu_wait_complete(&req);
1704 pmu_bright_complete(struct adb_request *req)
1706 if (req == &bright_req_1)
1707 clear_bit(1, &async_req_locks);
1708 if (req == &bright_req_2)
1709 clear_bit(2, &async_req_locks);
1713 pmu_set_backlight_level(int level, void* data)
1718 if (test_and_set_bit(1, &async_req_locks))
1720 pmu_request(&bright_req_1, pmu_bright_complete, 2, PMU_BACKLIGHT_BRIGHT,
1721 backlight_to_bright[level]);
1722 if (test_and_set_bit(2, &async_req_locks))
1724 pmu_request(&bright_req_2, pmu_bright_complete, 2, PMU_POWER_CTRL,
1725 PMU_POW_BACKLIGHT | (level > BACKLIGHT_OFF ?
1726 PMU_POW_ON : PMU_POW_OFF));
1730 #endif /* CONFIG_PMAC_BACKLIGHT */
1733 pmu_enable_irled(int on)
1735 struct adb_request req;
1739 if (pmu_kind == PMU_KEYLARGO_BASED)
1742 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1743 (on ? PMU_POW_ON : PMU_POW_OFF));
1744 pmu_wait_complete(&req);
1750 struct adb_request req;
1755 local_irq_disable();
1757 drop_interrupts = 1;
1759 if (pmu_kind != PMU_KEYLARGO_BASED) {
1760 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1762 while(!req.complete)
1766 pmu_request(&req, NULL, 1, PMU_RESET);
1767 pmu_wait_complete(&req);
1775 struct adb_request req;
1780 local_irq_disable();
1782 drop_interrupts = 1;
1784 if (pmu_kind != PMU_KEYLARGO_BASED) {
1785 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1787 pmu_wait_complete(&req);
1789 /* Disable server mode on shutdown or we'll just
1792 pmu_set_server_mode(0);
1795 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1796 'M', 'A', 'T', 'T');
1797 pmu_wait_complete(&req);
1808 struct pmu_i2c_hdr {
1819 pmu_i2c_combined_read(int bus, int addr, int subaddr, u8* data, int len)
1821 struct adb_request req;
1822 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1826 for (retry=0; retry<16; retry++) {
1827 memset(&req, 0, sizeof(req));
1830 hdr->address = addr & 0xfe;
1831 hdr->mode = PMU_I2C_MODE_COMBINED;
1833 hdr->sub_addr = subaddr;
1834 hdr->comb_addr = addr | 1;
1837 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1838 req.reply_expected = 0;
1840 req.data[0] = PMU_I2C_CMD;
1841 req.reply[0] = 0xff;
1842 rc = pmu_queue_request(&req);
1845 while(!req.complete)
1847 if (req.reply[0] == PMU_I2C_STATUS_OK)
1851 if (req.reply[0] != PMU_I2C_STATUS_OK)
1854 for (retry=0; retry<16; retry++) {
1855 memset(&req, 0, sizeof(req));
1859 hdr->bus = PMU_I2C_BUS_STATUS;
1860 req.reply[0] = 0xff;
1863 req.reply_expected = 0;
1865 req.data[0] = PMU_I2C_CMD;
1866 rc = pmu_queue_request(&req);
1869 while(!req.complete)
1871 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1872 memcpy(data, &req.reply[1], req.reply_len - 1);
1873 return req.reply_len - 1;
1880 pmu_i2c_stdsub_write(int bus, int addr, int subaddr, u8* data, int len)
1882 struct adb_request req;
1883 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1887 for (retry=0; retry<16; retry++) {
1888 memset(&req, 0, sizeof(req));
1891 hdr->address = addr & 0xfe;
1892 hdr->mode = PMU_I2C_MODE_STDSUB;
1894 hdr->sub_addr = subaddr;
1895 hdr->comb_addr = addr & 0xfe;
1898 req.data[0] = PMU_I2C_CMD;
1899 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
1900 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
1901 req.reply_expected = 0;
1903 req.reply[0] = 0xff;
1904 rc = pmu_queue_request(&req);
1907 while(!req.complete)
1909 if (req.reply[0] == PMU_I2C_STATUS_OK)
1913 if (req.reply[0] != PMU_I2C_STATUS_OK)
1916 for (retry=0; retry<16; retry++) {
1917 memset(&req, 0, sizeof(req));
1921 hdr->bus = PMU_I2C_BUS_STATUS;
1922 req.reply[0] = 0xff;
1925 req.reply_expected = 0;
1927 req.data[0] = PMU_I2C_CMD;
1928 rc = pmu_queue_request(&req);
1931 while(!req.complete)
1933 if (req.reply[0] == PMU_I2C_STATUS_OK)
1940 pmu_i2c_simple_read(int bus, int addr, u8* data, int len)
1942 struct adb_request req;
1943 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1947 for (retry=0; retry<16; retry++) {
1948 memset(&req, 0, sizeof(req));
1951 hdr->address = addr | 1;
1952 hdr->mode = PMU_I2C_MODE_SIMPLE;
1958 req.data[0] = PMU_I2C_CMD;
1959 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1960 req.reply_expected = 0;
1962 req.reply[0] = 0xff;
1963 rc = pmu_queue_request(&req);
1966 while(!req.complete)
1968 if (req.reply[0] == PMU_I2C_STATUS_OK)
1972 if (req.reply[0] != PMU_I2C_STATUS_OK)
1975 for (retry=0; retry<16; retry++) {
1976 memset(&req, 0, sizeof(req));
1980 hdr->bus = PMU_I2C_BUS_STATUS;
1981 req.reply[0] = 0xff;
1984 req.reply_expected = 0;
1986 req.data[0] = PMU_I2C_CMD;
1987 rc = pmu_queue_request(&req);
1990 while(!req.complete)
1992 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1993 memcpy(data, &req.reply[1], req.reply_len - 1);
1994 return req.reply_len - 1;
2001 pmu_i2c_simple_write(int bus, int addr, u8* data, int len)
2003 struct adb_request req;
2004 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
2008 for (retry=0; retry<16; retry++) {
2009 memset(&req, 0, sizeof(req));
2012 hdr->address = addr & 0xfe;
2013 hdr->mode = PMU_I2C_MODE_SIMPLE;
2019 req.data[0] = PMU_I2C_CMD;
2020 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
2021 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
2022 req.reply_expected = 0;
2024 req.reply[0] = 0xff;
2025 rc = pmu_queue_request(&req);
2028 while(!req.complete)
2030 if (req.reply[0] == PMU_I2C_STATUS_OK)
2034 if (req.reply[0] != PMU_I2C_STATUS_OK)
2037 for (retry=0; retry<16; retry++) {
2038 memset(&req, 0, sizeof(req));
2042 hdr->bus = PMU_I2C_BUS_STATUS;
2043 req.reply[0] = 0xff;
2046 req.reply_expected = 0;
2048 req.data[0] = PMU_I2C_CMD;
2049 rc = pmu_queue_request(&req);
2052 while(!req.complete)
2054 if (req.reply[0] == PMU_I2C_STATUS_OK)
2062 static LIST_HEAD(sleep_notifiers);
2065 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
2067 struct list_head *list;
2068 struct pmu_sleep_notifier *notifier;
2070 for (list = sleep_notifiers.next; list != &sleep_notifiers;
2071 list = list->next) {
2072 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2073 if (n->priority > notifier->priority)
2076 __list_add(&n->list, list->prev, list);
2079 EXPORT_SYMBOL(pmu_register_sleep_notifier);
2082 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
2084 if (n->list.next == 0)
2087 n->list.next = NULL;
2090 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
2091 #endif /* CONFIG_PM */
2093 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
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];
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 hid0 = mfspr(SPRN_HID0);
2696 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2697 mtspr(SPRN_HID0, hid0);
2698 mtmsr(mfmsr() | 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();
2718 #endif /* CONFIG_PM && CONFIG_PPC32 */
2721 * Support for /dev/pmu device
2723 #define RB_SIZE 0x10
2724 struct pmu_private {
2725 struct list_head list;
2730 unsigned char data[16];
2732 wait_queue_head_t wait;
2734 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2735 int backlight_locker;
2736 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2739 static LIST_HEAD(all_pmu_pvt);
2740 static DEFINE_SPINLOCK(all_pvt_lock);
2743 pmu_pass_intr(unsigned char *data, int len)
2745 struct pmu_private *pp;
2746 struct list_head *list;
2748 unsigned long flags;
2750 if (len > sizeof(pp->rb_buf[0].data))
2751 len = sizeof(pp->rb_buf[0].data);
2752 spin_lock_irqsave(&all_pvt_lock, flags);
2753 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2754 pp = list_entry(list, struct pmu_private, list);
2755 spin_lock(&pp->lock);
2759 if (i != pp->rb_get) {
2760 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2762 memcpy(rp->data, data, len);
2764 wake_up_interruptible(&pp->wait);
2766 spin_unlock(&pp->lock);
2768 spin_unlock_irqrestore(&all_pvt_lock, flags);
2772 pmu_open(struct inode *inode, struct file *file)
2774 struct pmu_private *pp;
2775 unsigned long flags;
2777 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2780 pp->rb_get = pp->rb_put = 0;
2781 spin_lock_init(&pp->lock);
2782 init_waitqueue_head(&pp->wait);
2783 spin_lock_irqsave(&all_pvt_lock, flags);
2784 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2785 pp->backlight_locker = 0;
2786 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2787 list_add(&pp->list, &all_pmu_pvt);
2788 spin_unlock_irqrestore(&all_pvt_lock, flags);
2789 file->private_data = pp;
2794 pmu_read(struct file *file, char __user *buf,
2795 size_t count, loff_t *ppos)
2797 struct pmu_private *pp = file->private_data;
2798 DECLARE_WAITQUEUE(wait, current);
2799 unsigned long flags;
2802 if (count < 1 || pp == 0)
2804 if (!access_ok(VERIFY_WRITE, buf, count))
2807 spin_lock_irqsave(&pp->lock, flags);
2808 add_wait_queue(&pp->wait, &wait);
2809 current->state = TASK_INTERRUPTIBLE;
2813 if (pp->rb_get != pp->rb_put) {
2815 struct rb_entry *rp = &pp->rb_buf[i];
2817 spin_unlock_irqrestore(&pp->lock, flags);
2820 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2824 spin_lock_irqsave(&pp->lock, flags);
2829 if (file->f_flags & O_NONBLOCK)
2832 if (signal_pending(current))
2834 spin_unlock_irqrestore(&pp->lock, flags);
2836 spin_lock_irqsave(&pp->lock, flags);
2838 current->state = TASK_RUNNING;
2839 remove_wait_queue(&pp->wait, &wait);
2840 spin_unlock_irqrestore(&pp->lock, flags);
2846 pmu_write(struct file *file, const char __user *buf,
2847 size_t count, loff_t *ppos)
2853 pmu_fpoll(struct file *filp, poll_table *wait)
2855 struct pmu_private *pp = filp->private_data;
2856 unsigned int mask = 0;
2857 unsigned long flags;
2861 poll_wait(filp, &pp->wait, wait);
2862 spin_lock_irqsave(&pp->lock, flags);
2863 if (pp->rb_get != pp->rb_put)
2865 spin_unlock_irqrestore(&pp->lock, flags);
2870 pmu_release(struct inode *inode, struct file *file)
2872 struct pmu_private *pp = file->private_data;
2873 unsigned long flags;
2877 file->private_data = NULL;
2878 spin_lock_irqsave(&all_pvt_lock, flags);
2879 list_del(&pp->list);
2880 spin_unlock_irqrestore(&all_pvt_lock, flags);
2881 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2882 if (pp->backlight_locker) {
2883 spin_lock_irqsave(&pmu_lock, flags);
2884 disable_kernel_backlight--;
2885 spin_unlock_irqrestore(&pmu_lock, flags);
2887 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2895 pmu_ioctl(struct inode * inode, struct file *filp,
2896 u_int cmd, u_long arg)
2898 __u32 __user *argp = (__u32 __user *)arg;
2899 int error = -EINVAL;
2902 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2904 if (!capable(CAP_SYS_ADMIN))
2906 if (sleep_in_progress)
2908 sleep_in_progress = 1;
2910 case PMU_OHARE_BASED:
2911 error = powerbook_sleep_3400();
2913 case PMU_HEATHROW_BASED:
2914 case PMU_PADDINGTON_BASED:
2915 error = powerbook_sleep_grackle();
2917 case PMU_KEYLARGO_BASED:
2918 error = powerbook_sleep_Core99();
2923 sleep_in_progress = 0;
2925 case PMU_IOC_CAN_SLEEP:
2926 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2927 return put_user(0, argp);
2929 return put_user(1, argp);
2930 #endif /* CONFIG_PM && CONFIG_PPC32 */
2932 #ifdef CONFIG_PMAC_BACKLIGHT
2933 /* Backlight should have its own device or go via
2936 case PMU_IOC_GET_BACKLIGHT:
2937 if (sleep_in_progress)
2939 error = get_backlight_level();
2942 return put_user(error, argp);
2943 case PMU_IOC_SET_BACKLIGHT:
2946 if (sleep_in_progress)
2948 error = get_user(value, argp);
2950 error = set_backlight_level(value);
2953 #ifdef CONFIG_INPUT_ADBHID
2954 case PMU_IOC_GRAB_BACKLIGHT: {
2955 struct pmu_private *pp = filp->private_data;
2956 unsigned long flags;
2958 if (pp->backlight_locker)
2960 pp->backlight_locker = 1;
2961 spin_lock_irqsave(&pmu_lock, flags);
2962 disable_kernel_backlight++;
2963 spin_unlock_irqrestore(&pmu_lock, flags);
2966 #endif /* CONFIG_INPUT_ADBHID */
2967 #endif /* CONFIG_PMAC_BACKLIGHT */
2968 case PMU_IOC_GET_MODEL:
2969 return put_user(pmu_kind, argp);
2970 case PMU_IOC_HAS_ADB:
2971 return put_user(pmu_has_adb, argp);
2976 static struct file_operations pmu_device_fops = {
2982 .release = pmu_release,
2985 static struct miscdevice pmu_device = {
2986 PMU_MINOR, "pmu", &pmu_device_fops
2989 static int pmu_device_init(void)
2993 if (misc_register(&pmu_device) < 0)
2994 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2997 device_initcall(pmu_device_init);
3002 polled_handshake(volatile unsigned char __iomem *via)
3004 via[B] &= ~TREQ; eieio();
3005 while ((via[B] & TACK) != 0)
3007 via[B] |= TREQ; eieio();
3008 while ((via[B] & TACK) == 0)
3013 polled_send_byte(volatile unsigned char __iomem *via, int x)
3015 via[ACR] |= SR_OUT | SR_EXT; eieio();
3016 via[SR] = x; eieio();
3017 polled_handshake(via);
3021 polled_recv_byte(volatile unsigned char __iomem *via)
3025 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
3026 x = via[SR]; eieio();
3027 polled_handshake(via);
3028 x = via[SR]; eieio();
3033 pmu_polled_request(struct adb_request *req)
3035 unsigned long flags;
3037 volatile unsigned char __iomem *v = via;
3041 l = pmu_data_len[c][0];
3042 if (l >= 0 && req->nbytes != l + 1)
3045 local_irq_save(flags);
3046 while (pmu_state != idle)
3049 while ((via[B] & TACK) == 0)
3051 polled_send_byte(v, c);
3053 l = req->nbytes - 1;
3054 polled_send_byte(v, l);
3056 for (i = 1; i <= l; ++i)
3057 polled_send_byte(v, req->data[i]);
3059 l = pmu_data_len[c][1];
3061 l = polled_recv_byte(v);
3062 for (i = 0; i < l; ++i)
3063 req->reply[i + req->reply_len] = polled_recv_byte(v);
3068 local_irq_restore(flags);
3071 #endif /* DEBUG_SLEEP */
3074 /* FIXME: This is a temporary set of callbacks to enable us
3075 * to do suspend-to-disk.
3078 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
3080 static int pmu_sys_suspended = 0;
3082 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
3084 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
3087 /* Suspend PMU event interrupts */
3090 pmu_sys_suspended = 1;
3094 static int pmu_sys_resume(struct sys_device *sysdev)
3096 struct adb_request req;
3098 if (!pmu_sys_suspended)
3101 /* Tell PMU we are ready */
3102 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
3103 pmu_wait_complete(&req);
3105 /* Resume PMU event interrupts */
3108 pmu_sys_suspended = 0;
3113 #endif /* CONFIG_PM && CONFIG_PPC32 */
3115 static struct sysdev_class pmu_sysclass = {
3116 set_kset_name("pmu"),
3119 static struct sys_device device_pmu = {
3121 .cls = &pmu_sysclass,
3124 static struct sysdev_driver driver_pmu = {
3125 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
3126 .suspend = &pmu_sys_suspend,
3127 .resume = &pmu_sys_resume,
3128 #endif /* CONFIG_PM && CONFIG_PPC32 */
3131 static int __init init_pmu_sysfs(void)
3135 rc = sysdev_class_register(&pmu_sysclass);
3137 printk(KERN_ERR "Failed registering PMU sys class\n");
3140 rc = sysdev_register(&device_pmu);
3142 printk(KERN_ERR "Failed registering PMU sys device\n");
3145 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
3147 printk(KERN_ERR "Failed registering PMU sys driver\n");
3153 subsys_initcall(init_pmu_sysfs);
3155 EXPORT_SYMBOL(pmu_request);
3156 EXPORT_SYMBOL(pmu_poll);
3157 EXPORT_SYMBOL(pmu_poll_adb);
3158 EXPORT_SYMBOL(pmu_wait_complete);
3159 EXPORT_SYMBOL(pmu_suspend);
3160 EXPORT_SYMBOL(pmu_resume);
3161 EXPORT_SYMBOL(pmu_unlock);
3162 EXPORT_SYMBOL(pmu_i2c_combined_read);
3163 EXPORT_SYMBOL(pmu_i2c_stdsub_write);
3164 EXPORT_SYMBOL(pmu_i2c_simple_read);
3165 EXPORT_SYMBOL(pmu_i2c_simple_write);
3166 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
3167 EXPORT_SYMBOL(pmu_enable_irled);
3168 EXPORT_SYMBOL(pmu_battery_count);
3169 EXPORT_SYMBOL(pmu_batteries);
3170 EXPORT_SYMBOL(pmu_power_flags);
3171 #endif /* CONFIG_PM && CONFIG_PPC32 */