Home | History | Annotate | Line # | Download | only in ofisa
ofisa.c revision 1.32
      1 /*	$NetBSD: ofisa.c,v 1.32 2021/04/27 21:39:39 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright 1997, 1998
      5  * Digital Equipment Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and
      8  * copied only in accordance with the following terms and conditions.
      9  * Subject to these conditions, you may download, copy, install,
     10  * use, modify and distribute this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce
     14  *    and retain this copyright notice and list of conditions as
     15  *    they appear in the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19  *    Corporation" name nor any trademark or logo of Digital Equipment
     20  *    Corporation may be used to endorse or promote products derived
     21  *    from this software without the prior written permission of
     22  *    Digital Equipment Corporation.
     23  *
     24  * 3) This software is provided "AS-IS" and any express or implied
     25  *    warranties, including but not limited to, any implied warranties
     26  *    of merchantability, fitness for a particular purpose, or
     27  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28  *    liable for any damages whatsoever, and in particular, DIGITAL
     29  *    shall not be liable for special, indirect, consequential, or
     30  *    incidental damages or damages for lost profits, loss of
     31  *    revenue or loss of use, whether such damages arise in contract,
     32  *    negligence, tort, under statute, in equity, at law or otherwise,
     33  *    even if advised of the possibility of such damage.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: ofisa.c,v 1.32 2021/04/27 21:39:39 thorpej Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/malloc.h>
     43 #include <sys/bus.h>
     44 #include <sys/intr.h>
     45 
     46 #include <dev/ofw/openfirm.h>
     47 #include <dev/isa/isavar.h>
     48 #include <dev/ofisa/ofisavar.h>
     49 
     50 #include "isadma.h"
     51 
     52 #define	OFW_MAX_STACK_BUF_SIZE	256
     53 
     54 static int	ofisamatch(device_t, cfdata_t, void *);
     55 static void	ofisaattach(device_t, device_t, void *);
     56 
     57 CFATTACH_DECL_NEW(ofisa, 0,
     58     ofisamatch, ofisaattach, NULL, NULL);
     59 
     60 extern struct cfdriver ofisa_cd;
     61 
     62 static int	ofisaprint(void *, const char *);
     63 
     64 static int
     65 ofisaprint(void *aux, const char *pnp)
     66 {
     67 	struct ofbus_attach_args *oba = aux;
     68 	char name[64];
     69 
     70 	(void)of_packagename(oba->oba_phandle, name, sizeof name);
     71 	if (pnp)
     72 		aprint_normal("%s at %s", name, pnp);
     73 	else
     74 		aprint_normal(" (%s)", name);
     75 	return UNCONF;
     76 }
     77 
     78 static const struct device_compatible_entry compat_data[] = {
     79 	{ .compat = "pnpPNP,a00" },
     80 	DEVICE_COMPAT_EOL
     81 };
     82 
     83 int
     84 ofisamatch(device_t parent, cfdata_t cf, void *aux)
     85 {
     86 	struct ofbus_attach_args *oba = aux;
     87 	int rv;
     88 
     89 	rv = of_compatible_match(oba->oba_phandle, compat_data) ? 5 : 0;
     90 #ifdef _OFISA_MD_MATCH
     91 	if (!rv)
     92 		rv = ofisa_md_match(parent, cf, aux);
     93 #endif
     94 
     95 	return (rv);
     96 }
     97 
     98 void
     99 ofisaattach(device_t parent, device_t self, void *aux)
    100 {
    101 	struct ofbus_attach_args *oba = aux;
    102 	struct isabus_attach_args iba;
    103 	struct ofisa_attach_args aa;
    104 	int child;
    105 
    106 	if (ofisa_get_isabus_data(oba->oba_phandle, &iba) < 0) {
    107 		printf(": couldn't get essential bus data\n");
    108 		return;
    109 	}
    110 
    111 	printf("\n");
    112 
    113 #if NISADMA > 0
    114 	/*
    115 	 * Initialize our DMA state.
    116 	 */
    117 	isa_dmainit(iba.iba_ic, iba.iba_iot, iba.iba_dmat, self);
    118 #endif
    119 
    120 	for (child = OF_child(oba->oba_phandle); child;
    121 	    child = OF_peer(child)) {
    122 		if (ofisa_ignore_child(oba->oba_phandle, child))
    123 			continue;
    124 
    125 		memset(&aa, 0, sizeof aa);
    126 
    127 		aa.oba.oba_busname = "ofisa";
    128 		aa.oba.oba_phandle = child;
    129 		aa.iot = iba.iba_iot;
    130 		aa.memt = iba.iba_memt;
    131 		aa.dmat = iba.iba_dmat;
    132 		aa.ic = iba.iba_ic;
    133 
    134 		config_found(self, &aa, ofisaprint,
    135 		    CFARG_DEVHANDLE, devhandle_from_of(child),
    136 		    CFARG_EOL);
    137 	}
    138 }
    139 
    140 int
    141 ofisa_reg_count(int phandle)
    142 {
    143 	int len;
    144 
    145 	len = OF_getproplen(phandle, "reg");
    146 
    147 	/* nonexistent or obviously malformed "reg" property */
    148 	if (len < 0 || (len % 12) != 0)
    149 		return (-1);
    150 	return (len / 12);
    151 }
    152 
    153 int
    154 ofisa_reg_get(int phandle, struct ofisa_reg_desc *descp, int ndescs)
    155 {
    156 	char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
    157 	int i, proplen, rv;
    158 
    159 	i = ofisa_reg_count(phandle);
    160 	if (i < 0)
    161 		return (-1);
    162 	proplen = i * 12;
    163 	ndescs = uimin(ndescs, i);
    164 
    165 	i = ndescs * 12;
    166 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    167 		buf = malloc(i, M_TEMP, M_WAITOK);
    168 	} else {
    169 		buf = small;
    170 	}
    171 
    172 	if (OF_getprop(phandle, "reg", buf, i) != proplen) {
    173 		rv = -1;
    174 		goto out;
    175 	}
    176 
    177 	for (i = 0, bp = buf; i < ndescs; i++, bp += 12) {
    178 		if (of_decode_int(&bp[0]) & 1)
    179 			descp[i].type = OFISA_REG_TYPE_IO;
    180 		else
    181 			descp[i].type = OFISA_REG_TYPE_MEM;
    182 		descp[i].addr = of_decode_int(&bp[4]);
    183 		descp[i].len = of_decode_int(&bp[8]);
    184 	}
    185 	rv = i;		/* number of descriptors processed (== ndescs) */
    186 
    187 out:
    188 	if (buf != small)
    189 		free(buf, M_TEMP);
    190 	return (rv);
    191 }
    192 
    193 void
    194 ofisa_reg_print(struct ofisa_reg_desc *descp, int ndescs)
    195 {
    196 	int i;
    197 
    198 	if (ndescs == 0) {
    199 		printf("none");
    200 		return;
    201 	}
    202 
    203 	for (i = 0; i < ndescs; i++) {
    204 		printf("%s%s 0x%lx/%ld", i ? ", " : "",
    205 		    descp[i].type == OFISA_REG_TYPE_IO ? "io" : "mem",
    206 		    (long)descp[i].addr, (long)descp[i].len);
    207 	}
    208 }
    209 
    210 int
    211 ofisa_intr_count(int phandle)
    212 {
    213 	int len;
    214 
    215 	len = OF_getproplen(phandle, "interrupts");
    216 
    217 	/* nonexistent or obviously malformed "reg" property */
    218 	if (len < 0 || (len % 8) != 0)
    219 		return (-1);
    220 	return (len / 8);
    221 }
    222 
    223 int
    224 ofisa_intr_get(int phandle, struct ofisa_intr_desc *descp, int ndescs)
    225 {
    226 	char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
    227 	int i, proplen, rv;
    228 
    229 	i = ofisa_intr_count(phandle);
    230 	if (i < 0)
    231 		return (-1);
    232 	proplen = i * 8;
    233 	ndescs = uimin(ndescs, i);
    234 
    235 	i = ndescs * 8;
    236 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    237 		buf = malloc(i, M_TEMP, M_WAITOK);
    238 	} else {
    239 		buf = small;
    240 	}
    241 
    242 	if (OF_getprop(phandle, "interrupts", buf, i) != proplen) {
    243 		rv = -1;
    244 		goto out;
    245 	}
    246 
    247 	for (i = 0, bp = buf; i < ndescs; i++, bp += 8) {
    248 		descp[i].irq = of_decode_int(&bp[0]);
    249 		switch (of_decode_int(&bp[4])) {
    250 		case 0:
    251 		case 1:
    252 			descp[i].share = IST_LEVEL;
    253 			break;
    254 		case 2:
    255 		case 3:
    256 			descp[i].share = IST_EDGE;
    257 			break;
    258 #ifdef DIAGNOSTIC
    259 		default:
    260 			/* Dunno what to do, so fail. */
    261 			printf("ofisa_intr_get: unknown interrupt type %d\n",
    262 			    of_decode_int(&bp[4]));
    263 			rv = -1;
    264 			goto out;
    265 #endif
    266 		}
    267 	}
    268 	rv = i;		/* number of descriptors processed (== ndescs) */
    269 
    270 out:
    271 	if (buf != small)
    272 		free(buf, M_TEMP);
    273 	return (rv);
    274 }
    275 
    276 void
    277 ofisa_intr_print(struct ofisa_intr_desc *descp, int ndescs)
    278 {
    279 	int i;
    280 
    281 	if (ndescs == 0) {
    282 		printf("none");
    283 		return;
    284 	}
    285 
    286 	for (i = 0; i < ndescs; i++) {
    287 		printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
    288 		    descp[i].share == IST_LEVEL ? "level" : "edge");
    289 	}
    290 }
    291 
    292 int
    293 ofisa_dma_count(int phandle)
    294 {
    295 	int len;
    296 
    297 	len = OF_getproplen(phandle, "dma");
    298 
    299 	/* nonexistent or obviously malformed "reg" property */
    300 	if (len < 0 || (len % 20) != 0)
    301 		return (-1);
    302 	return (len / 20);
    303 }
    304 
    305 int
    306 ofisa_dma_get(int phandle, struct ofisa_dma_desc *descp, int ndescs)
    307 {
    308 	char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
    309 	int i, proplen, rv;
    310 
    311 	i = ofisa_dma_count(phandle);
    312 	if (i < 0)
    313 		return (-1);
    314 	proplen = i * 20;
    315 	ndescs = uimin(ndescs, i);
    316 
    317 	i = ndescs * 20;
    318 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    319 		buf = malloc(i, M_TEMP, M_WAITOK);
    320 	} else {
    321 		buf = small;
    322 	}
    323 
    324 	if (OF_getprop(phandle, "dma", buf, i) != proplen) {
    325 		rv = -1;
    326 		goto out;
    327 	}
    328 
    329 	for (i = 0, bp = buf; i < ndescs; i++, bp += 20) {
    330 		descp[i].drq = of_decode_int(&bp[0]);
    331 		descp[i].mode = of_decode_int(&bp[4]);
    332 		descp[i].width = of_decode_int(&bp[8]);
    333 		descp[i].countwidth = of_decode_int(&bp[12]);
    334 		descp[i].busmaster = of_decode_int(&bp[16]);
    335 	}
    336 	rv = i;		/* number of descriptors processed (== ndescs) */
    337 
    338 out:
    339 	if (buf != small)
    340 		free(buf, M_TEMP);
    341 	return (rv);
    342 }
    343 
    344 void
    345 ofisa_dma_print(struct ofisa_dma_desc *descp, int ndescs)
    346 {
    347 	char unkmode[16];
    348 	const char *modestr;
    349 	int i;
    350 
    351 	if (ndescs == 0) {
    352 		printf("none");
    353 		return;
    354 	}
    355 
    356 	for (i = 0; i < ndescs; i++) {
    357 		switch (descp[i].mode) {
    358 		case OFISA_DMA_MODE_COMPAT:
    359 			modestr = "compat";
    360 			break;
    361 		case OFISA_DMA_MODE_A:
    362 			modestr = "A";
    363 			break;
    364 		case OFISA_DMA_MODE_B:
    365 			modestr = "B";
    366 			break;
    367 		case OFISA_DMA_MODE_F:
    368 			modestr = "F";
    369 			break;
    370 		case OFISA_DMA_MODE_C:
    371 			modestr = "C";
    372 			break;
    373 		default:
    374 			snprintf(unkmode, sizeof(unkmode), "??? (%d)",
    375 			    descp[i].mode);
    376 			modestr = unkmode;
    377 			break;
    378 		}
    379 
    380 		printf("%s%d %s mode %d-bit (%d-bit count)%s", i ? ", " : "",
    381 		    descp[i].drq, modestr, descp[i].width,
    382 		    descp[i].countwidth,
    383 		    descp[i].busmaster ? " busmaster" : "");
    384 
    385 	}
    386 }
    387 
    388 void
    389 ofisa_print_model(device_t self, int phandle)
    390 {
    391 	char *model, small[OFW_MAX_STACK_BUF_SIZE];
    392         int n = OF_getproplen(phandle, "model");
    393 
    394         if (n <= 0)
    395 		return;
    396 
    397 	if (n > OFW_MAX_STACK_BUF_SIZE) {
    398 		model = malloc(n, M_TEMP, M_WAITOK);
    399 	} else {
    400 		model = small;
    401 	}
    402 
    403 	if (OF_getprop(phandle, "model", model, n) != n)
    404 		goto out;
    405 
    406 	aprint_normal(": %s\n", model);
    407 	if (self)
    408 		aprint_normal_dev(self, "");
    409 out:
    410 	if (model != small)
    411 		free(model, M_TEMP);
    412 }
    413