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/types.h>
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/sched.h>
29 #include <linux/miscdevice.h>
30 #include <linux/blkdev.h>
31 #include <linux/pci.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/adb.h>
35 #include <linux/pmu.h>
36 #include <linux/cuda.h>
37 #include <linux/smp_lock.h>
38 #include <linux/module.h>
39 #include <linux/spinlock.h>
41 #include <linux/proc_fs.h>
42 #include <linux/init.h>
43 #include <linux/interrupt.h>
44 #include <linux/device.h>
45 #include <linux/sysdev.h>
46 #include <linux/suspend.h>
47 #include <linux/syscalls.h>
48 #include <linux/cpu.h>
50 #include <asm/machdep.h>
52 #include <asm/pgtable.h>
53 #include <asm/system.h>
54 #include <asm/sections.h>
56 #include <asm/pmac_feature.h>
57 #include <asm/pmac_pfunc.h>
58 #include <asm/pmac_low_i2c.h>
59 #include <asm/uaccess.h>
60 #include <asm/mmu_context.h>
61 #include <asm/cputable.h>
63 #ifdef CONFIG_PMAC_BACKLIGHT
64 #include <asm/backlight.h>
67 #include "via-pmu-event.h"
69 /* Some compile options */
70 #undef SUSPEND_USES_PMU
72 #undef HACKED_PCI_SAVE
74 /* Misc minor number allocated for /dev/pmu */
77 /* How many iterations between battery polls */
78 #define BATTERY_POLLING_COUNT 2
80 static volatile unsigned char __iomem
*via
;
82 /* VIA registers - spaced 0x200 bytes apart */
83 #define RS 0x200 /* skip between registers */
84 #define B 0 /* B-side data */
85 #define A RS /* A-side data */
86 #define DIRB (2*RS) /* B-side direction (1=output) */
87 #define DIRA (3*RS) /* A-side direction (1=output) */
88 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
89 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
90 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
91 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
92 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
93 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
94 #define SR (10*RS) /* Shift register */
95 #define ACR (11*RS) /* Auxiliary control register */
96 #define PCR (12*RS) /* Peripheral control register */
97 #define IFR (13*RS) /* Interrupt flag register */
98 #define IER (14*RS) /* Interrupt enable register */
99 #define ANH (15*RS) /* A-side data, no handshake */
101 /* Bits in B data register: both active low */
102 #define TACK 0x08 /* Transfer acknowledge (input) */
103 #define TREQ 0x10 /* Transfer request (output) */
106 #define SR_CTRL 0x1c /* Shift register control bits */
107 #define SR_EXT 0x0c /* Shift on external clock */
108 #define SR_OUT 0x10 /* Shift out if 1 */
110 /* Bits in IFR and IER */
111 #define IER_SET 0x80 /* set bits in IER */
112 #define IER_CLR 0 /* clear bits in IER */
113 #define SR_INT 0x04 /* Shift register full/empty */
115 #define CB1_INT 0x10 /* transition on CB1 input */
117 static volatile enum pmu_state
{
126 static volatile enum int_data_state
{
131 } int_data_state
[2] = { int_data_empty
, int_data_empty
};
133 static struct adb_request
*current_req
;
134 static struct adb_request
*last_req
;
135 static struct adb_request
*req_awaiting_reply
;
136 static unsigned char interrupt_data
[2][32];
137 static int interrupt_data_len
[2];
138 static int int_data_last
;
139 static unsigned char *reply_ptr
;
140 static int data_index
;
142 static volatile int adb_int_pending
;
143 static volatile int disable_poll
;
144 static struct device_node
*vias
;
145 static int pmu_kind
= PMU_UNKNOWN
;
146 static int pmu_fully_inited
= 0;
147 static int pmu_has_adb
;
148 static struct device_node
*gpio_node
;
149 static unsigned char __iomem
*gpio_reg
= NULL
;
150 static int gpio_irq
= NO_IRQ
;
151 static int gpio_irq_enabled
= -1;
152 static volatile int pmu_suspended
= 0;
153 static spinlock_t pmu_lock
;
154 static u8 pmu_intr_mask
;
155 static int pmu_version
;
156 static int drop_interrupts
;
157 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
158 static int option_lid_wakeup
= 1;
159 #endif /* CONFIG_PM && CONFIG_PPC32 */
160 #if (defined(CONFIG_PM)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT_LEGACY)
161 static int sleep_in_progress
;
163 static unsigned long async_req_locks
;
164 static unsigned int pmu_irq_stats
[11];
166 static struct proc_dir_entry
*proc_pmu_root
;
167 static struct proc_dir_entry
*proc_pmu_info
;
168 static struct proc_dir_entry
*proc_pmu_irqstats
;
169 static struct proc_dir_entry
*proc_pmu_options
;
170 static int option_server_mode
;
172 int pmu_battery_count
;
174 unsigned int pmu_power_flags
;
175 struct pmu_battery_info pmu_batteries
[PMU_MAX_BATTERIES
];
176 static int query_batt_timer
= BATTERY_POLLING_COUNT
;
177 static struct adb_request batt_req
;
178 static struct proc_dir_entry
*proc_pmu_batt
[PMU_MAX_BATTERIES
];
180 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
181 extern int disable_kernel_backlight
;
182 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
186 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list
);
189 static int adb_dev_map
= 0;
190 static int pmu_adb_flags
;
192 static int pmu_probe(void);
193 static int pmu_init(void);
194 static int pmu_send_request(struct adb_request
*req
, int sync
);
195 static int pmu_adb_autopoll(int devs
);
196 static int pmu_adb_reset_bus(void);
197 #endif /* CONFIG_ADB */
199 static int init_pmu(void);
200 static 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 static void pmu_pass_intr(unsigned char *data
, int len
);
208 static int proc_get_batt(char *page
, char **start
, off_t off
,
209 int count
, int *eof
, void *data
);
210 static int proc_read_options(char *page
, char **start
, off_t off
,
211 int count
, int *eof
, void *data
);
212 static int proc_write_options(struct file
*file
, const char __user
*buffer
,
213 unsigned long count
, void *data
);
216 struct adb_driver via_pmu_driver
= {
225 #endif /* CONFIG_ADB */
227 extern void low_sleep_handler(void);
228 extern void enable_kernel_altivec(void);
229 extern void enable_kernel_fp(void);
232 int pmu_polled_request(struct adb_request
*req
);
233 int pmu_wink(struct adb_request
*req
);
237 * This table indicates for each PMU opcode:
238 * - the number of data bytes to be sent with the command, or -1
239 * if a length byte should be sent,
240 * - the number of response bytes which the PMU will return, or
241 * -1 if it will send a length byte.
243 static const s8 pmu_data_len
[256][2] = {
244 /* 0 1 2 3 4 5 6 7 */
245 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
246 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
247 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
248 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
249 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
250 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
251 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
253 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
255 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
256 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
257 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
259 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
260 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
261 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
263 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
264 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
265 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
266 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
267 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
268 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
269 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
270 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
272 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
273 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
274 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
275 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
276 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
279 static char *pbook_type
[] = {
281 "PowerBook 2400/3400/3500(G3)",
282 "PowerBook G3 Series",
287 int __init
find_via_pmu(void)
294 vias
= of_find_node_by_name(NULL
, "via-pmu");
298 reg
= get_property(vias
, "reg", NULL
);
300 printk(KERN_ERR
"via-pmu: No \"reg\" property !\n");
303 taddr
= of_translate_address(vias
, reg
);
304 if (taddr
== OF_BAD_ADDR
) {
305 printk(KERN_ERR
"via-pmu: Can't translate address !\n");
309 spin_lock_init(&pmu_lock
);
313 pmu_intr_mask
= PMU_INT_PCEJECT
|
318 if (vias
->parent
->name
&& ((strcmp(vias
->parent
->name
, "ohare") == 0)
319 || device_is_compatible(vias
->parent
, "ohare")))
320 pmu_kind
= PMU_OHARE_BASED
;
321 else if (device_is_compatible(vias
->parent
, "paddington"))
322 pmu_kind
= PMU_PADDINGTON_BASED
;
323 else if (device_is_compatible(vias
->parent
, "heathrow"))
324 pmu_kind
= PMU_HEATHROW_BASED
;
325 else if (device_is_compatible(vias
->parent
, "Keylargo")
326 || device_is_compatible(vias
->parent
, "K2-Keylargo")) {
327 struct device_node
*gpiop
;
328 u64 gaddr
= OF_BAD_ADDR
;
330 pmu_kind
= PMU_KEYLARGO_BASED
;
331 pmu_has_adb
= (find_type_devices("adb") != NULL
);
332 pmu_intr_mask
= PMU_INT_PCEJECT
|
338 gpiop
= of_find_node_by_name(NULL
, "gpio");
340 reg
= get_property(gpiop
, "reg", NULL
);
342 gaddr
= of_translate_address(gpiop
, reg
);
343 if (gaddr
!= OF_BAD_ADDR
)
344 gpio_reg
= ioremap(gaddr
, 0x10);
346 if (gpio_reg
== NULL
)
347 printk(KERN_ERR
"via-pmu: Can't find GPIO reg !\n");
349 pmu_kind
= PMU_UNKNOWN
;
351 via
= ioremap(taddr
, 0x2000);
353 printk(KERN_ERR
"via-pmu: Can't map address !\n");
357 out_8(&via
[IER
], IER_CLR
| 0x7f); /* disable all intrs */
358 out_8(&via
[IFR
], 0x7f); /* clear IFR */
367 printk(KERN_INFO
"PMU driver v%d initialized for %s, firmware: %02x\n",
368 PMU_DRIVER_VERSION
, pbook_type
[pmu_kind
], pmu_version
);
370 sys_ctrler
= SYS_CTRLER_PMU
;
380 static int pmu_probe(void)
382 return vias
== NULL
? -ENODEV
: 0;
385 static int __init
pmu_init(void)
391 #endif /* CONFIG_ADB */
394 * We can't wait until pmu_init gets called, that happens too late.
395 * It happens after IDE and SCSI initialization, which can take a few
396 * seconds, and by that time the PMU could have given up on us and
398 * Thus this is called with arch_initcall rather than device_initcall.
400 static int __init
via_pmu_start(void)
407 batt_req
.complete
= 1;
409 irq
= irq_of_parse_and_map(vias
, 0);
411 printk(KERN_ERR
"via-pmu: can't map interruptn");
414 if (request_irq(irq
, via_pmu_interrupt
, 0, "VIA-PMU", (void *)0)) {
415 printk(KERN_ERR
"via-pmu: can't request irq %d\n", irq
);
419 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
420 gpio_node
= of_find_node_by_name(NULL
, "extint-gpio1");
421 if (gpio_node
== NULL
)
422 gpio_node
= of_find_node_by_name(NULL
,
425 gpio_irq
= irq_of_parse_and_map(gpio_node
, 0);
427 if (gpio_irq
!= NO_IRQ
) {
428 if (request_irq(gpio_irq
, gpio1_interrupt
, 0,
429 "GPIO1 ADB", (void *)0))
430 printk(KERN_ERR
"pmu: can't get irq %d"
431 " (GPIO1)\n", gpio_irq
);
433 gpio_irq_enabled
= 1;
437 /* Enable interrupts */
438 out_8(&via
[IER
], IER_SET
| SR_INT
| CB1_INT
);
440 pmu_fully_inited
= 1;
442 /* Make sure PMU settle down before continuing. This is _very_ important
443 * since the IDE probe may shut interrupts down for quite a bit of time. If
444 * a PMU communication is pending while this happens, the PMU may timeout
445 * Not that on Core99 machines, the PMU keeps sending us environement
446 * messages, we should find a way to either fix IDE or make it call
447 * pmu_suspend() before masking interrupts. This can also happens while
448 * scolling with some fbdevs.
452 } while (pmu_state
!= idle
);
457 arch_initcall(via_pmu_start
);
460 * This has to be done after pci_init, which is a subsys_initcall.
462 static int __init
via_pmu_dev_init(void)
467 #ifdef CONFIG_PMAC_BACKLIGHT
468 /* Initialize backlight */
469 pmu_backlight_init(vias
);
473 if (machine_is_compatible("AAPL,3400/2400") ||
474 machine_is_compatible("AAPL,3500")) {
475 int mb
= pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
476 NULL
, PMAC_MB_INFO_MODEL
, 0);
477 pmu_battery_count
= 1;
478 if (mb
== PMAC_TYPE_COMET
)
479 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_COMET
;
481 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_HOOPER
;
482 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
483 machine_is_compatible("PowerBook1,1")) {
484 pmu_battery_count
= 2;
485 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_SMART
;
486 pmu_batteries
[1].flags
|= PMU_BATT_TYPE_SMART
;
488 struct device_node
* prim
= find_devices("power-mgt");
489 const u32
*prim_info
= NULL
;
491 prim_info
= get_property(prim
, "prim-info", NULL
);
493 /* Other stuffs here yet unknown */
494 pmu_battery_count
= (prim_info
[6] >> 16) & 0xff;
495 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_SMART
;
496 if (pmu_battery_count
> 1)
497 pmu_batteries
[1].flags
|= PMU_BATT_TYPE_SMART
;
500 #endif /* CONFIG_PPC32 */
502 /* Create /proc/pmu */
503 proc_pmu_root
= proc_mkdir("pmu", NULL
);
507 for (i
=0; i
<pmu_battery_count
; i
++) {
509 sprintf(title
, "battery_%ld", i
);
510 proc_pmu_batt
[i
] = create_proc_read_entry(title
, 0, proc_pmu_root
,
511 proc_get_batt
, (void *)i
);
514 proc_pmu_info
= create_proc_read_entry("info", 0, proc_pmu_root
,
515 proc_get_info
, NULL
);
516 proc_pmu_irqstats
= create_proc_read_entry("interrupts", 0, proc_pmu_root
,
517 proc_get_irqstats
, NULL
);
518 proc_pmu_options
= create_proc_entry("options", 0600, proc_pmu_root
);
519 if (proc_pmu_options
) {
520 proc_pmu_options
->nlink
= 1;
521 proc_pmu_options
->read_proc
= proc_read_options
;
522 proc_pmu_options
->write_proc
= proc_write_options
;
528 device_initcall(via_pmu_dev_init
);
534 struct adb_request req
;
536 out_8(&via
[B
], via
[B
] | TREQ
); /* negate TREQ */
537 out_8(&via
[DIRB
], (via
[DIRB
] | TREQ
) & ~TACK
); /* TACK in, TREQ out */
539 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
541 while (!req
.complete
) {
543 printk(KERN_ERR
"init_pmu: no response from PMU\n");
550 /* ack all pending interrupts */
552 interrupt_data
[0][0] = 1;
553 while (interrupt_data
[0][0] || pmu_state
!= idle
) {
555 printk(KERN_ERR
"init_pmu: timed out acking intrs\n");
558 if (pmu_state
== idle
)
560 via_pmu_interrupt(0, NULL
, NULL
);
564 /* Tell PMU we are ready. */
565 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
566 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
567 while (!req
.complete
)
571 /* Read PMU version */
572 pmu_request(&req
, NULL
, 1, PMU_GET_VERSION
);
573 pmu_wait_complete(&req
);
574 if (req
.reply_len
> 0)
575 pmu_version
= req
.reply
[0];
577 /* Read server mode setting */
578 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
579 pmu_request(&req
, NULL
, 2, PMU_POWER_EVENTS
,
580 PMU_PWR_GET_POWERUP_EVENTS
);
581 pmu_wait_complete(&req
);
582 if (req
.reply_len
== 2) {
583 if (req
.reply
[1] & PMU_PWR_WAKEUP_AC_INSERT
)
584 option_server_mode
= 1;
585 printk(KERN_INFO
"via-pmu: Server Mode is %s\n",
586 option_server_mode
? "enabled" : "disabled");
598 static void pmu_set_server_mode(int server_mode
)
600 struct adb_request req
;
602 if (pmu_kind
!= PMU_KEYLARGO_BASED
)
605 option_server_mode
= server_mode
;
606 pmu_request(&req
, NULL
, 2, PMU_POWER_EVENTS
, PMU_PWR_GET_POWERUP_EVENTS
);
607 pmu_wait_complete(&req
);
608 if (req
.reply_len
< 2)
611 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
,
612 PMU_PWR_SET_POWERUP_EVENTS
,
613 req
.reply
[0], PMU_PWR_WAKEUP_AC_INSERT
);
615 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
,
616 PMU_PWR_CLR_POWERUP_EVENTS
,
617 req
.reply
[0], PMU_PWR_WAKEUP_AC_INSERT
);
618 pmu_wait_complete(&req
);
621 /* This new version of the code for 2400/3400/3500 powerbooks
622 * is inspired from the implementation in gkrellm-pmu
625 done_battery_state_ohare(struct adb_request
* req
)
629 * 0x01 : AC indicator
631 * 0x04 : battery exist
634 * 0x20 : full charged
635 * 0x40 : pcharge reset
636 * 0x80 : battery exist
638 * [1][2] : battery voltage
639 * [3] : CPU temperature
640 * [4] : battery temperature
645 unsigned int bat_flags
= PMU_BATT_TYPE_HOOPER
;
646 long pcharge
, charge
, vb
, vmax
, lmax
;
647 long vmax_charging
, vmax_charged
;
648 long amperage
, voltage
, time
, max
;
649 int mb
= pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
650 NULL
, PMAC_MB_INFO_MODEL
, 0);
652 if (req
->reply
[0] & 0x01)
653 pmu_power_flags
|= PMU_PWR_AC_PRESENT
;
655 pmu_power_flags
&= ~PMU_PWR_AC_PRESENT
;
657 if (mb
== PMAC_TYPE_COMET
) {
668 /* If battery installed */
669 if (req
->reply
[0] & 0x04) {
670 bat_flags
|= PMU_BATT_PRESENT
;
671 if (req
->reply
[0] & 0x02)
672 bat_flags
|= PMU_BATT_CHARGING
;
673 vb
= (req
->reply
[1] << 8) | req
->reply
[2];
674 voltage
= (vb
* 265 + 72665) / 10;
675 amperage
= req
->reply
[5];
676 if ((req
->reply
[0] & 0x01) == 0) {
678 vb
+= ((amperage
- 200) * 15)/100;
679 } else if (req
->reply
[0] & 0x02) {
680 vb
= (vb
* 97) / 100;
681 vmax
= vmax_charging
;
683 charge
= (100 * vb
) / vmax
;
684 if (req
->reply
[0] & 0x40) {
685 pcharge
= (req
->reply
[6] << 8) + req
->reply
[7];
689 pcharge
= 100 - pcharge
/ lmax
;
690 if (pcharge
< charge
)
694 time
= (charge
* 16440) / amperage
;
698 amperage
= -amperage
;
700 charge
= max
= amperage
= voltage
= time
= 0;
702 pmu_batteries
[pmu_cur_battery
].flags
= bat_flags
;
703 pmu_batteries
[pmu_cur_battery
].charge
= charge
;
704 pmu_batteries
[pmu_cur_battery
].max_charge
= max
;
705 pmu_batteries
[pmu_cur_battery
].amperage
= amperage
;
706 pmu_batteries
[pmu_cur_battery
].voltage
= voltage
;
707 pmu_batteries
[pmu_cur_battery
].time_remaining
= time
;
709 clear_bit(0, &async_req_locks
);
713 done_battery_state_smart(struct adb_request
* req
)
716 * [0] : format of this structure (known: 3,4,5)
729 * [4][5] : max charge
734 unsigned int bat_flags
= PMU_BATT_TYPE_SMART
;
736 unsigned int capa
, max
, voltage
;
738 if (req
->reply
[1] & 0x01)
739 pmu_power_flags
|= PMU_PWR_AC_PRESENT
;
741 pmu_power_flags
&= ~PMU_PWR_AC_PRESENT
;
744 capa
= max
= amperage
= voltage
= 0;
746 if (req
->reply
[1] & 0x04) {
747 bat_flags
|= PMU_BATT_PRESENT
;
748 switch(req
->reply
[0]) {
750 case 4: capa
= req
->reply
[2];
752 amperage
= *((signed char *)&req
->reply
[4]);
753 voltage
= req
->reply
[5];
755 case 5: capa
= (req
->reply
[2] << 8) | req
->reply
[3];
756 max
= (req
->reply
[4] << 8) | req
->reply
[5];
757 amperage
= *((signed short *)&req
->reply
[6]);
758 voltage
= (req
->reply
[8] << 8) | req
->reply
[9];
761 printk(KERN_WARNING
"pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
762 req
->reply_len
, req
->reply
[0], req
->reply
[1], req
->reply
[2], req
->reply
[3]);
767 if ((req
->reply
[1] & 0x01) && (amperage
> 0))
768 bat_flags
|= PMU_BATT_CHARGING
;
770 pmu_batteries
[pmu_cur_battery
].flags
= bat_flags
;
771 pmu_batteries
[pmu_cur_battery
].charge
= capa
;
772 pmu_batteries
[pmu_cur_battery
].max_charge
= max
;
773 pmu_batteries
[pmu_cur_battery
].amperage
= amperage
;
774 pmu_batteries
[pmu_cur_battery
].voltage
= voltage
;
776 if ((req
->reply
[1] & 0x01) && (amperage
> 0))
777 pmu_batteries
[pmu_cur_battery
].time_remaining
778 = ((max
-capa
) * 3600) / amperage
;
780 pmu_batteries
[pmu_cur_battery
].time_remaining
781 = (capa
* 3600) / (-amperage
);
783 pmu_batteries
[pmu_cur_battery
].time_remaining
= 0;
785 pmu_cur_battery
= (pmu_cur_battery
+ 1) % pmu_battery_count
;
787 clear_bit(0, &async_req_locks
);
791 query_battery_state(void)
793 if (test_and_set_bit(0, &async_req_locks
))
795 if (pmu_kind
== PMU_OHARE_BASED
)
796 pmu_request(&batt_req
, done_battery_state_ohare
,
797 1, PMU_BATTERY_STATE
);
799 pmu_request(&batt_req
, done_battery_state_smart
,
800 2, PMU_SMART_BATTERY_STATE
, pmu_cur_battery
+1);
804 proc_get_info(char *page
, char **start
, off_t off
,
805 int count
, int *eof
, void *data
)
809 p
+= sprintf(p
, "PMU driver version : %d\n", PMU_DRIVER_VERSION
);
810 p
+= sprintf(p
, "PMU firmware version : %02x\n", pmu_version
);
811 p
+= sprintf(p
, "AC Power : %d\n",
812 ((pmu_power_flags
& PMU_PWR_AC_PRESENT
) != 0) || pmu_battery_count
== 0);
813 p
+= sprintf(p
, "Battery count : %d\n", pmu_battery_count
);
819 proc_get_irqstats(char *page
, char **start
, off_t off
,
820 int count
, int *eof
, void *data
)
824 static const char *irq_names
[] = {
825 "Total CB1 triggered events",
826 "Total GPIO1 triggered events",
827 "PC-Card eject button",
828 "Sound/Brightness button",
830 "Battery state change",
831 "Environment interrupt",
833 "Ghost interrupt (zero len)",
834 "Empty interrupt (empty mask)",
838 for (i
=0; i
<11; i
++) {
839 p
+= sprintf(p
, " %2u: %10u (%s)\n",
840 i
, pmu_irq_stats
[i
], irq_names
[i
]);
846 proc_get_batt(char *page
, char **start
, off_t off
,
847 int count
, int *eof
, void *data
)
849 long batnum
= (long)data
;
852 p
+= sprintf(p
, "\n");
853 p
+= sprintf(p
, "flags : %08x\n",
854 pmu_batteries
[batnum
].flags
);
855 p
+= sprintf(p
, "charge : %d\n",
856 pmu_batteries
[batnum
].charge
);
857 p
+= sprintf(p
, "max_charge : %d\n",
858 pmu_batteries
[batnum
].max_charge
);
859 p
+= sprintf(p
, "current : %d\n",
860 pmu_batteries
[batnum
].amperage
);
861 p
+= sprintf(p
, "voltage : %d\n",
862 pmu_batteries
[batnum
].voltage
);
863 p
+= sprintf(p
, "time rem. : %d\n",
864 pmu_batteries
[batnum
].time_remaining
);
870 proc_read_options(char *page
, char **start
, off_t off
,
871 int count
, int *eof
, void *data
)
875 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
876 if (pmu_kind
== PMU_KEYLARGO_BASED
&&
877 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) >= 0)
878 p
+= sprintf(p
, "lid_wakeup=%d\n", option_lid_wakeup
);
880 if (pmu_kind
== PMU_KEYLARGO_BASED
)
881 p
+= sprintf(p
, "server_mode=%d\n", option_server_mode
);
887 proc_write_options(struct file
*file
, const char __user
*buffer
,
888 unsigned long count
, void *data
)
892 unsigned long fcount
= count
;
898 if (copy_from_user(tmp
, buffer
, count
))
906 while(*val
&& (*val
!= '=')) {
916 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
917 if (pmu_kind
== PMU_KEYLARGO_BASED
&&
918 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) >= 0)
919 if (!strcmp(label
, "lid_wakeup"))
920 option_lid_wakeup
= ((*val
) == '1');
922 if (pmu_kind
== PMU_KEYLARGO_BASED
&& !strcmp(label
, "server_mode")) {
924 new_value
= ((*val
) == '1');
925 if (new_value
!= option_server_mode
)
926 pmu_set_server_mode(new_value
);
932 /* Send an ADB command */
934 pmu_send_request(struct adb_request
*req
, int sync
)
938 if ((vias
== NULL
) || (!pmu_fully_inited
)) {
945 switch (req
->data
[0]) {
947 for (i
= 0; i
< req
->nbytes
- 1; ++i
)
948 req
->data
[i
] = req
->data
[i
+1];
950 if (pmu_data_len
[req
->data
[0]][1] != 0) {
951 req
->reply
[0] = ADB_RET_OK
;
955 ret
= pmu_queue_request(req
);
958 switch (req
->data
[1]) {
960 if (req
->nbytes
!= 2)
962 req
->data
[0] = PMU_READ_RTC
;
965 req
->reply
[0] = CUDA_PACKET
;
967 req
->reply
[2] = CUDA_GET_TIME
;
968 ret
= pmu_queue_request(req
);
971 if (req
->nbytes
!= 6)
973 req
->data
[0] = PMU_SET_RTC
;
975 for (i
= 1; i
<= 4; ++i
)
976 req
->data
[i
] = req
->data
[i
+1];
978 req
->reply
[0] = CUDA_PACKET
;
980 req
->reply
[2] = CUDA_SET_TIME
;
981 ret
= pmu_queue_request(req
);
988 for (i
= req
->nbytes
- 1; i
> 1; --i
)
989 req
->data
[i
+2] = req
->data
[i
];
990 req
->data
[3] = req
->nbytes
- 2;
991 req
->data
[2] = pmu_adb_flags
;
992 /*req->data[1] = req->data[1];*/
993 req
->data
[0] = PMU_ADB_CMD
;
995 req
->reply_expected
= 1;
997 ret
= pmu_queue_request(req
);
1006 while (!req
->complete
)
1012 /* Enable/disable autopolling */
1014 pmu_adb_autopoll(int devs
)
1016 struct adb_request req
;
1018 if ((vias
== NULL
) || (!pmu_fully_inited
) || !pmu_has_adb
)
1023 pmu_request(&req
, NULL
, 5, PMU_ADB_CMD
, 0, 0x86,
1024 adb_dev_map
>> 8, adb_dev_map
);
1027 pmu_request(&req
, NULL
, 1, PMU_ADB_POLL_OFF
);
1030 while (!req
.complete
)
1035 /* Reset the ADB bus */
1037 pmu_adb_reset_bus(void)
1039 struct adb_request req
;
1040 int save_autopoll
= adb_dev_map
;
1042 if ((vias
== NULL
) || (!pmu_fully_inited
) || !pmu_has_adb
)
1045 /* anyone got a better idea?? */
1046 pmu_adb_autopoll(0);
1050 req
.data
[0] = PMU_ADB_CMD
;
1052 req
.data
[2] = ADB_BUSRESET
;
1056 req
.reply_expected
= 1;
1057 if (pmu_queue_request(&req
) != 0) {
1058 printk(KERN_ERR
"pmu_adb_reset_bus: pmu_queue_request failed\n");
1061 pmu_wait_complete(&req
);
1063 if (save_autopoll
!= 0)
1064 pmu_adb_autopoll(save_autopoll
);
1068 #endif /* CONFIG_ADB */
1070 /* Construct and send a pmu request */
1072 pmu_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
1081 if (nbytes
< 0 || nbytes
> 32) {
1082 printk(KERN_ERR
"pmu_request: bad nbytes (%d)\n", nbytes
);
1086 req
->nbytes
= nbytes
;
1088 va_start(list
, nbytes
);
1089 for (i
= 0; i
< nbytes
; ++i
)
1090 req
->data
[i
] = va_arg(list
, int);
1093 req
->reply_expected
= 0;
1094 return pmu_queue_request(req
);
1098 pmu_queue_request(struct adb_request
*req
)
1100 unsigned long flags
;
1107 if (req
->nbytes
<= 0) {
1111 nsend
= pmu_data_len
[req
->data
[0]][0];
1112 if (nsend
>= 0 && req
->nbytes
!= nsend
+ 1) {
1121 spin_lock_irqsave(&pmu_lock
, flags
);
1122 if (current_req
!= 0) {
1123 last_req
->next
= req
;
1128 if (pmu_state
== idle
)
1131 spin_unlock_irqrestore(&pmu_lock
, flags
);
1139 /* Sightly increased the delay, I had one occurrence of the message
1143 while ((in_8(&via
[B
]) & TACK
) == 0) {
1144 if (--timeout
< 0) {
1145 printk(KERN_ERR
"PMU not responding (!ack)\n");
1152 /* New PMU seems to be very sensitive to those timings, so we make sure
1153 * PCI is flushed immediately */
1157 volatile unsigned char __iomem
*v
= via
;
1159 out_8(&v
[ACR
], in_8(&v
[ACR
]) | SR_OUT
| SR_EXT
);
1161 out_8(&v
[B
], in_8(&v
[B
]) & ~TREQ
); /* assert TREQ */
1168 volatile unsigned char __iomem
*v
= via
;
1170 out_8(&v
[ACR
], (in_8(&v
[ACR
]) & ~SR_OUT
) | SR_EXT
);
1171 in_8(&v
[SR
]); /* resets SR */
1172 out_8(&v
[B
], in_8(&v
[B
]) & ~TREQ
);
1177 pmu_done(struct adb_request
*req
)
1179 void (*done
)(struct adb_request
*) = req
->done
;
1182 /* Here, we assume that if the request has a done member, the
1183 * struct request will survive to setting req->complete to 1
1192 struct adb_request
*req
;
1194 /* assert pmu_state == idle */
1195 /* get the packet to send */
1197 if (req
== 0 || pmu_state
!= idle
1198 || (/*req->reply_expected && */req_awaiting_reply
))
1201 pmu_state
= sending
;
1203 data_len
= pmu_data_len
[req
->data
[0]][0];
1205 /* Sounds safer to make sure ACK is high before writing. This helped
1206 * kill a problem with ADB and some iBooks
1209 /* set the shift register to shift out and send a byte */
1210 send_byte(req
->data
[0]);
1220 via_pmu_interrupt(0, NULL
, NULL
);
1230 /* Kicks ADB read when PMU is suspended */
1231 adb_int_pending
= 1;
1233 via_pmu_interrupt(0, NULL
, NULL
);
1234 } while (pmu_suspended
&& (adb_int_pending
|| pmu_state
!= idle
1235 || req_awaiting_reply
));
1239 pmu_wait_complete(struct adb_request
*req
)
1243 while((pmu_state
!= idle
&& pmu_state
!= locked
) || !req
->complete
)
1244 via_pmu_interrupt(0, NULL
, NULL
);
1247 /* This function loops until the PMU is idle and prevents it from
1248 * anwsering to ADB interrupts. pmu_request can still be called.
1249 * This is done to avoid spurrious shutdowns when we know we'll have
1250 * interrupts switched off for a long time
1255 unsigned long flags
;
1256 #ifdef SUSPEND_USES_PMU
1257 struct adb_request
*req
;
1262 spin_lock_irqsave(&pmu_lock
, flags
);
1264 if (pmu_suspended
> 1) {
1265 spin_unlock_irqrestore(&pmu_lock
, flags
);
1270 spin_unlock_irqrestore(&pmu_lock
, flags
);
1271 if (req_awaiting_reply
)
1272 adb_int_pending
= 1;
1273 via_pmu_interrupt(0, NULL
, NULL
);
1274 spin_lock_irqsave(&pmu_lock
, flags
);
1275 if (!adb_int_pending
&& pmu_state
== idle
&& !req_awaiting_reply
) {
1276 #ifdef SUSPEND_USES_PMU
1277 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, 0);
1278 spin_unlock_irqrestore(&pmu_lock
, flags
);
1279 while(!req
.complete
)
1281 #else /* SUSPEND_USES_PMU */
1283 disable_irq_nosync(gpio_irq
);
1284 out_8(&via
[IER
], CB1_INT
| IER_CLR
);
1285 spin_unlock_irqrestore(&pmu_lock
, flags
);
1286 #endif /* SUSPEND_USES_PMU */
1295 unsigned long flags
;
1297 if (!via
|| (pmu_suspended
< 1))
1300 spin_lock_irqsave(&pmu_lock
, flags
);
1302 if (pmu_suspended
> 0) {
1303 spin_unlock_irqrestore(&pmu_lock
, flags
);
1306 adb_int_pending
= 1;
1307 #ifdef SUSPEND_USES_PMU
1308 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
1309 spin_unlock_irqrestore(&pmu_lock
, flags
);
1310 while(!req
.complete
)
1312 #else /* SUSPEND_USES_PMU */
1314 enable_irq(gpio_irq
);
1315 out_8(&via
[IER
], CB1_INT
| IER_SET
);
1316 spin_unlock_irqrestore(&pmu_lock
, flags
);
1318 #endif /* SUSPEND_USES_PMU */
1321 /* Interrupt data could be the result data from an ADB cmd */
1323 pmu_handle_data(unsigned char *data
, int len
, struct pt_regs
*regs
)
1325 unsigned char ints
, pirq
;
1329 if (drop_interrupts
|| len
< 1) {
1330 adb_int_pending
= 0;
1335 /* Get PMU interrupt mask */
1338 /* Record zero interrupts for stats */
1342 /* Hack to deal with ADB autopoll flag */
1343 if (ints
& PMU_INT_ADB
)
1344 ints
&= ~(PMU_INT_ADB_AUTO
| PMU_INT_AUTO_SRQ_POLL
);
1349 if (i
> pmu_irq_stats
[10])
1350 pmu_irq_stats
[10] = i
;
1354 for (pirq
= 0; pirq
< 8; pirq
++)
1355 if (ints
& (1 << pirq
))
1357 pmu_irq_stats
[pirq
]++;
1359 ints
&= ~(1 << pirq
);
1361 /* Note: for some reason, we get an interrupt with len=1,
1362 * data[0]==0 after each normal ADB interrupt, at least
1363 * on the Pismo. Still investigating... --BenH
1365 if ((1 << pirq
) & PMU_INT_ADB
) {
1366 if ((data
[0] & PMU_INT_ADB_AUTO
) == 0) {
1367 struct adb_request
*req
= req_awaiting_reply
;
1369 printk(KERN_ERR
"PMU: extra ADB reply\n");
1372 req_awaiting_reply
= NULL
;
1376 memcpy(req
->reply
, data
+ 1, len
- 1);
1377 req
->reply_len
= len
- 1;
1381 if (len
== 4 && data
[1] == 0x2c) {
1382 extern int xmon_wants_key
, xmon_adb_keycode
;
1383 if (xmon_wants_key
) {
1384 xmon_adb_keycode
= data
[2];
1390 * XXX On the [23]400 the PMU gives us an up
1391 * event for keycodes 0x74 or 0x75 when the PC
1392 * card eject buttons are released, so we
1393 * ignore those events.
1395 if (!(pmu_kind
== PMU_OHARE_BASED
&& len
== 4
1396 && data
[1] == 0x2c && data
[3] == 0xff
1397 && (data
[2] & ~1) == 0xf4))
1398 adb_input(data
+1, len
-1, regs
, 1);
1399 #endif /* CONFIG_ADB */
1402 /* Sound/brightness button pressed */
1403 else if ((1 << pirq
) & PMU_INT_SNDBRT
) {
1404 #ifdef CONFIG_PMAC_BACKLIGHT
1406 #ifdef CONFIG_INPUT_ADBHID
1407 if (!disable_kernel_backlight
)
1408 #endif /* CONFIG_INPUT_ADBHID */
1409 pmac_backlight_set_legacy_brightness(data
[1] >> 4);
1410 #endif /* CONFIG_PMAC_BACKLIGHT */
1412 /* Tick interrupt */
1413 else if ((1 << pirq
) & PMU_INT_TICK
) {
1414 /* Environement or tick interrupt, query batteries */
1415 if (pmu_battery_count
) {
1416 if ((--query_batt_timer
) == 0) {
1417 query_battery_state();
1418 query_batt_timer
= BATTERY_POLLING_COUNT
;
1422 else if ((1 << pirq
) & PMU_INT_ENVIRONMENT
) {
1423 if (pmu_battery_count
)
1424 query_battery_state();
1425 pmu_pass_intr(data
, len
);
1426 /* len == 6 is probably a bad check. But how do I
1427 * know what PMU versions send what events here? */
1429 via_pmu_event(PMU_EVT_POWER
, !!(data
[1]&8));
1430 via_pmu_event(PMU_EVT_LID
, data
[1]&1);
1433 pmu_pass_intr(data
, len
);
1438 static struct adb_request
*
1439 pmu_sr_intr(struct pt_regs
*regs
)
1441 struct adb_request
*req
;
1444 if (via
[B
] & TREQ
) {
1445 printk(KERN_ERR
"PMU: spurious SR intr (%x)\n", via
[B
]);
1446 out_8(&via
[IFR
], SR_INT
);
1449 /* The ack may not yet be low when we get the interrupt */
1450 while ((in_8(&via
[B
]) & TACK
) != 0)
1453 /* if reading grab the byte, and reset the interrupt */
1454 if (pmu_state
== reading
|| pmu_state
== reading_intr
)
1455 bite
= in_8(&via
[SR
]);
1457 /* reset TREQ and wait for TACK to go high */
1458 out_8(&via
[B
], in_8(&via
[B
]) | TREQ
);
1461 switch (pmu_state
) {
1465 data_len
= req
->nbytes
- 1;
1466 send_byte(data_len
);
1469 if (data_index
<= data_len
) {
1470 send_byte(req
->data
[data_index
++]);
1474 data_len
= pmu_data_len
[req
->data
[0]][1];
1475 if (data_len
== 0) {
1477 current_req
= req
->next
;
1478 if (req
->reply_expected
)
1479 req_awaiting_reply
= req
;
1483 pmu_state
= reading
;
1485 reply_ptr
= req
->reply
+ req
->reply_len
;
1493 pmu_state
= reading_intr
;
1494 reply_ptr
= interrupt_data
[int_data_last
];
1496 if (gpio_irq
>= 0 && !gpio_irq_enabled
) {
1497 enable_irq(gpio_irq
);
1498 gpio_irq_enabled
= 1;
1504 if (data_len
== -1) {
1507 printk(KERN_ERR
"PMU: bad reply len %d\n", bite
);
1508 } else if (data_index
< 32) {
1509 reply_ptr
[data_index
++] = bite
;
1511 if (data_index
< data_len
) {
1516 if (pmu_state
== reading_intr
) {
1518 int_data_state
[int_data_last
] = int_data_ready
;
1519 interrupt_data_len
[int_data_last
] = data_len
;
1523 * For PMU sleep and freq change requests, we lock the
1524 * PMU until it's explicitely unlocked. This avoids any
1525 * spurrious event polling getting in
1527 current_req
= req
->next
;
1528 req
->reply_len
+= data_index
;
1529 if (req
->data
[0] == PMU_SLEEP
|| req
->data
[0] == PMU_CPU_SPEED
)
1538 printk(KERN_ERR
"via_pmu_interrupt: unknown state %d?\n",
1545 via_pmu_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
1547 unsigned long flags
;
1551 struct adb_request
*req
= NULL
;
1554 /* This is a bit brutal, we can probably do better */
1555 spin_lock_irqsave(&pmu_lock
, flags
);
1559 intr
= in_8(&via
[IFR
]) & (SR_INT
| CB1_INT
);
1563 if (++nloop
> 1000) {
1564 printk(KERN_DEBUG
"PMU: stuck in intr loop, "
1565 "intr=%x, ier=%x pmu_state=%d\n",
1566 intr
, in_8(&via
[IER
]), pmu_state
);
1569 out_8(&via
[IFR
], intr
);
1570 if (intr
& CB1_INT
) {
1571 adb_int_pending
= 1;
1574 if (intr
& SR_INT
) {
1575 req
= pmu_sr_intr(regs
);
1582 if (pmu_state
== idle
) {
1583 if (adb_int_pending
) {
1584 if (int_data_state
[0] == int_data_empty
)
1586 else if (int_data_state
[1] == int_data_empty
)
1591 int_data_state
[int_data_last
] = int_data_fill
;
1592 /* Sounds safer to make sure ACK is high before writing.
1593 * This helped kill a problem with ADB and some iBooks
1596 send_byte(PMU_INT_ACK
);
1597 adb_int_pending
= 0;
1598 } else if (current_req
)
1602 /* Mark the oldest buffer for flushing */
1603 if (int_data_state
[!int_data_last
] == int_data_ready
) {
1604 int_data_state
[!int_data_last
] = int_data_flush
;
1605 int_data
= !int_data_last
;
1606 } else if (int_data_state
[int_data_last
] == int_data_ready
) {
1607 int_data_state
[int_data_last
] = int_data_flush
;
1608 int_data
= int_data_last
;
1611 spin_unlock_irqrestore(&pmu_lock
, flags
);
1613 /* Deal with completed PMU requests outside of the lock */
1619 /* Deal with interrupt datas outside of the lock */
1620 if (int_data
>= 0) {
1621 pmu_handle_data(interrupt_data
[int_data
], interrupt_data_len
[int_data
], regs
);
1622 spin_lock_irqsave(&pmu_lock
, flags
);
1624 int_data_state
[int_data
] = int_data_empty
;
1629 return IRQ_RETVAL(handled
);
1635 unsigned long flags
;
1637 spin_lock_irqsave(&pmu_lock
, flags
);
1638 if (pmu_state
== locked
)
1640 adb_int_pending
= 1;
1641 spin_unlock_irqrestore(&pmu_lock
, flags
);
1646 gpio1_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
1648 unsigned long flags
;
1650 if ((in_8(gpio_reg
+ 0x9) & 0x02) == 0) {
1651 spin_lock_irqsave(&pmu_lock
, flags
);
1652 if (gpio_irq_enabled
> 0) {
1653 disable_irq_nosync(gpio_irq
);
1654 gpio_irq_enabled
= 0;
1657 adb_int_pending
= 1;
1658 spin_unlock_irqrestore(&pmu_lock
, flags
);
1659 via_pmu_interrupt(0, NULL
, NULL
);
1666 pmu_enable_irled(int on
)
1668 struct adb_request req
;
1672 if (pmu_kind
== PMU_KEYLARGO_BASED
)
1675 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
, PMU_POW_IRLED
|
1676 (on
? PMU_POW_ON
: PMU_POW_OFF
));
1677 pmu_wait_complete(&req
);
1683 struct adb_request req
;
1688 local_irq_disable();
1690 drop_interrupts
= 1;
1692 if (pmu_kind
!= PMU_KEYLARGO_BASED
) {
1693 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|
1695 while(!req
.complete
)
1699 pmu_request(&req
, NULL
, 1, PMU_RESET
);
1700 pmu_wait_complete(&req
);
1708 struct adb_request req
;
1713 local_irq_disable();
1715 drop_interrupts
= 1;
1717 if (pmu_kind
!= PMU_KEYLARGO_BASED
) {
1718 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|
1720 pmu_wait_complete(&req
);
1722 /* Disable server mode on shutdown or we'll just
1725 pmu_set_server_mode(0);
1728 pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
,
1729 'M', 'A', 'T', 'T');
1730 pmu_wait_complete(&req
);
1743 static LIST_HEAD(sleep_notifiers
);
1746 pmu_register_sleep_notifier(struct pmu_sleep_notifier
*n
)
1748 struct list_head
*list
;
1749 struct pmu_sleep_notifier
*notifier
;
1751 for (list
= sleep_notifiers
.next
; list
!= &sleep_notifiers
;
1752 list
= list
->next
) {
1753 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
1754 if (n
->priority
> notifier
->priority
)
1757 __list_add(&n
->list
, list
->prev
, list
);
1760 EXPORT_SYMBOL(pmu_register_sleep_notifier
);
1763 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier
* n
)
1765 if (n
->list
.next
== 0)
1768 n
->list
.next
= NULL
;
1771 EXPORT_SYMBOL(pmu_unregister_sleep_notifier
);
1772 #endif /* CONFIG_PM */
1774 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1776 /* Sleep is broadcast last-to-first */
1778 broadcast_sleep(int when
, int fallback
)
1780 int ret
= PBOOK_SLEEP_OK
;
1781 struct list_head
*list
;
1782 struct pmu_sleep_notifier
*notifier
;
1784 for (list
= sleep_notifiers
.prev
; list
!= &sleep_notifiers
;
1785 list
= list
->prev
) {
1786 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
1787 ret
= notifier
->notifier_call(notifier
, when
);
1788 if (ret
!= PBOOK_SLEEP_OK
) {
1789 printk(KERN_DEBUG
"sleep %d rejected by %p (%p)\n",
1790 when
, notifier
, notifier
->notifier_call
);
1791 for (; list
!= &sleep_notifiers
; list
= list
->next
) {
1792 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
1793 notifier
->notifier_call(notifier
, fallback
);
1801 /* Wake is broadcast first-to-last */
1803 broadcast_wake(void)
1805 int ret
= PBOOK_SLEEP_OK
;
1806 struct list_head
*list
;
1807 struct pmu_sleep_notifier
*notifier
;
1809 for (list
= sleep_notifiers
.next
; list
!= &sleep_notifiers
;
1810 list
= list
->next
) {
1811 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
1812 notifier
->notifier_call(notifier
, PBOOK_WAKE
);
1818 * This struct is used to store config register values for
1819 * PCI devices which may get powered off when we sleep.
1821 static struct pci_save
{
1822 #ifndef HACKED_PCI_SAVE
1831 static int pbook_npci_saves
;
1834 pbook_alloc_pci_save(void)
1837 struct pci_dev
*pd
= NULL
;
1840 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
1845 pbook_pci_saves
= (struct pci_save
*)
1846 kmalloc(npci
* sizeof(struct pci_save
), GFP_KERNEL
);
1847 pbook_npci_saves
= npci
;
1851 pbook_free_pci_save(void)
1853 if (pbook_pci_saves
== NULL
)
1855 kfree(pbook_pci_saves
);
1856 pbook_pci_saves
= NULL
;
1857 pbook_npci_saves
= 0;
1861 pbook_pci_save(void)
1863 struct pci_save
*ps
= pbook_pci_saves
;
1864 struct pci_dev
*pd
= NULL
;
1865 int npci
= pbook_npci_saves
;
1870 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
1873 #ifndef HACKED_PCI_SAVE
1874 pci_read_config_word(pd
, PCI_COMMAND
, &ps
->command
);
1875 pci_read_config_word(pd
, PCI_CACHE_LINE_SIZE
, &ps
->cache_lat
);
1876 pci_read_config_word(pd
, PCI_INTERRUPT_LINE
, &ps
->intr
);
1877 pci_read_config_dword(pd
, PCI_ROM_ADDRESS
, &ps
->rom_address
);
1881 pci_read_config_dword(pd
, i
<<4, &ps
->config
[i
]);
1887 /* For this to work, we must take care of a few things: If gmac was enabled
1888 * during boot, it will be in the pci dev list. If it's disabled at this point
1889 * (and it will probably be), then you can't access it's config space.
1892 pbook_pci_restore(void)
1895 struct pci_save
*ps
= pbook_pci_saves
- 1;
1896 struct pci_dev
*pd
= NULL
;
1897 int npci
= pbook_npci_saves
;
1900 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
1901 #ifdef HACKED_PCI_SAVE
1907 pci_write_config_dword(pd
, i
<<4, ps
->config
[i
]);
1908 pci_write_config_dword(pd
, 4, ps
->config
[1]);
1913 if (ps
->command
== 0)
1915 pci_read_config_word(pd
, PCI_COMMAND
, &cmd
);
1916 if ((ps
->command
& ~cmd
) == 0)
1918 switch (pd
->hdr_type
) {
1919 case PCI_HEADER_TYPE_NORMAL
:
1920 for (j
= 0; j
< 6; ++j
)
1921 pci_write_config_dword(pd
,
1922 PCI_BASE_ADDRESS_0
+ j
*4,
1923 pd
->resource
[j
].start
);
1924 pci_write_config_dword(pd
, PCI_ROM_ADDRESS
,
1926 pci_write_config_word(pd
, PCI_CACHE_LINE_SIZE
,
1928 pci_write_config_word(pd
, PCI_INTERRUPT_LINE
,
1930 pci_write_config_word(pd
, PCI_COMMAND
, ps
->command
);
1938 /* N.B. This doesn't work on the 3400 */
1942 struct adb_request req
;
1944 memset(&req
, 0, sizeof(req
));
1946 for (; n
> 0; --n
) {
1953 req
.reply
[0] = ADB_RET_OK
;
1955 req
.reply_expected
= 0;
1956 pmu_polled_request(&req
);
1964 req
.reply
[0] = ADB_RET_OK
;
1966 req
.reply_expected
= 0;
1967 pmu_polled_request(&req
);
1975 * Put the powerbook to sleep.
1978 static u32 save_via
[8];
1981 save_via_state(void)
1983 save_via
[0] = in_8(&via
[ANH
]);
1984 save_via
[1] = in_8(&via
[DIRA
]);
1985 save_via
[2] = in_8(&via
[B
]);
1986 save_via
[3] = in_8(&via
[DIRB
]);
1987 save_via
[4] = in_8(&via
[PCR
]);
1988 save_via
[5] = in_8(&via
[ACR
]);
1989 save_via
[6] = in_8(&via
[T1CL
]);
1990 save_via
[7] = in_8(&via
[T1CH
]);
1993 restore_via_state(void)
1995 out_8(&via
[ANH
], save_via
[0]);
1996 out_8(&via
[DIRA
], save_via
[1]);
1997 out_8(&via
[B
], save_via
[2]);
1998 out_8(&via
[DIRB
], save_via
[3]);
1999 out_8(&via
[PCR
], save_via
[4]);
2000 out_8(&via
[ACR
], save_via
[5]);
2001 out_8(&via
[T1CL
], save_via
[6]);
2002 out_8(&via
[T1CH
], save_via
[7]);
2003 out_8(&via
[IER
], IER_CLR
| 0x7f); /* disable all intrs */
2004 out_8(&via
[IFR
], 0x7f); /* clear IFR */
2005 out_8(&via
[IER
], IER_SET
| SR_INT
| CB1_INT
);
2009 pmac_suspend_devices(void)
2013 pm_prepare_console();
2015 /* Notify old-style device drivers & userland */
2016 ret
= broadcast_sleep(PBOOK_SLEEP_REQUEST
, PBOOK_SLEEP_REJECT
);
2017 if (ret
!= PBOOK_SLEEP_OK
) {
2018 printk(KERN_ERR
"Sleep rejected by drivers\n");
2022 /* Sync the disks. */
2023 /* XXX It would be nice to have some way to ensure that
2024 * nobody is dirtying any new buffers while we wait. That
2025 * could be achieved using the refrigerator for processes
2030 /* Sleep can fail now. May not be very robust but useful for debugging */
2031 ret
= broadcast_sleep(PBOOK_SLEEP_NOW
, PBOOK_WAKE
);
2032 if (ret
!= PBOOK_SLEEP_OK
) {
2033 printk(KERN_ERR
"Driver sleep failed\n");
2037 /* Send suspend call to devices, hold the device core's dpm_sem */
2038 ret
= device_suspend(PMSG_SUSPEND
);
2041 printk(KERN_ERR
"Driver sleep failed\n");
2045 /* Call platform functions marked "on sleep" */
2046 pmac_pfunc_i2c_suspend();
2047 pmac_pfunc_base_suspend();
2049 /* Stop preemption */
2052 /* Make sure the decrementer won't interrupt us */
2053 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2054 /* Make sure any pending DEC interrupt occurring while we did
2055 * the above didn't re-enable the DEC */
2057 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2059 /* We can now disable MSR_EE. This code of course works properly only
2060 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2061 * stop the "other" CPUs way before we do all that stuff.
2063 local_irq_disable();
2065 /* Broadcast power down irq
2066 * This isn't that useful in most cases (only directly wired devices can
2067 * use this but still... This will take care of sysdev's as well, so
2068 * we exit from here with local irqs disabled and PIC off.
2070 ret
= device_power_down(PMSG_SUSPEND
);
2072 wakeup_decrementer();
2077 printk(KERN_ERR
"Driver powerdown failed\n");
2081 /* Wait for completion of async requests */
2082 while (!batt_req
.complete
)
2085 /* Giveup the lazy FPU & vec so we don't have to back them
2086 * up from the low level code
2090 #ifdef CONFIG_ALTIVEC
2091 if (cpu_has_feature(CPU_FTR_ALTIVEC
))
2092 enable_kernel_altivec();
2093 #endif /* CONFIG_ALTIVEC */
2099 pmac_wakeup_devices(void)
2103 /* Power back up system devices (including the PIC) */
2106 /* Force a poll of ADB interrupts */
2107 adb_int_pending
= 1;
2108 via_pmu_interrupt(0, NULL
, NULL
);
2110 /* Restart jiffies & scheduling */
2111 wakeup_decrementer();
2113 /* Re-enable local CPU interrupts */
2118 /* Call platform functions marked "on wake" */
2119 pmac_pfunc_base_resume();
2120 pmac_pfunc_i2c_resume();
2122 /* Resume devices */
2125 /* Notify old style drivers */
2128 pm_restore_console();
2133 #define GRACKLE_PM (1<<7)
2134 #define GRACKLE_DOZE (1<<5)
2135 #define GRACKLE_NAP (1<<4)
2136 #define GRACKLE_SLEEP (1<<3)
2138 static int powerbook_sleep_grackle(void)
2140 unsigned long save_l2cr
;
2141 unsigned short pmcr1
;
2142 struct adb_request req
;
2144 struct pci_dev
*grackle
;
2146 grackle
= pci_find_slot(0, 0);
2150 ret
= pmac_suspend_devices();
2152 printk(KERN_ERR
"Sleep rejected by devices\n");
2156 /* Turn off various things. Darwin does some retry tests here... */
2157 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL0
, PMU_POW0_OFF
|PMU_POW0_HARD_DRIVE
);
2158 pmu_wait_complete(&req
);
2159 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
2160 PMU_POW_OFF
|PMU_POW_BACKLIGHT
|PMU_POW_IRLED
|PMU_POW_MEDIABAY
);
2161 pmu_wait_complete(&req
);
2163 /* For 750, save backside cache setting and disable it */
2164 save_l2cr
= _get_L2CR(); /* (returns -1 if not available) */
2166 if (!__fake_sleep
) {
2167 /* Ask the PMU to put us to sleep */
2168 pmu_request(&req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2169 pmu_wait_complete(&req
);
2172 /* The VIA is supposed not to be restored correctly*/
2174 /* We shut down some HW */
2175 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,1);
2177 pci_read_config_word(grackle
, 0x70, &pmcr1
);
2178 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2179 pmcr1
&= ~(GRACKLE_DOZE
|GRACKLE_SLEEP
);
2180 pmcr1
|= GRACKLE_PM
|GRACKLE_NAP
;
2181 pci_write_config_word(grackle
, 0x70, pmcr1
);
2183 /* Call low-level ASM sleep handler */
2187 low_sleep_handler();
2189 /* We're awake again, stop grackle PM */
2190 pci_read_config_word(grackle
, 0x70, &pmcr1
);
2191 pmcr1
&= ~(GRACKLE_PM
|GRACKLE_DOZE
|GRACKLE_SLEEP
|GRACKLE_NAP
);
2192 pci_write_config_word(grackle
, 0x70, pmcr1
);
2194 /* Make sure the PMU is idle */
2195 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,0);
2196 restore_via_state();
2198 /* Restore L2 cache */
2199 if (save_l2cr
!= 0xffffffff && (save_l2cr
& L2CR_L2E
) != 0)
2200 _set_L2CR(save_l2cr
);
2202 /* Restore userland MMU context */
2203 set_context(current
->active_mm
->context
.id
, current
->active_mm
->pgd
);
2205 /* Power things up */
2207 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
2208 pmu_wait_complete(&req
);
2209 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL0
,
2210 PMU_POW0_ON
|PMU_POW0_HARD_DRIVE
);
2211 pmu_wait_complete(&req
);
2212 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
2213 PMU_POW_ON
|PMU_POW_BACKLIGHT
|PMU_POW_CHARGER
|PMU_POW_IRLED
|PMU_POW_MEDIABAY
);
2214 pmu_wait_complete(&req
);
2216 pmac_wakeup_devices();
2222 powerbook_sleep_Core99(void)
2224 unsigned long save_l2cr
;
2225 unsigned long save_l3cr
;
2226 struct adb_request req
;
2229 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) < 0) {
2230 printk(KERN_ERR
"Sleep mode not supported on this machine\n");
2234 if (num_online_cpus() > 1 || cpu_is_offline(0))
2237 ret
= pmac_suspend_devices();
2239 printk(KERN_ERR
"Sleep rejected by devices\n");
2243 /* Stop environment and ADB interrupts */
2244 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, 0);
2245 pmu_wait_complete(&req
);
2247 /* Tell PMU what events will wake us up */
2248 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
, PMU_PWR_CLR_WAKEUP_EVENTS
,
2250 pmu_wait_complete(&req
);
2251 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
, PMU_PWR_SET_WAKEUP_EVENTS
,
2252 0, PMU_PWR_WAKEUP_KEY
|
2253 (option_lid_wakeup
? PMU_PWR_WAKEUP_LID_OPEN
: 0));
2254 pmu_wait_complete(&req
);
2256 /* Save the state of the L2 and L3 caches */
2257 save_l3cr
= _get_L3CR(); /* (returns -1 if not available) */
2258 save_l2cr
= _get_L2CR(); /* (returns -1 if not available) */
2260 if (!__fake_sleep
) {
2261 /* Ask the PMU to put us to sleep */
2262 pmu_request(&req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2263 pmu_wait_complete(&req
);
2266 /* The VIA is supposed not to be restored correctly*/
2269 /* Shut down various ASICs. There's a chance that we can no longer
2270 * talk to the PMU after this, so I moved it to _after_ sending the
2271 * sleep command to it. Still need to be checked.
2273 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 1);
2275 /* Call low-level ASM sleep handler */
2279 low_sleep_handler();
2281 /* Restore Apple core ASICs state */
2282 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 0);
2285 restore_via_state();
2287 /* tweak LPJ before cpufreq is there */
2288 loops_per_jiffy
*= 2;
2291 pmac_call_early_video_resume();
2293 /* Restore L2 cache */
2294 if (save_l2cr
!= 0xffffffff && (save_l2cr
& L2CR_L2E
) != 0)
2295 _set_L2CR(save_l2cr
);
2296 /* Restore L3 cache */
2297 if (save_l3cr
!= 0xffffffff && (save_l3cr
& L3CR_L3E
) != 0)
2298 _set_L3CR(save_l3cr
);
2300 /* Restore userland MMU context */
2301 set_context(current
->active_mm
->context
.id
, current
->active_mm
->pgd
);
2303 /* Tell PMU we are ready */
2305 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
2306 pmu_wait_complete(&req
);
2307 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
2308 pmu_wait_complete(&req
);
2310 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2311 loops_per_jiffy
/= 2;
2313 pmac_wakeup_devices();
2318 #define PB3400_MEM_CTRL 0xf8000000
2319 #define PB3400_MEM_CTRL_SLEEP 0x70
2322 powerbook_sleep_3400(void)
2327 struct adb_request sleep_req
;
2328 void __iomem
*mem_ctrl
;
2329 unsigned int __iomem
*mem_ctrl_sleep
;
2331 /* first map in the memory controller registers */
2332 mem_ctrl
= ioremap(PB3400_MEM_CTRL
, 0x100);
2333 if (mem_ctrl
== NULL
) {
2334 printk("powerbook_sleep_3400: ioremap failed\n");
2337 mem_ctrl_sleep
= mem_ctrl
+ PB3400_MEM_CTRL_SLEEP
;
2339 /* Allocate room for PCI save */
2340 pbook_alloc_pci_save();
2342 ret
= pmac_suspend_devices();
2344 pbook_free_pci_save();
2345 printk(KERN_ERR
"Sleep rejected by devices\n");
2349 /* Save the state of PCI config space for some slots */
2352 /* Set the memory controller to keep the memory refreshed
2353 while we're asleep */
2354 for (i
= 0x403f; i
>= 0x4000; --i
) {
2355 out_be32(mem_ctrl_sleep
, i
);
2357 x
= (in_be32(mem_ctrl_sleep
) >> 16) & 0x3ff;
2363 /* Ask the PMU to put us to sleep */
2364 pmu_request(&sleep_req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2365 while (!sleep_req
.complete
)
2368 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,1);
2370 /* displacement-flush the L2 cache - necessary? */
2371 for (p
= KERNELBASE
; p
< KERNELBASE
+ 0x100000; p
+= 0x1000)
2372 i
= *(volatile int *)p
;
2375 /* Put the CPU into sleep mode */
2376 hid0
= mfspr(SPRN_HID0
);
2377 hid0
= (hid0
& ~(HID0_NAP
| HID0_DOZE
)) | HID0_SLEEP
;
2378 mtspr(SPRN_HID0
, hid0
);
2379 mtmsr(mfmsr() | MSR_POW
| MSR_EE
);
2382 /* OK, we're awake again, start restoring things */
2383 out_be32(mem_ctrl_sleep
, 0x3f);
2384 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,0);
2385 pbook_pci_restore();
2388 /* wait for the PMU interrupt sequence to complete */
2392 pmac_wakeup_devices();
2393 pbook_free_pci_save();
2399 #endif /* CONFIG_PM && CONFIG_PPC32 */
2402 * Support for /dev/pmu device
2404 #define RB_SIZE 0x10
2405 struct pmu_private
{
2406 struct list_head list
;
2411 unsigned char data
[16];
2413 wait_queue_head_t wait
;
2415 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2416 int backlight_locker
;
2417 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2420 static LIST_HEAD(all_pmu_pvt
);
2421 static DEFINE_SPINLOCK(all_pvt_lock
);
2424 pmu_pass_intr(unsigned char *data
, int len
)
2426 struct pmu_private
*pp
;
2427 struct list_head
*list
;
2429 unsigned long flags
;
2431 if (len
> sizeof(pp
->rb_buf
[0].data
))
2432 len
= sizeof(pp
->rb_buf
[0].data
);
2433 spin_lock_irqsave(&all_pvt_lock
, flags
);
2434 for (list
= &all_pmu_pvt
; (list
= list
->next
) != &all_pmu_pvt
; ) {
2435 pp
= list_entry(list
, struct pmu_private
, list
);
2436 spin_lock(&pp
->lock
);
2440 if (i
!= pp
->rb_get
) {
2441 struct rb_entry
*rp
= &pp
->rb_buf
[pp
->rb_put
];
2443 memcpy(rp
->data
, data
, len
);
2445 wake_up_interruptible(&pp
->wait
);
2447 spin_unlock(&pp
->lock
);
2449 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2453 pmu_open(struct inode
*inode
, struct file
*file
)
2455 struct pmu_private
*pp
;
2456 unsigned long flags
;
2458 pp
= kmalloc(sizeof(struct pmu_private
), GFP_KERNEL
);
2461 pp
->rb_get
= pp
->rb_put
= 0;
2462 spin_lock_init(&pp
->lock
);
2463 init_waitqueue_head(&pp
->wait
);
2464 spin_lock_irqsave(&all_pvt_lock
, flags
);
2465 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2466 pp
->backlight_locker
= 0;
2467 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2468 list_add(&pp
->list
, &all_pmu_pvt
);
2469 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2470 file
->private_data
= pp
;
2475 pmu_read(struct file
*file
, char __user
*buf
,
2476 size_t count
, loff_t
*ppos
)
2478 struct pmu_private
*pp
= file
->private_data
;
2479 DECLARE_WAITQUEUE(wait
, current
);
2480 unsigned long flags
;
2483 if (count
< 1 || pp
== 0)
2485 if (!access_ok(VERIFY_WRITE
, buf
, count
))
2488 spin_lock_irqsave(&pp
->lock
, flags
);
2489 add_wait_queue(&pp
->wait
, &wait
);
2490 current
->state
= TASK_INTERRUPTIBLE
;
2494 if (pp
->rb_get
!= pp
->rb_put
) {
2496 struct rb_entry
*rp
= &pp
->rb_buf
[i
];
2498 spin_unlock_irqrestore(&pp
->lock
, flags
);
2501 if (ret
> 0 && copy_to_user(buf
, rp
->data
, ret
))
2505 spin_lock_irqsave(&pp
->lock
, flags
);
2510 if (file
->f_flags
& O_NONBLOCK
)
2513 if (signal_pending(current
))
2515 spin_unlock_irqrestore(&pp
->lock
, flags
);
2517 spin_lock_irqsave(&pp
->lock
, flags
);
2519 current
->state
= TASK_RUNNING
;
2520 remove_wait_queue(&pp
->wait
, &wait
);
2521 spin_unlock_irqrestore(&pp
->lock
, flags
);
2527 pmu_write(struct file
*file
, const char __user
*buf
,
2528 size_t count
, loff_t
*ppos
)
2534 pmu_fpoll(struct file
*filp
, poll_table
*wait
)
2536 struct pmu_private
*pp
= filp
->private_data
;
2537 unsigned int mask
= 0;
2538 unsigned long flags
;
2542 poll_wait(filp
, &pp
->wait
, wait
);
2543 spin_lock_irqsave(&pp
->lock
, flags
);
2544 if (pp
->rb_get
!= pp
->rb_put
)
2546 spin_unlock_irqrestore(&pp
->lock
, flags
);
2551 pmu_release(struct inode
*inode
, struct file
*file
)
2553 struct pmu_private
*pp
= file
->private_data
;
2554 unsigned long flags
;
2558 file
->private_data
= NULL
;
2559 spin_lock_irqsave(&all_pvt_lock
, flags
);
2560 list_del(&pp
->list
);
2561 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2562 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2563 if (pp
->backlight_locker
) {
2564 spin_lock_irqsave(&pmu_lock
, flags
);
2565 disable_kernel_backlight
--;
2566 spin_unlock_irqrestore(&pmu_lock
, flags
);
2568 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2576 pmu_ioctl(struct inode
* inode
, struct file
*filp
,
2577 u_int cmd
, u_long arg
)
2579 __u32 __user
*argp
= (__u32 __user
*)arg
;
2580 int error
= -EINVAL
;
2583 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2585 if (!capable(CAP_SYS_ADMIN
))
2587 if (sleep_in_progress
)
2589 sleep_in_progress
= 1;
2591 case PMU_OHARE_BASED
:
2592 error
= powerbook_sleep_3400();
2594 case PMU_HEATHROW_BASED
:
2595 case PMU_PADDINGTON_BASED
:
2596 error
= powerbook_sleep_grackle();
2598 case PMU_KEYLARGO_BASED
:
2599 error
= powerbook_sleep_Core99();
2604 sleep_in_progress
= 0;
2606 case PMU_IOC_CAN_SLEEP
:
2607 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) < 0)
2608 return put_user(0, argp
);
2610 return put_user(1, argp
);
2611 #endif /* CONFIG_PM && CONFIG_PPC32 */
2613 #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2614 /* Compatibility ioctl's for backlight */
2615 case PMU_IOC_GET_BACKLIGHT
:
2619 if (sleep_in_progress
)
2622 brightness
= pmac_backlight_get_legacy_brightness();
2626 return put_user(brightness
, argp
);
2629 case PMU_IOC_SET_BACKLIGHT
:
2633 if (sleep_in_progress
)
2636 error
= get_user(brightness
, argp
);
2640 return pmac_backlight_set_legacy_brightness(brightness
);
2642 #ifdef CONFIG_INPUT_ADBHID
2643 case PMU_IOC_GRAB_BACKLIGHT
: {
2644 struct pmu_private
*pp
= filp
->private_data
;
2645 unsigned long flags
;
2647 if (pp
->backlight_locker
)
2649 pp
->backlight_locker
= 1;
2650 spin_lock_irqsave(&pmu_lock
, flags
);
2651 disable_kernel_backlight
++;
2652 spin_unlock_irqrestore(&pmu_lock
, flags
);
2655 #endif /* CONFIG_INPUT_ADBHID */
2656 #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
2657 case PMU_IOC_GET_MODEL
:
2658 return put_user(pmu_kind
, argp
);
2659 case PMU_IOC_HAS_ADB
:
2660 return put_user(pmu_has_adb
, argp
);
2665 static struct file_operations pmu_device_fops
= {
2671 .release
= pmu_release
,
2674 static struct miscdevice pmu_device
= {
2675 PMU_MINOR
, "pmu", &pmu_device_fops
2678 static int pmu_device_init(void)
2682 if (misc_register(&pmu_device
) < 0)
2683 printk(KERN_ERR
"via-pmu: cannot register misc device.\n");
2686 device_initcall(pmu_device_init
);
2691 polled_handshake(volatile unsigned char __iomem
*via
)
2693 via
[B
] &= ~TREQ
; eieio();
2694 while ((via
[B
] & TACK
) != 0)
2696 via
[B
] |= TREQ
; eieio();
2697 while ((via
[B
] & TACK
) == 0)
2702 polled_send_byte(volatile unsigned char __iomem
*via
, int x
)
2704 via
[ACR
] |= SR_OUT
| SR_EXT
; eieio();
2705 via
[SR
] = x
; eieio();
2706 polled_handshake(via
);
2710 polled_recv_byte(volatile unsigned char __iomem
*via
)
2714 via
[ACR
] = (via
[ACR
] & ~SR_OUT
) | SR_EXT
; eieio();
2715 x
= via
[SR
]; eieio();
2716 polled_handshake(via
);
2717 x
= via
[SR
]; eieio();
2722 pmu_polled_request(struct adb_request
*req
)
2724 unsigned long flags
;
2726 volatile unsigned char __iomem
*v
= via
;
2730 l
= pmu_data_len
[c
][0];
2731 if (l
>= 0 && req
->nbytes
!= l
+ 1)
2734 local_irq_save(flags
);
2735 while (pmu_state
!= idle
)
2738 while ((via
[B
] & TACK
) == 0)
2740 polled_send_byte(v
, c
);
2742 l
= req
->nbytes
- 1;
2743 polled_send_byte(v
, l
);
2745 for (i
= 1; i
<= l
; ++i
)
2746 polled_send_byte(v
, req
->data
[i
]);
2748 l
= pmu_data_len
[c
][1];
2750 l
= polled_recv_byte(v
);
2751 for (i
= 0; i
< l
; ++i
)
2752 req
->reply
[i
+ req
->reply_len
] = polled_recv_byte(v
);
2757 local_irq_restore(flags
);
2760 #endif /* DEBUG_SLEEP */
2763 /* FIXME: This is a temporary set of callbacks to enable us
2764 * to do suspend-to-disk.
2767 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2769 static int pmu_sys_suspended
= 0;
2771 static int pmu_sys_suspend(struct sys_device
*sysdev
, pm_message_t state
)
2773 if (state
.event
!= PM_EVENT_SUSPEND
|| pmu_sys_suspended
)
2776 /* Suspend PMU event interrupts */
2779 pmu_sys_suspended
= 1;
2783 static int pmu_sys_resume(struct sys_device
*sysdev
)
2785 struct adb_request req
;
2787 if (!pmu_sys_suspended
)
2790 /* Tell PMU we are ready */
2791 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
2792 pmu_wait_complete(&req
);
2794 /* Resume PMU event interrupts */
2797 pmu_sys_suspended
= 0;
2802 #endif /* CONFIG_PM && CONFIG_PPC32 */
2804 static struct sysdev_class pmu_sysclass
= {
2805 set_kset_name("pmu"),
2808 static struct sys_device device_pmu
= {
2810 .cls
= &pmu_sysclass
,
2813 static struct sysdev_driver driver_pmu
= {
2814 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2815 .suspend
= &pmu_sys_suspend
,
2816 .resume
= &pmu_sys_resume
,
2817 #endif /* CONFIG_PM && CONFIG_PPC32 */
2820 static int __init
init_pmu_sysfs(void)
2824 rc
= sysdev_class_register(&pmu_sysclass
);
2826 printk(KERN_ERR
"Failed registering PMU sys class\n");
2829 rc
= sysdev_register(&device_pmu
);
2831 printk(KERN_ERR
"Failed registering PMU sys device\n");
2834 rc
= sysdev_driver_register(&pmu_sysclass
, &driver_pmu
);
2836 printk(KERN_ERR
"Failed registering PMU sys driver\n");
2842 subsys_initcall(init_pmu_sysfs
);
2844 EXPORT_SYMBOL(pmu_request
);
2845 EXPORT_SYMBOL(pmu_queue_request
);
2846 EXPORT_SYMBOL(pmu_poll
);
2847 EXPORT_SYMBOL(pmu_poll_adb
);
2848 EXPORT_SYMBOL(pmu_wait_complete
);
2849 EXPORT_SYMBOL(pmu_suspend
);
2850 EXPORT_SYMBOL(pmu_resume
);
2851 EXPORT_SYMBOL(pmu_unlock
);
2852 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2853 EXPORT_SYMBOL(pmu_enable_irled
);
2854 EXPORT_SYMBOL(pmu_battery_count
);
2855 EXPORT_SYMBOL(pmu_batteries
);
2856 EXPORT_SYMBOL(pmu_power_flags
);
2857 #endif /* CONFIG_PM && CONFIG_PPC32 */