macio-adb.c 6.6 KB

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