if_sm_gxio.c revision 1.5 1 1.5 martin /* $NetBSD: if_sm_gxio.c,v 1.5 2008/04/28 20:23:16 martin 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 *
50 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 1.1 kiyohara * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 1.1 kiyohara * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 1.1 kiyohara * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 1.1 kiyohara * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 1.1 kiyohara * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 1.1 kiyohara * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 1.1 kiyohara * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 1.1 kiyohara * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 1.1 kiyohara * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
61 1.1 kiyohara */
62 1.1 kiyohara
63 1.1 kiyohara #include <sys/cdefs.h>
64 1.5 martin __KERNEL_RCSID(0, "$NetBSD: if_sm_gxio.c,v 1.5 2008/04/28 20:23:16 martin Exp $");
65 1.1 kiyohara
66 1.1 kiyohara #include <sys/param.h>
67 1.1 kiyohara #include <sys/device.h>
68 1.1 kiyohara #include <sys/errno.h>
69 1.1 kiyohara
70 1.1 kiyohara #include <sys/systm.h>
71 1.1 kiyohara #include <sys/mbuf.h>
72 1.1 kiyohara #include <sys/socket.h>
73 1.1 kiyohara #include <sys/ioctl.h>
74 1.1 kiyohara #include <sys/syslog.h>
75 1.1 kiyohara #include <sys/select.h>
76 1.1 kiyohara
77 1.1 kiyohara #include <net/if.h>
78 1.1 kiyohara #include <net/if_dl.h>
79 1.1 kiyohara #include <net/if_ether.h>
80 1.1 kiyohara #include <net/if_media.h>
81 1.1 kiyohara
82 1.1 kiyohara #include <machine/intr.h>
83 1.1 kiyohara #include <machine/bus.h>
84 1.1 kiyohara
85 1.1 kiyohara #include <dev/mii/mii.h>
86 1.1 kiyohara #include <dev/mii/miivar.h>
87 1.1 kiyohara
88 1.1 kiyohara #include <dev/ic/smc91cxxreg.h>
89 1.1 kiyohara #include <dev/ic/smc91cxxvar.h>
90 1.1 kiyohara
91 1.1 kiyohara #include <evbarm/gumstix/gumstixvar.h>
92 1.1 kiyohara
93 1.1 kiyohara #include "locators.h"
94 1.1 kiyohara
95 1.1 kiyohara
96 1.1 kiyohara static int sm_gxio_match(struct device *, struct cfdata *, void *);
97 1.1 kiyohara static void sm_gxio_attach(struct device *, struct device *, void *);
98 1.1 kiyohara
99 1.1 kiyohara static int ether_serial_digit = 1;
100 1.1 kiyohara
101 1.1 kiyohara struct sm_gxio_softc {
102 1.1 kiyohara struct smc91cxx_softc sc_smc;
103 1.1 kiyohara void *sc_ih;
104 1.1 kiyohara };
105 1.1 kiyohara
106 1.1 kiyohara CFATTACH_DECL(sm_gxio, sizeof(struct sm_gxio_softc),
107 1.1 kiyohara sm_gxio_match, sm_gxio_attach, NULL, NULL);
108 1.1 kiyohara
109 1.1 kiyohara
110 1.3 kiyohara /* ARGSUSED */
111 1.1 kiyohara static int
112 1.1 kiyohara sm_gxio_match(struct device *parent, struct cfdata *match, void *aux)
113 1.1 kiyohara {
114 1.1 kiyohara struct gxio_attach_args *gxa = aux;
115 1.1 kiyohara bus_space_tag_t iot = gxa->gxa_iot;
116 1.1 kiyohara bus_space_handle_t ioh;
117 1.1 kiyohara u_int16_t tmp;
118 1.1 kiyohara int rv = 0;
119 1.1 kiyohara extern const char *smc91cxx_idstrs[];
120 1.1 kiyohara
121 1.1 kiyohara /* Disallow wildcarded values. */
122 1.1 kiyohara if (gxa->gxa_addr == GXIOCF_ADDR_DEFAULT)
123 1.1 kiyohara return (0);
124 1.1 kiyohara if (gxa->gxa_gpirq == GXIOCF_GPIRQ_DEFAULT)
125 1.1 kiyohara return (0);
126 1.1 kiyohara
127 1.1 kiyohara if (bus_space_map(iot, gxa->gxa_addr, SMC_IOSIZE, 0, &ioh) != 0)
128 1.1 kiyohara return (0);
129 1.1 kiyohara
130 1.1 kiyohara /* Check that high byte of BANK_SELECT is what we expect. */
131 1.1 kiyohara tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
132 1.1 kiyohara if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
133 1.1 kiyohara goto out;
134 1.1 kiyohara
135 1.1 kiyohara /*
136 1.1 kiyohara * Switch to bank 0 and perform the test again.
137 1.1 kiyohara * XXX INVASIVE!
138 1.1 kiyohara */
139 1.1 kiyohara bus_space_write_1(iot, ioh, BANK_SELECT_REG_W, 0);
140 1.1 kiyohara tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
141 1.1 kiyohara if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
142 1.1 kiyohara goto out;
143 1.1 kiyohara
144 1.1 kiyohara /*
145 1.1 kiyohara * Check for a recognized chip id.
146 1.1 kiyohara * XXX INVASIVE!
147 1.1 kiyohara */
148 1.1 kiyohara bus_space_write_1(iot, ioh, BANK_SELECT_REG_W, 3);
149 1.1 kiyohara tmp = bus_space_read_2(iot, ioh, REVISION_REG_W);
150 1.1 kiyohara if (smc91cxx_idstrs[RR_ID(tmp)] == NULL)
151 1.1 kiyohara goto out;
152 1.1 kiyohara
153 1.1 kiyohara if (ether_serial_digit > 15)
154 1.1 kiyohara goto out;
155 1.1 kiyohara /*
156 1.1 kiyohara * Assume we have an SMC91Cxx.
157 1.1 kiyohara */
158 1.1 kiyohara
159 1.1 kiyohara rv = 1;
160 1.1 kiyohara
161 1.1 kiyohara out:
162 1.1 kiyohara bus_space_unmap(iot, ioh, SMC_IOSIZE);
163 1.1 kiyohara return (rv);
164 1.1 kiyohara }
165 1.1 kiyohara
166 1.3 kiyohara /* ARGSUSED */
167 1.1 kiyohara void
168 1.1 kiyohara sm_gxio_attach(struct device *parent, struct device *self, void *aux)
169 1.1 kiyohara {
170 1.2 kiyohara struct sm_gxio_softc *gsc = device_private(self);
171 1.1 kiyohara struct smc91cxx_softc *sc = &gsc->sc_smc;
172 1.1 kiyohara struct gxio_attach_args *gxa = aux;
173 1.1 kiyohara bus_space_handle_t ioh;
174 1.1 kiyohara u_int8_t myea[ETHER_ADDR_LEN];
175 1.1 kiyohara
176 1.2 kiyohara aprint_normal("\n");
177 1.2 kiyohara aprint_naive("\n");
178 1.1 kiyohara
179 1.1 kiyohara /* Map i/o space. */
180 1.1 kiyohara if (bus_space_map(gxa->gxa_iot, gxa->gxa_addr, SMC_IOSIZE, 0, &ioh))
181 1.1 kiyohara panic("sm_gxio_attach: can't map i/o space");
182 1.1 kiyohara
183 1.1 kiyohara sc->sc_bst = gxa->gxa_iot;
184 1.1 kiyohara sc->sc_bsh = ioh;
185 1.1 kiyohara
186 1.1 kiyohara /* should always be enabled */
187 1.1 kiyohara sc->sc_flags |= SMC_FLAGS_ENABLED;
188 1.1 kiyohara
189 1.1 kiyohara if (system_serial_high != 0 || system_serial_low != 0) {
190 1.1 kiyohara myea[0] = ((system_serial_high >> 8) & 0xfe) | 0x02;
191 1.1 kiyohara myea[1] = system_serial_high;
192 1.1 kiyohara myea[2] = system_serial_low >> 24;
193 1.1 kiyohara myea[3] = system_serial_low >> 16;
194 1.1 kiyohara myea[4] = system_serial_low >> 8;
195 1.1 kiyohara myea[5] = (system_serial_low & 0xc0) |
196 1.1 kiyohara (1 << 4) | ((ether_serial_digit++) & 0x0f);
197 1.1 kiyohara smc91cxx_attach(sc, myea);
198 1.1 kiyohara } else
199 1.1 kiyohara smc91cxx_attach(sc, NULL);
200 1.1 kiyohara
201 1.1 kiyohara /* Establish the interrupt handler. */
202 1.1 kiyohara gsc->sc_ih = gxio_intr_establish(gxa->gxa_sc,
203 1.1 kiyohara gxa->gxa_gpirq, IST_EDGE_RISING, IPL_NET, smc91cxx_intr, sc);
204 1.1 kiyohara
205 1.1 kiyohara if (gsc->sc_ih == NULL)
206 1.2 kiyohara aprint_error("%s: couldn't establish interrupt handler\n",
207 1.1 kiyohara sc->sc_dev.dv_xname);
208 1.1 kiyohara }
209