via-pmu68k.c 25 KB

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