xref: /netbsd-src/sys/arch/hp300/dev/dio.c (revision b8c616269f5ebf18ab2e35cb8099d683130a177c)
1 /*	$NetBSD: dio.c,v 1.21 2003/01/01 01:34:45 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.21 2003/01/01 01:34:45 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 	printf("\n");
104 
105 	scmax = DIO_SCMAX(machineid);
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 		aprint_normal("%s at %s",
198 		    dio_devinfo(da, buf, sizeof(buf)), pnp);
199 	aprint_normal(" scode %d", da->da_scode);
200 	return (UNCONF);
201 }
202 
203 /*
204  * Convert a select code to a system physical address.
205  */
206 void *
207 dio_scodetopa(scode)
208 	int scode;
209 {
210 	u_long rval;
211 
212 	if (scode == 7 && internalhpib)
213 		rval = DIO_IHPIBADDR;
214 	else if (DIO_ISDIO(scode))
215 		rval = DIO_BASE + (scode * DIO_DEVSIZE);
216 	else if (DIO_ISDIOII(scode))
217 		rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
218 	else
219 		rval = 0;
220 
221 	return ((void *)rval);
222 }
223 
224 /*
225  * Return the select code size for this device, defaulting to 1
226  * if we don't know what kind of device we have.
227  */
228 int
229 dio_scodesize(da)
230 	struct dio_attach_args *da;
231 {
232 	int i;
233 
234 	/*
235 	 * Deal with lame internal HP-IB controllers which don't have
236 	 * consistent/reliable device ids.
237 	 */
238 	if (da->da_scode == 7 && internalhpib)
239 		return (1);
240 
241 	/*
242 	 * Find the dio_devdata matchind the primary id.
243 	 * If we're a framebuffer, we also check the secondary id.
244 	 */
245 	for (i = 0; i < DIO_NDEVICES; i++) {
246 		if (da->da_id == dio_devdatas[i].dd_id) {
247 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
248 				if (da->da_secid == dio_devdatas[i].dd_secid) {
249 					goto foundit;
250 				}
251 			} else {
252 			foundit:
253 				return (dio_devdatas[i].dd_nscode);
254 			}
255 		}
256 	}
257 
258 	/*
259 	 * Device is unknown.  Print a warning and assume a default.
260 	 */
261 	printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n",
262 	    da->da_id, da->da_secid);
263 	return (1);
264 }
265 
266 /*
267  * Return a reasonable description of a DIO device.
268  */
269 char *
270 dio_devinfo(da, buf, buflen)
271 	struct dio_attach_args *da;
272 	char *buf;
273 	size_t buflen;
274 {
275 #ifdef DIOVERBOSE
276 	int i;
277 #endif
278 
279 	memset(buf, 0, buflen);
280 
281 	/*
282 	 * Deal with lame internal HP-IB controllers which don't have
283 	 * consistent/reliable device ids.
284 	 */
285 	if (da->da_scode == 7 && internalhpib) {
286 		sprintf(buf, DIO_DEVICE_DESC_IHPIB);
287 		return (buf);
288 	}
289 
290 #ifdef DIOVERBOSE
291 	/*
292 	 * Find the description matching our primary id.
293 	 * If we're a framebuffer, we also check the secondary id.
294 	 */
295 	for (i = 0; i < DIO_NDEVICES; i++) {
296 		if (da->da_id == dio_devdescs[i].dd_id) {
297 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
298 				if (da->da_secid == dio_devdescs[i].dd_secid) {
299 					goto foundit;
300 				}
301 			} else {
302 			foundit:
303 				sprintf(buf, "%s", dio_devdescs[i].dd_desc);
304 				return (buf);
305 			}
306 		}
307 	}
308 #endif /* DIOVERBOSE */
309 
310 	/*
311 	 * Device is unknown.  Construct something reasonable.
312 	 */
313 	sprintf(buf, "device id = 0x%x secid = 0x%x",
314 	    da->da_id, da->da_secid);
315 	return (buf);
316 }
317 
318 /*
319  * Establish an interrupt handler for a DIO device.
320  */
321 void *
322 dio_intr_establish(func, arg, ipl, priority)
323 	int (*func) __P((void *));
324 	void *arg;
325 	int ipl;
326 	int priority;
327 {
328 	void *ih;
329 
330 	ih = intr_establish(func, arg, ipl, priority);
331 
332 	if (priority == IPL_BIO)
333 		dmacomputeipl();
334 
335 	return (ih);
336 }
337 
338 /*
339  * Remove an interrupt handler for a DIO device.
340  */
341 void
342 dio_intr_disestablish(arg)
343 	void *arg;
344 {
345 	struct hp300_intrhand *ih = arg;
346 	int priority = ih->ih_priority;
347 
348 	intr_disestablish(arg);
349 
350 	if (priority == IPL_BIO)
351 		dmacomputeipl();
352 }
353