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