Home | History | Annotate | Line # | Download | only in dev
dio.c revision 1.6
      1 /*	$NetBSD: dio.c,v 1.6 1997/04/27 20:58:55 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 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 REGENTS OR CONTRIBUTORS BE
     30  * 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/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/device.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 
     49 #include <machine/autoconf.h>
     50 #include <machine/cpu.h>
     51 #include <machine/hp300spu.h>
     52 
     53 #include <hp300/dev/dioreg.h>
     54 #include <hp300/dev/diovar.h>
     55 
     56 #include <hp300/dev/diodevs.h>
     57 #include <hp300/dev/diodevs_data.h>
     58 
     59 extern	caddr_t internalhpib;
     60 
     61 int	dio_scodesize __P((struct dio_attach_args *));
     62 char	*dio_devinfo __P((struct dio_attach_args *, char *, size_t));
     63 
     64 int	diomatch __P((struct device *, struct cfdata *, void *));
     65 void	dioattach __P((struct device *, struct device *, void *));
     66 int	dioprint __P((void *, const char *));
     67 int	diosubmatch __P((struct device *, struct cfdata *, void *));
     68 
     69 struct cfattach dio_ca = {
     70 	sizeof(struct device), diomatch, dioattach
     71 };
     72 
     73 struct cfdriver dio_cd = {
     74 	NULL, "dio", DV_DULL
     75 };
     76 
     77 int
     78 diomatch(parent, match, aux)
     79 	struct device *parent;
     80 	struct cfdata *match;
     81 	void *aux;
     82 {
     83 	static int dio_matched = 0;
     84 
     85 	/* Allow only one instance. */
     86 	if (dio_matched)
     87 		return (0);
     88 
     89 	dio_matched = 1;
     90 	return (1);
     91 }
     92 
     93 void
     94 dioattach(parent, self, aux)
     95 	struct device *parent, *self;
     96 	void *aux;
     97 {
     98 	struct dio_attach_args da;
     99 	caddr_t pa, va;
    100 	int scode, scmax, didmap, scodesize;
    101 
    102 	scmax = DIO_SCMAX(machineid);
    103 	printf("\n");
    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 		 *	- this is the console select code.
    118 		 */
    119 		pa = dio_scodetopa(scode);
    120 		if (scode == conscode)
    121 			va = conaddr;
    122 		else if ((scode == 7) && internalhpib)
    123 			va = internalhpib = (caddr_t)IIOV(pa);
    124 		else {
    125 			va = iomap(pa, NBPG);
    126 			if (va == NULL) {
    127 				printf("%s: can't map scode %d\n",
    128 				    self->dv_xname, scode);
    129 				scode++;
    130 				continue;
    131 			}
    132 			didmap = 1;
    133 		}
    134 
    135 		/* Check for hardware. */
    136 		if (badaddr(va)) {
    137 			if (didmap)
    138 				iounmap(va, NBPG);
    139 			scode++;
    140 			continue;
    141 		}
    142 
    143 		/* Fill out attach args. */
    144 		bzero(&da, sizeof(da));
    145 		da.da_scode = scode;
    146 		da.da_id = DIO_ID(va);
    147 
    148 		if (DIO_ISFRAMEBUFFER(da.da_id))
    149 			da.da_secid = DIO_SECID(va);
    150 
    151 		da.da_size = DIO_SIZE(scode, va);
    152 		scodesize = dio_scodesize(&da);
    153 		if (DIO_ISDIO(scode))
    154 			da.da_size *= scodesize;
    155 
    156 		/* No longer need the device to be mapped. */
    157 		if (didmap)
    158 			iounmap(va, NBPG);
    159 
    160 		/* Attach matching device. */
    161 		config_found_sm(self, &da, dioprint, diosubmatch);
    162 		scode += scodesize;
    163 	}
    164 }
    165 
    166 int
    167 diosubmatch(parent, cf, aux)
    168 	struct device *parent;
    169 	struct cfdata *cf;
    170 	void *aux;
    171 {
    172 	struct dio_attach_args *da = aux;
    173 
    174 	if (cf->diocf_scode != DIO_UNKNOWN_SCODE &&
    175 	    cf->diocf_scode != da->da_scode)
    176 		return (0);
    177 
    178 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    179 }
    180 
    181 int
    182 dioprint(aux, pnp)
    183 	void *aux;
    184 	const char *pnp;
    185 {
    186 	struct dio_attach_args *da = aux;
    187 	char buf[128];
    188 
    189 	if (pnp)
    190 		printf("%s at %s", dio_devinfo(da, buf, sizeof(buf)), pnp);
    191 	printf(" scode %d", da->da_scode);
    192 	return (UNCONF);
    193 }
    194 
    195 /*
    196  * Convert a select code to a system physical address.
    197  */
    198 void *
    199 dio_scodetopa(scode)
    200 	int scode;
    201 {
    202 	u_long rval;
    203 
    204 	if (scode == 7 && internalhpib)
    205 		rval = DIO_IHPIBADDR;
    206 	else if (DIO_ISDIO(scode))
    207 		rval = DIO_BASE + (scode * DIO_DEVSIZE);
    208 	else if (DIO_ISDIOII(scode))
    209 		rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
    210 	else
    211 		rval = 0;
    212 
    213 	return ((void *)rval);
    214 }
    215 
    216 /*
    217  * Return the select code size for this device, defaulting to 1
    218  * if we don't know what kind of device we have.
    219  */
    220 int
    221 dio_scodesize(da)
    222 	struct dio_attach_args *da;
    223 {
    224 	int i;
    225 
    226 	/*
    227 	 * Deal with lame internal HP-IB controllers which don't have
    228 	 * consistent/reliable device ids.
    229 	 */
    230 	if (da->da_scode == 7 && internalhpib)
    231 		return (1);
    232 
    233 	/*
    234 	 * Find the dio_devdata matchind the primary id.
    235 	 * If we're a framebuffer, we also check the secondary id.
    236 	 */
    237 	for (i = 0; i < DIO_NDEVICES; i++) {
    238 		if (da->da_id == dio_devdatas[i].dd_id) {
    239 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
    240 				if (da->da_secid == dio_devdatas[i].dd_secid) {
    241 					goto foundit;
    242 				}
    243 			} else {
    244 			foundit:
    245 				return (dio_devdatas[i].dd_nscode);
    246 			}
    247 		}
    248 	}
    249 
    250 	/*
    251 	 * Device is unknown.  Print a warning and assume a default.
    252 	 */
    253 	printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n",
    254 	    da->da_id, da->da_secid);
    255 	return (1);
    256 }
    257 
    258 /*
    259  * Return a reasonable description of a DIO device.
    260  */
    261 char *
    262 dio_devinfo(da, buf, buflen)
    263 	struct dio_attach_args *da;
    264 	char *buf;
    265 	size_t buflen;
    266 {
    267 #ifdef DIOVERBOSE
    268 	int i;
    269 #endif
    270 
    271 	bzero(buf, buflen);
    272 
    273 	/*
    274 	 * Deal with lame internal HP-IB controllers which don't have
    275 	 * consistent/reliable device ids.
    276 	 */
    277 	if (da->da_scode == 7 && internalhpib) {
    278 		sprintf(buf, DIO_DEVICE_DESC_IHPIB);
    279 		return (buf);
    280 	}
    281 
    282 #ifdef DIOVERBOSE
    283 	/*
    284 	 * Find the description matching our primary id.
    285 	 * If we're a framebuffer, we also check the secondary id.
    286 	 */
    287 	for (i = 0; i < DIO_NDEVICES; i++) {
    288 		if (da->da_id == dio_devdescs[i].dd_id) {
    289 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
    290 				if (da->da_secid == dio_devdescs[i].dd_secid) {
    291 					goto foundit;
    292 				}
    293 			} else {
    294 			foundit:
    295 				sprintf(buf, "%s", dio_devdescs[i].dd_desc);
    296 				return (buf);
    297 			}
    298 		}
    299 	}
    300 #endif /* DIOVERBOSE */
    301 
    302 	/*
    303 	 * Device is unknown.  Construct something reasonable.
    304 	 */
    305 	sprintf(buf, "device id = 0x%x secid = 0x%x",
    306 	    da->da_id, da->da_secid);
    307 	return (buf);
    308 }
    309