Home | History | Annotate | Line # | Download | only in apbus
if_tlp_ap.c revision 1.6.8.1
      1 /*	$NetBSD: if_tlp_ap.c,v 1.6.8.1 2005/04/29 11:28:17 kent Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Atsushi Onoe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: if_tlp_ap.c,v 1.6.8.1 2005/04/29 11:28:17 kent Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/device.h>
     44 #include <sys/errno.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/malloc.h>
     47 #include <sys/socket.h>
     48 #include <sys/syslog.h>
     49 #include <sys/systm.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_dl.h>
     53 #include <net/if_media.h>
     54 #include <net/if_ether.h>
     55 
     56 #include <machine/bus.h>
     57 #include <machine/intr.h>
     58 
     59 #include <mips/cache.h>
     60 
     61 #include <dev/mii/miivar.h>
     62 #include <dev/mii/mii_bitbang.h>
     63 
     64 #include <dev/ic/tulipreg.h>
     65 #include <dev/ic/tulipvar.h>
     66 
     67 #include <newsmips/apbus/apbusvar.h>
     68 
     69 #define	TLP_AP_ROM	0x00000000	/* AProm entry address */
     70 #define	TLP_AP_CFG	0x00040000	/* PCI configuration registers */
     71 #define		TLP_AP_CFG_CFID	0x00		/* Identification */
     72 #define		TLP_AP_CFG_CFCS	0x04		/* Command and status */
     73 #define		TLP_AP_CFG_CFRV	0x08		/* Revision */
     74 #define		TLP_AP_CFG_CFLT	0x0c		/* Latency timer */
     75 #define		TLP_AP_CFG_CBIO	0x10		/* Base I/O address */
     76 #define		TLP_AP_CFG_CBMA	0x14		/* Base memory address */
     77 #define		TLP_AP_CFG_CFIT	0x3c		/* Interrupt */
     78 #define	TLP_AP_CSR	0x00080000	/* CSR base address */
     79 #define	TLP_AP_RST	0x00100000	/* Board Reset */
     80 
     81 
     82 extern void tlp_idle(struct tulip_softc *, uint32_t);
     83 
     84 struct tulip_ap_softc {
     85 	struct tulip_softc sc_tulip;	/* real Tulip softc */
     86 	bus_space_tag_t sc_cfst;
     87 	bus_space_handle_t sc_cfsh;
     88 };
     89 
     90 static int	tlp_ap_match(struct device *, struct cfdata *, void *);
     91 static void	tlp_ap_attach(struct device *, struct device *, void *);
     92 
     93 CFATTACH_DECL(tlp_ap, sizeof(struct tulip_ap_softc),
     94     tlp_ap_match, tlp_ap_attach, NULL, NULL);
     95 
     96 static void tlp_ap_preinit(struct tulip_softc *);
     97 static void tlp_ap_tmsw_init(struct tulip_softc *);
     98 static void tlp_ap_getmedia(struct tulip_softc *, struct ifmediareq *);
     99 static int tlp_ap_setmedia(struct tulip_softc *);
    100 
    101 const struct tulip_mediasw tlp_ap_mediasw = {
    102 	tlp_ap_tmsw_init, tlp_ap_getmedia, tlp_ap_setmedia
    103 };
    104 
    105 static int
    106 tlp_ap_match(struct device *parent, struct cfdata *cf, void *aux)
    107 {
    108 	struct apbus_attach_args *apa = aux;
    109 
    110 	if (strcmp(apa->apa_name, "cbasetx") != 0)
    111 		return 0;
    112 
    113 	return 1;
    114 }
    115 
    116 /*
    117  * Install interface into kernel networking data structures
    118  */
    119 static void
    120 tlp_ap_attach(struct device *parent, struct device *self, void *aux)
    121 {
    122 	struct tulip_ap_softc *psc = (void *) self;
    123 	struct tulip_softc *sc = &psc->sc_tulip;
    124 	struct apbus_attach_args *apa = aux;
    125 	uint8_t enaddr[ETHER_ADDR_LEN];
    126 	u_int intrmask;
    127 	int i;
    128 
    129 	printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
    130 
    131 	/* PCI configuration register */
    132 	psc->sc_cfst = 0;
    133 	psc->sc_cfsh = apa->apa_hwbase + TLP_AP_CFG;
    134 	sc->sc_devno = apa->apa_slotno;
    135 	sc->sc_rev = bus_space_read_4(psc->sc_cfst, psc->sc_cfsh,
    136 	    TLP_AP_CFG_CFRV);
    137 	switch (bus_space_read_4(psc->sc_cfst, psc->sc_cfsh, TLP_AP_CFG_CFID)) {
    138 	case 0x00091011:
    139 		if (sc->sc_rev >= 0x20)
    140 			sc->sc_chip = TULIP_CHIP_21140A;
    141 		else
    142 			sc->sc_chip = TULIP_CHIP_21140;
    143 		break;
    144 	default:
    145 		printf(": unable to handle your board\n");
    146 		return;
    147 	}
    148 
    149 	printf(": %s Ethernet, pass %d.%d\n",
    150 	    tlp_chip_names[sc->sc_chip],
    151 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    152 
    153 	/* CSR */
    154 	sc->sc_st = 0;
    155 	sc->sc_sh = apa->apa_hwbase + TLP_AP_CSR;
    156 	sc->sc_dmat = apbus_dmatag_init(apa);
    157 	if (sc->sc_dmat == NULL) {
    158 		printf("%s: cannot allocate memory\n", sc->sc_dev.dv_xname);
    159 		return;
    160 	}
    161 
    162 	/*
    163 	 * Initialize bus specific parameters.
    164 	 */
    165 	if (mips_sdcache_line_size > 0)
    166 		sc->sc_cacheline = mips_sdcache_line_size / 4;
    167 	else if (mips_pdcache_line_size > 0)
    168 		sc->sc_cacheline = mips_pdcache_line_size / 4;
    169 	else
    170 		sc->sc_cacheline = 4;
    171 	sc->sc_maxburst = sc->sc_cacheline;		/* XXX */
    172 	sc->sc_regshift = 3;
    173 	sc->sc_flags |= TULIPF_DBO | TULIPF_BLE;	/* Big Endian BUS */
    174 	sc->sc_flags |= TULIPF_ENABLED;			/* No Power Mgmt */
    175 
    176 	/*
    177 	 * Reset hardware.
    178 	 */
    179 	bus_space_write_4(0, apa->apa_hwbase + TLP_AP_RST, 0, 1);
    180 	delay(100);
    181 
    182 	/*
    183 	 * Initialize PCI configuration register
    184 	 */
    185 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    186 	    TLP_AP_CFG_CFCS, 0x00000005);	/* Master, IO */
    187 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    188 	    TLP_AP_CFG_CFLT, 0x00000100);
    189 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    190 	    TLP_AP_CFG_CBIO, MIPS_KSEG1_TO_PHYS(sc->sc_sh));
    191 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    192 	    TLP_AP_CFG_CFIT, 0x00000101);
    193 
    194 	/*
    195 	 * Initialize general purpose port register.
    196 	 */
    197 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xc0);	/* read */
    198 	TULIP_WRITE(sc, CSR_GPP, 0x40);			/* dipsw port on */
    199 	i = TULIP_READ(sc, CSR_GPP) & GPP_MD;		/* dipsw contents */
    200 	TULIP_WRITE(sc, CSR_GPP, 0xc0);			/* dipsw port off */
    201 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf);	/* read write */
    202 	if (sc->sc_maxburst == 16) {
    203 		TULIP_WRITE(sc, CSR_GPP, 0x8f);		/* 16word burst */
    204 		TULIP_WRITE(sc, CSR_GPP, 0xcf);
    205 	} else {
    206 		TULIP_WRITE(sc, CSR_GPP, 0x8e);		/* 8word burst */
    207 		TULIP_WRITE(sc, CSR_GPP, 0xce);
    208 	}
    209 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf);	/* read write */
    210 	TULIP_WRITE(sc, CSR_GPP, 0xc3);			/* mask abort/DMA err */
    211 	TULIP_WRITE(sc, CSR_GPP, 0xcf);			/* mask abort/DMA err */
    212 
    213 	if (tlp_read_srom(sc) == 0) {
    214 		printf("%s: srom read failed\n", sc->sc_dev.dv_xname);
    215 		free(sc->sc_dmat, M_DEVBUF);
    216 		return;
    217 	}
    218 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    219 		enaddr[i] = sc->sc_srom[0x11 + i * 2];
    220 
    221 	sc->sc_mediasw = &tlp_ap_mediasw;
    222 
    223 	/*
    224 	 * Finish off the attach.
    225 	 */
    226 	tlp_attach(sc, enaddr);
    227 
    228 	intrmask = SLOTTOMASK(apa->apa_slotno);
    229 	apbus_intr_establish(0, /* interrupt level (0 or 1) */
    230 	    intrmask,
    231 	    0, /* priority */
    232 	    tlp_intr, sc, apa->apa_name, apa->apa_ctlnum);
    233 }
    234 
    235 static void
    236 tlp_ap_preinit(struct tulip_softc *sc)
    237 {
    238 
    239 	sc->sc_opmode |= OPMODE_MBO | OPMODE_SCR | OPMODE_PCS | OPMODE_HBD |
    240 	    OPMODE_PS;
    241 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    242 }
    243 
    244 static void
    245 tlp_ap_tmsw_init(struct tulip_softc *sc)
    246 {
    247 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    248 
    249 	sc->sc_preinit = tlp_ap_preinit;
    250 
    251 	sc->sc_mii.mii_ifp = ifp;
    252 	sc->sc_mii.mii_readreg = NULL;
    253 	sc->sc_mii.mii_writereg = NULL;
    254 	sc->sc_mii.mii_statchg = sc->sc_statchg;
    255 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
    256 	    tlp_mediastatus);
    257 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX, 0, NULL);
    258 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX|IFM_FDX, 0,
    259 	    NULL);
    260 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX);
    261 }
    262 
    263 static void
    264 tlp_ap_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
    265 {
    266 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    267 
    268 	ifmr->ifm_status = IFM_AVALID;
    269 	if (ifp->if_flags & IFF_RUNNING)
    270 		ifmr->ifm_status |= IFM_ACTIVE;
    271 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    272 }
    273 
    274 static int
    275 tlp_ap_setmedia(struct tulip_softc *sc)
    276 {
    277 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    278 
    279 	sc->sc_mii.mii_media_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
    280 
    281 	if (ifp->if_flags & IFF_UP)
    282 		tlp_idle(sc, OPMODE_ST|OPMODE_SR);
    283 	if (sc->sc_mii.mii_media_active & IFM_FDX)
    284 		sc->sc_opmode |= OPMODE_FD;
    285 	else
    286 		sc->sc_opmode &= ~OPMODE_FD;
    287 	if (ifp->if_flags & IFF_UP)
    288 		TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    289 	return 0;
    290 }
    291