Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the ADB controller in the Mac I/O (Hydra) chip. |
| 3 | */ |
| 4 | #include <stdarg.h> |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <asm/prom.h> |
| 12 | #include <linux/adb.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/pgtable.h> |
| 15 | #include <asm/hydra.h> |
| 16 | #include <asm/irq.h> |
| 17 | #include <asm/system.h> |
| 18 | #include <linux/init.h> |
David Woodhouse | 3687457 | 2006-01-14 00:15:19 +0000 | [diff] [blame] | 19 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | struct preg { |
| 22 | unsigned char r; |
| 23 | char pad[15]; |
| 24 | }; |
| 25 | |
| 26 | struct adb_regs { |
| 27 | struct preg intr; |
| 28 | struct preg data[9]; |
| 29 | struct preg intr_enb; |
| 30 | struct preg dcount; |
| 31 | struct preg error; |
| 32 | struct preg ctrl; |
| 33 | struct preg autopoll; |
| 34 | struct preg active_hi; |
| 35 | struct preg active_lo; |
| 36 | struct preg test; |
| 37 | }; |
| 38 | |
| 39 | /* Bits in intr and intr_enb registers */ |
| 40 | #define DFB 1 /* data from bus */ |
| 41 | #define TAG 2 /* transfer access grant */ |
| 42 | |
| 43 | /* Bits in dcount register */ |
| 44 | #define HMB 0x0f /* how many bytes */ |
| 45 | #define APD 0x10 /* auto-poll data */ |
| 46 | |
| 47 | /* Bits in error register */ |
| 48 | #define NRE 1 /* no response error */ |
| 49 | #define DLE 2 /* data lost error */ |
| 50 | |
| 51 | /* Bits in ctrl register */ |
| 52 | #define TAR 1 /* transfer access request */ |
| 53 | #define DTB 2 /* data to bus */ |
| 54 | #define CRE 4 /* command response expected */ |
| 55 | #define ADB_RST 8 /* ADB reset */ |
| 56 | |
| 57 | /* Bits in autopoll register */ |
| 58 | #define APE 1 /* autopoll enable */ |
| 59 | |
| 60 | static volatile struct adb_regs __iomem *adb; |
| 61 | static struct adb_request *current_req, *last_req; |
| 62 | static DEFINE_SPINLOCK(macio_lock); |
| 63 | |
| 64 | static int macio_probe(void); |
| 65 | static int macio_init(void); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 66 | static irqreturn_t macio_adb_interrupt(int irq, void *arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static int macio_send_request(struct adb_request *req, int sync); |
| 68 | static int macio_adb_autopoll(int devs); |
| 69 | static void macio_adb_poll(void); |
| 70 | static int macio_adb_reset_bus(void); |
| 71 | |
| 72 | struct adb_driver macio_adb_driver = { |
| 73 | "MACIO", |
| 74 | macio_probe, |
| 75 | macio_init, |
| 76 | macio_send_request, |
| 77 | /*macio_write,*/ |
| 78 | macio_adb_autopoll, |
| 79 | macio_adb_poll, |
| 80 | macio_adb_reset_bus |
| 81 | }; |
| 82 | |
| 83 | int macio_probe(void) |
| 84 | { |
| 85 | return find_compatible_devices("adb", "chrp,adb0")? 0: -ENODEV; |
| 86 | } |
| 87 | |
| 88 | int macio_init(void) |
| 89 | { |
| 90 | struct device_node *adbs; |
David Woodhouse | 3687457 | 2006-01-14 00:15:19 +0000 | [diff] [blame] | 91 | struct resource r; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 92 | unsigned int irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | adbs = find_compatible_devices("adb", "chrp,adb0"); |
| 95 | if (adbs == 0) |
| 96 | return -ENXIO; |
| 97 | |
David Woodhouse | 3687457 | 2006-01-14 00:15:19 +0000 | [diff] [blame] | 98 | if (of_address_to_resource(adbs, 0, &r)) |
| 99 | return -ENXIO; |
| 100 | adb = ioremap(r.start, sizeof(struct adb_regs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | out_8(&adb->ctrl.r, 0); |
| 103 | out_8(&adb->intr.r, 0); |
| 104 | out_8(&adb->error.r, 0); |
| 105 | out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */ |
| 106 | out_8(&adb->active_lo.r, 0xff); |
| 107 | out_8(&adb->autopoll.r, APE); |
| 108 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 109 | irq = irq_of_parse_and_map(adbs, 0); |
| 110 | if (request_irq(irq, macio_adb_interrupt, 0, "ADB", (void *)0)) { |
| 111 | printk(KERN_ERR "ADB: can't get irq %d\n", irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return -EAGAIN; |
| 113 | } |
| 114 | out_8(&adb->intr_enb.r, DFB | TAG); |
| 115 | |
| 116 | printk("adb: mac-io driver 1.0 for unified ADB\n"); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int macio_adb_autopoll(int devs) |
| 122 | { |
| 123 | unsigned long flags; |
| 124 | |
| 125 | spin_lock_irqsave(&macio_lock, flags); |
| 126 | out_8(&adb->active_hi.r, devs >> 8); |
| 127 | out_8(&adb->active_lo.r, devs); |
| 128 | out_8(&adb->autopoll.r, devs? APE: 0); |
| 129 | spin_unlock_irqrestore(&macio_lock, flags); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int macio_adb_reset_bus(void) |
| 134 | { |
| 135 | unsigned long flags; |
| 136 | int timeout = 1000000; |
| 137 | |
| 138 | /* Hrm... we may want to not lock interrupts for so |
| 139 | * long ... oh well, who uses that chip anyway ? :) |
| 140 | * That function will be seldomly used during boot |
| 141 | * on rare machines, so... |
| 142 | */ |
| 143 | spin_lock_irqsave(&macio_lock, flags); |
| 144 | out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST); |
| 145 | while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) { |
| 146 | if (--timeout == 0) { |
| 147 | out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST); |
| 148 | return -1; |
| 149 | } |
| 150 | } |
| 151 | spin_unlock_irqrestore(&macio_lock, flags); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* Send an ADB command */ |
| 156 | static int macio_send_request(struct adb_request *req, int sync) |
| 157 | { |
| 158 | unsigned long flags; |
| 159 | int i; |
| 160 | |
| 161 | if (req->data[0] != ADB_PACKET) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | for (i = 0; i < req->nbytes - 1; ++i) |
| 165 | req->data[i] = req->data[i+1]; |
| 166 | --req->nbytes; |
| 167 | |
| 168 | req->next = NULL; |
| 169 | req->sent = 0; |
| 170 | req->complete = 0; |
| 171 | req->reply_len = 0; |
| 172 | |
| 173 | spin_lock_irqsave(&macio_lock, flags); |
| 174 | if (current_req != 0) { |
| 175 | last_req->next = req; |
| 176 | last_req = req; |
| 177 | } else { |
| 178 | current_req = last_req = req; |
| 179 | out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR); |
| 180 | } |
| 181 | spin_unlock_irqrestore(&macio_lock, flags); |
| 182 | |
| 183 | if (sync) { |
| 184 | while (!req->complete) |
| 185 | macio_adb_poll(); |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 191 | static irqreturn_t macio_adb_interrupt(int irq, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | int i, n, err; |
| 194 | struct adb_request *req = NULL; |
| 195 | unsigned char ibuf[16]; |
| 196 | int ibuf_len = 0; |
| 197 | int complete = 0; |
| 198 | int autopoll = 0; |
| 199 | int handled = 0; |
| 200 | |
| 201 | spin_lock(&macio_lock); |
| 202 | if (in_8(&adb->intr.r) & TAG) { |
| 203 | handled = 1; |
| 204 | if ((req = current_req) != 0) { |
| 205 | /* put the current request in */ |
| 206 | for (i = 0; i < req->nbytes; ++i) |
| 207 | out_8(&adb->data[i].r, req->data[i]); |
| 208 | out_8(&adb->dcount.r, req->nbytes & HMB); |
| 209 | req->sent = 1; |
| 210 | if (req->reply_expected) { |
| 211 | out_8(&adb->ctrl.r, DTB + CRE); |
| 212 | } else { |
| 213 | out_8(&adb->ctrl.r, DTB); |
| 214 | current_req = req->next; |
| 215 | complete = 1; |
| 216 | if (current_req) |
| 217 | out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR); |
| 218 | } |
| 219 | } |
| 220 | out_8(&adb->intr.r, 0); |
| 221 | } |
| 222 | |
| 223 | if (in_8(&adb->intr.r) & DFB) { |
| 224 | handled = 1; |
| 225 | err = in_8(&adb->error.r); |
| 226 | if (current_req && current_req->sent) { |
| 227 | /* this is the response to a command */ |
| 228 | req = current_req; |
| 229 | if (err == 0) { |
| 230 | req->reply_len = in_8(&adb->dcount.r) & HMB; |
| 231 | for (i = 0; i < req->reply_len; ++i) |
| 232 | req->reply[i] = in_8(&adb->data[i].r); |
| 233 | } |
| 234 | current_req = req->next; |
| 235 | complete = 1; |
| 236 | if (current_req) |
| 237 | out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR); |
| 238 | } else if (err == 0) { |
| 239 | /* autopoll data */ |
| 240 | n = in_8(&adb->dcount.r) & HMB; |
| 241 | for (i = 0; i < n; ++i) |
| 242 | ibuf[i] = in_8(&adb->data[i].r); |
| 243 | ibuf_len = n; |
| 244 | autopoll = (in_8(&adb->dcount.r) & APD) != 0; |
| 245 | } |
| 246 | out_8(&adb->error.r, 0); |
| 247 | out_8(&adb->intr.r, 0); |
| 248 | } |
| 249 | spin_unlock(&macio_lock); |
| 250 | if (complete && req) { |
| 251 | void (*done)(struct adb_request *) = req->done; |
| 252 | mb(); |
| 253 | req->complete = 1; |
| 254 | /* Here, we assume that if the request has a done member, the |
| 255 | * struct request will survive to setting req->complete to 1 |
| 256 | */ |
| 257 | if (done) |
| 258 | (*done)(req); |
| 259 | } |
| 260 | if (ibuf_len) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 261 | adb_input(ibuf, ibuf_len, autopoll); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | return IRQ_RETVAL(handled); |
| 264 | } |
| 265 | |
| 266 | static void macio_adb_poll(void) |
| 267 | { |
| 268 | unsigned long flags; |
| 269 | |
| 270 | local_irq_save(flags); |
| 271 | if (in_8(&adb->intr.r) != 0) |
Al Viro | 9da3b1a | 2006-10-08 15:00:44 +0100 | [diff] [blame] | 272 | macio_adb_interrupt(0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | local_irq_restore(flags); |
| 274 | } |