Home | History | Annotate | Line # | Download | only in nubus
nubus.c revision 1.33
      1  1.33    scottr /*	$NetBSD: nubus.c,v 1.33 1997/03/15 18:33:44 scottr Exp $	*/
      2   1.8       cgd 
      3  1.11    briggs /*
      4  1.29    scottr  * Copyright (c) 1995, 1996 Allen Briggs.  All rights reserved.
      5   1.1    briggs  *
      6   1.1    briggs  * Redistribution and use in source and binary forms, with or without
      7   1.1    briggs  * modification, are permitted provided that the following conditions
      8   1.1    briggs  * are met:
      9   1.1    briggs  * 1. Redistributions of source code must retain the above copyright
     10   1.1    briggs  *    notice, this list of conditions and the following disclaimer.
     11   1.1    briggs  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1    briggs  *    notice, this list of conditions and the following disclaimer in the
     13   1.1    briggs  *    documentation and/or other materials provided with the distribution.
     14   1.1    briggs  * 3. All advertising materials mentioning features or use of this software
     15   1.1    briggs  *    must display the following acknowledgement:
     16  1.11    briggs  *	This product includes software developed by Allen Briggs.
     17  1.11    briggs  * 4. The name of the author may not be used to endorse or promote products
     18  1.11    briggs  *    derived from this software without specific prior written permission.
     19   1.1    briggs  *
     20  1.11    briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.1    briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.1    briggs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.11    briggs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.1    briggs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.1    briggs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.1    briggs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.1    briggs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.11    briggs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.11    briggs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1    briggs  */
     31   1.1    briggs 
     32   1.4    briggs #include <sys/param.h>
     33   1.4    briggs #include <sys/systm.h>
     34  1.10    briggs #include <sys/device.h>
     35  1.29    scottr #include <sys/buf.h>
     36  1.29    scottr #include <sys/conf.h>
     37  1.29    scottr #include <sys/dmap.h>
     38  1.29    scottr 
     39  1.29    scottr #include <vm/vm.h>
     40  1.29    scottr #include <vm/vm_kern.h>
     41  1.29    scottr #include <vm/vm_map.h>
     42  1.10    briggs 
     43  1.19    briggs #include <machine/autoconf.h>
     44  1.30    scottr #include <machine/bus.h>
     45  1.29    scottr #include <machine/vmparam.h>
     46  1.29    scottr #include <machine/param.h>
     47   1.4    briggs #include <machine/cpu.h>
     48  1.29    scottr #include <machine/pte.h>
     49  1.33    scottr #include <machine/viareg.h>
     50   1.1    briggs 
     51  1.12    briggs #include <vm/vm.h>
     52  1.12    briggs 
     53   1.1    briggs #include "nubus.h"
     54   1.1    briggs 
     55  1.23    scottr #ifdef DEBUG
     56  1.12    briggs static int	nubus_debug = 0x01;
     57  1.12    briggs #define NDB_PROBE	0x1
     58  1.12    briggs #define NDB_FOLLOW	0x2
     59  1.12    briggs #define NDB_ARITH	0x4
     60  1.12    briggs #endif
     61  1.12    briggs 
     62  1.28    scottr static int	nubus_print __P((void *, const char *));
     63  1.28    scottr static int	nubus_match __P((struct device *, struct cfdata *, void *));
     64  1.28    scottr static void	nubus_attach __P((struct device *, struct device *, void *));
     65  1.28    scottr int		nubus_video_resource __P((int));
     66  1.11    briggs 
     67  1.11    briggs static int	probe_slot __P((int slot, nubus_slot *fmt));
     68  1.11    briggs static u_long	IncPtr __P((nubus_slot *fmt, u_long base, long amt));
     69  1.11    briggs static u_long	nubus_calc_CRC __P((nubus_slot *fmt));
     70  1.11    briggs static u_char	GetByte __P((nubus_slot *fmt, u_long ptr));
     71  1.17    briggs #ifdef notyet
     72  1.17    briggs /* unused */ static u_short	GetWord __P((nubus_slot *fmt, u_long ptr));
     73  1.17    briggs #endif
     74  1.11    briggs static u_long	GetLong __P((nubus_slot *fmt, u_long ptr));
     75  1.18    scottr 
     76  1.29    scottr static int	nubus_peek __P((vm_offset_t, int));
     77  1.29    scottr static char	*nubus_mapin __P((int, int));
     78  1.29    scottr 
     79  1.16   thorpej struct cfattach nubus_ca = {
     80  1.28    scottr 	sizeof(struct nubus_softc), nubus_match, nubus_attach
     81  1.16   thorpej };
     82  1.16   thorpej 
     83  1.16   thorpej struct cfdriver nubus_cd = {
     84  1.28    scottr 	NULL, "nubus", DV_DULL,
     85  1.11    briggs };
     86   1.1    briggs 
     87  1.19    briggs static int
     88  1.28    scottr nubus_match(parent, cf, aux)
     89  1.19    briggs 	struct device *parent;
     90  1.28    scottr 	struct cfdata *cf;
     91  1.28    scottr 	void *aux;
     92  1.19    briggs {
     93  1.29    scottr 	static int nubus_matched = 0;
     94  1.19    briggs 
     95  1.29    scottr 	/* Allow only one instance. */
     96  1.29    scottr 	if (nubus_matched)
     97  1.29    scottr 		return (0);
     98  1.29    scottr 
     99  1.29    scottr 	nubus_matched = 1;
    100  1.29    scottr 	return (1);
    101  1.19    briggs }
    102  1.19    briggs 
    103  1.11    briggs static void
    104  1.28    scottr nubus_attach(parent, self, aux)
    105  1.28    scottr 	struct device *parent, *self;
    106  1.28    scottr 	void *aux;
    107  1.11    briggs {
    108  1.28    scottr 	struct nubus_attach_args na_args;
    109  1.28    scottr 	nubus_slot fmtblock;
    110  1.28    scottr 	nubus_dir dir;
    111  1.28    scottr 	nubus_dirent dirent;
    112  1.28    scottr 	nubus_type slottype;
    113  1.28    scottr 	int i, rsrcid;
    114   1.1    briggs 
    115  1.26  christos 	printf("\n");
    116   1.1    briggs 
    117  1.19    briggs 	for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
    118  1.28    scottr 		if (probe_slot(i, &fmtblock) <= 0)
    119  1.28    scottr 			continue;
    120  1.28    scottr 
    121  1.28    scottr 		if ((rsrcid = nubus_video_resource(i)) == (-1))
    122  1.28    scottr 			rsrcid = 0x80;
    123  1.28    scottr 
    124  1.28    scottr 		nubus_get_main_dir(&fmtblock, &dir);
    125  1.28    scottr 
    126  1.28    scottr 		if (nubus_find_rsrc(&fmtblock, &dir, rsrcid, &dirent) <= 0)
    127  1.28    scottr 			continue;
    128  1.28    scottr 
    129  1.28    scottr 		nubus_get_dir_from_rsrc(&fmtblock, &dirent, &dir);
    130  1.28    scottr 
    131  1.28    scottr 		if (nubus_find_rsrc(&fmtblock, &dir, NUBUS_RSRC_TYPE,
    132  1.28    scottr 		    &dirent) <= 0)
    133  1.28    scottr 			continue;
    134  1.28    scottr 
    135  1.28    scottr 		if (nubus_get_ind_data(&fmtblock, &dirent,
    136  1.28    scottr 		    (caddr_t) &slottype, sizeof(nubus_type)) <= 0)
    137  1.28    scottr 			continue;
    138  1.28    scottr 
    139  1.28    scottr 		na_args.slot = i;
    140  1.28    scottr 		na_args.rsrcid = rsrcid;
    141  1.28    scottr 		na_args.category = slottype.category;
    142  1.28    scottr 		na_args.type = slottype.type;
    143  1.28    scottr 		na_args.drsw = slottype.drsw;
    144  1.28    scottr 		na_args.drhw = slottype.drhw;
    145  1.28    scottr 		na_args.fmt = &fmtblock;
    146  1.28    scottr 
    147  1.28    scottr 		config_found(self, &na_args, nubus_print);
    148  1.12    briggs 	}
    149  1.33    scottr 
    150  1.33    scottr 	enable_nubus_intr();
    151   1.1    briggs }
    152   1.1    briggs 
    153  1.10    briggs static int
    154  1.28    scottr nubus_print(aux, name)
    155  1.28    scottr 	void *aux;
    156  1.28    scottr 	const char *name;
    157  1.11    briggs {
    158  1.28    scottr 	struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
    159   1.1    briggs 
    160  1.11    briggs 	if (name) {
    161  1.28    scottr 		printf("%s: slot %x: %s ", name, na->fmt->slot,
    162  1.28    scottr 		    nubus_get_card_name(na->fmt));
    163  1.26  christos 		printf("(Vendor: %s, ",
    164  1.28    scottr 		    nubus_get_vendor(na->fmt, NUBUS_RSRC_VEND_ID));
    165  1.28    scottr 		printf("Part: %s)",
    166  1.28    scottr 		    nubus_get_vendor(na->fmt, NUBUS_RSRC_VEND_PART));
    167   1.1    briggs 	}
    168  1.11    briggs 	return (UNCONF);
    169  1.28    scottr }
    170  1.28    scottr 
    171  1.28    scottr int
    172  1.28    scottr nubus_video_resource(slot)
    173  1.28    scottr 	int slot;
    174  1.28    scottr {
    175  1.28    scottr 	extern u_int16_t mac68k_vrsrc_vec[];
    176  1.28    scottr 	int i;
    177  1.28    scottr 
    178  1.28    scottr 	for (i = 0 ; i < 6 ; i++)
    179  1.28    scottr 		if ((mac68k_vrsrc_vec[i] & 0xff) == slot)
    180  1.28    scottr 			return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
    181  1.28    scottr 	return (-1);
    182   1.1    briggs }
    183   1.1    briggs 
    184  1.11    briggs /*
    185  1.11    briggs  * Probe a given nubus slot.  If a card is there and we can get the
    186  1.11    briggs  * format block from it's clutching decl. ROMs, fill the format block
    187  1.11    briggs  * and return non-zero.  If we can't find a card there with a valid
    188  1.11    briggs  * decl. ROM, return 0.
    189  1.11    briggs  *
    190  1.11    briggs  * First, we check to see if we can access the memory at the tail
    191  1.11    briggs  * end of the slot.  If so, then we check for a bytelanes byte.  We
    192  1.11    briggs  * could probably just return a failure status if we bus error on
    193  1.11    briggs  * the first try, but there really is little reason not to go ahead
    194  1.11    briggs  * and check the other three locations in case there's a wierd card
    195  1.11    briggs  * out there.
    196  1.11    briggs  *
    197  1.11    briggs  * Checking for a card involves locating the "bytelanes" byte which
    198  1.11    briggs  * tells us how to interpret the declaration ROM's data.  The format
    199  1.11    briggs  * block is at the top of the card's standard memory space and the
    200  1.11    briggs  * bytelanes byte is at the end of that block.
    201  1.11    briggs  *
    202  1.11    briggs  * After some inspection of the bytelanes byte, it appears that it
    203  1.11    briggs  * takes the form 0xXY where Y is a bitmask of the bytelanes in use
    204  1.11    briggs  * and X is a bitmask of the lanes to ignore.  Hence, (X ^ Y) == 0
    205  1.11    briggs  * and (less obviously), Y will have the upper N bits clear if it is
    206  1.11    briggs  * found N bytes from the last possible location.  Both that and
    207  1.11    briggs  * the exclusive-or check are made.
    208  1.11    briggs  *
    209  1.11    briggs  * If a valid
    210  1.11    briggs  */
    211  1.11    briggs static u_char	nbits[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    212  1.11    briggs static int
    213  1.11    briggs probe_slot(slot, fmt)
    214  1.11    briggs 	int		slot;
    215  1.11    briggs 	nubus_slot	*fmt;
    216   1.1    briggs {
    217  1.11    briggs 	caddr_t		rom_probe;
    218  1.19    briggs 	vm_offset_t	hdr;
    219  1.23    scottr #ifdef DEBUG
    220  1.23    scottr 	vm_offset_t	pa;
    221  1.23    scottr #endif
    222  1.19    briggs 	u_int		data;
    223  1.11    briggs 	int		hdr_size, i;
    224  1.10    briggs 
    225  1.11    briggs 	fmt->bytelanes = 0;
    226  1.11    briggs 	fmt->slot = (u_long)slot;
    227  1.10    briggs 
    228  1.31    scottr 	rom_probe = (caddr_t) (NUBUS_SLOT2PA(fmt->slot) + NBMEMSIZE);
    229  1.10    briggs 
    230  1.12    briggs #ifdef DEBUG
    231  1.12    briggs 	if (nubus_debug & NDB_PROBE) {
    232  1.23    scottr 		pa = pmap_extract(pmap_kernel(), (vm_offset_t) rom_probe - 1);
    233  1.27    scottr 		printf("probing slot %d, first probe at 0x%p (PA 0x%lx).\n",
    234  1.23    scottr 		    slot, rom_probe - 1, pa);
    235  1.12    briggs 	}
    236  1.12    briggs #endif
    237  1.12    briggs 
    238  1.11    briggs 	for (i = 4; i && (fmt->bytelanes == 0); i--) {
    239  1.10    briggs 
    240  1.11    briggs 		rom_probe--;
    241  1.10    briggs 
    242  1.29    scottr 		data = nubus_peek((vm_offset_t) rom_probe, 1);
    243  1.19    briggs 		if (data == -1)
    244  1.11    briggs 			continue;
    245  1.10    briggs 
    246  1.19    briggs 		if (data == 0)
    247  1.11    briggs 			continue;
    248   1.1    briggs 
    249  1.11    briggs 		if (   ((((data & 0xf0) >> 4) ^ (data & 0x0f)) == 0x0f)
    250  1.11    briggs 		    && ((data & 0x0f) < (1 << i)) ) {
    251  1.11    briggs 			fmt->bytelanes = data;
    252  1.11    briggs 			fmt->step = nbits[(data & 0x0f)];
    253  1.11    briggs 		}
    254   1.1    briggs 	}
    255  1.11    briggs #ifdef DEBUG
    256  1.12    briggs 	if (nubus_debug & NDB_PROBE)
    257  1.12    briggs 		if (fmt->bytelanes == 0)
    258  1.26  christos 			printf("bytelanes not found for slot 0x%x.\n", slot);
    259  1.11    briggs #endif
    260  1.11    briggs 
    261  1.11    briggs 	if (fmt->bytelanes == 0)
    262  1.11    briggs 		return 0;
    263  1.11    briggs 
    264  1.11    briggs #ifdef DEBUG
    265  1.12    briggs 	if (nubus_debug & NDB_PROBE)
    266  1.26  christos 		printf("bytelanes of 0x%x found for slot 0x%x.\n",
    267  1.20    briggs 			fmt->bytelanes, slot);
    268  1.11    briggs #endif
    269   1.1    briggs 
    270  1.11    briggs 	hdr_size = 20;
    271  1.10    briggs 
    272  1.11    briggs 	/*
    273  1.11    briggs 	 * Go ahead and attempt to load format header.
    274  1.11    briggs 	 * First, we need to find the first byte beyond memory that
    275  1.11    briggs 	 * would be valid.  This is necessary for NUBUS_ROM_offset()
    276  1.11    briggs 	 * to work.
    277  1.11    briggs 	 */
    278  1.19    briggs 	hdr = (vm_offset_t)
    279  1.31    scottr 		nubus_mapin(NUBUS_SLOT2PA(fmt->slot), NBMEMSIZE);
    280  1.19    briggs 	if (hdr == NULL) {
    281  1.26  christos 		printf("Failed to map %d bytes for NuBUS slot %d probe.  ",
    282  1.22    briggs 			NBMEMSIZE, fmt->slot);
    283  1.26  christos 		printf("Physical slot address %x\n",
    284  1.31    scottr 			(unsigned int) NUBUS_SLOT2PA(fmt->slot));
    285  1.19    briggs 	}
    286  1.19    briggs 	fmt->virtual_base = hdr;
    287  1.19    briggs 	hdr += NBMEMSIZE;
    288  1.19    briggs 
    289  1.11    briggs 	i = 0x10 | (fmt->bytelanes & 0x0f);
    290  1.11    briggs 	while ((i & 1) == 0) {
    291  1.11    briggs 		hdr++;
    292  1.11    briggs 		i >>= 1;
    293  1.11    briggs 	}
    294  1.11    briggs 	fmt->top = hdr;
    295  1.11    briggs 	hdr = IncPtr(fmt, hdr, -hdr_size);
    296  1.11    briggs #ifdef DEBUG
    297  1.12    briggs 	if (nubus_debug & NDB_PROBE)
    298  1.27    scottr 		printf("fmt->top is 0x%lx, that minus 0x%x puts us at 0x%lx.\n",
    299  1.12    briggs 			fmt->top, hdr_size, hdr);
    300  1.11    briggs #if 0
    301  1.11    briggs 	for (i=1 ; i < 8 ; i++) {
    302  1.26  christos 		printf("0x%x - 0x%x = 0x%x, + 0x%x = 0x%x.\n",
    303  1.11    briggs 			hdr, i, IncPtr(fmt, hdr, -i),
    304  1.11    briggs 			     i, IncPtr(fmt, hdr,  i));
    305  1.11    briggs 	}
    306  1.11    briggs #endif
    307  1.11    briggs #endif
    308  1.11    briggs 
    309  1.11    briggs 	fmt->directory_offset = 0xff000000 | GetLong(fmt, hdr);
    310  1.11    briggs 	hdr = IncPtr(fmt, hdr, 4);
    311  1.11    briggs 	fmt->length = GetLong(fmt, hdr);
    312  1.11    briggs 	hdr = IncPtr(fmt, hdr, 4);
    313  1.11    briggs 	fmt->crc = GetLong(fmt, hdr);
    314  1.11    briggs 	hdr = IncPtr(fmt, hdr, 4);
    315  1.11    briggs 	fmt->revision_level = GetByte(fmt, hdr);
    316  1.11    briggs 	hdr = IncPtr(fmt, hdr, 1);
    317  1.11    briggs 	fmt->format = GetByte(fmt, hdr);
    318  1.11    briggs 	hdr = IncPtr(fmt, hdr, 1);
    319  1.11    briggs 	fmt->test_pattern = GetLong(fmt, hdr);
    320  1.11    briggs 
    321  1.23    scottr #ifdef DEBUG
    322  1.12    briggs 	if (nubus_debug & NDB_PROBE) {
    323  1.26  christos 		printf("Directory offset 0x%x\t", fmt->directory_offset);
    324  1.26  christos 		printf("Length 0x%x\t", fmt->length);
    325  1.26  christos 		printf("CRC 0x%x\n", fmt->crc);
    326  1.26  christos 		printf("Revision level 0x%x\t", fmt->revision_level);
    327  1.26  christos 		printf("Format 0x%x\t", fmt->format);
    328  1.26  christos 		printf("Test Pattern 0x%x\n", fmt->test_pattern);
    329  1.12    briggs 	}
    330  1.11    briggs #endif
    331  1.11    briggs 
    332  1.12    briggs 	if ((fmt->directory_offset & 0x00ff0000) == 0) {
    333  1.26  christos 		printf("Invalid looking directory offset (0x%x)!\n",
    334  1.12    briggs 			fmt->directory_offset);
    335  1.12    briggs 		return 0;
    336  1.12    briggs 	}
    337  1.11    briggs 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
    338  1.26  christos 		printf("Nubus--test pattern invalid:\n");
    339  1.26  christos 		printf("       slot 0x%x, bytelanes 0x%x?\n",
    340  1.11    briggs 			fmt->slot, fmt->bytelanes);
    341  1.26  christos 		printf("       read test 0x%x, compare with 0x%x.\n",
    342  1.11    briggs 			fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
    343  1.11    briggs 		return 0;
    344  1.11    briggs 	}
    345  1.11    briggs 
    346  1.11    briggs 	/* Perform CRC */
    347  1.11    briggs 	if (fmt->crc != nubus_calc_CRC(fmt)) {
    348  1.26  christos 		printf("Nubus--crc check failed, slot 0x%x.\n",
    349  1.11    briggs 			fmt->slot);
    350  1.11    briggs 		return 0;
    351   1.1    briggs 	}
    352  1.10    briggs 
    353  1.11    briggs 	return 1;
    354   1.1    briggs }
    355   1.1    briggs 
    356  1.11    briggs /*
    357  1.11    briggs  * Compute byte offset on card, taking into account bytelanes.
    358  1.11    briggs  * Base must be on a valid bytelane for this function to work.
    359  1.11    briggs  * Return the new address.
    360  1.11    briggs  *
    361  1.11    briggs  * XXX -- There has GOT to be a better way to do this.
    362  1.11    briggs  */
    363  1.11    briggs static u_long
    364  1.11    briggs IncPtr(fmt, base, amt)
    365  1.11    briggs 	nubus_slot	*fmt;
    366  1.11    briggs 	u_long		base;
    367  1.11    briggs 	long		amt;
    368  1.11    briggs {
    369  1.11    briggs 	u_char 	b, t;
    370  1.11    briggs 
    371  1.11    briggs 	if (!amt)
    372  1.11    briggs 		return base;
    373  1.11    briggs 
    374  1.11    briggs 	if (amt < 0) {
    375  1.11    briggs 		amt = -amt;
    376  1.11    briggs 		b = fmt->bytelanes;
    377  1.11    briggs 		t = (b << 4);
    378  1.11    briggs 		b <<= (3 - (base & 0x3));
    379  1.11    briggs 		while (amt) {
    380  1.11    briggs 			b <<= 1;
    381  1.11    briggs 			if (b == t)
    382  1.11    briggs 				b = fmt->bytelanes;
    383  1.11    briggs 			if (b & 0x08)
    384  1.11    briggs 				amt--;
    385  1.11    briggs 			base--;
    386  1.11    briggs 		}
    387  1.11    briggs 		return base;
    388  1.11    briggs 	}
    389  1.10    briggs 
    390  1.11    briggs 	t = (fmt->bytelanes & 0xf) | 0x10;
    391  1.11    briggs 	b = t >> (base & 0x3);
    392  1.11    briggs 	while (amt) {
    393  1.11    briggs 		b >>= 1;
    394  1.11    briggs 		if (b == 1)
    395  1.11    briggs 			b = t;
    396  1.11    briggs 		if (b & 1)
    397  1.11    briggs 			amt--;
    398  1.11    briggs 		base++;
    399  1.11    briggs 	}
    400  1.11    briggs 
    401  1.11    briggs 	return base;
    402  1.11    briggs }
    403  1.11    briggs 
    404  1.11    briggs static u_long
    405  1.11    briggs nubus_calc_CRC(fmt)
    406  1.11    briggs 	nubus_slot	*fmt;
    407  1.11    briggs {
    408  1.11    briggs #if 0
    409  1.11    briggs 	u_long	base, ptr, crc_loc, sum;
    410  1.11    briggs 	int	i;
    411  1.11    briggs 
    412  1.11    briggs 	base = fmt->top;
    413  1.11    briggs 	crc_loc = NUBUS_ROM_offset(fmt, base, -12);
    414  1.11    briggs 	ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
    415  1.11    briggs 
    416  1.11    briggs 	sum = 0;
    417  1.11    briggs 	while (ptr < base)
    418  1.11    briggs 		roll #1, sum
    419  1.11    briggs 		if (ptr == crc_loc) {
    420  1.11    briggs 			roll #3, sum
    421  1.11    briggs 			ptr = IncPtr(fmt, ptr, 3);
    422  1.11    briggs 		} else {
    423  1.11    briggs 			sum += GetByte(fmt, ptr);
    424  1.11    briggs 		}
    425  1.11    briggs 		ptr = IncPtr(fmt, ptr, 1);
    426   1.1    briggs 	}
    427  1.10    briggs 
    428  1.11    briggs 	return sum;
    429  1.11    briggs #endif
    430  1.11    briggs 	return fmt->crc;
    431   1.1    briggs }
    432   1.1    briggs 
    433  1.11    briggs static u_char
    434  1.11    briggs GetByte(fmt, ptr)
    435  1.11    briggs 	nubus_slot	*fmt;
    436  1.11    briggs 	u_long		ptr;
    437   1.1    briggs {
    438  1.11    briggs 	return *(caddr_t)ptr;
    439   1.1    briggs }
    440   1.1    briggs 
    441  1.17    briggs #ifdef notyet
    442  1.17    briggs /* Nothing uses this, yet */
    443  1.11    briggs static u_short
    444  1.11    briggs GetWord(fmt, ptr)
    445  1.11    briggs 	nubus_slot	*fmt;
    446  1.11    briggs 	u_long		ptr;
    447   1.1    briggs {
    448  1.11    briggs 	u_short	s;
    449  1.10    briggs 
    450  1.11    briggs 	s = (GetByte(fmt, ptr) << 8);
    451  1.11    briggs 	ptr = IncPtr(fmt, ptr, 1);
    452  1.11    briggs 	s |= GetByte(fmt, ptr);
    453  1.11    briggs 	return s;
    454  1.11    briggs }
    455  1.17    briggs #endif
    456   1.1    briggs 
    457  1.11    briggs static u_long
    458  1.11    briggs GetLong(fmt, ptr)
    459  1.11    briggs 	nubus_slot	*fmt;
    460  1.11    briggs 	u_long		ptr;
    461  1.11    briggs {
    462  1.11    briggs 	register u_long l;
    463  1.11    briggs 	register int	i;
    464  1.10    briggs 
    465  1.11    briggs 	l = 0;
    466  1.11    briggs 	for ( i = 0; i < 4; i++) {
    467  1.11    briggs 		l = (l << 8) | GetByte(fmt, ptr);
    468  1.11    briggs 		ptr = IncPtr(fmt, ptr, 1);
    469  1.11    briggs 	}
    470  1.11    briggs 	return l;
    471  1.11    briggs }
    472  1.10    briggs 
    473  1.11    briggs void
    474  1.11    briggs nubus_get_main_dir(slot, dir_return)
    475  1.11    briggs 	nubus_slot	*slot;
    476  1.11    briggs 	nubus_dir	*dir_return;
    477  1.11    briggs {
    478  1.23    scottr #ifdef DEBUG
    479  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    480  1.26  christos 		printf("nubus_get_main_dir(0x%x, 0x%x)\n",
    481  1.12    briggs 			(u_int) slot, (u_int) dir_return);
    482  1.12    briggs #endif
    483  1.11    briggs 	dir_return->dirbase = IncPtr(slot, slot->top,
    484  1.11    briggs 					slot->directory_offset - 20);
    485  1.11    briggs 	dir_return->curr_ent = dir_return->dirbase;
    486   1.1    briggs }
    487   1.1    briggs 
    488  1.11    briggs int
    489  1.11    briggs nubus_find_rsrc(slot, dir, rsrcid, dirent_return)
    490  1.11    briggs 	nubus_slot	*slot;
    491  1.11    briggs 	nubus_dir	*dir;
    492  1.11    briggs 	u_int8_t	rsrcid;
    493  1.11    briggs 	nubus_dirent	*dirent_return;
    494   1.1    briggs {
    495  1.11    briggs 	u_long		entry;
    496  1.11    briggs 	u_char		byte;
    497  1.10    briggs 
    498  1.23    scottr #ifdef DEBUG
    499  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    500  1.26  christos 		printf("nubus_find_rsrc(0x%x, 0x%x, 0x%x, 0x%x)\n",
    501  1.12    briggs 			(u_int) slot, (u_int) dir, (u_int) rsrcid,
    502  1.12    briggs 			(u_int) dirent_return);
    503  1.11    briggs #endif
    504  1.12    briggs 	if (slot->test_pattern != NUBUS_ROM_TEST_PATTERN)
    505  1.12    briggs 		return -1;
    506  1.12    briggs 
    507  1.12    briggs 	entry = dir->curr_ent;
    508  1.11    briggs 	do {
    509  1.11    briggs 		byte = GetByte(slot, entry);
    510  1.23    scottr #ifdef DEBUG
    511  1.12    briggs 		if (nubus_debug & NDB_FOLLOW)
    512  1.26  christos 			printf("\tFound rsrc 0x%x.\n", byte);
    513  1.11    briggs #endif
    514  1.11    briggs 		if (byte == rsrcid) {
    515  1.11    briggs 			dirent_return->myloc = entry;
    516  1.11    briggs 			dirent_return->rsrc_id = rsrcid;
    517  1.11    briggs 			entry = GetLong(slot, entry);
    518  1.11    briggs 			dirent_return->offset = (entry & 0x00ffffff);
    519  1.11    briggs 			return 1;
    520  1.11    briggs 		}
    521  1.11    briggs 		if (byte == 0xff) {
    522  1.11    briggs 			entry = dir->dirbase;
    523  1.11    briggs 		} else {
    524  1.11    briggs 			entry = IncPtr(slot, entry, 4);
    525  1.11    briggs 		}
    526  1.11    briggs 	} while (entry != (u_long) dir->curr_ent);
    527  1.11    briggs 	return 0;
    528   1.1    briggs }
    529   1.1    briggs 
    530  1.11    briggs void
    531  1.11    briggs nubus_get_dir_from_rsrc(slot, dirent, dir_return)
    532  1.11    briggs 	nubus_slot	*slot;
    533  1.11    briggs 	nubus_dirent	*dirent;
    534  1.11    briggs 	nubus_dir	*dir_return;
    535   1.1    briggs {
    536  1.11    briggs 	u_long	loc;
    537  1.10    briggs 
    538  1.23    scottr #ifdef DEBUG
    539  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    540  1.26  christos 		printf("nubus_get_dir_from_rsrc(0x%x, 0x%x, 0x%x).\n",
    541  1.12    briggs 			(u_int) slot, (u_int) dirent, (u_int) dir_return);
    542  1.12    briggs #endif
    543  1.11    briggs 	if ((loc = dirent->offset) & 0x800000) {
    544  1.11    briggs 		loc |= 0xff000000;
    545   1.1    briggs 	}
    546  1.11    briggs 	dir_return->dirbase = IncPtr(slot, dirent->myloc, loc);
    547  1.11    briggs 	dir_return->curr_ent = dir_return->dirbase;
    548   1.1    briggs }
    549   1.1    briggs 
    550  1.11    briggs int
    551  1.11    briggs nubus_get_ind_data(slot, dirent, data_return, nbytes)
    552  1.11    briggs 	nubus_slot *slot;
    553  1.11    briggs 	nubus_dirent *dirent;
    554  1.11    briggs 	caddr_t data_return;
    555  1.11    briggs 	int nbytes;
    556  1.11    briggs {
    557  1.11    briggs 	u_long	loc;
    558   1.1    briggs 
    559  1.23    scottr #ifdef DEBUG
    560  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    561  1.26  christos 		printf("nubus_get_ind_data(0x%x, 0x%x, 0x%x, %d).\n",
    562  1.12    briggs 			(u_int) slot, (u_int) dirent, (u_int) data_return,
    563  1.12    briggs 			nbytes);
    564  1.12    briggs #endif
    565  1.11    briggs 	if ((loc = dirent->offset) & 0x800000) {
    566  1.11    briggs 		loc |= 0xff000000;
    567  1.11    briggs 	}
    568  1.11    briggs 	loc = IncPtr(slot, dirent->myloc, loc);
    569   1.1    briggs 
    570  1.11    briggs 	while (nbytes--) {
    571  1.11    briggs 		*data_return++ = GetByte(slot, loc);
    572  1.11    briggs 		loc = IncPtr(slot, loc, 1);
    573   1.1    briggs 	}
    574  1.11    briggs 	return 1;
    575   1.1    briggs }
    576   1.1    briggs 
    577  1.11    briggs int
    578  1.11    briggs nubus_get_c_string(slot, dirent, data_return, max_bytes)
    579  1.11    briggs 	nubus_slot *slot;
    580  1.11    briggs 	nubus_dirent *dirent;
    581  1.11    briggs 	caddr_t data_return;
    582  1.11    briggs 	int max_bytes;
    583   1.1    briggs {
    584  1.11    briggs 	u_long	loc;
    585  1.10    briggs 
    586  1.23    scottr #ifdef DEBUG
    587  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    588  1.26  christos 		printf("nubus_get_c_string(0x%x, 0x%x, 0x%x, %d).\n",
    589  1.12    briggs 			(u_int) slot, (u_int) dirent, (u_int) data_return,
    590  1.12    briggs 			max_bytes);
    591  1.12    briggs #endif
    592  1.11    briggs 	if ((loc = dirent->offset) & 0x800000) {
    593  1.11    briggs 		loc |= 0xff000000;
    594  1.11    briggs 	}
    595  1.11    briggs 	loc = IncPtr(slot, dirent->myloc, loc);
    596  1.10    briggs 
    597  1.11    briggs 	*data_return = '\0';
    598  1.11    briggs 	while (max_bytes--) {
    599  1.11    briggs 		if ((*data_return++ = GetByte(slot, loc)) == 0)
    600  1.11    briggs 			return 1;
    601  1.11    briggs 		loc = IncPtr(slot, loc, 1);
    602  1.11    briggs 	}
    603  1.11    briggs 	return 0;
    604   1.1    briggs }
    605   1.2    briggs 
    606  1.11    briggs static char	*huh = "???";
    607   1.3    briggs 
    608  1.11    briggs char *
    609  1.11    briggs nubus_get_vendor(slot, rsrc)
    610  1.11    briggs 	nubus_slot	*slot;
    611  1.11    briggs 	int		rsrc;
    612   1.3    briggs {
    613  1.11    briggs static	char		str_ret[64];
    614  1.11    briggs 	nubus_dir	dir;
    615  1.11    briggs 	nubus_dirent	ent;
    616   1.3    briggs 
    617  1.23    scottr #ifdef DEBUG
    618  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    619  1.26  christos 		printf("nubus_get_vendor(0x%x, 0x%x).\n", (u_int) slot, rsrc);
    620  1.12    briggs #endif
    621  1.11    briggs 	nubus_get_main_dir(slot, &dir);
    622  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
    623  1.11    briggs 		return huh;
    624  1.11    briggs 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    625   1.2    briggs 
    626  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_VENDORINFO, &ent) <= 0)
    627  1.11    briggs 		return huh;
    628  1.11    briggs 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    629  1.10    briggs 
    630  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, rsrc, &ent) <= 0)
    631  1.11    briggs 		return huh;
    632  1.10    briggs 
    633  1.11    briggs 	nubus_get_c_string(slot, &ent, str_ret, 64);
    634   1.2    briggs 
    635  1.11    briggs 	return str_ret;
    636   1.2    briggs }
    637   1.2    briggs 
    638  1.11    briggs char *
    639  1.11    briggs nubus_get_card_name(slot)
    640  1.11    briggs 	nubus_slot	*slot;
    641   1.2    briggs {
    642  1.11    briggs static	char		name_ret[64];
    643  1.11    briggs 	nubus_dir	dir;
    644  1.11    briggs 	nubus_dirent	ent;
    645   1.2    briggs 
    646  1.23    scottr #ifdef DEBUG
    647  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    648  1.26  christos 		printf("nubus_get_card_name(0x%lx).\n", (u_long) slot);
    649  1.12    briggs #endif
    650  1.11    briggs 	nubus_get_main_dir(slot, &dir);
    651   1.2    briggs 
    652  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
    653  1.11    briggs 		return huh;
    654   1.2    briggs 
    655  1.11    briggs 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    656   1.2    briggs 
    657  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
    658  1.11    briggs 		return huh;
    659   1.2    briggs 
    660  1.11    briggs 	nubus_get_c_string(slot, &ent, name_ret, 64);
    661   1.2    briggs 
    662  1.11    briggs 	return name_ret;
    663   1.2    briggs }
    664  1.29    scottr 
    665  1.29    scottr /*
    666  1.29    scottr  * bus_*() functions adapted from sun3 generic "bus" support
    667  1.29    scottr  * by Allen Briggs.
    668  1.29    scottr  */
    669  1.29    scottr 
    670  1.29    scottr vm_offset_t tmp_vpages[1];
    671  1.29    scottr 
    672  1.29    scottr /*
    673  1.29    scottr  * Read addr with size len (1,2,4) into val.
    674  1.29    scottr  * If this generates a bus error, return -1
    675  1.29    scottr  *
    676  1.29    scottr  *	Create a temporary mapping,
    677  1.29    scottr  *	Try the access using peek_*
    678  1.29    scottr  *	Clean up temp. mapping
    679  1.29    scottr  */
    680  1.29    scottr static int
    681  1.29    scottr nubus_peek(paddr, sz)
    682  1.29    scottr 	vm_offset_t paddr;
    683  1.29    scottr 	int sz;
    684  1.29    scottr {
    685  1.29    scottr 	int off, pte, rv;
    686  1.29    scottr 	vm_offset_t pgva;
    687  1.29    scottr 	caddr_t va;
    688  1.29    scottr 
    689  1.29    scottr 	off = paddr & PGOFSET;
    690  1.29    scottr 	paddr -= off;
    691  1.29    scottr 	pte = (paddr & PG_FRAME) | (PG_V | PG_W | PG_CI);
    692  1.29    scottr 
    693  1.29    scottr 	pgva = tmp_vpages[0];
    694  1.29    scottr 	va = (caddr_t)pgva + off;
    695  1.29    scottr 
    696  1.29    scottr 	mac68k_set_pte(pgva, pte);
    697  1.29    scottr 	TBIS(pgva);
    698  1.29    scottr 
    699  1.29    scottr 	/*
    700  1.29    scottr 	 * OK, try the access using one of the assembly routines
    701  1.29    scottr 	 * that will set pcb_onfault and catch any bus errors.
    702  1.29    scottr 	 */
    703  1.29    scottr 	rv = -1;
    704  1.29    scottr 	switch (sz) {
    705  1.29    scottr 	case 1:
    706  1.29    scottr 		if (!badbaddr(va))
    707  1.29    scottr 			rv = *((u_char *) va);
    708  1.29    scottr 		break;
    709  1.29    scottr 	case 2:
    710  1.29    scottr 		if (!badwaddr(va))
    711  1.29    scottr 			rv = *((u_int16_t *) va);
    712  1.29    scottr 		break;
    713  1.29    scottr 	case 4:
    714  1.29    scottr 		if (!badladdr(va))
    715  1.29    scottr 			rv = *((u_int32_t *) va);
    716  1.29    scottr 		break;
    717  1.29    scottr 	default:
    718  1.29    scottr 		printf("bus_peek: invalid size=%d\n", sz);
    719  1.29    scottr 		rv = -1;
    720  1.29    scottr 	}
    721  1.29    scottr 
    722  1.29    scottr 	mac68k_set_pte(pgva, PG_NV);
    723  1.29    scottr 	TBIS(pgva);
    724  1.29    scottr 
    725  1.29    scottr 	return rv;
    726  1.29    scottr }
    727  1.29    scottr 
    728  1.29    scottr static char *
    729  1.29    scottr nubus_mapin(paddr, sz)
    730  1.29    scottr 	int paddr, sz;
    731  1.29    scottr {
    732  1.29    scottr 	int off, pa, pmt=0;
    733  1.29    scottr 	vm_offset_t va, retval;
    734  1.29    scottr 
    735  1.29    scottr 	off = paddr & PGOFSET;
    736  1.29    scottr 	pa = paddr - off;
    737  1.29    scottr 	sz += off;
    738  1.29    scottr 	sz = mac68k_round_page(sz);
    739  1.29    scottr 
    740  1.29    scottr 	/* Get some kernel virtual address space. */
    741  1.29    scottr 	va = kmem_alloc_wait(kernel_map, sz);
    742  1.29    scottr 	if (va == 0)
    743  1.29    scottr 		panic("bus_mapin");
    744  1.29    scottr 	retval = va + off;
    745  1.29    scottr 
    746  1.29    scottr 	/* Map it to the specified bus. */
    747  1.29    scottr #if 0	/* XXX */
    748  1.29    scottr 	/* This has a problem with wrap-around... */
    749  1.29    scottr 	pmap_map((int)va, pa | pmt, pa + sz, VM_PROT_ALL);
    750  1.29    scottr #else
    751  1.29    scottr 	do {
    752  1.29    scottr 		pmap_enter(pmap_kernel(), va, pa | pmt, VM_PROT_ALL, FALSE);
    753  1.29    scottr 		va += NBPG;
    754  1.29    scottr 		pa += NBPG;
    755  1.29    scottr 	} while ((sz -= NBPG) > 0);
    756  1.29    scottr #endif
    757  1.29    scottr 
    758  1.29    scottr 	return ((char*)retval);
    759  1.29    scottr }
    760