dl10019.c revision 1.3 1 /* $NetBSD: dl10019.c,v 1.3 2001/06/12 22:32:50 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 "bpfilter.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/syslog.h>
45 #include <sys/socket.h>
46 #include <sys/device.h>
47
48 #include <net/if.h>
49 #include <net/if_ether.h>
50 #include <net/if_media.h>
51
52 #include <machine/bus.h>
53 #include <machine/intr.h>
54
55 #include <dev/mii/miivar.h>
56 #include <dev/mii/mii.h>
57 #include <dev/mii/mii_bitbang.h>
58
59 #include <dev/ic/dp8390reg.h>
60 #include <dev/ic/dp8390var.h>
61
62 #include <dev/ic/ne2000reg.h>
63 #include <dev/ic/ne2000var.h>
64
65 #include <dev/ic/dl10019reg.h>
66 #include <dev/ic/dl10019var.h>
67
68 int dl10019_mii_readreg(struct device *, int, int);
69 void dl10019_mii_writereg(struct device *, int, int, int);
70 void dl10019_mii_statchg(struct device *);
71
72 /*
73 * MII bit-bang glue.
74 */
75 u_int32_t dl10019_mii_bitbang_read(struct device *);
76 void dl10019_mii_bitbang_write(struct device *, u_int32_t);
77
78 const struct mii_bitbang_ops dl10019_mii_bitbang_ops = {
79 dl10019_mii_bitbang_read,
80 dl10019_mii_bitbang_write,
81 {
82 DL0_GPIO_MII_DATAOUT, /* MII_BIT_MDO */
83 DL0_GPIO_MII_DATAIN, /* MII_BIT_MDI */
84 DL0_GPIO_MII_CLK, /* MII_BIT_MDC */
85 DL0_19_GPIO_MII_DIROUT, /* MII_BIT_DIR_HOST_PHY */
86 0, /* MII_BIT_DIR_PHY_HOST */
87 }
88 };
89
90 const struct mii_bitbang_ops dl10022_mii_bitbang_ops = {
91 dl10019_mii_bitbang_read,
92 dl10019_mii_bitbang_write,
93 {
94 DL0_GPIO_MII_DATAOUT, /* MII_BIT_MDO */
95 DL0_GPIO_MII_DATAIN, /* MII_BIT_MDI */
96 DL0_GPIO_MII_CLK, /* MII_BIT_MDC */
97 DL0_22_GPIO_MII_DIROUT, /* MII_BIT_DIR_HOST_PHY */
98 0, /* MII_BIT_DIR_PHY_HOST */
99 }
100 };
101
102 static void
103 dl10019_mii_reset(struct dp8390_softc *sc)
104 {
105 struct ne2000_softc *nsc = (void *) sc;
106 int i;
107
108 if (nsc->sc_type != NE2000_TYPE_DL10022)
109 return;
110
111 for (i = 0; i < 2; i++) {
112 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
113 0x08);
114 delay(1);
115 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
116 0x0c);
117 delay(1);
118 }
119 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 0x00);
120 }
121
122 void
123 dl10019_media_init(struct dp8390_softc *sc)
124 {
125 struct ifnet *ifp = &sc->sc_ec.ec_if;
126
127 sc->sc_mii.mii_ifp = ifp;
128 sc->sc_mii.mii_readreg = dl10019_mii_readreg;
129 sc->sc_mii.mii_writereg = dl10019_mii_writereg;
130 sc->sc_mii.mii_statchg = dl10019_mii_statchg;
131 ifmedia_init(&sc->sc_mii.mii_media, 0, dp8390_mediachange,
132 dp8390_mediastatus);
133
134 dl10019_mii_reset(sc);
135
136 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
137 MII_OFFSET_ANY, 0);
138
139 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
140 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
141 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
142 } else
143 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
144 }
145
146 void
147 dl10019_media_fini(struct dp8390_softc *sc)
148 {
149
150 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
151 }
152
153 int
154 dl10019_mediachange(struct dp8390_softc *sc)
155 {
156
157 mii_mediachg(&sc->sc_mii);
158 return (0);
159 }
160
161 void
162 dl10019_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr)
163 {
164
165 mii_pollstat(&sc->sc_mii);
166 ifmr->ifm_status = sc->sc_mii.mii_media_status;
167 ifmr->ifm_active = sc->sc_mii.mii_media_active;
168 }
169
170 void
171 dl10019_init_card(struct dp8390_softc *sc)
172 {
173
174 dl10019_mii_reset(sc);
175 mii_mediachg(&sc->sc_mii);
176 }
177
178 void
179 dl10019_stop_card(struct dp8390_softc *sc)
180 {
181
182 mii_down(&sc->sc_mii);
183 }
184
185 u_int32_t
186 dl10019_mii_bitbang_read(struct device *self)
187 {
188 struct dp8390_softc *sc = (void *) self;
189
190 /* We're already in Page 0. */
191 return (bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO) &
192 ~DL0_GPIO_PRESERVE);
193 }
194
195 void
196 dl10019_mii_bitbang_write(struct device *self, u_int32_t val)
197 {
198 struct dp8390_softc *sc = (void *) self;
199 u_int8_t gpio;
200
201 /* We're already in Page 0. */
202 gpio = bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO);
203 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
204 (val & ~DL0_GPIO_PRESERVE) | (gpio & DL0_GPIO_PRESERVE));
205 }
206
207 int
208 dl10019_mii_readreg(struct device *self, int phy, int reg)
209 {
210 struct ne2000_softc *nsc = (void *) self;
211 const struct mii_bitbang_ops *ops;
212 int val;
213
214 ops = (nsc->sc_type == NE2000_TYPE_DL10022) ?
215 &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops;
216
217 val = mii_bitbang_readreg(self, &dl10019_mii_bitbang_ops, phy, reg);
218
219 return (val);
220 }
221
222 void
223 dl10019_mii_writereg(struct device *self, int phy, int reg, int val)
224 {
225 struct ne2000_softc *nsc = (void *) self;
226 const struct mii_bitbang_ops *ops;
227
228 ops = (nsc->sc_type == NE2000_TYPE_DL10022) ?
229 &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops;
230
231 mii_bitbang_writereg(self, &dl10019_mii_bitbang_ops, phy, reg, val);
232 }
233
234 void
235 dl10019_mii_statchg(struct device *self)
236 {
237 struct dp8390_softc *sc = (void *) self;
238 struct ne2000_softc *nsc = (void *) self;
239
240 /*
241 * Disable collision detection on the DL10022 if
242 * we are on a full-duplex link.
243 */
244 if (nsc->sc_type == NE2000_TYPE_DL10022) {
245 u_int8_t diag;
246
247 if (sc->sc_mii.mii_media_active & IFM_FDX)
248 diag = DL0_DIAG_NOCOLLDETECT;
249 else
250 diag = 0;
251 bus_space_write_1(sc->sc_regt, sc->sc_regh,
252 NEDL_DL0_DIAG, diag);
253 }
254 }
255