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