via-pmu68k.c 25 KB

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