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