/*-
* Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* 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.
*/
/*
* Device driver for National Semiconductor DS8390/WD83C690 based ethernet
* adapters.
*
* Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
*
* Copyright (C) 1993, David Greenman. This software may be used, modified,
* copied, distributed, and sold, in both source and binary form provided that
* the above copyright and these terms are retained. Under no circumstances is
* the author responsible for the proper functioning of this software, nor does
* the author assume any responsibility for damages incurred with its use.
*/
/*
* Device driver for the 3Com Etherlink II (3c503).
*/
/* Disallow wildcarded mem address. */
if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
return (0);
/* Validate the i/o base. */
for (i = 0; i < NEC_IOBASE; i++)
if (ia->ia_io[0].ir_addr == ec_iobase[i])
break;
if (i == NEC_IOBASE)
return (0);
/* Validate the mem base. */
for (i = 0; i < NEC_MEMBASE; i++) {
if (ec_membase[i] == -1)
continue;
if (ia->ia_iomem[0].ir_addr == ec_membase[i])
break;
}
if (i == NEC_MEMBASE)
return (0);
/* Attempt to map the NIC space. */
if (bus_space_map(nict, ia->ia_io[0].ir_addr + ELINK2_NIC_OFFSET,
ELINK2_NIC_PORTS, 0, &nich))
goto out;
nich_valid = 1;
/* Attempt to map the ASIC space. */
if (bus_space_map(asict, ia->ia_io[0].ir_addr + ELINK2_ASIC_OFFSET,
ELINK2_ASIC_PORTS, 0, &asich))
goto out;
asich_valid = 1;
/* Attempt to map the memory space. */
if (bus_space_map(memt, ia->ia_iomem[0].ir_addr, memsize, 0, &memh))
goto out;
memh_valid = 1;
/*
* Verify that the kernel configured I/O address matches the
* board configured I/O address.
*
* This is really only useful to see if something that looks like
* the board is there; after all, we're already talking to it at
* this point.
*/
x = bus_space_read_1(asict, asich, ELINK2_BCFR);
if (x == 0 || (x & (x - 1)) != 0)
goto out;
i = ffs(x) - 1;
if (ia->ia_io[0].ir_addr != ec_iobase[i])
goto out;
/*
* ...and for the memory address. Note we do not support
* cards configured with shared memory disabled.
*/
x = bus_space_read_1(asict, asich, ELINK2_PCFR);
if (x == 0 || (x & (x - 1)) != 0)
goto out;
i = ffs(x) - 1;
if (ia->ia_iomem[0].ir_addr != ec_membase[i])
goto out;
/* So, we say we've found it! */
ia->ia_nio = 1; /* XXX Really 2! */
ia->ia_io[0].ir_size = ELINK2_NIC_PORTS;
/* Map the memory space. */
if (bus_space_map(memt, ia->ia_iomem[0].ir_addr, memsize, 0, &memh)) {
aprint_error_dev(self, "can't map shared memory\n");
return;
}
esc->sc_asict = asict;
esc->sc_asich = asich;
sc->sc_regt = nict;
sc->sc_regh = nich;
sc->sc_buft = memt;
sc->sc_bufh = memh;
/* Interface is always enabled. */
sc->sc_enabled = 1;
/* Registers are linear. */
for (i = 0; i < 16; i++)
sc->sc_reg_map[i] = i;
/* Now we can use the NIC_{GET,PUT}() macros. */
/*
* Reset NIC and ASIC. Enable on-board transceiver throughout
* reset sequence since it will lock up if the cable isn't
* connected if we don't.
*/
bus_space_write_1(asict, asich, ELINK2_CR,
ELINK2_CR_RST | ELINK2_CR_XSEL);
/* Wait for a while, then un-reset it. */
delay(50);
/*
* The 3Com ASIC defaults to rather strange settings for the CR
* after a reset. It's important to set it again after the
* following write (this is done when we map the PROM below).
*/
bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
/* Wait a bit for the NIC to recover from the reset. */
delay(5000);
/*
* Get the station address from on-board ROM.
*
* First, map Ethernet address PROM over the top of where the NIC
* registers normally appear.
*/
bus_space_write_1(asict, asich, ELINK2_CR,
ELINK2_CR_XSEL | ELINK2_CR_EALO);
for (i = 0; i < ETHER_ADDR_LEN; i++)
sc->sc_enaddr[i] = NIC_GET(nict, nich, i);
/*
* Unmap PROM - select NIC registers. The proper setting of the
* transceiver is set in later in ec_init_card() via dp8390_init().
*/
bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
/* Determine if this is an 8-bit or 16-bit board. */
/* The 3c503 forces the WTS bit to a one if this is a 16-bit board. */
if (NIC_GET(nict, nich, ED_P2_DCR) & ED_DCR_WTS)
esc->sc_16bitp = 1;
else
esc->sc_16bitp = 0;
/* Do generic parts of attach. */
if (dp8390_config(sc)) {
aprint_error_dev(self, " configuration failed\n");
return;
}
/*
* We need to override the way dp8390_config() set up our
* shared memory.
*
* We have an entire 8k window to put the transmit buffers on the
* 16-bit boards. But since the 16bit 3c503's shared memory is only
* fast enough to overlap the loading of one full-size packet, trying
* to load more than 2 buffers can actually leave the transmitter idle
* during the load. So 2 seems the best value. (Although a mix of
* variable-sized packets might change this assumption. Nonetheless,
* we optimize for linear transfers of same-size packets.)
*/
if (esc->sc_16bitp) {
if (device_cfdata(sc->sc_dev)->cf_flags &
DP8390_NO_MULTI_BUFFERING)
sc->txb_cnt = 1;
else
sc->txb_cnt = 2;
/*
* Initialize CA page start/stop registers. Probably only needed
* if doing DMA, but what the Hell.
*/
bus_space_write_1(asict, asich, ELINK2_PSTR, sc->rec_page_start);
bus_space_write_1(asict, asich, ELINK2_PSPR, sc->rec_page_stop);
/*
* Program the IRQ.
*/
switch (ia->ia_irq[0].ir_irq) {
case 9: tmp = ELINK2_IDCFR_IRQ2; break;
case 3: tmp = ELINK2_IDCFR_IRQ3; break;
case 4: tmp = ELINK2_IDCFR_IRQ4; break;
case 5: tmp = ELINK2_IDCFR_IRQ5; break;
break;
case ISA_UNKNOWN_IRQ:
aprint_error_dev(self, "wildcarded IRQ is not allowed\n");
return;
default:
aprint_error_dev(self, "invalid IRQ %d, must be 3, 4, 5, "
"or 9\n", ia->ia_irq[0].ir_irq);
return;
}
/*
* Initialize the GA configuration register. Set bank and enable
* shared memory.
*/
bus_space_write_1(asict, asich, ELINK2_GACFR,
ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
/*
* Initialize "Vector Pointer" registers. These gawd-awful things
* are compared to 20 bits of the address on the ISA, and if they
* match, the shared memory is disabled. We set them to 0xffff0...
* allegedly the reset vector.
*/
bus_space_write_1(asict, asich, ELINK2_VPTR2, 0xff);
bus_space_write_1(asict, asich, ELINK2_VPTR1, 0xff);
bus_space_write_1(asict, asich, ELINK2_VPTR0, 0x00);
/*
* Now run the real memory test.
*/
if (ec_test_mem(sc)) {
aprint_error_dev(self, "memory test failed\n");
return;
}
/*
* We have to do this after we initialize the GA, but we
* have to do that after calling dp8390_config(), which
* wants to test memory. Put this noop here, and then
* actually test memory later.
*/
return (0);
}
if (esc->sc_16bitp) {
for (i = 0; i < memsize; i += 2) {
if (bus_space_read_2(memt, memh, i) != 0)
goto fail;
}
} else {
for (i = 0; i < memsize; i++) {
if (bus_space_read_1(memt, memh, i) != 0)
goto fail;
}
}
return (0);
fail:
aprint_error_dev(sc->sc_dev,
"failed to clear shared memory at offset 0x%x\n", i);
return (1);
}
/*
* Given a NIC memory source address and a host memory destination address,
* copy 'len' from NIC to host using shared memory. The 'len' is rounded
* up to a word - ok as long as mbufs are word-sized.
*/
static inline void
ec_readmem(struct ec_softc *esc, int from, uint8_t *to, int len)
{
bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
if (len & 1)
++len;
if (esc->sc_16bitp)
bus_space_read_region_2(memt, memh, from, (u_int16_t *)to,
len >> 1);
else
bus_space_read_region_1(memt, memh, from, to, len);
}
/*
* 8-bit boards are simple: we're already in the correct
* page, and no alignment tricks are necessary.
*/
if (esc->sc_16bitp == 0) {
for (; m != NULL; buf += m->m_len, m = m->m_next)
bus_space_write_region_1(memt, memh, buf,
mtod(m, u_int8_t *), m->m_len);
if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
bus_space_set_region_1(memt, memh, buf,
0, ETHER_MIN_LEN - ETHER_CRC_LEN - savelen);
savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
}
return (savelen);
}
/*
* If it's a 16-bit board, we have transmit buffers
* in a different page; switch to it.
*/
if (esc->sc_16bitp)
bus_space_write_1(asict, asich, ELINK2_GACFR,
ELINK2_GACFR_RSEL);
/* Start out with no leftover data. */
leftover = 0;
savebyte[0] = savebyte[1] = 0;
for (; m != NULL; m = m->m_next) {
len = m->m_len;
if (len == 0)
continue;
data = mtod(m, u_int8_t *);
#ifdef DIAGNOSTIC
lim = data + len;
#endif
while (len > 0) {
if (leftover) {
/*
* Data left over (from mbuf or realignment).
* Buffer the next byte, and write it and
* the leftover data out.
*/
savebyte[1] = *data++;
len--;
bus_space_write_2(memt, memh, buf,
*(u_int16_t *)savebyte);
buf += 2;
leftover = 0;
} else if (BUS_SPACE_ALIGNED_POINTER(data, u_int16_t)
== 0) {
/*
* Unaligned data; buffer the next byte.
*/
savebyte[0] = *data++;
len--;
leftover = 1;
} else {
/*
* Aligned data; output contiguous words as
* much as we can, then buffer the remaining
* byte, if any.
*/
leftover = len & 1;
len &= ~1;
bus_space_write_region_2(memt, memh, buf,
(u_int16_t *)data, len >> 1);
data += len;
buf += len;
if (leftover)
savebyte[0] = *data++;
len = 0;
}
}
if (len < 0)
panic("ec_write_mbuf: negative len");
#ifdef DIAGNOSTIC
if (data != lim)
panic("ec_write_mbuf: data != lim");
#endif
}
if (leftover) {
savebyte[1] = 0;
bus_space_write_2(memt, memh, buf, *(u_int16_t *)savebyte);
buf += 2;
}
if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
bus_space_set_region_2(memt, memh, buf,
0, (ETHER_MIN_LEN - ETHER_CRC_LEN - savelen) >> 1);
savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
}
/*
* Switch back to receive page.
*/
if (esc->sc_16bitp)
bus_space_write_1(asict, asich, ELINK2_GACFR,
ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
return (savelen);
}
int
ec_ring_copy(struct dp8390_softc *sc, int src, void *dst, u_short amount)
{
struct ec_softc *esc = (struct ec_softc *)sc;
u_short tmp_amount;
/* Does copy wrap to lower addr in ring buffer? */
if (src + amount > sc->mem_end) {
tmp_amount = sc->mem_end - src;
/* Copy amount up to end of NIC memory. */
ec_readmem(esc, src, dst, tmp_amount);