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