if_le_ledma.c revision 1.39 1 1.39 thorpej /* $NetBSD: if_le_ledma.c,v 1.39 2025/10/15 01:36:25 thorpej Exp $ */
2 1.1 pk
3 1.1 pk /*-
4 1.1 pk * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.1 pk * All rights reserved.
6 1.1 pk *
7 1.1 pk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pk * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9 1.1 pk * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
10 1.1 pk *
11 1.1 pk * Redistribution and use in source and binary forms, with or without
12 1.1 pk * modification, are permitted provided that the following conditions
13 1.1 pk * are met:
14 1.1 pk * 1. Redistributions of source code must retain the above copyright
15 1.1 pk * notice, this list of conditions and the following disclaimer.
16 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 pk * notice, this list of conditions and the following disclaimer in the
18 1.1 pk * documentation and/or other materials provided with the distribution.
19 1.1 pk *
20 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 pk * POSSIBILITY OF SUCH DAMAGE.
31 1.1 pk */
32 1.15 lukem
33 1.15 lukem #include <sys/cdefs.h>
34 1.39 thorpej __KERNEL_RCSID(0, "$NetBSD: if_le_ledma.c,v 1.39 2025/10/15 01:36:25 thorpej Exp $");
35 1.1 pk
36 1.1 pk #include "opt_inet.h"
37 1.1 pk
38 1.1 pk #include <sys/param.h>
39 1.1 pk #include <sys/systm.h>
40 1.1 pk #include <sys/mbuf.h>
41 1.1 pk #include <sys/syslog.h>
42 1.1 pk #include <sys/socket.h>
43 1.1 pk #include <sys/device.h>
44 1.1 pk
45 1.1 pk #include <net/if.h>
46 1.1 pk #include <net/if_ether.h>
47 1.1 pk #include <net/if_media.h>
48 1.1 pk
49 1.1 pk #ifdef INET
50 1.1 pk #include <netinet/in.h>
51 1.1 pk #include <netinet/if_inarp.h>
52 1.1 pk #endif
53 1.1 pk
54 1.28 ad #include <sys/bus.h>
55 1.28 ad #include <sys/intr.h>
56 1.24 pk #include <machine/autoconf.h>
57 1.1 pk
58 1.1 pk #include <dev/sbus/sbusvar.h>
59 1.1 pk
60 1.1 pk #include <dev/ic/lsi64854reg.h>
61 1.1 pk #include <dev/ic/lsi64854var.h>
62 1.1 pk
63 1.1 pk #include <dev/ic/lancereg.h>
64 1.1 pk #include <dev/ic/lancevar.h>
65 1.1 pk #include <dev/ic/am7990reg.h>
66 1.1 pk #include <dev/ic/am7990var.h>
67 1.1 pk
68 1.29 tsutsui #include "ioconf.h"
69 1.29 tsutsui
70 1.1 pk /*
71 1.1 pk * LANCE registers.
72 1.1 pk */
73 1.1 pk #define LEREG1_RDP 0 /* Register Data port */
74 1.1 pk #define LEREG1_RAP 2 /* Register Address port */
75 1.1 pk
76 1.1 pk struct le_softc {
77 1.1 pk struct am7990_softc sc_am7990; /* glue to MI code */
78 1.1 pk bus_space_tag_t sc_bustag;
79 1.8 pk bus_dmamap_t sc_dmamap;
80 1.1 pk bus_space_handle_t sc_reg; /* LANCE registers */
81 1.1 pk struct lsi64854_softc *sc_dma; /* pointer to my dma */
82 1.4 eeh u_int sc_laddr; /* LANCE DMA address */
83 1.31 tsutsui u_int sc_lostcount;
84 1.31 tsutsui #define LE_LOSTTHRESH 5 /* lost carrior count to switch media */
85 1.1 pk };
86 1.1 pk
87 1.1 pk #define MEMSIZE (16*1024) /* LANCE memory size */
88 1.1 pk #define LEDMA_BOUNDARY (16*1024*1024) /* must not cross 16MB boundary */
89 1.1 pk
90 1.29 tsutsui int lematch_ledma(device_t, cfdata_t, void *);
91 1.29 tsutsui void leattach_ledma(device_t, device_t, void *);
92 1.1 pk
93 1.1 pk /*
94 1.1 pk * Media types supported by the Sun4m.
95 1.1 pk */
96 1.1 pk static int lemedia[] = {
97 1.36 msaitoh IFM_ETHER | IFM_10_T,
98 1.36 msaitoh IFM_ETHER | IFM_10_5,
99 1.36 msaitoh IFM_ETHER | IFM_AUTO,
100 1.1 pk };
101 1.29 tsutsui #define NLEMEDIA __arraycount(lemedia)
102 1.1 pk
103 1.25 perry void lesetutp(struct lance_softc *);
104 1.25 perry void lesetaui(struct lance_softc *);
105 1.1 pk
106 1.25 perry int lemediachange(struct lance_softc *);
107 1.25 perry void lemediastatus(struct lance_softc *, struct ifmediareq *);
108 1.1 pk
109 1.29 tsutsui CFATTACH_DECL_NEW(le_ledma, sizeof(struct le_softc),
110 1.21 thorpej lematch_ledma, leattach_ledma, NULL, NULL);
111 1.1 pk
112 1.14 mrg #if defined(_KERNEL_OPT)
113 1.1 pk #include "opt_ddb.h"
114 1.1 pk #endif
115 1.1 pk
116 1.29 tsutsui static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
117 1.29 tsutsui static uint16_t lerdcsr(struct lance_softc *, uint16_t);
118 1.25 perry static void lehwreset(struct lance_softc *);
119 1.25 perry static void lehwinit(struct lance_softc *);
120 1.25 perry static void lenocarrier(struct lance_softc *);
121 1.1 pk
122 1.1 pk static void
123 1.29 tsutsui lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
124 1.1 pk {
125 1.1 pk struct le_softc *lesc = (struct le_softc *)sc;
126 1.23 pk bus_space_tag_t t = lesc->sc_bustag;
127 1.23 pk bus_space_handle_t h = lesc->sc_reg;
128 1.1 pk
129 1.23 pk bus_space_write_2(t, h, LEREG1_RAP, port);
130 1.23 pk bus_space_write_2(t, h, LEREG1_RDP, val);
131 1.1 pk
132 1.1 pk #if defined(SUN4M)
133 1.1 pk /*
134 1.1 pk * We need to flush the Sbus->Mbus write buffers. This can most
135 1.1 pk * easily be accomplished by reading back the register that we
136 1.1 pk * just wrote (thanks to Chris Torek for this solution).
137 1.1 pk */
138 1.23 pk (void)bus_space_read_2(t, h, LEREG1_RDP);
139 1.1 pk #endif
140 1.1 pk }
141 1.1 pk
142 1.29 tsutsui static uint16_t
143 1.29 tsutsui lerdcsr(struct lance_softc *sc, uint16_t port)
144 1.1 pk {
145 1.1 pk struct le_softc *lesc = (struct le_softc *)sc;
146 1.23 pk bus_space_tag_t t = lesc->sc_bustag;
147 1.23 pk bus_space_handle_t h = lesc->sc_reg;
148 1.1 pk
149 1.23 pk bus_space_write_2(t, h, LEREG1_RAP, port);
150 1.36 msaitoh return bus_space_read_2(t, h, LEREG1_RDP);
151 1.1 pk }
152 1.1 pk
153 1.1 pk void
154 1.29 tsutsui lesetutp(struct lance_softc *sc)
155 1.1 pk {
156 1.3 pk struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
157 1.29 tsutsui uint32_t csr;
158 1.1 pk
159 1.3 pk csr = L64854_GCSR(dma);
160 1.1 pk csr |= E_TP_AUI;
161 1.3 pk L64854_SCSR(dma, csr);
162 1.1 pk delay(20000); /* must not touch le for 20ms */
163 1.1 pk }
164 1.1 pk
165 1.1 pk void
166 1.29 tsutsui lesetaui(struct lance_softc *sc)
167 1.1 pk {
168 1.3 pk struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
169 1.29 tsutsui uint32_t csr;
170 1.1 pk
171 1.3 pk csr = L64854_GCSR(dma);
172 1.1 pk csr &= ~E_TP_AUI;
173 1.3 pk L64854_SCSR(dma, csr);
174 1.1 pk delay(20000); /* must not touch le for 20ms */
175 1.1 pk }
176 1.1 pk
177 1.1 pk int
178 1.29 tsutsui lemediachange(struct lance_softc *sc)
179 1.1 pk {
180 1.1 pk struct ifmedia *ifm = &sc->sc_media;
181 1.1 pk
182 1.1 pk if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
183 1.36 msaitoh return EINVAL;
184 1.1 pk
185 1.1 pk /*
186 1.1 pk * Switch to the selected media. If autoselect is
187 1.1 pk * set, we don't really have to do anything. We'll
188 1.1 pk * switch to the other media when we detect loss of
189 1.1 pk * carrier.
190 1.1 pk */
191 1.1 pk switch (IFM_SUBTYPE(ifm->ifm_media)) {
192 1.1 pk case IFM_10_T:
193 1.1 pk lesetutp(sc);
194 1.1 pk break;
195 1.1 pk
196 1.1 pk case IFM_10_5:
197 1.1 pk lesetaui(sc);
198 1.1 pk break;
199 1.1 pk
200 1.1 pk case IFM_AUTO:
201 1.1 pk break;
202 1.1 pk
203 1.1 pk default:
204 1.36 msaitoh return EINVAL;
205 1.1 pk }
206 1.1 pk
207 1.36 msaitoh return 0;
208 1.1 pk }
209 1.1 pk
210 1.1 pk void
211 1.29 tsutsui lemediastatus(struct lance_softc *sc, struct ifmediareq *ifmr)
212 1.1 pk {
213 1.3 pk struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
214 1.1 pk
215 1.1 pk /*
216 1.1 pk * Notify the world which media we're currently using.
217 1.1 pk */
218 1.3 pk if (L64854_GCSR(dma) & E_TP_AUI)
219 1.36 msaitoh ifmr->ifm_active = IFM_ETHER | IFM_10_T;
220 1.1 pk else
221 1.36 msaitoh ifmr->ifm_active = IFM_ETHER | IFM_10_5;
222 1.1 pk }
223 1.1 pk
224 1.23 pk static void
225 1.29 tsutsui lehwreset(struct lance_softc *sc)
226 1.1 pk {
227 1.1 pk struct le_softc *lesc = (struct le_softc *)sc;
228 1.1 pk struct lsi64854_softc *dma = lesc->sc_dma;
229 1.29 tsutsui uint32_t csr;
230 1.4 eeh u_int aui_bit;
231 1.1 pk
232 1.1 pk /*
233 1.1 pk * Reset DMA channel.
234 1.1 pk */
235 1.1 pk csr = L64854_GCSR(dma);
236 1.1 pk aui_bit = csr & E_TP_AUI;
237 1.1 pk DMA_RESET(dma);
238 1.1 pk
239 1.1 pk /* Write bits 24-31 of Lance address */
240 1.1 pk bus_space_write_4(dma->sc_bustag, dma->sc_regs, L64854_REG_ENBAR,
241 1.1 pk lesc->sc_laddr & 0xff000000);
242 1.1 pk
243 1.1 pk DMA_ENINTR(dma);
244 1.1 pk
245 1.1 pk /*
246 1.1 pk * Disable E-cache invalidates on chip writes.
247 1.1 pk * Retain previous cable selection bit.
248 1.1 pk */
249 1.1 pk csr = L64854_GCSR(dma);
250 1.1 pk csr |= (E_DSBL_WR_INVAL | aui_bit);
251 1.1 pk L64854_SCSR(dma, csr);
252 1.5 pk delay(20000); /* must not touch le for 20ms */
253 1.1 pk }
254 1.1 pk
255 1.23 pk static void
256 1.29 tsutsui lehwinit(struct lance_softc *sc)
257 1.1 pk {
258 1.1 pk
259 1.1 pk /*
260 1.1 pk * Make sure we're using the currently-enabled media type.
261 1.1 pk * XXX Actually, this is probably unnecessary, now.
262 1.1 pk */
263 1.1 pk switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
264 1.1 pk case IFM_10_T:
265 1.1 pk lesetutp(sc);
266 1.1 pk break;
267 1.1 pk
268 1.1 pk case IFM_10_5:
269 1.1 pk lesetaui(sc);
270 1.1 pk break;
271 1.1 pk }
272 1.1 pk }
273 1.1 pk
274 1.23 pk static void
275 1.29 tsutsui lenocarrier(struct lance_softc *sc)
276 1.1 pk {
277 1.1 pk struct le_softc *lesc = (struct le_softc *)sc;
278 1.1 pk
279 1.31 tsutsui /* it may take a while for modern switches to set 10baseT media */
280 1.31 tsutsui if (lesc->sc_lostcount++ < LE_LOSTTHRESH)
281 1.31 tsutsui return;
282 1.31 tsutsui
283 1.31 tsutsui lesc->sc_lostcount = 0;
284 1.31 tsutsui
285 1.1 pk /*
286 1.1 pk * Check if the user has requested a certain cable type, and
287 1.1 pk * if so, honor that request.
288 1.1 pk */
289 1.29 tsutsui printf("%s: lost carrier on ", device_xname(sc->sc_dev));
290 1.1 pk
291 1.1 pk if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
292 1.1 pk printf("UTP port");
293 1.1 pk switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
294 1.1 pk case IFM_10_5:
295 1.1 pk case IFM_AUTO:
296 1.1 pk printf(", switching to AUI port");
297 1.1 pk lesetaui(sc);
298 1.1 pk }
299 1.1 pk } else {
300 1.1 pk printf("AUI port");
301 1.1 pk switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
302 1.1 pk case IFM_10_T:
303 1.1 pk case IFM_AUTO:
304 1.1 pk printf(", switching to UTP port");
305 1.1 pk lesetutp(sc);
306 1.1 pk }
307 1.1 pk }
308 1.1 pk printf("\n");
309 1.1 pk }
310 1.1 pk
311 1.1 pk int
312 1.29 tsutsui lematch_ledma(device_t parent, cfdata_t cf, void *aux)
313 1.1 pk {
314 1.1 pk struct sbus_attach_args *sa = aux;
315 1.1 pk
316 1.18 thorpej return (strcmp(cf->cf_name, sa->sa_name) == 0);
317 1.1 pk }
318 1.1 pk
319 1.1 pk
320 1.1 pk void
321 1.29 tsutsui leattach_ledma(device_t parent, device_t self, void *aux)
322 1.1 pk {
323 1.29 tsutsui struct le_softc *lesc = device_private(self);
324 1.29 tsutsui struct lance_softc *sc = &lesc->sc_am7990.lsc;
325 1.29 tsutsui struct lsi64854_softc *lsi = device_private(parent);
326 1.1 pk struct sbus_attach_args *sa = aux;
327 1.8 pk bus_dma_tag_t dmatag = sa->sa_dmatag;
328 1.1 pk bus_dma_segment_t seg;
329 1.1 pk int rseg, error;
330 1.1 pk
331 1.29 tsutsui sc->sc_dev = self;
332 1.1 pk lesc->sc_bustag = sa->sa_bustag;
333 1.1 pk
334 1.1 pk /* Establish link to `ledma' device */
335 1.1 pk lesc->sc_dma = lsi;
336 1.2 pk lesc->sc_dma->sc_client = lesc;
337 1.1 pk
338 1.1 pk /* Map device registers */
339 1.16 pk if (sbus_bus_map(sa->sa_bustag,
340 1.16 pk sa->sa_slot,
341 1.16 pk sa->sa_offset,
342 1.16 pk sa->sa_size,
343 1.17 eeh 0, &lesc->sc_reg) != 0) {
344 1.29 tsutsui aprint_error(": cannot map registers\n");
345 1.1 pk return;
346 1.1 pk }
347 1.1 pk
348 1.1 pk /* Allocate buffer memory */
349 1.1 pk sc->sc_memsize = MEMSIZE;
350 1.8 pk
351 1.9 pk /* Get a DMA handle */
352 1.8 pk if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE,
353 1.36 msaitoh LEDMA_BOUNDARY, BUS_DMA_NOWAIT, &lesc->sc_dmamap)) != 0) {
354 1.29 tsutsui aprint_error(": DMA map create error %d\n", error);
355 1.8 pk return;
356 1.8 pk }
357 1.8 pk
358 1.9 pk /* Allocate DMA buffer */
359 1.10 pk if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, LEDMA_BOUNDARY,
360 1.36 msaitoh &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
361 1.29 tsutsui aprint_error(": DMA buffer alloc error %d\n",error);
362 1.37 rin goto bad_destroy;
363 1.1 pk }
364 1.8 pk
365 1.12 mrg /* Map DMA buffer into kernel space */
366 1.12 mrg if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
367 1.36 msaitoh (void **)&sc->sc_mem, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
368 1.29 tsutsui aprint_error(": DMA buffer map error %d\n", error);
369 1.37 rin goto bad_free;
370 1.8 pk }
371 1.8 pk
372 1.12 mrg /* Load DMA buffer */
373 1.12 mrg if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap, sc->sc_mem,
374 1.36 msaitoh MEMSIZE, NULL, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
375 1.29 tsutsui aprint_error(": DMA buffer map load error %d\n", error);
376 1.37 rin goto bad_unmap;
377 1.1 pk }
378 1.1 pk
379 1.8 pk lesc->sc_laddr = lesc->sc_dmamap->dm_segs[0].ds_addr;
380 1.8 pk sc->sc_addr = lesc->sc_laddr & 0xffffff;
381 1.1 pk sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
382 1.31 tsutsui lesc->sc_lostcount = 0;
383 1.1 pk
384 1.1 pk sc->sc_mediachange = lemediachange;
385 1.1 pk sc->sc_mediastatus = lemediastatus;
386 1.1 pk sc->sc_supmedia = lemedia;
387 1.1 pk sc->sc_nsupmedia = NLEMEDIA;
388 1.36 msaitoh sc->sc_defaultmedia = IFM_ETHER | IFM_AUTO;
389 1.1 pk
390 1.39 thorpej if (! ether_getaddr(self, sc->sc_enaddr)) {
391 1.39 thorpej aprint_error(": unable to get MAC address\n");
392 1.39 thorpej goto bad_unmap;
393 1.39 thorpej }
394 1.1 pk
395 1.1 pk sc->sc_copytodesc = lance_copytobuf_contig;
396 1.1 pk sc->sc_copyfromdesc = lance_copyfrombuf_contig;
397 1.1 pk sc->sc_copytobuf = lance_copytobuf_contig;
398 1.1 pk sc->sc_copyfrombuf = lance_copyfrombuf_contig;
399 1.1 pk sc->sc_zerobuf = lance_zerobuf_contig;
400 1.1 pk
401 1.1 pk sc->sc_rdcsr = lerdcsr;
402 1.1 pk sc->sc_wrcsr = lewrcsr;
403 1.1 pk sc->sc_hwinit = lehwinit;
404 1.1 pk sc->sc_nocarrier = lenocarrier;
405 1.1 pk sc->sc_hwreset = lehwreset;
406 1.1 pk
407 1.6 pk /* Establish interrupt handler */
408 1.6 pk if (sa->sa_nintr != 0)
409 1.22 pk (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET,
410 1.6 pk am7990_intr, sc);
411 1.1 pk
412 1.1 pk am7990_config(&lesc->sc_am7990);
413 1.1 pk
414 1.1 pk /* now initialize DMA */
415 1.1 pk lehwreset(sc);
416 1.37 rin
417 1.37 rin return;
418 1.37 rin
419 1.37 rin bad_unmap:
420 1.37 rin bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE);
421 1.37 rin bad_free:
422 1.37 rin bus_dmamem_free(dmatag, &seg, rseg);
423 1.37 rin bad_destroy:
424 1.37 rin bus_dmamap_destroy(dmatag, lesc->sc_dmamap);
425 1.1 pk }
426