/* $NetBSD: if_ntwoc_isa.c,v 1.27 2016/07/14 10:19:06 msaitoh Exp $ */
/*
* Copyright (c) 1999 Christian E. Hopps
* Copyright (c) 1996 John Hay.
* Copyright (c) 1996 SDL Communications, Inc.
* 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. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $Id: if_ntwoc_isa.c,v 1.27 2016/07/14 10:19:06 msaitoh Exp $
*/
/*
* set the correct window/page
*/
static void
ntwoc_isa_set_page(struct sca_softc *sca, bus_addr_t addr)
{
u_int8_t psr;
/* get old psr value replace old window with new */
psr = bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR);
psr &= ~NTWOC_PG_MSK;
psr |= ((addr >> sca->scu_pageshift) & NTWOC_PG_MSK);
bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR, psr);
}
/* disallow wildcarded I/O base */
if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT) {
printf("ntwoc_isa_probe: must specify port address\n");
return (0);
}
if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
printf("ntwoc_isa_probe: must specify irq\n");
return (0);
}
if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM) {
printf("ntwoc_isa_probe: must specify iomem\n");
return (0);
}
tmp = (match->cf_flags & NTWOC_FLAGS_NPORT_MASK) + 1;
if (tmp < 1 || tmp > 2) {
printf("ntwoc_isa_probe: only 1 or 2 ports allowed\n");
return (0);
}
/* map the isa io addresses */
if ((tmp = bus_space_map(iot, ia->ia_io[0].ir_addr,
NTWOC_SRC_IOPORT_SIZE, 0, &ioh))) {
printf("ntwoc_isa_probe: mapping port 0x%x sz %d failed: %d\n",
ia->ia_io[0].ir_addr, NTWOC_SRC_IOPORT_SIZE, tmp);
return (0);
}
ioport = ia->ia_io[0].ir_addr + 0x8000;
for (i = 0; i < 16; ioport += (0x10 << 6), i++) {
/* map the isa io addresses */
if ((tmp = bus_space_map(iot, ioport, 16, 0, &sca_ioh[i]))) {
printf(
"ntwoc_isa_probe: mapping sca 0x%x sz %d failed: %d\n",
ioport, 16, tmp);
goto out;
}
gotsca[i] = 1;
}
/* map the isa memory addresses */
/* XXX we really want the user to select this */
if ((tmp = bus_space_map(ia->ia_memt, ia->ia_iomem[0].ir_addr,
NTWOC_WIN_SIZE, 0, &memh))) {
printf("ntwoc_isa_probe: mapping mem 0x%x sz %d failed: %d\n",
ia->ia_iomem[0].ir_addr, NTWOC_WIN_SIZE, tmp);
goto out;
}
gotmem = 1;
/* turn off the card */
bus_space_write_1(iot, ioh, NTWOC_PCR, 0);
/*
* Next, we'll test the Base Address Register to retension of
* data... ... seeing if we're *really* talking to an N2.
*/
for (i = 0; i < 0x100; i++) {
bus_space_write_1(iot, ioh, NTWOC_BAR, i);
(void)bus_space_read_1(iot, ioh, NTWOC_PCR);
if (bus_space_read_1(iot, ioh, NTWOC_BAR) != i) {
printf("ntwoc_isa_probe failed (BAR %x, %x)\n", i, tmp);
goto out;
}
}
/* XXX XXX update the calls to SCA_REG to use our mapping */
/*
* Now see if we can see the SCA.
*/
bus_space_write_1(iot, ioh, NTWOC_PCR,
NTWOC_PCR_SCARUN | bus_space_read_1(iot, ioh, NTWOC_PCR));
bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRL), 0);
bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRM), 0);
bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRH), 0);
bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_PCR), 0);
/*
* I had a weird card that didn't function correctly on a certain
* newer MB. I suspect it was the whacky port addresses.
* The following correctly failed it.
*/
bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0), 0x0);
(void)bus_space_read_1(iot, ioh, 0);
tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0));
if (tmp != 0) {
printf("ntwoc_isa_probe: Error reading SCA (TCNTL0 0, %x)\n",
tmp);
goto out;
}
/* program the card with the io address */
bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
((ia->ia_iomem[0].ir_addr >> 16) & NTWOC_PCR_16M_SEL)
|
(bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
& ~NTWOC_PCR_16M_SEL));
bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_BAR,
(ia->ia_iomem[0].ir_addr >> 12));
/* enable the memory window */
ntwoc_isa_set_on(sca);
/*
* write a magic value into each possible page of memory
* incrementing by our window size
*/
addr = 0;
for (i = 0; i <= NTWOC_PSR_PG_SEL; addr += sca->scu_pagesize, i++) {
/* select the page */
ntwoc_isa_set_page(sca, addr);
bus_space_write_2(sca->scu_memt, sca->scu_memh, 0, 0xAA55);
}
/*
* go back through pages and verify that value is different
* after writing to previous page
*/
addr = 0;
for (i = 0; i <= NTWOC_PSR_PG_SEL; addr += sca->scu_pagesize, i++) {
ntwoc_isa_set_page(sca, addr);
tmp = bus_space_read_2(sca->scu_memt, sca->scu_memh, 0);
if (tmp != 0xAA55)
break;
/* write a different value into this page now */
bus_space_write_2(sca->scu_memt, sca->scu_memh, 0, i);
}
sca->scu_npages = pgs = i; /* final count of 16K pages */
/* erase the pages */
addr = 0;
for (i = 0; i <= pgs; addr += sca->scu_pagesize, i++) {
ntwoc_isa_set_page(sca, addr);
bus_space_set_region_1(sca->scu_memt, sca->scu_memh, 0, 0,
sca->scu_pagesize);
}
/* now establish our irq -- perhaps sanity check the value */
sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
IST_EDGE, IPL_NET, ntwoc_isa_intr, sc);
if (sc->sc_ih == NULL) {
aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
return;
}
/* make sure we have 2 pages for each port */
if (pgs < 2 * sca->sc_numports) {
aprint_error_dev(self,
"%d less than required pages of memory of %d\n",
pgs, 2 * sca->sc_numports);
return;
}
/* sca_get_base_clock(sca); */
/*
* get clock information from user
*/
rdiv = (flags & NTWOC_FLAGS_RXDIV_MASK) >> NTWOC_FLAGS_RXDIV_SHIFT;
if (rdiv > 9)
panic("bad rx divisor in flags");
/*
* Add shutdown hook so that DMA is disabled prior to reboot. Not
* doing do could allow DMA to corrupt kernel memory during the
* reboot before the driver initializes.
*/
shutdownhook_establish(ntwoc_isa_shutdown, sc);
#if __NetBSD_Version__ >= 104160000
/*
* defer getting the base clock until interrupts are enabled
* (and thus we have microtime())
*/
config_interrupts(self, ntwoc_isa_config_interrupts);
#else
/* no callback pre 1.4-mumble */
sca->sc_baseclock = SCA_BASECLOCK;
sca_print_clock_info(&sc->sc_sca);
#endif
}
/*
* extract the clock information for a port from the flags field
*/
static void
ntwoc_isa_get_clock(struct sca_port *scp, u_int8_t flags, u_int8_t tmc,
u_int8_t rdiv, u_int8_t tdiv)
{
scp->sp_eclock =
(flags & NTWOC_FLAGS_ECLOCK_MASK) >> NTWOC_FLAGS_ECLOCK_SHIFT;
scp->sp_rxs = rdiv;
scp->sp_txs = tdiv;
scp->sp_tmc = tmc;
/* get rx source */
switch ((flags & NTWOC_FLAGS_RXS_MASK) >> NTWOC_FLAGS_RXS_SHIFT) {
case NTWOC_FLAGS_RXS_LINE:
scp->sp_rxs = 0;
break;
case NTWOC_FLAGS_RXS_LINE_SN:
scp->sp_rxs |= SCA_RXS_CLK_LINE_SN;
break;
case NTWOC_FLAGS_RXS_INTERNAL:
scp->sp_rxs |= SCA_RXS_CLK_INTERNAL;
break;
case NTWOC_FLAGS_RXS_ADPLL_OUT:
scp->sp_rxs |= SCA_RXS_CLK_ADPLL_OUT;
break;
case NTWOC_FLAGS_RXS_ADPLL_IN:
scp->sp_rxs |= SCA_RXS_CLK_ADPLL_IN;
break;
default:
panic("bad rx source in flags");
}
/* get tx source */
switch ((flags & NTWOC_FLAGS_TXS_MASK) >> NTWOC_FLAGS_TXS_SHIFT) {
case NTWOC_FLAGS_TXS_LINE:
scp->sp_txs = 0;
break;
case NTWOC_FLAGS_TXS_INTERNAL:
scp->sp_txs |= SCA_TXS_CLK_INTERNAL;
break;
case NTWOC_FLAGS_TXS_RXCLOCK:
scp->sp_txs |= SCA_TXS_CLK_RXCLK;
break;
default:
panic("bad rx source in flags");
}
}
/*
* shut down interrupts and DMA, so we don't trash the kernel on warm
* boot. Also, lower DTR on each port and disable card interrupts.
*/
static void
ntwoc_isa_shutdown(void *aux)
{
struct ntwoc_isa_softc *sc = aux;
u_int16_t mcr;
/*
* shut down the SCA ports
*/
sca_shutdown(&sc->sc_sca);
#if __NetBSD_Version__ >= 104160000
/*
* get the base clock frequency
*/
static void
ntwoc_isa_config_interrupts(device_t self)
{
struct ntwoc_isa_softc *sc;