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