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