Linux Audio

Check our new training course

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