xref: /freebsd/sys/dev/le/if_le_pci.c (revision 8f76bb7dad48538c6832c2fb466a433d2a3f8cd5)
1 /*	$NetBSD: if_le_pci.c,v 1.43 2005/12/11 12:22:49 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause AND BSD-3-Clause
5  *
6  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
11  * Simulation Facility, NASA Ames Research Center.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*-
36  * Copyright (c) 1992, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Ralph Campbell and Rick Macklem.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
67  */
68 
69 #include <sys/cdefs.h>
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/bus.h>
73 #include <sys/endian.h>
74 #include <sys/kernel.h>
75 #include <sys/lock.h>
76 #include <sys/module.h>
77 #include <sys/mutex.h>
78 #include <sys/resource.h>
79 #include <sys/rman.h>
80 #include <sys/socket.h>
81 
82 #include <net/ethernet.h>
83 #include <net/if.h>
84 #include <net/if_media.h>
85 
86 #include <machine/bus.h>
87 #include <machine/resource.h>
88 
89 #include <dev/pci/pcireg.h>
90 #include <dev/pci/pcivar.h>
91 
92 #include <dev/le/lancereg.h>
93 #include <dev/le/lancevar.h>
94 #include <dev/le/am79900var.h>
95 
96 #define	AMD_VENDOR	0x1022
97 #define	AMD_PCNET_PCI	0x2000
98 #define	AMD_PCNET_HOME	0x2001
99 #define	PCNET_MEMSIZE	(32*1024)
100 #define	PCNET_PCI_RDP	0x10
101 #define	PCNET_PCI_RAP	0x12
102 #define	PCNET_PCI_BDP	0x16
103 
104 struct le_pci_softc {
105 	struct am79900_softc	sc_am79900;	/* glue to MI code */
106 
107 	struct resource		*sc_rres;
108 
109 	struct resource		*sc_ires;
110 	void			*sc_ih;
111 
112 	bus_dma_tag_t		sc_pdmat;
113 	bus_dma_tag_t		sc_dmat;
114 	bus_dmamap_t		sc_dmam;
115 };
116 
117 static device_probe_t le_pci_probe;
118 static device_attach_t le_pci_attach;
119 static device_detach_t le_pci_detach;
120 static device_resume_t le_pci_resume;
121 static device_suspend_t le_pci_suspend;
122 
123 static device_method_t le_pci_methods[] = {
124 	/* Device interface */
125 	DEVMETHOD(device_probe,		le_pci_probe),
126 	DEVMETHOD(device_attach,	le_pci_attach),
127 	DEVMETHOD(device_detach,	le_pci_detach),
128 	/* We can just use the suspend method here. */
129 	DEVMETHOD(device_shutdown,	le_pci_suspend),
130 	DEVMETHOD(device_suspend,	le_pci_suspend),
131 	DEVMETHOD(device_resume,	le_pci_resume),
132 
133 	{ 0, 0 }
134 };
135 
136 DEFINE_CLASS_0(le, le_pci_driver, le_pci_methods, sizeof(struct le_pci_softc));
137 DRIVER_MODULE(le, pci, le_pci_driver, 0, 0);
138 MODULE_DEPEND(le, ether, 1, 1, 1);
139 
140 static const int le_home_supmedia[] = {
141 	IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, 0)
142 };
143 
144 static const int le_pci_supmedia[] = {
145 	IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
146 	IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, IFM_FDX, 0),
147 	IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
148 	IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
149 	IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0),
150 	IFM_MAKEWORD(IFM_ETHER, IFM_10_5, IFM_FDX, 0)
151 };
152 
153 static void le_pci_wrbcr(struct lance_softc *, uint16_t, uint16_t);
154 static uint16_t le_pci_rdbcr(struct lance_softc *, uint16_t);
155 static void le_pci_wrcsr(struct lance_softc *, uint16_t, uint16_t);
156 static uint16_t le_pci_rdcsr(struct lance_softc *, uint16_t);
157 static int le_pci_mediachange(struct lance_softc *);
158 static void le_pci_hwreset(struct lance_softc *);
159 static bus_dmamap_callback_t le_pci_dma_callback;
160 
161 static void
162 le_pci_wrbcr(struct lance_softc *sc, uint16_t port, uint16_t val)
163 {
164 	struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
165 
166 	bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
167 	bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
168 	bus_write_2(lesc->sc_rres, PCNET_PCI_BDP, val);
169 }
170 
171 static uint16_t
172 le_pci_rdbcr(struct lance_softc *sc, uint16_t port)
173 {
174 	struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
175 
176 	bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
177 	bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
178 	return (bus_read_2(lesc->sc_rres, PCNET_PCI_BDP));
179 }
180 
181 static void
182 le_pci_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
183 {
184 	struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
185 
186 	bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
187 	bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
188 	bus_write_2(lesc->sc_rres, PCNET_PCI_RDP, val);
189 }
190 
191 static uint16_t
192 le_pci_rdcsr(struct lance_softc *sc, uint16_t port)
193 {
194 	struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
195 
196 	bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
197 	bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
198 	return (bus_read_2(lesc->sc_rres, PCNET_PCI_RDP));
199 }
200 
201 static int
202 le_pci_mediachange(struct lance_softc *sc)
203 {
204 	struct ifmedia *ifm = &sc->sc_media;
205 	uint16_t reg;
206 
207 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
208 		return (EINVAL);
209 
210 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
211 		le_pci_wrbcr(sc, LE_BCR49,
212 		    (le_pci_rdbcr(sc, LE_BCR49) & ~LE_B49_PHYSEL) | 0x1);
213 	else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO)
214 		le_pci_wrbcr(sc, LE_BCR2,
215 		    le_pci_rdbcr(sc, LE_BCR2) | LE_B2_ASEL);
216 	else {
217 		le_pci_wrbcr(sc, LE_BCR2,
218 		    le_pci_rdbcr(sc, LE_BCR2) & ~LE_B2_ASEL);
219 
220 		reg = le_pci_rdcsr(sc, LE_CSR15);
221 		reg &= ~LE_C15_PORTSEL(LE_PORTSEL_MASK);
222 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T)
223 			reg |= LE_C15_PORTSEL(LE_PORTSEL_10T);
224 		else
225 			reg |= LE_C15_PORTSEL(LE_PORTSEL_AUI);
226 		le_pci_wrcsr(sc, LE_CSR15, reg);
227 	}
228 
229 	reg = le_pci_rdbcr(sc, LE_BCR9);
230 	if (IFM_OPTIONS(ifm->ifm_media) & IFM_FDX) {
231 		reg |= LE_B9_FDEN;
232 		/*
233 		 * Allow FDX on AUI only if explicitly chosen,
234 		 * not in autoselect mode.
235 		 */
236 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_5)
237 			reg |= LE_B9_AUIFD;
238 		else
239 			reg &= ~LE_B9_AUIFD;
240 	} else
241 		reg &= ~LE_B9_FDEN;
242 	le_pci_wrbcr(sc, LE_BCR9, reg);
243 
244 	return (0);
245 }
246 
247 static void
248 le_pci_hwreset(struct lance_softc *sc)
249 {
250 
251 	/*
252 	 * Chip is stopped. Set software style to PCnet-PCI (32-bit).
253 	 * Actually, am79900.c implements ILACC support (hence its
254 	 * name) but unfortunately VMware does not. As far as this
255 	 * driver is concerned that should not make a difference
256 	 * though, as the settings used have the same meaning for
257 	 * both, ILACC and PCnet-PCI (note that there would be a
258 	 * difference for the ADD_FCS/NO_FCS bit if used).
259 	 */
260 	le_pci_wrbcr(sc, LE_BCR20, LE_B20_SSTYLE_PCNETPCI2);
261 }
262 
263 static void
264 le_pci_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
265 {
266 	struct lance_softc *sc = (struct lance_softc *)xsc;
267 
268 	if (error != 0)
269 		return;
270 	KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__));
271 	sc->sc_addr = segs[0].ds_addr;
272 }
273 
274 static int
275 le_pci_probe(device_t dev)
276 {
277 
278 	if (pci_get_vendor(dev) != AMD_VENDOR)
279 		return (ENXIO);
280 
281 	switch (pci_get_device(dev)) {
282 	case AMD_PCNET_PCI:
283 		device_set_desc(dev, "AMD PCnet-PCI");
284 		/* Let pcn(4) win. */
285 		return (BUS_PROBE_LOW_PRIORITY);
286 	case AMD_PCNET_HOME:
287 		device_set_desc(dev, "AMD PCnet-Home");
288 		/* Let pcn(4) win. */
289 		return (BUS_PROBE_LOW_PRIORITY);
290 	default:
291 		return (ENXIO);
292 	}
293 }
294 
295 static int
296 le_pci_attach(device_t dev)
297 {
298 	struct le_pci_softc *lesc;
299 	struct lance_softc *sc;
300 	int error, i;
301 
302 	lesc = device_get_softc(dev);
303 	sc = &lesc->sc_am79900.lsc;
304 
305 	LE_LOCK_INIT(sc, device_get_nameunit(dev));
306 
307 	pci_enable_busmaster(dev);
308 
309 	i = PCIR_BAR(0);
310 	lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
311 	    &i, RF_ACTIVE);
312 	if (lesc->sc_rres == NULL) {
313 		device_printf(dev, "cannot allocate registers\n");
314 		error = ENXIO;
315 		goto fail_mtx;
316 	}
317 
318 	i = 0;
319 	if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
320 	    &i, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
321 		device_printf(dev, "cannot allocate interrupt\n");
322 		error = ENXIO;
323 		goto fail_rres;
324 	}
325 
326 	error = bus_dma_tag_create(
327 	    bus_get_dma_tag(dev),	/* parent */
328 	    1, 0,			/* alignment, boundary */
329 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
330 	    BUS_SPACE_MAXADDR,		/* highaddr */
331 	    NULL, NULL,			/* filter, filterarg */
332 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
333 	    0,				/* nsegments */
334 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
335 	    0,				/* flags */
336 	    NULL, NULL,			/* lockfunc, lockarg */
337 	    &lesc->sc_pdmat);
338 	if (error != 0) {
339 		device_printf(dev, "cannot allocate parent DMA tag\n");
340 		goto fail_ires;
341 	}
342 
343 	sc->sc_memsize = PCNET_MEMSIZE;
344 	/*
345 	 * For Am79C970A, Am79C971 and Am79C978 the init block must be 2-byte
346 	 * aligned and the ring descriptors must be 16-byte aligned when using
347 	 * a 32-bit software style.
348 	 */
349 	error = bus_dma_tag_create(
350 	    lesc->sc_pdmat,		/* parent */
351 	    16, 0,			/* alignment, boundary */
352 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
353 	    BUS_SPACE_MAXADDR,		/* highaddr */
354 	    NULL, NULL,			/* filter, filterarg */
355 	    sc->sc_memsize,		/* maxsize */
356 	    1,				/* nsegments */
357 	    sc->sc_memsize,		/* maxsegsize */
358 	    0,				/* flags */
359 	    NULL, NULL,			/* lockfunc, lockarg */
360 	    &lesc->sc_dmat);
361 	if (error != 0) {
362 		device_printf(dev, "cannot allocate buffer DMA tag\n");
363 		goto fail_pdtag;
364 	}
365 
366 	error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem,
367 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam);
368 	if (error != 0) {
369 		device_printf(dev, "cannot allocate DMA buffer memory\n");
370 		goto fail_dtag;
371 	}
372 
373 	sc->sc_addr = 0;
374 	error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem,
375 	    sc->sc_memsize, le_pci_dma_callback, sc, 0);
376 	if (error != 0 || sc->sc_addr == 0) {
377 		device_printf(dev, "cannot load DMA buffer map\n");
378 		goto fail_dmem;
379 	}
380 
381 	sc->sc_flags = LE_BSWAP;
382 	sc->sc_conf3 = 0;
383 
384 	sc->sc_mediastatus = NULL;
385 	switch (pci_get_device(dev)) {
386 	case AMD_PCNET_HOME:
387 		sc->sc_mediachange = le_pci_mediachange;
388 		sc->sc_supmedia = le_home_supmedia;
389 		sc->sc_nsupmedia = sizeof(le_home_supmedia) / sizeof(int);
390 		sc->sc_defaultmedia = le_home_supmedia[0];
391 		break;
392 	default:
393 		sc->sc_mediachange = le_pci_mediachange;
394 		sc->sc_supmedia = le_pci_supmedia;
395 		sc->sc_nsupmedia = sizeof(le_pci_supmedia) / sizeof(int);
396 		sc->sc_defaultmedia = le_pci_supmedia[0];
397 	}
398 
399 	/*
400 	 * Extract the physical MAC address from the ROM.
401 	 */
402 	bus_read_region_1(lesc->sc_rres, 0, sc->sc_enaddr,
403 	    sizeof(sc->sc_enaddr));
404 
405 	sc->sc_copytodesc = lance_copytobuf_contig;
406 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
407 	sc->sc_copytobuf = lance_copytobuf_contig;
408 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
409 	sc->sc_zerobuf = lance_zerobuf_contig;
410 
411 	sc->sc_rdcsr = le_pci_rdcsr;
412 	sc->sc_wrcsr = le_pci_wrcsr;
413 	sc->sc_hwreset = le_pci_hwreset;
414 	sc->sc_hwinit = NULL;
415 	sc->sc_hwintr = NULL;
416 	sc->sc_nocarrier = NULL;
417 
418 	error = am79900_config(&lesc->sc_am79900, device_get_name(dev),
419 	    device_get_unit(dev));
420 	if (error != 0) {
421 		device_printf(dev, "cannot attach Am79900\n");
422 		goto fail_dmap;
423 	}
424 
425 	error = bus_setup_intr(dev, lesc->sc_ires, INTR_TYPE_NET | INTR_MPSAFE,
426 	    NULL, am79900_intr, sc, &lesc->sc_ih);
427 	if (error != 0) {
428 		device_printf(dev, "cannot set up interrupt\n");
429 		goto fail_am79900;
430 	}
431 
432 	return (0);
433 
434  fail_am79900:
435 	am79900_detach(&lesc->sc_am79900);
436  fail_dmap:
437 	bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
438  fail_dmem:
439 	bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
440  fail_dtag:
441 	bus_dma_tag_destroy(lesc->sc_dmat);
442  fail_pdtag:
443 	bus_dma_tag_destroy(lesc->sc_pdmat);
444  fail_ires:
445 	bus_release_resource(dev, SYS_RES_IRQ,
446 	    rman_get_rid(lesc->sc_ires), lesc->sc_ires);
447  fail_rres:
448 	bus_release_resource(dev, SYS_RES_IOPORT,
449 	    rman_get_rid(lesc->sc_rres), lesc->sc_rres);
450  fail_mtx:
451 	LE_LOCK_DESTROY(sc);
452 	return (error);
453 }
454 
455 static int
456 le_pci_detach(device_t dev)
457 {
458 	struct le_pci_softc *lesc;
459 	struct lance_softc *sc;
460 
461 	lesc = device_get_softc(dev);
462 	sc = &lesc->sc_am79900.lsc;
463 
464 	bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih);
465 	am79900_detach(&lesc->sc_am79900);
466 	bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
467 	bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
468 	bus_dma_tag_destroy(lesc->sc_dmat);
469 	bus_dma_tag_destroy(lesc->sc_pdmat);
470 	bus_release_resource(dev, SYS_RES_IRQ,
471 	    rman_get_rid(lesc->sc_ires), lesc->sc_ires);
472 	bus_release_resource(dev, SYS_RES_IOPORT,
473 	    rman_get_rid(lesc->sc_rres), lesc->sc_rres);
474 	LE_LOCK_DESTROY(sc);
475 
476 	return (0);
477 }
478 
479 static int
480 le_pci_suspend(device_t dev)
481 {
482 	struct le_pci_softc *lesc;
483 
484 	lesc = device_get_softc(dev);
485 
486 	lance_suspend(&lesc->sc_am79900.lsc);
487 
488 	return (0);
489 }
490 
491 static int
492 le_pci_resume(device_t dev)
493 {
494 	struct le_pci_softc *lesc;
495 
496 	lesc = device_get_softc(dev);
497 
498 	lance_resume(&lesc->sc_am79900.lsc);
499 
500 	return (0);
501 }
502