macio-adb.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/spinlock.h>
  10. #include <linux/interrupt.h>
  11. #include <asm/prom.h>
  12. #include <linux/adb.h>
  13. #include <asm/io.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/hydra.h>
  16. #include <asm/irq.h>
  17. #include <asm/system.h>
  18. #include <linux/init.h>
  19. #include <linux/ioport.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);
  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. struct resource r;
  80. unsigned int irq;
  81. adbs = find_compatible_devices("adb", "chrp,adb0");
  82. if (adbs == 0)
  83. return -ENXIO;
  84. if (of_address_to_resource(adbs, 0, &r))
  85. return -ENXIO;
  86. adb = ioremap(r.start, sizeof(struct adb_regs));
  87. out_8(&adb->ctrl.r, 0);
  88. out_8(&adb->intr.r, 0);
  89. out_8(&adb->error.r, 0);
  90. out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
  91. out_8(&adb->active_lo.r, 0xff);
  92. out_8(&adb->autopoll.r, APE);
  93. irq = irq_of_parse_and_map(adbs, 0);
  94. if (request_irq(irq, macio_adb_interrupt, 0, "ADB", (void *)0)) {
  95. printk(KERN_ERR "ADB: can't get irq %d\n", irq);
  96. return -EAGAIN;
  97. }
  98. out_8(&adb->intr_enb.r, DFB | TAG);
  99. printk("adb: mac-io driver 1.0 for unified ADB\n");
  100. return 0;
  101. }
  102. static int macio_adb_autopoll(int devs)
  103. {
  104. unsigned long flags;
  105. spin_lock_irqsave(&macio_lock, flags);
  106. out_8(&adb->active_hi.r, devs >> 8);
  107. out_8(&adb->active_lo.r, devs);
  108. out_8(&adb->autopoll.r, devs? APE: 0);
  109. spin_unlock_irqrestore(&macio_lock, flags);
  110. return 0;
  111. }
  112. static int macio_adb_reset_bus(void)
  113. {
  114. unsigned long flags;
  115. int timeout = 1000000;
  116. /* Hrm... we may want to not lock interrupts for so
  117. * long ... oh well, who uses that chip anyway ? :)
  118. * That function will be seldomly used during boot
  119. * on rare machines, so...
  120. */
  121. spin_lock_irqsave(&macio_lock, flags);
  122. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
  123. while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
  124. if (--timeout == 0) {
  125. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
  126. return -1;
  127. }
  128. }
  129. spin_unlock_irqrestore(&macio_lock, flags);
  130. return 0;
  131. }
  132. /* Send an ADB command */
  133. static int macio_send_request(struct adb_request *req, int sync)
  134. {
  135. unsigned long flags;
  136. int i;
  137. if (req->data[0] != ADB_PACKET)
  138. return -EINVAL;
  139. for (i = 0; i < req->nbytes - 1; ++i)
  140. req->data[i] = req->data[i+1];
  141. --req->nbytes;
  142. req->next = NULL;
  143. req->sent = 0;
  144. req->complete = 0;
  145. req->reply_len = 0;
  146. spin_lock_irqsave(&macio_lock, flags);
  147. if (current_req != 0) {
  148. last_req->next = req;
  149. last_req = req;
  150. } else {
  151. current_req = last_req = req;
  152. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
  153. }
  154. spin_unlock_irqrestore(&macio_lock, flags);
  155. if (sync) {
  156. while (!req->complete)
  157. macio_adb_poll();
  158. }
  159. return 0;
  160. }
  161. static irqreturn_t macio_adb_interrupt(int irq, void *arg)
  162. {
  163. int i, n, err;
  164. struct adb_request *req = NULL;
  165. unsigned char ibuf[16];
  166. int ibuf_len = 0;
  167. int complete = 0;
  168. int autopoll = 0;
  169. int handled = 0;
  170. spin_lock(&macio_lock);
  171. if (in_8(&adb->intr.r) & TAG) {
  172. handled = 1;
  173. if ((req = current_req) != 0) {
  174. /* put the current request in */
  175. for (i = 0; i < req->nbytes; ++i)
  176. out_8(&adb->data[i].r, req->data[i]);
  177. out_8(&adb->dcount.r, req->nbytes & HMB);
  178. req->sent = 1;
  179. if (req->reply_expected) {
  180. out_8(&adb->ctrl.r, DTB + CRE);
  181. } else {
  182. out_8(&adb->ctrl.r, DTB);
  183. current_req = req->next;
  184. complete = 1;
  185. if (current_req)
  186. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
  187. }
  188. }
  189. out_8(&adb->intr.r, 0);
  190. }
  191. if (in_8(&adb->intr.r) & DFB) {
  192. handled = 1;
  193. err = in_8(&adb->error.r);
  194. if (current_req && current_req->sent) {
  195. /* this is the response to a command */
  196. req = current_req;
  197. if (err == 0) {
  198. req->reply_len = in_8(&adb->dcount.r) & HMB;
  199. for (i = 0; i < req->reply_len; ++i)
  200. req->reply[i] = in_8(&adb->data[i].r);
  201. }
  202. current_req = req->next;
  203. complete = 1;
  204. if (current_req)
  205. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
  206. } else if (err == 0) {
  207. /* autopoll data */
  208. n = in_8(&adb->dcount.r) & HMB;
  209. for (i = 0; i < n; ++i)
  210. ibuf[i] = in_8(&adb->data[i].r);
  211. ibuf_len = n;
  212. autopoll = (in_8(&adb->dcount.r) & APD) != 0;
  213. }
  214. out_8(&adb->error.r, 0);
  215. out_8(&adb->intr.r, 0);
  216. }
  217. spin_unlock(&macio_lock);
  218. if (complete && req) {
  219. void (*done)(struct adb_request *) = req->done;
  220. mb();
  221. req->complete = 1;
  222. /* Here, we assume that if the request has a done member, the
  223. * struct request will survive to setting req->complete to 1
  224. */
  225. if (done)
  226. (*done)(req);
  227. }
  228. if (ibuf_len)
  229. adb_input(ibuf, ibuf_len, autopoll);
  230. return IRQ_RETVAL(handled);
  231. }
  232. static void macio_adb_poll(void)
  233. {
  234. unsigned long flags;
  235. local_irq_save(flags);
  236. if (in_8(&adb->intr.r) != 0)
  237. macio_adb_interrupt(0, NULL);
  238. local_irq_restore(flags);
  239. }