]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/le/if_le_ledma.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / le / if_le_ledma.c
1 /*      $NetBSD: if_le_ledma.c,v 1.26 2005/12/11 12:23:44 christos Exp $        */
2
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
26  *
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.
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>
46 #include <sys/endian.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/resource.h>
52 #include <sys/rman.h>
53 #include <sys/socket.h>
54
55 #include <dev/ofw/ofw_bus.h>
56
57 #include <machine/bus.h>
58 #include <machine/ofw_machdep.h>
59 #include <machine/resource.h>
60
61 #include <net/ethernet.h>
62 #include <net/if.h>
63 #include <net/if_media.h>
64
65 #include <sparc64/sbus/lsi64854reg.h>
66 #include <sparc64/sbus/lsi64854var.h>
67
68 #include <dev/le/lancereg.h>
69 #include <dev/le/lancevar.h>
70 #include <dev/le/am7990var.h>
71
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 */
77
78 struct le_dma_softc {
79         struct am7990_softc     sc_am7990;      /* glue to MI code */
80
81         struct resource         *sc_rres;
82
83         struct resource         *sc_ires;
84         void                    *sc_ih;
85
86         bus_dma_tag_t           sc_dmat;
87         bus_dmamap_t            sc_dmam;
88         bus_addr_t              sc_laddr;       /* LANCE DMA address */
89
90         struct lsi64854_softc   *sc_dma;        /* pointer to DMA engine */
91 };
92
93 static device_probe_t le_dma_probe;
94 static device_attach_t le_dma_attach;
95 static device_detach_t le_dma_detach;
96 static device_resume_t le_dma_resume;
97 static device_suspend_t le_dma_suspend;
98
99 static device_method_t le_dma_methods[] = {
100         /* Device interface */
101         DEVMETHOD(device_probe,         le_dma_probe),
102         DEVMETHOD(device_attach,        le_dma_attach),
103         DEVMETHOD(device_detach,        le_dma_detach),
104         /* We can just use the suspend method here. */
105         DEVMETHOD(device_shutdown,      le_dma_suspend),
106         DEVMETHOD(device_suspend,       le_dma_suspend),
107         DEVMETHOD(device_resume,        le_dma_resume),
108
109         { 0, 0 }
110 };
111
112 DEFINE_CLASS_0(le, le_dma_driver, le_dma_methods, sizeof(struct le_dma_softc));
113 DRIVER_MODULE(le, dma, le_dma_driver, le_devclass, 0, 0);
114 MODULE_DEPEND(le, dma, 1, 1, 1);
115 MODULE_DEPEND(le, ether, 1, 1, 1);
116
117 /*
118  * Media types supported
119  */
120 static const int le_dma_supmedia[] = {
121         IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
122         IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
123         IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0)
124 };
125
126 static void le_dma_wrcsr(struct lance_softc *, uint16_t, uint16_t);
127 static uint16_t le_dma_rdcsr(struct lance_softc *, uint16_t);
128 static void le_dma_setutp(struct lance_softc *);
129 static void le_dma_setaui(struct lance_softc *);
130 static int le_dma_supmediachange(struct lance_softc *);
131 static void le_dma_supmediastatus(struct lance_softc *, struct ifmediareq *);
132 static void le_dma_hwreset(struct lance_softc *);
133 static int le_dma_hwintr(struct lance_softc *);
134 static void le_dma_nocarrier(struct lance_softc *);
135 static bus_dmamap_callback_t le_dma_dma_callback;
136
137 static void
138 le_dma_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
139 {
140         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
141
142         bus_write_2(lesc->sc_rres, LEREG1_RAP, port);
143         bus_barrier(lesc->sc_rres, LEREG1_RAP, 2, BUS_SPACE_BARRIER_WRITE);
144         bus_write_2(lesc->sc_rres, LEREG1_RDP, val);
145 }
146
147 static uint16_t
148 le_dma_rdcsr(struct lance_softc *sc, uint16_t port)
149 {
150         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
151
152         bus_write_2(lesc->sc_rres, LEREG1_RAP, port);
153         bus_barrier(lesc->sc_rres, LEREG1_RAP, 2, BUS_SPACE_BARRIER_WRITE);
154         return (bus_read_2(lesc->sc_rres, LEREG1_RDP));
155 }
156
157 static void
158 le_dma_setutp(struct lance_softc *sc)
159 {
160         struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
161
162         L64854_SCSR(dma, L64854_GCSR(dma) | E_TP_AUI);
163         DELAY(20000);   /* We must not touch the LANCE chip for 20ms. */
164 }
165
166 static void
167 le_dma_setaui(struct lance_softc *sc)
168 {
169         struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
170
171         L64854_SCSR(dma, L64854_GCSR(dma) & ~E_TP_AUI);
172         DELAY(20000);   /* We must not touch the LANCE chip for 20ms. */
173 }
174
175 static int
176 le_dma_supmediachange(struct lance_softc *sc)
177 {
178         struct ifmedia *ifm = &sc->sc_media;
179
180         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
181                 return (EINVAL);
182
183         /*
184          * Switch to the selected media. If autoselect is set, we don't
185          * really have to do anything. We'll switch to the other media
186          * when we detect loss of carrier.
187          */
188         switch (IFM_SUBTYPE(ifm->ifm_media)) {
189         case IFM_10_T:
190                 le_dma_setutp(sc);
191                 break;
192
193         case IFM_10_5:
194                 le_dma_setaui(sc);
195                 break;
196
197         case IFM_AUTO:
198                 break;
199
200         default:
201                 return (EINVAL);
202         }
203
204         return (0);
205 }
206
207 static void
208 le_dma_supmediastatus(struct lance_softc *sc, struct ifmediareq *ifmr)
209 {
210         struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
211
212         /*
213          * Notify the world which media we're currently using.
214          */
215         if (L64854_GCSR(dma) & E_TP_AUI)
216                 ifmr->ifm_active = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
217         else
218                 ifmr->ifm_active = IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0);
219 }
220
221 static void
222 le_dma_hwreset(struct lance_softc *sc)
223 {
224         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
225         struct lsi64854_softc *dma = lesc->sc_dma;
226         uint32_t aui_bit, csr;
227
228         /*
229          * Reset DMA channel.
230          */
231         csr = L64854_GCSR(dma);
232         aui_bit = csr & E_TP_AUI;
233         DMA_RESET(dma);
234
235         /* Write bits 24-31 of Lance address. */
236         bus_write_4(dma->sc_res, L64854_REG_ENBAR,
237             lesc->sc_laddr & 0xff000000);
238
239         DMA_ENINTR(dma);
240
241         /*
242          * Disable E-cache invalidates on chip writes.
243          * Retain previous cable selection bit.
244          */
245         csr = L64854_GCSR(dma);
246         csr |= (E_DSBL_WR_INVAL | aui_bit);
247         L64854_SCSR(dma, csr);
248         DELAY(20000);   /* We must not touch the LANCE chip for 20ms. */
249 }
250
251 static int
252 le_dma_hwintr(struct lance_softc *sc)
253 {
254         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
255         struct lsi64854_softc *dma = lesc->sc_dma;
256
257         return (DMA_INTR(dma));
258 }
259
260 static void
261 le_dma_nocarrier(struct lance_softc *sc)
262 {
263         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
264
265         /*
266          * Check if the user has requested a certain cable type, and
267          * if so, honor that request.
268          */
269
270         if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
271                 switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
272                 case IFM_10_5:
273                 case IFM_AUTO:
274                         if_printf(sc->sc_ifp, "lost carrier on UTP port, "
275                             "switching to AUI port\n");
276                         le_dma_setaui(sc);
277                 }
278         } else {
279                 switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
280                 case IFM_10_T:
281                 case IFM_AUTO:
282                         if_printf(sc->sc_ifp, "lost carrier on AUI port, "
283                             "switching to UTP port\n");
284                         le_dma_setutp(sc);
285                 }
286         }
287 }
288
289 static void
290 le_dma_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
291 {
292         struct le_dma_softc *lesc = (struct le_dma_softc *)xsc;
293
294         if (error != 0)
295                 return;
296         KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__));
297         lesc->sc_laddr = segs[0].ds_addr;
298 }
299
300 static int
301 le_dma_probe(device_t dev)
302 {
303
304         if (strcmp(ofw_bus_get_name(dev), "le") == 0) {
305                 device_set_desc(dev, "LANCE Ethernet");
306                 return (BUS_PROBE_DEFAULT);
307         }
308         return (ENXIO);
309 }
310
311 static int
312 le_dma_attach(device_t dev)
313 {
314         struct le_dma_softc *lesc;
315         struct lsi64854_softc *dma;
316         struct lance_softc *sc;
317         int error, i;
318
319         lesc = device_get_softc(dev);
320         sc = &lesc->sc_am7990.lsc;
321
322         LE_LOCK_INIT(sc, device_get_nameunit(dev));
323
324         /*
325          * Establish link to `ledma' device.
326          * XXX hackery.
327          */
328         dma = (struct lsi64854_softc *)device_get_softc(device_get_parent(dev));
329         lesc->sc_dma = dma;
330         lesc->sc_dma->sc_client = lesc;
331
332         i = 0;
333         lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
334             &i, RF_ACTIVE);
335         if (lesc->sc_rres == NULL) {
336                 device_printf(dev, "cannot allocate registers\n");
337                 error = ENXIO;
338                 goto fail_mtx;
339         }
340
341         i = 0;
342         if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
343             &i, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
344                 device_printf(dev, "cannot allocate interrupt\n");
345                 error = ENXIO;
346                 goto fail_rres;
347         }
348
349         /* Attach the DMA engine. */
350         error = lsi64854_attach(dma);
351         if (error != 0) {
352                 device_printf(dev, "lsi64854_attach failed\n");
353                 goto fail_ires;
354         }
355
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 */
365             1,                          /* nsegments */
366             sc->sc_memsize,             /* maxsegsize */
367             0,                          /* flags */
368             NULL, NULL,                 /* lockfunc, lockarg */
369             &lesc->sc_dmat);
370         if (error != 0) {
371                 device_printf(dev, "cannot allocate buffer DMA tag\n");
372                 goto fail_lsi;
373         }
374
375         error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem,
376             BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam);
377         if (error != 0) {
378                 device_printf(dev, "cannot allocate DMA buffer memory\n");
379                 goto fail_dtag;
380         }
381
382         lesc->sc_laddr = 0;
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");
387                 goto fail_dmem;
388         }
389
390         sc->sc_addr = lesc->sc_laddr & 0xffffff;
391         sc->sc_flags = 0;
392         sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
393
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];
399
400         OF_getetheraddr(dev, sc->sc_enaddr);
401
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;
407
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;
413
414         error = am7990_config(&lesc->sc_am7990, device_get_name(dev),
415             device_get_unit(dev));
416         if (error != 0) {
417                 device_printf(dev, "cannot attach Am7990\n");
418                 goto fail_dmap;
419         }
420
421         error = bus_setup_intr(dev, lesc->sc_ires, INTR_TYPE_NET | INTR_MPSAFE,
422             NULL, am7990_intr, sc, &lesc->sc_ih);
423         if (error != 0) {
424                 device_printf(dev, "cannot set up interrupt\n");
425                 goto fail_am7990;
426         }
427
428         return (0);
429
430  fail_am7990:
431         am7990_detach(&lesc->sc_am7990);
432  fail_dmap:
433         bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
434  fail_dmem:
435         bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
436  fail_dtag:
437         bus_dma_tag_destroy(lesc->sc_dmat);
438  fail_lsi:
439         lsi64854_detach(dma);
440  fail_ires:
441         bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(lesc->sc_ires),
442             lesc->sc_ires);
443  fail_rres:
444         bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lesc->sc_rres),
445             lesc->sc_rres);
446  fail_mtx:
447         LE_LOCK_DESTROY(sc);
448         return (error);
449 }
450
451 static int
452 le_dma_detach(device_t dev)
453 {
454         struct le_dma_softc *lesc;
455         struct lance_softc *sc;
456         int error;
457
458         lesc = device_get_softc(dev);
459         sc = &lesc->sc_am7990.lsc;
460
461         bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih);
462         am7990_detach(&lesc->sc_am7990);
463         bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
464         bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
465         bus_dma_tag_destroy(lesc->sc_dmat);
466         error = lsi64854_detach(lesc->sc_dma);
467         if (error != 0)
468                 return (error);
469         bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(lesc->sc_ires),
470             lesc->sc_ires);
471         bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lesc->sc_rres),
472             lesc->sc_rres);
473         LE_LOCK_DESTROY(sc);
474
475         return (0);
476 }
477
478 static int
479 le_dma_suspend(device_t dev)
480 {
481         struct le_dma_softc *lesc;
482
483         lesc = device_get_softc(dev);
484
485         lance_suspend(&lesc->sc_am7990.lsc);
486
487         return (0);
488 }
489
490 static int
491 le_dma_resume(device_t dev)
492 {
493         struct le_dma_softc *lesc;
494
495         lesc = device_get_softc(dev);
496
497         lance_resume(&lesc->sc_am7990.lsc);
498
499         return (0);
500 }