xref: /netbsd-src/sys/arch/evbppc/explora/dev/le_elb.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: le_elb.c,v 1.1 2003/03/11 10:57:57 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/param.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/systm.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <machine/bus.h>
47 
48 #include <net/if.h>
49 #include <net/if_ether.h>
50 #include <net/if_media.h>
51 
52 #include <dev/ic/lancereg.h>
53 #include <dev/ic/lancevar.h>
54 #include <dev/ic/am79900reg.h>
55 #include <dev/ic/am79900var.h>
56 
57 #include <evbppc/explora/dev/elbvar.h>
58 
59 #define LE_MEMSIZE	16384
60 #define LE_RDP		0x10	/* Indirect data register. */
61 #define LE_RAP		0x14	/* Indirect address register. */
62 #define LE_NPORTS	32
63 
64 struct le_elb_softc {
65 	struct am79900_softc sc_am79900;
66 	bus_dma_tag_t sc_dmat;
67 	bus_dmamap_t sc_dmam;
68 	bus_space_tag_t sc_iot;
69 	bus_space_handle_t sc_ioh;
70 	void *sc_ih;
71 };
72 
73 static int	le_elb_probe(struct device *, struct cfdata *, void *);
74 static void	le_elb_attach(struct device *, struct device *, void *);
75 static u_int16_t le_rdcsr(struct lance_softc *, u_int16_t);
76 static void	le_wrcsr(struct lance_softc *, u_int16_t, u_int16_t);
77 static void	le_copytodesc(struct lance_softc *, void *, int, int);
78 static void	le_copyfromdesc(struct lance_softc *, void *, int, int);
79 
80 CFATTACH_DECL(le_elb, sizeof(struct le_elb_softc),
81     le_elb_probe, le_elb_attach, NULL, NULL);
82 
83 int
84 le_elb_probe(struct device *parent, struct cfdata *cf, void *aux)
85 {
86 	struct elb_attach_args *oaa = aux;
87 
88 	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
89 		return 0;
90 
91 	return (1);
92 }
93 
94 void
95 le_elb_attach(struct device *parent, struct device *self, void *aux)
96 {
97 	struct le_elb_softc *msc = (void *)self;
98 	struct lance_softc *sc = &msc->sc_am79900.lsc;
99 	struct elb_attach_args *eaa = aux;
100 	bus_dma_segment_t seg;
101 	int i, rseg;
102 
103 	printf("\n");
104 
105 	if (booted_device == NULL)	/*XXX*/
106 		booted_device = self;
107 
108 	msc->sc_iot = eaa->elb_bt;
109 	msc->sc_dmat = eaa->elb_dmat;
110 
111 	bus_space_map(msc->sc_iot, eaa->elb_base, LE_NPORTS, 0, &msc->sc_ioh);
112 
113 	/*
114 	 * Allocate a DMA area for the card.
115 	 */
116 	if (bus_dmamem_alloc(msc->sc_dmat, LE_MEMSIZE, PAGE_SIZE, 0,
117 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
118 		printf("%s: couldn't allocate memory for card\n",
119 		    sc->sc_dev.dv_xname);
120 		return;
121 	}
122 	if (bus_dmamem_map(msc->sc_dmat, &seg, rseg, LE_MEMSIZE,
123 	    (caddr_t *)&sc->sc_mem,
124 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
125 		printf("%s: couldn't map memory for card\n",
126 		    sc->sc_dev.dv_xname);
127 		return;
128 	}
129 
130 	/*
131 	 * Create and load the DMA map for the DMA area.
132 	 */
133 	if (bus_dmamap_create(msc->sc_dmat, LE_MEMSIZE, 1,
134 	    LE_MEMSIZE, 0, BUS_DMA_NOWAIT, &msc->sc_dmam)) {
135 		printf("%s: couldn't create DMA map\n",
136 		    sc->sc_dev.dv_xname);
137 		bus_dmamem_free(msc->sc_dmat, &seg, rseg);
138 		return;
139 	}
140 	if (bus_dmamap_load(msc->sc_dmat, msc->sc_dmam,
141 	    sc->sc_mem, LE_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
142 		printf("%s: coundn't load DMA map\n",
143 		    sc->sc_dev.dv_xname);
144 		bus_dmamem_free(msc->sc_dmat, &seg, rseg);
145 		return;
146 	}
147 
148 	/*
149 	 * This is magic -- DMA doesn't work without address
150 	 * bit 30 set to one.
151 	 */
152 	sc->sc_addr = 0x40000000 | msc->sc_dmam->dm_segs[0].ds_addr;
153 	sc->sc_memsize = LE_MEMSIZE;
154 
155 	sc->sc_copytodesc = le_copytodesc;
156 	sc->sc_copyfromdesc = le_copyfromdesc;
157 	sc->sc_copytobuf = lance_copytobuf_contig;
158 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
159 	sc->sc_zerobuf = lance_zerobuf_contig;
160 
161 	sc->sc_rdcsr = le_rdcsr;
162 	sc->sc_wrcsr = le_wrcsr;
163 
164 	printf("%s", sc->sc_dev.dv_xname);
165 
166 	/* Save the MAC address. */
167 	for (i = 0; i < 3; i++) {
168 		sc->sc_enaddr[i*2] = le_rdcsr(sc, 12+i);
169 		sc->sc_enaddr[i*2+1] = le_rdcsr(sc, 12+i) >> 8;
170 	}
171 
172 	am79900_config(&msc->sc_am79900);
173 
174 	/* Chip is stopped. Set "software style" to 32-bit. */
175 	le_wrcsr(sc, LE_CSR58, 2);
176 
177 	intr_establish(eaa->elb_irq, IST_LEVEL, IPL_NET, am79900_intr, sc);
178 }
179 
180 /*
181  * Read from an indirect CSR.
182  */
183 static u_int16_t
184 le_rdcsr(struct lance_softc *sc, u_int16_t reg)
185 {
186 	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
187 	bus_space_tag_t iot = lesc->sc_iot;
188 	bus_space_handle_t ioh = lesc->sc_ioh;
189 	u_int16_t val;
190 
191 	bus_space_write_4(iot, ioh, LE_RAP, reg);
192 	val = bus_space_read_4(iot, ioh, LE_RDP);
193 
194 	return(val);
195 }
196 
197 /*
198  * Write to an indirect CSR.
199  */
200 static void
201 le_wrcsr(struct lance_softc *sc, u_int16_t reg, u_int16_t val)
202 {
203 	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
204 	bus_space_tag_t iot = lesc->sc_iot;
205 	bus_space_handle_t ioh = lesc->sc_ioh;
206 
207 	bus_space_write_4(iot, ioh, LE_RAP, reg);
208 	bus_space_write_4(iot, ioh, LE_RDP, val);
209 }
210 
211 /*
212  * Copy data to memory and swap bytes.
213  */
214 static void
215 le_copytodesc(struct lance_softc *sc, void *from, int boff, int len)
216 {
217 	volatile u_int32_t *src = from;
218 	volatile u_int32_t *dst = (u_int32_t *)((u_char *)sc->sc_mem+boff);
219 
220 	/* XXX lance_setladrf should be modified to use u_int32_t instead.
221 	 * The init block contains u_int16_t values that require
222 	 * special swapping.
223 	 */
224 	if (boff == LE_INITADDR(sc) && len == sizeof(struct leinit)) {
225 		src[3] = (src[3] >> 16) | (src[3] << 16);
226 		src[4] = (src[4] >> 16) | (src[4] << 16);
227 	}
228 
229 	len /= sizeof(u_int32_t);
230 	while (len-- > 0)
231 		*dst++ = bswap32(*src++);
232 }
233 
234 /*
235  * Copy data from memory and swap bytes.
236  */
237 static void
238 le_copyfromdesc(struct lance_softc *sc, void *to, int boff, int len)
239 {
240 	volatile u_int32_t *src = (u_int32_t *)((u_char *)sc->sc_mem+boff);
241 	volatile u_int32_t *dst = to;
242 
243 	len /= sizeof(u_int32_t);
244 	while (len-- > 0)
245 		*dst++ = bswap32(*src++);
246 }
247