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