2 * Device driver for the via-pmu on Apple Powermacs.
4 * The VIA (versatile interface adapter) interfaces to the PMU,
5 * a 6805 microprocessor core whose primary function is to control
6 * battery charging and system power on the PowerBook 3400 and 2400.
7 * The PMU also controls the ADB (Apple Desktop Bus) which connects
8 * to the keyboard and mouse, as well as the non-volatile RAM
9 * and the RTC (real time clock) chip.
11 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
12 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
14 * THIS DRIVER IS BECOMING A TOTAL MESS !
15 * - Cleanup atomically disabling reply to PMU events after
16 * a sleep or a freq. switch
17 * - Move sleep code out of here to pmac_pm, merge into new
18 * common PM infrastructure
19 * - Move backlight code out as well
20 * - Save/Restore PCI space properly
24 #include <linux/config.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/miscdevice.h>
31 #include <linux/blkdev.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/adb.h>
36 #include <linux/pmu.h>
37 #include <linux/cuda.h>
38 #include <linux/smp_lock.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
42 #include <linux/proc_fs.h>
43 #include <linux/init.h>
44 #include <linux/interrupt.h>
45 #include <linux/device.h>
46 #include <linux/sysdev.h>
47 #include <linux/suspend.h>
48 #include <linux/syscalls.h>
49 #include <linux/cpu.h>
51 #include <asm/machdep.h>
53 #include <asm/pgtable.h>
54 #include <asm/system.h>
55 #include <asm/sections.h>
57 #include <asm/pmac_feature.h>
58 #include <asm/uaccess.h>
59 #include <asm/mmu_context.h>
60 #include <asm/cputable.h>
62 #ifdef CONFIG_PMAC_BACKLIGHT
63 #include <asm/backlight.h>
67 #include <asm/open_pic.h>
70 /* Some compile options */
71 #undef SUSPEND_USES_PMU
73 #undef HACKED_PCI_SAVE
75 /* Misc minor number allocated for /dev/pmu */
78 /* How many iterations between battery polls */
79 #define BATTERY_POLLING_COUNT 2
81 static volatile unsigned char __iomem *via;
83 /* VIA registers - spaced 0x200 bytes apart */
84 #define RS 0x200 /* skip between registers */
85 #define B 0 /* B-side data */
86 #define A RS /* A-side data */
87 #define DIRB (2*RS) /* B-side direction (1=output) */
88 #define DIRA (3*RS) /* A-side direction (1=output) */
89 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
90 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
91 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
92 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
93 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
94 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
95 #define SR (10*RS) /* Shift register */
96 #define ACR (11*RS) /* Auxiliary control register */
97 #define PCR (12*RS) /* Peripheral control register */
98 #define IFR (13*RS) /* Interrupt flag register */
99 #define IER (14*RS) /* Interrupt enable register */
100 #define ANH (15*RS) /* A-side data, no handshake */
102 /* Bits in B data register: both active low */
103 #define TACK 0x08 /* Transfer acknowledge (input) */
104 #define TREQ 0x10 /* Transfer request (output) */
107 #define SR_CTRL 0x1c /* Shift register control bits */
108 #define SR_EXT 0x0c /* Shift on external clock */
109 #define SR_OUT 0x10 /* Shift out if 1 */
111 /* Bits in IFR and IER */
112 #define IER_SET 0x80 /* set bits in IER */
113 #define IER_CLR 0 /* clear bits in IER */
114 #define SR_INT 0x04 /* Shift register full/empty */
116 #define CB1_INT 0x10 /* transition on CB1 input */
118 static volatile enum pmu_state {
127 static volatile enum int_data_state {
132 } int_data_state[2] = { int_data_empty, int_data_empty };
134 static struct adb_request *current_req;
135 static struct adb_request *last_req;
136 static struct adb_request *req_awaiting_reply;
137 static unsigned char interrupt_data[2][32];
138 static int interrupt_data_len[2];
139 static int int_data_last;
140 static unsigned char *reply_ptr;
141 static int data_index;
143 static volatile int adb_int_pending;
144 static volatile int disable_poll;
145 static struct adb_request bright_req_1, bright_req_2;
146 static struct device_node *vias;
147 static int pmu_kind = PMU_UNKNOWN;
148 static int pmu_fully_inited = 0;
149 static int pmu_has_adb;
150 static unsigned char __iomem *gpio_reg = NULL;
151 static int gpio_irq = -1;
152 static int gpio_irq_enabled = -1;
153 static volatile int pmu_suspended = 0;
154 static spinlock_t pmu_lock;
155 static u8 pmu_intr_mask;
156 static int pmu_version;
157 static int drop_interrupts;
159 static int option_lid_wakeup = 1;
160 static int sleep_in_progress;
161 #endif /* CONFIG_PM */
162 static unsigned long async_req_locks;
163 static unsigned int pmu_irq_stats[11];
165 static struct proc_dir_entry *proc_pmu_root;
166 static struct proc_dir_entry *proc_pmu_info;
167 static struct proc_dir_entry *proc_pmu_irqstats;
168 static struct proc_dir_entry *proc_pmu_options;
169 static int option_server_mode;
171 int pmu_battery_count;
173 unsigned int pmu_power_flags;
174 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
175 static int query_batt_timer = BATTERY_POLLING_COUNT;
176 static struct adb_request batt_req;
177 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
179 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
180 extern int disable_kernel_backlight;
181 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
185 struct notifier_block *sleep_notifier_list;
188 static int adb_dev_map = 0;
189 static int pmu_adb_flags;
191 static int pmu_probe(void);
192 static int pmu_init(void);
193 static int pmu_send_request(struct adb_request *req, int sync);
194 static int pmu_adb_autopoll(int devs);
195 static int pmu_adb_reset_bus(void);
196 #endif /* CONFIG_ADB */
198 static int init_pmu(void);
199 static int pmu_queue_request(struct adb_request *req);
200 static void pmu_start(void);
201 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
202 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
203 static int proc_get_info(char *page, char **start, off_t off,
204 int count, int *eof, void *data);
205 static int proc_get_irqstats(char *page, char **start, off_t off,
206 int count, int *eof, void *data);
207 #ifdef CONFIG_PMAC_BACKLIGHT
208 static int pmu_set_backlight_level(int level, void* data);
209 static int pmu_set_backlight_enable(int on, int level, void* data);
210 #endif /* CONFIG_PMAC_BACKLIGHT */
211 static void pmu_pass_intr(unsigned char *data, int len);
212 static int proc_get_batt(char *page, char **start, off_t off,
213 int count, int *eof, void *data);
214 static int proc_read_options(char *page, char **start, off_t off,
215 int count, int *eof, void *data);
216 static int proc_write_options(struct file *file, const char __user *buffer,
217 unsigned long count, void *data);
220 struct adb_driver via_pmu_driver = {
229 #endif /* CONFIG_ADB */
231 extern void low_sleep_handler(void);
232 extern void enable_kernel_altivec(void);
233 extern void enable_kernel_fp(void);
236 int pmu_polled_request(struct adb_request *req);
237 int pmu_wink(struct adb_request *req);
241 * This table indicates for each PMU opcode:
242 * - the number of data bytes to be sent with the command, or -1
243 * if a length byte should be sent,
244 * - the number of response bytes which the PMU will return, or
245 * -1 if it will send a length byte.
247 static const s8 pmu_data_len[256][2] = {
248 /* 0 1 2 3 4 5 6 7 */
249 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
250 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
251 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
253 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
255 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
257 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
259 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
260 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
261 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
263 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
264 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
265 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
267 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
268 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
269 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
270 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
272 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
273 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
274 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
275 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
276 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
277 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
278 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
279 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
280 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
283 static char *pbook_type[] = {
285 "PowerBook 2400/3400/3500(G3)",
286 "PowerBook G3 Series",
291 #ifdef CONFIG_PMAC_BACKLIGHT
292 static struct backlight_controller pmu_backlight_controller = {
293 pmu_set_backlight_enable,
294 pmu_set_backlight_level
296 #endif /* CONFIG_PMAC_BACKLIGHT */
303 vias = find_devices("via-pmu");
307 printk(KERN_WARNING "Warning: only using 1st via-pmu\n");
309 if (vias->n_addrs < 1 || vias->n_intrs < 1) {
310 printk(KERN_ERR "via-pmu: %d addresses, %d interrupts!\n",
311 vias->n_addrs, vias->n_intrs);
312 if (vias->n_addrs < 1 || vias->n_intrs < 1)
316 spin_lock_init(&pmu_lock);
320 pmu_intr_mask = PMU_INT_PCEJECT |
325 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
326 || device_is_compatible(vias->parent, "ohare")))
327 pmu_kind = PMU_OHARE_BASED;
328 else if (device_is_compatible(vias->parent, "paddington"))
329 pmu_kind = PMU_PADDINGTON_BASED;
330 else if (device_is_compatible(vias->parent, "heathrow"))
331 pmu_kind = PMU_HEATHROW_BASED;
332 else if (device_is_compatible(vias->parent, "Keylargo")
333 || device_is_compatible(vias->parent, "K2-Keylargo")) {
334 struct device_node *gpio, *gpiop;
336 pmu_kind = PMU_KEYLARGO_BASED;
337 pmu_has_adb = (find_type_devices("adb") != NULL);
338 pmu_intr_mask = PMU_INT_PCEJECT |
344 gpiop = find_devices("gpio");
345 if (gpiop && gpiop->n_addrs) {
346 gpio_reg = ioremap(gpiop->addrs->address, 0x10);
347 gpio = find_devices("extint-gpio1");
349 gpio = find_devices("pmu-interrupt");
350 if (gpio && gpio->parent == gpiop && gpio->n_intrs)
351 gpio_irq = gpio->intrs[0].line;
354 pmu_kind = PMU_UNKNOWN;
356 via = ioremap(vias->addrs->address, 0x2000);
358 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
359 out_8(&via[IFR], 0x7f); /* clear IFR */
368 printk(KERN_INFO "PMU driver %d initialized for %s, firmware: %02x\n",
369 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
371 sys_ctrler = SYS_CTRLER_PMU;
380 return vias == NULL? -ENODEV: 0;
390 #endif /* CONFIG_ADB */
393 * We can't wait until pmu_init gets called, that happens too late.
394 * It happens after IDE and SCSI initialization, which can take a few
395 * seconds, and by that time the PMU could have given up on us and
397 * Thus this is called with arch_initcall rather than device_initcall.
399 static int __init via_pmu_start(void)
404 bright_req_1.complete = 1;
405 bright_req_2.complete = 1;
406 batt_req.complete = 1;
408 #if defined(CONFIG_PPC32) && !defined(CONFIG_PPC_MERGE)
409 if (pmu_kind == PMU_KEYLARGO_BASED)
410 openpic_set_irq_priority(vias->intrs[0].line,
411 OPENPIC_PRIORITY_DEFAULT + 1);
414 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
416 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
417 vias->intrs[0].line);
421 if (pmu_kind == PMU_KEYLARGO_BASED && gpio_irq != -1) {
422 if (request_irq(gpio_irq, gpio1_interrupt, 0, "GPIO1 ADB", (void *)0))
423 printk(KERN_ERR "pmu: can't get irq %d (GPIO1)\n", gpio_irq);
424 gpio_irq_enabled = 1;
427 /* Enable interrupts */
428 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
430 pmu_fully_inited = 1;
432 /* Make sure PMU settle down before continuing. This is _very_ important
433 * since the IDE probe may shut interrupts down for quite a bit of time. If
434 * a PMU communication is pending while this happens, the PMU may timeout
435 * Not that on Core99 machines, the PMU keeps sending us environement
436 * messages, we should find a way to either fix IDE or make it call
437 * pmu_suspend() before masking interrupts. This can also happens while
438 * scolling with some fbdevs.
442 } while (pmu_state != idle);
447 arch_initcall(via_pmu_start);
450 * This has to be done after pci_init, which is a subsys_initcall.
452 static int __init via_pmu_dev_init(void)
458 request_OF_resource(vias, 0, NULL);
460 #ifdef CONFIG_PMAC_BACKLIGHT
461 /* Enable backlight */
462 register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
463 #endif /* CONFIG_PMAC_BACKLIGHT */
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 = find_devices("power-mgt");
482 u32 *prim_info = NULL;
484 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
486 /* Other stuffs here yet unknown */
487 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
488 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
489 if (pmu_battery_count > 1)
490 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
493 #endif /* CONFIG_PPC32 */
495 /* Create /proc/pmu */
496 proc_pmu_root = proc_mkdir("pmu", NULL);
500 for (i=0; i<pmu_battery_count; i++) {
502 sprintf(title, "battery_%ld", i);
503 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
504 proc_get_batt, (void *)i);
507 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
508 proc_get_info, NULL);
509 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
510 proc_get_irqstats, NULL);
511 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
512 if (proc_pmu_options) {
513 proc_pmu_options->nlink = 1;
514 proc_pmu_options->read_proc = proc_read_options;
515 proc_pmu_options->write_proc = proc_write_options;
521 device_initcall(via_pmu_dev_init);
527 struct adb_request req;
529 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
530 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
532 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
534 while (!req.complete) {
536 printk(KERN_ERR "init_pmu: no response from PMU\n");
543 /* ack all pending interrupts */
545 interrupt_data[0][0] = 1;
546 while (interrupt_data[0][0] || pmu_state != idle) {
548 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
551 if (pmu_state == idle)
553 via_pmu_interrupt(0, NULL, NULL);
557 /* Tell PMU we are ready. */
558 if (pmu_kind == PMU_KEYLARGO_BASED) {
559 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
560 while (!req.complete)
564 /* Read PMU version */
565 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
566 pmu_wait_complete(&req);
567 if (req.reply_len > 0)
568 pmu_version = req.reply[0];
570 /* Read server mode setting */
571 if (pmu_kind == PMU_KEYLARGO_BASED) {
572 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
573 PMU_PWR_GET_POWERUP_EVENTS);
574 pmu_wait_complete(&req);
575 if (req.reply_len == 2) {
576 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
577 option_server_mode = 1;
578 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
579 option_server_mode ? "enabled" : "disabled");
591 static void pmu_set_server_mode(int server_mode)
593 struct adb_request req;
595 if (pmu_kind != PMU_KEYLARGO_BASED)
598 option_server_mode = server_mode;
599 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
600 pmu_wait_complete(&req);
601 if (req.reply_len < 2)
604 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
605 PMU_PWR_SET_POWERUP_EVENTS,
606 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
608 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
609 PMU_PWR_CLR_POWERUP_EVENTS,
610 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
611 pmu_wait_complete(&req);
614 /* This new version of the code for 2400/3400/3500 powerbooks
615 * is inspired from the implementation in gkrellm-pmu
618 done_battery_state_ohare(struct adb_request* req)
622 * 0x01 : AC indicator
624 * 0x04 : battery exist
627 * 0x20 : full charged
628 * 0x40 : pcharge reset
629 * 0x80 : battery exist
631 * [1][2] : battery voltage
632 * [3] : CPU temperature
633 * [4] : battery temperature
638 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
639 long pcharge, charge, vb, vmax, lmax;
640 long vmax_charging, vmax_charged;
641 long amperage, voltage, time, max;
642 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
643 NULL, PMAC_MB_INFO_MODEL, 0);
645 if (req->reply[0] & 0x01)
646 pmu_power_flags |= PMU_PWR_AC_PRESENT;
648 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
650 if (mb == PMAC_TYPE_COMET) {
661 /* If battery installed */
662 if (req->reply[0] & 0x04) {
663 bat_flags |= PMU_BATT_PRESENT;
664 if (req->reply[0] & 0x02)
665 bat_flags |= PMU_BATT_CHARGING;
666 vb = (req->reply[1] << 8) | req->reply[2];
667 voltage = (vb * 265 + 72665) / 10;
668 amperage = req->reply[5];
669 if ((req->reply[0] & 0x01) == 0) {
671 vb += ((amperage - 200) * 15)/100;
672 } else if (req->reply[0] & 0x02) {
673 vb = (vb * 97) / 100;
674 vmax = vmax_charging;
676 charge = (100 * vb) / vmax;
677 if (req->reply[0] & 0x40) {
678 pcharge = (req->reply[6] << 8) + req->reply[7];
682 pcharge = 100 - pcharge / lmax;
683 if (pcharge < charge)
687 time = (charge * 16440) / amperage;
691 amperage = -amperage;
693 charge = max = amperage = voltage = time = 0;
695 pmu_batteries[pmu_cur_battery].flags = bat_flags;
696 pmu_batteries[pmu_cur_battery].charge = charge;
697 pmu_batteries[pmu_cur_battery].max_charge = max;
698 pmu_batteries[pmu_cur_battery].amperage = amperage;
699 pmu_batteries[pmu_cur_battery].voltage = voltage;
700 pmu_batteries[pmu_cur_battery].time_remaining = time;
702 clear_bit(0, &async_req_locks);
706 done_battery_state_smart(struct adb_request* req)
709 * [0] : format of this structure (known: 3,4,5)
722 * [4][5] : max charge
727 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
729 unsigned int capa, max, voltage;
731 if (req->reply[1] & 0x01)
732 pmu_power_flags |= PMU_PWR_AC_PRESENT;
734 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
737 capa = max = amperage = voltage = 0;
739 if (req->reply[1] & 0x04) {
740 bat_flags |= PMU_BATT_PRESENT;
741 switch(req->reply[0]) {
743 case 4: capa = req->reply[2];
745 amperage = *((signed char *)&req->reply[4]);
746 voltage = req->reply[5];
748 case 5: capa = (req->reply[2] << 8) | req->reply[3];
749 max = (req->reply[4] << 8) | req->reply[5];
750 amperage = *((signed short *)&req->reply[6]);
751 voltage = (req->reply[8] << 8) | req->reply[9];
754 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
755 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
760 if ((req->reply[1] & 0x01) && (amperage > 0))
761 bat_flags |= PMU_BATT_CHARGING;
763 pmu_batteries[pmu_cur_battery].flags = bat_flags;
764 pmu_batteries[pmu_cur_battery].charge = capa;
765 pmu_batteries[pmu_cur_battery].max_charge = max;
766 pmu_batteries[pmu_cur_battery].amperage = amperage;
767 pmu_batteries[pmu_cur_battery].voltage = voltage;
769 if ((req->reply[1] & 0x01) && (amperage > 0))
770 pmu_batteries[pmu_cur_battery].time_remaining
771 = ((max-capa) * 3600) / amperage;
773 pmu_batteries[pmu_cur_battery].time_remaining
774 = (capa * 3600) / (-amperage);
776 pmu_batteries[pmu_cur_battery].time_remaining = 0;
778 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
780 clear_bit(0, &async_req_locks);
784 query_battery_state(void)
786 if (test_and_set_bit(0, &async_req_locks))
788 if (pmu_kind == PMU_OHARE_BASED)
789 pmu_request(&batt_req, done_battery_state_ohare,
790 1, PMU_BATTERY_STATE);
792 pmu_request(&batt_req, done_battery_state_smart,
793 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
797 proc_get_info(char *page, char **start, off_t off,
798 int count, int *eof, void *data)
802 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
803 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
804 p += sprintf(p, "AC Power : %d\n",
805 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0));
806 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
812 proc_get_irqstats(char *page, char **start, off_t off,
813 int count, int *eof, void *data)
817 static const char *irq_names[] = {
818 "Total CB1 triggered events",
819 "Total GPIO1 triggered events",
820 "PC-Card eject button",
821 "Sound/Brightness button",
823 "Battery state change",
824 "Environment interrupt",
826 "Ghost interrupt (zero len)",
827 "Empty interrupt (empty mask)",
831 for (i=0; i<11; i++) {
832 p += sprintf(p, " %2u: %10u (%s)\n",
833 i, pmu_irq_stats[i], irq_names[i]);
839 proc_get_batt(char *page, char **start, off_t off,
840 int count, int *eof, void *data)
842 long batnum = (long)data;
845 p += sprintf(p, "\n");
846 p += sprintf(p, "flags : %08x\n",
847 pmu_batteries[batnum].flags);
848 p += sprintf(p, "charge : %d\n",
849 pmu_batteries[batnum].charge);
850 p += sprintf(p, "max_charge : %d\n",
851 pmu_batteries[batnum].max_charge);
852 p += sprintf(p, "current : %d\n",
853 pmu_batteries[batnum].amperage);
854 p += sprintf(p, "voltage : %d\n",
855 pmu_batteries[batnum].voltage);
856 p += sprintf(p, "time rem. : %d\n",
857 pmu_batteries[batnum].time_remaining);
863 proc_read_options(char *page, char **start, off_t off,
864 int count, int *eof, void *data)
869 if (pmu_kind == PMU_KEYLARGO_BASED &&
870 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
871 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
873 if (pmu_kind == PMU_KEYLARGO_BASED)
874 p += sprintf(p, "server_mode=%d\n", option_server_mode);
880 proc_write_options(struct file *file, const char __user *buffer,
881 unsigned long count, void *data)
885 unsigned long fcount = count;
891 if (copy_from_user(tmp, buffer, count))
899 while(*val && (*val != '=')) {
910 if (pmu_kind == PMU_KEYLARGO_BASED &&
911 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
912 if (!strcmp(label, "lid_wakeup"))
913 option_lid_wakeup = ((*val) == '1');
915 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
917 new_value = ((*val) == '1');
918 if (new_value != option_server_mode)
919 pmu_set_server_mode(new_value);
925 /* Send an ADB command */
927 pmu_send_request(struct adb_request *req, int sync)
931 if ((vias == NULL) || (!pmu_fully_inited)) {
938 switch (req->data[0]) {
940 for (i = 0; i < req->nbytes - 1; ++i)
941 req->data[i] = req->data[i+1];
943 if (pmu_data_len[req->data[0]][1] != 0) {
944 req->reply[0] = ADB_RET_OK;
948 ret = pmu_queue_request(req);
951 switch (req->data[1]) {
953 if (req->nbytes != 2)
955 req->data[0] = PMU_READ_RTC;
958 req->reply[0] = CUDA_PACKET;
960 req->reply[2] = CUDA_GET_TIME;
961 ret = pmu_queue_request(req);
964 if (req->nbytes != 6)
966 req->data[0] = PMU_SET_RTC;
968 for (i = 1; i <= 4; ++i)
969 req->data[i] = req->data[i+1];
971 req->reply[0] = CUDA_PACKET;
973 req->reply[2] = CUDA_SET_TIME;
974 ret = pmu_queue_request(req);
981 for (i = req->nbytes - 1; i > 1; --i)
982 req->data[i+2] = req->data[i];
983 req->data[3] = req->nbytes - 2;
984 req->data[2] = pmu_adb_flags;
985 /*req->data[1] = req->data[1];*/
986 req->data[0] = PMU_ADB_CMD;
988 req->reply_expected = 1;
990 ret = pmu_queue_request(req);
999 while (!req->complete)
1005 /* Enable/disable autopolling */
1007 pmu_adb_autopoll(int devs)
1009 struct adb_request req;
1011 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1016 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1017 adb_dev_map >> 8, adb_dev_map);
1020 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1023 while (!req.complete)
1028 /* Reset the ADB bus */
1030 pmu_adb_reset_bus(void)
1032 struct adb_request req;
1033 int save_autopoll = adb_dev_map;
1035 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1038 /* anyone got a better idea?? */
1039 pmu_adb_autopoll(0);
1043 req.data[0] = PMU_ADB_CMD;
1045 req.data[2] = ADB_BUSRESET;
1049 req.reply_expected = 1;
1050 if (pmu_queue_request(&req) != 0) {
1051 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1054 pmu_wait_complete(&req);
1056 if (save_autopoll != 0)
1057 pmu_adb_autopoll(save_autopoll);
1061 #endif /* CONFIG_ADB */
1063 /* Construct and send a pmu request */
1065 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1074 if (nbytes < 0 || nbytes > 32) {
1075 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1079 req->nbytes = nbytes;
1081 va_start(list, nbytes);
1082 for (i = 0; i < nbytes; ++i)
1083 req->data[i] = va_arg(list, int);
1086 req->reply_expected = 0;
1087 return pmu_queue_request(req);
1091 pmu_queue_request(struct adb_request *req)
1093 unsigned long flags;
1100 if (req->nbytes <= 0) {
1104 nsend = pmu_data_len[req->data[0]][0];
1105 if (nsend >= 0 && req->nbytes != nsend + 1) {
1114 spin_lock_irqsave(&pmu_lock, flags);
1115 if (current_req != 0) {
1116 last_req->next = req;
1121 if (pmu_state == idle)
1124 spin_unlock_irqrestore(&pmu_lock, flags);
1132 /* Sightly increased the delay, I had one occurrence of the message
1136 while ((in_8(&via[B]) & TACK) == 0) {
1137 if (--timeout < 0) {
1138 printk(KERN_ERR "PMU not responding (!ack)\n");
1145 /* New PMU seems to be very sensitive to those timings, so we make sure
1146 * PCI is flushed immediately */
1150 volatile unsigned char __iomem *v = via;
1152 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1154 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1161 volatile unsigned char __iomem *v = via;
1163 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1164 in_8(&v[SR]); /* resets SR */
1165 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1170 pmu_done(struct adb_request *req)
1172 void (*done)(struct adb_request *) = req->done;
1175 /* Here, we assume that if the request has a done member, the
1176 * struct request will survive to setting req->complete to 1
1185 struct adb_request *req;
1187 /* assert pmu_state == idle */
1188 /* get the packet to send */
1190 if (req == 0 || pmu_state != idle
1191 || (/*req->reply_expected && */req_awaiting_reply))
1194 pmu_state = sending;
1196 data_len = pmu_data_len[req->data[0]][0];
1198 /* Sounds safer to make sure ACK is high before writing. This helped
1199 * kill a problem with ADB and some iBooks
1202 /* set the shift register to shift out and send a byte */
1203 send_byte(req->data[0]);
1213 via_pmu_interrupt(0, NULL, NULL);
1223 /* Kicks ADB read when PMU is suspended */
1224 adb_int_pending = 1;
1226 via_pmu_interrupt(0, NULL, NULL);
1227 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1228 || req_awaiting_reply));
1232 pmu_wait_complete(struct adb_request *req)
1236 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1237 via_pmu_interrupt(0, NULL, NULL);
1240 /* This function loops until the PMU is idle and prevents it from
1241 * anwsering to ADB interrupts. pmu_request can still be called.
1242 * This is done to avoid spurrious shutdowns when we know we'll have
1243 * interrupts switched off for a long time
1248 unsigned long flags;
1249 #ifdef SUSPEND_USES_PMU
1250 struct adb_request *req;
1255 spin_lock_irqsave(&pmu_lock, flags);
1257 if (pmu_suspended > 1) {
1258 spin_unlock_irqrestore(&pmu_lock, flags);
1263 spin_unlock_irqrestore(&pmu_lock, flags);
1264 if (req_awaiting_reply)
1265 adb_int_pending = 1;
1266 via_pmu_interrupt(0, NULL, NULL);
1267 spin_lock_irqsave(&pmu_lock, flags);
1268 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1269 #ifdef SUSPEND_USES_PMU
1270 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1271 spin_unlock_irqrestore(&pmu_lock, flags);
1272 while(!req.complete)
1274 #else /* SUSPEND_USES_PMU */
1276 disable_irq_nosync(gpio_irq);
1277 out_8(&via[IER], CB1_INT | IER_CLR);
1278 spin_unlock_irqrestore(&pmu_lock, flags);
1279 #endif /* SUSPEND_USES_PMU */
1288 unsigned long flags;
1290 if (!via || (pmu_suspended < 1))
1293 spin_lock_irqsave(&pmu_lock, flags);
1295 if (pmu_suspended > 0) {
1296 spin_unlock_irqrestore(&pmu_lock, flags);
1299 adb_int_pending = 1;
1300 #ifdef SUSPEND_USES_PMU
1301 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1302 spin_unlock_irqrestore(&pmu_lock, flags);
1303 while(!req.complete)
1305 #else /* SUSPEND_USES_PMU */
1307 enable_irq(gpio_irq);
1308 out_8(&via[IER], CB1_INT | IER_SET);
1309 spin_unlock_irqrestore(&pmu_lock, flags);
1311 #endif /* SUSPEND_USES_PMU */
1314 /* Interrupt data could be the result data from an ADB cmd */
1316 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1318 unsigned char ints, pirq;
1322 if (drop_interrupts || len < 1) {
1323 adb_int_pending = 0;
1328 /* Get PMU interrupt mask */
1331 /* Record zero interrupts for stats */
1335 /* Hack to deal with ADB autopoll flag */
1336 if (ints & PMU_INT_ADB)
1337 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1342 if (i > pmu_irq_stats[10])
1343 pmu_irq_stats[10] = i;
1347 for (pirq = 0; pirq < 8; pirq++)
1348 if (ints & (1 << pirq))
1350 pmu_irq_stats[pirq]++;
1352 ints &= ~(1 << pirq);
1354 /* Note: for some reason, we get an interrupt with len=1,
1355 * data[0]==0 after each normal ADB interrupt, at least
1356 * on the Pismo. Still investigating... --BenH
1358 if ((1 << pirq) & PMU_INT_ADB) {
1359 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1360 struct adb_request *req = req_awaiting_reply;
1362 printk(KERN_ERR "PMU: extra ADB reply\n");
1365 req_awaiting_reply = NULL;
1369 memcpy(req->reply, data + 1, len - 1);
1370 req->reply_len = len - 1;
1374 #if defined(CONFIG_XMON) && !defined(CONFIG_PPC64)
1375 if (len == 4 && data[1] == 0x2c) {
1376 extern int xmon_wants_key, xmon_adb_keycode;
1377 if (xmon_wants_key) {
1378 xmon_adb_keycode = data[2];
1382 #endif /* defined(CONFIG_XMON) && !defined(CONFIG_PPC64) */
1385 * XXX On the [23]400 the PMU gives us an up
1386 * event for keycodes 0x74 or 0x75 when the PC
1387 * card eject buttons are released, so we
1388 * ignore those events.
1390 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1391 && data[1] == 0x2c && data[3] == 0xff
1392 && (data[2] & ~1) == 0xf4))
1393 adb_input(data+1, len-1, regs, 1);
1394 #endif /* CONFIG_ADB */
1397 /* Sound/brightness button pressed */
1398 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1399 #ifdef CONFIG_PMAC_BACKLIGHT
1401 #ifdef CONFIG_INPUT_ADBHID
1402 if (!disable_kernel_backlight)
1403 #endif /* CONFIG_INPUT_ADBHID */
1404 set_backlight_level(data[1] >> 4);
1405 #endif /* CONFIG_PMAC_BACKLIGHT */
1407 /* Tick interrupt */
1408 else if ((1 << pirq) & PMU_INT_TICK) {
1409 /* Environement or tick interrupt, query batteries */
1410 if (pmu_battery_count) {
1411 if ((--query_batt_timer) == 0) {
1412 query_battery_state();
1413 query_batt_timer = BATTERY_POLLING_COUNT;
1417 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1418 if (pmu_battery_count)
1419 query_battery_state();
1420 pmu_pass_intr(data, len);
1422 pmu_pass_intr(data, len);
1427 static struct adb_request*
1428 pmu_sr_intr(struct pt_regs *regs)
1430 struct adb_request *req;
1433 if (via[B] & TREQ) {
1434 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1435 out_8(&via[IFR], SR_INT);
1438 /* The ack may not yet be low when we get the interrupt */
1439 while ((in_8(&via[B]) & TACK) != 0)
1442 /* if reading grab the byte, and reset the interrupt */
1443 if (pmu_state == reading || pmu_state == reading_intr)
1444 bite = in_8(&via[SR]);
1446 /* reset TREQ and wait for TACK to go high */
1447 out_8(&via[B], in_8(&via[B]) | TREQ);
1450 switch (pmu_state) {
1454 data_len = req->nbytes - 1;
1455 send_byte(data_len);
1458 if (data_index <= data_len) {
1459 send_byte(req->data[data_index++]);
1463 data_len = pmu_data_len[req->data[0]][1];
1464 if (data_len == 0) {
1466 current_req = req->next;
1467 if (req->reply_expected)
1468 req_awaiting_reply = req;
1472 pmu_state = reading;
1474 reply_ptr = req->reply + req->reply_len;
1482 pmu_state = reading_intr;
1483 reply_ptr = interrupt_data[int_data_last];
1485 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1486 enable_irq(gpio_irq);
1487 gpio_irq_enabled = 1;
1493 if (data_len == -1) {
1496 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1497 } else if (data_index < 32) {
1498 reply_ptr[data_index++] = bite;
1500 if (data_index < data_len) {
1505 if (pmu_state == reading_intr) {
1507 int_data_state[int_data_last] = int_data_ready;
1508 interrupt_data_len[int_data_last] = data_len;
1512 * For PMU sleep and freq change requests, we lock the
1513 * PMU until it's explicitely unlocked. This avoids any
1514 * spurrious event polling getting in
1516 current_req = req->next;
1517 req->reply_len += data_index;
1518 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1527 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1534 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1536 unsigned long flags;
1540 struct adb_request *req = NULL;
1543 /* This is a bit brutal, we can probably do better */
1544 spin_lock_irqsave(&pmu_lock, flags);
1548 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1552 if (++nloop > 1000) {
1553 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1554 "intr=%x, ier=%x pmu_state=%d\n",
1555 intr, in_8(&via[IER]), pmu_state);
1558 out_8(&via[IFR], intr);
1559 if (intr & CB1_INT) {
1560 adb_int_pending = 1;
1563 if (intr & SR_INT) {
1564 req = pmu_sr_intr(regs);
1571 if (pmu_state == idle) {
1572 if (adb_int_pending) {
1573 if (int_data_state[0] == int_data_empty)
1575 else if (int_data_state[1] == int_data_empty)
1580 int_data_state[int_data_last] = int_data_fill;
1581 /* Sounds safer to make sure ACK is high before writing.
1582 * This helped kill a problem with ADB and some iBooks
1585 send_byte(PMU_INT_ACK);
1586 adb_int_pending = 0;
1587 } else if (current_req)
1591 /* Mark the oldest buffer for flushing */
1592 if (int_data_state[!int_data_last] == int_data_ready) {
1593 int_data_state[!int_data_last] = int_data_flush;
1594 int_data = !int_data_last;
1595 } else if (int_data_state[int_data_last] == int_data_ready) {
1596 int_data_state[int_data_last] = int_data_flush;
1597 int_data = int_data_last;
1600 spin_unlock_irqrestore(&pmu_lock, flags);
1602 /* Deal with completed PMU requests outside of the lock */
1608 /* Deal with interrupt datas outside of the lock */
1609 if (int_data >= 0) {
1610 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1611 spin_lock_irqsave(&pmu_lock, flags);
1613 int_data_state[int_data] = int_data_empty;
1618 return IRQ_RETVAL(handled);
1624 unsigned long flags;
1626 spin_lock_irqsave(&pmu_lock, flags);
1627 if (pmu_state == locked)
1629 adb_int_pending = 1;
1630 spin_unlock_irqrestore(&pmu_lock, flags);
1635 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1637 unsigned long flags;
1639 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1640 spin_lock_irqsave(&pmu_lock, flags);
1641 if (gpio_irq_enabled > 0) {
1642 disable_irq_nosync(gpio_irq);
1643 gpio_irq_enabled = 0;
1646 adb_int_pending = 1;
1647 spin_unlock_irqrestore(&pmu_lock, flags);
1648 via_pmu_interrupt(0, NULL, NULL);
1654 #ifdef CONFIG_PMAC_BACKLIGHT
1655 static int backlight_to_bright[] = {
1656 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1657 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1661 pmu_set_backlight_enable(int on, int level, void* data)
1663 struct adb_request req;
1669 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1670 backlight_to_bright[level]);
1671 pmu_wait_complete(&req);
1673 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1674 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1675 pmu_wait_complete(&req);
1681 pmu_bright_complete(struct adb_request *req)
1683 if (req == &bright_req_1)
1684 clear_bit(1, &async_req_locks);
1685 if (req == &bright_req_2)
1686 clear_bit(2, &async_req_locks);
1690 pmu_set_backlight_level(int level, void* data)
1695 if (test_and_set_bit(1, &async_req_locks))
1697 pmu_request(&bright_req_1, pmu_bright_complete, 2, PMU_BACKLIGHT_BRIGHT,
1698 backlight_to_bright[level]);
1699 if (test_and_set_bit(2, &async_req_locks))
1701 pmu_request(&bright_req_2, pmu_bright_complete, 2, PMU_POWER_CTRL,
1702 PMU_POW_BACKLIGHT | (level > BACKLIGHT_OFF ?
1703 PMU_POW_ON : PMU_POW_OFF));
1707 #endif /* CONFIG_PMAC_BACKLIGHT */
1710 pmu_enable_irled(int on)
1712 struct adb_request req;
1716 if (pmu_kind == PMU_KEYLARGO_BASED)
1719 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1720 (on ? PMU_POW_ON : PMU_POW_OFF));
1721 pmu_wait_complete(&req);
1727 struct adb_request req;
1732 local_irq_disable();
1734 drop_interrupts = 1;
1736 if (pmu_kind != PMU_KEYLARGO_BASED) {
1737 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1739 while(!req.complete)
1743 pmu_request(&req, NULL, 1, PMU_RESET);
1744 pmu_wait_complete(&req);
1752 struct adb_request req;
1757 local_irq_disable();
1759 drop_interrupts = 1;
1761 if (pmu_kind != PMU_KEYLARGO_BASED) {
1762 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1764 pmu_wait_complete(&req);
1766 /* Disable server mode on shutdown or we'll just
1769 pmu_set_server_mode(0);
1772 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1773 'M', 'A', 'T', 'T');
1774 pmu_wait_complete(&req);
1785 struct pmu_i2c_hdr {
1796 pmu_i2c_combined_read(int bus, int addr, int subaddr, u8* data, int len)
1798 struct adb_request req;
1799 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1803 for (retry=0; retry<16; retry++) {
1804 memset(&req, 0, sizeof(req));
1807 hdr->address = addr & 0xfe;
1808 hdr->mode = PMU_I2C_MODE_COMBINED;
1810 hdr->sub_addr = subaddr;
1811 hdr->comb_addr = addr | 1;
1814 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1815 req.reply_expected = 0;
1817 req.data[0] = PMU_I2C_CMD;
1818 req.reply[0] = 0xff;
1819 rc = pmu_queue_request(&req);
1822 while(!req.complete)
1824 if (req.reply[0] == PMU_I2C_STATUS_OK)
1828 if (req.reply[0] != PMU_I2C_STATUS_OK)
1831 for (retry=0; retry<16; retry++) {
1832 memset(&req, 0, sizeof(req));
1836 hdr->bus = PMU_I2C_BUS_STATUS;
1837 req.reply[0] = 0xff;
1840 req.reply_expected = 0;
1842 req.data[0] = PMU_I2C_CMD;
1843 rc = pmu_queue_request(&req);
1846 while(!req.complete)
1848 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1849 memcpy(data, &req.reply[1], req.reply_len - 1);
1850 return req.reply_len - 1;
1857 pmu_i2c_stdsub_write(int bus, int addr, int subaddr, u8* data, int len)
1859 struct adb_request req;
1860 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1864 for (retry=0; retry<16; retry++) {
1865 memset(&req, 0, sizeof(req));
1868 hdr->address = addr & 0xfe;
1869 hdr->mode = PMU_I2C_MODE_STDSUB;
1871 hdr->sub_addr = subaddr;
1872 hdr->comb_addr = addr & 0xfe;
1875 req.data[0] = PMU_I2C_CMD;
1876 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
1877 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
1878 req.reply_expected = 0;
1880 req.reply[0] = 0xff;
1881 rc = pmu_queue_request(&req);
1884 while(!req.complete)
1886 if (req.reply[0] == PMU_I2C_STATUS_OK)
1890 if (req.reply[0] != PMU_I2C_STATUS_OK)
1893 for (retry=0; retry<16; retry++) {
1894 memset(&req, 0, sizeof(req));
1898 hdr->bus = PMU_I2C_BUS_STATUS;
1899 req.reply[0] = 0xff;
1902 req.reply_expected = 0;
1904 req.data[0] = PMU_I2C_CMD;
1905 rc = pmu_queue_request(&req);
1908 while(!req.complete)
1910 if (req.reply[0] == PMU_I2C_STATUS_OK)
1917 pmu_i2c_simple_read(int bus, int addr, u8* data, int len)
1919 struct adb_request req;
1920 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1924 for (retry=0; retry<16; retry++) {
1925 memset(&req, 0, sizeof(req));
1928 hdr->address = addr | 1;
1929 hdr->mode = PMU_I2C_MODE_SIMPLE;
1935 req.data[0] = PMU_I2C_CMD;
1936 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1937 req.reply_expected = 0;
1939 req.reply[0] = 0xff;
1940 rc = pmu_queue_request(&req);
1943 while(!req.complete)
1945 if (req.reply[0] == PMU_I2C_STATUS_OK)
1949 if (req.reply[0] != PMU_I2C_STATUS_OK)
1952 for (retry=0; retry<16; retry++) {
1953 memset(&req, 0, sizeof(req));
1957 hdr->bus = PMU_I2C_BUS_STATUS;
1958 req.reply[0] = 0xff;
1961 req.reply_expected = 0;
1963 req.data[0] = PMU_I2C_CMD;
1964 rc = pmu_queue_request(&req);
1967 while(!req.complete)
1969 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1970 memcpy(data, &req.reply[1], req.reply_len - 1);
1971 return req.reply_len - 1;
1978 pmu_i2c_simple_write(int bus, int addr, u8* data, int len)
1980 struct adb_request req;
1981 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1985 for (retry=0; retry<16; retry++) {
1986 memset(&req, 0, sizeof(req));
1989 hdr->address = addr & 0xfe;
1990 hdr->mode = PMU_I2C_MODE_SIMPLE;
1996 req.data[0] = PMU_I2C_CMD;
1997 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
1998 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
1999 req.reply_expected = 0;
2001 req.reply[0] = 0xff;
2002 rc = pmu_queue_request(&req);
2005 while(!req.complete)
2007 if (req.reply[0] == PMU_I2C_STATUS_OK)
2011 if (req.reply[0] != PMU_I2C_STATUS_OK)
2014 for (retry=0; retry<16; retry++) {
2015 memset(&req, 0, sizeof(req));
2019 hdr->bus = PMU_I2C_BUS_STATUS;
2020 req.reply[0] = 0xff;
2023 req.reply_expected = 0;
2025 req.data[0] = PMU_I2C_CMD;
2026 rc = pmu_queue_request(&req);
2029 while(!req.complete)
2031 if (req.reply[0] == PMU_I2C_STATUS_OK)
2039 static LIST_HEAD(sleep_notifiers);
2042 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
2044 struct list_head *list;
2045 struct pmu_sleep_notifier *notifier;
2047 for (list = sleep_notifiers.next; list != &sleep_notifiers;
2048 list = list->next) {
2049 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2050 if (n->priority > notifier->priority)
2053 __list_add(&n->list, list->prev, list);
2058 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
2060 if (n->list.next == 0)
2063 n->list.next = NULL;
2067 /* Sleep is broadcast last-to-first */
2069 broadcast_sleep(int when, int fallback)
2071 int ret = PBOOK_SLEEP_OK;
2072 struct list_head *list;
2073 struct pmu_sleep_notifier *notifier;
2075 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
2076 list = list->prev) {
2077 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2078 ret = notifier->notifier_call(notifier, when);
2079 if (ret != PBOOK_SLEEP_OK) {
2080 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
2081 when, notifier, notifier->notifier_call);
2082 for (; list != &sleep_notifiers; list = list->next) {
2083 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2084 notifier->notifier_call(notifier, fallback);
2092 /* Wake is broadcast first-to-last */
2094 broadcast_wake(void)
2096 int ret = PBOOK_SLEEP_OK;
2097 struct list_head *list;
2098 struct pmu_sleep_notifier *notifier;
2100 for (list = sleep_notifiers.next; list != &sleep_notifiers;
2101 list = list->next) {
2102 notifier = list_entry(list, struct pmu_sleep_notifier, list);
2103 notifier->notifier_call(notifier, PBOOK_WAKE);
2109 * This struct is used to store config register values for
2110 * PCI devices which may get powered off when we sleep.
2112 static struct pci_save {
2113 #ifndef HACKED_PCI_SAVE
2122 static int pbook_npci_saves;
2125 pbook_alloc_pci_save(void)
2128 struct pci_dev *pd = NULL;
2131 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2136 pbook_pci_saves = (struct pci_save *)
2137 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
2138 pbook_npci_saves = npci;
2142 pbook_free_pci_save(void)
2144 if (pbook_pci_saves == NULL)
2146 kfree(pbook_pci_saves);
2147 pbook_pci_saves = NULL;
2148 pbook_npci_saves = 0;
2152 pbook_pci_save(void)
2154 struct pci_save *ps = pbook_pci_saves;
2155 struct pci_dev *pd = NULL;
2156 int npci = pbook_npci_saves;
2161 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2164 #ifndef HACKED_PCI_SAVE
2165 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
2166 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
2167 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
2168 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
2172 pci_read_config_dword(pd, i<<4, &ps->config[i]);
2178 /* For this to work, we must take care of a few things: If gmac was enabled
2179 * during boot, it will be in the pci dev list. If it's disabled at this point
2180 * (and it will probably be), then you can't access it's config space.
2183 pbook_pci_restore(void)
2186 struct pci_save *ps = pbook_pci_saves - 1;
2187 struct pci_dev *pd = NULL;
2188 int npci = pbook_npci_saves;
2191 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
2192 #ifdef HACKED_PCI_SAVE
2198 pci_write_config_dword(pd, i<<4, ps->config[i]);
2199 pci_write_config_dword(pd, 4, ps->config[1]);
2204 if (ps->command == 0)
2206 pci_read_config_word(pd, PCI_COMMAND, &cmd);
2207 if ((ps->command & ~cmd) == 0)
2209 switch (pd->hdr_type) {
2210 case PCI_HEADER_TYPE_NORMAL:
2211 for (j = 0; j < 6; ++j)
2212 pci_write_config_dword(pd,
2213 PCI_BASE_ADDRESS_0 + j*4,
2214 pd->resource[j].start);
2215 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
2217 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
2219 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
2221 pci_write_config_word(pd, PCI_COMMAND, ps->command);
2229 /* N.B. This doesn't work on the 3400 */
2233 struct adb_request req;
2235 memset(&req, 0, sizeof(req));
2237 for (; n > 0; --n) {
2244 req.reply[0] = ADB_RET_OK;
2246 req.reply_expected = 0;
2247 pmu_polled_request(&req);
2255 req.reply[0] = ADB_RET_OK;
2257 req.reply_expected = 0;
2258 pmu_polled_request(&req);
2266 * Put the powerbook to sleep.
2269 static u32 save_via[8];
2272 save_via_state(void)
2274 save_via[0] = in_8(&via[ANH]);
2275 save_via[1] = in_8(&via[DIRA]);
2276 save_via[2] = in_8(&via[B]);
2277 save_via[3] = in_8(&via[DIRB]);
2278 save_via[4] = in_8(&via[PCR]);
2279 save_via[5] = in_8(&via[ACR]);
2280 save_via[6] = in_8(&via[T1CL]);
2281 save_via[7] = in_8(&via[T1CH]);
2284 restore_via_state(void)
2286 out_8(&via[ANH], save_via[0]);
2287 out_8(&via[DIRA], save_via[1]);
2288 out_8(&via[B], save_via[2]);
2289 out_8(&via[DIRB], save_via[3]);
2290 out_8(&via[PCR], save_via[4]);
2291 out_8(&via[ACR], save_via[5]);
2292 out_8(&via[T1CL], save_via[6]);
2293 out_8(&via[T1CH], save_via[7]);
2294 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
2295 out_8(&via[IFR], 0x7f); /* clear IFR */
2296 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2300 pmac_suspend_devices(void)
2304 pm_prepare_console();
2306 /* Notify old-style device drivers & userland */
2307 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2308 if (ret != PBOOK_SLEEP_OK) {
2309 printk(KERN_ERR "Sleep rejected by drivers\n");
2313 /* Sync the disks. */
2314 /* XXX It would be nice to have some way to ensure that
2315 * nobody is dirtying any new buffers while we wait. That
2316 * could be achieved using the refrigerator for processes
2321 /* Sleep can fail now. May not be very robust but useful for debugging */
2322 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2323 if (ret != PBOOK_SLEEP_OK) {
2324 printk(KERN_ERR "Driver sleep failed\n");
2328 /* Send suspend call to devices, hold the device core's dpm_sem */
2329 ret = device_suspend(PMSG_SUSPEND);
2332 printk(KERN_ERR "Driver sleep failed\n");
2336 /* Disable clock spreading on some machines */
2337 pmac_tweak_clock_spreading(0);
2339 /* Stop preemption */
2342 /* Make sure the decrementer won't interrupt us */
2343 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2344 /* Make sure any pending DEC interrupt occurring while we did
2345 * the above didn't re-enable the DEC */
2347 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2349 /* We can now disable MSR_EE. This code of course works properly only
2350 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2351 * stop the "other" CPUs way before we do all that stuff.
2353 local_irq_disable();
2355 /* Broadcast power down irq
2356 * This isn't that useful in most cases (only directly wired devices can
2357 * use this but still... This will take care of sysdev's as well, so
2358 * we exit from here with local irqs disabled and PIC off.
2360 ret = device_power_down(PMSG_SUSPEND);
2362 wakeup_decrementer();
2367 printk(KERN_ERR "Driver powerdown failed\n");
2371 /* Wait for completion of async backlight requests */
2372 while (!bright_req_1.complete || !bright_req_2.complete ||
2376 /* Giveup the lazy FPU & vec so we don't have to back them
2377 * up from the low level code
2381 #ifdef CONFIG_ALTIVEC
2382 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2383 enable_kernel_altivec();
2384 #endif /* CONFIG_ALTIVEC */
2390 pmac_wakeup_devices(void)
2394 /* Power back up system devices (including the PIC) */
2397 /* Force a poll of ADB interrupts */
2398 adb_int_pending = 1;
2399 via_pmu_interrupt(0, NULL, NULL);
2401 /* Restart jiffies & scheduling */
2402 wakeup_decrementer();
2404 /* Re-enable local CPU interrupts */
2409 /* Re-enable clock spreading on some machines */
2410 pmac_tweak_clock_spreading(1);
2412 /* Resume devices */
2415 /* Notify old style drivers */
2418 pm_restore_console();
2423 #define GRACKLE_PM (1<<7)
2424 #define GRACKLE_DOZE (1<<5)
2425 #define GRACKLE_NAP (1<<4)
2426 #define GRACKLE_SLEEP (1<<3)
2429 powerbook_sleep_grackle(void)
2431 unsigned long save_l2cr;
2432 unsigned short pmcr1;
2433 struct adb_request req;
2435 struct pci_dev *grackle;
2437 grackle = pci_find_slot(0, 0);
2441 ret = pmac_suspend_devices();
2443 printk(KERN_ERR "Sleep rejected by devices\n");
2447 /* Turn off various things. Darwin does some retry tests here... */
2448 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2449 pmu_wait_complete(&req);
2450 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2451 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2452 pmu_wait_complete(&req);
2454 /* For 750, save backside cache setting and disable it */
2455 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2457 if (!__fake_sleep) {
2458 /* Ask the PMU to put us to sleep */
2459 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2460 pmu_wait_complete(&req);
2463 /* The VIA is supposed not to be restored correctly*/
2465 /* We shut down some HW */
2466 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2468 pci_read_config_word(grackle, 0x70, &pmcr1);
2469 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2470 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2471 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2472 pci_write_config_word(grackle, 0x70, pmcr1);
2474 /* Call low-level ASM sleep handler */
2478 low_sleep_handler();
2480 /* We're awake again, stop grackle PM */
2481 pci_read_config_word(grackle, 0x70, &pmcr1);
2482 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2483 pci_write_config_word(grackle, 0x70, pmcr1);
2485 /* Make sure the PMU is idle */
2486 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2487 restore_via_state();
2489 /* Restore L2 cache */
2490 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2491 _set_L2CR(save_l2cr);
2493 /* Restore userland MMU context */
2494 set_context(current->active_mm->context, current->active_mm->pgd);
2496 /* Power things up */
2498 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2499 pmu_wait_complete(&req);
2500 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2501 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2502 pmu_wait_complete(&req);
2503 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2504 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2505 pmu_wait_complete(&req);
2507 pmac_wakeup_devices();
2513 powerbook_sleep_Core99(void)
2515 unsigned long save_l2cr;
2516 unsigned long save_l3cr;
2517 struct adb_request req;
2520 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2521 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2525 if (num_online_cpus() > 1 || cpu_is_offline(0))
2528 ret = pmac_suspend_devices();
2530 printk(KERN_ERR "Sleep rejected by devices\n");
2534 /* Stop environment and ADB interrupts */
2535 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2536 pmu_wait_complete(&req);
2538 /* Tell PMU what events will wake us up */
2539 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2541 pmu_wait_complete(&req);
2542 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2543 0, PMU_PWR_WAKEUP_KEY |
2544 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2545 pmu_wait_complete(&req);
2547 /* Save the state of the L2 and L3 caches */
2548 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2549 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2551 if (!__fake_sleep) {
2552 /* Ask the PMU to put us to sleep */
2553 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2554 pmu_wait_complete(&req);
2557 /* The VIA is supposed not to be restored correctly*/
2560 /* Shut down various ASICs. There's a chance that we can no longer
2561 * talk to the PMU after this, so I moved it to _after_ sending the
2562 * sleep command to it. Still need to be checked.
2564 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2566 /* Call low-level ASM sleep handler */
2570 low_sleep_handler();
2572 /* Restore Apple core ASICs state */
2573 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2576 restore_via_state();
2578 /* tweak LPJ before cpufreq is there */
2579 loops_per_jiffy *= 2;
2582 pmac_call_early_video_resume();
2584 /* Restore L2 cache */
2585 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2586 _set_L2CR(save_l2cr);
2587 /* Restore L3 cache */
2588 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2589 _set_L3CR(save_l3cr);
2591 /* Restore userland MMU context */
2592 set_context(current->active_mm->context, current->active_mm->pgd);
2594 /* Tell PMU we are ready */
2596 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2597 pmu_wait_complete(&req);
2598 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2599 pmu_wait_complete(&req);
2601 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2602 loops_per_jiffy /= 2;
2604 pmac_wakeup_devices();
2609 #define PB3400_MEM_CTRL 0xf8000000
2610 #define PB3400_MEM_CTRL_SLEEP 0x70
2613 powerbook_sleep_3400(void)
2618 struct adb_request sleep_req;
2619 void __iomem *mem_ctrl;
2620 unsigned int __iomem *mem_ctrl_sleep;
2622 /* first map in the memory controller registers */
2623 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2624 if (mem_ctrl == NULL) {
2625 printk("powerbook_sleep_3400: ioremap failed\n");
2628 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2630 /* Allocate room for PCI save */
2631 pbook_alloc_pci_save();
2633 ret = pmac_suspend_devices();
2635 pbook_free_pci_save();
2636 printk(KERN_ERR "Sleep rejected by devices\n");
2640 /* Save the state of PCI config space for some slots */
2643 /* Set the memory controller to keep the memory refreshed
2644 while we're asleep */
2645 for (i = 0x403f; i >= 0x4000; --i) {
2646 out_be32(mem_ctrl_sleep, i);
2648 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2654 /* Ask the PMU to put us to sleep */
2655 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2656 while (!sleep_req.complete)
2659 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2661 /* displacement-flush the L2 cache - necessary? */
2662 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2663 i = *(volatile int *)p;
2666 /* Put the CPU into sleep mode */
2667 asm volatile("mfspr %0,1008" : "=r" (hid0) :);
2668 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2669 asm volatile("mtspr 1008,%0" : : "r" (hid0));
2670 _nmask_and_or_msr(0, MSR_POW | MSR_EE);
2673 /* OK, we're awake again, start restoring things */
2674 out_be32(mem_ctrl_sleep, 0x3f);
2675 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2676 pbook_pci_restore();
2679 /* wait for the PMU interrupt sequence to complete */
2683 pmac_wakeup_devices();
2684 pbook_free_pci_save();
2690 #endif /* CONFIG_PM */
2693 * Support for /dev/pmu device
2695 #define RB_SIZE 0x10
2696 struct pmu_private {
2697 struct list_head list;
2702 unsigned char data[16];
2704 wait_queue_head_t wait;
2706 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2707 int backlight_locker;
2708 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2711 static LIST_HEAD(all_pmu_pvt);
2712 static DEFINE_SPINLOCK(all_pvt_lock);
2715 pmu_pass_intr(unsigned char *data, int len)
2717 struct pmu_private *pp;
2718 struct list_head *list;
2720 unsigned long flags;
2722 if (len > sizeof(pp->rb_buf[0].data))
2723 len = sizeof(pp->rb_buf[0].data);
2724 spin_lock_irqsave(&all_pvt_lock, flags);
2725 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2726 pp = list_entry(list, struct pmu_private, list);
2727 spin_lock(&pp->lock);
2731 if (i != pp->rb_get) {
2732 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2734 memcpy(rp->data, data, len);
2736 wake_up_interruptible(&pp->wait);
2738 spin_unlock(&pp->lock);
2740 spin_unlock_irqrestore(&all_pvt_lock, flags);
2744 pmu_open(struct inode *inode, struct file *file)
2746 struct pmu_private *pp;
2747 unsigned long flags;
2749 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2752 pp->rb_get = pp->rb_put = 0;
2753 spin_lock_init(&pp->lock);
2754 init_waitqueue_head(&pp->wait);
2755 spin_lock_irqsave(&all_pvt_lock, flags);
2756 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2757 pp->backlight_locker = 0;
2758 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2759 list_add(&pp->list, &all_pmu_pvt);
2760 spin_unlock_irqrestore(&all_pvt_lock, flags);
2761 file->private_data = pp;
2766 pmu_read(struct file *file, char __user *buf,
2767 size_t count, loff_t *ppos)
2769 struct pmu_private *pp = file->private_data;
2770 DECLARE_WAITQUEUE(wait, current);
2771 unsigned long flags;
2774 if (count < 1 || pp == 0)
2776 if (!access_ok(VERIFY_WRITE, buf, count))
2779 spin_lock_irqsave(&pp->lock, flags);
2780 add_wait_queue(&pp->wait, &wait);
2781 current->state = TASK_INTERRUPTIBLE;
2785 if (pp->rb_get != pp->rb_put) {
2787 struct rb_entry *rp = &pp->rb_buf[i];
2789 spin_unlock_irqrestore(&pp->lock, flags);
2792 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2796 spin_lock_irqsave(&pp->lock, flags);
2801 if (file->f_flags & O_NONBLOCK)
2804 if (signal_pending(current))
2806 spin_unlock_irqrestore(&pp->lock, flags);
2808 spin_lock_irqsave(&pp->lock, flags);
2810 current->state = TASK_RUNNING;
2811 remove_wait_queue(&pp->wait, &wait);
2812 spin_unlock_irqrestore(&pp->lock, flags);
2818 pmu_write(struct file *file, const char __user *buf,
2819 size_t count, loff_t *ppos)
2825 pmu_fpoll(struct file *filp, poll_table *wait)
2827 struct pmu_private *pp = filp->private_data;
2828 unsigned int mask = 0;
2829 unsigned long flags;
2833 poll_wait(filp, &pp->wait, wait);
2834 spin_lock_irqsave(&pp->lock, flags);
2835 if (pp->rb_get != pp->rb_put)
2837 spin_unlock_irqrestore(&pp->lock, flags);
2842 pmu_release(struct inode *inode, struct file *file)
2844 struct pmu_private *pp = file->private_data;
2845 unsigned long flags;
2849 file->private_data = NULL;
2850 spin_lock_irqsave(&all_pvt_lock, flags);
2851 list_del(&pp->list);
2852 spin_unlock_irqrestore(&all_pvt_lock, flags);
2853 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2854 if (pp->backlight_locker) {
2855 spin_lock_irqsave(&pmu_lock, flags);
2856 disable_kernel_backlight--;
2857 spin_unlock_irqrestore(&pmu_lock, flags);
2859 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2867 pmu_ioctl(struct inode * inode, struct file *filp,
2868 u_int cmd, u_long arg)
2870 __u32 __user *argp = (__u32 __user *)arg;
2871 int error = -EINVAL;
2876 if (!capable(CAP_SYS_ADMIN))
2878 if (sleep_in_progress)
2880 sleep_in_progress = 1;
2882 case PMU_OHARE_BASED:
2883 error = powerbook_sleep_3400();
2885 case PMU_HEATHROW_BASED:
2886 case PMU_PADDINGTON_BASED:
2887 error = powerbook_sleep_grackle();
2889 case PMU_KEYLARGO_BASED:
2890 error = powerbook_sleep_Core99();
2895 sleep_in_progress = 0;
2897 case PMU_IOC_CAN_SLEEP:
2898 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2899 return put_user(0, argp);
2901 return put_user(1, argp);
2902 #endif /* CONFIG_PM */
2904 #ifdef CONFIG_PMAC_BACKLIGHT
2905 /* Backlight should have its own device or go via
2908 case PMU_IOC_GET_BACKLIGHT:
2909 if (sleep_in_progress)
2911 error = get_backlight_level();
2914 return put_user(error, argp);
2915 case PMU_IOC_SET_BACKLIGHT:
2918 if (sleep_in_progress)
2920 error = get_user(value, argp);
2922 error = set_backlight_level(value);
2925 #ifdef CONFIG_INPUT_ADBHID
2926 case PMU_IOC_GRAB_BACKLIGHT: {
2927 struct pmu_private *pp = filp->private_data;
2928 unsigned long flags;
2930 if (pp->backlight_locker)
2932 pp->backlight_locker = 1;
2933 spin_lock_irqsave(&pmu_lock, flags);
2934 disable_kernel_backlight++;
2935 spin_unlock_irqrestore(&pmu_lock, flags);
2938 #endif /* CONFIG_INPUT_ADBHID */
2939 #endif /* CONFIG_PMAC_BACKLIGHT */
2940 case PMU_IOC_GET_MODEL:
2941 return put_user(pmu_kind, argp);
2942 case PMU_IOC_HAS_ADB:
2943 return put_user(pmu_has_adb, argp);
2948 static struct file_operations pmu_device_fops = {
2954 .release = pmu_release,
2957 static struct miscdevice pmu_device = {
2958 PMU_MINOR, "pmu", &pmu_device_fops
2961 static int pmu_device_init(void)
2965 if (misc_register(&pmu_device) < 0)
2966 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2969 device_initcall(pmu_device_init);
2974 polled_handshake(volatile unsigned char __iomem *via)
2976 via[B] &= ~TREQ; eieio();
2977 while ((via[B] & TACK) != 0)
2979 via[B] |= TREQ; eieio();
2980 while ((via[B] & TACK) == 0)
2985 polled_send_byte(volatile unsigned char __iomem *via, int x)
2987 via[ACR] |= SR_OUT | SR_EXT; eieio();
2988 via[SR] = x; eieio();
2989 polled_handshake(via);
2993 polled_recv_byte(volatile unsigned char __iomem *via)
2997 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2998 x = via[SR]; eieio();
2999 polled_handshake(via);
3000 x = via[SR]; eieio();
3005 pmu_polled_request(struct adb_request *req)
3007 unsigned long flags;
3009 volatile unsigned char __iomem *v = via;
3013 l = pmu_data_len[c][0];
3014 if (l >= 0 && req->nbytes != l + 1)
3017 local_irq_save(flags);
3018 while (pmu_state != idle)
3021 while ((via[B] & TACK) == 0)
3023 polled_send_byte(v, c);
3025 l = req->nbytes - 1;
3026 polled_send_byte(v, l);
3028 for (i = 1; i <= l; ++i)
3029 polled_send_byte(v, req->data[i]);
3031 l = pmu_data_len[c][1];
3033 l = polled_recv_byte(v);
3034 for (i = 0; i < l; ++i)
3035 req->reply[i + req->reply_len] = polled_recv_byte(v);
3040 local_irq_restore(flags);
3043 #endif /* DEBUG_SLEEP */
3046 /* FIXME: This is a temporary set of callbacks to enable us
3047 * to do suspend-to-disk.
3052 static int pmu_sys_suspended = 0;
3054 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
3056 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
3059 /* Suspend PMU event interrupts */
3062 pmu_sys_suspended = 1;
3066 static int pmu_sys_resume(struct sys_device *sysdev)
3068 struct adb_request req;
3070 if (!pmu_sys_suspended)
3073 /* Tell PMU we are ready */
3074 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
3075 pmu_wait_complete(&req);
3077 /* Resume PMU event interrupts */
3080 pmu_sys_suspended = 0;
3085 #endif /* CONFIG_PM */
3087 static struct sysdev_class pmu_sysclass = {
3088 set_kset_name("pmu"),
3091 static struct sys_device device_pmu = {
3093 .cls = &pmu_sysclass,
3096 static struct sysdev_driver driver_pmu = {
3098 .suspend = &pmu_sys_suspend,
3099 .resume = &pmu_sys_resume,
3100 #endif /* CONFIG_PM */
3103 static int __init init_pmu_sysfs(void)
3107 rc = sysdev_class_register(&pmu_sysclass);
3109 printk(KERN_ERR "Failed registering PMU sys class\n");
3112 rc = sysdev_register(&device_pmu);
3114 printk(KERN_ERR "Failed registering PMU sys device\n");
3117 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
3119 printk(KERN_ERR "Failed registering PMU sys driver\n");
3125 subsys_initcall(init_pmu_sysfs);
3127 EXPORT_SYMBOL(pmu_request);
3128 EXPORT_SYMBOL(pmu_poll);
3129 EXPORT_SYMBOL(pmu_poll_adb);
3130 EXPORT_SYMBOL(pmu_wait_complete);
3131 EXPORT_SYMBOL(pmu_suspend);
3132 EXPORT_SYMBOL(pmu_resume);
3133 EXPORT_SYMBOL(pmu_unlock);
3134 EXPORT_SYMBOL(pmu_i2c_combined_read);
3135 EXPORT_SYMBOL(pmu_i2c_stdsub_write);
3136 EXPORT_SYMBOL(pmu_i2c_simple_read);
3137 EXPORT_SYMBOL(pmu_i2c_simple_write);
3139 EXPORT_SYMBOL(pmu_register_sleep_notifier);
3140 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
3141 EXPORT_SYMBOL(pmu_enable_irled);
3142 EXPORT_SYMBOL(pmu_battery_count);
3143 EXPORT_SYMBOL(pmu_batteries);
3144 EXPORT_SYMBOL(pmu_power_flags);
3145 #endif /* CONFIG_PM */