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