/*
* Copyright (c) 1995, 1996 Allen Briggs. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Allen Briggs.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
na_args.slot = i;
na_args.na_tag = bst = mba->mba_bst;
na_args.na_dmat = mba->mba_dmat;
if (bus_space_map(bst,
NUBUS_SLOT2PA(na_args.slot), NBMEMSIZE, 0, &bsh)) {
#ifdef DEBUG
if (nubus_debug & NDB_PROBE)
printf("%s: failed to map slot %x, "
"address %p (in use?)\n",
device_xname(self), i,
(void *)NUBUS_SLOT2PA(i));
#endif
continue;
}
if (nubus_probe_slot(bst, bsh, i, &fmtblock) <= 0) {
notfound:
bus_space_unmap(bst, bsh, NBMEMSIZE);
continue;
}
rsrcid = 0x80;
lanes = fmtblock.bytelanes;
nubus_get_main_dir(&fmtblock, &dir);
/*
* Get the resource for the first function on the card.
* This is assumed to be at resource ID 0x80. If we can
* not find this entry (as we can not on some video cards),
* check to see if we can get a different ID from the list
* of video resources given to us by the booter. If that
* doesn't work either, take the first resource following
* the board resource.
* If we only find a board resource, report that.
* There are cards that do not have anything else; their
* driver then has to match on the board resource and
* the card name.
*/
if (nubus_find_rsrc(bst, bsh,
&fmtblock, &dir, rsrcid, &dirent) <= 0) {
if ((rsrcid = nubus_video_resource(i)) == -1) {
int has_board_rsrc = 0;
/*
* Since nubus_find_rsrc failed, the directory
* is back at its base.
*/
entry = dir.curr_ent;
/*
* All nubus cards should have a board
* resource, but be sure that's what it
* is before we skip it, and note the fact.
*/
rsrcid = nubus_read_1(bst, bsh,
lanes, entry);
if (rsrcid == 0x1) {
has_board_rsrc = 1;
entry = nubus_adjust_ptr(lanes,
dir.curr_ent, 4);
}
rsrcid = nubus_read_1(bst, bsh, lanes, entry);
/* end of chain? */
if (rsrcid == 0xff) {
if (!has_board_rsrc)
goto notfound;
else
rsrcid = 0x01;
}
#ifdef DEBUG
if (nubus_debug & NDB_FOLLOW)
printf("\tUsing rsrc 0x%x.\n", rsrcid);
#endif
}
/*
* Try to find the resource passed by the booter
* or the one we just tracked down.
*/
if (nubus_find_rsrc(bst, bsh,
&fmtblock, &dir, rsrcid, &dirent) <= 0)
goto notfound;
}
/*
* If this is a display card, try to pull out the correct
* display mode as passed by the booter.
*/
if (slottype.category == NUBUS_CATEGORY_DISPLAY) {
int r;
static int
nubus_video_resource(int slot)
{
extern u_int16_t mac68k_vrsrc_vec[];
int i;
for (i = 0 ; i < 6 ; i++)
if ((mac68k_vrsrc_vec[i] & 0xff) == slot)
return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
return (-1);
}
/*
* Probe a given nubus slot. If a card is there and we can get the
* format block from its clutching decl. ROMs, fill the format block
* and return non-zero. If we can't find a card there with a valid
* decl. ROM, return 0.
*
* First, we check to see if we can access the memory at the tail
* end of the slot. If so, then we check for a bytelanes byte. We
* could probably just return a failure status if we bus error on
* the first try, but there really is little reason not to go ahead
* and check the other three locations in case there's a weird card
* out there.
*
* Checking for a card involves locating the "bytelanes" byte which
* tells us how to interpret the declaration ROM's data. The format
* block is at the top of the card's standard memory space and the
* bytelanes byte is at the end of that block.
*
* After some inspection of the bytelanes byte, it appears that it
* takes the form 0xXY where Y is a bitmask of the bytelanes in use
* and X is a bitmask of the lanes to ignore. Hence, (X ^ Y) == 0
* and (less obviously), Y will have the upper N bits clear if it is
* found N bytes from the last possible location. Both that and
* the exclusive-or check are made.
*
* If a valid
*/
static u_int8_t nbits[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
static int
nubus_probe_slot(bus_space_tag_t bst, bus_space_handle_t bsh, int slot,
nubus_slot *fmt)
{
u_long ofs, hdr;
int i, j, found, hdr_size;
u_int8_t lanes;
/*
* The idea behind this glorious work of art is to probe for only
* valid bytelanes values at appropriate locations (see DC&D p. 159
* for a list). Note the pattern: the first 8 values are at offset
* 0xffffff in the slot's space; the next 4 values at 0xfffffe; the
* next 2 values at 0xfffffd; and the last one at 0xfffffc.
*
* The nested loops implement an efficient search of this space,
* probing first for a valid address, then checking for each of the
* valid bytelanes values at that address.
*/
ofs = NBMEMSIZE;
lanes = 0xf;
for (j = 8, found = 0; j > 0 && !found; j >>= 1) {
ofs--;
for (i = j; i > 0; i--, lanes--) {
if (!mac68k_bus_space_probe(bst, bsh, ofs, 1)) {
lanes -= i;
break;
}
if (bus_space_read_1(bst, bsh, ofs) ==
(((~lanes & 0xf) << 4) | lanes)) {
found = 1;
break;
}
}
}
if (!found) {
#ifdef DEBUG
if (nubus_debug & NDB_PROBE)
printf("bytelanes not found for slot %x\n", slot);
#endif
return 0;
}
fmt->bytelanes = lanes;
fmt->step = nbits[(lanes & 0x0f)];
fmt->slot = slot; /* XXX redundant; get rid of this someday */
#ifdef DEBUG
if (nubus_debug & NDB_PROBE)
printf("bytelanes of 0x%x found for slot 0x%x.\n",
fmt->bytelanes, slot);
#endif
/*
* Go ahead and attempt to load format header.
* First, we need to find the first byte beyond memory that
* would be valid. This is necessary for NUBUS_ROM_offset()
* to work.
*/
hdr = NBMEMSIZE;
hdr_size = 20;
i = 0x10 | (lanes & 0x0f);
while ((i & 1) == 0) {
hdr++;
i >>= 1;
}
fmt->top = hdr;
hdr = nubus_adjust_ptr(lanes, hdr, -hdr_size);
#ifdef DEBUG
if (nubus_debug & NDB_PROBE)
printf("fmt->top is 0x%lx, that minus 0x%x puts us at 0x%lx.\n",
fmt->top, hdr_size, hdr);
if (nubus_debug & NDB_ARITH)
for (i = 1 ; i < 8 ; i++)
printf("0x%lx - 0x%x = 0x%lx, + 0x%x = 0x%lx.\n",
hdr, i, nubus_adjust_ptr(lanes, hdr, -i),
i, nubus_adjust_ptr(lanes, hdr, i));
#endif
sum = 0;
while (ptr < base)
roll #1, sum
if (ptr == crc_loc) {
roll #3, sum
ptr = nubus_adjust_ptr(lanes, ptr, 3);
} else {
sum += nubus_read_1(bst, bsh, lanes, ptr);
}
ptr = nubus_adjust_ptr(lanes, ptr, 1);
}
return sum;
#endif
return fmt->crc;
}
/*
* Compute byte offset on card, taking into account bytelanes.
* Base must be on a valid bytelane for this function to work.
* Return the new address.
*
* XXX -- There has GOT to be a better way to do this.
*/
static u_long
nubus_adjust_ptr(u_int8_t lanes, u_long base, long amt)
{
u_int8_t b, t;
if (!amt)
return base;
if (amt < 0) {
amt = -amt;
b = lanes;
t = (b << 4);
b <<= (3 - (base & 0x3));
while (amt) {
b <<= 1;
if (b == t)
b = lanes;
if (b & 0x08)
amt--;
base--;
}
return base;
}
t = (lanes & 0xf) | 0x10;
b = t >> (base & 0x3);
while (amt) {
b >>= 1;
if (b == 1)
b = t;
if (b & 1)
amt--;
base++;
}