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