2 * I/O Processor (IOP) ADB Driver
3 * Written and (C) 1999 by Joshua M. Thompson (funaho@jurai.org)
4 * Based on via-cuda.c by Paul Mackerras.
6 * 1999-07-01 (jmt) - First implementation for new driver architecture.
8 * 1999-07-31 (jmt) - First working version.
12 * o Implement SRQ handling.
15 #include <linux/types.h>
16 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/proc_fs.h>
22 #include <asm/bootinfo.h>
23 #include <asm/macintosh.h>
24 #include <asm/macints.h>
25 #include <asm/mac_iop.h>
26 #include <asm/mac_oss.h>
27 #include <asm/adb_iop.h>
29 #include <linux/adb.h>
31 /*#define DEBUG_ADB_IOP*/
33 extern void iop_ism_irq(int, void *, struct pt_regs *);
35 static struct adb_request *current_req;
36 static struct adb_request *last_req;
38 static unsigned char reply_buff[16];
39 static unsigned char *reply_ptr;
42 static enum adb_iop_state {
48 static void adb_iop_start(void);
49 static int adb_iop_probe(void);
50 static int adb_iop_init(void);
51 static int adb_iop_send_request(struct adb_request *, int);
52 static int adb_iop_write(struct adb_request *);
53 static int adb_iop_autopoll(int);
54 static void adb_iop_poll(void);
55 static int adb_iop_reset_bus(void);
57 struct adb_driver adb_iop_driver = {
67 static void adb_iop_end_req(struct adb_request *req, int state)
70 current_req = req->next;
71 if (req->done) (*req->done)(req);
72 adb_iop_state = state;
76 * Completion routine for ADB commands sent to the IOP.
78 * This will be called when a packet has been successfully sent.
81 static void adb_iop_complete(struct iop_msg *msg, struct pt_regs *regs)
83 struct adb_request *req;
90 if ((adb_iop_state == sending) && req && req->reply_expected) {
91 adb_iop_state = awaiting_reply;
98 * Listen for ADB messages from the IOP.
100 * This will be called when unsolicited messages (usually replies to TALK
101 * commands or autopoll packets) are received.
104 static void adb_iop_listen(struct iop_msg *msg, struct pt_regs *regs)
106 struct adb_iopmsg *amsg = (struct adb_iopmsg *) msg->message;
107 struct adb_request *req;
119 printk("adb_iop_listen %p: rcvd packet, %d bytes: %02X %02X", req,
120 (uint) amsg->count + 2, (uint) amsg->flags, (uint) amsg->cmd);
121 for (i = 0; i < amsg->count; i++)
122 printk(" %02X", (uint) amsg->data[i]);
126 /* Handle a timeout. Timeout packets seem to occur even after */
127 /* we've gotten a valid reply to a TALK, so I'm assuming that */
128 /* a "timeout" is actually more like an "end-of-data" signal. */
129 /* We need to send back a timeout packet to the IOP to shut */
130 /* it up, plus complete the current request, if any. */
132 if (amsg->flags & ADB_IOP_TIMEOUT) {
133 msg->reply[0] = ADB_IOP_TIMEOUT | ADB_IOP_AUTOPOLL;
136 if (req && (adb_iop_state != idle)) {
137 adb_iop_end_req(req, idle);
140 /* TODO: is it possible for more than one chunk of data */
141 /* to arrive before the timeout? If so we need to */
142 /* use reply_ptr here like the other drivers do. */
143 if ((adb_iop_state == awaiting_reply) &&
144 (amsg->flags & ADB_IOP_EXPLICIT)) {
145 req->reply_len = amsg->count + 1;
146 memcpy(req->reply, &amsg->cmd, req->reply_len);
148 adb_input(&amsg->cmd, amsg->count + 1, regs,
149 amsg->flags & ADB_IOP_AUTOPOLL);
151 memcpy(msg->reply, msg->message, IOP_MSG_LEN);
153 iop_complete_message(msg);
154 restore_flags(flags);
158 * Start sending an ADB packet, IOP style
160 * There isn't much to do other than hand the packet over to the IOP
161 * after encapsulating it in an adb_iopmsg.
164 static void adb_iop_start(void)
167 struct adb_request *req;
168 struct adb_iopmsg amsg;
173 /* get the packet to send */
181 printk("adb_iop_start %p: sending packet, %d bytes:", req, req->nbytes);
182 for (i = 0 ; i < req->nbytes ; i++)
183 printk(" %02X", (uint) req->data[i]);
187 /* The IOP takes MacII-style packets, so */
188 /* strip the initial ADB_PACKET byte. */
190 amsg.flags = ADB_IOP_EXPLICIT;
191 amsg.count = req->nbytes - 2;
193 /* amsg.data immediately follows amsg.cmd, effectively making */
194 /* amsg.cmd a pointer to the beginning of a full ADB packet. */
195 memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1);
198 adb_iop_state = sending;
199 restore_flags(flags);
201 /* Now send it. The IOP manager will call adb_iop_complete */
202 /* when the packet has been sent. */
204 iop_send_message(ADB_IOP, ADB_CHAN, req,
205 sizeof(amsg), (__u8 *) &amsg, adb_iop_complete);
208 int adb_iop_probe(void)
210 if (!iop_ism_present) return -ENODEV;
214 int adb_iop_init(void)
216 printk("adb: IOP ISM driver v0.4 for Unified ADB.\n");
217 iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB");
221 int adb_iop_send_request(struct adb_request *req, int sync)
225 err = adb_iop_write(req);
229 while (!req->complete) adb_iop_poll();
234 static int adb_iop_write(struct adb_request *req)
238 if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) {
251 if (current_req != 0) {
252 last_req->next = req;
259 restore_flags(flags);
260 if (adb_iop_state == idle) adb_iop_start();
264 int adb_iop_autopoll(int devs)
266 /* TODO: how do we enable/disable autopoll? */
270 void adb_iop_poll(void)
272 if (adb_iop_state == idle) adb_iop_start();
273 iop_ism_irq(0, (void *) ADB_IOP, NULL);
276 int adb_iop_reset_bus(void)
278 struct adb_request req = {
281 .data = { ADB_PACKET, 0 },
285 while (!req.complete) {