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