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