Home | History | Annotate | Line # | Download | only in dev
      1  1.12       rin /*	$NetBSD: le_elb.c,v 1.12 2022/05/29 10:45:05 rin 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  *
     19   1.1   hannken  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1   hannken  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1   hannken  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1   hannken  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1   hannken  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1   hannken  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1   hannken  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1   hannken  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1   hannken  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1   hannken  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1   hannken  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1   hannken  */
     31   1.2     lukem 
     32   1.2     lukem #include <sys/cdefs.h>
     33  1.12       rin __KERNEL_RCSID(0, "$NetBSD: le_elb.c,v 1.12 2022/05/29 10:45:05 rin Exp $");
     34   1.1   hannken 
     35   1.1   hannken #include <sys/param.h>
     36   1.1   hannken #include <sys/conf.h>
     37   1.1   hannken #include <sys/device.h>
     38   1.1   hannken #include <sys/systm.h>
     39   1.1   hannken 
     40   1.1   hannken #include <uvm/uvm_extern.h>
     41   1.1   hannken 
     42   1.9    dyoung #include <sys/bus.h>
     43   1.1   hannken 
     44   1.1   hannken #include <net/if.h>
     45   1.1   hannken #include <net/if_ether.h>
     46   1.1   hannken #include <net/if_media.h>
     47   1.1   hannken 
     48   1.1   hannken #include <dev/ic/lancereg.h>
     49   1.1   hannken #include <dev/ic/lancevar.h>
     50   1.1   hannken #include <dev/ic/am79900reg.h>
     51   1.1   hannken #include <dev/ic/am79900var.h>
     52   1.1   hannken 
     53   1.1   hannken #include <evbppc/explora/dev/elbvar.h>
     54   1.1   hannken 
     55   1.1   hannken #define LE_MEMSIZE	16384
     56   1.1   hannken #define LE_RDP		0x10	/* Indirect data register. */
     57   1.1   hannken #define LE_RAP		0x14	/* Indirect address register. */
     58   1.1   hannken #define LE_NPORTS	32
     59   1.1   hannken 
     60   1.1   hannken struct le_elb_softc {
     61   1.1   hannken 	struct am79900_softc sc_am79900;
     62   1.1   hannken 	bus_dma_tag_t sc_dmat;
     63   1.1   hannken 	bus_dmamap_t sc_dmam;
     64   1.1   hannken 	bus_space_tag_t sc_iot;
     65   1.1   hannken 	bus_space_handle_t sc_ioh;
     66   1.1   hannken 	void *sc_ih;
     67   1.1   hannken };
     68   1.1   hannken 
     69   1.7   tsutsui static int	le_elb_probe(device_t, cfdata_t, void *);
     70   1.7   tsutsui static void	le_elb_attach(device_t, device_t, void *);
     71   1.7   tsutsui static uint16_t le_rdcsr(struct lance_softc *, uint16_t);
     72   1.7   tsutsui static void	le_wrcsr(struct lance_softc *, uint16_t, uint16_t);
     73   1.1   hannken static void	le_copytodesc(struct lance_softc *, void *, int, int);
     74   1.1   hannken static void	le_copyfromdesc(struct lance_softc *, void *, int, int);
     75   1.3   hannken static void	le_copytobuf(struct lance_softc *, void *, int, int);
     76   1.3   hannken static void	le_copyfrombuf(struct lance_softc *, void *, int, int);
     77   1.3   hannken static void	le_zerobuf(struct lance_softc *, int, int);
     78   1.1   hannken 
     79   1.7   tsutsui CFATTACH_DECL_NEW(le_elb, sizeof(struct le_elb_softc),
     80   1.1   hannken     le_elb_probe, le_elb_attach, NULL, NULL);
     81   1.1   hannken 
     82   1.1   hannken int
     83   1.7   tsutsui le_elb_probe(device_t parent, cfdata_t cf, void *aux)
     84   1.1   hannken {
     85   1.1   hannken 	struct elb_attach_args *oaa = aux;
     86   1.1   hannken 
     87   1.1   hannken 	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
     88   1.1   hannken 		return 0;
     89   1.1   hannken 
     90   1.1   hannken 	return (1);
     91   1.1   hannken }
     92   1.1   hannken 
     93   1.1   hannken void
     94   1.7   tsutsui le_elb_attach(device_t parent, device_t self, void *aux)
     95   1.1   hannken {
     96   1.7   tsutsui 	struct le_elb_softc *msc = device_private(self);
     97   1.1   hannken 	struct lance_softc *sc = &msc->sc_am79900.lsc;
     98   1.1   hannken 	struct elb_attach_args *eaa = aux;
     99   1.1   hannken 	bus_dma_segment_t seg;
    100   1.1   hannken 	int i, rseg;
    101   1.1   hannken 
    102   1.7   tsutsui 	sc->sc_dev = self;
    103   1.7   tsutsui 	aprint_normal("\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.7   tsutsui 		aprint_error_dev(self, "couldn't allocate memory for card\n");
    119  1.12       rin 		goto bad_bsunmap;
    120   1.1   hannken 	}
    121   1.1   hannken 	if (bus_dmamem_map(msc->sc_dmat, &seg, rseg, LE_MEMSIZE,
    122   1.5  christos 	    (void **)&sc->sc_mem,
    123   1.1   hannken 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    124   1.7   tsutsui 		aprint_error_dev(self, "couldn't map memory for card\n");
    125  1.12       rin 		goto bad_free;
    126   1.1   hannken 	}
    127   1.1   hannken 
    128   1.1   hannken 	/*
    129   1.1   hannken 	 * Create and load the DMA map for the DMA area.
    130   1.1   hannken 	 */
    131   1.1   hannken 	if (bus_dmamap_create(msc->sc_dmat, LE_MEMSIZE, 1,
    132   1.1   hannken 	    LE_MEMSIZE, 0, BUS_DMA_NOWAIT, &msc->sc_dmam)) {
    133   1.7   tsutsui 		aprint_error_dev(self, "couldn't create DMA map\n");
    134  1.12       rin 		goto bad_unmap;
    135   1.1   hannken 	}
    136   1.1   hannken 	if (bus_dmamap_load(msc->sc_dmat, msc->sc_dmam,
    137   1.1   hannken 	    sc->sc_mem, LE_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
    138  1.11    andvar 		aprint_error_dev(self, "couldn't load DMA map\n");
    139  1.12       rin 		goto bad_destroy;
    140   1.1   hannken 	}
    141   1.1   hannken 
    142   1.1   hannken 	/*
    143   1.1   hannken 	 * This is magic -- DMA doesn't work without address
    144   1.1   hannken 	 * bit 30 set to one.
    145   1.1   hannken 	 */
    146   1.1   hannken 	sc->sc_addr = 0x40000000 | msc->sc_dmam->dm_segs[0].ds_addr;
    147   1.1   hannken 	sc->sc_memsize = LE_MEMSIZE;
    148   1.1   hannken 
    149   1.1   hannken 	sc->sc_copytodesc = le_copytodesc;
    150   1.1   hannken 	sc->sc_copyfromdesc = le_copyfromdesc;
    151   1.3   hannken 	sc->sc_copytobuf = le_copytobuf;
    152   1.3   hannken 	sc->sc_copyfrombuf = le_copyfrombuf;
    153   1.3   hannken 	sc->sc_zerobuf = le_zerobuf;
    154   1.1   hannken 
    155   1.1   hannken 	sc->sc_rdcsr = le_rdcsr;
    156   1.1   hannken 	sc->sc_wrcsr = le_wrcsr;
    157   1.1   hannken 
    158   1.7   tsutsui 	aprint_normal("%s", device_xname(self));
    159   1.1   hannken 
    160   1.1   hannken 	/* Save the MAC address. */
    161   1.1   hannken 	for (i = 0; i < 3; i++) {
    162   1.7   tsutsui 		sc->sc_enaddr[i * 2] = le_rdcsr(sc, 12 + i);
    163   1.7   tsutsui 		sc->sc_enaddr[i * 2 + 1] = le_rdcsr(sc, 12 + i) >> 8;
    164   1.1   hannken 	}
    165   1.1   hannken 
    166   1.1   hannken 	am79900_config(&msc->sc_am79900);
    167   1.1   hannken 
    168   1.1   hannken 	/* Chip is stopped. Set "software style" to 32-bit. */
    169   1.1   hannken 	le_wrcsr(sc, LE_CSR58, 2);
    170   1.1   hannken 
    171  1.10       rin 	intr_establish_xname(eaa->elb_irq, IST_LEVEL, IPL_NET, am79900_intr,
    172  1.10       rin 	    sc, device_xname(self));
    173  1.12       rin 
    174  1.12       rin 	return;
    175  1.12       rin 
    176  1.12       rin  bad_destroy:
    177  1.12       rin 	bus_dmamap_destroy(msc->sc_dmat, msc->sc_dmam);
    178  1.12       rin  bad_unmap:
    179  1.12       rin 	bus_dmamem_unmap(msc->sc_dmat, sc->sc_mem, LE_MEMSIZE);
    180  1.12       rin  bad_free:
    181  1.12       rin 	bus_dmamem_free(msc->sc_dmat, &seg, rseg);
    182  1.12       rin  bad_bsunmap:
    183  1.12       rin 	bus_space_unmap(msc->sc_iot, msc->sc_ioh, LE_NPORTS);
    184   1.1   hannken }
    185   1.1   hannken 
    186   1.1   hannken /*
    187   1.1   hannken  * Read from an indirect CSR.
    188   1.1   hannken  */
    189   1.7   tsutsui static uint16_t
    190   1.7   tsutsui le_rdcsr(struct lance_softc *sc, uint16_t reg)
    191   1.1   hannken {
    192   1.1   hannken 	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
    193   1.1   hannken 	bus_space_tag_t iot = lesc->sc_iot;
    194   1.1   hannken 	bus_space_handle_t ioh = lesc->sc_ioh;
    195   1.7   tsutsui 	uint16_t val;
    196   1.1   hannken 
    197   1.1   hannken 	bus_space_write_4(iot, ioh, LE_RAP, reg);
    198   1.1   hannken 	val = bus_space_read_4(iot, ioh, LE_RDP);
    199   1.1   hannken 
    200   1.7   tsutsui 	return val;
    201   1.1   hannken }
    202   1.1   hannken 
    203   1.1   hannken /*
    204   1.1   hannken  * Write to an indirect CSR.
    205   1.1   hannken  */
    206   1.1   hannken static void
    207   1.7   tsutsui le_wrcsr(struct lance_softc *sc, uint16_t reg, uint16_t val)
    208   1.1   hannken {
    209   1.1   hannken 	struct le_elb_softc *lesc = (struct le_elb_softc *)sc;
    210   1.1   hannken 	bus_space_tag_t iot = lesc->sc_iot;
    211   1.1   hannken 	bus_space_handle_t ioh = lesc->sc_ioh;
    212   1.1   hannken 
    213   1.1   hannken 	bus_space_write_4(iot, ioh, LE_RAP, reg);
    214   1.1   hannken 	bus_space_write_4(iot, ioh, LE_RDP, val);
    215   1.1   hannken }
    216   1.1   hannken 
    217   1.1   hannken /*
    218   1.1   hannken  * Copy data to memory and swap bytes.
    219   1.1   hannken  */
    220   1.1   hannken static void
    221   1.1   hannken le_copytodesc(struct lance_softc *sc, void *from, int boff, int len)
    222   1.1   hannken {
    223   1.3   hannken 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    224   1.7   tsutsui 	volatile uint32_t *src = from;
    225   1.7   tsutsui 	volatile uint32_t *dst = (uint32_t *)((uint8_t *)sc->sc_mem + boff);
    226   1.3   hannken 	int todo = len;
    227   1.1   hannken 
    228   1.1   hannken 	/* XXX lance_setladrf should be modified to use u_int32_t instead.
    229   1.1   hannken 	 * The init block contains u_int16_t values that require
    230   1.1   hannken 	 * special swapping.
    231   1.1   hannken 	 */
    232   1.1   hannken 	if (boff == LE_INITADDR(sc) && len == sizeof(struct leinit)) {
    233   1.1   hannken 		src[3] = (src[3] >> 16) | (src[3] << 16);
    234   1.1   hannken 		src[4] = (src[4] >> 16) | (src[4] << 16);
    235   1.1   hannken 	}
    236   1.1   hannken 
    237   1.7   tsutsui 	todo /= sizeof(uint32_t);
    238   1.3   hannken 	while (todo-- > 0)
    239   1.1   hannken 		*dst++ = bswap32(*src++);
    240   1.3   hannken 
    241   1.3   hannken 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    242   1.3   hannken 	    BUS_DMASYNC_PREWRITE);
    243   1.1   hannken }
    244   1.1   hannken 
    245   1.1   hannken /*
    246   1.1   hannken  * Copy data from memory and swap bytes.
    247   1.1   hannken  */
    248   1.1   hannken static void
    249   1.1   hannken le_copyfromdesc(struct lance_softc *sc, void *to, int boff, int len)
    250   1.1   hannken {
    251   1.3   hannken 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    252   1.7   tsutsui 	volatile uint32_t *src = (uint32_t *)((uint8_t *)sc->sc_mem + boff);
    253   1.7   tsutsui 	volatile uint32_t *dst = to;
    254   1.1   hannken 
    255   1.3   hannken 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    256   1.3   hannken 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    257   1.3   hannken 
    258   1.7   tsutsui 	len /= sizeof(uint32_t);
    259   1.1   hannken 	while (len-- > 0)
    260   1.1   hannken 		*dst++ = bswap32(*src++);
    261   1.3   hannken }
    262   1.3   hannken 
    263   1.3   hannken /*
    264   1.3   hannken  * Copy data to memory.
    265   1.3   hannken  */
    266   1.3   hannken static void
    267   1.3   hannken le_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
    268   1.3   hannken {
    269   1.3   hannken 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    270   1.7   tsutsui 	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
    271   1.3   hannken 
    272   1.6        he 	memcpy(__UNVOLATILE(buf), from, len);
    273   1.3   hannken 
    274   1.3   hannken 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    275   1.3   hannken 	    BUS_DMASYNC_PREWRITE);
    276   1.3   hannken }
    277   1.3   hannken 
    278   1.3   hannken /*
    279   1.3   hannken  * Copy data from memory.
    280   1.3   hannken  */
    281   1.3   hannken static void
    282   1.3   hannken le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
    283   1.3   hannken {
    284   1.3   hannken 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    285   1.7   tsutsui 	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
    286   1.3   hannken 
    287   1.3   hannken 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    288   1.3   hannken 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    289   1.3   hannken 
    290   1.6        he 	memcpy(to, __UNVOLATILE(buf), len);
    291   1.3   hannken }
    292   1.3   hannken 
    293   1.3   hannken /*
    294   1.3   hannken  * Zero memory.
    295   1.3   hannken  */
    296   1.3   hannken static void
    297   1.3   hannken le_zerobuf(struct lance_softc *sc, int boff, int len)
    298   1.3   hannken {
    299   1.3   hannken 	struct le_elb_softc *msc = (struct le_elb_softc *)sc;
    300   1.7   tsutsui 	volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff);
    301   1.3   hannken 
    302   1.6        he 	memset(__UNVOLATILE(buf), 0, len);
    303   1.3   hannken 
    304   1.3   hannken 	bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len,
    305   1.3   hannken 	    BUS_DMASYNC_PREWRITE);
    306   1.1   hannken }
    307