Home | History | Annotate | Line # | Download | only in ofisa
ofisa.c revision 1.27
      1 /*	$NetBSD: ofisa.c,v 1.27 2021/01/19 14:39:20 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.27 2021/01/19 14:39:20 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 	{ 0 }
     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_match_compat_data(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 = "ofw";			/* XXX */
    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 	}
    136 }
    137 
    138 int
    139 ofisa_reg_count(int phandle)
    140 {
    141 	int len;
    142 
    143 	len = OF_getproplen(phandle, "reg");
    144 
    145 	/* nonexistent or obviously malformed "reg" property */
    146 	if (len < 0 || (len % 12) != 0)
    147 		return (-1);
    148 	return (len / 12);
    149 }
    150 
    151 int
    152 ofisa_reg_get(int phandle, struct ofisa_reg_desc *descp, int ndescs)
    153 {
    154 	char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
    155 	int i, proplen, rv;
    156 
    157 	i = ofisa_reg_count(phandle);
    158 	if (i < 0)
    159 		return (-1);
    160 	proplen = i * 12;
    161 	ndescs = uimin(ndescs, i);
    162 
    163 	i = ndescs * 12;
    164 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    165 		buf = malloc(i, M_TEMP, M_WAITOK);
    166 	} else {
    167 		buf = small;
    168 	}
    169 
    170 	if (OF_getprop(phandle, "reg", buf, i) != proplen) {
    171 		rv = -1;
    172 		goto out;
    173 	}
    174 
    175 	for (i = 0, bp = buf; i < ndescs; i++, bp += 12) {
    176 		if (of_decode_int(&bp[0]) & 1)
    177 			descp[i].type = OFISA_REG_TYPE_IO;
    178 		else
    179 			descp[i].type = OFISA_REG_TYPE_MEM;
    180 		descp[i].addr = of_decode_int(&bp[4]);
    181 		descp[i].len = of_decode_int(&bp[8]);
    182 	}
    183 	rv = i;		/* number of descriptors processed (== ndescs) */
    184 
    185 out:
    186 	if (buf != small)
    187 		free(buf, M_TEMP);
    188 	return (rv);
    189 }
    190 
    191 void
    192 ofisa_reg_print(struct ofisa_reg_desc *descp, int ndescs)
    193 {
    194 	int i;
    195 
    196 	if (ndescs == 0) {
    197 		printf("none");
    198 		return;
    199 	}
    200 
    201 	for (i = 0; i < ndescs; i++) {
    202 		printf("%s%s 0x%lx/%ld", i ? ", " : "",
    203 		    descp[i].type == OFISA_REG_TYPE_IO ? "io" : "mem",
    204 		    (long)descp[i].addr, (long)descp[i].len);
    205 	}
    206 }
    207 
    208 int
    209 ofisa_intr_count(int phandle)
    210 {
    211 	int len;
    212 
    213 	len = OF_getproplen(phandle, "interrupts");
    214 
    215 	/* nonexistent or obviously malformed "reg" property */
    216 	if (len < 0 || (len % 8) != 0)
    217 		return (-1);
    218 	return (len / 8);
    219 }
    220 
    221 int
    222 ofisa_intr_get(int phandle, struct ofisa_intr_desc *descp, int ndescs)
    223 {
    224 	char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
    225 	int i, proplen, rv;
    226 
    227 	i = ofisa_intr_count(phandle);
    228 	if (i < 0)
    229 		return (-1);
    230 	proplen = i * 8;
    231 	ndescs = uimin(ndescs, i);
    232 
    233 	i = ndescs * 8;
    234 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    235 		buf = malloc(i, M_TEMP, M_WAITOK);
    236 	} else {
    237 		buf = small;
    238 	}
    239 
    240 	if (OF_getprop(phandle, "interrupts", buf, i) != proplen) {
    241 		rv = -1;
    242 		goto out;
    243 	}
    244 
    245 	for (i = 0, bp = buf; i < ndescs; i++, bp += 8) {
    246 		descp[i].irq = of_decode_int(&bp[0]);
    247 		switch (of_decode_int(&bp[4])) {
    248 		case 0:
    249 		case 1:
    250 			descp[i].share = IST_LEVEL;
    251 			break;
    252 		case 2:
    253 		case 3:
    254 			descp[i].share = IST_EDGE;
    255 			break;
    256 #ifdef DIAGNOSTIC
    257 		default:
    258 			/* Dunno what to do, so fail. */
    259 			printf("ofisa_intr_get: unknown interrupt type %d\n",
    260 			    of_decode_int(&bp[4]));
    261 			rv = -1;
    262 			goto out;
    263 #endif
    264 		}
    265 	}
    266 	rv = i;		/* number of descriptors processed (== ndescs) */
    267 
    268 out:
    269 	if (buf != small)
    270 		free(buf, M_TEMP);
    271 	return (rv);
    272 }
    273 
    274 void
    275 ofisa_intr_print(struct ofisa_intr_desc *descp, int ndescs)
    276 {
    277 	int i;
    278 
    279 	if (ndescs == 0) {
    280 		printf("none");
    281 		return;
    282 	}
    283 
    284 	for (i = 0; i < ndescs; i++) {
    285 		printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
    286 		    descp[i].share == IST_LEVEL ? "level" : "edge");
    287 	}
    288 }
    289 
    290 int
    291 ofisa_dma_count(int phandle)
    292 {
    293 	int len;
    294 
    295 	len = OF_getproplen(phandle, "dma");
    296 
    297 	/* nonexistent or obviously malformed "reg" property */
    298 	if (len < 0 || (len % 20) != 0)
    299 		return (-1);
    300 	return (len / 20);
    301 }
    302 
    303 int
    304 ofisa_dma_get(int phandle, struct ofisa_dma_desc *descp, int ndescs)
    305 {
    306 	char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
    307 	int i, proplen, rv;
    308 
    309 	i = ofisa_dma_count(phandle);
    310 	if (i < 0)
    311 		return (-1);
    312 	proplen = i * 20;
    313 	ndescs = uimin(ndescs, i);
    314 
    315 	i = ndescs * 20;
    316 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    317 		buf = malloc(i, M_TEMP, M_WAITOK);
    318 	} else {
    319 		buf = small;
    320 	}
    321 
    322 	if (OF_getprop(phandle, "dma", buf, i) != proplen) {
    323 		rv = -1;
    324 		goto out;
    325 	}
    326 
    327 	for (i = 0, bp = buf; i < ndescs; i++, bp += 20) {
    328 		descp[i].drq = of_decode_int(&bp[0]);
    329 		descp[i].mode = of_decode_int(&bp[4]);
    330 		descp[i].width = of_decode_int(&bp[8]);
    331 		descp[i].countwidth = of_decode_int(&bp[12]);
    332 		descp[i].busmaster = of_decode_int(&bp[16]);
    333 	}
    334 	rv = i;		/* number of descriptors processed (== ndescs) */
    335 
    336 out:
    337 	if (buf != small)
    338 		free(buf, M_TEMP);
    339 	return (rv);
    340 }
    341 
    342 void
    343 ofisa_dma_print(struct ofisa_dma_desc *descp, int ndescs)
    344 {
    345 	char unkmode[16];
    346 	const char *modestr;
    347 	int i;
    348 
    349 	if (ndescs == 0) {
    350 		printf("none");
    351 		return;
    352 	}
    353 
    354 	for (i = 0; i < ndescs; i++) {
    355 		switch (descp[i].mode) {
    356 		case OFISA_DMA_MODE_COMPAT:
    357 			modestr = "compat";
    358 			break;
    359 		case OFISA_DMA_MODE_A:
    360 			modestr = "A";
    361 			break;
    362 		case OFISA_DMA_MODE_B:
    363 			modestr = "B";
    364 			break;
    365 		case OFISA_DMA_MODE_F:
    366 			modestr = "F";
    367 			break;
    368 		case OFISA_DMA_MODE_C:
    369 			modestr = "C";
    370 			break;
    371 		default:
    372 			snprintf(unkmode, sizeof(unkmode), "??? (%d)",
    373 			    descp[i].mode);
    374 			modestr = unkmode;
    375 			break;
    376 		}
    377 
    378 		printf("%s%d %s mode %d-bit (%d-bit count)%s", i ? ", " : "",
    379 		    descp[i].drq, modestr, descp[i].width,
    380 		    descp[i].countwidth,
    381 		    descp[i].busmaster ? " busmaster" : "");
    382 
    383 	}
    384 }
    385 
    386 void
    387 ofisa_print_model(device_t self, int phandle)
    388 {
    389 	char *model, small[OFW_MAX_STACK_BUF_SIZE];
    390         int n = OF_getproplen(phandle, "model");
    391 
    392         if (n <= 0)
    393 		return;
    394 
    395 	if (n > OFW_MAX_STACK_BUF_SIZE) {
    396 		model = malloc(n, M_TEMP, M_WAITOK);
    397 	} else {
    398 		model = small;
    399 	}
    400 
    401 	if (OF_getprop(phandle, "model", model, n) != n)
    402 		goto out;
    403 
    404 	aprint_normal(": %s\n", model);
    405 	if (self)
    406 		aprint_normal_dev(self, "");
    407 out:
    408 	if (model != small)
    409 		free(model, M_TEMP);
    410 }
    411