macio-adb.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 __free(device_node) =
  75. of_find_compatible_node(NULL, "adb", "chrp,adb0");
  76. if (np)
  77. return 0;
  78. return -ENODEV;
  79. }
  80. int macio_init(void)
  81. {
  82. struct device_node *adbs __free(device_node) =
  83. of_find_compatible_node(NULL, "adb", "chrp,adb0");
  84. struct resource r;
  85. unsigned int irq;
  86. if (!adbs)
  87. return -ENXIO;
  88. if (of_address_to_resource(adbs, 0, &r))
  89. return -ENXIO;
  90. adb = ioremap(r.start, sizeof(struct adb_regs));
  91. if (!adb)
  92. return -ENOMEM;
  93. out_8(&adb->ctrl.r, 0);
  94. out_8(&adb->intr.r, 0);
  95. out_8(&adb->error.r, 0);
  96. out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
  97. out_8(&adb->active_lo.r, 0xff);
  98. out_8(&adb->autopoll.r, APE);
  99. irq = irq_of_parse_and_map(adbs, 0);
  100. if (request_irq(irq, macio_adb_interrupt, 0, "ADB", (void *)0)) {
  101. iounmap(adb);
  102. printk(KERN_ERR "ADB: can't get irq %d\n", irq);
  103. return -EAGAIN;
  104. }
  105. out_8(&adb->intr_enb.r, DFB | TAG);
  106. printk("adb: mac-io driver 1.0 for unified ADB\n");
  107. return 0;
  108. }
  109. static int macio_adb_autopoll(int devs)
  110. {
  111. unsigned long flags;
  112. spin_lock_irqsave(&macio_lock, flags);
  113. out_8(&adb->active_hi.r, devs >> 8);
  114. out_8(&adb->active_lo.r, devs);
  115. out_8(&adb->autopoll.r, devs? APE: 0);
  116. spin_unlock_irqrestore(&macio_lock, flags);
  117. return 0;
  118. }
  119. static int macio_adb_reset_bus(void)
  120. {
  121. unsigned long flags;
  122. int timeout = 1000000;
  123. /* Hrm... we may want to not lock interrupts for so
  124. * long ... oh well, who uses that chip anyway ? :)
  125. * That function will be seldom used during boot
  126. * on rare machines, so...
  127. */
  128. spin_lock_irqsave(&macio_lock, flags);
  129. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
  130. while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
  131. if (--timeout == 0) {
  132. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
  133. spin_unlock_irqrestore(&macio_lock, flags);
  134. return -1;
  135. }
  136. }
  137. spin_unlock_irqrestore(&macio_lock, flags);
  138. return 0;
  139. }
  140. /* Send an ADB command */
  141. static int macio_send_request(struct adb_request *req, int sync)
  142. {
  143. unsigned long flags;
  144. int i;
  145. if (req->data[0] != ADB_PACKET)
  146. return -EINVAL;
  147. for (i = 0; i < req->nbytes - 1; ++i)
  148. req->data[i] = req->data[i+1];
  149. --req->nbytes;
  150. req->next = NULL;
  151. req->sent = 0;
  152. req->complete = 0;
  153. req->reply_len = 0;
  154. spin_lock_irqsave(&macio_lock, flags);
  155. if (current_req) {
  156. last_req->next = req;
  157. last_req = req;
  158. } else {
  159. current_req = last_req = req;
  160. out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
  161. }
  162. spin_unlock_irqrestore(&macio_lock, flags);
  163. if (sync) {
  164. while (!req->complete)
  165. macio_adb_poll();
  166. }
  167. return 0;
  168. }
  169. static irqreturn_t macio_adb_interrupt(int irq, void *arg)
  170. {
  171. int i, n, err;
  172. struct adb_request *req = NULL;
  173. unsigned char ibuf[16];
  174. int ibuf_len = 0;
  175. int complete = 0;
  176. int autopoll = 0;
  177. int handled = 0;
  178. spin_lock(&macio_lock);
  179. if (in_8(&adb->intr.r) & TAG) {
  180. handled = 1;
  181. req = current_req;
  182. if (req) {
  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. }