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