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