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
16 #include <linux/config.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/sched.h>
22 #include <linux/miscdevice.h>
23 #include <linux/blkdev.h>
24 #include <linux/pci.h>
25 #include <linux/slab.h>
26 #include <linux/poll.h>
27 #include <linux/adb.h>
28 #include <linux/pmu.h>
29 #include <linux/cuda.h>
30 #include <linux/smp_lock.h>
31 #include <linux/module.h>
32 #include <linux/spinlock.h>
34 #include <linux/proc_fs.h>
35 #include <linux/init.h>
37 #include <asm/machdep.h>
39 #include <asm/pgtable.h>
40 #include <asm/system.h>
41 #include <asm/sections.h>
43 #include <asm/hardirq.h>
44 #include <asm/pmac_feature.h>
45 #include <asm/uaccess.h>
46 #include <asm/mmu_context.h>
47 #include <asm/sections.h>
48 #include <asm/cputable.h>
50 #ifdef CONFIG_PMAC_BACKLIGHT
51 #include <asm/backlight.h>
54 /* Some compile options */
55 #undef SUSPEND_USES_PMU
58 #undef HACKED_PCI_SAVE
59 #define NEW_OHARE_CODE
61 /* Misc minor number allocated for /dev/pmu */
64 /* How many iterations between battery polls */
65 #define BATTERY_POLLING_COUNT 2
67 static volatile unsigned char *via;
69 /* VIA registers - spaced 0x200 bytes apart */
70 #define RS 0x200 /* skip between registers */
71 #define B 0 /* B-side data */
72 #define A RS /* A-side data */
73 #define DIRB (2*RS) /* B-side direction (1=output) */
74 #define DIRA (3*RS) /* A-side direction (1=output) */
75 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
76 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
77 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
78 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
79 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
80 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
81 #define SR (10*RS) /* Shift register */
82 #define ACR (11*RS) /* Auxiliary control register */
83 #define PCR (12*RS) /* Peripheral control register */
84 #define IFR (13*RS) /* Interrupt flag register */
85 #define IER (14*RS) /* Interrupt enable register */
86 #define ANH (15*RS) /* A-side data, no handshake */
88 /* Bits in B data register: both active low */
89 #define TACK 0x08 /* Transfer acknowledge (input) */
90 #define TREQ 0x10 /* Transfer request (output) */
93 #define SR_CTRL 0x1c /* Shift register control bits */
94 #define SR_EXT 0x0c /* Shift on external clock */
95 #define SR_OUT 0x10 /* Shift out if 1 */
97 /* Bits in IFR and IER */
98 #define IER_SET 0x80 /* set bits in IER */
99 #define IER_CLR 0 /* clear bits in IER */
100 #define SR_INT 0x04 /* Shift register full/empty */
102 #define CB1_INT 0x10 /* transition on CB1 input */
104 static volatile enum pmu_state {
112 static volatile enum int_data_state {
117 } int_data_state[2] = { int_data_empty, int_data_empty };
119 static struct adb_request *current_req;
120 static struct adb_request *last_req;
121 static struct adb_request *req_awaiting_reply;
122 static unsigned char interrupt_data[2][32];
123 static int interrupt_data_len[2];
124 static int int_data_last;
125 static unsigned char *reply_ptr;
126 static int data_index;
128 static volatile int adb_int_pending;
129 static volatile int disable_poll;
130 static struct adb_request bright_req_1, bright_req_2, bright_req_3;
131 static struct device_node *vias;
132 static int pmu_kind = PMU_UNKNOWN;
133 static int pmu_fully_inited = 0;
134 static int pmu_has_adb;
135 static unsigned char *gpio_reg = NULL;
136 static int gpio_irq = -1;
137 static volatile int pmu_suspended = 0;
138 static spinlock_t pmu_lock;
139 static u8 pmu_intr_mask;
140 static int pmu_version;
141 static int drop_interrupts;
142 #ifdef CONFIG_PMAC_PBOOK
143 static int option_lid_wakeup = 1;
144 static int sleep_in_progress;
145 static int can_sleep;
146 #endif /* CONFIG_PMAC_PBOOK */
148 static struct proc_dir_entry *proc_pmu_root;
149 static struct proc_dir_entry *proc_pmu_info;
150 static struct proc_dir_entry *proc_pmu_options;
152 #ifdef CONFIG_PMAC_PBOOK
153 int pmu_battery_count;
155 unsigned int pmu_power_flags;
156 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
157 static int query_batt_timer = BATTERY_POLLING_COUNT;
158 static struct adb_request batt_req;
159 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
160 #endif /* CONFIG_PMAC_PBOOK */
162 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
163 extern int disable_kernel_backlight;
164 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
168 struct notifier_block *sleep_notifier_list;
171 static int adb_dev_map = 0;
172 static int pmu_adb_flags;
174 static int pmu_probe(void);
175 static int pmu_init(void);
176 static int pmu_send_request(struct adb_request *req, int sync);
177 static int pmu_adb_autopoll(int devs);
178 static int pmu_adb_reset_bus(void);
179 #endif /* CONFIG_ADB */
181 static int init_pmu(void);
182 static int pmu_queue_request(struct adb_request *req);
183 static void pmu_start(void);
184 static void via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
185 static void gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
186 static int proc_get_info(char *page, char **start, off_t off,
187 int count, int *eof, void *data);
188 #ifdef CONFIG_PMAC_BACKLIGHT
189 static int pmu_set_backlight_level(int level, void* data);
190 static int pmu_set_backlight_enable(int on, int level, void* data);
191 #endif /* CONFIG_PMAC_BACKLIGHT */
192 #ifdef CONFIG_PMAC_PBOOK
193 static void pmu_pass_intr(unsigned char *data, int len);
194 static int proc_get_batt(char *page, char **start, off_t off,
195 int count, int *eof, void *data);
196 #endif /* CONFIG_PMAC_PBOOK */
197 static int proc_read_options(char *page, char **start, off_t off,
198 int count, int *eof, void *data);
199 static int proc_write_options(struct file *file, const char *buffer,
200 unsigned long count, void *data);
203 struct adb_driver via_pmu_driver = {
212 #endif /* CONFIG_ADB */
214 extern void low_sleep_handler(void);
215 extern void pmac_sleep_save_intrs(int);
216 extern void pmac_sleep_restore_intrs(void);
217 extern void openpic_sleep_save_intrs(void);
218 extern void openpic_sleep_restore_intrs(void);
219 extern void enable_kernel_altivec(void);
220 extern void enable_kernel_fp(void);
222 #if defined(DEBUG_SLEEP) || defined(DEBUG_FREQ)
223 int pmu_polled_request(struct adb_request *req);
224 void pmu_blink(int n);
227 #if defined(CONFIG_PMAC_PBOOK) && defined(CONFIG_PM)
228 static int generic_notify_sleep(struct pmu_sleep_notifier *self, int when);
229 static struct pmu_sleep_notifier generic_sleep_notifier = {
230 generic_notify_sleep,
233 #endif /* defined(CONFIG_PMAC_PBOOK) && defined(CONFIG_PM) */
236 * This table indicates for each PMU opcode:
237 * - the number of data bytes to be sent with the command, or -1
238 * if a length byte should be sent,
239 * - the number of response bytes which the PMU will return, or
240 * -1 if it will send a length byte.
242 static const s8 pmu_data_len[256][2] __openfirmwaredata = {
243 /* 0 1 2 3 4 5 6 7 */
244 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
245 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
246 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
247 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
248 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
249 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
250 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
251 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
252 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
253 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
254 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
255 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
256 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
257 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
258 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
259 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
260 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
261 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
262 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
263 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
264 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
265 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
266 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
267 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
268 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
269 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
270 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
271 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
272 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
273 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
274 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
275 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
278 static char *pbook_type[] = {
280 "PowerBook 2400/3400/3500(G3)",
281 "PowerBook G3 Series",
286 #ifdef CONFIG_PMAC_BACKLIGHT
287 static struct backlight_controller pmu_backlight_controller = {
288 pmu_set_backlight_enable,
289 pmu_set_backlight_level
291 #endif /* CONFIG_PMAC_BACKLIGHT */
298 vias = find_devices("via-pmu");
302 printk(KERN_WARNING "Warning: only using 1st via-pmu\n");
304 if (vias->n_addrs < 1 || vias->n_intrs < 1) {
305 printk(KERN_ERR "via-pmu: %d addresses, %d interrupts!\n",
306 vias->n_addrs, vias->n_intrs);
307 if (vias->n_addrs < 1 || vias->n_intrs < 1)
311 spin_lock_init(&pmu_lock);
315 pmu_intr_mask = PMU_INT_PCEJECT |
320 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
321 || device_is_compatible(vias->parent, "ohare")))
322 pmu_kind = PMU_OHARE_BASED;
323 else if (device_is_compatible(vias->parent, "paddington"))
324 pmu_kind = PMU_PADDINGTON_BASED;
325 else if (device_is_compatible(vias->parent, "heathrow"))
326 pmu_kind = PMU_HEATHROW_BASED;
327 else if (device_is_compatible(vias->parent, "Keylargo")) {
328 struct device_node *gpio, *gpiop;
330 pmu_kind = PMU_KEYLARGO_BASED;
331 pmu_has_adb = (find_type_devices("adb") != NULL);
332 pmu_intr_mask = PMU_INT_PCEJECT |
338 gpiop = find_devices("gpio");
339 if (gpiop && gpiop->n_addrs) {
340 gpio_reg = ioremap(gpiop->addrs->address, 0x10);
341 gpio = find_devices("extint-gpio1");
342 if (gpio && gpio->parent == gpiop && gpio->n_intrs)
343 gpio_irq = gpio->intrs[0].line;
346 pmu_kind = PMU_UNKNOWN;
348 via = (volatile unsigned char *) ioremap(vias->addrs->address, 0x2000);
350 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
351 out_8(&via[IFR], 0x7f); /* clear IFR */
360 printk(KERN_INFO "PMU driver %d initialized for %s, firmware: %02x\n",
361 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
363 sys_ctrler = SYS_CTRLER_PMU;
365 #if defined(CONFIG_PMAC_PBOOK) && defined(CONFIG_PM)
366 pmu_register_sleep_notifier(&generic_sleep_notifier);
374 static int __openfirmware
377 return vias == NULL? -ENODEV: 0;
380 static int __openfirmware
387 #endif /* CONFIG_ADB */
390 * We can't wait until pmu_init gets called, that happens too late.
391 * It happens after IDE and SCSI initialization, which can take a few
392 * seconds, and by that time the PMU could have given up on us and
394 * This is called from arch/ppc/kernel/pmac_setup.c:pmac_init2().
396 int via_pmu_start(void)
401 request_OF_resource(vias, 0, NULL);
403 bright_req_1.complete = 1;
404 bright_req_2.complete = 1;
405 bright_req_3.complete = 1;
406 #ifdef CONFIG_PMAC_PBOOK
407 batt_req.complete = 1;
408 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
412 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
414 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
415 vias->intrs[0].line);
419 if (pmu_kind == PMU_KEYLARGO_BASED && gpio_irq != -1) {
420 if (request_irq(gpio_irq, gpio1_interrupt, 0, "GPIO1/ADB", (void *)0))
421 printk(KERN_ERR "pmu: can't get irq %d (GPIO1)\n", gpio_irq);
424 /* Enable interrupts */
425 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
427 pmu_fully_inited = 1;
429 #ifdef CONFIG_PMAC_BACKLIGHT
430 /* Enable backlight */
431 register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
432 #endif /* CONFIG_PMAC_BACKLIGHT */
434 #ifdef CONFIG_PMAC_PBOOK
435 if (machine_is_compatible("AAPL,3400/2400") ||
436 machine_is_compatible("AAPL,3500")) {
437 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
438 NULL, PMAC_MB_INFO_MODEL, 0);
439 pmu_battery_count = 1;
440 if (mb == PMAC_TYPE_COMET)
441 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
443 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
444 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
445 machine_is_compatible("PowerBook1,1")) {
446 pmu_battery_count = 2;
447 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
448 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
450 struct device_node* prim = find_devices("power-mgt");
451 u32 *prim_info = NULL;
453 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
455 /* Other stuffs here yet unknown */
456 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
457 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
458 if (pmu_battery_count > 1)
459 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
462 #endif /* CONFIG_PMAC_PBOOK */
463 /* Create /proc/pmu */
464 proc_pmu_root = proc_mkdir("pmu", 0);
467 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
468 proc_get_info, NULL);
469 #ifdef CONFIG_PMAC_PBOOK
470 for (i=0; i<pmu_battery_count; i++) {
472 sprintf(title, "battery_%d", i);
473 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
474 proc_get_batt, (void *)i);
476 #endif /* CONFIG_PMAC_PBOOK */
477 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
478 if (proc_pmu_options) {
479 proc_pmu_options->nlink = 1;
480 proc_pmu_options->read_proc = proc_read_options;
481 proc_pmu_options->write_proc = proc_write_options;
485 /* Make sure PMU settle down before continuing. This is _very_ important
486 * since the IDE probe may shut interrupts down for quite a bit of time. If
487 * a PMU communication is pending while this happens, the PMU may timeout
488 * Not that on Core99 machines, the PMU keeps sending us environement
489 * messages, we should find a way to either fix IDE or make it call
490 * pmu_suspend() before masking interrupts. This can also happens while
491 * scolling with some fbdevs.
495 } while (pmu_state != idle);
500 static int __openfirmware
504 struct adb_request req;
506 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
507 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
509 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
511 while (!req.complete) {
513 printk(KERN_ERR "init_pmu: no response from PMU\n");
520 /* ack all pending interrupts */
522 interrupt_data[0][0] = 1;
523 while (interrupt_data[0][0] || pmu_state != idle) {
525 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
528 if (pmu_state == idle)
530 via_pmu_interrupt(0, 0, 0);
534 /* Tell PMU we are ready. */
535 if (pmu_kind == PMU_KEYLARGO_BASED) {
536 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
537 while (!req.complete)
541 /* Read PMU version */
542 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
543 while (!req.complete)
545 if (req.reply_len > 0)
546 pmu_version = req.reply[0];
557 static inline void wakeup_decrementer(void)
559 set_dec(tb_ticks_per_jiffy);
560 /* No currently-supported powerbook has a 601,
561 * so use get_tbl, not native
563 last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
567 #ifdef CONFIG_PMAC_PBOOK
569 /* This new version of the code for 2400/3400/3500 powerbooks
570 * is inspired from the implementation in gkrellm-pmu
573 done_battery_state_ohare(struct adb_request* req)
577 * 0x01 : AC indicator
579 * 0x04 : battery exist
582 * 0x20 : full charged
583 * 0x40 : pcharge reset
584 * 0x80 : battery exist
586 * [1][2] : battery voltage
587 * [3] : CPU temperature
588 * [4] : battery temperature
593 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
594 long pcharge, charge, vb, vmax, lmax;
595 long vmax_charging, vmax_charged;
596 long current, voltage, time, max;
597 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
598 NULL, PMAC_MB_INFO_MODEL, 0);
600 if (req->reply[0] & 0x01)
601 pmu_power_flags |= PMU_PWR_AC_PRESENT;
603 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
605 if (mb == PMAC_TYPE_COMET) {
616 /* If battery installed */
617 if (req->reply[0] & 0x04) {
618 bat_flags |= PMU_BATT_PRESENT;
619 if (req->reply[0] & 0x02)
620 bat_flags |= PMU_BATT_CHARGING;
621 vb = (req->reply[1] << 8) | req->reply[2];
622 voltage = (vb * 265 + 72665) / 10;
623 current = req->reply[5];
624 if ((req->reply[0] & 0x01) == 0) {
626 vb += ((current - 200) * 15)/100;
627 } else if (req->reply[0] & 0x02) {
628 vb = (vb * 97) / 100;
629 vmax = vmax_charging;
631 charge = (100 * vb) / vmax;
632 if (req->reply[0] & 0x40) {
633 pcharge = (req->reply[6] << 8) + req->reply[7];
637 pcharge = 100 - pcharge / lmax;
638 if (pcharge < charge)
642 time = (charge * 16440) / current;
648 charge = max = current = voltage = time = 0;
650 pmu_batteries[pmu_cur_battery].flags = bat_flags;
651 pmu_batteries[pmu_cur_battery].charge = charge;
652 pmu_batteries[pmu_cur_battery].max_charge = max;
653 pmu_batteries[pmu_cur_battery].current = current;
654 pmu_batteries[pmu_cur_battery].voltage = voltage;
655 pmu_batteries[pmu_cur_battery].time_remaining = time;
659 done_battery_state_smart(struct adb_request* req)
662 * [0] : format of this structure (known: 3,4,5)
675 * [4][5] : max charge
680 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
682 unsigned int capa, max, voltage;
684 if (req->reply[1] & 0x01)
685 pmu_power_flags |= PMU_PWR_AC_PRESENT;
687 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
690 if (req->reply[1] & 0x04) {
691 bat_flags |= PMU_BATT_PRESENT;
692 switch(req->reply[0]) {
694 case 4: capa = req->reply[2];
696 current = *((signed char *)&req->reply[4]);
697 voltage = req->reply[5];
699 case 5: capa = (req->reply[2] << 8) | req->reply[3];
700 max = (req->reply[4] << 8) | req->reply[5];
701 current = *((signed short *)&req->reply[6]);
702 voltage = (req->reply[8] << 8) | req->reply[9];
705 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
706 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
710 capa = max = current = voltage = 0;
712 if ((req->reply[1] & 0x01) && (current > 0))
713 bat_flags |= PMU_BATT_CHARGING;
715 pmu_batteries[pmu_cur_battery].flags = bat_flags;
716 pmu_batteries[pmu_cur_battery].charge = capa;
717 pmu_batteries[pmu_cur_battery].max_charge = max;
718 pmu_batteries[pmu_cur_battery].current = current;
719 pmu_batteries[pmu_cur_battery].voltage = voltage;
721 if ((req->reply[1] & 0x01) && (current > 0))
722 pmu_batteries[pmu_cur_battery].time_remaining
723 = ((max-capa) * 3600) / current;
725 pmu_batteries[pmu_cur_battery].time_remaining
726 = (capa * 3600) / (-current);
728 pmu_batteries[pmu_cur_battery].time_remaining = 0;
730 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
734 query_battery_state(void)
736 if (!batt_req.complete)
738 if (pmu_kind == PMU_OHARE_BASED)
739 pmu_request(&batt_req, done_battery_state_ohare,
740 1, PMU_BATTERY_STATE);
742 pmu_request(&batt_req, done_battery_state_smart,
743 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
746 #endif /* CONFIG_PMAC_PBOOK */
749 proc_get_info(char *page, char **start, off_t off,
750 int count, int *eof, void *data)
754 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
755 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
756 #ifdef CONFIG_PMAC_PBOOK
757 p += sprintf(p, "AC Power : %d\n",
758 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0));
759 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
760 #endif /* CONFIG_PMAC_PBOOK */
765 #ifdef CONFIG_PMAC_PBOOK
767 proc_get_batt(char *page, char **start, off_t off,
768 int count, int *eof, void *data)
770 int batnum = (int)data;
773 p += sprintf(p, "\n");
774 p += sprintf(p, "flags : %08x\n",
775 pmu_batteries[batnum].flags);
776 p += sprintf(p, "charge : %d\n",
777 pmu_batteries[batnum].charge);
778 p += sprintf(p, "max_charge : %d\n",
779 pmu_batteries[batnum].max_charge);
780 p += sprintf(p, "current : %d\n",
781 pmu_batteries[batnum].current);
782 p += sprintf(p, "voltage : %d\n",
783 pmu_batteries[batnum].voltage);
784 p += sprintf(p, "time rem. : %d\n",
785 pmu_batteries[batnum].time_remaining);
789 #endif /* CONFIG_PMAC_PBOOK */
792 proc_read_options(char *page, char **start, off_t off,
793 int count, int *eof, void *data)
797 #ifdef CONFIG_PMAC_PBOOK
798 if (pmu_kind == PMU_KEYLARGO_BASED && can_sleep)
799 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
800 #endif /* CONFIG_PMAC_PBOOK */
806 proc_write_options(struct file *file, const char *buffer,
807 unsigned long count, void *data)
811 unsigned long fcount = count;
817 if (copy_from_user(tmp, buffer, count))
825 while(*val && (*val != '=')) {
835 #ifdef CONFIG_PMAC_PBOOK
836 if (pmu_kind == PMU_KEYLARGO_BASED && can_sleep) {
837 if (!strcmp(label, "lid_wakeup"))
838 option_lid_wakeup = ((*val) == '1');
840 #endif /* CONFIG_PMAC_PBOOK */
845 /* Send an ADB command */
846 static int __openfirmware
847 pmu_send_request(struct adb_request *req, int sync)
851 if ((vias == NULL) || (!pmu_fully_inited)) {
858 switch (req->data[0]) {
860 for (i = 0; i < req->nbytes - 1; ++i)
861 req->data[i] = req->data[i+1];
863 if (pmu_data_len[req->data[0]][1] != 0) {
864 req->reply[0] = ADB_RET_OK;
868 ret = pmu_queue_request(req);
871 switch (req->data[1]) {
873 if (req->nbytes != 2)
875 req->data[0] = PMU_READ_RTC;
878 req->reply[0] = CUDA_PACKET;
880 req->reply[2] = CUDA_GET_TIME;
881 ret = pmu_queue_request(req);
884 if (req->nbytes != 6)
886 req->data[0] = PMU_SET_RTC;
888 for (i = 1; i <= 4; ++i)
889 req->data[i] = req->data[i+1];
891 req->reply[0] = CUDA_PACKET;
893 req->reply[2] = CUDA_SET_TIME;
894 ret = pmu_queue_request(req);
901 for (i = req->nbytes - 1; i > 1; --i)
902 req->data[i+2] = req->data[i];
903 req->data[3] = req->nbytes - 2;
904 req->data[2] = pmu_adb_flags;
905 /*req->data[1] = req->data[1];*/
906 req->data[0] = PMU_ADB_CMD;
908 req->reply_expected = 1;
910 ret = pmu_queue_request(req);
919 while (!req->complete)
925 /* Enable/disable autopolling */
926 static int __openfirmware
927 pmu_adb_autopoll(int devs)
929 struct adb_request req;
931 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
936 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
937 adb_dev_map >> 8, adb_dev_map);
940 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
943 while (!req.complete)
948 /* Reset the ADB bus */
949 static int __openfirmware
950 pmu_adb_reset_bus(void)
952 struct adb_request req;
953 int save_autopoll = adb_dev_map;
955 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
958 /* anyone got a better idea?? */
963 req.data[0] = PMU_ADB_CMD;
965 req.data[2] = ADB_BUSRESET;
969 req.reply_expected = 1;
970 if (pmu_queue_request(&req) != 0) {
971 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
974 while (!req.complete)
977 if (save_autopoll != 0)
978 pmu_adb_autopoll(save_autopoll);
982 #endif /* CONFIG_ADB */
984 /* Construct and send a pmu request */
986 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
995 if (nbytes < 0 || nbytes > 32) {
996 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1000 req->nbytes = nbytes;
1002 va_start(list, nbytes);
1003 for (i = 0; i < nbytes; ++i)
1004 req->data[i] = va_arg(list, int);
1007 req->reply_expected = 0;
1008 return pmu_queue_request(req);
1012 pmu_queue_request(struct adb_request *req)
1014 unsigned long flags;
1021 if (req->nbytes <= 0) {
1025 nsend = pmu_data_len[req->data[0]][0];
1026 if (nsend >= 0 && req->nbytes != nsend + 1) {
1035 spin_lock_irqsave(&pmu_lock, flags);
1036 if (current_req != 0) {
1037 last_req->next = req;
1042 if (pmu_state == idle)
1045 spin_unlock_irqrestore(&pmu_lock, flags);
1053 /* Sightly increased the delay, I had one occurence of the message
1057 while ((in_8(&via[B]) & TACK) == 0) {
1058 if (--timeout < 0) {
1059 printk(KERN_ERR "PMU not responding (!ack)\n");
1066 /* New PMU seems to be very sensitive to those timings, so we make sure
1067 * PCI is flushed immediately */
1071 volatile unsigned char *v = via;
1073 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1075 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1082 volatile unsigned char *v = via;
1084 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1085 in_8(&v[SR]); /* resets SR */
1086 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1091 pmu_done(struct adb_request *req)
1093 void (*done)(struct adb_request *) = req->done;
1096 /* Here, we assume that if the request has a done member, the
1097 * struct request will survive to setting req->complete to 1
1103 static void __openfirmware
1106 struct adb_request *req;
1108 /* assert pmu_state == idle */
1109 /* get the packet to send */
1111 if (req == 0 || pmu_state != idle
1112 || (/*req->reply_expected && */req_awaiting_reply))
1115 pmu_state = sending;
1117 data_len = pmu_data_len[req->data[0]][0];
1119 /* Sounds safer to make sure ACK is high before writing. This helped
1120 * kill a problem with ADB and some iBooks
1123 /* set the shift register to shift out and send a byte */
1124 send_byte(req->data[0]);
1134 /* Kicks ADB read when PMU is suspended */
1136 adb_int_pending = 1;
1138 via_pmu_interrupt(0, 0, 0);
1139 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1140 || req_awaiting_reply));
1143 /* This function loops until the PMU is idle and prevents it from
1144 * anwsering to ADB interrupts. pmu_request can still be called.
1145 * This is done to avoid spurrious shutdowns when we know we'll have
1146 * interrupts switched off for a long time
1151 unsigned long flags;
1152 #ifdef SUSPEND_USES_PMU
1153 struct adb_request *req;
1158 spin_lock_irqsave(&pmu_lock, flags);
1160 if (pmu_suspended > 1) {
1161 spin_unlock_irqrestore(&pmu_lock, flags);
1166 spin_unlock_irqrestore(&pmu_lock, flags);
1167 via_pmu_interrupt(0, 0, 0);
1168 spin_lock_irqsave(&pmu_lock, flags);
1169 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1170 #ifdef SUSPEND_USES_PMU
1171 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1172 spin_unlock_irqrestore(&pmu_lock, flags);
1173 while(!req.complete)
1175 #else /* SUSPEND_USES_PMU */
1177 disable_irq(gpio_irq);
1178 out_8(&via[IER], CB1_INT | IER_CLR);
1179 spin_unlock_irqrestore(&pmu_lock, flags);
1180 #endif /* SUSPEND_USES_PMU */
1189 unsigned long flags;
1191 if (!via || (pmu_suspended < 1))
1194 spin_lock_irqsave(&pmu_lock, flags);
1196 if (pmu_suspended > 0) {
1197 spin_unlock_irqrestore(&pmu_lock, flags);
1200 adb_int_pending = 1;
1201 #ifdef SUSPEND_USES_PMU
1202 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1203 spin_unlock_irqrestore(&pmu_lock, flags);
1204 while(!req.complete)
1206 #else /* SUSPEND_USES_PMU */
1208 enable_irq(gpio_irq);
1209 out_8(&via[IER], CB1_INT | IER_SET);
1210 spin_unlock_irqrestore(&pmu_lock, flags);
1212 #endif /* SUSPEND_USES_PMU */
1215 /* Interrupt data could be the result data from an ADB cmd */
1216 static void __openfirmware
1217 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1220 if (drop_interrupts || len < 1) {
1221 adb_int_pending = 0;
1224 /* Note: for some reason, we get an interrupt with len=1,
1225 * data[0]==0 after each normal ADB interrupt, at least
1226 * on the Pismo. Still investigating... --BenH
1228 if (data[0] & PMU_INT_ADB) {
1229 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1230 struct adb_request *req = req_awaiting_reply;
1232 printk(KERN_ERR "PMU: extra ADB reply\n");
1235 req_awaiting_reply = 0;
1239 memcpy(req->reply, data + 1, len - 1);
1240 req->reply_len = len - 1;
1245 if (len == 4 && data[1] == 0x2c) {
1246 extern int xmon_wants_key, xmon_adb_keycode;
1247 if (xmon_wants_key) {
1248 xmon_adb_keycode = data[2];
1252 #endif /* CONFIG_XMON */
1255 * XXX On the [23]400 the PMU gives us an up
1256 * event for keycodes 0x74 or 0x75 when the PC
1257 * card eject buttons are released, so we
1258 * ignore those events.
1260 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1261 && data[1] == 0x2c && data[3] == 0xff
1262 && (data[2] & ~1) == 0xf4))
1263 adb_input(data+1, len-1, regs, 1);
1264 #endif /* CONFIG_ADB */
1267 /* Sound/brightness button pressed */
1268 if ((data[0] & PMU_INT_SNDBRT) && len == 3) {
1269 #ifdef CONFIG_PMAC_BACKLIGHT
1270 #ifdef CONFIG_INPUT_ADBHID
1271 if (!disable_kernel_backlight)
1272 #endif /* CONFIG_INPUT_ADBHID */
1273 set_backlight_level(data[1] >> 4);
1274 #endif /* CONFIG_PMAC_BACKLIGHT */
1276 #ifdef CONFIG_PMAC_PBOOK
1277 /* Environement or tick interrupt, query batteries */
1278 if (pmu_battery_count && (data[0] & PMU_INT_TICK)) {
1279 if ((--query_batt_timer) == 0) {
1280 query_battery_state();
1281 query_batt_timer = BATTERY_POLLING_COUNT;
1283 } else if (pmu_battery_count && (data[0] & PMU_INT_ENVIRONMENT))
1284 query_battery_state();
1286 pmu_pass_intr(data, len);
1287 #endif /* CONFIG_PMAC_PBOOK */
1291 static struct adb_request* __openfirmware
1292 pmu_sr_intr(struct pt_regs *regs)
1294 struct adb_request *req;
1297 if (via[B] & TREQ) {
1298 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1299 out_8(&via[IFR], SR_INT);
1302 /* The ack may not yet be low when we get the interrupt */
1303 while ((in_8(&via[B]) & TACK) != 0)
1306 /* if reading grab the byte, and reset the interrupt */
1307 if (pmu_state == reading || pmu_state == reading_intr)
1308 bite = in_8(&via[SR]);
1310 /* reset TREQ and wait for TACK to go high */
1311 out_8(&via[B], in_8(&via[B]) | TREQ);
1314 switch (pmu_state) {
1318 data_len = req->nbytes - 1;
1319 send_byte(data_len);
1322 if (data_index <= data_len) {
1323 send_byte(req->data[data_index++]);
1327 data_len = pmu_data_len[req->data[0]][1];
1328 if (data_len == 0) {
1330 current_req = req->next;
1331 if (req->reply_expected)
1332 req_awaiting_reply = req;
1336 pmu_state = reading;
1338 reply_ptr = req->reply + req->reply_len;
1346 pmu_state = reading_intr;
1347 reply_ptr = interrupt_data[int_data_last];
1353 if (data_len == -1) {
1356 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1357 } else if (data_index < 32) {
1358 reply_ptr[data_index++] = bite;
1360 if (data_index < data_len) {
1365 if (pmu_state == reading_intr) {
1367 int_data_state[int_data_last] = int_data_ready;
1368 interrupt_data_len[int_data_last] = data_len;
1371 current_req = req->next;
1372 req->reply_len += data_index;
1379 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1385 static void __openfirmware
1386 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1388 unsigned long flags;
1392 struct adb_request *req = NULL;
1394 /* This is a bit brutal, we can probably do better */
1395 spin_lock_irqsave(&pmu_lock, flags);
1399 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1402 if (++nloop > 1000) {
1403 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1404 "intr=%x, ier=%x pmu_state=%d\n",
1405 intr, in_8(&via[IER]), pmu_state);
1408 out_8(&via[IFR], intr);
1410 adb_int_pending = 1;
1411 if (intr & SR_INT) {
1412 req = pmu_sr_intr(regs);
1419 if (pmu_state == idle) {
1420 if (adb_int_pending) {
1421 if (int_data_state[0] == int_data_empty)
1423 else if (int_data_state[1] == int_data_empty)
1428 int_data_state[int_data_last] = int_data_fill;
1429 /* Sounds safer to make sure ACK is high before writing.
1430 * This helped kill a problem with ADB and some iBooks
1433 send_byte(PMU_INT_ACK);
1434 adb_int_pending = 0;
1437 } else if (current_req)
1440 /* Mark the oldest buffer for flushing */
1441 if (int_data_state[!int_data_last] == int_data_ready) {
1442 int_data_state[!int_data_last] = int_data_flush;
1443 int_data = !int_data_last;
1444 } else if (int_data_state[int_data_last] == int_data_ready) {
1445 int_data_state[int_data_last] = int_data_flush;
1446 int_data = int_data_last;
1449 spin_unlock_irqrestore(&pmu_lock, flags);
1451 /* Deal with completed PMU requests outside of the lock */
1457 /* Deal with interrupt datas outside of the lock */
1458 if (int_data >= 0) {
1459 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1460 spin_lock_irqsave(&pmu_lock, flags);
1462 int_data_state[int_data] = int_data_empty;
1468 static void __openfirmware
1469 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1471 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1472 adb_int_pending = 1;
1473 via_pmu_interrupt(0, 0, 0);
1477 #ifdef CONFIG_PMAC_BACKLIGHT
1478 static int backlight_to_bright[] = {
1479 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1480 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1483 static int __openfirmware
1484 pmu_set_backlight_enable(int on, int level, void* data)
1486 struct adb_request req;
1492 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1493 backlight_to_bright[level]);
1494 while (!req.complete)
1497 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1498 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1499 while (!req.complete)
1505 static int __openfirmware
1506 pmu_set_backlight_level(int level, void* data)
1511 if (!bright_req_1.complete)
1513 pmu_request(&bright_req_1, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1514 backlight_to_bright[level]);
1515 if (!bright_req_2.complete)
1517 pmu_request(&bright_req_2, NULL, 2, PMU_POWER_CTRL, PMU_POW_BACKLIGHT
1518 | (level > BACKLIGHT_OFF ? PMU_POW_ON : PMU_POW_OFF));
1522 #endif /* CONFIG_PMAC_BACKLIGHT */
1525 pmu_enable_irled(int on)
1527 struct adb_request req;
1531 if (pmu_kind == PMU_KEYLARGO_BASED)
1534 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1535 (on ? PMU_POW_ON : PMU_POW_OFF));
1536 while (!req.complete)
1543 struct adb_request req;
1545 local_irq_disable();
1547 drop_interrupts = 1;
1549 if (pmu_kind != PMU_KEYLARGO_BASED) {
1550 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1552 while(!req.complete)
1556 pmu_request(&req, NULL, 1, PMU_RESET);
1557 while(!req.complete || (pmu_state != idle))
1566 struct adb_request req;
1568 local_irq_disable();
1570 drop_interrupts = 1;
1572 if (pmu_kind != PMU_KEYLARGO_BASED) {
1573 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1575 while(!req.complete)
1579 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1580 'M', 'A', 'T', 'T');
1581 while(!req.complete || (pmu_state != idle))
1593 struct pmu_i2c_hdr {
1604 pmu_i2c_combined_read(int bus, int addr, int subaddr, u8* data, int len)
1606 struct adb_request req;
1607 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1611 for (retry=0; retry<16; retry++) {
1612 memset(&req, 0, sizeof(req));
1615 hdr->address = addr & 0xfe;
1616 hdr->mode = PMU_I2C_MODE_COMBINED;
1618 hdr->sub_addr = subaddr;
1619 hdr->comb_addr = addr | 1;
1622 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1623 req.reply_expected = 0;
1625 req.data[0] = PMU_I2C_CMD;
1626 req.reply[0] = 0xff;
1627 rc = pmu_queue_request(&req);
1630 while(!req.complete)
1632 if (req.reply[0] == PMU_I2C_STATUS_OK)
1636 if (req.reply[0] != PMU_I2C_STATUS_OK)
1639 for (retry=0; retry<16; retry++) {
1640 memset(&req, 0, sizeof(req));
1644 hdr->bus = PMU_I2C_BUS_STATUS;
1645 req.reply[0] = 0xff;
1648 req.reply_expected = 0;
1650 req.data[0] = PMU_I2C_CMD;
1651 rc = pmu_queue_request(&req);
1654 while(!req.complete)
1656 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1657 memcpy(data, &req.reply[1], req.reply_len - 1);
1658 return req.reply_len - 1;
1665 pmu_i2c_stdsub_write(int bus, int addr, int subaddr, u8* data, int len)
1667 struct adb_request req;
1668 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1672 for (retry=0; retry<16; retry++) {
1673 memset(&req, 0, sizeof(req));
1676 hdr->address = addr & 0xfe;
1677 hdr->mode = PMU_I2C_MODE_STDSUB;
1679 hdr->sub_addr = subaddr;
1680 hdr->comb_addr = addr & 0xfe;
1683 req.data[0] = PMU_I2C_CMD;
1684 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
1685 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
1686 req.reply_expected = 0;
1688 req.reply[0] = 0xff;
1689 rc = pmu_queue_request(&req);
1692 while(!req.complete)
1694 if (req.reply[0] == PMU_I2C_STATUS_OK)
1698 if (req.reply[0] != PMU_I2C_STATUS_OK)
1701 for (retry=0; retry<16; retry++) {
1702 memset(&req, 0, sizeof(req));
1706 hdr->bus = PMU_I2C_BUS_STATUS;
1707 req.reply[0] = 0xff;
1710 req.reply_expected = 0;
1712 req.data[0] = PMU_I2C_CMD;
1713 rc = pmu_queue_request(&req);
1716 while(!req.complete)
1718 if (req.reply[0] == PMU_I2C_STATUS_OK)
1725 pmu_i2c_simple_read(int bus, int addr, u8* data, int len)
1727 struct adb_request req;
1728 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1732 for (retry=0; retry<16; retry++) {
1733 memset(&req, 0, sizeof(req));
1736 hdr->address = addr | 1;
1737 hdr->mode = PMU_I2C_MODE_SIMPLE;
1743 req.data[0] = PMU_I2C_CMD;
1744 req.nbytes = sizeof(struct pmu_i2c_hdr) + 1;
1745 req.reply_expected = 0;
1747 req.reply[0] = 0xff;
1748 rc = pmu_queue_request(&req);
1751 while(!req.complete)
1753 if (req.reply[0] == PMU_I2C_STATUS_OK)
1757 if (req.reply[0] != PMU_I2C_STATUS_OK)
1760 for (retry=0; retry<16; retry++) {
1761 memset(&req, 0, sizeof(req));
1765 hdr->bus = PMU_I2C_BUS_STATUS;
1766 req.reply[0] = 0xff;
1769 req.reply_expected = 0;
1771 req.data[0] = PMU_I2C_CMD;
1772 rc = pmu_queue_request(&req);
1775 while(!req.complete)
1777 if (req.reply[0] == PMU_I2C_STATUS_DATAREAD) {
1778 memcpy(data, &req.reply[1], req.reply_len - 1);
1779 return req.reply_len - 1;
1786 pmu_i2c_simple_write(int bus, int addr, u8* data, int len)
1788 struct adb_request req;
1789 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req.data[1];
1793 for (retry=0; retry<16; retry++) {
1794 memset(&req, 0, sizeof(req));
1797 hdr->address = addr & 0xfe;
1798 hdr->mode = PMU_I2C_MODE_SIMPLE;
1804 req.data[0] = PMU_I2C_CMD;
1805 memcpy(&req.data[sizeof(struct pmu_i2c_hdr) + 1], data, len);
1806 req.nbytes = sizeof(struct pmu_i2c_hdr) + len + 1;
1807 req.reply_expected = 0;
1809 req.reply[0] = 0xff;
1810 rc = pmu_queue_request(&req);
1813 while(!req.complete)
1815 if (req.reply[0] == PMU_I2C_STATUS_OK)
1819 if (req.reply[0] != PMU_I2C_STATUS_OK)
1822 for (retry=0; retry<16; retry++) {
1823 memset(&req, 0, sizeof(req));
1827 hdr->bus = PMU_I2C_BUS_STATUS;
1828 req.reply[0] = 0xff;
1831 req.reply_expected = 0;
1833 req.data[0] = PMU_I2C_CMD;
1834 rc = pmu_queue_request(&req);
1837 while(!req.complete)
1839 if (req.reply[0] == PMU_I2C_STATUS_OK)
1846 #if defined(DEBUG_SLEEP) || defined(DEBUG_FREQ)
1847 /* N.B. This doesn't work on the 3400 */
1851 struct adb_request req;
1853 memset(&req, 0, sizeof(req));
1855 for (; n > 0; --n) {
1862 req.reply[0] = ADB_RET_OK;
1864 req.reply_expected = 0;
1865 pmu_polled_request(&req);
1873 req.reply[0] = ADB_RET_OK;
1875 req.reply_expected = 0;
1876 pmu_polled_request(&req);
1881 #endif /* defined(DEBUG_SLEEP) || defined(DEBUG_FREQ) */
1883 #ifdef CONFIG_PMAC_PBOOK
1885 static LIST_HEAD(sleep_notifiers);
1889 generic_notify_sleep(struct pmu_sleep_notifier *self, int when)
1892 case PBOOK_SLEEP_NOW:
1893 if (pm_send_all(PM_SUSPEND, (void *)3))
1894 return PBOOK_SLEEP_REJECT;
1897 (void) pm_send_all(PM_RESUME, (void *)0);
1899 return PBOOK_SLEEP_OK;
1901 #endif /* CONFIG_PM */
1904 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1906 struct list_head *list;
1907 struct pmu_sleep_notifier *notifier;
1909 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1910 list = list->next) {
1911 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1912 if (n->priority > notifier->priority)
1915 __list_add(&n->list, list->prev, list);
1920 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1922 if (n->list.next == 0)
1929 /* Sleep is broadcast last-to-first */
1931 broadcast_sleep(int when, int fallback)
1933 int ret = PBOOK_SLEEP_OK;
1934 struct list_head *list;
1935 struct pmu_sleep_notifier *notifier;
1937 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1938 list = list->prev) {
1939 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1940 ret = notifier->notifier_call(notifier, when);
1941 if (ret != PBOOK_SLEEP_OK) {
1942 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
1943 when, notifier, notifier->notifier_call);
1944 for (; list != &sleep_notifiers; list = list->next) {
1945 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1946 notifier->notifier_call(notifier, fallback);
1954 /* Wake is broadcast first-to-last */
1956 broadcast_wake(void)
1958 int ret = PBOOK_SLEEP_OK;
1959 struct list_head *list;
1960 struct pmu_sleep_notifier *notifier;
1962 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1963 list = list->next) {
1964 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1965 #ifdef VERBOSE_WAKEUP
1966 if (notifier->priority < SLEEP_LEVEL_VIDEO)
1967 xmon_printf("wake, before notifier %x\n", notifier);
1969 notifier->notifier_call(notifier, PBOOK_WAKE);
1970 #ifdef VERBOSE_WAKEUP
1971 if (notifier->priority <= SLEEP_LEVEL_VIDEO)
1972 xmon_printf("wake, after notifier %x\n", notifier);
1979 * This struct is used to store config register values for
1980 * PCI devices which may get powered off when we sleep.
1982 static struct pci_save {
1983 #ifndef HACKED_PCI_SAVE
1992 static int pbook_npci_saves;
1994 static void __openfirmware
1995 pbook_alloc_pci_save(void)
2001 pci_for_each_dev(pd) {
2006 pbook_pci_saves = (struct pci_save *)
2007 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
2008 pbook_npci_saves = npci;
2011 static void __openfirmware
2012 pbook_free_pci_save(void)
2014 if (pbook_pci_saves == NULL)
2016 kfree(pbook_pci_saves);
2017 pbook_pci_saves = NULL;
2018 pbook_npci_saves = 0;
2021 static void __openfirmware
2022 pbook_pci_save(void)
2024 struct pci_save *ps = pbook_pci_saves;
2026 int npci = pbook_npci_saves;
2031 pci_for_each_dev(pd) {
2034 #ifndef HACKED_PCI_SAVE
2035 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
2036 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
2037 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
2038 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
2042 pci_read_config_dword(pd, i<<4, &ps->config[i]);
2048 /* For this to work, we must take care of a few things: If gmac was enabled
2049 * during boot, it will be in the pci dev list. If it's disabled at this point
2050 * (and it will probably be), then you can't access it's config space.
2052 static void __openfirmware
2053 pbook_pci_restore(void)
2056 struct pci_save *ps = pbook_pci_saves - 1;
2058 int npci = pbook_npci_saves;
2061 pci_for_each_dev(pd) {
2062 #ifdef HACKED_PCI_SAVE
2068 pci_write_config_dword(pd, i<<4, ps->config[i]);
2069 pci_write_config_dword(pd, 4, ps->config[1]);
2074 if (ps->command == 0)
2076 pci_read_config_word(pd, PCI_COMMAND, &cmd);
2077 if ((ps->command & ~cmd) == 0)
2079 switch (pd->hdr_type) {
2080 case PCI_HEADER_TYPE_NORMAL:
2081 for (j = 0; j < 6; ++j)
2082 pci_write_config_dword(pd,
2083 PCI_BASE_ADDRESS_0 + j*4,
2084 pd->resource[j].start);
2085 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
2087 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
2089 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
2091 pci_write_config_word(pd, PCI_COMMAND, ps->command);
2099 * Put the powerbook to sleep.
2102 static u32 save_via[8];
2103 static void save_via_state(void)
2105 save_via[0] = in_8(&via[ANH]);
2106 save_via[1] = in_8(&via[DIRA]);
2107 save_via[2] = in_8(&via[B]);
2108 save_via[3] = in_8(&via[DIRB]);
2109 save_via[4] = in_8(&via[PCR]);
2110 save_via[5] = in_8(&via[ACR]);
2111 save_via[6] = in_8(&via[T1CL]);
2112 save_via[7] = in_8(&via[T1CH]);
2114 static void restore_via_state(void)
2116 out_8(&via[ANH], save_via[0]);
2117 out_8(&via[DIRA], save_via[1]);
2118 out_8(&via[B], save_via[2]);
2119 out_8(&via[DIRB], save_via[3]);
2120 out_8(&via[PCR], save_via[4]);
2121 out_8(&via[ACR], save_via[5]);
2122 out_8(&via[T1CL], save_via[6]);
2123 out_8(&via[T1CH], save_via[7]);
2124 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
2125 out_8(&via[IFR], 0x7f); /* clear IFR */
2126 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2129 #define GRACKLE_PM (1<<7)
2130 #define GRACKLE_DOZE (1<<5)
2131 #define GRACKLE_NAP (1<<4)
2132 #define GRACKLE_SLEEP (1<<3)
2134 int __openfirmware powerbook_sleep_G3(void)
2136 unsigned long save_l2cr;
2138 unsigned short pmcr1;
2139 struct adb_request req;
2141 struct pci_dev *grackle;
2143 grackle = pci_find_slot(0, 0);
2147 /* Notify device drivers */
2148 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2149 if (ret != PBOOK_SLEEP_OK) {
2150 printk("pmu: sleep rejected\n");
2154 /* Sync the disks. */
2155 /* XXX It would be nice to have some way to ensure that
2156 * nobody is dirtying any new buffers while we wait.
2157 * BenH: Moved to _after_ sleep request and changed video
2158 * drivers to vmalloc() during sleep request. This way, all
2159 * vmalloc's are done before actual sleep of block drivers */
2162 /* Give the disks a little time to actually finish writing */
2163 for (wait = jiffies + (HZ/2); time_before(jiffies, wait); )
2166 /* Sleep can fail now. May not be very robust but useful for debugging */
2167 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2168 if (ret != PBOOK_SLEEP_OK) {
2169 printk("pmu: sleep failed\n");
2173 /* Wait for completion of async backlight requests */
2174 while (!bright_req_1.complete || !bright_req_2.complete ||
2175 !bright_req_3.complete || !batt_req.complete)
2178 /* Turn off various things. Darwin does some retry tests here... */
2179 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2180 while (!req.complete)
2182 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2183 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2184 while (!req.complete)
2187 /* Disable all interrupts */
2188 pmac_sleep_save_intrs(-1);
2190 /* Make sure the PMU is idle */
2191 while (pmu_state != idle)
2194 /* Make sure the decrementer won't interrupt us */
2195 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2196 /* Make sure any pending DEC interrupt occuring while we did
2197 * the above didn't re-enable the DEC */
2199 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2201 /* We can now disable MSR_EE */
2202 local_irq_disable();
2204 /* Giveup the FPU */
2207 /* For 750, save backside cache setting and disable it */
2208 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2209 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2210 _set_L2CR(save_l2cr & 0x7fffffff);
2212 /* Ask the PMU to put us to sleep */
2213 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2214 while (!req.complete)
2217 /* The VIA is supposed not to be restored correctly*/
2219 /* We shut down some HW */
2220 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2222 pci_read_config_word(grackle, 0x70, &pmcr1);
2223 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2224 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2225 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2226 pci_write_config_word(grackle, 0x70, pmcr1);
2228 /* Call low-level ASM sleep handler */
2229 low_sleep_handler();
2231 /* We're awake again, stop grackle PM */
2232 pci_read_config_word(grackle, 0x70, &pmcr1);
2233 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2234 pci_write_config_word(grackle, 0x70, pmcr1);
2236 /* Make sure the PMU is idle */
2237 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2238 restore_via_state();
2240 /* Restore L2 cache */
2241 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2242 _set_L2CR(save_l2cr);
2244 /* Restore userland MMU context */
2245 set_context(current->active_mm->context, current->active_mm->pgd);
2247 /* Power things up */
2248 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0xfc);
2249 while (!req.complete)
2251 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2252 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2253 while (!req.complete)
2255 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2256 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2257 while (!req.complete)
2260 /* reenable interrupt controller */
2261 pmac_sleep_restore_intrs();
2263 /* Leave some time for HW to settle down */
2266 /* Restart jiffies & scheduling */
2267 wakeup_decrementer();
2269 /* Force a poll of ADB interrupts */
2270 adb_int_pending = 1;
2271 via_pmu_interrupt(0, 0, 0);
2273 /* Re-enable local CPU interrupts */
2276 /* Notify drivers */
2282 int __openfirmware powerbook_sleep_Core99(void)
2284 unsigned long save_l2cr;
2285 unsigned long save_l3cr;
2287 struct adb_request req;
2291 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2295 /* Notify device drivers */
2296 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2297 if (ret != PBOOK_SLEEP_OK) {
2298 printk("pmu: sleep rejected\n");
2302 /* Sync the disks. */
2303 /* XXX It would be nice to have some way to ensure that
2304 * nobody is dirtying any new buffers while we wait.
2305 * BenH: Moved to _after_ sleep request and changed video
2306 * drivers to vmalloc() during sleep request. This way, all
2307 * vmalloc's are done before actual sleep of block drivers */
2310 /* Give the disks a little time to actually finish writing */
2311 for (wait = jiffies + HZ; time_before(jiffies, wait); )
2314 /* Sleep can fail now. May not be very robust but useful for debugging */
2315 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2316 if (ret != PBOOK_SLEEP_OK) {
2317 printk("pmu: sleep failed\n");
2320 /* Wait for completion of async backlight requests */
2321 while (!bright_req_1.complete || !bright_req_2.complete ||
2322 !bright_req_3.complete || !batt_req.complete)
2325 /* Tell PMU what events will wake us up */
2326 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2328 while (!req.complete)
2331 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2332 0, PMU_PWR_WAKEUP_KEY |
2333 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2334 while (!req.complete)
2337 /* Save & disable all interrupts */
2338 openpic_sleep_save_intrs();
2340 /* Make sure the PMU is idle */
2341 while (pmu_state != idle)
2344 /* Make sure the decrementer won't interrupt us */
2345 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2346 /* Make sure any pending DEC interrupt occuring while we did
2347 * the above didn't re-enable the DEC */
2349 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2351 /* We can now disable MSR_EE */
2352 local_irq_disable();
2354 /* Giveup the FPU & vec */
2357 #ifdef CONFIG_ALTIVEC
2358 if (cur_cpu_spec[0]->cpu_features & CPU_FTR_ALTIVEC)
2359 enable_kernel_altivec();
2360 #endif /* CONFIG_ALTIVEC */
2362 /* Save & disable L2 and L3 caches*/
2363 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2364 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2365 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2366 _set_L3CR(save_l3cr & 0x7fffffff);
2367 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2368 _set_L2CR(save_l2cr & 0x7fffffff);
2370 /* Save the state of PCI config space for some slots */
2373 if (!__fake_sleep) {
2374 /* Ask the PMU to put us to sleep */
2375 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2376 while (!req.complete && pmu_state != idle)
2380 out_8(&via[B], in_8(&via[B]) | TREQ);
2383 /* The VIA is supposed not to be restored correctly*/
2386 /* Shut down various ASICs. There's a chance that we can no longer
2387 * talk to the PMU after this, so I moved it to _after_ sending the
2388 * sleep command to it. Still need to be checked.
2390 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2392 /* Call low-level ASM sleep handler */
2396 low_sleep_handler();
2398 /* Restore Apple core ASICs state */
2399 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2402 restore_via_state();
2404 /* Restore PCI config space. This should be overridable by PCI device
2405 * drivers as some of them may need special restore code. That's yet
2406 * another issue that should be handled by the common code properly,
2409 /* Don't restore PCI for now, it crashes. Maybe unnecessary on pbook */
2410 //pbook_pci_restore();
2415 /* Restore L2 cache */
2416 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2417 _set_L2CR(save_l2cr);
2418 /* Restore L3 cache */
2419 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2420 _set_L3CR(save_l3cr);
2422 /* Restore userland MMU context */
2423 set_context(current->active_mm->context, current->active_mm->pgd);
2425 /* Tell PMU we are ready */
2426 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2427 while (!req.complete)
2429 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0xfc);
2430 while (!req.complete)
2433 /* reenable interrupt controller */
2434 openpic_sleep_restore_intrs();
2436 /* Leave some time for HW to settle down */
2439 /* Restart jiffies & scheduling */
2440 wakeup_decrementer();
2442 /* Force a poll of ADB interrupts */
2443 adb_int_pending = 1;
2444 via_pmu_interrupt(0, 0, 0);
2446 /* Re-enable local CPU interrupts */
2449 /* Notify drivers */
2455 #define PB3400_MEM_CTRL 0xf8000000
2456 #define PB3400_MEM_CTRL_SLEEP 0x70
2458 int __openfirmware powerbook_sleep_3400(void)
2462 unsigned long p, wait;
2463 struct adb_request sleep_req;
2465 unsigned int *mem_ctrl_sleep;
2467 /* first map in the memory controller registers */
2468 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2469 if (mem_ctrl == NULL) {
2470 printk("powerbook_sleep_3400: ioremap failed\n");
2473 mem_ctrl_sleep = (unsigned int *) (mem_ctrl + PB3400_MEM_CTRL_SLEEP);
2475 /* Allocate room for PCI save */
2476 pbook_alloc_pci_save();
2478 /* Notify device drivers */
2479 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2480 if (ret != PBOOK_SLEEP_OK) {
2481 pbook_free_pci_save();
2482 printk("pmu: sleep rejected\n");
2486 /* Sync the disks. */
2487 /* XXX It would be nice to have some way to ensure that
2488 * nobody is dirtying any new buffers while we wait.
2489 * BenH: Moved to _after_ sleep request and changed video
2490 * drivers to vmalloc() during sleep request. This way, all
2491 * vmalloc's are done before actual sleep of block drivers */
2494 /* Give the disks a little time to actually finish writing */
2495 for (wait = jiffies + (HZ/4); time_before(jiffies, wait); )
2498 /* Sleep can fail now. May not be very robust but useful for debugging */
2499 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2500 if (ret != PBOOK_SLEEP_OK) {
2501 printk("pmu: sleep failed\n");
2502 pbook_free_pci_save();
2506 /* Wait for completion of async backlight requests */
2507 while (!bright_req_1.complete || !bright_req_2.complete ||
2508 !bright_req_3.complete || !batt_req.complete)
2511 /* Disable all interrupts except pmu */
2512 pmac_sleep_save_intrs(vias->intrs[0].line);
2514 /* Make sure the decrementer won't interrupt us */
2515 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2516 /* Make sure any pending DEC interrupt occuring while we did
2517 * the above didn't re-enable the DEC */
2519 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2521 /* Save the state of PCI config space for some slots */
2524 /* Set the memory controller to keep the memory refreshed
2525 while we're asleep */
2526 for (i = 0x403f; i >= 0x4000; --i) {
2527 out_be32(mem_ctrl_sleep, i);
2529 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2535 /* Ask the PMU to put us to sleep */
2536 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2537 while (!sleep_req.complete)
2540 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2542 /* displacement-flush the L2 cache - necessary? */
2543 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2544 i = *(volatile int *)p;
2547 /* Put the CPU into sleep mode */
2548 asm volatile("mfspr %0,1008" : "=r" (hid0) :);
2549 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2550 asm volatile("mtspr 1008,%0" : : "r" (hid0));
2551 _nmask_and_or_msr(0, MSR_POW | MSR_EE);
2554 /* OK, we're awake again, start restoring things */
2555 out_be32(mem_ctrl_sleep, 0x3f);
2556 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2557 pbook_pci_restore();
2559 /* wait for the PMU interrupt sequence to complete */
2563 /* reenable interrupts */
2564 pmac_sleep_restore_intrs();
2566 /* Leave some time for HW to settle down */
2569 /* Restart jiffies & scheduling */
2570 wakeup_decrementer();
2572 /* Re-enable local CPU interrupts */
2575 /* Notify drivers */
2578 pbook_free_pci_save();
2584 * Support for /dev/pmu device
2586 #define RB_SIZE 0x10
2587 struct pmu_private {
2588 struct list_head list;
2593 unsigned char data[16];
2595 wait_queue_head_t wait;
2597 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2598 int backlight_locker;
2599 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2602 static LIST_HEAD(all_pmu_pvt);
2603 static spinlock_t all_pvt_lock = SPIN_LOCK_UNLOCKED;
2605 static void pmu_pass_intr(unsigned char *data, int len)
2607 struct pmu_private *pp;
2608 struct list_head *list;
2610 unsigned long flags;
2612 if (len > sizeof(pp->rb_buf[0].data))
2613 len = sizeof(pp->rb_buf[0].data);
2614 spin_lock_irqsave(&all_pvt_lock, flags);
2615 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2616 pp = list_entry(list, struct pmu_private, list);
2617 spin_lock(&pp->lock);
2621 if (i != pp->rb_get) {
2622 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2624 memcpy(rp->data, data, len);
2626 wake_up_interruptible(&pp->wait);
2628 spin_unlock(&pp->lock);
2630 spin_unlock_irqrestore(&all_pvt_lock, flags);
2633 static int __openfirmware pmu_open(struct inode *inode, struct file *file)
2635 struct pmu_private *pp;
2636 unsigned long flags;
2638 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2641 pp->rb_get = pp->rb_put = 0;
2642 spin_lock_init(&pp->lock);
2643 init_waitqueue_head(&pp->wait);
2644 spin_lock_irqsave(&all_pvt_lock, flags);
2645 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2646 pp->backlight_locker = 0;
2647 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2648 list_add(&pp->list, &all_pmu_pvt);
2649 spin_unlock_irqrestore(&all_pvt_lock, flags);
2650 file->private_data = pp;
2655 static ssize_t __openfirmware pmu_read(struct file *file, char *buf,
2656 size_t count, loff_t *ppos)
2658 struct pmu_private *pp = file->private_data;
2659 DECLARE_WAITQUEUE(wait, current);
2660 unsigned long flags;
2663 if (count < 1 || pp == 0)
2665 ret = verify_area(VERIFY_WRITE, buf, count);
2669 spin_lock_irqsave(&pp->lock, flags);
2670 add_wait_queue(&pp->wait, &wait);
2671 current->state = TASK_INTERRUPTIBLE;
2675 if (pp->rb_get != pp->rb_put) {
2677 struct rb_entry *rp = &pp->rb_buf[i];
2679 spin_unlock_irqrestore(&pp->lock, flags);
2682 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2686 spin_lock_irqsave(&pp->lock, flags);
2691 if (file->f_flags & O_NONBLOCK)
2694 if (signal_pending(current))
2696 spin_unlock_irqrestore(&pp->lock, flags);
2698 spin_lock_irqsave(&pp->lock, flags);
2700 current->state = TASK_RUNNING;
2701 remove_wait_queue(&pp->wait, &wait);
2702 spin_unlock_irqrestore(&pp->lock, flags);
2707 static ssize_t __openfirmware pmu_write(struct file *file, const char *buf,
2708 size_t count, loff_t *ppos)
2713 static unsigned int pmu_fpoll(struct file *filp, poll_table *wait)
2715 struct pmu_private *pp = filp->private_data;
2716 unsigned int mask = 0;
2717 unsigned long flags;
2721 poll_wait(filp, &pp->wait, wait);
2722 spin_lock_irqsave(&pp->lock, flags);
2723 if (pp->rb_get != pp->rb_put)
2725 spin_unlock_irqrestore(&pp->lock, flags);
2729 static int pmu_release(struct inode *inode, struct file *file)
2731 struct pmu_private *pp = file->private_data;
2732 unsigned long flags;
2736 file->private_data = 0;
2737 spin_lock_irqsave(&all_pvt_lock, flags);
2738 list_del(&pp->list);
2739 spin_unlock_irqrestore(&all_pvt_lock, flags);
2740 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2741 if (pp->backlight_locker) {
2742 spin_lock_irqsave(&pmu_lock, flags);
2743 disable_kernel_backlight--;
2744 spin_unlock_irqrestore(&pmu_lock, flags);
2746 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2753 /* Note: removed __openfirmware here since it causes link errors */
2754 static int pmu_ioctl(struct inode * inode, struct file *filp,
2755 u_int cmd, u_long arg)
2757 struct pmu_private *pp = filp->private_data;
2762 if (!capable(CAP_SYS_ADMIN))
2764 if (sleep_in_progress)
2766 sleep_in_progress = 1;
2768 case PMU_OHARE_BASED:
2769 error = powerbook_sleep_3400();
2771 case PMU_HEATHROW_BASED:
2772 case PMU_PADDINGTON_BASED:
2773 error = powerbook_sleep_G3();
2775 case PMU_KEYLARGO_BASED:
2776 error = powerbook_sleep_Core99();
2781 sleep_in_progress = 0;
2783 case PMU_IOC_CAN_SLEEP:
2784 return put_user((u32)can_sleep, (__u32 *)arg);
2786 #ifdef CONFIG_PMAC_BACKLIGHT
2787 /* Backlight should have its own device or go via
2790 case PMU_IOC_GET_BACKLIGHT:
2791 if (sleep_in_progress)
2793 error = get_backlight_level();
2796 return put_user(error, (__u32 *)arg);
2797 case PMU_IOC_SET_BACKLIGHT:
2800 if (sleep_in_progress)
2802 error = get_user(value, (__u32 *)arg);
2804 error = set_backlight_level(value);
2807 #ifdef CONFIG_INPUT_ADBHID
2808 case PMU_IOC_GRAB_BACKLIGHT: {
2809 unsigned long flags;
2810 if (pp->backlight_locker)
2812 pp->backlight_locker = 1;
2813 spin_lock_irqsave(&pmu_lock, flags);
2814 disable_kernel_backlight++;
2815 spin_unlock_irqrestore(&pmu_lock, flags);
2818 #endif /* CONFIG_INPUT_ADBHID */
2819 #endif /* CONFIG_PMAC_BACKLIGHT */
2820 case PMU_IOC_GET_MODEL:
2821 return put_user(pmu_kind, (__u32 *)arg);
2822 case PMU_IOC_HAS_ADB:
2823 return put_user(pmu_has_adb, (__u32 *)arg);
2828 static struct file_operations pmu_device_fops = {
2834 release: pmu_release,
2837 static struct miscdevice pmu_device = {
2838 PMU_MINOR, "pmu", &pmu_device_fops
2841 void pmu_device_init(void)
2844 misc_register(&pmu_device);
2846 #endif /* CONFIG_PMAC_PBOOK */
2848 #if defined(DEBUG_SLEEP) || defined(DEBUG_FREQ)
2849 static inline void __pmac
2850 polled_handshake(volatile unsigned char *via)
2852 via[B] &= ~TREQ; eieio();
2853 while ((via[B] & TACK) != 0)
2855 via[B] |= TREQ; eieio();
2856 while ((via[B] & TACK) == 0)
2860 static inline void __pmac
2861 polled_send_byte(volatile unsigned char *via, int x)
2863 via[ACR] |= SR_OUT | SR_EXT; eieio();
2864 via[SR] = x; eieio();
2865 polled_handshake(via);
2868 static inline int __pmac
2869 polled_recv_byte(volatile unsigned char *via)
2873 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2874 x = via[SR]; eieio();
2875 polled_handshake(via);
2876 x = via[SR]; eieio();
2881 pmu_polled_request(struct adb_request *req)
2883 unsigned long flags;
2885 volatile unsigned char *v = via;
2889 l = pmu_data_len[c][0];
2890 if (l >= 0 && req->nbytes != l + 1)
2893 local_irq_save(flags);
2894 while (pmu_state != idle)
2897 while ((via[B] & TACK) == 0)
2899 polled_send_byte(v, c);
2901 l = req->nbytes - 1;
2902 polled_send_byte(v, l);
2904 for (i = 1; i <= l; ++i)
2905 polled_send_byte(v, req->data[i]);
2907 l = pmu_data_len[c][1];
2909 l = polled_recv_byte(v);
2910 for (i = 0; i < l; ++i)
2911 req->reply[i + req->reply_len] = polled_recv_byte(v);
2916 local_irq_restore(flags);
2919 #endif /* defined(DEBUG_SLEEP) || defined(DEBUG_FREQ) */
2922 EXPORT_SYMBOL(pmu_request);
2923 EXPORT_SYMBOL(pmu_poll);
2924 EXPORT_SYMBOL(pmu_suspend);
2925 EXPORT_SYMBOL(pmu_resume);
2926 EXPORT_SYMBOL(pmu_i2c_combined_read);
2927 EXPORT_SYMBOL(pmu_i2c_stdsub_write);
2928 EXPORT_SYMBOL(pmu_i2c_simple_read);
2929 EXPORT_SYMBOL(pmu_i2c_simple_write);
2930 #ifdef CONFIG_PMAC_PBOOK
2931 EXPORT_SYMBOL(pmu_register_sleep_notifier);
2932 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
2933 EXPORT_SYMBOL(pmu_enable_irled);
2934 EXPORT_SYMBOL(pmu_battery_count);
2935 EXPORT_SYMBOL(pmu_batteries);
2936 EXPORT_SYMBOL(pmu_power_flags);
2937 #endif /* CONFIG_PMAC_PBOOK */