Home | History | Annotate | Line # | Download | only in nubus
nubus.c revision 1.11
      1 /*	$NetBSD: nubus.c,v 1.11 1995/04/29 20:23:46 briggs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Allen Briggs.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Allen Briggs.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 
     36 #include <machine/cpu.h>
     37 
     38 #include "nubus.h"
     39 
     40 extern int	matchbyname();
     41 
     42 static int	nubusprint __P((void *aux, char *name));
     43 static void	nubusattach __P((struct device *parent, struct device *self,
     44 				 void *aux));
     45 
     46 static int	probe_slot __P((int slot, nubus_slot *fmt));
     47 static u_long	IncPtr __P((nubus_slot *fmt, u_long base, long amt));
     48 static u_long	nubus_calc_CRC __P((nubus_slot *fmt));
     49 static u_char	GetByte __P((nubus_slot *fmt, u_long ptr));
     50 static u_short	GetWord __P((nubus_slot *fmt, u_long ptr));
     51 static u_long	GetLong __P((nubus_slot *fmt, u_long ptr));
     52 
     53 struct cfdriver nubuscd = {
     54 	NULL, "nubus", matchbyname, nubusattach,
     55 	DV_DULL, sizeof(struct nubus_softc), 1
     56 };
     57 
     58 static void
     59 nubusattach(parent, self, aux)
     60 	struct	device	*parent, *self;
     61 	void		*aux;
     62 {
     63 	nubus_slot	fmtblock;
     64 	int		i;
     65 
     66 	printf("\n");
     67 
     68 	for ( i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
     69 		if (probe_slot(i, &fmtblock)) {
     70 			config_found(self, &fmtblock, nubusprint);
     71 		}
     72 	}
     73 }
     74 
     75 static int
     76 nubusprint(aux, name)
     77 	void	*aux;
     78 	char	*name;
     79 {
     80 	nubus_slot	*fmt;
     81 	int		slot;
     82 	char		*info;
     83 
     84 	fmt = (nubus_slot *) aux;
     85 	if (name) {
     86 		printf("%s: Card in slot %d: Name: %s", name, fmt->slot,
     87 				nubus_get_card_name(fmt));
     88 		printf("Vendor: %s, \n",
     89 				nubus_get_vendor(fmt, NUBUS_RSRC_VEND_ID));
     90 		printf("Part: %s.\n",
     91 				nubus_get_vendor(fmt, NUBUS_RSRC_VEND_PART));
     92 	}
     93 	return (UNCONF);
     94 }
     95 
     96 /*
     97  * Probe a given nubus slot.  If a card is there and we can get the
     98  * format block from it's clutching decl. ROMs, fill the format block
     99  * and return non-zero.  If we can't find a card there with a valid
    100  * decl. ROM, return 0.
    101  *
    102  * First, we check to see if we can access the memory at the tail
    103  * end of the slot.  If so, then we check for a bytelanes byte.  We
    104  * could probably just return a failure status if we bus error on
    105  * the first try, but there really is little reason not to go ahead
    106  * and check the other three locations in case there's a wierd card
    107  * out there.
    108  *
    109  * Checking for a card involves locating the "bytelanes" byte which
    110  * tells us how to interpret the declaration ROM's data.  The format
    111  * block is at the top of the card's standard memory space and the
    112  * bytelanes byte is at the end of that block.
    113  *
    114  * After some inspection of the bytelanes byte, it appears that it
    115  * takes the form 0xXY where Y is a bitmask of the bytelanes in use
    116  * and X is a bitmask of the lanes to ignore.  Hence, (X ^ Y) == 0
    117  * and (less obviously), Y will have the upper N bits clear if it is
    118  * found N bytes from the last possible location.  Both that and
    119  * the exclusive-or check are made.
    120  *
    121  * If a valid
    122  */
    123 static u_char	nbits[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    124 static int
    125 probe_slot(slot, fmt)
    126 	int		slot;
    127 	nubus_slot	*fmt;
    128 {
    129 	caddr_t		rom_probe;
    130 	u_long		hdr;
    131 	u_char		data;
    132 	int		hdr_size, i;
    133 
    134 	fmt->bytelanes = 0;
    135 	fmt->slot = (u_long)slot;
    136 
    137 	rom_probe = (caddr_t) (NUBUS_SLOT_TO_BASE(fmt->slot) + NBMEMSIZE);
    138 
    139 	for (i = 4; i && (fmt->bytelanes == 0); i--) {
    140 
    141 		rom_probe--;
    142 
    143 		if (badbaddr(rom_probe))
    144 			continue;
    145 
    146 		if ((data = *rom_probe) == 0)
    147 			continue;
    148 
    149 		if (   ((((data & 0xf0) >> 4) ^ (data & 0x0f)) == 0x0f)
    150 		    && ((data & 0x0f) < (1 << i)) ) {
    151 			fmt->bytelanes = data;
    152 			fmt->step = nbits[(data & 0x0f)];
    153 		}
    154 	}
    155 #ifdef DEBUG
    156 	if (fmt->bytelanes == 0)
    157 		printf("bytelanes not found for slot 0x%x.\n", slot);
    158 #endif
    159 
    160 	if (fmt->bytelanes == 0)
    161 		return 0;
    162 
    163 #ifdef DEBUG
    164 	printf("bytelanes of 0x%x found for slot 0x%x.\n",
    165 		fmt->bytelanes, slot);
    166 #endif
    167 
    168 	hdr_size = 20;
    169 
    170 	/*
    171 	 * Go ahead and attempt to load format header.
    172 	 * First, we need to find the first byte beyond memory that
    173 	 * would be valid.  This is necessary for NUBUS_ROM_offset()
    174 	 * to work.
    175 	 */
    176 	hdr = NUBUS_SLOT_TO_BASE(fmt->slot) + NBMEMSIZE;
    177 	i = 0x10 | (fmt->bytelanes & 0x0f);
    178 	while ((i & 1) == 0) {
    179 		hdr++;
    180 		i >>= 1;
    181 	}
    182 	fmt->top = hdr;
    183 	hdr = IncPtr(fmt, hdr, -hdr_size);
    184 #ifdef DEBUG
    185 	printf("fmt->top is 0x%x, that minus 0x%x puts us at 0x%x.\n",
    186 		fmt->top, hdr_size, hdr);
    187 #if 0
    188 	for (i=1 ; i < 8 ; i++) {
    189 		printf("0x%x - 0x%x = 0x%x, + 0x%x = 0x%x.\n",
    190 			hdr, i, IncPtr(fmt, hdr, -i),
    191 			     i, IncPtr(fmt, hdr,  i));
    192 	}
    193 #endif
    194 #endif
    195 
    196 	fmt->directory_offset = 0xff000000 | GetLong(fmt, hdr);
    197 	hdr = IncPtr(fmt, hdr, 4);
    198 	fmt->length = GetLong(fmt, hdr);
    199 	hdr = IncPtr(fmt, hdr, 4);
    200 	fmt->crc = GetLong(fmt, hdr);
    201 	hdr = IncPtr(fmt, hdr, 4);
    202 	fmt->revision_level = GetByte(fmt, hdr);
    203 	hdr = IncPtr(fmt, hdr, 1);
    204 	fmt->format = GetByte(fmt, hdr);
    205 	hdr = IncPtr(fmt, hdr, 1);
    206 	fmt->test_pattern = GetLong(fmt, hdr);
    207 
    208 #if DEBUG
    209 printf("Directory offset 0x%x\t", fmt->directory_offset);
    210 printf("Length 0x%x\t", fmt->length);
    211 printf("CRC 0x%x\n", fmt->crc);
    212 printf("Revision level 0x%x\t", fmt->revision_level);
    213 printf("Format 0x%x\t", fmt->format);
    214 printf("Test Pattern 0x%x\n", fmt->test_pattern);
    215 #endif
    216 
    217 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
    218 		printf("Nubus--test pattern invalid:\n");
    219 		printf("       slot 0x%x, bytelanes 0x%x?\n",
    220 			fmt->slot, fmt->bytelanes);
    221 		printf("       read test 0x%x, compare with 0x%x.\n",
    222 			fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
    223 		return 0;
    224 	}
    225 
    226 	/* Perform CRC */
    227 	if (fmt->crc != nubus_calc_CRC(fmt)) {
    228 		printf("Nubus--crc check failed, slot 0x%x.\n",
    229 			fmt->slot);
    230 		return 0;
    231 	}
    232 
    233 	return 1;
    234 }
    235 
    236 /*
    237  * Compute byte offset on card, taking into account bytelanes.
    238  * Base must be on a valid bytelane for this function to work.
    239  * Return the new address.
    240  *
    241  * XXX -- There has GOT to be a better way to do this.
    242  */
    243 static u_long
    244 IncPtr(fmt, base, amt)
    245 	nubus_slot	*fmt;
    246 	u_long		base;
    247 	long		amt;
    248 {
    249 	u_char 	b, t;
    250 
    251 	if (!amt)
    252 		return base;
    253 
    254 	if (amt < 0) {
    255 		amt = -amt;
    256 		b = fmt->bytelanes;
    257 		t = (b << 4);
    258 		b <<= (3 - (base & 0x3));
    259 		while (amt) {
    260 			b <<= 1;
    261 			if (b == t)
    262 				b = fmt->bytelanes;
    263 			if (b & 0x08)
    264 				amt--;
    265 			base--;
    266 		}
    267 		return base;
    268 	}
    269 
    270 	t = (fmt->bytelanes & 0xf) | 0x10;
    271 	b = t >> (base & 0x3);
    272 	while (amt) {
    273 		b >>= 1;
    274 		if (b == 1)
    275 			b = t;
    276 		if (b & 1)
    277 			amt--;
    278 		base++;
    279 	}
    280 
    281 	return base;
    282 }
    283 
    284 static u_long
    285 nubus_calc_CRC(fmt)
    286 	nubus_slot	*fmt;
    287 {
    288 #if 0
    289 	u_long	base, ptr, crc_loc, sum;
    290 	int	i;
    291 
    292 	base = fmt->top;
    293 	crc_loc = NUBUS_ROM_offset(fmt, base, -12);
    294 	ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
    295 
    296 	sum = 0;
    297 	while (ptr < base)
    298 		roll #1, sum
    299 		if (ptr == crc_loc) {
    300 			roll #3, sum
    301 			ptr = IncPtr(fmt, ptr, 3);
    302 		} else {
    303 			sum += GetByte(fmt, ptr);
    304 		}
    305 		ptr = IncPtr(fmt, ptr, 1);
    306 	}
    307 
    308 	return sum;
    309 #endif
    310 	return fmt->crc;
    311 }
    312 
    313 static u_char
    314 GetByte(fmt, ptr)
    315 	nubus_slot	*fmt;
    316 	u_long		ptr;
    317 {
    318 	return *(caddr_t)ptr;
    319 }
    320 
    321 static u_short
    322 GetWord(fmt, ptr)
    323 	nubus_slot	*fmt;
    324 	u_long		ptr;
    325 {
    326 	u_short	s;
    327 
    328 	s = (GetByte(fmt, ptr) << 8);
    329 	ptr = IncPtr(fmt, ptr, 1);
    330 	s |= GetByte(fmt, ptr);
    331 	return s;
    332 }
    333 
    334 static u_long
    335 GetLong(fmt, ptr)
    336 	nubus_slot	*fmt;
    337 	u_long		ptr;
    338 {
    339 	register u_long l;
    340 	register int	i;
    341 
    342 	l = 0;
    343 	for ( i = 0; i < 4; i++) {
    344 		l = (l << 8) | GetByte(fmt, ptr);
    345 		ptr = IncPtr(fmt, ptr, 1);
    346 	}
    347 	return l;
    348 }
    349 
    350 void
    351 nubus_get_main_dir(slot, dir_return)
    352 	nubus_slot	*slot;
    353 	nubus_dir	*dir_return;
    354 {
    355 	u_long		off;
    356 
    357 	dir_return->dirbase = IncPtr(slot, slot->top,
    358 					slot->directory_offset - 20);
    359 	dir_return->curr_ent = dir_return->dirbase;
    360 }
    361 
    362 int
    363 nubus_find_rsrc(slot, dir, rsrcid, dirent_return)
    364 	nubus_slot	*slot;
    365 	nubus_dir	*dir;
    366 	u_int8_t	rsrcid;
    367 	nubus_dirent	*dirent_return;
    368 {
    369 	u_long		entry;
    370 	u_char		byte;
    371 
    372 	entry = dir->curr_ent;
    373 #if DEBUG
    374 printf("nubus_find_rsrc (0x%x,0x%x)\n", dir, rsrcid);
    375 #endif
    376 	do {
    377 		byte = GetByte(slot, entry);
    378 #if DEBUG
    379 printf("\tFound rsrc 0x%x.\n", byte);
    380 #endif
    381 		if (byte == rsrcid) {
    382 			dirent_return->myloc = entry;
    383 			dirent_return->rsrc_id = rsrcid;
    384 			entry = GetLong(slot, entry);
    385 			dirent_return->offset = (entry & 0x00ffffff);
    386 			return 1;
    387 		}
    388 		if (byte == 0xff) {
    389 			entry = dir->dirbase;
    390 		} else {
    391 			entry = IncPtr(slot, entry, 4);
    392 		}
    393 	} while (entry != (u_long) dir->curr_ent);
    394 	return 0;
    395 }
    396 
    397 void
    398 nubus_get_dir_from_rsrc(slot, dirent, dir_return)
    399 	nubus_slot	*slot;
    400 	nubus_dirent	*dirent;
    401 	nubus_dir	*dir_return;
    402 {
    403 	u_long	loc;
    404 
    405 	if ((loc = dirent->offset) & 0x800000) {
    406 		loc |= 0xff000000;
    407 	}
    408 	dir_return->dirbase = IncPtr(slot, dirent->myloc, loc);
    409 	dir_return->curr_ent = dir_return->dirbase;
    410 }
    411 
    412 int
    413 nubus_get_ind_data(slot, dirent, data_return, nbytes)
    414 	nubus_slot *slot;
    415 	nubus_dirent *dirent;
    416 	caddr_t data_return;
    417 	int nbytes;
    418 {
    419 	u_long	loc;
    420 
    421 	if ((loc = dirent->offset) & 0x800000) {
    422 		loc |= 0xff000000;
    423 	}
    424 	loc = IncPtr(slot, dirent->myloc, loc);
    425 
    426 	while (nbytes--) {
    427 		*data_return++ = GetByte(slot, loc);
    428 		loc = IncPtr(slot, loc, 1);
    429 	}
    430 	return 1;
    431 }
    432 
    433 int
    434 nubus_get_c_string(slot, dirent, data_return, max_bytes)
    435 	nubus_slot *slot;
    436 	nubus_dirent *dirent;
    437 	caddr_t data_return;
    438 	int max_bytes;
    439 {
    440 	u_long	loc;
    441 
    442 	if ((loc = dirent->offset) & 0x800000) {
    443 		loc |= 0xff000000;
    444 	}
    445 	loc = IncPtr(slot, dirent->myloc, loc);
    446 
    447 	*data_return = '\0';
    448 	while (max_bytes--) {
    449 		if ((*data_return++ = GetByte(slot, loc)) == 0)
    450 			return 1;
    451 		loc = IncPtr(slot, loc, 1);
    452 	}
    453 	return 0;
    454 }
    455 
    456 static char	*huh = "???";
    457 
    458 char *
    459 nubus_get_vendor(slot, rsrc)
    460 	nubus_slot	*slot;
    461 	int		rsrc;
    462 {
    463 static	char		str_ret[64];
    464 	nubus_dir	dir;
    465 	nubus_dirent	ent;
    466 
    467 	nubus_get_main_dir(slot, &dir);
    468 	if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
    469 		return huh;
    470 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    471 
    472 	if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_VENDORINFO, &ent) <= 0)
    473 		return huh;
    474 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    475 
    476 	if (nubus_find_rsrc(slot, &dir, rsrc, &ent) <= 0)
    477 		return huh;
    478 
    479 	nubus_get_c_string(slot, &ent, str_ret, 64);
    480 
    481 	return str_ret;
    482 }
    483 
    484 char *
    485 nubus_get_card_name(slot)
    486 	nubus_slot	*slot;
    487 {
    488 static	char		name_ret[64];
    489 	nubus_dir	dir;
    490 	nubus_dirent	ent;
    491 
    492 	nubus_get_main_dir(slot, &dir);
    493 
    494 	if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
    495 		return huh;
    496 
    497 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    498 
    499 	if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
    500 		return huh;
    501 
    502 	nubus_get_c_string(slot, &ent, name_ret, 64);
    503 
    504 	return name_ret;
    505 }
    506