Home | History | Annotate | Line # | Download | only in obio
if_le.c revision 1.11.4.2
      1  1.11.4.2      yamt /*	$NetBSD: if_le.c,v 1.11.4.2 2010/03/11 15:02:42 yamt Exp $	*/
      2       1.1       wdk 
      3       1.1       wdk /*-
      4       1.1       wdk  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
      5       1.1       wdk  * All rights reserved.
      6       1.1       wdk  *
      7       1.1       wdk  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1       wdk  * by Adam Glass, Gordon W. Ross and Wayne Knowles
      9       1.1       wdk  *
     10       1.1       wdk  * Redistribution and use in source and binary forms, with or without
     11       1.1       wdk  * modification, are permitted provided that the following conditions
     12       1.1       wdk  * are met:
     13       1.1       wdk  * 1. Redistributions of source code must retain the above copyright
     14       1.1       wdk  *    notice, this list of conditions and the following disclaimer.
     15       1.1       wdk  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       wdk  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       wdk  *    documentation and/or other materials provided with the distribution.
     18       1.1       wdk  *
     19       1.1       wdk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1       wdk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1       wdk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1       wdk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1       wdk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1       wdk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1       wdk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1       wdk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1       wdk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1       wdk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1       wdk  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1       wdk  */
     31       1.6     lukem 
     32       1.6     lukem #include <sys/cdefs.h>
     33  1.11.4.2      yamt __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.11.4.2 2010/03/11 15:02:42 yamt Exp $");
     34       1.1       wdk 
     35       1.1       wdk #include "opt_inet.h"
     36       1.1       wdk 
     37       1.1       wdk #include <sys/param.h>
     38       1.1       wdk #include <sys/systm.h>
     39       1.1       wdk #include <sys/mbuf.h>
     40       1.1       wdk #include <sys/syslog.h>
     41       1.1       wdk #include <sys/socket.h>
     42       1.1       wdk #include <sys/device.h>
     43       1.1       wdk 
     44       1.1       wdk #include <net/if.h>
     45       1.1       wdk #include <net/if_ether.h>
     46       1.1       wdk #include <net/if_media.h>
     47       1.1       wdk 
     48       1.1       wdk #ifdef INET
     49       1.1       wdk #include <netinet/in.h>
     50       1.1       wdk #include <netinet/if_inarp.h>
     51       1.1       wdk #endif
     52       1.1       wdk 
     53       1.1       wdk #include <machine/cpu.h>
     54       1.1       wdk #include <machine/mainboard.h>
     55       1.1       wdk #include <machine/autoconf.h>
     56       1.1       wdk #include <machine/bus.h>
     57       1.1       wdk 
     58       1.1       wdk #include <dev/ic/lancereg.h>
     59       1.1       wdk #include <dev/ic/lancevar.h>
     60       1.1       wdk #include <dev/ic/am7990reg.h>
     61       1.1       wdk #include <dev/ic/am7990var.h>
     62       1.1       wdk 
     63       1.1       wdk /*
     64       1.1       wdk  * LANCE registers.
     65       1.1       wdk  */
     66       1.1       wdk 
     67       1.1       wdk #define	LEREG1_RDP	2	/* Offset to LANCE data register */
     68       1.1       wdk #define	LEREG1_RAP	6	/* Offset to LANCE address register */
     69       1.1       wdk 
     70       1.1       wdk /*
     71       1.1       wdk  * Ethernet software status per interface.
     72       1.1       wdk  * The real stuff is in dev/ic/am7990var.h
     73       1.1       wdk  */
     74       1.1       wdk struct	le_softc {
     75       1.1       wdk 	struct am7990_softc	sc_am7990;	/* glue to MI code */
     76       1.1       wdk 	bus_space_tag_t		sc_bustag;
     77       1.1       wdk 	bus_dma_tag_t		sc_dmatag;
     78       1.1       wdk 	bus_space_handle_t	sc_reg;		/* LANCE registers */
     79       1.1       wdk         bus_dmamap_t		sc_dmamap;
     80       1.1       wdk 	struct evcnt		sc_intrcnt;	/* Interrupt event counter */
     81       1.1       wdk };
     82       1.1       wdk 
     83      1.11   tsutsui static int	le_match(device_t, cfdata_t, void *);
     84      1.11   tsutsui static void	le_attach(device_t, device_t, void *);
     85       1.1       wdk 
     86      1.11   tsutsui CFATTACH_DECL_NEW(le, sizeof(struct le_softc),
     87       1.5   thorpej     le_match, le_attach, NULL, NULL);
     88       1.1       wdk 
     89       1.7       chs static int le_attached;
     90       1.7       chs 
     91       1.3       mrg #if defined(_KERNEL_OPT)
     92       1.1       wdk #include "opt_ddb.h"
     93       1.1       wdk #endif
     94       1.1       wdk 
     95       1.1       wdk #ifdef DDB
     96       1.1       wdk #define	integrate
     97       1.1       wdk #define hide
     98       1.1       wdk #else
     99       1.9     perry #define	integrate	static inline
    100       1.1       wdk #define hide		static
    101       1.1       wdk #endif
    102       1.1       wdk 
    103      1.11   tsutsui int le_intr(void *);
    104       1.2       wdk 
    105      1.11   tsutsui hide void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
    106      1.11   tsutsui hide uint16_t lerdcsr(struct lance_softc *, uint16_t);
    107       1.1       wdk 
    108       1.1       wdk hide void
    109      1.11   tsutsui lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
    110       1.1       wdk {
    111       1.1       wdk 	struct le_softc *lesc = (struct le_softc *)sc;
    112       1.1       wdk 
    113       1.1       wdk 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    114       1.1       wdk 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
    115       1.1       wdk }
    116       1.1       wdk 
    117      1.11   tsutsui hide uint16_t
    118      1.11   tsutsui lerdcsr(struct lance_softc *sc, uint16_t port)
    119       1.1       wdk {
    120       1.1       wdk 	struct le_softc *lesc = (struct le_softc *)sc;
    121       1.1       wdk 
    122       1.1       wdk 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
    123       1.1       wdk 	return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
    124       1.1       wdk }
    125       1.1       wdk 
    126       1.1       wdk int
    127      1.11   tsutsui le_match(device_t parent, cfdata_t cf, void *aux)
    128       1.1       wdk {
    129       1.1       wdk 	struct confargs *ca = aux;
    130       1.1       wdk 	int addr;
    131       1.1       wdk 
    132       1.1       wdk 	if (strcmp(ca->ca_name, "le"))
    133       1.1       wdk 		return 0;
    134       1.1       wdk 
    135       1.7       chs 	if (le_attached)
    136       1.1       wdk 		return 0;
    137       1.1       wdk 
    138       1.7       chs 	addr = LANCE_PORT;
    139       1.1       wdk 	if (badaddr((void *)addr, 1))
    140       1.1       wdk 		return 0;
    141       1.1       wdk 
    142       1.1       wdk 	return 1;
    143       1.1       wdk }
    144       1.1       wdk 
    145       1.1       wdk #define	LE_MEMSIZE	(32*1024)
    146       1.1       wdk 
    147       1.1       wdk void
    148      1.11   tsutsui le_attach(device_t parent, device_t self, void *aux)
    149       1.1       wdk {
    150      1.11   tsutsui 	struct le_softc *lesc = device_private(self);
    151       1.1       wdk 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    152       1.1       wdk 	struct confargs *ca = aux;
    153       1.1       wdk 
    154       1.1       wdk 	bus_dma_tag_t dmat;
    155       1.1       wdk 	bus_dma_segment_t seg;
    156       1.1       wdk 	int rseg;
    157      1.11   tsutsui 	uint8_t *id;
    158       1.1       wdk 	int i;
    159      1.10  christos 	void *kvaddr;
    160       1.1       wdk 
    161      1.11   tsutsui 	sc->sc_dev = self;
    162      1.11   tsutsui 	id = (uint8_t *)ETHER_ID;
    163       1.1       wdk 	lesc->sc_bustag = ca->ca_bustag;
    164       1.1       wdk 	dmat = lesc->sc_dmatag = ca->ca_dmatag;
    165       1.1       wdk 
    166       1.1       wdk 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
    167       1.1       wdk 			  8,	/* size */
    168       1.1       wdk 			  BUS_SPACE_MAP_LINEAR,
    169       1.1       wdk 			  &lesc->sc_reg) != 0) {
    170      1.11   tsutsui 		aprint_error(": cannot map registers\n");
    171       1.1       wdk 		return;
    172       1.1       wdk 	}
    173       1.1       wdk 
    174       1.1       wdk 	/*
    175       1.1       wdk 	 * Allocate a physically contiguous DMA area for the chip.
    176       1.1       wdk 	 */
    177       1.1       wdk 	if (bus_dmamem_alloc(dmat, LE_MEMSIZE, 0, 0, &seg, 1,
    178       1.1       wdk 			     &rseg, BUS_DMA_NOWAIT)) {
    179      1.11   tsutsui 		aprint_error(": can't allocate DMA area\n");
    180       1.1       wdk 		return;
    181       1.1       wdk 	}
    182       1.1       wdk 	/* Map pages into kernel memory */
    183       1.1       wdk 	if (bus_dmamem_map(dmat, &seg, rseg, LE_MEMSIZE,
    184       1.1       wdk 	    &kvaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    185      1.11   tsutsui 		aprint_error(": can't map DMA area\n");
    186       1.1       wdk 		bus_dmamem_free(dmat, &seg, rseg);
    187       1.1       wdk 		return;
    188       1.1       wdk 	}
    189       1.1       wdk 	/* Build DMA map so we can get physical address */
    190       1.1       wdk 	if (bus_dmamap_create(dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
    191       1.1       wdk 			      0, BUS_DMA_NOWAIT, &lesc->sc_dmamap)) {
    192      1.11   tsutsui 		aprint_error(": can't create DMA map\n");
    193       1.1       wdk 		goto bad;
    194       1.1       wdk 	}
    195       1.1       wdk 	if (bus_dmamap_load(dmat, lesc->sc_dmamap, kvaddr, LE_MEMSIZE,
    196       1.1       wdk 			    NULL, BUS_DMA_NOWAIT)) {
    197      1.11   tsutsui 		aprint_error(": can't load DMA map\n");
    198       1.1       wdk 		goto bad;
    199       1.1       wdk 	}
    200       1.1       wdk 
    201       1.1       wdk 	sc->sc_memsize = LE_MEMSIZE;	/* 16K Buffer space*/
    202      1.11   tsutsui 	sc->sc_mem  = (void *)MIPS_PHYS_TO_KSEG1(kvaddr);
    203       1.1       wdk 	sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
    204       1.1       wdk 
    205       1.1       wdk 	sc->sc_conf3 = LE_C3_BSWP;
    206       1.1       wdk 
    207       1.1       wdk 	/* Copy Ethernet hardware address from NVRAM */
    208      1.11   tsutsui 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    209      1.11   tsutsui 		sc->sc_enaddr[i] = id[i * 4 + 3];	/* XXX */
    210       1.1       wdk 
    211       1.1       wdk 	sc->sc_copytodesc = lance_copytobuf_contig;
    212       1.1       wdk 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    213       1.1       wdk 	sc->sc_copytobuf = lance_copytobuf_contig;
    214       1.1       wdk 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    215       1.1       wdk 	sc->sc_zerobuf = lance_zerobuf_contig;
    216       1.1       wdk 
    217       1.1       wdk 	sc->sc_rdcsr = lerdcsr;
    218       1.1       wdk 	sc->sc_wrcsr = lewrcsr;
    219       1.1       wdk 	sc->sc_hwinit = NULL;
    220       1.1       wdk 
    221       1.1       wdk 	evcnt_attach_dynamic(&lesc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    222      1.11   tsutsui 			     device_xname(self), "intr");
    223       1.2       wdk 	bus_intr_establish(lesc->sc_bustag, SYS_INTR_ETHER, 0, 0,
    224       1.2       wdk 			   le_intr, lesc);
    225       1.1       wdk 
    226       1.1       wdk 	am7990_config(&lesc->sc_am7990);
    227       1.1       wdk 	return;
    228       1.1       wdk 
    229       1.1       wdk bad:
    230       1.1       wdk 	bus_dmamem_unmap(dmat, kvaddr, LE_MEMSIZE);
    231       1.1       wdk 	bus_dmamem_free(dmat, &seg, rseg);
    232       1.1       wdk }
    233       1.1       wdk 
    234       1.1       wdk int
    235      1.11   tsutsui le_intr(void *arg)
    236       1.1       wdk {
    237       1.2       wdk 	struct le_softc *lesc = arg;
    238       1.1       wdk 
    239       1.2       wdk 	lesc->sc_intrcnt.ev_count++;
    240       1.2       wdk 	return am7990_intr(&lesc->sc_am7990);
    241       1.1       wdk }
    242