Home | History | Annotate | Line # | Download | only in nubus
nubus.c revision 1.52
      1  1.52    briggs /*	$NetBSD: nubus.c,v 1.52 2002/04/10 04:38:50 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 
     38  1.49       mrg #include <uvm/uvm_extern.h>
     39  1.10    briggs 
     40  1.19    briggs #include <machine/autoconf.h>
     41  1.29    scottr #include <machine/vmparam.h>
     42  1.29    scottr #include <machine/param.h>
     43   1.4    briggs #include <machine/cpu.h>
     44  1.29    scottr #include <machine/pte.h>
     45  1.33    scottr #include <machine/viareg.h>
     46  1.12    briggs 
     47  1.44    scottr #include <mac68k/nubus/nubus.h>
     48   1.1    briggs 
     49  1.23    scottr #ifdef DEBUG
     50  1.12    briggs #define NDB_PROBE	0x1
     51  1.12    briggs #define NDB_FOLLOW	0x2
     52  1.12    briggs #define NDB_ARITH	0x4
     53  1.37    scottr static int	nubus_debug = 0 /* | NDB_PROBE */;
     54  1.12    briggs #endif
     55  1.12    briggs 
     56  1.28    scottr static int	nubus_print __P((void *, const char *));
     57  1.28    scottr static int	nubus_match __P((struct device *, struct cfdata *, void *));
     58  1.28    scottr static void	nubus_attach __P((struct device *, struct device *, void *));
     59  1.37    scottr static int	nubus_video_resource __P((int));
     60  1.11    briggs 
     61  1.37    scottr static int	nubus_probe_slot __P((bus_space_tag_t, bus_space_handle_t,
     62  1.37    scottr 		    int, nubus_slot *));
     63  1.37    scottr static u_int32_t nubus_calc_CRC __P((bus_space_tag_t, bus_space_handle_t,
     64  1.37    scottr 		    nubus_slot *));
     65  1.37    scottr 
     66  1.37    scottr static u_long	nubus_adjust_ptr __P((u_int8_t, u_long, long));
     67  1.37    scottr static u_int8_t	nubus_read_1 __P((bus_space_tag_t, bus_space_handle_t,
     68  1.37    scottr 		    u_int8_t, u_long));
     69  1.17    briggs #ifdef notyet
     70  1.37    scottr static u_int16_t nubus_read_2 __P((bus_space_tag_t, bus_space_handle_t,
     71  1.37    scottr 		    u_int8_t, u_long));
     72  1.17    briggs #endif
     73  1.37    scottr static u_int32_t nubus_read_4 __P((bus_space_tag_t, bus_space_handle_t,
     74  1.37    scottr 		    u_int8_t, u_long));
     75  1.29    scottr 
     76  1.16   thorpej struct cfattach nubus_ca = {
     77  1.28    scottr 	sizeof(struct nubus_softc), nubus_match, nubus_attach
     78  1.11    briggs };
     79   1.1    briggs 
     80  1.19    briggs static int
     81  1.28    scottr nubus_match(parent, cf, aux)
     82  1.19    briggs 	struct device *parent;
     83  1.28    scottr 	struct cfdata *cf;
     84  1.28    scottr 	void *aux;
     85  1.19    briggs {
     86  1.29    scottr 	static int nubus_matched = 0;
     87  1.19    briggs 
     88  1.29    scottr 	/* Allow only one instance. */
     89  1.29    scottr 	if (nubus_matched)
     90  1.29    scottr 		return (0);
     91  1.29    scottr 
     92  1.29    scottr 	nubus_matched = 1;
     93  1.29    scottr 	return (1);
     94  1.19    briggs }
     95  1.19    briggs 
     96  1.11    briggs static void
     97  1.28    scottr nubus_attach(parent, self, aux)
     98  1.28    scottr 	struct device *parent, *self;
     99  1.28    scottr 	void *aux;
    100  1.11    briggs {
    101  1.28    scottr 	struct nubus_attach_args na_args;
    102  1.52    briggs 	struct mainbus_attach_args *mba = (struct mainbus_attach_args *) mba;
    103  1.37    scottr 	bus_space_tag_t bst;
    104  1.37    scottr 	bus_space_handle_t bsh;
    105  1.28    scottr 	nubus_slot fmtblock;
    106  1.28    scottr 	nubus_dir dir;
    107  1.28    scottr 	nubus_dirent dirent;
    108  1.28    scottr 	nubus_type slottype;
    109  1.34    briggs 	u_long entry;
    110  1.28    scottr 	int i, rsrcid;
    111  1.37    scottr 	u_int8_t lanes;
    112   1.1    briggs 
    113  1.26  christos 	printf("\n");
    114   1.1    briggs 
    115  1.19    briggs 	for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
    116  1.37    scottr 		na_args.slot = i;
    117  1.52    briggs 		na_args.na_tag = bst = mba->mba_bst;
    118  1.52    briggs 		na_args.na_dmat = mba->mba_dmat;
    119  1.37    scottr 
    120  1.37    scottr 		if (bus_space_map(bst,
    121  1.37    scottr 		    NUBUS_SLOT2PA(na_args.slot), NBMEMSIZE, 0, &bsh)) {
    122  1.45    scottr #ifdef DEBUG
    123  1.45    scottr 			if (nubus_debug & NDB_PROBE)
    124  1.45    scottr 				printf("%s: failed to map slot %x, "
    125  1.45    scottr 				    "address %p (in use?)\n",
    126  1.45    scottr 				    self->dv_xname, i,
    127  1.45    scottr 				    (void *)NUBUS_SLOT2PA(i));
    128  1.37    scottr #endif
    129  1.28    scottr 			continue;
    130  1.37    scottr 		}
    131  1.28    scottr 
    132  1.37    scottr 		if (nubus_probe_slot(bst, bsh, i, &fmtblock) <= 0) {
    133  1.37    scottr notfound:
    134  1.37    scottr 			bus_space_unmap(bst, bsh, NBMEMSIZE);
    135  1.37    scottr 			continue;
    136  1.37    scottr 		}
    137  1.37    scottr 
    138  1.34    briggs 		rsrcid = 0x80;
    139  1.37    scottr 		lanes = fmtblock.bytelanes;
    140  1.28    scottr 
    141  1.28    scottr 		nubus_get_main_dir(&fmtblock, &dir);
    142  1.28    scottr 
    143  1.34    briggs 		/*
    144  1.34    briggs 		 * Get the resource for the first function on the card.
    145  1.34    briggs 		 * This is assumed to be at resource ID 0x80.  If we can
    146  1.34    briggs 		 * not find this entry (as we can not on some video cards),
    147  1.34    briggs 		 * check to see if we can get a different ID from the list
    148  1.34    briggs 		 * of video resources given to us by the booter.  If that
    149  1.34    briggs 		 * doesn't work either, take the first resource following
    150  1.34    briggs 		 * the board resource.
    151  1.34    briggs 		 */
    152  1.37    scottr 		if (nubus_find_rsrc(bst, bsh,
    153  1.37    scottr 		    &fmtblock, &dir, rsrcid, &dirent) <= 0) {
    154  1.34    briggs 			if ((rsrcid = nubus_video_resource(i)) == -1) {
    155  1.34    briggs 				/*
    156  1.34    briggs 				 * Since nubus_find_rsrc failed, the directory
    157  1.34    briggs 				 * is back at its base.
    158  1.34    briggs 				 */
    159  1.34    briggs 				entry = dir.curr_ent;
    160  1.34    briggs 
    161  1.34    briggs 				/*
    162  1.34    briggs 				 * All nubus cards should have a board
    163  1.34    briggs 				 * resource, but be sure that's what it
    164  1.34    briggs 				 * is before we skip it.
    165  1.34    briggs 				 */
    166  1.37    scottr 				rsrcid = nubus_read_1(bst, bsh,
    167  1.37    scottr 				    lanes, entry);
    168  1.34    briggs 				if (rsrcid == 0x1)
    169  1.37    scottr 					entry = nubus_adjust_ptr(lanes,
    170  1.37    scottr 					    dir.curr_ent, 4);
    171  1.34    briggs 
    172  1.37    scottr 				rsrcid = nubus_read_1(bst, bsh, lanes, entry);
    173  1.34    briggs #ifdef DEBUG
    174  1.34    briggs 				if (nubus_debug & NDB_FOLLOW)
    175  1.34    briggs 					printf("\tUsing rsrc 0x%x.\n", rsrcid);
    176  1.34    briggs #endif
    177  1.37    scottr 				if (rsrcid == 0xff)	/* end of chain */
    178  1.37    scottr 					goto notfound;
    179  1.34    briggs 			}
    180  1.34    briggs 			/*
    181  1.34    briggs 			 * Try to find the resource passed by the booter
    182  1.34    briggs 			 * or the one we just tracked down.
    183  1.34    briggs 			 */
    184  1.37    scottr 			if (nubus_find_rsrc(bst, bsh,
    185  1.37    scottr 			    &fmtblock, &dir, rsrcid, &dirent) <= 0)
    186  1.37    scottr 				goto notfound;
    187  1.34    briggs 		}
    188  1.28    scottr 
    189  1.28    scottr 		nubus_get_dir_from_rsrc(&fmtblock, &dirent, &dir);
    190  1.28    scottr 
    191  1.37    scottr 		if (nubus_find_rsrc(bst, bsh,
    192  1.37    scottr 		    &fmtblock, &dir, NUBUS_RSRC_TYPE, &dirent) <= 0)
    193  1.37    scottr 			goto notfound;
    194  1.37    scottr 
    195  1.37    scottr 		if (nubus_get_ind_data(bst, bsh, &fmtblock, &dirent,
    196  1.37    scottr 		    (caddr_t)&slottype, sizeof(nubus_type)) <= 0)
    197  1.37    scottr 			goto notfound;
    198  1.34    briggs 
    199  1.34    briggs 		/*
    200  1.34    briggs 		 * If this is a display card, try to pull out the correct
    201  1.34    briggs 		 * display mode as passed by the booter.
    202  1.34    briggs 		 */
    203  1.34    briggs 		if (slottype.category == NUBUS_CATEGORY_DISPLAY) {
    204  1.37    scottr 			int r;
    205  1.34    briggs 
    206  1.34    briggs 			if ((r = nubus_video_resource(i)) != -1) {
    207  1.34    briggs 
    208  1.34    briggs 				nubus_get_main_dir(&fmtblock, &dir);
    209  1.34    briggs 
    210  1.37    scottr 				if (nubus_find_rsrc(bst, bsh,
    211  1.37    scottr 				    &fmtblock, &dir, r, &dirent) <= 0)
    212  1.37    scottr 					goto notfound;
    213  1.34    briggs 
    214  1.34    briggs 				nubus_get_dir_from_rsrc(&fmtblock,
    215  1.37    scottr 				    &dirent, &dir);
    216  1.34    briggs 
    217  1.37    scottr 				if (nubus_find_rsrc(bst, bsh, &fmtblock, &dir,
    218  1.37    scottr 				    NUBUS_RSRC_TYPE, &dirent) <= 0)
    219  1.37    scottr 					goto notfound;
    220  1.37    scottr 
    221  1.37    scottr 				if (nubus_get_ind_data(bst, bsh,
    222  1.37    scottr 				    &fmtblock, &dirent, (caddr_t)&slottype,
    223  1.37    scottr 				    sizeof(nubus_type)) <= 0)
    224  1.37    scottr 					goto notfound;
    225  1.36    briggs 
    226  1.36    briggs 				rsrcid = r;
    227  1.34    briggs 			}
    228  1.34    briggs 		}
    229  1.28    scottr 
    230  1.28    scottr 		na_args.slot = i;
    231  1.28    scottr 		na_args.rsrcid = rsrcid;
    232  1.28    scottr 		na_args.category = slottype.category;
    233  1.28    scottr 		na_args.type = slottype.type;
    234  1.28    scottr 		na_args.drsw = slottype.drsw;
    235  1.28    scottr 		na_args.drhw = slottype.drhw;
    236  1.28    scottr 		na_args.fmt = &fmtblock;
    237  1.28    scottr 
    238  1.37    scottr 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    239  1.37    scottr 
    240  1.28    scottr 		config_found(self, &na_args, nubus_print);
    241  1.12    briggs 	}
    242  1.33    scottr 
    243  1.33    scottr 	enable_nubus_intr();
    244   1.1    briggs }
    245   1.1    briggs 
    246  1.10    briggs static int
    247  1.40    scottr nubus_print(aux, pnp)
    248  1.28    scottr 	void *aux;
    249  1.40    scottr 	const char *pnp;
    250  1.11    briggs {
    251  1.37    scottr 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    252  1.37    scottr 	bus_space_tag_t bst = na->na_tag;
    253  1.37    scottr 	bus_space_handle_t bsh;
    254   1.1    briggs 
    255  1.40    scottr 	if (pnp) {
    256  1.40    scottr 		printf("%s slot %x", pnp, na->slot);
    257  1.37    scottr 		if (bus_space_map(bst,
    258  1.40    scottr 		    NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &bsh) == 0) {
    259  1.40    scottr 			printf(": %s", nubus_get_card_name(bst, bsh, na->fmt));
    260  1.37    scottr 			printf(" (Vendor: %s,", nubus_get_vendor(bst, bsh,
    261  1.37    scottr 			    na->fmt, NUBUS_RSRC_VEND_ID));
    262  1.40    scottr 			printf(" Part: %s)", nubus_get_vendor(bst, bsh,
    263  1.37    scottr 			    na->fmt, NUBUS_RSRC_VEND_PART));
    264  1.40    scottr 			bus_space_unmap(bst, bsh, NBMEMSIZE);
    265  1.40    scottr 		}
    266  1.35    scottr #ifdef DIAGNOSTIC
    267  1.40    scottr 		else
    268  1.40    scottr 			printf(":");
    269  1.40    scottr 		printf(" Type: %04x %04x %04x %04x",
    270  1.40    scottr 		    na->category, na->type, na->drsw, na->drhw);
    271  1.35    scottr #endif
    272  1.40    scottr 	} else {
    273  1.40    scottr 		printf(" slot %x", na->slot);
    274   1.1    briggs 	}
    275  1.11    briggs 	return (UNCONF);
    276  1.28    scottr }
    277  1.28    scottr 
    278  1.37    scottr static int
    279  1.28    scottr nubus_video_resource(slot)
    280  1.28    scottr 	int slot;
    281  1.28    scottr {
    282  1.28    scottr 	extern u_int16_t mac68k_vrsrc_vec[];
    283  1.28    scottr 	int i;
    284  1.28    scottr 
    285  1.28    scottr 	for (i = 0 ; i < 6 ; i++)
    286  1.28    scottr 		if ((mac68k_vrsrc_vec[i] & 0xff) == slot)
    287  1.28    scottr 			return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
    288  1.28    scottr 	return (-1);
    289   1.1    briggs }
    290   1.1    briggs 
    291  1.11    briggs /*
    292  1.11    briggs  * Probe a given nubus slot.  If a card is there and we can get the
    293  1.11    briggs  * format block from it's clutching decl. ROMs, fill the format block
    294  1.11    briggs  * and return non-zero.  If we can't find a card there with a valid
    295  1.11    briggs  * decl. ROM, return 0.
    296  1.11    briggs  *
    297  1.11    briggs  * First, we check to see if we can access the memory at the tail
    298  1.11    briggs  * end of the slot.  If so, then we check for a bytelanes byte.  We
    299  1.11    briggs  * could probably just return a failure status if we bus error on
    300  1.11    briggs  * the first try, but there really is little reason not to go ahead
    301  1.51       wiz  * and check the other three locations in case there's a weird card
    302  1.11    briggs  * out there.
    303  1.11    briggs  *
    304  1.11    briggs  * Checking for a card involves locating the "bytelanes" byte which
    305  1.11    briggs  * tells us how to interpret the declaration ROM's data.  The format
    306  1.11    briggs  * block is at the top of the card's standard memory space and the
    307  1.11    briggs  * bytelanes byte is at the end of that block.
    308  1.11    briggs  *
    309  1.11    briggs  * After some inspection of the bytelanes byte, it appears that it
    310  1.11    briggs  * takes the form 0xXY where Y is a bitmask of the bytelanes in use
    311  1.11    briggs  * and X is a bitmask of the lanes to ignore.  Hence, (X ^ Y) == 0
    312  1.11    briggs  * and (less obviously), Y will have the upper N bits clear if it is
    313  1.11    briggs  * found N bytes from the last possible location.  Both that and
    314  1.11    briggs  * the exclusive-or check are made.
    315  1.11    briggs  *
    316  1.11    briggs  * If a valid
    317  1.11    briggs  */
    318  1.37    scottr static u_int8_t	nbits[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    319  1.11    briggs static int
    320  1.37    scottr nubus_probe_slot(bst, bsh, slot, fmt)
    321  1.37    scottr 	bus_space_tag_t bst;
    322  1.37    scottr 	bus_space_handle_t bsh;
    323  1.37    scottr 	int slot;
    324  1.37    scottr 	nubus_slot *fmt;
    325   1.1    briggs {
    326  1.37    scottr 	u_long ofs, hdr;
    327  1.37    scottr 	int i, j, found, hdr_size;
    328  1.37    scottr 	u_int8_t lanes;
    329  1.10    briggs 
    330  1.12    briggs #ifdef DEBUG
    331  1.37    scottr 	if (nubus_debug & NDB_PROBE)
    332  1.37    scottr 		printf("probing slot %x\n", slot);
    333  1.12    briggs #endif
    334  1.12    briggs 
    335  1.37    scottr 	/*
    336  1.37    scottr 	 * The idea behind this glorious work of art is to probe for only
    337  1.38    scottr 	 * valid bytelanes values at appropriate locations (see DC&D p. 159
    338  1.37    scottr 	 * for a list).  Note the pattern:  the first 8 values are at offset
    339  1.37    scottr 	 * 0xffffff in the slot's space; the next 4 values at 0xfffffe; the
    340  1.37    scottr 	 * next 2 values at 0xfffffd; and the last one at 0xfffffc.
    341  1.37    scottr 	 *
    342  1.37    scottr 	 * The nested loops implement an efficient search of this space,
    343  1.37    scottr 	 * probing first for a valid address, then checking for each of the
    344  1.37    scottr 	 * valid bytelanes values at that address.
    345  1.37    scottr 	 */
    346  1.37    scottr 	ofs = NBMEMSIZE;
    347  1.37    scottr 	lanes = 0xf;
    348  1.10    briggs 
    349  1.37    scottr 	for (j = 8, found = 0; j > 0 && !found; j >>= 1) {
    350  1.37    scottr 		ofs--;
    351  1.37    scottr 		for (i = j; i > 0; i--, lanes--) {
    352  1.43    scottr 			if (!mac68k_bus_space_probe(bst, bsh, ofs, 1)) {
    353  1.37    scottr 				lanes -= i;
    354  1.37    scottr 				break;
    355  1.37    scottr 			}
    356  1.37    scottr 			if (bus_space_read_1(bst, bsh, ofs) ==
    357  1.37    scottr 			    (((~lanes & 0xf) << 4) | lanes)) {
    358  1.37    scottr 				found = 1;
    359  1.37    scottr 				break;
    360  1.37    scottr 			}
    361  1.11    briggs 		}
    362   1.1    briggs 	}
    363  1.37    scottr 
    364  1.37    scottr 	if (!found) {
    365  1.11    briggs #ifdef DEBUG
    366  1.37    scottr 		if (nubus_debug & NDB_PROBE)
    367  1.37    scottr 			printf("bytelanes not found for slot %x\n", slot);
    368  1.11    briggs #endif
    369  1.37    scottr 		return 0;
    370  1.37    scottr 	}
    371  1.11    briggs 
    372  1.37    scottr 	fmt->bytelanes = lanes;
    373  1.37    scottr 	fmt->step = nbits[(lanes & 0x0f)];
    374  1.37    scottr 	fmt->slot = slot;	/* XXX redundant; get rid of this someday */
    375  1.11    briggs 
    376  1.11    briggs #ifdef DEBUG
    377  1.12    briggs 	if (nubus_debug & NDB_PROBE)
    378  1.26  christos 		printf("bytelanes of 0x%x found for slot 0x%x.\n",
    379  1.37    scottr 		    fmt->bytelanes, slot);
    380  1.11    briggs #endif
    381   1.1    briggs 
    382  1.11    briggs 	/*
    383  1.11    briggs 	 * Go ahead and attempt to load format header.
    384  1.11    briggs 	 * First, we need to find the first byte beyond memory that
    385  1.11    briggs 	 * would be valid.  This is necessary for NUBUS_ROM_offset()
    386  1.11    briggs 	 * to work.
    387  1.11    briggs 	 */
    388  1.37    scottr 	hdr = NBMEMSIZE;
    389  1.37    scottr 	hdr_size = 20;
    390  1.19    briggs 
    391  1.37    scottr 	i = 0x10 | (lanes & 0x0f);
    392  1.11    briggs 	while ((i & 1) == 0) {
    393  1.11    briggs 		hdr++;
    394  1.11    briggs 		i >>= 1;
    395  1.11    briggs 	}
    396  1.11    briggs 	fmt->top = hdr;
    397  1.37    scottr 	hdr = nubus_adjust_ptr(lanes, hdr, -hdr_size);
    398  1.11    briggs #ifdef DEBUG
    399  1.12    briggs 	if (nubus_debug & NDB_PROBE)
    400  1.27    scottr 		printf("fmt->top is 0x%lx, that minus 0x%x puts us at 0x%lx.\n",
    401  1.37    scottr 		    fmt->top, hdr_size, hdr);
    402  1.37    scottr 	if (nubus_debug & NDB_ARITH)
    403  1.37    scottr 		for (i = 1 ; i < 8 ; i++)
    404  1.37    scottr 			printf("0x%lx - 0x%x = 0x%lx, + 0x%x = 0x%lx.\n",
    405  1.37    scottr 			    hdr, i, nubus_adjust_ptr(lanes, hdr, -i),
    406  1.37    scottr 			    i, nubus_adjust_ptr(lanes, hdr,  i));
    407  1.37    scottr #endif
    408  1.37    scottr 
    409  1.37    scottr 	fmt->directory_offset =
    410  1.37    scottr 	    0xff000000 | nubus_read_4(bst, bsh, lanes, hdr);
    411  1.37    scottr 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    412  1.37    scottr 	fmt->length = nubus_read_4(bst, bsh, lanes, hdr);
    413  1.37    scottr 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    414  1.37    scottr 	fmt->crc = nubus_read_4(bst, bsh, lanes, hdr);
    415  1.37    scottr 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    416  1.37    scottr 	fmt->revision_level = nubus_read_1(bst, bsh, lanes, hdr);
    417  1.37    scottr 	hdr = nubus_adjust_ptr(lanes, hdr, 1);
    418  1.37    scottr 	fmt->format = nubus_read_1(bst, bsh, lanes, hdr);
    419  1.37    scottr 	hdr = nubus_adjust_ptr(lanes, hdr, 1);
    420  1.37    scottr 	fmt->test_pattern = nubus_read_4(bst, bsh, lanes, hdr);
    421  1.11    briggs 
    422  1.23    scottr #ifdef DEBUG
    423  1.12    briggs 	if (nubus_debug & NDB_PROBE) {
    424  1.26  christos 		printf("Directory offset 0x%x\t", fmt->directory_offset);
    425  1.26  christos 		printf("Length 0x%x\t", fmt->length);
    426  1.26  christos 		printf("CRC 0x%x\n", fmt->crc);
    427  1.26  christos 		printf("Revision level 0x%x\t", fmt->revision_level);
    428  1.26  christos 		printf("Format 0x%x\t", fmt->format);
    429  1.26  christos 		printf("Test Pattern 0x%x\n", fmt->test_pattern);
    430  1.12    briggs 	}
    431  1.11    briggs #endif
    432  1.11    briggs 
    433  1.12    briggs 	if ((fmt->directory_offset & 0x00ff0000) == 0) {
    434  1.26  christos 		printf("Invalid looking directory offset (0x%x)!\n",
    435  1.37    scottr 		    fmt->directory_offset);
    436  1.12    briggs 		return 0;
    437  1.12    briggs 	}
    438  1.11    briggs 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
    439  1.26  christos 		printf("Nubus--test pattern invalid:\n");
    440  1.37    scottr 		printf("       slot 0x%x, bytelanes 0x%x?\n", fmt->slot, lanes);
    441  1.26  christos 		printf("       read test 0x%x, compare with 0x%x.\n",
    442  1.37    scottr 		    fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
    443  1.11    briggs 		return 0;
    444  1.11    briggs 	}
    445  1.11    briggs 
    446  1.11    briggs 	/* Perform CRC */
    447  1.37    scottr 	if (fmt->crc != nubus_calc_CRC(bst, bsh, fmt)) {
    448  1.37    scottr 		printf("Nubus--crc check failed, slot 0x%x.\n", fmt->slot);
    449  1.11    briggs 		return 0;
    450   1.1    briggs 	}
    451  1.10    briggs 
    452  1.11    briggs 	return 1;
    453   1.1    briggs }
    454   1.1    briggs 
    455  1.37    scottr static u_int32_t
    456  1.37    scottr nubus_calc_CRC(bst, bsh, fmt)
    457  1.37    scottr 	bus_space_tag_t bst;
    458  1.37    scottr 	bus_space_handle_t bsh;
    459  1.37    scottr 	nubus_slot	*fmt;
    460  1.37    scottr {
    461  1.37    scottr #if 0
    462  1.37    scottr 	u_long base, ptr, crc_loc;
    463  1.37    scottr 	u_int32_t sum;
    464  1.37    scottr 	u_int8_t lanes = fmt->bytelanes;
    465  1.37    scottr 
    466  1.37    scottr 	base = fmt->top;
    467  1.37    scottr 	crc_loc = NUBUS_ROM_offset(fmt, base, -12);
    468  1.37    scottr 	ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
    469  1.37    scottr 
    470  1.37    scottr 	sum = 0;
    471  1.37    scottr 	while (ptr < base)
    472  1.37    scottr 		roll #1, sum
    473  1.37    scottr 		if (ptr == crc_loc) {
    474  1.37    scottr 			roll #3, sum
    475  1.37    scottr 			ptr = nubus_adjust_ptr(lanes, ptr, 3);
    476  1.37    scottr 		} else {
    477  1.37    scottr 			sum += nubus_read_1(bst, bsh, lanes, ptr);
    478  1.37    scottr 		}
    479  1.37    scottr 		ptr = nubus_adjust_ptr(lanes, ptr, 1);
    480  1.37    scottr 	}
    481  1.37    scottr 
    482  1.37    scottr 	return sum;
    483  1.37    scottr #endif
    484  1.37    scottr 	return fmt->crc;
    485  1.37    scottr }
    486  1.37    scottr 
    487  1.11    briggs /*
    488  1.11    briggs  * Compute byte offset on card, taking into account bytelanes.
    489  1.11    briggs  * Base must be on a valid bytelane for this function to work.
    490  1.11    briggs  * Return the new address.
    491  1.11    briggs  *
    492  1.11    briggs  * XXX -- There has GOT to be a better way to do this.
    493  1.11    briggs  */
    494  1.11    briggs static u_long
    495  1.37    scottr nubus_adjust_ptr(lanes, base, amt)
    496  1.37    scottr 	u_int8_t lanes;
    497  1.37    scottr 	u_long base;
    498  1.37    scottr 	long amt;
    499  1.11    briggs {
    500  1.37    scottr 	u_int8_t b, t;
    501  1.11    briggs 
    502  1.11    briggs 	if (!amt)
    503  1.11    briggs 		return base;
    504  1.11    briggs 
    505  1.11    briggs 	if (amt < 0) {
    506  1.11    briggs 		amt = -amt;
    507  1.37    scottr 		b = lanes;
    508  1.11    briggs 		t = (b << 4);
    509  1.11    briggs 		b <<= (3 - (base & 0x3));
    510  1.11    briggs 		while (amt) {
    511  1.11    briggs 			b <<= 1;
    512  1.11    briggs 			if (b == t)
    513  1.37    scottr 				b = lanes;
    514  1.11    briggs 			if (b & 0x08)
    515  1.11    briggs 				amt--;
    516  1.11    briggs 			base--;
    517  1.11    briggs 		}
    518  1.11    briggs 		return base;
    519  1.11    briggs 	}
    520  1.10    briggs 
    521  1.37    scottr 	t = (lanes & 0xf) | 0x10;
    522  1.11    briggs 	b = t >> (base & 0x3);
    523  1.11    briggs 	while (amt) {
    524  1.11    briggs 		b >>= 1;
    525  1.11    briggs 		if (b == 1)
    526  1.11    briggs 			b = t;
    527  1.11    briggs 		if (b & 1)
    528  1.11    briggs 			amt--;
    529  1.11    briggs 		base++;
    530  1.11    briggs 	}
    531  1.11    briggs 
    532  1.11    briggs 	return base;
    533  1.11    briggs }
    534  1.11    briggs 
    535  1.37    scottr static u_int8_t
    536  1.37    scottr nubus_read_1(bst, bsh, lanes, ofs)
    537  1.37    scottr 	bus_space_tag_t bst;
    538  1.37    scottr 	bus_space_handle_t bsh;
    539  1.37    scottr 	u_int8_t lanes;
    540  1.37    scottr 	u_long ofs;
    541   1.1    briggs {
    542  1.37    scottr 	return bus_space_read_1(bst, bsh, ofs);
    543   1.1    briggs }
    544   1.1    briggs 
    545  1.17    briggs #ifdef notyet
    546  1.17    briggs /* Nothing uses this, yet */
    547  1.37    scottr static u_int16_t
    548  1.37    scottr nubus_read_2(bst, bsh, lanes, ofs)
    549  1.37    scottr 	bus_space_tag_t bst;
    550  1.37    scottr 	bus_space_handle_t bsh;
    551  1.37    scottr 	u_int8_t lanes;
    552  1.37    scottr 	u_long ofs;
    553  1.37    scottr {
    554  1.37    scottr 	u_int16_t s;
    555  1.37    scottr 
    556  1.37    scottr 	s = (nubus_read_1(bst, bsh, lanes, ofs) << 8);
    557  1.37    scottr 	ofs = nubus_adjust_ptr(lanes, ofs, 1);
    558  1.37    scottr 	s |= nubus_read_1(bst, bsh, lanes, ofs);
    559  1.11    briggs 	return s;
    560  1.11    briggs }
    561  1.17    briggs #endif
    562   1.1    briggs 
    563  1.37    scottr static u_int32_t
    564  1.37    scottr nubus_read_4(bst, bsh, lanes, ofs)
    565  1.37    scottr 	bus_space_tag_t bst;
    566  1.37    scottr 	bus_space_handle_t bsh;
    567  1.37    scottr 	u_int8_t lanes;
    568  1.37    scottr 	u_long ofs;
    569  1.11    briggs {
    570  1.37    scottr 	u_int32_t l;
    571  1.37    scottr 	int i;
    572  1.10    briggs 
    573  1.11    briggs 	l = 0;
    574  1.37    scottr 	for (i = 0; i < 4; i++) {
    575  1.37    scottr 		l = (l << 8) | nubus_read_1(bst, bsh, lanes, ofs);
    576  1.37    scottr 		ofs = nubus_adjust_ptr(lanes, ofs, 1);
    577  1.11    briggs 	}
    578  1.11    briggs 	return l;
    579  1.11    briggs }
    580  1.10    briggs 
    581  1.11    briggs void
    582  1.37    scottr nubus_get_main_dir(fmt, dir_return)
    583  1.37    scottr 	nubus_slot *fmt;
    584  1.37    scottr 	nubus_dir *dir_return;
    585  1.11    briggs {
    586  1.23    scottr #ifdef DEBUG
    587  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    588  1.37    scottr 		printf("nubus_get_main_dir(%p, %p)\n",
    589  1.37    scottr 		    fmt, dir_return);
    590  1.12    briggs #endif
    591  1.37    scottr 	dir_return->dirbase = nubus_adjust_ptr(fmt->bytelanes, fmt->top,
    592  1.37    scottr 	    fmt->directory_offset - 20);
    593  1.37    scottr 	dir_return->curr_ent = dir_return->dirbase;
    594  1.37    scottr }
    595  1.37    scottr 
    596  1.37    scottr void
    597  1.37    scottr nubus_get_dir_from_rsrc(fmt, dirent, dir_return)
    598  1.37    scottr 	nubus_slot *fmt;
    599  1.37    scottr 	nubus_dirent *dirent;
    600  1.37    scottr 	nubus_dir *dir_return;
    601  1.37    scottr {
    602  1.37    scottr 	u_long loc;
    603  1.37    scottr 
    604  1.37    scottr #ifdef DEBUG
    605  1.37    scottr 	if (nubus_debug & NDB_FOLLOW)
    606  1.37    scottr 		printf("nubus_get_dir_from_rsrc(%p, %p, %p).\n",
    607  1.37    scottr 		    fmt, dirent, dir_return);
    608  1.37    scottr #endif
    609  1.37    scottr 	if ((loc = dirent->offset) & 0x800000) {
    610  1.37    scottr 		loc |= 0xff000000;
    611  1.37    scottr 	}
    612  1.37    scottr 	dir_return->dirbase =
    613  1.37    scottr 	    nubus_adjust_ptr(fmt->bytelanes, dirent->myloc, loc);
    614  1.11    briggs 	dir_return->curr_ent = dir_return->dirbase;
    615   1.1    briggs }
    616   1.1    briggs 
    617  1.11    briggs int
    618  1.37    scottr nubus_find_rsrc(bst, bsh, fmt, dir, rsrcid, dirent_return)
    619  1.37    scottr 	bus_space_tag_t bst;
    620  1.37    scottr 	bus_space_handle_t bsh;
    621  1.37    scottr 	nubus_slot *fmt;
    622  1.37    scottr 	nubus_dir *dir;
    623  1.37    scottr 	u_int8_t rsrcid;
    624  1.37    scottr 	nubus_dirent *dirent_return;
    625   1.1    briggs {
    626  1.37    scottr 	u_long entry;
    627  1.37    scottr 	u_int8_t byte, lanes = fmt->bytelanes;
    628  1.10    briggs 
    629  1.23    scottr #ifdef DEBUG
    630  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    631  1.37    scottr 		printf("nubus_find_rsrc(%p, %p, 0x%x, %p)\n",
    632  1.37    scottr 		    fmt, dir, rsrcid, dirent_return);
    633  1.11    briggs #endif
    634  1.37    scottr 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN)
    635  1.12    briggs 		return -1;
    636  1.12    briggs 
    637  1.12    briggs 	entry = dir->curr_ent;
    638  1.11    briggs 	do {
    639  1.37    scottr 		byte = nubus_read_1(bst, bsh, lanes, entry);
    640  1.23    scottr #ifdef DEBUG
    641  1.12    briggs 		if (nubus_debug & NDB_FOLLOW)
    642  1.26  christos 			printf("\tFound rsrc 0x%x.\n", byte);
    643  1.11    briggs #endif
    644  1.11    briggs 		if (byte == rsrcid) {
    645  1.11    briggs 			dirent_return->myloc = entry;
    646  1.11    briggs 			dirent_return->rsrc_id = rsrcid;
    647  1.37    scottr 			entry = nubus_read_4(bst, bsh, lanes, entry);
    648  1.11    briggs 			dirent_return->offset = (entry & 0x00ffffff);
    649  1.11    briggs 			return 1;
    650  1.11    briggs 		}
    651  1.11    briggs 		if (byte == 0xff) {
    652  1.11    briggs 			entry = dir->dirbase;
    653  1.11    briggs 		} else {
    654  1.37    scottr 			entry = nubus_adjust_ptr(lanes, entry, 4);
    655  1.11    briggs 		}
    656  1.37    scottr 	} while (entry != (u_long)dir->curr_ent);
    657  1.11    briggs 	return 0;
    658   1.1    briggs }
    659   1.1    briggs 
    660  1.11    briggs int
    661  1.37    scottr nubus_get_ind_data(bst, bsh, fmt, dirent, data_return, nbytes)
    662  1.37    scottr 	bus_space_tag_t bst;
    663  1.37    scottr 	bus_space_handle_t bsh;
    664  1.37    scottr 	nubus_slot *fmt;
    665  1.11    briggs 	nubus_dirent *dirent;
    666  1.11    briggs 	caddr_t data_return;
    667  1.11    briggs 	int nbytes;
    668  1.11    briggs {
    669  1.37    scottr 	u_long loc;
    670  1.37    scottr 	u_int8_t lanes = fmt->bytelanes;
    671   1.1    briggs 
    672  1.23    scottr #ifdef DEBUG
    673  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    674  1.37    scottr 		printf("nubus_get_ind_data(%p, %p, %p, %d).\n",
    675  1.37    scottr 		    fmt, dirent, data_return, nbytes);
    676  1.12    briggs #endif
    677  1.11    briggs 	if ((loc = dirent->offset) & 0x800000) {
    678  1.11    briggs 		loc |= 0xff000000;
    679  1.11    briggs 	}
    680  1.37    scottr 	loc = nubus_adjust_ptr(lanes, dirent->myloc, loc);
    681   1.1    briggs 
    682  1.11    briggs 	while (nbytes--) {
    683  1.37    scottr 		*data_return++ = nubus_read_1(bst, bsh, lanes, loc);
    684  1.37    scottr 		loc = nubus_adjust_ptr(lanes, loc, 1);
    685   1.1    briggs 	}
    686  1.11    briggs 	return 1;
    687   1.1    briggs }
    688   1.1    briggs 
    689  1.11    briggs int
    690  1.37    scottr nubus_get_c_string(bst, bsh, fmt, dirent, data_return, max_bytes)
    691  1.37    scottr 	bus_space_tag_t bst;
    692  1.37    scottr 	bus_space_handle_t bsh;
    693  1.37    scottr 	nubus_slot *fmt;
    694  1.11    briggs 	nubus_dirent *dirent;
    695  1.11    briggs 	caddr_t data_return;
    696  1.11    briggs 	int max_bytes;
    697   1.1    briggs {
    698  1.37    scottr 	u_long loc;
    699  1.37    scottr 	u_int8_t lanes = fmt->bytelanes;
    700  1.10    briggs 
    701  1.23    scottr #ifdef DEBUG
    702  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    703  1.37    scottr 		printf("nubus_get_c_string(%p, %p, %p, %d).\n",
    704  1.37    scottr 		    fmt, dirent, data_return, max_bytes);
    705  1.12    briggs #endif
    706  1.37    scottr 	if ((loc = dirent->offset) & 0x800000)
    707  1.11    briggs 		loc |= 0xff000000;
    708  1.37    scottr 
    709  1.37    scottr 	loc = nubus_adjust_ptr(lanes, dirent->myloc, loc);
    710  1.10    briggs 
    711  1.11    briggs 	*data_return = '\0';
    712  1.11    briggs 	while (max_bytes--) {
    713  1.37    scottr 		if ((*data_return++ =
    714  1.37    scottr 		    nubus_read_1(bst, bsh, lanes, loc)) == 0)
    715  1.11    briggs 			return 1;
    716  1.37    scottr 		loc = nubus_adjust_ptr(lanes, loc, 1);
    717  1.11    briggs 	}
    718  1.50    briggs 	*(data_return-1) = '\0';
    719  1.11    briggs 	return 0;
    720   1.1    briggs }
    721   1.2    briggs 
    722  1.11    briggs static char	*huh = "???";
    723   1.3    briggs 
    724  1.11    briggs char *
    725  1.37    scottr nubus_get_vendor(bst, bsh, fmt, rsrc)
    726  1.37    scottr 	bus_space_tag_t bst;
    727  1.37    scottr 	bus_space_handle_t bsh;
    728  1.37    scottr 	nubus_slot *fmt;
    729  1.37    scottr 	int rsrc;
    730  1.37    scottr {
    731  1.37    scottr 	static char str_ret[64];
    732  1.37    scottr 	nubus_dir dir;
    733  1.37    scottr 	nubus_dirent ent;
    734   1.3    briggs 
    735  1.23    scottr #ifdef DEBUG
    736  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    737  1.37    scottr 		printf("nubus_get_vendor(%p, 0x%x).\n", fmt, rsrc);
    738  1.12    briggs #endif
    739  1.37    scottr 	nubus_get_main_dir(fmt, &dir);
    740  1.37    scottr 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, 1, &ent) <= 0)
    741  1.11    briggs 		return huh;
    742  1.37    scottr 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    743   1.2    briggs 
    744  1.37    scottr 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, NUBUS_RSRC_VENDORINFO, &ent)
    745  1.37    scottr 	    <= 0)
    746  1.11    briggs 		return huh;
    747  1.37    scottr 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    748  1.10    briggs 
    749  1.37    scottr 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, rsrc, &ent) <= 0)
    750  1.11    briggs 		return huh;
    751  1.10    briggs 
    752  1.37    scottr 	nubus_get_c_string(bst, bsh, fmt, &ent, str_ret, 64);
    753   1.2    briggs 
    754  1.11    briggs 	return str_ret;
    755   1.2    briggs }
    756   1.2    briggs 
    757  1.11    briggs char *
    758  1.37    scottr nubus_get_card_name(bst, bsh, fmt)
    759  1.37    scottr 	bus_space_tag_t bst;
    760  1.37    scottr 	bus_space_handle_t bsh;
    761  1.37    scottr 	nubus_slot *fmt;
    762   1.2    briggs {
    763  1.37    scottr 	static char name_ret[64];
    764  1.37    scottr 	nubus_dir dir;
    765  1.37    scottr 	nubus_dirent ent;
    766   1.2    briggs 
    767  1.23    scottr #ifdef DEBUG
    768  1.12    briggs 	if (nubus_debug & NDB_FOLLOW)
    769  1.37    scottr 		printf("nubus_get_card_name(%p).\n", fmt);
    770  1.12    briggs #endif
    771  1.37    scottr 	nubus_get_main_dir(fmt, &dir);
    772   1.2    briggs 
    773  1.37    scottr 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, 1, &ent) <= 0)
    774  1.11    briggs 		return huh;
    775   1.2    briggs 
    776  1.37    scottr 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    777   1.2    briggs 
    778  1.37    scottr 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
    779  1.11    briggs 		return huh;
    780   1.2    briggs 
    781  1.37    scottr 	nubus_get_c_string(bst, bsh, fmt, &ent, name_ret, 64);
    782   1.2    briggs 
    783  1.11    briggs 	return name_ret;
    784   1.2    briggs }
    785  1.46    briggs 
    786  1.46    briggs #ifdef DEBUG
    787  1.46    briggs void
    788  1.46    briggs nubus_scan_slot(bst, slotno)
    789  1.46    briggs 	bus_space_tag_t bst;
    790  1.46    briggs 	int slotno;
    791  1.46    briggs {
    792  1.46    briggs 	int i=0, state=0;
    793  1.46    briggs 	char twirl[] = "-\\|/";
    794  1.46    briggs 	bus_space_handle_t sc_bsh;
    795  1.46    briggs 
    796  1.46    briggs 	if (bus_space_map(bst, NUBUS_SLOT2PA(slotno), NBMEMSIZE, 0, &sc_bsh)) {
    797  1.46    briggs 		printf("nubus_scan_slot: failed to map slot %d\n", slotno);
    798  1.46    briggs 		return;
    799  1.46    briggs 	}
    800  1.46    briggs 
    801  1.46    briggs 	printf("Scanning slot %c for accessible regions:\n",
    802  1.46    briggs 		slotno == 9 ? '9' : slotno - 10 + 'A');
    803  1.46    briggs 	for (i=0 ; i<NBMEMSIZE; i++) {
    804  1.46    briggs 		if (mac68k_bus_space_probe(bst, sc_bsh, i, 1)) {
    805  1.46    briggs 			if (state == 0) {
    806  1.46    briggs 				printf("\t0x%x-", i);
    807  1.46    briggs 				state = 1;
    808  1.46    briggs 			}
    809  1.46    briggs 		} else {
    810  1.46    briggs 			if (state) {
    811  1.46    briggs 				printf("0x%x\n", i);
    812  1.46    briggs 				state = 0;
    813  1.46    briggs 			}
    814  1.46    briggs 		}
    815  1.46    briggs 		if (i%100 == 0) {
    816  1.46    briggs 			printf("%c\b", twirl[(i/100)%4]);
    817  1.46    briggs 		}
    818  1.46    briggs 	}
    819  1.46    briggs 	if (state) {
    820  1.46    briggs 		printf("0x%x\n", i);
    821  1.46    briggs 	}
    822  1.46    briggs 	return;
    823  1.46    briggs }
    824  1.46    briggs #endif
    825