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