File: /usr/src/linux/drivers/macintosh/macio-adb.c
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 <asm/prom.h>
11 #include <linux/adb.h>
12 #include <asm/io.h>
13 #include <asm/pgtable.h>
14 #include <asm/hydra.h>
15 #include <asm/irq.h>
16 #include <asm/system.h>
17 #include <linux/init.h>
18
19 struct preg {
20 unsigned char r;
21 char pad[15];
22 };
23
24 struct adb_regs {
25 struct preg intr;
26 struct preg data[9];
27 struct preg intr_enb;
28 struct preg dcount;
29 struct preg error;
30 struct preg ctrl;
31 struct preg autopoll;
32 struct preg active_hi;
33 struct preg active_lo;
34 struct preg test;
35 };
36
37 /* Bits in intr and intr_enb registers */
38 #define DFB 1 /* data from bus */
39 #define TAG 2 /* transfer access grant */
40
41 /* Bits in dcount register */
42 #define HMB 0x0f /* how many bytes */
43 #define APD 0x10 /* auto-poll data */
44
45 /* Bits in error register */
46 #define NRE 1 /* no response error */
47 #define DLE 2 /* data lost error */
48
49 /* Bits in ctrl register */
50 #define TAR 1 /* transfer access request */
51 #define DTB 2 /* data to bus */
52 #define CRE 4 /* command response expected */
53 #define ADB_RST 8 /* ADB reset */
54
55 /* Bits in autopoll register */
56 #define APE 1 /* autopoll enable */
57
58 static volatile struct adb_regs *adb;
59 static struct adb_request *current_req, *last_req;
60 static unsigned char adb_rbuf[16];
61
62 static int macio_probe(void);
63 static int macio_init(void);
64 static void macio_adb_interrupt(int irq, void *arg, struct pt_regs *regs);
65 static int macio_send_request(struct adb_request *req, int sync);
66 static int macio_adb_autopoll(int devs);
67 static void macio_adb_poll(void);
68 static int macio_adb_reset_bus(void);
69 static void completed(void);
70
71 struct adb_driver macio_adb_driver = {
72 "MACIO",
73 macio_probe,
74 macio_init,
75 macio_send_request,
76 /*macio_write,*/
77 macio_adb_autopoll,
78 macio_adb_poll,
79 macio_adb_reset_bus
80 };
81
82 int macio_probe(void)
83 {
84 return find_compatible_devices("adb", "chrp,adb0")? 0: -ENODEV;
85 }
86
87 int macio_init(void)
88 {
89 struct device_node *adbs;
90
91 adbs = find_compatible_devices("adb", "chrp,adb0");
92 if (adbs == 0)
93 return -ENXIO;
94
95 #if 0
96 { int i;
97
98 printk("macio_adb_init: node = %p, addrs =", adbs->node);
99 for (i = 0; i < adbs->n_addrs; ++i)
100 printk(" %x(%x)", adbs->addrs[i].address, adbs->addrs[i].size);
101 printk(", intrs =");
102 for (i = 0; i < adbs->n_intrs; ++i)
103 printk(" %x", adbs->intrs[i].line);
104 printk("\n"); }
105 #endif
106
107 adb = (volatile struct adb_regs *)
108 ioremap(adbs->addrs->address, sizeof(struct adb_regs));
109
110 if (request_irq(adbs->intrs[0].line, macio_adb_interrupt,
111 0, "ADB", (void *)0)) {
112 printk(KERN_ERR "ADB: can't get irq %d\n",
113 adbs->intrs[0].line);
114 return -EAGAIN;
115 }
116
117 out_8(&adb->ctrl.r, 0);
118 out_8(&adb->intr.r, 0);
119 out_8(&adb->error.r, 0);
120 out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
121 out_8(&adb->active_lo.r, 0xff);
122 out_8(&adb->autopoll.r, APE);
123 out_8(&adb->intr_enb.r, DFB | TAG);
124
125 printk("adb: mac-io driver 1.0 for unified ADB\n");
126
127 return 0;
128 }
129
130 static int macio_adb_autopoll(int devs)
131 {
132 out_8(&adb->active_hi.r, devs >> 8);
133 out_8(&adb->active_lo.r, devs);
134 out_8(&adb->autopoll.r, devs? APE: 0);
135 return 0;
136 }
137
138 static int macio_adb_reset_bus(void)
139 {
140 int timeout = 1000000;
141
142 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
143 while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
144 if (--timeout == 0) {
145 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
146 return -1;
147 }
148 }
149 return 0;
150 }
151
152 /* Send an ADB command */
153 static int macio_send_request(struct adb_request *req, int sync)
154 {
155 unsigned long mflags;
156 int i;
157
158 if (req->data[0] != ADB_PACKET)
159 return -EINVAL;
160
161 for (i = 0; i < req->nbytes - 1; ++i)
162 req->data[i] = req->data[i+1];
163 --req->nbytes;
164
165 req->next = 0;
166 req->sent = 0;
167 req->complete = 0;
168 req->reply_len = 0;
169
170 save_flags(mflags);
171 cli();
172 if (current_req != 0) {
173 last_req->next = req;
174 last_req = req;
175 } else {
176 current_req = last_req = req;
177 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
178 }
179 restore_flags(mflags);
180
181 if (sync) {
182 while (!req->complete)
183 macio_adb_poll();
184 }
185
186 return 0;
187 }
188
189 static void macio_adb_interrupt(int irq, void *arg, struct pt_regs *regs)
190 {
191 int i, n, err;
192 struct adb_request *req;
193
194 if (in_8(&adb->intr.r) & TAG) {
195 if ((req = current_req) != 0) {
196 /* put the current request in */
197 for (i = 0; i < req->nbytes; ++i)
198 out_8(&adb->data[i].r, req->data[i]);
199 out_8(&adb->dcount.r, req->nbytes & HMB);
200 req->sent = 1;
201 if (req->reply_expected) {
202 out_8(&adb->ctrl.r, DTB + CRE);
203 } else {
204 out_8(&adb->ctrl.r, DTB);
205 completed();
206 }
207 }
208 out_8(&adb->intr.r, 0);
209 }
210
211 if (in_8(&adb->intr.r) & DFB) {
212 err = in_8(&adb->error.r);
213 if (current_req && current_req->sent) {
214 /* this is the response to a command */
215 req = current_req;
216 if (err == 0) {
217 req->reply_len = in_8(&adb->dcount.r) & HMB;
218 for (i = 0; i < req->reply_len; ++i)
219 req->reply[i] = in_8(&adb->data[i].r);
220 }
221 completed();
222 } else if (err == 0) {
223 /* autopoll data */
224 n = in_8(&adb->dcount.r) & HMB;
225 for (i = 0; i < n; ++i)
226 adb_rbuf[i] = in_8(&adb->data[i].r);
227 adb_input(adb_rbuf, n, regs,
228 in_8(&adb->dcount.r) & APD);
229 }
230 out_8(&adb->error.r, 0);
231 out_8(&adb->intr.r, 0);
232 }
233 }
234
235 static void completed(void)
236 {
237 struct adb_request *req = current_req;
238
239 req->complete = 1;
240 current_req = req->next;
241 if (current_req)
242 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
243 if (req->done)
244 (*req->done)(req);
245 }
246
247 static void macio_adb_poll(void)
248 {
249 unsigned long flags;
250
251 save_flags(flags);
252 cli();
253 if (in_8(&adb->intr.r) != 0)
254 macio_adb_interrupt(0, 0, 0);
255 restore_flags(flags);
256 }
257