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