1/*	$NetBSD: if_sm_gxio.c,v 1.9 2010/08/28 04:33:00 kiyohara Exp $ */
2/*
3 * Copyright (C) 2005, 2006 WIDE Project and SOUM Corporation.
4 * All rights reserved.
5 *
6 * Written by Takashi Kiyohara and Susumu Miki for WIDE Project and SOUM
7 * Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the name of SOUM Corporation
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT and SOUM CORPORATION ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT AND SOUM CORPORATION
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33/*-
34 * Copyright (c) 1997 The NetBSD Foundation, Inc.
35 * All rights reserved.
36 *
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
39 * NASA Ames Research Center.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 *    notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 *    notice, this list of conditions and the following disclaimer in the
48 *    documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 */
62
63#include <sys/cdefs.h>
64__KERNEL_RCSID(0, "$NetBSD: if_sm_gxio.c,v 1.9 2010/08/28 04:33:00 kiyohara Exp $");
65
66#include <sys/param.h>
67#include <sys/device.h>
68#include <sys/errno.h>
69
70#include <sys/systm.h>
71#include <sys/mbuf.h>
72#include <sys/socket.h>
73#include <sys/ioctl.h>
74#include <sys/syslog.h>
75#include <sys/select.h>
76
77#include <net/if.h>
78#include <net/if_dl.h>
79#include <net/if_ether.h>
80#include <net/if_media.h>
81
82#include <machine/intr.h>
83#include <sys/bus.h>
84
85#include <dev/mii/mii.h>
86#include <dev/mii/miivar.h>
87
88#include <dev/ic/smc91cxxreg.h>
89#include <dev/ic/smc91cxxvar.h>
90
91#include <evbarm/gumstix/gumstixvar.h>
92
93#include "locators.h"
94
95
96static int sm_gxio_match(device_t, struct cfdata *, void *);
97static void sm_gxio_attach(device_t, device_t, void *);
98
99static int ether_serial_digit = 1;
100
101struct sm_gxio_softc {
102	struct	smc91cxx_softc sc_smc;
103	void	*sc_ih;
104};
105
106CFATTACH_DECL(sm_gxio, sizeof(struct sm_gxio_softc),
107    sm_gxio_match, sm_gxio_attach, NULL, NULL);
108
109
110/* ARGSUSED */
111static int
112sm_gxio_match(device_t parent, struct cfdata *match, void *aux)
113{
114	struct gxio_attach_args *gxa = aux;
115	bus_space_tag_t iot = gxa->gxa_iot;
116	bus_space_handle_t ioh;
117	uint16_t tmp;
118	int rv = 0;
119	extern const char *smc91cxx_idstrs[];
120
121	/* Disallow wildcarded values. */
122	if (gxa->gxa_addr == GXIOCF_ADDR_DEFAULT)
123		return 0;
124	if (gxa->gxa_gpirq == GXIOCF_GPIRQ_DEFAULT)
125		return 0;
126
127	if (bus_space_map(iot, gxa->gxa_addr, SMC_IOSIZE, 0, &ioh) != 0)
128		return 0;
129
130	/* Check that high byte of BANK_SELECT is what we expect. */
131	tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
132	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
133		goto out;
134
135	/*
136	 * Switch to bank 0 and perform the test again.
137	 * XXX INVASIVE!
138	 */
139	bus_space_write_2(iot, ioh, BANK_SELECT_REG_W, 0);
140	tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
141	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
142		goto out;
143
144	/*
145	 * Check for a recognized chip id.
146	 * XXX INVASIVE!
147	 */
148	bus_space_write_2(iot, ioh, BANK_SELECT_REG_W, 3);
149	tmp = bus_space_read_2(iot, ioh, REVISION_REG_W);
150	if (smc91cxx_idstrs[RR_ID(tmp)] == NULL)
151		goto out;
152
153	if (ether_serial_digit > 15)
154		goto out;
155	/*
156	 * Assume we have an SMC91Cxx.
157	 */
158
159	rv = 1;
160
161 out:
162	bus_space_unmap(iot, ioh, SMC_IOSIZE);
163	return rv;
164}
165
166/* ARGSUSED */
167static void
168sm_gxio_attach(device_t parent, device_t self, void *aux)
169{
170	struct sm_gxio_softc *gsc = device_private(self);
171	struct smc91cxx_softc *sc = &gsc->sc_smc;
172	struct gxio_attach_args *gxa = aux;
173	bus_space_handle_t ioh;
174	uint8_t myea[ETHER_ADDR_LEN];
175
176	aprint_normal("\n");
177	aprint_naive("\n");
178
179	KASSERT(system_serial_high != 0 || system_serial_low != 0);
180
181	/* Map i/o space. */
182	if (bus_space_map(gxa->gxa_iot, gxa->gxa_addr, SMC_IOSIZE, 0, &ioh))
183		panic("sm_gxio_attach: can't map i/o space");
184
185	sc->sc_bst = gxa->gxa_iot;
186	sc->sc_bsh = ioh;
187
188	/* should always be enabled */
189	sc->sc_flags |= SMC_FLAGS_ENABLED;
190
191	myea[0] = ((system_serial_high >> 8) & 0xfe) | 0x02;
192	myea[1] = system_serial_high;
193	myea[2] = system_serial_low >> 24;
194	myea[3] = system_serial_low >> 16;
195	myea[4] = system_serial_low >> 8;
196	myea[5] = (system_serial_low & 0xc0) |
197	    (1 << 4) | ((ether_serial_digit++) & 0x0f);
198	smc91cxx_attach(sc, myea);
199
200	/* Establish the interrupt handler. */
201	gsc->sc_ih = gxio_intr_establish(gxa->gxa_sc,
202	    gxa->gxa_gpirq, IST_EDGE_RISING, IPL_NET, smc91cxx_intr, sc);
203
204	if (gsc->sc_ih == NULL)
205		aprint_error_dev(self,
206		    "couldn't establish interrupt handler\n");
207}
208