via-pmu68k.c 19 KB

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