1 /* $NetBSD: if_mbe_g2.c,v 1.3 2003/02/05 12:03:55 tsutsui 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 2003/02/05 12:03:55 tsutsui 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, u_int8_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 u_int8_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(iot, ioh, enaddr) 156 bus_space_tag_t iot; 157 bus_space_handle_t ioh; 158 u_int8_t *enaddr; 159 { 160 u_int8_t eeprom[FE_EEPROM_SIZE]; 161 162 /* Read the chip type */ 163 if ((bus_space_read_1(iot, ioh, FE_DLCR7) & FE_D7_IDENT) != 164 FE_D7_IDENT_86967) { 165 #ifdef LANA_DEBUG 166 printf("mbe_g2_detect: unknown chip type\n"); 167 #endif 168 return (0); 169 } 170 171 memset(eeprom, 0, FE_EEPROM_SIZE); 172 173 /* Get our station address from EEPROM. */ 174 mb86965_read_eeprom(iot, ioh, eeprom); 175 memcpy(enaddr, eeprom, ETHER_ADDR_LEN); 176 177 #if LANA_DEBUG > 1 178 printf("Ethernet address: %s\n", ether_sprintf(enaddr)); 179 #endif 180 181 /* Make sure we got a valid station address. */ 182 if ((enaddr[0] & 0x03) != 0x00 || 183 (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) { 184 #ifdef LANA_DEBUG 185 printf("mbe_g2_detect: invalid ethernet address\n"); 186 #endif 187 return (0); 188 } 189 190 return (1); 191 } 192 193 void 194 mbe_g2_attach(parent, self, aux) 195 struct device *parent, *self; 196 void *aux; 197 { 198 struct g2bus_attach_args *ga = aux; 199 struct mbe_g2_softc *isc = (struct mbe_g2_softc *)self; 200 struct mb86960_softc *sc = &isc->sc_mb86960; 201 bus_space_tag_t memt = &mbe_g2_dbs; 202 bus_space_handle_t memh; 203 u_int8_t myea[ETHER_ADDR_LEN]; 204 205 memcpy(memt, ga->ga_memt, sizeof(struct dreamcast_bus_space)); 206 g2bus_set_bus_mem_sparse(memt); 207 208 /* Map i/o ports. */ 209 if (bus_space_map(memt, 0x00600400, LANA_NPORTS, 0, &memh)) { 210 printf(": can't map i/o space\n"); 211 return; 212 } 213 214 sc->sc_bst = memt; 215 sc->sc_bsh = memh; 216 217 /* Determine the card type. */ 218 if (mbe_g2_detect(memt, memh, myea) == 0) { 219 printf(": where did the card go?!\n"); 220 panic("unknown card"); 221 } 222 223 printf(": Sega LAN-Adapter Ethernet\n"); 224 225 /* This interface is always enabled. */ 226 sc->sc_stat |= FE_STAT_ENABLED; 227 228 /* The LAN-Adapter uses 8 bit bus mode and slow SRAM. */ 229 sc->sc_flags |= FE_FLAGS_SBW_BYTE | FE_FLAGS_SRAM_150ns; 230 231 /* 232 * Do generic MB86960 attach. 233 */ 234 mb86960_attach(sc, myea); 235 236 mb86960_config(sc, NULL, 0, 0); 237 238 sysasic_intr_establish(SYSASIC_EVENT_8BIT, IPL_NET, mb86960_intr, sc); 239 } 240