1 // SPDX-License-Identifier: GPL-2.0
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.
7 * 1999-07-01 (jmt) - First implementation for new driver architecture.
9 * 1999-07-31 (jmt) - First working version.
12 #include <linux/types.h>
13 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/init.h>
18 #include <asm/macintosh.h>
19 #include <asm/macints.h>
20 #include <asm/mac_iop.h>
21 #include <asm/adb_iop.h>
23 #include <linux/adb.h>
25 static struct adb_request *current_req;
26 static struct adb_request *last_req;
27 static unsigned int autopoll_devs;
29 static enum adb_iop_state {
35 static void adb_iop_start(void);
36 static int adb_iop_probe(void);
37 static int adb_iop_init(void);
38 static int adb_iop_send_request(struct adb_request *, int);
39 static int adb_iop_write(struct adb_request *);
40 static int adb_iop_autopoll(int);
41 static void adb_iop_poll(void);
42 static int adb_iop_reset_bus(void);
44 struct adb_driver adb_iop_driver = {
46 .probe = adb_iop_probe,
48 .send_request = adb_iop_send_request,
49 .autopoll = adb_iop_autopoll,
51 .reset_bus = adb_iop_reset_bus
54 static void adb_iop_done(void)
56 struct adb_request *req = current_req;
61 current_req = req->next;
65 if (adb_iop_state == idle)
70 * Completion routine for ADB commands sent to the IOP.
72 * This will be called when a packet has been successfully sent.
75 static void adb_iop_complete(struct iop_msg *msg)
79 local_irq_save(flags);
81 if (current_req->reply_expected)
82 adb_iop_state = awaiting_reply;
86 local_irq_restore(flags);
90 * Listen for ADB messages from the IOP.
92 * This will be called when unsolicited messages (usually replies to TALK
93 * commands or autopoll packets) are received.
96 static void adb_iop_listen(struct iop_msg *msg)
98 struct adb_iopmsg *amsg = (struct adb_iopmsg *)msg->message;
100 bool req_done = false;
102 local_irq_save(flags);
104 /* Handle a timeout. Timeout packets seem to occur even after
105 * we've gotten a valid reply to a TALK, presumably because of
109 if (amsg->flags & ADB_IOP_EXPLICIT) {
110 if (adb_iop_state == awaiting_reply) {
111 struct adb_request *req = current_req;
113 req->reply_len = amsg->count + 1;
114 memcpy(req->reply, &amsg->cmd, req->reply_len);
118 } else if (!(amsg->flags & ADB_IOP_TIMEOUT)) {
119 adb_input(&amsg->cmd, amsg->count + 1,
120 amsg->flags & ADB_IOP_AUTOPOLL);
123 msg->reply[0] = autopoll_devs ? ADB_IOP_AUTOPOLL : 0;
124 iop_complete_message(msg);
129 local_irq_restore(flags);
133 * Start sending an ADB packet, IOP style
135 * There isn't much to do other than hand the packet over to the IOP
136 * after encapsulating it in an adb_iopmsg.
139 static void adb_iop_start(void)
141 struct adb_request *req;
142 struct adb_iopmsg amsg;
144 /* get the packet to send */
149 /* The IOP takes MacII-style packets, so strip the initial
152 amsg.flags = ADB_IOP_EXPLICIT;
153 amsg.count = req->nbytes - 2;
155 /* amsg.data immediately follows amsg.cmd, effectively making
156 * &amsg.cmd a pointer to the beginning of a full ADB packet.
158 memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1);
161 adb_iop_state = sending;
163 /* Now send it. The IOP manager will call adb_iop_complete
164 * when the message has been sent.
166 iop_send_message(ADB_IOP, ADB_CHAN, req, sizeof(amsg), (__u8 *)&amsg,
170 static int adb_iop_probe(void)
172 if (!iop_ism_present)
177 static int adb_iop_init(void)
179 pr_info("adb: IOP ISM driver v0.4 for Unified ADB\n");
180 iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB");
184 static int adb_iop_send_request(struct adb_request *req, int sync)
188 err = adb_iop_write(req);
193 while (!req->complete)
199 static int adb_iop_write(struct adb_request *req)
203 if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) {
213 local_irq_save(flags);
216 last_req->next = req;
223 if (adb_iop_state == idle)
226 local_irq_restore(flags);
231 static void adb_iop_set_ap_complete(struct iop_msg *msg)
233 struct adb_iopmsg *amsg = (struct adb_iopmsg *)msg->message;
235 autopoll_devs = (amsg->data[1] << 8) | amsg->data[0];
238 static int adb_iop_autopoll(int devs)
240 struct adb_iopmsg amsg;
242 unsigned int mask = (unsigned int)devs & 0xFFFE;
244 local_irq_save(flags);
246 amsg.flags = ADB_IOP_SET_AUTOPOLL | (mask ? ADB_IOP_AUTOPOLL : 0);
249 amsg.data[0] = mask & 0xFF;
250 amsg.data[1] = (mask >> 8) & 0xFF;
252 iop_send_message(ADB_IOP, ADB_CHAN, NULL, sizeof(amsg), (__u8 *)&amsg,
253 adb_iop_set_ap_complete);
255 local_irq_restore(flags);
260 static void adb_iop_poll(void)
262 iop_ism_irq_poll(ADB_IOP);
265 static int adb_iop_reset_bus(void)
267 struct adb_request req;
269 /* Command = 0, Address = ignored */
270 adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET);
271 adb_iop_send_request(&req, 1);
273 /* Don't want any more requests during the Global Reset low time. */