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