Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 18 | #include <asm/macintosh.h> |
| 19 | #include <asm/macints.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/mac_iop.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/adb_iop.h> |
Finn Thain | c396dd2 | 2021-01-18 17:19:40 +1100 | [diff] [blame] | 22 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 24 | #include <linux/adb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static struct adb_request *current_req; |
| 27 | static struct adb_request *last_req; |
Finn Thain | c66da95 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 28 | static unsigned int autopoll_devs; |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 29 | static u8 autopoll_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | static enum adb_iop_state { |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 32 | idle, |
| 33 | sending, |
| 34 | awaiting_reply |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } adb_iop_state; |
| 36 | |
| 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); |
| 45 | |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 46 | /* ADB command byte structure */ |
| 47 | #define ADDR_MASK 0xF0 |
| 48 | #define OP_MASK 0x0C |
| 49 | #define TALK 0x0C |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | struct adb_driver adb_iop_driver = { |
Finn Thain | 3a52f6f | 2018-03-29 11:36:04 +1100 | [diff] [blame] | 52 | .name = "ISM IOP", |
| 53 | .probe = adb_iop_probe, |
| 54 | .init = adb_iop_init, |
| 55 | .send_request = adb_iop_send_request, |
| 56 | .autopoll = adb_iop_autopoll, |
| 57 | .poll = adb_iop_poll, |
| 58 | .reset_bus = adb_iop_reset_bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Finn Thain | 32226e8 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 61 | static void adb_iop_done(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Finn Thain | 32226e8 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 63 | struct adb_request *req = current_req; |
| 64 | |
| 65 | adb_iop_state = idle; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | req->complete = 1; |
| 68 | current_req = req->next; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 69 | if (req->done) |
| 70 | (*req->done)(req); |
Finn Thain | 32226e8 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 71 | |
| 72 | if (adb_iop_state == idle) |
| 73 | adb_iop_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Completion routine for ADB commands sent to the IOP. |
| 78 | * |
| 79 | * This will be called when a packet has been successfully sent. |
| 80 | */ |
| 81 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 82 | static void adb_iop_complete(struct iop_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
Geert Uytterhoeven | 38b7a2a | 2010-11-11 14:05:06 -0800 | [diff] [blame] | 84 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | local_irq_save(flags); |
| 87 | |
Finn Thain | 2c9cfba | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 88 | adb_iop_state = awaiting_reply; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | local_irq_restore(flags); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Listen for ADB messages from the IOP. |
| 95 | * |
Finn Thain | 2c9cfba | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | */ |
| 100 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 101 | static void adb_iop_listen(struct iop_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 103 | struct adb_iopmsg *amsg = (struct adb_iopmsg *)msg->message; |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 104 | u8 addr = (amsg->cmd & ADDR_MASK) >> 4; |
| 105 | u8 op = amsg->cmd & OP_MASK; |
Geert Uytterhoeven | 38b7a2a | 2010-11-11 14:05:06 -0800 | [diff] [blame] | 106 | unsigned long flags; |
Finn Thain | 32226e8 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 107 | bool req_done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | local_irq_save(flags); |
| 110 | |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 111 | /* Responses to Talk commands may be unsolicited as they are |
| 112 | * produced when the IOP polls devices. They are mostly timeouts. |
Finn Thain | ff785e1 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 113 | */ |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 114 | if (op == TALK && ((1 << addr) & autopoll_devs)) |
| 115 | autopoll_addr = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 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: |
Finn Thain | 32226e8 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 122 | if (adb_iop_state == awaiting_reply) { |
| 123 | struct adb_request *req = current_req; |
| 124 | |
Finn Thain | 2c9cfba | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 125 | if (req->reply_expected) { |
| 126 | req->reply_len = amsg->count + 1; |
| 127 | memcpy(req->reply, &amsg->cmd, req->reply_len); |
| 128 | } |
Finn Thain | 32226e8 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 129 | |
| 130 | req_done = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | iop_complete_message(msg); |
Finn Thain | 32226e8 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 143 | |
| 144 | if (req_done) |
| 145 | adb_iop_done(); |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | local_irq_restore(flags); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Start sending an ADB packet, IOP style |
| 152 | * |
| 153 | * There isn't much to do other than hand the packet over to the IOP |
| 154 | * after encapsulating it in an adb_iopmsg. |
| 155 | */ |
| 156 | |
| 157 | static void adb_iop_start(void) |
| 158 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | struct adb_request *req; |
| 160 | struct adb_iopmsg amsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | /* get the packet to send */ |
| 163 | req = current_req; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 164 | if (!req) |
| 165 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Finn Thain | ff785e1 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 167 | /* The IOP takes MacII-style packets, so strip the initial |
| 168 | * ADB_PACKET byte. |
| 169 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | amsg.flags = ADB_IOP_EXPLICIT; |
| 171 | amsg.count = req->nbytes - 2; |
| 172 | |
Finn Thain | ff785e1 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 173 | /* amsg.data immediately follows amsg.cmd, effectively making |
| 174 | * &amsg.cmd a pointer to the beginning of a full ADB packet. |
| 175 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1); |
| 177 | |
| 178 | req->sent = 1; |
| 179 | adb_iop_state = sending; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Finn Thain | ff785e1 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 181 | /* Now send it. The IOP manager will call adb_iop_complete |
| 182 | * when the message has been sent. |
| 183 | */ |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 184 | iop_send_message(ADB_IOP, ADB_CHAN, req, sizeof(amsg), (__u8 *)&amsg, |
| 185 | adb_iop_complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Finn Thain | 56b732e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 188 | static int adb_iop_probe(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 190 | if (!iop_ism_present) |
| 191 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
Finn Thain | 56b732e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 195 | static int adb_iop_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Finn Thain | 351e5ad | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 197 | pr_info("adb: IOP ISM driver v0.4 for Unified ADB\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB"); |
| 199 | return 0; |
| 200 | } |
| 201 | |
Finn Thain | 56b732e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 202 | static int adb_iop_send_request(struct adb_request *req, int sync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | int err; |
| 205 | |
| 206 | err = adb_iop_write(req); |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 207 | if (err) |
| 208 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | if (sync) { |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 211 | while (!req->complete) |
| 212 | adb_iop_poll(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int adb_iop_write(struct adb_request *req) |
| 218 | { |
| 219 | unsigned long flags; |
| 220 | |
| 221 | if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) { |
| 222 | req->complete = 1; |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
Al Viro | a5d361f | 2006-01-12 01:06:34 -0800 | [diff] [blame] | 226 | req->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | req->sent = 0; |
| 228 | req->complete = 0; |
| 229 | req->reply_len = 0; |
| 230 | |
Finn Thain | aac840e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 231 | local_irq_save(flags); |
| 232 | |
Finn Thain | 56b732e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 233 | if (current_req) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | last_req->next = req; |
| 235 | last_req = req; |
| 236 | } else { |
| 237 | current_req = req; |
| 238 | last_req = req; |
| 239 | } |
| 240 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 241 | if (adb_iop_state == idle) |
| 242 | adb_iop_start(); |
Finn Thain | aac840e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 243 | |
| 244 | local_irq_restore(flags); |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Finn Thain | c66da95 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 249 | static void adb_iop_set_ap_complete(struct iop_msg *msg) |
| 250 | { |
| 251 | struct adb_iopmsg *amsg = (struct adb_iopmsg *)msg->message; |
| 252 | |
Finn Thain | c396dd2 | 2021-01-18 17:19:40 +1100 | [diff] [blame] | 253 | autopoll_devs = get_unaligned_be16(amsg->data); |
Finn Thain | 10199e9 | 2020-11-20 15:39:56 +1100 | [diff] [blame] | 254 | if (autopoll_devs & (1 << autopoll_addr)) |
| 255 | return; |
| 256 | autopoll_addr = autopoll_devs ? (ffs(autopoll_devs) - 1) : 0; |
Finn Thain | c66da95 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 257 | } |
| 258 | |
Finn Thain | 56b732e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 259 | static int adb_iop_autopoll(int devs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Finn Thain | c66da95 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 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; |
Finn Thain | c396dd2 | 2021-01-18 17:19:40 +1100 | [diff] [blame] | 270 | put_unaligned_be16(mask, amsg.data); |
Finn Thain | c66da95 | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
Finn Thain | 56b732e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 280 | static void adb_iop_poll(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Finn Thain | 92178fc | 2017-10-26 22:45:24 -0400 | [diff] [blame] | 282 | iop_ism_irq_poll(ADB_IOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Finn Thain | 56b732e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 285 | static int adb_iop_reset_bus(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Finn Thain | 303511e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 287 | struct adb_request req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Finn Thain | 303511e | 2020-05-31 09:17:03 +1000 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |