macio-adb.c 6.5 KB

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