2 * Device driver for the via-pmu on Apple Powermacs.
4 * The VIA (versatile interface adapter) interfaces to the PMU,
5 * a 6805 microprocessor core whose primary function is to control
6 * battery charging and system power on the PowerBook 3400 and 2400.
7 * The PMU also controls the ADB (Apple Desktop Bus) which connects
8 * to the keyboard and mouse, as well as the non-volatile RAM
9 * and the RTC (real time clock) chip.
11 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
12 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
14 * THIS DRIVER IS BECOMING A TOTAL MESS !
15 * - Cleanup atomically disabling reply to PMU events after
16 * a sleep or a freq. switch
17 * - Move sleep code out of here to pmac_pm, merge into new
18 * common PM infrastructure
19 * - Move backlight code out as well
20 * - Save/Restore PCI space properly
24 #include <linux/config.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/miscdevice.h>
31 #include <linux/blkdev.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/adb.h>
36 #include <linux/pmu.h>
37 #include <linux/cuda.h>
38 #include <linux/smp_lock.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
42 #include <linux/proc_fs.h>
43 #include <linux/init.h>
44 #include <linux/interrupt.h>
45 #include <linux/device.h>
46 #include <linux/sysdev.h>
47 #include <linux/suspend.h>
48 #include <linux/syscalls.h>
49 #include <linux/cpu.h>
51 #include <asm/machdep.h>
53 #include <asm/pgtable.h>
54 #include <asm/system.h>
55 #include <asm/sections.h>
57 #include <asm/pmac_feature.h>
58 #include <asm/uaccess.h>
59 #include <asm/mmu_context.h>
60 #include <asm/cputable.h>
62 #ifdef CONFIG_PMAC_BACKLIGHT
63 #include <asm/backlight.h>
67 #include <asm/open_pic.h>
70 /* Some compile options */
71 #undef SUSPEND_USES_PMU
73 #undef HACKED_PCI_SAVE
75 /* Misc minor number allocated for /dev/pmu */
78 /* How many iterations between battery polls */
79 #define BATTERY_POLLING_COUNT 2
81 static volatile unsigned char __iomem
*via
;
83 /* VIA registers - spaced 0x200 bytes apart */
84 #define RS 0x200 /* skip between registers */
85 #define B 0 /* B-side data */
86 #define A RS /* A-side data */
87 #define DIRB (2*RS) /* B-side direction (1=output) */
88 #define DIRA (3*RS) /* A-side direction (1=output) */
89 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
90 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
91 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
92 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
93 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
94 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
95 #define SR (10*RS) /* Shift register */
96 #define ACR (11*RS) /* Auxiliary control register */
97 #define PCR (12*RS) /* Peripheral control register */
98 #define IFR (13*RS) /* Interrupt flag register */
99 #define IER (14*RS) /* Interrupt enable register */
100 #define ANH (15*RS) /* A-side data, no handshake */
102 /* Bits in B data register: both active low */
103 #define TACK 0x08 /* Transfer acknowledge (input) */
104 #define TREQ 0x10 /* Transfer request (output) */
107 #define SR_CTRL 0x1c /* Shift register control bits */
108 #define SR_EXT 0x0c /* Shift on external clock */
109 #define SR_OUT 0x10 /* Shift out if 1 */
111 /* Bits in IFR and IER */
112 #define IER_SET 0x80 /* set bits in IER */
113 #define IER_CLR 0 /* clear bits in IER */
114 #define SR_INT 0x04 /* Shift register full/empty */
116 #define CB1_INT 0x10 /* transition on CB1 input */
118 static volatile enum pmu_state
{
127 static volatile enum int_data_state
{
132 } int_data_state
[2] = { int_data_empty
, int_data_empty
};
134 static struct adb_request
*current_req
;
135 static struct adb_request
*last_req
;
136 static struct adb_request
*req_awaiting_reply
;
137 static unsigned char interrupt_data
[2][32];
138 static int interrupt_data_len
[2];
139 static int int_data_last
;
140 static unsigned char *reply_ptr
;
141 static int data_index
;
143 static volatile int adb_int_pending
;
144 static volatile int disable_poll
;
145 static struct adb_request bright_req_1
, bright_req_2
;
146 static struct device_node
*vias
;
147 static int pmu_kind
= PMU_UNKNOWN
;
148 static int pmu_fully_inited
= 0;
149 static int pmu_has_adb
;
150 static struct device_node
*gpio_node
;
151 static unsigned char __iomem
*gpio_reg
= NULL
;
152 static int gpio_irq
= -1;
153 static int gpio_irq_enabled
= -1;
154 static volatile int pmu_suspended
= 0;
155 static spinlock_t pmu_lock
;
156 static u8 pmu_intr_mask
;
157 static int pmu_version
;
158 static int drop_interrupts
;
159 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
160 static int option_lid_wakeup
= 1;
161 static int sleep_in_progress
;
162 #endif /* CONFIG_PM && CONFIG_PPC32 */
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 struct notifier_block
*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 int pmu_queue_request(struct adb_request
*req
);
201 static void pmu_start(void);
202 static irqreturn_t
via_pmu_interrupt(int irq
, void *arg
, struct pt_regs
*regs
);
203 static irqreturn_t
gpio1_interrupt(int irq
, void *arg
, struct pt_regs
*regs
);
204 static int proc_get_info(char *page
, char **start
, off_t off
,
205 int count
, int *eof
, void *data
);
206 static int proc_get_irqstats(char *page
, char **start
, off_t off
,
207 int count
, int *eof
, void *data
);
208 #ifdef CONFIG_PMAC_BACKLIGHT
209 static int pmu_set_backlight_level(int level
, void* data
);
210 static int pmu_set_backlight_enable(int on
, int level
, void* data
);
211 #endif /* CONFIG_PMAC_BACKLIGHT */
212 static void pmu_pass_intr(unsigned char *data
, int len
);
213 static int proc_get_batt(char *page
, char **start
, off_t off
,
214 int count
, int *eof
, void *data
);
215 static int proc_read_options(char *page
, char **start
, off_t off
,
216 int count
, int *eof
, void *data
);
217 static int proc_write_options(struct file
*file
, const char __user
*buffer
,
218 unsigned long count
, void *data
);
221 struct adb_driver via_pmu_driver
= {
230 #endif /* CONFIG_ADB */
232 extern void low_sleep_handler(void);
233 extern void enable_kernel_altivec(void);
234 extern void enable_kernel_fp(void);
237 int pmu_polled_request(struct adb_request
*req
);
238 int pmu_wink(struct adb_request
*req
);
242 * This table indicates for each PMU opcode:
243 * - the number of data bytes to be sent with the command, or -1
244 * if a length byte should be sent,
245 * - the number of response bytes which the PMU will return, or
246 * -1 if it will send a length byte.
248 static const s8 pmu_data_len
[256][2] = {
249 /* 0 1 2 3 4 5 6 7 */
250 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
251 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
252 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
253 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
254 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
255 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
256 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
257 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
258 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
259 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
260 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
261 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
262 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
263 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
264 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
265 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
266 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
267 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
268 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
269 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
270 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
271 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
272 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
273 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
274 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
275 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
276 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
277 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
278 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
279 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
280 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
281 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
284 static char *pbook_type
[] = {
286 "PowerBook 2400/3400/3500(G3)",
287 "PowerBook G3 Series",
292 #ifdef CONFIG_PMAC_BACKLIGHT
293 static struct backlight_controller pmu_backlight_controller
= {
294 pmu_set_backlight_enable
,
295 pmu_set_backlight_level
297 #endif /* CONFIG_PMAC_BACKLIGHT */
299 int __init
find_via_pmu(void)
306 vias
= of_find_node_by_name(NULL
, "via-pmu");
310 reg
= (u32
*)get_property(vias
, "reg", NULL
);
312 printk(KERN_ERR
"via-pmu: No \"reg\" property !\n");
315 taddr
= of_translate_address(vias
, reg
);
316 if (taddr
== OF_BAD_ADDR
) {
317 printk(KERN_ERR
"via-pmu: Can't translate address !\n");
321 spin_lock_init(&pmu_lock
);
325 pmu_intr_mask
= PMU_INT_PCEJECT
|
330 if (vias
->parent
->name
&& ((strcmp(vias
->parent
->name
, "ohare") == 0)
331 || device_is_compatible(vias
->parent
, "ohare")))
332 pmu_kind
= PMU_OHARE_BASED
;
333 else if (device_is_compatible(vias
->parent
, "paddington"))
334 pmu_kind
= PMU_PADDINGTON_BASED
;
335 else if (device_is_compatible(vias
->parent
, "heathrow"))
336 pmu_kind
= PMU_HEATHROW_BASED
;
337 else if (device_is_compatible(vias
->parent
, "Keylargo")
338 || device_is_compatible(vias
->parent
, "K2-Keylargo")) {
339 struct device_node
*gpiop
;
340 u64 gaddr
= OF_BAD_ADDR
;
342 pmu_kind
= PMU_KEYLARGO_BASED
;
343 pmu_has_adb
= (find_type_devices("adb") != NULL
);
344 pmu_intr_mask
= PMU_INT_PCEJECT
|
350 gpiop
= of_find_node_by_name(NULL
, "gpio");
352 reg
= (u32
*)get_property(gpiop
, "reg", NULL
);
354 gaddr
= of_translate_address(gpiop
, reg
);
355 if (gaddr
!= OF_BAD_ADDR
)
356 gpio_reg
= ioremap(gaddr
, 0x10);
358 if (gpio_reg
== NULL
)
359 printk(KERN_ERR
"via-pmu: Can't find GPIO reg !\n");
361 pmu_kind
= PMU_UNKNOWN
;
363 via
= ioremap(taddr
, 0x2000);
365 printk(KERN_ERR
"via-pmu: Can't map address !\n");
369 out_8(&via
[IER
], IER_CLR
| 0x7f); /* disable all intrs */
370 out_8(&via
[IFR
], 0x7f); /* clear IFR */
379 printk(KERN_INFO
"PMU driver v%d initialized for %s, firmware: %02x\n",
380 PMU_DRIVER_VERSION
, pbook_type
[pmu_kind
], pmu_version
);
382 sys_ctrler
= SYS_CTRLER_PMU
;
392 static int pmu_probe(void)
394 return vias
== NULL
? -ENODEV
: 0;
397 static int __init
pmu_init(void)
403 #endif /* CONFIG_ADB */
406 * We can't wait until pmu_init gets called, that happens too late.
407 * It happens after IDE and SCSI initialization, which can take a few
408 * seconds, and by that time the PMU could have given up on us and
410 * Thus this is called with arch_initcall rather than device_initcall.
412 static int __init
via_pmu_start(void)
417 bright_req_1
.complete
= 1;
418 bright_req_2
.complete
= 1;
419 batt_req
.complete
= 1;
421 #ifndef CONFIG_PPC_MERGE
422 if (pmu_kind
== PMU_KEYLARGO_BASED
)
423 openpic_set_irq_priority(vias
->intrs
[0].line
,
424 OPENPIC_PRIORITY_DEFAULT
+ 1);
427 if (request_irq(vias
->intrs
[0].line
, via_pmu_interrupt
, 0, "VIA-PMU",
429 printk(KERN_ERR
"VIA-PMU: can't get irq %d\n",
430 vias
->intrs
[0].line
);
434 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
435 gpio_node
= of_find_node_by_name(NULL
, "extint-gpio1");
436 if (gpio_node
== NULL
)
437 gpio_node
= of_find_node_by_name(NULL
,
439 if (gpio_node
&& gpio_node
->n_intrs
> 0)
440 gpio_irq
= gpio_node
->intrs
[0].line
;
442 if (gpio_irq
!= -1) {
443 if (request_irq(gpio_irq
, gpio1_interrupt
, 0,
444 "GPIO1 ADB", (void *)0))
445 printk(KERN_ERR
"pmu: can't get irq %d"
446 " (GPIO1)\n", gpio_irq
);
448 gpio_irq_enabled
= 1;
452 /* Enable interrupts */
453 out_8(&via
[IER
], IER_SET
| SR_INT
| CB1_INT
);
455 pmu_fully_inited
= 1;
457 /* Make sure PMU settle down before continuing. This is _very_ important
458 * since the IDE probe may shut interrupts down for quite a bit of time. If
459 * a PMU communication is pending while this happens, the PMU may timeout
460 * Not that on Core99 machines, the PMU keeps sending us environement
461 * messages, we should find a way to either fix IDE or make it call
462 * pmu_suspend() before masking interrupts. This can also happens while
463 * scolling with some fbdevs.
467 } while (pmu_state
!= idle
);
472 arch_initcall(via_pmu_start
);
475 * This has to be done after pci_init, which is a subsys_initcall.
477 static int __init
via_pmu_dev_init(void)
482 #ifdef CONFIG_PMAC_BACKLIGHT
483 /* Enable backlight */
484 register_backlight_controller(&pmu_backlight_controller
, NULL
, "pmu");
485 #endif /* CONFIG_PMAC_BACKLIGHT */
488 if (machine_is_compatible("AAPL,3400/2400") ||
489 machine_is_compatible("AAPL,3500")) {
490 int mb
= pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
491 NULL
, PMAC_MB_INFO_MODEL
, 0);
492 pmu_battery_count
= 1;
493 if (mb
== PMAC_TYPE_COMET
)
494 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_COMET
;
496 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_HOOPER
;
497 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
498 machine_is_compatible("PowerBook1,1")) {
499 pmu_battery_count
= 2;
500 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_SMART
;
501 pmu_batteries
[1].flags
|= PMU_BATT_TYPE_SMART
;
503 struct device_node
* prim
= find_devices("power-mgt");
504 u32
*prim_info
= NULL
;
506 prim_info
= (u32
*)get_property(prim
, "prim-info", NULL
);
508 /* Other stuffs here yet unknown */
509 pmu_battery_count
= (prim_info
[6] >> 16) & 0xff;
510 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_SMART
;
511 if (pmu_battery_count
> 1)
512 pmu_batteries
[1].flags
|= PMU_BATT_TYPE_SMART
;
515 #endif /* CONFIG_PPC32 */
517 /* Create /proc/pmu */
518 proc_pmu_root
= proc_mkdir("pmu", NULL
);
522 for (i
=0; i
<pmu_battery_count
; i
++) {
524 sprintf(title
, "battery_%ld", i
);
525 proc_pmu_batt
[i
] = create_proc_read_entry(title
, 0, proc_pmu_root
,
526 proc_get_batt
, (void *)i
);
529 proc_pmu_info
= create_proc_read_entry("info", 0, proc_pmu_root
,
530 proc_get_info
, NULL
);
531 proc_pmu_irqstats
= create_proc_read_entry("interrupts", 0, proc_pmu_root
,
532 proc_get_irqstats
, NULL
);
533 proc_pmu_options
= create_proc_entry("options", 0600, proc_pmu_root
);
534 if (proc_pmu_options
) {
535 proc_pmu_options
->nlink
= 1;
536 proc_pmu_options
->read_proc
= proc_read_options
;
537 proc_pmu_options
->write_proc
= proc_write_options
;
543 device_initcall(via_pmu_dev_init
);
549 struct adb_request req
;
551 out_8(&via
[B
], via
[B
] | TREQ
); /* negate TREQ */
552 out_8(&via
[DIRB
], (via
[DIRB
] | TREQ
) & ~TACK
); /* TACK in, TREQ out */
554 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
556 while (!req
.complete
) {
558 printk(KERN_ERR
"init_pmu: no response from PMU\n");
565 /* ack all pending interrupts */
567 interrupt_data
[0][0] = 1;
568 while (interrupt_data
[0][0] || pmu_state
!= idle
) {
570 printk(KERN_ERR
"init_pmu: timed out acking intrs\n");
573 if (pmu_state
== idle
)
575 via_pmu_interrupt(0, NULL
, NULL
);
579 /* Tell PMU we are ready. */
580 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
581 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
582 while (!req
.complete
)
586 /* Read PMU version */
587 pmu_request(&req
, NULL
, 1, PMU_GET_VERSION
);
588 pmu_wait_complete(&req
);
589 if (req
.reply_len
> 0)
590 pmu_version
= req
.reply
[0];
592 /* Read server mode setting */
593 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
594 pmu_request(&req
, NULL
, 2, PMU_POWER_EVENTS
,
595 PMU_PWR_GET_POWERUP_EVENTS
);
596 pmu_wait_complete(&req
);
597 if (req
.reply_len
== 2) {
598 if (req
.reply
[1] & PMU_PWR_WAKEUP_AC_INSERT
)
599 option_server_mode
= 1;
600 printk(KERN_INFO
"via-pmu: Server Mode is %s\n",
601 option_server_mode
? "enabled" : "disabled");
613 static void pmu_set_server_mode(int server_mode
)
615 struct adb_request req
;
617 if (pmu_kind
!= PMU_KEYLARGO_BASED
)
620 option_server_mode
= server_mode
;
621 pmu_request(&req
, NULL
, 2, PMU_POWER_EVENTS
, PMU_PWR_GET_POWERUP_EVENTS
);
622 pmu_wait_complete(&req
);
623 if (req
.reply_len
< 2)
626 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
,
627 PMU_PWR_SET_POWERUP_EVENTS
,
628 req
.reply
[0], PMU_PWR_WAKEUP_AC_INSERT
);
630 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
,
631 PMU_PWR_CLR_POWERUP_EVENTS
,
632 req
.reply
[0], PMU_PWR_WAKEUP_AC_INSERT
);
633 pmu_wait_complete(&req
);
636 /* This new version of the code for 2400/3400/3500 powerbooks
637 * is inspired from the implementation in gkrellm-pmu
640 done_battery_state_ohare(struct adb_request
* req
)
644 * 0x01 : AC indicator
646 * 0x04 : battery exist
649 * 0x20 : full charged
650 * 0x40 : pcharge reset
651 * 0x80 : battery exist
653 * [1][2] : battery voltage
654 * [3] : CPU temperature
655 * [4] : battery temperature
660 unsigned int bat_flags
= PMU_BATT_TYPE_HOOPER
;
661 long pcharge
, charge
, vb
, vmax
, lmax
;
662 long vmax_charging
, vmax_charged
;
663 long amperage
, voltage
, time
, max
;
664 int mb
= pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
665 NULL
, PMAC_MB_INFO_MODEL
, 0);
667 if (req
->reply
[0] & 0x01)
668 pmu_power_flags
|= PMU_PWR_AC_PRESENT
;
670 pmu_power_flags
&= ~PMU_PWR_AC_PRESENT
;
672 if (mb
== PMAC_TYPE_COMET
) {
683 /* If battery installed */
684 if (req
->reply
[0] & 0x04) {
685 bat_flags
|= PMU_BATT_PRESENT
;
686 if (req
->reply
[0] & 0x02)
687 bat_flags
|= PMU_BATT_CHARGING
;
688 vb
= (req
->reply
[1] << 8) | req
->reply
[2];
689 voltage
= (vb
* 265 + 72665) / 10;
690 amperage
= req
->reply
[5];
691 if ((req
->reply
[0] & 0x01) == 0) {
693 vb
+= ((amperage
- 200) * 15)/100;
694 } else if (req
->reply
[0] & 0x02) {
695 vb
= (vb
* 97) / 100;
696 vmax
= vmax_charging
;
698 charge
= (100 * vb
) / vmax
;
699 if (req
->reply
[0] & 0x40) {
700 pcharge
= (req
->reply
[6] << 8) + req
->reply
[7];
704 pcharge
= 100 - pcharge
/ lmax
;
705 if (pcharge
< charge
)
709 time
= (charge
* 16440) / amperage
;
713 amperage
= -amperage
;
715 charge
= max
= amperage
= voltage
= time
= 0;
717 pmu_batteries
[pmu_cur_battery
].flags
= bat_flags
;
718 pmu_batteries
[pmu_cur_battery
].charge
= charge
;
719 pmu_batteries
[pmu_cur_battery
].max_charge
= max
;
720 pmu_batteries
[pmu_cur_battery
].amperage
= amperage
;
721 pmu_batteries
[pmu_cur_battery
].voltage
= voltage
;
722 pmu_batteries
[pmu_cur_battery
].time_remaining
= time
;
724 clear_bit(0, &async_req_locks
);
728 done_battery_state_smart(struct adb_request
* req
)
731 * [0] : format of this structure (known: 3,4,5)
744 * [4][5] : max charge
749 unsigned int bat_flags
= PMU_BATT_TYPE_SMART
;
751 unsigned int capa
, max
, voltage
;
753 if (req
->reply
[1] & 0x01)
754 pmu_power_flags
|= PMU_PWR_AC_PRESENT
;
756 pmu_power_flags
&= ~PMU_PWR_AC_PRESENT
;
759 capa
= max
= amperage
= voltage
= 0;
761 if (req
->reply
[1] & 0x04) {
762 bat_flags
|= PMU_BATT_PRESENT
;
763 switch(req
->reply
[0]) {
765 case 4: capa
= req
->reply
[2];
767 amperage
= *((signed char *)&req
->reply
[4]);
768 voltage
= req
->reply
[5];
770 case 5: capa
= (req
->reply
[2] << 8) | req
->reply
[3];
771 max
= (req
->reply
[4] << 8) | req
->reply
[5];
772 amperage
= *((signed short *)&req
->reply
[6]);
773 voltage
= (req
->reply
[8] << 8) | req
->reply
[9];
776 printk(KERN_WARNING
"pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
777 req
->reply_len
, req
->reply
[0], req
->reply
[1], req
->reply
[2], req
->reply
[3]);
782 if ((req
->reply
[1] & 0x01) && (amperage
> 0))
783 bat_flags
|= PMU_BATT_CHARGING
;
785 pmu_batteries
[pmu_cur_battery
].flags
= bat_flags
;
786 pmu_batteries
[pmu_cur_battery
].charge
= capa
;
787 pmu_batteries
[pmu_cur_battery
].max_charge
= max
;
788 pmu_batteries
[pmu_cur_battery
].amperage
= amperage
;
789 pmu_batteries
[pmu_cur_battery
].voltage
= voltage
;
791 if ((req
->reply
[1] & 0x01) && (amperage
> 0))
792 pmu_batteries
[pmu_cur_battery
].time_remaining
793 = ((max
-capa
) * 3600) / amperage
;
795 pmu_batteries
[pmu_cur_battery
].time_remaining
796 = (capa
* 3600) / (-amperage
);
798 pmu_batteries
[pmu_cur_battery
].time_remaining
= 0;
800 pmu_cur_battery
= (pmu_cur_battery
+ 1) % pmu_battery_count
;
802 clear_bit(0, &async_req_locks
);
806 query_battery_state(void)
808 if (test_and_set_bit(0, &async_req_locks
))
810 if (pmu_kind
== PMU_OHARE_BASED
)
811 pmu_request(&batt_req
, done_battery_state_ohare
,
812 1, PMU_BATTERY_STATE
);
814 pmu_request(&batt_req
, done_battery_state_smart
,
815 2, PMU_SMART_BATTERY_STATE
, pmu_cur_battery
+1);
819 proc_get_info(char *page
, char **start
, off_t off
,
820 int count
, int *eof
, void *data
)
824 p
+= sprintf(p
, "PMU driver version : %d\n", PMU_DRIVER_VERSION
);
825 p
+= sprintf(p
, "PMU firmware version : %02x\n", pmu_version
);
826 p
+= sprintf(p
, "AC Power : %d\n",
827 ((pmu_power_flags
& PMU_PWR_AC_PRESENT
) != 0));
828 p
+= sprintf(p
, "Battery count : %d\n", pmu_battery_count
);
834 proc_get_irqstats(char *page
, char **start
, off_t off
,
835 int count
, int *eof
, void *data
)
839 static const char *irq_names
[] = {
840 "Total CB1 triggered events",
841 "Total GPIO1 triggered events",
842 "PC-Card eject button",
843 "Sound/Brightness button",
845 "Battery state change",
846 "Environment interrupt",
848 "Ghost interrupt (zero len)",
849 "Empty interrupt (empty mask)",
853 for (i
=0; i
<11; i
++) {
854 p
+= sprintf(p
, " %2u: %10u (%s)\n",
855 i
, pmu_irq_stats
[i
], irq_names
[i
]);
861 proc_get_batt(char *page
, char **start
, off_t off
,
862 int count
, int *eof
, void *data
)
864 long batnum
= (long)data
;
867 p
+= sprintf(p
, "\n");
868 p
+= sprintf(p
, "flags : %08x\n",
869 pmu_batteries
[batnum
].flags
);
870 p
+= sprintf(p
, "charge : %d\n",
871 pmu_batteries
[batnum
].charge
);
872 p
+= sprintf(p
, "max_charge : %d\n",
873 pmu_batteries
[batnum
].max_charge
);
874 p
+= sprintf(p
, "current : %d\n",
875 pmu_batteries
[batnum
].amperage
);
876 p
+= sprintf(p
, "voltage : %d\n",
877 pmu_batteries
[batnum
].voltage
);
878 p
+= sprintf(p
, "time rem. : %d\n",
879 pmu_batteries
[batnum
].time_remaining
);
885 proc_read_options(char *page
, char **start
, off_t off
,
886 int count
, int *eof
, void *data
)
890 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
891 if (pmu_kind
== PMU_KEYLARGO_BASED
&&
892 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) >= 0)
893 p
+= sprintf(p
, "lid_wakeup=%d\n", option_lid_wakeup
);
895 if (pmu_kind
== PMU_KEYLARGO_BASED
)
896 p
+= sprintf(p
, "server_mode=%d\n", option_server_mode
);
902 proc_write_options(struct file
*file
, const char __user
*buffer
,
903 unsigned long count
, void *data
)
907 unsigned long fcount
= count
;
913 if (copy_from_user(tmp
, buffer
, count
))
921 while(*val
&& (*val
!= '=')) {
931 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
932 if (pmu_kind
== PMU_KEYLARGO_BASED
&&
933 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) >= 0)
934 if (!strcmp(label
, "lid_wakeup"))
935 option_lid_wakeup
= ((*val
) == '1');
937 if (pmu_kind
== PMU_KEYLARGO_BASED
&& !strcmp(label
, "server_mode")) {
939 new_value
= ((*val
) == '1');
940 if (new_value
!= option_server_mode
)
941 pmu_set_server_mode(new_value
);
947 /* Send an ADB command */
949 pmu_send_request(struct adb_request
*req
, int sync
)
953 if ((vias
== NULL
) || (!pmu_fully_inited
)) {
960 switch (req
->data
[0]) {
962 for (i
= 0; i
< req
->nbytes
- 1; ++i
)
963 req
->data
[i
] = req
->data
[i
+1];
965 if (pmu_data_len
[req
->data
[0]][1] != 0) {
966 req
->reply
[0] = ADB_RET_OK
;
970 ret
= pmu_queue_request(req
);
973 switch (req
->data
[1]) {
975 if (req
->nbytes
!= 2)
977 req
->data
[0] = PMU_READ_RTC
;
980 req
->reply
[0] = CUDA_PACKET
;
982 req
->reply
[2] = CUDA_GET_TIME
;
983 ret
= pmu_queue_request(req
);
986 if (req
->nbytes
!= 6)
988 req
->data
[0] = PMU_SET_RTC
;
990 for (i
= 1; i
<= 4; ++i
)
991 req
->data
[i
] = req
->data
[i
+1];
993 req
->reply
[0] = CUDA_PACKET
;
995 req
->reply
[2] = CUDA_SET_TIME
;
996 ret
= pmu_queue_request(req
);
1003 for (i
= req
->nbytes
- 1; i
> 1; --i
)
1004 req
->data
[i
+2] = req
->data
[i
];
1005 req
->data
[3] = req
->nbytes
- 2;
1006 req
->data
[2] = pmu_adb_flags
;
1007 /*req->data[1] = req->data[1];*/
1008 req
->data
[0] = PMU_ADB_CMD
;
1010 req
->reply_expected
= 1;
1012 ret
= pmu_queue_request(req
);
1021 while (!req
->complete
)
1027 /* Enable/disable autopolling */
1029 pmu_adb_autopoll(int devs
)
1031 struct adb_request req
;
1033 if ((vias
== NULL
) || (!pmu_fully_inited
) || !pmu_has_adb
)
1038 pmu_request(&req
, NULL
, 5, PMU_ADB_CMD
, 0, 0x86,
1039 adb_dev_map
>> 8, adb_dev_map
);
1042 pmu_request(&req
, NULL
, 1, PMU_ADB_POLL_OFF
);
1045 while (!req
.complete
)
1050 /* Reset the ADB bus */
1052 pmu_adb_reset_bus(void)
1054 struct adb_request req
;
1055 int save_autopoll
= adb_dev_map
;
1057 if ((vias
== NULL
) || (!pmu_fully_inited
) || !pmu_has_adb
)
1060 /* anyone got a better idea?? */
1061 pmu_adb_autopoll(0);
1065 req
.data
[0] = PMU_ADB_CMD
;
1067 req
.data
[2] = ADB_BUSRESET
;
1071 req
.reply_expected
= 1;
1072 if (pmu_queue_request(&req
) != 0) {
1073 printk(KERN_ERR
"pmu_adb_reset_bus: pmu_queue_request failed\n");
1076 pmu_wait_complete(&req
);
1078 if (save_autopoll
!= 0)
1079 pmu_adb_autopoll(save_autopoll
);
1083 #endif /* CONFIG_ADB */
1085 /* Construct and send a pmu request */
1087 pmu_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
1096 if (nbytes
< 0 || nbytes
> 32) {
1097 printk(KERN_ERR
"pmu_request: bad nbytes (%d)\n", nbytes
);
1101 req
->nbytes
= nbytes
;
1103 va_start(list
, nbytes
);
1104 for (i
= 0; i
< nbytes
; ++i
)
1105 req
->data
[i
] = va_arg(list
, int);
1108 req
->reply_expected
= 0;
1109 return pmu_queue_request(req
);
1113 pmu_queue_request(struct adb_request
*req
)
1115 unsigned long flags
;
1122 if (req
->nbytes
<= 0) {
1126 nsend
= pmu_data_len
[req
->data
[0]][0];
1127 if (nsend
>= 0 && req
->nbytes
!= nsend
+ 1) {
1136 spin_lock_irqsave(&pmu_lock
, flags
);
1137 if (current_req
!= 0) {
1138 last_req
->next
= req
;
1143 if (pmu_state
== idle
)
1146 spin_unlock_irqrestore(&pmu_lock
, flags
);
1154 /* Sightly increased the delay, I had one occurrence of the message
1158 while ((in_8(&via
[B
]) & TACK
) == 0) {
1159 if (--timeout
< 0) {
1160 printk(KERN_ERR
"PMU not responding (!ack)\n");
1167 /* New PMU seems to be very sensitive to those timings, so we make sure
1168 * PCI is flushed immediately */
1172 volatile unsigned char __iomem
*v
= via
;
1174 out_8(&v
[ACR
], in_8(&v
[ACR
]) | SR_OUT
| SR_EXT
);
1176 out_8(&v
[B
], in_8(&v
[B
]) & ~TREQ
); /* assert TREQ */
1183 volatile unsigned char __iomem
*v
= via
;
1185 out_8(&v
[ACR
], (in_8(&v
[ACR
]) & ~SR_OUT
) | SR_EXT
);
1186 in_8(&v
[SR
]); /* resets SR */
1187 out_8(&v
[B
], in_8(&v
[B
]) & ~TREQ
);
1192 pmu_done(struct adb_request
*req
)
1194 void (*done
)(struct adb_request
*) = req
->done
;
1197 /* Here, we assume that if the request has a done member, the
1198 * struct request will survive to setting req->complete to 1
1207 struct adb_request
*req
;
1209 /* assert pmu_state == idle */
1210 /* get the packet to send */
1212 if (req
== 0 || pmu_state
!= idle
1213 || (/*req->reply_expected && */req_awaiting_reply
))
1216 pmu_state
= sending
;
1218 data_len
= pmu_data_len
[req
->data
[0]][0];
1220 /* Sounds safer to make sure ACK is high before writing. This helped
1221 * kill a problem with ADB and some iBooks
1224 /* set the shift register to shift out and send a byte */
1225 send_byte(req
->data
[0]);
1235 via_pmu_interrupt(0, NULL
, NULL
);
1245 /* Kicks ADB read when PMU is suspended */
1246 adb_int_pending
= 1;
1248 via_pmu_interrupt(0, NULL
, NULL
);
1249 } while (pmu_suspended
&& (adb_int_pending
|| pmu_state
!= idle
1250 || req_awaiting_reply
));
1254 pmu_wait_complete(struct adb_request
*req
)
1258 while((pmu_state
!= idle
&& pmu_state
!= locked
) || !req
->complete
)
1259 via_pmu_interrupt(0, NULL
, NULL
);
1262 /* This function loops until the PMU is idle and prevents it from
1263 * anwsering to ADB interrupts. pmu_request can still be called.
1264 * This is done to avoid spurrious shutdowns when we know we'll have
1265 * interrupts switched off for a long time
1270 unsigned long flags
;
1271 #ifdef SUSPEND_USES_PMU
1272 struct adb_request
*req
;
1277 spin_lock_irqsave(&pmu_lock
, flags
);
1279 if (pmu_suspended
> 1) {
1280 spin_unlock_irqrestore(&pmu_lock
, flags
);
1285 spin_unlock_irqrestore(&pmu_lock
, flags
);
1286 if (req_awaiting_reply
)
1287 adb_int_pending
= 1;
1288 via_pmu_interrupt(0, NULL
, NULL
);
1289 spin_lock_irqsave(&pmu_lock
, flags
);
1290 if (!adb_int_pending
&& pmu_state
== idle
&& !req_awaiting_reply
) {
1291 #ifdef SUSPEND_USES_PMU
1292 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, 0);
1293 spin_unlock_irqrestore(&pmu_lock
, flags
);
1294 while(!req
.complete
)
1296 #else /* SUSPEND_USES_PMU */
1298 disable_irq_nosync(gpio_irq
);
1299 out_8(&via
[IER
], CB1_INT
| IER_CLR
);
1300 spin_unlock_irqrestore(&pmu_lock
, flags
);
1301 #endif /* SUSPEND_USES_PMU */
1310 unsigned long flags
;
1312 if (!via
|| (pmu_suspended
< 1))
1315 spin_lock_irqsave(&pmu_lock
, flags
);
1317 if (pmu_suspended
> 0) {
1318 spin_unlock_irqrestore(&pmu_lock
, flags
);
1321 adb_int_pending
= 1;
1322 #ifdef SUSPEND_USES_PMU
1323 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
1324 spin_unlock_irqrestore(&pmu_lock
, flags
);
1325 while(!req
.complete
)
1327 #else /* SUSPEND_USES_PMU */
1329 enable_irq(gpio_irq
);
1330 out_8(&via
[IER
], CB1_INT
| IER_SET
);
1331 spin_unlock_irqrestore(&pmu_lock
, flags
);
1333 #endif /* SUSPEND_USES_PMU */
1336 /* Interrupt data could be the result data from an ADB cmd */
1338 pmu_handle_data(unsigned char *data
, int len
, struct pt_regs
*regs
)
1340 unsigned char ints
, pirq
;
1344 if (drop_interrupts
|| len
< 1) {
1345 adb_int_pending
= 0;
1350 /* Get PMU interrupt mask */
1353 /* Record zero interrupts for stats */
1357 /* Hack to deal with ADB autopoll flag */
1358 if (ints
& PMU_INT_ADB
)
1359 ints
&= ~(PMU_INT_ADB_AUTO
| PMU_INT_AUTO_SRQ_POLL
);
1364 if (i
> pmu_irq_stats
[10])
1365 pmu_irq_stats
[10] = i
;
1369 for (pirq
= 0; pirq
< 8; pirq
++)
1370 if (ints
& (1 << pirq
))
1372 pmu_irq_stats
[pirq
]++;
1374 ints
&= ~(1 << pirq
);
1376 /* Note: for some reason, we get an interrupt with len=1,
1377 * data[0]==0 after each normal ADB interrupt, at least
1378 * on the Pismo. Still investigating... --BenH
1380 if ((1 << pirq
) & PMU_INT_ADB
) {
1381 if ((data
[0] & PMU_INT_ADB_AUTO
) == 0) {
1382 struct adb_request
*req
= req_awaiting_reply
;
1384 printk(KERN_ERR
"PMU: extra ADB reply\n");
1387 req_awaiting_reply
= NULL
;
1391 memcpy(req
->reply
, data
+ 1, len
- 1);
1392 req
->reply_len
= len
- 1;
1396 if (len
== 4 && data
[1] == 0x2c) {
1397 extern int xmon_wants_key
, xmon_adb_keycode
;
1398 if (xmon_wants_key
) {
1399 xmon_adb_keycode
= data
[2];
1405 * XXX On the [23]400 the PMU gives us an up
1406 * event for keycodes 0x74 or 0x75 when the PC
1407 * card eject buttons are released, so we
1408 * ignore those events.
1410 if (!(pmu_kind
== PMU_OHARE_BASED
&& len
== 4
1411 && data
[1] == 0x2c && data
[3] == 0xff
1412 && (data
[2] & ~1) == 0xf4))
1413 adb_input(data
+1, len
-1, regs
, 1);
1414 #endif /* CONFIG_ADB */
1417 /* Sound/brightness button pressed */
1418 else if ((1 << pirq
) & PMU_INT_SNDBRT
) {
1419 #ifdef CONFIG_PMAC_BACKLIGHT
1421 #ifdef CONFIG_INPUT_ADBHID
1422 if (!disable_kernel_backlight
)
1423 #endif /* CONFIG_INPUT_ADBHID */
1424 set_backlight_level(data
[1] >> 4);
1425 #endif /* CONFIG_PMAC_BACKLIGHT */
1427 /* Tick interrupt */
1428 else if ((1 << pirq
) & PMU_INT_TICK
) {
1429 /* Environement or tick interrupt, query batteries */
1430 if (pmu_battery_count
) {
1431 if ((--query_batt_timer
) == 0) {
1432 query_battery_state();
1433 query_batt_timer
= BATTERY_POLLING_COUNT
;
1437 else if ((1 << pirq
) & PMU_INT_ENVIRONMENT
) {
1438 if (pmu_battery_count
)
1439 query_battery_state();
1440 pmu_pass_intr(data
, len
);
1442 pmu_pass_intr(data
, len
);
1447 static struct adb_request
*
1448 pmu_sr_intr(struct pt_regs
*regs
)
1450 struct adb_request
*req
;
1453 if (via
[B
] & TREQ
) {
1454 printk(KERN_ERR
"PMU: spurious SR intr (%x)\n", via
[B
]);
1455 out_8(&via
[IFR
], SR_INT
);
1458 /* The ack may not yet be low when we get the interrupt */
1459 while ((in_8(&via
[B
]) & TACK
) != 0)
1462 /* if reading grab the byte, and reset the interrupt */
1463 if (pmu_state
== reading
|| pmu_state
== reading_intr
)
1464 bite
= in_8(&via
[SR
]);
1466 /* reset TREQ and wait for TACK to go high */
1467 out_8(&via
[B
], in_8(&via
[B
]) | TREQ
);
1470 switch (pmu_state
) {
1474 data_len
= req
->nbytes
- 1;
1475 send_byte(data_len
);
1478 if (data_index
<= data_len
) {
1479 send_byte(req
->data
[data_index
++]);
1483 data_len
= pmu_data_len
[req
->data
[0]][1];
1484 if (data_len
== 0) {
1486 current_req
= req
->next
;
1487 if (req
->reply_expected
)
1488 req_awaiting_reply
= req
;
1492 pmu_state
= reading
;
1494 reply_ptr
= req
->reply
+ req
->reply_len
;
1502 pmu_state
= reading_intr
;
1503 reply_ptr
= interrupt_data
[int_data_last
];
1505 if (gpio_irq
>= 0 && !gpio_irq_enabled
) {
1506 enable_irq(gpio_irq
);
1507 gpio_irq_enabled
= 1;
1513 if (data_len
== -1) {
1516 printk(KERN_ERR
"PMU: bad reply len %d\n", bite
);
1517 } else if (data_index
< 32) {
1518 reply_ptr
[data_index
++] = bite
;
1520 if (data_index
< data_len
) {
1525 if (pmu_state
== reading_intr
) {
1527 int_data_state
[int_data_last
] = int_data_ready
;
1528 interrupt_data_len
[int_data_last
] = data_len
;
1532 * For PMU sleep and freq change requests, we lock the
1533 * PMU until it's explicitely unlocked. This avoids any
1534 * spurrious event polling getting in
1536 current_req
= req
->next
;
1537 req
->reply_len
+= data_index
;
1538 if (req
->data
[0] == PMU_SLEEP
|| req
->data
[0] == PMU_CPU_SPEED
)
1547 printk(KERN_ERR
"via_pmu_interrupt: unknown state %d?\n",
1554 via_pmu_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
1556 unsigned long flags
;
1560 struct adb_request
*req
= NULL
;
1563 /* This is a bit brutal, we can probably do better */
1564 spin_lock_irqsave(&pmu_lock
, flags
);
1568 intr
= in_8(&via
[IFR
]) & (SR_INT
| CB1_INT
);
1572 if (++nloop
> 1000) {
1573 printk(KERN_DEBUG
"PMU: stuck in intr loop, "
1574 "intr=%x, ier=%x pmu_state=%d\n",
1575 intr
, in_8(&via
[IER
]), pmu_state
);
1578 out_8(&via
[IFR
], intr
);
1579 if (intr
& CB1_INT
) {
1580 adb_int_pending
= 1;
1583 if (intr
& SR_INT
) {
1584 req
= pmu_sr_intr(regs
);
1591 if (pmu_state
== idle
) {
1592 if (adb_int_pending
) {
1593 if (int_data_state
[0] == int_data_empty
)
1595 else if (int_data_state
[1] == int_data_empty
)
1600 int_data_state
[int_data_last
] = int_data_fill
;
1601 /* Sounds safer to make sure ACK is high before writing.
1602 * This helped kill a problem with ADB and some iBooks
1605 send_byte(PMU_INT_ACK
);
1606 adb_int_pending
= 0;
1607 } else if (current_req
)
1611 /* Mark the oldest buffer for flushing */
1612 if (int_data_state
[!int_data_last
] == int_data_ready
) {
1613 int_data_state
[!int_data_last
] = int_data_flush
;
1614 int_data
= !int_data_last
;
1615 } else if (int_data_state
[int_data_last
] == int_data_ready
) {
1616 int_data_state
[int_data_last
] = int_data_flush
;
1617 int_data
= int_data_last
;
1620 spin_unlock_irqrestore(&pmu_lock
, flags
);
1622 /* Deal with completed PMU requests outside of the lock */
1628 /* Deal with interrupt datas outside of the lock */
1629 if (int_data
>= 0) {
1630 pmu_handle_data(interrupt_data
[int_data
], interrupt_data_len
[int_data
], regs
);
1631 spin_lock_irqsave(&pmu_lock
, flags
);
1633 int_data_state
[int_data
] = int_data_empty
;
1638 return IRQ_RETVAL(handled
);
1644 unsigned long flags
;
1646 spin_lock_irqsave(&pmu_lock
, flags
);
1647 if (pmu_state
== locked
)
1649 adb_int_pending
= 1;
1650 spin_unlock_irqrestore(&pmu_lock
, flags
);
1655 gpio1_interrupt(int irq
, void *arg
, struct pt_regs
*regs
)
1657 unsigned long flags
;
1659 if ((in_8(gpio_reg
+ 0x9) & 0x02) == 0) {
1660 spin_lock_irqsave(&pmu_lock
, flags
);
1661 if (gpio_irq_enabled
> 0) {
1662 disable_irq_nosync(gpio_irq
);
1663 gpio_irq_enabled
= 0;
1666 adb_int_pending
= 1;
1667 spin_unlock_irqrestore(&pmu_lock
, flags
);
1668 via_pmu_interrupt(0, NULL
, NULL
);
1674 #ifdef CONFIG_PMAC_BACKLIGHT
1675 static int backlight_to_bright
[] = {
1676 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1677 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1681 pmu_set_backlight_enable(int on
, int level
, void* data
)
1683 struct adb_request req
;
1689 pmu_request(&req
, NULL
, 2, PMU_BACKLIGHT_BRIGHT
,
1690 backlight_to_bright
[level
]);
1691 pmu_wait_complete(&req
);
1693 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
1694 PMU_POW_BACKLIGHT
| (on
? PMU_POW_ON
: PMU_POW_OFF
));
1695 pmu_wait_complete(&req
);
1701 pmu_bright_complete(struct adb_request
*req
)
1703 if (req
== &bright_req_1
)
1704 clear_bit(1, &async_req_locks
);
1705 if (req
== &bright_req_2
)
1706 clear_bit(2, &async_req_locks
);
1710 pmu_set_backlight_level(int level
, void* data
)
1715 if (test_and_set_bit(1, &async_req_locks
))
1717 pmu_request(&bright_req_1
, pmu_bright_complete
, 2, PMU_BACKLIGHT_BRIGHT
,
1718 backlight_to_bright
[level
]);
1719 if (test_and_set_bit(2, &async_req_locks
))
1721 pmu_request(&bright_req_2
, pmu_bright_complete
, 2, PMU_POWER_CTRL
,
1722 PMU_POW_BACKLIGHT
| (level
> BACKLIGHT_OFF
?
1723 PMU_POW_ON
: PMU_POW_OFF
));
1727 #endif /* CONFIG_PMAC_BACKLIGHT */
1730 pmu_enable_irled(int on
)
1732 struct adb_request req
;
1736 if (pmu_kind
== PMU_KEYLARGO_BASED
)
1739 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
, PMU_POW_IRLED
|
1740 (on
? PMU_POW_ON
: PMU_POW_OFF
));
1741 pmu_wait_complete(&req
);
1747 struct adb_request req
;
1752 local_irq_disable();
1754 drop_interrupts
= 1;
1756 if (pmu_kind
!= PMU_KEYLARGO_BASED
) {
1757 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|
1759 while(!req
.complete
)
1763 pmu_request(&req
, NULL
, 1, PMU_RESET
);
1764 pmu_wait_complete(&req
);
1772 struct adb_request req
;
1777 local_irq_disable();
1779 drop_interrupts
= 1;
1781 if (pmu_kind
!= PMU_KEYLARGO_BASED
) {
1782 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|
1784 pmu_wait_complete(&req
);
1786 /* Disable server mode on shutdown or we'll just
1789 pmu_set_server_mode(0);
1792 pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
,
1793 'M', 'A', 'T', 'T');
1794 pmu_wait_complete(&req
);
1805 struct pmu_i2c_hdr
{
1816 pmu_i2c_combined_read(int bus
, int addr
, int subaddr
, u8
* data
, int len
)
1818 struct adb_request req
;
1819 struct pmu_i2c_hdr
*hdr
= (struct pmu_i2c_hdr
*)&req
.data
[1];
1823 for (retry
=0; retry
<16; retry
++) {
1824 memset(&req
, 0, sizeof(req
));
1827 hdr
->address
= addr
& 0xfe;
1828 hdr
->mode
= PMU_I2C_MODE_COMBINED
;
1830 hdr
->sub_addr
= subaddr
;
1831 hdr
->comb_addr
= addr
| 1;
1834 req
.nbytes
= sizeof(struct pmu_i2c_hdr
) + 1;
1835 req
.reply_expected
= 0;
1837 req
.data
[0] = PMU_I2C_CMD
;
1838 req
.reply
[0] = 0xff;
1839 rc
= pmu_queue_request(&req
);
1842 while(!req
.complete
)
1844 if (req
.reply
[0] == PMU_I2C_STATUS_OK
)
1848 if (req
.reply
[0] != PMU_I2C_STATUS_OK
)
1851 for (retry
=0; retry
<16; retry
++) {
1852 memset(&req
, 0, sizeof(req
));
1856 hdr
->bus
= PMU_I2C_BUS_STATUS
;
1857 req
.reply
[0] = 0xff;
1860 req
.reply_expected
= 0;
1862 req
.data
[0] = PMU_I2C_CMD
;
1863 rc
= pmu_queue_request(&req
);
1866 while(!req
.complete
)
1868 if (req
.reply
[0] == PMU_I2C_STATUS_DATAREAD
) {
1869 memcpy(data
, &req
.reply
[1], req
.reply_len
- 1);
1870 return req
.reply_len
- 1;
1877 pmu_i2c_stdsub_write(int bus
, int addr
, int subaddr
, u8
* data
, int len
)
1879 struct adb_request req
;
1880 struct pmu_i2c_hdr
*hdr
= (struct pmu_i2c_hdr
*)&req
.data
[1];
1884 for (retry
=0; retry
<16; retry
++) {
1885 memset(&req
, 0, sizeof(req
));
1888 hdr
->address
= addr
& 0xfe;
1889 hdr
->mode
= PMU_I2C_MODE_STDSUB
;
1891 hdr
->sub_addr
= subaddr
;
1892 hdr
->comb_addr
= addr
& 0xfe;
1895 req
.data
[0] = PMU_I2C_CMD
;
1896 memcpy(&req
.data
[sizeof(struct pmu_i2c_hdr
) + 1], data
, len
);
1897 req
.nbytes
= sizeof(struct pmu_i2c_hdr
) + len
+ 1;
1898 req
.reply_expected
= 0;
1900 req
.reply
[0] = 0xff;
1901 rc
= pmu_queue_request(&req
);
1904 while(!req
.complete
)
1906 if (req
.reply
[0] == PMU_I2C_STATUS_OK
)
1910 if (req
.reply
[0] != PMU_I2C_STATUS_OK
)
1913 for (retry
=0; retry
<16; retry
++) {
1914 memset(&req
, 0, sizeof(req
));
1918 hdr
->bus
= PMU_I2C_BUS_STATUS
;
1919 req
.reply
[0] = 0xff;
1922 req
.reply_expected
= 0;
1924 req
.data
[0] = PMU_I2C_CMD
;
1925 rc
= pmu_queue_request(&req
);
1928 while(!req
.complete
)
1930 if (req
.reply
[0] == PMU_I2C_STATUS_OK
)
1937 pmu_i2c_simple_read(int bus
, int addr
, u8
* data
, int len
)
1939 struct adb_request req
;
1940 struct pmu_i2c_hdr
*hdr
= (struct pmu_i2c_hdr
*)&req
.data
[1];
1944 for (retry
=0; retry
<16; retry
++) {
1945 memset(&req
, 0, sizeof(req
));
1948 hdr
->address
= addr
| 1;
1949 hdr
->mode
= PMU_I2C_MODE_SIMPLE
;
1955 req
.data
[0] = PMU_I2C_CMD
;
1956 req
.nbytes
= sizeof(struct pmu_i2c_hdr
) + 1;
1957 req
.reply_expected
= 0;
1959 req
.reply
[0] = 0xff;
1960 rc
= pmu_queue_request(&req
);
1963 while(!req
.complete
)
1965 if (req
.reply
[0] == PMU_I2C_STATUS_OK
)
1969 if (req
.reply
[0] != PMU_I2C_STATUS_OK
)
1972 for (retry
=0; retry
<16; retry
++) {
1973 memset(&req
, 0, sizeof(req
));
1977 hdr
->bus
= PMU_I2C_BUS_STATUS
;
1978 req
.reply
[0] = 0xff;
1981 req
.reply_expected
= 0;
1983 req
.data
[0] = PMU_I2C_CMD
;
1984 rc
= pmu_queue_request(&req
);
1987 while(!req
.complete
)
1989 if (req
.reply
[0] == PMU_I2C_STATUS_DATAREAD
) {
1990 memcpy(data
, &req
.reply
[1], req
.reply_len
- 1);
1991 return req
.reply_len
- 1;
1998 pmu_i2c_simple_write(int bus
, int addr
, u8
* data
, int len
)
2000 struct adb_request req
;
2001 struct pmu_i2c_hdr
*hdr
= (struct pmu_i2c_hdr
*)&req
.data
[1];
2005 for (retry
=0; retry
<16; retry
++) {
2006 memset(&req
, 0, sizeof(req
));
2009 hdr
->address
= addr
& 0xfe;
2010 hdr
->mode
= PMU_I2C_MODE_SIMPLE
;
2016 req
.data
[0] = PMU_I2C_CMD
;
2017 memcpy(&req
.data
[sizeof(struct pmu_i2c_hdr
) + 1], data
, len
);
2018 req
.nbytes
= sizeof(struct pmu_i2c_hdr
) + len
+ 1;
2019 req
.reply_expected
= 0;
2021 req
.reply
[0] = 0xff;
2022 rc
= pmu_queue_request(&req
);
2025 while(!req
.complete
)
2027 if (req
.reply
[0] == PMU_I2C_STATUS_OK
)
2031 if (req
.reply
[0] != PMU_I2C_STATUS_OK
)
2034 for (retry
=0; retry
<16; retry
++) {
2035 memset(&req
, 0, sizeof(req
));
2039 hdr
->bus
= PMU_I2C_BUS_STATUS
;
2040 req
.reply
[0] = 0xff;
2043 req
.reply_expected
= 0;
2045 req
.data
[0] = PMU_I2C_CMD
;
2046 rc
= pmu_queue_request(&req
);
2049 while(!req
.complete
)
2051 if (req
.reply
[0] == PMU_I2C_STATUS_OK
)
2059 static LIST_HEAD(sleep_notifiers
);
2062 pmu_register_sleep_notifier(struct pmu_sleep_notifier
*n
)
2064 struct list_head
*list
;
2065 struct pmu_sleep_notifier
*notifier
;
2067 for (list
= sleep_notifiers
.next
; list
!= &sleep_notifiers
;
2068 list
= list
->next
) {
2069 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
2070 if (n
->priority
> notifier
->priority
)
2073 __list_add(&n
->list
, list
->prev
, list
);
2076 EXPORT_SYMBOL(pmu_register_sleep_notifier
);
2079 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier
* n
)
2081 if (n
->list
.next
== 0)
2084 n
->list
.next
= NULL
;
2087 EXPORT_SYMBOL(pmu_unregister_sleep_notifier
);
2088 #endif /* CONFIG_PM */
2090 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2092 /* Sleep is broadcast last-to-first */
2094 broadcast_sleep(int when
, int fallback
)
2096 int ret
= PBOOK_SLEEP_OK
;
2097 struct list_head
*list
;
2098 struct pmu_sleep_notifier
*notifier
;
2100 for (list
= sleep_notifiers
.prev
; list
!= &sleep_notifiers
;
2101 list
= list
->prev
) {
2102 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
2103 ret
= notifier
->notifier_call(notifier
, when
);
2104 if (ret
!= PBOOK_SLEEP_OK
) {
2105 printk(KERN_DEBUG
"sleep %d rejected by %p (%p)\n",
2106 when
, notifier
, notifier
->notifier_call
);
2107 for (; list
!= &sleep_notifiers
; list
= list
->next
) {
2108 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
2109 notifier
->notifier_call(notifier
, fallback
);
2117 /* Wake is broadcast first-to-last */
2119 broadcast_wake(void)
2121 int ret
= PBOOK_SLEEP_OK
;
2122 struct list_head
*list
;
2123 struct pmu_sleep_notifier
*notifier
;
2125 for (list
= sleep_notifiers
.next
; list
!= &sleep_notifiers
;
2126 list
= list
->next
) {
2127 notifier
= list_entry(list
, struct pmu_sleep_notifier
, list
);
2128 notifier
->notifier_call(notifier
, PBOOK_WAKE
);
2134 * This struct is used to store config register values for
2135 * PCI devices which may get powered off when we sleep.
2137 static struct pci_save
{
2138 #ifndef HACKED_PCI_SAVE
2147 static int pbook_npci_saves
;
2150 pbook_alloc_pci_save(void)
2153 struct pci_dev
*pd
= NULL
;
2156 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
2161 pbook_pci_saves
= (struct pci_save
*)
2162 kmalloc(npci
* sizeof(struct pci_save
), GFP_KERNEL
);
2163 pbook_npci_saves
= npci
;
2167 pbook_free_pci_save(void)
2169 if (pbook_pci_saves
== NULL
)
2171 kfree(pbook_pci_saves
);
2172 pbook_pci_saves
= NULL
;
2173 pbook_npci_saves
= 0;
2177 pbook_pci_save(void)
2179 struct pci_save
*ps
= pbook_pci_saves
;
2180 struct pci_dev
*pd
= NULL
;
2181 int npci
= pbook_npci_saves
;
2186 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
2189 #ifndef HACKED_PCI_SAVE
2190 pci_read_config_word(pd
, PCI_COMMAND
, &ps
->command
);
2191 pci_read_config_word(pd
, PCI_CACHE_LINE_SIZE
, &ps
->cache_lat
);
2192 pci_read_config_word(pd
, PCI_INTERRUPT_LINE
, &ps
->intr
);
2193 pci_read_config_dword(pd
, PCI_ROM_ADDRESS
, &ps
->rom_address
);
2197 pci_read_config_dword(pd
, i
<<4, &ps
->config
[i
]);
2203 /* For this to work, we must take care of a few things: If gmac was enabled
2204 * during boot, it will be in the pci dev list. If it's disabled at this point
2205 * (and it will probably be), then you can't access it's config space.
2208 pbook_pci_restore(void)
2211 struct pci_save
*ps
= pbook_pci_saves
- 1;
2212 struct pci_dev
*pd
= NULL
;
2213 int npci
= pbook_npci_saves
;
2216 while ((pd
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pd
)) != NULL
) {
2217 #ifdef HACKED_PCI_SAVE
2223 pci_write_config_dword(pd
, i
<<4, ps
->config
[i
]);
2224 pci_write_config_dword(pd
, 4, ps
->config
[1]);
2229 if (ps
->command
== 0)
2231 pci_read_config_word(pd
, PCI_COMMAND
, &cmd
);
2232 if ((ps
->command
& ~cmd
) == 0)
2234 switch (pd
->hdr_type
) {
2235 case PCI_HEADER_TYPE_NORMAL
:
2236 for (j
= 0; j
< 6; ++j
)
2237 pci_write_config_dword(pd
,
2238 PCI_BASE_ADDRESS_0
+ j
*4,
2239 pd
->resource
[j
].start
);
2240 pci_write_config_dword(pd
, PCI_ROM_ADDRESS
,
2242 pci_write_config_word(pd
, PCI_CACHE_LINE_SIZE
,
2244 pci_write_config_word(pd
, PCI_INTERRUPT_LINE
,
2246 pci_write_config_word(pd
, PCI_COMMAND
, ps
->command
);
2254 /* N.B. This doesn't work on the 3400 */
2258 struct adb_request req
;
2260 memset(&req
, 0, sizeof(req
));
2262 for (; n
> 0; --n
) {
2269 req
.reply
[0] = ADB_RET_OK
;
2271 req
.reply_expected
= 0;
2272 pmu_polled_request(&req
);
2280 req
.reply
[0] = ADB_RET_OK
;
2282 req
.reply_expected
= 0;
2283 pmu_polled_request(&req
);
2291 * Put the powerbook to sleep.
2294 static u32 save_via
[8];
2297 save_via_state(void)
2299 save_via
[0] = in_8(&via
[ANH
]);
2300 save_via
[1] = in_8(&via
[DIRA
]);
2301 save_via
[2] = in_8(&via
[B
]);
2302 save_via
[3] = in_8(&via
[DIRB
]);
2303 save_via
[4] = in_8(&via
[PCR
]);
2304 save_via
[5] = in_8(&via
[ACR
]);
2305 save_via
[6] = in_8(&via
[T1CL
]);
2306 save_via
[7] = in_8(&via
[T1CH
]);
2309 restore_via_state(void)
2311 out_8(&via
[ANH
], save_via
[0]);
2312 out_8(&via
[DIRA
], save_via
[1]);
2313 out_8(&via
[B
], save_via
[2]);
2314 out_8(&via
[DIRB
], save_via
[3]);
2315 out_8(&via
[PCR
], save_via
[4]);
2316 out_8(&via
[ACR
], save_via
[5]);
2317 out_8(&via
[T1CL
], save_via
[6]);
2318 out_8(&via
[T1CH
], save_via
[7]);
2319 out_8(&via
[IER
], IER_CLR
| 0x7f); /* disable all intrs */
2320 out_8(&via
[IFR
], 0x7f); /* clear IFR */
2321 out_8(&via
[IER
], IER_SET
| SR_INT
| CB1_INT
);
2325 pmac_suspend_devices(void)
2329 pm_prepare_console();
2331 /* Notify old-style device drivers & userland */
2332 ret
= broadcast_sleep(PBOOK_SLEEP_REQUEST
, PBOOK_SLEEP_REJECT
);
2333 if (ret
!= PBOOK_SLEEP_OK
) {
2334 printk(KERN_ERR
"Sleep rejected by drivers\n");
2338 /* Sync the disks. */
2339 /* XXX It would be nice to have some way to ensure that
2340 * nobody is dirtying any new buffers while we wait. That
2341 * could be achieved using the refrigerator for processes
2346 /* Sleep can fail now. May not be very robust but useful for debugging */
2347 ret
= broadcast_sleep(PBOOK_SLEEP_NOW
, PBOOK_WAKE
);
2348 if (ret
!= PBOOK_SLEEP_OK
) {
2349 printk(KERN_ERR
"Driver sleep failed\n");
2353 /* Send suspend call to devices, hold the device core's dpm_sem */
2354 ret
= device_suspend(PMSG_SUSPEND
);
2357 printk(KERN_ERR
"Driver sleep failed\n");
2361 /* Disable clock spreading on some machines */
2362 pmac_tweak_clock_spreading(0);
2364 /* Stop preemption */
2367 /* Make sure the decrementer won't interrupt us */
2368 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2369 /* Make sure any pending DEC interrupt occurring while we did
2370 * the above didn't re-enable the DEC */
2372 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2374 /* We can now disable MSR_EE. This code of course works properly only
2375 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2376 * stop the "other" CPUs way before we do all that stuff.
2378 local_irq_disable();
2380 /* Broadcast power down irq
2381 * This isn't that useful in most cases (only directly wired devices can
2382 * use this but still... This will take care of sysdev's as well, so
2383 * we exit from here with local irqs disabled and PIC off.
2385 ret
= device_power_down(PMSG_SUSPEND
);
2387 wakeup_decrementer();
2392 printk(KERN_ERR
"Driver powerdown failed\n");
2396 /* Wait for completion of async backlight requests */
2397 while (!bright_req_1
.complete
|| !bright_req_2
.complete
||
2401 /* Giveup the lazy FPU & vec so we don't have to back them
2402 * up from the low level code
2406 #ifdef CONFIG_ALTIVEC
2407 if (cpu_has_feature(CPU_FTR_ALTIVEC
))
2408 enable_kernel_altivec();
2409 #endif /* CONFIG_ALTIVEC */
2415 pmac_wakeup_devices(void)
2419 /* Power back up system devices (including the PIC) */
2422 /* Force a poll of ADB interrupts */
2423 adb_int_pending
= 1;
2424 via_pmu_interrupt(0, NULL
, NULL
);
2426 /* Restart jiffies & scheduling */
2427 wakeup_decrementer();
2429 /* Re-enable local CPU interrupts */
2434 /* Re-enable clock spreading on some machines */
2435 pmac_tweak_clock_spreading(1);
2437 /* Resume devices */
2440 /* Notify old style drivers */
2443 pm_restore_console();
2448 #define GRACKLE_PM (1<<7)
2449 #define GRACKLE_DOZE (1<<5)
2450 #define GRACKLE_NAP (1<<4)
2451 #define GRACKLE_SLEEP (1<<3)
2454 powerbook_sleep_grackle(void)
2456 unsigned long save_l2cr
;
2457 unsigned short pmcr1
;
2458 struct adb_request req
;
2460 struct pci_dev
*grackle
;
2462 grackle
= pci_find_slot(0, 0);
2466 ret
= pmac_suspend_devices();
2468 printk(KERN_ERR
"Sleep rejected by devices\n");
2472 /* Turn off various things. Darwin does some retry tests here... */
2473 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL0
, PMU_POW0_OFF
|PMU_POW0_HARD_DRIVE
);
2474 pmu_wait_complete(&req
);
2475 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
2476 PMU_POW_OFF
|PMU_POW_BACKLIGHT
|PMU_POW_IRLED
|PMU_POW_MEDIABAY
);
2477 pmu_wait_complete(&req
);
2479 /* For 750, save backside cache setting and disable it */
2480 save_l2cr
= _get_L2CR(); /* (returns -1 if not available) */
2482 if (!__fake_sleep
) {
2483 /* Ask the PMU to put us to sleep */
2484 pmu_request(&req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2485 pmu_wait_complete(&req
);
2488 /* The VIA is supposed not to be restored correctly*/
2490 /* We shut down some HW */
2491 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,1);
2493 pci_read_config_word(grackle
, 0x70, &pmcr1
);
2494 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2495 pmcr1
&= ~(GRACKLE_DOZE
|GRACKLE_SLEEP
);
2496 pmcr1
|= GRACKLE_PM
|GRACKLE_NAP
;
2497 pci_write_config_word(grackle
, 0x70, pmcr1
);
2499 /* Call low-level ASM sleep handler */
2503 low_sleep_handler();
2505 /* We're awake again, stop grackle PM */
2506 pci_read_config_word(grackle
, 0x70, &pmcr1
);
2507 pmcr1
&= ~(GRACKLE_PM
|GRACKLE_DOZE
|GRACKLE_SLEEP
|GRACKLE_NAP
);
2508 pci_write_config_word(grackle
, 0x70, pmcr1
);
2510 /* Make sure the PMU is idle */
2511 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,0);
2512 restore_via_state();
2514 /* Restore L2 cache */
2515 if (save_l2cr
!= 0xffffffff && (save_l2cr
& L2CR_L2E
) != 0)
2516 _set_L2CR(save_l2cr
);
2518 /* Restore userland MMU context */
2519 set_context(current
->active_mm
->context
, current
->active_mm
->pgd
);
2521 /* Power things up */
2523 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
2524 pmu_wait_complete(&req
);
2525 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL0
,
2526 PMU_POW0_ON
|PMU_POW0_HARD_DRIVE
);
2527 pmu_wait_complete(&req
);
2528 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
2529 PMU_POW_ON
|PMU_POW_BACKLIGHT
|PMU_POW_CHARGER
|PMU_POW_IRLED
|PMU_POW_MEDIABAY
);
2530 pmu_wait_complete(&req
);
2532 pmac_wakeup_devices();
2538 powerbook_sleep_Core99(void)
2540 unsigned long save_l2cr
;
2541 unsigned long save_l3cr
;
2542 struct adb_request req
;
2545 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) < 0) {
2546 printk(KERN_ERR
"Sleep mode not supported on this machine\n");
2550 if (num_online_cpus() > 1 || cpu_is_offline(0))
2553 ret
= pmac_suspend_devices();
2555 printk(KERN_ERR
"Sleep rejected by devices\n");
2559 /* Stop environment and ADB interrupts */
2560 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, 0);
2561 pmu_wait_complete(&req
);
2563 /* Tell PMU what events will wake us up */
2564 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
, PMU_PWR_CLR_WAKEUP_EVENTS
,
2566 pmu_wait_complete(&req
);
2567 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
, PMU_PWR_SET_WAKEUP_EVENTS
,
2568 0, PMU_PWR_WAKEUP_KEY
|
2569 (option_lid_wakeup
? PMU_PWR_WAKEUP_LID_OPEN
: 0));
2570 pmu_wait_complete(&req
);
2572 /* Save the state of the L2 and L3 caches */
2573 save_l3cr
= _get_L3CR(); /* (returns -1 if not available) */
2574 save_l2cr
= _get_L2CR(); /* (returns -1 if not available) */
2576 if (!__fake_sleep
) {
2577 /* Ask the PMU to put us to sleep */
2578 pmu_request(&req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2579 pmu_wait_complete(&req
);
2582 /* The VIA is supposed not to be restored correctly*/
2585 /* Shut down various ASICs. There's a chance that we can no longer
2586 * talk to the PMU after this, so I moved it to _after_ sending the
2587 * sleep command to it. Still need to be checked.
2589 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 1);
2591 /* Call low-level ASM sleep handler */
2595 low_sleep_handler();
2597 /* Restore Apple core ASICs state */
2598 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 0);
2601 restore_via_state();
2603 /* tweak LPJ before cpufreq is there */
2604 loops_per_jiffy
*= 2;
2607 pmac_call_early_video_resume();
2609 /* Restore L2 cache */
2610 if (save_l2cr
!= 0xffffffff && (save_l2cr
& L2CR_L2E
) != 0)
2611 _set_L2CR(save_l2cr
);
2612 /* Restore L3 cache */
2613 if (save_l3cr
!= 0xffffffff && (save_l3cr
& L3CR_L3E
) != 0)
2614 _set_L3CR(save_l3cr
);
2616 /* Restore userland MMU context */
2617 set_context(current
->active_mm
->context
, current
->active_mm
->pgd
);
2619 /* Tell PMU we are ready */
2621 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
2622 pmu_wait_complete(&req
);
2623 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
2624 pmu_wait_complete(&req
);
2626 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2627 loops_per_jiffy
/= 2;
2629 pmac_wakeup_devices();
2634 #define PB3400_MEM_CTRL 0xf8000000
2635 #define PB3400_MEM_CTRL_SLEEP 0x70
2638 powerbook_sleep_3400(void)
2643 struct adb_request sleep_req
;
2644 void __iomem
*mem_ctrl
;
2645 unsigned int __iomem
*mem_ctrl_sleep
;
2647 /* first map in the memory controller registers */
2648 mem_ctrl
= ioremap(PB3400_MEM_CTRL
, 0x100);
2649 if (mem_ctrl
== NULL
) {
2650 printk("powerbook_sleep_3400: ioremap failed\n");
2653 mem_ctrl_sleep
= mem_ctrl
+ PB3400_MEM_CTRL_SLEEP
;
2655 /* Allocate room for PCI save */
2656 pbook_alloc_pci_save();
2658 ret
= pmac_suspend_devices();
2660 pbook_free_pci_save();
2661 printk(KERN_ERR
"Sleep rejected by devices\n");
2665 /* Save the state of PCI config space for some slots */
2668 /* Set the memory controller to keep the memory refreshed
2669 while we're asleep */
2670 for (i
= 0x403f; i
>= 0x4000; --i
) {
2671 out_be32(mem_ctrl_sleep
, i
);
2673 x
= (in_be32(mem_ctrl_sleep
) >> 16) & 0x3ff;
2679 /* Ask the PMU to put us to sleep */
2680 pmu_request(&sleep_req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
2681 while (!sleep_req
.complete
)
2684 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,1);
2686 /* displacement-flush the L2 cache - necessary? */
2687 for (p
= KERNELBASE
; p
< KERNELBASE
+ 0x100000; p
+= 0x1000)
2688 i
= *(volatile int *)p
;
2691 /* Put the CPU into sleep mode */
2692 hid0
= mfspr(SPRN_HID0
);
2693 hid0
= (hid0
& ~(HID0_NAP
| HID0_DOZE
)) | HID0_SLEEP
;
2694 mtspr(SPRN_HID0
, hid0
);
2695 mtmsr(mfmsr() | MSR_POW
| MSR_EE
);
2698 /* OK, we're awake again, start restoring things */
2699 out_be32(mem_ctrl_sleep
, 0x3f);
2700 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,0);
2701 pbook_pci_restore();
2704 /* wait for the PMU interrupt sequence to complete */
2708 pmac_wakeup_devices();
2709 pbook_free_pci_save();
2715 #endif /* CONFIG_PM && CONFIG_PPC32 */
2718 * Support for /dev/pmu device
2720 #define RB_SIZE 0x10
2721 struct pmu_private
{
2722 struct list_head list
;
2727 unsigned char data
[16];
2729 wait_queue_head_t wait
;
2731 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2732 int backlight_locker
;
2733 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2736 static LIST_HEAD(all_pmu_pvt
);
2737 static DEFINE_SPINLOCK(all_pvt_lock
);
2740 pmu_pass_intr(unsigned char *data
, int len
)
2742 struct pmu_private
*pp
;
2743 struct list_head
*list
;
2745 unsigned long flags
;
2747 if (len
> sizeof(pp
->rb_buf
[0].data
))
2748 len
= sizeof(pp
->rb_buf
[0].data
);
2749 spin_lock_irqsave(&all_pvt_lock
, flags
);
2750 for (list
= &all_pmu_pvt
; (list
= list
->next
) != &all_pmu_pvt
; ) {
2751 pp
= list_entry(list
, struct pmu_private
, list
);
2752 spin_lock(&pp
->lock
);
2756 if (i
!= pp
->rb_get
) {
2757 struct rb_entry
*rp
= &pp
->rb_buf
[pp
->rb_put
];
2759 memcpy(rp
->data
, data
, len
);
2761 wake_up_interruptible(&pp
->wait
);
2763 spin_unlock(&pp
->lock
);
2765 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2769 pmu_open(struct inode
*inode
, struct file
*file
)
2771 struct pmu_private
*pp
;
2772 unsigned long flags
;
2774 pp
= kmalloc(sizeof(struct pmu_private
), GFP_KERNEL
);
2777 pp
->rb_get
= pp
->rb_put
= 0;
2778 spin_lock_init(&pp
->lock
);
2779 init_waitqueue_head(&pp
->wait
);
2780 spin_lock_irqsave(&all_pvt_lock
, flags
);
2781 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2782 pp
->backlight_locker
= 0;
2783 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2784 list_add(&pp
->list
, &all_pmu_pvt
);
2785 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2786 file
->private_data
= pp
;
2791 pmu_read(struct file
*file
, char __user
*buf
,
2792 size_t count
, loff_t
*ppos
)
2794 struct pmu_private
*pp
= file
->private_data
;
2795 DECLARE_WAITQUEUE(wait
, current
);
2796 unsigned long flags
;
2799 if (count
< 1 || pp
== 0)
2801 if (!access_ok(VERIFY_WRITE
, buf
, count
))
2804 spin_lock_irqsave(&pp
->lock
, flags
);
2805 add_wait_queue(&pp
->wait
, &wait
);
2806 current
->state
= TASK_INTERRUPTIBLE
;
2810 if (pp
->rb_get
!= pp
->rb_put
) {
2812 struct rb_entry
*rp
= &pp
->rb_buf
[i
];
2814 spin_unlock_irqrestore(&pp
->lock
, flags
);
2817 if (ret
> 0 && copy_to_user(buf
, rp
->data
, ret
))
2821 spin_lock_irqsave(&pp
->lock
, flags
);
2826 if (file
->f_flags
& O_NONBLOCK
)
2829 if (signal_pending(current
))
2831 spin_unlock_irqrestore(&pp
->lock
, flags
);
2833 spin_lock_irqsave(&pp
->lock
, flags
);
2835 current
->state
= TASK_RUNNING
;
2836 remove_wait_queue(&pp
->wait
, &wait
);
2837 spin_unlock_irqrestore(&pp
->lock
, flags
);
2843 pmu_write(struct file
*file
, const char __user
*buf
,
2844 size_t count
, loff_t
*ppos
)
2850 pmu_fpoll(struct file
*filp
, poll_table
*wait
)
2852 struct pmu_private
*pp
= filp
->private_data
;
2853 unsigned int mask
= 0;
2854 unsigned long flags
;
2858 poll_wait(filp
, &pp
->wait
, wait
);
2859 spin_lock_irqsave(&pp
->lock
, flags
);
2860 if (pp
->rb_get
!= pp
->rb_put
)
2862 spin_unlock_irqrestore(&pp
->lock
, flags
);
2867 pmu_release(struct inode
*inode
, struct file
*file
)
2869 struct pmu_private
*pp
= file
->private_data
;
2870 unsigned long flags
;
2874 file
->private_data
= NULL
;
2875 spin_lock_irqsave(&all_pvt_lock
, flags
);
2876 list_del(&pp
->list
);
2877 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2878 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2879 if (pp
->backlight_locker
) {
2880 spin_lock_irqsave(&pmu_lock
, flags
);
2881 disable_kernel_backlight
--;
2882 spin_unlock_irqrestore(&pmu_lock
, flags
);
2884 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2892 pmu_ioctl(struct inode
* inode
, struct file
*filp
,
2893 u_int cmd
, u_long arg
)
2895 __u32 __user
*argp
= (__u32 __user
*)arg
;
2896 int error
= -EINVAL
;
2899 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2901 if (!capable(CAP_SYS_ADMIN
))
2903 if (sleep_in_progress
)
2905 sleep_in_progress
= 1;
2907 case PMU_OHARE_BASED
:
2908 error
= powerbook_sleep_3400();
2910 case PMU_HEATHROW_BASED
:
2911 case PMU_PADDINGTON_BASED
:
2912 error
= powerbook_sleep_grackle();
2914 case PMU_KEYLARGO_BASED
:
2915 error
= powerbook_sleep_Core99();
2920 sleep_in_progress
= 0;
2922 case PMU_IOC_CAN_SLEEP
:
2923 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) < 0)
2924 return put_user(0, argp
);
2926 return put_user(1, argp
);
2927 #endif /* CONFIG_PM && CONFIG_PPC32 */
2929 #ifdef CONFIG_PMAC_BACKLIGHT
2930 /* Backlight should have its own device or go via
2933 case PMU_IOC_GET_BACKLIGHT
:
2934 if (sleep_in_progress
)
2936 error
= get_backlight_level();
2939 return put_user(error
, argp
);
2940 case PMU_IOC_SET_BACKLIGHT
:
2943 if (sleep_in_progress
)
2945 error
= get_user(value
, argp
);
2947 error
= set_backlight_level(value
);
2950 #ifdef CONFIG_INPUT_ADBHID
2951 case PMU_IOC_GRAB_BACKLIGHT
: {
2952 struct pmu_private
*pp
= filp
->private_data
;
2953 unsigned long flags
;
2955 if (pp
->backlight_locker
)
2957 pp
->backlight_locker
= 1;
2958 spin_lock_irqsave(&pmu_lock
, flags
);
2959 disable_kernel_backlight
++;
2960 spin_unlock_irqrestore(&pmu_lock
, flags
);
2963 #endif /* CONFIG_INPUT_ADBHID */
2964 #endif /* CONFIG_PMAC_BACKLIGHT */
2965 case PMU_IOC_GET_MODEL
:
2966 return put_user(pmu_kind
, argp
);
2967 case PMU_IOC_HAS_ADB
:
2968 return put_user(pmu_has_adb
, argp
);
2973 static struct file_operations pmu_device_fops
= {
2979 .release
= pmu_release
,
2982 static struct miscdevice pmu_device
= {
2983 PMU_MINOR
, "pmu", &pmu_device_fops
2986 static int pmu_device_init(void)
2990 if (misc_register(&pmu_device
) < 0)
2991 printk(KERN_ERR
"via-pmu: cannot register misc device.\n");
2994 device_initcall(pmu_device_init
);
2999 polled_handshake(volatile unsigned char __iomem
*via
)
3001 via
[B
] &= ~TREQ
; eieio();
3002 while ((via
[B
] & TACK
) != 0)
3004 via
[B
] |= TREQ
; eieio();
3005 while ((via
[B
] & TACK
) == 0)
3010 polled_send_byte(volatile unsigned char __iomem
*via
, int x
)
3012 via
[ACR
] |= SR_OUT
| SR_EXT
; eieio();
3013 via
[SR
] = x
; eieio();
3014 polled_handshake(via
);
3018 polled_recv_byte(volatile unsigned char __iomem
*via
)
3022 via
[ACR
] = (via
[ACR
] & ~SR_OUT
) | SR_EXT
; eieio();
3023 x
= via
[SR
]; eieio();
3024 polled_handshake(via
);
3025 x
= via
[SR
]; eieio();
3030 pmu_polled_request(struct adb_request
*req
)
3032 unsigned long flags
;
3034 volatile unsigned char __iomem
*v
= via
;
3038 l
= pmu_data_len
[c
][0];
3039 if (l
>= 0 && req
->nbytes
!= l
+ 1)
3042 local_irq_save(flags
);
3043 while (pmu_state
!= idle
)
3046 while ((via
[B
] & TACK
) == 0)
3048 polled_send_byte(v
, c
);
3050 l
= req
->nbytes
- 1;
3051 polled_send_byte(v
, l
);
3053 for (i
= 1; i
<= l
; ++i
)
3054 polled_send_byte(v
, req
->data
[i
]);
3056 l
= pmu_data_len
[c
][1];
3058 l
= polled_recv_byte(v
);
3059 for (i
= 0; i
< l
; ++i
)
3060 req
->reply
[i
+ req
->reply_len
] = polled_recv_byte(v
);
3065 local_irq_restore(flags
);
3068 #endif /* DEBUG_SLEEP */
3071 /* FIXME: This is a temporary set of callbacks to enable us
3072 * to do suspend-to-disk.
3075 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
3077 static int pmu_sys_suspended
= 0;
3079 static int pmu_sys_suspend(struct sys_device
*sysdev
, pm_message_t state
)
3081 if (state
.event
!= PM_EVENT_SUSPEND
|| pmu_sys_suspended
)
3084 /* Suspend PMU event interrupts */
3087 pmu_sys_suspended
= 1;
3091 static int pmu_sys_resume(struct sys_device
*sysdev
)
3093 struct adb_request req
;
3095 if (!pmu_sys_suspended
)
3098 /* Tell PMU we are ready */
3099 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
3100 pmu_wait_complete(&req
);
3102 /* Resume PMU event interrupts */
3105 pmu_sys_suspended
= 0;
3110 #endif /* CONFIG_PM && CONFIG_PPC32 */
3112 static struct sysdev_class pmu_sysclass
= {
3113 set_kset_name("pmu"),
3116 static struct sys_device device_pmu
= {
3118 .cls
= &pmu_sysclass
,
3121 static struct sysdev_driver driver_pmu
= {
3122 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
3123 .suspend
= &pmu_sys_suspend
,
3124 .resume
= &pmu_sys_resume
,
3125 #endif /* CONFIG_PM && CONFIG_PPC32 */
3128 static int __init
init_pmu_sysfs(void)
3132 rc
= sysdev_class_register(&pmu_sysclass
);
3134 printk(KERN_ERR
"Failed registering PMU sys class\n");
3137 rc
= sysdev_register(&device_pmu
);
3139 printk(KERN_ERR
"Failed registering PMU sys device\n");
3142 rc
= sysdev_driver_register(&pmu_sysclass
, &driver_pmu
);
3144 printk(KERN_ERR
"Failed registering PMU sys driver\n");
3150 subsys_initcall(init_pmu_sysfs
);
3152 EXPORT_SYMBOL(pmu_request
);
3153 EXPORT_SYMBOL(pmu_poll
);
3154 EXPORT_SYMBOL(pmu_poll_adb
);
3155 EXPORT_SYMBOL(pmu_wait_complete
);
3156 EXPORT_SYMBOL(pmu_suspend
);
3157 EXPORT_SYMBOL(pmu_resume
);
3158 EXPORT_SYMBOL(pmu_unlock
);
3159 EXPORT_SYMBOL(pmu_i2c_combined_read
);
3160 EXPORT_SYMBOL(pmu_i2c_stdsub_write
);
3161 EXPORT_SYMBOL(pmu_i2c_simple_read
);
3162 EXPORT_SYMBOL(pmu_i2c_simple_write
);
3163 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
3164 EXPORT_SYMBOL(pmu_enable_irled
);
3165 EXPORT_SYMBOL(pmu_battery_count
);
3166 EXPORT_SYMBOL(pmu_batteries
);
3167 EXPORT_SYMBOL(pmu_power_flags
);
3168 #endif /* CONFIG_PM && CONFIG_PPC32 */