if_mbe_g2.c revision 1.3.12.1 1 1.3.12.1 yamt /* $NetBSD: if_mbe_g2.c,v 1.3.12.1 2005/03/19 08:32:52 yamt 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.3.12.1 yamt __KERNEL_RCSID(0, "$NetBSD: if_mbe_g2.c,v 1.3.12.1 2005/03/19 08:32:52 yamt 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.1 tsutsui
73 1.1 tsutsui #include <net/if.h>
74 1.1 tsutsui #include <net/if_ether.h>
75 1.1 tsutsui #include <net/if_media.h>
76 1.1 tsutsui
77 1.1 tsutsui #include <machine/bus.h>
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.1 tsutsui int mbe_g2_match(struct device *, struct cfdata *, void *);
89 1.1 tsutsui void mbe_g2_attach(struct device *, struct device *, void *);
90 1.3.12.1 yamt 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.1 tsutsui CFATTACH_DECL(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.1 tsutsui static struct dreamcast_bus_space mbe_g2_dbs;
102 1.1 tsutsui
103 1.1 tsutsui /*
104 1.1 tsutsui * Determine if the device is present.
105 1.1 tsutsui */
106 1.1 tsutsui int
107 1.1 tsutsui mbe_g2_match(struct device *parent, struct cfdata *cf, void *aux)
108 1.1 tsutsui {
109 1.1 tsutsui struct g2bus_attach_args *ga = aux;
110 1.1 tsutsui bus_space_handle_t memh;
111 1.1 tsutsui struct dreamcast_bus_space dbs;
112 1.1 tsutsui bus_space_tag_t memt = &dbs;
113 1.1 tsutsui static int lanafound;
114 1.1 tsutsui int rv;
115 1.3.12.1 yamt uint8_t myea[ETHER_ADDR_LEN];
116 1.1 tsutsui
117 1.1 tsutsui if (lanafound)
118 1.3.12.1 yamt return 0;
119 1.1 tsutsui
120 1.1 tsutsui if (strcmp("mbe", cf->cf_name))
121 1.3.12.1 yamt return 0;
122 1.1 tsutsui
123 1.1 tsutsui memcpy(memt, ga->ga_memt, sizeof(struct dreamcast_bus_space));
124 1.1 tsutsui g2bus_set_bus_mem_sparse(memt);
125 1.1 tsutsui
126 1.1 tsutsui /* Map i/o ports. */
127 1.1 tsutsui if (bus_space_map(memt, 0x00600400, LANA_NPORTS, 0, &memh)) {
128 1.1 tsutsui #ifdef LANA_DEBUG
129 1.1 tsutsui printf("mbe_g2_match: couldn't map iospace 0x%x\n",
130 1.3.12.1 yamt 0x00600400);
131 1.1 tsutsui #endif
132 1.3.12.1 yamt return 0;
133 1.1 tsutsui }
134 1.1 tsutsui
135 1.1 tsutsui rv = 0;
136 1.1 tsutsui if (mbe_g2_detect(memt, memh, myea) == 0) {
137 1.1 tsutsui #ifdef LANA_DEBUG
138 1.1 tsutsui printf("mbe_g2_match: LAN Adapter detection failed\n");
139 1.1 tsutsui #endif
140 1.1 tsutsui goto out;
141 1.1 tsutsui }
142 1.1 tsutsui
143 1.1 tsutsui rv = 1;
144 1.1 tsutsui lanafound = 1;
145 1.1 tsutsui out:
146 1.1 tsutsui bus_space_unmap(memt, memh, LANA_NPORTS);
147 1.3.12.1 yamt return rv;
148 1.1 tsutsui }
149 1.1 tsutsui
150 1.1 tsutsui
151 1.1 tsutsui /*
152 1.1 tsutsui * Determine type and ethernet address.
153 1.1 tsutsui */
154 1.1 tsutsui static int
155 1.3.12.1 yamt mbe_g2_detect(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t *enaddr)
156 1.1 tsutsui {
157 1.3.12.1 yamt uint8_t eeprom[FE_EEPROM_SIZE];
158 1.1 tsutsui
159 1.2 tsutsui /* Read the chip type */
160 1.2 tsutsui if ((bus_space_read_1(iot, ioh, FE_DLCR7) & FE_D7_IDENT) !=
161 1.2 tsutsui FE_D7_IDENT_86967) {
162 1.2 tsutsui #ifdef LANA_DEBUG
163 1.2 tsutsui printf("mbe_g2_detect: unknown chip type\n");
164 1.2 tsutsui #endif
165 1.3.12.1 yamt 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.1 tsutsui #if LANA_DEBUG > 1
175 1.1 tsutsui printf("Ethernet address: %s\n", ether_sprintf(enaddr));
176 1.1 tsutsui #endif
177 1.1 tsutsui
178 1.1 tsutsui /* Make sure we got a valid station address. */
179 1.1 tsutsui if ((enaddr[0] & 0x03) != 0x00 ||
180 1.1 tsutsui (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) {
181 1.1 tsutsui #ifdef LANA_DEBUG
182 1.1 tsutsui printf("mbe_g2_detect: invalid ethernet address\n");
183 1.1 tsutsui #endif
184 1.3.12.1 yamt return 0;
185 1.1 tsutsui }
186 1.1 tsutsui
187 1.3.12.1 yamt return 1;
188 1.1 tsutsui }
189 1.1 tsutsui
190 1.1 tsutsui void
191 1.3.12.1 yamt mbe_g2_attach(struct device *parent, struct device *self, void *aux)
192 1.1 tsutsui {
193 1.1 tsutsui struct g2bus_attach_args *ga = aux;
194 1.1 tsutsui struct mbe_g2_softc *isc = (struct mbe_g2_softc *)self;
195 1.1 tsutsui struct mb86960_softc *sc = &isc->sc_mb86960;
196 1.1 tsutsui bus_space_tag_t memt = &mbe_g2_dbs;
197 1.1 tsutsui bus_space_handle_t memh;
198 1.3.12.1 yamt uint8_t myea[ETHER_ADDR_LEN];
199 1.1 tsutsui
200 1.1 tsutsui memcpy(memt, ga->ga_memt, sizeof(struct dreamcast_bus_space));
201 1.1 tsutsui g2bus_set_bus_mem_sparse(memt);
202 1.1 tsutsui
203 1.1 tsutsui /* Map i/o ports. */
204 1.1 tsutsui if (bus_space_map(memt, 0x00600400, LANA_NPORTS, 0, &memh)) {
205 1.1 tsutsui printf(": can't map i/o space\n");
206 1.1 tsutsui return;
207 1.1 tsutsui }
208 1.1 tsutsui
209 1.1 tsutsui sc->sc_bst = memt;
210 1.1 tsutsui sc->sc_bsh = memh;
211 1.1 tsutsui
212 1.1 tsutsui /* Determine the card type. */
213 1.1 tsutsui if (mbe_g2_detect(memt, memh, myea) == 0) {
214 1.1 tsutsui printf(": where did the card go?!\n");
215 1.1 tsutsui panic("unknown card");
216 1.1 tsutsui }
217 1.1 tsutsui
218 1.1 tsutsui printf(": Sega LAN-Adapter Ethernet\n");
219 1.1 tsutsui
220 1.1 tsutsui /* This interface is always enabled. */
221 1.1 tsutsui sc->sc_stat |= FE_STAT_ENABLED;
222 1.1 tsutsui
223 1.3 tsutsui /* The LAN-Adapter uses 8 bit bus mode and slow SRAM. */
224 1.3 tsutsui sc->sc_flags |= FE_FLAGS_SBW_BYTE | FE_FLAGS_SRAM_150ns;
225 1.1 tsutsui
226 1.1 tsutsui /*
227 1.1 tsutsui * Do generic MB86960 attach.
228 1.1 tsutsui */
229 1.1 tsutsui mb86960_attach(sc, myea);
230 1.1 tsutsui
231 1.1 tsutsui mb86960_config(sc, NULL, 0, 0);
232 1.1 tsutsui
233 1.1 tsutsui sysasic_intr_establish(SYSASIC_EVENT_8BIT, IPL_NET, mb86960_intr, sc);
234 1.1 tsutsui }
235