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