dl10019.c revision 1.1 1 /* $NetBSD: dl10019.c,v 1.1 2001/02/13 01:43:02 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include "opt_inet.h"
40 #include "bpfilter.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/syslog.h>
46 #include <sys/socket.h>
47 #include <sys/device.h>
48
49 #include <net/if.h>
50 #include <net/if_ether.h>
51 #include <net/if_media.h>
52
53 #ifdef INET
54 #include <netinet/in.h>
55 #include <netinet/if_inarp.h>
56 #endif
57
58 #include <machine/bus.h>
59 #include <machine/intr.h>
60
61 #include <dev/mii/miivar.h>
62 #include <dev/mii/mii.h>
63 #include <dev/mii/mii_bitbang.h>
64
65 #include <dev/ic/dp8390reg.h>
66 #include <dev/ic/dp8390var.h>
67
68 #include <dev/ic/ne2000reg.h>
69 #include <dev/ic/ne2000var.h>
70
71 #include <dev/ic/dl10019reg.h>
72 #include <dev/ic/dl10019var.h>
73
74 int dl10019_mii_readreg(struct device *, int, int);
75 void dl10019_mii_writereg(struct device *, int, int, int);
76 void dl10019_mii_statchg(struct device *);
77
78 /*
79 * MII bit-bang glue.
80 */
81 u_int32_t dl10019_mii_bitbang_read(struct device *);
82 void dl10019_mii_bitbang_write(struct device *, u_int32_t);
83
84 const struct mii_bitbang_ops dl10019_mii_bitbang_ops = {
85 dl10019_mii_bitbang_read,
86 dl10019_mii_bitbang_write,
87 {
88 DL0_GPIO_MII_DATAOUT, /* MII_BIT_MDO */
89 DL0_GPIO_MII_DATAIN, /* MII_BIT_MDI */
90 DL0_GPIO_MII_CLK, /* MII_BIT_MDC */
91 DL0_19_GPIO_MII_DIROUT, /* MII_BIT_DIR_HOST_PHY */
92 0, /* MII_BIT_DIR_PHY_HOST */
93 }
94 };
95
96 const struct mii_bitbang_ops dl10022_mii_bitbang_ops = {
97 dl10019_mii_bitbang_read,
98 dl10019_mii_bitbang_write,
99 {
100 DL0_GPIO_MII_DATAOUT, /* MII_BIT_MDO */
101 DL0_GPIO_MII_DATAIN, /* MII_BIT_MDI */
102 DL0_GPIO_MII_CLK, /* MII_BIT_MDC */
103 DL0_22_GPIO_MII_DIROUT, /* MII_BIT_DIR_HOST_PHY */
104 0, /* MII_BIT_DIR_PHY_HOST */
105 }
106 };
107
108 static void
109 dl10019_mii_reset(struct dp8390_softc *sc)
110 {
111 struct ne2000_softc *nsc = (void *) sc;
112 int i;
113
114 if (nsc->sc_type != NE2000_TYPE_DL10022)
115 return;
116
117 for (i = 0; i < 2; i++) {
118 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
119 0x08);
120 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
121 0x0c);
122 }
123 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 0x00);
124 }
125
126 void
127 dl10019_media_init(struct dp8390_softc *sc)
128 {
129 struct ifnet *ifp = &sc->sc_ec.ec_if;
130
131 sc->sc_mii.mii_ifp = ifp;
132 sc->sc_mii.mii_readreg = dl10019_mii_readreg;
133 sc->sc_mii.mii_writereg = dl10019_mii_writereg;
134 sc->sc_mii.mii_statchg = dl10019_mii_statchg;
135 ifmedia_init(&sc->sc_mii.mii_media, 0, dp8390_mediachange,
136 dp8390_mediastatus);
137
138 dl10019_mii_reset(sc);
139
140 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
141 MII_OFFSET_ANY, 0);
142
143 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
144 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
145 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
146 } else
147 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
148 }
149
150 void
151 dl10019_media_fini(struct dp8390_softc *sc)
152 {
153
154 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
155 }
156
157 int
158 dl10019_mediachange(struct dp8390_softc *sc)
159 {
160
161 mii_mediachg(&sc->sc_mii);
162 return (0);
163 }
164
165 void
166 dl10019_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr)
167 {
168
169 mii_pollstat(&sc->sc_mii);
170 ifmr->ifm_status = sc->sc_mii.mii_media_status;
171 ifmr->ifm_active = sc->sc_mii.mii_media_active;
172 }
173
174 void
175 dl10019_init_card(struct dp8390_softc *sc)
176 {
177
178 dl10019_mii_reset(sc);
179 mii_mediachg(&sc->sc_mii);
180 }
181
182 void
183 dl10019_stop_card(struct dp8390_softc *sc)
184 {
185
186 mii_down(&sc->sc_mii);
187 }
188
189 u_int32_t
190 dl10019_mii_bitbang_read(struct device *self)
191 {
192 struct dp8390_softc *sc = (void *) self;
193
194 /* We're already in Page 0. */
195 return (bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO) &
196 ~DL0_GPIO_PRESERVE);
197 }
198
199 void
200 dl10019_mii_bitbang_write(struct device *self, u_int32_t val)
201 {
202 struct dp8390_softc *sc = (void *) self;
203 u_int8_t gpio;
204
205 /* We're already in Page 0. */
206 gpio = bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO);
207 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
208 (val & ~DL0_GPIO_PRESERVE) | (gpio & DL0_GPIO_PRESERVE));
209 }
210
211 int
212 dl10019_mii_readreg(struct device *self, int phy, int reg)
213 {
214 int val;
215
216 val = mii_bitbang_readreg(self, &dl10019_mii_bitbang_ops, phy, reg);
217
218 return (val);
219 }
220
221 void
222 dl10019_mii_writereg(struct device *self, int phy, int reg, int val)
223 {
224
225 mii_bitbang_writereg(self, &dl10019_mii_bitbang_ops, phy, reg, val);
226 }
227
228 void
229 dl10019_mii_statchg(struct device *self)
230 {
231 struct dp8390_softc *sc = (void *) self;
232 struct ne2000_softc *nsc = (void *) self;
233
234 /*
235 * Disable collision detection on the DL10022 if
236 * we are on a full-duplex link.
237 */
238 if (nsc->sc_type == NE2000_TYPE_DL10022) {
239 u_int8_t diag;
240
241 if (sc->sc_mii.mii_media_active & IFM_FDX)
242 diag = DL0_DIAG_NOCOLLDETECT;
243 else
244 diag = 0;
245 bus_space_write_1(sc->sc_regt, sc->sc_regh,
246 NEDL_DL0_DIAG, diag);
247 }
248 }
249