1 /* $NetBSD: if_hme_pci.c,v 1.20 2005/12/11 12:22:49 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2000 Matthew R. Green 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * PCI front-end device driver for the HME ethernet device. 33 */ 34 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: if_hme_pci.c,v 1.20 2005/12/11 12:22:49 christos Exp $"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/syslog.h> 41 #include <sys/device.h> 42 #include <sys/malloc.h> 43 #include <sys/socket.h> 44 45 #include <net/if.h> 46 #include <net/if_dl.h> 47 #include <net/if_ether.h> 48 #include <net/if_media.h> 49 50 #include <dev/mii/mii.h> 51 #include <dev/mii/miivar.h> 52 53 #include <machine/intr.h> 54 55 #include <dev/pci/pcivar.h> 56 #include <dev/pci/pcireg.h> 57 #include <dev/pci/pcidevs.h> 58 59 #include <dev/ic/hmevar.h> 60 #ifdef __sparc__ 61 #include <machine/promlib.h> 62 #endif 63 64 #ifndef HME_USE_LOCAL_MAC_ADDRESS 65 #ifdef __sparc__ 66 #define HME_USE_LOCAL_MAC_ADDRESS 0 /* use system-wide address */ 67 #else 68 #define HME_USE_LOCAL_MAC_ADDRESS 1 69 #endif 70 #endif 71 72 struct hme_pci_softc { 73 struct hme_softc hsc_hme; /* HME device */ 74 bus_space_tag_t hsc_memt; 75 bus_space_handle_t hsc_memh; 76 void *hsc_ih; 77 }; 78 79 int hmematch_pci(struct device *, struct cfdata *, void *); 80 void hmeattach_pci(struct device *, struct device *, void *); 81 82 CFATTACH_DECL(hme_pci, sizeof(struct hme_pci_softc), 83 hmematch_pci, hmeattach_pci, NULL, NULL); 84 85 int 86 hmematch_pci(parent, cf, aux) 87 struct device *parent; 88 struct cfdata *cf; 89 void *aux; 90 { 91 struct pci_attach_args *pa = aux; 92 93 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN && 94 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_HMENETWORK) 95 return (1); 96 97 return (0); 98 } 99 100 #if HME_USE_LOCAL_MAC_ADDRESS 101 static inline int 102 hmepromvalid(u_int8_t* buf) 103 { 104 return buf[0] == 0x18 && buf[1] == 0x00 && /* structure length */ 105 buf[2] == 0x00 && /* revision */ 106 (buf[3] == 0x00 || /* hme */ 107 buf[3] == 0x80) && /* qfe */ 108 buf[4] == PCI_SUBCLASS_NETWORK_ETHERNET && /* subclass code */ 109 buf[5] == PCI_CLASS_NETWORK; /* class code */ 110 } 111 112 static inline int 113 hmevpdoff(bus_space_tag_t romt, bus_space_handle_t romh, int vpdoff, int dev) 114 { 115 #define VPDLEN (3 + sizeof(struct pci_vpd) + ETHER_ADDR_LEN) 116 if (bus_space_read_1(romt, romh, vpdoff + VPDLEN) != 0x79 && 117 bus_space_read_1(romt, romh, vpdoff + 4 * VPDLEN) == 0x79) { 118 /* 119 * Use the Nth NA for the Nth HME on 120 * this SUNW,qfe. 121 */ 122 vpdoff += dev * VPDLEN; 123 } 124 return vpdoff; 125 } 126 #endif 127 128 void 129 hmeattach_pci(parent, self, aux) 130 struct device *parent, *self; 131 void *aux; 132 { 133 struct pci_attach_args *pa = aux; 134 struct hme_pci_softc *hsc = (void *)self; 135 struct hme_softc *sc = &hsc->hsc_hme; 136 pci_intr_handle_t ih; 137 pcireg_t csr; 138 const char *intrstr; 139 int type; 140 #if HME_USE_LOCAL_MAC_ADDRESS 141 struct pci_attach_args ebus_pa; 142 pcireg_t ebus_cl, ebus_id; 143 u_int8_t *enaddr; 144 bus_space_tag_t romt; 145 bus_space_handle_t romh; 146 bus_size_t romsize; 147 u_int8_t buf[64]; 148 int dataoff, vpdoff; 149 struct pci_vpd *vpd; 150 static const u_int8_t promhdr[] = { 0x55, 0xaa }; 151 #define PROMHDR_PTR_DATA 0x18 152 static const u_int8_t promdat[] = { 153 0x50, 0x43, 0x49, 0x52, /* "PCIR" */ 154 PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8, 155 PCI_PRODUCT_SUN_HMENETWORK & 0xff, 156 PCI_PRODUCT_SUN_HMENETWORK >> 8 157 }; 158 #define PROMDATA_PTR_VPD 0x08 159 #define PROMDATA_DATA2 0x0a 160 #endif /* HME_USE_LOCAL_MAC_ADDRESS */ 161 162 printf(": Sun Happy Meal Ethernet, rev. %d\n", 163 PCI_REVISION(pa->pa_class)); 164 165 /* 166 * enable io/memory-space accesses. this is kinda of gross; but 167 # the hme comes up with neither IO space enabled, or memory space. 168 */ 169 if (pa->pa_memt) 170 pa->pa_flags |= PCI_FLAGS_MEM_ENABLED; 171 if (pa->pa_iot) 172 pa->pa_flags |= PCI_FLAGS_IO_ENABLED; 173 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 174 if (pa->pa_memt) { 175 type = PCI_MAPREG_TYPE_MEM; 176 csr |= PCI_COMMAND_MEM_ENABLE; 177 sc->sc_bustag = pa->pa_memt; 178 } else { 179 type = PCI_MAPREG_TYPE_IO; 180 csr |= PCI_COMMAND_IO_ENABLE; 181 sc->sc_bustag = pa->pa_iot; 182 } 183 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 184 csr | PCI_COMMAND_MEM_ENABLE); 185 186 sc->sc_dmatag = pa->pa_dmat; 187 188 sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */ 189 /* 190 * Map five register banks: 191 * 192 * bank 0: HME SEB registers: +0x0000 193 * bank 1: HME ETX registers: +0x2000 194 * bank 2: HME ERX registers: +0x4000 195 * bank 3: HME MAC registers: +0x6000 196 * bank 4: HME MIF registers: +0x7000 197 * 198 */ 199 200 #define PCI_HME_BASEADDR 0x10 201 if (pci_mapreg_map(pa, PCI_HME_BASEADDR, type, 0, 202 &hsc->hsc_memt, &hsc->hsc_memh, NULL, NULL) != 0) 203 { 204 printf("%s: unable to map device registers\n", 205 sc->sc_dev.dv_xname); 206 return; 207 } 208 sc->sc_seb = hsc->hsc_memh; 209 if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x2000, 210 0x1000, &sc->sc_etx)) { 211 printf("%s: unable to subregion ETX registers\n", 212 sc->sc_dev.dv_xname); 213 return; 214 } 215 if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x4000, 216 0x1000, &sc->sc_erx)) { 217 printf("%s: unable to subregion ERX registers\n", 218 sc->sc_dev.dv_xname); 219 return; 220 } 221 if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x6000, 222 0x1000, &sc->sc_mac)) { 223 printf("%s: unable to subregion MAC registers\n", 224 sc->sc_dev.dv_xname); 225 return; 226 } 227 if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x7000, 228 0x1000, &sc->sc_mif)) { 229 printf("%s: unable to subregion MIF registers\n", 230 sc->sc_dev.dv_xname); 231 return; 232 } 233 234 #if HME_USE_LOCAL_MAC_ADDRESS 235 /* 236 * Dig out VPD (vital product data) and acquire Ethernet address. 237 * The VPD of hme resides in the Boot PROM (PCI FCode) attached 238 * to the EBus interface. 239 */ 240 /* 241 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later) 242 * chapter 2 describes the data structure. 243 */ 244 245 enaddr = NULL; 246 247 /* get a PCI tag for the EBus bridge (function 0 of the same device) */ 248 ebus_pa = *pa; 249 ebus_pa.pa_tag = pci_make_tag(pa->pa_pc, pa->pa_bus, pa->pa_device, 0); 250 251 ebus_cl = pci_conf_read(ebus_pa.pa_pc, ebus_pa.pa_tag, PCI_CLASS_REG); 252 ebus_id = pci_conf_read(ebus_pa.pa_pc, ebus_pa.pa_tag, PCI_ID_REG); 253 254 #define PCI_EBUS2_BOOTROM 0x10 255 if (PCI_CLASS(ebus_cl) == PCI_CLASS_BRIDGE && 256 PCI_PRODUCT(ebus_id) == PCI_PRODUCT_SUN_EBUS && 257 pci_mapreg_map(&ebus_pa, PCI_EBUS2_BOOTROM, PCI_MAPREG_TYPE_MEM, 258 BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE, 259 &romt, &romh, 0, &romsize) == 0) { 260 261 /* read PCI Expansion PROM Header */ 262 bus_space_read_region_1(romt, romh, 0, buf, sizeof buf); 263 if (memcmp(buf, promhdr, sizeof promhdr) == 0 && 264 (dataoff = (buf[PROMHDR_PTR_DATA] | 265 (buf[PROMHDR_PTR_DATA + 1] << 8))) >= 0x1c) { 266 267 /* read PCI Expansion PROM Data */ 268 bus_space_read_region_1(romt, romh, dataoff, 269 buf, sizeof buf); 270 if (memcmp(buf, promdat, sizeof promdat) == 0 && 271 hmepromvalid(buf + PROMDATA_DATA2) && 272 (vpdoff = (buf[PROMDATA_PTR_VPD] | 273 (buf[PROMDATA_PTR_VPD + 1] << 8))) >= 0x1c) { 274 275 /* 276 * The VPD of hme is not in PCI 2.2 standard 277 * format. The length in the resource header 278 * is in big endian, and resources are not 279 * properly terminated (only one resource 280 * and no end tag). 281 */ 282 vpdoff = hmevpdoff(romt, romh, vpdoff, 283 pa->pa_device); 284 /* read PCI VPD */ 285 bus_space_read_region_1(romt, romh, 286 vpdoff, buf, sizeof buf); 287 vpd = (void *)(buf + 3); 288 if (PCI_VPDRES_ISLARGE(buf[0]) && 289 PCI_VPDRES_LARGE_NAME(buf[0]) 290 == PCI_VPDRES_TYPE_VPD && 291 /* buf[1] == 0 && buf[2] == 9 && */ /*len*/ 292 vpd->vpd_key0 == 0x4e /* N */ && 293 vpd->vpd_key1 == 0x41 /* A */ && 294 vpd->vpd_len == ETHER_ADDR_LEN) { 295 /* 296 * Ethernet address found 297 */ 298 enaddr = buf + 6; 299 } 300 } 301 } 302 bus_space_unmap(romt, romh, romsize); 303 } 304 305 if (enaddr) 306 memcpy(sc->sc_enaddr, enaddr, ETHER_ADDR_LEN); 307 else 308 #endif /* HME_USE_LOCAL_MAC_ADDRESS */ 309 #ifdef __sparc__ 310 prom_getether(PCITAG_NODE(pa->pa_tag), sc->sc_enaddr); 311 #else 312 printf("%s: no Ethernet address found\n", sc->sc_dev.dv_xname); 313 #endif 314 315 /* 316 * Map and establish our interrupt. 317 */ 318 if (pci_intr_map(pa, &ih) != 0) { 319 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname); 320 return; 321 } 322 intrstr = pci_intr_string(pa->pa_pc, ih); 323 hsc->hsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, hme_intr, sc); 324 if (hsc->hsc_ih == NULL) { 325 printf("%s: unable to establish interrupt", 326 sc->sc_dev.dv_xname); 327 if (intrstr != NULL) 328 printf(" at %s", intrstr); 329 printf("\n"); 330 return; 331 } 332 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 333 334 sc->sc_burst = 16; /* XXX */ 335 336 /* Finish off the attach. */ 337 hme_config(sc); 338 } 339