via-pmu68k.c 19 KB

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