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