blob: f3d1a460fbce1de25213e1b7d70f8039e9360c61 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/delay.h>
16#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Finn Thain47fd2062018-09-11 20:18:44 -040018#include <asm/macintosh.h>
19#include <asm/macints.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/mac_iop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/adb_iop.h>
22
Finn Thain47fd2062018-09-11 20:18:44 -040023#include <linux/adb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static struct adb_request *current_req;
26static struct adb_request *last_req;
Finn Thainc66da952020-05-31 09:17:03 +100027static unsigned int autopoll_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static enum adb_iop_state {
Finn Thain47fd2062018-09-11 20:18:44 -040030 idle,
31 sending,
32 awaiting_reply
Linus Torvalds1da177e2005-04-16 15:20:36 -070033} adb_iop_state;
34
35static void adb_iop_start(void);
36static int adb_iop_probe(void);
37static int adb_iop_init(void);
38static int adb_iop_send_request(struct adb_request *, int);
39static int adb_iop_write(struct adb_request *);
40static int adb_iop_autopoll(int);
41static void adb_iop_poll(void);
42static int adb_iop_reset_bus(void);
43
44struct adb_driver adb_iop_driver = {
Finn Thain3a52f6f2018-03-29 11:36:04 +110045 .name = "ISM IOP",
46 .probe = adb_iop_probe,
47 .init = adb_iop_init,
48 .send_request = adb_iop_send_request,
49 .autopoll = adb_iop_autopoll,
50 .poll = adb_iop_poll,
51 .reset_bus = adb_iop_reset_bus
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
Finn Thain32226e82020-05-31 09:17:03 +100054static void adb_iop_done(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Finn Thain32226e82020-05-31 09:17:03 +100056 struct adb_request *req = current_req;
57
58 adb_iop_state = idle;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 req->complete = 1;
61 current_req = req->next;
Finn Thain47fd2062018-09-11 20:18:44 -040062 if (req->done)
63 (*req->done)(req);
Finn Thain32226e82020-05-31 09:17:03 +100064
65 if (adb_iop_state == idle)
66 adb_iop_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69/*
70 * Completion routine for ADB commands sent to the IOP.
71 *
72 * This will be called when a packet has been successfully sent.
73 */
74
David Howells7d12e782006-10-05 14:55:46 +010075static void adb_iop_complete(struct iop_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Geert Uytterhoeven38b7a2a2010-11-11 14:05:06 -080077 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 local_irq_save(flags);
80
Finn Thaine2954e52020-05-31 09:17:03 +100081 if (current_req->reply_expected)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 adb_iop_state = awaiting_reply;
Finn Thaine2954e52020-05-31 09:17:03 +100083 else
84 adb_iop_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 local_irq_restore(flags);
87}
88
89/*
90 * Listen for ADB messages from the IOP.
91 *
92 * This will be called when unsolicited messages (usually replies to TALK
93 * commands or autopoll packets) are received.
94 */
95
David Howells7d12e782006-10-05 14:55:46 +010096static void adb_iop_listen(struct iop_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Finn Thain47fd2062018-09-11 20:18:44 -040098 struct adb_iopmsg *amsg = (struct adb_iopmsg *)msg->message;
Geert Uytterhoeven38b7a2a2010-11-11 14:05:06 -080099 unsigned long flags;
Finn Thain32226e82020-05-31 09:17:03 +1000100 bool req_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 local_irq_save(flags);
103
Finn Thainff785e12020-05-31 09:17:03 +1000104 /* Handle a timeout. Timeout packets seem to occur even after
105 * we've gotten a valid reply to a TALK, presumably because of
106 * autopolling.
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Finn Thain32226e82020-05-31 09:17:03 +1000109 if (amsg->flags & ADB_IOP_EXPLICIT) {
110 if (adb_iop_state == awaiting_reply) {
111 struct adb_request *req = current_req;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 req->reply_len = amsg->count + 1;
114 memcpy(req->reply, &amsg->cmd, req->reply_len);
Finn Thain32226e82020-05-31 09:17:03 +1000115
116 req_done = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Finn Thain32226e82020-05-31 09:17:03 +1000118 } else if (!(amsg->flags & ADB_IOP_TIMEOUT)) {
119 adb_input(&amsg->cmd, amsg->count + 1,
120 amsg->flags & ADB_IOP_AUTOPOLL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Finn Thain32226e82020-05-31 09:17:03 +1000122
Finn Thainc66da952020-05-31 09:17:03 +1000123 msg->reply[0] = autopoll_devs ? ADB_IOP_AUTOPOLL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 iop_complete_message(msg);
Finn Thain32226e82020-05-31 09:17:03 +1000125
126 if (req_done)
127 adb_iop_done();
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 local_irq_restore(flags);
130}
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
139static void adb_iop_start(void)
140{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct adb_request *req;
142 struct adb_iopmsg amsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* get the packet to send */
145 req = current_req;
Finn Thain47fd2062018-09-11 20:18:44 -0400146 if (!req)
147 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Finn Thainff785e12020-05-31 09:17:03 +1000149 /* The IOP takes MacII-style packets, so strip the initial
150 * ADB_PACKET byte.
151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 amsg.flags = ADB_IOP_EXPLICIT;
153 amsg.count = req->nbytes - 2;
154
Finn Thainff785e12020-05-31 09:17:03 +1000155 /* amsg.data immediately follows amsg.cmd, effectively making
156 * &amsg.cmd a pointer to the beginning of a full ADB packet.
157 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1);
159
160 req->sent = 1;
161 adb_iop_state = sending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Finn Thainff785e12020-05-31 09:17:03 +1000163 /* Now send it. The IOP manager will call adb_iop_complete
164 * when the message has been sent.
165 */
Finn Thain47fd2062018-09-11 20:18:44 -0400166 iop_send_message(ADB_IOP, ADB_CHAN, req, sizeof(amsg), (__u8 *)&amsg,
167 adb_iop_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Finn Thain56b732e2020-05-31 09:17:03 +1000170static int adb_iop_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Finn Thain47fd2062018-09-11 20:18:44 -0400172 if (!iop_ism_present)
173 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175}
176
Finn Thain56b732e2020-05-31 09:17:03 +1000177static int adb_iop_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Finn Thain351e5ad2018-09-11 20:18:44 -0400179 pr_info("adb: IOP ISM driver v0.4 for Unified ADB\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB");
181 return 0;
182}
183
Finn Thain56b732e2020-05-31 09:17:03 +1000184static int adb_iop_send_request(struct adb_request *req, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 int err;
187
188 err = adb_iop_write(req);
Finn Thain47fd2062018-09-11 20:18:44 -0400189 if (err)
190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (sync) {
Finn Thain47fd2062018-09-11 20:18:44 -0400193 while (!req->complete)
194 adb_iop_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 return 0;
197}
198
199static int adb_iop_write(struct adb_request *req)
200{
201 unsigned long flags;
202
203 if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) {
204 req->complete = 1;
205 return -EINVAL;
206 }
207
Al Viroa5d361f2006-01-12 01:06:34 -0800208 req->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 req->sent = 0;
210 req->complete = 0;
211 req->reply_len = 0;
212
Finn Thainaac840e2020-05-31 09:17:03 +1000213 local_irq_save(flags);
214
Finn Thain56b732e2020-05-31 09:17:03 +1000215 if (current_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 last_req->next = req;
217 last_req = req;
218 } else {
219 current_req = req;
220 last_req = req;
221 }
222
Finn Thain47fd2062018-09-11 20:18:44 -0400223 if (adb_iop_state == idle)
224 adb_iop_start();
Finn Thainaac840e2020-05-31 09:17:03 +1000225
226 local_irq_restore(flags);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
229}
230
Finn Thainc66da952020-05-31 09:17:03 +1000231static void adb_iop_set_ap_complete(struct iop_msg *msg)
232{
233 struct adb_iopmsg *amsg = (struct adb_iopmsg *)msg->message;
234
235 autopoll_devs = (amsg->data[1] << 8) | amsg->data[0];
236}
237
Finn Thain56b732e2020-05-31 09:17:03 +1000238static int adb_iop_autopoll(int devs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Finn Thainc66da952020-05-31 09:17:03 +1000240 struct adb_iopmsg amsg;
241 unsigned long flags;
242 unsigned int mask = (unsigned int)devs & 0xFFFE;
243
244 local_irq_save(flags);
245
246 amsg.flags = ADB_IOP_SET_AUTOPOLL | (mask ? ADB_IOP_AUTOPOLL : 0);
247 amsg.count = 2;
248 amsg.cmd = 0;
249 amsg.data[0] = mask & 0xFF;
250 amsg.data[1] = (mask >> 8) & 0xFF;
251
252 iop_send_message(ADB_IOP, ADB_CHAN, NULL, sizeof(amsg), (__u8 *)&amsg,
253 adb_iop_set_ap_complete);
254
255 local_irq_restore(flags);
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258}
259
Finn Thain56b732e2020-05-31 09:17:03 +1000260static void adb_iop_poll(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Finn Thain92178fc2017-10-26 22:45:24 -0400262 iop_ism_irq_poll(ADB_IOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Finn Thain56b732e2020-05-31 09:17:03 +1000265static int adb_iop_reset_bus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Finn Thain303511e2020-05-31 09:17:03 +1000267 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Finn Thain303511e2020-05-31 09:17:03 +1000269 /* Command = 0, Address = ignored */
270 adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET);
271 adb_iop_send_request(&req, 1);
272
273 /* Don't want any more requests during the Global Reset low time. */
274 mdelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return 0;
277}