[POWERPC] pci_32.c: Switch to ref counting PCI API
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / macintosh / via-pmu.c
CommitLineData
1da177e4
LT
1/*
2 * Device driver for the via-pmu on Apple Powermacs.
3 *
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.
10 *
11 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
12 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
13 *
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
1da177e4
LT
19 * - Save/Restore PCI space properly
20 *
21 */
22#include <stdarg.h>
1da177e4
LT
23#include <linux/types.h>
24#include <linux/errno.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
28#include <linux/miscdevice.h>
29#include <linux/blkdev.h>
30#include <linux/pci.h>
31#include <linux/slab.h>
32#include <linux/poll.h>
33#include <linux/adb.h>
34#include <linux/pmu.h>
35#include <linux/cuda.h>
36#include <linux/smp_lock.h>
37#include <linux/module.h>
38#include <linux/spinlock.h>
39#include <linux/pm.h>
40#include <linux/proc_fs.h>
41#include <linux/init.h>
42#include <linux/interrupt.h>
43#include <linux/device.h>
44#include <linux/sysdev.h>
7dfb7103 45#include <linux/freezer.h>
1da177e4 46#include <linux/syscalls.h>
6002f544 47#include <linux/suspend.h>
1da177e4
LT
48#include <linux/cpu.h>
49#include <asm/prom.h>
50#include <asm/machdep.h>
51#include <asm/io.h>
52#include <asm/pgtable.h>
53#include <asm/system.h>
54#include <asm/sections.h>
55#include <asm/irq.h>
56#include <asm/pmac_feature.h>
5b9ca526
BH
57#include <asm/pmac_pfunc.h>
58#include <asm/pmac_low_i2c.h>
1da177e4
LT
59#include <asm/uaccess.h>
60#include <asm/mmu_context.h>
61#include <asm/cputable.h>
62#include <asm/time.h>
1da177e4 63#include <asm/backlight.h>
1da177e4 64
9e8e30a0
JB
65#include "via-pmu-event.h"
66
1da177e4
LT
67/* Some compile options */
68#undef SUSPEND_USES_PMU
69#define DEBUG_SLEEP
70#undef HACKED_PCI_SAVE
71
72/* Misc minor number allocated for /dev/pmu */
73#define PMU_MINOR 154
74
75/* How many iterations between battery polls */
76#define BATTERY_POLLING_COUNT 2
77
78static volatile unsigned char __iomem *via;
79
80/* VIA registers - spaced 0x200 bytes apart */
81#define RS 0x200 /* skip between registers */
82#define B 0 /* B-side data */
83#define A RS /* A-side data */
84#define DIRB (2*RS) /* B-side direction (1=output) */
85#define DIRA (3*RS) /* A-side direction (1=output) */
86#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
87#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
88#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
89#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
90#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
91#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
92#define SR (10*RS) /* Shift register */
93#define ACR (11*RS) /* Auxiliary control register */
94#define PCR (12*RS) /* Peripheral control register */
95#define IFR (13*RS) /* Interrupt flag register */
96#define IER (14*RS) /* Interrupt enable register */
97#define ANH (15*RS) /* A-side data, no handshake */
98
99/* Bits in B data register: both active low */
100#define TACK 0x08 /* Transfer acknowledge (input) */
101#define TREQ 0x10 /* Transfer request (output) */
102
103/* Bits in ACR */
104#define SR_CTRL 0x1c /* Shift register control bits */
105#define SR_EXT 0x0c /* Shift on external clock */
106#define SR_OUT 0x10 /* Shift out if 1 */
107
108/* Bits in IFR and IER */
109#define IER_SET 0x80 /* set bits in IER */
110#define IER_CLR 0 /* clear bits in IER */
111#define SR_INT 0x04 /* Shift register full/empty */
112#define CB2_INT 0x08
113#define CB1_INT 0x10 /* transition on CB1 input */
114
115static volatile enum pmu_state {
116 idle,
117 sending,
118 intack,
119 reading,
120 reading_intr,
121 locked,
122} pmu_state;
123
124static volatile enum int_data_state {
125 int_data_empty,
126 int_data_fill,
127 int_data_ready,
128 int_data_flush
129} int_data_state[2] = { int_data_empty, int_data_empty };
130
131static struct adb_request *current_req;
132static struct adb_request *last_req;
133static struct adb_request *req_awaiting_reply;
134static unsigned char interrupt_data[2][32];
135static int interrupt_data_len[2];
136static int int_data_last;
137static unsigned char *reply_ptr;
138static int data_index;
139static int data_len;
140static volatile int adb_int_pending;
141static volatile int disable_poll;
1da177e4
LT
142static struct device_node *vias;
143static int pmu_kind = PMU_UNKNOWN;
87275856 144static int pmu_fully_inited;
1da177e4 145static int pmu_has_adb;
51d3082f 146static struct device_node *gpio_node;
87275856 147static unsigned char __iomem *gpio_reg;
0ebfff14 148static int gpio_irq = NO_IRQ;
1da177e4 149static int gpio_irq_enabled = -1;
87275856 150static volatile int pmu_suspended;
1da177e4
LT
151static spinlock_t pmu_lock;
152static u8 pmu_intr_mask;
153static int pmu_version;
154static int drop_interrupts;
a0005034 155#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1da177e4 156static int option_lid_wakeup = 1;
a0005034 157#endif /* CONFIG_PM && CONFIG_PPC32 */
5474c120 158#if (defined(CONFIG_PM)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT_LEGACY)
a04c8780 159static int sleep_in_progress;
57ae595f 160#endif
1da177e4
LT
161static unsigned long async_req_locks;
162static unsigned int pmu_irq_stats[11];
163
164static struct proc_dir_entry *proc_pmu_root;
165static struct proc_dir_entry *proc_pmu_info;
166static struct proc_dir_entry *proc_pmu_irqstats;
167static struct proc_dir_entry *proc_pmu_options;
168static int option_server_mode;
169
1da177e4
LT
170int pmu_battery_count;
171int pmu_cur_battery;
a334bdbd 172unsigned int pmu_power_flags = PMU_PWR_AC_PRESENT;
1da177e4
LT
173struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
174static int query_batt_timer = BATTERY_POLLING_COUNT;
175static struct adb_request batt_req;
176static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
1da177e4 177
1da177e4
LT
178int __fake_sleep;
179int asleep;
e041c683 180BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
1da177e4
LT
181
182#ifdef CONFIG_ADB
87275856 183static int adb_dev_map;
1da177e4
LT
184static int pmu_adb_flags;
185
186static int pmu_probe(void);
187static int pmu_init(void);
188static int pmu_send_request(struct adb_request *req, int sync);
189static int pmu_adb_autopoll(int devs);
190static int pmu_adb_reset_bus(void);
191#endif /* CONFIG_ADB */
192
193static int init_pmu(void);
1da177e4 194static void pmu_start(void);
7d12e780
DH
195static irqreturn_t via_pmu_interrupt(int irq, void *arg);
196static irqreturn_t gpio1_interrupt(int irq, void *arg);
1da177e4
LT
197static int proc_get_info(char *page, char **start, off_t off,
198 int count, int *eof, void *data);
199static int proc_get_irqstats(char *page, char **start, off_t off,
200 int count, int *eof, void *data);
1da177e4
LT
201static void pmu_pass_intr(unsigned char *data, int len);
202static int proc_get_batt(char *page, char **start, off_t off,
203 int count, int *eof, void *data);
1da177e4
LT
204static int proc_read_options(char *page, char **start, off_t off,
205 int count, int *eof, void *data);
206static int proc_write_options(struct file *file, const char __user *buffer,
207 unsigned long count, void *data);
208
209#ifdef CONFIG_ADB
210struct adb_driver via_pmu_driver = {
211 "PMU",
212 pmu_probe,
213 pmu_init,
214 pmu_send_request,
215 pmu_adb_autopoll,
216 pmu_poll_adb,
217 pmu_adb_reset_bus
218};
219#endif /* CONFIG_ADB */
220
221extern void low_sleep_handler(void);
222extern void enable_kernel_altivec(void);
223extern void enable_kernel_fp(void);
224
225#ifdef DEBUG_SLEEP
226int pmu_polled_request(struct adb_request *req);
227int pmu_wink(struct adb_request *req);
228#endif
229
230/*
231 * This table indicates for each PMU opcode:
232 * - the number of data bytes to be sent with the command, or -1
233 * if a length byte should be sent,
234 * - the number of response bytes which the PMU will return, or
235 * -1 if it will send a length byte.
236 */
aacaf9bd 237static const s8 pmu_data_len[256][2] = {
1da177e4
LT
238/* 0 1 2 3 4 5 6 7 */
239/*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
240/*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
241/*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
242/*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
243/*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
244/*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
245/*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
246/*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
247/*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
248/*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
249/*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
250/*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
251/*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252/*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
253/*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254/*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
255/*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256/*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
257/*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258/*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
259/*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
260/*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
261/*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262/*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
263/*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
264/*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
265/*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266/*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
267/*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
268/*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
269/*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
270/*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271};
272
273static char *pbook_type[] = {
274 "Unknown PowerBook",
275 "PowerBook 2400/3400/3500(G3)",
276 "PowerBook G3 Series",
277 "1999 PowerBook G3",
278 "Core99"
279};
280
51d3082f 281int __init find_via_pmu(void)
1da177e4 282{
cc5d0189 283 u64 taddr;
018a3d1d 284 const u32 *reg;
51d3082f 285
1da177e4
LT
286 if (via != 0)
287 return 1;
51d3082f
BH
288 vias = of_find_node_by_name(NULL, "via-pmu");
289 if (vias == NULL)
1da177e4 290 return 0;
1da177e4 291
d05c7a80 292 reg = of_get_property(vias, "reg", NULL);
51d3082f
BH
293 if (reg == NULL) {
294 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
295 goto fail;
296 }
297 taddr = of_translate_address(vias, reg);
bb6b9b28 298 if (taddr == OF_BAD_ADDR) {
51d3082f
BH
299 printk(KERN_ERR "via-pmu: Can't translate address !\n");
300 goto fail;
1da177e4
LT
301 }
302
303 spin_lock_init(&pmu_lock);
304
305 pmu_has_adb = 1;
306
307 pmu_intr_mask = PMU_INT_PCEJECT |
308 PMU_INT_SNDBRT |
309 PMU_INT_ADB |
310 PMU_INT_TICK;
311
312 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
313 || device_is_compatible(vias->parent, "ohare")))
314 pmu_kind = PMU_OHARE_BASED;
315 else if (device_is_compatible(vias->parent, "paddington"))
316 pmu_kind = PMU_PADDINGTON_BASED;
317 else if (device_is_compatible(vias->parent, "heathrow"))
318 pmu_kind = PMU_HEATHROW_BASED;
319 else if (device_is_compatible(vias->parent, "Keylargo")
320 || device_is_compatible(vias->parent, "K2-Keylargo")) {
51d3082f 321 struct device_node *gpiop;
1658ab66 322 struct device_node *adbp;
cc5d0189 323 u64 gaddr = OF_BAD_ADDR;
1da177e4
LT
324
325 pmu_kind = PMU_KEYLARGO_BASED;
1658ab66
SR
326 adbp = of_find_node_by_type(NULL, "adb");
327 pmu_has_adb = (adbp != NULL);
328 of_node_put(adbp);
1da177e4
LT
329 pmu_intr_mask = PMU_INT_PCEJECT |
330 PMU_INT_SNDBRT |
331 PMU_INT_ADB |
332 PMU_INT_TICK |
333 PMU_INT_ENVIRONMENT;
334
51d3082f
BH
335 gpiop = of_find_node_by_name(NULL, "gpio");
336 if (gpiop) {
d05c7a80 337 reg = of_get_property(gpiop, "reg", NULL);
51d3082f
BH
338 if (reg)
339 gaddr = of_translate_address(gpiop, reg);
cc5d0189 340 if (gaddr != OF_BAD_ADDR)
51d3082f 341 gpio_reg = ioremap(gaddr, 0x10);
1da177e4 342 }
61e37ca2 343 if (gpio_reg == NULL) {
51d3082f 344 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
61e37ca2
OH
345 goto fail_gpio;
346 }
1da177e4
LT
347 } else
348 pmu_kind = PMU_UNKNOWN;
349
51d3082f
BH
350 via = ioremap(taddr, 0x2000);
351 if (via == NULL) {
352 printk(KERN_ERR "via-pmu: Can't map address !\n");
353 goto fail;
354 }
1da177e4
LT
355
356 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
357 out_8(&via[IFR], 0x7f); /* clear IFR */
358
359 pmu_state = idle;
360
361 if (!init_pmu()) {
362 via = NULL;
363 return 0;
364 }
365
bb6b9b28 366 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
1da177e4
LT
367 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
368
369 sys_ctrler = SYS_CTRLER_PMU;
370
371 return 1;
51d3082f
BH
372 fail:
373 of_node_put(vias);
61e37ca2
OH
374 iounmap(gpio_reg);
375 gpio_reg = NULL;
376 fail_gpio:
51d3082f
BH
377 vias = NULL;
378 return 0;
1da177e4
LT
379}
380
381#ifdef CONFIG_ADB
51d3082f 382static int pmu_probe(void)
1da177e4
LT
383{
384 return vias == NULL? -ENODEV: 0;
385}
386
51d3082f 387static int __init pmu_init(void)
1da177e4
LT
388{
389 if (vias == NULL)
390 return -ENODEV;
391 return 0;
392}
393#endif /* CONFIG_ADB */
394
395/*
396 * We can't wait until pmu_init gets called, that happens too late.
397 * It happens after IDE and SCSI initialization, which can take a few
398 * seconds, and by that time the PMU could have given up on us and
399 * turned us off.
400 * Thus this is called with arch_initcall rather than device_initcall.
401 */
402static int __init via_pmu_start(void)
403{
0ebfff14
BH
404 unsigned int irq;
405
1da177e4
LT
406 if (vias == NULL)
407 return -ENODEV;
408
1da177e4 409 batt_req.complete = 1;
1da177e4 410
0ebfff14
BH
411 irq = irq_of_parse_and_map(vias, 0);
412 if (irq == NO_IRQ) {
413 printk(KERN_ERR "via-pmu: can't map interruptn");
414 return -ENODEV;
415 }
416 if (request_irq(irq, via_pmu_interrupt, 0, "VIA-PMU", (void *)0)) {
417 printk(KERN_ERR "via-pmu: can't request irq %d\n", irq);
418 return -ENODEV;
1da177e4
LT
419 }
420
51d3082f
BH
421 if (pmu_kind == PMU_KEYLARGO_BASED) {
422 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
423 if (gpio_node == NULL)
424 gpio_node = of_find_node_by_name(NULL,
425 "pmu-interrupt");
0ebfff14
BH
426 if (gpio_node)
427 gpio_irq = irq_of_parse_and_map(gpio_node, 0);
51d3082f 428
0ebfff14 429 if (gpio_irq != NO_IRQ) {
51d3082f
BH
430 if (request_irq(gpio_irq, gpio1_interrupt, 0,
431 "GPIO1 ADB", (void *)0))
432 printk(KERN_ERR "pmu: can't get irq %d"
433 " (GPIO1)\n", gpio_irq);
434 else
435 gpio_irq_enabled = 1;
436 }
1da177e4
LT
437 }
438
439 /* Enable interrupts */
440 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
441
442 pmu_fully_inited = 1;
443
444 /* Make sure PMU settle down before continuing. This is _very_ important
445 * since the IDE probe may shut interrupts down for quite a bit of time. If
446 * a PMU communication is pending while this happens, the PMU may timeout
447 * Not that on Core99 machines, the PMU keeps sending us environement
448 * messages, we should find a way to either fix IDE or make it call
449 * pmu_suspend() before masking interrupts. This can also happens while
450 * scolling with some fbdevs.
451 */
452 do {
453 pmu_poll();
454 } while (pmu_state != idle);
455
456 return 0;
457}
458
459arch_initcall(via_pmu_start);
460
461/*
462 * This has to be done after pci_init, which is a subsys_initcall.
463 */
464static int __init via_pmu_dev_init(void)
465{
466 if (vias == NULL)
467 return -ENODEV;
468
1da177e4 469#ifdef CONFIG_PMAC_BACKLIGHT
5474c120 470 /* Initialize backlight */
4b755999 471 pmu_backlight_init();
5474c120 472#endif
1da177e4 473
8c870933 474#ifdef CONFIG_PPC32
1da177e4
LT
475 if (machine_is_compatible("AAPL,3400/2400") ||
476 machine_is_compatible("AAPL,3500")) {
477 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
478 NULL, PMAC_MB_INFO_MODEL, 0);
479 pmu_battery_count = 1;
480 if (mb == PMAC_TYPE_COMET)
481 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
482 else
483 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
484 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
485 machine_is_compatible("PowerBook1,1")) {
486 pmu_battery_count = 2;
487 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
488 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
489 } else {
30686ba6
SR
490 struct device_node* prim =
491 of_find_node_by_name(NULL, "power-mgt");
018a3d1d 492 const u32 *prim_info = NULL;
1da177e4 493 if (prim)
d05c7a80 494 prim_info = of_get_property(prim, "prim-info", NULL);
1da177e4
LT
495 if (prim_info) {
496 /* Other stuffs here yet unknown */
497 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
498 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
499 if (pmu_battery_count > 1)
500 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
501 }
30686ba6 502 of_node_put(prim);
1da177e4 503 }
8c870933
BH
504#endif /* CONFIG_PPC32 */
505
1da177e4
LT
506 /* Create /proc/pmu */
507 proc_pmu_root = proc_mkdir("pmu", NULL);
508 if (proc_pmu_root) {
8c870933 509 long i;
1da177e4
LT
510
511 for (i=0; i<pmu_battery_count; i++) {
512 char title[16];
8c870933 513 sprintf(title, "battery_%ld", i);
1da177e4
LT
514 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
515 proc_get_batt, (void *)i);
516 }
1da177e4
LT
517
518 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
519 proc_get_info, NULL);
520 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
521 proc_get_irqstats, NULL);
522 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
523 if (proc_pmu_options) {
1da177e4
LT
524 proc_pmu_options->read_proc = proc_read_options;
525 proc_pmu_options->write_proc = proc_write_options;
526 }
527 }
528 return 0;
529}
530
531device_initcall(via_pmu_dev_init);
532
aacaf9bd 533static int
1da177e4
LT
534init_pmu(void)
535{
536 int timeout;
537 struct adb_request req;
538
539 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
540 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
541
542 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
543 timeout = 100000;
544 while (!req.complete) {
545 if (--timeout < 0) {
546 printk(KERN_ERR "init_pmu: no response from PMU\n");
547 return 0;
548 }
549 udelay(10);
550 pmu_poll();
551 }
552
553 /* ack all pending interrupts */
554 timeout = 100000;
555 interrupt_data[0][0] = 1;
556 while (interrupt_data[0][0] || pmu_state != idle) {
557 if (--timeout < 0) {
558 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
559 return 0;
560 }
561 if (pmu_state == idle)
562 adb_int_pending = 1;
7d12e780 563 via_pmu_interrupt(0, NULL);
1da177e4
LT
564 udelay(10);
565 }
566
567 /* Tell PMU we are ready. */
568 if (pmu_kind == PMU_KEYLARGO_BASED) {
569 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
570 while (!req.complete)
571 pmu_poll();
572 }
573
574 /* Read PMU version */
575 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
576 pmu_wait_complete(&req);
577 if (req.reply_len > 0)
578 pmu_version = req.reply[0];
579
580 /* Read server mode setting */
581 if (pmu_kind == PMU_KEYLARGO_BASED) {
582 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
583 PMU_PWR_GET_POWERUP_EVENTS);
584 pmu_wait_complete(&req);
585 if (req.reply_len == 2) {
586 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
587 option_server_mode = 1;
588 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
589 option_server_mode ? "enabled" : "disabled");
590 }
591 }
592 return 1;
593}
594
595int
596pmu_get_model(void)
597{
598 return pmu_kind;
599}
600
1da177e4
LT
601static void pmu_set_server_mode(int server_mode)
602{
603 struct adb_request req;
604
605 if (pmu_kind != PMU_KEYLARGO_BASED)
606 return;
607
608 option_server_mode = server_mode;
609 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
610 pmu_wait_complete(&req);
611 if (req.reply_len < 2)
612 return;
613 if (server_mode)
614 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
615 PMU_PWR_SET_POWERUP_EVENTS,
616 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
617 else
618 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
619 PMU_PWR_CLR_POWERUP_EVENTS,
620 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
621 pmu_wait_complete(&req);
622}
623
1da177e4
LT
624/* This new version of the code for 2400/3400/3500 powerbooks
625 * is inspired from the implementation in gkrellm-pmu
626 */
aacaf9bd 627static void
1da177e4
LT
628done_battery_state_ohare(struct adb_request* req)
629{
630 /* format:
631 * [0] : flags
632 * 0x01 : AC indicator
633 * 0x02 : charging
634 * 0x04 : battery exist
635 * 0x08 :
636 * 0x10 :
637 * 0x20 : full charged
638 * 0x40 : pcharge reset
639 * 0x80 : battery exist
640 *
641 * [1][2] : battery voltage
642 * [3] : CPU temperature
643 * [4] : battery temperature
644 * [5] : current
645 * [6][7] : pcharge
646 * --tkoba
647 */
648 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
649 long pcharge, charge, vb, vmax, lmax;
650 long vmax_charging, vmax_charged;
651 long amperage, voltage, time, max;
652 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
653 NULL, PMAC_MB_INFO_MODEL, 0);
654
655 if (req->reply[0] & 0x01)
656 pmu_power_flags |= PMU_PWR_AC_PRESENT;
657 else
658 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
659
660 if (mb == PMAC_TYPE_COMET) {
661 vmax_charged = 189;
662 vmax_charging = 213;
663 lmax = 6500;
664 } else {
665 vmax_charged = 330;
666 vmax_charging = 330;
667 lmax = 6500;
668 }
669 vmax = vmax_charged;
670
671 /* If battery installed */
672 if (req->reply[0] & 0x04) {
673 bat_flags |= PMU_BATT_PRESENT;
674 if (req->reply[0] & 0x02)
675 bat_flags |= PMU_BATT_CHARGING;
676 vb = (req->reply[1] << 8) | req->reply[2];
677 voltage = (vb * 265 + 72665) / 10;
678 amperage = req->reply[5];
679 if ((req->reply[0] & 0x01) == 0) {
680 if (amperage > 200)
681 vb += ((amperage - 200) * 15)/100;
682 } else if (req->reply[0] & 0x02) {
683 vb = (vb * 97) / 100;
684 vmax = vmax_charging;
685 }
686 charge = (100 * vb) / vmax;
687 if (req->reply[0] & 0x40) {
688 pcharge = (req->reply[6] << 8) + req->reply[7];
689 if (pcharge > lmax)
690 pcharge = lmax;
691 pcharge *= 100;
692 pcharge = 100 - pcharge / lmax;
693 if (pcharge < charge)
694 charge = pcharge;
695 }
696 if (amperage > 0)
697 time = (charge * 16440) / amperage;
698 else
699 time = 0;
700 max = 100;
701 amperage = -amperage;
702 } else
703 charge = max = amperage = voltage = time = 0;
704
705 pmu_batteries[pmu_cur_battery].flags = bat_flags;
706 pmu_batteries[pmu_cur_battery].charge = charge;
707 pmu_batteries[pmu_cur_battery].max_charge = max;
708 pmu_batteries[pmu_cur_battery].amperage = amperage;
709 pmu_batteries[pmu_cur_battery].voltage = voltage;
710 pmu_batteries[pmu_cur_battery].time_remaining = time;
711
712 clear_bit(0, &async_req_locks);
713}
714
aacaf9bd 715static void
1da177e4
LT
716done_battery_state_smart(struct adb_request* req)
717{
718 /* format:
719 * [0] : format of this structure (known: 3,4,5)
720 * [1] : flags
721 *
722 * format 3 & 4:
723 *
724 * [2] : charge
725 * [3] : max charge
726 * [4] : current
727 * [5] : voltage
728 *
729 * format 5:
730 *
731 * [2][3] : charge
732 * [4][5] : max charge
733 * [6][7] : current
734 * [8][9] : voltage
735 */
736
737 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
738 int amperage;
739 unsigned int capa, max, voltage;
740
741 if (req->reply[1] & 0x01)
742 pmu_power_flags |= PMU_PWR_AC_PRESENT;
743 else
744 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
745
746
747 capa = max = amperage = voltage = 0;
748
749 if (req->reply[1] & 0x04) {
750 bat_flags |= PMU_BATT_PRESENT;
751 switch(req->reply[0]) {
752 case 3:
753 case 4: capa = req->reply[2];
754 max = req->reply[3];
755 amperage = *((signed char *)&req->reply[4]);
756 voltage = req->reply[5];
757 break;
758 case 5: capa = (req->reply[2] << 8) | req->reply[3];
759 max = (req->reply[4] << 8) | req->reply[5];
760 amperage = *((signed short *)&req->reply[6]);
761 voltage = (req->reply[8] << 8) | req->reply[9];
762 break;
763 default:
764 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
765 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
766 break;
767 }
768 }
769
770 if ((req->reply[1] & 0x01) && (amperage > 0))
771 bat_flags |= PMU_BATT_CHARGING;
772
773 pmu_batteries[pmu_cur_battery].flags = bat_flags;
774 pmu_batteries[pmu_cur_battery].charge = capa;
775 pmu_batteries[pmu_cur_battery].max_charge = max;
776 pmu_batteries[pmu_cur_battery].amperage = amperage;
777 pmu_batteries[pmu_cur_battery].voltage = voltage;
778 if (amperage) {
779 if ((req->reply[1] & 0x01) && (amperage > 0))
780 pmu_batteries[pmu_cur_battery].time_remaining
781 = ((max-capa) * 3600) / amperage;
782 else
783 pmu_batteries[pmu_cur_battery].time_remaining
784 = (capa * 3600) / (-amperage);
785 } else
786 pmu_batteries[pmu_cur_battery].time_remaining = 0;
787
788 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
789
790 clear_bit(0, &async_req_locks);
791}
792
aacaf9bd 793static void
1da177e4
LT
794query_battery_state(void)
795{
796 if (test_and_set_bit(0, &async_req_locks))
797 return;
798 if (pmu_kind == PMU_OHARE_BASED)
799 pmu_request(&batt_req, done_battery_state_ohare,
800 1, PMU_BATTERY_STATE);
801 else
802 pmu_request(&batt_req, done_battery_state_smart,
803 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
804}
805
aacaf9bd 806static int
1da177e4
LT
807proc_get_info(char *page, char **start, off_t off,
808 int count, int *eof, void *data)
809{
810 char* p = page;
811
812 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
813 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
1da177e4 814 p += sprintf(p, "AC Power : %d\n",
63e1fd41 815 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
1da177e4 816 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
1da177e4
LT
817
818 return p - page;
819}
820
aacaf9bd 821static int
1da177e4
LT
822proc_get_irqstats(char *page, char **start, off_t off,
823 int count, int *eof, void *data)
824{
825 int i;
826 char* p = page;
827 static const char *irq_names[] = {
828 "Total CB1 triggered events",
829 "Total GPIO1 triggered events",
830 "PC-Card eject button",
831 "Sound/Brightness button",
832 "ADB message",
833 "Battery state change",
834 "Environment interrupt",
835 "Tick timer",
836 "Ghost interrupt (zero len)",
837 "Empty interrupt (empty mask)",
838 "Max irqs in a row"
839 };
840
841 for (i=0; i<11; i++) {
842 p += sprintf(p, " %2u: %10u (%s)\n",
843 i, pmu_irq_stats[i], irq_names[i]);
844 }
845 return p - page;
846}
847
aacaf9bd 848static int
1da177e4
LT
849proc_get_batt(char *page, char **start, off_t off,
850 int count, int *eof, void *data)
851{
8c870933 852 long batnum = (long)data;
1da177e4
LT
853 char *p = page;
854
855 p += sprintf(p, "\n");
856 p += sprintf(p, "flags : %08x\n",
857 pmu_batteries[batnum].flags);
858 p += sprintf(p, "charge : %d\n",
859 pmu_batteries[batnum].charge);
860 p += sprintf(p, "max_charge : %d\n",
861 pmu_batteries[batnum].max_charge);
862 p += sprintf(p, "current : %d\n",
863 pmu_batteries[batnum].amperage);
864 p += sprintf(p, "voltage : %d\n",
865 pmu_batteries[batnum].voltage);
866 p += sprintf(p, "time rem. : %d\n",
867 pmu_batteries[batnum].time_remaining);
868
869 return p - page;
870}
1da177e4 871
aacaf9bd 872static int
1da177e4
LT
873proc_read_options(char *page, char **start, off_t off,
874 int count, int *eof, void *data)
875{
876 char *p = page;
877
a0005034 878#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1da177e4
LT
879 if (pmu_kind == PMU_KEYLARGO_BASED &&
880 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
881 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
8c870933 882#endif
1da177e4
LT
883 if (pmu_kind == PMU_KEYLARGO_BASED)
884 p += sprintf(p, "server_mode=%d\n", option_server_mode);
885
886 return p - page;
887}
888
aacaf9bd 889static int
1da177e4
LT
890proc_write_options(struct file *file, const char __user *buffer,
891 unsigned long count, void *data)
892{
893 char tmp[33];
894 char *label, *val;
895 unsigned long fcount = count;
896
897 if (!count)
898 return -EINVAL;
899 if (count > 32)
900 count = 32;
901 if (copy_from_user(tmp, buffer, count))
902 return -EFAULT;
903 tmp[count] = 0;
904
905 label = tmp;
906 while(*label == ' ')
907 label++;
908 val = label;
909 while(*val && (*val != '=')) {
910 if (*val == ' ')
911 *val = 0;
912 val++;
913 }
914 if ((*val) == 0)
915 return -EINVAL;
916 *(val++) = 0;
917 while(*val == ' ')
918 val++;
a0005034 919#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1da177e4
LT
920 if (pmu_kind == PMU_KEYLARGO_BASED &&
921 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
922 if (!strcmp(label, "lid_wakeup"))
923 option_lid_wakeup = ((*val) == '1');
8c870933 924#endif
1da177e4
LT
925 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
926 int new_value;
927 new_value = ((*val) == '1');
928 if (new_value != option_server_mode)
929 pmu_set_server_mode(new_value);
930 }
931 return fcount;
932}
933
934#ifdef CONFIG_ADB
935/* Send an ADB command */
aacaf9bd 936static int
1da177e4
LT
937pmu_send_request(struct adb_request *req, int sync)
938{
939 int i, ret;
940
941 if ((vias == NULL) || (!pmu_fully_inited)) {
942 req->complete = 1;
943 return -ENXIO;
944 }
945
946 ret = -EINVAL;
947
948 switch (req->data[0]) {
949 case PMU_PACKET:
950 for (i = 0; i < req->nbytes - 1; ++i)
951 req->data[i] = req->data[i+1];
952 --req->nbytes;
953 if (pmu_data_len[req->data[0]][1] != 0) {
954 req->reply[0] = ADB_RET_OK;
955 req->reply_len = 1;
956 } else
957 req->reply_len = 0;
958 ret = pmu_queue_request(req);
959 break;
960 case CUDA_PACKET:
961 switch (req->data[1]) {
962 case CUDA_GET_TIME:
963 if (req->nbytes != 2)
964 break;
965 req->data[0] = PMU_READ_RTC;
966 req->nbytes = 1;
967 req->reply_len = 3;
968 req->reply[0] = CUDA_PACKET;
969 req->reply[1] = 0;
970 req->reply[2] = CUDA_GET_TIME;
971 ret = pmu_queue_request(req);
972 break;
973 case CUDA_SET_TIME:
974 if (req->nbytes != 6)
975 break;
976 req->data[0] = PMU_SET_RTC;
977 req->nbytes = 5;
978 for (i = 1; i <= 4; ++i)
979 req->data[i] = req->data[i+1];
980 req->reply_len = 3;
981 req->reply[0] = CUDA_PACKET;
982 req->reply[1] = 0;
983 req->reply[2] = CUDA_SET_TIME;
984 ret = pmu_queue_request(req);
985 break;
986 }
987 break;
988 case ADB_PACKET:
989 if (!pmu_has_adb)
990 return -ENXIO;
991 for (i = req->nbytes - 1; i > 1; --i)
992 req->data[i+2] = req->data[i];
993 req->data[3] = req->nbytes - 2;
994 req->data[2] = pmu_adb_flags;
995 /*req->data[1] = req->data[1];*/
996 req->data[0] = PMU_ADB_CMD;
997 req->nbytes += 2;
998 req->reply_expected = 1;
999 req->reply_len = 0;
1000 ret = pmu_queue_request(req);
1001 break;
1002 }
1003 if (ret) {
1004 req->complete = 1;
1005 return ret;
1006 }
1007
1008 if (sync)
1009 while (!req->complete)
1010 pmu_poll();
1011
1012 return 0;
1013}
1014
1015/* Enable/disable autopolling */
aacaf9bd 1016static int
1da177e4
LT
1017pmu_adb_autopoll(int devs)
1018{
1019 struct adb_request req;
1020
1021 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1022 return -ENXIO;
1023
1024 if (devs) {
1025 adb_dev_map = devs;
1026 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1027 adb_dev_map >> 8, adb_dev_map);
1028 pmu_adb_flags = 2;
1029 } else {
1030 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1031 pmu_adb_flags = 0;
1032 }
1033 while (!req.complete)
1034 pmu_poll();
1035 return 0;
1036}
1037
1038/* Reset the ADB bus */
aacaf9bd 1039static int
1da177e4
LT
1040pmu_adb_reset_bus(void)
1041{
1042 struct adb_request req;
1043 int save_autopoll = adb_dev_map;
1044
1045 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1046 return -ENXIO;
1047
1048 /* anyone got a better idea?? */
1049 pmu_adb_autopoll(0);
1050
1051 req.nbytes = 5;
1052 req.done = NULL;
1053 req.data[0] = PMU_ADB_CMD;
1054 req.data[1] = 0;
1055 req.data[2] = ADB_BUSRESET;
1056 req.data[3] = 0;
1057 req.data[4] = 0;
1058 req.reply_len = 0;
1059 req.reply_expected = 1;
1060 if (pmu_queue_request(&req) != 0) {
1061 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1062 return -EIO;
1063 }
1064 pmu_wait_complete(&req);
1065
1066 if (save_autopoll != 0)
1067 pmu_adb_autopoll(save_autopoll);
1068
1069 return 0;
1070}
1071#endif /* CONFIG_ADB */
1072
1073/* Construct and send a pmu request */
aacaf9bd 1074int
1da177e4
LT
1075pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1076 int nbytes, ...)
1077{
1078 va_list list;
1079 int i;
1080
1081 if (vias == NULL)
1082 return -ENXIO;
1083
1084 if (nbytes < 0 || nbytes > 32) {
1085 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1086 req->complete = 1;
1087 return -EINVAL;
1088 }
1089 req->nbytes = nbytes;
1090 req->done = done;
1091 va_start(list, nbytes);
1092 for (i = 0; i < nbytes; ++i)
1093 req->data[i] = va_arg(list, int);
1094 va_end(list);
1095 req->reply_len = 0;
1096 req->reply_expected = 0;
1097 return pmu_queue_request(req);
1098}
1099
aacaf9bd 1100int
1da177e4
LT
1101pmu_queue_request(struct adb_request *req)
1102{
1103 unsigned long flags;
1104 int nsend;
1105
1106 if (via == NULL) {
1107 req->complete = 1;
1108 return -ENXIO;
1109 }
1110 if (req->nbytes <= 0) {
1111 req->complete = 1;
1112 return 0;
1113 }
1114 nsend = pmu_data_len[req->data[0]][0];
1115 if (nsend >= 0 && req->nbytes != nsend + 1) {
1116 req->complete = 1;
1117 return -EINVAL;
1118 }
1119
1120 req->next = NULL;
1121 req->sent = 0;
1122 req->complete = 0;
1123
1124 spin_lock_irqsave(&pmu_lock, flags);
1125 if (current_req != 0) {
1126 last_req->next = req;
1127 last_req = req;
1128 } else {
1129 current_req = req;
1130 last_req = req;
1131 if (pmu_state == idle)
1132 pmu_start();
1133 }
1134 spin_unlock_irqrestore(&pmu_lock, flags);
1135
1136 return 0;
1137}
1138
1139static inline void
1140wait_for_ack(void)
1141{
1142 /* Sightly increased the delay, I had one occurrence of the message
1143 * reported
1144 */
1145 int timeout = 4000;
1146 while ((in_8(&via[B]) & TACK) == 0) {
1147 if (--timeout < 0) {
1148 printk(KERN_ERR "PMU not responding (!ack)\n");
1149 return;
1150 }
1151 udelay(10);
1152 }
1153}
1154
1155/* New PMU seems to be very sensitive to those timings, so we make sure
1156 * PCI is flushed immediately */
1157static inline void
1158send_byte(int x)
1159{
1160 volatile unsigned char __iomem *v = via;
1161
1162 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1163 out_8(&v[SR], x);
1164 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1165 (void)in_8(&v[B]);
1166}
1167
1168static inline void
1169recv_byte(void)
1170{
1171 volatile unsigned char __iomem *v = via;
1172
1173 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1174 in_8(&v[SR]); /* resets SR */
1175 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1176 (void)in_8(&v[B]);
1177}
1178
1179static inline void
1180pmu_done(struct adb_request *req)
1181{
1182 void (*done)(struct adb_request *) = req->done;
1183 mb();
1184 req->complete = 1;
1185 /* Here, we assume that if the request has a done member, the
1186 * struct request will survive to setting req->complete to 1
1187 */
1188 if (done)
1189 (*done)(req);
1190}
1191
aacaf9bd 1192static void
1da177e4
LT
1193pmu_start(void)
1194{
1195 struct adb_request *req;
1196
1197 /* assert pmu_state == idle */
1198 /* get the packet to send */
1199 req = current_req;
1200 if (req == 0 || pmu_state != idle
1201 || (/*req->reply_expected && */req_awaiting_reply))
1202 return;
1203
1204 pmu_state = sending;
1205 data_index = 1;
1206 data_len = pmu_data_len[req->data[0]][0];
1207
1208 /* Sounds safer to make sure ACK is high before writing. This helped
1209 * kill a problem with ADB and some iBooks
1210 */
1211 wait_for_ack();
1212 /* set the shift register to shift out and send a byte */
1213 send_byte(req->data[0]);
1214}
1215
aacaf9bd 1216void
1da177e4
LT
1217pmu_poll(void)
1218{
1219 if (!via)
1220 return;
1221 if (disable_poll)
1222 return;
7d12e780 1223 via_pmu_interrupt(0, NULL);
1da177e4
LT
1224}
1225
aacaf9bd 1226void
1da177e4
LT
1227pmu_poll_adb(void)
1228{
1229 if (!via)
1230 return;
1231 if (disable_poll)
1232 return;
1233 /* Kicks ADB read when PMU is suspended */
1234 adb_int_pending = 1;
1235 do {
7d12e780 1236 via_pmu_interrupt(0, NULL);
1da177e4
LT
1237 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1238 || req_awaiting_reply));
1239}
1240
aacaf9bd 1241void
1da177e4
LT
1242pmu_wait_complete(struct adb_request *req)
1243{
1244 if (!via)
1245 return;
1246 while((pmu_state != idle && pmu_state != locked) || !req->complete)
7d12e780 1247 via_pmu_interrupt(0, NULL);
1da177e4
LT
1248}
1249
1250/* This function loops until the PMU is idle and prevents it from
1251 * anwsering to ADB interrupts. pmu_request can still be called.
1252 * This is done to avoid spurrious shutdowns when we know we'll have
1253 * interrupts switched off for a long time
1254 */
aacaf9bd 1255void
1da177e4
LT
1256pmu_suspend(void)
1257{
1258 unsigned long flags;
1259#ifdef SUSPEND_USES_PMU
1260 struct adb_request *req;
1261#endif
1262 if (!via)
1263 return;
1264
1265 spin_lock_irqsave(&pmu_lock, flags);
1266 pmu_suspended++;
1267 if (pmu_suspended > 1) {
1268 spin_unlock_irqrestore(&pmu_lock, flags);
1269 return;
1270 }
1271
1272 do {
1273 spin_unlock_irqrestore(&pmu_lock, flags);
1274 if (req_awaiting_reply)
1275 adb_int_pending = 1;
7d12e780 1276 via_pmu_interrupt(0, NULL);
1da177e4
LT
1277 spin_lock_irqsave(&pmu_lock, flags);
1278 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1279#ifdef SUSPEND_USES_PMU
1280 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1281 spin_unlock_irqrestore(&pmu_lock, flags);
1282 while(!req.complete)
1283 pmu_poll();
1284#else /* SUSPEND_USES_PMU */
1285 if (gpio_irq >= 0)
1286 disable_irq_nosync(gpio_irq);
1287 out_8(&via[IER], CB1_INT | IER_CLR);
1288 spin_unlock_irqrestore(&pmu_lock, flags);
1289#endif /* SUSPEND_USES_PMU */
1290 break;
1291 }
1292 } while (1);
1293}
1294
aacaf9bd 1295void
1da177e4
LT
1296pmu_resume(void)
1297{
1298 unsigned long flags;
1299
1300 if (!via || (pmu_suspended < 1))
1301 return;
1302
1303 spin_lock_irqsave(&pmu_lock, flags);
1304 pmu_suspended--;
1305 if (pmu_suspended > 0) {
1306 spin_unlock_irqrestore(&pmu_lock, flags);
1307 return;
1308 }
1309 adb_int_pending = 1;
1310#ifdef SUSPEND_USES_PMU
1311 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1312 spin_unlock_irqrestore(&pmu_lock, flags);
1313 while(!req.complete)
1314 pmu_poll();
1315#else /* SUSPEND_USES_PMU */
1316 if (gpio_irq >= 0)
1317 enable_irq(gpio_irq);
1318 out_8(&via[IER], CB1_INT | IER_SET);
1319 spin_unlock_irqrestore(&pmu_lock, flags);
1320 pmu_poll();
1321#endif /* SUSPEND_USES_PMU */
1322}
1323
1324/* Interrupt data could be the result data from an ADB cmd */
aacaf9bd 1325static void
7d12e780 1326pmu_handle_data(unsigned char *data, int len)
1da177e4
LT
1327{
1328 unsigned char ints, pirq;
1329 int i = 0;
1330
1331 asleep = 0;
1332 if (drop_interrupts || len < 1) {
1333 adb_int_pending = 0;
1334 pmu_irq_stats[8]++;
1335 return;
1336 }
1337
1338 /* Get PMU interrupt mask */
1339 ints = data[0];
1340
1341 /* Record zero interrupts for stats */
1342 if (ints == 0)
1343 pmu_irq_stats[9]++;
1344
1345 /* Hack to deal with ADB autopoll flag */
1346 if (ints & PMU_INT_ADB)
1347 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1348
1349next:
1350
1351 if (ints == 0) {
1352 if (i > pmu_irq_stats[10])
1353 pmu_irq_stats[10] = i;
1354 return;
1355 }
1356
1357 for (pirq = 0; pirq < 8; pirq++)
1358 if (ints & (1 << pirq))
1359 break;
1360 pmu_irq_stats[pirq]++;
1361 i++;
1362 ints &= ~(1 << pirq);
1363
1364 /* Note: for some reason, we get an interrupt with len=1,
1365 * data[0]==0 after each normal ADB interrupt, at least
1366 * on the Pismo. Still investigating... --BenH
1367 */
1368 if ((1 << pirq) & PMU_INT_ADB) {
1369 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1370 struct adb_request *req = req_awaiting_reply;
1371 if (req == 0) {
1372 printk(KERN_ERR "PMU: extra ADB reply\n");
1373 return;
1374 }
1375 req_awaiting_reply = NULL;
1376 if (len <= 2)
1377 req->reply_len = 0;
1378 else {
1379 memcpy(req->reply, data + 1, len - 1);
1380 req->reply_len = len - 1;
1381 }
1382 pmu_done(req);
1383 } else {
1da177e4
LT
1384 if (len == 4 && data[1] == 0x2c) {
1385 extern int xmon_wants_key, xmon_adb_keycode;
1386 if (xmon_wants_key) {
1387 xmon_adb_keycode = data[2];
1388 return;
1389 }
1390 }
1da177e4
LT
1391#ifdef CONFIG_ADB
1392 /*
1393 * XXX On the [23]400 the PMU gives us an up
1394 * event for keycodes 0x74 or 0x75 when the PC
1395 * card eject buttons are released, so we
1396 * ignore those events.
1397 */
1398 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1399 && data[1] == 0x2c && data[3] == 0xff
1400 && (data[2] & ~1) == 0xf4))
7d12e780 1401 adb_input(data+1, len-1, 1);
1da177e4
LT
1402#endif /* CONFIG_ADB */
1403 }
1404 }
1405 /* Sound/brightness button pressed */
1406 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1407#ifdef CONFIG_PMAC_BACKLIGHT
1408 if (len == 3)
4b755999
MH
1409 pmac_backlight_set_legacy_brightness_pmu(data[1] >> 4);
1410#endif
1da177e4
LT
1411 }
1412 /* Tick interrupt */
1413 else if ((1 << pirq) & PMU_INT_TICK) {
1da177e4
LT
1414 /* Environement or tick interrupt, query batteries */
1415 if (pmu_battery_count) {
1416 if ((--query_batt_timer) == 0) {
1417 query_battery_state();
1418 query_batt_timer = BATTERY_POLLING_COUNT;
1419 }
1420 }
1421 }
1422 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1423 if (pmu_battery_count)
1424 query_battery_state();
1425 pmu_pass_intr(data, len);
9e8e30a0
JB
1426 /* len == 6 is probably a bad check. But how do I
1427 * know what PMU versions send what events here? */
1428 if (len == 6) {
1429 via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
1430 via_pmu_event(PMU_EVT_LID, data[1]&1);
1431 }
1da177e4
LT
1432 } else {
1433 pmu_pass_intr(data, len);
1da177e4
LT
1434 }
1435 goto next;
1436}
1437
aacaf9bd 1438static struct adb_request*
7d12e780 1439pmu_sr_intr(void)
1da177e4
LT
1440{
1441 struct adb_request *req;
1442 int bite = 0;
1443
1444 if (via[B] & TREQ) {
1445 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1446 out_8(&via[IFR], SR_INT);
1447 return NULL;
1448 }
1449 /* The ack may not yet be low when we get the interrupt */
1450 while ((in_8(&via[B]) & TACK) != 0)
1451 ;
1452
1453 /* if reading grab the byte, and reset the interrupt */
1454 if (pmu_state == reading || pmu_state == reading_intr)
1455 bite = in_8(&via[SR]);
1456
1457 /* reset TREQ and wait for TACK to go high */
1458 out_8(&via[B], in_8(&via[B]) | TREQ);
1459 wait_for_ack();
1460
1461 switch (pmu_state) {
1462 case sending:
1463 req = current_req;
1464 if (data_len < 0) {
1465 data_len = req->nbytes - 1;
1466 send_byte(data_len);
1467 break;
1468 }
1469 if (data_index <= data_len) {
1470 send_byte(req->data[data_index++]);
1471 break;
1472 }
1473 req->sent = 1;
1474 data_len = pmu_data_len[req->data[0]][1];
1475 if (data_len == 0) {
1476 pmu_state = idle;
1477 current_req = req->next;
1478 if (req->reply_expected)
1479 req_awaiting_reply = req;
1480 else
1481 return req;
1482 } else {
1483 pmu_state = reading;
1484 data_index = 0;
1485 reply_ptr = req->reply + req->reply_len;
1486 recv_byte();
1487 }
1488 break;
1489
1490 case intack:
1491 data_index = 0;
1492 data_len = -1;
1493 pmu_state = reading_intr;
1494 reply_ptr = interrupt_data[int_data_last];
1495 recv_byte();
1496 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1497 enable_irq(gpio_irq);
1498 gpio_irq_enabled = 1;
1499 }
1500 break;
1501
1502 case reading:
1503 case reading_intr:
1504 if (data_len == -1) {
1505 data_len = bite;
1506 if (bite > 32)
1507 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1508 } else if (data_index < 32) {
1509 reply_ptr[data_index++] = bite;
1510 }
1511 if (data_index < data_len) {
1512 recv_byte();
1513 break;
1514 }
1515
1516 if (pmu_state == reading_intr) {
1517 pmu_state = idle;
1518 int_data_state[int_data_last] = int_data_ready;
1519 interrupt_data_len[int_data_last] = data_len;
1520 } else {
1521 req = current_req;
1522 /*
1523 * For PMU sleep and freq change requests, we lock the
1524 * PMU until it's explicitely unlocked. This avoids any
1525 * spurrious event polling getting in
1526 */
1527 current_req = req->next;
1528 req->reply_len += data_index;
1529 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1530 pmu_state = locked;
1531 else
1532 pmu_state = idle;
1533 return req;
1534 }
1535 break;
1536
1537 default:
1538 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1539 pmu_state);
1540 }
1541 return NULL;
1542}
1543
aacaf9bd 1544static irqreturn_t
7d12e780 1545via_pmu_interrupt(int irq, void *arg)
1da177e4
LT
1546{
1547 unsigned long flags;
1548 int intr;
1549 int nloop = 0;
1550 int int_data = -1;
1551 struct adb_request *req = NULL;
1552 int handled = 0;
1553
1554 /* This is a bit brutal, we can probably do better */
1555 spin_lock_irqsave(&pmu_lock, flags);
1556 ++disable_poll;
1557
1558 for (;;) {
1559 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1560 if (intr == 0)
1561 break;
1562 handled = 1;
1563 if (++nloop > 1000) {
1564 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1565 "intr=%x, ier=%x pmu_state=%d\n",
1566 intr, in_8(&via[IER]), pmu_state);
1567 break;
1568 }
1569 out_8(&via[IFR], intr);
1570 if (intr & CB1_INT) {
1571 adb_int_pending = 1;
1572 pmu_irq_stats[0]++;
1573 }
1574 if (intr & SR_INT) {
7d12e780 1575 req = pmu_sr_intr();
1da177e4
LT
1576 if (req)
1577 break;
1578 }
1579 }
1580
1581recheck:
1582 if (pmu_state == idle) {
1583 if (adb_int_pending) {
1584 if (int_data_state[0] == int_data_empty)
1585 int_data_last = 0;
1586 else if (int_data_state[1] == int_data_empty)
1587 int_data_last = 1;
1588 else
1589 goto no_free_slot;
1590 pmu_state = intack;
1591 int_data_state[int_data_last] = int_data_fill;
1592 /* Sounds safer to make sure ACK is high before writing.
1593 * This helped kill a problem with ADB and some iBooks
1594 */
1595 wait_for_ack();
1596 send_byte(PMU_INT_ACK);
1597 adb_int_pending = 0;
1598 } else if (current_req)
1599 pmu_start();
1600 }
1601no_free_slot:
1602 /* Mark the oldest buffer for flushing */
1603 if (int_data_state[!int_data_last] == int_data_ready) {
1604 int_data_state[!int_data_last] = int_data_flush;
1605 int_data = !int_data_last;
1606 } else if (int_data_state[int_data_last] == int_data_ready) {
1607 int_data_state[int_data_last] = int_data_flush;
1608 int_data = int_data_last;
1609 }
1610 --disable_poll;
1611 spin_unlock_irqrestore(&pmu_lock, flags);
1612
1613 /* Deal with completed PMU requests outside of the lock */
1614 if (req) {
1615 pmu_done(req);
1616 req = NULL;
1617 }
1618
1619 /* Deal with interrupt datas outside of the lock */
1620 if (int_data >= 0) {
7d12e780 1621 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data]);
1da177e4
LT
1622 spin_lock_irqsave(&pmu_lock, flags);
1623 ++disable_poll;
1624 int_data_state[int_data] = int_data_empty;
1625 int_data = -1;
1626 goto recheck;
1627 }
1628
1629 return IRQ_RETVAL(handled);
1630}
1631
aacaf9bd 1632void
1da177e4
LT
1633pmu_unlock(void)
1634{
1635 unsigned long flags;
1636
1637 spin_lock_irqsave(&pmu_lock, flags);
1638 if (pmu_state == locked)
1639 pmu_state = idle;
1640 adb_int_pending = 1;
1641 spin_unlock_irqrestore(&pmu_lock, flags);
1642}
1643
1644
aacaf9bd 1645static irqreturn_t
7d12e780 1646gpio1_interrupt(int irq, void *arg)
1da177e4
LT
1647{
1648 unsigned long flags;
1649
1650 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1651 spin_lock_irqsave(&pmu_lock, flags);
1652 if (gpio_irq_enabled > 0) {
1653 disable_irq_nosync(gpio_irq);
1654 gpio_irq_enabled = 0;
1655 }
1656 pmu_irq_stats[1]++;
1657 adb_int_pending = 1;
1658 spin_unlock_irqrestore(&pmu_lock, flags);
7d12e780 1659 via_pmu_interrupt(0, NULL);
1da177e4
LT
1660 return IRQ_HANDLED;
1661 }
1662 return IRQ_NONE;
1663}
1664
aacaf9bd 1665void
1da177e4
LT
1666pmu_enable_irled(int on)
1667{
1668 struct adb_request req;
1669
1670 if (vias == NULL)
1671 return ;
1672 if (pmu_kind == PMU_KEYLARGO_BASED)
1673 return ;
1674
1675 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1676 (on ? PMU_POW_ON : PMU_POW_OFF));
1677 pmu_wait_complete(&req);
1678}
1679
aacaf9bd 1680void
1da177e4
LT
1681pmu_restart(void)
1682{
1683 struct adb_request req;
1684
1685 if (via == NULL)
1686 return;
1687
1688 local_irq_disable();
1689
1690 drop_interrupts = 1;
1691
1692 if (pmu_kind != PMU_KEYLARGO_BASED) {
1693 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1694 PMU_INT_TICK );
1695 while(!req.complete)
1696 pmu_poll();
1697 }
1698
1699 pmu_request(&req, NULL, 1, PMU_RESET);
1700 pmu_wait_complete(&req);
1701 for (;;)
1702 ;
1703}
1704
aacaf9bd 1705void
1da177e4
LT
1706pmu_shutdown(void)
1707{
1708 struct adb_request req;
1709
1710 if (via == NULL)
1711 return;
1712
1713 local_irq_disable();
1714
1715 drop_interrupts = 1;
1716
1717 if (pmu_kind != PMU_KEYLARGO_BASED) {
1718 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1719 PMU_INT_TICK );
1720 pmu_wait_complete(&req);
1721 } else {
1722 /* Disable server mode on shutdown or we'll just
1723 * wake up again
1724 */
1725 pmu_set_server_mode(0);
1726 }
1727
1728 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1729 'M', 'A', 'T', 'T');
1730 pmu_wait_complete(&req);
1731 for (;;)
1732 ;
1733}
1734
1735int
1736pmu_present(void)
1737{
1738 return via != 0;
1739}
1740
8c870933 1741#ifdef CONFIG_PM
1da177e4
LT
1742
1743static LIST_HEAD(sleep_notifiers);
1744
1745int
1746pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1747{
1748 struct list_head *list;
1749 struct pmu_sleep_notifier *notifier;
1750
1751 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1752 list = list->next) {
1753 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1754 if (n->priority > notifier->priority)
1755 break;
1756 }
1757 __list_add(&n->list, list->prev, list);
1758 return 0;
1759}
3fb62b51 1760EXPORT_SYMBOL(pmu_register_sleep_notifier);
1da177e4
LT
1761
1762int
1763pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1764{
1765 if (n->list.next == 0)
1766 return -ENOENT;
1767 list_del(&n->list);
1768 n->list.next = NULL;
1769 return 0;
1770}
3fb62b51 1771EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
a0005034
PM
1772#endif /* CONFIG_PM */
1773
1774#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1da177e4
LT
1775
1776/* Sleep is broadcast last-to-first */
70b52b38 1777static void broadcast_sleep(int when)
1da177e4 1778{
1da177e4
LT
1779 struct list_head *list;
1780 struct pmu_sleep_notifier *notifier;
1781
1782 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1783 list = list->prev) {
1784 notifier = list_entry(list, struct pmu_sleep_notifier, list);
70b52b38 1785 notifier->notifier_call(notifier, when);
1da177e4 1786 }
1da177e4
LT
1787}
1788
1789/* Wake is broadcast first-to-last */
70b52b38 1790static void broadcast_wake(void)
1da177e4 1791{
1da177e4
LT
1792 struct list_head *list;
1793 struct pmu_sleep_notifier *notifier;
1794
1795 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1796 list = list->next) {
1797 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1798 notifier->notifier_call(notifier, PBOOK_WAKE);
1799 }
1da177e4
LT
1800}
1801
1802/*
1803 * This struct is used to store config register values for
1804 * PCI devices which may get powered off when we sleep.
1805 */
1806static struct pci_save {
1807#ifndef HACKED_PCI_SAVE
1808 u16 command;
1809 u16 cache_lat;
1810 u16 intr;
1811 u32 rom_address;
1812#else
1813 u32 config[16];
1814#endif
1815} *pbook_pci_saves;
1816static int pbook_npci_saves;
1817
aacaf9bd 1818static void
1da177e4
LT
1819pbook_alloc_pci_save(void)
1820{
1821 int npci;
1822 struct pci_dev *pd = NULL;
1823
1824 npci = 0;
edceeaf5 1825 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1da177e4
LT
1826 ++npci;
1827 }
1828 if (npci == 0)
1829 return;
1830 pbook_pci_saves = (struct pci_save *)
1831 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
1832 pbook_npci_saves = npci;
1833}
1834
aacaf9bd 1835static void
1da177e4
LT
1836pbook_free_pci_save(void)
1837{
1838 if (pbook_pci_saves == NULL)
1839 return;
1840 kfree(pbook_pci_saves);
1841 pbook_pci_saves = NULL;
1842 pbook_npci_saves = 0;
1843}
1844
aacaf9bd 1845static void
1da177e4
LT
1846pbook_pci_save(void)
1847{
1848 struct pci_save *ps = pbook_pci_saves;
1849 struct pci_dev *pd = NULL;
1850 int npci = pbook_npci_saves;
1851
1852 if (ps == NULL)
1853 return;
1854
edceeaf5
AC
1855 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1856 if (npci-- == 0) {
1857 pci_dev_put(pd);
1da177e4 1858 return;
edceeaf5 1859 }
1da177e4
LT
1860#ifndef HACKED_PCI_SAVE
1861 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1862 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1863 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1864 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1865#else
1866 int i;
1867 for (i=1;i<16;i++)
1868 pci_read_config_dword(pd, i<<4, &ps->config[i]);
1869#endif
1870 ++ps;
1871 }
1872}
1873
1874/* For this to work, we must take care of a few things: If gmac was enabled
1875 * during boot, it will be in the pci dev list. If it's disabled at this point
1876 * (and it will probably be), then you can't access it's config space.
1877 */
aacaf9bd 1878static void
1da177e4
LT
1879pbook_pci_restore(void)
1880{
1881 u16 cmd;
1882 struct pci_save *ps = pbook_pci_saves - 1;
1883 struct pci_dev *pd = NULL;
1884 int npci = pbook_npci_saves;
1885 int j;
1886
edceeaf5 1887 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1da177e4
LT
1888#ifdef HACKED_PCI_SAVE
1889 int i;
edceeaf5
AC
1890 if (npci-- == 0) {
1891 pci_dev_put(pd);
1da177e4 1892 return;
edceeaf5 1893 }
1da177e4
LT
1894 ps++;
1895 for (i=2;i<16;i++)
1896 pci_write_config_dword(pd, i<<4, ps->config[i]);
1897 pci_write_config_dword(pd, 4, ps->config[1]);
1898#else
1899 if (npci-- == 0)
1900 return;
1901 ps++;
1902 if (ps->command == 0)
1903 continue;
1904 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1905 if ((ps->command & ~cmd) == 0)
1906 continue;
1907 switch (pd->hdr_type) {
1908 case PCI_HEADER_TYPE_NORMAL:
1909 for (j = 0; j < 6; ++j)
1910 pci_write_config_dword(pd,
1911 PCI_BASE_ADDRESS_0 + j*4,
1912 pd->resource[j].start);
1913 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1914 ps->rom_address);
1915 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1916 ps->cache_lat);
1917 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1918 ps->intr);
1919 pci_write_config_word(pd, PCI_COMMAND, ps->command);
1920 break;
1921 }
1922#endif
1923 }
1924}
1925
1926#ifdef DEBUG_SLEEP
1927/* N.B. This doesn't work on the 3400 */
aacaf9bd 1928void
1da177e4
LT
1929pmu_blink(int n)
1930{
1931 struct adb_request req;
1932
1933 memset(&req, 0, sizeof(req));
1934
1935 for (; n > 0; --n) {
1936 req.nbytes = 4;
1937 req.done = NULL;
1938 req.data[0] = 0xee;
1939 req.data[1] = 4;
1940 req.data[2] = 0;
1941 req.data[3] = 1;
1942 req.reply[0] = ADB_RET_OK;
1943 req.reply_len = 1;
1944 req.reply_expected = 0;
1945 pmu_polled_request(&req);
1946 mdelay(50);
1947 req.nbytes = 4;
1948 req.done = NULL;
1949 req.data[0] = 0xee;
1950 req.data[1] = 4;
1951 req.data[2] = 0;
1952 req.data[3] = 0;
1953 req.reply[0] = ADB_RET_OK;
1954 req.reply_len = 1;
1955 req.reply_expected = 0;
1956 pmu_polled_request(&req);
1957 mdelay(50);
1958 }
1959 mdelay(50);
1960}
1961#endif
1962
1963/*
1964 * Put the powerbook to sleep.
1965 */
1966
aacaf9bd 1967static u32 save_via[8];
1da177e4 1968
aacaf9bd 1969static void
1da177e4
LT
1970save_via_state(void)
1971{
1972 save_via[0] = in_8(&via[ANH]);
1973 save_via[1] = in_8(&via[DIRA]);
1974 save_via[2] = in_8(&via[B]);
1975 save_via[3] = in_8(&via[DIRB]);
1976 save_via[4] = in_8(&via[PCR]);
1977 save_via[5] = in_8(&via[ACR]);
1978 save_via[6] = in_8(&via[T1CL]);
1979 save_via[7] = in_8(&via[T1CH]);
1980}
aacaf9bd 1981static void
1da177e4
LT
1982restore_via_state(void)
1983{
1984 out_8(&via[ANH], save_via[0]);
1985 out_8(&via[DIRA], save_via[1]);
1986 out_8(&via[B], save_via[2]);
1987 out_8(&via[DIRB], save_via[3]);
1988 out_8(&via[PCR], save_via[4]);
1989 out_8(&via[ACR], save_via[5]);
1990 out_8(&via[T1CL], save_via[6]);
1991 out_8(&via[T1CH], save_via[7]);
1992 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
1993 out_8(&via[IFR], 0x7f); /* clear IFR */
1994 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
1995}
1996
d565dd3b
BH
1997extern void pmu_backlight_set_sleep(int sleep);
1998
aacaf9bd 1999static int
1da177e4
LT
2000pmac_suspend_devices(void)
2001{
2002 int ret;
2003
2004 pm_prepare_console();
2005
70b52b38
JB
2006 /* Notify old-style device drivers */
2007 broadcast_sleep(PBOOK_SLEEP_REQUEST);
1da177e4
LT
2008
2009 /* Sync the disks. */
2010 /* XXX It would be nice to have some way to ensure that
2011 * nobody is dirtying any new buffers while we wait. That
2012 * could be achieved using the refrigerator for processes
2013 * that swsusp uses
2014 */
2015 sys_sync();
2016
70b52b38 2017 broadcast_sleep(PBOOK_SLEEP_NOW);
1da177e4
LT
2018
2019 /* Send suspend call to devices, hold the device core's dpm_sem */
2020 ret = device_suspend(PMSG_SUSPEND);
2021 if (ret) {
2022 broadcast_wake();
2023 printk(KERN_ERR "Driver sleep failed\n");
2024 return -EBUSY;
2025 }
2026
d565dd3b
BH
2027#ifdef CONFIG_PMAC_BACKLIGHT
2028 /* Tell backlight code not to muck around with the chip anymore */
2029 pmu_backlight_set_sleep(1);
2030#endif
2031
5b9ca526
BH
2032 /* Call platform functions marked "on sleep" */
2033 pmac_pfunc_i2c_suspend();
2034 pmac_pfunc_base_suspend();
2035
e521dca6 2036 /* Stop preemption */
1da177e4
LT
2037 preempt_disable();
2038
2039 /* Make sure the decrementer won't interrupt us */
2040 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2041 /* Make sure any pending DEC interrupt occurring while we did
2042 * the above didn't re-enable the DEC */
2043 mb();
2044 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2045
2046 /* We can now disable MSR_EE. This code of course works properly only
2047 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2048 * stop the "other" CPUs way before we do all that stuff.
2049 */
2050 local_irq_disable();
2051
2052 /* Broadcast power down irq
2053 * This isn't that useful in most cases (only directly wired devices can
2054 * use this but still... This will take care of sysdev's as well, so
2055 * we exit from here with local irqs disabled and PIC off.
2056 */
bf2049f9 2057 ret = device_power_down(PMSG_SUSPEND);
1da177e4
LT
2058 if (ret) {
2059 wakeup_decrementer();
2060 local_irq_enable();
2061 preempt_enable();
2062 device_resume();
2063 broadcast_wake();
2064 printk(KERN_ERR "Driver powerdown failed\n");
2065 return -EBUSY;
2066 }
2067
5474c120
MH
2068 /* Wait for completion of async requests */
2069 while (!batt_req.complete)
1da177e4
LT
2070 pmu_poll();
2071
2072 /* Giveup the lazy FPU & vec so we don't have to back them
2073 * up from the low level code
2074 */
2075 enable_kernel_fp();
2076
2077#ifdef CONFIG_ALTIVEC
2078 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2079 enable_kernel_altivec();
2080#endif /* CONFIG_ALTIVEC */
2081
2082 return 0;
2083}
2084
aacaf9bd 2085static int
1da177e4
LT
2086pmac_wakeup_devices(void)
2087{
2088 mdelay(100);
2089
d565dd3b
BH
2090#ifdef CONFIG_PMAC_BACKLIGHT
2091 /* Tell backlight code it can use the chip again */
2092 pmu_backlight_set_sleep(0);
2093#endif
2094
1da177e4
LT
2095 /* Power back up system devices (including the PIC) */
2096 device_power_up();
2097
2098 /* Force a poll of ADB interrupts */
2099 adb_int_pending = 1;
7d12e780 2100 via_pmu_interrupt(0, NULL);
1da177e4
LT
2101
2102 /* Restart jiffies & scheduling */
2103 wakeup_decrementer();
2104
2105 /* Re-enable local CPU interrupts */
2106 local_irq_enable();
b16eeb47 2107 mdelay(10);
1da177e4
LT
2108 preempt_enable();
2109
5b9ca526
BH
2110 /* Call platform functions marked "on wake" */
2111 pmac_pfunc_base_resume();
2112 pmac_pfunc_i2c_resume();
2113
1da177e4
LT
2114 /* Resume devices */
2115 device_resume();
2116
2117 /* Notify old style drivers */
2118 broadcast_wake();
2119
2120 pm_restore_console();
2121
2122 return 0;
2123}
2124
2125#define GRACKLE_PM (1<<7)
2126#define GRACKLE_DOZE (1<<5)
2127#define GRACKLE_NAP (1<<4)
2128#define GRACKLE_SLEEP (1<<3)
2129
3bea6313 2130static int powerbook_sleep_grackle(void)
1da177e4
LT
2131{
2132 unsigned long save_l2cr;
2133 unsigned short pmcr1;
2134 struct adb_request req;
2135 int ret;
2136 struct pci_dev *grackle;
2137
2138 grackle = pci_find_slot(0, 0);
2139 if (!grackle)
2140 return -ENODEV;
2141
2142 ret = pmac_suspend_devices();
2143 if (ret) {
2144 printk(KERN_ERR "Sleep rejected by devices\n");
2145 return ret;
2146 }
2147
2148 /* Turn off various things. Darwin does some retry tests here... */
2149 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2150 pmu_wait_complete(&req);
2151 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2152 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2153 pmu_wait_complete(&req);
2154
2155 /* For 750, save backside cache setting and disable it */
2156 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2157
2158 if (!__fake_sleep) {
2159 /* Ask the PMU to put us to sleep */
2160 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2161 pmu_wait_complete(&req);
2162 }
2163
2164 /* The VIA is supposed not to be restored correctly*/
2165 save_via_state();
2166 /* We shut down some HW */
2167 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2168
2169 pci_read_config_word(grackle, 0x70, &pmcr1);
2170 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2171 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2172 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2173 pci_write_config_word(grackle, 0x70, pmcr1);
2174
2175 /* Call low-level ASM sleep handler */
2176 if (__fake_sleep)
2177 mdelay(5000);
2178 else
2179 low_sleep_handler();
2180
2181 /* We're awake again, stop grackle PM */
2182 pci_read_config_word(grackle, 0x70, &pmcr1);
2183 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2184 pci_write_config_word(grackle, 0x70, pmcr1);
2185
2186 /* Make sure the PMU is idle */
2187 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2188 restore_via_state();
2189
2190 /* Restore L2 cache */
2191 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2192 _set_L2CR(save_l2cr);
2193
2194 /* Restore userland MMU context */
6218a761 2195 set_context(current->active_mm->context.id, current->active_mm->pgd);
1da177e4
LT
2196
2197 /* Power things up */
2198 pmu_unlock();
2199 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2200 pmu_wait_complete(&req);
2201 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2202 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2203 pmu_wait_complete(&req);
2204 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2205 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2206 pmu_wait_complete(&req);
2207
2208 pmac_wakeup_devices();
2209
2210 return 0;
2211}
2212
aacaf9bd 2213static int
1da177e4
LT
2214powerbook_sleep_Core99(void)
2215{
2216 unsigned long save_l2cr;
2217 unsigned long save_l3cr;
2218 struct adb_request req;
2219 int ret;
2220
2221 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2222 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2223 return -ENOSYS;
2224 }
2225
2226 if (num_online_cpus() > 1 || cpu_is_offline(0))
2227 return -EAGAIN;
2228
2229 ret = pmac_suspend_devices();
2230 if (ret) {
2231 printk(KERN_ERR "Sleep rejected by devices\n");
2232 return ret;
2233 }
2234
b16eeb47
BH
2235 /* Stop environment and ADB interrupts */
2236 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2237 pmu_wait_complete(&req);
1da177e4
LT
2238
2239 /* Tell PMU what events will wake us up */
2240 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2241 0xff, 0xff);
2242 pmu_wait_complete(&req);
2243 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2244 0, PMU_PWR_WAKEUP_KEY |
2245 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2246 pmu_wait_complete(&req);
2247
2248 /* Save the state of the L2 and L3 caches */
2249 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2250 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2251
2252 if (!__fake_sleep) {
2253 /* Ask the PMU to put us to sleep */
2254 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2255 pmu_wait_complete(&req);
2256 }
2257
2258 /* The VIA is supposed not to be restored correctly*/
2259 save_via_state();
2260
2261 /* Shut down various ASICs. There's a chance that we can no longer
2262 * talk to the PMU after this, so I moved it to _after_ sending the
2263 * sleep command to it. Still need to be checked.
2264 */
2265 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2266
2267 /* Call low-level ASM sleep handler */
2268 if (__fake_sleep)
2269 mdelay(5000);
2270 else
2271 low_sleep_handler();
2272
2273 /* Restore Apple core ASICs state */
2274 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2275
2276 /* Restore VIA */
2277 restore_via_state();
2278
0086b5ec
BH
2279 /* tweak LPJ before cpufreq is there */
2280 loops_per_jiffy *= 2;
2281
1da177e4
LT
2282 /* Restore video */
2283 pmac_call_early_video_resume();
2284
2285 /* Restore L2 cache */
2286 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2287 _set_L2CR(save_l2cr);
2288 /* Restore L3 cache */
2289 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2290 _set_L3CR(save_l3cr);
2291
2292 /* Restore userland MMU context */
6218a761 2293 set_context(current->active_mm->context.id, current->active_mm->pgd);
1da177e4
LT
2294
2295 /* Tell PMU we are ready */
2296 pmu_unlock();
2297 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2298 pmu_wait_complete(&req);
2299 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2300 pmu_wait_complete(&req);
2301
0086b5ec
BH
2302 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2303 loops_per_jiffy /= 2;
2304
1da177e4
LT
2305 pmac_wakeup_devices();
2306
2307 return 0;
2308}
2309
2310#define PB3400_MEM_CTRL 0xf8000000
2311#define PB3400_MEM_CTRL_SLEEP 0x70
2312
aacaf9bd 2313static int
1da177e4
LT
2314powerbook_sleep_3400(void)
2315{
2316 int ret, i, x;
2317 unsigned int hid0;
2318 unsigned long p;
2319 struct adb_request sleep_req;
2320 void __iomem *mem_ctrl;
2321 unsigned int __iomem *mem_ctrl_sleep;
2322
2323 /* first map in the memory controller registers */
2324 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2325 if (mem_ctrl == NULL) {
2326 printk("powerbook_sleep_3400: ioremap failed\n");
2327 return -ENOMEM;
2328 }
2329 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2330
2331 /* Allocate room for PCI save */
2332 pbook_alloc_pci_save();
2333
2334 ret = pmac_suspend_devices();
2335 if (ret) {
2336 pbook_free_pci_save();
2337 printk(KERN_ERR "Sleep rejected by devices\n");
2338 return ret;
2339 }
2340
2341 /* Save the state of PCI config space for some slots */
2342 pbook_pci_save();
2343
2344 /* Set the memory controller to keep the memory refreshed
2345 while we're asleep */
2346 for (i = 0x403f; i >= 0x4000; --i) {
2347 out_be32(mem_ctrl_sleep, i);
2348 do {
2349 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2350 } while (x == 0);
2351 if (x >= 0x100)
2352 break;
2353 }
2354
2355 /* Ask the PMU to put us to sleep */
2356 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2357 while (!sleep_req.complete)
2358 mb();
2359
2360 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2361
2362 /* displacement-flush the L2 cache - necessary? */
2363 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2364 i = *(volatile int *)p;
2365 asleep = 1;
2366
2367 /* Put the CPU into sleep mode */
21fe3301 2368 hid0 = mfspr(SPRN_HID0);
1da177e4 2369 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
21fe3301
BH
2370 mtspr(SPRN_HID0, hid0);
2371 mtmsr(mfmsr() | MSR_POW | MSR_EE);
1da177e4
LT
2372 udelay(10);
2373
2374 /* OK, we're awake again, start restoring things */
2375 out_be32(mem_ctrl_sleep, 0x3f);
2376 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2377 pbook_pci_restore();
2378 pmu_unlock();
2379
2380 /* wait for the PMU interrupt sequence to complete */
2381 while (asleep)
2382 mb();
2383
2384 pmac_wakeup_devices();
2385 pbook_free_pci_save();
2386 iounmap(mem_ctrl);
2387
2388 return 0;
2389}
2390
a0005034 2391#endif /* CONFIG_PM && CONFIG_PPC32 */
8c870933 2392
1da177e4
LT
2393/*
2394 * Support for /dev/pmu device
2395 */
2396#define RB_SIZE 0x10
2397struct pmu_private {
2398 struct list_head list;
2399 int rb_get;
2400 int rb_put;
2401 struct rb_entry {
2402 unsigned short len;
2403 unsigned char data[16];
2404 } rb_buf[RB_SIZE];
2405 wait_queue_head_t wait;
2406 spinlock_t lock;
2407#if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2408 int backlight_locker;
4b755999 2409#endif
1da177e4
LT
2410};
2411
2412static LIST_HEAD(all_pmu_pvt);
aacaf9bd 2413static DEFINE_SPINLOCK(all_pvt_lock);
1da177e4 2414
aacaf9bd 2415static void
1da177e4
LT
2416pmu_pass_intr(unsigned char *data, int len)
2417{
2418 struct pmu_private *pp;
2419 struct list_head *list;
2420 int i;
2421 unsigned long flags;
2422
2423 if (len > sizeof(pp->rb_buf[0].data))
2424 len = sizeof(pp->rb_buf[0].data);
2425 spin_lock_irqsave(&all_pvt_lock, flags);
2426 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2427 pp = list_entry(list, struct pmu_private, list);
2428 spin_lock(&pp->lock);
2429 i = pp->rb_put + 1;
2430 if (i >= RB_SIZE)
2431 i = 0;
2432 if (i != pp->rb_get) {
2433 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2434 rp->len = len;
2435 memcpy(rp->data, data, len);
2436 pp->rb_put = i;
2437 wake_up_interruptible(&pp->wait);
2438 }
2439 spin_unlock(&pp->lock);
2440 }
2441 spin_unlock_irqrestore(&all_pvt_lock, flags);
2442}
2443
aacaf9bd 2444static int
1da177e4
LT
2445pmu_open(struct inode *inode, struct file *file)
2446{
2447 struct pmu_private *pp;
2448 unsigned long flags;
2449
2450 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2451 if (pp == 0)
2452 return -ENOMEM;
2453 pp->rb_get = pp->rb_put = 0;
2454 spin_lock_init(&pp->lock);
2455 init_waitqueue_head(&pp->wait);
2456 spin_lock_irqsave(&all_pvt_lock, flags);
2457#if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2458 pp->backlight_locker = 0;
4b755999 2459#endif
1da177e4
LT
2460 list_add(&pp->list, &all_pmu_pvt);
2461 spin_unlock_irqrestore(&all_pvt_lock, flags);
2462 file->private_data = pp;
2463 return 0;
2464}
2465
aacaf9bd 2466static ssize_t
1da177e4
LT
2467pmu_read(struct file *file, char __user *buf,
2468 size_t count, loff_t *ppos)
2469{
2470 struct pmu_private *pp = file->private_data;
2471 DECLARE_WAITQUEUE(wait, current);
2472 unsigned long flags;
2473 int ret = 0;
2474
2475 if (count < 1 || pp == 0)
2476 return -EINVAL;
2477 if (!access_ok(VERIFY_WRITE, buf, count))
2478 return -EFAULT;
2479
2480 spin_lock_irqsave(&pp->lock, flags);
2481 add_wait_queue(&pp->wait, &wait);
2482 current->state = TASK_INTERRUPTIBLE;
2483
2484 for (;;) {
2485 ret = -EAGAIN;
2486 if (pp->rb_get != pp->rb_put) {
2487 int i = pp->rb_get;
2488 struct rb_entry *rp = &pp->rb_buf[i];
2489 ret = rp->len;
2490 spin_unlock_irqrestore(&pp->lock, flags);
2491 if (ret > count)
2492 ret = count;
2493 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2494 ret = -EFAULT;
2495 if (++i >= RB_SIZE)
2496 i = 0;
2497 spin_lock_irqsave(&pp->lock, flags);
2498 pp->rb_get = i;
2499 }
2500 if (ret >= 0)
2501 break;
2502 if (file->f_flags & O_NONBLOCK)
2503 break;
2504 ret = -ERESTARTSYS;
2505 if (signal_pending(current))
2506 break;
2507 spin_unlock_irqrestore(&pp->lock, flags);
2508 schedule();
2509 spin_lock_irqsave(&pp->lock, flags);
2510 }
2511 current->state = TASK_RUNNING;
2512 remove_wait_queue(&pp->wait, &wait);
2513 spin_unlock_irqrestore(&pp->lock, flags);
2514
2515 return ret;
2516}
2517
aacaf9bd 2518static ssize_t
1da177e4
LT
2519pmu_write(struct file *file, const char __user *buf,
2520 size_t count, loff_t *ppos)
2521{
2522 return 0;
2523}
2524
aacaf9bd 2525static unsigned int
1da177e4
LT
2526pmu_fpoll(struct file *filp, poll_table *wait)
2527{
2528 struct pmu_private *pp = filp->private_data;
2529 unsigned int mask = 0;
2530 unsigned long flags;
2531
2532 if (pp == 0)
2533 return 0;
2534 poll_wait(filp, &pp->wait, wait);
2535 spin_lock_irqsave(&pp->lock, flags);
2536 if (pp->rb_get != pp->rb_put)
2537 mask |= POLLIN;
2538 spin_unlock_irqrestore(&pp->lock, flags);
2539 return mask;
2540}
2541
aacaf9bd 2542static int
1da177e4
LT
2543pmu_release(struct inode *inode, struct file *file)
2544{
2545 struct pmu_private *pp = file->private_data;
2546 unsigned long flags;
2547
2548 lock_kernel();
2549 if (pp != 0) {
2550 file->private_data = NULL;
2551 spin_lock_irqsave(&all_pvt_lock, flags);
2552 list_del(&pp->list);
2553 spin_unlock_irqrestore(&all_pvt_lock, flags);
4b755999 2554
1da177e4 2555#if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
4b755999
MH
2556 if (pp->backlight_locker)
2557 pmac_backlight_enable();
2558#endif
2559
1da177e4
LT
2560 kfree(pp);
2561 }
2562 unlock_kernel();
2563 return 0;
2564}
2565
aacaf9bd 2566static int
1da177e4
LT
2567pmu_ioctl(struct inode * inode, struct file *filp,
2568 u_int cmd, u_long arg)
2569{
1da177e4 2570 __u32 __user *argp = (__u32 __user *)arg;
8c870933 2571 int error = -EINVAL;
1da177e4
LT
2572
2573 switch (cmd) {
a0005034 2574#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1da177e4
LT
2575 case PMU_IOC_SLEEP:
2576 if (!capable(CAP_SYS_ADMIN))
2577 return -EACCES;
2578 if (sleep_in_progress)
2579 return -EBUSY;
2580 sleep_in_progress = 1;
2581 switch (pmu_kind) {
2582 case PMU_OHARE_BASED:
2583 error = powerbook_sleep_3400();
2584 break;
2585 case PMU_HEATHROW_BASED:
2586 case PMU_PADDINGTON_BASED:
2587 error = powerbook_sleep_grackle();
2588 break;
2589 case PMU_KEYLARGO_BASED:
2590 error = powerbook_sleep_Core99();
2591 break;
2592 default:
2593 error = -ENOSYS;
2594 }
2595 sleep_in_progress = 0;
8c870933 2596 break;
1da177e4
LT
2597 case PMU_IOC_CAN_SLEEP:
2598 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2599 return put_user(0, argp);
2600 else
2601 return put_user(1, argp);
a0005034 2602#endif /* CONFIG_PM && CONFIG_PPC32 */
1da177e4 2603
5474c120
MH
2604#ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2605 /* Compatibility ioctl's for backlight */
1da177e4 2606 case PMU_IOC_GET_BACKLIGHT:
5474c120
MH
2607 {
2608 int brightness;
2609
1da177e4
LT
2610 if (sleep_in_progress)
2611 return -EBUSY;
5474c120
MH
2612
2613 brightness = pmac_backlight_get_legacy_brightness();
2614 if (brightness < 0)
2615 return brightness;
2616 else
2617 return put_user(brightness, argp);
2618
2619 }
1da177e4
LT
2620 case PMU_IOC_SET_BACKLIGHT:
2621 {
5474c120
MH
2622 int brightness;
2623
1da177e4
LT
2624 if (sleep_in_progress)
2625 return -EBUSY;
5474c120
MH
2626
2627 error = get_user(brightness, argp);
2628 if (error)
2629 return error;
2630
2631 return pmac_backlight_set_legacy_brightness(brightness);
1da177e4
LT
2632 }
2633#ifdef CONFIG_INPUT_ADBHID
2634 case PMU_IOC_GRAB_BACKLIGHT: {
8c870933 2635 struct pmu_private *pp = filp->private_data;
8c870933 2636
1da177e4
LT
2637 if (pp->backlight_locker)
2638 return 0;
4b755999 2639
1da177e4 2640 pp->backlight_locker = 1;
4b755999
MH
2641 pmac_backlight_disable();
2642
1da177e4
LT
2643 return 0;
2644 }
2645#endif /* CONFIG_INPUT_ADBHID */
5474c120 2646#endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
4b755999 2647
1da177e4
LT
2648 case PMU_IOC_GET_MODEL:
2649 return put_user(pmu_kind, argp);
2650 case PMU_IOC_HAS_ADB:
2651 return put_user(pmu_has_adb, argp);
2652 }
8c870933 2653 return error;
1da177e4
LT
2654}
2655
fa027c2a 2656static const struct file_operations pmu_device_fops = {
1da177e4
LT
2657 .read = pmu_read,
2658 .write = pmu_write,
2659 .poll = pmu_fpoll,
2660 .ioctl = pmu_ioctl,
2661 .open = pmu_open,
2662 .release = pmu_release,
2663};
2664
aacaf9bd 2665static struct miscdevice pmu_device = {
1da177e4
LT
2666 PMU_MINOR, "pmu", &pmu_device_fops
2667};
2668
8c870933 2669static int pmu_device_init(void)
1da177e4
LT
2670{
2671 if (!via)
8c870933 2672 return 0;
1da177e4
LT
2673 if (misc_register(&pmu_device) < 0)
2674 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
8c870933 2675 return 0;
1da177e4 2676}
8c870933
BH
2677device_initcall(pmu_device_init);
2678
1da177e4
LT
2679
2680#ifdef DEBUG_SLEEP
aacaf9bd 2681static inline void
1da177e4
LT
2682polled_handshake(volatile unsigned char __iomem *via)
2683{
2684 via[B] &= ~TREQ; eieio();
2685 while ((via[B] & TACK) != 0)
2686 ;
2687 via[B] |= TREQ; eieio();
2688 while ((via[B] & TACK) == 0)
2689 ;
2690}
2691
aacaf9bd 2692static inline void
1da177e4
LT
2693polled_send_byte(volatile unsigned char __iomem *via, int x)
2694{
2695 via[ACR] |= SR_OUT | SR_EXT; eieio();
2696 via[SR] = x; eieio();
2697 polled_handshake(via);
2698}
2699
aacaf9bd 2700static inline int
1da177e4
LT
2701polled_recv_byte(volatile unsigned char __iomem *via)
2702{
2703 int x;
2704
2705 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2706 x = via[SR]; eieio();
2707 polled_handshake(via);
2708 x = via[SR]; eieio();
2709 return x;
2710}
2711
aacaf9bd 2712int
1da177e4
LT
2713pmu_polled_request(struct adb_request *req)
2714{
2715 unsigned long flags;
2716 int i, l, c;
2717 volatile unsigned char __iomem *v = via;
2718
2719 req->complete = 1;
2720 c = req->data[0];
2721 l = pmu_data_len[c][0];
2722 if (l >= 0 && req->nbytes != l + 1)
2723 return -EINVAL;
2724
2725 local_irq_save(flags);
2726 while (pmu_state != idle)
2727 pmu_poll();
2728
2729 while ((via[B] & TACK) == 0)
2730 ;
2731 polled_send_byte(v, c);
2732 if (l < 0) {
2733 l = req->nbytes - 1;
2734 polled_send_byte(v, l);
2735 }
2736 for (i = 1; i <= l; ++i)
2737 polled_send_byte(v, req->data[i]);
2738
2739 l = pmu_data_len[c][1];
2740 if (l < 0)
2741 l = polled_recv_byte(v);
2742 for (i = 0; i < l; ++i)
2743 req->reply[i + req->reply_len] = polled_recv_byte(v);
2744
2745 if (req->done)
2746 (*req->done)(req);
2747
2748 local_irq_restore(flags);
2749 return 0;
2750}
2751#endif /* DEBUG_SLEEP */
2752
2753
2754/* FIXME: This is a temporary set of callbacks to enable us
2755 * to do suspend-to-disk.
2756 */
2757
a0005034 2758#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1da177e4 2759
87275856 2760static int pmu_sys_suspended;
1da177e4 2761
3bfffd97 2762static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
1da177e4 2763{
ca078bae 2764 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
1da177e4
LT
2765 return 0;
2766
2767 /* Suspend PMU event interrupts */
2768 pmu_suspend();
2769
2770 pmu_sys_suspended = 1;
2771 return 0;
2772}
2773
2774static int pmu_sys_resume(struct sys_device *sysdev)
2775{
2776 struct adb_request req;
2777
2778 if (!pmu_sys_suspended)
2779 return 0;
2780
2781 /* Tell PMU we are ready */
2782 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2783 pmu_wait_complete(&req);
2784
2785 /* Resume PMU event interrupts */
2786 pmu_resume();
2787
2788 pmu_sys_suspended = 0;
2789
2790 return 0;
2791}
2792
a0005034 2793#endif /* CONFIG_PM && CONFIG_PPC32 */
1da177e4
LT
2794
2795static struct sysdev_class pmu_sysclass = {
2796 set_kset_name("pmu"),
2797};
2798
2799static struct sys_device device_pmu = {
1da177e4
LT
2800 .cls = &pmu_sysclass,
2801};
2802
2803static struct sysdev_driver driver_pmu = {
a0005034 2804#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1da177e4
LT
2805 .suspend = &pmu_sys_suspend,
2806 .resume = &pmu_sys_resume,
a0005034 2807#endif /* CONFIG_PM && CONFIG_PPC32 */
1da177e4
LT
2808};
2809
2810static int __init init_pmu_sysfs(void)
2811{
2812 int rc;
2813
2814 rc = sysdev_class_register(&pmu_sysclass);
2815 if (rc) {
2816 printk(KERN_ERR "Failed registering PMU sys class\n");
2817 return -ENODEV;
2818 }
2819 rc = sysdev_register(&device_pmu);
2820 if (rc) {
2821 printk(KERN_ERR "Failed registering PMU sys device\n");
2822 return -ENODEV;
2823 }
2824 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2825 if (rc) {
2826 printk(KERN_ERR "Failed registering PMU sys driver\n");
2827 return -ENODEV;
2828 }
2829 return 0;
2830}
2831
2832subsys_initcall(init_pmu_sysfs);
2833
2834EXPORT_SYMBOL(pmu_request);
730745a5 2835EXPORT_SYMBOL(pmu_queue_request);
1da177e4
LT
2836EXPORT_SYMBOL(pmu_poll);
2837EXPORT_SYMBOL(pmu_poll_adb);
2838EXPORT_SYMBOL(pmu_wait_complete);
2839EXPORT_SYMBOL(pmu_suspend);
2840EXPORT_SYMBOL(pmu_resume);
2841EXPORT_SYMBOL(pmu_unlock);
a0005034 2842#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1da177e4
LT
2843EXPORT_SYMBOL(pmu_enable_irled);
2844EXPORT_SYMBOL(pmu_battery_count);
2845EXPORT_SYMBOL(pmu_batteries);
2846EXPORT_SYMBOL(pmu_power_flags);
a0005034 2847#endif /* CONFIG_PM && CONFIG_PPC32 */
1da177e4 2848