xref: /netbsd-src/sys/arch/evbppc/explora/dev/le_elb.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: le_elb.c,v 1.3 2003/08/16 15:40:41 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Juergen Hannken-Illjes.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: le_elb.c,v 1.3 2003/08/16 15:40:41 hannken Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/conf.h>
44 #include <sys/device.h>
45 #include <sys/systm.h>
46 
47 #include <uvm/uvm_extern.h>
48 
49 #include <machine/bus.h>
50 
51 #include <net/if.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54 
55 #include <dev/ic/lancereg.h>
56 #include <dev/ic/lancevar.h>
57 #include <dev/ic/am79900reg.h>
58 #include <dev/ic/am79900var.h>
59 
60 #include <evbppc/explora/dev/elbvar.h>
61 
62 #define LE_MEMSIZE	16384
63 #define LE_RDP		0x10	/* Indirect data register. */
64 #define LE_RAP		0x14	/* Indirect address register. */
65 #define LE_NPORTS	32
66 
67 struct le_elb_softc {
68 	struct am79900_softc sc_am79900;
69 	bus_dma_tag_t sc_dmat;
70 	bus_dmamap_t sc_dmam;
71 	bus_space_tag_t sc_iot;
72 	bus_space_handle_t sc_ioh;
73 	void *sc_ih;
74 };
75 
76 static int	le_elb_probe(struct device *, struct cfdata *, void *);
77 static void	le_elb_attach(struct device *, struct device *, void *);
78 static u_int16_t le_rdcsr(struct lance_softc *, u_int16_t);
79 static void	le_wrcsr(struct lance_softc *, u_int16_t, u_int16_t);
80 static void	le_copytodesc(struct lance_softc *, void *, int, int);
81 static void	le_copyfromdesc(struct lance_softc *, void *, int, int);
82 static void	le_copytobuf(struct lance_softc *, void *, int, int);
83 static void	le_copyfrombuf(struct lance_softc *, void *, int, int);
84 static void	le_zerobuf(struct lance_softc *, int, int);
85 
86 CFATTACH_DECL(le_elb, sizeof(struct le_elb_softc),
87     le_elb_probe, le_elb_attach, NULL, NULL);
88 
89 int
90 le_elb_probe(struct device *parent, struct cfdata *cf, void *aux)
91 {
92 	struct elb_attach_args *oaa = aux;
93 
94 	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
95 		return 0;
96 
97 	return (1);
98 }
99 
100 void
101 le_elb_attach(struct device *parent, struct device *self, void *aux)
102 {
103 	struct le_elb_softc *msc = (void *)self;
104 	struct lance_softc *sc = &msc->sc_am79900.lsc;
105 	struct elb_attach_args *eaa = aux;
106 	bus_dma_segment_t seg;
107 	int i, rseg;
108 
109 	printf("\n");
110 
111 	if (booted_device == NULL)	/*XXX*/
112 		booted_device = self;
113 
114 	msc->sc_iot = eaa->elb_bt;
115 	msc->sc_dmat = eaa->elb_dmat;
116 
117 	bus_space_map(msc->sc_iot, eaa->elb_base, LE_NPORTS, 0, &msc->sc_ioh);
118 
119 	/*
120 	 * Allocate a DMA area for the card.
121 	 */
122 	if (bus_dmamem_alloc(msc->sc_dmat, LE_MEMSIZE, PAGE_SIZE, 0,
123 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
124 		printf("%s: couldn't allocate memory for card\n",
125 		    sc->sc_dev.dv_xname);
126 		return;
127 	}
128 	if (bus_dmamem_map(msc->sc_dmat, &seg, rseg, LE_MEMSIZE,
129 	    (caddr_t *)&sc->sc_mem,
130 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
131 		printf("%s: couldn't map memory for card\n",
132 		    sc->sc_dev.dv_xname);
133 		return;
134 	}
135 
136 	/*
137 	 * Create and load the DMA map for the DMA area.
138 	 */
139 	if (bus_dmamap_create(msc->sc_dmat, LE_MEMSIZE, 1,
140 	    LE_MEMSIZE, 0, BUS_DMA_NOWAIT, &msc->sc_dmam)) {
141 		printf("%s: couldn't create DMA map\n",
142 		    sc->sc_dev.dv_xname);
143 		bus_dmamem_free(msc->sc_dmat, &seg, rseg);
144 		return;
145 	}
146 	if (bus_dmamap_load(msc->sc_dmat, msc->sc_dmam,
147 	    sc->sc_mem, LE_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
148 		printf("%s: coundn't load DMA map\n",
149 		    sc->sc_dev.dv_xname);
150 		bus_dmamem_free(msc->sc_dmat, &seg, rseg);
151 		return;
152 	}
153 
154 	/*
155 	 * This is magic -- DMA doesn't work without address
156 	 * bit 30 set to one.
157 	 */
158 	sc->sc_addr = 0x40000000 | msc->sc_dmam->dm_segs[0].ds_addr;
159 	sc->sc_memsize = LE_MEMSIZE;
160 
161 	sc->sc_copytodesc = le_copytodesc;
162 	sc->sc_copyfromdesc = le_copyfromdesc;
163 	sc->sc_copytobuf = le_copytobuf;
164 	sc->sc_copyfrombuf = le_copyfrombuf;
165 	sc->sc_zerobuf = le_zerobuf;
166 
167 	sc->sc_rdcsr = le_rdcsr;
168 	sc->sc_wrcsr = le_wrcsr;
169 
170 	printf("%s", sc->sc_dev.dv_xname);
171 
172 	/* Save the MAC address. */
173 	for (i = 0; i < 3; i++) {
174 		sc->sc_enaddr[i*2] = le_rdcsr(sc, 12+i);
175 		sc->sc_enaddr[i*2+1] = le_rdcsr(sc, 12+i) >> 8;
176 	}
177 
178 	am79900_config(&msc->sc_am79900);
179 
180 	/* Chip is stopped. Set "software style" to 32-bit. */
181 	le_wrcsr(sc, LE_CSR58, 2);
182 
183 	intr_establish(eaa->elb_irq, IST_LEVEL, IPL_NET, am79900_intr, sc);
184 }
185 
186 /*
187  * Read from an indirect CSR.
188  */
189 static u_int16_t
190 le_rdcsr(struct lance_softc *sc, u_int16_t reg)
191 {
192 	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
193 	bus_space_tag_t iot = lesc->sc_iot;
194 	bus_space_handle_t ioh = lesc->sc_ioh;
195 	u_int16_t val;
196 
197 	bus_space_write_4(iot, ioh, LE_RAP, reg);
198 	val = bus_space_read_4(iot, ioh, LE_RDP);
199 
200 	return(val);
201 }
202 
203 /*
204  * Write to an indirect CSR.
205  */
206 static void
207 le_wrcsr(struct lance_softc *sc, u_int16_t reg, u_int16_t val)
208 {
209 	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
210 	bus_space_tag_t iot = lesc->sc_iot;
211 	bus_space_handle_t ioh = lesc->sc_ioh;
212 
213 	bus_space_write_4(iot, ioh, LE_RAP, reg);
214 	bus_space_write_4(iot, ioh, LE_RDP, val);
215 }
216 
217 /*
218  * Copy data to memory and swap bytes.
219  */
220 static void
221 le_copytodesc(struct lance_softc *sc, void *from, int boff, int len)
222 {
223 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
224 	volatile u_int32_t *src = from;
225 	volatile u_int32_t *dst = (u_int32_t *)((u_char *)sc->sc_mem+boff);
226 	int todo = len;
227 
228 	/* XXX lance_setladrf should be modified to use u_int32_t instead.
229 	 * The init block contains u_int16_t values that require
230 	 * special swapping.
231 	 */
232 	if (boff == LE_INITADDR(sc) && len == sizeof(struct leinit)) {
233 		src[3] = (src[3] >> 16) | (src[3] << 16);
234 		src[4] = (src[4] >> 16) | (src[4] << 16);
235 	}
236 
237 	todo /= sizeof(u_int32_t);
238 	while (todo-- > 0)
239 		*dst++ = bswap32(*src++);
240 
241 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
242 	    BUS_DMASYNC_PREWRITE);
243 }
244 
245 /*
246  * Copy data from memory and swap bytes.
247  */
248 static void
249 le_copyfromdesc(struct lance_softc *sc, void *to, int boff, int len)
250 {
251 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
252 	volatile u_int32_t *src = (u_int32_t *)((u_char *)sc->sc_mem+boff);
253 	volatile u_int32_t *dst = to;
254 
255 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
256 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
257 
258 	len /= sizeof(u_int32_t);
259 	while (len-- > 0)
260 		*dst++ = bswap32(*src++);
261 }
262 
263 /*
264  * Copy data to memory.
265  */
266 static void
267 le_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
268 {
269 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
270 	volatile caddr_t buf = (caddr_t)((u_char *)sc->sc_mem+boff);
271 
272 	memcpy(buf, from, len);
273 
274 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
275 	    BUS_DMASYNC_PREWRITE);
276 }
277 
278 /*
279  * Copy data from memory.
280  */
281 static void
282 le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
283 {
284 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
285 	volatile caddr_t buf = (caddr_t)((u_char *)sc->sc_mem+boff);
286 
287 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
288 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
289 
290 	memcpy(to, buf, len);
291 }
292 
293 /*
294  * Zero memory.
295  */
296 static void
297 le_zerobuf(struct lance_softc *sc, int boff, int len)
298 {
299 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
300 	volatile caddr_t buf = (caddr_t)((u_char *)sc->sc_mem+boff);
301 
302 	memset(buf, 0, len);
303 
304 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
305 	    BUS_DMASYNC_PREWRITE);
306 }
307