summaryrefslogtreecommitdiff
path: root/sys/dev/pci/if_dwqe_pci.c
blob: e87e7a871607c6e9ddd171b35c6d28237a3389bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* $OpenBSD: if_dwqe_pci.c,v 1.3 2023/11/11 16:50:25 stsp Exp $ */

/*
 * Copyright (c) 2023 Stefan Sperling <stsp@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Driver for the Intel Elkhart Lake ethernet controller.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>

#include <net/if.h>
#include <net/if_media.h>

#include <netinet/in.h>
#include <netinet/if_ether.h>

#include <dev/mii/miivar.h>

#include <dev/ic/dwqereg.h>
#include <dev/ic/dwqevar.h>

static const struct pci_matchid dwqe_pci_devices[] = {
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EHL_PSE0_RGMII_1G	},
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EHL_PSE0_SGMII_1G },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EHL_PSE0_SGMII_2G },
#if 0
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EHL_PSE1_RGMII_1G },
#endif
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EHL_PSE1_SGMII_1G },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_EHL_PSE1_SGMII_2G },
};

struct dwqe_pci_softc {
	struct dwqe_softc	sc_sc;
	pci_chipset_tag_t	sc_pct;
	pcitag_t		sc_pcitag;
	bus_size_t		sc_mapsize;
};

int
dwqe_pci_match(struct device *parent, void *cfdata, void *aux)
{
	struct pci_attach_args *pa = aux;
	return pci_matchbyid(pa, dwqe_pci_devices, nitems(dwqe_pci_devices));
}

void
dwqe_pci_attach(struct device *parent, struct device *self, void *aux)
{
	struct pci_attach_args *pa = aux;
	struct dwqe_pci_softc *psc = (void *)self;
	struct dwqe_softc *sc = &psc->sc_sc;
	pci_intr_handle_t ih;
	pcireg_t memtype;
	int err;
	const char *intrstr;

	psc->sc_pct = pa->pa_pc;
	psc->sc_pcitag = pa->pa_tag;

	sc->sc_dmat = pa->pa_dmat;

	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
	err = pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0,
	    &sc->sc_iot, &sc->sc_ioh, NULL, &psc->sc_mapsize, 0);
	if (err) {
		printf("%s: can't map mem space\n", DEVNAME(sc));
		return;
	}

	if (pci_intr_map_msi(pa, &ih) && pci_intr_map(pa, &ih)) {
		printf("%s: can't map interrupt\n", DEVNAME(sc));
		return;
	}

	intrstr = pci_intr_string(psc->sc_pct, ih);
	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET | IPL_MPSAFE,
	    dwqe_intr, psc, sc->sc_dev.dv_xname);
	if (sc->sc_ih == NULL) {
		printf(": can't establish interrupt");
		if (intrstr != NULL)
			printf(" at %s", intrstr);
		printf("\n");
		return;
	}

	switch (PCI_PRODUCT(pa->pa_id)) {
	case PCI_PRODUCT_INTEL_EHL_PSE0_RGMII_1G:
		sc->sc_phy_mode = DWQE_PHY_MODE_RGMII_ID;
		sc->sc_clk = GMAC_MAC_MDIO_ADDR_CR_250_300;
		sc->sc_clkrate = 200000000;
		break;
	case PCI_PRODUCT_INTEL_EHL_PSE0_SGMII_1G:
	case PCI_PRODUCT_INTEL_EHL_PSE0_SGMII_2G:
	case PCI_PRODUCT_INTEL_EHL_PSE1_SGMII_1G:
	case PCI_PRODUCT_INTEL_EHL_PSE1_SGMII_2G:
		sc->sc_phy_mode = DWQE_PHY_MODE_SGMII;
		sc->sc_clk = GMAC_MAC_MDIO_ADDR_CR_250_300;
		sc->sc_clkrate = 200000000;
		break;
	default:
		sc->sc_phy_mode = DWQE_PHY_MODE_UNKNOWN;
		break;
	}

	sc->sc_phyloc = MII_PHY_ANY;
	sc->sc_8xpbl = 1;
	sc->sc_txpbl = 32;
	sc->sc_rxpbl = 32;
	sc->sc_txfifo_size = 32768;
	sc->sc_rxfifo_size = 32768;

	sc->sc_axi_config = 1;
	sc->sc_wr_osr_lmt = 1;
	sc->sc_rd_osr_lmt = 1;
	sc->sc_blen[0] = 4;
	sc->sc_blen[1] = 8;
	sc->sc_blen[2] = 16;

	dwqe_lladdr_read(sc, sc->sc_lladdr);

	dwqe_reset(sc);
	dwqe_attach(sc);
}

const struct cfattach dwqe_pci_ca = {
	sizeof(struct dwqe_softc), dwqe_pci_match, dwqe_pci_attach
};