blob: 314fc0830d9004d4a32998e6bb563c5719357822 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
David Woodhouse36874572006-01-14 00:15:19 +000020#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22struct preg {
23 unsigned char r;
24 char pad[15];
25};
26
27struct 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
61static volatile struct adb_regs __iomem *adb;
62static struct adb_request *current_req, *last_req;
63static DEFINE_SPINLOCK(macio_lock);
64
65static int macio_probe(void);
66static int macio_init(void);
67static irqreturn_t macio_adb_interrupt(int irq, void *arg, struct pt_regs *regs);
68static int macio_send_request(struct adb_request *req, int sync);
69static int macio_adb_autopoll(int devs);
70static void macio_adb_poll(void);
71static int macio_adb_reset_bus(void);
72
73struct 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
84int macio_probe(void)
85{
86 return find_compatible_devices("adb", "chrp,adb0")? 0: -ENODEV;
87}
88
89int macio_init(void)
90{
91 struct device_node *adbs;
David Woodhouse36874572006-01-14 00:15:19 +000092 struct resource r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 adbs = find_compatible_devices("adb", "chrp,adb0");
95 if (adbs == 0)
96 return -ENXIO;
97
98#if 0
David Woodhouse36874572006-01-14 00:15:19 +000099 { int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 printk("macio_adb_init: node = %p, addrs =", adbs->node);
David Woodhouse36874572006-01-14 00:15:19 +0000102 while(!of_address_to_resource(adbs, i, &r))
103 printk(" %x(%x)", r.start, r.end - r.start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 printk(", intrs =");
105 for (i = 0; i < adbs->n_intrs; ++i)
106 printk(" %x", adbs->intrs[i].line);
107 printk("\n"); }
108#endif
David Woodhouse36874572006-01-14 00:15:19 +0000109 if (of_address_to_resource(adbs, 0, &r))
110 return -ENXIO;
111 adb = ioremap(r.start, sizeof(struct adb_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 out_8(&adb->ctrl.r, 0);
114 out_8(&adb->intr.r, 0);
115 out_8(&adb->error.r, 0);
116 out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
117 out_8(&adb->active_lo.r, 0xff);
118 out_8(&adb->autopoll.r, APE);
119
120 if (request_irq(adbs->intrs[0].line, macio_adb_interrupt,
121 0, "ADB", (void *)0)) {
122 printk(KERN_ERR "ADB: can't get irq %d\n",
123 adbs->intrs[0].line);
124 return -EAGAIN;
125 }
126 out_8(&adb->intr_enb.r, DFB | TAG);
127
128 printk("adb: mac-io driver 1.0 for unified ADB\n");
129
130 return 0;
131}
132
133static int macio_adb_autopoll(int devs)
134{
135 unsigned long flags;
136
137 spin_lock_irqsave(&macio_lock, flags);
138 out_8(&adb->active_hi.r, devs >> 8);
139 out_8(&adb->active_lo.r, devs);
140 out_8(&adb->autopoll.r, devs? APE: 0);
141 spin_unlock_irqrestore(&macio_lock, flags);
142 return 0;
143}
144
145static int macio_adb_reset_bus(void)
146{
147 unsigned long flags;
148 int timeout = 1000000;
149
150 /* Hrm... we may want to not lock interrupts for so
151 * long ... oh well, who uses that chip anyway ? :)
152 * That function will be seldomly used during boot
153 * on rare machines, so...
154 */
155 spin_lock_irqsave(&macio_lock, flags);
156 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
157 while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
158 if (--timeout == 0) {
159 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
160 return -1;
161 }
162 }
163 spin_unlock_irqrestore(&macio_lock, flags);
164 return 0;
165}
166
167/* Send an ADB command */
168static int macio_send_request(struct adb_request *req, int sync)
169{
170 unsigned long flags;
171 int i;
172
173 if (req->data[0] != ADB_PACKET)
174 return -EINVAL;
175
176 for (i = 0; i < req->nbytes - 1; ++i)
177 req->data[i] = req->data[i+1];
178 --req->nbytes;
179
180 req->next = NULL;
181 req->sent = 0;
182 req->complete = 0;
183 req->reply_len = 0;
184
185 spin_lock_irqsave(&macio_lock, flags);
186 if (current_req != 0) {
187 last_req->next = req;
188 last_req = req;
189 } else {
190 current_req = last_req = req;
191 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
192 }
193 spin_unlock_irqrestore(&macio_lock, flags);
194
195 if (sync) {
196 while (!req->complete)
197 macio_adb_poll();
198 }
199
200 return 0;
201}
202
203static irqreturn_t macio_adb_interrupt(int irq, void *arg,
204 struct pt_regs *regs)
205{
206 int i, n, err;
207 struct adb_request *req = NULL;
208 unsigned char ibuf[16];
209 int ibuf_len = 0;
210 int complete = 0;
211 int autopoll = 0;
212 int handled = 0;
213
214 spin_lock(&macio_lock);
215 if (in_8(&adb->intr.r) & TAG) {
216 handled = 1;
217 if ((req = current_req) != 0) {
218 /* put the current request in */
219 for (i = 0; i < req->nbytes; ++i)
220 out_8(&adb->data[i].r, req->data[i]);
221 out_8(&adb->dcount.r, req->nbytes & HMB);
222 req->sent = 1;
223 if (req->reply_expected) {
224 out_8(&adb->ctrl.r, DTB + CRE);
225 } else {
226 out_8(&adb->ctrl.r, DTB);
227 current_req = req->next;
228 complete = 1;
229 if (current_req)
230 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
231 }
232 }
233 out_8(&adb->intr.r, 0);
234 }
235
236 if (in_8(&adb->intr.r) & DFB) {
237 handled = 1;
238 err = in_8(&adb->error.r);
239 if (current_req && current_req->sent) {
240 /* this is the response to a command */
241 req = current_req;
242 if (err == 0) {
243 req->reply_len = in_8(&adb->dcount.r) & HMB;
244 for (i = 0; i < req->reply_len; ++i)
245 req->reply[i] = in_8(&adb->data[i].r);
246 }
247 current_req = req->next;
248 complete = 1;
249 if (current_req)
250 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
251 } else if (err == 0) {
252 /* autopoll data */
253 n = in_8(&adb->dcount.r) & HMB;
254 for (i = 0; i < n; ++i)
255 ibuf[i] = in_8(&adb->data[i].r);
256 ibuf_len = n;
257 autopoll = (in_8(&adb->dcount.r) & APD) != 0;
258 }
259 out_8(&adb->error.r, 0);
260 out_8(&adb->intr.r, 0);
261 }
262 spin_unlock(&macio_lock);
263 if (complete && req) {
264 void (*done)(struct adb_request *) = req->done;
265 mb();
266 req->complete = 1;
267 /* Here, we assume that if the request has a done member, the
268 * struct request will survive to setting req->complete to 1
269 */
270 if (done)
271 (*done)(req);
272 }
273 if (ibuf_len)
274 adb_input(ibuf, ibuf_len, regs, autopoll);
275
276 return IRQ_RETVAL(handled);
277}
278
279static void macio_adb_poll(void)
280{
281 unsigned long flags;
282
283 local_irq_save(flags);
284 if (in_8(&adb->intr.r) != 0)
285 macio_adb_interrupt(0, NULL, NULL);
286 local_irq_restore(flags);
287}