if_le_ledma.c revision 1.35.64.1 1 /* $NetBSD: if_le_ledma.c,v 1.35.64.1 2019/06/10 22:07:32 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 __KERNEL_RCSID(0, "$NetBSD: if_le_ledma.c,v 1.35.64.1 2019/06/10 22:07:32 christos Exp $");
35
36 #include "opt_inet.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/syslog.h>
42 #include <sys/socket.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45
46 #include <net/if.h>
47 #include <net/if_ether.h>
48 #include <net/if_media.h>
49
50 #ifdef INET
51 #include <netinet/in.h>
52 #include <netinet/if_inarp.h>
53 #endif
54
55 #include <sys/bus.h>
56 #include <sys/intr.h>
57 #include <machine/autoconf.h>
58
59 #include <dev/sbus/sbusvar.h>
60
61 #include <dev/ic/lsi64854reg.h>
62 #include <dev/ic/lsi64854var.h>
63
64 #include <dev/ic/lancereg.h>
65 #include <dev/ic/lancevar.h>
66 #include <dev/ic/am7990reg.h>
67 #include <dev/ic/am7990var.h>
68
69 #include "ioconf.h"
70
71 /*
72 * LANCE registers.
73 */
74 #define LEREG1_RDP 0 /* Register Data port */
75 #define LEREG1_RAP 2 /* Register Address port */
76
77 struct le_softc {
78 struct am7990_softc sc_am7990; /* glue to MI code */
79 bus_space_tag_t sc_bustag;
80 bus_dmamap_t sc_dmamap;
81 bus_space_handle_t sc_reg; /* LANCE registers */
82 struct lsi64854_softc *sc_dma; /* pointer to my dma */
83 u_int sc_laddr; /* LANCE DMA address */
84 u_int sc_lostcount;
85 #define LE_LOSTTHRESH 5 /* lost carrior count to switch media */
86 };
87
88 #define MEMSIZE (16*1024) /* LANCE memory size */
89 #define LEDMA_BOUNDARY (16*1024*1024) /* must not cross 16MB boundary */
90
91 int lematch_ledma(device_t, cfdata_t, void *);
92 void leattach_ledma(device_t, device_t, void *);
93
94 /*
95 * Media types supported by the Sun4m.
96 */
97 static int lemedia[] = {
98 IFM_ETHER | IFM_10_T,
99 IFM_ETHER | IFM_10_5,
100 IFM_ETHER | IFM_AUTO,
101 };
102 #define NLEMEDIA __arraycount(lemedia)
103
104 void lesetutp(struct lance_softc *);
105 void lesetaui(struct lance_softc *);
106
107 int lemediachange(struct lance_softc *);
108 void lemediastatus(struct lance_softc *, struct ifmediareq *);
109
110 CFATTACH_DECL_NEW(le_ledma, sizeof(struct le_softc),
111 lematch_ledma, leattach_ledma, NULL, NULL);
112
113 #if defined(_KERNEL_OPT)
114 #include "opt_ddb.h"
115 #endif
116
117 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
118 static uint16_t lerdcsr(struct lance_softc *, uint16_t);
119 static void lehwreset(struct lance_softc *);
120 static void lehwinit(struct lance_softc *);
121 static void lenocarrier(struct lance_softc *);
122
123 static void
124 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
125 {
126 struct le_softc *lesc = (struct le_softc *)sc;
127 bus_space_tag_t t = lesc->sc_bustag;
128 bus_space_handle_t h = lesc->sc_reg;
129
130 bus_space_write_2(t, h, LEREG1_RAP, port);
131 bus_space_write_2(t, h, LEREG1_RDP, val);
132
133 #if defined(SUN4M)
134 /*
135 * We need to flush the Sbus->Mbus write buffers. This can most
136 * easily be accomplished by reading back the register that we
137 * just wrote (thanks to Chris Torek for this solution).
138 */
139 (void)bus_space_read_2(t, h, LEREG1_RDP);
140 #endif
141 }
142
143 static uint16_t
144 lerdcsr(struct lance_softc *sc, uint16_t port)
145 {
146 struct le_softc *lesc = (struct le_softc *)sc;
147 bus_space_tag_t t = lesc->sc_bustag;
148 bus_space_handle_t h = lesc->sc_reg;
149
150 bus_space_write_2(t, h, LEREG1_RAP, port);
151 return bus_space_read_2(t, h, LEREG1_RDP);
152 }
153
154 void
155 lesetutp(struct lance_softc *sc)
156 {
157 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
158 uint32_t csr;
159
160 csr = L64854_GCSR(dma);
161 csr |= E_TP_AUI;
162 L64854_SCSR(dma, csr);
163 delay(20000); /* must not touch le for 20ms */
164 }
165
166 void
167 lesetaui(struct lance_softc *sc)
168 {
169 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
170 uint32_t csr;
171
172 csr = L64854_GCSR(dma);
173 csr &= ~E_TP_AUI;
174 L64854_SCSR(dma, csr);
175 delay(20000); /* must not touch le for 20ms */
176 }
177
178 int
179 lemediachange(struct lance_softc *sc)
180 {
181 struct ifmedia *ifm = &sc->sc_media;
182
183 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
184 return EINVAL;
185
186 /*
187 * Switch to the selected media. If autoselect is
188 * set, we don't really have to do anything. We'll
189 * switch to the other media when we detect loss of
190 * carrier.
191 */
192 switch (IFM_SUBTYPE(ifm->ifm_media)) {
193 case IFM_10_T:
194 lesetutp(sc);
195 break;
196
197 case IFM_10_5:
198 lesetaui(sc);
199 break;
200
201 case IFM_AUTO:
202 break;
203
204 default:
205 return EINVAL;
206 }
207
208 return 0;
209 }
210
211 void
212 lemediastatus(struct lance_softc *sc, struct ifmediareq *ifmr)
213 {
214 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma;
215
216 /*
217 * Notify the world which media we're currently using.
218 */
219 if (L64854_GCSR(dma) & E_TP_AUI)
220 ifmr->ifm_active = IFM_ETHER | IFM_10_T;
221 else
222 ifmr->ifm_active = IFM_ETHER | IFM_10_5;
223 }
224
225 static void
226 lehwreset(struct lance_softc *sc)
227 {
228 struct le_softc *lesc = (struct le_softc *)sc;
229 struct lsi64854_softc *dma = lesc->sc_dma;
230 uint32_t csr;
231 u_int aui_bit;
232
233 /*
234 * Reset DMA channel.
235 */
236 csr = L64854_GCSR(dma);
237 aui_bit = csr & E_TP_AUI;
238 DMA_RESET(dma);
239
240 /* Write bits 24-31 of Lance address */
241 bus_space_write_4(dma->sc_bustag, dma->sc_regs, L64854_REG_ENBAR,
242 lesc->sc_laddr & 0xff000000);
243
244 DMA_ENINTR(dma);
245
246 /*
247 * Disable E-cache invalidates on chip writes.
248 * Retain previous cable selection bit.
249 */
250 csr = L64854_GCSR(dma);
251 csr |= (E_DSBL_WR_INVAL | aui_bit);
252 L64854_SCSR(dma, csr);
253 delay(20000); /* must not touch le for 20ms */
254 }
255
256 static void
257 lehwinit(struct lance_softc *sc)
258 {
259
260 /*
261 * Make sure we're using the currently-enabled media type.
262 * XXX Actually, this is probably unnecessary, now.
263 */
264 switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
265 case IFM_10_T:
266 lesetutp(sc);
267 break;
268
269 case IFM_10_5:
270 lesetaui(sc);
271 break;
272 }
273 }
274
275 static void
276 lenocarrier(struct lance_softc *sc)
277 {
278 struct le_softc *lesc = (struct le_softc *)sc;
279
280 /* it may take a while for modern switches to set 10baseT media */
281 if (lesc->sc_lostcount++ < LE_LOSTTHRESH)
282 return;
283
284 lesc->sc_lostcount = 0;
285
286 /*
287 * Check if the user has requested a certain cable type, and
288 * if so, honor that request.
289 */
290 printf("%s: lost carrier on ", device_xname(sc->sc_dev));
291
292 if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) {
293 printf("UTP port");
294 switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
295 case IFM_10_5:
296 case IFM_AUTO:
297 printf(", switching to AUI port");
298 lesetaui(sc);
299 }
300 } else {
301 printf("AUI port");
302 switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
303 case IFM_10_T:
304 case IFM_AUTO:
305 printf(", switching to UTP port");
306 lesetutp(sc);
307 }
308 }
309 printf("\n");
310 }
311
312 int
313 lematch_ledma(device_t parent, cfdata_t cf, void *aux)
314 {
315 struct sbus_attach_args *sa = aux;
316
317 return (strcmp(cf->cf_name, sa->sa_name) == 0);
318 }
319
320
321 void
322 leattach_ledma(device_t parent, device_t self, void *aux)
323 {
324 struct le_softc *lesc = device_private(self);
325 struct lance_softc *sc = &lesc->sc_am7990.lsc;
326 struct lsi64854_softc *lsi = device_private(parent);
327 struct sbus_attach_args *sa = aux;
328 bus_dma_tag_t dmatag = sa->sa_dmatag;
329 bus_dma_segment_t seg;
330 int rseg, error;
331
332 sc->sc_dev = self;
333 lesc->sc_bustag = sa->sa_bustag;
334
335 /* Establish link to `ledma' device */
336 lesc->sc_dma = lsi;
337 lesc->sc_dma->sc_client = lesc;
338
339 /* Map device registers */
340 if (sbus_bus_map(sa->sa_bustag,
341 sa->sa_slot,
342 sa->sa_offset,
343 sa->sa_size,
344 0, &lesc->sc_reg) != 0) {
345 aprint_error(": cannot map registers\n");
346 return;
347 }
348
349 /* Allocate buffer memory */
350 sc->sc_memsize = MEMSIZE;
351
352 /* Get a DMA handle */
353 if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE,
354 LEDMA_BOUNDARY, BUS_DMA_NOWAIT, &lesc->sc_dmamap)) != 0) {
355 aprint_error(": DMA map create error %d\n", error);
356 return;
357 }
358
359 /* Allocate DMA buffer */
360 if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, LEDMA_BOUNDARY,
361 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
362 aprint_error(": DMA buffer alloc error %d\n",error);
363 return;
364 }
365
366 /* Map DMA buffer into kernel space */
367 if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
368 (void **)&sc->sc_mem, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
369 aprint_error(": DMA buffer map error %d\n", error);
370 bus_dmamem_free(dmatag, &seg, rseg);
371 return;
372 }
373
374 /* Load DMA buffer */
375 if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap, sc->sc_mem,
376 MEMSIZE, NULL, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
377 aprint_error(": DMA buffer map load error %d\n", error);
378 bus_dmamem_free(dmatag, &seg, rseg);
379 bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE);
380 return;
381 }
382
383 lesc->sc_laddr = lesc->sc_dmamap->dm_segs[0].ds_addr;
384 sc->sc_addr = lesc->sc_laddr & 0xffffff;
385 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
386 lesc->sc_lostcount = 0;
387
388 sc->sc_mediachange = lemediachange;
389 sc->sc_mediastatus = lemediastatus;
390 sc->sc_supmedia = lemedia;
391 sc->sc_nsupmedia = NLEMEDIA;
392 sc->sc_defaultmedia = IFM_ETHER | IFM_AUTO;
393
394 prom_getether(sa->sa_node, 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 = lerdcsr;
403 sc->sc_wrcsr = lewrcsr;
404 sc->sc_hwinit = lehwinit;
405 sc->sc_nocarrier = lenocarrier;
406 sc->sc_hwreset = lehwreset;
407
408 /* Establish interrupt handler */
409 if (sa->sa_nintr != 0)
410 (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET,
411 am7990_intr, sc);
412
413 am7990_config(&lesc->sc_am7990);
414
415 /* now initialize DMA */
416 lehwreset(sc);
417 }
418