xref: /netbsd/sys/arch/hp300/dev/dio.c (revision c4a72b64)
1 /*	$NetBSD: dio.c,v 1.19 2002/10/02 05:15:49 thorpej 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 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: dio.c,v 1.19 2002/10/02 05:15:49 thorpej Exp $");
45 
46 #define	_HP300_INTR_H_PRIVATE
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 
53 #include <machine/autoconf.h>
54 #include <machine/cpu.h>
55 #include <machine/hp300spu.h>
56 
57 #include <hp300/dev/dmavar.h>
58 
59 #include <hp300/dev/dioreg.h>
60 #include <hp300/dev/diovar.h>
61 
62 #include <hp300/dev/diodevs.h>
63 #include <hp300/dev/diodevs_data.h>
64 
65 extern	caddr_t internalhpib;
66 
67 int	dio_scodesize __P((struct dio_attach_args *));
68 char	*dio_devinfo __P((struct dio_attach_args *, char *, size_t));
69 
70 int	diomatch __P((struct device *, struct cfdata *, void *));
71 void	dioattach __P((struct device *, struct device *, void *));
72 int	dioprint __P((void *, const char *));
73 int	diosubmatch __P((struct device *, struct cfdata *, void *));
74 
75 CFATTACH_DECL(dio, sizeof(struct device),
76     diomatch, dioattach, NULL, NULL);
77 
78 int
79 diomatch(parent, match, aux)
80 	struct device *parent;
81 	struct cfdata *match;
82 	void *aux;
83 {
84 	static int dio_matched = 0;
85 
86 	/* Allow only one instance. */
87 	if (dio_matched)
88 		return (0);
89 
90 	dio_matched = 1;
91 	return (1);
92 }
93 
94 void
95 dioattach(parent, self, aux)
96 	struct device *parent, *self;
97 	void *aux;
98 {
99 	struct dio_attach_args da;
100 	caddr_t pa, va;
101 	int scode, scmax, didmap, scodesize;
102 
103 	scmax = DIO_SCMAX(machineid);
104 	printf(": ");
105 	dmainit();
106 
107 	for (scode = 0; scode < scmax; ) {
108 		if (DIO_INHOLE(scode)) {
109 			scode++;
110 			continue;
111 		}
112 
113 		didmap = 0;
114 
115 		/*
116 		 * Temporarily map the space corresponding to
117 		 * the current select code unless:
118 		 *	- this is the internal hpib select code,
119 		 */
120 		pa = dio_scodetopa(scode);
121 		if ((scode == 7) && internalhpib)
122 			va = internalhpib = (caddr_t)IIOV(pa);
123 		else {
124 			va = iomap(pa, NBPG);
125 			if (va == NULL) {
126 				printf("%s: can't map scode %d\n",
127 				    self->dv_xname, scode);
128 				scode++;
129 				continue;
130 			}
131 			didmap = 1;
132 		}
133 
134 		/* Check for hardware. */
135 		if (badaddr(va)) {
136 			if (didmap)
137 				iounmap(va, NBPG);
138 			scode++;
139 			continue;
140 		}
141 
142 		/* Fill out attach args. */
143 		memset(&da, 0, sizeof(da));
144 		da.da_bst = HP300_BUS_SPACE_DIO;
145 		da.da_scode = scode;
146 		/*
147 		 * Internal HP-IB doesn't always return a device ID,
148 		 * so we rely on the sysflags.
149 		 */
150 		if ((scode == 7) && internalhpib)
151 			da.da_id = DIO_DEVICE_ID_IHPIB;
152 		else
153 			da.da_id = DIO_ID(va);
154 
155 		if (DIO_ISFRAMEBUFFER(da.da_id))
156 			da.da_secid = DIO_SECID(va);
157 
158 		da.da_size = DIO_SIZE(scode, va);
159 		scodesize = dio_scodesize(&da);
160 		if (DIO_ISDIO(scode))
161 			da.da_size *= scodesize;
162 
163 		/* No longer need the device to be mapped. */
164 		if (didmap)
165 			iounmap(va, NBPG);
166 
167 		/* Attach matching device. */
168 		config_found_sm(self, &da, dioprint, diosubmatch);
169 		scode += scodesize;
170 	}
171 }
172 
173 int
174 diosubmatch(parent, cf, aux)
175 	struct device *parent;
176 	struct cfdata *cf;
177 	void *aux;
178 {
179 	struct dio_attach_args *da = aux;
180 
181 	if (cf->diocf_scode != DIOCF_SCODE_DEFAULT &&
182 	    cf->diocf_scode != da->da_scode)
183 		return (0);
184 
185 	return (config_match(parent, cf, aux));
186 }
187 
188 int
189 dioprint(aux, pnp)
190 	void *aux;
191 	const char *pnp;
192 {
193 	struct dio_attach_args *da = aux;
194 	char buf[128];
195 
196 	if (pnp)
197 		printf("%s at %s", dio_devinfo(da, buf, sizeof(buf)), pnp);
198 	printf(" scode %d", da->da_scode);
199 	return (UNCONF);
200 }
201 
202 /*
203  * Convert a select code to a system physical address.
204  */
205 void *
206 dio_scodetopa(scode)
207 	int scode;
208 {
209 	u_long rval;
210 
211 	if (scode == 7 && internalhpib)
212 		rval = DIO_IHPIBADDR;
213 	else if (DIO_ISDIO(scode))
214 		rval = DIO_BASE + (scode * DIO_DEVSIZE);
215 	else if (DIO_ISDIOII(scode))
216 		rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
217 	else
218 		rval = 0;
219 
220 	return ((void *)rval);
221 }
222 
223 /*
224  * Return the select code size for this device, defaulting to 1
225  * if we don't know what kind of device we have.
226  */
227 int
228 dio_scodesize(da)
229 	struct dio_attach_args *da;
230 {
231 	int i;
232 
233 	/*
234 	 * Deal with lame internal HP-IB controllers which don't have
235 	 * consistent/reliable device ids.
236 	 */
237 	if (da->da_scode == 7 && internalhpib)
238 		return (1);
239 
240 	/*
241 	 * Find the dio_devdata matchind the primary id.
242 	 * If we're a framebuffer, we also check the secondary id.
243 	 */
244 	for (i = 0; i < DIO_NDEVICES; i++) {
245 		if (da->da_id == dio_devdatas[i].dd_id) {
246 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
247 				if (da->da_secid == dio_devdatas[i].dd_secid) {
248 					goto foundit;
249 				}
250 			} else {
251 			foundit:
252 				return (dio_devdatas[i].dd_nscode);
253 			}
254 		}
255 	}
256 
257 	/*
258 	 * Device is unknown.  Print a warning and assume a default.
259 	 */
260 	printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n",
261 	    da->da_id, da->da_secid);
262 	return (1);
263 }
264 
265 /*
266  * Return a reasonable description of a DIO device.
267  */
268 char *
269 dio_devinfo(da, buf, buflen)
270 	struct dio_attach_args *da;
271 	char *buf;
272 	size_t buflen;
273 {
274 #ifdef DIOVERBOSE
275 	int i;
276 #endif
277 
278 	memset(buf, 0, buflen);
279 
280 	/*
281 	 * Deal with lame internal HP-IB controllers which don't have
282 	 * consistent/reliable device ids.
283 	 */
284 	if (da->da_scode == 7 && internalhpib) {
285 		sprintf(buf, DIO_DEVICE_DESC_IHPIB);
286 		return (buf);
287 	}
288 
289 #ifdef DIOVERBOSE
290 	/*
291 	 * Find the description matching our primary id.
292 	 * If we're a framebuffer, we also check the secondary id.
293 	 */
294 	for (i = 0; i < DIO_NDEVICES; i++) {
295 		if (da->da_id == dio_devdescs[i].dd_id) {
296 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
297 				if (da->da_secid == dio_devdescs[i].dd_secid) {
298 					goto foundit;
299 				}
300 			} else {
301 			foundit:
302 				sprintf(buf, "%s", dio_devdescs[i].dd_desc);
303 				return (buf);
304 			}
305 		}
306 	}
307 #endif /* DIOVERBOSE */
308 
309 	/*
310 	 * Device is unknown.  Construct something reasonable.
311 	 */
312 	sprintf(buf, "device id = 0x%x secid = 0x%x",
313 	    da->da_id, da->da_secid);
314 	return (buf);
315 }
316 
317 /*
318  * Establish an interrupt handler for a DIO device.
319  */
320 void *
321 dio_intr_establish(func, arg, ipl, priority)
322 	int (*func) __P((void *));
323 	void *arg;
324 	int ipl;
325 	int priority;
326 {
327 	void *ih;
328 
329 	ih = intr_establish(func, arg, ipl, priority);
330 
331 	if (priority == IPL_BIO)
332 		dmacomputeipl();
333 
334 	return (ih);
335 }
336 
337 /*
338  * Remove an interrupt handler for a DIO device.
339  */
340 void
341 dio_intr_disestablish(arg)
342 	void *arg;
343 {
344 	struct hp300_intrhand *ih = arg;
345 	int priority = ih->ih_priority;
346 
347 	intr_disestablish(arg);
348 
349 	if (priority == IPL_BIO)
350 		dmacomputeipl();
351 }
352