Home | History | Annotate | Line # | Download | only in ofisa
ofisa.c revision 1.2
      1 /*	$NetBSD: ofisa.c,v 1.2 1998/03/21 02:06:17 cgd 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/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <sys/malloc.h>
     40 #include <machine/bus.h>
     41 #include <machine/intr.h>
     42 
     43 #include <dev/ofw/openfirm.h>
     44 #include <dev/isa/isavar.h>
     45 #include <dev/ofisa/ofisavar.h>
     46 
     47 #define	OFW_MAX_STACK_BUF_SIZE	256
     48 
     49 static int	ofisamatch __P((struct device *, struct cfdata *, void *));
     50 static void	ofisaattach __P((struct device *, struct device *, void *));
     51 
     52 struct cfattach ofisa_ca = {
     53 	sizeof(struct device), ofisamatch, ofisaattach
     54 };
     55 
     56 extern struct cfdriver ofisa_cd;
     57 
     58 static int	ofisaprint __P((void *, const char *));
     59 
     60 static int
     61 ofisaprint(aux, pnp)
     62 	void *aux;
     63 	const char *pnp;
     64 {
     65 	struct ofbus_attach_args *oba = aux;
     66 	char name[64];
     67 
     68 	(void)of_packagename(oba->oba_phandle, name, sizeof name);
     69 	if (pnp)
     70 		printf("%s at %s", name, pnp);
     71 	else
     72 		printf(" (%s)", name);
     73 	return UNCONF;
     74 }
     75 
     76 int
     77 ofisamatch(parent, cf, aux)
     78 	struct device *parent;
     79 	struct cfdata *cf;
     80 	void *aux;
     81 {
     82 	struct ofbus_attach_args *oba = aux;
     83 	const char *compatible_strings[] = { "pnpPNP,a00", NULL };
     84 	int rv = 0;
     85 
     86 	if (of_compatible(oba->oba_phandle, compatible_strings) != -1)
     87 		rv = 5;
     88 
     89 #ifdef _OFISA_MD_MATCH
     90 	if (!rv)
     91 		rv = ofisa_md_match(parent, cf, aux);
     92 #endif
     93 
     94 	return (rv);
     95 }
     96 
     97 void
     98 ofisaattach(parent, dev, aux)
     99 	struct device *parent, *dev;
    100 	void *aux;
    101 {
    102 
    103 	struct ofbus_attach_args *oba = aux;
    104 	struct isabus_attach_args iba;
    105 	struct ofisa_attach_args aa;
    106 	int child;
    107 
    108 	if (ofisa_get_isabus_data(oba->oba_phandle, &iba) < 0) {
    109 		printf(": couldn't get essential bus data\n");
    110 		return;
    111 	}
    112 
    113 	printf("\n");
    114 
    115 	for (child = OF_child(oba->oba_phandle); child;
    116 	    child = OF_peer(child)) {
    117 		if (ofisa_ignore_child(oba->oba_phandle, child))
    118 			continue;
    119 
    120 		bzero(&aa, sizeof aa);
    121 
    122 		aa.oba.oba_busname = "ofw";			/* XXX */
    123 		aa.oba.oba_phandle = child;
    124 		aa.iot = iba.iba_iot;
    125 		aa.memt = iba.iba_memt;
    126 		aa.dmat = iba.iba_dmat;
    127 		aa.ic = iba.iba_ic;
    128 
    129 		config_found(dev, &aa, ofisaprint);
    130 	}
    131 }
    132 
    133 int
    134 ofisa_reg_count(phandle)
    135 	int phandle;
    136 {
    137 	int len;
    138 
    139 	len = OF_getproplen(phandle, "reg");
    140 
    141 	/* nonexistant or obviously malformed "reg" property */
    142 	if (len < 0 || (len % 12) != 0)
    143 		return (-1);
    144 	return (len / 12);
    145 }
    146 
    147 int
    148 ofisa_reg_get(phandle, descp, ndescs)
    149 	int phandle;
    150 	struct ofisa_reg_desc *descp;
    151 	int ndescs;
    152 {
    153 	char *buf, *bp;
    154 	int i, allocated, rv;
    155 
    156 	i = ofisa_reg_count(phandle);
    157 	if (i < 0)
    158 		return (-1);
    159 	ndescs = min(ndescs, i);
    160 
    161 	i = ndescs * 12;
    162 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    163 		buf = malloc(i, M_TEMP, M_WAITOK);
    164 		allocated = 1;
    165 	} else {
    166 		buf = alloca(i);
    167 		allocated = 0;
    168 	}
    169 
    170 	if (OF_getprop(phandle, "reg", buf, i) != i) {
    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 (allocated)
    187 		free(buf, M_TEMP);
    188 	return (rv);
    189 }
    190 
    191 void
    192 ofisa_reg_print(descp, ndescs)
    193 	struct ofisa_reg_desc *descp;
    194 	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(phandle)
    212 	int phandle;
    213 {
    214 	int len;
    215 
    216 	len = OF_getproplen(phandle, "interrupts");
    217 
    218 	/* nonexistant or obviously malformed "reg" property */
    219 	if (len < 0 || (len % 8) != 0)
    220 		return (-1);
    221 	return (len / 8);
    222 }
    223 
    224 int
    225 ofisa_intr_get(phandle, descp, ndescs)
    226 	int phandle;
    227 	struct ofisa_intr_desc *descp;
    228 	int ndescs;
    229 {
    230 	char *buf, *bp;
    231 	int i, allocated, rv;
    232 
    233 	i = ofisa_intr_count(phandle);
    234 	if (i < 0)
    235 		return (-1);
    236 	ndescs = min(ndescs, i);
    237 
    238 	i = ndescs * 8;
    239 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    240 		buf = malloc(i, M_TEMP, M_WAITOK);
    241 		allocated = 1;
    242 	} else {
    243 		buf = alloca(i);
    244 		allocated = 0;
    245 	}
    246 
    247 	if (OF_getprop(phandle, "interrupts", buf, i) != i) {
    248 		rv = -1;
    249 		goto out;
    250 	}
    251 
    252 	for (i = 0, bp = buf; i < ndescs; i++, bp += 8) {
    253 		descp[i].irq = of_decode_int(&bp[0]);
    254 		switch (of_decode_int(&bp[4])) {
    255 		case 0:
    256 		case 1:
    257 			descp[i].share = IST_LEVEL;
    258 			break;
    259 		case 2:
    260 		case 3:
    261 			descp[i].share = IST_EDGE;
    262 			break;
    263 #ifdef DIAGNOSTIC
    264 		default:
    265 			/* Dunno what to do, so fail. */
    266 			printf("ofisa_intr_get: unknown intrerrupt type %d\n",
    267 			    of_decode_int(&bp[4]));
    268 			rv = -1;
    269 			goto out;
    270 #endif
    271 		}
    272 	}
    273 	rv = i;		/* number of descriptors processed (== ndescs) */
    274 
    275 out:
    276 	if (allocated)
    277 		free(buf, M_TEMP);
    278 	return (rv);
    279 }
    280 
    281 void
    282 ofisa_intr_print(descp, ndescs)
    283 	struct ofisa_intr_desc *descp;
    284 	int ndescs;
    285 {
    286 	int i;
    287 
    288 	if (ndescs == 0) {
    289 		printf("none");
    290 		return;
    291 	}
    292 
    293 	for (i = 0; i < ndescs; i++) {
    294 		printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
    295 		    descp[i].share == IST_LEVEL ? "level" : "edge");
    296 	}
    297 }
    298