1 /* $NetBSD: if_le_ledma.c,v 1.26 2005/12/11 12:23:44 christos Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/endian.h>
47 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/resource.h>
53 #include <sys/socket.h>
55 #include <dev/ofw/ofw_bus.h>
57 #include <machine/bus.h>
58 #include <machine/ofw_machdep.h>
59 #include <machine/resource.h>
61 #include <net/ethernet.h>
63 #include <net/if_media.h>
65 #include <sparc64/sbus/lsi64854reg.h>
66 #include <sparc64/sbus/lsi64854var.h>
68 #include <dev/le/lancereg.h>
69 #include <dev/le/lancevar.h>
70 #include <dev/le/am7990var.h>
72 #define LEDMA_ALIGNMENT 8 /* ring desc. alignmet for NCR92C990 */
73 #define LEDMA_BOUNDARY (16*1024*1024) /* must not cross 16MB boundary */
74 #define LEDMA_MEMSIZE (16*1024) /* LANCE memory size */
75 #define LEREG1_RDP 0 /* Register Data Port */
76 #define LEREG1_RAP 2 /* Register Address Port */
79 struct am7990_softc sc_am7990; /* glue to MI code */
82 struct resource *sc_rres;
83 bus_space_tag_t sc_regt;
84 bus_space_handle_t sc_regh;
87 struct resource *sc_ires;
90 bus_dma_tag_t sc_dmat;
92 bus_addr_t sc_laddr; /* LANCE DMA address */
94 struct lsi64854_softc *sc_dma; /* pointer to DMA engine */
97 static device_probe_t le_dma_probe;
98 static device_attach_t le_dma_attach;
99 static device_detach_t le_dma_detach;
100 static device_resume_t le_dma_resume;
101 static device_suspend_t le_dma_suspend;
103 static device_method_t le_dma_methods[] = {
104 /* Device interface */
105 DEVMETHOD(device_probe, le_dma_probe),
106 DEVMETHOD(device_attach, le_dma_attach),
107 DEVMETHOD(device_detach, le_dma_detach),
108 /* We can just use the suspend method here. */
109 DEVMETHOD(device_shutdown, le_dma_suspend),
110 DEVMETHOD(device_suspend, le_dma_suspend),
111 DEVMETHOD(device_resume, le_dma_resume),
116 DEFINE_CLASS_0(le, le_dma_driver, le_dma_methods, sizeof(struct le_dma_softc));
117 DRIVER_MODULE(le, dma, le_dma_driver, le_devclass, 0, 0);
118 MODULE_DEPEND(le, ether, 1, 1, 1);
121 * Media types supported
123 static const int le_dma_supmedia[] = {
124 IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
125 IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
126 IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0)
129 static void le_dma_wrcsr(struct lance_softc *, uint16_t, uint16_t);
130 static uint16_t le_dma_rdcsr(struct lance_softc *, uint16_t);
131 static void le_dma_setutp(struct lance_softc *);
132 static void le_dma_setaui(struct lance_softc *);
133 static int le_dma_supmediachange(struct lance_softc *);
134 static void le_dma_supmediastatus(struct lance_softc *, struct ifmediareq *);
135 static void le_dma_hwreset(struct lance_softc *);
136 static int le_dma_hwintr(struct lance_softc *);
137 static void le_dma_nocarrier(struct lance_softc *);
138 static bus_dmamap_callback_t le_dma_dma_callback;
141 le_dma_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
143 struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
145 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, LEREG1_RAP, port);
146 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, LEREG1_RAP, 2,
147 BUS_SPACE_BARRIER_WRITE);
148 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, LEREG1_RDP, val);
152 le_dma_rdcsr(struct lance_softc *sc, uint16_t port)
154 struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
156 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, LEREG1_RAP, port);
157 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, LEREG1_RAP, 2,
158 BUS_SPACE_BARRIER_WRITE);
159 return (bus_space_read_2(lesc->sc_regt, lesc->sc_regh, LEREG1_RDP));
163 le_dma_setutp(struct lance_softc *sc)
165 struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
167 L64854_SCSR(dma, L64854_GCSR(dma) | E_TP_AUI);
168 DELAY(20000); /* We must not touch the LANCE chip for 20ms. */
172 le_dma_setaui(struct lance_softc *sc)
174 struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
176 L64854_SCSR(dma, L64854_GCSR(dma) & ~E_TP_AUI);
177 DELAY(20000); /* We must not touch the LANCE chip for 20ms. */
181 le_dma_supmediachange(struct lance_softc *sc)
183 struct ifmedia *ifm = &sc->sc_media;
185 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
189 * Switch to the selected media. If autoselect is set, we don't
190 * really have to do anything. We'll switch to the other media
191 * when we detect loss of carrier.
193 switch (IFM_SUBTYPE(ifm->ifm_media)) {
213 le_dma_supmediastatus(struct lance_softc *sc, struct ifmediareq *ifmr)
215 struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
218 * Notify the world which media we're currently using.
220 if (L64854_GCSR(dma) & E_TP_AUI)
221 ifmr->ifm_active = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
223 ifmr->ifm_active = IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0);
227 le_dma_hwreset(struct lance_softc *sc)
229 struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
230 struct lsi64854_softc *dma = lesc->sc_dma;
231 uint32_t aui_bit, csr;
236 csr = L64854_GCSR(dma);
237 aui_bit = csr & E_TP_AUI;
240 /* Write bits 24-31 of Lance address. */
241 bus_space_write_4(dma->sc_regt, dma->sc_regh, L64854_REG_ENBAR,
242 lesc->sc_laddr & 0xff000000);
247 * Disable E-cache invalidates on chip writes.
248 * Retain previous cable selection bit.
250 csr = L64854_GCSR(dma);
251 csr |= (E_DSBL_WR_INVAL | aui_bit);
252 L64854_SCSR(dma, csr);
253 DELAY(20000); /* We must not touch the LANCE chip for 20ms. */
257 le_dma_hwintr(struct lance_softc *sc)
259 struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
260 struct lsi64854_softc *dma = lesc->sc_dma;
262 return (DMA_INTR(dma));
266 le_dma_nocarrier(struct lance_softc *sc)
268 struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
271 * Check if the user has requested a certain cable type, and
272 * if so, honor that request.
275 if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
276 switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
279 if_printf(sc->sc_ifp, "lost carrier on UTP port, "
280 "switching to AUI port\n");
284 switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
287 if_printf(sc->sc_ifp, "lost carrier on AUI port, "
288 "switching to UTP port\n");
295 le_dma_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
297 struct le_dma_softc *lesc = (struct le_dma_softc *)xsc;
301 KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__));
302 lesc->sc_laddr = segs[0].ds_addr;
306 le_dma_probe(device_t dev)
309 if (strcmp(ofw_bus_get_name(dev), "le") == 0) {
310 device_set_desc(dev, "LANCE Ethernet");
311 return (BUS_PROBE_DEFAULT);
317 le_dma_attach(device_t dev)
319 struct le_dma_softc *lesc;
320 struct lsi64854_softc *dma;
321 struct lance_softc *sc;
324 lesc = device_get_softc(dev);
325 sc = &lesc->sc_am7990.lsc;
327 LE_LOCK_INIT(sc, device_get_nameunit(dev));
330 * Establish link to `ledma' device.
333 dma = (struct lsi64854_softc *)device_get_softc(device_get_parent(dev));
335 lesc->sc_dma->sc_client = lesc;
338 lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
339 &lesc->sc_rrid, RF_ACTIVE);
340 if (lesc->sc_rres == NULL) {
341 device_printf(dev, "cannot allocate registers\n");
345 lesc->sc_regt = rman_get_bustag(lesc->sc_rres);
346 lesc->sc_regh = rman_get_bushandle(lesc->sc_rres);
349 if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
350 &lesc->sc_irid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
351 device_printf(dev, "cannot allocate interrupt\n");
356 sc->sc_memsize = LEDMA_MEMSIZE;
357 error = bus_dma_tag_create(
358 dma->sc_parent_dmat, /* parent */
359 LEDMA_ALIGNMENT, /* alignment */
360 LEDMA_BOUNDARY, /* boundary */
361 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
362 BUS_SPACE_MAXADDR, /* highaddr */
363 NULL, NULL, /* filter, filterarg */
364 sc->sc_memsize, /* maxsize */
366 sc->sc_memsize, /* maxsegsize */
367 BUS_DMA_WAITOK, /* flags */
368 NULL, NULL, /* lockfunc, lockarg */
371 device_printf(dev, "cannot allocate buffer DMA tag\n");
375 error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem,
376 BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam);
378 device_printf(dev, "cannot allocate DMA buffer memory\n");
383 error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem,
384 sc->sc_memsize, le_dma_dma_callback, lesc, 0);
385 if (error != 0 || lesc->sc_laddr == 0) {
386 device_printf(dev, "cannot load DMA buffer map\n");
390 sc->sc_addr = lesc->sc_laddr & 0xffffff;
392 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
394 sc->sc_mediachange = le_dma_supmediachange;
395 sc->sc_mediastatus = le_dma_supmediastatus;
396 sc->sc_supmedia = le_dma_supmedia;
397 sc->sc_nsupmedia = sizeof(le_dma_supmedia) / sizeof(le_dma_supmedia[0]);
398 sc->sc_defaultmedia = le_dma_supmedia[0];
400 OF_getetheraddr(dev, sc->sc_enaddr);
402 sc->sc_copytodesc = lance_copytobuf_contig;
403 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
404 sc->sc_copytobuf = lance_copytobuf_contig;
405 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
406 sc->sc_zerobuf = lance_zerobuf_contig;
408 sc->sc_rdcsr = le_dma_rdcsr;
409 sc->sc_wrcsr = le_dma_wrcsr;
410 sc->sc_hwreset = le_dma_hwreset;
411 sc->sc_hwintr = le_dma_hwintr;
412 sc->sc_nocarrier = le_dma_nocarrier;
414 error = am7990_config(&lesc->sc_am7990, device_get_name(dev),
415 device_get_unit(dev));
417 device_printf(dev, "cannot attach Am7990\n");
421 error = bus_setup_intr(dev, lesc->sc_ires, INTR_TYPE_NET | INTR_MPSAFE,
422 am7990_intr, sc, &lesc->sc_ih);
424 device_printf(dev, "cannot set up interrupt\n");
431 am7990_detach(&lesc->sc_am7990);
433 bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
435 bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
437 bus_dma_tag_destroy(lesc->sc_dmat);
439 bus_release_resource(dev, SYS_RES_IRQ, lesc->sc_irid, lesc->sc_ires);
441 bus_release_resource(dev, SYS_RES_MEMORY, lesc->sc_rrid, lesc->sc_rres);
448 le_dma_detach(device_t dev)
450 struct le_dma_softc *lesc;
451 struct lance_softc *sc;
453 lesc = device_get_softc(dev);
454 sc = &lesc->sc_am7990.lsc;
456 bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih);
457 am7990_detach(&lesc->sc_am7990);
458 bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
459 bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
460 bus_dma_tag_destroy(lesc->sc_dmat);
461 bus_release_resource(dev, SYS_RES_IRQ, lesc->sc_irid, lesc->sc_ires);
462 bus_release_resource(dev, SYS_RES_MEMORY, lesc->sc_rrid, lesc->sc_rres);
469 le_dma_suspend(device_t dev)
471 struct le_dma_softc *lesc;
473 lesc = device_get_softc(dev);
475 lance_suspend(&lesc->sc_am7990.lsc);
481 le_dma_resume(device_t dev)
483 struct le_dma_softc *lesc;
485 lesc = device_get_softc(dev);
487 lance_resume(&lesc->sc_am7990.lsc);