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