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