xref: /openbsd-src/sys/dev/pci/if_hme_pci.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: if_hme_pci.c,v 1.22 2019/12/05 12:46:54 mpi 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/syslog.h>
37 #include <sys/device.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 
41 #include <net/if.h>
42 #include <net/if_media.h>
43 
44 #include <netinet/in.h>
45 #include <netinet/if_ether.h>
46 
47 #include <dev/mii/miivar.h>
48 
49 #ifdef __sparc64__
50 #include <machine/autoconf.h>
51 #include <dev/ofw/openfirm.h>
52 #endif
53 #include <machine/cpu.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 
61 struct hme_pci_softc {
62 	struct	hme_softc	hsc_hme;	/* HME device */
63 	bus_space_tag_t		hsc_memt;
64 	bus_space_handle_t	hsc_memh;
65 	bus_size_t		hsc_memsize;
66 	void			*hsc_ih;
67 	pci_chipset_tag_t	hsc_pc;
68 };
69 
70 int	hmematch_pci(struct device *, void *, void *);
71 void	hmeattach_pci(struct device *, struct device *, void *);
72 int	hmedetach_pci(struct device *, int);
73 int	hme_pci_enaddr(struct hme_softc *, struct pci_attach_args *);
74 
75 struct cfattach hme_pci_ca = {
76 	sizeof(struct hme_pci_softc), hmematch_pci, hmeattach_pci, hmedetach_pci
77 };
78 
79 int
80 hmematch_pci(parent, vcf, aux)
81 	struct device *parent;
82 	void *vcf;
83 	void *aux;
84 {
85 	struct pci_attach_args *pa = aux;
86 
87 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
88 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_HME)
89 		return (1);
90 
91 	return (0);
92 }
93 
94 #define	PCI_EBUS2_BOOTROM	0x10
95 #define	PCI_EBUS2_BOOTROM_SIZE	0x20000
96 #define	PROMHDR_PTR_DATA	0x18
97 #define	PROMDATA_PTR_VPD	0x08
98 #define	PROMDATA_LENGTH		0x0a
99 #define	PROMDATA_REVISION	0x0c
100 #define	PROMDATA_SUBCLASS	0x0e
101 #define	PROMDATA_CLASS		0x0f
102 
103 static const u_int8_t hme_promhdr[] = { 0x55, 0xaa };
104 static const u_int8_t hme_promdat[] = {
105 	'P', 'C', 'I', 'R',
106 	PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
107 	PCI_PRODUCT_SUN_HME & 0xff, PCI_PRODUCT_SUN_HME >> 8
108 };
109 
110 int
111 hme_pci_enaddr(struct hme_softc *sc, struct pci_attach_args *hpa)
112 {
113 	struct pci_attach_args epa;
114 	struct pci_vpd *vpd;
115 	pcireg_t cl, id;
116 	bus_space_handle_t romh;
117 	bus_space_tag_t romt;
118 	bus_size_t romsize = 0;
119 	u_int8_t buf[32];
120 	int dataoff, vpdoff, length;
121 
122 	/*
123 	 * Dig out VPD (vital product data) and acquire Ethernet address.
124 	 * The VPD of hme resides in the Boot PROM (PCI FCode) attached
125 	 * to the EBus interface.
126 	 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
127 	 * chapter 2 describes the data structure.
128 	 */
129 
130 	/* get a PCI tag for the EBus bridge (function 0 of the same device) */
131 	epa = *hpa;
132 	epa.pa_tag = pci_make_tag(hpa->pa_pc, hpa->pa_bus, hpa->pa_device, 0);
133 	cl = pci_conf_read(epa.pa_pc, epa.pa_tag, PCI_CLASS_REG);
134 	id = pci_conf_read(epa.pa_pc, epa.pa_tag, PCI_ID_REG);
135 
136 	if (PCI_CLASS(cl) != PCI_CLASS_BRIDGE ||
137 	    PCI_PRODUCT(id) != PCI_PRODUCT_SUN_EBUS)
138 		goto fail;
139 
140 	if (pci_mapreg_map(&epa, PCI_EBUS2_BOOTROM, PCI_MAPREG_TYPE_MEM, 0,
141 	    &romt, &romh, 0, &romsize, PCI_EBUS2_BOOTROM_SIZE))
142 		goto fail;
143 
144 	bus_space_read_region_1(romt, romh, 0, buf, sizeof(buf));
145 	if (bcmp(buf, hme_promhdr, sizeof(hme_promhdr)))
146 		goto fail;
147 
148 	dataoff = buf[PROMHDR_PTR_DATA] | (buf[PROMHDR_PTR_DATA + 1] << 8);
149 	if (dataoff < 0x1c)
150 		goto fail;
151 
152 	bus_space_read_region_1(romt, romh, dataoff, buf, sizeof(buf));
153 	if (bcmp(buf, hme_promdat, sizeof(hme_promdat)))
154 		goto fail;
155 
156 	/*
157 	 * Don't check the interface part of the class code, since
158 	 * some cards have a bogus value there.
159 	 */
160 	length = buf[PROMDATA_LENGTH] | (buf[PROMDATA_LENGTH + 1] << 8);
161 	if (length != 0x18 || buf[PROMDATA_REVISION] != 0x00 ||
162 	    buf[PROMDATA_SUBCLASS] != PCI_SUBCLASS_NETWORK_ETHERNET ||
163 	    buf[PROMDATA_CLASS] != PCI_CLASS_NETWORK)
164 		goto fail;
165 
166 	vpdoff = buf[PROMDATA_PTR_VPD] | (buf[PROMDATA_PTR_VPD + 1] << 8);
167 	if (vpdoff < 0x1c)
168 		goto fail;
169 
170 	/*
171 	 * The VPD of hme is not in PCI 2.2 standard format.  The length
172 	 * in the resource header is in big endian, and resources are not
173 	 * properly terminated (only one resource and no end tag).
174 	 */
175 	bus_space_read_region_1(romt, romh, vpdoff, buf, sizeof(buf));
176 
177 	/* XXX TODO: Get the data from VPD */
178 	vpd = (struct pci_vpd *)(buf + 3);
179 	if (!PCI_VPDRES_ISLARGE(buf[0]) ||
180 	    PCI_VPDRES_LARGE_NAME(buf[0]) != PCI_VPDRES_TYPE_VPD)
181 		goto fail;
182 	if (vpd->vpd_key0 != 'N' || vpd->vpd_key1 != 'A')
183 		goto fail;
184 
185 	bcopy(buf + 6, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
186 	sc->sc_arpcom.ac_enaddr[5] += hpa->pa_device;
187 	bus_space_unmap(romt, romh, romsize);
188 	return (0);
189 
190 fail:
191 	if (romsize != 0)
192 		bus_space_unmap(romt, romh, romsize);
193 	return (-1);
194 }
195 
196 void
197 hmeattach_pci(parent, self, aux)
198 	struct device *parent, *self;
199 	void *aux;
200 {
201 	struct pci_attach_args *pa = aux;
202 	struct hme_pci_softc *hsc = (void *)self;
203 	struct hme_softc *sc = &hsc->hsc_hme;
204 	pci_intr_handle_t ih;
205 	/* XXX the following declarations should be elsewhere */
206 	extern void myetheraddr(u_char *);
207 	pcireg_t csr;
208 	const char *intrstr = NULL;
209 	int type, gotenaddr = 0;
210 
211 	hsc->hsc_pc = pa->pa_pc;
212 
213 	/*
214 	 * enable io/memory-space accesses.  this is kinda of gross; but
215 	 * the hme comes up with neither IO space enabled, or memory space.
216 	 */
217 	if (pa->pa_memt)
218 		pa->pa_flags |= PCI_FLAGS_MEM_ENABLED;
219 	if (pa->pa_iot)
220 		pa->pa_flags |= PCI_FLAGS_IO_ENABLED;
221 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
222 	if (pa->pa_memt) {
223 		type = PCI_MAPREG_TYPE_MEM;
224 		csr |= PCI_COMMAND_MEM_ENABLE;
225 		sc->sc_bustag = pa->pa_memt;
226 	} else {
227 		type = PCI_MAPREG_TYPE_IO;
228 		csr |= PCI_COMMAND_IO_ENABLE;
229 		sc->sc_bustag = pa->pa_iot;
230 	}
231 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
232 	    csr | PCI_COMMAND_MEM_ENABLE);
233 
234 	sc->sc_dmatag = pa->pa_dmat;
235 
236 	sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
237 	/*
238 	 * Map five register banks:
239 	 *
240 	 *	bank 0: HME SEB registers:	+0x0000
241 	 *	bank 1: HME ETX registers:	+0x2000
242 	 *	bank 2: HME ERX registers:	+0x4000
243 	 *	bank 3: HME MAC registers:	+0x6000
244 	 *	bank 4: HME MIF registers:	+0x7000
245 	 *
246 	 */
247 
248 #define PCI_HME_BASEADDR	0x10
249 	if (pci_mapreg_map(pa, PCI_HME_BASEADDR, type, 0,
250 	    &hsc->hsc_memt, &hsc->hsc_memh, NULL, &hsc->hsc_memsize, 0) != 0) {
251 		printf(": can't map registers\n");
252 		return;
253 	}
254 	sc->sc_seb = hsc->hsc_memh;
255 	bus_space_subregion(sc->sc_bustag, hsc->hsc_memh, 0x2000, 0x2000,
256 	    &sc->sc_etx);
257 	bus_space_subregion(sc->sc_bustag, hsc->hsc_memh, 0x4000, 0x2000,
258 	    &sc->sc_erx);
259 	bus_space_subregion(sc->sc_bustag, hsc->hsc_memh, 0x6000, 0x1000,
260 	    &sc->sc_mac);
261 	bus_space_subregion(sc->sc_bustag, hsc->hsc_memh, 0x7000, 0x1000,
262 	    &sc->sc_mif);
263 
264 	if (hme_pci_enaddr(sc, pa) == 0)
265 		gotenaddr = 1;
266 
267 #ifdef __sparc64__
268 	if (!gotenaddr) {
269 		if (OF_getprop(PCITAG_NODE(pa->pa_tag), "local-mac-address",
270 		    sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0)
271 			myetheraddr(sc->sc_arpcom.ac_enaddr);
272 		gotenaddr = 1;
273 	}
274 #endif
275 #ifdef __powerpc__
276 	if (!gotenaddr) {
277 		pci_ether_hw_addr(pa->pa_pc, sc->sc_arpcom.ac_enaddr);
278 		gotenaddr = 1;
279 	}
280 #endif
281 
282 	sc->sc_burst = 16;	/* XXX */
283 
284 	if (pci_intr_map(pa, &ih) != 0) {
285 		printf(": couldn't map interrupt\n");
286 		bus_space_unmap(hsc->hsc_memt, hsc->hsc_memh, hsc->hsc_memsize);
287 		return;
288 	}
289 	intrstr = pci_intr_string(pa->pa_pc, ih);
290 	hsc->hsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET,
291 	    hme_intr, sc, self->dv_xname);
292 	if (hsc->hsc_ih == NULL) {
293 		printf(": couldn't establish interrupt");
294 		if (intrstr != NULL)
295 			printf(" at %s", intrstr);
296 		printf("\n");
297 		bus_space_unmap(hsc->hsc_memt, hsc->hsc_memh, hsc->hsc_memsize);
298 		return;
299 	}
300 
301 	printf(": %s", intrstr);
302 
303 	/*
304 	 * call the main configure
305 	 */
306 	hme_config(sc);
307 }
308 
309 int
310 hmedetach_pci(struct device *self, int flags)
311 {
312 	struct hme_pci_softc *hsc = (void *)self;
313 	struct hme_softc *sc = &hsc->hsc_hme;
314 
315 	timeout_del(&sc->sc_tick_ch);
316 	pci_intr_disestablish(hsc->hsc_pc, hsc->hsc_ih);
317 
318 	hme_unconfig(sc);
319 	bus_space_unmap(hsc->hsc_memt, hsc->hsc_memh, hsc->hsc_memsize);
320 	return (0);
321 }
322