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