blob: dfdf11c1eec45f127cf2849a789862b85ece1797 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Device driver for the PMU on 68K-based Apple PowerBooks
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 PowerBooks.
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 * Adapted for 68K PMU by Joshua M. Thompson
12 *
13 * Based largely on the PowerMac PMU code by Paul Mackerras and
14 * Fabio Riccardi.
15 *
16 * Also based on the PMU driver from MkLinux by Apple Computer, Inc.
17 * and the Open Software Foundation, Inc.
18 */
19
20#include <stdarg.h>
21#include <linux/types.h>
22#include <linux/errno.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/miscdevice.h>
26#include <linux/blkdev.h>
27#include <linux/pci.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31
32#include <linux/adb.h>
33#include <linux/pmu.h>
34#include <linux/cuda.h>
35
36#include <asm/macintosh.h>
37#include <asm/macints.h>
38#include <asm/machw.h>
39#include <asm/mac_via.h>
40
41#include <asm/pgtable.h>
42#include <asm/system.h>
43#include <asm/irq.h>
44#include <asm/uaccess.h>
45
46/* Misc minor number allocated for /dev/pmu */
47#define PMU_MINOR 154
48
49/* VIA registers - spaced 0x200 bytes apart */
50#define RS 0x200 /* skip between registers */
51#define B 0 /* B-side data */
52#define A RS /* A-side data */
53#define DIRB (2*RS) /* B-side direction (1=output) */
54#define DIRA (3*RS) /* A-side direction (1=output) */
55#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
56#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
57#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
58#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
59#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
60#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
61#define SR (10*RS) /* Shift register */
62#define ACR (11*RS) /* Auxiliary control register */
63#define PCR (12*RS) /* Peripheral control register */
64#define IFR (13*RS) /* Interrupt flag register */
65#define IER (14*RS) /* Interrupt enable register */
66#define ANH (15*RS) /* A-side data, no handshake */
67
68/* Bits in B data register: both active low */
69#define TACK 0x02 /* Transfer acknowledge (input) */
70#define TREQ 0x04 /* Transfer request (output) */
71
72/* Bits in ACR */
73#define SR_CTRL 0x1c /* Shift register control bits */
74#define SR_EXT 0x0c /* Shift on external clock */
75#define SR_OUT 0x10 /* Shift out if 1 */
76
77/* Bits in IFR and IER */
78#define SR_INT 0x04 /* Shift register full/empty */
79#define CB1_INT 0x10 /* transition on CB1 input */
80
81static enum pmu_state {
82 idle,
83 sending,
84 intack,
85 reading,
86 reading_intr,
87} pmu_state;
88
89static struct adb_request *current_req;
90static struct adb_request *last_req;
91static struct adb_request *req_awaiting_reply;
92static unsigned char interrupt_data[32];
93static unsigned char *reply_ptr;
94static int data_index;
95static int data_len;
96static int adb_int_pending;
97static int pmu_adb_flags;
Olaf Hering87275852007-02-10 21:35:12 +010098static int adb_dev_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static struct adb_request bright_req_1, bright_req_2, bright_req_3;
100static int pmu_kind = PMU_UNKNOWN;
Olaf Hering87275852007-02-10 21:35:12 +0100101static int pmu_fully_inited;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103int asleep;
Alan Sterne041c682006-03-27 01:16:30 -0800104BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static int pmu_probe(void);
107static int pmu_init(void);
108static void pmu_start(void);
David Howells7d12e782006-10-05 14:55:46 +0100109static irqreturn_t pmu_interrupt(int irq, void *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int pmu_send_request(struct adb_request *req, int sync);
111static int pmu_autopoll(int devs);
112void pmu_poll(void);
113static int pmu_reset_bus(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static void pmu_start(void);
116static void send_byte(int x);
117static void recv_byte(void);
118static void pmu_done(struct adb_request *req);
David Howells7d12e782006-10-05 14:55:46 +0100119static void pmu_handle_data(unsigned char *data, int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void set_volume(int level);
121static void pmu_enable_backlight(int on);
122static void pmu_set_brightness(int level);
123
124struct adb_driver via_pmu_driver = {
125 "68K PMU",
126 pmu_probe,
127 pmu_init,
128 pmu_send_request,
129 pmu_autopoll,
130 pmu_poll,
131 pmu_reset_bus
132};
133
134/*
135 * This table indicates for each PMU opcode:
136 * - the number of data bytes to be sent with the command, or -1
137 * if a length byte should be sent,
138 * - the number of response bytes which the PMU will return, or
139 * -1 if it will send a length byte.
140 */
141static s8 pmu_data_len[256][2] = {
142/* 0 1 2 3 4 5 6 7 */
143/*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
144/*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
145/*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
146/*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
147/*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
148/*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
149/*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
150/*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
151/*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
152/*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
153/*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
154/*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
155/*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
156/*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
157/*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
158/*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
159/*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
160/*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
161/*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
162/*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
163/*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
164/*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
165/*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
166/*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
167/*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
168/*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
169/*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
170/*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
171/*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
172/*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
173/*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
174/*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
175};
176
177int pmu_probe(void)
178{
179 if (macintosh_config->adb_type == MAC_ADB_PB1) {
180 pmu_kind = PMU_68K_V1;
181 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
182 pmu_kind = PMU_68K_V2;
183 } else {
184 return -ENODEV;
185 }
186
187 pmu_state = idle;
188
189 return 0;
190}
191
192static int
193pmu_init(void)
194{
195 int timeout;
196 volatile struct adb_request req;
197
198 via2[B] |= TREQ; /* negate TREQ */
199 via2[DIRB] = (via2[DIRB] | TREQ) & ~TACK; /* TACK in, TREQ out */
200
201 pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB);
202 timeout = 100000;
203 while (!req.complete) {
204 if (--timeout < 0) {
205 printk(KERN_ERR "pmu_init: no response from PMU\n");
206 return -EAGAIN;
207 }
208 udelay(10);
209 pmu_poll();
210 }
211
212 /* ack all pending interrupts */
213 timeout = 100000;
214 interrupt_data[0] = 1;
215 while (interrupt_data[0] || pmu_state != idle) {
216 if (--timeout < 0) {
217 printk(KERN_ERR "pmu_init: timed out acking intrs\n");
218 return -EAGAIN;
219 }
220 if (pmu_state == idle) {
221 adb_int_pending = 1;
Al Viro2850bc22006-10-07 14:16:45 +0100222 pmu_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 pmu_poll();
225 udelay(10);
226 }
227
228 pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK,
229 PMU_INT_ADB_AUTO|PMU_INT_SNDBRT|PMU_INT_ADB);
230 timeout = 100000;
231 while (!req.complete) {
232 if (--timeout < 0) {
233 printk(KERN_ERR "pmu_init: no response from PMU\n");
234 return -EAGAIN;
235 }
236 udelay(10);
237 pmu_poll();
238 }
239
240 bright_req_1.complete = 1;
241 bright_req_2.complete = 1;
242 bright_req_3.complete = 1;
243
244 if (request_irq(IRQ_MAC_ADB_SR, pmu_interrupt, 0, "pmu-shift",
245 pmu_interrupt)) {
246 printk(KERN_ERR "pmu_init: can't get irq %d\n",
247 IRQ_MAC_ADB_SR);
248 return -EAGAIN;
249 }
250 if (request_irq(IRQ_MAC_ADB_CL, pmu_interrupt, 0, "pmu-clock",
251 pmu_interrupt)) {
252 printk(KERN_ERR "pmu_init: can't get irq %d\n",
253 IRQ_MAC_ADB_CL);
254 free_irq(IRQ_MAC_ADB_SR, pmu_interrupt);
255 return -EAGAIN;
256 }
257
258 pmu_fully_inited = 1;
259
260 /* Enable backlight */
261 pmu_enable_backlight(1);
262
263 printk("adb: PMU 68K driver v0.5 for Unified ADB.\n");
264
265 return 0;
266}
267
268int
269pmu_get_model(void)
270{
271 return pmu_kind;
272}
273
274/* Send an ADB command */
275static int
276pmu_send_request(struct adb_request *req, int sync)
277{
278 int i, ret;
279
280 if (!pmu_fully_inited)
281 {
282 req->complete = 1;
283 return -ENXIO;
284 }
285
286 ret = -EINVAL;
287
288 switch (req->data[0]) {
289 case PMU_PACKET:
290 for (i = 0; i < req->nbytes - 1; ++i)
291 req->data[i] = req->data[i+1];
292 --req->nbytes;
293 if (pmu_data_len[req->data[0]][1] != 0) {
294 req->reply[0] = ADB_RET_OK;
295 req->reply_len = 1;
296 } else
297 req->reply_len = 0;
298 ret = pmu_queue_request(req);
299 break;
300 case CUDA_PACKET:
301 switch (req->data[1]) {
302 case CUDA_GET_TIME:
303 if (req->nbytes != 2)
304 break;
305 req->data[0] = PMU_READ_RTC;
306 req->nbytes = 1;
307 req->reply_len = 3;
308 req->reply[0] = CUDA_PACKET;
309 req->reply[1] = 0;
310 req->reply[2] = CUDA_GET_TIME;
311 ret = pmu_queue_request(req);
312 break;
313 case CUDA_SET_TIME:
314 if (req->nbytes != 6)
315 break;
316 req->data[0] = PMU_SET_RTC;
317 req->nbytes = 5;
318 for (i = 1; i <= 4; ++i)
319 req->data[i] = req->data[i+1];
320 req->reply_len = 3;
321 req->reply[0] = CUDA_PACKET;
322 req->reply[1] = 0;
323 req->reply[2] = CUDA_SET_TIME;
324 ret = pmu_queue_request(req);
325 break;
326 case CUDA_GET_PRAM:
327 if (req->nbytes != 4)
328 break;
329 req->data[0] = PMU_READ_NVRAM;
330 req->data[1] = req->data[2];
331 req->data[2] = req->data[3];
332 req->nbytes = 3;
333 req->reply_len = 3;
334 req->reply[0] = CUDA_PACKET;
335 req->reply[1] = 0;
336 req->reply[2] = CUDA_GET_PRAM;
337 ret = pmu_queue_request(req);
338 break;
339 case CUDA_SET_PRAM:
340 if (req->nbytes != 5)
341 break;
342 req->data[0] = PMU_WRITE_NVRAM;
343 req->data[1] = req->data[2];
344 req->data[2] = req->data[3];
345 req->data[3] = req->data[4];
346 req->nbytes = 4;
347 req->reply_len = 3;
348 req->reply[0] = CUDA_PACKET;
349 req->reply[1] = 0;
350 req->reply[2] = CUDA_SET_PRAM;
351 ret = pmu_queue_request(req);
352 break;
353 }
354 break;
355 case ADB_PACKET:
356 for (i = req->nbytes - 1; i > 1; --i)
357 req->data[i+2] = req->data[i];
358 req->data[3] = req->nbytes - 2;
359 req->data[2] = pmu_adb_flags;
360 /*req->data[1] = req->data[1];*/
361 req->data[0] = PMU_ADB_CMD;
362 req->nbytes += 2;
363 req->reply_expected = 1;
364 req->reply_len = 0;
365 ret = pmu_queue_request(req);
366 break;
367 }
368 if (ret)
369 {
370 req->complete = 1;
371 return ret;
372 }
373
374 if (sync) {
375 while (!req->complete)
376 pmu_poll();
377 }
378
379 return 0;
380}
381
382/* Enable/disable autopolling */
383static int
384pmu_autopoll(int devs)
385{
386 struct adb_request req;
387
388 if (!pmu_fully_inited) return -ENXIO;
389
390 if (devs) {
391 adb_dev_map = devs;
392 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
393 adb_dev_map >> 8, adb_dev_map);
394 pmu_adb_flags = 2;
395 } else {
396 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
397 pmu_adb_flags = 0;
398 }
399 while (!req.complete)
400 pmu_poll();
401 return 0;
402}
403
404/* Reset the ADB bus */
405static int
406pmu_reset_bus(void)
407{
408 struct adb_request req;
409 long timeout;
410 int save_autopoll = adb_dev_map;
411
412 if (!pmu_fully_inited) return -ENXIO;
413
414 /* anyone got a better idea?? */
415 pmu_autopoll(0);
416
417 req.nbytes = 5;
418 req.done = NULL;
419 req.data[0] = PMU_ADB_CMD;
420 req.data[1] = 0;
421 req.data[2] = 3; /* ADB_BUSRESET ??? */
422 req.data[3] = 0;
423 req.data[4] = 0;
424 req.reply_len = 0;
425 req.reply_expected = 1;
426 if (pmu_queue_request(&req) != 0)
427 {
428 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
429 return -EIO;
430 }
431 while (!req.complete)
432 pmu_poll();
433 timeout = 100000;
434 while (!req.complete) {
435 if (--timeout < 0) {
436 printk(KERN_ERR "pmu_adb_reset_bus (reset): no response from PMU\n");
437 return -EIO;
438 }
439 udelay(10);
440 pmu_poll();
441 }
442
443 if (save_autopoll != 0)
444 pmu_autopoll(save_autopoll);
445
446 return 0;
447}
448
449/* Construct and send a pmu request */
450int
451pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
452 int nbytes, ...)
453{
454 va_list list;
455 int i;
456
457 if (nbytes < 0 || nbytes > 32) {
458 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
459 req->complete = 1;
460 return -EINVAL;
461 }
462 req->nbytes = nbytes;
463 req->done = done;
464 va_start(list, nbytes);
465 for (i = 0; i < nbytes; ++i)
466 req->data[i] = va_arg(list, int);
467 va_end(list);
468 if (pmu_data_len[req->data[0]][1] != 0) {
469 req->reply[0] = ADB_RET_OK;
470 req->reply_len = 1;
471 } else
472 req->reply_len = 0;
473 req->reply_expected = 0;
474 return pmu_queue_request(req);
475}
476
Finn Thainbff832c2007-05-01 22:32:49 +0200477int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478pmu_queue_request(struct adb_request *req)
479{
480 unsigned long flags;
481 int nsend;
482
483 if (req->nbytes <= 0) {
484 req->complete = 1;
485 return 0;
486 }
487 nsend = pmu_data_len[req->data[0]][0];
488 if (nsend >= 0 && req->nbytes != nsend + 1) {
489 req->complete = 1;
490 return -EINVAL;
491 }
492
Al Viroa5d361f2006-01-12 01:06:34 -0800493 req->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 req->sent = 0;
495 req->complete = 0;
496 local_irq_save(flags);
497
498 if (current_req != 0) {
499 last_req->next = req;
500 last_req = req;
501 } else {
502 current_req = req;
503 last_req = req;
504 if (pmu_state == idle)
505 pmu_start();
506 }
507
508 local_irq_restore(flags);
509 return 0;
510}
511
512static void
513send_byte(int x)
514{
515 via1[ACR] |= SR_CTRL;
516 via1[SR] = x;
517 via2[B] &= ~TREQ; /* assert TREQ */
518}
519
520static void
521recv_byte(void)
522{
523 char c;
524
525 via1[ACR] = (via1[ACR] | SR_EXT) & ~SR_OUT;
526 c = via1[SR]; /* resets SR */
527 via2[B] &= ~TREQ;
528}
529
530static void
531pmu_start(void)
532{
533 unsigned long flags;
534 struct adb_request *req;
535
536 /* assert pmu_state == idle */
537 /* get the packet to send */
538 local_irq_save(flags);
539 req = current_req;
540 if (req == 0 || pmu_state != idle
541 || (req->reply_expected && req_awaiting_reply))
542 goto out;
543
544 pmu_state = sending;
545 data_index = 1;
546 data_len = pmu_data_len[req->data[0]][0];
547
548 /* set the shift register to shift out and send a byte */
549 send_byte(req->data[0]);
550
551out:
552 local_irq_restore(flags);
553}
554
555void
556pmu_poll(void)
557{
558 unsigned long flags;
559
560 local_irq_save(flags);
561 if (via1[IFR] & SR_INT) {
562 via1[IFR] = SR_INT;
Al Viro2850bc22006-10-07 14:16:45 +0100563 pmu_interrupt(IRQ_MAC_ADB_SR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 if (via1[IFR] & CB1_INT) {
566 via1[IFR] = CB1_INT;
Al Viro2850bc22006-10-07 14:16:45 +0100567 pmu_interrupt(IRQ_MAC_ADB_CL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 local_irq_restore(flags);
570}
571
572static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100573pmu_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct adb_request *req;
576 int timeout, bite = 0; /* to prevent compiler warning */
577
578#if 0
579 printk("pmu_interrupt: irq %d state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
580 irq, pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
581#endif
582
583 if (irq == IRQ_MAC_ADB_CL) { /* CB1 interrupt */
584 adb_int_pending = 1;
585 } else if (irq == IRQ_MAC_ADB_SR) { /* SR interrupt */
586 if (via2[B] & TACK) {
587 printk(KERN_DEBUG "PMU: SR_INT but ack still high! (%x)\n", via2[B]);
588 }
589
590 /* if reading grab the byte */
591 if ((via1[ACR] & SR_OUT) == 0) bite = via1[SR];
592
593 /* reset TREQ and wait for TACK to go high */
594 via2[B] |= TREQ;
595 timeout = 3200;
596 while (!(via2[B] & TACK)) {
597 if (--timeout < 0) {
598 printk(KERN_ERR "PMU not responding (!ack)\n");
599 goto finish;
600 }
601 udelay(10);
602 }
603
604 switch (pmu_state) {
605 case sending:
606 req = current_req;
607 if (data_len < 0) {
608 data_len = req->nbytes - 1;
609 send_byte(data_len);
610 break;
611 }
612 if (data_index <= data_len) {
613 send_byte(req->data[data_index++]);
614 break;
615 }
616 req->sent = 1;
617 data_len = pmu_data_len[req->data[0]][1];
618 if (data_len == 0) {
619 pmu_state = idle;
620 current_req = req->next;
621 if (req->reply_expected)
622 req_awaiting_reply = req;
623 else
624 pmu_done(req);
625 } else {
626 pmu_state = reading;
627 data_index = 0;
628 reply_ptr = req->reply + req->reply_len;
629 recv_byte();
630 }
631 break;
632
633 case intack:
634 data_index = 0;
635 data_len = -1;
636 pmu_state = reading_intr;
637 reply_ptr = interrupt_data;
638 recv_byte();
639 break;
640
641 case reading:
642 case reading_intr:
643 if (data_len == -1) {
644 data_len = bite;
645 if (bite > 32)
646 printk(KERN_ERR "PMU: bad reply len %d\n",
647 bite);
648 } else {
649 reply_ptr[data_index++] = bite;
650 }
651 if (data_index < data_len) {
652 recv_byte();
653 break;
654 }
655
656 if (pmu_state == reading_intr) {
David Howells7d12e782006-10-05 14:55:46 +0100657 pmu_handle_data(interrupt_data, data_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 } else {
659 req = current_req;
660 current_req = req->next;
661 req->reply_len += data_index;
662 pmu_done(req);
663 }
664 pmu_state = idle;
665
666 break;
667
668 default:
669 printk(KERN_ERR "pmu_interrupt: unknown state %d?\n",
670 pmu_state);
671 }
672 }
673finish:
674 if (pmu_state == idle) {
675 if (adb_int_pending) {
676 pmu_state = intack;
677 send_byte(PMU_INT_ACK);
678 adb_int_pending = 0;
679 } else if (current_req) {
680 pmu_start();
681 }
682 }
683
684#if 0
685 printk("pmu_interrupt: exit state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
686 pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
687#endif
688 return IRQ_HANDLED;
689}
690
691static void
692pmu_done(struct adb_request *req)
693{
694 req->complete = 1;
695 if (req->done)
696 (*req->done)(req);
697}
698
699/* Interrupt data could be the result data from an ADB cmd */
700static void
David Howells7d12e782006-10-05 14:55:46 +0100701pmu_handle_data(unsigned char *data, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 static int show_pmu_ints = 1;
704
705 asleep = 0;
706 if (len < 1) {
707 adb_int_pending = 0;
708 return;
709 }
710 if (data[0] & PMU_INT_ADB) {
711 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
712 struct adb_request *req = req_awaiting_reply;
713 if (req == 0) {
714 printk(KERN_ERR "PMU: extra ADB reply\n");
715 return;
716 }
Al Viroa5d361f2006-01-12 01:06:34 -0800717 req_awaiting_reply = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (len <= 2)
719 req->reply_len = 0;
720 else {
721 memcpy(req->reply, data + 1, len - 1);
722 req->reply_len = len - 1;
723 }
724 pmu_done(req);
725 } else {
David Howells7d12e782006-10-05 14:55:46 +0100726 adb_input(data+1, len-1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728 } else {
729 if (data[0] == 0x08 && len == 3) {
730 /* sound/brightness buttons pressed */
731 pmu_set_brightness(data[1] >> 3);
732 set_volume(data[2]);
733 } else if (show_pmu_ints
734 && !(data[0] == PMU_INT_TICK && len == 1)) {
735 int i;
736 printk(KERN_DEBUG "pmu intr");
737 for (i = 0; i < len; ++i)
738 printk(" %.2x", data[i]);
739 printk("\n");
740 }
741 }
742}
743
744int backlight_level = -1;
745int backlight_enabled = 0;
746
747#define LEVEL_TO_BRIGHT(lev) ((lev) < 1? 0x7f: 0x4a - ((lev) << 1))
748
749static void
750pmu_enable_backlight(int on)
751{
752 struct adb_request req;
753
754 if (on) {
755 /* first call: get current backlight value */
756 if (backlight_level < 0) {
757 switch(pmu_kind) {
758 case PMU_68K_V1:
759 case PMU_68K_V2:
760 pmu_request(&req, NULL, 3, PMU_READ_NVRAM, 0x14, 0xe);
761 while (!req.complete)
762 pmu_poll();
763 printk(KERN_DEBUG "pmu: nvram returned bright: %d\n", (int)req.reply[1]);
764 backlight_level = req.reply[1];
765 break;
766 default:
767 backlight_enabled = 0;
768 return;
769 }
770 }
771 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
772 LEVEL_TO_BRIGHT(backlight_level));
773 while (!req.complete)
774 pmu_poll();
775 }
776 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
777 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
778 while (!req.complete)
779 pmu_poll();
780 backlight_enabled = on;
781}
782
783static void
784pmu_set_brightness(int level)
785{
786 int bright;
787
788 backlight_level = level;
789 bright = LEVEL_TO_BRIGHT(level);
790 if (!backlight_enabled)
791 return;
792 if (bright_req_1.complete)
793 pmu_request(&bright_req_1, NULL, 2, PMU_BACKLIGHT_BRIGHT,
794 bright);
795 if (bright_req_2.complete)
796 pmu_request(&bright_req_2, NULL, 2, PMU_POWER_CTRL,
797 PMU_POW_BACKLIGHT | (bright < 0x7f ? PMU_POW_ON : PMU_POW_OFF));
798}
799
800void
801pmu_enable_irled(int on)
802{
803 struct adb_request req;
804
805 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
806 (on ? PMU_POW_ON : PMU_POW_OFF));
807 while (!req.complete)
808 pmu_poll();
809}
810
811static void
812set_volume(int level)
813{
814}
815
816int
817pmu_present(void)
818{
819 return (pmu_kind != PMU_UNKNOWN);
820}
821
822#if 0 /* needs some work for 68K */
823
824/*
825 * This struct is used to store config register values for
826 * PCI devices which may get powered off when we sleep.
827 */
828static struct pci_save {
829 u16 command;
830 u16 cache_lat;
831 u16 intr;
832} *pbook_pci_saves;
833static int n_pbook_pci_saves;
834
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500835static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836pbook_pci_save(void)
837{
838 int npci;
839 struct pci_dev *pd = NULL;
840 struct pci_save *ps;
841
842 npci = 0;
Alan Coxedceeaf2006-09-30 23:27:32 -0700843 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ++npci;
845 n_pbook_pci_saves = npci;
846 if (npci == 0)
847 return;
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800848 ps = kmalloc(npci * sizeof(*ps), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 pbook_pci_saves = ps;
850 if (ps == NULL)
851 return;
852
853 pd = NULL;
Alan Coxedceeaf2006-09-30 23:27:32 -0700854 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
856 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
857 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
858 ++ps;
859 --npci;
860 }
861}
862
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500863static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864pbook_pci_restore(void)
865{
866 u16 cmd;
867 struct pci_save *ps = pbook_pci_saves;
868 struct pci_dev *pd = NULL;
869 int j;
870
Alan Coxedceeaf2006-09-30 23:27:32 -0700871 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (ps->command == 0)
873 continue;
874 pci_read_config_word(pd, PCI_COMMAND, &cmd);
875 if ((ps->command & ~cmd) == 0)
876 continue;
877 switch (pd->hdr_type) {
878 case PCI_HEADER_TYPE_NORMAL:
879 for (j = 0; j < 6; ++j)
880 pci_write_config_dword(pd,
881 PCI_BASE_ADDRESS_0 + j*4,
882 pd->resource[j].start);
883 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
884 pd->resource[PCI_ROM_RESOURCE].start);
885 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
886 ps->cache_lat);
887 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
888 ps->intr);
889 pci_write_config_word(pd, PCI_COMMAND, ps->command);
890 break;
891 /* other header types not restored at present */
892 }
893 }
894}
895
896/*
897 * Put the powerbook to sleep.
898 */
899#define IRQ_ENABLE ((unsigned int *)0xf3000024)
900#define MEM_CTRL ((unsigned int *)0xf8000070)
901
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500902int powerbook_sleep(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 int ret, i, x;
905 static int save_backlight;
906 static unsigned int save_irqen;
907 unsigned long msr;
908 unsigned int hid0;
909 unsigned long p, wait;
910 struct adb_request sleep_req;
911
912 /* Notify device drivers */
Alan Sterne041c682006-03-27 01:16:30 -0800913 ret = blocking_notifier_call_chain(&sleep_notifier_list,
914 PBOOK_SLEEP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (ret & NOTIFY_STOP_MASK)
916 return -EBUSY;
917
918 /* Sync the disks. */
919 /* XXX It would be nice to have some way to ensure that
920 * nobody is dirtying any new buffers while we wait. */
921 sys_sync();
922
923 /* Turn off the display backlight */
924 save_backlight = backlight_enabled;
925 if (save_backlight)
926 pmu_enable_backlight(0);
927
928 /* Give the disks a little time to actually finish writing */
929 for (wait = jiffies + (HZ/4); time_before(jiffies, wait); )
930 mb();
931
932 /* Disable all interrupts except pmu */
933 save_irqen = in_le32(IRQ_ENABLE);
934 for (i = 0; i < 32; ++i)
935 if (i != vias->intrs[0].line && (save_irqen & (1 << i)))
936 disable_irq(i);
937 asm volatile("mtdec %0" : : "r" (0x7fffffff));
938
939 /* Save the state of PCI config space for some slots */
940 pbook_pci_save();
941
942 /* Set the memory controller to keep the memory refreshed
943 while we're asleep */
944 for (i = 0x403f; i >= 0x4000; --i) {
945 out_be32(MEM_CTRL, i);
946 do {
947 x = (in_be32(MEM_CTRL) >> 16) & 0x3ff;
948 } while (x == 0);
949 if (x >= 0x100)
950 break;
951 }
952
953 /* Ask the PMU to put us to sleep */
954 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
955 while (!sleep_req.complete)
956 mb();
957 /* displacement-flush the L2 cache - necessary? */
958 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
959 i = *(volatile int *)p;
960 asleep = 1;
961
962 /* Put the CPU into sleep mode */
963 asm volatile("mfspr %0,1008" : "=r" (hid0) :);
964 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
965 asm volatile("mtspr 1008,%0" : : "r" (hid0));
966 local_save_flags(msr);
967 msr |= MSR_POW | MSR_EE;
968 local_irq_restore(msr);
969 udelay(10);
970
971 /* OK, we're awake again, start restoring things */
972 out_be32(MEM_CTRL, 0x3f);
973 pbook_pci_restore();
974
975 /* wait for the PMU interrupt sequence to complete */
976 while (asleep)
977 mb();
978
979 /* reenable interrupts */
980 for (i = 0; i < 32; ++i)
981 if (i != vias->intrs[0].line && (save_irqen & (1 << i)))
982 enable_irq(i);
983
984 /* Notify drivers */
Alan Sterne041c682006-03-27 01:16:30 -0800985 blocking_notifier_call_chain(&sleep_notifier_list, PBOOK_WAKE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* reenable ADB autopoll */
988 pmu_adb_autopoll(adb_dev_map);
989
990 /* Turn on the screen backlight, if it was on before */
991 if (save_backlight)
992 pmu_enable_backlight(1);
993
994 /* Wait for the hard disk to spin up */
995
996 return 0;
997}
998
999/*
1000 * Support for /dev/pmu device
1001 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001002static int pmu_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 return 0;
1005}
1006
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001007static ssize_t pmu_read(struct file *file, char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 size_t count, loff_t *ppos)
1009{
1010 return 0;
1011}
1012
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001013static ssize_t pmu_write(struct file *file, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 size_t count, loff_t *ppos)
1015{
1016 return 0;
1017}
1018
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001019static int pmu_ioctl(struct inode * inode, struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 u_int cmd, u_long arg)
1021{
1022 int error;
1023 __u32 value;
1024
1025 switch (cmd) {
1026 case PMU_IOC_SLEEP:
1027 return -ENOSYS;
1028 case PMU_IOC_GET_BACKLIGHT:
1029 return put_user(backlight_level, (__u32 *)arg);
1030 case PMU_IOC_SET_BACKLIGHT:
1031 error = get_user(value, (__u32 *)arg);
1032 if (!error)
1033 pmu_set_brightness(value);
1034 return error;
1035 case PMU_IOC_GET_MODEL:
1036 return put_user(pmu_kind, (__u32 *)arg);
1037 }
1038 return -EINVAL;
1039}
1040
Arjan van de Venfa027c22007-02-12 00:55:33 -08001041static const struct file_operations pmu_device_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 .read = pmu_read,
1043 .write = pmu_write,
1044 .ioctl = pmu_ioctl,
1045 .open = pmu_open,
1046};
1047
1048static struct miscdevice pmu_device = {
1049 PMU_MINOR, "pmu", &pmu_device_fops
1050};
1051
1052void pmu_device_init(void)
1053{
1054 if (!via)
1055 return;
1056 if (misc_register(&pmu_device) < 0)
1057 printk(KERN_ERR "via-pmu68k: cannot register misc device.\n");
1058}
1059#endif /* CONFIG_PMAC_PBOOK */
1060