Home | History | Annotate | Line # | Download | only in gumstix
if_sm_gxio.c revision 1.1.4.2
      1  1.1.4.2  ad /*	$NetBSD: if_sm_gxio.c,v 1.1.4.2 2006/11/18 21:29:10 ad Exp $ */
      2  1.1.4.2  ad /*
      3  1.1.4.2  ad  * Copyright (C) 2005, 2006 WIDE Project and SOUM Corporation.
      4  1.1.4.2  ad  * All rights reserved.
      5  1.1.4.2  ad  *
      6  1.1.4.2  ad  * Written by Takashi Kiyohara and Susumu Miki for WIDE Project and SOUM
      7  1.1.4.2  ad  * Corporation.
      8  1.1.4.2  ad  *
      9  1.1.4.2  ad  * Redistribution and use in source and binary forms, with or without
     10  1.1.4.2  ad  * modification, are permitted provided that the following conditions
     11  1.1.4.2  ad  * are met:
     12  1.1.4.2  ad  * 1. Redistributions of source code must retain the above copyright
     13  1.1.4.2  ad  *    notice, this list of conditions and the following disclaimer.
     14  1.1.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
     16  1.1.4.2  ad  *    documentation and/or other materials provided with the distribution.
     17  1.1.4.2  ad  * 3. Neither the name of the project nor the name of SOUM Corporation
     18  1.1.4.2  ad  *    may be used to endorse or promote products derived from this software
     19  1.1.4.2  ad  *    without specific prior written permission.
     20  1.1.4.2  ad  *
     21  1.1.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE PROJECT and SOUM CORPORATION ``AS IS''
     22  1.1.4.2  ad  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  1.1.4.2  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.1.4.2  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT AND SOUM CORPORATION
     25  1.1.4.2  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.1.4.2  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  1.1.4.2  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.1.4.2  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.1.4.2  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.1.4.2  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.1.4.2  ad  * POSSIBILITY OF SUCH DAMAGE.
     32  1.1.4.2  ad  */
     33  1.1.4.2  ad /*-
     34  1.1.4.2  ad  * Copyright (c) 1997 The NetBSD Foundation, Inc.
     35  1.1.4.2  ad  * All rights reserved.
     36  1.1.4.2  ad  *
     37  1.1.4.2  ad  * This code is derived from software contributed to The NetBSD Foundation
     38  1.1.4.2  ad  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     39  1.1.4.2  ad  * NASA Ames Research Center.
     40  1.1.4.2  ad  *
     41  1.1.4.2  ad  * Redistribution and use in source and binary forms, with or without
     42  1.1.4.2  ad  * modification, are permitted provided that the following conditions
     43  1.1.4.2  ad  * are met:
     44  1.1.4.2  ad  * 1. Redistributions of source code must retain the above copyright
     45  1.1.4.2  ad  *    notice, this list of conditions and the following disclaimer.
     46  1.1.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     47  1.1.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
     48  1.1.4.2  ad  *    documentation and/or other materials provided with the distribution.
     49  1.1.4.2  ad  * 3. All advertising materials mentioning features or use of this software
     50  1.1.4.2  ad  *    must display the following acknowledgement:
     51  1.1.4.2  ad  *	This product includes software developed by the NetBSD
     52  1.1.4.2  ad  *	Foundation, Inc. and its contributors.
     53  1.1.4.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     54  1.1.4.2  ad  *    contributors may be used to endorse or promote products derived
     55  1.1.4.2  ad  *    from this software without specific prior written permission.
     56  1.1.4.2  ad  *
     57  1.1.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     58  1.1.4.2  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     59  1.1.4.2  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     60  1.1.4.2  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     61  1.1.4.2  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     62  1.1.4.2  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     63  1.1.4.2  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     64  1.1.4.2  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     65  1.1.4.2  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     66  1.1.4.2  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     67  1.1.4.2  ad  * POSSIBILITY OF SUCH DAMAGE.
     68  1.1.4.2  ad  */
     69  1.1.4.2  ad 
     70  1.1.4.2  ad #include <sys/cdefs.h>
     71  1.1.4.2  ad __KERNEL_RCSID(0, "$NetBSD: if_sm_gxio.c,v 1.1.4.2 2006/11/18 21:29:10 ad Exp $");
     72  1.1.4.2  ad 
     73  1.1.4.2  ad #include <sys/param.h>
     74  1.1.4.2  ad #include <sys/device.h>
     75  1.1.4.2  ad #include <sys/errno.h>
     76  1.1.4.2  ad 
     77  1.1.4.2  ad #include <sys/systm.h>
     78  1.1.4.2  ad #include <sys/mbuf.h>
     79  1.1.4.2  ad #include <sys/socket.h>
     80  1.1.4.2  ad #include <sys/ioctl.h>
     81  1.1.4.2  ad #include <sys/syslog.h>
     82  1.1.4.2  ad #include <sys/select.h>
     83  1.1.4.2  ad 
     84  1.1.4.2  ad #include <net/if.h>
     85  1.1.4.2  ad #include <net/if_dl.h>
     86  1.1.4.2  ad #include <net/if_ether.h>
     87  1.1.4.2  ad #include <net/if_media.h>
     88  1.1.4.2  ad 
     89  1.1.4.2  ad #include <machine/intr.h>
     90  1.1.4.2  ad #include <machine/bus.h>
     91  1.1.4.2  ad 
     92  1.1.4.2  ad #include <dev/mii/mii.h>
     93  1.1.4.2  ad #include <dev/mii/miivar.h>
     94  1.1.4.2  ad 
     95  1.1.4.2  ad #include <dev/ic/smc91cxxreg.h>
     96  1.1.4.2  ad #include <dev/ic/smc91cxxvar.h>
     97  1.1.4.2  ad 
     98  1.1.4.2  ad #include <evbarm/gumstix/gumstixvar.h>
     99  1.1.4.2  ad 
    100  1.1.4.2  ad #include "locators.h"
    101  1.1.4.2  ad 
    102  1.1.4.2  ad 
    103  1.1.4.2  ad static int sm_gxio_match(struct device *, struct cfdata *, void *);
    104  1.1.4.2  ad static void sm_gxio_attach(struct device *, struct device *, void *);
    105  1.1.4.2  ad 
    106  1.1.4.2  ad static int ether_serial_digit = 1;
    107  1.1.4.2  ad 
    108  1.1.4.2  ad struct sm_gxio_softc {
    109  1.1.4.2  ad 	struct	smc91cxx_softc sc_smc;
    110  1.1.4.2  ad 	void	*sc_ih;
    111  1.1.4.2  ad };
    112  1.1.4.2  ad 
    113  1.1.4.2  ad CFATTACH_DECL(sm_gxio, sizeof(struct sm_gxio_softc),
    114  1.1.4.2  ad     sm_gxio_match, sm_gxio_attach, NULL, NULL);
    115  1.1.4.2  ad 
    116  1.1.4.2  ad 
    117  1.1.4.2  ad static int
    118  1.1.4.2  ad sm_gxio_match(struct device *parent, struct cfdata *match, void *aux)
    119  1.1.4.2  ad {
    120  1.1.4.2  ad 	struct gxio_attach_args *gxa = aux;
    121  1.1.4.2  ad 	bus_space_tag_t iot = gxa->gxa_iot;
    122  1.1.4.2  ad 	bus_space_handle_t ioh;
    123  1.1.4.2  ad 	u_int16_t tmp;
    124  1.1.4.2  ad 	int rv = 0;
    125  1.1.4.2  ad 	extern const char *smc91cxx_idstrs[];
    126  1.1.4.2  ad 
    127  1.1.4.2  ad 	/* Disallow wildcarded values. */
    128  1.1.4.2  ad 	if (gxa->gxa_addr == GXIOCF_ADDR_DEFAULT)
    129  1.1.4.2  ad 		return (0);
    130  1.1.4.2  ad 	if (gxa->gxa_gpirq == GXIOCF_GPIRQ_DEFAULT)
    131  1.1.4.2  ad 		return (0);
    132  1.1.4.2  ad 
    133  1.1.4.2  ad 	if (bus_space_map(iot, gxa->gxa_addr, SMC_IOSIZE, 0, &ioh) != 0)
    134  1.1.4.2  ad 		return (0);
    135  1.1.4.2  ad 
    136  1.1.4.2  ad 	/* Check that high byte of BANK_SELECT is what we expect. */
    137  1.1.4.2  ad 	tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
    138  1.1.4.2  ad 	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
    139  1.1.4.2  ad 		goto out;
    140  1.1.4.2  ad 
    141  1.1.4.2  ad 	/*
    142  1.1.4.2  ad 	 * Switch to bank 0 and perform the test again.
    143  1.1.4.2  ad 	 * XXX INVASIVE!
    144  1.1.4.2  ad 	 */
    145  1.1.4.2  ad 	bus_space_write_1(iot, ioh, BANK_SELECT_REG_W, 0);
    146  1.1.4.2  ad 	tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
    147  1.1.4.2  ad 	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
    148  1.1.4.2  ad 		goto out;
    149  1.1.4.2  ad 
    150  1.1.4.2  ad 	/*
    151  1.1.4.2  ad 	 * Check for a recognized chip id.
    152  1.1.4.2  ad 	 * XXX INVASIVE!
    153  1.1.4.2  ad 	 */
    154  1.1.4.2  ad 	bus_space_write_1(iot, ioh, BANK_SELECT_REG_W, 3);
    155  1.1.4.2  ad 	tmp = bus_space_read_2(iot, ioh, REVISION_REG_W);
    156  1.1.4.2  ad 	if (smc91cxx_idstrs[RR_ID(tmp)] == NULL)
    157  1.1.4.2  ad 		goto out;
    158  1.1.4.2  ad 
    159  1.1.4.2  ad 	if (ether_serial_digit > 15)
    160  1.1.4.2  ad 		goto out;
    161  1.1.4.2  ad 	/*
    162  1.1.4.2  ad 	 * Assume we have an SMC91Cxx.
    163  1.1.4.2  ad 	 */
    164  1.1.4.2  ad 
    165  1.1.4.2  ad 	rv = 1;
    166  1.1.4.2  ad 
    167  1.1.4.2  ad  out:
    168  1.1.4.2  ad 	bus_space_unmap(iot, ioh, SMC_IOSIZE);
    169  1.1.4.2  ad 	return (rv);
    170  1.1.4.2  ad }
    171  1.1.4.2  ad 
    172  1.1.4.2  ad void
    173  1.1.4.2  ad sm_gxio_attach(struct device *parent, struct device *self, void *aux)
    174  1.1.4.2  ad {
    175  1.1.4.2  ad 	struct sm_gxio_softc *gsc = (struct sm_gxio_softc *)self;
    176  1.1.4.2  ad 	struct smc91cxx_softc *sc = &gsc->sc_smc;
    177  1.1.4.2  ad 	struct gxio_attach_args *gxa = aux;
    178  1.1.4.2  ad 	bus_space_handle_t ioh;
    179  1.1.4.2  ad 	u_int8_t myea[ETHER_ADDR_LEN];
    180  1.1.4.2  ad 
    181  1.1.4.2  ad 	printf("\n");
    182  1.1.4.2  ad 
    183  1.1.4.2  ad 	/* Map i/o space. */
    184  1.1.4.2  ad 	if (bus_space_map(gxa->gxa_iot, gxa->gxa_addr, SMC_IOSIZE, 0, &ioh))
    185  1.1.4.2  ad 		panic("sm_gxio_attach: can't map i/o space");
    186  1.1.4.2  ad 
    187  1.1.4.2  ad 	sc->sc_bst = gxa->gxa_iot;
    188  1.1.4.2  ad 	sc->sc_bsh = ioh;
    189  1.1.4.2  ad 
    190  1.1.4.2  ad 	/* should always be enabled */
    191  1.1.4.2  ad 	sc->sc_flags |= SMC_FLAGS_ENABLED;
    192  1.1.4.2  ad 
    193  1.1.4.2  ad 	if (system_serial_high != 0 || system_serial_low != 0) {
    194  1.1.4.2  ad 		myea[0] = ((system_serial_high >> 8) & 0xfe) | 0x02;
    195  1.1.4.2  ad 		myea[1] = system_serial_high;
    196  1.1.4.2  ad 		myea[2] = system_serial_low >> 24;
    197  1.1.4.2  ad 		myea[3] = system_serial_low >> 16;
    198  1.1.4.2  ad 		myea[4] = system_serial_low >> 8;
    199  1.1.4.2  ad 		myea[5] = (system_serial_low & 0xc0) |
    200  1.1.4.2  ad 		    (1 << 4) | ((ether_serial_digit++) & 0x0f);
    201  1.1.4.2  ad 		smc91cxx_attach(sc, myea);
    202  1.1.4.2  ad 	} else
    203  1.1.4.2  ad 		smc91cxx_attach(sc, NULL);
    204  1.1.4.2  ad 
    205  1.1.4.2  ad 	/* Establish the interrupt handler. */
    206  1.1.4.2  ad 	gsc->sc_ih = gxio_intr_establish(gxa->gxa_sc,
    207  1.1.4.2  ad 	    gxa->gxa_gpirq, IST_EDGE_RISING, IPL_NET, smc91cxx_intr, sc);
    208  1.1.4.2  ad 
    209  1.1.4.2  ad 	if (gsc->sc_ih == NULL)
    210  1.1.4.2  ad 		printf("%s: couldn't establish interrupt handler\n",
    211  1.1.4.2  ad 		    sc->sc_dev.dv_xname);
    212  1.1.4.2  ad }
    213