2 * Device driver for the via-pmu on Apple Powermacs.
4 * The VIA (versatile interface adapter) interfaces to the PMU,
5 * a 6805 microprocessor core whose primary function is to control
6 * battery charging and system power on the PowerBook 3400 and 2400.
7 * The PMU also controls the ADB (Apple Desktop Bus) which connects
8 * to the keyboard and mouse, as well as the non-volatile RAM
9 * and the RTC (real time clock) chip.
11 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
12 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
14 * THIS DRIVER IS BECOMING A TOTAL MESS !
15 * - Cleanup atomically disabling reply to PMU events after
16 * a sleep or a freq. switch
17 * - Move sleep code out of here to pmac_pm, merge into new
18 * common PM infrastructure
19 * - Move backlight code out as well
20 * - Save/Restore PCI space properly
24 #include <linux/config.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/miscdevice.h>
31 #include <linux/blkdev.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/adb.h>
36 #include <linux/pmu.h>
37 #include <linux/cuda.h>
38 #include <linux/smp_lock.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
42 #include <linux/proc_fs.h>
43 #include <linux/init.h>
44 #include <linux/interrupt.h>
45 #include <linux/device.h>
46 #include <linux/sysdev.h>
47 #include <linux/suspend.h>
48 #include <linux/syscalls.h>
49 #include <linux/cpu.h>
51 #include <asm/machdep.h>
53 #include <asm/pgtable.h>
54 #include <asm/system.h>
55 #include <asm/sections.h>
57 #include <asm/pmac_feature.h>
58 #include <asm/pmac_pfunc.h>
59 #include <asm/pmac_low_i2c.h>
60 #include <asm/uaccess.h>
61 #include <asm/mmu_context.h>
62 #include <asm/cputable.h>
64 #ifdef CONFIG_PMAC_BACKLIGHT
65 #include <asm/backlight.h>
69 #include <asm/open_pic.h>
72 /* Some compile options */
73 #undef SUSPEND_USES_PMU
75 #undef HACKED_PCI_SAVE
77 /* Misc minor number allocated for /dev/pmu */
80 /* How many iterations between battery polls */
81 #define BATTERY_POLLING_COUNT 2
83 static volatile unsigned char __iomem
*via
;
85 /* VIA registers - spaced 0x200 bytes apart */
86 #define RS 0x200 /* skip between registers */
87 #define B 0 /* B-side data */
88 #define A RS /* A-side data */
89 #define DIRB (2*RS) /* B-side direction (1=output) */
90 #define DIRA (3*RS) /* A-side direction (1=output) */
91 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
92 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
93 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
94 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
95 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
96 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
97 #define SR (10*RS) /* Shift register */
98 #define ACR (11*RS) /* Auxiliary control register */
99 #define PCR (12*RS) /* Peripheral control register */
100 #define IFR (13*RS) /* Interrupt flag register */
101 #define IER (14*RS) /* Interrupt enable register */
102 #define ANH (15*RS) /* A-side data, no handshake */
104 /* Bits in B data register: both active low */
105 #define TACK 0x08 /* Transfer acknowledge (input) */
106 #define TREQ 0x10 /* Transfer request (output) */
109 #define SR_CTRL 0x1c /* Shift register control bits */
110 #define SR_EXT 0x0c /* Shift on external clock */
111 #define SR_OUT 0x10 /* Shift out if 1 */
113 /* Bits in IFR and IER */
114 #define IER_SET 0x80 /* set bits in IER */
115 #define IER_CLR 0 /* clear bits in IER */
116 #define SR_INT 0x04 /* Shift register full/empty */
118 #define CB1_INT 0x10 /* transition on CB1 input */
120 static volatile enum pmu_state
{
129 static volatile enum int_data_state
{
134 } int_data_state
[2] = { int_data_empty
, int_data_empty
};
136 static struct adb_request
*current_req
;
137 static struct adb_request
*last_req
;
138 static struct adb_request
*req_awaiting_reply
;
139 static unsigned char interrupt_data
[2][32];
140 static int interrupt_data_len
[2];
141 static int int_data_last
;
142 static unsigned char *reply_ptr
;
143 static int data_index
;
145 static volatile int adb_int_pending
;
146 static volatile int disable_poll
;
147 static struct adb_request bright_req_1
, bright_req_2
;
148 static struct device_node
*vias
;
149 static int pmu_kind
= PMU_UNKNOWN
;
150 static int pmu_fully_inited
= 0;
151 static int pmu_has_adb
;
152 static struct device_node
*gpio_node
;
153 static unsigned char __iomem
*gpio_reg
= NULL
;
154 static int gpio_irq
= -1;
155 static int gpio_irq_enabled
= -1;
156 static volatile int pmu_suspended
= 0;
157 static spinlock_t pmu_lock
;
158 static u8 pmu_intr_mask
;
159 static int pmu_version
;
160 static int drop_interrupts
;
161 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
162 static int option_lid_wakeup
= 1;
163 #endif /* CONFIG_PM && CONFIG_PPC32 */
164 static int sleep_in_progress
;
165 static unsigned long async_req_locks
;
166 static unsigned int pmu_irq_stats
[11];
168 static struct proc_dir_entry
*proc_pmu_root
;
169 static struct proc_dir_entry
*proc_pmu_info
;
170 static struct proc_dir_entry
*proc_pmu_irqstats
;
171 static struct proc_dir_entry
*proc_pmu_options
;
172 static int option_server_mode
;
174 int pmu_battery_count
;
176 unsigned int pmu_power_flags
;
177 struct pmu_battery_info pmu_batteries
[PMU_MAX_BATTERIES
];
178 static int query_batt_timer
= BATTERY_POLLING_COUNT
;
179 static struct adb_request batt_req
;
180 static struct proc_dir_entry
*proc_pmu_batt
[PMU_MAX_BATTERIES
];
182 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
183 extern int disable_kernel_backlight
;
184 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
188 struct notifier_block
*sleep_notifier_list
;
191 static int adb_dev_map
= 0;
192 static int pmu_adb_flags
;
194 static int pmu_probe(void);
195 static int pmu_init(void);
196 static int pmu_send_request(struct adb_request
*req
, int sync
);
197 static int pmu_adb_autopoll(int devs
);
198 static int pmu_adb_reset_bus(void);
199 #endif /* CONFIG_ADB */
201 static int init_pmu(void);
202 static void pmu_start(void);
203 static irqreturn_t
via_pmu_interrupt(int irq
, void *arg
, struct pt_regs
*regs
);
204 static irqreturn_t
gpio1_interrupt(int irq
, void *arg
, struct pt_regs
*regs
);
205 static int proc_get_info(char *page
, char **start
, off_t off
,
206 int count
, int *eof
, void *data
);
207 static int proc_get_irqstats(char *page
, char **start
, off_t off
,
208 int count
, int *eof
, void *data
);
209 #ifdef CONFIG_PMAC_BACKLIGHT
210 static int pmu_set_backlight_level(int level
, void* data
);
211 static int pmu_set_backlight_enable(int on
, int level
, void* data
);
212 #endif /* CONFIG_PMAC_BACKLIGHT */
213 static void pmu_pass_intr(unsigned char *data
, int len
);
214 static int proc_get_batt(char *page
, char **start
, off_t off
,
215 int count
, int *eof
, void *data
);
216 static int proc_read_options(char *page
, char **start
, off_t off
,
217 int count
, int *eof
, void *data
);
218 static int proc_write_options(struct file
*file
, const char __user
*buffer
,
219 unsigned long count
, void *data
);
222 struct adb_driver via_pmu_driver
= {
231 #endif /* CONFIG_ADB */
233 extern void low_sleep_handler(void);
234 extern void enable_kernel_altivec(void);
235 extern void enable_kernel_fp(void);
238 int pmu_polled_request(struct adb_request
*req
);
239 int pmu_wink(struct adb_request
*req
);
243 * This table indicates for each PMU opcode:
244 * - the number of data bytes to be sent with the command, or -1
245 * if a length byte should be sent,
246 * - the number of response bytes which the PMU will return, or
247 * -1 if it will send a length byte.
249 static const s8 pmu_data_len
[256][2] = {
250 /* 0 1 2 3 4 5 6 7 */
251 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
253 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
255 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
257 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
259 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
260 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
261 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
262 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
263 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
264 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
265 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
267 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
268 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
269 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
270 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
272 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
273 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
274 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
275 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
276 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
277 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
278 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
279 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
280 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
281 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
282 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
285 static char *pbook_type
[] = {
287 "PowerBook 2400/3400/3500(G3)",
288 "PowerBook G3 Series",
293 #ifdef CONFIG_PMAC_BACKLIGHT
294 static struct backlight_controller pmu_backlight_controller
= {
295 pmu_set_backlight_enable
,
296 pmu_set_backlight_level
298 #endif /* CONFIG_PMAC_BACKLIGHT */
300 int __init
find_via_pmu(void)
307 vias
= of_find_node_by_name(NULL
, "via-pmu");
311 reg
= (u32
*)get_property(vias
, "reg", NULL
);
313 printk(KERN_ERR
"via-pmu: No \"reg\" property !\n");
316 taddr
= of_translate_address(vias
, reg
);
317 if (taddr
== OF_BAD_ADDR
) {
318 printk(KERN_ERR
"via-pmu: Can't translate address !\n");
322 spin_lock_init(&pmu_lock
);
326 pmu_intr_mask
= PMU_INT_PCEJECT
|
331 if (vias
->parent
->name
&& ((strcmp(vias
->parent
->name
, "ohare") == 0)
332 || device_is_compatible(vias
->parent
, "ohare")))
333 pmu_kind
= PMU_OHARE_BASED
;
334 else if (device_is_compatible(vias
->parent
, "paddington"))
335 pmu_kind
= PMU_PADDINGTON_BASED
;
336 else if (device_is_compatible(vias
->parent
, "heathrow"))
337 pmu_kind
= PMU_HEATHROW_BASED
;
338 else if (device_is_compatible(vias
->parent
, "Keylargo")
339 || device_is_compatible(vias
->parent
, "K2-Keylargo")) {
340 struct device_node
*gpiop
;
341 u64 gaddr
= OF_BAD_ADDR
;
343 pmu_kind
= PMU_KEYLARGO_BASED
;
344 pmu_has_adb
= (find_type_devices("adb") != NULL
);
345 pmu_intr_mask
= PMU_INT_PCEJECT
|
351 gpiop
= of_find_node_by_name(NULL
, "gpio");
353 reg
= (u32
*)get_property(gpiop
, "reg", NULL
);
355 gaddr
= of_translate_address(gpiop
, reg
);
356 if (gaddr
!= OF_BAD_ADDR
)
357 gpio_reg
= ioremap(gaddr
, 0x10);
359 if (gpio_reg
== NULL
)
360 printk(KERN_ERR
"via-pmu: Can't find GPIO reg !\n");
362 pmu_kind
= PMU_UNKNOWN
;
364 via
= ioremap(taddr
, 0x2000);
366 printk(KERN_ERR
"via-pmu: Can't map address !\n");
370 out_8(&via
[IER
], IER_CLR
| 0x7f); /* disable all intrs */
371 out_8(&via
[IFR
], 0x7f); /* clear IFR */
380 printk(KERN_INFO
"PMU driver v%d initialized for %s, firmware: %02x\n",
381 PMU_DRIVER_VERSION
, pbook_type
[pmu_kind
], pmu_version
);
383 sys_ctrler
= SYS_CTRLER_PMU
;
393 static int pmu_probe(void)
395 return vias
== NULL
? -ENODEV
: 0;
398 static int __init
pmu_init(void)
404 #endif /* CONFIG_ADB */
407 * We can't wait until pmu_init gets called, that happens too late.
408 * It happens after IDE and SCSI initialization, which can take a few
409 * seconds, and by that time the PMU could have given up on us and
411 * Thus this is called with arch_initcall rather than device_initcall.
413 static int __init
via_pmu_start(void)
418 bright_req_1
.complete
= 1;
419 bright_req_2
.complete
= 1;
420 batt_req
.complete
= 1;
422 #ifndef CONFIG_PPC_MERGE
423 if (pmu_kind
== PMU_KEYLARGO_BASED
)
424 openpic_set_irq_priority(vias
->intrs
[0].line
,
425 OPENPIC_PRIORITY_DEFAULT
+ 1);
428 if (request_irq(vias
->intrs
[0].line
, via_pmu_interrupt
, 0, "VIA-PMU",
430 printk(KERN_ERR
"VIA-PMU: can't get irq %d\n",
431 vias
->intrs
[0].line
);
435 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
436 gpio_node
= of_find_node_by_name(NULL
, "extint-gpio1");
437 if (gpio_node
== NULL
)
438 gpio_node
= of_find_node_by_name(NULL
,
440 if (gpio_node
&& gpio_node
->n_intrs
> 0)
441 gpio_irq
= gpio_node
->intrs
[0].line
;
443 if (gpio_irq
!= -1) {
444 if (request_irq(gpio_irq
, gpio1_interrupt
, 0,
445 "GPIO1 ADB", (void *)0))
446 printk(KERN_ERR
"pmu: can't get irq %d"
447 " (GPIO1)\n", gpio_irq
);
449 gpio_irq_enabled
= 1;
453 /* Enable interrupts */
454 out_8(&via
[IER
], IER_SET
| SR_INT
| CB1_INT
);
456 pmu_fully_inited
= 1;
458 /* Make sure PMU settle down before continuing. This is _very_ important
459 * since the IDE probe may shut interrupts down for quite a bit of time. If
460 * a PMU communication is pending while this happens, the PMU may timeout
461 * Not that on Core99 machines, the PMU keeps sending us environement
462 * messages, we should find a way to either fix IDE or make it call
463 * pmu_suspend() before masking interrupts. This can also happens while
464 * scolling with some fbdevs.
468 } while (pmu_state
!= idle
);
473 arch_initcall(via_pmu_start
);
476 * This has to be done after pci_init, which is a subsys_initcall.
478 static int __init
via_pmu_dev_init(void)
483 #ifdef CONFIG_PMAC_BACKLIGHT
484 /* Enable backlight */
485 register_backlight_controller(&pmu_backlight_controller
, NULL
, "pmu");
486 #endif /* CONFIG_PMAC_BACKLIGHT */
489 if (machine_is_compatible("AAPL,3400/2400") ||
490 machine_is_compatible("AAPL,3500")) {
491 int mb
= pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
492 NULL
, PMAC_MB_INFO_MODEL
, 0);
493 pmu_battery_count
= 1;
494 if (mb
== PMAC_TYPE_COMET
)
495 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_COMET
;
497 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_HOOPER
;
498 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
499 machine_is_compatible("PowerBook1,1")) {
500 pmu_battery_count
= 2;
501 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_SMART
;
502 pmu_batteries
[1].flags
|= PMU_BATT_TYPE_SMART
;
504 struct device_node
* prim
= find_devices("power-mgt");
505 u32
*prim_info
= NULL
;
507 prim_info
= (u32
*)get_property(prim
, "prim-info", NULL
);
509 /* Other stuffs here yet unknown */
510 pmu_battery_count
= (prim_info
[6] >> 16) & 0xff;
511 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_SMART
;
512 if (pmu_battery_count
> 1)
513 pmu_batteries
[1].flags
|= PMU_BATT_TYPE_SMART
;
516 #endif /* CONFIG_PPC32 */
518 /* Create /proc/pmu */
519 proc_pmu_root
= proc_mkdir("pmu", NULL
);
523 for (i
=0; i
<pmu_battery_count
; i
++) {
525 sprintf(title
, "battery_%ld", i
);
526 proc_pmu_batt
[i
] = create_proc_read_entry(title
, 0, proc_pmu_root
,
527 proc_get_batt
, (void *)i
);
530 proc_pmu_info
= create_proc_read_entry("info", 0, proc_pmu_root
,
531 proc_get_info
, NULL
);
532 proc_pmu_irqstats
= create_proc_read_entry("interrupts", 0, proc_pmu_root
,
533 proc_get_irqstats
, NULL
);
534 proc_pmu_options
= create_proc_entry("options", 0600, proc_pmu_root
);
535 if (proc_pmu_options
) {
536 proc_pmu_options
->nlink
= 1;
537 proc_pmu_options
->read_proc
= proc_read_options
;
538 proc_pmu_options
->write_proc
= proc_write_options
;
544 device_initcall(via_pmu_dev_init
);
550 struct adb_request req
;
552 out_8(&via
[B
], via
[B
] | TREQ
); /* negate TREQ */
553 out_8(&via
[DIRB
], (via
[DIRB
] | TREQ
) & ~TACK
); /* TACK in, TREQ out */
555 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
557 while (!req
.complete
) {
559 printk(KERN_ERR
"init_pmu: no response from PMU\n");
566 /* ack all pending interrupts */
568 interrupt_data
[0][0] = 1;
569 while (interrupt_data
[0][0] || pmu_state
!= idle
) {
571 printk(KERN_ERR
"init_pmu: timed out acking intrs\n");
574 if (pmu_state
== idle
)
576 via_pmu_interrupt(0, NULL
, NULL
);
580 /* Tell PMU we are ready. */
581 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
582 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
583 while (!req
.complete
)
587 /* Read PMU version */
588 pmu_request(&req
, NULL
, 1, PMU_GET_VERSION
);
589 pmu_wait_complete(&req
);
590 if (req
.reply_len
> 0)
591 pmu_version
= req
.reply
[0];
593 /* Read server mode setting */
594 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
595 pmu_request(&req
, NULL
, 2, PMU_POWER_EVENTS
,
596 PMU_PWR_GET_POWERUP_EVENTS
);
597 pmu_wait_complete(&req
);
598 if (req
.reply_len
== 2) {
599 if (req
.reply
[1] & PMU_PWR_WAKEUP_AC_INSERT
)
600 option_server_mode
= 1;
601 printk(KERN_INFO
"via-pmu: Server Mode is %s\n",
602 option_server_mode
? "enabled" : "disabled");
614 static void pmu_set_server_mode(int server_mode
)
616 struct adb_request req
;
618 if (pmu_kind
!= PMU_KEYLARGO_BASED
)
621 option_server_mode
= server_mode
;
622 pmu_request(&req
, NULL
, 2, PMU_POWER_EVENTS
, PMU_PWR_GET_POWERUP_EVENTS
);
623 pmu_wait_complete(&req
);
624 if (req
.reply_len
< 2)
627 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
,
628 PMU_PWR_SET_POWERUP_EVENTS
,
629 req
.reply
[0], PMU_PWR_WAKEUP_AC_INSERT
);
631 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
,
632 PMU_PWR_CLR_POWERUP_EVENTS
,
633 req
.reply
[0], PMU_PWR_WAKEUP_AC_INSERT
);
634 pmu_wait_complete(&req
);
637 /* This new version of the code for 2400/3400/3500 powerbooks
638 * is inspired from the implementation in gkrellm-pmu
641 done_battery_state_ohare(struct adb_request
* req
)
645 * 0x01 : AC indicator
647 * 0x04 : battery exist
650 * 0x20 : full charged
651 * 0x40 : pcharge reset
652 * 0x80 : battery exist
654 * [1][2] : battery voltage
655 * [3] : CPU temperature
656 * [4] : battery temperature
661 unsigned int bat_flags
= PMU_BATT_TYPE_HOOPER
;
662 long pcharge
, charge
, vb
, vmax
, lmax
;
663 long vmax_charging
, vmax_charged
;
664 long amperage
, voltage
, time
, max
;
665 int mb
= pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
666 NULL
, PMAC_MB_INFO_MODEL
, 0);
668 if (req
->reply
[0] & 0x01)
669 pmu_power_flags
|= PMU_PWR_AC_PRESENT
;
671 pmu_power_flags
&= ~PMU_PWR_AC_PRESENT
;
673 if (mb
== PMAC_TYPE_COMET
) {
684 /* If battery installed */
685 if (req
->reply
[0] & 0x04) {
686 bat_flags
|= PMU_BATT_PRESENT
;
687 if (req
->reply
[0] & 0x02)
688 bat_flags
|= PMU_BATT_CHARGING
;
689 vb
= (req
->reply
[1] << 8) | req
->reply
[2];
690 voltage
= (vb
* 265 + 72665) / 10;
691 amperage
= req
->reply
[5];
692 if ((req
->reply
[0] & 0x01) == 0) {
694 vb
+= ((amperage
- 200) * 15)/100;
695 } else if (req
->reply
[0] & 0x02) {
696 vb
= (vb
* 97) / 100;
697 vmax
= vmax_charging
;
699 charge
= (100 * vb
) / vmax
;
700 if (req
->reply
[0] & 0x40) {
701 pcharge
= (req
->reply
[6] << 8) + req
->reply
[7];
705 pcharge
= 100 - pcharge
/ lmax
;
706 if (pcharge
< charge
)
710 time
= (charge
* 16440) / amperage
;
714 amperage
= -amperage
;
716 charge
= max
= amperage
= voltage
= time
= 0;
718 pmu_batteries
[pmu_cur_battery
].flags
= bat_flags
;
719 pmu_batteries
[pmu_cur_battery
].charge
= charge
;
720 pmu_batteries
[pmu_cur_battery
].max_charge
= max
;
721 pmu_batteries
[pmu_cur_battery
].amperage
= amperage
;
722 pmu_batteries
[pmu_cur_battery
].voltage
= voltage
;
723 pmu_batteries
[pmu_cur_battery
].time_remaining
= time
;
725 clear_bit(0, &async_req_locks
);
729 done_battery_state_smart(struct adb_request
* req
)
732 * [0] : format of this structure (known: 3,4,5)
745 * [4][5] : max charge
750 unsigned int bat_flags
= PMU_BATT_TYPE_SMART
;
752 unsigned int capa
, max
, voltage
;
754 if (req
->reply
[1] & 0x01)
755 pmu_power_flags
|= PMU_PWR_AC_PRESENT
;
757 pmu_power_flags
&= ~PMU_PWR_AC_PRESENT
;
760 capa
= max
= amperage
= voltage
= 0;
762 if (req
->reply
[1] & 0x04) {
763 bat_flags
|= PMU_BATT_PRESENT
;
764 switch(req
->reply
[0]) {
766 case 4: capa
= req
->reply
[2];
768 amperage
= *((signed char *)&req
->reply
[4]);
769 voltage
= req
->reply
[5];
771 case 5: capa
= (req
->reply
[2] << 8) | req
->reply
[3];
772 max
= (req
->reply
[4] << 8) | req
->reply
[5];
773 amperage
= *((signed short *)&req
->reply
[6]);
774 voltage
= (req
->reply
[8] << 8) | req
->reply
[9];
777 printk(KERN_WARNING
"pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
778 req
->reply_len
, req
->reply
[0], req
->reply
[1], req
->reply
[2], req
->reply
[3]);
783 if ((req
->reply
[1] & 0x01) && (amperage
> 0))
784 bat_flags
|= PMU_BATT_CHARGING
;
786 pmu_batteries
[pmu_cur_battery
].flags
= bat_flags
;
787 pmu_batteries
[pmu_cur_battery
].charge
= capa
;
788 pmu_batteries
[pmu_cur_battery
].max_charge
= max
;
789 pmu_batteries
[pmu_cur_battery
].amperage
= amperage
;
790 pmu_batteries
[pmu_cur_battery
].voltage
= voltage
;
792 if ((req
->reply
[1] & 0x01) && (amperage
> 0))
793 pmu_batteries
[pmu_cur_battery
].time_remaining
794 = ((max
-capa
) * 3600) / amperage
;
796 pmu_batteries
[pmu_cur_battery
].time_remaining
797 = (capa
* 3600) / (-amperage
);
799 pmu_batteries
[pmu_cur_battery
].time_remaining
= 0;
801 pmu_cur_battery
= (pmu_cur_battery
+ 1) % pmu_battery_count
;
803 clear_bit(0, &async_req_locks
);
807 query_battery_state(void)
809 if (test_and_set_bit(0, &async_req_locks
))
811 if (pmu_kind
== PMU_OHARE_BASED
)
812 pmu_request(&batt_req
, done_battery_state_ohare
,
813 1, PMU_BATTERY_STATE
);
815 pmu_request(&batt_req
, done_battery_state_smart
,
816 2, PMU_SMART_BATTERY_STATE
, pmu_cur_battery
+1);
820 proc_get_info(char *page
, char **start
, off_t off
,
821 int count
, int *eof
, void *data
)
825 p
+= sprintf(p
, "PMU driver version : %d\n", PMU_DRIVER_VERSION
);
826 p
+= sprintf(p
, "PMU firmware version : %02x\n", pmu_version
);
827 p
+= sprintf(p
, "AC Power : %d\n",
828 ((pmu_power_flags
& PMU_PWR_AC_PRESENT
) != 0) || pmu_battery_count
== 0);
829 p
+= sprintf(p
, "Battery count : %d\n", pmu_battery_count
);
835 proc_get_irqstats(char *page
, char **start
, off_t off
,
836 int count
, int *eof
, void *data
)
840 static const char *irq_names
[] = {
841 "Total CB1 triggered events",
842 "Total GPIO1 triggered events",
843 "PC-Card eject button",
844 "Sound/Brightness button",
846 "Battery state change",
847 "Environment interrupt",
849 "Ghost interrupt (zero len)",
850 "Empty interrupt (empty mask)",
854 for (i
=0; i
<11; i
++) {
855 p
+= sprintf(p
, " %2u: %10u (%s)\n",
856 i
, pmu_irq_stats
[i
], irq_names
[i
]);
862 proc_get_batt(char *page
, char **start
, off_t off
,
863 int count
, int *eof
, void *data
)
865 long batnum
= (long)data
;
868 p
+= sprintf(p
, "\n");
869 p
+= sprintf(p
, "flags : %08x\n",
870 pmu_batteries
[batnum
].flags
);
871 p
+= sprintf(p
, "charge : %d\n",
872 pmu_batteries
[batnum
].charge
);
873 p
+= sprintf(p
, "max_charge : %d\n",
874 pmu_batteries
[batnum
].max_charge
);
875 p
+= sprintf(p
, "current : %d\n",
876 pmu_batteries
[batnum
].amperage
);
877 p
+= sprintf(p
, "voltage : %d\n",
878 pmu_batteries
[batnum
].voltage
);
879 p
+= sprintf(p
, "time rem. : %d\n",
880 pmu_batteries
[batnum
].time_remaining
);
886 proc_read_options(char *page
, char **start
, off_t off
,
887 int count
, int *eof
, void *data
)
891 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
892 if (pmu_kind
== PMU_KEYLARGO_BASED
&&
893 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) >= 0)
894 p
+= sprintf(p
, "lid_wakeup=%d\n", option_lid_wakeup
);
896 if (pmu_kind
== PMU_KEYLARGO_BASED
)
897 p
+= sprintf(p
, "server_mode=%d\n", option_server_mode
);
903 proc_write_options(struct file
*file
, const char __user
*buffer
,
904 unsigned long count
, void *data
)
908 unsigned long fcount
= count
;
914 if (copy_from_user(tmp
, buffer
, count
))
922 while(*val
&& (*val
!= '=')) {
932 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
933 if (pmu_kind
== PMU_KEYLARGO_BASED
&&
934 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) >= 0)
935 if (!strcmp(label
, "lid_wakeup"))
936 option_lid_wakeup
= ((*val
) == '1');
938 if (pmu_kind
== PMU_KEYLARGO_BASED
&& !strcmp(label
, "server_mode")) {
940 new_value
= ((*val
) == '1');
941 if (new_value
!= option_server_mode
)
942 pmu_set_server_mode(new_value
);
948 /* Send an ADB command */
950 pmu_send_request(struct adb_request
*req
, int sync
)
954 if ((vias
== NULL
) || (!pmu_fully_inited
)) {
961 switch (req
->data
[0]) {
963 for (i
= 0; i
< req
->nbytes
- 1; ++i
)
964 req
->data
[i
] = req
->data
[i
+1];
966 if (pmu_data_len
[req
->data
[0]][1] != 0) {
967 req
->reply
[0] = ADB_RET_OK
;
971 ret
= pmu_queue_request(req
);
974 switch (req
->data
[1]) {
976 if (req
->nbytes
!= 2)
978 req
->data
[0] = PMU_READ_RTC
;
981 req
->reply
[0] = CUDA_PACKET
;
983 req
->reply
[2] = CUDA_GET_TIME
;
984 ret
= pmu_queue_request(req
);
987 if (req
->nbytes
!= 6)
989 req
->data
[0] = PMU_SET_RTC
;
991 for (i
= 1; i
<= 4; ++i
)
992 req
->data
[i
] = req
->data
[i
+1];
994 req
->reply
[0] = CUDA_PACKET
;
996 req
->reply
[2] = CUDA_SET_TIME
;
997 ret
= pmu_queue_request(req
);
1004 for (i
= req
->nbytes
- 1; i
> 1; --i
)
1005 req
->data
[i
+2] = req
->data
[i
];
1006 req
->data
[3] = req
->nbytes
- 2;
1007 req
->data
[2] = pmu_adb_flags
;
1008 /*req->data[1] = req->data[1];*/
1009 req
->data
[0] = PMU_ADB_CMD
;
1011 req
->reply_expected
= 1;
1013 ret
= pmu_queue_request(req
);
1022 while (!req
->complete
)
1028 /* Enable/disable autopolling */
1030 pmu_adb_autopoll(int devs
)
1032 struct adb_request req
;
1034 if ((vias
== NULL
) || (!pmu_fully_inited
) || !pmu_has_adb
)
1039 pmu_request(&req
, NULL
, 5, PMU_ADB_CMD
, 0, 0x86,
1040 adb_dev_map
>> 8, adb_dev_map
);
1043 pmu_request(&req
, NULL
, 1, PMU_ADB_POLL_OFF
);
1046 while (!req
.complete
)
1051 /* Reset the ADB bus */
1053 pmu_adb_reset_bus(void)
1055 struct adb_request req
;
1056 int save_autopoll
= adb_dev_map
;
1058 if ((vias
== NULL
) || (!pmu_fully_inited
) || !pmu_has_adb
)
1061 /* anyone got a better idea?? */
1062 pmu_adb_autopoll(0);
1066 req
.data
[0] = PMU_ADB_CMD
;
1068 req
.data
[2] = ADB_BUSRESET
;
1072 req
.reply_expected
= 1;
1073 if (pmu_queue_request(&req
) != 0) {
1074 printk(KERN_ERR
"pmu_adb_reset_bus: pmu_queue_request failed\n");
1077 pmu_wait_complete(&req
);
1079 if (save_autopoll
!= 0)
1080 pmu_adb_autopoll(save_autopoll
);
1084 #endif /* CONFIG_ADB */
1086 /* Construct and send a pmu request */
1088 pmu_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
1097 if (nbytes
< 0 || nbytes
> 32) {
1098 printk(KERN_ERR
"pmu_request: bad nbytes (%d)\n", nbytes
);
1102 req
->nbytes
= nbytes
;
1104 va_start(list
, nbytes
);
1105 for (i
= 0; i
< nbytes
; ++i
)
1106 req
->data
[i
] = va_arg(list
, int);
1109 req
->reply_expected
= 0;
1110 return pmu_queue_request(req
);
1114 pmu_queue_request(struct adb_request
*req
)
1116 unsigned long flags
;
1123 if (req
->nbytes
<= 0) {
1127 nsend
= pmu_data_len
[req
->data
[0]][0];
1128 if (nsend
>= 0 && req
->nbytes
!= nsend
+ 1) {
1137 spin_lock_irqsave(&pmu_lock
, flags
);
1138 if (current_req
!= 0) {
1139 last_req
->next
= req
;
1144 if (pmu_state
== idle
)
1147 spin_unlock_irqrestore(&pmu_lock
, flags
);
1155 /* Sightly increased the delay, I had one occurrence of the message
1159 while ((in_8(&via
[B
]) & TACK
) == 0) {
1160 if (--timeout
< 0) {
1161 printk(KERN_ERR
"PMU not responding (!ack)\n");
1168 /* New PMU seems to be very sensitive to those timings, so we make sure
1169 * PCI is flushed immediately */
1173 volatile unsigned char __iomem
*v
= via
;
1175 out_8(&v
[ACR
], in_8(&v
[ACR
]) | SR_OUT
| SR_EXT
);
1177 out_8(&v
[B
], in_8(&v
[B
]) & ~TREQ
); /* assert TREQ */
1184 volatile unsigned char __iomem
*v
= via
;
1186 out_8(&v
[ACR
], (in_8(&v
[ACR
]) & ~SR_OUT
) | SR_EXT
);
1187 in_8(&v
[SR
]); /* resets SR */
1188 out_8(&v
[B
], in_8(&v
[B
]) & ~TREQ
);
1193 pmu_done(struct adb_request
*req
)
1195 void (*done
)(struct adb_request
*) = req
->done
;
1198 /* Here, we assume that if the request has a done member, the
1199 * struct request will survive to setting req->complete to 1
1208 struct adb_request
*req
;
1210 /* assert pmu_state == idle */
1211 /* get the packet to send */
1213 if (req
== 0 || pmu_state
!= idle
1214 || (/*req->reply_expected && */req_awaiting_reply
))
1217 pmu_state
= sending
;
1219 data_len
= pmu_data_len
[req
->data
[0]][0];
1221 /* Sounds safer to make sure ACK is high before writing. This helped
1222 * kill a problem with ADB and some iBooks
1225 /* set the shift register to shift out and send a byte */
1226 send_byte(req
->data
[0]);
1236 via_pmu_interrupt(0, NULL
, NULL
);
1246 /* Kicks ADB read when PMU is suspended */
1247 adb_int_pending
= 1;
1249 via_pmu_interrupt(0, NULL
, NULL
);
1250 } while (pmu_suspended
&& (adb_int_pending
|| pmu_state
!= idle
1251 || req_awaiting_reply
));
1255 pmu_wait_complete(struct adb_request
*req
)
1259 while((pmu_state
!= idle
&& pmu_state
!= locked
) || !req
->complete
)
1260 via_pmu_interrupt(0, NULL
, NULL
);
1263 /* This function loops until the PMU is idle and prevents it from
1264 * anwsering to ADB interrupts. pmu_request can still be called.
1265 * This is done to avoid spurrious shutdowns when we know we'll have
1266 * interrupts switched off for a long time
1271 unsigned long flags
;
1272 #ifdef SUSPEND_USES_PMU
1273 struct adb_request
*req
;
1278 spin_lock_irqsave(&pmu_lock
, flags
);
1280 if (pmu_suspended
> 1) {
1281 spin_unlock_irqrestore(&pmu_lock
, flags
);
1286 spin_unlock_irqrestore(&pmu_lock
, flags
);
1287 if (req_awaiting_reply
)
1288 adb_int_pending
= 1;
1289 via_pmu_interrupt(0, NULL
, NULL
);
1290 spin_lock_irqsave(&pmu_lock
, flags
);
1291 if (!adb_int_pending
&& pmu_state
== idle
&& !req_awaiting_reply
) {
1292 #ifdef SUSPEND_USES_PMU
1293 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, 0);
1294 spin_unlock_irqrestore(&pmu_lock
, flags
);
1295 while(!req
.complete
)
1297 #else /* SUSPEND_USES_PMU */
1299 disable_irq_nosync(gpio_irq
);
1300 out_8(&via
[IER
], CB1_INT
| IER_CLR
);
1301 spin_unlock_irqrestore(&pmu_lock
, flags
);
1302 #endif /* SUSPEND_USES_PMU */
1311 unsigned long flags
;
1313 if (!via
|| (pmu_suspended
< 1))
1316 spin_lock_irqsave(&pmu_lock
, flags
);
1318 if (pmu_suspended
> 0) {
1319 spin_unlock_irqrestore(&pmu_lock
, flags
);
1322 adb_int_pending
= 1;
1323 #ifdef SUSPEND_USES_PMU
1324 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
1325 spin_unlock_irqrestore(&pmu_lock
, flags
);
1326 while(!req
.complete
)
1328 #else /* SUSPEND_USES_PMU */
1330 enable_irq(gpio_irq
);
1331 out_8(&via
[IER
], CB1_INT
| IER_SET
);
1332 spin_unlock_irqrestore(&pmu_lock
, flags
);
1334 #endif /* SUSPEND_USES_PMU */
1337 /* Interrupt data could be the result data from an ADB cmd */
1339 pmu_handle_data(unsigned char *data
, int len
, struct pt_regs
*regs
)
1341 unsigned char ints
, pirq
;
1345 if (drop_interrupts
|| len
< 1) {
1346 adb_int_pending
= 0;
1351 /* Get PMU interrupt mask */
1354 /* Record zero interrupts for stats */
1358 /* Hack to deal with ADB autopoll flag */
1359 if (ints
& PMU_INT_ADB
)
1360 ints
&= ~(PMU_INT_ADB_AUTO
| PMU_INT_AUTO_SRQ_POLL
);
1365 if (i
> pmu_irq_stats
[10])
1366 pmu_irq_stats
[10] = i
;
1370 for (pirq
= 0; pirq
< 8; pirq
++)
1371 if (ints
& (1 << pirq
))
1373 pmu_irq_stats
[pirq
]++;
1375 ints
&= ~(1 << pirq
);
1377 /* Note: for some reason, we get an interrupt with len=1,
1378 * data[0]==0 after each normal ADB interrupt, at least
1379 * on the Pismo. Still investigating... --BenH
1381 if ((1 << pirq
) & PMU_INT_ADB
) {
1382 if ((data
[0] & PMU_INT_ADB_AUTO
) == 0) {
1383 struct adb_request
*req
= req_awaiting_reply
;
1385 printk(KERN_ERR
"PMU: extra ADB reply\n");
1388 req_awaiting_reply
= NULL
;
1392 memcpy(req
->reply
, data
+ 1, len
- 1);
1393 req
->reply_len
= len
- 1;
1397 if (len
== 4 && data
[1] == 0x2c) {
1398 extern int xmon_wants_key
, xmon_adb_keycode
;
1399 if (xmon_wants_key
) {
1400 xmon_adb_keycode
= data
[2];
1406 * XXX On the [23]400 the PMU gives us an up
1407 * event for keycodes 0x74 or 0x75 when the PC
1408 * card eject buttons are released, so we
1409 * ignore those events.
1411 if (!(pmu_kind
== PMU_OHARE_BASED
&& len
== 4
1412 && data
[1] == 0x2c && data
[3] == 0xff
1413 && (data
[2] & ~1) == 0xf4))
1414 adb_input(data
+1, len
-1, regs
, 1);
1415 #endif /* CONFIG_ADB */
1418 /* Sound/brightness button pressed */
1419 else if ((1 << pirq
) & PMU_INT_SNDBRT
) {
1420 #ifdef CONFIG_PMAC_BACKLIGHT
1422 #ifdef CONFIG_INPUT_ADBHID
1423 if (!disable_kernel_backlight
)
1424 #endif /* CONFIG_INPUT_ADBHID */
1425 set_backlight_level(data
[1] >> 4);
1426 #endif /* CONFIG_PMAC_BACKLIGHT */
1428 /* Tick interrupt */
1429 else if ((1 << pirq
) & PMU_INT_TICK
) {
1430 /* Environement or tick interrupt, query batteries */
1431 if (pmu_battery_count
) {
1432 if ((--query_batt_timer
) == 0) {
1433 query_battery_state();
1434 query_batt_timer
= BATTERY_POLLING_COUNT
;
1438 else if ((1 << pirq
) & PMU_INT_ENVIRONMENT
) {
1439 if (pmu_battery_count
)
1440 query_battery_state();
1441 pmu_pass_intr(data
, len
);
1443 pmu_pass_intr(data
, len
);
1448 static struct adb_request
*
1449 pmu_sr_intr(struct pt_regs
*regs
)
1451 struct adb_request
*req
;
1454 if (via
[B
] & TREQ
) {
1455 printk(KERN_ERR
"PMU: spurious SR intr (%x)\n", via
[B
]);
1456 out_8(&via
[IFR
], SR_INT
);
1459 /* The ack may not yet be low when we get the interrupt */
1460 while ((in_8(&via
[B
]) & TACK
) != 0)
1463 /* if reading grab the byte, and reset the interrupt */
1464 if (pmu_state
== reading
|| pmu_state
== reading_intr
)
1465 bite
= in_8(&via
[SR
]);
1467 /* reset TREQ and wait for TACK to go high */
1468 out_8(&via
[B
], in_8(&via
[B
]) | TREQ
);
1471 switch (pmu_state
) {
1475 data_len
= req
->nbytes
- 1;
1476 send_byte(data_len
);
1479 if (data_index
<= data_len
) {
1480 send_byte(req
->data
[data_index
++]);
1484 data_len
= pmu_data_len
[req
->data
[0]][1];
1485 if (data_len
== 0) {
1487 current_req
= req
->next
;
1488 if (req
->reply_expected
)
1489 req_awaiting_reply
= req
;
1493 pmu_state
= reading
;
1495 reply_ptr
= req
->reply
+ req
->reply_len
;
1503 pmu_state
= reading_intr
;
1504 reply_ptr
= interrupt_data
[int_data_last
];
1506 if (gpio_irq
>= 0 && !gpio_irq_enabled
) {
1507 enable_irq(gpio_irq
);
1508 gpio_irq_enabled
= 1;
1514 if (data_len
== -1) {
1517 printk(KERN_ERR
"PMU: bad reply len %d\n", bite
);
1518 } else if (data_index
< 32) {
1519 reply_ptr
[data_index
++] = bite
;
1521 if (data_index
< data_len
) {
1526 if (pmu_state
== reading_intr
) {
1528 int_data_state
[int_data_last
] = int_data_ready
;
1529 interrupt_data_len
[int_data_last
] = data_len
;
1533 * For PMU sleep and freq change requests, we lock the
1534 * PMU until it's explicitely unlocked. This avoids any
1535 * spurrious event polling getting in
1537 current_req
= req
->next
;
1538 req
->reply_len
+= data_index
;
1539 if (req
->data
[0] == PMU_SLEEP
|| req
->data
[0] == PMU_CPU_SPEED
)
1548 printk(KERN_ERR
"via_pmu_interrupt: unknown state %d?\n",
1555 via_pmu_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
1557 unsigned long flags
;
1561 struct adb_request
*req
= NULL
;
1564 /* This is a bit brutal, we can probably do better */
1565 spin_lock_irqsave(&pmu_lock
, flags
);
1569 intr
= in_8(&via
[IFR
]) & (SR_INT
| CB1_INT
);
1573 if (++nloop
> 1000) {
1574 printk(KERN_DEBUG
"PMU: stuck in intr loop, "
1575 "intr=%x, ier=%x pmu_state=%d\n",
1576 intr
, in_8(&via
[IER
]), pmu_state
);
1579 out_8(&via
[IFR
], intr
);
1580 if (intr
& CB1_INT
) {
1581 adb_int_pending
= 1;
1584 if (intr
& SR_INT
) {
1585 req
= pmu_sr_intr(regs
);
1592 if (pmu_state
== idle
) {
1593 if (adb_int_pending
) {
1594 if (int_data_state
[0] == int_data_empty
)
1596 else if (int_data_state
[1] == int_data_empty
)
1601 int_data_state
[int_data_last
] = int_data_fill
;
1602 /* Sounds safer to make sure ACK is high before writing.
1603 * This helped kill a problem with ADB and some iBooks
1606 send_byte(PMU_INT_ACK
);
1607 adb_int_pending
= 0;
1608 } else if (current_req
)
1612 /* Mark the oldest buffer for flushing */
1613 if (int_data_state
[!int_data_last
] == int_data_ready
) {
1614 int_data_state
[!int_data_last
] = int_data_flush
;
1615 int_data
= !int_data_last
;
1616 } else if (int_data_state
[int_data_last
] == int_data_ready
) {
1617 int_data_state
[int_data_last
] = int_data_flush
;
1618 int_data
= int_data_last
;
1621 spin_unlock_irqrestore(&pmu_lock
, flags
);
1623 /* Deal with completed PMU requests outside of the lock */
1629 /* Deal with interrupt datas outside of the lock */
1630 if (int_data
>= 0) {
1631 pmu_handle_data(interrupt_data
[int_data
], interrupt_data_len
[int_data
], regs
);
1632 spin_lock_irqsave(&pmu_lock
, flags
);
1634 int_data_state
[int_data
] = int_data_empty
;
1639 return IRQ_RETVAL(handled
);
1645 unsigned long flags
;
1647 spin_lock_irqsave(&pmu_lock
, flags
);
1648 if (pmu_state
== locked
)
1650 adb_int_pending
= 1;
1651 spin_unlock_irqrestore(&pmu_lock
, flags
);
1656 gpio1_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
1658 unsigned long flags
;
1660 if ((in_8(gpio_reg
+ 0x9) & 0x02) == 0) {
1661 spin_lock_irqsave(&pmu_lock
, flags
);
1662 if (gpio_irq_enabled
> 0) {
1663 disable_irq_nosync(gpio_irq
);
1664 gpio_irq_enabled
= 0;
1667 adb_int_pending
= 1;
1668 spin_unlock_irqrestore(&pmu_lock
, flags
);
1669 via_pmu_interrupt(0, NULL
, NULL
);
1675 #ifdef CONFIG_PMAC_BACKLIGHT
1676 static int backlight_to_bright
[] = {
1677 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1678 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1682 pmu_set_backlight_enable(int on
, int level
, void* data
)
1684 struct adb_request req
;
1690 pmu_request(&req
, NULL
, 2, PMU_BACKLIGHT_BRIGHT
,
1691 backlight_to_bright
[level
]);
1692 pmu_wait_complete(&req
);
1694 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
1695 PMU_POW_BACKLIGHT
| (on
? PMU_POW_ON
: PMU_POW_OFF
));
1696 pmu_wait_complete(&req
);
1702 pmu_bright_complete(struct adb_request
*req
)
1704 if (req
== &bright_req_1
)
1705 clear_bit(1, &async_req_locks
);
1706 if (req
== &bright_req_2
)
1707 clear_bit(2, &async_req_locks
);
1711 pmu_set_backlight_level(int level
, void* data
)
1716 if (test_and_set_bit(1, &async_req_locks
))
1718 pmu_request(&bright_req_1
, pmu_bright_complete
, 2, PMU_BACKLIGHT_BRIGHT
,
1719 backlight_to_bright
[level
]);
1720 if (test_and_set_bit(2, &async_req_locks
))
1722 pmu_request(&bright_req_2
, pmu_bright_complete
, 2, PMU_POWER_CTRL
,
1723 PMU_POW_BACKLIGHT
| (level
> BACKLIGHT_OFF
?
1724 PMU_POW_ON
: PMU_POW_OFF
));
1728 #endif /* CONFIG_PMAC_BACKLIGHT */
1731 pmu_enable_irled(int on
)
1733 struct adb_request req
;
1737 if (pmu_kind
== PMU_KEYLARGO_BASED
)
1740 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
, PMU_POW_IRLED
|
1741 (on
? PMU_POW_ON
: PMU_POW_OFF
));
1742 pmu_wait_complete(&req
);
1748 struct adb_request req
;
1753 local_irq_disable();
1755 drop_interrupts
= 1;
1757 if (pmu_kind
!= PMU_KEYLARGO_BASED
) {
1758 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|
1760 while(!req
.complete
)
1764 pmu_request(&req
, NULL
, 1, PMU_RESET
);
1765 pmu_wait_complete(&req
);
1773 struct adb_request req
;
1778 local_irq_disable();
1780 drop_interrupts
= 1;
1782 if (pmu_kind
!= PMU_KEYLARGO_BASED
) {
1783 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|
1785 pmu_wait_complete(&req
);
1787 /* Disable server mode on shutdown or we'll just
1790 pmu_set_server_mode(0);
1793 pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
,
1794 'M', 'A', 'T', 'T');
1795 pmu_wait_complete(&req
);
1808 static LIST_HEAD(sleep_notifiers
);
1811 pmu_register_sleep_notifier(struct pmu_sleep_notifier
*n
)
1813 struct list_head
*list
;
1814 struct pmu_sleep_notifier
*notifier
;
1816 for (list
= sleep_notifiers
.next
; list
!= &sleep_notifiers
;
1817 list
= list
->next
) {
1818 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
1819 if (n
->priority
> notifier
->priority
)
1822 __list_add(&n
->list
, list
->prev
, list
);
1825 EXPORT_SYMBOL(pmu_register_sleep_notifier
);
1828 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier
* n
)
1830 if (n
->list
.next
== 0)
1833 n
->list
.next
= NULL
;
1836 EXPORT_SYMBOL(pmu_unregister_sleep_notifier
);
1837 #endif /* CONFIG_PM */
1839 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1841 /* Sleep is broadcast last-to-first */
1843 broadcast_sleep(int when
, int fallback
)
1845 int ret
= PBOOK_SLEEP_OK
;
1846 struct list_head
*list
;
1847 struct pmu_sleep_notifier
*notifier
;
1849 for (list
= sleep_notifiers
.prev
; list
!= &sleep_notifiers
;
1850 list
= list
->prev
) {
1851 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
1852 ret
= notifier
->notifier_call(notifier
, when
);
1853 if (ret
!= PBOOK_SLEEP_OK
) {
1854 printk(KERN_DEBUG
"sleep %d rejected by %p (%p)\n",
1855 when
, notifier
, notifier
->notifier_call
);
1856 for (; list
!= &sleep_notifiers
; list
= list
->next
) {
1857 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
1858 notifier
->notifier_call(notifier
, fallback
);
1866 /* Wake is broadcast first-to-last */
1868 broadcast_wake(void)
1870 int ret
= PBOOK_SLEEP_OK
;
1871 struct list_head
*list
;
1872 struct pmu_sleep_notifier
*notifier
;
1874 for (list
= sleep_notifiers
.next
; list
!= &sleep_notifiers
;
1875 list
= list
->next
) {
1876 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
1877 notifier
->notifier_call(notifier
, PBOOK_WAKE
);
1883 * This struct is used to store config register values for
1884 * PCI devices which may get powered off when we sleep.
1886 static struct pci_save
{
1887 #ifndef HACKED_PCI_SAVE
1896 static int pbook_npci_saves
;
1899 pbook_alloc_pci_save(void)
1902 struct pci_dev
*pd
= NULL
;
1905 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
1910 pbook_pci_saves
= (struct pci_save
*)
1911 kmalloc(npci
* sizeof(struct pci_save
), GFP_KERNEL
);
1912 pbook_npci_saves
= npci
;
1916 pbook_free_pci_save(void)
1918 if (pbook_pci_saves
== NULL
)
1920 kfree(pbook_pci_saves
);
1921 pbook_pci_saves
= NULL
;
1922 pbook_npci_saves
= 0;
1926 pbook_pci_save(void)
1928 struct pci_save
*ps
= pbook_pci_saves
;
1929 struct pci_dev
*pd
= NULL
;
1930 int npci
= pbook_npci_saves
;
1935 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
1938 #ifndef HACKED_PCI_SAVE
1939 pci_read_config_word(pd
, PCI_COMMAND
, &ps
->command
);
1940 pci_read_config_word(pd
, PCI_CACHE_LINE_SIZE
, &ps
->cache_lat
);
1941 pci_read_config_word(pd
, PCI_INTERRUPT_LINE
, &ps
->intr
);
1942 pci_read_config_dword(pd
, PCI_ROM_ADDRESS
, &ps
->rom_address
);
1946 pci_read_config_dword(pd
, i
<<4, &ps
->config
[i
]);
1952 /* For this to work, we must take care of a few things: If gmac was enabled
1953 * during boot, it will be in the pci dev list. If it's disabled at this point
1954 * (and it will probably be), then you can't access it's config space.
1957 pbook_pci_restore(void)
1960 struct pci_save
*ps
= pbook_pci_saves
- 1;
1961 struct pci_dev
*pd
= NULL
;
1962 int npci
= pbook_npci_saves
;
1965 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
1966 #ifdef HACKED_PCI_SAVE
1972 pci_write_config_dword(pd
, i
<<4, ps
->config
[i
]);
1973 pci_write_config_dword(pd
, 4, ps
->config
[1]);
1978 if (ps
->command
== 0)
1980 pci_read_config_word(pd
, PCI_COMMAND
, &cmd
);
1981 if ((ps
->command
& ~cmd
) == 0)
1983 switch (pd
->hdr_type
) {
1984 case PCI_HEADER_TYPE_NORMAL
:
1985 for (j
= 0; j
< 6; ++j
)
1986 pci_write_config_dword(pd
,
1987 PCI_BASE_ADDRESS_0
+ j
*4,
1988 pd
->resource
[j
].start
);
1989 pci_write_config_dword(pd
, PCI_ROM_ADDRESS
,
1991 pci_write_config_word(pd
, PCI_CACHE_LINE_SIZE
,
1993 pci_write_config_word(pd
, PCI_INTERRUPT_LINE
,
1995 pci_write_config_word(pd
, PCI_COMMAND
, ps
->command
);
2003 /* N.B. This doesn't work on the 3400 */
2007 struct adb_request req
;
2009 memset(&req
, 0, sizeof(req
));
2011 for (; n
> 0; --n
) {
2018 req
.reply
[0] = ADB_RET_OK
;
2020 req
.reply_expected
= 0;
2021 pmu_polled_request(&req
);
2029 req
.reply
[0] = ADB_RET_OK
;
2031 req
.reply_expected
= 0;
2032 pmu_polled_request(&req
);
2040 * Put the powerbook to sleep.
2043 static u32 save_via
[8];
2046 save_via_state(void)
2048 save_via
[0] = in_8(&via
[ANH
]);
2049 save_via
[1] = in_8(&via
[DIRA
]);
2050 save_via
[2] = in_8(&via
[B
]);
2051 save_via
[3] = in_8(&via
[DIRB
]);
2052 save_via
[4] = in_8(&via
[PCR
]);
2053 save_via
[5] = in_8(&via
[ACR
]);
2054 save_via
[6] = in_8(&via
[T1CL
]);
2055 save_via
[7] = in_8(&via
[T1CH
]);
2058 restore_via_state(void)
2060 out_8(&via
[ANH
], save_via
[0]);
2061 out_8(&via
[DIRA
], save_via
[1]);
2062 out_8(&via
[B
], save_via
[2]);
2063 out_8(&via
[DIRB
], save_via
[3]);
2064 out_8(&via
[PCR
], save_via
[4]);
2065 out_8(&via
[ACR
], save_via
[5]);
2066 out_8(&via
[T1CL
], save_via
[6]);
2067 out_8(&via
[T1CH
], save_via
[7]);
2068 out_8(&via
[IER
], IER_CLR
| 0x7f); /* disable all intrs */
2069 out_8(&via
[IFR
], 0x7f); /* clear IFR */
2070 out_8(&via
[IER
], IER_SET
| SR_INT
| CB1_INT
);
2074 pmac_suspend_devices(void)
2078 pm_prepare_console();
2080 /* Notify old-style device drivers & userland */
2081 ret
= broadcast_sleep(PBOOK_SLEEP_REQUEST
, PBOOK_SLEEP_REJECT
);
2082 if (ret
!= PBOOK_SLEEP_OK
) {
2083 printk(KERN_ERR
"Sleep rejected by drivers\n");
2087 /* Sync the disks. */
2088 /* XXX It would be nice to have some way to ensure that
2089 * nobody is dirtying any new buffers while we wait. That
2090 * could be achieved using the refrigerator for processes
2095 /* Sleep can fail now. May not be very robust but useful for debugging */
2096 ret
= broadcast_sleep(PBOOK_SLEEP_NOW
, PBOOK_WAKE
);
2097 if (ret
!= PBOOK_SLEEP_OK
) {
2098 printk(KERN_ERR
"Driver sleep failed\n");
2102 /* Send suspend call to devices, hold the device core's dpm_sem */
2103 ret
= device_suspend(PMSG_SUSPEND
);
2106 printk(KERN_ERR
"Driver sleep failed\n");
2110 /* Call platform functions marked "on sleep" */
2111 pmac_pfunc_i2c_suspend();
2112 pmac_pfunc_base_suspend();
2114 /* Stop preemption */
2117 /* Make sure the decrementer won't interrupt us */
2118 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2119 /* Make sure any pending DEC interrupt occurring while we did
2120 * the above didn't re-enable the DEC */
2122 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2124 /* We can now disable MSR_EE. This code of course works properly only
2125 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2126 * stop the "other" CPUs way before we do all that stuff.
2128 local_irq_disable();
2130 /* Broadcast power down irq
2131 * This isn't that useful in most cases (only directly wired devices can
2132 * use this but still... This will take care of sysdev's as well, so
2133 * we exit from here with local irqs disabled and PIC off.
2135 ret
= device_power_down(PMSG_SUSPEND
);
2137 wakeup_decrementer();
2142 printk(KERN_ERR
"Driver powerdown failed\n");
2146 /* Wait for completion of async backlight requests */
2147 while (!bright_req_1
.complete
|| !bright_req_2
.complete
||
2151 /* Giveup the lazy FPU & vec so we don't have to back them
2152 * up from the low level code
2156 #ifdef CONFIG_ALTIVEC
2157 if (cpu_has_feature(CPU_FTR_ALTIVEC
))
2158 enable_kernel_altivec();
2159 #endif /* CONFIG_ALTIVEC */
2165 pmac_wakeup_devices(void)
2169 /* Power back up system devices (including the PIC) */
2172 /* Force a poll of ADB interrupts */
2173 adb_int_pending
= 1;
2174 via_pmu_interrupt(0, NULL
, NULL
);
2176 /* Restart jiffies & scheduling */
2177 wakeup_decrementer();
2179 /* Re-enable local CPU interrupts */
2184 /* Call platform functions marked "on wake" */
2185 pmac_pfunc_base_resume();
2186 pmac_pfunc_i2c_resume();
2188 /* Resume devices */
2191 /* Notify old style drivers */
2194 pm_restore_console();
2199 #define GRACKLE_PM (1<<7)
2200 #define GRACKLE_DOZE (1<<5)
2201 #define GRACKLE_NAP (1<<4)
2202 #define GRACKLE_SLEEP (1<<3)
2205 powerbook_sleep_grackle(void)
2207 unsigned long save_l2cr
;
2208 unsigned short pmcr1
;
2209 struct adb_request req
;
2211 struct pci_dev
*grackle
;
2213 grackle
= pci_find_slot(0, 0);
2217 ret
= pmac_suspend_devices();
2219 printk(KERN_ERR
"Sleep rejected by devices\n");
2223 /* Turn off various things. Darwin does some retry tests here... */
2224 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL0
, PMU_POW0_OFF
|PMU_POW0_HARD_DRIVE
);
2225 pmu_wait_complete(&req
);
2226 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
2227 PMU_POW_OFF
|PMU_POW_BACKLIGHT
|PMU_POW_IRLED
|PMU_POW_MEDIABAY
);
2228 pmu_wait_complete(&req
);
2230 /* For 750, save backside cache setting and disable it */
2231 save_l2cr
= _get_L2CR(); /* (returns -1 if not available) */
2233 if (!__fake_sleep
) {
2234 /* Ask the PMU to put us to sleep */
2235 pmu_request(&req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2236 pmu_wait_complete(&req
);
2239 /* The VIA is supposed not to be restored correctly*/
2241 /* We shut down some HW */
2242 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,1);
2244 pci_read_config_word(grackle
, 0x70, &pmcr1
);
2245 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2246 pmcr1
&= ~(GRACKLE_DOZE
|GRACKLE_SLEEP
);
2247 pmcr1
|= GRACKLE_PM
|GRACKLE_NAP
;
2248 pci_write_config_word(grackle
, 0x70, pmcr1
);
2250 /* Call low-level ASM sleep handler */
2254 low_sleep_handler();
2256 /* We're awake again, stop grackle PM */
2257 pci_read_config_word(grackle
, 0x70, &pmcr1
);
2258 pmcr1
&= ~(GRACKLE_PM
|GRACKLE_DOZE
|GRACKLE_SLEEP
|GRACKLE_NAP
);
2259 pci_write_config_word(grackle
, 0x70, pmcr1
);
2261 /* Make sure the PMU is idle */
2262 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,0);
2263 restore_via_state();
2265 /* Restore L2 cache */
2266 if (save_l2cr
!= 0xffffffff && (save_l2cr
& L2CR_L2E
) != 0)
2267 _set_L2CR(save_l2cr
);
2269 /* Restore userland MMU context */
2270 set_context(current
->active_mm
->context
, current
->active_mm
->pgd
);
2272 /* Power things up */
2274 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
2275 pmu_wait_complete(&req
);
2276 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL0
,
2277 PMU_POW0_ON
|PMU_POW0_HARD_DRIVE
);
2278 pmu_wait_complete(&req
);
2279 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
2280 PMU_POW_ON
|PMU_POW_BACKLIGHT
|PMU_POW_CHARGER
|PMU_POW_IRLED
|PMU_POW_MEDIABAY
);
2281 pmu_wait_complete(&req
);
2283 pmac_wakeup_devices();
2289 powerbook_sleep_Core99(void)
2291 unsigned long save_l2cr
;
2292 unsigned long save_l3cr
;
2293 struct adb_request req
;
2296 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) < 0) {
2297 printk(KERN_ERR
"Sleep mode not supported on this machine\n");
2301 if (num_online_cpus() > 1 || cpu_is_offline(0))
2304 ret
= pmac_suspend_devices();
2306 printk(KERN_ERR
"Sleep rejected by devices\n");
2310 /* Stop environment and ADB interrupts */
2311 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, 0);
2312 pmu_wait_complete(&req
);
2314 /* Tell PMU what events will wake us up */
2315 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
, PMU_PWR_CLR_WAKEUP_EVENTS
,
2317 pmu_wait_complete(&req
);
2318 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
, PMU_PWR_SET_WAKEUP_EVENTS
,
2319 0, PMU_PWR_WAKEUP_KEY
|
2320 (option_lid_wakeup
? PMU_PWR_WAKEUP_LID_OPEN
: 0));
2321 pmu_wait_complete(&req
);
2323 /* Save the state of the L2 and L3 caches */
2324 save_l3cr
= _get_L3CR(); /* (returns -1 if not available) */
2325 save_l2cr
= _get_L2CR(); /* (returns -1 if not available) */
2327 if (!__fake_sleep
) {
2328 /* Ask the PMU to put us to sleep */
2329 pmu_request(&req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2330 pmu_wait_complete(&req
);
2333 /* The VIA is supposed not to be restored correctly*/
2336 /* Shut down various ASICs. There's a chance that we can no longer
2337 * talk to the PMU after this, so I moved it to _after_ sending the
2338 * sleep command to it. Still need to be checked.
2340 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 1);
2342 /* Call low-level ASM sleep handler */
2346 low_sleep_handler();
2348 /* Restore Apple core ASICs state */
2349 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 0);
2352 restore_via_state();
2354 /* tweak LPJ before cpufreq is there */
2355 loops_per_jiffy
*= 2;
2358 pmac_call_early_video_resume();
2360 /* Restore L2 cache */
2361 if (save_l2cr
!= 0xffffffff && (save_l2cr
& L2CR_L2E
) != 0)
2362 _set_L2CR(save_l2cr
);
2363 /* Restore L3 cache */
2364 if (save_l3cr
!= 0xffffffff && (save_l3cr
& L3CR_L3E
) != 0)
2365 _set_L3CR(save_l3cr
);
2367 /* Restore userland MMU context */
2368 set_context(current
->active_mm
->context
, current
->active_mm
->pgd
);
2370 /* Tell PMU we are ready */
2372 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
2373 pmu_wait_complete(&req
);
2374 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
2375 pmu_wait_complete(&req
);
2377 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2378 loops_per_jiffy
/= 2;
2380 pmac_wakeup_devices();
2385 #define PB3400_MEM_CTRL 0xf8000000
2386 #define PB3400_MEM_CTRL_SLEEP 0x70
2389 powerbook_sleep_3400(void)
2394 struct adb_request sleep_req
;
2395 void __iomem
*mem_ctrl
;
2396 unsigned int __iomem
*mem_ctrl_sleep
;
2398 /* first map in the memory controller registers */
2399 mem_ctrl
= ioremap(PB3400_MEM_CTRL
, 0x100);
2400 if (mem_ctrl
== NULL
) {
2401 printk("powerbook_sleep_3400: ioremap failed\n");
2404 mem_ctrl_sleep
= mem_ctrl
+ PB3400_MEM_CTRL_SLEEP
;
2406 /* Allocate room for PCI save */
2407 pbook_alloc_pci_save();
2409 ret
= pmac_suspend_devices();
2411 pbook_free_pci_save();
2412 printk(KERN_ERR
"Sleep rejected by devices\n");
2416 /* Save the state of PCI config space for some slots */
2419 /* Set the memory controller to keep the memory refreshed
2420 while we're asleep */
2421 for (i
= 0x403f; i
>= 0x4000; --i
) {
2422 out_be32(mem_ctrl_sleep
, i
);
2424 x
= (in_be32(mem_ctrl_sleep
) >> 16) & 0x3ff;
2430 /* Ask the PMU to put us to sleep */
2431 pmu_request(&sleep_req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2432 while (!sleep_req
.complete
)
2435 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,1);
2437 /* displacement-flush the L2 cache - necessary? */
2438 for (p
= KERNELBASE
; p
< KERNELBASE
+ 0x100000; p
+= 0x1000)
2439 i
= *(volatile int *)p
;
2442 /* Put the CPU into sleep mode */
2443 hid0
= mfspr(SPRN_HID0
);
2444 hid0
= (hid0
& ~(HID0_NAP
| HID0_DOZE
)) | HID0_SLEEP
;
2445 mtspr(SPRN_HID0
, hid0
);
2446 mtmsr(mfmsr() | MSR_POW
| MSR_EE
);
2449 /* OK, we're awake again, start restoring things */
2450 out_be32(mem_ctrl_sleep
, 0x3f);
2451 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,0);
2452 pbook_pci_restore();
2455 /* wait for the PMU interrupt sequence to complete */
2459 pmac_wakeup_devices();
2460 pbook_free_pci_save();
2466 #endif /* CONFIG_PM && CONFIG_PPC32 */
2469 * Support for /dev/pmu device
2471 #define RB_SIZE 0x10
2472 struct pmu_private
{
2473 struct list_head list
;
2478 unsigned char data
[16];
2480 wait_queue_head_t wait
;
2482 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2483 int backlight_locker
;
2484 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2487 static LIST_HEAD(all_pmu_pvt
);
2488 static DEFINE_SPINLOCK(all_pvt_lock
);
2491 pmu_pass_intr(unsigned char *data
, int len
)
2493 struct pmu_private
*pp
;
2494 struct list_head
*list
;
2496 unsigned long flags
;
2498 if (len
> sizeof(pp
->rb_buf
[0].data
))
2499 len
= sizeof(pp
->rb_buf
[0].data
);
2500 spin_lock_irqsave(&all_pvt_lock
, flags
);
2501 for (list
= &all_pmu_pvt
; (list
= list
->next
) != &all_pmu_pvt
; ) {
2502 pp
= list_entry(list
, struct pmu_private
, list
);
2503 spin_lock(&pp
->lock
);
2507 if (i
!= pp
->rb_get
) {
2508 struct rb_entry
*rp
= &pp
->rb_buf
[pp
->rb_put
];
2510 memcpy(rp
->data
, data
, len
);
2512 wake_up_interruptible(&pp
->wait
);
2514 spin_unlock(&pp
->lock
);
2516 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2520 pmu_open(struct inode
*inode
, struct file
*file
)
2522 struct pmu_private
*pp
;
2523 unsigned long flags
;
2525 pp
= kmalloc(sizeof(struct pmu_private
), GFP_KERNEL
);
2528 pp
->rb_get
= pp
->rb_put
= 0;
2529 spin_lock_init(&pp
->lock
);
2530 init_waitqueue_head(&pp
->wait
);
2531 spin_lock_irqsave(&all_pvt_lock
, flags
);
2532 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2533 pp
->backlight_locker
= 0;
2534 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2535 list_add(&pp
->list
, &all_pmu_pvt
);
2536 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2537 file
->private_data
= pp
;
2542 pmu_read(struct file
*file
, char __user
*buf
,
2543 size_t count
, loff_t
*ppos
)
2545 struct pmu_private
*pp
= file
->private_data
;
2546 DECLARE_WAITQUEUE(wait
, current
);
2547 unsigned long flags
;
2550 if (count
< 1 || pp
== 0)
2552 if (!access_ok(VERIFY_WRITE
, buf
, count
))
2555 spin_lock_irqsave(&pp
->lock
, flags
);
2556 add_wait_queue(&pp
->wait
, &wait
);
2557 current
->state
= TASK_INTERRUPTIBLE
;
2561 if (pp
->rb_get
!= pp
->rb_put
) {
2563 struct rb_entry
*rp
= &pp
->rb_buf
[i
];
2565 spin_unlock_irqrestore(&pp
->lock
, flags
);
2568 if (ret
> 0 && copy_to_user(buf
, rp
->data
, ret
))
2572 spin_lock_irqsave(&pp
->lock
, flags
);
2577 if (file
->f_flags
& O_NONBLOCK
)
2580 if (signal_pending(current
))
2582 spin_unlock_irqrestore(&pp
->lock
, flags
);
2584 spin_lock_irqsave(&pp
->lock
, flags
);
2586 current
->state
= TASK_RUNNING
;
2587 remove_wait_queue(&pp
->wait
, &wait
);
2588 spin_unlock_irqrestore(&pp
->lock
, flags
);
2594 pmu_write(struct file
*file
, const char __user
*buf
,
2595 size_t count
, loff_t
*ppos
)
2601 pmu_fpoll(struct file
*filp
, poll_table
*wait
)
2603 struct pmu_private
*pp
= filp
->private_data
;
2604 unsigned int mask
= 0;
2605 unsigned long flags
;
2609 poll_wait(filp
, &pp
->wait
, wait
);
2610 spin_lock_irqsave(&pp
->lock
, flags
);
2611 if (pp
->rb_get
!= pp
->rb_put
)
2613 spin_unlock_irqrestore(&pp
->lock
, flags
);
2618 pmu_release(struct inode
*inode
, struct file
*file
)
2620 struct pmu_private
*pp
= file
->private_data
;
2621 unsigned long flags
;
2625 file
->private_data
= NULL
;
2626 spin_lock_irqsave(&all_pvt_lock
, flags
);
2627 list_del(&pp
->list
);
2628 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2629 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2630 if (pp
->backlight_locker
) {
2631 spin_lock_irqsave(&pmu_lock
, flags
);
2632 disable_kernel_backlight
--;
2633 spin_unlock_irqrestore(&pmu_lock
, flags
);
2635 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2643 pmu_ioctl(struct inode
* inode
, struct file
*filp
,
2644 u_int cmd
, u_long arg
)
2646 __u32 __user
*argp
= (__u32 __user
*)arg
;
2647 int error
= -EINVAL
;
2650 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2652 if (!capable(CAP_SYS_ADMIN
))
2654 if (sleep_in_progress
)
2656 sleep_in_progress
= 1;
2658 case PMU_OHARE_BASED
:
2659 error
= powerbook_sleep_3400();
2661 case PMU_HEATHROW_BASED
:
2662 case PMU_PADDINGTON_BASED
:
2663 error
= powerbook_sleep_grackle();
2665 case PMU_KEYLARGO_BASED
:
2666 error
= powerbook_sleep_Core99();
2671 sleep_in_progress
= 0;
2673 case PMU_IOC_CAN_SLEEP
:
2674 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) < 0)
2675 return put_user(0, argp
);
2677 return put_user(1, argp
);
2678 #endif /* CONFIG_PM && CONFIG_PPC32 */
2680 #ifdef CONFIG_PMAC_BACKLIGHT
2681 /* Backlight should have its own device or go via
2684 case PMU_IOC_GET_BACKLIGHT
:
2685 if (sleep_in_progress
)
2687 error
= get_backlight_level();
2690 return put_user(error
, argp
);
2691 case PMU_IOC_SET_BACKLIGHT
:
2694 if (sleep_in_progress
)
2696 error
= get_user(value
, argp
);
2698 error
= set_backlight_level(value
);
2701 #ifdef CONFIG_INPUT_ADBHID
2702 case PMU_IOC_GRAB_BACKLIGHT
: {
2703 struct pmu_private
*pp
= filp
->private_data
;
2704 unsigned long flags
;
2706 if (pp
->backlight_locker
)
2708 pp
->backlight_locker
= 1;
2709 spin_lock_irqsave(&pmu_lock
, flags
);
2710 disable_kernel_backlight
++;
2711 spin_unlock_irqrestore(&pmu_lock
, flags
);
2714 #endif /* CONFIG_INPUT_ADBHID */
2715 #endif /* CONFIG_PMAC_BACKLIGHT */
2716 case PMU_IOC_GET_MODEL
:
2717 return put_user(pmu_kind
, argp
);
2718 case PMU_IOC_HAS_ADB
:
2719 return put_user(pmu_has_adb
, argp
);
2724 static struct file_operations pmu_device_fops
= {
2730 .release
= pmu_release
,
2733 static struct miscdevice pmu_device
= {
2734 PMU_MINOR
, "pmu", &pmu_device_fops
2737 static int pmu_device_init(void)
2741 if (misc_register(&pmu_device
) < 0)
2742 printk(KERN_ERR
"via-pmu: cannot register misc device.\n");
2745 device_initcall(pmu_device_init
);
2750 polled_handshake(volatile unsigned char __iomem
*via
)
2752 via
[B
] &= ~TREQ
; eieio();
2753 while ((via
[B
] & TACK
) != 0)
2755 via
[B
] |= TREQ
; eieio();
2756 while ((via
[B
] & TACK
) == 0)
2761 polled_send_byte(volatile unsigned char __iomem
*via
, int x
)
2763 via
[ACR
] |= SR_OUT
| SR_EXT
; eieio();
2764 via
[SR
] = x
; eieio();
2765 polled_handshake(via
);
2769 polled_recv_byte(volatile unsigned char __iomem
*via
)
2773 via
[ACR
] = (via
[ACR
] & ~SR_OUT
) | SR_EXT
; eieio();
2774 x
= via
[SR
]; eieio();
2775 polled_handshake(via
);
2776 x
= via
[SR
]; eieio();
2781 pmu_polled_request(struct adb_request
*req
)
2783 unsigned long flags
;
2785 volatile unsigned char __iomem
*v
= via
;
2789 l
= pmu_data_len
[c
][0];
2790 if (l
>= 0 && req
->nbytes
!= l
+ 1)
2793 local_irq_save(flags
);
2794 while (pmu_state
!= idle
)
2797 while ((via
[B
] & TACK
) == 0)
2799 polled_send_byte(v
, c
);
2801 l
= req
->nbytes
- 1;
2802 polled_send_byte(v
, l
);
2804 for (i
= 1; i
<= l
; ++i
)
2805 polled_send_byte(v
, req
->data
[i
]);
2807 l
= pmu_data_len
[c
][1];
2809 l
= polled_recv_byte(v
);
2810 for (i
= 0; i
< l
; ++i
)
2811 req
->reply
[i
+ req
->reply_len
] = polled_recv_byte(v
);
2816 local_irq_restore(flags
);
2819 #endif /* DEBUG_SLEEP */
2822 /* FIXME: This is a temporary set of callbacks to enable us
2823 * to do suspend-to-disk.
2826 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2828 static int pmu_sys_suspended
= 0;
2830 static int pmu_sys_suspend(struct sys_device
*sysdev
, pm_message_t state
)
2832 if (state
.event
!= PM_EVENT_SUSPEND
|| pmu_sys_suspended
)
2835 /* Suspend PMU event interrupts */
2838 pmu_sys_suspended
= 1;
2842 static int pmu_sys_resume(struct sys_device
*sysdev
)
2844 struct adb_request req
;
2846 if (!pmu_sys_suspended
)
2849 /* Tell PMU we are ready */
2850 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
2851 pmu_wait_complete(&req
);
2853 /* Resume PMU event interrupts */
2856 pmu_sys_suspended
= 0;
2861 #endif /* CONFIG_PM && CONFIG_PPC32 */
2863 static struct sysdev_class pmu_sysclass
= {
2864 set_kset_name("pmu"),
2867 static struct sys_device device_pmu
= {
2869 .cls
= &pmu_sysclass
,
2872 static struct sysdev_driver driver_pmu
= {
2873 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2874 .suspend
= &pmu_sys_suspend
,
2875 .resume
= &pmu_sys_resume
,
2876 #endif /* CONFIG_PM && CONFIG_PPC32 */
2879 static int __init
init_pmu_sysfs(void)
2883 rc
= sysdev_class_register(&pmu_sysclass
);
2885 printk(KERN_ERR
"Failed registering PMU sys class\n");
2888 rc
= sysdev_register(&device_pmu
);
2890 printk(KERN_ERR
"Failed registering PMU sys device\n");
2893 rc
= sysdev_driver_register(&pmu_sysclass
, &driver_pmu
);
2895 printk(KERN_ERR
"Failed registering PMU sys driver\n");
2901 subsys_initcall(init_pmu_sysfs
);
2903 EXPORT_SYMBOL(pmu_request
);
2904 EXPORT_SYMBOL(pmu_queue_request
);
2905 EXPORT_SYMBOL(pmu_poll
);
2906 EXPORT_SYMBOL(pmu_poll_adb
);
2907 EXPORT_SYMBOL(pmu_wait_complete
);
2908 EXPORT_SYMBOL(pmu_suspend
);
2909 EXPORT_SYMBOL(pmu_resume
);
2910 EXPORT_SYMBOL(pmu_unlock
);
2911 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2912 EXPORT_SYMBOL(pmu_enable_irled
);
2913 EXPORT_SYMBOL(pmu_battery_count
);
2914 EXPORT_SYMBOL(pmu_batteries
);
2915 EXPORT_SYMBOL(pmu_power_flags
);
2916 #endif /* CONFIG_PM && CONFIG_PPC32 */