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