xref: /openbsd-src/sys/dev/ic/dl10019.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: dl10019.c,v 1.4 2008/06/26 05:42:15 ray 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/syslog.h>
39 
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/if_media.h>
44 
45 #ifdef INET
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/in_var.h>
49 #include <netinet/ip.h>
50 #include <netinet/if_ether.h>
51 #endif
52 
53 #include <machine/bus.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 void	dl10019_mii_reset(struct dp8390_softc *);
73 
74 /*
75  * MII bit-bang glue.
76  */
77 u_int32_t dl10019_mii_bitbang_read(struct device *);
78 void dl10019_mii_bitbang_write(struct device *, u_int32_t);
79 
80 const struct mii_bitbang_ops dl10019_mii_bitbang_ops = {
81 	dl10019_mii_bitbang_read,
82 	dl10019_mii_bitbang_write,
83 	{
84 		DL0_GPIO_MII_DATAOUT,	/* MII_BIT_MDO */
85 		DL0_GPIO_MII_DATAIN,	/* MII_BIT_MDI */
86 		DL0_GPIO_MII_CLK,	/* MII_BIT_MDC */
87 		DL0_19_GPIO_MII_DIROUT,	/* MII_BIT_DIR_HOST_PHY */
88 		0,			/* MII_BIT_DIR_PHY_HOST */
89 	}
90 };
91 
92 const struct mii_bitbang_ops dl10022_mii_bitbang_ops = {
93 	dl10019_mii_bitbang_read,
94 	dl10019_mii_bitbang_write,
95 	{
96 		DL0_GPIO_MII_DATAOUT,	/* MII_BIT_MDO */
97 		DL0_GPIO_MII_DATAIN,	/* MII_BIT_MDI */
98 		DL0_GPIO_MII_CLK,	/* MII_BIT_MDC */
99 		DL0_22_GPIO_MII_DIROUT,	/* MII_BIT_DIR_HOST_PHY */
100 		0,			/* MII_BIT_DIR_PHY_HOST */
101 	}
102 };
103 
104 void
105 dl10019_mii_reset(struct dp8390_softc *sc)
106 {
107 	struct ne2000_softc *nsc = (void *) sc;
108 	int i;
109 
110 	if (nsc->sc_type != NE2000_TYPE_DL10022)
111 		return;
112 
113 	for (i = 0; i < 2; i++) {
114 		bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
115 		    0x08);
116 		DELAY(1);
117 		bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
118 		    0x0c);
119 		DELAY(1);
120 	}
121 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 0x00);
122 }
123 
124 void
125 dl10019_media_init(struct dp8390_softc *sc)
126 {
127 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
128 
129 	sc->sc_mii.mii_ifp = ifp;
130 	sc->sc_mii.mii_readreg = dl10019_mii_readreg;
131 	sc->sc_mii.mii_writereg = dl10019_mii_writereg;
132 	sc->sc_mii.mii_statchg = dl10019_mii_statchg;
133 	ifmedia_init(&sc->sc_mii.mii_media, 0, dp8390_mediachange,
134 	    dp8390_mediastatus);
135 
136 	dl10019_mii_reset(sc);
137 
138 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
139 	    MII_OFFSET_ANY, 0);
140 
141 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
142 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
143 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
144 	} else
145 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
146 }
147 
148 void
149 dl10019_media_fini(struct dp8390_softc *sc)
150 {
151 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
152 }
153 
154 int
155 dl10019_mediachange(struct dp8390_softc *sc)
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 	mii_pollstat(&sc->sc_mii);
165 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
166 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
167 }
168 
169 void
170 dl10019_init_card(struct dp8390_softc *sc)
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 	mii_down(&sc->sc_mii);
180 }
181 
182 u_int32_t
183 dl10019_mii_bitbang_read(struct device *self)
184 {
185 	struct dp8390_softc *sc = (void *) self;
186 
187 	/* We're already in Page 0. */
188 	return (bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO) &
189 	    ~DL0_GPIO_PRESERVE);
190 }
191 
192 void
193 dl10019_mii_bitbang_write(struct device *self, u_int32_t val)
194 {
195 	struct dp8390_softc *sc = (void *) self;
196 	u_int8_t gpio;
197 
198 	/* We're already in Page 0. */
199 	gpio = bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO);
200 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
201 	    (val & ~DL0_GPIO_PRESERVE) | (gpio & DL0_GPIO_PRESERVE));
202 }
203 
204 int
205 dl10019_mii_readreg(struct device *self, int phy, int reg)
206 {
207 	struct ne2000_softc *nsc = (void *) self;
208 	const struct mii_bitbang_ops *ops;
209 
210 	ops = (nsc->sc_type == NE2000_TYPE_DL10022) ?
211 	    &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops;
212 
213 	return (mii_bitbang_readreg(self, ops, phy, reg));
214 }
215 
216 void
217 dl10019_mii_writereg(struct device *self, int phy, int reg, int val)
218 {
219 	struct ne2000_softc *nsc = (void *) self;
220 	const struct mii_bitbang_ops *ops;
221 
222 	ops = (nsc->sc_type == NE2000_TYPE_DL10022) ?
223 	    &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops;
224 
225 	mii_bitbang_writereg(self, 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