macio-adb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Driver for the ADB controller in the Mac I/O (Hydra) chip.
  3. */
  4. #include <stdarg.h>
  5. #include <linux/types.h>
  6. #include <linux/errno.h>
  7. #include <linux/kernel.h>
  8. #include <linux/delay.h>
  9. #include <linux/sched.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/interrupt.h>
  12. #include <asm/prom.h>
  13. #include <linux/adb.h>
  14. #include <asm/io.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/hydra.h>
  17. #include <asm/irq.h>
  18. #include <asm/system.h>
  19. #include <linux/init.h>
  20. struct preg {
  21. unsigned char r;
  22. char pad[15];
  23. };
  24. struct adb_regs {
  25. struct preg intr;
  26. struct preg data[9];
  27. struct preg intr_enb;
  28. struct preg dcount;
  29. struct preg error;
  30. struct preg ctrl;
  31. struct preg autopoll;
  32. struct preg active_hi;
  33. struct preg active_lo;
  34. struct preg test;
  35. };
  36. /* Bits in intr and intr_enb registers */
  37. #define DFB 1 /* data from bus */
  38. #define TAG 2 /* transfer access grant */
  39. /* Bits in dcount register */
  40. #define HMB 0x0f /* how many bytes */
  41. #define APD 0x10 /* auto-poll data */
  42. /* Bits in error register */
  43. #define NRE 1 /* no response error */
  44. #define DLE 2 /* data lost error */
  45. /* Bits in ctrl register */
  46. #define TAR 1 /* transfer access request */
  47. #define DTB 2 /* data to bus */
  48. #define CRE 4 /* command response expected */
  49. #define ADB_RST 8 /* ADB reset */
  50. /* Bits in autopoll register */
  51. #define APE 1 /* autopoll enable */
  52. static volatile struct adb_regs __iomem *adb;
  53. static struct adb_request *current_req, *last_req;
  54. static DEFINE_SPINLOCK(macio_lock);
  55. static int macio_probe(void);
  56. static int macio_init(void);
  57. static irqreturn_t macio_adb_interrupt(int irq, void *arg, struct pt_regs *regs);
  58. static int macio_send_request(struct adb_request *req, int sync);
  59. static int macio_adb_autopoll(int devs);
  60. static void macio_adb_poll(void);
  61. static int macio_adb_reset_bus(void);
  62. struct adb_driver macio_adb_driver = {
  63. "MACIO",
  64. macio_probe,
  65. macio_init,
  66. macio_send_request,
  67. /*macio_write,*/
  68. macio_adb_autopoll,
  69. macio_adb_poll,
  70. macio_adb_reset_bus
  71. };
  72. int macio_probe(void)
  73. {
  74. return find_compatible_devices("adb", "chrp,adb0")? 0: -ENODEV;
  75. }
  76. int macio_init(void)
  77. {
  78. struct device_node *adbs;
  79. adbs = find_compatible_devices("adb", "chrp,adb0");
  80. if (adbs == 0)
  81. return -ENXIO;
  82. #if 0
  83. { int i;
  84. printk("macio_adb_init: node = %p, addrs =", adbs->node);
  85. for (i = 0; i < adbs->n_addrs; ++i)
  86. printk(" %x(%x)", adbs->addrs[i].address, adbs->addrs[i].size);
  87. printk(", intrs =");
  88. for (i = 0; i < adbs->n_intrs; ++i)
  89. printk(" %x", adbs->intrs[i].line);
  90. printk("\n"); }
  91. #endif
  92. adb = ioremap(adbs->addrs->address, sizeof(struct adb_regs));
  93. out_8(&adb->ctrl.r, 0);
  94. out_8(&adb->intr.r, 0);
  95. out_8(&adb->error.r, 0);
  96. out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
  97. out_8(&adb->active_lo.r, 0xff);
  98. out_8(&adb->autopoll.r, APE);
  99. if (request_irq(adbs->intrs[0].line, macio_adb_interrupt,
  100. 0, "ADB", (void *)0)) {
  101. printk(KERN_ERR "ADB: can't get irq %d\n",
  102. adbs->intrs[0].line);
  103. return -EAGAIN;
  104. }
  105. out_8(&adb->intr_enb.r, DFB | TAG);
  106. printk("adb: mac-io driver 1.0 for unified ADB\n");
  107. return 0;
  108. }
  109. static int macio_adb_autopoll(int devs)
  110. {
  111. unsigned long flags;
  112. spin_lock_irqsave(&macio_lock, flags);
  113. out_8(&adb->active_hi.r, devs >> 8);
  114. out_8(&adb->active_lo.r, devs);
  115. out_8(&adb->autopoll.r, devs? APE: 0);
  116. spin_unlock_irqrestore(&macio_lock, flags);
  117. return 0;
  118. }
  119. static int macio_adb_reset_bus(void)
  120. {
  121. unsigned long flags;
  122. int timeout = 1000000;
  123. /* Hrm... we may want to not lock interrupts for so
  124. * long ... oh well, who uses that chip anyway ? :)
  125. * That function will be seldomly used during boot
  126. * on rare machines, so...
  127. */
  128. spin_lock_irqsave(&macio_lock, flags);
  129. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
  130. while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
  131. if (--timeout == 0) {
  132. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
  133. return -1;
  134. }
  135. }
  136. spin_unlock_irqrestore(&macio_lock, flags);
  137. return 0;
  138. }
  139. /* Send an ADB command */
  140. static int macio_send_request(struct adb_request *req, int sync)
  141. {
  142. unsigned long flags;
  143. int i;
  144. if (req->data[0] != ADB_PACKET)
  145. return -EINVAL;
  146. for (i = 0; i < req->nbytes - 1; ++i)
  147. req->data[i] = req->data[i+1];
  148. --req->nbytes;
  149. req->next = NULL;
  150. req->sent = 0;
  151. req->complete = 0;
  152. req->reply_len = 0;
  153. spin_lock_irqsave(&macio_lock, flags);
  154. if (current_req != 0) {
  155. last_req->next = req;
  156. last_req = req;
  157. } else {
  158. current_req = last_req = req;
  159. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
  160. }
  161. spin_unlock_irqrestore(&macio_lock, flags);
  162. if (sync) {
  163. while (!req->complete)
  164. macio_adb_poll();
  165. }
  166. return 0;
  167. }
  168. static irqreturn_t macio_adb_interrupt(int irq, void *arg,
  169. struct pt_regs *regs)
  170. {
  171. int i, n, err;
  172. struct adb_request *req = NULL;
  173. unsigned char ibuf[16];
  174. int ibuf_len = 0;
  175. int complete = 0;
  176. int autopoll = 0;
  177. int handled = 0;
  178. spin_lock(&macio_lock);
  179. if (in_8(&adb->intr.r) & TAG) {
  180. handled = 1;
  181. if ((req = current_req) != 0) {
  182. /* put the current request in */
  183. for (i = 0; i < req->nbytes; ++i)
  184. out_8(&adb->data[i].r, req->data[i]);
  185. out_8(&adb->dcount.r, req->nbytes & HMB);
  186. req->sent = 1;
  187. if (req->reply_expected) {
  188. out_8(&adb->ctrl.r, DTB + CRE);
  189. } else {
  190. out_8(&adb->ctrl.r, DTB);
  191. current_req = req->next;
  192. complete = 1;
  193. if (current_req)
  194. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
  195. }
  196. }
  197. out_8(&adb->intr.r, 0);
  198. }
  199. if (in_8(&adb->intr.r) & DFB) {
  200. handled = 1;
  201. err = in_8(&adb->error.r);
  202. if (current_req && current_req->sent) {
  203. /* this is the response to a command */
  204. req = current_req;
  205. if (err == 0) {
  206. req->reply_len = in_8(&adb->dcount.r) & HMB;
  207. for (i = 0; i < req->reply_len; ++i)
  208. req->reply[i] = in_8(&adb->data[i].r);
  209. }
  210. current_req = req->next;
  211. complete = 1;
  212. if (current_req)
  213. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
  214. } else if (err == 0) {
  215. /* autopoll data */
  216. n = in_8(&adb->dcount.r) & HMB;
  217. for (i = 0; i < n; ++i)
  218. ibuf[i] = in_8(&adb->data[i].r);
  219. ibuf_len = n;
  220. autopoll = (in_8(&adb->dcount.r) & APD) != 0;
  221. }
  222. out_8(&adb->error.r, 0);
  223. out_8(&adb->intr.r, 0);
  224. }
  225. spin_unlock(&macio_lock);
  226. if (complete && req) {
  227. void (*done)(struct adb_request *) = req->done;
  228. mb();
  229. req->complete = 1;
  230. /* Here, we assume that if the request has a done member, the
  231. * struct request will survive to setting req->complete to 1
  232. */
  233. if (done)
  234. (*done)(req);
  235. }
  236. if (ibuf_len)
  237. adb_input(ibuf, ibuf_len, regs, autopoll);
  238. return IRQ_RETVAL(handled);
  239. }
  240. static void macio_adb_poll(void)
  241. {
  242. unsigned long flags;
  243. local_irq_save(flags);
  244. if (in_8(&adb->intr.r) != 0)
  245. macio_adb_interrupt(0, NULL, NULL);
  246. local_irq_restore(flags);
  247. }