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