blob: 0ee3272491501c522012a8d8062215d660cfc245 [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;
Finn Thain10199e92020-11-20 15:39:56 +110028static u8 autopoll_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static enum adb_iop_state {
Finn Thain47fd2062018-09-11 20:18:44 -040031 idle,
32 sending,
33 awaiting_reply
Linus Torvalds1da177e2005-04-16 15:20:36 -070034} adb_iop_state;
35
36static void adb_iop_start(void);
37static int adb_iop_probe(void);
38static int adb_iop_init(void);
39static int adb_iop_send_request(struct adb_request *, int);
40static int adb_iop_write(struct adb_request *);
41static int adb_iop_autopoll(int);
42static void adb_iop_poll(void);
43static int adb_iop_reset_bus(void);
44
Finn Thain10199e92020-11-20 15:39:56 +110045/* ADB command byte structure */
46#define ADDR_MASK 0xF0
47#define OP_MASK 0x0C
48#define TALK 0x0C
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct adb_driver adb_iop_driver = {
Finn Thain3a52f6f2018-03-29 11:36:04 +110051 .name = "ISM IOP",
52 .probe = adb_iop_probe,
53 .init = adb_iop_init,
54 .send_request = adb_iop_send_request,
55 .autopoll = adb_iop_autopoll,
56 .poll = adb_iop_poll,
57 .reset_bus = adb_iop_reset_bus
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
Finn Thain32226e82020-05-31 09:17:03 +100060static void adb_iop_done(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Finn Thain32226e82020-05-31 09:17:03 +100062 struct adb_request *req = current_req;
63
64 adb_iop_state = idle;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 req->complete = 1;
67 current_req = req->next;
Finn Thain47fd2062018-09-11 20:18:44 -040068 if (req->done)
69 (*req->done)(req);
Finn Thain32226e82020-05-31 09:17:03 +100070
71 if (adb_iop_state == idle)
72 adb_iop_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75/*
76 * Completion routine for ADB commands sent to the IOP.
77 *
78 * This will be called when a packet has been successfully sent.
79 */
80
David Howells7d12e782006-10-05 14:55:46 +010081static void adb_iop_complete(struct iop_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Geert Uytterhoeven38b7a2a2010-11-11 14:05:06 -080083 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 local_irq_save(flags);
86
Finn Thain2c9cfba2020-11-20 15:39:56 +110087 adb_iop_state = awaiting_reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 local_irq_restore(flags);
90}
91
92/*
93 * Listen for ADB messages from the IOP.
94 *
Finn Thain2c9cfba2020-11-20 15:39:56 +110095 * This will be called when unsolicited IOP messages are received.
96 * These IOP messages can carry ADB autopoll responses and also occur
97 * after explicit ADB commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
99
David Howells7d12e782006-10-05 14:55:46 +0100100static void adb_iop_listen(struct iop_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Finn Thain47fd2062018-09-11 20:18:44 -0400102 struct adb_iopmsg *amsg = (struct adb_iopmsg *)msg->message;
Finn Thain10199e92020-11-20 15:39:56 +1100103 u8 addr = (amsg->cmd & ADDR_MASK) >> 4;
104 u8 op = amsg->cmd & OP_MASK;
Geert Uytterhoeven38b7a2a2010-11-11 14:05:06 -0800105 unsigned long flags;
Finn Thain32226e82020-05-31 09:17:03 +1000106 bool req_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 local_irq_save(flags);
109
Finn Thain10199e92020-11-20 15:39:56 +1100110 /* Responses to Talk commands may be unsolicited as they are
111 * produced when the IOP polls devices. They are mostly timeouts.
Finn Thainff785e12020-05-31 09:17:03 +1000112 */
Finn Thain10199e92020-11-20 15:39:56 +1100113 if (op == TALK && ((1 << addr) & autopoll_devs))
114 autopoll_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Finn Thain10199e92020-11-20 15:39:56 +1100116 switch (amsg->flags & (ADB_IOP_EXPLICIT |
117 ADB_IOP_AUTOPOLL |
118 ADB_IOP_TIMEOUT)) {
119 case ADB_IOP_EXPLICIT:
120 case ADB_IOP_EXPLICIT | ADB_IOP_TIMEOUT:
Finn Thain32226e82020-05-31 09:17:03 +1000121 if (adb_iop_state == awaiting_reply) {
122 struct adb_request *req = current_req;
123
Finn Thain2c9cfba2020-11-20 15:39:56 +1100124 if (req->reply_expected) {
125 req->reply_len = amsg->count + 1;
126 memcpy(req->reply, &amsg->cmd, req->reply_len);
127 }
Finn Thain32226e82020-05-31 09:17:03 +1000128
129 req_done = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Finn Thain10199e92020-11-20 15:39:56 +1100131 break;
132 case ADB_IOP_AUTOPOLL:
133 if (((1 << addr) & autopoll_devs) &&
134 amsg->cmd == ADB_READREG(addr, 0))
135 adb_input(&amsg->cmd, amsg->count + 1, 1);
136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Finn Thain10199e92020-11-20 15:39:56 +1100138 msg->reply[0] = autopoll_addr ? ADB_IOP_AUTOPOLL : 0;
139 msg->reply[1] = 0;
140 msg->reply[2] = autopoll_addr ? ADB_READREG(autopoll_addr, 0) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 iop_complete_message(msg);
Finn Thain32226e82020-05-31 09:17:03 +1000142
143 if (req_done)
144 adb_iop_done();
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 local_irq_restore(flags);
147}
148
149/*
150 * Start sending an ADB packet, IOP style
151 *
152 * There isn't much to do other than hand the packet over to the IOP
153 * after encapsulating it in an adb_iopmsg.
154 */
155
156static void adb_iop_start(void)
157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct adb_request *req;
159 struct adb_iopmsg amsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* get the packet to send */
162 req = current_req;
Finn Thain47fd2062018-09-11 20:18:44 -0400163 if (!req)
164 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Finn Thainff785e12020-05-31 09:17:03 +1000166 /* The IOP takes MacII-style packets, so strip the initial
167 * ADB_PACKET byte.
168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 amsg.flags = ADB_IOP_EXPLICIT;
170 amsg.count = req->nbytes - 2;
171
Finn Thainff785e12020-05-31 09:17:03 +1000172 /* amsg.data immediately follows amsg.cmd, effectively making
173 * &amsg.cmd a pointer to the beginning of a full ADB packet.
174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1);
176
177 req->sent = 1;
178 adb_iop_state = sending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Finn Thainff785e12020-05-31 09:17:03 +1000180 /* Now send it. The IOP manager will call adb_iop_complete
181 * when the message has been sent.
182 */
Finn Thain47fd2062018-09-11 20:18:44 -0400183 iop_send_message(ADB_IOP, ADB_CHAN, req, sizeof(amsg), (__u8 *)&amsg,
184 adb_iop_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Finn Thain56b732e2020-05-31 09:17:03 +1000187static int adb_iop_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Finn Thain47fd2062018-09-11 20:18:44 -0400189 if (!iop_ism_present)
190 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192}
193
Finn Thain56b732e2020-05-31 09:17:03 +1000194static int adb_iop_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Finn Thain351e5ad2018-09-11 20:18:44 -0400196 pr_info("adb: IOP ISM driver v0.4 for Unified ADB\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB");
198 return 0;
199}
200
Finn Thain56b732e2020-05-31 09:17:03 +1000201static int adb_iop_send_request(struct adb_request *req, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int err;
204
205 err = adb_iop_write(req);
Finn Thain47fd2062018-09-11 20:18:44 -0400206 if (err)
207 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (sync) {
Finn Thain47fd2062018-09-11 20:18:44 -0400210 while (!req->complete)
211 adb_iop_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 return 0;
214}
215
216static int adb_iop_write(struct adb_request *req)
217{
218 unsigned long flags;
219
220 if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) {
221 req->complete = 1;
222 return -EINVAL;
223 }
224
Al Viroa5d361f2006-01-12 01:06:34 -0800225 req->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 req->sent = 0;
227 req->complete = 0;
228 req->reply_len = 0;
229
Finn Thainaac840e2020-05-31 09:17:03 +1000230 local_irq_save(flags);
231
Finn Thain56b732e2020-05-31 09:17:03 +1000232 if (current_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 last_req->next = req;
234 last_req = req;
235 } else {
236 current_req = req;
237 last_req = req;
238 }
239
Finn Thain47fd2062018-09-11 20:18:44 -0400240 if (adb_iop_state == idle)
241 adb_iop_start();
Finn Thainaac840e2020-05-31 09:17:03 +1000242
243 local_irq_restore(flags);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
Finn Thainc66da952020-05-31 09:17:03 +1000248static void adb_iop_set_ap_complete(struct iop_msg *msg)
249{
250 struct adb_iopmsg *amsg = (struct adb_iopmsg *)msg->message;
251
252 autopoll_devs = (amsg->data[1] << 8) | amsg->data[0];
Finn Thain10199e92020-11-20 15:39:56 +1100253 if (autopoll_devs & (1 << autopoll_addr))
254 return;
255 autopoll_addr = autopoll_devs ? (ffs(autopoll_devs) - 1) : 0;
Finn Thainc66da952020-05-31 09:17:03 +1000256}
257
Finn Thain56b732e2020-05-31 09:17:03 +1000258static int adb_iop_autopoll(int devs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Finn Thainc66da952020-05-31 09:17:03 +1000260 struct adb_iopmsg amsg;
261 unsigned long flags;
262 unsigned int mask = (unsigned int)devs & 0xFFFE;
263
264 local_irq_save(flags);
265
266 amsg.flags = ADB_IOP_SET_AUTOPOLL | (mask ? ADB_IOP_AUTOPOLL : 0);
267 amsg.count = 2;
268 amsg.cmd = 0;
269 amsg.data[0] = mask & 0xFF;
270 amsg.data[1] = (mask >> 8) & 0xFF;
271
272 iop_send_message(ADB_IOP, ADB_CHAN, NULL, sizeof(amsg), (__u8 *)&amsg,
273 adb_iop_set_ap_complete);
274
275 local_irq_restore(flags);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278}
279
Finn Thain56b732e2020-05-31 09:17:03 +1000280static void adb_iop_poll(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Finn Thain92178fc2017-10-26 22:45:24 -0400282 iop_ism_irq_poll(ADB_IOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Finn Thain56b732e2020-05-31 09:17:03 +1000285static int adb_iop_reset_bus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Finn Thain303511e2020-05-31 09:17:03 +1000287 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Finn Thain303511e2020-05-31 09:17:03 +1000289 /* Command = 0, Address = ignored */
290 adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET);
291 adb_iop_send_request(&req, 1);
292
293 /* Don't want any more requests during the Global Reset low time. */
294 mdelay(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 return 0;
297}