Home | History | Annotate | Line # | Download | only in apbus
if_tlp_ap.c revision 1.6
      1 /*	$NetBSD: if_tlp_ap.c,v 1.6 2003/07/15 02:59:28 lukem 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 2003/07/15 02:59:28 lukem 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 __P((struct tulip_softc *, u_int32_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 __P((struct device *, struct cfdata *, void *));
     91 static void	tlp_ap_attach __P((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 __P((struct tulip_softc *));
     97 static void tlp_ap_tmsw_init __P((struct tulip_softc *));
     98 static void tlp_ap_getmedia __P((struct tulip_softc *, struct ifmediareq *));
     99 static int tlp_ap_setmedia __P((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(parent, cf, aux)
    107 	struct device *parent;
    108 	struct cfdata *cf;
    109 	void *aux;
    110 {
    111 	struct apbus_attach_args *apa = aux;
    112 
    113 	if (strcmp(apa->apa_name, "cbasetx") != 0)
    114 		return 0;
    115 
    116 	return 1;
    117 }
    118 
    119 /*
    120  * Install interface into kernel networking data structures
    121  */
    122 static void
    123 tlp_ap_attach(parent, self, aux)
    124 	struct device *parent, *self;
    125 	void   *aux;
    126 {
    127 	struct tulip_ap_softc *psc = (void *) self;
    128 	struct tulip_softc *sc = &psc->sc_tulip;
    129 	struct apbus_attach_args *apa = aux;
    130 	u_int8_t enaddr[ETHER_ADDR_LEN];
    131 	u_int intrmask;
    132 	int i;
    133 
    134 	printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
    135 
    136 	/* PCI configuration register */
    137 	psc->sc_cfst = 0;
    138 	psc->sc_cfsh = apa->apa_hwbase + TLP_AP_CFG;
    139 	sc->sc_devno = apa->apa_slotno;
    140 	sc->sc_rev = bus_space_read_4(psc->sc_cfst, psc->sc_cfsh,
    141 	    TLP_AP_CFG_CFRV);
    142 	switch (bus_space_read_4(psc->sc_cfst, psc->sc_cfsh, TLP_AP_CFG_CFID)) {
    143 	case 0x00091011:
    144 		if (sc->sc_rev >= 0x20)
    145 			sc->sc_chip = TULIP_CHIP_21140A;
    146 		else
    147 			sc->sc_chip = TULIP_CHIP_21140;
    148 		break;
    149 	default:
    150 		printf(": unable to handle your board\n");
    151 		return;
    152 	}
    153 
    154 	printf(": %s Ethernet, pass %d.%d\n",
    155 	    tlp_chip_names[sc->sc_chip],
    156 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    157 
    158 	/* CSR */
    159 	sc->sc_st = 0;
    160 	sc->sc_sh = apa->apa_hwbase + TLP_AP_CSR;
    161 	sc->sc_dmat = apbus_dmatag_init(apa);
    162 	if (sc->sc_dmat == NULL) {
    163 		printf("%s: cannot allocate memory\n", sc->sc_dev.dv_xname);
    164 		return;
    165 	}
    166 
    167 	/*
    168 	 * Initialize bus specific parameters.
    169 	 */
    170 	if (mips_sdcache_line_size > 0)
    171 		sc->sc_cacheline = mips_sdcache_line_size / 4;
    172 	else if (mips_pdcache_line_size > 0)
    173 		sc->sc_cacheline = mips_pdcache_line_size / 4;
    174 	else
    175 		sc->sc_cacheline = 4;
    176 	sc->sc_maxburst = sc->sc_cacheline;		/* XXX */
    177 	sc->sc_regshift = 3;
    178 	sc->sc_flags |= TULIPF_DBO | TULIPF_BLE;	/* Big Endian BUS */
    179 	sc->sc_flags |= TULIPF_ENABLED;			/* No Power Mgmt */
    180 
    181 	/*
    182 	 * Reset hardware.
    183 	 */
    184 	bus_space_write_4(0, apa->apa_hwbase + TLP_AP_RST, 0, 1);
    185 	delay(100);
    186 
    187 	/*
    188 	 * Initialize PCI configuration register
    189 	 */
    190 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    191 	    TLP_AP_CFG_CFCS, 0x00000005);	/* Master, IO */
    192 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    193 	    TLP_AP_CFG_CFLT, 0x00000100);
    194 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    195 	    TLP_AP_CFG_CBIO, MIPS_KSEG1_TO_PHYS(sc->sc_sh));
    196 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
    197 	    TLP_AP_CFG_CFIT, 0x00000101);
    198 
    199 	/*
    200 	 * Initialize general purpose port register.
    201 	 */
    202 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xc0);	/* read */
    203 	TULIP_WRITE(sc, CSR_GPP, 0x40);			/* dipsw port on */
    204 	i = TULIP_READ(sc, CSR_GPP) & GPP_MD;		/* dipsw contents */
    205 	TULIP_WRITE(sc, CSR_GPP, 0xc0);			/* dipsw port off */
    206 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf);	/* read write */
    207 	if (sc->sc_maxburst == 16) {
    208 		TULIP_WRITE(sc, CSR_GPP, 0x8f);		/* 16word burst */
    209 		TULIP_WRITE(sc, CSR_GPP, 0xcf);
    210 	} else {
    211 		TULIP_WRITE(sc, CSR_GPP, 0x8e);		/* 8word burst */
    212 		TULIP_WRITE(sc, CSR_GPP, 0xce);
    213 	}
    214 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf);	/* read write */
    215 	TULIP_WRITE(sc, CSR_GPP, 0xc3);			/* mask abort/DMA err */
    216 	TULIP_WRITE(sc, CSR_GPP, 0xcf);			/* mask abort/DMA err */
    217 
    218 	if (tlp_read_srom(sc) == 0) {
    219 		printf("%s: srom read failed\n", sc->sc_dev.dv_xname);
    220 		free(sc->sc_dmat, M_DEVBUF);
    221 		return;
    222 	}
    223 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    224 		enaddr[i] = sc->sc_srom[0x11 + i * 2];
    225 
    226 	sc->sc_mediasw = &tlp_ap_mediasw;
    227 
    228 	/*
    229 	 * Finish off the attach.
    230 	 */
    231 	tlp_attach(sc, enaddr);
    232 
    233 	intrmask = SLOTTOMASK(apa->apa_slotno);
    234 	apbus_intr_establish(0, /* interrupt level (0 or 1) */
    235 			     intrmask,
    236 			     0, /* priority */
    237 			     tlp_intr, sc, apa->apa_name, apa->apa_ctlnum);
    238 }
    239 
    240 static void
    241 tlp_ap_preinit(sc)
    242 	struct tulip_softc *sc;
    243 {
    244 
    245 	sc->sc_opmode |= OPMODE_MBO | OPMODE_SCR | OPMODE_PCS | OPMODE_HBD |
    246 	    OPMODE_PS;
    247 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    248 }
    249 
    250 static void
    251 tlp_ap_tmsw_init(sc)
    252 	struct tulip_softc *sc;
    253 {
    254 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    255 
    256 	sc->sc_preinit = tlp_ap_preinit;
    257 
    258 	sc->sc_mii.mii_ifp = ifp;
    259 	sc->sc_mii.mii_readreg = NULL;
    260 	sc->sc_mii.mii_writereg = NULL;
    261 	sc->sc_mii.mii_statchg = sc->sc_statchg;
    262 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
    263 	    tlp_mediastatus);
    264 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX, 0, NULL);
    265 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX|IFM_FDX, 0,
    266 	    NULL);
    267 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX);
    268 }
    269 
    270 static void
    271 tlp_ap_getmedia(sc, ifmr)
    272 	struct tulip_softc *sc;
    273 	struct ifmediareq *ifmr;
    274 {
    275 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    276 
    277 	ifmr->ifm_status = IFM_AVALID;
    278 	if (ifp->if_flags & IFF_RUNNING)
    279 		ifmr->ifm_status |= IFM_ACTIVE;
    280 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    281 }
    282 
    283 static int
    284 tlp_ap_setmedia(sc)
    285 	struct tulip_softc *sc;
    286 {
    287 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    288 
    289 	sc->sc_mii.mii_media_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
    290 
    291 	if (ifp->if_flags & IFF_UP)
    292 		tlp_idle(sc, OPMODE_ST|OPMODE_SR);
    293 	if (sc->sc_mii.mii_media_active & IFM_FDX)
    294 		sc->sc_opmode |= OPMODE_FD;
    295 	else
    296 		sc->sc_opmode &= ~OPMODE_FD;
    297 	if (ifp->if_flags & IFF_UP)
    298 		TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    299 	return (0);
    300 }
    301