Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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. |
| 5 | * |
| 6 | * 1999-07-01 (jmt) - First implementation for new driver architecture. |
| 7 | * |
| 8 | * 1999-07-31 (jmt) - First working version. |
| 9 | * |
| 10 | * TODO: |
| 11 | * |
| 12 | * o Implement SRQ handling. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/proc_fs.h> |
| 21 | |
| 22 | #include <asm/bootinfo.h> |
| 23 | #include <asm/macintosh.h> |
| 24 | #include <asm/macints.h> |
| 25 | #include <asm/mac_iop.h> |
| 26 | #include <asm/mac_oss.h> |
| 27 | #include <asm/adb_iop.h> |
| 28 | |
| 29 | #include <linux/adb.h> |
| 30 | |
| 31 | /*#define DEBUG_ADB_IOP*/ |
| 32 | |
| 33 | extern void iop_ism_irq(int, void *, struct pt_regs *); |
| 34 | |
| 35 | static struct adb_request *current_req; |
| 36 | static struct adb_request *last_req; |
| 37 | #if 0 |
| 38 | static unsigned char reply_buff[16]; |
| 39 | static unsigned char *reply_ptr; |
| 40 | #endif |
| 41 | |
| 42 | static enum adb_iop_state { |
| 43 | idle, |
| 44 | sending, |
| 45 | awaiting_reply |
| 46 | } adb_iop_state; |
| 47 | |
| 48 | static void adb_iop_start(void); |
| 49 | static int adb_iop_probe(void); |
| 50 | static int adb_iop_init(void); |
| 51 | static int adb_iop_send_request(struct adb_request *, int); |
| 52 | static int adb_iop_write(struct adb_request *); |
| 53 | static int adb_iop_autopoll(int); |
| 54 | static void adb_iop_poll(void); |
| 55 | static int adb_iop_reset_bus(void); |
| 56 | |
| 57 | struct adb_driver adb_iop_driver = { |
| 58 | "ISM IOP", |
| 59 | adb_iop_probe, |
| 60 | adb_iop_init, |
| 61 | adb_iop_send_request, |
| 62 | adb_iop_autopoll, |
| 63 | adb_iop_poll, |
| 64 | adb_iop_reset_bus |
| 65 | }; |
| 66 | |
| 67 | static void adb_iop_end_req(struct adb_request *req, int state) |
| 68 | { |
| 69 | req->complete = 1; |
| 70 | current_req = req->next; |
| 71 | if (req->done) (*req->done)(req); |
| 72 | adb_iop_state = state; |
| 73 | } |
| 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 | |
| 81 | static void adb_iop_complete(struct iop_msg *msg, struct pt_regs *regs) |
| 82 | { |
| 83 | struct adb_request *req; |
| 84 | uint flags; |
| 85 | |
| 86 | local_irq_save(flags); |
| 87 | |
| 88 | req = current_req; |
| 89 | if ((adb_iop_state == sending) && req && req->reply_expected) { |
| 90 | adb_iop_state = awaiting_reply; |
| 91 | } |
| 92 | |
| 93 | local_irq_restore(flags); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Listen for ADB messages from the IOP. |
| 98 | * |
| 99 | * This will be called when unsolicited messages (usually replies to TALK |
| 100 | * commands or autopoll packets) are received. |
| 101 | */ |
| 102 | |
| 103 | static void adb_iop_listen(struct iop_msg *msg, struct pt_regs *regs) |
| 104 | { |
| 105 | struct adb_iopmsg *amsg = (struct adb_iopmsg *) msg->message; |
| 106 | struct adb_request *req; |
| 107 | uint flags; |
| 108 | #ifdef DEBUG_ADB_IOP |
| 109 | int i; |
| 110 | #endif |
| 111 | |
| 112 | local_irq_save(flags); |
| 113 | |
| 114 | req = current_req; |
| 115 | |
| 116 | #ifdef DEBUG_ADB_IOP |
| 117 | printk("adb_iop_listen %p: rcvd packet, %d bytes: %02X %02X", req, |
| 118 | (uint) amsg->count + 2, (uint) amsg->flags, (uint) amsg->cmd); |
| 119 | for (i = 0; i < amsg->count; i++) |
| 120 | printk(" %02X", (uint) amsg->data[i]); |
| 121 | printk("\n"); |
| 122 | #endif |
| 123 | |
| 124 | /* Handle a timeout. Timeout packets seem to occur even after */ |
| 125 | /* we've gotten a valid reply to a TALK, so I'm assuming that */ |
| 126 | /* a "timeout" is actually more like an "end-of-data" signal. */ |
| 127 | /* We need to send back a timeout packet to the IOP to shut */ |
| 128 | /* it up, plus complete the current request, if any. */ |
| 129 | |
| 130 | if (amsg->flags & ADB_IOP_TIMEOUT) { |
| 131 | msg->reply[0] = ADB_IOP_TIMEOUT | ADB_IOP_AUTOPOLL; |
| 132 | msg->reply[1] = 0; |
| 133 | msg->reply[2] = 0; |
| 134 | if (req && (adb_iop_state != idle)) { |
| 135 | adb_iop_end_req(req, idle); |
| 136 | } |
| 137 | } else { |
| 138 | /* TODO: is it possible for more than one chunk of data */ |
| 139 | /* to arrive before the timeout? If so we need to */ |
| 140 | /* use reply_ptr here like the other drivers do. */ |
| 141 | if ((adb_iop_state == awaiting_reply) && |
| 142 | (amsg->flags & ADB_IOP_EXPLICIT)) { |
| 143 | req->reply_len = amsg->count + 1; |
| 144 | memcpy(req->reply, &amsg->cmd, req->reply_len); |
| 145 | } else { |
| 146 | adb_input(&amsg->cmd, amsg->count + 1, regs, |
| 147 | amsg->flags & ADB_IOP_AUTOPOLL); |
| 148 | } |
| 149 | memcpy(msg->reply, msg->message, IOP_MSG_LEN); |
| 150 | } |
| 151 | iop_complete_message(msg); |
| 152 | local_irq_restore(flags); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Start sending an ADB packet, IOP style |
| 157 | * |
| 158 | * There isn't much to do other than hand the packet over to the IOP |
| 159 | * after encapsulating it in an adb_iopmsg. |
| 160 | */ |
| 161 | |
| 162 | static void adb_iop_start(void) |
| 163 | { |
| 164 | unsigned long flags; |
| 165 | struct adb_request *req; |
| 166 | struct adb_iopmsg amsg; |
| 167 | #ifdef DEBUG_ADB_IOP |
| 168 | int i; |
| 169 | #endif |
| 170 | |
| 171 | /* get the packet to send */ |
| 172 | req = current_req; |
| 173 | if (!req) return; |
| 174 | |
| 175 | local_irq_save(flags); |
| 176 | |
| 177 | #ifdef DEBUG_ADB_IOP |
| 178 | printk("adb_iop_start %p: sending packet, %d bytes:", req, req->nbytes); |
| 179 | for (i = 0 ; i < req->nbytes ; i++) |
| 180 | printk(" %02X", (uint) req->data[i]); |
| 181 | printk("\n"); |
| 182 | #endif |
| 183 | |
| 184 | /* The IOP takes MacII-style packets, so */ |
| 185 | /* strip the initial ADB_PACKET byte. */ |
| 186 | |
| 187 | amsg.flags = ADB_IOP_EXPLICIT; |
| 188 | amsg.count = req->nbytes - 2; |
| 189 | |
| 190 | /* amsg.data immediately follows amsg.cmd, effectively making */ |
| 191 | /* amsg.cmd a pointer to the beginning of a full ADB packet. */ |
| 192 | memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1); |
| 193 | |
| 194 | req->sent = 1; |
| 195 | adb_iop_state = sending; |
| 196 | local_irq_restore(flags); |
| 197 | |
| 198 | /* Now send it. The IOP manager will call adb_iop_complete */ |
| 199 | /* when the packet has been sent. */ |
| 200 | |
| 201 | iop_send_message(ADB_IOP, ADB_CHAN, req, |
| 202 | sizeof(amsg), (__u8 *) &amsg, adb_iop_complete); |
| 203 | } |
| 204 | |
| 205 | int adb_iop_probe(void) |
| 206 | { |
| 207 | if (!iop_ism_present) return -ENODEV; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int adb_iop_init(void) |
| 212 | { |
| 213 | printk("adb: IOP ISM driver v0.4 for Unified ADB.\n"); |
| 214 | iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB"); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | int adb_iop_send_request(struct adb_request *req, int sync) |
| 219 | { |
| 220 | int err; |
| 221 | |
| 222 | err = adb_iop_write(req); |
| 223 | if (err) return err; |
| 224 | |
| 225 | if (sync) { |
| 226 | while (!req->complete) adb_iop_poll(); |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int adb_iop_write(struct adb_request *req) |
| 232 | { |
| 233 | unsigned long flags; |
| 234 | |
| 235 | if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) { |
| 236 | req->complete = 1; |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | |
| 240 | local_irq_save(flags); |
| 241 | |
| 242 | req->next = 0; |
| 243 | req->sent = 0; |
| 244 | req->complete = 0; |
| 245 | req->reply_len = 0; |
| 246 | |
| 247 | if (current_req != 0) { |
| 248 | last_req->next = req; |
| 249 | last_req = req; |
| 250 | } else { |
| 251 | current_req = req; |
| 252 | last_req = req; |
| 253 | } |
| 254 | |
| 255 | local_irq_restore(flags); |
| 256 | if (adb_iop_state == idle) adb_iop_start(); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | int adb_iop_autopoll(int devs) |
| 261 | { |
| 262 | /* TODO: how do we enable/disable autopoll? */ |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | void adb_iop_poll(void) |
| 267 | { |
| 268 | if (adb_iop_state == idle) adb_iop_start(); |
| 269 | iop_ism_irq(0, (void *) ADB_IOP, NULL); |
| 270 | } |
| 271 | |
| 272 | int adb_iop_reset_bus(void) |
| 273 | { |
| 274 | struct adb_request req = { |
| 275 | .reply_expected = 0, |
| 276 | .nbytes = 2, |
| 277 | .data = { ADB_PACKET, 0 }, |
| 278 | }; |
| 279 | |
| 280 | adb_iop_write(&req); |
| 281 | while (!req.complete) { |
| 282 | adb_iop_poll(); |
| 283 | schedule(); |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |