via-pmu68k.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Device driver for the PMU on 68K-based Apple PowerBooks
  4. *
  5. * The VIA (versatile interface adapter) interfaces to the PMU,
  6. * a 6805 microprocessor core whose primary function is to control
  7. * battery charging and system power on the PowerBooks.
  8. * The PMU also controls the ADB (Apple Desktop Bus) which connects
  9. * to the keyboard and mouse, as well as the non-volatile RAM
  10. * and the RTC (real time clock) chip.
  11. *
  12. * Adapted for 68K PMU by Joshua M. Thompson
  13. *
  14. * Based largely on the PowerMac PMU code by Paul Mackerras and
  15. * Fabio Riccardi.
  16. *
  17. * Also based on the PMU driver from MkLinux by Apple Computer, Inc.
  18. * and the Open Software Foundation, Inc.
  19. */
  20. #include <stdarg.h>
  21. #include <linux/types.h>
  22. #include <linux/errno.h>
  23. #include <linux/kernel.h>
  24. #include <linux/delay.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/pci.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/irq.h>
  38. #include <linux/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 int init_pmu(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. .name = "68K PMU",
  109. .probe = pmu_probe,
  110. .init = pmu_init,
  111. .send_request = pmu_send_request,
  112. .autopoll = pmu_autopoll,
  113. .poll = pmu_poll,
  114. .reset_bus = 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 __init find_via_pmu(void)
  159. {
  160. switch (macintosh_config->adb_type) {
  161. case MAC_ADB_PB1:
  162. pmu_kind = PMU_68K_V1;
  163. break;
  164. case MAC_ADB_PB2:
  165. pmu_kind = PMU_68K_V2;
  166. break;
  167. default:
  168. pmu_kind = PMU_UNKNOWN;
  169. return -ENODEV;
  170. }
  171. pmu_state = idle;
  172. if (!init_pmu())
  173. goto fail_init;
  174. pr_info("adb: PMU 68K driver v0.5 for Unified ADB\n");
  175. return 1;
  176. fail_init:
  177. pmu_kind = PMU_UNKNOWN;
  178. return 0;
  179. }
  180. static int pmu_probe(void)
  181. {
  182. if (pmu_kind == PMU_UNKNOWN)
  183. return -ENODEV;
  184. return 0;
  185. }
  186. static int pmu_init(void)
  187. {
  188. if (pmu_kind == PMU_UNKNOWN)
  189. return -ENODEV;
  190. return 0;
  191. }
  192. static int __init via_pmu_start(void)
  193. {
  194. if (pmu_kind == PMU_UNKNOWN)
  195. return -ENODEV;
  196. if (request_irq(IRQ_MAC_ADB_SR, pmu_interrupt, 0, "PMU_SR",
  197. pmu_interrupt)) {
  198. pr_err("%s: can't get SR irq\n", __func__);
  199. return -ENODEV;
  200. }
  201. if (request_irq(IRQ_MAC_ADB_CL, pmu_interrupt, 0, "PMU_CL",
  202. pmu_interrupt)) {
  203. pr_err("%s: can't get CL irq\n", __func__);
  204. free_irq(IRQ_MAC_ADB_SR, pmu_interrupt);
  205. return -ENODEV;
  206. }
  207. pmu_fully_inited = 1;
  208. /* Enable backlight */
  209. pmu_enable_backlight(1);
  210. return 0;
  211. }
  212. arch_initcall(via_pmu_start);
  213. static int __init init_pmu(void)
  214. {
  215. int timeout;
  216. volatile struct adb_request req;
  217. via2[B] |= TREQ; /* negate TREQ */
  218. via2[DIRB] = (via2[DIRB] | TREQ) & ~TACK; /* TACK in, TREQ out */
  219. pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB);
  220. timeout = 100000;
  221. while (!req.complete) {
  222. if (--timeout < 0) {
  223. printk(KERN_ERR "pmu_init: no response from PMU\n");
  224. return -EAGAIN;
  225. }
  226. udelay(10);
  227. pmu_poll();
  228. }
  229. /* ack all pending interrupts */
  230. timeout = 100000;
  231. interrupt_data[0] = 1;
  232. while (interrupt_data[0] || pmu_state != idle) {
  233. if (--timeout < 0) {
  234. printk(KERN_ERR "pmu_init: timed out acking intrs\n");
  235. return -EAGAIN;
  236. }
  237. if (pmu_state == idle) {
  238. adb_int_pending = 1;
  239. pmu_interrupt(0, NULL);
  240. }
  241. pmu_poll();
  242. udelay(10);
  243. }
  244. pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK,
  245. PMU_INT_ADB_AUTO|PMU_INT_SNDBRT|PMU_INT_ADB);
  246. timeout = 100000;
  247. while (!req.complete) {
  248. if (--timeout < 0) {
  249. printk(KERN_ERR "pmu_init: no response from PMU\n");
  250. return -EAGAIN;
  251. }
  252. udelay(10);
  253. pmu_poll();
  254. }
  255. bright_req_1.complete = 1;
  256. bright_req_2.complete = 1;
  257. bright_req_3.complete = 1;
  258. return 1;
  259. }
  260. int
  261. pmu_get_model(void)
  262. {
  263. return pmu_kind;
  264. }
  265. /* Send an ADB command */
  266. static int
  267. pmu_send_request(struct adb_request *req, int sync)
  268. {
  269. int i, ret;
  270. if (!pmu_fully_inited)
  271. {
  272. req->complete = 1;
  273. return -ENXIO;
  274. }
  275. ret = -EINVAL;
  276. switch (req->data[0]) {
  277. case PMU_PACKET:
  278. for (i = 0; i < req->nbytes - 1; ++i)
  279. req->data[i] = req->data[i+1];
  280. --req->nbytes;
  281. if (pmu_data_len[req->data[0]][1] != 0) {
  282. req->reply[0] = ADB_RET_OK;
  283. req->reply_len = 1;
  284. } else
  285. req->reply_len = 0;
  286. ret = pmu_queue_request(req);
  287. break;
  288. case CUDA_PACKET:
  289. switch (req->data[1]) {
  290. case CUDA_GET_TIME:
  291. if (req->nbytes != 2)
  292. break;
  293. req->data[0] = PMU_READ_RTC;
  294. req->nbytes = 1;
  295. req->reply_len = 3;
  296. req->reply[0] = CUDA_PACKET;
  297. req->reply[1] = 0;
  298. req->reply[2] = CUDA_GET_TIME;
  299. ret = pmu_queue_request(req);
  300. break;
  301. case CUDA_SET_TIME:
  302. if (req->nbytes != 6)
  303. break;
  304. req->data[0] = PMU_SET_RTC;
  305. req->nbytes = 5;
  306. for (i = 1; i <= 4; ++i)
  307. req->data[i] = req->data[i+1];
  308. req->reply_len = 3;
  309. req->reply[0] = CUDA_PACKET;
  310. req->reply[1] = 0;
  311. req->reply[2] = CUDA_SET_TIME;
  312. ret = pmu_queue_request(req);
  313. break;
  314. case CUDA_GET_PRAM:
  315. if (req->nbytes != 4)
  316. break;
  317. req->data[0] = PMU_READ_NVRAM;
  318. req->data[1] = req->data[2];
  319. req->data[2] = req->data[3];
  320. req->nbytes = 3;
  321. req->reply_len = 3;
  322. req->reply[0] = CUDA_PACKET;
  323. req->reply[1] = 0;
  324. req->reply[2] = CUDA_GET_PRAM;
  325. ret = pmu_queue_request(req);
  326. break;
  327. case CUDA_SET_PRAM:
  328. if (req->nbytes != 5)
  329. break;
  330. req->data[0] = PMU_WRITE_NVRAM;
  331. req->data[1] = req->data[2];
  332. req->data[2] = req->data[3];
  333. req->data[3] = req->data[4];
  334. req->nbytes = 4;
  335. req->reply_len = 3;
  336. req->reply[0] = CUDA_PACKET;
  337. req->reply[1] = 0;
  338. req->reply[2] = CUDA_SET_PRAM;
  339. ret = pmu_queue_request(req);
  340. break;
  341. }
  342. break;
  343. case ADB_PACKET:
  344. for (i = req->nbytes - 1; i > 1; --i)
  345. req->data[i+2] = req->data[i];
  346. req->data[3] = req->nbytes - 2;
  347. req->data[2] = pmu_adb_flags;
  348. /*req->data[1] = req->data[1];*/
  349. req->data[0] = PMU_ADB_CMD;
  350. req->nbytes += 2;
  351. req->reply_expected = 1;
  352. req->reply_len = 0;
  353. ret = pmu_queue_request(req);
  354. break;
  355. }
  356. if (ret)
  357. {
  358. req->complete = 1;
  359. return ret;
  360. }
  361. if (sync) {
  362. while (!req->complete)
  363. pmu_poll();
  364. }
  365. return 0;
  366. }
  367. /* Enable/disable autopolling */
  368. static int
  369. pmu_autopoll(int devs)
  370. {
  371. struct adb_request req;
  372. if (!pmu_fully_inited) return -ENXIO;
  373. if (devs) {
  374. adb_dev_map = devs;
  375. pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
  376. adb_dev_map >> 8, adb_dev_map);
  377. pmu_adb_flags = 2;
  378. } else {
  379. pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
  380. pmu_adb_flags = 0;
  381. }
  382. while (!req.complete)
  383. pmu_poll();
  384. return 0;
  385. }
  386. /* Reset the ADB bus */
  387. static int
  388. pmu_reset_bus(void)
  389. {
  390. struct adb_request req;
  391. long timeout;
  392. int save_autopoll = adb_dev_map;
  393. if (!pmu_fully_inited) return -ENXIO;
  394. /* anyone got a better idea?? */
  395. pmu_autopoll(0);
  396. req.nbytes = 5;
  397. req.done = NULL;
  398. req.data[0] = PMU_ADB_CMD;
  399. req.data[1] = 0;
  400. req.data[2] = 3; /* ADB_BUSRESET ??? */
  401. req.data[3] = 0;
  402. req.data[4] = 0;
  403. req.reply_len = 0;
  404. req.reply_expected = 1;
  405. if (pmu_queue_request(&req) != 0)
  406. {
  407. printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
  408. return -EIO;
  409. }
  410. while (!req.complete)
  411. pmu_poll();
  412. timeout = 100000;
  413. while (!req.complete) {
  414. if (--timeout < 0) {
  415. printk(KERN_ERR "pmu_adb_reset_bus (reset): no response from PMU\n");
  416. return -EIO;
  417. }
  418. udelay(10);
  419. pmu_poll();
  420. }
  421. if (save_autopoll != 0)
  422. pmu_autopoll(save_autopoll);
  423. return 0;
  424. }
  425. /* Construct and send a pmu request */
  426. int
  427. pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
  428. int nbytes, ...)
  429. {
  430. va_list list;
  431. int i;
  432. if (nbytes < 0 || nbytes > 32) {
  433. printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
  434. req->complete = 1;
  435. return -EINVAL;
  436. }
  437. req->nbytes = nbytes;
  438. req->done = done;
  439. va_start(list, nbytes);
  440. for (i = 0; i < nbytes; ++i)
  441. req->data[i] = va_arg(list, int);
  442. va_end(list);
  443. if (pmu_data_len[req->data[0]][1] != 0) {
  444. req->reply[0] = ADB_RET_OK;
  445. req->reply_len = 1;
  446. } else
  447. req->reply_len = 0;
  448. req->reply_expected = 0;
  449. return pmu_queue_request(req);
  450. }
  451. int
  452. pmu_queue_request(struct adb_request *req)
  453. {
  454. unsigned long flags;
  455. int nsend;
  456. if (req->nbytes <= 0) {
  457. req->complete = 1;
  458. return 0;
  459. }
  460. nsend = pmu_data_len[req->data[0]][0];
  461. if (nsend >= 0 && req->nbytes != nsend + 1) {
  462. req->complete = 1;
  463. return -EINVAL;
  464. }
  465. req->next = NULL;
  466. req->sent = 0;
  467. req->complete = 0;
  468. local_irq_save(flags);
  469. if (current_req != 0) {
  470. last_req->next = req;
  471. last_req = req;
  472. } else {
  473. current_req = req;
  474. last_req = req;
  475. if (pmu_state == idle)
  476. pmu_start();
  477. }
  478. local_irq_restore(flags);
  479. return 0;
  480. }
  481. static void
  482. send_byte(int x)
  483. {
  484. via1[ACR] |= SR_CTRL;
  485. via1[SR] = x;
  486. via2[B] &= ~TREQ; /* assert TREQ */
  487. }
  488. static void
  489. recv_byte(void)
  490. {
  491. char c;
  492. via1[ACR] = (via1[ACR] | SR_EXT) & ~SR_OUT;
  493. c = via1[SR]; /* resets SR */
  494. via2[B] &= ~TREQ;
  495. }
  496. static void
  497. pmu_start(void)
  498. {
  499. unsigned long flags;
  500. struct adb_request *req;
  501. /* assert pmu_state == idle */
  502. /* get the packet to send */
  503. local_irq_save(flags);
  504. req = current_req;
  505. if (req == 0 || pmu_state != idle
  506. || (req->reply_expected && req_awaiting_reply))
  507. goto out;
  508. pmu_state = sending;
  509. data_index = 1;
  510. data_len = pmu_data_len[req->data[0]][0];
  511. /* set the shift register to shift out and send a byte */
  512. send_byte(req->data[0]);
  513. out:
  514. local_irq_restore(flags);
  515. }
  516. void
  517. pmu_poll(void)
  518. {
  519. unsigned long flags;
  520. local_irq_save(flags);
  521. if (via1[IFR] & SR_INT) {
  522. via1[IFR] = SR_INT;
  523. pmu_interrupt(IRQ_MAC_ADB_SR, NULL);
  524. }
  525. if (via1[IFR] & CB1_INT) {
  526. via1[IFR] = CB1_INT;
  527. pmu_interrupt(IRQ_MAC_ADB_CL, NULL);
  528. }
  529. local_irq_restore(flags);
  530. }
  531. static irqreturn_t
  532. pmu_interrupt(int irq, void *dev_id)
  533. {
  534. struct adb_request *req;
  535. int timeout, bite = 0; /* to prevent compiler warning */
  536. #if 0
  537. printk("pmu_interrupt: irq %d state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
  538. irq, pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
  539. #endif
  540. if (irq == IRQ_MAC_ADB_CL) { /* CB1 interrupt */
  541. adb_int_pending = 1;
  542. } else if (irq == IRQ_MAC_ADB_SR) { /* SR interrupt */
  543. if (via2[B] & TACK) {
  544. printk(KERN_DEBUG "PMU: SR_INT but ack still high! (%x)\n", via2[B]);
  545. }
  546. /* if reading grab the byte */
  547. if ((via1[ACR] & SR_OUT) == 0) bite = via1[SR];
  548. /* reset TREQ and wait for TACK to go high */
  549. via2[B] |= TREQ;
  550. timeout = 3200;
  551. while (!(via2[B] & TACK)) {
  552. if (--timeout < 0) {
  553. printk(KERN_ERR "PMU not responding (!ack)\n");
  554. goto finish;
  555. }
  556. udelay(10);
  557. }
  558. switch (pmu_state) {
  559. case sending:
  560. req = current_req;
  561. if (data_len < 0) {
  562. data_len = req->nbytes - 1;
  563. send_byte(data_len);
  564. break;
  565. }
  566. if (data_index <= data_len) {
  567. send_byte(req->data[data_index++]);
  568. break;
  569. }
  570. req->sent = 1;
  571. data_len = pmu_data_len[req->data[0]][1];
  572. if (data_len == 0) {
  573. pmu_state = idle;
  574. current_req = req->next;
  575. if (req->reply_expected)
  576. req_awaiting_reply = req;
  577. else
  578. pmu_done(req);
  579. } else {
  580. pmu_state = reading;
  581. data_index = 0;
  582. reply_ptr = req->reply + req->reply_len;
  583. recv_byte();
  584. }
  585. break;
  586. case intack:
  587. data_index = 0;
  588. data_len = -1;
  589. pmu_state = reading_intr;
  590. reply_ptr = interrupt_data;
  591. recv_byte();
  592. break;
  593. case reading:
  594. case reading_intr:
  595. if (data_len == -1) {
  596. data_len = bite;
  597. if (bite > 32)
  598. printk(KERN_ERR "PMU: bad reply len %d\n",
  599. bite);
  600. } else {
  601. reply_ptr[data_index++] = bite;
  602. }
  603. if (data_index < data_len) {
  604. recv_byte();
  605. break;
  606. }
  607. if (pmu_state == reading_intr) {
  608. pmu_handle_data(interrupt_data, data_index);
  609. } else {
  610. req = current_req;
  611. current_req = req->next;
  612. req->reply_len += data_index;
  613. pmu_done(req);
  614. }
  615. pmu_state = idle;
  616. break;
  617. default:
  618. printk(KERN_ERR "pmu_interrupt: unknown state %d?\n",
  619. pmu_state);
  620. }
  621. }
  622. finish:
  623. if (pmu_state == idle) {
  624. if (adb_int_pending) {
  625. pmu_state = intack;
  626. send_byte(PMU_INT_ACK);
  627. adb_int_pending = 0;
  628. } else if (current_req) {
  629. pmu_start();
  630. }
  631. }
  632. #if 0
  633. printk("pmu_interrupt: exit state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
  634. pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
  635. #endif
  636. return IRQ_HANDLED;
  637. }
  638. static void
  639. pmu_done(struct adb_request *req)
  640. {
  641. req->complete = 1;
  642. if (req->done)
  643. (*req->done)(req);
  644. }
  645. /* Interrupt data could be the result data from an ADB cmd */
  646. static void
  647. pmu_handle_data(unsigned char *data, int len)
  648. {
  649. static int show_pmu_ints = 1;
  650. asleep = 0;
  651. if (len < 1) {
  652. adb_int_pending = 0;
  653. return;
  654. }
  655. if (data[0] & PMU_INT_ADB) {
  656. if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
  657. struct adb_request *req = req_awaiting_reply;
  658. if (req == 0) {
  659. printk(KERN_ERR "PMU: extra ADB reply\n");
  660. return;
  661. }
  662. req_awaiting_reply = NULL;
  663. if (len <= 2)
  664. req->reply_len = 0;
  665. else {
  666. memcpy(req->reply, data + 1, len - 1);
  667. req->reply_len = len - 1;
  668. }
  669. pmu_done(req);
  670. } else {
  671. adb_input(data+1, len-1, 1);
  672. }
  673. } else {
  674. if (data[0] == 0x08 && len == 3) {
  675. /* sound/brightness buttons pressed */
  676. pmu_set_brightness(data[1] >> 3);
  677. set_volume(data[2]);
  678. } else if (show_pmu_ints
  679. && !(data[0] == PMU_INT_TICK && len == 1)) {
  680. int i;
  681. printk(KERN_DEBUG "pmu intr");
  682. for (i = 0; i < len; ++i)
  683. printk(" %.2x", data[i]);
  684. printk("\n");
  685. }
  686. }
  687. }
  688. static int backlight_level = -1;
  689. static int backlight_enabled = 0;
  690. #define LEVEL_TO_BRIGHT(lev) ((lev) < 1? 0x7f: 0x4a - ((lev) << 1))
  691. static void
  692. pmu_enable_backlight(int on)
  693. {
  694. struct adb_request req;
  695. if (on) {
  696. /* first call: get current backlight value */
  697. if (backlight_level < 0) {
  698. switch(pmu_kind) {
  699. case PMU_68K_V1:
  700. case PMU_68K_V2:
  701. pmu_request(&req, NULL, 3, PMU_READ_NVRAM, 0x14, 0xe);
  702. while (!req.complete)
  703. pmu_poll();
  704. printk(KERN_DEBUG "pmu: nvram returned bright: %d\n", (int)req.reply[1]);
  705. backlight_level = req.reply[1];
  706. break;
  707. default:
  708. backlight_enabled = 0;
  709. return;
  710. }
  711. }
  712. pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
  713. LEVEL_TO_BRIGHT(backlight_level));
  714. while (!req.complete)
  715. pmu_poll();
  716. }
  717. pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  718. PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
  719. while (!req.complete)
  720. pmu_poll();
  721. backlight_enabled = on;
  722. }
  723. static void
  724. pmu_set_brightness(int level)
  725. {
  726. int bright;
  727. backlight_level = level;
  728. bright = LEVEL_TO_BRIGHT(level);
  729. if (!backlight_enabled)
  730. return;
  731. if (bright_req_1.complete)
  732. pmu_request(&bright_req_1, NULL, 2, PMU_BACKLIGHT_BRIGHT,
  733. bright);
  734. if (bright_req_2.complete)
  735. pmu_request(&bright_req_2, NULL, 2, PMU_POWER_CTRL,
  736. PMU_POW_BACKLIGHT | (bright < 0x7f ? PMU_POW_ON : PMU_POW_OFF));
  737. }
  738. void
  739. pmu_enable_irled(int on)
  740. {
  741. struct adb_request req;
  742. pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
  743. (on ? PMU_POW_ON : PMU_POW_OFF));
  744. while (!req.complete)
  745. pmu_poll();
  746. }
  747. static void
  748. set_volume(int level)
  749. {
  750. }
  751. int
  752. pmu_present(void)
  753. {
  754. return (pmu_kind != PMU_UNKNOWN);
  755. }