xref: /netbsd-src/sys/dev/sbus/if_le.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: if_le.c,v 1.35 2008/04/04 12:25:07 tsutsui 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.c,v 1.35 2008/04/04 12:25:07 tsutsui 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 #include <sys/bus.h>
59 #include <sys/intr.h>
60 #include <machine/autoconf.h>
61 
62 #include <dev/sbus/sbusvar.h>
63 #include <dev/sbus/lebuffervar.h>	/*XXX*/
64 
65 #include <dev/ic/lancereg.h>
66 #include <dev/ic/lancevar.h>
67 #include <dev/ic/am7990reg.h>
68 #include <dev/ic/am7990var.h>
69 
70 #include "ioconf.h"
71 
72 /*
73  * LANCE registers.
74  */
75 #define LEREG1_RDP	0	/* Register Data port */
76 #define LEREG1_RAP	2	/* Register Address port */
77 
78 struct	le_softc {
79 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
80 	struct	sbusdev		sc_sd;		/* sbus device */
81 	bus_space_tag_t		sc_bustag;
82 	bus_dma_tag_t		sc_dmatag;
83 	bus_dmamap_t		sc_dmamap;
84 	bus_space_handle_t	sc_reg;
85 };
86 
87 #define MEMSIZE 0x4000		/* LANCE memory size */
88 
89 int	lematch_sbus(device_t, cfdata_t, void *);
90 void	leattach_sbus(device_t, device_t, void *);
91 
92 /*
93  * Media types supported.
94  */
95 static int lemedia[] = {
96 	IFM_ETHER|IFM_10_5,
97 };
98 #define NLEMEDIA	__arraycount(lemedia)
99 
100 CFATTACH_DECL_NEW(le_sbus, sizeof(struct le_softc),
101     lematch_sbus, leattach_sbus, NULL, NULL);
102 
103 #if defined(_KERNEL_OPT)
104 #include "opt_ddb.h"
105 #endif
106 
107 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
108 static uint16_t lerdcsr(struct lance_softc *, uint16_t);
109 
110 static void
111 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
112 {
113 	struct le_softc *lesc = (struct le_softc *)sc;
114 	bus_space_tag_t t = lesc->sc_bustag;
115 	bus_space_handle_t h = lesc->sc_reg;
116 
117 	bus_space_write_2(t, h, LEREG1_RAP, port);
118 	bus_space_write_2(t, h, LEREG1_RDP, val);
119 
120 #if defined(SUN4M)
121 	/*
122 	 * We need to flush the Sbus->Mbus write buffers. This can most
123 	 * easily be accomplished by reading back the register that we
124 	 * just wrote (thanks to Chris Torek for this solution).
125 	 */
126 	(void)bus_space_read_2(t, h, LEREG1_RDP);
127 #endif
128 }
129 
130 static uint16_t
131 lerdcsr(struct lance_softc *sc, uint16_t port)
132 {
133 	struct le_softc *lesc = (struct le_softc *)sc;
134 	bus_space_tag_t t = lesc->sc_bustag;
135 	bus_space_handle_t h = lesc->sc_reg;
136 
137 	bus_space_write_2(t, h, LEREG1_RAP, port);
138 	return (bus_space_read_2(t, h, LEREG1_RDP));
139 }
140 
141 
142 int
143 lematch_sbus(device_t parent, cfdata_t cf, void *aux)
144 {
145 	struct sbus_attach_args *sa = aux;
146 
147 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
148 }
149 
150 void
151 leattach_sbus(device_t parent, device_t self, void *aux)
152 {
153 	struct le_softc *lesc = device_private(self);
154 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
155 	struct sbus_softc *sbsc = device_private(parent);
156 	struct sbus_attach_args *sa = aux;
157 	bus_dma_tag_t dmatag;
158 	struct sbusdev *sd;
159 
160 	sc->sc_dev = self;
161 	lesc->sc_bustag = sa->sa_bustag;
162 	lesc->sc_dmatag = dmatag = sa->sa_dmatag;
163 
164 	if (sbus_bus_map(sa->sa_bustag,
165 			 sa->sa_slot,
166 			 sa->sa_offset,
167 			 sa->sa_size,
168 			 0, &lesc->sc_reg) != 0) {
169 		aprint_error(": cannot map registers\n");
170 		return;
171 	}
172 
173 	/*
174 	 * Look for an "unallocated" lebuffer and pair it with
175 	 * this `le' device on the assumption that we're on
176 	 * a pre-historic ROM that doesn't establish le<=>lebuffer
177 	 * parent-child relationships.
178 	 */
179 	for (sd = sbsc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
180 
181 		struct lebuf_softc *lebuf = device_private(sd->sd_dev);
182 
183 		if (strncmp("lebuffer", device_xname(sd->sd_dev), 8) != 0)
184 			continue;
185 
186 		if (lebuf->attached != 0)
187 			continue;
188 
189 		sc->sc_mem = lebuf->sc_buffer;
190 		sc->sc_memsize = lebuf->sc_bufsiz;
191 		sc->sc_addr = 0; /* Lance view is offset by buffer location */
192 		lebuf->attached = 1;
193 
194 		/* That old black magic... */
195 		sc->sc_conf3 = prom_getpropint(sa->sa_node,
196 					  "busmaster-regval",
197 					  LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
198 		break;
199 	}
200 
201 	lesc->sc_sd.sd_reset = (void *)lance_reset;
202 	sbus_establish(&lesc->sc_sd, self);
203 
204 	if (sc->sc_mem == 0) {
205 		bus_dma_segment_t seg;
206 		int rseg, error;
207 
208 #ifndef BUS_DMA_24BIT
209 /* XXX - This flag is not defined on all archs */
210 #define BUS_DMA_24BIT	0
211 #endif
212 		/* Get a DMA handle */
213 		if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0,
214 						BUS_DMA_NOWAIT|BUS_DMA_24BIT,
215 						&lesc->sc_dmamap)) != 0) {
216 			aprint_error(": DMA map create error %d\n", error);
217 			return;
218 		}
219 
220 		/* Allocate DMA buffer */
221 		if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, 0,
222 					 &seg, 1, &rseg,
223 					 BUS_DMA_NOWAIT|BUS_DMA_24BIT)) != 0){
224 			aprint_error(": DMA buffer allocation error %d\n",
225 			    error);
226 			return;
227 		}
228 
229 		/* Map DMA buffer into kernel space */
230 		if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
231 				       (void **)&sc->sc_mem,
232 				       BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
233 			aprint_error(": DMA buffer map error %d\n", error);
234 			bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
235 			return;
236 		}
237 
238 		/* Load DMA buffer */
239 		if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap,
240 		    sc->sc_mem, MEMSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
241 			aprint_error(": DMA buffer map load error %d\n", error);
242 			bus_dmamem_free(dmatag, &seg, rseg);
243 			bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE);
244 			return;
245 		}
246 
247 		sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff;
248 		sc->sc_memsize = MEMSIZE;
249 		sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
250 	}
251 
252 	prom_getether(sa->sa_node, sc->sc_enaddr);
253 
254 	sc->sc_supmedia = lemedia;
255 	sc->sc_nsupmedia = NLEMEDIA;
256 	sc->sc_defaultmedia = lemedia[0];
257 
258 	sc->sc_copytodesc = lance_copytobuf_contig;
259 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
260 	sc->sc_copytobuf = lance_copytobuf_contig;
261 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
262 	sc->sc_zerobuf = lance_zerobuf_contig;
263 
264 	sc->sc_rdcsr = lerdcsr;
265 	sc->sc_wrcsr = lewrcsr;
266 
267 	am7990_config(&lesc->sc_am7990);
268 
269 	/* Establish interrupt handler */
270 	if (sa->sa_nintr != 0)
271 		(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
272 					 IPL_NET, am7990_intr, sc);
273 }
274