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