macio-adb.c 6.5 KB

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