macio-adb.c 6.3 KB

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