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