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