adb-iop.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * I/O Processor (IOP) ADB Driver
  4. * Written and (C) 1999 by Joshua M. Thompson (funaho@jurai.org)
  5. * Based on via-cuda.c by Paul Mackerras.
  6. *
  7. * 1999-07-01 (jmt) - First implementation for new driver architecture.
  8. *
  9. * 1999-07-31 (jmt) - First working version.
  10. *
  11. * TODO:
  12. *
  13. * o Implement SRQ handling.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/proc_fs.h>
  21. #include <asm/macintosh.h>
  22. #include <asm/macints.h>
  23. #include <asm/mac_iop.h>
  24. #include <asm/mac_oss.h>
  25. #include <asm/adb_iop.h>
  26. #include <linux/adb.h>
  27. /*#define DEBUG_ADB_IOP*/
  28. extern void iop_ism_irq(int, void *);
  29. static struct adb_request *current_req;
  30. static struct adb_request *last_req;
  31. #if 0
  32. static unsigned char reply_buff[16];
  33. static unsigned char *reply_ptr;
  34. #endif
  35. static enum adb_iop_state {
  36. idle,
  37. sending,
  38. awaiting_reply
  39. } adb_iop_state;
  40. static void adb_iop_start(void);
  41. static int adb_iop_probe(void);
  42. static int adb_iop_init(void);
  43. static int adb_iop_send_request(struct adb_request *, int);
  44. static int adb_iop_write(struct adb_request *);
  45. static int adb_iop_autopoll(int);
  46. static void adb_iop_poll(void);
  47. static int adb_iop_reset_bus(void);
  48. struct adb_driver adb_iop_driver = {
  49. "ISM IOP",
  50. adb_iop_probe,
  51. adb_iop_init,
  52. adb_iop_send_request,
  53. adb_iop_autopoll,
  54. adb_iop_poll,
  55. adb_iop_reset_bus
  56. };
  57. static void adb_iop_end_req(struct adb_request *req, int state)
  58. {
  59. req->complete = 1;
  60. current_req = req->next;
  61. if (req->done) (*req->done)(req);
  62. adb_iop_state = state;
  63. }
  64. /*
  65. * Completion routine for ADB commands sent to the IOP.
  66. *
  67. * This will be called when a packet has been successfully sent.
  68. */
  69. static void adb_iop_complete(struct iop_msg *msg)
  70. {
  71. struct adb_request *req;
  72. unsigned long flags;
  73. local_irq_save(flags);
  74. req = current_req;
  75. if ((adb_iop_state == sending) && req && req->reply_expected) {
  76. adb_iop_state = awaiting_reply;
  77. }
  78. local_irq_restore(flags);
  79. }
  80. /*
  81. * Listen for ADB messages from the IOP.
  82. *
  83. * This will be called when unsolicited messages (usually replies to TALK
  84. * commands or autopoll packets) are received.
  85. */
  86. static void adb_iop_listen(struct iop_msg *msg)
  87. {
  88. struct adb_iopmsg *amsg = (struct adb_iopmsg *) msg->message;
  89. struct adb_request *req;
  90. unsigned long flags;
  91. #ifdef DEBUG_ADB_IOP
  92. int i;
  93. #endif
  94. local_irq_save(flags);
  95. req = current_req;
  96. #ifdef DEBUG_ADB_IOP
  97. printk("adb_iop_listen %p: rcvd packet, %d bytes: %02X %02X", req,
  98. (uint) amsg->count + 2, (uint) amsg->flags, (uint) amsg->cmd);
  99. for (i = 0; i < amsg->count; i++)
  100. printk(" %02X", (uint) amsg->data[i]);
  101. printk("\n");
  102. #endif
  103. /* Handle a timeout. Timeout packets seem to occur even after */
  104. /* we've gotten a valid reply to a TALK, so I'm assuming that */
  105. /* a "timeout" is actually more like an "end-of-data" signal. */
  106. /* We need to send back a timeout packet to the IOP to shut */
  107. /* it up, plus complete the current request, if any. */
  108. if (amsg->flags & ADB_IOP_TIMEOUT) {
  109. msg->reply[0] = ADB_IOP_TIMEOUT | ADB_IOP_AUTOPOLL;
  110. msg->reply[1] = 0;
  111. msg->reply[2] = 0;
  112. if (req && (adb_iop_state != idle)) {
  113. adb_iop_end_req(req, idle);
  114. }
  115. } else {
  116. /* TODO: is it possible for more than one chunk of data */
  117. /* to arrive before the timeout? If so we need to */
  118. /* use reply_ptr here like the other drivers do. */
  119. if ((adb_iop_state == awaiting_reply) &&
  120. (amsg->flags & ADB_IOP_EXPLICIT)) {
  121. req->reply_len = amsg->count + 1;
  122. memcpy(req->reply, &amsg->cmd, req->reply_len);
  123. } else {
  124. adb_input(&amsg->cmd, amsg->count + 1,
  125. amsg->flags & ADB_IOP_AUTOPOLL);
  126. }
  127. memcpy(msg->reply, msg->message, IOP_MSG_LEN);
  128. }
  129. iop_complete_message(msg);
  130. local_irq_restore(flags);
  131. }
  132. /*
  133. * Start sending an ADB packet, IOP style
  134. *
  135. * There isn't much to do other than hand the packet over to the IOP
  136. * after encapsulating it in an adb_iopmsg.
  137. */
  138. static void adb_iop_start(void)
  139. {
  140. unsigned long flags;
  141. struct adb_request *req;
  142. struct adb_iopmsg amsg;
  143. #ifdef DEBUG_ADB_IOP
  144. int i;
  145. #endif
  146. /* get the packet to send */
  147. req = current_req;
  148. if (!req) return;
  149. local_irq_save(flags);
  150. #ifdef DEBUG_ADB_IOP
  151. printk("adb_iop_start %p: sending packet, %d bytes:", req, req->nbytes);
  152. for (i = 0 ; i < req->nbytes ; i++)
  153. printk(" %02X", (uint) req->data[i]);
  154. printk("\n");
  155. #endif
  156. /* The IOP takes MacII-style packets, so */
  157. /* strip the initial ADB_PACKET byte. */
  158. amsg.flags = ADB_IOP_EXPLICIT;
  159. amsg.count = req->nbytes - 2;
  160. /* amsg.data immediately follows amsg.cmd, effectively making */
  161. /* amsg.cmd a pointer to the beginning of a full ADB packet. */
  162. memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1);
  163. req->sent = 1;
  164. adb_iop_state = sending;
  165. local_irq_restore(flags);
  166. /* Now send it. The IOP manager will call adb_iop_complete */
  167. /* when the packet has been sent. */
  168. iop_send_message(ADB_IOP, ADB_CHAN, req,
  169. sizeof(amsg), (__u8 *) &amsg, adb_iop_complete);
  170. }
  171. int adb_iop_probe(void)
  172. {
  173. if (!iop_ism_present) return -ENODEV;
  174. return 0;
  175. }
  176. int adb_iop_init(void)
  177. {
  178. printk("adb: IOP ISM driver v0.4 for Unified ADB.\n");
  179. iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB");
  180. return 0;
  181. }
  182. int adb_iop_send_request(struct adb_request *req, int sync)
  183. {
  184. int err;
  185. err = adb_iop_write(req);
  186. if (err) return err;
  187. if (sync) {
  188. while (!req->complete) adb_iop_poll();
  189. }
  190. return 0;
  191. }
  192. static int adb_iop_write(struct adb_request *req)
  193. {
  194. unsigned long flags;
  195. if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) {
  196. req->complete = 1;
  197. return -EINVAL;
  198. }
  199. local_irq_save(flags);
  200. req->next = NULL;
  201. req->sent = 0;
  202. req->complete = 0;
  203. req->reply_len = 0;
  204. if (current_req != 0) {
  205. last_req->next = req;
  206. last_req = req;
  207. } else {
  208. current_req = req;
  209. last_req = req;
  210. }
  211. local_irq_restore(flags);
  212. if (adb_iop_state == idle) adb_iop_start();
  213. return 0;
  214. }
  215. int adb_iop_autopoll(int devs)
  216. {
  217. /* TODO: how do we enable/disable autopoll? */
  218. return 0;
  219. }
  220. void adb_iop_poll(void)
  221. {
  222. if (adb_iop_state == idle) adb_iop_start();
  223. iop_ism_irq(0, (void *) ADB_IOP);
  224. }
  225. int adb_iop_reset_bus(void)
  226. {
  227. struct adb_request req = {
  228. .reply_expected = 0,
  229. .nbytes = 2,
  230. .data = { ADB_PACKET, 0 },
  231. };
  232. adb_iop_write(&req);
  233. while (!req.complete) {
  234. adb_iop_poll();
  235. schedule();
  236. }
  237. return 0;
  238. }