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