xref: /netbsd-src/sys/dev/pci/if_hme_pci.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: if_hme_pci.c,v 1.25 2008/05/29 14:51:27 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * PCI front-end device driver for the HME ethernet device.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_hme_pci.c,v 1.25 2008/05/29 14:51:27 mrg Exp $");
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_ether.h>
46 #include <net/if_media.h>
47 
48 #include <dev/mii/mii.h>
49 #include <dev/mii/miivar.h>
50 
51 #include <sys/intr.h>
52 
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcidevs.h>
56 
57 #include <dev/ic/hmevar.h>
58 #ifdef __sparc__
59 #include <machine/promlib.h>
60 #endif
61 
62 #ifndef HME_USE_LOCAL_MAC_ADDRESS
63 #ifdef __sparc__
64 #define HME_USE_LOCAL_MAC_ADDRESS	0	/* use system-wide address */
65 #else
66 #define HME_USE_LOCAL_MAC_ADDRESS	1
67 #endif
68 #endif
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 *, struct cfdata *, void *);
78 void	hmeattach_pci(struct device *, struct device *, void *);
79 
80 CFATTACH_DECL(hme_pci, sizeof(struct hme_pci_softc),
81     hmematch_pci, hmeattach_pci, NULL, NULL);
82 
83 int
84 hmematch_pci(struct device *parent, struct cfdata *cf,
85     void *aux)
86 {
87 	struct pci_attach_args *pa = aux;
88 
89 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
90 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_HMENETWORK)
91 		return (1);
92 
93 	return (0);
94 }
95 
96 #if HME_USE_LOCAL_MAC_ADDRESS
97 static inline int
98 hmepromvalid(u_int8_t* buf)
99 {
100 	return buf[0] == 0x18 && buf[1] == 0x00 &&	/* structure length */
101 	    buf[2] == 0x00 &&				/* revision */
102 	    (buf[3] == 0x00 ||				/* hme */
103 	     buf[3] == 0x80) &&				/* qfe */
104 	    buf[4] == PCI_SUBCLASS_NETWORK_ETHERNET &&	/* subclass code */
105 	    buf[5] == PCI_CLASS_NETWORK;		/* class code */
106 }
107 
108 static inline int
109 hmevpdoff(bus_space_tag_t romt, bus_space_handle_t romh, int vpdoff, int dev)
110 {
111 #define VPDLEN (3 + sizeof(struct pci_vpd) + ETHER_ADDR_LEN)
112 	if (bus_space_read_1(romt, romh, vpdoff + VPDLEN) != 0x79 &&
113 	    bus_space_read_1(romt, romh, vpdoff + 4 * VPDLEN) == 0x79) {
114 		/*
115 		 * Use the Nth NA for the Nth HME on
116 		 * this SUNW,qfe.
117 		 */
118 		vpdoff += dev * VPDLEN;
119 	}
120 	return vpdoff;
121 }
122 #endif
123 
124 void
125 hmeattach_pci(struct device *parent, struct device *self, void *aux)
126 {
127 	struct pci_attach_args *pa = aux;
128 	struct hme_pci_softc *hsc = (void *)self;
129 	struct hme_softc *sc = &hsc->hsc_hme;
130 	pci_intr_handle_t ih;
131 	pcireg_t csr;
132 	const char *intrstr;
133 	int type;
134 #if HME_USE_LOCAL_MAC_ADDRESS
135 	struct pci_attach_args	ebus_pa;
136 	pcireg_t		ebus_cl, ebus_id;
137 	u_int8_t		*enaddr;
138 	bus_space_tag_t		romt;
139 	bus_space_handle_t	romh;
140 	bus_size_t		romsize;
141 	u_int8_t		buf[64];
142 	int			dataoff, vpdoff;
143 	struct pci_vpd		*vpd;
144 	static const u_int8_t promhdr[] = { 0x55, 0xaa };
145 #define PROMHDR_PTR_DATA	0x18
146 	static const u_int8_t promdat[] = {
147 		0x50, 0x43, 0x49, 0x52,		/* "PCIR" */
148 		PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
149 		PCI_PRODUCT_SUN_HMENETWORK & 0xff,
150 		PCI_PRODUCT_SUN_HMENETWORK >> 8
151 	};
152 #define PROMDATA_PTR_VPD	0x08
153 #define PROMDATA_DATA2		0x0a
154 #endif	/* HME_USE_LOCAL_MAC_ADDRESS */
155 
156 	printf(": Sun Happy Meal Ethernet, rev. %d\n",
157 	    PCI_REVISION(pa->pa_class));
158 
159 	/*
160 	 * enable io/memory-space accesses.  this is kinda of gross; but
161 	 # the hme comes up with neither IO space enabled, or memory space.
162 	 */
163 	if (pa->pa_memt)
164 		pa->pa_flags |= PCI_FLAGS_MEM_ENABLED;
165 	if (pa->pa_iot)
166 		pa->pa_flags |= PCI_FLAGS_IO_ENABLED;
167 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
168 	if (pa->pa_memt) {
169 		type = PCI_MAPREG_TYPE_MEM;
170 		csr |= PCI_COMMAND_MEM_ENABLE;
171 		sc->sc_bustag = pa->pa_memt;
172 	} else {
173 		type = PCI_MAPREG_TYPE_IO;
174 		csr |= PCI_COMMAND_IO_ENABLE;
175 		sc->sc_bustag = pa->pa_iot;
176 	}
177 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
178 	    csr | PCI_COMMAND_MEM_ENABLE);
179 
180 	sc->sc_dmatag = pa->pa_dmat;
181 
182 	sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
183 	/*
184 	 * Map five register banks:
185 	 *
186 	 *	bank 0: HME SEB registers:	+0x0000
187 	 *	bank 1: HME ETX registers:	+0x2000
188 	 *	bank 2: HME ERX registers:	+0x4000
189 	 *	bank 3: HME MAC registers:	+0x6000
190 	 *	bank 4: HME MIF registers:	+0x7000
191 	 *
192 	 */
193 
194 #define PCI_HME_BASEADDR	0x10
195 	if (pci_mapreg_map(pa, PCI_HME_BASEADDR, type, 0,
196 	    &hsc->hsc_memt, &hsc->hsc_memh, NULL, NULL) != 0)
197 	{
198 		aprint_error_dev(&sc->sc_dev, "unable to map device registers\n");
199 		return;
200 	}
201 	sc->sc_seb = hsc->hsc_memh;
202 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x2000,
203 	    0x1000, &sc->sc_etx)) {
204 		aprint_error_dev(&sc->sc_dev, "unable to subregion ETX registers\n");
205 		return;
206 	}
207 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x4000,
208 	    0x1000, &sc->sc_erx)) {
209 		aprint_error_dev(&sc->sc_dev, "unable to subregion ERX registers\n");
210 		return;
211 	}
212 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x6000,
213 	    0x1000, &sc->sc_mac)) {
214 		aprint_error_dev(&sc->sc_dev, "unable to subregion MAC registers\n");
215 		return;
216 	}
217 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x7000,
218 	    0x1000, &sc->sc_mif)) {
219 		aprint_error_dev(&sc->sc_dev, "unable to subregion MIF registers\n");
220 		return;
221 	}
222 
223 #if HME_USE_LOCAL_MAC_ADDRESS
224 	/*
225 	 * Dig out VPD (vital product data) and acquire Ethernet address.
226 	 * The VPD of hme resides in the Boot PROM (PCI FCode) attached
227 	 * to the EBus interface.
228 	 */
229 	/*
230 	 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
231 	 * chapter 2 describes the data structure.
232 	 */
233 
234 	enaddr = NULL;
235 
236 	/* get a PCI tag for the EBus bridge (function 0 of the same device) */
237 	ebus_pa = *pa;
238 	ebus_pa.pa_tag = pci_make_tag(pa->pa_pc, pa->pa_bus, pa->pa_device, 0);
239 
240 	ebus_cl = pci_conf_read(ebus_pa.pa_pc, ebus_pa.pa_tag, PCI_CLASS_REG);
241 	ebus_id = pci_conf_read(ebus_pa.pa_pc, ebus_pa.pa_tag, PCI_ID_REG);
242 
243 #define PCI_EBUS2_BOOTROM	0x10
244 	if (PCI_CLASS(ebus_cl) == PCI_CLASS_BRIDGE &&
245 	    PCI_PRODUCT(ebus_id) == PCI_PRODUCT_SUN_EBUS &&
246 	    pci_mapreg_map(&ebus_pa, PCI_EBUS2_BOOTROM, PCI_MAPREG_TYPE_MEM,
247 		BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE,
248 		&romt, &romh, 0, &romsize) == 0) {
249 
250 		/* read PCI Expansion PROM Header */
251 		bus_space_read_region_1(romt, romh, 0, buf, sizeof buf);
252 		if (memcmp(buf, promhdr, sizeof promhdr) == 0 &&
253 		    (dataoff = (buf[PROMHDR_PTR_DATA] |
254 			(buf[PROMHDR_PTR_DATA + 1] << 8))) >= 0x1c) {
255 
256 			/* read PCI Expansion PROM Data */
257 			bus_space_read_region_1(romt, romh, dataoff,
258 			    buf, sizeof buf);
259 			if (memcmp(buf, promdat, sizeof promdat) == 0 &&
260 			    hmepromvalid(buf + PROMDATA_DATA2) &&
261 			    (vpdoff = (buf[PROMDATA_PTR_VPD] |
262 				(buf[PROMDATA_PTR_VPD + 1] << 8))) >= 0x1c) {
263 
264 				/*
265 				 * The VPD of hme is not in PCI 2.2 standard
266 				 * format.  The length in the resource header
267 				 * is in big endian, and resources are not
268 				 * properly terminated (only one resource
269 				 * and no end tag).
270 				 */
271 				vpdoff = hmevpdoff(romt, romh, vpdoff,
272 				    pa->pa_device);
273 				/* read PCI VPD */
274 				bus_space_read_region_1(romt, romh,
275 				    vpdoff, buf, sizeof buf);
276 				vpd = (void *)(buf + 3);
277 				if (PCI_VPDRES_ISLARGE(buf[0]) &&
278 				    PCI_VPDRES_LARGE_NAME(buf[0])
279 					== PCI_VPDRES_TYPE_VPD &&
280 				    /* buf[1] == 0 && buf[2] == 9 && */ /*len*/
281 				    vpd->vpd_key0 == 0x4e /* N */ &&
282 				    vpd->vpd_key1 == 0x41 /* A */ &&
283 				    vpd->vpd_len == ETHER_ADDR_LEN) {
284 					/*
285 					 * Ethernet address found
286 					 */
287 					enaddr = buf + 6;
288 				}
289 			}
290 		}
291 		bus_space_unmap(romt, romh, romsize);
292 	}
293 
294 	if (enaddr)
295 		memcpy(sc->sc_enaddr, enaddr, ETHER_ADDR_LEN);
296 	else
297 #endif	/* HME_USE_LOCAL_MAC_ADDRESS */
298 #ifdef __sparc__
299 		prom_getether(PCITAG_NODE(pa->pa_tag), sc->sc_enaddr);
300 #else
301 		printf("%s: no Ethernet address found\n", device_xname(&sc->sc_dev));
302 #endif
303 
304 	/*
305 	 * Map and establish our interrupt.
306 	 */
307 	if (pci_intr_map(pa, &ih) != 0) {
308 		aprint_error_dev(&sc->sc_dev, "unable to map interrupt\n");
309 		return;
310 	}
311 	intrstr = pci_intr_string(pa->pa_pc, ih);
312 	hsc->hsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, hme_intr, sc);
313 	if (hsc->hsc_ih == NULL) {
314 		aprint_error_dev(&sc->sc_dev, "unable to establish interrupt");
315 		if (intrstr != NULL)
316 			printf(" at %s", intrstr);
317 		printf("\n");
318 		return;
319 	}
320 	printf("%s: interrupting at %s\n", device_xname(&sc->sc_dev), intrstr);
321 
322 	sc->sc_burst = 16;	/* XXX */
323 
324 	/* Finish off the attach. */
325 	hme_config(sc);
326 }
327