Home | History | Annotate | Line # | Download | only in ofisa
ofisa.c revision 1.1
      1  1.1  cgd /*	$NetBSD: ofisa.c,v 1.1 1998/02/07 00:46:52 cgd Exp $	*/
      2  1.1  cgd 
      3  1.1  cgd /*
      4  1.1  cgd  * Copyright 1997, 1998
      5  1.1  cgd  * Digital Equipment Corporation. All rights reserved.
      6  1.1  cgd  *
      7  1.1  cgd  * This software is furnished under license and may be used and
      8  1.1  cgd  * copied only in accordance with the following terms and conditions.
      9  1.1  cgd  * Subject to these conditions, you may download, copy, install,
     10  1.1  cgd  * use, modify and distribute this software in source and/or binary
     11  1.1  cgd  * form. No title or ownership is transferred hereby.
     12  1.1  cgd  *
     13  1.1  cgd  * 1) Any source code used, modified or distributed must reproduce
     14  1.1  cgd  *    and retain this copyright notice and list of conditions as
     15  1.1  cgd  *    they appear in the source file.
     16  1.1  cgd  *
     17  1.1  cgd  * 2) No right is granted to use any trade name, trademark, or logo of
     18  1.1  cgd  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19  1.1  cgd  *    Corporation" name nor any trademark or logo of Digital Equipment
     20  1.1  cgd  *    Corporation may be used to endorse or promote products derived
     21  1.1  cgd  *    from this software without the prior written permission of
     22  1.1  cgd  *    Digital Equipment Corporation.
     23  1.1  cgd  *
     24  1.1  cgd  * 3) This software is provided "AS-IS" and any express or implied
     25  1.1  cgd  *    warranties, including but not limited to, any implied warranties
     26  1.1  cgd  *    of merchantability, fitness for a particular purpose, or
     27  1.1  cgd  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28  1.1  cgd  *    liable for any damages whatsoever, and in particular, DIGITAL
     29  1.1  cgd  *    shall not be liable for special, indirect, consequential, or
     30  1.1  cgd  *    incidental damages or damages for lost profits, loss of
     31  1.1  cgd  *    revenue or loss of use, whether such damages arise in contract,
     32  1.1  cgd  *    negligence, tort, under statute, in equity, at law or otherwise,
     33  1.1  cgd  *    even if advised of the possibility of such damage.
     34  1.1  cgd  */
     35  1.1  cgd 
     36  1.1  cgd #include <sys/param.h>
     37  1.1  cgd #include <sys/systm.h>
     38  1.1  cgd #include <sys/device.h>
     39  1.1  cgd #include <sys/malloc.h>
     40  1.1  cgd #include <machine/bus.h>
     41  1.1  cgd #include <machine/intr.h>
     42  1.1  cgd 
     43  1.1  cgd #include <dev/ofw/openfirm.h>
     44  1.1  cgd #include <dev/isa/isavar.h>
     45  1.1  cgd #include <dev/ofisa/ofisavar.h>
     46  1.1  cgd 
     47  1.1  cgd #define	OFW_MAX_STACK_BUF_SIZE	256
     48  1.1  cgd 
     49  1.1  cgd static int	ofisamatch __P((struct device *, struct cfdata *, void *));
     50  1.1  cgd static void	ofisaattach __P((struct device *, struct device *, void *));
     51  1.1  cgd 
     52  1.1  cgd struct cfattach ofisa_ca = {
     53  1.1  cgd 	sizeof(struct device), ofisamatch, ofisaattach
     54  1.1  cgd };
     55  1.1  cgd 
     56  1.1  cgd struct cfdriver ofisa_cd = {
     57  1.1  cgd 	NULL, "ofisa", DV_DULL
     58  1.1  cgd };
     59  1.1  cgd 
     60  1.1  cgd static int	ofisaprint __P((void *, const char *));
     61  1.1  cgd 
     62  1.1  cgd static int
     63  1.1  cgd ofisaprint(aux, pnp)
     64  1.1  cgd 	void *aux;
     65  1.1  cgd 	const char *pnp;
     66  1.1  cgd {
     67  1.1  cgd 	struct ofprobe *ofp = aux;
     68  1.1  cgd 	char name[64];
     69  1.1  cgd 
     70  1.1  cgd 	(void)of_packagename(ofp->phandle, name, sizeof name);
     71  1.1  cgd 	if (pnp)
     72  1.1  cgd 		printf("%s at %s", name, pnp);
     73  1.1  cgd 	else
     74  1.1  cgd 		printf(" (%s)", name);
     75  1.1  cgd 	return UNCONF;
     76  1.1  cgd }
     77  1.1  cgd 
     78  1.1  cgd int
     79  1.1  cgd ofisamatch(parent, cf, aux)
     80  1.1  cgd 	struct device *parent;
     81  1.1  cgd 	struct cfdata *cf;
     82  1.1  cgd 	void *aux;
     83  1.1  cgd {
     84  1.1  cgd 	struct ofprobe *ofp = aux;
     85  1.1  cgd 	const char *compatible_strings[] = { "pnpPNP,a00", NULL };
     86  1.1  cgd 	int rv = 0;
     87  1.1  cgd 
     88  1.1  cgd 	if (of_compatible(ofp->phandle, compatible_strings) != -1)
     89  1.1  cgd 		rv = 5;
     90  1.1  cgd 
     91  1.1  cgd #ifdef _OFISA_MD_MATCH
     92  1.1  cgd 	if (!rv)
     93  1.1  cgd 		rv = ofisa_md_match(parent, cf, aux);
     94  1.1  cgd #endif
     95  1.1  cgd 
     96  1.1  cgd 	return (rv);
     97  1.1  cgd }
     98  1.1  cgd 
     99  1.1  cgd void
    100  1.1  cgd ofisaattach(parent, dev, aux)
    101  1.1  cgd 	struct device *parent, *dev;
    102  1.1  cgd 	void *aux;
    103  1.1  cgd {
    104  1.1  cgd 
    105  1.1  cgd 	struct ofprobe *ofp = aux;
    106  1.1  cgd 	struct isabus_attach_args iba;
    107  1.1  cgd 	struct ofisa_attach_args aa;
    108  1.1  cgd 	int child;
    109  1.1  cgd 
    110  1.1  cgd 	if (ofisa_get_isabus_data(ofp->phandle, &iba) < 0) {
    111  1.1  cgd 		printf(": couldn't get essential bus data\n");
    112  1.1  cgd 		return;
    113  1.1  cgd 	}
    114  1.1  cgd 
    115  1.1  cgd 	printf("\n");
    116  1.1  cgd 
    117  1.1  cgd 	for (child = OF_child(ofp->phandle); child;
    118  1.1  cgd 	    child = OF_peer(child)) {
    119  1.1  cgd 		if (ofisa_ignore_child(ofp->phandle, child))
    120  1.1  cgd 			continue;
    121  1.1  cgd 
    122  1.1  cgd 		bzero(&aa, sizeof aa);
    123  1.1  cgd 
    124  1.1  cgd 		aa.ofp.phandle = child;
    125  1.1  cgd 		aa.iot = iba.iba_iot;
    126  1.1  cgd 		aa.memt = iba.iba_memt;
    127  1.1  cgd 		aa.dmat = iba.iba_dmat;
    128  1.1  cgd 		aa.ic = iba.iba_ic;
    129  1.1  cgd 
    130  1.1  cgd 		config_found(dev, &aa, ofisaprint);
    131  1.1  cgd 	}
    132  1.1  cgd }
    133  1.1  cgd 
    134  1.1  cgd int
    135  1.1  cgd ofisa_reg_count(phandle)
    136  1.1  cgd 	int phandle;
    137  1.1  cgd {
    138  1.1  cgd 	int len;
    139  1.1  cgd 
    140  1.1  cgd 	len = OF_getproplen(phandle, "reg");
    141  1.1  cgd 
    142  1.1  cgd 	/* nonexistant or obviously malformed "reg" property */
    143  1.1  cgd 	if (len < 0 || (len % 12) != 0)
    144  1.1  cgd 		return (-1);
    145  1.1  cgd 	return (len / 12);
    146  1.1  cgd }
    147  1.1  cgd 
    148  1.1  cgd int
    149  1.1  cgd ofisa_reg_get(phandle, descp, ndescs)
    150  1.1  cgd 	int phandle;
    151  1.1  cgd 	struct ofisa_reg_desc *descp;
    152  1.1  cgd 	int ndescs;
    153  1.1  cgd {
    154  1.1  cgd 	char *buf, *bp;
    155  1.1  cgd 	int i, allocated, rv;
    156  1.1  cgd 
    157  1.1  cgd 	i = ofisa_reg_count(phandle);
    158  1.1  cgd 	if (i < 0)
    159  1.1  cgd 		return (-1);
    160  1.1  cgd 	ndescs = min(ndescs, i);
    161  1.1  cgd 
    162  1.1  cgd 	i = ndescs * 12;
    163  1.1  cgd 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    164  1.1  cgd 		buf = malloc(i, M_TEMP, M_WAITOK);
    165  1.1  cgd 		allocated = 1;
    166  1.1  cgd 	} else {
    167  1.1  cgd 		buf = alloca(i);
    168  1.1  cgd 		allocated = 0;
    169  1.1  cgd 	}
    170  1.1  cgd 
    171  1.1  cgd 	if (OF_getprop(phandle, "reg", buf, i) != i) {
    172  1.1  cgd 		rv = -1;
    173  1.1  cgd 		goto out;
    174  1.1  cgd 	}
    175  1.1  cgd 
    176  1.1  cgd 	for (i = 0, bp = buf; i < ndescs; i++, bp += 12) {
    177  1.1  cgd 		if (of_decode_int(&bp[0]) & 1)
    178  1.1  cgd 			descp[i].type = OFISA_REG_TYPE_IO;
    179  1.1  cgd 		else
    180  1.1  cgd 			descp[i].type = OFISA_REG_TYPE_MEM;
    181  1.1  cgd 		descp[i].addr = of_decode_int(&bp[4]);
    182  1.1  cgd 		descp[i].len = of_decode_int(&bp[8]);
    183  1.1  cgd 	}
    184  1.1  cgd 	rv = i;		/* number of descriptors processed (== ndescs) */
    185  1.1  cgd 
    186  1.1  cgd out:
    187  1.1  cgd 	if (allocated)
    188  1.1  cgd 		free(buf, M_TEMP);
    189  1.1  cgd 	return (rv);
    190  1.1  cgd }
    191  1.1  cgd 
    192  1.1  cgd void
    193  1.1  cgd ofisa_reg_print(descp, ndescs)
    194  1.1  cgd 	struct ofisa_reg_desc *descp;
    195  1.1  cgd 	int ndescs;
    196  1.1  cgd {
    197  1.1  cgd 	int i;
    198  1.1  cgd 
    199  1.1  cgd 	if (ndescs == 0) {
    200  1.1  cgd 		printf("none");
    201  1.1  cgd 		return;
    202  1.1  cgd 	}
    203  1.1  cgd 
    204  1.1  cgd 	for (i = 0; i < ndescs; i++) {
    205  1.1  cgd 		printf("%s%s 0x%lx/%ld", i ? ", " : "",
    206  1.1  cgd 		    descp[i].type == OFISA_REG_TYPE_IO ? "io" : "mem",
    207  1.1  cgd 		    (long)descp[i].addr, (long)descp[i].len);
    208  1.1  cgd 	}
    209  1.1  cgd }
    210  1.1  cgd 
    211  1.1  cgd int
    212  1.1  cgd ofisa_intr_count(phandle)
    213  1.1  cgd 	int phandle;
    214  1.1  cgd {
    215  1.1  cgd 	int len;
    216  1.1  cgd 
    217  1.1  cgd 	len = OF_getproplen(phandle, "interrupts");
    218  1.1  cgd 
    219  1.1  cgd 	/* nonexistant or obviously malformed "reg" property */
    220  1.1  cgd 	if (len < 0 || (len % 8) != 0)
    221  1.1  cgd 		return (-1);
    222  1.1  cgd 	return (len / 8);
    223  1.1  cgd }
    224  1.1  cgd 
    225  1.1  cgd int
    226  1.1  cgd ofisa_intr_get(phandle, descp, ndescs)
    227  1.1  cgd 	int phandle;
    228  1.1  cgd 	struct ofisa_intr_desc *descp;
    229  1.1  cgd 	int ndescs;
    230  1.1  cgd {
    231  1.1  cgd 	char *buf, *bp;
    232  1.1  cgd 	int i, allocated, rv;
    233  1.1  cgd 
    234  1.1  cgd 	i = ofisa_intr_count(phandle);
    235  1.1  cgd 	if (i < 0)
    236  1.1  cgd 		return (-1);
    237  1.1  cgd 	ndescs = min(ndescs, i);
    238  1.1  cgd 
    239  1.1  cgd 	i = ndescs * 8;
    240  1.1  cgd 	if (i > OFW_MAX_STACK_BUF_SIZE) {
    241  1.1  cgd 		buf = malloc(i, M_TEMP, M_WAITOK);
    242  1.1  cgd 		allocated = 1;
    243  1.1  cgd 	} else {
    244  1.1  cgd 		buf = alloca(i);
    245  1.1  cgd 		allocated = 0;
    246  1.1  cgd 	}
    247  1.1  cgd 
    248  1.1  cgd 	if (OF_getprop(phandle, "interrupts", buf, i) != i) {
    249  1.1  cgd 		rv = -1;
    250  1.1  cgd 		goto out;
    251  1.1  cgd 	}
    252  1.1  cgd 
    253  1.1  cgd 	for (i = 0, bp = buf; i < ndescs; i++, bp += 8) {
    254  1.1  cgd 		descp[i].irq = of_decode_int(&bp[0]);
    255  1.1  cgd 		switch (of_decode_int(&bp[4])) {
    256  1.1  cgd 		case 0:
    257  1.1  cgd 		case 1:
    258  1.1  cgd 			descp[i].share = IST_LEVEL;
    259  1.1  cgd 			break;
    260  1.1  cgd 		case 2:
    261  1.1  cgd 		case 3:
    262  1.1  cgd 			descp[i].share = IST_EDGE;
    263  1.1  cgd 			break;
    264  1.1  cgd #ifdef DIAGNOSTIC
    265  1.1  cgd 		default:
    266  1.1  cgd 			/* Dunno what to do, so fail. */
    267  1.1  cgd 			printf("ofisa_intr_get: unknown intrerrupt type %d\n",
    268  1.1  cgd 			    of_decode_int(&bp[4]));
    269  1.1  cgd 			rv = -1;
    270  1.1  cgd 			goto out;
    271  1.1  cgd #endif
    272  1.1  cgd 		}
    273  1.1  cgd 	}
    274  1.1  cgd 	rv = i;		/* number of descriptors processed (== ndescs) */
    275  1.1  cgd 
    276  1.1  cgd out:
    277  1.1  cgd 	if (allocated)
    278  1.1  cgd 		free(buf, M_TEMP);
    279  1.1  cgd 	return (rv);
    280  1.1  cgd }
    281  1.1  cgd 
    282  1.1  cgd void
    283  1.1  cgd ofisa_intr_print(descp, ndescs)
    284  1.1  cgd 	struct ofisa_intr_desc *descp;
    285  1.1  cgd 	int ndescs;
    286  1.1  cgd {
    287  1.1  cgd 	int i;
    288  1.1  cgd 
    289  1.1  cgd 	if (ndescs == 0) {
    290  1.1  cgd 		printf("none");
    291  1.1  cgd 		return;
    292  1.1  cgd 	}
    293  1.1  cgd 
    294  1.1  cgd 	for (i = 0; i < ndescs; i++) {
    295  1.1  cgd 		printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
    296  1.1  cgd 		    descp[i].share == IST_LEVEL ? "level" : "edge");
    297  1.1  cgd 	}
    298  1.1  cgd }
    299