2 * Driver for the ADB controller in the Mac I/O (Hydra) chip.
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>
13 #include <linux/adb.h>
15 #include <asm/pgtable.h>
16 #include <asm/hydra.h>
18 #include <asm/system.h>
19 #include <linux/init.h>
34 struct preg active_hi;
35 struct preg active_lo;
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 __iomem *adb;
61 static struct adb_request *current_req, *last_req;
62 static DEFINE_SPINLOCK(macio_lock);
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 = {
85 return find_compatible_devices("adb", "chrp,adb0")? 0: -ENODEV;
90 struct device_node *adbs;
92 adbs = find_compatible_devices("adb", "chrp,adb0");
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);
103 for (i = 0; i < adbs->n_intrs; ++i)
104 printk(" %x", adbs->intrs[i].line);
108 adb = ioremap(adbs->addrs->address, sizeof(struct adb_regs));
110 out_8(&adb->ctrl.r, 0);
111 out_8(&adb->intr.r, 0);
112 out_8(&adb->error.r, 0);
113 out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
114 out_8(&adb->active_lo.r, 0xff);
115 out_8(&adb->autopoll.r, APE);
117 if (request_irq(adbs->intrs[0].line, macio_adb_interrupt,
118 0, "ADB", (void *)0)) {
119 printk(KERN_ERR "ADB: can't get irq %d\n",
120 adbs->intrs[0].line);
123 out_8(&adb->intr_enb.r, DFB | TAG);
125 printk("adb: mac-io driver 1.0 for unified ADB\n");
130 static int macio_adb_autopoll(int devs)
134 spin_lock_irqsave(&macio_lock, flags);
135 out_8(&adb->active_hi.r, devs >> 8);
136 out_8(&adb->active_lo.r, devs);
137 out_8(&adb->autopoll.r, devs? APE: 0);
138 spin_unlock_irqrestore(&macio_lock, flags);
142 static int macio_adb_reset_bus(void)
145 int timeout = 1000000;
147 /* Hrm... we may want to not lock interrupts for so
148 * long ... oh well, who uses that chip anyway ? :)
149 * That function will be seldomly used during boot
150 * on rare machines, so...
152 spin_lock_irqsave(&macio_lock, flags);
153 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
154 while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
155 if (--timeout == 0) {
156 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
160 spin_unlock_irqrestore(&macio_lock, flags);
164 /* Send an ADB command */
165 static int macio_send_request(struct adb_request *req, int sync)
170 if (req->data[0] != ADB_PACKET)
173 for (i = 0; i < req->nbytes - 1; ++i)
174 req->data[i] = req->data[i+1];
182 spin_lock_irqsave(&macio_lock, flags);
183 if (current_req != 0) {
184 last_req->next = req;
187 current_req = last_req = req;
188 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
190 spin_unlock_irqrestore(&macio_lock, flags);
193 while (!req->complete)
200 static irqreturn_t macio_adb_interrupt(int irq, void *arg,
201 struct pt_regs *regs)
204 struct adb_request *req = NULL;
205 unsigned char ibuf[16];
211 spin_lock(&macio_lock);
212 if (in_8(&adb->intr.r) & TAG) {
214 if ((req = current_req) != 0) {
215 /* put the current request in */
216 for (i = 0; i < req->nbytes; ++i)
217 out_8(&adb->data[i].r, req->data[i]);
218 out_8(&adb->dcount.r, req->nbytes & HMB);
220 if (req->reply_expected) {
221 out_8(&adb->ctrl.r, DTB + CRE);
223 out_8(&adb->ctrl.r, DTB);
224 current_req = req->next;
227 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
230 out_8(&adb->intr.r, 0);
233 if (in_8(&adb->intr.r) & DFB) {
235 err = in_8(&adb->error.r);
236 if (current_req && current_req->sent) {
237 /* this is the response to a command */
240 req->reply_len = in_8(&adb->dcount.r) & HMB;
241 for (i = 0; i < req->reply_len; ++i)
242 req->reply[i] = in_8(&adb->data[i].r);
244 current_req = req->next;
247 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
248 } else if (err == 0) {
250 n = in_8(&adb->dcount.r) & HMB;
251 for (i = 0; i < n; ++i)
252 ibuf[i] = in_8(&adb->data[i].r);
254 autopoll = (in_8(&adb->dcount.r) & APD) != 0;
256 out_8(&adb->error.r, 0);
257 out_8(&adb->intr.r, 0);
259 spin_unlock(&macio_lock);
260 if (complete && req) {
261 void (*done)(struct adb_request *) = req->done;
264 /* Here, we assume that if the request has a done member, the
265 * struct request will survive to setting req->complete to 1
271 adb_input(ibuf, ibuf_len, regs, autopoll);
273 return IRQ_RETVAL(handled);
276 static void macio_adb_poll(void)
280 local_irq_save(flags);
281 if (in_8(&adb->intr.r) != 0)
282 macio_adb_interrupt(0, NULL, NULL);
283 local_irq_restore(flags);