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