via-pmu68k.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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/sched.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/pci.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/adb.h>
  32. #include <linux/pmu.h>
  33. #include <linux/cuda.h>
  34. #include <asm/macintosh.h>
  35. #include <asm/macints.h>
  36. #include <asm/machw.h>
  37. #include <asm/mac_via.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/system.h>
  40. #include <asm/irq.h>
  41. #include <asm/uaccess.h>
  42. /* Misc minor number allocated for /dev/pmu */
  43. #define PMU_MINOR 154
  44. /* VIA registers - spaced 0x200 bytes apart */
  45. #define RS 0x200 /* skip between registers */
  46. #define B 0 /* B-side data */
  47. #define A RS /* A-side data */
  48. #define DIRB (2*RS) /* B-side direction (1=output) */
  49. #define DIRA (3*RS) /* A-side direction (1=output) */
  50. #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
  51. #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
  52. #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
  53. #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
  54. #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
  55. #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
  56. #define SR (10*RS) /* Shift register */
  57. #define ACR (11*RS) /* Auxiliary control register */
  58. #define PCR (12*RS) /* Peripheral control register */
  59. #define IFR (13*RS) /* Interrupt flag register */
  60. #define IER (14*RS) /* Interrupt enable register */
  61. #define ANH (15*RS) /* A-side data, no handshake */
  62. /* Bits in B data register: both active low */
  63. #define TACK 0x02 /* Transfer acknowledge (input) */
  64. #define TREQ 0x04 /* Transfer request (output) */
  65. /* Bits in ACR */
  66. #define SR_CTRL 0x1c /* Shift register control bits */
  67. #define SR_EXT 0x0c /* Shift on external clock */
  68. #define SR_OUT 0x10 /* Shift out if 1 */
  69. /* Bits in IFR and IER */
  70. #define SR_INT 0x04 /* Shift register full/empty */
  71. #define CB1_INT 0x10 /* transition on CB1 input */
  72. static enum pmu_state {
  73. idle,
  74. sending,
  75. intack,
  76. reading,
  77. reading_intr,
  78. } pmu_state;
  79. static struct adb_request *current_req;
  80. static struct adb_request *last_req;
  81. static struct adb_request *req_awaiting_reply;
  82. static unsigned char interrupt_data[32];
  83. static unsigned char *reply_ptr;
  84. static int data_index;
  85. static int data_len;
  86. static int adb_int_pending;
  87. static int pmu_adb_flags;
  88. static int adb_dev_map = 0;
  89. static struct adb_request bright_req_1, bright_req_2, bright_req_3;
  90. static int pmu_kind = PMU_UNKNOWN;
  91. static int pmu_fully_inited = 0;
  92. int asleep;
  93. BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
  94. static int pmu_probe(void);
  95. static int pmu_init(void);
  96. static void pmu_start(void);
  97. static irqreturn_t pmu_interrupt(int irq, void *arg);
  98. static int pmu_send_request(struct adb_request *req, int sync);
  99. static int pmu_autopoll(int devs);
  100. void pmu_poll(void);
  101. static int pmu_reset_bus(void);
  102. static int pmu_queue_request(struct adb_request *req);
  103. static void pmu_start(void);
  104. static void send_byte(int x);
  105. static void recv_byte(void);
  106. static void pmu_done(struct adb_request *req);
  107. static void pmu_handle_data(unsigned char *data, int len);
  108. static void set_volume(int level);
  109. static void pmu_enable_backlight(int on);
  110. static void pmu_set_brightness(int level);
  111. struct adb_driver via_pmu_driver = {
  112. "68K PMU",
  113. pmu_probe,
  114. pmu_init,
  115. pmu_send_request,
  116. pmu_autopoll,
  117. pmu_poll,
  118. pmu_reset_bus
  119. };
  120. /*
  121. * This table indicates for each PMU opcode:
  122. * - the number of data bytes to be sent with the command, or -1
  123. * if a length byte should be sent,
  124. * - the number of response bytes which the PMU will return, or
  125. * -1 if it will send a length byte.
  126. */
  127. static s8 pmu_data_len[256][2] = {
  128. /* 0 1 2 3 4 5 6 7 */
  129. /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  130. /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  131. /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  132. /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
  133. /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
  134. /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
  135. /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  136. /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
  137. /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  138. /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
  139. /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
  140. /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
  141. /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  142. /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
  143. /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  144. /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
  145. /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  146. /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  147. /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  148. /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  149. /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
  150. /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  151. /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  152. /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  153. /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  154. /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  155. /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  156. /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
  157. /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
  158. /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
  159. /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  160. /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  161. };
  162. int pmu_probe(void)
  163. {
  164. if (macintosh_config->adb_type == MAC_ADB_PB1) {
  165. pmu_kind = PMU_68K_V1;
  166. } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
  167. pmu_kind = PMU_68K_V2;
  168. } else {
  169. return -ENODEV;
  170. }
  171. pmu_state = idle;
  172. return 0;
  173. }
  174. static int
  175. pmu_init(void)
  176. {
  177. int timeout;
  178. volatile struct adb_request req;
  179. via2[B] |= TREQ; /* negate TREQ */
  180. via2[DIRB] = (via2[DIRB] | TREQ) & ~TACK; /* TACK in, TREQ out */
  181. pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB);
  182. timeout = 100000;
  183. while (!req.complete) {
  184. if (--timeout < 0) {
  185. printk(KERN_ERR "pmu_init: no response from PMU\n");
  186. return -EAGAIN;
  187. }
  188. udelay(10);
  189. pmu_poll();
  190. }
  191. /* ack all pending interrupts */
  192. timeout = 100000;
  193. interrupt_data[0] = 1;
  194. while (interrupt_data[0] || pmu_state != idle) {
  195. if (--timeout < 0) {
  196. printk(KERN_ERR "pmu_init: timed out acking intrs\n");
  197. return -EAGAIN;
  198. }
  199. if (pmu_state == idle) {
  200. adb_int_pending = 1;
  201. pmu_interrupt(0, NULL);
  202. }
  203. pmu_poll();
  204. udelay(10);
  205. }
  206. pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK,
  207. PMU_INT_ADB_AUTO|PMU_INT_SNDBRT|PMU_INT_ADB);
  208. timeout = 100000;
  209. while (!req.complete) {
  210. if (--timeout < 0) {
  211. printk(KERN_ERR "pmu_init: no response from PMU\n");
  212. return -EAGAIN;
  213. }
  214. udelay(10);
  215. pmu_poll();
  216. }
  217. bright_req_1.complete = 1;
  218. bright_req_2.complete = 1;
  219. bright_req_3.complete = 1;
  220. if (request_irq(IRQ_MAC_ADB_SR, pmu_interrupt, 0, "pmu-shift",
  221. pmu_interrupt)) {
  222. printk(KERN_ERR "pmu_init: can't get irq %d\n",
  223. IRQ_MAC_ADB_SR);
  224. return -EAGAIN;
  225. }
  226. if (request_irq(IRQ_MAC_ADB_CL, pmu_interrupt, 0, "pmu-clock",
  227. pmu_interrupt)) {
  228. printk(KERN_ERR "pmu_init: can't get irq %d\n",
  229. IRQ_MAC_ADB_CL);
  230. free_irq(IRQ_MAC_ADB_SR, pmu_interrupt);
  231. return -EAGAIN;
  232. }
  233. pmu_fully_inited = 1;
  234. /* Enable backlight */
  235. pmu_enable_backlight(1);
  236. printk("adb: PMU 68K driver v0.5 for Unified ADB.\n");
  237. return 0;
  238. }
  239. int
  240. pmu_get_model(void)
  241. {
  242. return pmu_kind;
  243. }
  244. /* Send an ADB command */
  245. static int
  246. pmu_send_request(struct adb_request *req, int sync)
  247. {
  248. int i, ret;
  249. if (!pmu_fully_inited)
  250. {
  251. req->complete = 1;
  252. return -ENXIO;
  253. }
  254. ret = -EINVAL;
  255. switch (req->data[0]) {
  256. case PMU_PACKET:
  257. for (i = 0; i < req->nbytes - 1; ++i)
  258. req->data[i] = req->data[i+1];
  259. --req->nbytes;
  260. if (pmu_data_len[req->data[0]][1] != 0) {
  261. req->reply[0] = ADB_RET_OK;
  262. req->reply_len = 1;
  263. } else
  264. req->reply_len = 0;
  265. ret = pmu_queue_request(req);
  266. break;
  267. case CUDA_PACKET:
  268. switch (req->data[1]) {
  269. case CUDA_GET_TIME:
  270. if (req->nbytes != 2)
  271. break;
  272. req->data[0] = PMU_READ_RTC;
  273. req->nbytes = 1;
  274. req->reply_len = 3;
  275. req->reply[0] = CUDA_PACKET;
  276. req->reply[1] = 0;
  277. req->reply[2] = CUDA_GET_TIME;
  278. ret = pmu_queue_request(req);
  279. break;
  280. case CUDA_SET_TIME:
  281. if (req->nbytes != 6)
  282. break;
  283. req->data[0] = PMU_SET_RTC;
  284. req->nbytes = 5;
  285. for (i = 1; i <= 4; ++i)
  286. req->data[i] = req->data[i+1];
  287. req->reply_len = 3;
  288. req->reply[0] = CUDA_PACKET;
  289. req->reply[1] = 0;
  290. req->reply[2] = CUDA_SET_TIME;
  291. ret = pmu_queue_request(req);
  292. break;
  293. case CUDA_GET_PRAM:
  294. if (req->nbytes != 4)
  295. break;
  296. req->data[0] = PMU_READ_NVRAM;
  297. req->data[1] = req->data[2];
  298. req->data[2] = req->data[3];
  299. req->nbytes = 3;
  300. req->reply_len = 3;
  301. req->reply[0] = CUDA_PACKET;
  302. req->reply[1] = 0;
  303. req->reply[2] = CUDA_GET_PRAM;
  304. ret = pmu_queue_request(req);
  305. break;
  306. case CUDA_SET_PRAM:
  307. if (req->nbytes != 5)
  308. break;
  309. req->data[0] = PMU_WRITE_NVRAM;
  310. req->data[1] = req->data[2];
  311. req->data[2] = req->data[3];
  312. req->data[3] = req->data[4];
  313. req->nbytes = 4;
  314. req->reply_len = 3;
  315. req->reply[0] = CUDA_PACKET;
  316. req->reply[1] = 0;
  317. req->reply[2] = CUDA_SET_PRAM;
  318. ret = pmu_queue_request(req);
  319. break;
  320. }
  321. break;
  322. case ADB_PACKET:
  323. for (i = req->nbytes - 1; i > 1; --i)
  324. req->data[i+2] = req->data[i];
  325. req->data[3] = req->nbytes - 2;
  326. req->data[2] = pmu_adb_flags;
  327. /*req->data[1] = req->data[1];*/
  328. req->data[0] = PMU_ADB_CMD;
  329. req->nbytes += 2;
  330. req->reply_expected = 1;
  331. req->reply_len = 0;
  332. ret = pmu_queue_request(req);
  333. break;
  334. }
  335. if (ret)
  336. {
  337. req->complete = 1;
  338. return ret;
  339. }
  340. if (sync) {
  341. while (!req->complete)
  342. pmu_poll();
  343. }
  344. return 0;
  345. }
  346. /* Enable/disable autopolling */
  347. static int
  348. pmu_autopoll(int devs)
  349. {
  350. struct adb_request req;
  351. if (!pmu_fully_inited) return -ENXIO;
  352. if (devs) {
  353. adb_dev_map = devs;
  354. pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
  355. adb_dev_map >> 8, adb_dev_map);
  356. pmu_adb_flags = 2;
  357. } else {
  358. pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
  359. pmu_adb_flags = 0;
  360. }
  361. while (!req.complete)
  362. pmu_poll();
  363. return 0;
  364. }
  365. /* Reset the ADB bus */
  366. static int
  367. pmu_reset_bus(void)
  368. {
  369. struct adb_request req;
  370. long timeout;
  371. int save_autopoll = adb_dev_map;
  372. if (!pmu_fully_inited) return -ENXIO;
  373. /* anyone got a better idea?? */
  374. pmu_autopoll(0);
  375. req.nbytes = 5;
  376. req.done = NULL;
  377. req.data[0] = PMU_ADB_CMD;
  378. req.data[1] = 0;
  379. req.data[2] = 3; /* ADB_BUSRESET ??? */
  380. req.data[3] = 0;
  381. req.data[4] = 0;
  382. req.reply_len = 0;
  383. req.reply_expected = 1;
  384. if (pmu_queue_request(&req) != 0)
  385. {
  386. printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
  387. return -EIO;
  388. }
  389. while (!req.complete)
  390. pmu_poll();
  391. timeout = 100000;
  392. while (!req.complete) {
  393. if (--timeout < 0) {
  394. printk(KERN_ERR "pmu_adb_reset_bus (reset): no response from PMU\n");
  395. return -EIO;
  396. }
  397. udelay(10);
  398. pmu_poll();
  399. }
  400. if (save_autopoll != 0)
  401. pmu_autopoll(save_autopoll);
  402. return 0;
  403. }
  404. /* Construct and send a pmu request */
  405. int
  406. pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
  407. int nbytes, ...)
  408. {
  409. va_list list;
  410. int i;
  411. if (nbytes < 0 || nbytes > 32) {
  412. printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
  413. req->complete = 1;
  414. return -EINVAL;
  415. }
  416. req->nbytes = nbytes;
  417. req->done = done;
  418. va_start(list, nbytes);
  419. for (i = 0; i < nbytes; ++i)
  420. req->data[i] = va_arg(list, int);
  421. va_end(list);
  422. if (pmu_data_len[req->data[0]][1] != 0) {
  423. req->reply[0] = ADB_RET_OK;
  424. req->reply_len = 1;
  425. } else
  426. req->reply_len = 0;
  427. req->reply_expected = 0;
  428. return pmu_queue_request(req);
  429. }
  430. static int
  431. pmu_queue_request(struct adb_request *req)
  432. {
  433. unsigned long flags;
  434. int nsend;
  435. if (req->nbytes <= 0) {
  436. req->complete = 1;
  437. return 0;
  438. }
  439. nsend = pmu_data_len[req->data[0]][0];
  440. if (nsend >= 0 && req->nbytes != nsend + 1) {
  441. req->complete = 1;
  442. return -EINVAL;
  443. }
  444. req->next = NULL;
  445. req->sent = 0;
  446. req->complete = 0;
  447. local_irq_save(flags);
  448. if (current_req != 0) {
  449. last_req->next = req;
  450. last_req = req;
  451. } else {
  452. current_req = req;
  453. last_req = req;
  454. if (pmu_state == idle)
  455. pmu_start();
  456. }
  457. local_irq_restore(flags);
  458. return 0;
  459. }
  460. static void
  461. send_byte(int x)
  462. {
  463. via1[ACR] |= SR_CTRL;
  464. via1[SR] = x;
  465. via2[B] &= ~TREQ; /* assert TREQ */
  466. }
  467. static void
  468. recv_byte(void)
  469. {
  470. char c;
  471. via1[ACR] = (via1[ACR] | SR_EXT) & ~SR_OUT;
  472. c = via1[SR]; /* resets SR */
  473. via2[B] &= ~TREQ;
  474. }
  475. static void
  476. pmu_start(void)
  477. {
  478. unsigned long flags;
  479. struct adb_request *req;
  480. /* assert pmu_state == idle */
  481. /* get the packet to send */
  482. local_irq_save(flags);
  483. req = current_req;
  484. if (req == 0 || pmu_state != idle
  485. || (req->reply_expected && req_awaiting_reply))
  486. goto out;
  487. pmu_state = sending;
  488. data_index = 1;
  489. data_len = pmu_data_len[req->data[0]][0];
  490. /* set the shift register to shift out and send a byte */
  491. send_byte(req->data[0]);
  492. out:
  493. local_irq_restore(flags);
  494. }
  495. void
  496. pmu_poll(void)
  497. {
  498. unsigned long flags;
  499. local_irq_save(flags);
  500. if (via1[IFR] & SR_INT) {
  501. via1[IFR] = SR_INT;
  502. pmu_interrupt(IRQ_MAC_ADB_SR, NULL);
  503. }
  504. if (via1[IFR] & CB1_INT) {
  505. via1[IFR] = CB1_INT;
  506. pmu_interrupt(IRQ_MAC_ADB_CL, NULL);
  507. }
  508. local_irq_restore(flags);
  509. }
  510. static irqreturn_t
  511. pmu_interrupt(int irq, void *dev_id)
  512. {
  513. struct adb_request *req;
  514. int timeout, bite = 0; /* to prevent compiler warning */
  515. #if 0
  516. printk("pmu_interrupt: irq %d state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
  517. irq, pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
  518. #endif
  519. if (irq == IRQ_MAC_ADB_CL) { /* CB1 interrupt */
  520. adb_int_pending = 1;
  521. } else if (irq == IRQ_MAC_ADB_SR) { /* SR interrupt */
  522. if (via2[B] & TACK) {
  523. printk(KERN_DEBUG "PMU: SR_INT but ack still high! (%x)\n", via2[B]);
  524. }
  525. /* if reading grab the byte */
  526. if ((via1[ACR] & SR_OUT) == 0) bite = via1[SR];
  527. /* reset TREQ and wait for TACK to go high */
  528. via2[B] |= TREQ;
  529. timeout = 3200;
  530. while (!(via2[B] & TACK)) {
  531. if (--timeout < 0) {
  532. printk(KERN_ERR "PMU not responding (!ack)\n");
  533. goto finish;
  534. }
  535. udelay(10);
  536. }
  537. switch (pmu_state) {
  538. case sending:
  539. req = current_req;
  540. if (data_len < 0) {
  541. data_len = req->nbytes - 1;
  542. send_byte(data_len);
  543. break;
  544. }
  545. if (data_index <= data_len) {
  546. send_byte(req->data[data_index++]);
  547. break;
  548. }
  549. req->sent = 1;
  550. data_len = pmu_data_len[req->data[0]][1];
  551. if (data_len == 0) {
  552. pmu_state = idle;
  553. current_req = req->next;
  554. if (req->reply_expected)
  555. req_awaiting_reply = req;
  556. else
  557. pmu_done(req);
  558. } else {
  559. pmu_state = reading;
  560. data_index = 0;
  561. reply_ptr = req->reply + req->reply_len;
  562. recv_byte();
  563. }
  564. break;
  565. case intack:
  566. data_index = 0;
  567. data_len = -1;
  568. pmu_state = reading_intr;
  569. reply_ptr = interrupt_data;
  570. recv_byte();
  571. break;
  572. case reading:
  573. case reading_intr:
  574. if (data_len == -1) {
  575. data_len = bite;
  576. if (bite > 32)
  577. printk(KERN_ERR "PMU: bad reply len %d\n",
  578. bite);
  579. } else {
  580. reply_ptr[data_index++] = bite;
  581. }
  582. if (data_index < data_len) {
  583. recv_byte();
  584. break;
  585. }
  586. if (pmu_state == reading_intr) {
  587. pmu_handle_data(interrupt_data, data_index);
  588. } else {
  589. req = current_req;
  590. current_req = req->next;
  591. req->reply_len += data_index;
  592. pmu_done(req);
  593. }
  594. pmu_state = idle;
  595. break;
  596. default:
  597. printk(KERN_ERR "pmu_interrupt: unknown state %d?\n",
  598. pmu_state);
  599. }
  600. }
  601. finish:
  602. if (pmu_state == idle) {
  603. if (adb_int_pending) {
  604. pmu_state = intack;
  605. send_byte(PMU_INT_ACK);
  606. adb_int_pending = 0;
  607. } else if (current_req) {
  608. pmu_start();
  609. }
  610. }
  611. #if 0
  612. printk("pmu_interrupt: exit state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
  613. pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
  614. #endif
  615. return IRQ_HANDLED;
  616. }
  617. static void
  618. pmu_done(struct adb_request *req)
  619. {
  620. req->complete = 1;
  621. if (req->done)
  622. (*req->done)(req);
  623. }
  624. /* Interrupt data could be the result data from an ADB cmd */
  625. static void
  626. pmu_handle_data(unsigned char *data, int len)
  627. {
  628. static int show_pmu_ints = 1;
  629. asleep = 0;
  630. if (len < 1) {
  631. adb_int_pending = 0;
  632. return;
  633. }
  634. if (data[0] & PMU_INT_ADB) {
  635. if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
  636. struct adb_request *req = req_awaiting_reply;
  637. if (req == 0) {
  638. printk(KERN_ERR "PMU: extra ADB reply\n");
  639. return;
  640. }
  641. req_awaiting_reply = NULL;
  642. if (len <= 2)
  643. req->reply_len = 0;
  644. else {
  645. memcpy(req->reply, data + 1, len - 1);
  646. req->reply_len = len - 1;
  647. }
  648. pmu_done(req);
  649. } else {
  650. adb_input(data+1, len-1, 1);
  651. }
  652. } else {
  653. if (data[0] == 0x08 && len == 3) {
  654. /* sound/brightness buttons pressed */
  655. pmu_set_brightness(data[1] >> 3);
  656. set_volume(data[2]);
  657. } else if (show_pmu_ints
  658. && !(data[0] == PMU_INT_TICK && len == 1)) {
  659. int i;
  660. printk(KERN_DEBUG "pmu intr");
  661. for (i = 0; i < len; ++i)
  662. printk(" %.2x", data[i]);
  663. printk("\n");
  664. }
  665. }
  666. }
  667. int backlight_level = -1;
  668. int backlight_enabled = 0;
  669. #define LEVEL_TO_BRIGHT(lev) ((lev) < 1? 0x7f: 0x4a - ((lev) << 1))
  670. static void
  671. pmu_enable_backlight(int on)
  672. {
  673. struct adb_request req;
  674. if (on) {
  675. /* first call: get current backlight value */
  676. if (backlight_level < 0) {
  677. switch(pmu_kind) {
  678. case PMU_68K_V1:
  679. case PMU_68K_V2:
  680. pmu_request(&req, NULL, 3, PMU_READ_NVRAM, 0x14, 0xe);
  681. while (!req.complete)
  682. pmu_poll();
  683. printk(KERN_DEBUG "pmu: nvram returned bright: %d\n", (int)req.reply[1]);
  684. backlight_level = req.reply[1];
  685. break;
  686. default:
  687. backlight_enabled = 0;
  688. return;
  689. }
  690. }
  691. pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
  692. LEVEL_TO_BRIGHT(backlight_level));
  693. while (!req.complete)
  694. pmu_poll();
  695. }
  696. pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  697. PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
  698. while (!req.complete)
  699. pmu_poll();
  700. backlight_enabled = on;
  701. }
  702. static void
  703. pmu_set_brightness(int level)
  704. {
  705. int bright;
  706. backlight_level = level;
  707. bright = LEVEL_TO_BRIGHT(level);
  708. if (!backlight_enabled)
  709. return;
  710. if (bright_req_1.complete)
  711. pmu_request(&bright_req_1, NULL, 2, PMU_BACKLIGHT_BRIGHT,
  712. bright);
  713. if (bright_req_2.complete)
  714. pmu_request(&bright_req_2, NULL, 2, PMU_POWER_CTRL,
  715. PMU_POW_BACKLIGHT | (bright < 0x7f ? PMU_POW_ON : PMU_POW_OFF));
  716. }
  717. void
  718. pmu_enable_irled(int on)
  719. {
  720. struct adb_request req;
  721. pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
  722. (on ? PMU_POW_ON : PMU_POW_OFF));
  723. while (!req.complete)
  724. pmu_poll();
  725. }
  726. static void
  727. set_volume(int level)
  728. {
  729. }
  730. int
  731. pmu_present(void)
  732. {
  733. return (pmu_kind != PMU_UNKNOWN);
  734. }
  735. #if 0 /* needs some work for 68K */
  736. /*
  737. * This struct is used to store config register values for
  738. * PCI devices which may get powered off when we sleep.
  739. */
  740. static struct pci_save {
  741. u16 command;
  742. u16 cache_lat;
  743. u16 intr;
  744. } *pbook_pci_saves;
  745. static int n_pbook_pci_saves;
  746. static inline void
  747. pbook_pci_save(void)
  748. {
  749. int npci;
  750. struct pci_dev *pd = NULL;
  751. struct pci_save *ps;
  752. npci = 0;
  753. while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL)
  754. ++npci;
  755. n_pbook_pci_saves = npci;
  756. if (npci == 0)
  757. return;
  758. ps = (struct pci_save *) kmalloc(npci * sizeof(*ps), GFP_KERNEL);
  759. pbook_pci_saves = ps;
  760. if (ps == NULL)
  761. return;
  762. pd = NULL;
  763. while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
  764. pci_read_config_word(pd, PCI_COMMAND, &ps->command);
  765. pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
  766. pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
  767. ++ps;
  768. --npci;
  769. }
  770. }
  771. static inline void
  772. pbook_pci_restore(void)
  773. {
  774. u16 cmd;
  775. struct pci_save *ps = pbook_pci_saves;
  776. struct pci_dev *pd = NULL;
  777. int j;
  778. while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
  779. if (ps->command == 0)
  780. continue;
  781. pci_read_config_word(pd, PCI_COMMAND, &cmd);
  782. if ((ps->command & ~cmd) == 0)
  783. continue;
  784. switch (pd->hdr_type) {
  785. case PCI_HEADER_TYPE_NORMAL:
  786. for (j = 0; j < 6; ++j)
  787. pci_write_config_dword(pd,
  788. PCI_BASE_ADDRESS_0 + j*4,
  789. pd->resource[j].start);
  790. pci_write_config_dword(pd, PCI_ROM_ADDRESS,
  791. pd->resource[PCI_ROM_RESOURCE].start);
  792. pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
  793. ps->cache_lat);
  794. pci_write_config_word(pd, PCI_INTERRUPT_LINE,
  795. ps->intr);
  796. pci_write_config_word(pd, PCI_COMMAND, ps->command);
  797. break;
  798. /* other header types not restored at present */
  799. }
  800. }
  801. }
  802. /*
  803. * Put the powerbook to sleep.
  804. */
  805. #define IRQ_ENABLE ((unsigned int *)0xf3000024)
  806. #define MEM_CTRL ((unsigned int *)0xf8000070)
  807. int powerbook_sleep(void)
  808. {
  809. int ret, i, x;
  810. static int save_backlight;
  811. static unsigned int save_irqen;
  812. unsigned long msr;
  813. unsigned int hid0;
  814. unsigned long p, wait;
  815. struct adb_request sleep_req;
  816. /* Notify device drivers */
  817. ret = blocking_notifier_call_chain(&sleep_notifier_list,
  818. PBOOK_SLEEP, NULL);
  819. if (ret & NOTIFY_STOP_MASK)
  820. return -EBUSY;
  821. /* Sync the disks. */
  822. /* XXX It would be nice to have some way to ensure that
  823. * nobody is dirtying any new buffers while we wait. */
  824. sys_sync();
  825. /* Turn off the display backlight */
  826. save_backlight = backlight_enabled;
  827. if (save_backlight)
  828. pmu_enable_backlight(0);
  829. /* Give the disks a little time to actually finish writing */
  830. for (wait = jiffies + (HZ/4); time_before(jiffies, wait); )
  831. mb();
  832. /* Disable all interrupts except pmu */
  833. save_irqen = in_le32(IRQ_ENABLE);
  834. for (i = 0; i < 32; ++i)
  835. if (i != vias->intrs[0].line && (save_irqen & (1 << i)))
  836. disable_irq(i);
  837. asm volatile("mtdec %0" : : "r" (0x7fffffff));
  838. /* Save the state of PCI config space for some slots */
  839. pbook_pci_save();
  840. /* Set the memory controller to keep the memory refreshed
  841. while we're asleep */
  842. for (i = 0x403f; i >= 0x4000; --i) {
  843. out_be32(MEM_CTRL, i);
  844. do {
  845. x = (in_be32(MEM_CTRL) >> 16) & 0x3ff;
  846. } while (x == 0);
  847. if (x >= 0x100)
  848. break;
  849. }
  850. /* Ask the PMU to put us to sleep */
  851. pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
  852. while (!sleep_req.complete)
  853. mb();
  854. /* displacement-flush the L2 cache - necessary? */
  855. for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
  856. i = *(volatile int *)p;
  857. asleep = 1;
  858. /* Put the CPU into sleep mode */
  859. asm volatile("mfspr %0,1008" : "=r" (hid0) :);
  860. hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
  861. asm volatile("mtspr 1008,%0" : : "r" (hid0));
  862. local_save_flags(msr);
  863. msr |= MSR_POW | MSR_EE;
  864. local_irq_restore(msr);
  865. udelay(10);
  866. /* OK, we're awake again, start restoring things */
  867. out_be32(MEM_CTRL, 0x3f);
  868. pbook_pci_restore();
  869. /* wait for the PMU interrupt sequence to complete */
  870. while (asleep)
  871. mb();
  872. /* reenable interrupts */
  873. for (i = 0; i < 32; ++i)
  874. if (i != vias->intrs[0].line && (save_irqen & (1 << i)))
  875. enable_irq(i);
  876. /* Notify drivers */
  877. blocking_notifier_call_chain(&sleep_notifier_list, PBOOK_WAKE, NULL);
  878. /* reenable ADB autopoll */
  879. pmu_adb_autopoll(adb_dev_map);
  880. /* Turn on the screen backlight, if it was on before */
  881. if (save_backlight)
  882. pmu_enable_backlight(1);
  883. /* Wait for the hard disk to spin up */
  884. return 0;
  885. }
  886. /*
  887. * Support for /dev/pmu device
  888. */
  889. static int pmu_open(struct inode *inode, struct file *file)
  890. {
  891. return 0;
  892. }
  893. static ssize_t pmu_read(struct file *file, char *buf,
  894. size_t count, loff_t *ppos)
  895. {
  896. return 0;
  897. }
  898. static ssize_t pmu_write(struct file *file, const char *buf,
  899. size_t count, loff_t *ppos)
  900. {
  901. return 0;
  902. }
  903. static int pmu_ioctl(struct inode * inode, struct file *filp,
  904. u_int cmd, u_long arg)
  905. {
  906. int error;
  907. __u32 value;
  908. switch (cmd) {
  909. case PMU_IOC_SLEEP:
  910. return -ENOSYS;
  911. case PMU_IOC_GET_BACKLIGHT:
  912. return put_user(backlight_level, (__u32 *)arg);
  913. case PMU_IOC_SET_BACKLIGHT:
  914. error = get_user(value, (__u32 *)arg);
  915. if (!error)
  916. pmu_set_brightness(value);
  917. return error;
  918. case PMU_IOC_GET_MODEL:
  919. return put_user(pmu_kind, (__u32 *)arg);
  920. }
  921. return -EINVAL;
  922. }
  923. static struct file_operations pmu_device_fops = {
  924. .read = pmu_read,
  925. .write = pmu_write,
  926. .ioctl = pmu_ioctl,
  927. .open = pmu_open,
  928. };
  929. static struct miscdevice pmu_device = {
  930. PMU_MINOR, "pmu", &pmu_device_fops
  931. };
  932. void pmu_device_init(void)
  933. {
  934. if (!via)
  935. return;
  936. if (misc_register(&pmu_device) < 0)
  937. printk(KERN_ERR "via-pmu68k: cannot register misc device.\n");
  938. }
  939. #endif /* CONFIG_PMAC_PBOOK */