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/macintosh.h>
23 #include <asm/macints.h>
24 #include <asm/mac_iop.h>
25 #include <asm/mac_oss.h>
26 #include <asm/adb_iop.h>
28 #include <linux/adb.h>
30 /*#define DEBUG_ADB_IOP*/
32 extern void iop_ism_irq(int, void *);
34 static struct adb_request
*current_req
;
35 static struct adb_request
*last_req
;
37 static unsigned char reply_buff
[16];
38 static unsigned char *reply_ptr
;
41 static enum adb_iop_state
{
47 static void adb_iop_start(void);
48 static int adb_iop_probe(void);
49 static int adb_iop_init(void);
50 static int adb_iop_send_request(struct adb_request
*, int);
51 static int adb_iop_write(struct adb_request
*);
52 static int adb_iop_autopoll(int);
53 static void adb_iop_poll(void);
54 static int adb_iop_reset_bus(void);
56 struct adb_driver adb_iop_driver
= {
66 static void adb_iop_end_req(struct adb_request
*req
, int state
)
69 current_req
= req
->next
;
70 if (req
->done
) (*req
->done
)(req
);
71 adb_iop_state
= state
;
75 * Completion routine for ADB commands sent to the IOP.
77 * This will be called when a packet has been successfully sent.
80 static void adb_iop_complete(struct iop_msg
*msg
)
82 struct adb_request
*req
;
85 local_irq_save(flags
);
88 if ((adb_iop_state
== sending
) && req
&& req
->reply_expected
) {
89 adb_iop_state
= awaiting_reply
;
92 local_irq_restore(flags
);
96 * Listen for ADB messages from the IOP.
98 * This will be called when unsolicited messages (usually replies to TALK
99 * commands or autopoll packets) are received.
102 static void adb_iop_listen(struct iop_msg
*msg
)
104 struct adb_iopmsg
*amsg
= (struct adb_iopmsg
*) msg
->message
;
105 struct adb_request
*req
;
111 local_irq_save(flags
);
116 printk("adb_iop_listen %p: rcvd packet, %d bytes: %02X %02X", req
,
117 (uint
) amsg
->count
+ 2, (uint
) amsg
->flags
, (uint
) amsg
->cmd
);
118 for (i
= 0; i
< amsg
->count
; i
++)
119 printk(" %02X", (uint
) amsg
->data
[i
]);
123 /* Handle a timeout. Timeout packets seem to occur even after */
124 /* we've gotten a valid reply to a TALK, so I'm assuming that */
125 /* a "timeout" is actually more like an "end-of-data" signal. */
126 /* We need to send back a timeout packet to the IOP to shut */
127 /* it up, plus complete the current request, if any. */
129 if (amsg
->flags
& ADB_IOP_TIMEOUT
) {
130 msg
->reply
[0] = ADB_IOP_TIMEOUT
| ADB_IOP_AUTOPOLL
;
133 if (req
&& (adb_iop_state
!= idle
)) {
134 adb_iop_end_req(req
, idle
);
137 /* TODO: is it possible for more than one chunk of data */
138 /* to arrive before the timeout? If so we need to */
139 /* use reply_ptr here like the other drivers do. */
140 if ((adb_iop_state
== awaiting_reply
) &&
141 (amsg
->flags
& ADB_IOP_EXPLICIT
)) {
142 req
->reply_len
= amsg
->count
+ 1;
143 memcpy(req
->reply
, &amsg
->cmd
, req
->reply_len
);
145 adb_input(&amsg
->cmd
, amsg
->count
+ 1,
146 amsg
->flags
& ADB_IOP_AUTOPOLL
);
148 memcpy(msg
->reply
, msg
->message
, IOP_MSG_LEN
);
150 iop_complete_message(msg
);
151 local_irq_restore(flags
);
155 * Start sending an ADB packet, IOP style
157 * There isn't much to do other than hand the packet over to the IOP
158 * after encapsulating it in an adb_iopmsg.
161 static void adb_iop_start(void)
164 struct adb_request
*req
;
165 struct adb_iopmsg amsg
;
170 /* get the packet to send */
174 local_irq_save(flags
);
177 printk("adb_iop_start %p: sending packet, %d bytes:", req
, req
->nbytes
);
178 for (i
= 0 ; i
< req
->nbytes
; i
++)
179 printk(" %02X", (uint
) req
->data
[i
]);
183 /* The IOP takes MacII-style packets, so */
184 /* strip the initial ADB_PACKET byte. */
186 amsg
.flags
= ADB_IOP_EXPLICIT
;
187 amsg
.count
= req
->nbytes
- 2;
189 /* amsg.data immediately follows amsg.cmd, effectively making */
190 /* amsg.cmd a pointer to the beginning of a full ADB packet. */
191 memcpy(&amsg
.cmd
, req
->data
+ 1, req
->nbytes
- 1);
194 adb_iop_state
= sending
;
195 local_irq_restore(flags
);
197 /* Now send it. The IOP manager will call adb_iop_complete */
198 /* when the packet has been sent. */
200 iop_send_message(ADB_IOP
, ADB_CHAN
, req
,
201 sizeof(amsg
), (__u8
*) &amsg
, adb_iop_complete
);
204 int adb_iop_probe(void)
206 if (!iop_ism_present
) return -ENODEV
;
210 int adb_iop_init(void)
212 printk("adb: IOP ISM driver v0.4 for Unified ADB.\n");
213 iop_listen(ADB_IOP
, ADB_CHAN
, adb_iop_listen
, "ADB");
217 int adb_iop_send_request(struct adb_request
*req
, int sync
)
221 err
= adb_iop_write(req
);
225 while (!req
->complete
) adb_iop_poll();
230 static int adb_iop_write(struct adb_request
*req
)
234 if ((req
->nbytes
< 2) || (req
->data
[0] != ADB_PACKET
)) {
239 local_irq_save(flags
);
246 if (current_req
!= 0) {
247 last_req
->next
= req
;
254 local_irq_restore(flags
);
255 if (adb_iop_state
== idle
) adb_iop_start();
259 int adb_iop_autopoll(int devs
)
261 /* TODO: how do we enable/disable autopoll? */
265 void adb_iop_poll(void)
267 if (adb_iop_state
== idle
) adb_iop_start();
268 iop_ism_irq(0, (void *) ADB_IOP
);
271 int adb_iop_reset_bus(void)
273 struct adb_request req
= {
276 .data
= { ADB_PACKET
, 0 },
280 while (!req
.complete
) {