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