1 /*        $NetBSD: if_le_sbdio.c,v 1.8 2015/07/11 10:32:45 kamil Exp $          */
2 
3 /*-
4  * Copyright (c) 1996, 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass and Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_le_sbdio.c,v 1.8 2015/07/11 10:32:45 kamil Exp $");
34 
35 #include "opt_inet.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/syslog.h>
41 #include <sys/socket.h>
42 #include <sys/device.h>
43 
44 #include <net/if.h>
45 #include <net/if_ether.h>
46 #include <net/if_media.h>
47 
48 #ifdef INET
49 #include <netinet/in.h>
50 #include <netinet/if_inarp.h>
51 #endif
52 
53 #include <machine/bus.h>
54 #include <machine/intr.h>
55 #include <machine/sbdiovar.h>
56 #include <machine/sbdvar.h>   /* for ether_addr() */
57 
58 #include <dev/ic/lancereg.h>
59 #include <dev/ic/lancevar.h>
60 #include <dev/ic/am7990reg.h>
61 #include <dev/ic/am7990var.h>
62 
63 #define   LEREG1_RDP          0         /* offset to lance data register */
64 #define   LEREG1_RAP          6         /* offset to lance address register */
65 #define   LE_MEMSIZE          (64 * 1024)
66 
67 struct le_sbdio_softc {
68           struct am7990_softc sc_am7990;
69 
70           bus_space_tag_t sc_bst;
71           bus_space_handle_t sc_bsh;
72           bus_dma_tag_t sc_dmat;
73           bus_dmamap_t sc_dmamap;
74 };
75 
76 int le_sbdio_match(device_t, cfdata_t, void *);
77 void le_sbdio_attach(device_t, device_t, void *);
78 static void le_sbdio_wrcsr(struct lance_softc *, uint16_t, uint16_t);
79 static uint16_t le_sbdio_rdcsr(struct lance_softc *, uint16_t);
80 
81 CFATTACH_DECL_NEW(le_sbdio, sizeof(struct le_sbdio_softc),
82     le_sbdio_match, le_sbdio_attach, NULL, NULL);
83 
84 int
le_sbdio_match(device_t parent,cfdata_t cf,void * aux)85 le_sbdio_match(device_t parent, cfdata_t cf, void *aux)
86 {
87           struct sbdio_attach_args *sa = aux;
88 
89           return strcmp(sa->sa_name, "lance") ? 0 : 1;
90 }
91 
92 void
le_sbdio_attach(device_t parent,device_t self,void * aux)93 le_sbdio_attach(device_t parent, device_t self, void *aux)
94 {
95           struct le_sbdio_softc *lesc = device_private(self);
96           struct sbdio_attach_args *sa = aux;
97           struct lance_softc *sc = &lesc->sc_am7990.lsc;
98           bus_dma_segment_t seg;
99           int rseg;
100 
101           sc->sc_dev = self;
102           lesc->sc_dmat = sa->sa_dmat;
103           lesc->sc_bst  = sa->sa_bust;
104 
105           if (bus_space_map(lesc->sc_bst, sa->sa_addr1, 8 /* XXX */,
106               BUS_SPACE_MAP_LINEAR, &lesc->sc_bsh) != 0) {
107                     aprint_error(": cannot map registers\n");
108                     return;
109           }
110 
111           /* Allocate DMA memory for the chip. */
112           if (bus_dmamem_alloc(lesc->sc_dmat, LE_MEMSIZE, 0, 0, &seg, 1, &rseg,
113               BUS_DMA_NOWAIT) != 0) {
114                     aprint_error(": can't allocate DMA memory\n");
115                     return;
116           }
117           if (bus_dmamem_map(lesc->sc_dmat, &seg, rseg, LE_MEMSIZE,
118               (void **)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0) {
119                     aprint_error(": can't map DMA memory\n");
120                     return;
121           }
122           if (bus_dmamap_create(lesc->sc_dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
123               0, BUS_DMA_NOWAIT, &lesc->sc_dmamap) != 0) {
124                     aprint_error(": can't create DMA map\n");
125                     return;
126           }
127           if (bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmamap, sc->sc_mem,
128               LE_MEMSIZE, NULL, BUS_DMA_NOWAIT) != 0) {
129                     aprint_error(": can't load DMA map\n");
130                     return;
131           }
132 
133           sc->sc_memsize = LE_MEMSIZE;
134           sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
135           sc->sc_conf3 = LE_C3_BSWP | LE_C3_BCON;
136           (*platform.ether_addr)(sc->sc_enaddr);
137 
138           sc->sc_copytodesc = lance_copytobuf_contig;
139           sc->sc_copyfromdesc = lance_copyfrombuf_contig;
140           sc->sc_copytobuf = lance_copytobuf_contig;
141           sc->sc_copyfrombuf = lance_copyfrombuf_contig;
142           sc->sc_zerobuf = lance_zerobuf_contig;
143 #ifdef LEDEBUG
144           sc->sc_debug = 0xff;
145 #endif
146           sc->sc_rdcsr = le_sbdio_rdcsr;
147           sc->sc_wrcsr = le_sbdio_wrcsr;
148 
149           am7990_config(&lesc->sc_am7990);
150           intr_establish(sa->sa_irq, am7990_intr, sc);
151 }
152 
153 static void
le_sbdio_wrcsr(struct lance_softc * sc,uint16_t port,uint16_t val)154 le_sbdio_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
155 {
156           struct le_sbdio_softc *lesc = (struct le_sbdio_softc *)sc;
157 
158           bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
159           bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP, val);
160 }
161 
162 static uint16_t
le_sbdio_rdcsr(struct lance_softc * sc,uint16_t port)163 le_sbdio_rdcsr(struct lance_softc *sc, uint16_t port)
164 {
165           struct le_sbdio_softc *lesc = (struct le_sbdio_softc *)sc;
166 
167           bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
168           return bus_space_read_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP);
169 }
170