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