1 /* $NetBSD: le_elb.c,v 1.7 2008/04/04 12:25:06 tsutsui Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juergen Hannken-Illjes.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: le_elb.c,v 1.7 2008/04/04 12:25:06 tsutsui Exp $");
35 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
40 #include <uvm/uvm_extern.h>
42 #include <machine/bus.h>
45 #include <net/if_ether.h>
46 #include <net/if_media.h>
48 #include <dev/ic/lancereg.h>
49 #include <dev/ic/lancevar.h>
50 #include <dev/ic/am79900reg.h>
51 #include <dev/ic/am79900var.h>
53 #include <evbppc/explora/dev/elbvar.h>
55 #define LE_MEMSIZE 16384
56 #define LE_RDP 0x10 /* Indirect data register. */
57 #define LE_RAP 0x14 /* Indirect address register. */
61 struct am79900_softc sc_am79900
;
62 bus_dma_tag_t sc_dmat
;
64 bus_space_tag_t sc_iot
;
65 bus_space_handle_t sc_ioh
;
69 static int le_elb_probe(device_t
, cfdata_t
, void *);
70 static void le_elb_attach(device_t
, device_t
, void *);
71 static uint16_t le_rdcsr(struct lance_softc
*, uint16_t);
72 static void le_wrcsr(struct lance_softc
*, uint16_t, uint16_t);
73 static void le_copytodesc(struct lance_softc
*, void *, int, int);
74 static void le_copyfromdesc(struct lance_softc
*, void *, int, int);
75 static void le_copytobuf(struct lance_softc
*, void *, int, int);
76 static void le_copyfrombuf(struct lance_softc
*, void *, int, int);
77 static void le_zerobuf(struct lance_softc
*, int, int);
79 CFATTACH_DECL_NEW(le_elb
, sizeof(struct le_elb_softc
),
80 le_elb_probe
, le_elb_attach
, NULL
, NULL
);
83 le_elb_probe(device_t parent
, cfdata_t cf
, void *aux
)
85 struct elb_attach_args
*oaa
= aux
;
87 if (strcmp(oaa
->elb_name
, cf
->cf_name
) != 0)
94 le_elb_attach(device_t parent
, device_t self
, void *aux
)
96 struct le_elb_softc
*msc
= device_private(self
);
97 struct lance_softc
*sc
= &msc
->sc_am79900
.lsc
;
98 struct elb_attach_args
*eaa
= aux
;
99 bus_dma_segment_t seg
;
105 if (booted_device
== NULL
) /*XXX*/
106 booted_device
= self
;
108 msc
->sc_iot
= eaa
->elb_bt
;
109 msc
->sc_dmat
= eaa
->elb_dmat
;
111 bus_space_map(msc
->sc_iot
, eaa
->elb_base
, LE_NPORTS
, 0, &msc
->sc_ioh
);
114 * Allocate a DMA area for the card.
116 if (bus_dmamem_alloc(msc
->sc_dmat
, LE_MEMSIZE
, PAGE_SIZE
, 0,
117 &seg
, 1, &rseg
, BUS_DMA_NOWAIT
)) {
118 aprint_error_dev(self
, "couldn't allocate memory for card\n");
121 if (bus_dmamem_map(msc
->sc_dmat
, &seg
, rseg
, LE_MEMSIZE
,
122 (void **)&sc
->sc_mem
,
123 BUS_DMA_NOWAIT
|BUS_DMA_COHERENT
)) {
124 aprint_error_dev(self
, "couldn't map memory for card\n");
129 * Create and load the DMA map for the DMA area.
131 if (bus_dmamap_create(msc
->sc_dmat
, LE_MEMSIZE
, 1,
132 LE_MEMSIZE
, 0, BUS_DMA_NOWAIT
, &msc
->sc_dmam
)) {
133 aprint_error_dev(self
, "couldn't create DMA map\n");
134 bus_dmamem_free(msc
->sc_dmat
, &seg
, rseg
);
137 if (bus_dmamap_load(msc
->sc_dmat
, msc
->sc_dmam
,
138 sc
->sc_mem
, LE_MEMSIZE
, NULL
, BUS_DMA_NOWAIT
)) {
139 aprint_error_dev(self
, "coundn't load DMA map\n");
140 bus_dmamem_free(msc
->sc_dmat
, &seg
, rseg
);
145 * This is magic -- DMA doesn't work without address
148 sc
->sc_addr
= 0x40000000 | msc
->sc_dmam
->dm_segs
[0].ds_addr
;
149 sc
->sc_memsize
= LE_MEMSIZE
;
151 sc
->sc_copytodesc
= le_copytodesc
;
152 sc
->sc_copyfromdesc
= le_copyfromdesc
;
153 sc
->sc_copytobuf
= le_copytobuf
;
154 sc
->sc_copyfrombuf
= le_copyfrombuf
;
155 sc
->sc_zerobuf
= le_zerobuf
;
157 sc
->sc_rdcsr
= le_rdcsr
;
158 sc
->sc_wrcsr
= le_wrcsr
;
160 aprint_normal("%s", device_xname(self
));
162 /* Save the MAC address. */
163 for (i
= 0; i
< 3; i
++) {
164 sc
->sc_enaddr
[i
* 2] = le_rdcsr(sc
, 12 + i
);
165 sc
->sc_enaddr
[i
* 2 + 1] = le_rdcsr(sc
, 12 + i
) >> 8;
168 am79900_config(&msc
->sc_am79900
);
170 /* Chip is stopped. Set "software style" to 32-bit. */
171 le_wrcsr(sc
, LE_CSR58
, 2);
173 intr_establish(eaa
->elb_irq
, IST_LEVEL
, IPL_NET
, am79900_intr
, sc
);
177 * Read from an indirect CSR.
180 le_rdcsr(struct lance_softc
*sc
, uint16_t reg
)
182 struct le_elb_softc
*lesc
= (struct le_elb_softc
*)sc
;
183 bus_space_tag_t iot
= lesc
->sc_iot
;
184 bus_space_handle_t ioh
= lesc
->sc_ioh
;
187 bus_space_write_4(iot
, ioh
, LE_RAP
, reg
);
188 val
= bus_space_read_4(iot
, ioh
, LE_RDP
);
194 * Write to an indirect CSR.
197 le_wrcsr(struct lance_softc
*sc
, uint16_t reg
, uint16_t val
)
199 struct le_elb_softc
*lesc
= (struct le_elb_softc
*)sc
;
200 bus_space_tag_t iot
= lesc
->sc_iot
;
201 bus_space_handle_t ioh
= lesc
->sc_ioh
;
203 bus_space_write_4(iot
, ioh
, LE_RAP
, reg
);
204 bus_space_write_4(iot
, ioh
, LE_RDP
, val
);
208 * Copy data to memory and swap bytes.
211 le_copytodesc(struct lance_softc
*sc
, void *from
, int boff
, int len
)
213 struct le_elb_softc
*msc
= (struct le_elb_softc
*)sc
;
214 volatile uint32_t *src
= from
;
215 volatile uint32_t *dst
= (uint32_t *)((uint8_t *)sc
->sc_mem
+ boff
);
218 /* XXX lance_setladrf should be modified to use u_int32_t instead.
219 * The init block contains u_int16_t values that require
222 if (boff
== LE_INITADDR(sc
) && len
== sizeof(struct leinit
)) {
223 src
[3] = (src
[3] >> 16) | (src
[3] << 16);
224 src
[4] = (src
[4] >> 16) | (src
[4] << 16);
227 todo
/= sizeof(uint32_t);
229 *dst
++ = bswap32(*src
++);
231 bus_dmamap_sync(msc
->sc_dmat
, msc
->sc_dmam
, boff
, len
,
232 BUS_DMASYNC_PREWRITE
);
236 * Copy data from memory and swap bytes.
239 le_copyfromdesc(struct lance_softc
*sc
, void *to
, int boff
, int len
)
241 struct le_elb_softc
*msc
= (struct le_elb_softc
*)sc
;
242 volatile uint32_t *src
= (uint32_t *)((uint8_t *)sc
->sc_mem
+ boff
);
243 volatile uint32_t *dst
= to
;
245 bus_dmamap_sync(msc
->sc_dmat
, msc
->sc_dmam
, boff
, len
,
246 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
248 len
/= sizeof(uint32_t);
250 *dst
++ = bswap32(*src
++);
254 * Copy data to memory.
257 le_copytobuf(struct lance_softc
*sc
, void *from
, int boff
, int len
)
259 struct le_elb_softc
*msc
= (struct le_elb_softc
*)sc
;
260 volatile void *buf
= (void *)((uint8_t *)sc
->sc_mem
+ boff
);
262 memcpy(__UNVOLATILE(buf
), from
, len
);
264 bus_dmamap_sync(msc
->sc_dmat
, msc
->sc_dmam
, boff
, len
,
265 BUS_DMASYNC_PREWRITE
);
269 * Copy data from memory.
272 le_copyfrombuf(struct lance_softc
*sc
, void *to
, int boff
, int len
)
274 struct le_elb_softc
*msc
= (struct le_elb_softc
*)sc
;
275 volatile void *buf
= (void *)((uint8_t *)sc
->sc_mem
+ boff
);
277 bus_dmamap_sync(msc
->sc_dmat
, msc
->sc_dmam
, boff
, len
,
278 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
280 memcpy(to
, __UNVOLATILE(buf
), len
);
287 le_zerobuf(struct lance_softc
*sc
, int boff
, int len
)
289 struct le_elb_softc
*msc
= (struct le_elb_softc
*)sc
;
290 volatile void *buf
= (void *)((uint8_t *)sc
->sc_mem
+ boff
);
292 memset(__UNVOLATILE(buf
), 0, len
);
294 bus_dmamap_sync(msc
->sc_dmat
, msc
->sc_dmam
, boff
, len
,
295 BUS_DMASYNC_PREWRITE
);