1 /* $OpenBSD: if_hme_pci.c,v 1.25 2024/05/24 06:02:53 jsg Exp $ */ 2 /* $NetBSD: if_hme_pci.c,v 1.3 2000/12/28 22:59:13 sommerfeld Exp $ */ 3 4 /* 5 * Copyright (c) 2000 Matthew R. Green 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * PCI front-end device driver for the HME ethernet device. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/device.h> 37 38 #include <net/if.h> 39 #include <net/if_media.h> 40 41 #include <netinet/in.h> 42 #include <netinet/if_ether.h> 43 44 #include <dev/mii/miivar.h> 45 46 #ifdef __sparc64__ 47 #include <machine/autoconf.h> 48 #include <dev/ofw/openfirm.h> 49 #endif 50 51 #include <dev/pci/pcivar.h> 52 #include <dev/pci/pcireg.h> 53 #include <dev/pci/pcidevs.h> 54 55 #include <dev/ic/hmevar.h> 56 57 struct hme_pci_softc { 58 struct hme_softc hsc_hme; /* HME device */ 59 bus_space_tag_t hsc_memt; 60 bus_space_handle_t hsc_memh; 61 bus_size_t hsc_memsize; 62 void *hsc_ih; 63 pci_chipset_tag_t hsc_pc; 64 }; 65 66 int hmematch_pci(struct device *, void *, void *); 67 void hmeattach_pci(struct device *, struct device *, void *); 68 int hmedetach_pci(struct device *, int); 69 int hme_pci_enaddr(struct hme_softc *, struct pci_attach_args *); 70 71 const struct cfattach hme_pci_ca = { 72 sizeof(struct hme_pci_softc), hmematch_pci, hmeattach_pci, hmedetach_pci 73 }; 74 75 int 76 hmematch_pci(struct device *parent, void *vcf, void *aux) 77 { 78 struct pci_attach_args *pa = aux; 79 80 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN && 81 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_HME) 82 return (1); 83 84 return (0); 85 } 86 87 #define PCI_EBUS2_BOOTROM 0x10 88 #define PCI_EBUS2_BOOTROM_SIZE 0x20000 89 #define PROMHDR_PTR_DATA 0x18 90 #define PROMDATA_PTR_VPD 0x08 91 #define PROMDATA_LENGTH 0x0a 92 #define PROMDATA_REVISION 0x0c 93 #define PROMDATA_SUBCLASS 0x0e 94 #define PROMDATA_CLASS 0x0f 95 96 static const u_int8_t hme_promhdr[] = { 0x55, 0xaa }; 97 static const u_int8_t hme_promdat[] = { 98 'P', 'C', 'I', 'R', 99 PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8, 100 PCI_PRODUCT_SUN_HME & 0xff, PCI_PRODUCT_SUN_HME >> 8 101 }; 102 103 int 104 hme_pci_enaddr(struct hme_softc *sc, struct pci_attach_args *hpa) 105 { 106 struct pci_attach_args epa; 107 struct pci_vpd *vpd; 108 pcireg_t cl, id; 109 bus_space_handle_t romh; 110 bus_space_tag_t romt; 111 bus_size_t romsize = 0; 112 u_int8_t buf[32]; 113 int dataoff, vpdoff, length; 114 115 /* 116 * Dig out VPD (vital product data) and acquire Ethernet address. 117 * The VPD of hme resides in the Boot PROM (PCI FCode) attached 118 * to the EBus interface. 119 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later) 120 * chapter 2 describes the data structure. 121 */ 122 123 /* get a PCI tag for the EBus bridge (function 0 of the same device) */ 124 epa = *hpa; 125 epa.pa_tag = pci_make_tag(hpa->pa_pc, hpa->pa_bus, hpa->pa_device, 0); 126 cl = pci_conf_read(epa.pa_pc, epa.pa_tag, PCI_CLASS_REG); 127 id = pci_conf_read(epa.pa_pc, epa.pa_tag, PCI_ID_REG); 128 129 if (PCI_CLASS(cl) != PCI_CLASS_BRIDGE || 130 PCI_PRODUCT(id) != PCI_PRODUCT_SUN_EBUS) 131 goto fail; 132 133 if (pci_mapreg_map(&epa, PCI_EBUS2_BOOTROM, PCI_MAPREG_TYPE_MEM, 0, 134 &romt, &romh, 0, &romsize, PCI_EBUS2_BOOTROM_SIZE)) 135 goto fail; 136 137 bus_space_read_region_1(romt, romh, 0, buf, sizeof(buf)); 138 if (bcmp(buf, hme_promhdr, sizeof(hme_promhdr))) 139 goto fail; 140 141 dataoff = buf[PROMHDR_PTR_DATA] | (buf[PROMHDR_PTR_DATA + 1] << 8); 142 if (dataoff < 0x1c) 143 goto fail; 144 145 bus_space_read_region_1(romt, romh, dataoff, buf, sizeof(buf)); 146 if (bcmp(buf, hme_promdat, sizeof(hme_promdat))) 147 goto fail; 148 149 /* 150 * Don't check the interface part of the class code, since 151 * some cards have a bogus value there. 152 */ 153 length = buf[PROMDATA_LENGTH] | (buf[PROMDATA_LENGTH + 1] << 8); 154 if (length != 0x18 || buf[PROMDATA_REVISION] != 0x00 || 155 buf[PROMDATA_SUBCLASS] != PCI_SUBCLASS_NETWORK_ETHERNET || 156 buf[PROMDATA_CLASS] != PCI_CLASS_NETWORK) 157 goto fail; 158 159 vpdoff = buf[PROMDATA_PTR_VPD] | (buf[PROMDATA_PTR_VPD + 1] << 8); 160 if (vpdoff < 0x1c) 161 goto fail; 162 163 /* 164 * The VPD of hme is not in PCI 2.2 standard format. The length 165 * in the resource header is in big endian, and resources are not 166 * properly terminated (only one resource and no end tag). 167 */ 168 bus_space_read_region_1(romt, romh, vpdoff, buf, sizeof(buf)); 169 170 /* XXX TODO: Get the data from VPD */ 171 vpd = (struct pci_vpd *)(buf + 3); 172 if (!PCI_VPDRES_ISLARGE(buf[0]) || 173 PCI_VPDRES_LARGE_NAME(buf[0]) != PCI_VPDRES_TYPE_VPD) 174 goto fail; 175 if (vpd->vpd_key0 != 'N' || vpd->vpd_key1 != 'A') 176 goto fail; 177 178 bcopy(buf + 6, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN); 179 sc->sc_arpcom.ac_enaddr[5] += hpa->pa_device; 180 bus_space_unmap(romt, romh, romsize); 181 return (0); 182 183 fail: 184 if (romsize != 0) 185 bus_space_unmap(romt, romh, romsize); 186 return (-1); 187 } 188 189 void 190 hmeattach_pci(struct device *parent, struct device *self, void *aux) 191 { 192 struct pci_attach_args *pa = aux; 193 struct hme_pci_softc *hsc = (void *)self; 194 struct hme_softc *sc = &hsc->hsc_hme; 195 pci_intr_handle_t ih; 196 /* XXX the following declarations should be elsewhere */ 197 extern void myetheraddr(u_char *); 198 pcireg_t csr; 199 const char *intrstr = NULL; 200 int type, gotenaddr = 0; 201 202 hsc->hsc_pc = pa->pa_pc; 203 204 /* 205 * enable io/memory-space accesses. this is kinda of gross; but 206 * the hme comes up with neither IO space enabled, or memory space. 207 */ 208 if (pa->pa_memt) 209 pa->pa_flags |= PCI_FLAGS_MEM_ENABLED; 210 if (pa->pa_iot) 211 pa->pa_flags |= PCI_FLAGS_IO_ENABLED; 212 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 213 if (pa->pa_memt) { 214 type = PCI_MAPREG_TYPE_MEM; 215 csr |= PCI_COMMAND_MEM_ENABLE; 216 sc->sc_bustag = pa->pa_memt; 217 } else { 218 type = PCI_MAPREG_TYPE_IO; 219 csr |= PCI_COMMAND_IO_ENABLE; 220 sc->sc_bustag = pa->pa_iot; 221 } 222 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 223 csr | PCI_COMMAND_MEM_ENABLE); 224 225 sc->sc_dmatag = pa->pa_dmat; 226 227 sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */ 228 /* 229 * Map five register banks: 230 * 231 * bank 0: HME SEB registers: +0x0000 232 * bank 1: HME ETX registers: +0x2000 233 * bank 2: HME ERX registers: +0x4000 234 * bank 3: HME MAC registers: +0x6000 235 * bank 4: HME MIF registers: +0x7000 236 * 237 */ 238 239 #define PCI_HME_BASEADDR 0x10 240 if (pci_mapreg_map(pa, PCI_HME_BASEADDR, type, 0, 241 &hsc->hsc_memt, &hsc->hsc_memh, NULL, &hsc->hsc_memsize, 0) != 0) { 242 printf(": can't map registers\n"); 243 return; 244 } 245 sc->sc_seb = hsc->hsc_memh; 246 bus_space_subregion(sc->sc_bustag, hsc->hsc_memh, 0x2000, 0x2000, 247 &sc->sc_etx); 248 bus_space_subregion(sc->sc_bustag, hsc->hsc_memh, 0x4000, 0x2000, 249 &sc->sc_erx); 250 bus_space_subregion(sc->sc_bustag, hsc->hsc_memh, 0x6000, 0x1000, 251 &sc->sc_mac); 252 bus_space_subregion(sc->sc_bustag, hsc->hsc_memh, 0x7000, 0x1000, 253 &sc->sc_mif); 254 255 if (hme_pci_enaddr(sc, pa) == 0) 256 gotenaddr = 1; 257 258 #ifdef __sparc64__ 259 if (!gotenaddr) { 260 if (OF_getprop(PCITAG_NODE(pa->pa_tag), "local-mac-address", 261 sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0) 262 myetheraddr(sc->sc_arpcom.ac_enaddr); 263 gotenaddr = 1; 264 } 265 #endif 266 #ifdef __powerpc__ 267 if (!gotenaddr) { 268 pci_ether_hw_addr(pa->pa_pc, sc->sc_arpcom.ac_enaddr); 269 gotenaddr = 1; 270 } 271 #endif 272 273 sc->sc_burst = 16; /* XXX */ 274 275 if (pci_intr_map(pa, &ih) != 0) { 276 printf(": couldn't map interrupt\n"); 277 bus_space_unmap(hsc->hsc_memt, hsc->hsc_memh, hsc->hsc_memsize); 278 return; 279 } 280 intrstr = pci_intr_string(pa->pa_pc, ih); 281 hsc->hsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, 282 hme_intr, sc, self->dv_xname); 283 if (hsc->hsc_ih == NULL) { 284 printf(": couldn't establish interrupt"); 285 if (intrstr != NULL) 286 printf(" at %s", intrstr); 287 printf("\n"); 288 bus_space_unmap(hsc->hsc_memt, hsc->hsc_memh, hsc->hsc_memsize); 289 return; 290 } 291 292 printf(": %s", intrstr); 293 294 /* 295 * call the main configure 296 */ 297 hme_config(sc); 298 } 299 300 int 301 hmedetach_pci(struct device *self, int flags) 302 { 303 struct hme_pci_softc *hsc = (void *)self; 304 struct hme_softc *sc = &hsc->hsc_hme; 305 306 timeout_del(&sc->sc_tick_ch); 307 pci_intr_disestablish(hsc->hsc_pc, hsc->hsc_ih); 308 309 hme_unconfig(sc); 310 bus_space_unmap(hsc->hsc_memt, hsc->hsc_memh, hsc->hsc_memsize); 311 return (0); 312 } 313