via-pmu68k.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  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. #include <stdarg.h>
  20. #include <linux/types.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/pci.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/adb.h>
  31. #include <linux/pmu.h>
  32. #include <linux/cuda.h>
  33. #include <asm/macintosh.h>
  34. #include <asm/macints.h>
  35. #include <asm/mac_via.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/system.h>
  38. #include <asm/irq.h>
  39. #include <asm/uaccess.h>
  40. /* Misc minor number allocated for /dev/pmu */
  41. #define PMU_MINOR 154
  42. /* VIA registers - spaced 0x200 bytes apart */
  43. #define RS 0x200 /* skip between registers */
  44. #define B 0 /* B-side data */
  45. #define A RS /* A-side data */
  46. #define DIRB (2*RS) /* B-side direction (1=output) */
  47. #define DIRA (3*RS) /* A-side direction (1=output) */
  48. #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
  49. #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
  50. #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
  51. #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
  52. #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
  53. #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
  54. #define SR (10*RS) /* Shift register */
  55. #define ACR (11*RS) /* Auxiliary control register */
  56. #define PCR (12*RS) /* Peripheral control register */
  57. #define IFR (13*RS) /* Interrupt flag register */
  58. #define IER (14*RS) /* Interrupt enable register */
  59. #define ANH (15*RS) /* A-side data, no handshake */
  60. /* Bits in B data register: both active low */
  61. #define TACK 0x02 /* Transfer acknowledge (input) */
  62. #define TREQ 0x04 /* Transfer request (output) */
  63. /* Bits in ACR */
  64. #define SR_CTRL 0x1c /* Shift register control bits */
  65. #define SR_EXT 0x0c /* Shift on external clock */
  66. #define SR_OUT 0x10 /* Shift out if 1 */
  67. /* Bits in IFR and IER */
  68. #define SR_INT 0x04 /* Shift register full/empty */
  69. #define CB1_INT 0x10 /* transition on CB1 input */
  70. static enum pmu_state {
  71. idle,
  72. sending,
  73. intack,
  74. reading,
  75. reading_intr,
  76. } pmu_state;
  77. static struct adb_request *current_req;
  78. static struct adb_request *last_req;
  79. static struct adb_request *req_awaiting_reply;
  80. static unsigned char interrupt_data[32];
  81. static unsigned char *reply_ptr;
  82. static int data_index;
  83. static int data_len;
  84. static int adb_int_pending;
  85. static int pmu_adb_flags;
  86. static int adb_dev_map;
  87. static struct adb_request bright_req_1, bright_req_2, bright_req_3;
  88. static int pmu_kind = PMU_UNKNOWN;
  89. static int pmu_fully_inited;
  90. int asleep;
  91. static int pmu_probe(void);
  92. static int pmu_init(void);
  93. static void pmu_start(void);
  94. static irqreturn_t pmu_interrupt(int irq, void *arg);
  95. static int pmu_send_request(struct adb_request *req, int sync);
  96. static int pmu_autopoll(int devs);
  97. void pmu_poll(void);
  98. static int pmu_reset_bus(void);
  99. static void pmu_start(void);
  100. static void send_byte(int x);
  101. static void recv_byte(void);
  102. static void pmu_done(struct adb_request *req);
  103. static void pmu_handle_data(unsigned char *data, int len);
  104. static void set_volume(int level);
  105. static void pmu_enable_backlight(int on);
  106. static void pmu_set_brightness(int level);
  107. struct adb_driver via_pmu_driver = {
  108. "68K PMU",
  109. pmu_probe,
  110. pmu_init,
  111. pmu_send_request,
  112. pmu_autopoll,
  113. pmu_poll,
  114. pmu_reset_bus
  115. };
  116. /*
  117. * This table indicates for each PMU opcode:
  118. * - the number of data bytes to be sent with the command, or -1
  119. * if a length byte should be sent,
  120. * - the number of response bytes which the PMU will return, or
  121. * -1 if it will send a length byte.
  122. */
  123. static s8 pmu_data_len[256][2] = {
  124. /* 0 1 2 3 4 5 6 7 */
  125. /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  126. /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  127. /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  128. /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
  129. /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
  130. /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
  131. /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  132. /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
  133. /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  134. /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
  135. /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
  136. /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
  137. /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  138. /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
  139. /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  140. /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
  141. /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  142. /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  143. /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  144. /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  145. /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
  146. /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  147. /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  148. /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  149. /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  150. /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  151. /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  152. /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
  153. /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
  154. /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
  155. /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  156. /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  157. };
  158. int pmu_probe(void)
  159. {
  160. if (macintosh_config->adb_type == MAC_ADB_PB1) {
  161. pmu_kind = PMU_68K_V1;
  162. } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
  163. pmu_kind = PMU_68K_V2;
  164. } else {
  165. return -ENODEV;
  166. }
  167. pmu_state = idle;
  168. return 0;
  169. }
  170. static int
  171. pmu_init(void)
  172. {
  173. int timeout;
  174. volatile struct adb_request req;
  175. via2[B] |= TREQ; /* negate TREQ */
  176. via2[DIRB] = (via2[DIRB] | TREQ) & ~TACK; /* TACK in, TREQ out */
  177. pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB);
  178. timeout = 100000;
  179. while (!req.complete) {
  180. if (--timeout < 0) {
  181. printk(KERN_ERR "pmu_init: no response from PMU\n");
  182. return -EAGAIN;
  183. }
  184. udelay(10);
  185. pmu_poll();
  186. }
  187. /* ack all pending interrupts */
  188. timeout = 100000;
  189. interrupt_data[0] = 1;
  190. while (interrupt_data[0] || pmu_state != idle) {
  191. if (--timeout < 0) {
  192. printk(KERN_ERR "pmu_init: timed out acking intrs\n");
  193. return -EAGAIN;
  194. }
  195. if (pmu_state == idle) {
  196. adb_int_pending = 1;
  197. pmu_interrupt(0, NULL);
  198. }
  199. pmu_poll();
  200. udelay(10);
  201. }
  202. pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK,
  203. PMU_INT_ADB_AUTO|PMU_INT_SNDBRT|PMU_INT_ADB);
  204. timeout = 100000;
  205. while (!req.complete) {
  206. if (--timeout < 0) {
  207. printk(KERN_ERR "pmu_init: no response from PMU\n");
  208. return -EAGAIN;
  209. }
  210. udelay(10);
  211. pmu_poll();
  212. }
  213. bright_req_1.complete = 1;
  214. bright_req_2.complete = 1;
  215. bright_req_3.complete = 1;
  216. if (request_irq(IRQ_MAC_ADB_SR, pmu_interrupt, 0, "pmu-shift",
  217. pmu_interrupt)) {
  218. printk(KERN_ERR "pmu_init: can't get irq %d\n",
  219. IRQ_MAC_ADB_SR);
  220. return -EAGAIN;
  221. }
  222. if (request_irq(IRQ_MAC_ADB_CL, pmu_interrupt, 0, "pmu-clock",
  223. pmu_interrupt)) {
  224. printk(KERN_ERR "pmu_init: can't get irq %d\n",
  225. IRQ_MAC_ADB_CL);
  226. free_irq(IRQ_MAC_ADB_SR, pmu_interrupt);
  227. return -EAGAIN;
  228. }
  229. pmu_fully_inited = 1;
  230. /* Enable backlight */
  231. pmu_enable_backlight(1);
  232. printk("adb: PMU 68K driver v0.5 for Unified ADB.\n");
  233. return 0;
  234. }
  235. int
  236. pmu_get_model(void)
  237. {
  238. return pmu_kind;
  239. }
  240. /* Send an ADB command */
  241. static int
  242. pmu_send_request(struct adb_request *req, int sync)
  243. {
  244. int i, ret;
  245. if (!pmu_fully_inited)
  246. {
  247. req->complete = 1;
  248. return -ENXIO;
  249. }
  250. ret = -EINVAL;
  251. switch (req->data[0]) {
  252. case PMU_PACKET:
  253. for (i = 0; i < req->nbytes - 1; ++i)
  254. req->data[i] = req->data[i+1];
  255. --req->nbytes;
  256. if (pmu_data_len[req->data[0]][1] != 0) {
  257. req->reply[0] = ADB_RET_OK;
  258. req->reply_len = 1;
  259. } else
  260. req->reply_len = 0;
  261. ret = pmu_queue_request(req);
  262. break;
  263. case CUDA_PACKET:
  264. switch (req->data[1]) {
  265. case CUDA_GET_TIME:
  266. if (req->nbytes != 2)
  267. break;
  268. req->data[0] = PMU_READ_RTC;
  269. req->nbytes = 1;
  270. req->reply_len = 3;
  271. req->reply[0] = CUDA_PACKET;
  272. req->reply[1] = 0;
  273. req->reply[2] = CUDA_GET_TIME;
  274. ret = pmu_queue_request(req);
  275. break;
  276. case CUDA_SET_TIME:
  277. if (req->nbytes != 6)
  278. break;
  279. req->data[0] = PMU_SET_RTC;
  280. req->nbytes = 5;
  281. for (i = 1; i <= 4; ++i)
  282. req->data[i] = req->data[i+1];
  283. req->reply_len = 3;
  284. req->reply[0] = CUDA_PACKET;
  285. req->reply[1] = 0;
  286. req->reply[2] = CUDA_SET_TIME;
  287. ret = pmu_queue_request(req);
  288. break;
  289. case CUDA_GET_PRAM:
  290. if (req->nbytes != 4)
  291. break;
  292. req->data[0] = PMU_READ_NVRAM;
  293. req->data[1] = req->data[2];
  294. req->data[2] = req->data[3];
  295. req->nbytes = 3;
  296. req->reply_len = 3;
  297. req->reply[0] = CUDA_PACKET;
  298. req->reply[1] = 0;
  299. req->reply[2] = CUDA_GET_PRAM;
  300. ret = pmu_queue_request(req);
  301. break;
  302. case CUDA_SET_PRAM:
  303. if (req->nbytes != 5)
  304. break;
  305. req->data[0] = PMU_WRITE_NVRAM;
  306. req->data[1] = req->data[2];
  307. req->data[2] = req->data[3];
  308. req->data[3] = req->data[4];
  309. req->nbytes = 4;
  310. req->reply_len = 3;
  311. req->reply[0] = CUDA_PACKET;
  312. req->reply[1] = 0;
  313. req->reply[2] = CUDA_SET_PRAM;
  314. ret = pmu_queue_request(req);
  315. break;
  316. }
  317. break;
  318. case ADB_PACKET:
  319. for (i = req->nbytes - 1; i > 1; --i)
  320. req->data[i+2] = req->data[i];
  321. req->data[3] = req->nbytes - 2;
  322. req->data[2] = pmu_adb_flags;
  323. /*req->data[1] = req->data[1];*/
  324. req->data[0] = PMU_ADB_CMD;
  325. req->nbytes += 2;
  326. req->reply_expected = 1;
  327. req->reply_len = 0;
  328. ret = pmu_queue_request(req);
  329. break;
  330. }
  331. if (ret)
  332. {
  333. req->complete = 1;
  334. return ret;
  335. }
  336. if (sync) {
  337. while (!req->complete)
  338. pmu_poll();
  339. }
  340. return 0;
  341. }
  342. /* Enable/disable autopolling */
  343. static int
  344. pmu_autopoll(int devs)
  345. {
  346. struct adb_request req;
  347. if (!pmu_fully_inited) return -ENXIO;
  348. if (devs) {
  349. adb_dev_map = devs;
  350. pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
  351. adb_dev_map >> 8, adb_dev_map);
  352. pmu_adb_flags = 2;
  353. } else {
  354. pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
  355. pmu_adb_flags = 0;
  356. }
  357. while (!req.complete)
  358. pmu_poll();
  359. return 0;
  360. }
  361. /* Reset the ADB bus */
  362. static int
  363. pmu_reset_bus(void)
  364. {
  365. struct adb_request req;
  366. long timeout;
  367. int save_autopoll = adb_dev_map;
  368. if (!pmu_fully_inited) return -ENXIO;
  369. /* anyone got a better idea?? */
  370. pmu_autopoll(0);
  371. req.nbytes = 5;
  372. req.done = NULL;
  373. req.data[0] = PMU_ADB_CMD;
  374. req.data[1] = 0;
  375. req.data[2] = 3; /* ADB_BUSRESET ??? */
  376. req.data[3] = 0;
  377. req.data[4] = 0;
  378. req.reply_len = 0;
  379. req.reply_expected = 1;
  380. if (pmu_queue_request(&req) != 0)
  381. {
  382. printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
  383. return -EIO;
  384. }
  385. while (!req.complete)
  386. pmu_poll();
  387. timeout = 100000;
  388. while (!req.complete) {
  389. if (--timeout < 0) {
  390. printk(KERN_ERR "pmu_adb_reset_bus (reset): no response from PMU\n");
  391. return -EIO;
  392. }
  393. udelay(10);
  394. pmu_poll();
  395. }
  396. if (save_autopoll != 0)
  397. pmu_autopoll(save_autopoll);
  398. return 0;
  399. }
  400. /* Construct and send a pmu request */
  401. int
  402. pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
  403. int nbytes, ...)
  404. {
  405. va_list list;
  406. int i;
  407. if (nbytes < 0 || nbytes > 32) {
  408. printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
  409. req->complete = 1;
  410. return -EINVAL;
  411. }
  412. req->nbytes = nbytes;
  413. req->done = done;
  414. va_start(list, nbytes);
  415. for (i = 0; i < nbytes; ++i)
  416. req->data[i] = va_arg(list, int);
  417. va_end(list);
  418. if (pmu_data_len[req->data[0]][1] != 0) {
  419. req->reply[0] = ADB_RET_OK;
  420. req->reply_len = 1;
  421. } else
  422. req->reply_len = 0;
  423. req->reply_expected = 0;
  424. return pmu_queue_request(req);
  425. }
  426. int
  427. pmu_queue_request(struct adb_request *req)
  428. {
  429. unsigned long flags;
  430. int nsend;
  431. if (req->nbytes <= 0) {
  432. req->complete = 1;
  433. return 0;
  434. }
  435. nsend = pmu_data_len[req->data[0]][0];
  436. if (nsend >= 0 && req->nbytes != nsend + 1) {
  437. req->complete = 1;
  438. return -EINVAL;
  439. }
  440. req->next = NULL;
  441. req->sent = 0;
  442. req->complete = 0;
  443. local_irq_save(flags);
  444. if (current_req != 0) {
  445. last_req->next = req;
  446. last_req = req;
  447. } else {
  448. current_req = req;
  449. last_req = req;
  450. if (pmu_state == idle)
  451. pmu_start();
  452. }
  453. local_irq_restore(flags);
  454. return 0;
  455. }
  456. static void
  457. send_byte(int x)
  458. {
  459. via1[ACR] |= SR_CTRL;
  460. via1[SR] = x;
  461. via2[B] &= ~TREQ; /* assert TREQ */
  462. }
  463. static void
  464. recv_byte(void)
  465. {
  466. char c;
  467. via1[ACR] = (via1[ACR] | SR_EXT) & ~SR_OUT;
  468. c = via1[SR]; /* resets SR */
  469. via2[B] &= ~TREQ;
  470. }
  471. static void
  472. pmu_start(void)
  473. {
  474. unsigned long flags;
  475. struct adb_request *req;
  476. /* assert pmu_state == idle */
  477. /* get the packet to send */
  478. local_irq_save(flags);
  479. req = current_req;
  480. if (req == 0 || pmu_state != idle
  481. || (req->reply_expected && req_awaiting_reply))
  482. goto out;
  483. pmu_state = sending;
  484. data_index = 1;
  485. data_len = pmu_data_len[req->data[0]][0];
  486. /* set the shift register to shift out and send a byte */
  487. send_byte(req->data[0]);
  488. out:
  489. local_irq_restore(flags);
  490. }
  491. void
  492. pmu_poll(void)
  493. {
  494. unsigned long flags;
  495. local_irq_save(flags);
  496. if (via1[IFR] & SR_INT) {
  497. via1[IFR] = SR_INT;
  498. pmu_interrupt(IRQ_MAC_ADB_SR, NULL);
  499. }
  500. if (via1[IFR] & CB1_INT) {
  501. via1[IFR] = CB1_INT;
  502. pmu_interrupt(IRQ_MAC_ADB_CL, NULL);
  503. }
  504. local_irq_restore(flags);
  505. }
  506. static irqreturn_t
  507. pmu_interrupt(int irq, void *dev_id)
  508. {
  509. struct adb_request *req;
  510. int timeout, bite = 0; /* to prevent compiler warning */
  511. #if 0
  512. printk("pmu_interrupt: irq %d state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
  513. irq, pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
  514. #endif
  515. if (irq == IRQ_MAC_ADB_CL) { /* CB1 interrupt */
  516. adb_int_pending = 1;
  517. } else if (irq == IRQ_MAC_ADB_SR) { /* SR interrupt */
  518. if (via2[B] & TACK) {
  519. printk(KERN_DEBUG "PMU: SR_INT but ack still high! (%x)\n", via2[B]);
  520. }
  521. /* if reading grab the byte */
  522. if ((via1[ACR] & SR_OUT) == 0) bite = via1[SR];
  523. /* reset TREQ and wait for TACK to go high */
  524. via2[B] |= TREQ;
  525. timeout = 3200;
  526. while (!(via2[B] & TACK)) {
  527. if (--timeout < 0) {
  528. printk(KERN_ERR "PMU not responding (!ack)\n");
  529. goto finish;
  530. }
  531. udelay(10);
  532. }
  533. switch (pmu_state) {
  534. case sending:
  535. req = current_req;
  536. if (data_len < 0) {
  537. data_len = req->nbytes - 1;
  538. send_byte(data_len);
  539. break;
  540. }
  541. if (data_index <= data_len) {
  542. send_byte(req->data[data_index++]);
  543. break;
  544. }
  545. req->sent = 1;
  546. data_len = pmu_data_len[req->data[0]][1];
  547. if (data_len == 0) {
  548. pmu_state = idle;
  549. current_req = req->next;
  550. if (req->reply_expected)
  551. req_awaiting_reply = req;
  552. else
  553. pmu_done(req);
  554. } else {
  555. pmu_state = reading;
  556. data_index = 0;
  557. reply_ptr = req->reply + req->reply_len;
  558. recv_byte();
  559. }
  560. break;
  561. case intack:
  562. data_index = 0;
  563. data_len = -1;
  564. pmu_state = reading_intr;
  565. reply_ptr = interrupt_data;
  566. recv_byte();
  567. break;
  568. case reading:
  569. case reading_intr:
  570. if (data_len == -1) {
  571. data_len = bite;
  572. if (bite > 32)
  573. printk(KERN_ERR "PMU: bad reply len %d\n",
  574. bite);
  575. } else {
  576. reply_ptr[data_index++] = bite;
  577. }
  578. if (data_index < data_len) {
  579. recv_byte();
  580. break;
  581. }
  582. if (pmu_state == reading_intr) {
  583. pmu_handle_data(interrupt_data, data_index);
  584. } else {
  585. req = current_req;
  586. current_req = req->next;
  587. req->reply_len += data_index;
  588. pmu_done(req);
  589. }
  590. pmu_state = idle;
  591. break;
  592. default:
  593. printk(KERN_ERR "pmu_interrupt: unknown state %d?\n",
  594. pmu_state);
  595. }
  596. }
  597. finish:
  598. if (pmu_state == idle) {
  599. if (adb_int_pending) {
  600. pmu_state = intack;
  601. send_byte(PMU_INT_ACK);
  602. adb_int_pending = 0;
  603. } else if (current_req) {
  604. pmu_start();
  605. }
  606. }
  607. #if 0
  608. printk("pmu_interrupt: exit state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
  609. pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
  610. #endif
  611. return IRQ_HANDLED;
  612. }
  613. static void
  614. pmu_done(struct adb_request *req)
  615. {
  616. req->complete = 1;
  617. if (req->done)
  618. (*req->done)(req);
  619. }
  620. /* Interrupt data could be the result data from an ADB cmd */
  621. static void
  622. pmu_handle_data(unsigned char *data, int len)
  623. {
  624. static int show_pmu_ints = 1;
  625. asleep = 0;
  626. if (len < 1) {
  627. adb_int_pending = 0;
  628. return;
  629. }
  630. if (data[0] & PMU_INT_ADB) {
  631. if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
  632. struct adb_request *req = req_awaiting_reply;
  633. if (req == 0) {
  634. printk(KERN_ERR "PMU: extra ADB reply\n");
  635. return;
  636. }
  637. req_awaiting_reply = NULL;
  638. if (len <= 2)
  639. req->reply_len = 0;
  640. else {
  641. memcpy(req->reply, data + 1, len - 1);
  642. req->reply_len = len - 1;
  643. }
  644. pmu_done(req);
  645. } else {
  646. adb_input(data+1, len-1, 1);
  647. }
  648. } else {
  649. if (data[0] == 0x08 && len == 3) {
  650. /* sound/brightness buttons pressed */
  651. pmu_set_brightness(data[1] >> 3);
  652. set_volume(data[2]);
  653. } else if (show_pmu_ints
  654. && !(data[0] == PMU_INT_TICK && len == 1)) {
  655. int i;
  656. printk(KERN_DEBUG "pmu intr");
  657. for (i = 0; i < len; ++i)
  658. printk(" %.2x", data[i]);
  659. printk("\n");
  660. }
  661. }
  662. }
  663. static int backlight_level = -1;
  664. static int backlight_enabled = 0;
  665. #define LEVEL_TO_BRIGHT(lev) ((lev) < 1? 0x7f: 0x4a - ((lev) << 1))
  666. static void
  667. pmu_enable_backlight(int on)
  668. {
  669. struct adb_request req;
  670. if (on) {
  671. /* first call: get current backlight value */
  672. if (backlight_level < 0) {
  673. switch(pmu_kind) {
  674. case PMU_68K_V1:
  675. case PMU_68K_V2:
  676. pmu_request(&req, NULL, 3, PMU_READ_NVRAM, 0x14, 0xe);
  677. while (!req.complete)
  678. pmu_poll();
  679. printk(KERN_DEBUG "pmu: nvram returned bright: %d\n", (int)req.reply[1]);
  680. backlight_level = req.reply[1];
  681. break;
  682. default:
  683. backlight_enabled = 0;
  684. return;
  685. }
  686. }
  687. pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
  688. LEVEL_TO_BRIGHT(backlight_level));
  689. while (!req.complete)
  690. pmu_poll();
  691. }
  692. pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  693. PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
  694. while (!req.complete)
  695. pmu_poll();
  696. backlight_enabled = on;
  697. }
  698. static void
  699. pmu_set_brightness(int level)
  700. {
  701. int bright;
  702. backlight_level = level;
  703. bright = LEVEL_TO_BRIGHT(level);
  704. if (!backlight_enabled)
  705. return;
  706. if (bright_req_1.complete)
  707. pmu_request(&bright_req_1, NULL, 2, PMU_BACKLIGHT_BRIGHT,
  708. bright);
  709. if (bright_req_2.complete)
  710. pmu_request(&bright_req_2, NULL, 2, PMU_POWER_CTRL,
  711. PMU_POW_BACKLIGHT | (bright < 0x7f ? PMU_POW_ON : PMU_POW_OFF));
  712. }
  713. void
  714. pmu_enable_irled(int on)
  715. {
  716. struct adb_request req;
  717. pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
  718. (on ? PMU_POW_ON : PMU_POW_OFF));
  719. while (!req.complete)
  720. pmu_poll();
  721. }
  722. static void
  723. set_volume(int level)
  724. {
  725. }
  726. int
  727. pmu_present(void)
  728. {
  729. return (pmu_kind != PMU_UNKNOWN);
  730. }