2005-04-21 22:31:51 +04:00
|
|
|
/* $NetBSD: if_hme_pci.c,v 1.18 2005/04/21 18:31:51 christos Exp $ */
|
2000-04-05 06:31:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Matthew R. Green
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PCI front-end device driver for the HME ethernet device.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 10:48:40 +03:00
|
|
|
#include <sys/cdefs.h>
|
2005-04-21 22:31:51 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: if_hme_pci.c,v 1.18 2005/04/21 18:31:51 christos Exp $");
|
2001-11-13 10:48:40 +03:00
|
|
|
|
2000-04-05 06:31:19 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_ether.h>
|
|
|
|
#include <net/if_media.h>
|
|
|
|
|
|
|
|
#include <dev/mii/mii.h>
|
|
|
|
#include <dev/mii/miivar.h>
|
|
|
|
|
2001-08-28 02:37:33 +04:00
|
|
|
#include <machine/intr.h>
|
2000-04-05 06:31:19 +04:00
|
|
|
|
|
|
|
#include <dev/pci/pcivar.h>
|
|
|
|
#include <dev/pci/pcireg.h>
|
|
|
|
#include <dev/pci/pcidevs.h>
|
|
|
|
|
|
|
|
#include <dev/ic/hmevar.h>
|
2004-03-17 11:58:23 +03:00
|
|
|
#ifdef __sparc__
|
|
|
|
#include <machine/promlib.h>
|
|
|
|
#endif
|
2000-04-05 06:31:19 +04:00
|
|
|
|
2002-12-27 01:59:51 +03:00
|
|
|
#ifndef HME_USE_LOCAL_MAC_ADDRESS
|
|
|
|
#ifdef __sparc__
|
|
|
|
#define HME_USE_LOCAL_MAC_ADDRESS 0 /* use system-wide address */
|
|
|
|
#else
|
|
|
|
#define HME_USE_LOCAL_MAC_ADDRESS 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2000-04-05 06:31:19 +04:00
|
|
|
struct hme_pci_softc {
|
|
|
|
struct hme_softc hsc_hme; /* HME device */
|
|
|
|
bus_space_tag_t hsc_memt;
|
|
|
|
bus_space_handle_t hsc_memh;
|
|
|
|
void *hsc_ih;
|
|
|
|
};
|
|
|
|
|
2005-02-04 05:10:35 +03:00
|
|
|
int hmematch_pci(struct device *, struct cfdata *, void *);
|
|
|
|
void hmeattach_pci(struct device *, struct device *, void *);
|
2000-04-05 06:31:19 +04:00
|
|
|
|
2002-10-01 00:37:04 +04:00
|
|
|
CFATTACH_DECL(hme_pci, sizeof(struct hme_pci_softc),
|
2002-10-02 20:51:16 +04:00
|
|
|
hmematch_pci, hmeattach_pci, NULL, NULL);
|
2000-04-05 06:31:19 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
hmematch_pci(parent, cf, aux)
|
|
|
|
struct device *parent;
|
|
|
|
struct cfdata *cf;
|
|
|
|
void *aux;
|
|
|
|
{
|
|
|
|
struct pci_attach_args *pa = aux;
|
|
|
|
|
2005-02-27 03:26:58 +03:00
|
|
|
if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
|
2000-04-05 06:31:19 +04:00
|
|
|
PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_HMENETWORK)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:31:51 +04:00
|
|
|
#if HME_USE_LOCAL_MAC_ADDRESS
|
|
|
|
static inline int
|
|
|
|
hmepromvalid(u_int8_t* buf)
|
|
|
|
{
|
|
|
|
return buf[0] == 0x18 && buf[1] == 0x00 && /* structure length */
|
|
|
|
buf[2] == 0x00 && /* revision */
|
|
|
|
(buf[3] == 0x00 || /* hme */
|
|
|
|
buf[3] == 0x80) && /* qfe */
|
|
|
|
buf[4] == PCI_SUBCLASS_NETWORK_ETHERNET && /* subclass code */
|
|
|
|
buf[5] == PCI_CLASS_NETWORK; /* class code */
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
hmevpdoff(bus_space_handle_t romh, bus_space_tag_t romt, int vpdoff, int dev)
|
|
|
|
{
|
|
|
|
#define VPDLEN (3 + sizeof(struct pci_vpd) + ETHER_ADDR_LEN)
|
|
|
|
if (bus_space_read_1(romt, romh, vpdoff + VPDLEN) != 0x79 &&
|
|
|
|
bus_space_read_1(romt, romh, vpdoff + 4 * VPDLEN) == 0x79) {
|
|
|
|
/*
|
|
|
|
* Use the Nth NA for the Nth HME on
|
|
|
|
* this SUNW,qfe.
|
|
|
|
*/
|
|
|
|
vpdoff += dev * VPDLEN;
|
|
|
|
}
|
|
|
|
return vpdoff;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-04-05 06:31:19 +04:00
|
|
|
void
|
|
|
|
hmeattach_pci(parent, self, aux)
|
|
|
|
struct device *parent, *self;
|
|
|
|
void *aux;
|
|
|
|
{
|
|
|
|
struct pci_attach_args *pa = aux;
|
|
|
|
struct hme_pci_softc *hsc = (void *)self;
|
|
|
|
struct hme_softc *sc = &hsc->hsc_hme;
|
2001-10-05 21:49:43 +04:00
|
|
|
pci_intr_handle_t ih;
|
2000-04-05 06:31:19 +04:00
|
|
|
pcireg_t csr;
|
|
|
|
const char *intrstr;
|
|
|
|
int type;
|
2002-12-27 01:59:51 +03:00
|
|
|
#if HME_USE_LOCAL_MAC_ADDRESS
|
|
|
|
struct pci_attach_args ebus_pa;
|
|
|
|
pcireg_t ebus_cl, ebus_id;
|
|
|
|
u_int8_t *enaddr;
|
|
|
|
bus_space_tag_t romt;
|
|
|
|
bus_space_handle_t romh;
|
|
|
|
bus_size_t romsize;
|
2005-04-21 15:35:01 +04:00
|
|
|
u_int8_t buf[64];
|
2002-12-27 01:59:51 +03:00
|
|
|
int dataoff, vpdoff;
|
|
|
|
struct pci_vpd *vpd;
|
|
|
|
static const u_int8_t promhdr[] = { 0x55, 0xaa };
|
|
|
|
#define PROMHDR_PTR_DATA 0x18
|
|
|
|
static const u_int8_t promdat[] = {
|
|
|
|
0x50, 0x43, 0x49, 0x52, /* "PCIR" */
|
|
|
|
PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
|
|
|
|
PCI_PRODUCT_SUN_HMENETWORK & 0xff,
|
|
|
|
PCI_PRODUCT_SUN_HMENETWORK >> 8
|
|
|
|
};
|
|
|
|
#define PROMDATA_PTR_VPD 0x08
|
2005-02-27 03:26:58 +03:00
|
|
|
#define PROMDATA_DATA2 0x0a
|
2002-12-27 01:59:51 +03:00
|
|
|
#endif /* HME_USE_LOCAL_MAC_ADDRESS */
|
2001-10-05 21:49:43 +04:00
|
|
|
|
|
|
|
printf(": Sun Happy Meal Ethernet, rev. %d\n",
|
|
|
|
PCI_REVISION(pa->pa_class));
|
|
|
|
|
2000-04-05 06:31:19 +04:00
|
|
|
/*
|
|
|
|
* enable io/memory-space accesses. this is kinda of gross; but
|
|
|
|
# the hme comes up with neither IO space enabled, or memory space.
|
|
|
|
*/
|
|
|
|
if (pa->pa_memt)
|
|
|
|
pa->pa_flags |= PCI_FLAGS_MEM_ENABLED;
|
|
|
|
if (pa->pa_iot)
|
|
|
|
pa->pa_flags |= PCI_FLAGS_IO_ENABLED;
|
|
|
|
csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
|
|
|
|
if (pa->pa_memt) {
|
|
|
|
type = PCI_MAPREG_TYPE_MEM;
|
|
|
|
csr |= PCI_COMMAND_MEM_ENABLE;
|
|
|
|
sc->sc_bustag = pa->pa_memt;
|
|
|
|
} else {
|
|
|
|
type = PCI_MAPREG_TYPE_IO;
|
|
|
|
csr |= PCI_COMMAND_IO_ENABLE;
|
|
|
|
sc->sc_bustag = pa->pa_iot;
|
|
|
|
}
|
|
|
|
pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
|
|
|
|
csr | PCI_COMMAND_MEM_ENABLE);
|
|
|
|
|
|
|
|
sc->sc_dmatag = pa->pa_dmat;
|
|
|
|
|
2000-06-25 05:05:16 +04:00
|
|
|
sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
|
2000-04-05 06:31:19 +04:00
|
|
|
/*
|
|
|
|
* Map five register banks:
|
|
|
|
*
|
|
|
|
* bank 0: HME SEB registers: +0x0000
|
|
|
|
* bank 1: HME ETX registers: +0x2000
|
|
|
|
* bank 2: HME ERX registers: +0x4000
|
|
|
|
* bank 3: HME MAC registers: +0x6000
|
|
|
|
* bank 4: HME MIF registers: +0x7000
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PCI_HME_BASEADDR 0x10
|
|
|
|
if (pci_mapreg_map(pa, PCI_HME_BASEADDR, type, 0,
|
|
|
|
&hsc->hsc_memt, &hsc->hsc_memh, NULL, NULL) != 0)
|
|
|
|
{
|
2001-10-05 21:49:43 +04:00
|
|
|
printf("%s: unable to map device registers\n",
|
|
|
|
sc->sc_dev.dv_xname);
|
2000-04-05 06:31:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sc->sc_seb = hsc->hsc_memh;
|
2001-10-05 21:49:43 +04:00
|
|
|
if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x2000,
|
|
|
|
0x1000, &sc->sc_etx)) {
|
|
|
|
printf("%s: unable to subregion ETX registers\n",
|
|
|
|
sc->sc_dev.dv_xname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x4000,
|
|
|
|
0x1000, &sc->sc_erx)) {
|
|
|
|
printf("%s: unable to subregion ERX registers\n",
|
|
|
|
sc->sc_dev.dv_xname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x6000,
|
|
|
|
0x1000, &sc->sc_mac)) {
|
|
|
|
printf("%s: unable to subregion MAC registers\n",
|
|
|
|
sc->sc_dev.dv_xname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x7000,
|
|
|
|
0x1000, &sc->sc_mif)) {
|
|
|
|
printf("%s: unable to subregion MIF registers\n",
|
|
|
|
sc->sc_dev.dv_xname);
|
|
|
|
return;
|
|
|
|
}
|
2000-04-05 06:31:19 +04:00
|
|
|
|
2002-12-27 01:59:51 +03:00
|
|
|
#if HME_USE_LOCAL_MAC_ADDRESS
|
|
|
|
/*
|
|
|
|
* Dig out VPD (vital product data) and acquire Ethernet address.
|
|
|
|
* The VPD of hme resides in the Boot PROM (PCI FCode) attached
|
|
|
|
* to the EBus interface.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
|
|
|
|
* chapter 2 describes the data structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enaddr = NULL;
|
|
|
|
|
|
|
|
/* get a PCI tag for the EBus bridge (function 0 of the same device) */
|
|
|
|
ebus_pa = *pa;
|
|
|
|
ebus_pa.pa_tag = pci_make_tag(pa->pa_pc, pa->pa_bus, pa->pa_device, 0);
|
|
|
|
|
|
|
|
ebus_cl = pci_conf_read(ebus_pa.pa_pc, ebus_pa.pa_tag, PCI_CLASS_REG);
|
|
|
|
ebus_id = pci_conf_read(ebus_pa.pa_pc, ebus_pa.pa_tag, PCI_ID_REG);
|
|
|
|
|
|
|
|
#define PCI_EBUS2_BOOTROM 0x10
|
|
|
|
if (PCI_CLASS(ebus_cl) == PCI_CLASS_BRIDGE &&
|
|
|
|
PCI_PRODUCT(ebus_id) == PCI_PRODUCT_SUN_EBUS &&
|
|
|
|
pci_mapreg_map(&ebus_pa, PCI_EBUS2_BOOTROM, PCI_MAPREG_TYPE_MEM,
|
|
|
|
BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE,
|
|
|
|
&romt, &romh, 0, &romsize) == 0) {
|
|
|
|
|
|
|
|
/* read PCI Expansion PROM Header */
|
|
|
|
bus_space_read_region_1(romt, romh, 0, buf, sizeof buf);
|
|
|
|
if (memcmp(buf, promhdr, sizeof promhdr) == 0 &&
|
|
|
|
(dataoff = (buf[PROMHDR_PTR_DATA] |
|
|
|
|
(buf[PROMHDR_PTR_DATA + 1] << 8))) >= 0x1c) {
|
|
|
|
|
|
|
|
/* read PCI Expansion PROM Data */
|
|
|
|
bus_space_read_region_1(romt, romh, dataoff,
|
|
|
|
buf, sizeof buf);
|
|
|
|
if (memcmp(buf, promdat, sizeof promdat) == 0 &&
|
2005-04-21 22:31:51 +04:00
|
|
|
hmepromvalid(buf + PROMDATA_DATA2) &&
|
2002-12-27 01:59:51 +03:00
|
|
|
(vpdoff = (buf[PROMDATA_PTR_VPD] |
|
|
|
|
(buf[PROMDATA_PTR_VPD + 1] << 8))) >= 0x1c) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The VPD of hme is not in PCI 2.2 standard
|
|
|
|
* format. The length in the resource header
|
|
|
|
* is in big endian, and resources are not
|
|
|
|
* properly terminated (only one resource
|
|
|
|
* and no end tag).
|
|
|
|
*/
|
2005-04-21 22:31:51 +04:00
|
|
|
vpdoff = hmevpdoff(romt, romh, vpdoff,
|
|
|
|
pa->pa_device);
|
2002-12-27 01:59:51 +03:00
|
|
|
/* read PCI VPD */
|
|
|
|
bus_space_read_region_1(romt, romh,
|
|
|
|
vpdoff, buf, sizeof buf);
|
|
|
|
vpd = (void *)(buf + 3);
|
|
|
|
if (PCI_VPDRES_ISLARGE(buf[0]) &&
|
|
|
|
PCI_VPDRES_LARGE_NAME(buf[0])
|
|
|
|
== PCI_VPDRES_TYPE_VPD &&
|
|
|
|
/* buf[1] == 0 && buf[2] == 9 && */ /*len*/
|
|
|
|
vpd->vpd_key0 == 0x4e /* N */ &&
|
|
|
|
vpd->vpd_key1 == 0x41 /* A */ &&
|
|
|
|
vpd->vpd_len == ETHER_ADDR_LEN) {
|
|
|
|
/*
|
|
|
|
* Ethernet address found
|
|
|
|
*/
|
|
|
|
enaddr = buf + 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bus_space_unmap(romt, romh, romsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enaddr)
|
|
|
|
memcpy(sc->sc_enaddr, enaddr, ETHER_ADDR_LEN);
|
|
|
|
else
|
|
|
|
#endif /* HME_USE_LOCAL_MAC_ADDRESS */
|
|
|
|
#ifdef __sparc__
|
2004-03-17 11:58:23 +03:00
|
|
|
prom_getether(PCITAG_NODE(pa->pa_tag), sc->sc_enaddr);
|
2002-12-27 01:59:51 +03:00
|
|
|
#else
|
|
|
|
printf("%s: no Ethernet address found\n", sc->sc_dev.dv_xname);
|
|
|
|
#endif
|
2000-04-05 06:31:19 +04:00
|
|
|
|
2001-10-05 21:49:43 +04:00
|
|
|
/*
|
|
|
|
* Map and establish our interrupt.
|
|
|
|
*/
|
|
|
|
if (pci_intr_map(pa, &ih) != 0) {
|
|
|
|
printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
|
|
|
|
return;
|
2005-02-27 03:26:58 +03:00
|
|
|
}
|
2001-10-05 21:49:43 +04:00
|
|
|
intrstr = pci_intr_string(pa->pa_pc, ih);
|
|
|
|
hsc->hsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, hme_intr, sc);
|
|
|
|
if (hsc->hsc_ih == NULL) {
|
|
|
|
printf("%s: unable to establish interrupt",
|
|
|
|
sc->sc_dev.dv_xname);
|
|
|
|
if (intrstr != NULL)
|
|
|
|
printf(" at %s", intrstr);
|
|
|
|
printf("\n");
|
2001-10-18 10:29:55 +04:00
|
|
|
return;
|
2001-10-05 21:49:43 +04:00
|
|
|
}
|
|
|
|
printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
|
|
|
|
|
2000-04-05 06:31:19 +04:00
|
|
|
sc->sc_burst = 16; /* XXX */
|
|
|
|
|
2001-10-05 21:49:43 +04:00
|
|
|
/* Finish off the attach. */
|
2000-04-05 06:31:19 +04:00
|
|
|
hme_config(sc);
|
|
|
|
}
|