]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/le/if_le_ledma.c
There is no Python in the FreeBSD base system
[FreeBSD/FreeBSD.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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/resource.h>
45 #include <sys/rman.h>
46 #include <sys/socket.h>
47
48 #include <dev/ofw/ofw_bus.h>
49
50 #include <machine/bus.h>
51 #include <machine/ofw_machdep.h>
52 #include <machine/resource.h>
53
54 #include <net/ethernet.h>
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_media.h>
58
59 #include <sparc64/sbus/lsi64854reg.h>
60 #include <sparc64/sbus/lsi64854var.h>
61
62 #include <dev/le/lancereg.h>
63 #include <dev/le/lancevar.h>
64 #include <dev/le/am7990var.h>
65
66 #define LEDMA_ALIGNMENT 8               /* ring desc. alignmet for NCR92C990 */
67 #define LEDMA_BOUNDARY  (16*1024*1024)  /* must not cross 16MB boundary */
68 #define LEDMA_MEMSIZE   (16*1024)       /* LANCE memory size */
69 #define LEREG1_RDP      0               /* Register Data Port */
70 #define LEREG1_RAP      2               /* Register Address Port */
71
72 struct le_dma_softc {
73         struct am7990_softc     sc_am7990;      /* glue to MI code */
74
75         struct resource         *sc_rres;
76
77         struct resource         *sc_ires;
78         void                    *sc_ih;
79
80         bus_dma_tag_t           sc_dmat;
81         bus_dmamap_t            sc_dmam;
82         bus_addr_t              sc_laddr;       /* LANCE DMA address */
83
84         struct lsi64854_softc   *sc_dma;        /* pointer to DMA engine */
85 };
86
87 static device_probe_t le_dma_probe;
88 static device_attach_t le_dma_attach;
89 static device_detach_t le_dma_detach;
90 static device_resume_t le_dma_resume;
91 static device_suspend_t le_dma_suspend;
92
93 static device_method_t le_dma_methods[] = {
94         /* Device interface */
95         DEVMETHOD(device_probe,         le_dma_probe),
96         DEVMETHOD(device_attach,        le_dma_attach),
97         DEVMETHOD(device_detach,        le_dma_detach),
98         /* We can just use the suspend method here. */
99         DEVMETHOD(device_shutdown,      le_dma_suspend),
100         DEVMETHOD(device_suspend,       le_dma_suspend),
101         DEVMETHOD(device_resume,        le_dma_resume),
102
103         { 0, 0 }
104 };
105
106 DEFINE_CLASS_0(le, le_dma_driver, le_dma_methods, sizeof(struct le_dma_softc));
107 DRIVER_MODULE(le, dma, le_dma_driver, le_devclass, 0, 0);
108 MODULE_DEPEND(le, dma, 1, 1, 1);
109 MODULE_DEPEND(le, ether, 1, 1, 1);
110
111 /*
112  * Media types supported
113  */
114 static const int le_dma_supmedia[] = {
115         IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
116         IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
117         IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0)
118 };
119
120 static void le_dma_wrcsr(struct lance_softc *, uint16_t, uint16_t);
121 static uint16_t le_dma_rdcsr(struct lance_softc *, uint16_t);
122 static void le_dma_setutp(struct lance_softc *);
123 static void le_dma_setaui(struct lance_softc *);
124 static int le_dma_supmediachange(struct lance_softc *);
125 static void le_dma_supmediastatus(struct lance_softc *, struct ifmediareq *);
126 static void le_dma_hwreset(struct lance_softc *);
127 static int le_dma_hwintr(struct lance_softc *);
128 static void le_dma_nocarrier(struct lance_softc *);
129 static bus_dmamap_callback_t le_dma_dma_callback;
130
131 static void
132 le_dma_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
133 {
134         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
135
136         bus_write_2(lesc->sc_rres, LEREG1_RAP, port);
137         bus_barrier(lesc->sc_rres, LEREG1_RAP, 2, BUS_SPACE_BARRIER_WRITE);
138         bus_write_2(lesc->sc_rres, LEREG1_RDP, val);
139 }
140
141 static uint16_t
142 le_dma_rdcsr(struct lance_softc *sc, uint16_t port)
143 {
144         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
145
146         bus_write_2(lesc->sc_rres, LEREG1_RAP, port);
147         bus_barrier(lesc->sc_rres, LEREG1_RAP, 2, BUS_SPACE_BARRIER_WRITE);
148         return (bus_read_2(lesc->sc_rres, LEREG1_RDP));
149 }
150
151 static void
152 le_dma_setutp(struct lance_softc *sc)
153 {
154         struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
155
156         L64854_SCSR(dma, L64854_GCSR(dma) | E_TP_AUI);
157         DELAY(20000);   /* We must not touch the LANCE chip for 20ms. */
158 }
159
160 static void
161 le_dma_setaui(struct lance_softc *sc)
162 {
163         struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
164
165         L64854_SCSR(dma, L64854_GCSR(dma) & ~E_TP_AUI);
166         DELAY(20000);   /* We must not touch the LANCE chip for 20ms. */
167 }
168
169 static int
170 le_dma_supmediachange(struct lance_softc *sc)
171 {
172         struct ifmedia *ifm = &sc->sc_media;
173
174         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
175                 return (EINVAL);
176
177         /*
178          * Switch to the selected media. If autoselect is set, we don't
179          * really have to do anything. We'll switch to the other media
180          * when we detect loss of carrier.
181          */
182         switch (IFM_SUBTYPE(ifm->ifm_media)) {
183         case IFM_10_T:
184                 le_dma_setutp(sc);
185                 break;
186
187         case IFM_10_5:
188                 le_dma_setaui(sc);
189                 break;
190
191         case IFM_AUTO:
192                 break;
193
194         default:
195                 return (EINVAL);
196         }
197
198         return (0);
199 }
200
201 static void
202 le_dma_supmediastatus(struct lance_softc *sc, struct ifmediareq *ifmr)
203 {
204         struct lsi64854_softc *dma = ((struct le_dma_softc *)sc)->sc_dma;
205
206         /*
207          * Notify the world which media we're currently using.
208          */
209         if (L64854_GCSR(dma) & E_TP_AUI)
210                 ifmr->ifm_active = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
211         else
212                 ifmr->ifm_active = IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0);
213 }
214
215 static void
216 le_dma_hwreset(struct lance_softc *sc)
217 {
218         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
219         struct lsi64854_softc *dma = lesc->sc_dma;
220         uint32_t aui_bit, csr;
221
222         /*
223          * Reset DMA channel.
224          */
225         csr = L64854_GCSR(dma);
226         aui_bit = csr & E_TP_AUI;
227         DMA_RESET(dma);
228
229         /* Write bits 24-31 of Lance address. */
230         bus_write_4(dma->sc_res, L64854_REG_ENBAR,
231             lesc->sc_laddr & 0xff000000);
232
233         DMA_ENINTR(dma);
234
235         /*
236          * Disable E-cache invalidates on chip writes.
237          * Retain previous cable selection bit.
238          */
239         csr = L64854_GCSR(dma);
240         csr |= (E_DSBL_WR_INVAL | aui_bit);
241         L64854_SCSR(dma, csr);
242         DELAY(20000);   /* We must not touch the LANCE chip for 20ms. */
243 }
244
245 static int
246 le_dma_hwintr(struct lance_softc *sc)
247 {
248         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
249         struct lsi64854_softc *dma = lesc->sc_dma;
250
251         return (DMA_INTR(dma));
252 }
253
254 static void
255 le_dma_nocarrier(struct lance_softc *sc)
256 {
257         struct le_dma_softc *lesc = (struct le_dma_softc *)sc;
258
259         /*
260          * Check if the user has requested a certain cable type, and
261          * if so, honor that request.
262          */
263
264         if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
265                 switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
266                 case IFM_10_5:
267                 case IFM_AUTO:
268                         if_printf(sc->sc_ifp, "lost carrier on UTP port, "
269                             "switching to AUI port\n");
270                         le_dma_setaui(sc);
271                 }
272         } else {
273                 switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
274                 case IFM_10_T:
275                 case IFM_AUTO:
276                         if_printf(sc->sc_ifp, "lost carrier on AUI port, "
277                             "switching to UTP port\n");
278                         le_dma_setutp(sc);
279                 }
280         }
281 }
282
283 static void
284 le_dma_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
285 {
286         struct le_dma_softc *lesc = (struct le_dma_softc *)xsc;
287
288         if (error != 0)
289                 return;
290         KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__));
291         lesc->sc_laddr = segs[0].ds_addr;
292 }
293
294 static int
295 le_dma_probe(device_t dev)
296 {
297
298         if (strcmp(ofw_bus_get_name(dev), "le") == 0) {
299                 device_set_desc(dev, "LANCE Ethernet");
300                 return (BUS_PROBE_DEFAULT);
301         }
302         return (ENXIO);
303 }
304
305 static int
306 le_dma_attach(device_t dev)
307 {
308         struct le_dma_softc *lesc;
309         struct lsi64854_softc *dma;
310         struct lance_softc *sc;
311         int error, i;
312
313         lesc = device_get_softc(dev);
314         sc = &lesc->sc_am7990.lsc;
315
316         LE_LOCK_INIT(sc, device_get_nameunit(dev));
317
318         /*
319          * Establish link to `ledma' device.
320          * XXX hackery.
321          */
322         dma = (struct lsi64854_softc *)device_get_softc(device_get_parent(dev));
323         lesc->sc_dma = dma;
324         lesc->sc_dma->sc_client = lesc;
325
326         i = 0;
327         lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
328             &i, RF_ACTIVE);
329         if (lesc->sc_rres == NULL) {
330                 device_printf(dev, "cannot allocate registers\n");
331                 error = ENXIO;
332                 goto fail_mtx;
333         }
334
335         i = 0;
336         if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
337             &i, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
338                 device_printf(dev, "cannot allocate interrupt\n");
339                 error = ENXIO;
340                 goto fail_rres;
341         }
342
343         /* Attach the DMA engine. */
344         error = lsi64854_attach(dma);
345         if (error != 0) {
346                 device_printf(dev, "lsi64854_attach failed\n");
347                 goto fail_ires;
348         }
349
350         sc->sc_memsize = LEDMA_MEMSIZE;
351         error = bus_dma_tag_create(
352             dma->sc_parent_dmat,        /* parent */
353             LEDMA_ALIGNMENT,            /* alignment */
354             LEDMA_BOUNDARY,             /* boundary */
355             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
356             BUS_SPACE_MAXADDR,          /* highaddr */
357             NULL, NULL,                 /* filter, filterarg */
358             sc->sc_memsize,             /* maxsize */
359             1,                          /* nsegments */
360             sc->sc_memsize,             /* maxsegsize */
361             0,                          /* flags */
362             NULL, NULL,                 /* lockfunc, lockarg */
363             &lesc->sc_dmat);
364         if (error != 0) {
365                 device_printf(dev, "cannot allocate buffer DMA tag\n");
366                 goto fail_lsi;
367         }
368
369         error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem,
370             BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam);
371         if (error != 0) {
372                 device_printf(dev, "cannot allocate DMA buffer memory\n");
373                 goto fail_dtag;
374         }
375
376         lesc->sc_laddr = 0;
377         error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem,
378             sc->sc_memsize, le_dma_dma_callback, lesc, 0);
379         if (error != 0 || lesc->sc_laddr == 0) {
380                 device_printf(dev, "cannot load DMA buffer map\n");
381                 goto fail_dmem;
382         }
383
384         sc->sc_addr = lesc->sc_laddr & 0xffffff;
385         sc->sc_flags = 0;
386         sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
387
388         sc->sc_mediachange = le_dma_supmediachange;
389         sc->sc_mediastatus = le_dma_supmediastatus;
390         sc->sc_supmedia = le_dma_supmedia;
391         sc->sc_nsupmedia = sizeof(le_dma_supmedia) / sizeof(le_dma_supmedia[0]);
392         sc->sc_defaultmedia = le_dma_supmedia[0];
393
394         OF_getetheraddr(dev, sc->sc_enaddr);
395
396         sc->sc_copytodesc = lance_copytobuf_contig;
397         sc->sc_copyfromdesc = lance_copyfrombuf_contig;
398         sc->sc_copytobuf = lance_copytobuf_contig;
399         sc->sc_copyfrombuf = lance_copyfrombuf_contig;
400         sc->sc_zerobuf = lance_zerobuf_contig;
401
402         sc->sc_rdcsr = le_dma_rdcsr;
403         sc->sc_wrcsr = le_dma_wrcsr;
404         sc->sc_hwreset = le_dma_hwreset;
405         sc->sc_hwintr = le_dma_hwintr;
406         sc->sc_nocarrier = le_dma_nocarrier;
407
408         error = am7990_config(&lesc->sc_am7990, device_get_name(dev),
409             device_get_unit(dev));
410         if (error != 0) {
411                 device_printf(dev, "cannot attach Am7990\n");
412                 goto fail_dmap;
413         }
414
415         error = bus_setup_intr(dev, lesc->sc_ires, INTR_TYPE_NET | INTR_MPSAFE,
416             NULL, am7990_intr, sc, &lesc->sc_ih);
417         if (error != 0) {
418                 device_printf(dev, "cannot set up interrupt\n");
419                 goto fail_am7990;
420         }
421
422         return (0);
423
424  fail_am7990:
425         am7990_detach(&lesc->sc_am7990);
426  fail_dmap:
427         bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
428  fail_dmem:
429         bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
430  fail_dtag:
431         bus_dma_tag_destroy(lesc->sc_dmat);
432  fail_lsi:
433         lsi64854_detach(dma);
434  fail_ires:
435         bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(lesc->sc_ires),
436             lesc->sc_ires);
437  fail_rres:
438         bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lesc->sc_rres),
439             lesc->sc_rres);
440  fail_mtx:
441         LE_LOCK_DESTROY(sc);
442         return (error);
443 }
444
445 static int
446 le_dma_detach(device_t dev)
447 {
448         struct le_dma_softc *lesc;
449         struct lance_softc *sc;
450         int error;
451
452         lesc = device_get_softc(dev);
453         sc = &lesc->sc_am7990.lsc;
454
455         bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih);
456         am7990_detach(&lesc->sc_am7990);
457         bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
458         bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
459         bus_dma_tag_destroy(lesc->sc_dmat);
460         error = lsi64854_detach(lesc->sc_dma);
461         if (error != 0)
462                 return (error);
463         bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(lesc->sc_ires),
464             lesc->sc_ires);
465         bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lesc->sc_rres),
466             lesc->sc_rres);
467         LE_LOCK_DESTROY(sc);
468
469         return (0);
470 }
471
472 static int
473 le_dma_suspend(device_t dev)
474 {
475         struct le_dma_softc *lesc;
476
477         lesc = device_get_softc(dev);
478
479         lance_suspend(&lesc->sc_am7990.lsc);
480
481         return (0);
482 }
483
484 static int
485 le_dma_resume(device_t dev)
486 {
487         struct le_dma_softc *lesc;
488
489         lesc = device_get_softc(dev);
490
491         lance_resume(&lesc->sc_am7990.lsc);
492
493         return (0);
494 }