MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / macintosh / macio-adb.c
blob5283a6d11b45115b5d9907e45eeea2bc50ead36f
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>
21 struct preg {
22 unsigned char r;
23 char pad[15];
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;
39 /* Bits in intr and intr_enb registers */
40 #define DFB 1 /* data from bus */
41 #define TAG 2 /* transfer access grant */
43 /* Bits in dcount register */
44 #define HMB 0x0f /* how many bytes */
45 #define APD 0x10 /* auto-poll data */
47 /* Bits in error register */
48 #define NRE 1 /* no response error */
49 #define DLE 2 /* data lost error */
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 */
57 /* Bits in autopoll register */
58 #define APE 1 /* autopoll enable */
60 static volatile struct adb_regs *adb;
61 static struct adb_request *current_req, *last_req;
62 static spinlock_t macio_lock = SPIN_LOCK_UNLOCKED;
64 static int macio_probe(void);
65 static int macio_init(void);
66 static irqreturn_t macio_adb_interrupt(int irq, void *arg, struct pt_regs *regs);
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);
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
83 int macio_probe(void)
85 return find_compatible_devices("adb", "chrp,adb0")? 0: -ENODEV;
88 int macio_init(void)
90 struct device_node *adbs;
92 adbs = find_compatible_devices("adb", "chrp,adb0");
93 if (adbs == 0)
94 return -ENXIO;
96 #if 0
97 { int i;
99 printk("macio_adb_init: node = %p, addrs =", adbs->node);
100 for (i = 0; i < adbs->n_addrs; ++i)
101 printk(" %x(%x)", adbs->addrs[i].address, adbs->addrs[i].size);
102 printk(", intrs =");
103 for (i = 0; i < adbs->n_intrs; ++i)
104 printk(" %x", adbs->intrs[i].line);
105 printk("\n"); }
106 #endif
108 adb = (volatile struct adb_regs *)
109 ioremap(adbs->addrs->address, sizeof(struct adb_regs));
111 out_8(&adb->ctrl.r, 0);
112 out_8(&adb->intr.r, 0);
113 out_8(&adb->error.r, 0);
114 out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
115 out_8(&adb->active_lo.r, 0xff);
116 out_8(&adb->autopoll.r, APE);
118 if (request_irq(adbs->intrs[0].line, macio_adb_interrupt,
119 0, "ADB", (void *)0)) {
120 printk(KERN_ERR "ADB: can't get irq %d\n",
121 adbs->intrs[0].line);
122 return -EAGAIN;
124 out_8(&adb->intr_enb.r, DFB | TAG);
126 printk("adb: mac-io driver 1.0 for unified ADB\n");
128 return 0;
131 static int macio_adb_autopoll(int devs)
133 unsigned long flags;
135 spin_lock_irqsave(&macio_lock, flags);
136 out_8(&adb->active_hi.r, devs >> 8);
137 out_8(&adb->active_lo.r, devs);
138 out_8(&adb->autopoll.r, devs? APE: 0);
139 spin_unlock_irqrestore(&macio_lock, flags);
140 return 0;
143 static int macio_adb_reset_bus(void)
145 unsigned long flags;
146 int timeout = 1000000;
148 /* Hrm... we may want to not lock interrupts for so
149 * long ... oh well, who uses that chip anyway ? :)
150 * That function will be seldomly used during boot
151 * on rare machines, so...
153 spin_lock_irqsave(&macio_lock, flags);
154 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
155 while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
156 if (--timeout == 0) {
157 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
158 return -1;
161 spin_unlock_irqrestore(&macio_lock, flags);
162 return 0;
165 /* Send an ADB command */
166 static int macio_send_request(struct adb_request *req, int sync)
168 unsigned long flags;
169 int i;
171 if (req->data[0] != ADB_PACKET)
172 return -EINVAL;
174 for (i = 0; i < req->nbytes - 1; ++i)
175 req->data[i] = req->data[i+1];
176 --req->nbytes;
178 req->next = NULL;
179 req->sent = 0;
180 req->complete = 0;
181 req->reply_len = 0;
183 spin_lock_irqsave(&macio_lock, flags);
184 if (current_req != 0) {
185 last_req->next = req;
186 last_req = req;
187 } else {
188 current_req = last_req = req;
189 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
191 spin_unlock_irqrestore(&macio_lock, flags);
193 if (sync) {
194 while (!req->complete)
195 macio_adb_poll();
198 return 0;
201 static irqreturn_t macio_adb_interrupt(int irq, void *arg,
202 struct pt_regs *regs)
204 int i, n, err;
205 struct adb_request *req;
206 unsigned char ibuf[16];
207 int ibuf_len = 0;
208 int complete = 0;
209 int autopoll = 0;
210 int handled = 0;
212 spin_lock(&macio_lock);
213 if (in_8(&adb->intr.r) & TAG) {
214 handled = 1;
215 if ((req = current_req) != 0) {
216 /* put the current request in */
217 for (i = 0; i < req->nbytes; ++i)
218 out_8(&adb->data[i].r, req->data[i]);
219 out_8(&adb->dcount.r, req->nbytes & HMB);
220 req->sent = 1;
221 if (req->reply_expected) {
222 out_8(&adb->ctrl.r, DTB + CRE);
223 } else {
224 out_8(&adb->ctrl.r, DTB);
225 current_req = req->next;
226 complete = 1;
227 if (current_req)
228 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
231 out_8(&adb->intr.r, 0);
234 if (in_8(&adb->intr.r) & DFB) {
235 handled = 1;
236 err = in_8(&adb->error.r);
237 if (current_req && current_req->sent) {
238 /* this is the response to a command */
239 req = current_req;
240 if (err == 0) {
241 req->reply_len = in_8(&adb->dcount.r) & HMB;
242 for (i = 0; i < req->reply_len; ++i)
243 req->reply[i] = in_8(&adb->data[i].r);
245 current_req = req->next;
246 complete = 1;
247 if (current_req)
248 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
249 } else if (err == 0) {
250 /* autopoll data */
251 n = in_8(&adb->dcount.r) & HMB;
252 for (i = 0; i < n; ++i)
253 ibuf[i] = in_8(&adb->data[i].r);
254 ibuf_len = n;
255 autopoll = (in_8(&adb->dcount.r) & APD) != 0;
257 out_8(&adb->error.r, 0);
258 out_8(&adb->intr.r, 0);
260 spin_unlock(&macio_lock);
261 if (complete && req) {
262 void (*done)(struct adb_request *) = req->done;
263 mb();
264 req->complete = 1;
265 /* Here, we assume that if the request has a done member, the
266 * struct request will survive to setting req->complete to 1
268 if (done)
269 (*done)(req);
271 if (ibuf_len)
272 adb_input(ibuf, ibuf_len, regs, autopoll);
274 return IRQ_RETVAL(handled);
277 static void macio_adb_poll(void)
279 unsigned long flags;
281 local_irq_save(flags);
282 if (in_8(&adb->intr.r) != 0)
283 macio_adb_interrupt(0, NULL, NULL);
284 local_irq_restore(flags);