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