xref: /netbsd-src/sys/arch/hp300/dev/dio.c (revision 0dd5877adce57db949b16ae963e5a6831cccdfb6)
1 /*	$NetBSD: dio.c,v 1.15 2001/12/14 08:34:27 gmcgarry Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Autoconfiguration and mapping support for the DIO bus.
41  */
42 
43 #define	_HP300_INTR_H_PRIVATE
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 
50 #include <machine/autoconf.h>
51 #include <machine/cpu.h>
52 #include <machine/hp300spu.h>
53 
54 #include <hp300/dev/dmavar.h>
55 
56 #include <hp300/dev/dioreg.h>
57 #include <hp300/dev/diovar.h>
58 
59 #include <hp300/dev/diodevs.h>
60 #include <hp300/dev/diodevs_data.h>
61 
62 extern	caddr_t internalhpib;
63 
64 int	dio_scodesize __P((struct dio_attach_args *));
65 char	*dio_devinfo __P((struct dio_attach_args *, char *, size_t));
66 
67 int	diomatch __P((struct device *, struct cfdata *, void *));
68 void	dioattach __P((struct device *, struct device *, void *));
69 int	dioprint __P((void *, const char *));
70 int	diosubmatch __P((struct device *, struct cfdata *, void *));
71 
72 struct cfattach dio_ca = {
73 	sizeof(struct device), diomatch, dioattach
74 };
75 
76 int
77 diomatch(parent, match, aux)
78 	struct device *parent;
79 	struct cfdata *match;
80 	void *aux;
81 {
82 	static int dio_matched = 0;
83 
84 	/* Allow only one instance. */
85 	if (dio_matched)
86 		return (0);
87 
88 	dio_matched = 1;
89 	return (1);
90 }
91 
92 void
93 dioattach(parent, self, aux)
94 	struct device *parent, *self;
95 	void *aux;
96 {
97 	struct dio_attach_args da;
98 	caddr_t pa, va;
99 	int scode, scmax, didmap, scodesize;
100 
101 	scmax = DIO_SCMAX(machineid);
102 	printf(": ");
103 	dmainit();
104 
105 	for (scode = 0; scode < scmax; ) {
106 		if (DIO_INHOLE(scode)) {
107 			scode++;
108 			continue;
109 		}
110 
111 		didmap = 0;
112 
113 		/*
114 		 * Temporarily map the space corresponding to
115 		 * the current select code unless:
116 		 *	- this is the internal hpib select code,
117 		 */
118 		pa = dio_scodetopa(scode);
119 		if ((scode == 7) && internalhpib)
120 			va = internalhpib = (caddr_t)IIOV(pa);
121 		else {
122 			va = iomap(pa, NBPG);
123 			if (va == NULL) {
124 				printf("%s: can't map scode %d\n",
125 				    self->dv_xname, scode);
126 				scode++;
127 				continue;
128 			}
129 			didmap = 1;
130 		}
131 
132 		/* Check for hardware. */
133 		if (badaddr(va)) {
134 			if (didmap)
135 				iounmap(va, NBPG);
136 			scode++;
137 			continue;
138 		}
139 
140 		/* Fill out attach args. */
141 		memset(&da, 0, sizeof(da));
142 		da.da_bst = HP300_BUS_SPACE_DIO;
143 		da.da_scode = scode;
144 		/*
145 		 * Internal HP-IB doesn't always return a device ID,
146 		 * so we rely on the sysflags.
147 		 */
148 		if ((scode == 7) && internalhpib)
149 			da.da_id = DIO_DEVICE_ID_IHPIB;
150 		else
151 			da.da_id = DIO_ID(va);
152 
153 		if (DIO_ISFRAMEBUFFER(da.da_id))
154 			da.da_secid = DIO_SECID(va);
155 
156 		da.da_size = DIO_SIZE(scode, va);
157 		scodesize = dio_scodesize(&da);
158 		if (DIO_ISDIO(scode))
159 			da.da_size *= scodesize;
160 
161 		/* No longer need the device to be mapped. */
162 		if (didmap)
163 			iounmap(va, NBPG);
164 
165 		/* Attach matching device. */
166 		config_found_sm(self, &da, dioprint, diosubmatch);
167 		scode += scodesize;
168 	}
169 }
170 
171 int
172 diosubmatch(parent, cf, aux)
173 	struct device *parent;
174 	struct cfdata *cf;
175 	void *aux;
176 {
177 	struct dio_attach_args *da = aux;
178 
179 	if (cf->diocf_scode != DIOCF_SCODE_DEFAULT &&
180 	    cf->diocf_scode != da->da_scode)
181 		return (0);
182 
183 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
184 }
185 
186 int
187 dioprint(aux, pnp)
188 	void *aux;
189 	const char *pnp;
190 {
191 	struct dio_attach_args *da = aux;
192 	char buf[128];
193 
194 	if (pnp)
195 		printf("%s at %s", dio_devinfo(da, buf, sizeof(buf)), pnp);
196 	printf(" scode %d", da->da_scode);
197 	return (UNCONF);
198 }
199 
200 /*
201  * Convert a select code to a system physical address.
202  */
203 void *
204 dio_scodetopa(scode)
205 	int scode;
206 {
207 	u_long rval;
208 
209 	if (scode == 7 && internalhpib)
210 		rval = DIO_IHPIBADDR;
211 	else if (DIO_ISDIO(scode))
212 		rval = DIO_BASE + (scode * DIO_DEVSIZE);
213 	else if (DIO_ISDIOII(scode))
214 		rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
215 	else
216 		rval = 0;
217 
218 	return ((void *)rval);
219 }
220 
221 /*
222  * Return the select code size for this device, defaulting to 1
223  * if we don't know what kind of device we have.
224  */
225 int
226 dio_scodesize(da)
227 	struct dio_attach_args *da;
228 {
229 	int i;
230 
231 	/*
232 	 * Deal with lame internal HP-IB controllers which don't have
233 	 * consistent/reliable device ids.
234 	 */
235 	if (da->da_scode == 7 && internalhpib)
236 		return (1);
237 
238 	/*
239 	 * Find the dio_devdata matchind the primary id.
240 	 * If we're a framebuffer, we also check the secondary id.
241 	 */
242 	for (i = 0; i < DIO_NDEVICES; i++) {
243 		if (da->da_id == dio_devdatas[i].dd_id) {
244 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
245 				if (da->da_secid == dio_devdatas[i].dd_secid) {
246 					goto foundit;
247 				}
248 			} else {
249 			foundit:
250 				return (dio_devdatas[i].dd_nscode);
251 			}
252 		}
253 	}
254 
255 	/*
256 	 * Device is unknown.  Print a warning and assume a default.
257 	 */
258 	printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n",
259 	    da->da_id, da->da_secid);
260 	return (1);
261 }
262 
263 /*
264  * Return a reasonable description of a DIO device.
265  */
266 char *
267 dio_devinfo(da, buf, buflen)
268 	struct dio_attach_args *da;
269 	char *buf;
270 	size_t buflen;
271 {
272 #ifdef DIOVERBOSE
273 	int i;
274 #endif
275 
276 	memset(buf, 0, buflen);
277 
278 	/*
279 	 * Deal with lame internal HP-IB controllers which don't have
280 	 * consistent/reliable device ids.
281 	 */
282 	if (da->da_scode == 7 && internalhpib) {
283 		sprintf(buf, DIO_DEVICE_DESC_IHPIB);
284 		return (buf);
285 	}
286 
287 #ifdef DIOVERBOSE
288 	/*
289 	 * Find the description matching our primary id.
290 	 * If we're a framebuffer, we also check the secondary id.
291 	 */
292 	for (i = 0; i < DIO_NDEVICES; i++) {
293 		if (da->da_id == dio_devdescs[i].dd_id) {
294 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
295 				if (da->da_secid == dio_devdescs[i].dd_secid) {
296 					goto foundit;
297 				}
298 			} else {
299 			foundit:
300 				sprintf(buf, "%s", dio_devdescs[i].dd_desc);
301 				return (buf);
302 			}
303 		}
304 	}
305 #endif /* DIOVERBOSE */
306 
307 	/*
308 	 * Device is unknown.  Construct something reasonable.
309 	 */
310 	sprintf(buf, "device id = 0x%x secid = 0x%x",
311 	    da->da_id, da->da_secid);
312 	return (buf);
313 }
314 
315 /*
316  * Establish an interrupt handler for a DIO device.
317  */
318 void *
319 dio_intr_establish(func, arg, ipl, priority)
320 	int (*func) __P((void *));
321 	void *arg;
322 	int ipl;
323 	int priority;
324 {
325 	void *ih;
326 
327 	ih = intr_establish(func, arg, ipl, priority);
328 
329 	if (priority == IPL_BIO)
330 		dmacomputeipl();
331 
332 	return (ih);
333 }
334 
335 /*
336  * Remove an interrupt handler for a DIO device.
337  */
338 void
339 dio_intr_disestablish(arg)
340 	void *arg;
341 {
342 	struct hp300_intrhand *ih = arg;
343 	int priority = ih->ih_priority;
344 
345 	intr_disestablish(arg);
346 
347 	if (priority == IPL_BIO)
348 		dmacomputeipl();
349 }
350