Home | History | Annotate | Line # | Download | only in dev
le_elb.c revision 1.10
      1 /*	$NetBSD: le_elb.c,v 1.10 2021/03/02 12:01:02 rin 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  *
     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.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: le_elb.c,v 1.10 2021/03/02 12:01:02 rin Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/conf.h>
     37 #include <sys/device.h>
     38 #include <sys/systm.h>
     39 
     40 #include <uvm/uvm_extern.h>
     41 
     42 #include <sys/bus.h>
     43 
     44 #include <net/if.h>
     45 #include <net/if_ether.h>
     46 #include <net/if_media.h>
     47 
     48 #include <dev/ic/lancereg.h>
     49 #include <dev/ic/lancevar.h>
     50 #include <dev/ic/am79900reg.h>
     51 #include <dev/ic/am79900var.h>
     52 
     53 #include <evbppc/explora/dev/elbvar.h>
     54 
     55 #define LE_MEMSIZE	16384
     56 #define LE_RDP		0x10	/* Indirect data register. */
     57 #define LE_RAP		0x14	/* Indirect address register. */
     58 #define LE_NPORTS	32
     59 
     60 struct le_elb_softc {
     61 	struct am79900_softc sc_am79900;
     62 	bus_dma_tag_t sc_dmat;
     63 	bus_dmamap_t sc_dmam;
     64 	bus_space_tag_t sc_iot;
     65 	bus_space_handle_t sc_ioh;
     66 	void *sc_ih;
     67 };
     68 
     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);
     78 
     79 CFATTACH_DECL_NEW(le_elb, sizeof(struct le_elb_softc),
     80     le_elb_probe, le_elb_attach, NULL, NULL);
     81 
     82 int
     83 le_elb_probe(device_t parent, cfdata_t cf, void *aux)
     84 {
     85 	struct elb_attach_args *oaa = aux;
     86 
     87 	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
     88 		return 0;
     89 
     90 	return (1);
     91 }
     92 
     93 void
     94 le_elb_attach(device_t parent, device_t self, void *aux)
     95 {
     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;
    100 	int i, rseg;
    101 
    102 	sc->sc_dev = self;
    103 	aprint_normal("\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 		aprint_error_dev(self, "couldn't allocate memory for card\n");
    119 		return;
    120 	}
    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");
    125 		return;
    126 	}
    127 
    128 	/*
    129 	 * Create and load the DMA map for the DMA area.
    130 	 */
    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);
    135 		return;
    136 	}
    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);
    141 		return;
    142 	}
    143 
    144 	/*
    145 	 * This is magic -- DMA doesn't work without address
    146 	 * bit 30 set to one.
    147 	 */
    148 	sc->sc_addr = 0x40000000 | msc->sc_dmam->dm_segs[0].ds_addr;
    149 	sc->sc_memsize = LE_MEMSIZE;
    150 
    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;
    156 
    157 	sc->sc_rdcsr = le_rdcsr;
    158 	sc->sc_wrcsr = le_wrcsr;
    159 
    160 	aprint_normal("%s", device_xname(self));
    161 
    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;
    166 	}
    167 
    168 	am79900_config(&msc->sc_am79900);
    169 
    170 	/* Chip is stopped. Set "software style" to 32-bit. */
    171 	le_wrcsr(sc, LE_CSR58, 2);
    172 
    173 	intr_establish_xname(eaa->elb_irq, IST_LEVEL, IPL_NET, am79900_intr,
    174 	    sc, device_xname(self));
    175 }
    176 
    177 /*
    178  * Read from an indirect CSR.
    179  */
    180 static uint16_t
    181 le_rdcsr(struct lance_softc *sc, uint16_t reg)
    182 {
    183 	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
    184 	bus_space_tag_t iot = lesc->sc_iot;
    185 	bus_space_handle_t ioh = lesc->sc_ioh;
    186 	uint16_t val;
    187 
    188 	bus_space_write_4(iot, ioh, LE_RAP, reg);
    189 	val = bus_space_read_4(iot, ioh, LE_RDP);
    190 
    191 	return val;
    192 }
    193 
    194 /*
    195  * Write to an indirect CSR.
    196  */
    197 static void
    198 le_wrcsr(struct lance_softc *sc, uint16_t reg, uint16_t val)
    199 {
    200 	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
    201 	bus_space_tag_t iot = lesc->sc_iot;
    202 	bus_space_handle_t ioh = lesc->sc_ioh;
    203 
    204 	bus_space_write_4(iot, ioh, LE_RAP, reg);
    205 	bus_space_write_4(iot, ioh, LE_RDP, val);
    206 }
    207 
    208 /*
    209  * Copy data to memory and swap bytes.
    210  */
    211 static void
    212 le_copytodesc(struct lance_softc *sc, void *from, int boff, int len)
    213 {
    214 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    215 	volatile uint32_t *src = from;
    216 	volatile uint32_t *dst = (uint32_t *)((uint8_t *)sc->sc_mem + boff);
    217 	int todo = len;
    218 
    219 	/* XXX lance_setladrf should be modified to use u_int32_t instead.
    220 	 * The init block contains u_int16_t values that require
    221 	 * special swapping.
    222 	 */
    223 	if (boff == LE_INITADDR(sc) && len == sizeof(struct leinit)) {
    224 		src[3] = (src[3] >> 16) | (src[3] << 16);
    225 		src[4] = (src[4] >> 16) | (src[4] << 16);
    226 	}
    227 
    228 	todo /= sizeof(uint32_t);
    229 	while (todo-- > 0)
    230 		*dst++ = bswap32(*src++);
    231 
    232 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    233 	    BUS_DMASYNC_PREWRITE);
    234 }
    235 
    236 /*
    237  * Copy data from memory and swap bytes.
    238  */
    239 static void
    240 le_copyfromdesc(struct lance_softc *sc, void *to, int boff, int len)
    241 {
    242 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    243 	volatile uint32_t *src = (uint32_t *)((uint8_t *)sc->sc_mem + boff);
    244 	volatile uint32_t *dst = to;
    245 
    246 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    247 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    248 
    249 	len /= sizeof(uint32_t);
    250 	while (len-- > 0)
    251 		*dst++ = bswap32(*src++);
    252 }
    253 
    254 /*
    255  * Copy data to memory.
    256  */
    257 static void
    258 le_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
    259 {
    260 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    261 	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
    262 
    263 	memcpy(__UNVOLATILE(buf), from, len);
    264 
    265 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    266 	    BUS_DMASYNC_PREWRITE);
    267 }
    268 
    269 /*
    270  * Copy data from memory.
    271  */
    272 static void
    273 le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
    274 {
    275 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    276 	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
    277 
    278 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    279 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    280 
    281 	memcpy(to, __UNVOLATILE(buf), len);
    282 }
    283 
    284 /*
    285  * Zero memory.
    286  */
    287 static void
    288 le_zerobuf(struct lance_softc *sc, int boff, int len)
    289 {
    290 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    291 	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
    292 
    293 	memset(__UNVOLATILE(buf), 0, len);
    294 
    295 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    296 	    BUS_DMASYNC_PREWRITE);
    297 }
    298