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
13 * Copyright (C) 2006-2007 Johannes Berg
15 * THIS DRIVER IS BECOMING A TOTAL MESS !
16 * - Cleanup atomically disabling reply to PMU events after
17 * a sleep or a freq. switch
21 #include <linux/types.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/sched.h>
26 #include <linux/miscdevice.h>
27 #include <linux/blkdev.h>
28 #include <linux/pci.h>
29 #include <linux/slab.h>
30 #include <linux/poll.h>
31 #include <linux/adb.h>
32 #include <linux/pmu.h>
33 #include <linux/cuda.h>
34 #include <linux/module.h>
35 #include <linux/spinlock.h>
37 #include <linux/proc_fs.h>
38 #include <linux/init.h>
39 #include <linux/interrupt.h>
40 #include <linux/device.h>
41 #include <linux/sysdev.h>
42 #include <linux/freezer.h>
43 #include <linux/syscalls.h>
44 #include <linux/suspend.h>
45 #include <linux/cpu.h>
47 #include <asm/machdep.h>
49 #include <asm/pgtable.h>
50 #include <asm/system.h>
51 #include <asm/sections.h>
53 #include <asm/pmac_feature.h>
54 #include <asm/pmac_pfunc.h>
55 #include <asm/pmac_low_i2c.h>
56 #include <asm/uaccess.h>
57 #include <asm/mmu_context.h>
58 #include <asm/cputable.h>
60 #include <asm/backlight.h>
62 #include "via-pmu-event.h"
64 /* Some compile options */
67 /* Misc minor number allocated for /dev/pmu */
70 /* How many iterations between battery polls */
71 #define BATTERY_POLLING_COUNT 2
73 static volatile unsigned char __iomem *via;
75 /* VIA registers - spaced 0x200 bytes apart */
76 #define RS 0x200 /* skip between registers */
77 #define B 0 /* B-side data */
78 #define A RS /* A-side data */
79 #define DIRB (2*RS) /* B-side direction (1=output) */
80 #define DIRA (3*RS) /* A-side direction (1=output) */
81 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
82 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
83 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
84 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
85 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
86 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
87 #define SR (10*RS) /* Shift register */
88 #define ACR (11*RS) /* Auxiliary control register */
89 #define PCR (12*RS) /* Peripheral control register */
90 #define IFR (13*RS) /* Interrupt flag register */
91 #define IER (14*RS) /* Interrupt enable register */
92 #define ANH (15*RS) /* A-side data, no handshake */
94 /* Bits in B data register: both active low */
95 #define TACK 0x08 /* Transfer acknowledge (input) */
96 #define TREQ 0x10 /* Transfer request (output) */
99 #define SR_CTRL 0x1c /* Shift register control bits */
100 #define SR_EXT 0x0c /* Shift on external clock */
101 #define SR_OUT 0x10 /* Shift out if 1 */
103 /* Bits in IFR and IER */
104 #define IER_SET 0x80 /* set bits in IER */
105 #define IER_CLR 0 /* clear bits in IER */
106 #define SR_INT 0x04 /* Shift register full/empty */
108 #define CB1_INT 0x10 /* transition on CB1 input */
110 static volatile enum pmu_state {
119 static volatile enum int_data_state {
124 } int_data_state[2] = { int_data_empty, int_data_empty };
126 static struct adb_request *current_req;
127 static struct adb_request *last_req;
128 static struct adb_request *req_awaiting_reply;
129 static unsigned char interrupt_data[2][32];
130 static int interrupt_data_len[2];
131 static int int_data_last;
132 static unsigned char *reply_ptr;
133 static int data_index;
135 static volatile int adb_int_pending;
136 static volatile int disable_poll;
137 static struct device_node *vias;
138 static int pmu_kind = PMU_UNKNOWN;
139 static int pmu_fully_inited;
140 static int pmu_has_adb;
141 static struct device_node *gpio_node;
142 static unsigned char __iomem *gpio_reg;
143 static int gpio_irq = NO_IRQ;
144 static int gpio_irq_enabled = -1;
145 static volatile int pmu_suspended;
146 static spinlock_t pmu_lock;
147 static u8 pmu_intr_mask;
148 static int pmu_version;
149 static int drop_interrupts;
150 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
151 static int option_lid_wakeup = 1;
152 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
153 static unsigned long async_req_locks;
154 static unsigned int pmu_irq_stats[11];
156 static struct proc_dir_entry *proc_pmu_root;
157 static struct proc_dir_entry *proc_pmu_info;
158 static struct proc_dir_entry *proc_pmu_irqstats;
159 static struct proc_dir_entry *proc_pmu_options;
160 static int option_server_mode;
162 int pmu_battery_count;
164 unsigned int pmu_power_flags = PMU_PWR_AC_PRESENT;
165 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
166 static int query_batt_timer = BATTERY_POLLING_COUNT;
167 static struct adb_request batt_req;
168 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
174 static int adb_dev_map;
175 static int pmu_adb_flags;
177 static int pmu_probe(void);
178 static int pmu_init(void);
179 static int pmu_send_request(struct adb_request *req, int sync);
180 static int pmu_adb_autopoll(int devs);
181 static int pmu_adb_reset_bus(void);
182 #endif /* CONFIG_ADB */
184 static int init_pmu(void);
185 static void pmu_start(void);
186 static irqreturn_t via_pmu_interrupt(int irq, void *arg);
187 static irqreturn_t gpio1_interrupt(int irq, void *arg);
188 static int proc_get_info(char *page, char **start, off_t off,
189 int count, int *eof, void *data);
190 static int proc_get_irqstats(char *page, char **start, off_t off,
191 int count, int *eof, void *data);
192 static void pmu_pass_intr(unsigned char *data, int len);
193 static int proc_get_batt(char *page, char **start, off_t off,
194 int count, int *eof, void *data);
195 static int proc_read_options(char *page, char **start, off_t off,
196 int count, int *eof, void *data);
197 static int proc_write_options(struct file *file, const char __user *buffer,
198 unsigned long count, void *data);
201 struct adb_driver via_pmu_driver = {
210 #endif /* CONFIG_ADB */
212 extern void low_sleep_handler(void);
213 extern void enable_kernel_altivec(void);
214 extern void enable_kernel_fp(void);
217 int pmu_polled_request(struct adb_request *req);
218 void pmu_blink(int n);
222 * This table indicates for each PMU opcode:
223 * - the number of data bytes to be sent with the command, or -1
224 * if a length byte should be sent,
225 * - the number of response bytes which the PMU will return, or
226 * -1 if it will send a length byte.
228 static const s8 pmu_data_len[256][2] = {
229 /* 0 1 2 3 4 5 6 7 */
230 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
231 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
232 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
233 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
234 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
235 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
236 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
237 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
238 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
239 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
240 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
241 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
242 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
243 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
244 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
245 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
246 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
247 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
248 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
249 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
250 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
251 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
252 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
253 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
254 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
255 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
256 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
257 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
258 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
259 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
260 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
261 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
264 static char *pbook_type[] = {
266 "PowerBook 2400/3400/3500(G3)",
267 "PowerBook G3 Series",
272 int __init find_via_pmu(void)
279 vias = of_find_node_by_name(NULL, "via-pmu");
283 reg = of_get_property(vias, "reg", NULL);
285 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
288 taddr = of_translate_address(vias, reg);
289 if (taddr == OF_BAD_ADDR) {
290 printk(KERN_ERR "via-pmu: Can't translate address !\n");
294 spin_lock_init(&pmu_lock);
298 pmu_intr_mask = PMU_INT_PCEJECT |
303 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
304 || of_device_is_compatible(vias->parent, "ohare")))
305 pmu_kind = PMU_OHARE_BASED;
306 else if (of_device_is_compatible(vias->parent, "paddington"))
307 pmu_kind = PMU_PADDINGTON_BASED;
308 else if (of_device_is_compatible(vias->parent, "heathrow"))
309 pmu_kind = PMU_HEATHROW_BASED;
310 else if (of_device_is_compatible(vias->parent, "Keylargo")
311 || of_device_is_compatible(vias->parent, "K2-Keylargo")) {
312 struct device_node *gpiop;
313 struct device_node *adbp;
314 u64 gaddr = OF_BAD_ADDR;
316 pmu_kind = PMU_KEYLARGO_BASED;
317 adbp = of_find_node_by_type(NULL, "adb");
318 pmu_has_adb = (adbp != NULL);
320 pmu_intr_mask = PMU_INT_PCEJECT |
326 gpiop = of_find_node_by_name(NULL, "gpio");
328 reg = of_get_property(gpiop, "reg", NULL);
330 gaddr = of_translate_address(gpiop, reg);
331 if (gaddr != OF_BAD_ADDR)
332 gpio_reg = ioremap(gaddr, 0x10);
334 if (gpio_reg == NULL) {
335 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
339 pmu_kind = PMU_UNKNOWN;
341 via = ioremap(taddr, 0x2000);
343 printk(KERN_ERR "via-pmu: Can't map address !\n");
347 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
348 out_8(&via[IFR], 0x7f); /* clear IFR */
357 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
358 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
360 sys_ctrler = SYS_CTRLER_PMU;
373 static int pmu_probe(void)
375 return vias == NULL? -ENODEV: 0;
378 static int __init pmu_init(void)
384 #endif /* CONFIG_ADB */
387 * We can't wait until pmu_init gets called, that happens too late.
388 * It happens after IDE and SCSI initialization, which can take a few
389 * seconds, and by that time the PMU could have given up on us and
391 * Thus this is called with arch_initcall rather than device_initcall.
393 static int __init via_pmu_start(void)
400 batt_req.complete = 1;
402 irq = irq_of_parse_and_map(vias, 0);
404 printk(KERN_ERR "via-pmu: can't map interrupt\n");
407 if (request_irq(irq, via_pmu_interrupt, 0, "VIA-PMU", (void *)0)) {
408 printk(KERN_ERR "via-pmu: can't request irq %d\n", irq);
412 if (pmu_kind == PMU_KEYLARGO_BASED) {
413 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
414 if (gpio_node == NULL)
415 gpio_node = of_find_node_by_name(NULL,
418 gpio_irq = irq_of_parse_and_map(gpio_node, 0);
420 if (gpio_irq != NO_IRQ) {
421 if (request_irq(gpio_irq, gpio1_interrupt, 0,
422 "GPIO1 ADB", (void *)0))
423 printk(KERN_ERR "pmu: can't get irq %d"
424 " (GPIO1)\n", gpio_irq);
426 gpio_irq_enabled = 1;
430 /* Enable interrupts */
431 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
433 pmu_fully_inited = 1;
435 /* Make sure PMU settle down before continuing. This is _very_ important
436 * since the IDE probe may shut interrupts down for quite a bit of time. If
437 * a PMU communication is pending while this happens, the PMU may timeout
438 * Not that on Core99 machines, the PMU keeps sending us environement
439 * messages, we should find a way to either fix IDE or make it call
440 * pmu_suspend() before masking interrupts. This can also happens while
441 * scolling with some fbdevs.
445 } while (pmu_state != idle);
450 arch_initcall(via_pmu_start);
453 * This has to be done after pci_init, which is a subsys_initcall.
455 static int __init via_pmu_dev_init(void)
460 #ifdef CONFIG_PMAC_BACKLIGHT
461 /* Initialize backlight */
462 pmu_backlight_init();
466 if (machine_is_compatible("AAPL,3400/2400") ||
467 machine_is_compatible("AAPL,3500")) {
468 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
469 NULL, PMAC_MB_INFO_MODEL, 0);
470 pmu_battery_count = 1;
471 if (mb == PMAC_TYPE_COMET)
472 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
474 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
475 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
476 machine_is_compatible("PowerBook1,1")) {
477 pmu_battery_count = 2;
478 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
479 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
481 struct device_node* prim =
482 of_find_node_by_name(NULL, "power-mgt");
483 const u32 *prim_info = NULL;
485 prim_info = of_get_property(prim, "prim-info", NULL);
487 /* Other stuffs here yet unknown */
488 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
489 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
490 if (pmu_battery_count > 1)
491 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
495 #endif /* CONFIG_PPC32 */
497 /* Create /proc/pmu */
498 proc_pmu_root = proc_mkdir("pmu", NULL);
502 for (i=0; i<pmu_battery_count; i++) {
504 sprintf(title, "battery_%ld", i);
505 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
506 proc_get_batt, (void *)i);
509 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
510 proc_get_info, NULL);
511 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
512 proc_get_irqstats, NULL);
513 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
514 if (proc_pmu_options) {
515 proc_pmu_options->read_proc = proc_read_options;
516 proc_pmu_options->write_proc = proc_write_options;
522 device_initcall(via_pmu_dev_init);
528 struct adb_request req;
530 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
531 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
533 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
535 while (!req.complete) {
537 printk(KERN_ERR "init_pmu: no response from PMU\n");
544 /* ack all pending interrupts */
546 interrupt_data[0][0] = 1;
547 while (interrupt_data[0][0] || pmu_state != idle) {
549 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
552 if (pmu_state == idle)
554 via_pmu_interrupt(0, NULL);
558 /* Tell PMU we are ready. */
559 if (pmu_kind == PMU_KEYLARGO_BASED) {
560 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
561 while (!req.complete)
565 /* Read PMU version */
566 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
567 pmu_wait_complete(&req);
568 if (req.reply_len > 0)
569 pmu_version = req.reply[0];
571 /* Read server mode setting */
572 if (pmu_kind == PMU_KEYLARGO_BASED) {
573 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
574 PMU_PWR_GET_POWERUP_EVENTS);
575 pmu_wait_complete(&req);
576 if (req.reply_len == 2) {
577 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
578 option_server_mode = 1;
579 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
580 option_server_mode ? "enabled" : "disabled");
592 static void pmu_set_server_mode(int server_mode)
594 struct adb_request req;
596 if (pmu_kind != PMU_KEYLARGO_BASED)
599 option_server_mode = server_mode;
600 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
601 pmu_wait_complete(&req);
602 if (req.reply_len < 2)
605 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
606 PMU_PWR_SET_POWERUP_EVENTS,
607 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
609 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
610 PMU_PWR_CLR_POWERUP_EVENTS,
611 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
612 pmu_wait_complete(&req);
615 /* This new version of the code for 2400/3400/3500 powerbooks
616 * is inspired from the implementation in gkrellm-pmu
619 done_battery_state_ohare(struct adb_request* req)
623 * 0x01 : AC indicator
625 * 0x04 : battery exist
628 * 0x20 : full charged
629 * 0x40 : pcharge reset
630 * 0x80 : battery exist
632 * [1][2] : battery voltage
633 * [3] : CPU temperature
634 * [4] : battery temperature
639 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
640 long pcharge, charge, vb, vmax, lmax;
641 long vmax_charging, vmax_charged;
642 long amperage, voltage, time, max;
643 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
644 NULL, PMAC_MB_INFO_MODEL, 0);
646 if (req->reply[0] & 0x01)
647 pmu_power_flags |= PMU_PWR_AC_PRESENT;
649 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
651 if (mb == PMAC_TYPE_COMET) {
662 /* If battery installed */
663 if (req->reply[0] & 0x04) {
664 bat_flags |= PMU_BATT_PRESENT;
665 if (req->reply[0] & 0x02)
666 bat_flags |= PMU_BATT_CHARGING;
667 vb = (req->reply[1] << 8) | req->reply[2];
668 voltage = (vb * 265 + 72665) / 10;
669 amperage = req->reply[5];
670 if ((req->reply[0] & 0x01) == 0) {
672 vb += ((amperage - 200) * 15)/100;
673 } else if (req->reply[0] & 0x02) {
674 vb = (vb * 97) / 100;
675 vmax = vmax_charging;
677 charge = (100 * vb) / vmax;
678 if (req->reply[0] & 0x40) {
679 pcharge = (req->reply[6] << 8) + req->reply[7];
683 pcharge = 100 - pcharge / lmax;
684 if (pcharge < charge)
688 time = (charge * 16440) / amperage;
692 amperage = -amperage;
694 charge = max = amperage = voltage = time = 0;
696 pmu_batteries[pmu_cur_battery].flags = bat_flags;
697 pmu_batteries[pmu_cur_battery].charge = charge;
698 pmu_batteries[pmu_cur_battery].max_charge = max;
699 pmu_batteries[pmu_cur_battery].amperage = amperage;
700 pmu_batteries[pmu_cur_battery].voltage = voltage;
701 pmu_batteries[pmu_cur_battery].time_remaining = time;
703 clear_bit(0, &async_req_locks);
707 done_battery_state_smart(struct adb_request* req)
710 * [0] : format of this structure (known: 3,4,5)
723 * [4][5] : max charge
728 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
730 unsigned int capa, max, voltage;
732 if (req->reply[1] & 0x01)
733 pmu_power_flags |= PMU_PWR_AC_PRESENT;
735 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
738 capa = max = amperage = voltage = 0;
740 if (req->reply[1] & 0x04) {
741 bat_flags |= PMU_BATT_PRESENT;
742 switch(req->reply[0]) {
744 case 4: capa = req->reply[2];
746 amperage = *((signed char *)&req->reply[4]);
747 voltage = req->reply[5];
749 case 5: capa = (req->reply[2] << 8) | req->reply[3];
750 max = (req->reply[4] << 8) | req->reply[5];
751 amperage = *((signed short *)&req->reply[6]);
752 voltage = (req->reply[8] << 8) | req->reply[9];
755 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
756 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
761 if ((req->reply[1] & 0x01) && (amperage > 0))
762 bat_flags |= PMU_BATT_CHARGING;
764 pmu_batteries[pmu_cur_battery].flags = bat_flags;
765 pmu_batteries[pmu_cur_battery].charge = capa;
766 pmu_batteries[pmu_cur_battery].max_charge = max;
767 pmu_batteries[pmu_cur_battery].amperage = amperage;
768 pmu_batteries[pmu_cur_battery].voltage = voltage;
770 if ((req->reply[1] & 0x01) && (amperage > 0))
771 pmu_batteries[pmu_cur_battery].time_remaining
772 = ((max-capa) * 3600) / amperage;
774 pmu_batteries[pmu_cur_battery].time_remaining
775 = (capa * 3600) / (-amperage);
777 pmu_batteries[pmu_cur_battery].time_remaining = 0;
779 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
781 clear_bit(0, &async_req_locks);
785 query_battery_state(void)
787 if (test_and_set_bit(0, &async_req_locks))
789 if (pmu_kind == PMU_OHARE_BASED)
790 pmu_request(&batt_req, done_battery_state_ohare,
791 1, PMU_BATTERY_STATE);
793 pmu_request(&batt_req, done_battery_state_smart,
794 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
798 proc_get_info(char *page, char **start, off_t off,
799 int count, int *eof, void *data)
803 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
804 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
805 p += sprintf(p, "AC Power : %d\n",
806 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
807 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
813 proc_get_irqstats(char *page, char **start, off_t off,
814 int count, int *eof, void *data)
818 static const char *irq_names[] = {
819 "Total CB1 triggered events",
820 "Total GPIO1 triggered events",
821 "PC-Card eject button",
822 "Sound/Brightness button",
824 "Battery state change",
825 "Environment interrupt",
827 "Ghost interrupt (zero len)",
828 "Empty interrupt (empty mask)",
832 for (i=0; i<11; i++) {
833 p += sprintf(p, " %2u: %10u (%s)\n",
834 i, pmu_irq_stats[i], irq_names[i]);
840 proc_get_batt(char *page, char **start, off_t off,
841 int count, int *eof, void *data)
843 long batnum = (long)data;
846 p += sprintf(p, "\n");
847 p += sprintf(p, "flags : %08x\n",
848 pmu_batteries[batnum].flags);
849 p += sprintf(p, "charge : %d\n",
850 pmu_batteries[batnum].charge);
851 p += sprintf(p, "max_charge : %d\n",
852 pmu_batteries[batnum].max_charge);
853 p += sprintf(p, "current : %d\n",
854 pmu_batteries[batnum].amperage);
855 p += sprintf(p, "voltage : %d\n",
856 pmu_batteries[batnum].voltage);
857 p += sprintf(p, "time rem. : %d\n",
858 pmu_batteries[batnum].time_remaining);
864 proc_read_options(char *page, char **start, off_t off,
865 int count, int *eof, void *data)
869 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
870 if (pmu_kind == PMU_KEYLARGO_BASED &&
871 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
872 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
874 if (pmu_kind == PMU_KEYLARGO_BASED)
875 p += sprintf(p, "server_mode=%d\n", option_server_mode);
881 proc_write_options(struct file *file, const char __user *buffer,
882 unsigned long count, void *data)
886 unsigned long fcount = count;
892 if (copy_from_user(tmp, buffer, count))
900 while(*val && (*val != '=')) {
910 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
911 if (pmu_kind == PMU_KEYLARGO_BASED &&
912 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
913 if (!strcmp(label, "lid_wakeup"))
914 option_lid_wakeup = ((*val) == '1');
916 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
918 new_value = ((*val) == '1');
919 if (new_value != option_server_mode)
920 pmu_set_server_mode(new_value);
926 /* Send an ADB command */
928 pmu_send_request(struct adb_request *req, int sync)
932 if ((vias == NULL) || (!pmu_fully_inited)) {
939 switch (req->data[0]) {
941 for (i = 0; i < req->nbytes - 1; ++i)
942 req->data[i] = req->data[i+1];
944 if (pmu_data_len[req->data[0]][1] != 0) {
945 req->reply[0] = ADB_RET_OK;
949 ret = pmu_queue_request(req);
952 switch (req->data[1]) {
954 if (req->nbytes != 2)
956 req->data[0] = PMU_READ_RTC;
959 req->reply[0] = CUDA_PACKET;
961 req->reply[2] = CUDA_GET_TIME;
962 ret = pmu_queue_request(req);
965 if (req->nbytes != 6)
967 req->data[0] = PMU_SET_RTC;
969 for (i = 1; i <= 4; ++i)
970 req->data[i] = req->data[i+1];
972 req->reply[0] = CUDA_PACKET;
974 req->reply[2] = CUDA_SET_TIME;
975 ret = pmu_queue_request(req);
982 for (i = req->nbytes - 1; i > 1; --i)
983 req->data[i+2] = req->data[i];
984 req->data[3] = req->nbytes - 2;
985 req->data[2] = pmu_adb_flags;
986 /*req->data[1] = req->data[1];*/
987 req->data[0] = PMU_ADB_CMD;
989 req->reply_expected = 1;
991 ret = pmu_queue_request(req);
1000 while (!req->complete)
1006 /* Enable/disable autopolling */
1008 pmu_adb_autopoll(int devs)
1010 struct adb_request req;
1012 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1017 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1018 adb_dev_map >> 8, adb_dev_map);
1021 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1024 while (!req.complete)
1029 /* Reset the ADB bus */
1031 pmu_adb_reset_bus(void)
1033 struct adb_request req;
1034 int save_autopoll = adb_dev_map;
1036 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1039 /* anyone got a better idea?? */
1040 pmu_adb_autopoll(0);
1044 req.data[0] = PMU_ADB_CMD;
1046 req.data[2] = ADB_BUSRESET;
1050 req.reply_expected = 1;
1051 if (pmu_queue_request(&req) != 0) {
1052 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1055 pmu_wait_complete(&req);
1057 if (save_autopoll != 0)
1058 pmu_adb_autopoll(save_autopoll);
1062 #endif /* CONFIG_ADB */
1064 /* Construct and send a pmu request */
1066 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1075 if (nbytes < 0 || nbytes > 32) {
1076 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1080 req->nbytes = nbytes;
1082 va_start(list, nbytes);
1083 for (i = 0; i < nbytes; ++i)
1084 req->data[i] = va_arg(list, int);
1087 req->reply_expected = 0;
1088 return pmu_queue_request(req);
1092 pmu_queue_request(struct adb_request *req)
1094 unsigned long flags;
1101 if (req->nbytes <= 0) {
1105 nsend = pmu_data_len[req->data[0]][0];
1106 if (nsend >= 0 && req->nbytes != nsend + 1) {
1115 spin_lock_irqsave(&pmu_lock, flags);
1116 if (current_req != 0) {
1117 last_req->next = req;
1122 if (pmu_state == idle)
1125 spin_unlock_irqrestore(&pmu_lock, flags);
1133 /* Sightly increased the delay, I had one occurrence of the message
1137 while ((in_8(&via[B]) & TACK) == 0) {
1138 if (--timeout < 0) {
1139 printk(KERN_ERR "PMU not responding (!ack)\n");
1146 /* New PMU seems to be very sensitive to those timings, so we make sure
1147 * PCI is flushed immediately */
1151 volatile unsigned char __iomem *v = via;
1153 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1155 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1162 volatile unsigned char __iomem *v = via;
1164 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1165 in_8(&v[SR]); /* resets SR */
1166 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1171 pmu_done(struct adb_request *req)
1173 void (*done)(struct adb_request *) = req->done;
1176 /* Here, we assume that if the request has a done member, the
1177 * struct request will survive to setting req->complete to 1
1186 struct adb_request *req;
1188 /* assert pmu_state == idle */
1189 /* get the packet to send */
1191 if (req == 0 || pmu_state != idle
1192 || (/*req->reply_expected && */req_awaiting_reply))
1195 pmu_state = sending;
1197 data_len = pmu_data_len[req->data[0]][0];
1199 /* Sounds safer to make sure ACK is high before writing. This helped
1200 * kill a problem with ADB and some iBooks
1203 /* set the shift register to shift out and send a byte */
1204 send_byte(req->data[0]);
1214 via_pmu_interrupt(0, NULL);
1224 /* Kicks ADB read when PMU is suspended */
1225 adb_int_pending = 1;
1227 via_pmu_interrupt(0, NULL);
1228 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1229 || req_awaiting_reply));
1233 pmu_wait_complete(struct adb_request *req)
1237 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1238 via_pmu_interrupt(0, NULL);
1241 /* This function loops until the PMU is idle and prevents it from
1242 * anwsering to ADB interrupts. pmu_request can still be called.
1243 * This is done to avoid spurrious shutdowns when we know we'll have
1244 * interrupts switched off for a long time
1249 unsigned long flags;
1254 spin_lock_irqsave(&pmu_lock, flags);
1256 if (pmu_suspended > 1) {
1257 spin_unlock_irqrestore(&pmu_lock, flags);
1262 spin_unlock_irqrestore(&pmu_lock, flags);
1263 if (req_awaiting_reply)
1264 adb_int_pending = 1;
1265 via_pmu_interrupt(0, NULL);
1266 spin_lock_irqsave(&pmu_lock, flags);
1267 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1269 disable_irq_nosync(gpio_irq);
1270 out_8(&via[IER], CB1_INT | IER_CLR);
1271 spin_unlock_irqrestore(&pmu_lock, flags);
1280 unsigned long flags;
1282 if (!via || (pmu_suspended < 1))
1285 spin_lock_irqsave(&pmu_lock, flags);
1287 if (pmu_suspended > 0) {
1288 spin_unlock_irqrestore(&pmu_lock, flags);
1291 adb_int_pending = 1;
1293 enable_irq(gpio_irq);
1294 out_8(&via[IER], CB1_INT | IER_SET);
1295 spin_unlock_irqrestore(&pmu_lock, flags);
1299 /* Interrupt data could be the result data from an ADB cmd */
1301 pmu_handle_data(unsigned char *data, int len)
1303 unsigned char ints, pirq;
1307 if (drop_interrupts || len < 1) {
1308 adb_int_pending = 0;
1313 /* Get PMU interrupt mask */
1316 /* Record zero interrupts for stats */
1320 /* Hack to deal with ADB autopoll flag */
1321 if (ints & PMU_INT_ADB)
1322 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1327 if (i > pmu_irq_stats[10])
1328 pmu_irq_stats[10] = i;
1332 for (pirq = 0; pirq < 8; pirq++)
1333 if (ints & (1 << pirq))
1335 pmu_irq_stats[pirq]++;
1337 ints &= ~(1 << pirq);
1339 /* Note: for some reason, we get an interrupt with len=1,
1340 * data[0]==0 after each normal ADB interrupt, at least
1341 * on the Pismo. Still investigating... --BenH
1343 if ((1 << pirq) & PMU_INT_ADB) {
1344 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1345 struct adb_request *req = req_awaiting_reply;
1347 printk(KERN_ERR "PMU: extra ADB reply\n");
1350 req_awaiting_reply = NULL;
1354 memcpy(req->reply, data + 1, len - 1);
1355 req->reply_len = len - 1;
1359 if (len == 4 && data[1] == 0x2c) {
1360 extern int xmon_wants_key, xmon_adb_keycode;
1361 if (xmon_wants_key) {
1362 xmon_adb_keycode = data[2];
1368 * XXX On the [23]400 the PMU gives us an up
1369 * event for keycodes 0x74 or 0x75 when the PC
1370 * card eject buttons are released, so we
1371 * ignore those events.
1373 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1374 && data[1] == 0x2c && data[3] == 0xff
1375 && (data[2] & ~1) == 0xf4))
1376 adb_input(data+1, len-1, 1);
1377 #endif /* CONFIG_ADB */
1380 /* Sound/brightness button pressed */
1381 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1382 #ifdef CONFIG_PMAC_BACKLIGHT
1384 pmac_backlight_set_legacy_brightness_pmu(data[1] >> 4);
1387 /* Tick interrupt */
1388 else if ((1 << pirq) & PMU_INT_TICK) {
1389 /* Environement or tick interrupt, query batteries */
1390 if (pmu_battery_count) {
1391 if ((--query_batt_timer) == 0) {
1392 query_battery_state();
1393 query_batt_timer = BATTERY_POLLING_COUNT;
1397 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1398 if (pmu_battery_count)
1399 query_battery_state();
1400 pmu_pass_intr(data, len);
1401 /* len == 6 is probably a bad check. But how do I
1402 * know what PMU versions send what events here? */
1404 via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
1405 via_pmu_event(PMU_EVT_LID, data[1]&1);
1408 pmu_pass_intr(data, len);
1413 static struct adb_request*
1416 struct adb_request *req;
1419 if (via[B] & TREQ) {
1420 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1421 out_8(&via[IFR], SR_INT);
1424 /* The ack may not yet be low when we get the interrupt */
1425 while ((in_8(&via[B]) & TACK) != 0)
1428 /* if reading grab the byte, and reset the interrupt */
1429 if (pmu_state == reading || pmu_state == reading_intr)
1430 bite = in_8(&via[SR]);
1432 /* reset TREQ and wait for TACK to go high */
1433 out_8(&via[B], in_8(&via[B]) | TREQ);
1436 switch (pmu_state) {
1440 data_len = req->nbytes - 1;
1441 send_byte(data_len);
1444 if (data_index <= data_len) {
1445 send_byte(req->data[data_index++]);
1449 data_len = pmu_data_len[req->data[0]][1];
1450 if (data_len == 0) {
1452 current_req = req->next;
1453 if (req->reply_expected)
1454 req_awaiting_reply = req;
1458 pmu_state = reading;
1460 reply_ptr = req->reply + req->reply_len;
1468 pmu_state = reading_intr;
1469 reply_ptr = interrupt_data[int_data_last];
1471 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1472 enable_irq(gpio_irq);
1473 gpio_irq_enabled = 1;
1479 if (data_len == -1) {
1482 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1483 } else if (data_index < 32) {
1484 reply_ptr[data_index++] = bite;
1486 if (data_index < data_len) {
1491 if (pmu_state == reading_intr) {
1493 int_data_state[int_data_last] = int_data_ready;
1494 interrupt_data_len[int_data_last] = data_len;
1498 * For PMU sleep and freq change requests, we lock the
1499 * PMU until it's explicitly unlocked. This avoids any
1500 * spurrious event polling getting in
1502 current_req = req->next;
1503 req->reply_len += data_index;
1504 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1513 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1520 via_pmu_interrupt(int irq, void *arg)
1522 unsigned long flags;
1526 struct adb_request *req = NULL;
1529 /* This is a bit brutal, we can probably do better */
1530 spin_lock_irqsave(&pmu_lock, flags);
1534 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1538 if (++nloop > 1000) {
1539 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1540 "intr=%x, ier=%x pmu_state=%d\n",
1541 intr, in_8(&via[IER]), pmu_state);
1544 out_8(&via[IFR], intr);
1545 if (intr & CB1_INT) {
1546 adb_int_pending = 1;
1549 if (intr & SR_INT) {
1550 req = pmu_sr_intr();
1557 if (pmu_state == idle) {
1558 if (adb_int_pending) {
1559 if (int_data_state[0] == int_data_empty)
1561 else if (int_data_state[1] == int_data_empty)
1566 int_data_state[int_data_last] = int_data_fill;
1567 /* Sounds safer to make sure ACK is high before writing.
1568 * This helped kill a problem with ADB and some iBooks
1571 send_byte(PMU_INT_ACK);
1572 adb_int_pending = 0;
1573 } else if (current_req)
1577 /* Mark the oldest buffer for flushing */
1578 if (int_data_state[!int_data_last] == int_data_ready) {
1579 int_data_state[!int_data_last] = int_data_flush;
1580 int_data = !int_data_last;
1581 } else if (int_data_state[int_data_last] == int_data_ready) {
1582 int_data_state[int_data_last] = int_data_flush;
1583 int_data = int_data_last;
1586 spin_unlock_irqrestore(&pmu_lock, flags);
1588 /* Deal with completed PMU requests outside of the lock */
1594 /* Deal with interrupt datas outside of the lock */
1595 if (int_data >= 0) {
1596 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data]);
1597 spin_lock_irqsave(&pmu_lock, flags);
1599 int_data_state[int_data] = int_data_empty;
1604 return IRQ_RETVAL(handled);
1610 unsigned long flags;
1612 spin_lock_irqsave(&pmu_lock, flags);
1613 if (pmu_state == locked)
1615 adb_int_pending = 1;
1616 spin_unlock_irqrestore(&pmu_lock, flags);
1621 gpio1_interrupt(int irq, void *arg)
1623 unsigned long flags;
1625 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1626 spin_lock_irqsave(&pmu_lock, flags);
1627 if (gpio_irq_enabled > 0) {
1628 disable_irq_nosync(gpio_irq);
1629 gpio_irq_enabled = 0;
1632 adb_int_pending = 1;
1633 spin_unlock_irqrestore(&pmu_lock, flags);
1634 via_pmu_interrupt(0, NULL);
1641 pmu_enable_irled(int on)
1643 struct adb_request req;
1647 if (pmu_kind == PMU_KEYLARGO_BASED)
1650 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1651 (on ? PMU_POW_ON : PMU_POW_OFF));
1652 pmu_wait_complete(&req);
1658 struct adb_request req;
1663 local_irq_disable();
1665 drop_interrupts = 1;
1667 if (pmu_kind != PMU_KEYLARGO_BASED) {
1668 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1670 while(!req.complete)
1674 pmu_request(&req, NULL, 1, PMU_RESET);
1675 pmu_wait_complete(&req);
1683 struct adb_request req;
1688 local_irq_disable();
1690 drop_interrupts = 1;
1692 if (pmu_kind != PMU_KEYLARGO_BASED) {
1693 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1695 pmu_wait_complete(&req);
1697 /* Disable server mode on shutdown or we'll just
1700 pmu_set_server_mode(0);
1703 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1704 'M', 'A', 'T', 'T');
1705 pmu_wait_complete(&req);
1716 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
1718 * Put the powerbook to sleep.
1721 static u32 save_via[8];
1724 save_via_state(void)
1726 save_via[0] = in_8(&via[ANH]);
1727 save_via[1] = in_8(&via[DIRA]);
1728 save_via[2] = in_8(&via[B]);
1729 save_via[3] = in_8(&via[DIRB]);
1730 save_via[4] = in_8(&via[PCR]);
1731 save_via[5] = in_8(&via[ACR]);
1732 save_via[6] = in_8(&via[T1CL]);
1733 save_via[7] = in_8(&via[T1CH]);
1736 restore_via_state(void)
1738 out_8(&via[ANH], save_via[0]);
1739 out_8(&via[DIRA], save_via[1]);
1740 out_8(&via[B], save_via[2]);
1741 out_8(&via[DIRB], save_via[3]);
1742 out_8(&via[PCR], save_via[4]);
1743 out_8(&via[ACR], save_via[5]);
1744 out_8(&via[T1CL], save_via[6]);
1745 out_8(&via[T1CH], save_via[7]);
1746 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
1747 out_8(&via[IFR], 0x7f); /* clear IFR */
1748 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
1751 #define GRACKLE_PM (1<<7)
1752 #define GRACKLE_DOZE (1<<5)
1753 #define GRACKLE_NAP (1<<4)
1754 #define GRACKLE_SLEEP (1<<3)
1756 static int powerbook_sleep_grackle(void)
1758 unsigned long save_l2cr;
1759 unsigned short pmcr1;
1760 struct adb_request req;
1761 struct pci_dev *grackle;
1763 grackle = pci_get_bus_and_slot(0, 0);
1767 /* Turn off various things. Darwin does some retry tests here... */
1768 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
1769 pmu_wait_complete(&req);
1770 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1771 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
1772 pmu_wait_complete(&req);
1774 /* For 750, save backside cache setting and disable it */
1775 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
1777 if (!__fake_sleep) {
1778 /* Ask the PMU to put us to sleep */
1779 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
1780 pmu_wait_complete(&req);
1783 /* The VIA is supposed not to be restored correctly*/
1785 /* We shut down some HW */
1786 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
1788 pci_read_config_word(grackle, 0x70, &pmcr1);
1789 /* Apparently, MacOS uses NAP mode for Grackle ??? */
1790 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
1791 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
1792 pci_write_config_word(grackle, 0x70, pmcr1);
1794 /* Call low-level ASM sleep handler */
1798 low_sleep_handler();
1800 /* We're awake again, stop grackle PM */
1801 pci_read_config_word(grackle, 0x70, &pmcr1);
1802 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
1803 pci_write_config_word(grackle, 0x70, pmcr1);
1805 pci_dev_put(grackle);
1807 /* Make sure the PMU is idle */
1808 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
1809 restore_via_state();
1811 /* Restore L2 cache */
1812 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
1813 _set_L2CR(save_l2cr);
1815 /* Restore userland MMU context */
1816 set_context(current->active_mm->context.id, current->active_mm->pgd);
1818 /* Power things up */
1820 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1821 pmu_wait_complete(&req);
1822 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
1823 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
1824 pmu_wait_complete(&req);
1825 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1826 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
1827 pmu_wait_complete(&req);
1833 powerbook_sleep_Core99(void)
1835 unsigned long save_l2cr;
1836 unsigned long save_l3cr;
1837 struct adb_request req;
1839 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
1840 printk(KERN_ERR "Sleep mode not supported on this machine\n");
1844 if (num_online_cpus() > 1 || cpu_is_offline(0))
1847 /* Stop environment and ADB interrupts */
1848 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1849 pmu_wait_complete(&req);
1851 /* Tell PMU what events will wake us up */
1852 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
1854 pmu_wait_complete(&req);
1855 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
1856 0, PMU_PWR_WAKEUP_KEY |
1857 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
1858 pmu_wait_complete(&req);
1860 /* Save the state of the L2 and L3 caches */
1861 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
1862 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
1864 if (!__fake_sleep) {
1865 /* Ask the PMU to put us to sleep */
1866 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
1867 pmu_wait_complete(&req);
1870 /* The VIA is supposed not to be restored correctly*/
1873 /* Shut down various ASICs. There's a chance that we can no longer
1874 * talk to the PMU after this, so I moved it to _after_ sending the
1875 * sleep command to it. Still need to be checked.
1877 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
1879 /* Call low-level ASM sleep handler */
1883 low_sleep_handler();
1885 /* Restore Apple core ASICs state */
1886 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
1889 restore_via_state();
1891 /* tweak LPJ before cpufreq is there */
1892 loops_per_jiffy *= 2;
1895 pmac_call_early_video_resume();
1897 /* Restore L2 cache */
1898 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
1899 _set_L2CR(save_l2cr);
1900 /* Restore L3 cache */
1901 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
1902 _set_L3CR(save_l3cr);
1904 /* Restore userland MMU context */
1905 set_context(current->active_mm->context.id, current->active_mm->pgd);
1907 /* Tell PMU we are ready */
1909 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
1910 pmu_wait_complete(&req);
1911 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1912 pmu_wait_complete(&req);
1914 /* Restore LPJ, cpufreq will adjust the cpu frequency */
1915 loops_per_jiffy /= 2;
1920 #define PB3400_MEM_CTRL 0xf8000000
1921 #define PB3400_MEM_CTRL_SLEEP 0x70
1923 static void __iomem *pb3400_mem_ctrl;
1925 static void powerbook_sleep_init_3400(void)
1927 /* map in the memory controller registers */
1928 pb3400_mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
1929 if (pb3400_mem_ctrl == NULL)
1930 printk(KERN_WARNING "ioremap failed: sleep won't be possible");
1933 static int powerbook_sleep_3400(void)
1938 struct adb_request sleep_req;
1939 unsigned int __iomem *mem_ctrl_sleep;
1941 if (pb3400_mem_ctrl == NULL)
1943 mem_ctrl_sleep = pb3400_mem_ctrl + PB3400_MEM_CTRL_SLEEP;
1945 /* Set the memory controller to keep the memory refreshed
1946 while we're asleep */
1947 for (i = 0x403f; i >= 0x4000; --i) {
1948 out_be32(mem_ctrl_sleep, i);
1950 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
1956 /* Ask the PMU to put us to sleep */
1957 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
1958 pmu_wait_complete(&sleep_req);
1961 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
1965 /* Put the CPU into sleep mode */
1966 hid0 = mfspr(SPRN_HID0);
1967 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
1968 mtspr(SPRN_HID0, hid0);
1970 msr = mfmsr() | MSR_POW;
1976 local_irq_disable();
1978 /* OK, we're awake again, start restoring things */
1979 out_be32(mem_ctrl_sleep, 0x3f);
1980 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
1985 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
1988 * Support for /dev/pmu device
1990 #define RB_SIZE 0x10
1991 struct pmu_private {
1992 struct list_head list;
1997 unsigned char data[16];
1999 wait_queue_head_t wait;
2001 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2002 int backlight_locker;
2006 static LIST_HEAD(all_pmu_pvt);
2007 static DEFINE_SPINLOCK(all_pvt_lock);
2010 pmu_pass_intr(unsigned char *data, int len)
2012 struct pmu_private *pp;
2013 struct list_head *list;
2015 unsigned long flags;
2017 if (len > sizeof(pp->rb_buf[0].data))
2018 len = sizeof(pp->rb_buf[0].data);
2019 spin_lock_irqsave(&all_pvt_lock, flags);
2020 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2021 pp = list_entry(list, struct pmu_private, list);
2022 spin_lock(&pp->lock);
2026 if (i != pp->rb_get) {
2027 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2029 memcpy(rp->data, data, len);
2031 wake_up_interruptible(&pp->wait);
2033 spin_unlock(&pp->lock);
2035 spin_unlock_irqrestore(&all_pvt_lock, flags);
2039 pmu_open(struct inode *inode, struct file *file)
2041 struct pmu_private *pp;
2042 unsigned long flags;
2044 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2047 pp->rb_get = pp->rb_put = 0;
2048 spin_lock_init(&pp->lock);
2049 init_waitqueue_head(&pp->wait);
2050 spin_lock_irqsave(&all_pvt_lock, flags);
2051 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2052 pp->backlight_locker = 0;
2054 list_add(&pp->list, &all_pmu_pvt);
2055 spin_unlock_irqrestore(&all_pvt_lock, flags);
2056 file->private_data = pp;
2061 pmu_read(struct file *file, char __user *buf,
2062 size_t count, loff_t *ppos)
2064 struct pmu_private *pp = file->private_data;
2065 DECLARE_WAITQUEUE(wait, current);
2066 unsigned long flags;
2069 if (count < 1 || pp == 0)
2071 if (!access_ok(VERIFY_WRITE, buf, count))
2074 spin_lock_irqsave(&pp->lock, flags);
2075 add_wait_queue(&pp->wait, &wait);
2076 current->state = TASK_INTERRUPTIBLE;
2080 if (pp->rb_get != pp->rb_put) {
2082 struct rb_entry *rp = &pp->rb_buf[i];
2084 spin_unlock_irqrestore(&pp->lock, flags);
2087 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2091 spin_lock_irqsave(&pp->lock, flags);
2096 if (file->f_flags & O_NONBLOCK)
2099 if (signal_pending(current))
2101 spin_unlock_irqrestore(&pp->lock, flags);
2103 spin_lock_irqsave(&pp->lock, flags);
2105 current->state = TASK_RUNNING;
2106 remove_wait_queue(&pp->wait, &wait);
2107 spin_unlock_irqrestore(&pp->lock, flags);
2113 pmu_write(struct file *file, const char __user *buf,
2114 size_t count, loff_t *ppos)
2120 pmu_fpoll(struct file *filp, poll_table *wait)
2122 struct pmu_private *pp = filp->private_data;
2123 unsigned int mask = 0;
2124 unsigned long flags;
2128 poll_wait(filp, &pp->wait, wait);
2129 spin_lock_irqsave(&pp->lock, flags);
2130 if (pp->rb_get != pp->rb_put)
2132 spin_unlock_irqrestore(&pp->lock, flags);
2137 pmu_release(struct inode *inode, struct file *file)
2139 struct pmu_private *pp = file->private_data;
2140 unsigned long flags;
2143 file->private_data = NULL;
2144 spin_lock_irqsave(&all_pvt_lock, flags);
2145 list_del(&pp->list);
2146 spin_unlock_irqrestore(&all_pvt_lock, flags);
2148 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2149 if (pp->backlight_locker)
2150 pmac_backlight_enable();
2158 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
2159 static void pmac_suspend_disable_irqs(void)
2161 /* Call platform functions marked "on sleep" */
2162 pmac_pfunc_i2c_suspend();
2163 pmac_pfunc_base_suspend();
2166 static int powerbook_sleep(suspend_state_t state)
2170 /* Wait for completion of async requests */
2171 while (!batt_req.complete)
2174 /* Giveup the lazy FPU & vec so we don't have to back them
2175 * up from the low level code
2179 #ifdef CONFIG_ALTIVEC
2180 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2181 enable_kernel_altivec();
2182 #endif /* CONFIG_ALTIVEC */
2185 case PMU_OHARE_BASED:
2186 error = powerbook_sleep_3400();
2188 case PMU_HEATHROW_BASED:
2189 case PMU_PADDINGTON_BASED:
2190 error = powerbook_sleep_grackle();
2192 case PMU_KEYLARGO_BASED:
2193 error = powerbook_sleep_Core99();
2207 static void pmac_suspend_enable_irqs(void)
2209 /* Force a poll of ADB interrupts */
2210 adb_int_pending = 1;
2211 via_pmu_interrupt(0, NULL);
2215 /* Call platform functions marked "on wake" */
2216 pmac_pfunc_base_resume();
2217 pmac_pfunc_i2c_resume();
2220 static int pmu_sleep_valid(suspend_state_t state)
2222 return state == PM_SUSPEND_MEM
2223 && (pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, -1) >= 0);
2226 static struct platform_suspend_ops pmu_pm_ops = {
2227 .enter = powerbook_sleep,
2228 .valid = pmu_sleep_valid,
2231 static int register_pmu_pm_ops(void)
2233 if (pmu_kind == PMU_OHARE_BASED)
2234 powerbook_sleep_init_3400();
2235 ppc_md.suspend_disable_irqs = pmac_suspend_disable_irqs;
2236 ppc_md.suspend_enable_irqs = pmac_suspend_enable_irqs;
2237 suspend_set_ops(&pmu_pm_ops);
2242 device_initcall(register_pmu_pm_ops);
2246 pmu_ioctl(struct inode * inode, struct file *filp,
2247 u_int cmd, u_long arg)
2249 __u32 __user *argp = (__u32 __user *)arg;
2250 int error = -EINVAL;
2254 if (!capable(CAP_SYS_ADMIN))
2256 return pm_suspend(PM_SUSPEND_MEM);
2257 case PMU_IOC_CAN_SLEEP:
2258 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, -1) < 0)
2259 return put_user(0, argp);
2261 return put_user(1, argp);
2263 #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2264 /* Compatibility ioctl's for backlight */
2265 case PMU_IOC_GET_BACKLIGHT:
2269 brightness = pmac_backlight_get_legacy_brightness();
2273 return put_user(brightness, argp);
2276 case PMU_IOC_SET_BACKLIGHT:
2280 error = get_user(brightness, argp);
2284 return pmac_backlight_set_legacy_brightness(brightness);
2286 #ifdef CONFIG_INPUT_ADBHID
2287 case PMU_IOC_GRAB_BACKLIGHT: {
2288 struct pmu_private *pp = filp->private_data;
2290 if (pp->backlight_locker)
2293 pp->backlight_locker = 1;
2294 pmac_backlight_disable();
2298 #endif /* CONFIG_INPUT_ADBHID */
2299 #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
2301 case PMU_IOC_GET_MODEL:
2302 return put_user(pmu_kind, argp);
2303 case PMU_IOC_HAS_ADB:
2304 return put_user(pmu_has_adb, argp);
2309 static const struct file_operations pmu_device_fops = {
2315 .release = pmu_release,
2318 static struct miscdevice pmu_device = {
2319 PMU_MINOR, "pmu", &pmu_device_fops
2322 static int pmu_device_init(void)
2326 if (misc_register(&pmu_device) < 0)
2327 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2330 device_initcall(pmu_device_init);
2335 polled_handshake(volatile unsigned char __iomem *via)
2337 via[B] &= ~TREQ; eieio();
2338 while ((via[B] & TACK) != 0)
2340 via[B] |= TREQ; eieio();
2341 while ((via[B] & TACK) == 0)
2346 polled_send_byte(volatile unsigned char __iomem *via, int x)
2348 via[ACR] |= SR_OUT | SR_EXT; eieio();
2349 via[SR] = x; eieio();
2350 polled_handshake(via);
2354 polled_recv_byte(volatile unsigned char __iomem *via)
2358 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2359 x = via[SR]; eieio();
2360 polled_handshake(via);
2361 x = via[SR]; eieio();
2366 pmu_polled_request(struct adb_request *req)
2368 unsigned long flags;
2370 volatile unsigned char __iomem *v = via;
2374 l = pmu_data_len[c][0];
2375 if (l >= 0 && req->nbytes != l + 1)
2378 local_irq_save(flags);
2379 while (pmu_state != idle)
2382 while ((via[B] & TACK) == 0)
2384 polled_send_byte(v, c);
2386 l = req->nbytes - 1;
2387 polled_send_byte(v, l);
2389 for (i = 1; i <= l; ++i)
2390 polled_send_byte(v, req->data[i]);
2392 l = pmu_data_len[c][1];
2394 l = polled_recv_byte(v);
2395 for (i = 0; i < l; ++i)
2396 req->reply[i + req->reply_len] = polled_recv_byte(v);
2401 local_irq_restore(flags);
2405 /* N.B. This doesn't work on the 3400 */
2406 void pmu_blink(int n)
2408 struct adb_request req;
2410 memset(&req, 0, sizeof(req));
2412 for (; n > 0; --n) {
2419 req.reply[0] = ADB_RET_OK;
2421 req.reply_expected = 0;
2422 pmu_polled_request(&req);
2430 req.reply[0] = ADB_RET_OK;
2432 req.reply_expected = 0;
2433 pmu_polled_request(&req);
2438 #endif /* DEBUG_SLEEP */
2440 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
2441 int pmu_sys_suspended;
2443 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
2445 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
2448 /* Suspend PMU event interrupts */\
2450 pmu_sys_suspended = 1;
2452 #ifdef CONFIG_PMAC_BACKLIGHT
2453 /* Tell backlight code not to muck around with the chip anymore */
2454 pmu_backlight_set_sleep(1);
2460 static int pmu_sys_resume(struct sys_device *sysdev)
2462 struct adb_request req;
2464 if (!pmu_sys_suspended)
2467 /* Tell PMU we are ready */
2468 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2469 pmu_wait_complete(&req);
2471 #ifdef CONFIG_PMAC_BACKLIGHT
2472 /* Tell backlight code it can use the chip again */
2473 pmu_backlight_set_sleep(0);
2475 /* Resume PMU event interrupts */
2477 pmu_sys_suspended = 0;
2482 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
2484 static struct sysdev_class pmu_sysclass = {
2488 static struct sys_device device_pmu = {
2489 .cls = &pmu_sysclass,
2492 static struct sysdev_driver driver_pmu = {
2493 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
2494 .suspend = &pmu_sys_suspend,
2495 .resume = &pmu_sys_resume,
2496 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
2499 static int __init init_pmu_sysfs(void)
2503 rc = sysdev_class_register(&pmu_sysclass);
2505 printk(KERN_ERR "Failed registering PMU sys class\n");
2508 rc = sysdev_register(&device_pmu);
2510 printk(KERN_ERR "Failed registering PMU sys device\n");
2513 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2515 printk(KERN_ERR "Failed registering PMU sys driver\n");
2521 subsys_initcall(init_pmu_sysfs);
2523 EXPORT_SYMBOL(pmu_request);
2524 EXPORT_SYMBOL(pmu_queue_request);
2525 EXPORT_SYMBOL(pmu_poll);
2526 EXPORT_SYMBOL(pmu_poll_adb);
2527 EXPORT_SYMBOL(pmu_wait_complete);
2528 EXPORT_SYMBOL(pmu_suspend);
2529 EXPORT_SYMBOL(pmu_resume);
2530 EXPORT_SYMBOL(pmu_unlock);
2531 #if defined(CONFIG_PPC32)
2532 EXPORT_SYMBOL(pmu_enable_irled);
2533 EXPORT_SYMBOL(pmu_battery_count);
2534 EXPORT_SYMBOL(pmu_batteries);
2535 EXPORT_SYMBOL(pmu_power_flags);
2536 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */