nubus.c revision 1.34       1  1.34    briggs /*	$NetBSD: nubus.c,v 1.34 1997/04/10 03:28:54 briggs 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.34    briggs 	u_long entry;
    114  1.28    scottr 	int i, rsrcid;
    115   1.1    briggs 
    116  1.26  christos 	printf("\n");
    117   1.1    briggs 
    118  1.19    briggs 	for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
    119  1.28    scottr 		if (probe_slot(i, &fmtblock) <= 0)
    120  1.28    scottr 			continue;
    121  1.28    scottr 
    122  1.34    briggs 		rsrcid = 0x80;
    123  1.28    scottr 
    124  1.28    scottr 		nubus_get_main_dir(&fmtblock, &dir);
    125  1.28    scottr 
    126  1.34    briggs 		/*
    127  1.34    briggs 		 * Get the resource for the first function on the card.
    128  1.34    briggs 		 * This is assumed to be at resource ID 0x80.  If we can
    129  1.34    briggs 		 * not find this entry (as we can not on some video cards),
    130  1.34    briggs 		 * check to see if we can get a different ID from the list
    131  1.34    briggs 		 * of video resources given to us by the booter.  If that
    132  1.34    briggs 		 * doesn't work either, take the first resource following
    133  1.34    briggs 		 * the board resource.
    134  1.34    briggs 		 */
    135  1.34    briggs 		if (nubus_find_rsrc(&fmtblock, &dir, rsrcid, &dirent) <= 0) {
    136  1.34    briggs 			if ((rsrcid = nubus_video_resource(i)) == -1) {
    137  1.34    briggs 				/*
    138  1.34    briggs 				 * Since nubus_find_rsrc failed, the directory
    139  1.34    briggs 				 * is back at its base.
    140  1.34    briggs 				 */
    141  1.34    briggs 				entry = dir.curr_ent;
    142  1.34    briggs 
    143  1.34    briggs 				/*
    144  1.34    briggs 				 * All nubus cards should have a board
    145  1.34    briggs 				 * resource, but be sure that's what it
    146  1.34    briggs 				 * is before we skip it.
    147  1.34    briggs 				 */
    148  1.34    briggs 				rsrcid = GetByte(&fmtblock, entry);
    149  1.34    briggs 				if (rsrcid == 0x1)
    150  1.34    briggs 					entry =
    151  1.34    briggs 					    IncPtr(&fmtblock, dir.curr_ent, 4);
    152  1.34    briggs 
    153  1.34    briggs 				rsrcid = GetByte(&fmtblock, entry);
    154  1.34    briggs #ifdef DEBUG
    155  1.34    briggs 				if (nubus_debug & NDB_FOLLOW)
    156  1.34    briggs 					printf("\tUsing rsrc 0x%x.\n", rsrcid);
    157  1.34    briggs #endif
    158  1.34    briggs 				if (rsrcid == 0xff)
    159  1.34    briggs 					continue;	/* end of chain */
    160  1.34    briggs 			}
    161  1.34    briggs 			/*
    162  1.34    briggs 			 * Try to find the resource passed by the booter
    163  1.34    briggs 			 * or the one we just tracked down.
    164  1.34    briggs 			 */
    165  1.34    briggs 			if (nubus_find_rsrc(&fmtblock, &dir,
    166  1.34    briggs 					    rsrcid, &dirent) <= 0) {
    167  1.34    briggs 				continue;
    168  1.34    briggs 			}
    169  1.34    briggs 		}
    170  1.28    scottr 
    171  1.28    scottr 		nubus_get_dir_from_rsrc(&fmtblock, &dirent, &dir);
    172  1.28    scottr 
    173  1.28    scottr 		if (nubus_find_rsrc(&fmtblock, &dir, NUBUS_RSRC_TYPE,
    174  1.28    scottr 		    &dirent) <= 0)
    175  1.28    scottr 			continue;
    176  1.28    scottr 
    177  1.28    scottr 		if (nubus_get_ind_data(&fmtblock, &dirent,
    178  1.28    scottr 		    (caddr_t) &slottype, sizeof(nubus_type)) <= 0)
    179  1.28    scottr 			continue;
    180  1.34    briggs 
    181  1.34    briggs 		/*
    182  1.34    briggs 		 * If this is a display card, try to pull out the correct
    183  1.34    briggs 		 * display mode as passed by the booter.
    184  1.34    briggs 		 */
    185  1.34    briggs 		if (slottype.category == NUBUS_CATEGORY_DISPLAY) {
    186  1.34    briggs 			int	r;
    187  1.34    briggs 
    188  1.34    briggs 			if ((r = nubus_video_resource(i)) != -1) {
    189  1.34    briggs 
    190  1.34    briggs 				nubus_get_main_dir(&fmtblock, &dir);
    191  1.34    briggs 
    192  1.34    briggs 				if (nubus_find_rsrc(&fmtblock, &dir,
    193  1.34    briggs 						    r, &dirent) <= 0)
    194  1.34    briggs 					continue;
    195  1.34    briggs 
    196  1.34    briggs 				nubus_get_dir_from_rsrc(&fmtblock,
    197  1.34    briggs 							&dirent, &dir);
    198  1.34    briggs 
    199  1.34    briggs 				if (nubus_find_rsrc(&fmtblock, &dir,
    200  1.34    briggs 						NUBUS_RSRC_TYPE, &dirent) <= 0)
    201  1.34    briggs 					continue;
    202  1.34    briggs 
    203  1.34    briggs 				if (nubus_get_ind_data(&fmtblock, &dirent,
    204  1.34    briggs 						(caddr_t) &slottype,
    205  1.34    briggs 						sizeof(nubus_type)) <= 0)
    206  1.34    briggs 					continue;
    207  1.34    briggs 			}
    208  1.34    briggs 		}
    209  1.28    scottr 
    210  1.28    scottr 		na_args.slot = i;
    211  1.28    scottr 		na_args.rsrcid = rsrcid;
    212  1.28    scottr 		na_args.category = slottype.category;
    213  1.28    scottr 		na_args.type = slottype.type;
    214  1.28    scottr 		na_args.drsw = slottype.drsw;
    215  1.28    scottr 		na_args.drhw = slottype.drhw;
    216  1.28    scottr 		na_args.fmt = &fmtblock;
    217  1.28    scottr 
    218  1.28    scottr 		config_found(self, &na_args, nubus_print);
    219  1.12    briggs 	}
    220  1.33    scottr 
    221  1.33    scottr 	enable_nubus_intr();
    222   1.1    briggs }
    223   1.1    briggs 
    224  1.10    briggs static int
    225  1.28    scottr nubus_print(aux, name)
    226  1.28    scottr 	void *aux;
    227  1.28    scottr 	const char *name;
    228  1.11    briggs {
    229  1.28    scottr 	struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
    230   1.1    briggs 
    231  1.11    briggs 	if (name) {
    232  1.28    scottr 		printf("%s: slot %x: %s ", name, na->fmt->slot,
    233  1.28    scottr 		    nubus_get_card_name(na->fmt));
    234  1.26  christos 		printf("(Vendor: %s, ",
    235  1.28    scottr 		    nubus_get_vendor(na->fmt, NUBUS_RSRC_VEND_ID));
    236  1.28    scottr 		printf("Part: %s)",
    237  1.28    scottr 		    nubus_get_vendor(na->fmt, NUBUS_RSRC_VEND_PART));
    238   1.1    briggs 	}
    239  1.11    briggs 	return (UNCONF);
    240  1.28    scottr }
    241  1.28    scottr 
    242  1.28    scottr int
    243  1.28    scottr nubus_video_resource(slot)
    244  1.28    scottr 	int slot;
    245  1.28    scottr {
    246  1.28    scottr 	extern u_int16_t mac68k_vrsrc_vec[];
    247  1.28    scottr 	int i;
    248  1.28    scottr 
    249  1.28    scottr 	for (i = 0 ; i < 6 ; i++)
    250  1.28    scottr 		if ((mac68k_vrsrc_vec[i] & 0xff) == slot)
    251  1.28    scottr 			return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
    252  1.28    scottr 	return (-1);
    253   1.1    briggs }
    254   1.1    briggs 
    255  1.11    briggs /*
    256  1.11    briggs  * Probe a given nubus slot.  If a card is there and we can get the
    257  1.11    briggs  * format block from it's clutching decl. ROMs, fill the format block
    258  1.11    briggs  * and return non-zero.  If we can't find a card there with a valid
    259  1.11    briggs  * decl. ROM, return 0.
    260  1.11    briggs  *
    261  1.11    briggs  * First, we check to see if we can access the memory at the tail
    262  1.11    briggs  * end of the slot.  If so, then we check for a bytelanes byte.  We
    263  1.11    briggs  * could probably just return a failure status if we bus error on
    264  1.11    briggs  * the first try, but there really is little reason not to go ahead
    265  1.11    briggs  * and check the other three locations in case there's a wierd card
    266  1.11    briggs  * out there.
    267  1.11    briggs  *
    268  1.11    briggs  * Checking for a card involves locating the "bytelanes" byte which
    269  1.11    briggs  * tells us how to interpret the declaration ROM's data.  The format
    270  1.11    briggs  * block is at the top of the card's standard memory space and the
    271  1.11    briggs  * bytelanes byte is at the end of that block.
    272  1.11    briggs  *
    273  1.11    briggs  * After some inspection of the bytelanes byte, it appears that it
    274  1.11    briggs  * takes the form 0xXY where Y is a bitmask of the bytelanes in use
    275  1.11    briggs  * and X is a bitmask of the lanes to ignore.  Hence, (X ^ Y) == 0
    276  1.11    briggs  * and (less obviously), Y will have the upper N bits clear if it is
    277  1.11    briggs  * found N bytes from the last possible location.  Both that and
    278  1.11    briggs  * the exclusive-or check are made.
    279  1.11    briggs  *
    280  1.11    briggs  * If a valid
    281  1.11    briggs  */
    282  1.11    briggs static u_char	nbits[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    283  1.11    briggs static int
    284  1.11    briggs probe_slot(slot, fmt)
    285  1.11    briggs 	int		slot;
    286  1.11    briggs 	nubus_slot	*fmt;
    287   1.1    briggs {
    288  1.11    briggs 	caddr_t		rom_probe;
    289  1.19    briggs 	vm_offset_t	hdr;
    290  1.23    scottr #ifdef DEBUG
    291  1.23    scottr 	vm_offset_t	pa;
    292  1.23    scottr #endif
    293  1.19    briggs 	u_int		data;
    294  1.11    briggs 	int		hdr_size, i;
    295  1.10    briggs 
    296  1.11    briggs 	fmt->bytelanes = 0;
    297  1.11    briggs 	fmt->slot = (u_long)slot;
    298  1.10    briggs 
    299  1.31    scottr 	rom_probe = (caddr_t) (NUBUS_SLOT2PA(fmt->slot) + NBMEMSIZE);
    300  1.10    briggs 
    301  1.12    briggs #ifdef DEBUG
    302  1.12    briggs 	if (nubus_debug & NDB_PROBE) {
    303  1.23    scottr 		pa = pmap_extract(pmap_kernel(), (vm_offset_t) rom_probe - 1);
    304  1.27    scottr 		printf("probing slot %d, first probe at 0x%p (PA 0x%lx).\n",
    305  1.23    scottr 		    slot, rom_probe - 1, pa);
    306  1.12    briggs 	}
    307  1.12    briggs #endif
    308  1.12    briggs 
    309  1.11    briggs 	for (i = 4; i && (fmt->bytelanes == 0); i--) {
    310  1.10    briggs 
    311  1.11    briggs 		rom_probe--;
    312  1.10    briggs 
    313  1.29    scottr 		data = nubus_peek((vm_offset_t) rom_probe, 1);
    314  1.19    briggs 		if (data == -1)
    315  1.11    briggs 			continue;
    316  1.10    briggs 
    317  1.19    briggs 		if (data == 0)
    318  1.11    briggs 			continue;
    319   1.1    briggs 
    320  1.11    briggs 		if (   ((((data & 0xf0) >> 4) ^ (data & 0x0f)) == 0x0f)
    321  1.11    briggs 		    && ((data & 0x0f) < (1 << i)) ) {
    322  1.11    briggs 			fmt->bytelanes = data;
    323  1.11    briggs 			fmt->step = nbits[(data & 0x0f)];
    324  1.11    briggs 		}
    325   1.1    briggs 	}
    326  1.11    briggs #ifdef DEBUG
    327  1.12    briggs 	if (nubus_debug & NDB_PROBE)
    328  1.12    briggs 		if (fmt->bytelanes == 0)
    329  1.26  christos 			printf("bytelanes not found for slot 0x%x.\n", slot);
    330  1.11    briggs #endif
    331  1.11    briggs 
    332  1.11    briggs 	if (fmt->bytelanes == 0)
    333  1.11    briggs 		return 0;
    334  1.11    briggs 
    335  1.11    briggs #ifdef DEBUG
    336  1.12    briggs 	if (nubus_debug & NDB_PROBE)
    337  1.26  christos 		printf("bytelanes of 0x%x found for slot 0x%x.\n",
    338  1.20    briggs 			fmt->bytelanes, slot);
    339  1.11    briggs #endif
    340   1.1    briggs 
    341  1.11    briggs 	hdr_size = 20;
    342  1.10    briggs 
    343  1.11    briggs 	/*
    344  1.11    briggs 	 * Go ahead and attempt to load format header.
    345  1.11    briggs 	 * First, we need to find the first byte beyond memory that
    346  1.11    briggs 	 * would be valid.  This is necessary for NUBUS_ROM_offset()
    347  1.11    briggs 	 * to work.
    348  1.11    briggs 	 */
    349  1.19    briggs 	hdr = (vm_offset_t)
    350  1.31    scottr 		nubus_mapin(NUBUS_SLOT2PA(fmt->slot), NBMEMSIZE);
    351  1.19    briggs 	if (hdr == NULL) {
    352  1.26  christos 		printf("Failed to map %d bytes for NuBUS slot %d probe.  ",
    353  1.22    briggs 			NBMEMSIZE, fmt->slot);
    354  1.26  christos 		printf("Physical slot address %x\n",
    355  1.31    scottr 			(unsigned int) NUBUS_SLOT2PA(fmt->slot));
    356  1.19    briggs 	}
    357  1.19    briggs 	fmt->virtual_base = hdr;
    358  1.19    briggs 	hdr += NBMEMSIZE;
    359  1.19    briggs 
    360  1.11    briggs 	i = 0x10 | (fmt->bytelanes & 0x0f);
    361  1.11    briggs 	while ((i & 1) == 0) {
    362  1.11    briggs 		hdr++;
    363  1.11    briggs 		i >>= 1;
    364  1.11    briggs 	}
    365  1.11    briggs 	fmt->top = hdr;
    366  1.11    briggs 	hdr = IncPtr(fmt, hdr, -hdr_size);
    367  1.11    briggs #ifdef DEBUG
    368  1.12    briggs 	if (nubus_debug & NDB_PROBE)
    369  1.27    scottr 		printf("fmt->top is 0x%lx, that minus 0x%x puts us at 0x%lx.\n",
    370  1.12    briggs 			fmt->top, hdr_size, hdr);
    371  1.11    briggs #if 0
    372  1.11    briggs 	for (i=1 ; i < 8 ; i++) {
    373  1.26  christos 		printf("0x%x - 0x%x = 0x%x, + 0x%x = 0x%x.\n",
    374  1.11    briggs 			hdr, i, IncPtr(fmt, hdr, -i),
    375  1.11    briggs 			     i, IncPtr(fmt, hdr,  i));
    376  1.11    briggs 	}
    377  1.11    briggs #endif
    378  1.11    briggs #endif
    379  1.11    briggs 
    380  1.11    briggs 	fmt->directory_offset = 0xff000000 | GetLong(fmt, hdr);
    381  1.11    briggs 	hdr = IncPtr(fmt, hdr, 4);
    382  1.11    briggs 	fmt->length = GetLong(fmt, hdr);
    383  1.11    briggs 	hdr = IncPtr(fmt, hdr, 4);
    384  1.11    briggs 	fmt->crc = GetLong(fmt, hdr);
    385  1.11    briggs 	hdr = IncPtr(fmt, hdr, 4);
    386  1.11    briggs 	fmt->revision_level = GetByte(fmt, hdr);
    387  1.11    briggs 	hdr = IncPtr(fmt, hdr, 1);
    388  1.11    briggs 	fmt->format = GetByte(fmt, hdr);
    389  1.11    briggs 	hdr = IncPtr(fmt, hdr, 1);
    390  1.11    briggs 	fmt->test_pattern = GetLong(fmt, hdr);
    391  1.11    briggs 
    392  1.23    scottr #ifdef DEBUG
    393  1.12    briggs 	if (nubus_debug & NDB_PROBE) {
    394  1.26  christos 		printf("Directory offset 0x%x\t", fmt->directory_offset);
    395  1.26  christos 		printf("Length 0x%x\t", fmt->length);
    396  1.26  christos 		printf("CRC 0x%x\n", fmt->crc);
    397  1.26  christos 		printf("Revision level 0x%x\t", fmt->revision_level);
    398  1.26  christos 		printf("Format 0x%x\t", fmt->format);
    399  1.26  christos 		printf("Test Pattern 0x%x\n", fmt->test_pattern);
    400  1.12    briggs 	}
    401  1.11    briggs #endif
    402  1.11    briggs 
    403  1.12    briggs 	if ((fmt->directory_offset & 0x00ff0000) == 0) {
    404  1.26  christos 		printf("Invalid looking directory offset (0x%x)!\n",
    405  1.12    briggs 			fmt->directory_offset);
    406  1.12    briggs 		return 0;
    407  1.12    briggs 	}
    408  1.11    briggs 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
    409  1.26  christos 		printf("Nubus--test pattern invalid:\n");
    410  1.26  christos 		printf("       slot 0x%x, bytelanes 0x%x?\n",
    411  1.11    briggs 			fmt->slot, fmt->bytelanes);
    412  1.26  christos 		printf("       read test 0x%x, compare with 0x%x.\n",
    413  1.11    briggs 			fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
    414  1.11    briggs 		return 0;
    415  1.11    briggs 	}
    416  1.11    briggs 
    417  1.11    briggs 	/* Perform CRC */
    418  1.11    briggs 	if (fmt->crc != nubus_calc_CRC(fmt)) {
    419  1.26  christos 		printf("Nubus--crc check failed, slot 0x%x.\n",
    420  1.11    briggs 			fmt->slot);
    421  1.11    briggs 		return 0;
    422   1.1    briggs 	}
    423  1.10    briggs 
    424  1.11    briggs 	return 1;
    425   1.1    briggs }
    426   1.1    briggs 
    427  1.11    briggs /*
    428  1.11    briggs  * Compute byte offset on card, taking into account bytelanes.
    429  1.11    briggs  * Base must be on a valid bytelane for this function to work.
    430  1.11    briggs  * Return the new address.
    431  1.11    briggs  *
    432  1.11    briggs  * XXX -- There has GOT to be a better way to do this.
    433  1.11    briggs  */
    434  1.11    briggs static u_long
    435  1.11    briggs IncPtr(fmt, base, amt)
    436  1.11    briggs 	nubus_slot	*fmt;
    437  1.11    briggs 	u_long		base;
    438  1.11    briggs 	long		amt;
    439  1.11    briggs {
    440  1.11    briggs 	u_char 	b, t;
    441  1.11    briggs 
    442  1.11    briggs 	if (!amt)
    443  1.11    briggs 		return base;
    444  1.11    briggs 
    445  1.11    briggs 	if (amt < 0) {
    446  1.11    briggs 		amt = -amt;
    447  1.11    briggs 		b = fmt->bytelanes;
    448  1.11    briggs 		t = (b << 4);
    449  1.11    briggs 		b <<= (3 - (base & 0x3));
    450  1.11    briggs 		while (amt) {
    451  1.11    briggs 			b <<= 1;
    452  1.11    briggs 			if (b == t)
    453  1.11    briggs 				b = fmt->bytelanes;
    454  1.11    briggs 			if (b & 0x08)
    455  1.11    briggs 				amt--;
    456  1.11    briggs 			base--;
    457  1.11    briggs 		}
    458  1.11    briggs 		return base;
    459  1.11    briggs 	}
    460  1.10    briggs 
    461  1.11    briggs 	t = (fmt->bytelanes & 0xf) | 0x10;
    462  1.11    briggs 	b = t >> (base & 0x3);
    463  1.11    briggs 	while (amt) {
    464  1.11    briggs 		b >>= 1;
    465  1.11    briggs 		if (b == 1)
    466  1.11    briggs 			b = t;
    467  1.11    briggs 		if (b & 1)
    468  1.11    briggs 			amt--;
    469  1.11    briggs 		base++;
    470  1.11    briggs 	}
    471  1.11    briggs 
    472  1.11    briggs 	return base;
    473  1.11    briggs }
    474  1.11    briggs 
    475  1.11    briggs static u_long
    476  1.11    briggs nubus_calc_CRC(fmt)
    477  1.11    briggs 	nubus_slot	*fmt;
    478  1.11    briggs {
    479  1.11    briggs #if 0
    480  1.11    briggs 	u_long	base, ptr, crc_loc, sum;
    481  1.11    briggs 	int	i;
    482  1.11    briggs 
    483  1.11    briggs 	base = fmt->top;
    484  1.11    briggs 	crc_loc = NUBUS_ROM_offset(fmt, base, -12);
    485  1.11    briggs 	ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
    486  1.11    briggs 
    487  1.11    briggs 	sum = 0;
    488  1.11    briggs 	while (ptr < base)
    489  1.11    briggs 		roll #1, sum
    490  1.11    briggs 		if (ptr == crc_loc) {
    491  1.11    briggs 			roll #3, sum
    492  1.11    briggs 			ptr = IncPtr(fmt, ptr, 3);
    493  1.11    briggs 		} else {
    494  1.11    briggs 			sum += GetByte(fmt, ptr);
    495  1.11    briggs 		}
    496  1.11    briggs 		ptr = IncPtr(fmt, ptr, 1);
    497   1.1    briggs 	}
    498  1.10    briggs 
    499  1.11    briggs 	return sum;
    500  1.11    briggs #endif
    501  1.11    briggs 	return fmt->crc;
    502   1.1    briggs }
    503   1.1    briggs 
    504  1.11    briggs static u_char
    505  1.11    briggs GetByte(fmt, ptr)
    506  1.11    briggs 	nubus_slot	*fmt;
    507  1.11    briggs 	u_long		ptr;
    508   1.1    briggs {
    509  1.11    briggs 	return *(caddr_t)ptr;
    510   1.1    briggs }
    511   1.1    briggs 
    512  1.17    briggs #ifdef notyet
    513  1.17    briggs /* Nothing uses this, yet */
    514  1.11    briggs static u_short
    515  1.11    briggs GetWord(fmt, ptr)
    516  1.11    briggs 	nubus_slot	*fmt;
    517  1.11    briggs 	u_long		ptr;
    518   1.1    briggs {
    519  1.11    briggs 	u_short	s;
    520  1.10    briggs 
    521  1.11    briggs 	s = (GetByte(fmt, ptr) << 8);
    522  1.11    briggs 	ptr = IncPtr(fmt, ptr, 1);
    523  1.11    briggs 	s |= GetByte(fmt, ptr);
    524  1.11    briggs 	return s;
    525  1.11    briggs }
    526  1.17    briggs #endif
    527   1.1    briggs 
    528  1.11    briggs static u_long
    529  1.11    briggs GetLong(fmt, ptr)
    530  1.11    briggs 	nubus_slot	*fmt;
    531  1.11    briggs 	u_long		ptr;
    532  1.11    briggs {
    533  1.11    briggs 	register u_long l;
    534  1.11    briggs 	register int	i;
    535  1.10    briggs 
    536  1.11    briggs 	l = 0;
    537  1.11    briggs 	for ( i = 0; i < 4; i++) {
    538  1.11    briggs 		l = (l << 8) | GetByte(fmt, ptr);
    539  1.11    briggs 		ptr = IncPtr(fmt, ptr, 1);
    540  1.11    briggs 	}
    541  1.11    briggs 	return l;
    542  1.11    briggs }
    543  1.10    briggs 
    544  1.11    briggs void
    545  1.11    briggs nubus_get_main_dir(slot, dir_return)
    546  1.11    briggs 	nubus_slot	*slot;
    547  1.11    briggs 	nubus_dir	*dir_return;
    548  1.11    briggs {
    549  1.23    scottr #ifdef DEBUG
    550  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    551  1.26  christos 		printf("nubus_get_main_dir(0x%x, 0x%x)\n",
    552  1.12    briggs 			(u_int) slot, (u_int) dir_return);
    553  1.12    briggs #endif
    554  1.11    briggs 	dir_return->dirbase = IncPtr(slot, slot->top,
    555  1.11    briggs 					slot->directory_offset - 20);
    556  1.11    briggs 	dir_return->curr_ent = dir_return->dirbase;
    557   1.1    briggs }
    558   1.1    briggs 
    559  1.11    briggs int
    560  1.11    briggs nubus_find_rsrc(slot, dir, rsrcid, dirent_return)
    561  1.11    briggs 	nubus_slot	*slot;
    562  1.11    briggs 	nubus_dir	*dir;
    563  1.11    briggs 	u_int8_t	rsrcid;
    564  1.11    briggs 	nubus_dirent	*dirent_return;
    565   1.1    briggs {
    566  1.11    briggs 	u_long		entry;
    567  1.11    briggs 	u_char		byte;
    568  1.10    briggs 
    569  1.23    scottr #ifdef DEBUG
    570  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    571  1.26  christos 		printf("nubus_find_rsrc(0x%x, 0x%x, 0x%x, 0x%x)\n",
    572  1.12    briggs 			(u_int) slot, (u_int) dir, (u_int) rsrcid,
    573  1.12    briggs 			(u_int) dirent_return);
    574  1.11    briggs #endif
    575  1.12    briggs 	if (slot->test_pattern != NUBUS_ROM_TEST_PATTERN)
    576  1.12    briggs 		return -1;
    577  1.12    briggs 
    578  1.12    briggs 	entry = dir->curr_ent;
    579  1.11    briggs 	do {
    580  1.11    briggs 		byte = GetByte(slot, entry);
    581  1.23    scottr #ifdef DEBUG
    582  1.12    briggs 		if (nubus_debug & NDB_FOLLOW)
    583  1.26  christos 			printf("\tFound rsrc 0x%x.\n", byte);
    584  1.11    briggs #endif
    585  1.11    briggs 		if (byte == rsrcid) {
    586  1.11    briggs 			dirent_return->myloc = entry;
    587  1.11    briggs 			dirent_return->rsrc_id = rsrcid;
    588  1.11    briggs 			entry = GetLong(slot, entry);
    589  1.11    briggs 			dirent_return->offset = (entry & 0x00ffffff);
    590  1.11    briggs 			return 1;
    591  1.11    briggs 		}
    592  1.11    briggs 		if (byte == 0xff) {
    593  1.11    briggs 			entry = dir->dirbase;
    594  1.11    briggs 		} else {
    595  1.11    briggs 			entry = IncPtr(slot, entry, 4);
    596  1.11    briggs 		}
    597  1.11    briggs 	} while (entry != (u_long) dir->curr_ent);
    598  1.11    briggs 	return 0;
    599   1.1    briggs }
    600   1.1    briggs 
    601  1.11    briggs void
    602  1.11    briggs nubus_get_dir_from_rsrc(slot, dirent, dir_return)
    603  1.11    briggs 	nubus_slot	*slot;
    604  1.11    briggs 	nubus_dirent	*dirent;
    605  1.11    briggs 	nubus_dir	*dir_return;
    606   1.1    briggs {
    607  1.11    briggs 	u_long	loc;
    608  1.10    briggs 
    609  1.23    scottr #ifdef DEBUG
    610  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    611  1.26  christos 		printf("nubus_get_dir_from_rsrc(0x%x, 0x%x, 0x%x).\n",
    612  1.12    briggs 			(u_int) slot, (u_int) dirent, (u_int) dir_return);
    613  1.12    briggs #endif
    614  1.11    briggs 	if ((loc = dirent->offset) & 0x800000) {
    615  1.11    briggs 		loc |= 0xff000000;
    616   1.1    briggs 	}
    617  1.11    briggs 	dir_return->dirbase = IncPtr(slot, dirent->myloc, loc);
    618  1.11    briggs 	dir_return->curr_ent = dir_return->dirbase;
    619   1.1    briggs }
    620   1.1    briggs 
    621  1.11    briggs int
    622  1.11    briggs nubus_get_ind_data(slot, dirent, data_return, nbytes)
    623  1.11    briggs 	nubus_slot *slot;
    624  1.11    briggs 	nubus_dirent *dirent;
    625  1.11    briggs 	caddr_t data_return;
    626  1.11    briggs 	int nbytes;
    627  1.11    briggs {
    628  1.11    briggs 	u_long	loc;
    629   1.1    briggs 
    630  1.23    scottr #ifdef DEBUG
    631  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    632  1.26  christos 		printf("nubus_get_ind_data(0x%x, 0x%x, 0x%x, %d).\n",
    633  1.12    briggs 			(u_int) slot, (u_int) dirent, (u_int) data_return,
    634  1.12    briggs 			nbytes);
    635  1.12    briggs #endif
    636  1.11    briggs 	if ((loc = dirent->offset) & 0x800000) {
    637  1.11    briggs 		loc |= 0xff000000;
    638  1.11    briggs 	}
    639  1.11    briggs 	loc = IncPtr(slot, dirent->myloc, loc);
    640   1.1    briggs 
    641  1.11    briggs 	while (nbytes--) {
    642  1.11    briggs 		*data_return++ = GetByte(slot, loc);
    643  1.11    briggs 		loc = IncPtr(slot, loc, 1);
    644   1.1    briggs 	}
    645  1.11    briggs 	return 1;
    646   1.1    briggs }
    647   1.1    briggs 
    648  1.11    briggs int
    649  1.11    briggs nubus_get_c_string(slot, dirent, data_return, max_bytes)
    650  1.11    briggs 	nubus_slot *slot;
    651  1.11    briggs 	nubus_dirent *dirent;
    652  1.11    briggs 	caddr_t data_return;
    653  1.11    briggs 	int max_bytes;
    654   1.1    briggs {
    655  1.11    briggs 	u_long	loc;
    656  1.10    briggs 
    657  1.23    scottr #ifdef DEBUG
    658  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    659  1.26  christos 		printf("nubus_get_c_string(0x%x, 0x%x, 0x%x, %d).\n",
    660  1.12    briggs 			(u_int) slot, (u_int) dirent, (u_int) data_return,
    661  1.12    briggs 			max_bytes);
    662  1.12    briggs #endif
    663  1.11    briggs 	if ((loc = dirent->offset) & 0x800000) {
    664  1.11    briggs 		loc |= 0xff000000;
    665  1.11    briggs 	}
    666  1.11    briggs 	loc = IncPtr(slot, dirent->myloc, loc);
    667  1.10    briggs 
    668  1.11    briggs 	*data_return = '\0';
    669  1.11    briggs 	while (max_bytes--) {
    670  1.11    briggs 		if ((*data_return++ = GetByte(slot, loc)) == 0)
    671  1.11    briggs 			return 1;
    672  1.11    briggs 		loc = IncPtr(slot, loc, 1);
    673  1.11    briggs 	}
    674  1.11    briggs 	return 0;
    675   1.1    briggs }
    676   1.2    briggs 
    677  1.11    briggs static char	*huh = "???";
    678   1.3    briggs 
    679  1.11    briggs char *
    680  1.11    briggs nubus_get_vendor(slot, rsrc)
    681  1.11    briggs 	nubus_slot	*slot;
    682  1.11    briggs 	int		rsrc;
    683   1.3    briggs {
    684  1.11    briggs static	char		str_ret[64];
    685  1.11    briggs 	nubus_dir	dir;
    686  1.11    briggs 	nubus_dirent	ent;
    687   1.3    briggs 
    688  1.23    scottr #ifdef DEBUG
    689  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    690  1.26  christos 		printf("nubus_get_vendor(0x%x, 0x%x).\n", (u_int) slot, rsrc);
    691  1.12    briggs #endif
    692  1.11    briggs 	nubus_get_main_dir(slot, &dir);
    693  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
    694  1.11    briggs 		return huh;
    695  1.11    briggs 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    696   1.2    briggs 
    697  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_VENDORINFO, &ent) <= 0)
    698  1.11    briggs 		return huh;
    699  1.11    briggs 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    700  1.10    briggs 
    701  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, rsrc, &ent) <= 0)
    702  1.11    briggs 		return huh;
    703  1.10    briggs 
    704  1.11    briggs 	nubus_get_c_string(slot, &ent, str_ret, 64);
    705   1.2    briggs 
    706  1.11    briggs 	return str_ret;
    707   1.2    briggs }
    708   1.2    briggs 
    709  1.11    briggs char *
    710  1.11    briggs nubus_get_card_name(slot)
    711  1.11    briggs 	nubus_slot	*slot;
    712   1.2    briggs {
    713  1.11    briggs static	char		name_ret[64];
    714  1.11    briggs 	nubus_dir	dir;
    715  1.11    briggs 	nubus_dirent	ent;
    716   1.2    briggs 
    717  1.23    scottr #ifdef DEBUG
    718  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    719  1.26  christos 		printf("nubus_get_card_name(0x%lx).\n", (u_long) slot);
    720  1.12    briggs #endif
    721  1.11    briggs 	nubus_get_main_dir(slot, &dir);
    722   1.2    briggs 
    723  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
    724  1.11    briggs 		return huh;
    725   1.2    briggs 
    726  1.11    briggs 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    727   1.2    briggs 
    728  1.11    briggs 	if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
    729  1.11    briggs 		return huh;
    730   1.2    briggs 
    731  1.11    briggs 	nubus_get_c_string(slot, &ent, name_ret, 64);
    732   1.2    briggs 
    733  1.11    briggs 	return name_ret;
    734   1.2    briggs }
    735  1.29    scottr 
    736  1.29    scottr /*
    737  1.29    scottr  * bus_*() functions adapted from sun3 generic "bus" support
    738  1.29    scottr  * by Allen Briggs.
    739  1.29    scottr  */
    740  1.29    scottr 
    741  1.29    scottr vm_offset_t tmp_vpages[1];
    742  1.29    scottr 
    743  1.29    scottr /*
    744  1.29    scottr  * Read addr with size len (1,2,4) into val.
    745  1.29    scottr  * If this generates a bus error, return -1
    746  1.29    scottr  *
    747  1.29    scottr  *	Create a temporary mapping,
    748  1.29    scottr  *	Try the access using peek_*
    749  1.29    scottr  *	Clean up temp. mapping
    750  1.29    scottr  */
    751  1.29    scottr static int
    752  1.29    scottr nubus_peek(paddr, sz)
    753  1.29    scottr 	vm_offset_t paddr;
    754  1.29    scottr 	int sz;
    755  1.29    scottr {
    756  1.29    scottr 	int off, pte, rv;
    757  1.29    scottr 	vm_offset_t pgva;
    758  1.29    scottr 	caddr_t va;
    759  1.29    scottr 
    760  1.29    scottr 	off = paddr & PGOFSET;
    761  1.29    scottr 	paddr -= off;
    762  1.29    scottr 	pte = (paddr & PG_FRAME) | (PG_V | PG_W | PG_CI);
    763  1.29    scottr 
    764  1.29    scottr 	pgva = tmp_vpages[0];
    765  1.29    scottr 	va = (caddr_t)pgva + off;
    766  1.29    scottr 
    767  1.29    scottr 	mac68k_set_pte(pgva, pte);
    768  1.29    scottr 	TBIS(pgva);
    769  1.29    scottr 
    770  1.29    scottr 	/*
    771  1.29    scottr 	 * OK, try the access using one of the assembly routines
    772  1.29    scottr 	 * that will set pcb_onfault and catch any bus errors.
    773  1.29    scottr 	 */
    774  1.29    scottr 	rv = -1;
    775  1.29    scottr 	switch (sz) {
    776  1.29    scottr 	case 1:
    777  1.29    scottr 		if (!badbaddr(va))
    778  1.29    scottr 			rv = *((u_char *) va);
    779  1.29    scottr 		break;
    780  1.29    scottr 	case 2:
    781  1.29    scottr 		if (!badwaddr(va))
    782  1.29    scottr 			rv = *((u_int16_t *) va);
    783  1.29    scottr 		break;
    784  1.29    scottr 	case 4:
    785  1.29    scottr 		if (!badladdr(va))
    786  1.29    scottr 			rv = *((u_int32_t *) va);
    787  1.29    scottr 		break;
    788  1.29    scottr 	default:
    789  1.29    scottr 		printf("bus_peek: invalid size=%d\n", sz);
    790  1.29    scottr 		rv = -1;
    791  1.29    scottr 	}
    792  1.29    scottr 
    793  1.29    scottr 	mac68k_set_pte(pgva, PG_NV);
    794  1.29    scottr 	TBIS(pgva);
    795  1.29    scottr 
    796  1.29    scottr 	return rv;
    797  1.29    scottr }
    798  1.29    scottr 
    799  1.29    scottr static char *
    800  1.29    scottr nubus_mapin(paddr, sz)
    801  1.29    scottr 	int paddr, sz;
    802  1.29    scottr {
    803  1.29    scottr 	int off, pa, pmt=0;
    804  1.29    scottr 	vm_offset_t va, retval;
    805  1.29    scottr 
    806  1.29    scottr 	off = paddr & PGOFSET;
    807  1.29    scottr 	pa = paddr - off;
    808  1.29    scottr 	sz += off;
    809  1.29    scottr 	sz = mac68k_round_page(sz);
    810  1.29    scottr 
    811  1.29    scottr 	/* Get some kernel virtual address space. */
    812  1.29    scottr 	va = kmem_alloc_wait(kernel_map, sz);
    813  1.29    scottr 	if (va == 0)
    814  1.29    scottr 		panic("bus_mapin");
    815  1.29    scottr 	retval = va + off;
    816  1.29    scottr 
    817  1.29    scottr 	/* Map it to the specified bus. */
    818  1.29    scottr #if 0	/* XXX */
    819  1.29    scottr 	/* This has a problem with wrap-around... */
    820  1.29    scottr 	pmap_map((int)va, pa | pmt, pa + sz, VM_PROT_ALL);
    821  1.29    scottr #else
    822  1.29    scottr 	do {
    823  1.29    scottr 		pmap_enter(pmap_kernel(), va, pa | pmt, VM_PROT_ALL, FALSE);
    824  1.29    scottr 		va += NBPG;
    825  1.29    scottr 		pa += NBPG;
    826  1.29    scottr 	} while ((sz -= NBPG) > 0);
    827  1.29    scottr #endif
    828  1.29    scottr 
    829  1.29    scottr 	return ((char*)retval);
    830  1.29    scottr }
    831