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