Home | History | Annotate | Line # | Download | only in pci
if_ntwoc_pci.c revision 1.27.2.2
      1  1.27.2.2      yamt /*	$NetBSD: if_ntwoc_pci.c,v 1.27.2.2 2014/05/22 11:40:25 yamt Exp $	*/
      2       1.1  explorer 
      3       1.1  explorer /*
      4       1.1  explorer  * Copyright (c) 1998 Vixie Enterprises
      5       1.1  explorer  * All rights reserved.
      6       1.1  explorer  *
      7       1.1  explorer  * Redistribution and use in source and binary forms, with or without
      8       1.1  explorer  * modification, are permitted provided that the following conditions
      9       1.1  explorer  * are met:
     10       1.1  explorer  *
     11       1.1  explorer  * 1. Redistributions of source code must retain the above copyright
     12       1.1  explorer  *    notice, this list of conditions and the following disclaimer.
     13       1.1  explorer  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  explorer  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  explorer  *    documentation and/or other materials provided with the distribution.
     16       1.1  explorer  * 3. Neither the name of Vixie Enterprises nor the names
     17       1.1  explorer  *    of its contributors may be used to endorse or promote products derived
     18       1.1  explorer  *    from this software without specific prior written permission.
     19       1.1  explorer  *
     20       1.1  explorer  * THIS SOFTWARE IS PROVIDED BY VIXIE ENTERPRISES AND
     21       1.1  explorer  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22       1.1  explorer  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     23       1.1  explorer  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24       1.1  explorer  * DISCLAIMED.  IN NO EVENT SHALL VIXIE ENTERPRISES OR
     25       1.1  explorer  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26       1.1  explorer  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27       1.1  explorer  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28       1.1  explorer  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29       1.1  explorer  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30       1.1  explorer  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     31       1.1  explorer  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1  explorer  * SUCH DAMAGE.
     33       1.1  explorer  *
     34       1.1  explorer  * This software has been written for Vixie Enterprises by Michael Graff
     35       1.1  explorer  * <explorer (at) flame.org>.  To learn more about Vixie Enterprises, see
     36       1.1  explorer  * ``http://www.vix.com''.
     37       1.1  explorer  */
     38       1.6     lukem 
     39       1.6     lukem #include <sys/cdefs.h>
     40  1.27.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: if_ntwoc_pci.c,v 1.27.2.2 2014/05/22 11:40:25 yamt Exp $");
     41       1.1  explorer 
     42       1.1  explorer #include <sys/param.h>
     43       1.1  explorer #include <sys/systm.h>
     44       1.1  explorer #include <sys/device.h>
     45       1.1  explorer #include <sys/mbuf.h>
     46       1.1  explorer #include <sys/socket.h>
     47       1.1  explorer 
     48       1.1  explorer #include <net/if.h>
     49       1.1  explorer 
     50      1.20        ad #include <sys/cpu.h>
     51      1.20        ad #include <sys/bus.h>
     52      1.20        ad #include <sys/intr.h>
     53       1.1  explorer 
     54       1.1  explorer #include <dev/pci/pcivar.h>
     55       1.1  explorer #include <dev/pci/pcireg.h>
     56       1.1  explorer #include <dev/pci/pcidevs.h>
     57       1.1  explorer 
     58       1.1  explorer #include <dev/ic/hd64570reg.h>
     59       1.1  explorer #include <dev/ic/hd64570var.h>
     60       1.1  explorer 
     61       1.1  explorer #include <dev/pci/if_ntwoc_pcireg.h>
     62       1.1  explorer 
     63       1.1  explorer #if 0
     64       1.1  explorer #define NTWO_DEBUG
     65       1.1  explorer #endif
     66       1.1  explorer 
     67       1.1  explorer #ifdef NTWO_DEBUG
     68       1.1  explorer #define NTWO_DPRINTF(x) printf x
     69       1.1  explorer #else
     70       1.1  explorer #define NTWO_DPRINTF(x)
     71       1.1  explorer #endif
     72       1.1  explorer 
     73       1.1  explorer /*
     74       1.3    chopps  * buffers per tx and rx channels, per port, and the size of each.
     75       1.3    chopps  * Don't use these constants directly, as they are really only hints.
     76       1.3    chopps  * Use the calculated values stored in struct sca_softc instead.
     77       1.3    chopps  *
     78       1.3    chopps  * Each must be at least 2, receive would be better at around 20 or so.
     79       1.3    chopps  *
     80       1.3    chopps  * XXX Due to a damned near impossible to track down bug, transmit buffers
     81       1.3    chopps  * MUST be 2, no more, no less.
     82       1.3    chopps  */
     83       1.3    chopps #ifndef NTWOC_NtxBUFS
     84       1.3    chopps #define NTWOC_NtxBUFS     40
     85       1.3    chopps #endif
     86       1.3    chopps #ifndef NTWOC_NrxBUFS
     87       1.3    chopps #define NTWOC_NrxBUFS     20
     88       1.3    chopps #endif
     89       1.3    chopps 
     90       1.3    chopps #if __NetBSD_Version__ >= 104160000
     91      1.24    cegger static	void ntwoc_pci_config_interrupts(device_t);
     92       1.3    chopps #else
     93       1.3    chopps #define	SCA_BASECLOCK	16000000
     94       1.3    chopps #endif
     95       1.3    chopps 
     96       1.3    chopps /*
     97       1.1  explorer  * Card specific config register location
     98       1.1  explorer  */
     99      1.27    dyoung #define PCI_CBMA_ASIC PCI_BAR(0)	/* Configuration Base Memory Address */
    100      1.27    dyoung #define PCI_CBMA_SCA PCI_BAR(2)
    101       1.1  explorer 
    102       1.1  explorer struct ntwoc_pci_softc {
    103       1.1  explorer 	/* Generic device stuff */
    104  1.27.2.1      yamt 	device_t sc_dev;		/* Common to all devices */
    105       1.1  explorer 
    106       1.1  explorer 	/* PCI chipset glue */
    107       1.1  explorer 	pci_intr_handle_t *sc_ih;	/* Interrupt handler */
    108       1.1  explorer 	pci_chipset_tag_t sc_sr;	/* PCI chipset handle */
    109       1.1  explorer 
    110       1.1  explorer 	bus_space_tag_t sc_asic_iot;	/* space cookie (for ASIC) */
    111       1.1  explorer 	bus_space_handle_t sc_asic_ioh;	/* bus space handle (for ASIC) */
    112       1.1  explorer 
    113       1.1  explorer 	struct sca_softc sc_sca;	/* the SCA itself */
    114       1.1  explorer };
    115       1.1  explorer 
    116      1.24    cegger static  int ntwoc_pci_match(device_t, cfdata_t, void *);
    117      1.24    cegger static  void ntwoc_pci_attach(device_t, device_t, void *);
    118       1.1  explorer 
    119      1.14     perry static	int ntwoc_pci_alloc_dma(struct sca_softc *);
    120      1.14     perry static	void ntwoc_pci_clock_callback(void *, int, int);
    121      1.14     perry static	void ntwoc_pci_dtr_callback(void *, int, int);
    122      1.14     perry static	void ntwoc_pci_get_clock(struct sca_port *, u_int8_t, u_int8_t,
    123      1.14     perry     u_int8_t, u_int8_t);
    124      1.14     perry static	int ntwoc_pci_intr(void *);
    125      1.14     perry static	void ntwoc_pci_setup_dma(struct sca_softc *);
    126      1.14     perry static	void ntwoc_pci_shutdown(void *sc);
    127       1.1  explorer 
    128  1.27.2.1      yamt CFATTACH_DECL_NEW(ntwoc_pci, sizeof(struct ntwoc_pci_softc),
    129       1.9   thorpej     ntwoc_pci_match, ntwoc_pci_attach, NULL, NULL);
    130       1.1  explorer 
    131       1.1  explorer /*
    132       1.1  explorer  * Names for daughter card types.  These match the NTWOC_DB_* defines.
    133       1.1  explorer  */
    134      1.15  christos const char *ntwoc_pci_db_names[] = {
    135       1.1  explorer 	"V.35", "Unknown 0x01", "Test", "Unknown 0x03",
    136       1.1  explorer 	"RS232", "Unknown 0x05", "RS422", "None"
    137       1.1  explorer };
    138       1.1  explorer 
    139       1.3    chopps /*
    140       1.3    chopps  * At least one implementation uses a somewhat strange register address
    141       1.3    chopps  * mapping.  If a card doesn't, define this to be a pass-through
    142       1.3    chopps  * macro.  (The ntwo driver needs this...)
    143       1.3    chopps  */
    144       1.3    chopps #define SCA_REG(y)  (((y) & 0x0002) ? (((y) & 0x00fd) + 0x100) : (y))
    145       1.3    chopps 
    146       1.3    chopps /*
    147       1.3    chopps  * functions that read and write to the sca registers
    148       1.3    chopps  */
    149       1.3    chopps static void
    150       1.3    chopps ntwoc_pci_sca_write_1(struct sca_softc *sc, u_int reg, u_int8_t val)
    151       1.3    chopps {
    152       1.3    chopps 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SCA_REG(reg), val);
    153       1.3    chopps }
    154       1.3    chopps 
    155       1.3    chopps static void
    156       1.3    chopps ntwoc_pci_sca_write_2(struct sca_softc *sc, u_int reg, u_int16_t val)
    157       1.3    chopps {
    158       1.3    chopps 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCA_REG(reg), val);
    159       1.3    chopps }
    160       1.3    chopps 
    161       1.3    chopps static u_int8_t
    162       1.3    chopps ntwoc_pci_sca_read_1(struct sca_softc *sc, u_int reg)
    163       1.3    chopps {
    164       1.3    chopps 	return
    165       1.3    chopps 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, SCA_REG(reg));
    166       1.3    chopps }
    167       1.3    chopps 
    168       1.3    chopps static u_int16_t
    169       1.3    chopps ntwoc_pci_sca_read_2(struct sca_softc *sc, u_int reg)
    170       1.3    chopps {
    171       1.3    chopps 	return
    172       1.3    chopps 	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCA_REG(reg));
    173       1.3    chopps }
    174       1.3    chopps 
    175       1.3    chopps 
    176       1.3    chopps 
    177       1.1  explorer static int
    178      1.24    cegger ntwoc_pci_match(device_t parent, cfdata_t match, void *aux)
    179       1.1  explorer {
    180       1.1  explorer 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    181       1.1  explorer 
    182       1.1  explorer 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RISCOM)
    183       1.1  explorer 	    && (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RISCOM_N2))
    184       1.1  explorer 		return 1;
    185       1.1  explorer 
    186       1.1  explorer 	return 0;
    187       1.1  explorer }
    188       1.1  explorer 
    189       1.1  explorer static void
    190      1.24    cegger ntwoc_pci_attach(device_t parent, device_t self, void *aux)
    191       1.1  explorer {
    192      1.25    cegger 	struct ntwoc_pci_softc *sc = device_private(self);
    193       1.1  explorer 	struct pci_attach_args *pa = aux;
    194       1.1  explorer 	struct sca_softc *sca = &sc->sc_sca;
    195       1.1  explorer 	pci_intr_handle_t ih;
    196       1.1  explorer 	const char *intrstr;
    197       1.1  explorer 	pcireg_t csr;
    198       1.3    chopps 	u_int8_t tmc, rdiv, tdiv;
    199       1.1  explorer 	u_int16_t frontend_cr;
    200       1.1  explorer 	u_int16_t db0, db1;
    201       1.3    chopps 	u_int32_t flags;
    202       1.1  explorer 	u_int numports;
    203  1.27.2.2      yamt 	char intrbuf[PCI_INTRSTR_LEN];
    204       1.1  explorer 
    205       1.1  explorer 	printf(": N2 Serial Interface\n");
    206  1.27.2.1      yamt 	flags = device_cfdata(self)->cf_flags;
    207       1.1  explorer 
    208       1.1  explorer 	/*
    209       1.1  explorer 	 * Map in the ASIC configuration space
    210       1.1  explorer 	 */
    211       1.1  explorer 	if (pci_mapreg_map(pa, PCI_CBMA_ASIC, PCI_MAPREG_TYPE_MEM, 0,
    212       1.1  explorer 			   &sc->sc_asic_iot, &sc->sc_asic_ioh, NULL, NULL)) {
    213  1.27.2.1      yamt 		aprint_error_dev(self, "Can't map register space (ASIC)\n");
    214       1.1  explorer 		return;
    215       1.1  explorer 	}
    216       1.1  explorer 	/*
    217       1.1  explorer 	 * Map in the serial controller configuration space
    218       1.1  explorer 	 */
    219       1.1  explorer 	if (pci_mapreg_map(pa, PCI_CBMA_SCA, PCI_MAPREG_TYPE_MEM, 0,
    220       1.1  explorer 			   &sca->sc_iot, &sca->sc_ioh, NULL, NULL)) {
    221  1.27.2.1      yamt 		aprint_error_dev(self, "Can't map register space (SCA)\n");
    222       1.1  explorer 		return;
    223       1.1  explorer 	}
    224       1.1  explorer 
    225       1.1  explorer 	/*
    226       1.1  explorer 	 * Enable the card
    227       1.1  explorer 	 */
    228       1.1  explorer 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    229       1.1  explorer 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    230       1.1  explorer 
    231       1.1  explorer 	/*
    232       1.1  explorer 	 * Map and establish the interrupt
    233       1.1  explorer 	 */
    234       1.4  sommerfe 	if (pci_intr_map(pa, &ih)) {
    235  1.27.2.1      yamt 		aprint_error_dev(self, "couldn't map interrupt\n");
    236       1.1  explorer 		return;
    237       1.1  explorer 	}
    238  1.27.2.2      yamt 	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
    239       1.3    chopps 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, ntwoc_pci_intr,
    240       1.3    chopps 	    sc);
    241       1.1  explorer 	if (sc->sc_ih == NULL) {
    242  1.27.2.1      yamt 		aprint_error_dev(self, "couldn't establish interrupt");
    243       1.1  explorer 		if (intrstr != NULL)
    244      1.26     njoly 			aprint_error(" at %s", intrstr);
    245      1.26     njoly 		aprint_error("\n");
    246       1.1  explorer 		return;
    247       1.1  explorer 	}
    248  1.27.2.1      yamt 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    249       1.1  explorer 
    250       1.1  explorer 	/*
    251       1.1  explorer 	 * Perform total black magic.  This is not only extremely
    252       1.1  explorer 	 * disgusting, but it should be explained a lot more in the
    253       1.1  explorer 	 * card's documentation.
    254       1.1  explorer 	 *
    255       1.1  explorer 	 * From what I gather, this does nothing more than configure the
    256       1.1  explorer 	 * PCI to ISA translator ASIC the N2pci card uses.
    257       1.1  explorer 	 *
    258       1.1  explorer 	 * From the FreeBSD driver:
    259       1.1  explorer 	 * offset
    260       1.1  explorer 	 *  0x00 - Map Range    - Mem-mapped to locate anywhere
    261       1.1  explorer 	 *  0x04 - Re-Map       - PCI address decode enable
    262       1.1  explorer 	 *  0x18 - Bus Region   - 32-bit bus, ready enable
    263       1.1  explorer 	 *  0x1c - Master Range - include all 16 MB
    264       1.1  explorer 	 *  0x20 - Master RAM   - Map SCA Base at 0
    265       1.1  explorer 	 *  0x28 - Master Remap - direct master memory enable
    266       1.1  explorer 	 *  0x68 - Interrupt    - Enable interrupt (0 to disable)
    267       1.1  explorer 	 */
    268       1.1  explorer 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
    269       1.1  explorer 			  0x00, 0xfffff000);
    270       1.1  explorer 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
    271       1.1  explorer 			  0x04, 1);
    272       1.1  explorer 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
    273       1.1  explorer 			  0x18, 0x40030043);
    274       1.1  explorer 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
    275       1.1  explorer 			  0x1c, 0xff000000);
    276       1.1  explorer 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
    277       1.1  explorer 			  0x20, 0);
    278       1.1  explorer 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
    279       1.1  explorer 			  0x28, 0xe9);
    280       1.1  explorer 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
    281       1.1  explorer 			  0x68, 0x10900);
    282       1.1  explorer 
    283       1.1  explorer 	/*
    284      1.12       wiz 	 * pass the DMA tag to the SCA
    285       1.1  explorer 	 */
    286       1.3    chopps 	sca->sc_usedma = 1;
    287       1.3    chopps 	sca->scu_dmat = pa->pa_dmat;
    288       1.1  explorer 
    289       1.1  explorer 	/*
    290       1.1  explorer 	 * Read the configuration information off the daughter card.
    291       1.1  explorer 	 */
    292       1.1  explorer 	frontend_cr = bus_space_read_2(sca->sc_iot, sca->sc_ioh, NTWOC_FECR);
    293       1.1  explorer 	NTWO_DPRINTF(("%s: frontend_cr = 0x%04x\n",
    294  1.27.2.1      yamt 		      device_xname(self), frontend_cr));
    295       1.1  explorer 
    296       1.1  explorer 	db0 = (frontend_cr & NTWOC_FECR_ID0) >> NTWOC_FECR_ID0_SHIFT;
    297       1.1  explorer 	db1 = (frontend_cr & NTWOC_FECR_ID1) >> NTWOC_FECR_ID1_SHIFT;
    298       1.1  explorer 
    299       1.1  explorer 	/*
    300       1.1  explorer 	 * Port 1 HAS to be present.  If it isn't, don't attach anything.
    301       1.1  explorer 	 */
    302       1.1  explorer 	if (db0 == NTWOC_FE_ID_NONE) {
    303  1.27.2.1      yamt 		printf("%s: no ports available\n", device_xname(self));
    304       1.1  explorer 		return;
    305       1.1  explorer 	}
    306       1.1  explorer 
    307       1.1  explorer 	/*
    308       1.1  explorer 	 * Port 1 is present.  Now, check to see if port 2 is also
    309       1.1  explorer 	 * present.
    310       1.1  explorer 	 */
    311       1.1  explorer 	numports = 1;
    312       1.1  explorer 	if (db1 != NTWOC_FE_ID_NONE)
    313       1.1  explorer 		numports++;
    314       1.1  explorer 
    315  1.27.2.1      yamt 	printf("%s: %d port%s\n", device_xname(self), numports,
    316       1.1  explorer 	       (numports > 1 ? "s" : ""));
    317  1.27.2.1      yamt 	printf("%s: port 0 interface card: %s\n", device_xname(self),
    318       1.3    chopps 	       ntwoc_pci_db_names[db0]);
    319       1.1  explorer 	if (numports > 1)
    320  1.27.2.1      yamt 		printf("%s: port 1 interface card: %s\n", device_xname(self),
    321       1.3    chopps 		       ntwoc_pci_db_names[db1]);
    322       1.1  explorer 
    323       1.1  explorer 	/*
    324       1.1  explorer 	 * enable the RS422 tristate transmit
    325       1.1  explorer 	 * diable clock output (use receiver clock for both)
    326       1.1  explorer 	 */
    327       1.1  explorer 	frontend_cr |= (NTWOC_FECR_TE0 | NTWOC_FECR_TE1);
    328       1.1  explorer 	frontend_cr &= ~(NTWOC_FECR_ETC0 | NTWOC_FECR_ETC1);
    329       1.1  explorer 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
    330       1.1  explorer 			  NTWOC_FECR, frontend_cr);
    331       1.1  explorer 
    332       1.1  explorer 	/*
    333       1.1  explorer 	 * initialize the SCA.  This will allocate DMAable memory based
    334       1.1  explorer 	 * on the number of ports we passed in, the size of each
    335       1.1  explorer 	 * buffer, and the number of buffers per port.
    336       1.1  explorer 	 */
    337  1.27.2.1      yamt 	sca->sc_parent = self;
    338       1.3    chopps 	sca->sc_read_1 = ntwoc_pci_sca_read_1;
    339       1.3    chopps 	sca->sc_read_2 = ntwoc_pci_sca_read_2;
    340       1.3    chopps 	sca->sc_write_1 = ntwoc_pci_sca_write_1;
    341       1.3    chopps 	sca->sc_write_2 = ntwoc_pci_sca_write_2;
    342       1.3    chopps 	sca->sc_dtr_callback = ntwoc_pci_dtr_callback;
    343       1.3    chopps 	sca->sc_clock_callback = ntwoc_pci_clock_callback;
    344       1.3    chopps 	sca->sc_aux = sc;
    345       1.3    chopps 	sca->sc_numports = numports;
    346       1.3    chopps 
    347       1.3    chopps 	/*
    348       1.3    chopps 	 * get clock information from user
    349       1.3    chopps 	 */
    350       1.3    chopps 	rdiv = (flags & NTWOC_FLAGS_RXDIV_MASK) >> NTWOC_FLAGS_RXDIV_SHIFT;
    351       1.3    chopps 	if (rdiv > 9)
    352       1.3    chopps 		panic("bad rx divisor in flags");
    353       1.3    chopps 
    354       1.3    chopps 	tdiv = (flags & NTWOC_FLAGS_TXDIV_MASK) >> NTWOC_FLAGS_TXDIV_SHIFT;
    355       1.3    chopps 	if (tdiv > 9)
    356       1.3    chopps 		panic("bad tx divisor in flags");
    357       1.3    chopps 	tmc = (flags & NTWOC_FLAGS_TMC_MASK) >> NTWOC_FLAGS_TMC_SHIFT;
    358       1.3    chopps 
    359       1.3    chopps 	ntwoc_pci_get_clock(&sca->sc_ports[0], flags & NTWOC_FLAGS_CLK0_MASK,
    360       1.3    chopps 	    tmc, rdiv, tdiv);
    361       1.3    chopps 	if (sca->sc_numports > 1)
    362       1.3    chopps 		ntwoc_pci_get_clock(&sca->sc_ports[1],
    363       1.3    chopps 		    (flags & NTWOC_FLAGS_CLK1_MASK) >> NTWOC_FLAGS_CLK1_SHIFT,
    364       1.3    chopps 		    tmc, rdiv, tdiv);
    365       1.3    chopps 
    366      1.12       wiz 	/* allocate DMA'able memory for card to use */
    367       1.3    chopps 	ntwoc_pci_alloc_dma(sca);
    368       1.3    chopps 	ntwoc_pci_setup_dma(sca);
    369       1.3    chopps 
    370       1.3    chopps 	sca_init(sca);
    371       1.1  explorer 
    372       1.1  explorer 	/*
    373       1.1  explorer 	 * always initialize port 0, since we have to have found it to
    374       1.1  explorer 	 * get this far.  If we have two ports, attach the second
    375       1.1  explorer 	 * as well.
    376       1.1  explorer 	 */
    377       1.1  explorer 	sca_port_attach(sca, 0);
    378       1.1  explorer 	if (numports == 2)
    379       1.1  explorer 		sca_port_attach(sca, 1);
    380       1.1  explorer 
    381       1.1  explorer 	/*
    382       1.1  explorer 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
    383       1.1  explorer 	 * doing do could allow DMA to corrupt kernel memory during the
    384       1.1  explorer 	 * reboot before the driver initializes.
    385       1.1  explorer 	 */
    386       1.3    chopps 	shutdownhook_establish(ntwoc_pci_shutdown, sc);
    387       1.3    chopps 
    388       1.3    chopps #if __NetBSD_Version__ >= 104160000
    389       1.3    chopps 	/*
    390       1.3    chopps 	 * defer getting the base clock until interrupts are enabled
    391       1.3    chopps 	 * (and thus we have microtime())
    392       1.3    chopps 	 */
    393       1.3    chopps 	config_interrupts(self, ntwoc_pci_config_interrupts);
    394       1.3    chopps #else
    395       1.3    chopps 	sca->sc_baseclock = SCA_BASECLOCK;
    396       1.3    chopps 	sca_print_clock_info(&sc->sc_sca);
    397       1.3    chopps #endif
    398       1.1  explorer }
    399       1.1  explorer 
    400       1.3    chopps /*
    401       1.3    chopps  * extract the clock information for a port from the flags field
    402       1.3    chopps  */
    403       1.3    chopps static void
    404       1.3    chopps ntwoc_pci_get_clock(struct sca_port *scp, u_int8_t flags, u_int8_t tmc,
    405       1.3    chopps     u_int8_t rdiv, u_int8_t tdiv)
    406       1.3    chopps {
    407       1.3    chopps 	scp->sp_eclock =
    408       1.3    chopps 	    (flags & NTWOC_FLAGS_ECLOCK_MASK) >> NTWOC_FLAGS_ECLOCK_SHIFT;
    409       1.3    chopps 	scp->sp_rxs = rdiv;
    410       1.3    chopps 	scp->sp_txs = tdiv;
    411       1.3    chopps 	scp->sp_tmc = tmc;
    412       1.3    chopps 
    413       1.3    chopps 	/* get rx source */
    414       1.3    chopps 	switch ((flags & NTWOC_FLAGS_RXS_MASK) >> NTWOC_FLAGS_RXS_SHIFT) {
    415       1.3    chopps 	case NTWOC_FLAGS_RXS_LINE:
    416       1.3    chopps 		scp->sp_rxs = 0;
    417       1.3    chopps 		break;
    418       1.3    chopps 	case NTWOC_FLAGS_RXS_LINE_SN:
    419       1.3    chopps 		scp->sp_rxs |= SCA_RXS_CLK_LINE_SN;
    420       1.3    chopps 		break;
    421       1.3    chopps 	case NTWOC_FLAGS_RXS_INTERNAL:
    422       1.3    chopps 		scp->sp_rxs |= SCA_RXS_CLK_INTERNAL;
    423       1.3    chopps 		break;
    424       1.3    chopps 	case NTWOC_FLAGS_RXS_ADPLL_OUT:
    425       1.3    chopps 		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_OUT;
    426       1.3    chopps 		break;
    427       1.3    chopps 	case NTWOC_FLAGS_RXS_ADPLL_IN:
    428       1.3    chopps 		scp->sp_rxs |= SCA_RXS_CLK_ADPLL_IN;
    429       1.3    chopps 		break;
    430       1.3    chopps 	default:
    431       1.3    chopps 		panic("bad rx source in flags");
    432       1.3    chopps 	}
    433       1.3    chopps 
    434       1.3    chopps 	/* get tx source */
    435       1.3    chopps 	switch ((flags & NTWOC_FLAGS_TXS_MASK) >> NTWOC_FLAGS_TXS_SHIFT) {
    436       1.3    chopps 	case NTWOC_FLAGS_TXS_LINE:
    437       1.3    chopps 		scp->sp_txs = 0;
    438       1.3    chopps 		break;
    439       1.3    chopps 	case NTWOC_FLAGS_TXS_INTERNAL:
    440       1.3    chopps 		scp->sp_txs |= SCA_TXS_CLK_INTERNAL;
    441       1.3    chopps 		break;
    442       1.3    chopps 	case NTWOC_FLAGS_TXS_RXCLOCK:
    443       1.3    chopps 		scp->sp_txs |= SCA_TXS_CLK_RXCLK;
    444       1.3    chopps 		break;
    445       1.3    chopps 	default:
    446       1.3    chopps 		panic("bad rx source in flags");
    447       1.3    chopps 	}
    448       1.3    chopps }
    449       1.3    chopps 
    450       1.3    chopps 
    451       1.1  explorer static int
    452       1.3    chopps ntwoc_pci_intr(void *arg)
    453       1.1  explorer {
    454       1.1  explorer 	struct ntwoc_pci_softc *sc = (struct ntwoc_pci_softc *)arg;
    455       1.1  explorer 
    456       1.1  explorer 	return sca_hardintr(&sc->sc_sca);
    457       1.1  explorer }
    458       1.1  explorer 
    459       1.1  explorer /*
    460       1.1  explorer  * shut down interrupts and DMA, so we don't trash the kernel on warm
    461       1.1  explorer  * boot.  Also, lower DTR on each port and disable card interrupts.
    462       1.1  explorer  */
    463       1.1  explorer static void
    464       1.3    chopps ntwoc_pci_shutdown(void *aux)
    465       1.1  explorer {
    466       1.1  explorer 	struct ntwoc_pci_softc *sc = aux;
    467       1.1  explorer 	u_int16_t fecr;
    468       1.1  explorer 
    469       1.1  explorer 	/*
    470       1.1  explorer 	 * shut down the SCA ports
    471       1.1  explorer 	 */
    472       1.1  explorer 	sca_shutdown(&sc->sc_sca);
    473       1.1  explorer 
    474       1.1  explorer 	/*
    475      1.10       wiz 	 * disable interrupts for the whole card.  Black magic, see comment
    476       1.1  explorer 	 * above.
    477       1.1  explorer 	 */
    478       1.1  explorer 	bus_space_write_4(sc->sc_asic_iot, sc->sc_asic_ioh,
    479       1.1  explorer 			  0x68, 0x10900);
    480       1.1  explorer 
    481       1.1  explorer 	/*
    482       1.1  explorer 	 * lower DTR on both ports
    483       1.1  explorer 	 */
    484       1.1  explorer 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
    485       1.1  explorer 				sc->sc_sca.sc_ioh, NTWOC_FECR);
    486       1.1  explorer 	fecr |= (NTWOC_FECR_DTR0 | NTWOC_FECR_DTR1);
    487       1.1  explorer 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
    488       1.1  explorer 			  NTWOC_FECR, fecr);
    489       1.1  explorer }
    490       1.1  explorer 
    491       1.1  explorer static void
    492       1.3    chopps ntwoc_pci_dtr_callback(void *aux, int port, int state)
    493       1.1  explorer {
    494       1.1  explorer 	struct ntwoc_pci_softc *sc = aux;
    495       1.1  explorer 	u_int16_t fecr;
    496       1.1  explorer 
    497       1.1  explorer 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
    498       1.1  explorer 				sc->sc_sca.sc_ioh, NTWOC_FECR);
    499       1.1  explorer 
    500       1.3    chopps 	NTWO_DPRINTF(("dtr: port == %d, state == %d, old fecr:  0x%04x\n",
    501       1.1  explorer 		       port, state, fecr));
    502       1.1  explorer 
    503       1.1  explorer 	if (port == 0) {
    504       1.1  explorer 		if (state == 0)
    505       1.1  explorer 			fecr |= NTWOC_FECR_DTR0;
    506       1.1  explorer 		else
    507       1.1  explorer 			fecr &= ~NTWOC_FECR_DTR0;
    508       1.1  explorer 	} else {
    509       1.1  explorer 		if (state == 0)
    510       1.1  explorer 			fecr |= NTWOC_FECR_DTR1;
    511       1.1  explorer 		else
    512       1.1  explorer 			fecr &= ~NTWOC_FECR_DTR1;
    513       1.1  explorer 	}
    514       1.1  explorer 
    515       1.1  explorer 	NTWO_DPRINTF(("new fecr:  0x%04x\n", fecr));
    516       1.1  explorer 
    517       1.1  explorer 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
    518       1.1  explorer 			  NTWOC_FECR, fecr);
    519       1.1  explorer }
    520       1.3    chopps 
    521       1.3    chopps static void
    522       1.3    chopps ntwoc_pci_clock_callback(void *aux, int port, int enable)
    523       1.3    chopps {
    524       1.3    chopps 	struct ntwoc_pci_softc *sc = aux;
    525       1.3    chopps 	u_int16_t fecr;
    526       1.3    chopps 
    527       1.3    chopps 	fecr = bus_space_read_2(sc->sc_sca.sc_iot,
    528       1.3    chopps 				sc->sc_sca.sc_ioh, NTWOC_FECR);
    529       1.3    chopps 
    530       1.3    chopps 	NTWO_DPRINTF(("clk: port == %d, enable == %d, old fecr:  0x%04x\n",
    531       1.3    chopps 		       port, enable, fecr));
    532       1.3    chopps 
    533       1.3    chopps 	if (port == 0) {
    534       1.3    chopps 		if (enable)
    535       1.3    chopps 			fecr |= NTWOC_FECR_ETC0;
    536       1.3    chopps 		else
    537       1.3    chopps 			fecr &= ~NTWOC_FECR_ETC0;
    538       1.3    chopps 	} else {
    539       1.3    chopps 		if (enable)
    540       1.3    chopps 			fecr |= NTWOC_FECR_ETC1;
    541       1.3    chopps 		else
    542       1.3    chopps 			fecr &= ~NTWOC_FECR_ETC1;
    543       1.3    chopps 	}
    544       1.3    chopps 
    545       1.3    chopps 	NTWO_DPRINTF(("new fecr:  0x%04x\n", fecr));
    546       1.3    chopps 
    547       1.3    chopps 	bus_space_write_2(sc->sc_sca.sc_iot, sc->sc_sca.sc_ioh,
    548       1.3    chopps 			  NTWOC_FECR, fecr);
    549       1.3    chopps }
    550       1.3    chopps 
    551       1.3    chopps static int
    552       1.3    chopps ntwoc_pci_alloc_dma(struct sca_softc *sc)
    553       1.3    chopps {
    554       1.3    chopps 	u_int	allocsize;
    555       1.3    chopps 	int	err;
    556       1.3    chopps 	int	rsegs;
    557       1.3    chopps 	u_int	bpp;
    558       1.3    chopps 
    559       1.3    chopps 	/* first initialize the number of descriptors */
    560       1.3    chopps 	sc->sc_ports[0].sp_nrxdesc = NTWOC_NrxBUFS;
    561       1.3    chopps 	sc->sc_ports[0].sp_ntxdesc = NTWOC_NtxBUFS;
    562       1.3    chopps 	if (sc->sc_numports == 2) {
    563       1.3    chopps 		sc->sc_ports[1].sp_nrxdesc = NTWOC_NrxBUFS;
    564       1.3    chopps 		sc->sc_ports[1].sp_ntxdesc = NTWOC_NtxBUFS;
    565       1.3    chopps 	}
    566       1.3    chopps 
    567       1.3    chopps 	NTWO_DPRINTF(("sizeof sca_desc_t: %d bytes\n", sizeof (sca_desc_t)));
    568       1.3    chopps 
    569       1.3    chopps 	bpp = sc->sc_numports * (NTWOC_NtxBUFS + NTWOC_NrxBUFS);
    570       1.3    chopps 
    571       1.3    chopps 	allocsize = bpp * (SCA_BSIZE + sizeof (sca_desc_t));
    572       1.3    chopps 
    573       1.3    chopps 	/*
    574       1.3    chopps 	 * sanity checks:
    575       1.3    chopps 	 *
    576       1.3    chopps 	 * Check the total size of the data buffers, and so on.  The total
    577       1.3    chopps 	 * DMAable space needs to fit within a single 16M region, and the
    578       1.3    chopps 	 * descriptors need to fit within a 64K region.
    579       1.3    chopps 	 */
    580       1.3    chopps 	if (allocsize > 16 * 1024 * 1024)
    581       1.3    chopps 		return 1;
    582       1.3    chopps 	if (bpp * sizeof (sca_desc_t) > 64 * 1024)
    583       1.3    chopps 		return 1;
    584       1.3    chopps 
    585       1.3    chopps 	sc->scu_allocsize = allocsize;
    586       1.3    chopps 
    587       1.3    chopps 	/*
    588       1.3    chopps 	 * Allocate one huge chunk of memory.
    589       1.3    chopps 	 */
    590       1.3    chopps 	if (bus_dmamem_alloc(sc->scu_dmat,
    591       1.3    chopps 			     allocsize,
    592       1.3    chopps 			     SCA_DMA_ALIGNMENT,
    593      1.13       wiz 			     SCA_DMA_BOUNDARY,
    594       1.3    chopps 			     &sc->scu_seg, 1, &rsegs, BUS_DMA_NOWAIT) != 0) {
    595       1.3    chopps 		printf("Could not allocate DMA memory\n");
    596       1.3    chopps 		return 1;
    597       1.3    chopps 	}
    598       1.3    chopps 	NTWO_DPRINTF(("DMA memory allocated:  %d bytes\n", allocsize));
    599       1.3    chopps 
    600       1.3    chopps 	if (bus_dmamem_map(sc->scu_dmat, &sc->scu_seg, 1, allocsize,
    601       1.3    chopps 			   &sc->scu_dma_addr, BUS_DMA_NOWAIT) != 0) {
    602       1.3    chopps 		printf("Could not map DMA memory into kernel space\n");
    603       1.3    chopps 		return 1;
    604       1.3    chopps 	}
    605       1.3    chopps 	NTWO_DPRINTF(("DMA memory mapped\n"));
    606       1.3    chopps 
    607       1.3    chopps 	if (bus_dmamap_create(sc->scu_dmat, allocsize, 2,
    608      1.13       wiz 			      allocsize, SCA_DMA_BOUNDARY,
    609       1.3    chopps 			      BUS_DMA_NOWAIT, &sc->scu_dmam) != 0) {
    610       1.3    chopps 		printf("Could not create DMA map\n");
    611       1.3    chopps 		return 1;
    612       1.3    chopps 	}
    613       1.3    chopps 	NTWO_DPRINTF(("DMA map created\n"));
    614       1.3    chopps 
    615       1.3    chopps 	err = bus_dmamap_load(sc->scu_dmat, sc->scu_dmam, sc->scu_dma_addr,
    616       1.3    chopps 			      allocsize, NULL, BUS_DMA_NOWAIT);
    617       1.3    chopps 	if (err != 0) {
    618       1.3    chopps 		printf("Could not load DMA segment:  %d\n", err);
    619       1.3    chopps 		return 1;
    620       1.3    chopps 	}
    621       1.3    chopps 	NTWO_DPRINTF(("DMA map loaded\n"));
    622       1.3    chopps 
    623       1.3    chopps 	return 0;
    624       1.3    chopps }
    625       1.3    chopps 
    626       1.3    chopps /*
    627       1.3    chopps  * Take the memory allocated with sca_alloc_dma() and divide it among the
    628       1.3    chopps  * two ports.
    629       1.3    chopps  */
    630       1.3    chopps static void
    631       1.3    chopps ntwoc_pci_setup_dma(struct sca_softc *sc)
    632       1.3    chopps {
    633       1.3    chopps 	sca_port_t *scp0, *scp1;
    634       1.3    chopps 	u_int8_t  *vaddr0;
    635       1.3    chopps 	u_int32_t paddr0;
    636       1.3    chopps 	u_long addroff;
    637       1.3    chopps 
    638       1.3    chopps 	/*
    639       1.3    chopps 	 * remember the physical address to 24 bits only, since the upper
    640       1.3    chopps 	 * 8 bits is programed into the device at a different layer.
    641       1.3    chopps 	 */
    642       1.3    chopps 	paddr0 = (sc->scu_dmam->dm_segs[0].ds_addr & 0x00ffffff);
    643       1.3    chopps 	vaddr0 = sc->scu_dma_addr;
    644       1.3    chopps 
    645       1.3    chopps 	/*
    646       1.3    chopps 	 * if we have only one port it gets the full range.  If we have
    647       1.3    chopps 	 * two we need to do a little magic to divide things up.
    648       1.3    chopps 	 *
    649       1.3    chopps 	 * The descriptors will all end up in the front of the area, while
    650       1.3    chopps 	 * the remainder of the buffer is used for transmit and receive
    651       1.3    chopps 	 * data.
    652       1.3    chopps 	 *
    653       1.3    chopps 	 * -------------------- start of memory
    654       1.3    chopps 	 *    tx desc port 0
    655       1.3    chopps 	 *    rx desc port 0
    656       1.3    chopps 	 *    tx desc port 1
    657       1.3    chopps 	 *    rx desc port 1
    658       1.3    chopps 	 *    tx buffer port 0
    659       1.3    chopps 	 *    rx buffer port 0
    660       1.3    chopps 	 *    tx buffer port 1
    661       1.3    chopps 	 *    rx buffer port 1
    662       1.3    chopps 	 * -------------------- end of memory
    663       1.3    chopps 	 */
    664       1.3    chopps 	scp0 = &sc->sc_ports[0];
    665       1.3    chopps 	scp1 = &sc->sc_ports[1];
    666       1.3    chopps 
    667       1.3    chopps 	scp0->sp_txdesc_p = paddr0;
    668       1.3    chopps 	scp0->sp_txdesc = (sca_desc_t *)vaddr0;
    669      1.11    simonb 	addroff = sizeof(sca_desc_t) * scp0->sp_ntxdesc;
    670       1.3    chopps 
    671       1.3    chopps 	/*
    672       1.3    chopps 	 * point to the range following the tx descriptors, and
    673       1.3    chopps 	 * set the rx descriptors there.
    674       1.3    chopps 	 */
    675       1.3    chopps 	scp0->sp_rxdesc_p = paddr0 + addroff;
    676       1.3    chopps 	scp0->sp_rxdesc = (sca_desc_t *)(vaddr0 + addroff);
    677       1.3    chopps 	addroff += sizeof(sca_desc_t) * scp0->sp_nrxdesc;
    678       1.3    chopps 
    679       1.3    chopps 	if (sc->sc_numports == 2) {
    680       1.3    chopps 		scp1->sp_txdesc_p = paddr0 + addroff;
    681       1.3    chopps 		scp1->sp_txdesc = (sca_desc_t *)(vaddr0 + addroff);
    682       1.3    chopps 		addroff += sizeof(sca_desc_t) * scp1->sp_ntxdesc;
    683       1.3    chopps 
    684       1.3    chopps 		scp1->sp_rxdesc_p = paddr0 + addroff;
    685       1.3    chopps 		scp1->sp_rxdesc = (sca_desc_t *)(vaddr0 + addroff);
    686       1.3    chopps 		addroff += sizeof(sca_desc_t) * scp1->sp_nrxdesc;
    687       1.3    chopps 	}
    688       1.3    chopps 
    689       1.3    chopps 	/*
    690       1.3    chopps 	 * point to the memory following the descriptors, and set the
    691       1.3    chopps 	 * transmit buffer there.
    692       1.3    chopps 	 */
    693       1.3    chopps 	scp0->sp_txbuf_p = paddr0 + addroff;
    694       1.3    chopps 	scp0->sp_txbuf = vaddr0 + addroff;
    695       1.3    chopps 	addroff += SCA_BSIZE * scp0->sp_ntxdesc;
    696       1.3    chopps 
    697       1.3    chopps 	/*
    698       1.3    chopps 	 * lastly, skip over the transmit buffer and set up pointers into
    699       1.3    chopps 	 * the receive buffer.
    700       1.3    chopps 	 */
    701       1.3    chopps 	scp0->sp_rxbuf_p = paddr0 + addroff;
    702       1.3    chopps 	scp0->sp_rxbuf = vaddr0 + addroff;
    703       1.3    chopps 	addroff += SCA_BSIZE * scp0->sp_nrxdesc;
    704       1.3    chopps 
    705       1.3    chopps 	if (sc->sc_numports == 2) {
    706       1.3    chopps 		scp1->sp_txbuf_p = paddr0 + addroff;
    707       1.3    chopps 		scp1->sp_txbuf = vaddr0 + addroff;
    708       1.3    chopps 		addroff += SCA_BSIZE * scp1->sp_ntxdesc;
    709       1.3    chopps 
    710       1.3    chopps 		scp1->sp_rxbuf_p = paddr0 + addroff;
    711       1.3    chopps 		scp1->sp_rxbuf = vaddr0 + addroff;
    712       1.3    chopps 		addroff += SCA_BSIZE * scp1->sp_nrxdesc;
    713       1.3    chopps 	}
    714       1.3    chopps 
    715       1.3    chopps 	/*
    716       1.3    chopps 	 * as a consistancy check, addroff should be equal to the allocation
    717       1.3    chopps 	 * size.
    718       1.3    chopps 	 */
    719       1.3    chopps 	if (sc->scu_allocsize != addroff)
    720       1.3    chopps 		printf("ERROR:  scu_allocsize != addroff: %lu != %lu\n",
    721       1.5       mrg 		       (u_long)sc->scu_allocsize, addroff);
    722       1.3    chopps }
    723       1.3    chopps 
    724       1.3    chopps #if __NetBSD_Version__ >= 104160000
    725       1.3    chopps static void
    726      1.24    cegger ntwoc_pci_config_interrupts(device_t self)
    727       1.3    chopps {
    728       1.3    chopps 	struct ntwoc_pci_softc *sc;
    729       1.3    chopps 
    730  1.27.2.1      yamt 	sc = device_private(self);
    731       1.3    chopps 	sca_get_base_clock(&sc->sc_sca);
    732       1.3    chopps 	sca_print_clock_info(&sc->sc_sca);
    733       1.3    chopps }
    734       1.3    chopps #endif
    735