via-pmu68k.c 19 KB

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