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