Home | History | Annotate | Line # | Download | only in nubus
nubus.c revision 1.18
      1 /*	$NetBSD: nubus.c,v 1.18 1996/04/04 06:59:29 scottr 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 <vm/vm.h>
     39 
     40 #include "nubus.h"
     41 
     42 #if DEBUG
     43 static int	nubus_debug = 0x01;
     44 #define NDB_PROBE	0x1
     45 #define NDB_FOLLOW	0x2
     46 #define NDB_ARITH	0x4
     47 #endif
     48 
     49 static int	nubusprint __P((void *aux, char *name));
     50 static void	nubusattach __P((struct device *parent, struct device *self,
     51 				 void *aux));
     52 
     53 static int	probe_slot __P((int slot, nubus_slot *fmt));
     54 static u_long	IncPtr __P((nubus_slot *fmt, u_long base, long amt));
     55 static u_long	nubus_calc_CRC __P((nubus_slot *fmt));
     56 static u_char	GetByte __P((nubus_slot *fmt, u_long ptr));
     57 #ifdef notyet
     58 /* unused */ static u_short	GetWord __P((nubus_slot *fmt, u_long ptr));
     59 #endif
     60 static u_long	GetLong __P((nubus_slot *fmt, u_long ptr));
     61 
     62 extern int matchbyname();	/* XXX - this really ought to be elsewhere! */
     63 
     64 struct cfattach nubus_ca = {
     65 	sizeof(struct nubus_softc), matchbyname, nubusattach
     66 };
     67 
     68 struct cfdriver nubus_cd = {
     69 	NULL, "nubus", DV_DULL, 1
     70 };
     71 
     72 static void
     73 nubusattach(parent, self, aux)
     74 	struct	device	*parent, *self;
     75 	void		*aux;
     76 {
     77 	extern u_int32_t	mac68k_vidlog;
     78 	nubus_slot		fmtblock;
     79 	int			i;
     80 
     81 	printf("\n");
     82 
     83 	/*
     84 	 * Kludge for internal video.
     85 	 */
     86 	if (mac68k_vidlog) {
     87 		int	int_video_slot = NUBUS_INT_VIDEO_PSUEDO_SLOT;
     88 
     89 		fmtblock.top = NUBUS_SLOT_TO_BASE(int_video_slot);
     90 		fmtblock.slot = int_video_slot;
     91 		fmtblock.bytelanes = 0x0F;
     92 		fmtblock.step = 4;
     93 		fmtblock.test_pattern = ~NUBUS_ROM_TEST_PATTERN;
     94 		fmtblock.format = 1;
     95 		fmtblock.revision_level = 1;
     96 		fmtblock.crc = 1;
     97 		fmtblock.length = 0;
     98 		fmtblock.directory_offset = 0;
     99 		config_found(self, &fmtblock, nubusprint);
    100 	}
    101 
    102 	for ( i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
    103 		if (probe_slot(i, &fmtblock)) {
    104 			config_found(self, &fmtblock, nubusprint);
    105 		}
    106 	}
    107 }
    108 
    109 static int
    110 nubusprint(aux, name)
    111 	void	*aux;
    112 	char	*name;
    113 {
    114 	nubus_slot	*fmt;
    115 
    116 	fmt = (nubus_slot *) aux;
    117 	if (name) {
    118 		printf("%s: slot %x: %s ", name, fmt->slot,
    119 				nubus_get_card_name(fmt));
    120 		printf("(Vendor: %s, ",
    121 				nubus_get_vendor(fmt, NUBUS_RSRC_VEND_ID));
    122 		printf("Part: %s) ",
    123 				nubus_get_vendor(fmt, NUBUS_RSRC_VEND_PART));
    124 	}
    125 	return (UNCONF);
    126 }
    127 
    128 /*
    129  * Probe a given nubus slot.  If a card is there and we can get the
    130  * format block from it's clutching decl. ROMs, fill the format block
    131  * and return non-zero.  If we can't find a card there with a valid
    132  * decl. ROM, return 0.
    133  *
    134  * First, we check to see if we can access the memory at the tail
    135  * end of the slot.  If so, then we check for a bytelanes byte.  We
    136  * could probably just return a failure status if we bus error on
    137  * the first try, but there really is little reason not to go ahead
    138  * and check the other three locations in case there's a wierd card
    139  * out there.
    140  *
    141  * Checking for a card involves locating the "bytelanes" byte which
    142  * tells us how to interpret the declaration ROM's data.  The format
    143  * block is at the top of the card's standard memory space and the
    144  * bytelanes byte is at the end of that block.
    145  *
    146  * After some inspection of the bytelanes byte, it appears that it
    147  * takes the form 0xXY where Y is a bitmask of the bytelanes in use
    148  * and X is a bitmask of the lanes to ignore.  Hence, (X ^ Y) == 0
    149  * and (less obviously), Y will have the upper N bits clear if it is
    150  * found N bytes from the last possible location.  Both that and
    151  * the exclusive-or check are made.
    152  *
    153  * If a valid
    154  */
    155 static u_char	nbits[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    156 static int
    157 probe_slot(slot, fmt)
    158 	int		slot;
    159 	nubus_slot	*fmt;
    160 {
    161 	caddr_t		rom_probe;
    162 	u_long		hdr;
    163 	u_char		data;
    164 	int		hdr_size, i;
    165 
    166 	fmt->bytelanes = 0;
    167 	fmt->slot = (u_long)slot;
    168 
    169 	rom_probe = (caddr_t) (NUBUS_SLOT_TO_BASE(fmt->slot) + NBMEMSIZE);
    170 
    171 #ifdef DEBUG
    172 	if (nubus_debug & NDB_PROBE) {
    173 		phys = pmap_extract(pmap_kernel(), (vm_offset_t)rom_probe-1);
    174 		printf("probing slot %d, first probe at 0x%x (phys 0x%x).\n",
    175 			slot, rom_probe-1, phys);
    176 	}
    177 #endif
    178 
    179 	for (i = 4; i && (fmt->bytelanes == 0); i--) {
    180 
    181 		rom_probe--;
    182 
    183 		if (badbaddr(rom_probe))
    184 			continue;
    185 
    186 		if ((data = *rom_probe) == 0)
    187 			continue;
    188 
    189 		if (   ((((data & 0xf0) >> 4) ^ (data & 0x0f)) == 0x0f)
    190 		    && ((data & 0x0f) < (1 << i)) ) {
    191 			fmt->bytelanes = data;
    192 			fmt->step = nbits[(data & 0x0f)];
    193 		}
    194 	}
    195 #ifdef DEBUG
    196 	if (nubus_debug & NDB_PROBE)
    197 		if (fmt->bytelanes == 0)
    198 			printf("bytelanes not found for slot 0x%x.\n", slot);
    199 #endif
    200 
    201 	if (fmt->bytelanes == 0)
    202 		return 0;
    203 
    204 #ifdef DEBUG
    205 	if (nubus_debug & NDB_PROBE)
    206 		printf("bytelanes of 0x%x found for slot 0x%x (base 0x%x).\n",
    207 			fmt->bytelanes, slot, NUBUS_SLOT_TO_BASE(slot));
    208 #endif
    209 
    210 	hdr_size = 20;
    211 
    212 	/*
    213 	 * Go ahead and attempt to load format header.
    214 	 * First, we need to find the first byte beyond memory that
    215 	 * would be valid.  This is necessary for NUBUS_ROM_offset()
    216 	 * to work.
    217 	 */
    218 	hdr = NUBUS_SLOT_TO_BASE(fmt->slot) + NBMEMSIZE;
    219 	i = 0x10 | (fmt->bytelanes & 0x0f);
    220 	while ((i & 1) == 0) {
    221 		hdr++;
    222 		i >>= 1;
    223 	}
    224 	fmt->top = hdr;
    225 	hdr = IncPtr(fmt, hdr, -hdr_size);
    226 #ifdef DEBUG
    227 	if (nubus_debug & NDB_PROBE)
    228 		printf("fmt->top is 0x%x, that minus 0x%x puts us at 0x%x.\n",
    229 			fmt->top, hdr_size, hdr);
    230 #if 0
    231 	for (i=1 ; i < 8 ; i++) {
    232 		printf("0x%x - 0x%x = 0x%x, + 0x%x = 0x%x.\n",
    233 			hdr, i, IncPtr(fmt, hdr, -i),
    234 			     i, IncPtr(fmt, hdr,  i));
    235 	}
    236 #endif
    237 #endif
    238 
    239 	fmt->directory_offset = 0xff000000 | GetLong(fmt, hdr);
    240 	hdr = IncPtr(fmt, hdr, 4);
    241 	fmt->length = GetLong(fmt, hdr);
    242 	hdr = IncPtr(fmt, hdr, 4);
    243 	fmt->crc = GetLong(fmt, hdr);
    244 	hdr = IncPtr(fmt, hdr, 4);
    245 	fmt->revision_level = GetByte(fmt, hdr);
    246 	hdr = IncPtr(fmt, hdr, 1);
    247 	fmt->format = GetByte(fmt, hdr);
    248 	hdr = IncPtr(fmt, hdr, 1);
    249 	fmt->test_pattern = GetLong(fmt, hdr);
    250 
    251 #if DEBUG
    252 	if (nubus_debug & NDB_PROBE) {
    253 		printf("Directory offset 0x%x\t", fmt->directory_offset);
    254 		printf("Length 0x%x\t", fmt->length);
    255 		printf("CRC 0x%x\n", fmt->crc);
    256 		printf("Revision level 0x%x\t", fmt->revision_level);
    257 		printf("Format 0x%x\t", fmt->format);
    258 		printf("Test Pattern 0x%x\n", fmt->test_pattern);
    259 	}
    260 #endif
    261 
    262 	if ((fmt->directory_offset & 0x00ff0000) == 0) {
    263 		printf("Invalid looking directory offset (0x%x)!\n",
    264 			fmt->directory_offset);
    265 		return 0;
    266 	}
    267 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
    268 		printf("Nubus--test pattern invalid:\n");
    269 		printf("       slot 0x%x, bytelanes 0x%x?\n",
    270 			fmt->slot, fmt->bytelanes);
    271 		printf("       read test 0x%x, compare with 0x%x.\n",
    272 			fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
    273 		return 0;
    274 	}
    275 
    276 	/* Perform CRC */
    277 	if (fmt->crc != nubus_calc_CRC(fmt)) {
    278 		printf("Nubus--crc check failed, slot 0x%x.\n",
    279 			fmt->slot);
    280 		return 0;
    281 	}
    282 
    283 	return 1;
    284 }
    285 
    286 /*
    287  * Compute byte offset on card, taking into account bytelanes.
    288  * Base must be on a valid bytelane for this function to work.
    289  * Return the new address.
    290  *
    291  * XXX -- There has GOT to be a better way to do this.
    292  */
    293 static u_long
    294 IncPtr(fmt, base, amt)
    295 	nubus_slot	*fmt;
    296 	u_long		base;
    297 	long		amt;
    298 {
    299 	u_char 	b, t;
    300 
    301 	if (!amt)
    302 		return base;
    303 
    304 	if (amt < 0) {
    305 		amt = -amt;
    306 		b = fmt->bytelanes;
    307 		t = (b << 4);
    308 		b <<= (3 - (base & 0x3));
    309 		while (amt) {
    310 			b <<= 1;
    311 			if (b == t)
    312 				b = fmt->bytelanes;
    313 			if (b & 0x08)
    314 				amt--;
    315 			base--;
    316 		}
    317 		return base;
    318 	}
    319 
    320 	t = (fmt->bytelanes & 0xf) | 0x10;
    321 	b = t >> (base & 0x3);
    322 	while (amt) {
    323 		b >>= 1;
    324 		if (b == 1)
    325 			b = t;
    326 		if (b & 1)
    327 			amt--;
    328 		base++;
    329 	}
    330 
    331 	return base;
    332 }
    333 
    334 static u_long
    335 nubus_calc_CRC(fmt)
    336 	nubus_slot	*fmt;
    337 {
    338 #if 0
    339 	u_long	base, ptr, crc_loc, sum;
    340 	int	i;
    341 
    342 	base = fmt->top;
    343 	crc_loc = NUBUS_ROM_offset(fmt, base, -12);
    344 	ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
    345 
    346 	sum = 0;
    347 	while (ptr < base)
    348 		roll #1, sum
    349 		if (ptr == crc_loc) {
    350 			roll #3, sum
    351 			ptr = IncPtr(fmt, ptr, 3);
    352 		} else {
    353 			sum += GetByte(fmt, ptr);
    354 		}
    355 		ptr = IncPtr(fmt, ptr, 1);
    356 	}
    357 
    358 	return sum;
    359 #endif
    360 	return fmt->crc;
    361 }
    362 
    363 static u_char
    364 GetByte(fmt, ptr)
    365 	nubus_slot	*fmt;
    366 	u_long		ptr;
    367 {
    368 	return *(caddr_t)ptr;
    369 }
    370 
    371 #ifdef notyet
    372 /* Nothing uses this, yet */
    373 static u_short
    374 GetWord(fmt, ptr)
    375 	nubus_slot	*fmt;
    376 	u_long		ptr;
    377 {
    378 	u_short	s;
    379 
    380 	s = (GetByte(fmt, ptr) << 8);
    381 	ptr = IncPtr(fmt, ptr, 1);
    382 	s |= GetByte(fmt, ptr);
    383 	return s;
    384 }
    385 #endif
    386 
    387 static u_long
    388 GetLong(fmt, ptr)
    389 	nubus_slot	*fmt;
    390 	u_long		ptr;
    391 {
    392 	register u_long l;
    393 	register int	i;
    394 
    395 	l = 0;
    396 	for ( i = 0; i < 4; i++) {
    397 		l = (l << 8) | GetByte(fmt, ptr);
    398 		ptr = IncPtr(fmt, ptr, 1);
    399 	}
    400 	return l;
    401 }
    402 
    403 void
    404 nubus_get_main_dir(slot, dir_return)
    405 	nubus_slot	*slot;
    406 	nubus_dir	*dir_return;
    407 {
    408 #if DEBUG
    409 	if (nubus_debug & NDB_FOLLOW)
    410 		printf("nubus_get_main_dir(0x%x, 0x%x)\n",
    411 			(u_int) slot, (u_int) dir_return);
    412 #endif
    413 	dir_return->dirbase = IncPtr(slot, slot->top,
    414 					slot->directory_offset - 20);
    415 	dir_return->curr_ent = dir_return->dirbase;
    416 }
    417 
    418 int
    419 nubus_find_rsrc(slot, dir, rsrcid, dirent_return)
    420 	nubus_slot	*slot;
    421 	nubus_dir	*dir;
    422 	u_int8_t	rsrcid;
    423 	nubus_dirent	*dirent_return;
    424 {
    425 	u_long		entry;
    426 	u_char		byte;
    427 
    428 #if DEBUG
    429 	if (nubus_debug & NDB_FOLLOW)
    430 		printf("nubus_find_rsrc(0x%x, 0x%x, 0x%x, 0x%x)\n",
    431 			(u_int) slot, (u_int) dir, (u_int) rsrcid,
    432 			(u_int) dirent_return);
    433 #endif
    434 	if (slot->test_pattern != NUBUS_ROM_TEST_PATTERN)
    435 		return -1;
    436 
    437 	entry = dir->curr_ent;
    438 	do {
    439 		byte = GetByte(slot, entry);
    440 #if DEBUG
    441 		if (nubus_debug & NDB_FOLLOW)
    442 			printf("\tFound rsrc 0x%x.\n", byte);
    443 #endif
    444 		if (byte == rsrcid) {
    445 			dirent_return->myloc = entry;
    446 			dirent_return->rsrc_id = rsrcid;
    447 			entry = GetLong(slot, entry);
    448 			dirent_return->offset = (entry & 0x00ffffff);
    449 			return 1;
    450 		}
    451 		if (byte == 0xff) {
    452 			entry = dir->dirbase;
    453 		} else {
    454 			entry = IncPtr(slot, entry, 4);
    455 		}
    456 	} while (entry != (u_long) dir->curr_ent);
    457 	return 0;
    458 }
    459 
    460 void
    461 nubus_get_dir_from_rsrc(slot, dirent, dir_return)
    462 	nubus_slot	*slot;
    463 	nubus_dirent	*dirent;
    464 	nubus_dir	*dir_return;
    465 {
    466 	u_long	loc;
    467 
    468 #if DEBUG
    469 	if (nubus_debug & NDB_FOLLOW)
    470 		printf("nubus_get_dir_from_rsrc(0x%x, 0x%x, 0x%x).\n",
    471 			(u_int) slot, (u_int) dirent, (u_int) dir_return);
    472 #endif
    473 	if ((loc = dirent->offset) & 0x800000) {
    474 		loc |= 0xff000000;
    475 	}
    476 	dir_return->dirbase = IncPtr(slot, dirent->myloc, loc);
    477 	dir_return->curr_ent = dir_return->dirbase;
    478 }
    479 
    480 int
    481 nubus_get_ind_data(slot, dirent, data_return, nbytes)
    482 	nubus_slot *slot;
    483 	nubus_dirent *dirent;
    484 	caddr_t data_return;
    485 	int nbytes;
    486 {
    487 	u_long	loc;
    488 
    489 #if DEBUG
    490 	if (nubus_debug & NDB_FOLLOW)
    491 		printf("nubus_get_ind_data(0x%x, 0x%x, 0x%x, %d).\n",
    492 			(u_int) slot, (u_int) dirent, (u_int) data_return,
    493 			nbytes);
    494 #endif
    495 	if ((loc = dirent->offset) & 0x800000) {
    496 		loc |= 0xff000000;
    497 	}
    498 	loc = IncPtr(slot, dirent->myloc, loc);
    499 
    500 	while (nbytes--) {
    501 		*data_return++ = GetByte(slot, loc);
    502 		loc = IncPtr(slot, loc, 1);
    503 	}
    504 	return 1;
    505 }
    506 
    507 int
    508 nubus_get_c_string(slot, dirent, data_return, max_bytes)
    509 	nubus_slot *slot;
    510 	nubus_dirent *dirent;
    511 	caddr_t data_return;
    512 	int max_bytes;
    513 {
    514 	u_long	loc;
    515 
    516 #if DEBUG
    517 	if (nubus_debug & NDB_FOLLOW)
    518 		printf("nubus_get_c_string(0x%x, 0x%x, 0x%x, %d).\n",
    519 			(u_int) slot, (u_int) dirent, (u_int) data_return,
    520 			max_bytes);
    521 #endif
    522 	if ((loc = dirent->offset) & 0x800000) {
    523 		loc |= 0xff000000;
    524 	}
    525 	loc = IncPtr(slot, dirent->myloc, loc);
    526 
    527 	*data_return = '\0';
    528 	while (max_bytes--) {
    529 		if ((*data_return++ = GetByte(slot, loc)) == 0)
    530 			return 1;
    531 		loc = IncPtr(slot, loc, 1);
    532 	}
    533 	return 0;
    534 }
    535 
    536 static char	*huh = "???";
    537 
    538 char *
    539 nubus_get_vendor(slot, rsrc)
    540 	nubus_slot	*slot;
    541 	int		rsrc;
    542 {
    543 static	char		str_ret[64];
    544 	nubus_dir	dir;
    545 	nubus_dirent	ent;
    546 
    547 #if DEBUG
    548 	if (nubus_debug & NDB_FOLLOW)
    549 		printf("nubus_get_vendor(0x%x, 0x%x).\n", (u_int) slot, rsrc);
    550 #endif
    551 	nubus_get_main_dir(slot, &dir);
    552 	if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
    553 		return huh;
    554 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    555 
    556 	if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_VENDORINFO, &ent) <= 0)
    557 		return huh;
    558 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    559 
    560 	if (nubus_find_rsrc(slot, &dir, rsrc, &ent) <= 0)
    561 		return huh;
    562 
    563 	nubus_get_c_string(slot, &ent, str_ret, 64);
    564 
    565 	return str_ret;
    566 }
    567 
    568 char *
    569 nubus_get_card_name(slot)
    570 	nubus_slot	*slot;
    571 {
    572 static	char		name_ret[64];
    573 	nubus_dir	dir;
    574 	nubus_dirent	ent;
    575 
    576 #if DEBUG
    577 	if (nubus_debug & NDB_FOLLOW)
    578 		printf("nubus_get_card_name(0x%x).\n", (u_long) slot);
    579 #endif
    580 	nubus_get_main_dir(slot, &dir);
    581 
    582 	if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
    583 		return huh;
    584 
    585 	nubus_get_dir_from_rsrc(slot, &ent, &dir);
    586 
    587 	if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
    588 		return huh;
    589 
    590 	nubus_get_c_string(slot, &ent, name_ret, 64);
    591 
    592 	return name_ret;
    593 }
    594