xref: /freebsd/sys/dev/le/if_le_pci.c (revision 7c43148a974877188a930e4078a164f83da8e652)
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 
67 #include <sys/cdefs.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/bus.h>
71 #include <sys/endian.h>
72 #include <sys/kernel.h>
73 #include <sys/lock.h>
74 #include <sys/module.h>
75 #include <sys/mutex.h>
76 #include <sys/resource.h>
77 #include <sys/rman.h>
78 #include <sys/socket.h>
79 
80 #include <net/ethernet.h>
81 #include <net/if.h>
82 #include <net/if_media.h>
83 
84 #include <machine/bus.h>
85 #include <machine/resource.h>
86 
87 #include <dev/pci/pcireg.h>
88 #include <dev/pci/pcivar.h>
89 
90 #include <dev/le/lancereg.h>
91 #include <dev/le/lancevar.h>
92 #include <dev/le/am79900var.h>
93 
94 #define	AMD_VENDOR	0x1022
95 #define	AMD_PCNET_PCI	0x2000
96 #define	AMD_PCNET_HOME	0x2001
97 #define	PCNET_MEMSIZE	(32*1024)
98 #define	PCNET_PCI_RDP	0x10
99 #define	PCNET_PCI_RAP	0x12
100 #define	PCNET_PCI_BDP	0x16
101 
102 struct le_pci_softc {
103 	struct am79900_softc	sc_am79900;	/* glue to MI code */
104 
105 	struct resource		*sc_rres;
106 
107 	struct resource		*sc_ires;
108 	void			*sc_ih;
109 
110 	bus_dma_tag_t		sc_pdmat;
111 	bus_dma_tag_t		sc_dmat;
112 	bus_dmamap_t		sc_dmam;
113 };
114 
115 static device_probe_t le_pci_probe;
116 static device_attach_t le_pci_attach;
117 static device_detach_t le_pci_detach;
118 static device_resume_t le_pci_resume;
119 static device_suspend_t le_pci_suspend;
120 
121 static device_method_t le_pci_methods[] = {
122 	/* Device interface */
123 	DEVMETHOD(device_probe,		le_pci_probe),
124 	DEVMETHOD(device_attach,	le_pci_attach),
125 	DEVMETHOD(device_detach,	le_pci_detach),
126 	/* We can just use the suspend method here. */
127 	DEVMETHOD(device_shutdown,	le_pci_suspend),
128 	DEVMETHOD(device_suspend,	le_pci_suspend),
129 	DEVMETHOD(device_resume,	le_pci_resume),
130 
131 	{ 0, 0 }
132 };
133 
134 DEFINE_CLASS_0(le, le_pci_driver, le_pci_methods, sizeof(struct le_pci_softc));
135 DRIVER_MODULE(le, pci, le_pci_driver, 0, 0);
136 MODULE_DEPEND(le, ether, 1, 1, 1);
137 
138 static const int le_home_supmedia[] = {
139 	IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, 0)
140 };
141 
142 static const int le_pci_supmedia[] = {
143 	IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
144 	IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, IFM_FDX, 0),
145 	IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
146 	IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
147 	IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0),
148 	IFM_MAKEWORD(IFM_ETHER, IFM_10_5, IFM_FDX, 0)
149 };
150 
151 static void le_pci_wrbcr(struct lance_softc *, uint16_t, uint16_t);
152 static uint16_t le_pci_rdbcr(struct lance_softc *, uint16_t);
153 static void le_pci_wrcsr(struct lance_softc *, uint16_t, uint16_t);
154 static uint16_t le_pci_rdcsr(struct lance_softc *, uint16_t);
155 static int le_pci_mediachange(struct lance_softc *);
156 static void le_pci_hwreset(struct lance_softc *);
157 static bus_dmamap_callback_t le_pci_dma_callback;
158 
159 static void
160 le_pci_wrbcr(struct lance_softc *sc, uint16_t port, uint16_t val)
161 {
162 	struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
163 
164 	bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
165 	bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
166 	bus_write_2(lesc->sc_rres, PCNET_PCI_BDP, val);
167 }
168 
169 static uint16_t
170 le_pci_rdbcr(struct lance_softc *sc, uint16_t port)
171 {
172 	struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
173 
174 	bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
175 	bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
176 	return (bus_read_2(lesc->sc_rres, PCNET_PCI_BDP));
177 }
178 
179 static void
180 le_pci_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
181 {
182 	struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
183 
184 	bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
185 	bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
186 	bus_write_2(lesc->sc_rres, PCNET_PCI_RDP, val);
187 }
188 
189 static uint16_t
190 le_pci_rdcsr(struct lance_softc *sc, uint16_t port)
191 {
192 	struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
193 
194 	bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
195 	bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
196 	return (bus_read_2(lesc->sc_rres, PCNET_PCI_RDP));
197 }
198 
199 static int
200 le_pci_mediachange(struct lance_softc *sc)
201 {
202 	struct ifmedia *ifm = &sc->sc_media;
203 	uint16_t reg;
204 
205 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
206 		return (EINVAL);
207 
208 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
209 		le_pci_wrbcr(sc, LE_BCR49,
210 		    (le_pci_rdbcr(sc, LE_BCR49) & ~LE_B49_PHYSEL) | 0x1);
211 	else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO)
212 		le_pci_wrbcr(sc, LE_BCR2,
213 		    le_pci_rdbcr(sc, LE_BCR2) | LE_B2_ASEL);
214 	else {
215 		le_pci_wrbcr(sc, LE_BCR2,
216 		    le_pci_rdbcr(sc, LE_BCR2) & ~LE_B2_ASEL);
217 
218 		reg = le_pci_rdcsr(sc, LE_CSR15);
219 		reg &= ~LE_C15_PORTSEL(LE_PORTSEL_MASK);
220 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T)
221 			reg |= LE_C15_PORTSEL(LE_PORTSEL_10T);
222 		else
223 			reg |= LE_C15_PORTSEL(LE_PORTSEL_AUI);
224 		le_pci_wrcsr(sc, LE_CSR15, reg);
225 	}
226 
227 	reg = le_pci_rdbcr(sc, LE_BCR9);
228 	if (IFM_OPTIONS(ifm->ifm_media) & IFM_FDX) {
229 		reg |= LE_B9_FDEN;
230 		/*
231 		 * Allow FDX on AUI only if explicitly chosen,
232 		 * not in autoselect mode.
233 		 */
234 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_5)
235 			reg |= LE_B9_AUIFD;
236 		else
237 			reg &= ~LE_B9_AUIFD;
238 	} else
239 		reg &= ~LE_B9_FDEN;
240 	le_pci_wrbcr(sc, LE_BCR9, reg);
241 
242 	return (0);
243 }
244 
245 static void
246 le_pci_hwreset(struct lance_softc *sc)
247 {
248 
249 	/*
250 	 * Chip is stopped. Set software style to PCnet-PCI (32-bit).
251 	 * Actually, am79900.c implements ILACC support (hence its
252 	 * name) but unfortunately VMware does not. As far as this
253 	 * driver is concerned that should not make a difference
254 	 * though, as the settings used have the same meaning for
255 	 * both, ILACC and PCnet-PCI (note that there would be a
256 	 * difference for the ADD_FCS/NO_FCS bit if used).
257 	 */
258 	le_pci_wrbcr(sc, LE_BCR20, LE_B20_SSTYLE_PCNETPCI2);
259 }
260 
261 static void
262 le_pci_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
263 {
264 	struct lance_softc *sc = (struct lance_softc *)xsc;
265 
266 	if (error != 0)
267 		return;
268 	KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__));
269 	sc->sc_addr = segs[0].ds_addr;
270 }
271 
272 static int
273 le_pci_probe(device_t dev)
274 {
275 
276 	if (pci_get_vendor(dev) != AMD_VENDOR)
277 		return (ENXIO);
278 
279 	switch (pci_get_device(dev)) {
280 	case AMD_PCNET_PCI:
281 		device_set_desc(dev, "AMD PCnet-PCI");
282 		/* Let pcn(4) win. */
283 		return (BUS_PROBE_LOW_PRIORITY);
284 	case AMD_PCNET_HOME:
285 		device_set_desc(dev, "AMD PCnet-Home");
286 		/* Let pcn(4) win. */
287 		return (BUS_PROBE_LOW_PRIORITY);
288 	default:
289 		return (ENXIO);
290 	}
291 }
292 
293 static int
294 le_pci_attach(device_t dev)
295 {
296 	struct le_pci_softc *lesc;
297 	struct lance_softc *sc;
298 	int error, i;
299 
300 	lesc = device_get_softc(dev);
301 	sc = &lesc->sc_am79900.lsc;
302 
303 	LE_LOCK_INIT(sc, device_get_nameunit(dev));
304 
305 	pci_enable_busmaster(dev);
306 
307 	i = PCIR_BAR(0);
308 	lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
309 	    &i, RF_ACTIVE);
310 	if (lesc->sc_rres == NULL) {
311 		device_printf(dev, "cannot allocate registers\n");
312 		error = ENXIO;
313 		goto fail_mtx;
314 	}
315 
316 	i = 0;
317 	if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
318 	    &i, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
319 		device_printf(dev, "cannot allocate interrupt\n");
320 		error = ENXIO;
321 		goto fail_rres;
322 	}
323 
324 	error = bus_dma_tag_create(
325 	    bus_get_dma_tag(dev),	/* parent */
326 	    1, 0,			/* alignment, boundary */
327 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
328 	    BUS_SPACE_MAXADDR,		/* highaddr */
329 	    NULL, NULL,			/* filter, filterarg */
330 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
331 	    0,				/* nsegments */
332 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
333 	    0,				/* flags */
334 	    NULL, NULL,			/* lockfunc, lockarg */
335 	    &lesc->sc_pdmat);
336 	if (error != 0) {
337 		device_printf(dev, "cannot allocate parent DMA tag\n");
338 		goto fail_ires;
339 	}
340 
341 	sc->sc_memsize = PCNET_MEMSIZE;
342 	/*
343 	 * For Am79C970A, Am79C971 and Am79C978 the init block must be 2-byte
344 	 * aligned and the ring descriptors must be 16-byte aligned when using
345 	 * a 32-bit software style.
346 	 */
347 	error = bus_dma_tag_create(
348 	    lesc->sc_pdmat,		/* parent */
349 	    16, 0,			/* alignment, boundary */
350 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
351 	    BUS_SPACE_MAXADDR,		/* highaddr */
352 	    NULL, NULL,			/* filter, filterarg */
353 	    sc->sc_memsize,		/* maxsize */
354 	    1,				/* nsegments */
355 	    sc->sc_memsize,		/* maxsegsize */
356 	    0,				/* flags */
357 	    NULL, NULL,			/* lockfunc, lockarg */
358 	    &lesc->sc_dmat);
359 	if (error != 0) {
360 		device_printf(dev, "cannot allocate buffer DMA tag\n");
361 		goto fail_pdtag;
362 	}
363 
364 	error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem,
365 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam);
366 	if (error != 0) {
367 		device_printf(dev, "cannot allocate DMA buffer memory\n");
368 		goto fail_dtag;
369 	}
370 
371 	sc->sc_addr = 0;
372 	error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem,
373 	    sc->sc_memsize, le_pci_dma_callback, sc, 0);
374 	if (error != 0 || sc->sc_addr == 0) {
375 		device_printf(dev, "cannot load DMA buffer map\n");
376 		goto fail_dmem;
377 	}
378 
379 	sc->sc_flags = LE_BSWAP;
380 	sc->sc_conf3 = 0;
381 
382 	sc->sc_mediastatus = NULL;
383 	switch (pci_get_device(dev)) {
384 	case AMD_PCNET_HOME:
385 		sc->sc_mediachange = le_pci_mediachange;
386 		sc->sc_supmedia = le_home_supmedia;
387 		sc->sc_nsupmedia = sizeof(le_home_supmedia) / sizeof(int);
388 		sc->sc_defaultmedia = le_home_supmedia[0];
389 		break;
390 	default:
391 		sc->sc_mediachange = le_pci_mediachange;
392 		sc->sc_supmedia = le_pci_supmedia;
393 		sc->sc_nsupmedia = sizeof(le_pci_supmedia) / sizeof(int);
394 		sc->sc_defaultmedia = le_pci_supmedia[0];
395 	}
396 
397 	/*
398 	 * Extract the physical MAC address from the ROM.
399 	 */
400 	bus_read_region_1(lesc->sc_rres, 0, sc->sc_enaddr,
401 	    sizeof(sc->sc_enaddr));
402 
403 	sc->sc_copytodesc = lance_copytobuf_contig;
404 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
405 	sc->sc_copytobuf = lance_copytobuf_contig;
406 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
407 	sc->sc_zerobuf = lance_zerobuf_contig;
408 
409 	sc->sc_rdcsr = le_pci_rdcsr;
410 	sc->sc_wrcsr = le_pci_wrcsr;
411 	sc->sc_hwreset = le_pci_hwreset;
412 	sc->sc_hwinit = NULL;
413 	sc->sc_hwintr = NULL;
414 	sc->sc_nocarrier = NULL;
415 
416 	error = am79900_config(&lesc->sc_am79900, device_get_name(dev),
417 	    device_get_unit(dev));
418 	if (error != 0) {
419 		device_printf(dev, "cannot attach Am79900\n");
420 		goto fail_dmap;
421 	}
422 
423 	error = bus_setup_intr(dev, lesc->sc_ires, INTR_TYPE_NET | INTR_MPSAFE,
424 	    NULL, am79900_intr, sc, &lesc->sc_ih);
425 	if (error != 0) {
426 		device_printf(dev, "cannot set up interrupt\n");
427 		goto fail_am79900;
428 	}
429 
430 	return (0);
431 
432  fail_am79900:
433 	am79900_detach(&lesc->sc_am79900);
434  fail_dmap:
435 	bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
436  fail_dmem:
437 	bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
438  fail_dtag:
439 	bus_dma_tag_destroy(lesc->sc_dmat);
440  fail_pdtag:
441 	bus_dma_tag_destroy(lesc->sc_pdmat);
442  fail_ires:
443 	bus_release_resource(dev, SYS_RES_IRQ,
444 	    rman_get_rid(lesc->sc_ires), lesc->sc_ires);
445  fail_rres:
446 	bus_release_resource(dev, SYS_RES_IOPORT,
447 	    rman_get_rid(lesc->sc_rres), lesc->sc_rres);
448  fail_mtx:
449 	LE_LOCK_DESTROY(sc);
450 	return (error);
451 }
452 
453 static int
454 le_pci_detach(device_t dev)
455 {
456 	struct le_pci_softc *lesc;
457 	struct lance_softc *sc;
458 
459 	lesc = device_get_softc(dev);
460 	sc = &lesc->sc_am79900.lsc;
461 
462 	bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih);
463 	am79900_detach(&lesc->sc_am79900);
464 	bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
465 	bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
466 	bus_dma_tag_destroy(lesc->sc_dmat);
467 	bus_dma_tag_destroy(lesc->sc_pdmat);
468 	bus_release_resource(dev, SYS_RES_IRQ,
469 	    rman_get_rid(lesc->sc_ires), lesc->sc_ires);
470 	bus_release_resource(dev, SYS_RES_IOPORT,
471 	    rman_get_rid(lesc->sc_rres), lesc->sc_rres);
472 	LE_LOCK_DESTROY(sc);
473 
474 	return (0);
475 }
476 
477 static int
478 le_pci_suspend(device_t dev)
479 {
480 	struct le_pci_softc *lesc;
481 
482 	lesc = device_get_softc(dev);
483 
484 	lance_suspend(&lesc->sc_am79900.lsc);
485 
486 	return (0);
487 }
488 
489 static int
490 le_pci_resume(device_t dev)
491 {
492 	struct le_pci_softc *lesc;
493 
494 	lesc = device_get_softc(dev);
495 
496 	lance_resume(&lesc->sc_am79900.lsc);
497 
498 	return (0);
499 }
500