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