macio-adb.c 6.6 KB

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