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