via-pmu68k.c 25 KB

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