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>
20 #include <linux/ioport.h>
35 struct preg active_hi
;
36 struct preg active_lo
;
40 /* Bits in intr and intr_enb registers */
41 #define DFB 1 /* data from bus */
42 #define TAG 2 /* transfer access grant */
44 /* Bits in dcount register */
45 #define HMB 0x0f /* how many bytes */
46 #define APD 0x10 /* auto-poll data */
48 /* Bits in error register */
49 #define NRE 1 /* no response error */
50 #define DLE 2 /* data lost error */
52 /* Bits in ctrl register */
53 #define TAR 1 /* transfer access request */
54 #define DTB 2 /* data to bus */
55 #define CRE 4 /* command response expected */
56 #define ADB_RST 8 /* ADB reset */
58 /* Bits in autopoll register */
59 #define APE 1 /* autopoll enable */
61 static volatile struct adb_regs __iomem
*adb
;
62 static struct adb_request
*current_req
, *last_req
;
63 static DEFINE_SPINLOCK(macio_lock
);
65 static int macio_probe(void);
66 static int macio_init(void);
67 static irqreturn_t
macio_adb_interrupt(int irq
, void *arg
, struct pt_regs
*regs
);
68 static int macio_send_request(struct adb_request
*req
, int sync
);
69 static int macio_adb_autopoll(int devs
);
70 static void macio_adb_poll(void);
71 static int macio_adb_reset_bus(void);
73 struct adb_driver macio_adb_driver
= {
86 return find_compatible_devices("adb", "chrp,adb0")? 0: -ENODEV
;
91 struct device_node
*adbs
;
95 adbs
= find_compatible_devices("adb", "chrp,adb0");
99 if (of_address_to_resource(adbs
, 0, &r
))
101 adb
= ioremap(r
.start
, sizeof(struct adb_regs
));
103 out_8(&adb
->ctrl
.r
, 0);
104 out_8(&adb
->intr
.r
, 0);
105 out_8(&adb
->error
.r
, 0);
106 out_8(&adb
->active_hi
.r
, 0xff); /* for now, set all devices active */
107 out_8(&adb
->active_lo
.r
, 0xff);
108 out_8(&adb
->autopoll
.r
, APE
);
110 irq
= irq_of_parse_and_map(adbs
, 0);
111 if (request_irq(irq
, macio_adb_interrupt
, 0, "ADB", (void *)0)) {
112 printk(KERN_ERR
"ADB: can't get irq %d\n", irq
);
115 out_8(&adb
->intr_enb
.r
, DFB
| TAG
);
117 printk("adb: mac-io driver 1.0 for unified ADB\n");
122 static int macio_adb_autopoll(int devs
)
126 spin_lock_irqsave(&macio_lock
, flags
);
127 out_8(&adb
->active_hi
.r
, devs
>> 8);
128 out_8(&adb
->active_lo
.r
, devs
);
129 out_8(&adb
->autopoll
.r
, devs
? APE
: 0);
130 spin_unlock_irqrestore(&macio_lock
, flags
);
134 static int macio_adb_reset_bus(void)
137 int timeout
= 1000000;
139 /* Hrm... we may want to not lock interrupts for so
140 * long ... oh well, who uses that chip anyway ? :)
141 * That function will be seldomly used during boot
142 * on rare machines, so...
144 spin_lock_irqsave(&macio_lock
, flags
);
145 out_8(&adb
->ctrl
.r
, in_8(&adb
->ctrl
.r
) | ADB_RST
);
146 while ((in_8(&adb
->ctrl
.r
) & ADB_RST
) != 0) {
147 if (--timeout
== 0) {
148 out_8(&adb
->ctrl
.r
, in_8(&adb
->ctrl
.r
) & ~ADB_RST
);
152 spin_unlock_irqrestore(&macio_lock
, flags
);
156 /* Send an ADB command */
157 static int macio_send_request(struct adb_request
*req
, int sync
)
162 if (req
->data
[0] != ADB_PACKET
)
165 for (i
= 0; i
< req
->nbytes
- 1; ++i
)
166 req
->data
[i
] = req
->data
[i
+1];
174 spin_lock_irqsave(&macio_lock
, flags
);
175 if (current_req
!= 0) {
176 last_req
->next
= req
;
179 current_req
= last_req
= req
;
180 out_8(&adb
->ctrl
.r
, in_8(&adb
->ctrl
.r
) | TAR
);
182 spin_unlock_irqrestore(&macio_lock
, flags
);
185 while (!req
->complete
)
192 static irqreturn_t
macio_adb_interrupt(int irq
, void *arg
,
193 struct pt_regs
*regs
)
196 struct adb_request
*req
= NULL
;
197 unsigned char ibuf
[16];
203 spin_lock(&macio_lock
);
204 if (in_8(&adb
->intr
.r
) & TAG
) {
206 if ((req
= current_req
) != 0) {
207 /* put the current request in */
208 for (i
= 0; i
< req
->nbytes
; ++i
)
209 out_8(&adb
->data
[i
].r
, req
->data
[i
]);
210 out_8(&adb
->dcount
.r
, req
->nbytes
& HMB
);
212 if (req
->reply_expected
) {
213 out_8(&adb
->ctrl
.r
, DTB
+ CRE
);
215 out_8(&adb
->ctrl
.r
, DTB
);
216 current_req
= req
->next
;
219 out_8(&adb
->ctrl
.r
, in_8(&adb
->ctrl
.r
) | TAR
);
222 out_8(&adb
->intr
.r
, 0);
225 if (in_8(&adb
->intr
.r
) & DFB
) {
227 err
= in_8(&adb
->error
.r
);
228 if (current_req
&& current_req
->sent
) {
229 /* this is the response to a command */
232 req
->reply_len
= in_8(&adb
->dcount
.r
) & HMB
;
233 for (i
= 0; i
< req
->reply_len
; ++i
)
234 req
->reply
[i
] = in_8(&adb
->data
[i
].r
);
236 current_req
= req
->next
;
239 out_8(&adb
->ctrl
.r
, in_8(&adb
->ctrl
.r
) | TAR
);
240 } else if (err
== 0) {
242 n
= in_8(&adb
->dcount
.r
) & HMB
;
243 for (i
= 0; i
< n
; ++i
)
244 ibuf
[i
] = in_8(&adb
->data
[i
].r
);
246 autopoll
= (in_8(&adb
->dcount
.r
) & APD
) != 0;
248 out_8(&adb
->error
.r
, 0);
249 out_8(&adb
->intr
.r
, 0);
251 spin_unlock(&macio_lock
);
252 if (complete
&& req
) {
253 void (*done
)(struct adb_request
*) = req
->done
;
256 /* Here, we assume that if the request has a done member, the
257 * struct request will survive to setting req->complete to 1
263 adb_input(ibuf
, ibuf_len
, regs
, autopoll
);
265 return IRQ_RETVAL(handled
);
268 static void macio_adb_poll(void)
272 local_irq_save(flags
);
273 if (in_8(&adb
->intr
.r
) != 0)
274 macio_adb_interrupt(0, NULL
, NULL
);
275 local_irq_restore(flags
);