1 /* $NetBSD: le_elb.c,v 1.12 2022/05/29 10:45:05 rin Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juergen Hannken-Illjes.
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: le_elb.c,v 1.12 2022/05/29 10:45:05 rin Exp $");
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39
40 #include <uvm/uvm_extern.h>
41
42 #include <sys/bus.h>
43
44 #include <net/if.h>
45 #include <net/if_ether.h>
46 #include <net/if_media.h>
47
48 #include <dev/ic/lancereg.h>
49 #include <dev/ic/lancevar.h>
50 #include <dev/ic/am79900reg.h>
51 #include <dev/ic/am79900var.h>
52
53 #include <evbppc/explora/dev/elbvar.h>
54
55 #define LE_MEMSIZE 16384
56 #define LE_RDP 0x10 /* Indirect data register. */
57 #define LE_RAP 0x14 /* Indirect address register. */
58 #define LE_NPORTS 32
59
60 struct le_elb_softc {
61 struct am79900_softc sc_am79900;
62 bus_dma_tag_t sc_dmat;
63 bus_dmamap_t sc_dmam;
64 bus_space_tag_t sc_iot;
65 bus_space_handle_t sc_ioh;
66 void *sc_ih;
67 };
68
69 static int le_elb_probe(device_t, cfdata_t, void *);
70 static void le_elb_attach(device_t, device_t, void *);
71 static uint16_t le_rdcsr(struct lance_softc *, uint16_t);
72 static void le_wrcsr(struct lance_softc *, uint16_t, uint16_t);
73 static void le_copytodesc(struct lance_softc *, void *, int, int);
74 static void le_copyfromdesc(struct lance_softc *, void *, int, int);
75 static void le_copytobuf(struct lance_softc *, void *, int, int);
76 static void le_copyfrombuf(struct lance_softc *, void *, int, int);
77 static void le_zerobuf(struct lance_softc *, int, int);
78
79 CFATTACH_DECL_NEW(le_elb, sizeof(struct le_elb_softc),
80 le_elb_probe, le_elb_attach, NULL, NULL);
81
82 int
le_elb_probe(device_t parent,cfdata_t cf,void * aux)83 le_elb_probe(device_t parent, cfdata_t cf, void *aux)
84 {
85 struct elb_attach_args *oaa = aux;
86
87 if (strcmp(oaa->elb_name, cf->cf_name) != 0)
88 return 0;
89
90 return (1);
91 }
92
93 void
le_elb_attach(device_t parent,device_t self,void * aux)94 le_elb_attach(device_t parent, device_t self, void *aux)
95 {
96 struct le_elb_softc *msc = device_private(self);
97 struct lance_softc *sc = &msc->sc_am79900.lsc;
98 struct elb_attach_args *eaa = aux;
99 bus_dma_segment_t seg;
100 int i, rseg;
101
102 sc->sc_dev = self;
103 aprint_normal("\n");
104
105 if (booted_device == NULL) /*XXX*/
106 booted_device = self;
107
108 msc->sc_iot = eaa->elb_bt;
109 msc->sc_dmat = eaa->elb_dmat;
110
111 bus_space_map(msc->sc_iot, eaa->elb_base, LE_NPORTS, 0, &msc->sc_ioh);
112
113 /*
114 * Allocate a DMA area for the card.
115 */
116 if (bus_dmamem_alloc(msc->sc_dmat, LE_MEMSIZE, PAGE_SIZE, 0,
117 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
118 aprint_error_dev(self, "couldn't allocate memory for card\n");
119 goto bad_bsunmap;
120 }
121 if (bus_dmamem_map(msc->sc_dmat, &seg, rseg, LE_MEMSIZE,
122 (void **)&sc->sc_mem,
123 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
124 aprint_error_dev(self, "couldn't map memory for card\n");
125 goto bad_free;
126 }
127
128 /*
129 * Create and load the DMA map for the DMA area.
130 */
131 if (bus_dmamap_create(msc->sc_dmat, LE_MEMSIZE, 1,
132 LE_MEMSIZE, 0, BUS_DMA_NOWAIT, &msc->sc_dmam)) {
133 aprint_error_dev(self, "couldn't create DMA map\n");
134 goto bad_unmap;
135 }
136 if (bus_dmamap_load(msc->sc_dmat, msc->sc_dmam,
137 sc->sc_mem, LE_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
138 aprint_error_dev(self, "couldn't load DMA map\n");
139 goto bad_destroy;
140 }
141
142 /*
143 * This is magic -- DMA doesn't work without address
144 * bit 30 set to one.
145 */
146 sc->sc_addr = 0x40000000 | msc->sc_dmam->dm_segs[0].ds_addr;
147 sc->sc_memsize = LE_MEMSIZE;
148
149 sc->sc_copytodesc = le_copytodesc;
150 sc->sc_copyfromdesc = le_copyfromdesc;
151 sc->sc_copytobuf = le_copytobuf;
152 sc->sc_copyfrombuf = le_copyfrombuf;
153 sc->sc_zerobuf = le_zerobuf;
154
155 sc->sc_rdcsr = le_rdcsr;
156 sc->sc_wrcsr = le_wrcsr;
157
158 aprint_normal("%s", device_xname(self));
159
160 /* Save the MAC address. */
161 for (i = 0; i < 3; i++) {
162 sc->sc_enaddr[i * 2] = le_rdcsr(sc, 12 + i);
163 sc->sc_enaddr[i * 2 + 1] = le_rdcsr(sc, 12 + i) >> 8;
164 }
165
166 am79900_config(&msc->sc_am79900);
167
168 /* Chip is stopped. Set "software style" to 32-bit. */
169 le_wrcsr(sc, LE_CSR58, 2);
170
171 intr_establish_xname(eaa->elb_irq, IST_LEVEL, IPL_NET, am79900_intr,
172 sc, device_xname(self));
173
174 return;
175
176 bad_destroy:
177 bus_dmamap_destroy(msc->sc_dmat, msc->sc_dmam);
178 bad_unmap:
179 bus_dmamem_unmap(msc->sc_dmat, sc->sc_mem, LE_MEMSIZE);
180 bad_free:
181 bus_dmamem_free(msc->sc_dmat, &seg, rseg);
182 bad_bsunmap:
183 bus_space_unmap(msc->sc_iot, msc->sc_ioh, LE_NPORTS);
184 }
185
186 /*
187 * Read from an indirect CSR.
188 */
189 static uint16_t
le_rdcsr(struct lance_softc * sc,uint16_t reg)190 le_rdcsr(struct lance_softc *sc, uint16_t reg)
191 {
192 struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
193 bus_space_tag_t iot = lesc->sc_iot;
194 bus_space_handle_t ioh = lesc->sc_ioh;
195 uint16_t val;
196
197 bus_space_write_4(iot, ioh, LE_RAP, reg);
198 val = bus_space_read_4(iot, ioh, LE_RDP);
199
200 return val;
201 }
202
203 /*
204 * Write to an indirect CSR.
205 */
206 static void
le_wrcsr(struct lance_softc * sc,uint16_t reg,uint16_t val)207 le_wrcsr(struct lance_softc *sc, uint16_t reg, uint16_t val)
208 {
209 struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
210 bus_space_tag_t iot = lesc->sc_iot;
211 bus_space_handle_t ioh = lesc->sc_ioh;
212
213 bus_space_write_4(iot, ioh, LE_RAP, reg);
214 bus_space_write_4(iot, ioh, LE_RDP, val);
215 }
216
217 /*
218 * Copy data to memory and swap bytes.
219 */
220 static void
le_copytodesc(struct lance_softc * sc,void * from,int boff,int len)221 le_copytodesc(struct lance_softc *sc, void *from, int boff, int len)
222 {
223 struct le_elb_softc *msc = (struct le_elb_softc *)sc;
224 volatile uint32_t *src = from;
225 volatile uint32_t *dst = (uint32_t *)((uint8_t *)sc->sc_mem + boff);
226 int todo = len;
227
228 /* XXX lance_setladrf should be modified to use u_int32_t instead.
229 * The init block contains u_int16_t values that require
230 * special swapping.
231 */
232 if (boff == LE_INITADDR(sc) && len == sizeof(struct leinit)) {
233 src[3] = (src[3] >> 16) | (src[3] << 16);
234 src[4] = (src[4] >> 16) | (src[4] << 16);
235 }
236
237 todo /= sizeof(uint32_t);
238 while (todo-- > 0)
239 *dst++ = bswap32(*src++);
240
241 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
242 BUS_DMASYNC_PREWRITE);
243 }
244
245 /*
246 * Copy data from memory and swap bytes.
247 */
248 static void
le_copyfromdesc(struct lance_softc * sc,void * to,int boff,int len)249 le_copyfromdesc(struct lance_softc *sc, void *to, int boff, int len)
250 {
251 struct le_elb_softc *msc = (struct le_elb_softc *)sc;
252 volatile uint32_t *src = (uint32_t *)((uint8_t *)sc->sc_mem + boff);
253 volatile uint32_t *dst = to;
254
255 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
256 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
257
258 len /= sizeof(uint32_t);
259 while (len-- > 0)
260 *dst++ = bswap32(*src++);
261 }
262
263 /*
264 * Copy data to memory.
265 */
266 static void
le_copytobuf(struct lance_softc * sc,void * from,int boff,int len)267 le_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
268 {
269 struct le_elb_softc *msc = (struct le_elb_softc *)sc;
270 volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
271
272 memcpy(__UNVOLATILE(buf), from, len);
273
274 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
275 BUS_DMASYNC_PREWRITE);
276 }
277
278 /*
279 * Copy data from memory.
280 */
281 static void
le_copyfrombuf(struct lance_softc * sc,void * to,int boff,int len)282 le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
283 {
284 struct le_elb_softc *msc = (struct le_elb_softc *)sc;
285 volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
286
287 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
288 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
289
290 memcpy(to, __UNVOLATILE(buf), len);
291 }
292
293 /*
294 * Zero memory.
295 */
296 static void
le_zerobuf(struct lance_softc * sc,int boff,int len)297 le_zerobuf(struct lance_softc *sc, int boff, int len)
298 {
299 struct le_elb_softc *msc = (struct le_elb_softc *)sc;
300 volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
301
302 memset(__UNVOLATILE(buf), 0, len);
303
304 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
305 BUS_DMASYNC_PREWRITE);
306 }
307