/*-
* Copyright (c) 2007 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Tohru Nishimura.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
bound = 1000 * timo;
printf("recving with %u sec. timeout\n", timo);
again:
rxd = &l->rxd[l->rx];
do {
inv(rxd, sizeof(struct desc));
rxstat = le32toh(rxd->xd1);
if ((rxstat & XD1_OWN) == 0)
goto gotone;
DELAY(1000); /* 1 milli second */
} while (--bound > 0);
errno = 0;
return -1;
gotone:
if ((rxstat & XD1_OK) == 0) {
rxd->xd1 = htole32(XD1_OWN | FRAMESIZE);
wbinv(rxd, sizeof(struct desc));
l->rx ^= 1;
goto again;
}
/* good frame */
len = (rxstat & 0xfff) - 4 /* HASFCS */;
if (len > maxlen)
len = maxlen;
ptr = l->store[l->rx];
inv(ptr, len);
memcpy(buf, ptr, len);
rxd->xd1 = htole32(XD1_OWN | FRAMESIZE);
wbinv(rxd, sizeof(struct desc));
l->rx ^= 1;
CSR_WRITE(l, SIP_CR, l->cr);
return len;
}
static int
read_eeprom(struct local *l, int loc)
{
#define R110 06 /* SEEPROM READ op. */
unsigned data, v, i;
/* hold chip select */
v = MEAR_EESEL;
CSR_WRITE(l, SIP_MEAR, v);
data = (R110 << 6) | (loc & 0x3f); /* 6 bit addressing */
/* instruct R110 op. at loc in MSB first order */
for (i = (1 << 8); i != 0; i >>= 1) {
if (data & i)
v |= MEAR_EEDI;
else
v &= ~MEAR_EEDI;
CSR_WRITE(l, SIP_MEAR, v);
CSR_WRITE(l, SIP_MEAR, v | MEAR_EECLK);
DELAY(4);
CSR_WRITE(l, SIP_MEAR, v);
DELAY(4);
}
v = MEAR_EESEL;
/* read 16bit quantity in MSB first order */
data = 0;
for (i = 0; i < 16; i++) {
CSR_WRITE(l, SIP_MEAR, v | MEAR_EECLK);
DELAY(4);
data = (data << 1) | !!(CSR_READ(l, SIP_MEAR) & MEAR_EEDO);
CSR_WRITE(l, SIP_MEAR, v);
DELAY(4);
}
/* turn off chip select */
CSR_WRITE(l, SIP_MEAR, 0);
DELAY(4);
return data;
}