1 // SPDX-License-Identifier: GPL-2.0
3 * 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/spinlock.h>
10 #include <linux/interrupt.h>
11 #include <linux/pgtable.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/adb.h>
18 #include <asm/hydra.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
36 struct preg active_hi
;
37 struct preg active_lo
;
41 /* Bits in intr and intr_enb registers */
42 #define DFB 1 /* data from bus */
43 #define TAG 2 /* transfer access grant */
45 /* Bits in dcount register */
46 #define HMB 0x0f /* how many bytes */
47 #define APD 0x10 /* auto-poll data */
49 /* Bits in error register */
50 #define NRE 1 /* no response error */
51 #define DLE 2 /* data lost error */
53 /* Bits in ctrl register */
54 #define TAR 1 /* transfer access request */
55 #define DTB 2 /* data to bus */
56 #define CRE 4 /* command response expected */
57 #define ADB_RST 8 /* ADB reset */
59 /* Bits in autopoll register */
60 #define APE 1 /* autopoll enable */
62 static volatile struct adb_regs __iomem
*adb
;
63 static struct adb_request
*current_req
, *last_req
;
64 static DEFINE_SPINLOCK(macio_lock
);
66 static int macio_probe(void);
67 static int macio_init(void);
68 static irqreturn_t
macio_adb_interrupt(int irq
, void *arg
);
69 static int macio_send_request(struct adb_request
*req
, int sync
);
70 static int macio_adb_autopoll(int devs
);
71 static void macio_adb_poll(void);
72 static int macio_adb_reset_bus(void);
74 struct adb_driver macio_adb_driver
= {
78 .send_request
= macio_send_request
,
79 .autopoll
= macio_adb_autopoll
,
80 .poll
= macio_adb_poll
,
81 .reset_bus
= macio_adb_reset_bus
,
86 struct device_node
*np
__free(device_node
) =
87 of_find_compatible_node(NULL
, "adb", "chrp,adb0");
97 struct device_node
*adbs
__free(device_node
) =
98 of_find_compatible_node(NULL
, "adb", "chrp,adb0");
105 if (of_address_to_resource(adbs
, 0, &r
))
108 adb
= ioremap(r
.start
, sizeof(struct adb_regs
));
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
);
120 irq
= irq_of_parse_and_map(adbs
, 0);
121 if (request_irq(irq
, macio_adb_interrupt
, 0, "ADB", (void *)0)) {
123 printk(KERN_ERR
"ADB: can't get irq %d\n", irq
);
126 out_8(&adb
->intr_enb
.r
, DFB
| TAG
);
128 printk("adb: mac-io driver 1.0 for unified ADB\n");
133 static int macio_adb_autopoll(int devs
)
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
);
145 static int macio_adb_reset_bus(void)
148 int timeout
= 1000000;
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 seldom used during boot
153 * on rare machines, so...
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 spin_unlock_irqrestore(&macio_lock
, flags
);
164 spin_unlock_irqrestore(&macio_lock
, flags
);
168 /* Send an ADB command */
169 static int macio_send_request(struct adb_request
*req
, int sync
)
174 if (req
->data
[0] != ADB_PACKET
)
177 for (i
= 0; i
< req
->nbytes
- 1; ++i
)
178 req
->data
[i
] = req
->data
[i
+1];
186 spin_lock_irqsave(&macio_lock
, flags
);
188 last_req
->next
= req
;
191 current_req
= last_req
= req
;
192 out_8(&adb
->ctrl
.r
, in_8(&adb
->ctrl
.r
) | TAR
);
194 spin_unlock_irqrestore(&macio_lock
, flags
);
197 while (!req
->complete
)
204 static irqreturn_t
macio_adb_interrupt(int irq
, void *arg
)
207 struct adb_request
*req
= NULL
;
208 unsigned char ibuf
[16];
214 spin_lock(&macio_lock
);
215 if (in_8(&adb
->intr
.r
) & TAG
) {
219 /* put the current request in */
220 for (i
= 0; i
< req
->nbytes
; ++i
)
221 out_8(&adb
->data
[i
].r
, req
->data
[i
]);
222 out_8(&adb
->dcount
.r
, req
->nbytes
& HMB
);
224 if (req
->reply_expected
) {
225 out_8(&adb
->ctrl
.r
, DTB
+ CRE
);
227 out_8(&adb
->ctrl
.r
, DTB
);
228 current_req
= req
->next
;
231 out_8(&adb
->ctrl
.r
, in_8(&adb
->ctrl
.r
) | TAR
);
234 out_8(&adb
->intr
.r
, 0);
237 if (in_8(&adb
->intr
.r
) & DFB
) {
239 err
= in_8(&adb
->error
.r
);
240 if (current_req
&& current_req
->sent
) {
241 /* this is the response to a command */
244 req
->reply_len
= in_8(&adb
->dcount
.r
) & HMB
;
245 for (i
= 0; i
< req
->reply_len
; ++i
)
246 req
->reply
[i
] = in_8(&adb
->data
[i
].r
);
248 current_req
= req
->next
;
251 out_8(&adb
->ctrl
.r
, in_8(&adb
->ctrl
.r
) | TAR
);
252 } else if (err
== 0) {
254 n
= in_8(&adb
->dcount
.r
) & HMB
;
255 for (i
= 0; i
< n
; ++i
)
256 ibuf
[i
] = in_8(&adb
->data
[i
].r
);
258 autopoll
= (in_8(&adb
->dcount
.r
) & APD
) != 0;
260 out_8(&adb
->error
.r
, 0);
261 out_8(&adb
->intr
.r
, 0);
263 spin_unlock(&macio_lock
);
264 if (complete
&& req
) {
265 void (*done
)(struct adb_request
*) = req
->done
;
268 /* Here, we assume that if the request has a done member, the
269 * struct request will survive to setting req->complete to 1
275 adb_input(ibuf
, ibuf_len
, autopoll
);
277 return IRQ_RETVAL(handled
);
280 static void macio_adb_poll(void)
284 local_irq_save(flags
);
285 if (in_8(&adb
->intr
.r
) != 0)
286 macio_adb_interrupt(0, NULL
);
287 local_irq_restore(flags
);