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>
22 #include <asm/unaligned.h>
24 #include <linux/adb.h>
26 static struct adb_request
*current_req
;
27 static struct adb_request
*last_req
;
28 static unsigned int autopoll_devs
;
29 static u8 autopoll_addr
;
31 static enum adb_iop_state
{
37 static void adb_iop_start(void);
38 static int adb_iop_probe(void);
39 static int adb_iop_init(void);
40 static int adb_iop_send_request(struct adb_request
*, int);
41 static int adb_iop_write(struct adb_request
*);
42 static int adb_iop_autopoll(int);
43 static void adb_iop_poll(void);
44 static int adb_iop_reset_bus(void);
46 /* ADB command byte structure */
47 #define ADDR_MASK 0xF0
51 struct adb_driver adb_iop_driver
= {
53 .probe
= adb_iop_probe
,
55 .send_request
= adb_iop_send_request
,
56 .autopoll
= adb_iop_autopoll
,
58 .reset_bus
= adb_iop_reset_bus
61 static void adb_iop_done(void)
63 struct adb_request
*req
= current_req
;
68 current_req
= req
->next
;
72 if (adb_iop_state
== idle
)
77 * Completion routine for ADB commands sent to the IOP.
79 * This will be called when a packet has been successfully sent.
82 static void adb_iop_complete(struct iop_msg
*msg
)
86 local_irq_save(flags
);
88 adb_iop_state
= awaiting_reply
;
90 local_irq_restore(flags
);
94 * Listen for ADB messages from the IOP.
96 * This will be called when unsolicited IOP messages are received.
97 * These IOP messages can carry ADB autopoll responses and also occur
98 * after explicit ADB commands.
101 static void adb_iop_listen(struct iop_msg
*msg
)
103 struct adb_iopmsg
*amsg
= (struct adb_iopmsg
*)msg
->message
;
104 u8 addr
= (amsg
->cmd
& ADDR_MASK
) >> 4;
105 u8 op
= amsg
->cmd
& OP_MASK
;
107 bool req_done
= false;
109 local_irq_save(flags
);
111 /* Responses to Talk commands may be unsolicited as they are
112 * produced when the IOP polls devices. They are mostly timeouts.
114 if (op
== TALK
&& ((1 << addr
) & autopoll_devs
))
115 autopoll_addr
= addr
;
117 switch (amsg
->flags
& (ADB_IOP_EXPLICIT
|
120 case ADB_IOP_EXPLICIT
:
121 case ADB_IOP_EXPLICIT
| ADB_IOP_TIMEOUT
:
122 if (adb_iop_state
== awaiting_reply
) {
123 struct adb_request
*req
= current_req
;
125 if (req
->reply_expected
) {
126 req
->reply_len
= amsg
->count
+ 1;
127 memcpy(req
->reply
, &amsg
->cmd
, req
->reply_len
);
133 case ADB_IOP_AUTOPOLL
:
134 if (((1 << addr
) & autopoll_devs
) &&
135 amsg
->cmd
== ADB_READREG(addr
, 0))
136 adb_input(&amsg
->cmd
, amsg
->count
+ 1, 1);
139 msg
->reply
[0] = autopoll_addr
? ADB_IOP_AUTOPOLL
: 0;
141 msg
->reply
[2] = autopoll_addr
? ADB_READREG(autopoll_addr
, 0) : 0;
142 iop_complete_message(msg
);
147 local_irq_restore(flags
);
151 * Start sending an ADB packet, IOP style
153 * There isn't much to do other than hand the packet over to the IOP
154 * after encapsulating it in an adb_iopmsg.
157 static void adb_iop_start(void)
159 struct adb_request
*req
;
160 struct adb_iopmsg amsg
;
162 /* get the packet to send */
167 /* The IOP takes MacII-style packets, so strip the initial
170 amsg
.flags
= ADB_IOP_EXPLICIT
;
171 amsg
.count
= req
->nbytes
- 2;
173 /* amsg.data immediately follows amsg.cmd, effectively making
174 * &amsg.cmd a pointer to the beginning of a full ADB packet.
176 memcpy(&amsg
.cmd
, req
->data
+ 1, req
->nbytes
- 1);
179 adb_iop_state
= sending
;
181 /* Now send it. The IOP manager will call adb_iop_complete
182 * when the message has been sent.
184 iop_send_message(ADB_IOP
, ADB_CHAN
, req
, sizeof(amsg
), (__u8
*)&amsg
,
188 static int adb_iop_probe(void)
190 if (!iop_ism_present
)
195 static int adb_iop_init(void)
197 pr_info("adb: IOP ISM driver v0.4 for Unified ADB\n");
198 iop_listen(ADB_IOP
, ADB_CHAN
, adb_iop_listen
, "ADB");
202 static int adb_iop_send_request(struct adb_request
*req
, int sync
)
206 err
= adb_iop_write(req
);
211 while (!req
->complete
)
217 static int adb_iop_write(struct adb_request
*req
)
221 if ((req
->nbytes
< 2) || (req
->data
[0] != ADB_PACKET
)) {
231 local_irq_save(flags
);
234 last_req
->next
= req
;
241 if (adb_iop_state
== idle
)
244 local_irq_restore(flags
);
249 static void adb_iop_set_ap_complete(struct iop_msg
*msg
)
251 struct adb_iopmsg
*amsg
= (struct adb_iopmsg
*)msg
->message
;
253 autopoll_devs
= get_unaligned_be16(amsg
->data
);
254 if (autopoll_devs
& (1 << autopoll_addr
))
256 autopoll_addr
= autopoll_devs
? (ffs(autopoll_devs
) - 1) : 0;
259 static int adb_iop_autopoll(int devs
)
261 struct adb_iopmsg amsg
;
263 unsigned int mask
= (unsigned int)devs
& 0xFFFE;
265 local_irq_save(flags
);
267 amsg
.flags
= ADB_IOP_SET_AUTOPOLL
| (mask
? ADB_IOP_AUTOPOLL
: 0);
270 put_unaligned_be16(mask
, amsg
.data
);
272 iop_send_message(ADB_IOP
, ADB_CHAN
, NULL
, sizeof(amsg
), (__u8
*)&amsg
,
273 adb_iop_set_ap_complete
);
275 local_irq_restore(flags
);
280 static void adb_iop_poll(void)
282 iop_ism_irq_poll(ADB_IOP
);
285 static int adb_iop_reset_bus(void)
287 struct adb_request req
;
289 /* Command = 0, Address = ignored */
290 adb_request(&req
, NULL
, ADBREQ_NOSEND
, 1, ADB_BUSRESET
);
291 adb_iop_send_request(&req
, 1);
293 /* Don't want any more requests during the Global Reset low time. */