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