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