Home | History | Annotate | Line # | Download | only in isa
if_ntwoc_isa.c revision 1.2
      1 /*	$NetBSD: if_ntwoc_isa.c,v 1.2 2001/11/13 08:01:20 lukem Exp $	*/
      2 /*
      3  * Copyright (c) 1999 Christian E. Hopps
      4  * Copyright (c) 1996 John Hay.
      5  * Copyright (c) 1996 SDL Communications, Inc.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the author nor the names of any co-contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  *
     32  * $Id: if_ntwoc_isa.c,v 1.2 2001/11/13 08:01:20 lukem Exp $
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: if_ntwoc_isa.c,v 1.2 2001/11/13 08:01:20 lukem Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/mbuf.h>
     42 #include <sys/socket.h>
     43 
     44 #include <net/if.h>
     45 
     46 #include <machine/cpu.h>
     47 #include <machine/bus.h>
     48 #include <machine/intr.h>
     49 
     50 #include <dev/isa/isavar.h>
     51 
     52 #include <dev/ic/hd64570reg.h>
     53 #include <dev/ic/hd64570var.h>
     54 
     55 #include <dev/isa/if_ntwoc_isareg.h>
     56 
     57 #if 1
     58 #define NTWO_DEBUG
     59 #endif
     60 
     61 #ifdef NTWO_DEBUG
     62 #define NTWO_DPRINTF(x) printf x
     63 #else
     64 #define NTWO_DPRINTF(x)
     65 #endif
     66 
     67 #if __NetBSD_Version__ >= 104160000
     68 static	void ntwoc_isa_config_interrupts __P((struct device *));
     69 #else
     70 #define	SCA_BASECLOCK	9830400
     71 #endif
     72 
     73 /* hard core 16k for now */
     74 #define	NTWOC_WIN_SIZE	0x4000
     75 
     76 struct ntwoc_isa_softc {
     77 	/* Generic device stuff */
     78 	struct device sc_dev;		/* Common to all devices */
     79 
     80 	/* PCI chipset glue */
     81 	void		*sc_ih;	/* Interrupt handler */
     82 	isa_chipset_tag_t sc_ic;	/* ISA chipset handle */
     83 
     84 	struct sca_softc sc_sca;	/* the SCA itself */
     85 };
     86 
     87 static  int ntwoc_isa_probe __P((struct device *, struct cfdata *, void *));
     88 static  void ntwoc_isa_attach __P((struct device *, struct device *, void *));
     89 
     90 static	void ntwoc_isa_clock_callback __P((void *, int, int));
     91 static	void ntwoc_isa_dtr_callback __P((void *, int, int));
     92 static	int ntwoc_isa_intr __P((void *));
     93 static	void ntwoc_isa_get_clock __P((struct sca_port *, u_int8_t, u_int8_t,
     94     u_int8_t, u_int8_t));
     95 static	void ntwoc_isa_setup_memory(struct sca_softc *sc);
     96 static	void ntwoc_isa_shutdown __P((void *sc));
     97 
     98 struct cfattach ntwoc_isa_ca = {
     99 	sizeof(struct ntwoc_isa_softc), ntwoc_isa_probe, ntwoc_isa_attach,
    100 };
    101 
    102 /*
    103  * Names for daughter card types.  These match the NTWOC_DB_* defines.
    104  */
    105 char *ntwoc_db_names[] = {
    106 	"V.35", "Unknown 0x01", "Test", "Unknown 0x03",
    107 	"RS232", "Unknown 0x05", "RS422", "None"
    108 };
    109 
    110 /* some weird offset XXX */
    111 #define SCA_REG(r)	(((r) & 0xf) + (((r) & 0xf0) << 6))
    112 
    113 /*
    114  * functions that read and write to the sca registers
    115  */
    116 static void
    117 ntwoc_isa_sca_write_1(struct sca_softc *sc, u_int reg, u_int8_t val)
    118 {
    119 	bus_space_write_1(sc->sc_iot, sc->scu_sca_ioh[(reg & 0xf0) >> 4],
    120 	    (reg & 0xf), val);
    121 }
    122 
    123 static void
    124 ntwoc_isa_sca_write_2(struct sca_softc *sc, u_int reg, u_int16_t val)
    125 {
    126 	bus_space_write_2(sc->sc_iot, sc->scu_sca_ioh[(reg & 0xf0) >> 4],
    127 	    (reg & 0xf), val);
    128 }
    129 
    130 static u_int8_t
    131 ntwoc_isa_sca_read_1(struct sca_softc *sc, u_int reg)
    132 {
    133 	return
    134 	    bus_space_read_1(sc->sc_iot, sc->scu_sca_ioh[(reg & 0xf0) >> 4],
    135 	    (reg & 0xf));
    136 }
    137 
    138 static u_int16_t
    139 ntwoc_isa_sca_read_2(struct sca_softc *sc, u_int reg)
    140 {
    141 	return
    142 	    bus_space_read_2(sc->sc_iot, sc->scu_sca_ioh[(reg & 0xf0) >> 4],
    143 	    (reg & 0xf));
    144 }
    145 
    146 /*
    147  * set the correct window/page
    148  */
    149 static void
    150 ntwoc_isa_set_page(struct sca_softc *sca, bus_addr_t addr)
    151 {
    152 	u_int8_t psr;
    153 
    154 	/* get old psr value replace old window with new */
    155 	psr = bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR);
    156 	psr &= ~NTWOC_PG_MSK;
    157 	psr |= ((addr >> sca->scu_pageshift) & NTWOC_PG_MSK);
    158 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR, psr);
    159 }
    160 
    161 /*
    162  * enable the memory window
    163  */
    164 static void
    165 ntwoc_isa_set_on(struct sca_softc *sca)
    166 {
    167 	u_int8_t pcr;
    168 
    169 	/* get old value and add window enable */
    170 	pcr = bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR);
    171 	pcr |= NTWOC_PCR_MEM_WIN;
    172 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR, pcr);
    173 }
    174 
    175 /*
    176  * turn off memory window
    177  */
    178 static void
    179 ntwoc_isa_set_off(struct sca_softc *sca)
    180 {
    181 	u_int8_t pcr;
    182 
    183 	/* get old value and remove window enable */
    184 	pcr = bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR);
    185 	pcr &= ~NTWOC_PCR_MEM_WIN;
    186 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR, pcr);
    187 }
    188 
    189 static int
    190 ntwoc_isa_probe(struct device *parent, struct cfdata *match, void *aux)
    191 {
    192 	struct isa_attach_args *ia;
    193 	bus_space_tag_t iot, memt;
    194 	bus_space_handle_t ioh, memh, sca_ioh[16];
    195 	int i, tmp, dbg, rv;
    196 	int gotmem, gotsca[16];
    197 	u_int32_t ioport;
    198 
    199 	ia = (struct isa_attach_args *)aux;
    200 	iot = ia->ia_iot;
    201 	memt = ia->ia_memt;
    202 
    203 	memset(gotsca, 0, sizeof(gotsca));
    204 	gotmem = rv = 0;
    205 	dbg = 0;
    206 
    207 	/* disallow wildcarded I/O base */
    208 	if (ia->ia_iobase == ISACF_PORT_DEFAULT) {
    209 		printf("ntwoc_isa_probe: must specify port address\n");
    210 		return (0);
    211 	}
    212 
    213 	if (ia->ia_irq == ISACF_IRQ_DEFAULT) {
    214 		printf("ntwoc_isa_probe: must specify irq\n");
    215 		return (0);
    216 	}
    217 
    218 	if (ia->ia_maddr == ISACF_IOMEM_DEFAULT) {
    219 		printf("ntwoc_isa_probe: must specify iomem\n");
    220 		return (0);
    221 	}
    222 
    223 	tmp = (match->cf_flags & NTWOC_FLAGS_NPORT_MASK) + 1;
    224 	if (tmp < 1 || tmp > 2) {
    225 		printf("ntwoc_isa_probe: only 1 or 2 ports allowed\n");
    226 		return (0);
    227 	}
    228 
    229 	/* map the isa io addresses */
    230 	if ((tmp = bus_space_map(iot, ia->ia_iobase, NTWOC_SRC_IOPORT_SIZE, 0,
    231 	    &ioh))) {
    232 		printf("ntwoc_isa_probe: mapping port 0x%x sz %d failed: %d\n",
    233 		    ia->ia_iobase, NTWOC_SRC_IOPORT_SIZE, tmp);
    234 		return (0);
    235 	}
    236 
    237 	ioport = ia->ia_iobase + 0x8000;
    238 	for (i = 0; i < 16; ioport += (0x10 << 6), i++) {
    239 		/* map the isa io addresses */
    240 		if ((tmp = bus_space_map(iot, ioport, 16, 0, &sca_ioh[i]))) {
    241 			printf(
    242 			 "ntwoc_isa_probe: mapping sca 0x%x sz %d failed: %d\n",
    243 			    ioport, 16, tmp);
    244 			goto out;
    245 		}
    246 		gotsca[i] = 1;
    247 	}
    248 
    249 	/* map the isa memory addresses */
    250 	/* XXX we really want the user to select this */
    251 	if ((tmp = bus_space_map(ia->ia_memt, ia->ia_maddr, NTWOC_WIN_SIZE, 0,
    252 	    &memh))) {
    253 		printf("ntwoc_isa_probe: mapping mem 0x%x sz %d failed: %d\n",
    254 		    ia->ia_maddr, NTWOC_WIN_SIZE, tmp);
    255 		goto out;
    256 	}
    257 	gotmem = 1;
    258 
    259 	/* turn off the card */
    260 	bus_space_write_1(iot, ioh, NTWOC_PCR, 0);
    261 
    262 	/*
    263 	 * Next, we'll test the Base Address Register to retension of
    264 	 * data... ... seeing if we're *really* talking to an N2.
    265 	 */
    266 	for (i = 0; i < 0x100; i++) {
    267 		bus_space_write_1(iot, ioh, NTWOC_BAR, i);
    268 		(void)bus_space_read_1(iot, ioh, NTWOC_PCR);
    269 		if (bus_space_read_1(iot, ioh, NTWOC_BAR) != i) {
    270 			printf("ntwoc_isa_probe failed (BAR %x, %x)\n", i, tmp);
    271 			goto out;
    272 		}
    273 	}
    274 
    275 	/* XXX XXX update the calls to SCA_REG to use our mapping */
    276 
    277 	/*
    278 	 * Now see if we can see the SCA.
    279 	 */
    280 	bus_space_write_1(iot, ioh, NTWOC_PCR,
    281 	     NTWOC_PCR_SCARUN | bus_space_read_1(iot, ioh, NTWOC_PCR));
    282 	bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRL), 0);
    283 	bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRM), 0);
    284 	bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_WCRH), 0);
    285 	bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_PCR), 0);
    286 
    287 	bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_TMC0), 0);
    288 	(void)bus_space_read_1(iot, ioh, 0);
    289 	if ((tmp = bus_space_read_1(iot, sca_ioh[0], SCA_REG(SCA_TMC0))) != 0) {
    290 		printf("ntwoc_isa_probe: Error reading SCA (TMC0 0, %x)\n",
    291 		   tmp);
    292 		goto out;
    293 	}
    294 
    295 	bus_space_write_1(iot, sca_ioh[0], SCA_REG(SCA_TMC0), 0x5A);
    296 	(void)bus_space_read_1(iot, ioh, 0);
    297 
    298 	tmp = bus_space_read_1(iot, sca_ioh[0], SCA_REG(SCA_TMC0));
    299 	if (tmp != 0x5A) {
    300 		printf("ntwoc_isa_probe: Error reading SCA (TMC0 5A, %x)\n",
    301 		    tmp);
    302 		goto out;
    303 	}
    304 
    305 	bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_CDAL0), 0);
    306 	(void)bus_space_read_1(iot, ioh, 0);
    307 	tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_CDAL0));
    308 	if (tmp != 0) {
    309 		printf("ntwoc_isa_probe: Error reading SCA (CDAL0 0, %x)\n",
    310 		    tmp);
    311 		goto out;
    312 	}
    313 
    314 	bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_CDAL0), 0x55AA);
    315 	(void)bus_space_read_1(iot, ioh, 0);
    316 	tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_CDAL0));
    317 	if (tmp != 0x55AA) {
    318 		printf("ntwoc_isa_probe: Error reading SCA (CDAL0 55AA, %x)\n",
    319 		    tmp);
    320 		goto out;
    321 	}
    322 
    323 	/*
    324 	 * I had a weird card that didn't function correctly on a certain
    325 	 * newer MB.  I suspect it was the whacky port addresses.
    326 	 * The following correctly failed it.
    327 	 */
    328 	bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0), 0x0);
    329 	(void)bus_space_read_1(iot, ioh, 0);
    330 	tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0));
    331 	if (tmp != 0) {
    332 		printf("ntwoc_isa_probe: Error reading SCA (TCNTL0 0, %x)\n",
    333 		    tmp);
    334 		goto out;
    335 	}
    336 
    337 	bus_space_write_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0), 0x55AA);
    338 	(void)bus_space_read_1(iot, ioh, 0);
    339 	tmp = bus_space_read_2(iot, sca_ioh[0], SCA_REG(SCA_TCNTL0));
    340 	if (tmp != 0x55AA) {
    341 		printf("ntwoc_isa_probe: Error reading SCA (TCNTL0 55AA, %x)\n",
    342 		    tmp);
    343 		goto out;
    344 	}
    345 
    346 	ia->ia_iosize = NTWOC_SRC_IOPORT_SIZE;
    347 	ia->ia_msize = NTWOC_WIN_SIZE;
    348 	rv = 1;
    349 out:
    350 	/* turn off the card */
    351 	bus_space_write_1(iot, ioh, NTWOC_PCR, 0);
    352 
    353 	if (gotmem)
    354 		bus_space_unmap(memt, memh, NTWOC_WIN_SIZE);
    355 	for (i = 0; i < 16; i++) {
    356 		if (gotsca[i])
    357 			bus_space_unmap(iot, sca_ioh[i], 16);
    358 	}
    359 	bus_space_unmap(iot, ioh, NTWOC_SRC_IOPORT_SIZE);
    360 	return (rv);
    361 }
    362 
    363 /*
    364  * we win! attach the card
    365  */
    366 static void
    367 ntwoc_isa_attach(struct device *parent, struct device *self, void *aux)
    368 {
    369 	struct ntwoc_isa_softc *sc;
    370 	struct isa_attach_args *ia;
    371 	struct sca_softc *sca;
    372 	bus_addr_t addr;
    373 	u_int8_t rdiv, tdiv, tmc;
    374 	u_int32_t flags, ioport;
    375 	u_int16_t tmp;
    376 	int i, dbg, pgs, rv;
    377 
    378 	ia = (struct isa_attach_args *)aux;
    379 	sc = (struct ntwoc_isa_softc *)self;
    380 	sca = &sc->sc_sca;
    381 	dbg = 0;
    382 
    383 	printf(": N2 Serial Interface\n");
    384 	flags = sc->sc_dev.dv_cfdata->cf_flags;
    385 
    386 	sc->sc_ic = ia->ia_ic;
    387 	sca->sc_parent = &sc->sc_dev;
    388 	sca->sc_numports = (flags & NTWOC_FLAGS_NPORT_MASK) + 1;
    389 	sca->sc_usedma = 0;
    390 	sca->sc_aux = sc;
    391 	sca->sc_dtr_callback = ntwoc_isa_dtr_callback;
    392 	sca->sc_clock_callback = ntwoc_isa_clock_callback;
    393 	sca->sc_read_1 = ntwoc_isa_sca_read_1;
    394 	sca->sc_read_2 = ntwoc_isa_sca_read_2;
    395 	sca->sc_write_1 = ntwoc_isa_sca_write_1;
    396 	sca->sc_write_2 = ntwoc_isa_sca_write_2;
    397 	sca->scu_set_page = ntwoc_isa_set_page;
    398 	sca->scu_page_on = ntwoc_isa_set_on;
    399 	sca->scu_page_off = ntwoc_isa_set_off;
    400 
    401 	/* map the io */
    402 	sca->sc_iot = ia->ia_iot;
    403 	if ((rv = bus_space_map(ia->ia_iot, ia->ia_iobase,
    404 	    NTWOC_SRC_IOPORT_SIZE, 0, &sca->sc_ioh))) {
    405 		printf("%s: can't map io 0x%x sz %d, %d\n",
    406 		    sc->sc_dev.dv_xname, ia->ia_iobase, NTWOC_SRC_IOPORT_SIZE,
    407 		    rv);
    408 		return;
    409 	}
    410 
    411 	/* support weird mapping (they used this to avoid 10-bit aliasing) */
    412 	ioport = ia->ia_iobase + 0x8000;
    413 	for (i = 0; i < 16; ioport += (0x10 << 6), i++) {
    414 		/* map the isa io addresses */
    415 		if ((tmp = bus_space_map(ia->ia_iot, ioport, 16, 0,
    416 		    &sca->scu_sca_ioh[i]))) {
    417 			printf("%s: mapping sca 0x%x sz %d failed: %d\n",
    418 			    sc->sc_dev.dv_xname, ioport, 16, tmp);
    419 			return;
    420 		}
    421 	}
    422 
    423 	/* map the isa memory */
    424 	sca->scu_memt = ia->ia_memt;
    425 	sca->scu_pagesize = 0x4000;	/* force 16k for now */
    426 	if (sca->scu_pagesize < 0x8000) {
    427 		/* round down to 16k */
    428 		sca->scu_pagesize = 0x4000;
    429 		sca->scu_pageshift = 14;
    430 		tmp = NTWOC_PSR_WIN_16K;
    431 	} else if (sca->scu_pagesize < 0x10000) {
    432 		/* round down to 32k */
    433 		sca->scu_pagesize = 0x8000;
    434 		sca->scu_pageshift = 15;
    435 		tmp = NTWOC_PSR_WIN_32K;
    436 	} else if (sca->scu_pagesize < 0x20000) {
    437 		/* round down to 64k */
    438 		sca->scu_pagesize = 0x10000;
    439 		sca->scu_pageshift = 16;
    440 		tmp = NTWOC_PSR_WIN_64K;
    441 	} else {
    442 		sca->scu_pagesize = 0x20000;
    443 		sca->scu_pageshift = 17;
    444 		tmp = NTWOC_PSR_WIN_128K;
    445 	}
    446 	sca->scu_pagemask = sca->scu_pagesize - 1;
    447 	if ((rv = bus_space_map(ia->ia_memt, ia->ia_maddr, sca->scu_pagesize, 0,
    448 	    &sca->scu_memh))) {
    449 		printf("%s: can't map mem 0x%x sz %ld, %d\n",
    450 		    sc->sc_dev.dv_xname, ia->ia_maddr, sca->scu_pagesize, rv);
    451 		return;
    452 	}
    453 
    454 	/* turn the card on!! */
    455 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
    456 	    bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
    457 	    | NTWOC_PCR_SCARUN);
    458 
    459 	/* set the window size to 16k */
    460 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR, tmp);
    461 
    462 	/* reset mcr */
    463 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_MCR,
    464 	    NTWOC_MCR_DTR0 | NTWOC_MCR_DTR1 | NTWOC_MCR_TE0 | NTWOC_MCR_TE1);
    465 
    466 
    467 	/* allow for address above 1M and 16 bit i/o */
    468 #if 0
    469 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
    470 	    bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
    471 	    | NTWOC_PCR_EN_VPM | NTWOC_PCR_ISA16);
    472 #endif
    473 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
    474 	    bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
    475 	    | NTWOC_PCR_ISA16);
    476 
    477 	/* program the card with the io address */
    478 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR,
    479 	    ((ia->ia_maddr >> 16) & NTWOC_PCR_16M_SEL)
    480 	    |
    481 	    (bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PCR)
    482 	    & ~NTWOC_PCR_16M_SEL));
    483 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_BAR,
    484 	    (ia->ia_maddr >> 12));
    485 
    486 	/* enable the memory window */
    487 	ntwoc_isa_set_on(sca);
    488 
    489 	/*
    490 	 * write a magic value into each possible page of memory
    491 	 * incrementing by our window size
    492 	 */
    493 	addr = 0;
    494 	for (i = 0; i <= NTWOC_PSR_PG_SEL; addr += sca->scu_pagesize, i++) {
    495 		/* select the page */
    496 		ntwoc_isa_set_page(sca, addr);
    497 		bus_space_write_2(sca->scu_memt, sca->scu_memh, 0, 0xAA55);
    498 	}
    499 
    500 	/*
    501 	 * go back through pages and verify that value is different
    502 	 * after writing to previous page
    503 	 */
    504 	addr = 0;
    505 	for (i = 0; i <= NTWOC_PSR_PG_SEL; addr += sca->scu_pagesize, i++) {
    506 		ntwoc_isa_set_page(sca, addr);
    507 
    508 		tmp = bus_space_read_2(sca->scu_memt, sca->scu_memh, 0);
    509 		if (tmp != 0xAA55)
    510 			break;
    511 
    512 		/* write a different value into this page now */
    513 		bus_space_write_2(sca->scu_memt, sca->scu_memh, 0, i);
    514 	}
    515 	sca->scu_npages = pgs = i;	/* final count of 16K pages */
    516 
    517 	/* erase the pages */
    518 	addr = 0;
    519 	for (i = 0; i <= pgs; addr += sca->scu_pagesize, i++) {
    520 		ntwoc_isa_set_page(sca, addr);
    521 		bus_space_set_region_1(sca->scu_memt, sca->scu_memh, 0, 0,
    522 		    sca->scu_pagesize);
    523 	}
    524 
    525 #if 0
    526 	printf("%s: sca port 0x%x-0x%x dpram %ldk %d serial port%s\n",
    527 	    sc->sc_dev.dv_xname, ia->ia_iobase | 0x8000,
    528 	    (ia->ia_iobase | 0x8000) + NTWOC_SRC_ASIC_SIZE - 1,
    529 	    pgs * (sca->scu_pagesize / 1024), sca->sc_numports,
    530 	    (sca->sc_numports > 1 ? "s" : ""));
    531 #else
    532 	printf("%s: dpram %ldk %d serial port%s\n",
    533 	    sc->sc_dev.dv_xname, pgs * (sca->scu_pagesize / 1024),
    534 	    sca->sc_numports, (sca->sc_numports > 1 ? "s" : ""));
    535 #endif
    536 
    537 	/* disable the memory window */
    538 	ntwoc_isa_set_off(sca);
    539 
    540 	/* enabled sca dma */
    541 	bus_space_write_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR,
    542 	    bus_space_read_1(sca->sc_iot, sca->sc_ioh, NTWOC_PSR)
    543 	    | NTWOC_PSR_EN_SCA_DMA);
    544 
    545 	/* now establish our irq -- perhaps sanity check the value */
    546 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    547 	    IPL_NET, ntwoc_isa_intr, sc);
    548 	if (sc->sc_ih == NULL) {
    549 		printf("%s: can't establish interrupt\n",
    550 		    sc->sc_dev.dv_xname);
    551 		return;
    552 	}
    553 
    554 	/* make sure we have 2 pages for each port */
    555 	if (pgs < 2 * sca->sc_numports) {
    556 		printf("%s: %d less than required pages of memory of %d\n",
    557 		    sc->sc_dev.dv_xname, pgs, 2 * sca->sc_numports);
    558 		return;
    559 	}
    560 
    561 	/* sca_get_base_clock(sca); */
    562 
    563 	/*
    564 	 * get clock information from user
    565 	 */
    566 	rdiv = (flags & NTWOC_FLAGS_RXDIV_MASK) >> NTWOC_FLAGS_RXDIV_SHIFT;
    567 	if (rdiv > 9)
    568 		panic("bad rx divisor in flags");
    569 
    570 	tdiv = (flags & NTWOC_FLAGS_TXDIV_MASK) >> NTWOC_FLAGS_TXDIV_SHIFT;
    571 	if (tdiv > 9)
    572 		panic("bad tx divisor in flags");
    573 	tmc = (flags & NTWOC_FLAGS_TMC_MASK) >> NTWOC_FLAGS_TMC_SHIFT;
    574 
    575 	ntwoc_isa_get_clock(&sca->sc_ports[0],
    576 	    flags & NTWOC_FLAGS_CLK0_MASK, tmc, rdiv, tdiv);
    577 	if (sca->sc_numports > 1)
    578 		ntwoc_isa_get_clock(&sca->sc_ports[1],
    579 		    (flags & NTWOC_FLAGS_CLK1_MASK) >> NTWOC_FLAGS_CLK1_SHIFT,
    580 		    tmc, rdiv, tdiv);
    581 
    582 	ntwoc_isa_setup_memory(sca);
    583 
    584 	sca_init(sca);
    585 
    586 	/* attach configured ports */
    587 	sca_port_attach(sca, 0);
    588 	if (sca->sc_numports == 2)
    589 		sca_port_attach(sca, 1);
    590 
    591 	/*
    592 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
    593 	 * doing do could allow DMA to corrupt kernel memory during the
    594 	 * reboot before the driver initializes.
    595 	 */
    596 	shutdownhook_establish(ntwoc_isa_shutdown, sc);
    597 
    598 #if __NetBSD_Version__ >= 104160000
    599 	/*
    600 	 * defer getting the base clock until interrupts are enabled
    601 	 * (and thus we have microtime())
    602 	 */
    603 	config_interrupts(self, ntwoc_isa_config_interrupts);
    604 #else
    605 	/* no callback pre 1.4-mumble */
    606 	sca->sc_baseclock = SCA_BASECLOCK;
    607 	sca_print_clock_info(&sc->sc_sca);
    608 #endif
    609 }
    610 
    611 /*
    612  * extract the clock information for a port from the flags field
    613  */
    614 static void
    615 ntwoc_isa_get_clock(struct sca_port *scp, u_int8_t flags, u_int8_t tmc,
    616     u_int8_t rdiv, u_int8_t tdiv)
    617 {
    618 	scp->sp_eclock =
    619 	    (flags & NTWOC_FLAGS_ECLOCK_MASK) >> NTWOC_FLAGS_ECLOCK_SHIFT;
    620 	scp->sp_rxs = rdiv;
    621 	scp->sp_txs = tdiv;
    622 	scp->sp_tmc = tmc;
    623 
    624 	/* get rx source */
    625 	switch ((flags & NTWOC_FLAGS_RXS_MASK) >> NTWOC_FLAGS_RXS_SHIFT) {
    626 	case NTWOC_FLAGS_RXS_LINE:
    627 		scp->sp_rxs = 0;
    628 		break;
    629 	case NTWOC_FLAGS_RXS_LINE_SN:
    630 		scp->sp_rxs |= SCA_RXS_CLK_LINE_SN;
    631 		break;
    632 	case NTWOC_FLAGS_RXS_INTERNAL:
    633 		scp->sp_rxs |= SCA_RXS_CLK_INTERNAL;
    634 		break;
    635 	case NTWOC_FLAGS_RXS_ADPLL_OUT:
    636 		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_OUT;
    637 		break;
    638 	case NTWOC_FLAGS_RXS_ADPLL_IN:
    639 		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_IN;
    640 		break;
    641 	default:
    642 		panic("bad rx source in flags");
    643 	}
    644 
    645 	/* get tx source */
    646 	switch ((flags & NTWOC_FLAGS_TXS_MASK) >> NTWOC_FLAGS_TXS_SHIFT) {
    647 	case NTWOC_FLAGS_TXS_LINE:
    648 		scp->sp_txs = 0;
    649 		break;
    650 	case NTWOC_FLAGS_TXS_INTERNAL:
    651 		scp->sp_txs |= SCA_TXS_CLK_INTERNAL;
    652 		break;
    653 	case NTWOC_FLAGS_TXS_RXCLOCK:
    654 		scp->sp_txs |= SCA_TXS_CLK_RXCLK;
    655 		break;
    656 	default:
    657 		panic("bad rx source in flags");
    658 	}
    659 }
    660 
    661 
    662 static int
    663 ntwoc_isa_intr(void *arg)
    664 {
    665 	struct ntwoc_isa_softc *sc = (struct ntwoc_isa_softc *)arg;
    666 
    667 	return sca_hardintr(&sc->sc_sca);
    668 }
    669 
    670 /*
    671  * shut down interrupts and DMA, so we don't trash the kernel on warm
    672  * boot.  Also, lower DTR on each port and disable card interrupts.
    673  */
    674 static void
    675 ntwoc_isa_shutdown(void *aux)
    676 {
    677 	struct ntwoc_isa_softc *sc = aux;
    678 	u_int16_t mcr;
    679 
    680 	/*
    681 	 * shut down the SCA ports
    682 	 */
    683 	sca_shutdown(&sc->sc_sca);
    684 
    685 	/*
    686 	 * lower DTR on both ports
    687 	 */
    688 	mcr = bus_space_read_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR);
    689 	mcr |= (NTWOC_MCR_DTR0 | NTWOC_MCR_DTR1);
    690 	bus_space_write_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR, mcr);
    691 }
    692 
    693 static void
    694 ntwoc_isa_dtr_callback(void *aux, int port, int state)
    695 {
    696 	struct ntwoc_isa_softc *sc = aux;
    697 	u_int8_t mcr;
    698 
    699 	mcr = bus_space_read_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR);
    700 
    701 	NTWO_DPRINTF(("port == %d, state == %d, old mcr:  0x%02x\n",
    702 	    port, state, mcr));
    703 
    704 	if (port == 0) {
    705 		if (state == 0)
    706 			mcr |= NTWOC_MCR_DTR0;
    707 		else
    708 			mcr &= ~NTWOC_MCR_DTR0;
    709 	} else {
    710 		if (state == 0)
    711 			mcr |= NTWOC_MCR_DTR1;
    712 		else
    713 			mcr &= ~NTWOC_MCR_DTR1;
    714 	}
    715 
    716 	NTWO_DPRINTF(("new mcr:  0x%02x\n", mcr));
    717 
    718 	bus_space_write_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR, mcr);
    719 }
    720 
    721 static void
    722 ntwoc_isa_clock_callback(void *aux, int port, int enable)
    723 {
    724 	struct ntwoc_isa_softc *sc = aux;
    725 	u_int8_t mcr;
    726 
    727 	mcr = bus_space_read_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR);
    728 
    729 	NTWO_DPRINTF(("clock: port == %d, enable == %d, old mcr:  0x%02x\n",
    730 	    port, enable, mcr));
    731 
    732 	if (port == 0) {
    733 		if (enable == 0)
    734 			mcr &= ~NTWOC_MCR_ETC0;
    735 		else
    736 			mcr |= NTWOC_MCR_ETC0;
    737 	} else {
    738 		if (enable == 0)
    739 			mcr &= ~NTWOC_MCR_ETC1;
    740 		else
    741 			mcr |= NTWOC_MCR_ETC1;
    742 	}
    743 
    744 	NTWO_DPRINTF(("clock: new mcr:  0x%02x\n", mcr));
    745 
    746 	bus_space_write_1(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh, NTWOC_MCR, mcr);
    747 }
    748 
    749 static void
    750 ntwoc_isa_setup_memory(struct sca_softc *sc)
    751 {
    752 	struct sca_port *scp;
    753 	u_int i, j;
    754 
    755 	/* allocate enough descriptors for a full page */
    756 
    757 	sc->sc_ports[0].sp_ntxdesc = (sc->scu_pagesize / SCA_BSIZE) - 1;
    758 	sc->sc_ports[0].sp_nrxdesc = (sc->scu_pagesize / SCA_BSIZE) - 1;
    759 	if (sc->sc_numports == 2) {
    760 		sc->sc_ports[1].sp_ntxdesc = sc->sc_ports[0].sp_ntxdesc;
    761 		sc->sc_ports[1].sp_nrxdesc = sc->sc_ports[0].sp_nrxdesc;
    762 	}
    763 
    764 	j = 0;
    765 	for (i = 0; i < sc->sc_numports; i++) {
    766 		scp = &sc->sc_ports[i];
    767 		scp->sp_txdesc_p = (bus_addr_t)(j * sc->scu_pagesize);
    768 		scp->sp_txdesc = (void *)scp->sp_txdesc_p;
    769 		scp->sp_txbuf_p = scp->sp_txdesc_p;
    770 		scp->sp_txbuf_p += SCA_BSIZE;
    771 		scp->sp_txbuf = (void *)scp->sp_txbuf_p;
    772 		j++;
    773 
    774 		scp->sp_rxdesc_p = (bus_addr_t)(j * sc->scu_pagesize);
    775 		scp->sp_rxdesc = (void *)scp->sp_txdesc_p;
    776 		scp->sp_rxbuf_p = scp->sp_rxdesc_p;
    777 		scp->sp_rxbuf_p += SCA_BSIZE;
    778 		scp->sp_rxbuf = (void *)scp->sp_rxbuf_p;
    779 		j++;
    780 	}
    781 }
    782 
    783 #if __NetBSD_Version__ >= 104160000
    784 /*
    785  * get the base clock frequency
    786  */
    787 static void
    788 ntwoc_isa_config_interrupts(self)
    789 	struct device *self;
    790 {
    791 	struct ntwoc_isa_softc *sc;
    792 
    793 	sc = (void *)self;
    794 	sca_get_base_clock(&sc->sc_sca);
    795 	sca_print_clock_info(&sc->sc_sca);
    796 }
    797 #endif
    798