Home | History | Annotate | Line # | Download | only in g2
if_mbe_g2.c revision 1.8
      1  1.8   dyoung /*	$NetBSD: if_mbe_g2.c,v 1.8 2011/07/19 15:52:30 dyoung Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*
      4  1.1  tsutsui  * Copyright (c) 2002 Christian Groessler
      5  1.1  tsutsui  * All rights reserved.
      6  1.1  tsutsui  *
      7  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsutsui  * modification, are permitted provided that the following conditions
      9  1.1  tsutsui  * are met:
     10  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsutsui  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  tsutsui  *    derived from this software without specific prior written permission.
     17  1.1  tsutsui  *
     18  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  tsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.1  tsutsui  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.1  tsutsui  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.1  tsutsui  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.1  tsutsui  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  tsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  tsutsui  * SUCH DAMAGE.
     29  1.1  tsutsui  */
     30  1.1  tsutsui 
     31  1.1  tsutsui /*
     32  1.1  tsutsui  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
     33  1.1  tsutsui  *
     34  1.1  tsutsui  * This software may be used, modified, copied, distributed, and sold, in
     35  1.1  tsutsui  * both source and binary form provided that the above copyright, these
     36  1.1  tsutsui  * terms and the following disclaimer are retained.  The name of the author
     37  1.1  tsutsui  * and/or the contributor may not be used to endorse or promote products
     38  1.1  tsutsui  * derived from this software without specific prior written permission.
     39  1.1  tsutsui  *
     40  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
     41  1.1  tsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     42  1.1  tsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     43  1.1  tsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
     44  1.1  tsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     45  1.1  tsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     46  1.1  tsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
     47  1.1  tsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     48  1.1  tsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     49  1.1  tsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     50  1.1  tsutsui  * SUCH DAMAGE.
     51  1.1  tsutsui  */
     52  1.1  tsutsui 
     53  1.1  tsutsui /*
     54  1.1  tsutsui  * Portions copyright (C) 1993, David Greenman.	 This software may be used,
     55  1.1  tsutsui  * modified, copied, distributed, and sold, in both source and binary form
     56  1.1  tsutsui  * provided that the above copyright and these terms are retained.  Under no
     57  1.1  tsutsui  * circumstances is the author responsible for the proper functioning of this
     58  1.1  tsutsui  * software, nor does the author assume any responsibility for damages
     59  1.1  tsutsui  * incurred with its use.
     60  1.1  tsutsui  */
     61  1.1  tsutsui 
     62  1.1  tsutsui /*
     63  1.1  tsutsui  * Driver for Sega LAN Adapter (HIT-0300)
     64  1.1  tsutsui  */
     65  1.1  tsutsui 
     66  1.1  tsutsui #include <sys/cdefs.h>
     67  1.8   dyoung __KERNEL_RCSID(0, "$NetBSD: if_mbe_g2.c,v 1.8 2011/07/19 15:52:30 dyoung Exp $");
     68  1.1  tsutsui 
     69  1.1  tsutsui #include <sys/param.h>
     70  1.1  tsutsui #include <sys/systm.h>
     71  1.1  tsutsui #include <sys/device.h>
     72  1.8   dyoung #include <sys/bus.h>
     73  1.1  tsutsui 
     74  1.1  tsutsui #include <net/if.h>
     75  1.1  tsutsui #include <net/if_ether.h>
     76  1.1  tsutsui #include <net/if_media.h>
     77  1.1  tsutsui 
     78  1.1  tsutsui #include <machine/intr.h>
     79  1.1  tsutsui #include <machine/sysasicvar.h>
     80  1.1  tsutsui #include <machine/cpu.h>
     81  1.1  tsutsui 
     82  1.1  tsutsui #include <dev/ic/mb86960reg.h>
     83  1.1  tsutsui #include <dev/ic/mb86960var.h>
     84  1.1  tsutsui 
     85  1.1  tsutsui #include <dreamcast/dev/g2/g2busvar.h>
     86  1.1  tsutsui 
     87  1.1  tsutsui 
     88  1.7  tsutsui int	mbe_g2_match(device_t, cfdata_t, void *);
     89  1.7  tsutsui void	mbe_g2_attach(device_t, device_t, void *);
     90  1.4  tsutsui static int mbe_g2_detect(bus_space_tag_t, bus_space_handle_t, uint8_t *);
     91  1.1  tsutsui 
     92  1.1  tsutsui struct mbe_g2_softc {
     93  1.1  tsutsui 	struct	mb86960_softc sc_mb86960;	/* real "mb86960" softc */
     94  1.1  tsutsui };
     95  1.1  tsutsui 
     96  1.7  tsutsui CFATTACH_DECL_NEW(mbe_g2bus, sizeof(struct mbe_g2_softc),
     97  1.1  tsutsui     mbe_g2_match, mbe_g2_attach, NULL, NULL);
     98  1.1  tsutsui 
     99  1.1  tsutsui #define LANA_NPORTS (0x20 * 4)
    100  1.1  tsutsui 
    101  1.7  tsutsui #ifdef LANA_DEBUG
    102  1.7  tsutsui #define DPRINTF	printf
    103  1.7  tsutsui #else
    104  1.7  tsutsui #define DPRINTF	while (/* CONSTCOND */0) printf
    105  1.7  tsutsui #endif
    106  1.7  tsutsui 
    107  1.1  tsutsui static struct dreamcast_bus_space mbe_g2_dbs;
    108  1.1  tsutsui 
    109  1.1  tsutsui /*
    110  1.1  tsutsui  * Determine if the device is present.
    111  1.1  tsutsui  */
    112  1.1  tsutsui int
    113  1.7  tsutsui mbe_g2_match(device_t parent, cfdata_t cf, void *aux)
    114  1.1  tsutsui {
    115  1.1  tsutsui 	struct g2bus_attach_args *ga = aux;
    116  1.1  tsutsui 	bus_space_handle_t memh;
    117  1.1  tsutsui 	struct dreamcast_bus_space dbs;
    118  1.1  tsutsui 	bus_space_tag_t memt = &dbs;
    119  1.1  tsutsui 	static int lanafound;
    120  1.1  tsutsui 	int rv;
    121  1.4  tsutsui 	uint8_t myea[ETHER_ADDR_LEN];
    122  1.1  tsutsui 
    123  1.1  tsutsui 	if (lanafound)
    124  1.4  tsutsui 		return 0;
    125  1.1  tsutsui 
    126  1.1  tsutsui 	if (strcmp("mbe", cf->cf_name))
    127  1.4  tsutsui 		return 0;
    128  1.1  tsutsui 
    129  1.1  tsutsui 	memcpy(memt, ga->ga_memt, sizeof(struct dreamcast_bus_space));
    130  1.1  tsutsui 	g2bus_set_bus_mem_sparse(memt);
    131  1.1  tsutsui 
    132  1.1  tsutsui 	/* Map i/o ports. */
    133  1.1  tsutsui 	if (bus_space_map(memt, 0x00600400, LANA_NPORTS, 0, &memh)) {
    134  1.7  tsutsui 		DPRINTF("%s: couldn't map iospace 0x%x\n",
    135  1.7  tsutsui 		    __func__, 0x00600400);
    136  1.4  tsutsui 		return 0;
    137  1.1  tsutsui 	}
    138  1.1  tsutsui 
    139  1.1  tsutsui 	rv = 0;
    140  1.1  tsutsui 	if (mbe_g2_detect(memt, memh, myea) == 0) {
    141  1.7  tsutsui 		DPRINTF("%s: LAN Adapter detection failed\n", __func__);
    142  1.1  tsutsui 		goto out;
    143  1.1  tsutsui 	}
    144  1.1  tsutsui 
    145  1.1  tsutsui 	rv = 1;
    146  1.1  tsutsui 	lanafound = 1;
    147  1.1  tsutsui  out:
    148  1.1  tsutsui 	bus_space_unmap(memt, memh, LANA_NPORTS);
    149  1.4  tsutsui 	return rv;
    150  1.1  tsutsui }
    151  1.1  tsutsui 
    152  1.1  tsutsui 
    153  1.1  tsutsui /*
    154  1.1  tsutsui  * Determine type and ethernet address.
    155  1.1  tsutsui  */
    156  1.1  tsutsui static int
    157  1.4  tsutsui mbe_g2_detect(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t *enaddr)
    158  1.1  tsutsui {
    159  1.4  tsutsui 	uint8_t eeprom[FE_EEPROM_SIZE];
    160  1.1  tsutsui 
    161  1.2  tsutsui 	/* Read the chip type */
    162  1.2  tsutsui 	if ((bus_space_read_1(iot, ioh, FE_DLCR7) & FE_D7_IDENT) !=
    163  1.2  tsutsui 	    FE_D7_IDENT_86967) {
    164  1.7  tsutsui 		DPRINTF("%s: unknown chip type\n", __func__);
    165  1.4  tsutsui 		return 0;
    166  1.2  tsutsui 	}
    167  1.2  tsutsui 
    168  1.1  tsutsui 	memset(eeprom, 0, FE_EEPROM_SIZE);
    169  1.1  tsutsui 
    170  1.1  tsutsui 	/* Get our station address from EEPROM. */
    171  1.1  tsutsui 	mb86965_read_eeprom(iot, ioh, eeprom);
    172  1.1  tsutsui 	memcpy(enaddr, eeprom, ETHER_ADDR_LEN);
    173  1.1  tsutsui 
    174  1.7  tsutsui 	DPRINTF("Ethernet address: %s\n", ether_sprintf(enaddr));
    175  1.1  tsutsui 
    176  1.1  tsutsui 	/* Make sure we got a valid station address. */
    177  1.1  tsutsui 	if ((enaddr[0] & 0x03) != 0x00 ||
    178  1.1  tsutsui 	    (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) {
    179  1.7  tsutsui 		DPRINTF("%s: invalid ethernet address\n", __func__);
    180  1.4  tsutsui 		return 0;
    181  1.1  tsutsui 	}
    182  1.1  tsutsui 
    183  1.4  tsutsui 	return 1;
    184  1.1  tsutsui }
    185  1.1  tsutsui 
    186  1.1  tsutsui void
    187  1.7  tsutsui mbe_g2_attach(device_t parent, device_t self, void *aux)
    188  1.1  tsutsui {
    189  1.7  tsutsui 	struct mbe_g2_softc *isc = device_private(self);
    190  1.7  tsutsui 	struct mb86960_softc *sc = &isc->sc_mb86960;
    191  1.1  tsutsui 	struct g2bus_attach_args *ga = aux;
    192  1.1  tsutsui 	bus_space_tag_t memt = &mbe_g2_dbs;
    193  1.1  tsutsui 	bus_space_handle_t memh;
    194  1.4  tsutsui 	uint8_t myea[ETHER_ADDR_LEN];
    195  1.1  tsutsui 
    196  1.7  tsutsui 	sc->sc_dev = self;
    197  1.7  tsutsui 
    198  1.1  tsutsui 	memcpy(memt, ga->ga_memt, sizeof(struct dreamcast_bus_space));
    199  1.1  tsutsui 	g2bus_set_bus_mem_sparse(memt);
    200  1.1  tsutsui 
    201  1.1  tsutsui 	/* Map i/o ports. */
    202  1.1  tsutsui 	if (bus_space_map(memt, 0x00600400, LANA_NPORTS, 0, &memh)) {
    203  1.7  tsutsui 		aprint_error(": can't map i/o space\n");
    204  1.1  tsutsui 		return;
    205  1.1  tsutsui 	}
    206  1.1  tsutsui 
    207  1.1  tsutsui 	sc->sc_bst = memt;
    208  1.1  tsutsui 	sc->sc_bsh = memh;
    209  1.1  tsutsui 
    210  1.1  tsutsui 	/* Determine the card type. */
    211  1.1  tsutsui 	if (mbe_g2_detect(memt, memh, myea) == 0) {
    212  1.7  tsutsui 		aprint_error(": where did the card go?!\n");
    213  1.1  tsutsui 		panic("unknown card");
    214  1.1  tsutsui 	}
    215  1.1  tsutsui 
    216  1.7  tsutsui 	aprint_normal(": Sega LAN-Adapter Ethernet\n");
    217  1.1  tsutsui 
    218  1.1  tsutsui 	/* This interface is always enabled. */
    219  1.1  tsutsui 	sc->sc_stat |= FE_STAT_ENABLED;
    220  1.1  tsutsui 
    221  1.3  tsutsui 	/* The LAN-Adapter uses 8 bit bus mode and slow SRAM. */
    222  1.3  tsutsui 	sc->sc_flags |= FE_FLAGS_SBW_BYTE | FE_FLAGS_SRAM_150ns;
    223  1.1  tsutsui 
    224  1.1  tsutsui 	/*
    225  1.1  tsutsui 	 * Do generic MB86960 attach.
    226  1.1  tsutsui 	 */
    227  1.1  tsutsui 	mb86960_attach(sc, myea);
    228  1.1  tsutsui 
    229  1.1  tsutsui 	mb86960_config(sc, NULL, 0, 0);
    230  1.1  tsutsui 
    231  1.6  tsutsui 	sysasic_intr_establish(SYSASIC_EVENT_8BIT, IPL_NET, SYSASIC_IRL11,
    232  1.6  tsutsui 	    mb86960_intr, sc);
    233  1.1  tsutsui }
    234