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