Deleted Added
sdiff udiff text old ( 213364 ) new ( 213384 )
full compact
1/* $NetBSD: tlphy.c,v 1.18 1999/05/14 11:40:28 drochner Exp $ */
2
3/*-
4 * Copyright (c) 1998, 1999 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 of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
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/*-
34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57#include <sys/cdefs.h>
58__FBSDID("$FreeBSD: head/sys/dev/mii/tlphy.c 213384 2010-10-03 17:00:57Z marius $");
59
60/*
61 * Driver for Texas Instruments's ThunderLAN PHYs
62 */
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/kernel.h>
67#include <sys/socket.h>
68#include <sys/errno.h>
69#include <sys/module.h>
70#include <sys/bus.h>
71#include <sys/malloc.h>
72
73#include <machine/bus.h>
74
75#include <net/if.h>
76#include <net/if_media.h>
77
78#include <dev/mii/mii.h>
79#include <dev/mii/miivar.h>
80#include "miidevs.h"
81
82#include <dev/mii/tlphyreg.h>
83
84#include "miibus_if.h"
85
86struct tlphy_softc {
87 struct mii_softc sc_mii; /* generic PHY */
88 int sc_need_acomp;
89};
90
91static int tlphy_probe(device_t);
92static int tlphy_attach(device_t);
93
94static device_method_t tlphy_methods[] = {
95 /* device interface */
96 DEVMETHOD(device_probe, tlphy_probe),
97 DEVMETHOD(device_attach, tlphy_attach),
98 DEVMETHOD(device_detach, mii_phy_detach),
99 DEVMETHOD(device_shutdown, bus_generic_shutdown),
100 { 0, 0 }
101};
102
103static devclass_t tlphy_devclass;
104
105static driver_t tlphy_driver = {
106 "tlphy",
107 tlphy_methods,
108 sizeof(struct tlphy_softc)
109};
110
111DRIVER_MODULE(tlphy, miibus, tlphy_driver, tlphy_devclass, 0, 0);
112
113static int tlphy_service(struct mii_softc *, struct mii_data *, int);
114static int tlphy_auto(struct tlphy_softc *);
115static void tlphy_acomp(struct tlphy_softc *);
116static void tlphy_status(struct tlphy_softc *);
117
118static const struct mii_phydesc tlphys[] = {
119 MII_PHY_DESC(xxTI, TLAN10T),
120 MII_PHY_END
121};
122
123static int
124tlphy_probe(device_t dev)
125{
126
127 return (mii_phy_dev_probe(dev, tlphys, BUS_PROBE_DEFAULT));
128}
129
130static int
131tlphy_attach(device_t dev)
132{
133 device_t *devlist;
134 struct tlphy_softc *sc;
135 struct mii_softc *other;
136 struct mii_attach_args *ma;
137 struct mii_data *mii;
138 const char *sep = "";
139 int capmask, devs, i;
140
141 sc = device_get_softc(dev);
142 ma = device_get_ivars(dev);
143 sc->sc_mii.mii_dev = device_get_parent(dev);
144 mii = device_get_softc(sc->sc_mii.mii_dev);
145 LIST_INSERT_HEAD(&mii->mii_phys, &sc->sc_mii, mii_list);
146
147 sc->sc_mii.mii_inst = mii->mii_instance;
148 sc->sc_mii.mii_phy = ma->mii_phyno;
149 sc->sc_mii.mii_service = tlphy_service;
150 sc->sc_mii.mii_pdata = mii;
151
152 capmask = 0xFFFFFFFF;
153 if (mii->mii_instance &&
154 device_get_children(sc->sc_mii.mii_dev, &devlist, &devs) == 0) {
155 for (i = 0; i < devs; i++) {
156 if (strcmp(device_get_name(devlist[i]), "tlphy")) {
157 other = device_get_softc(devlist[i]);
158 capmask &= ~other->mii_capabilities;
159 break;
160 }
161 }
162 free(devlist, M_TEMP);
163 }
164
165 mii->mii_instance++;
166
167 mii_phy_reset(&sc->sc_mii);
168
169 /*
170 * Note that if we're on a device that also supports 100baseTX,
171 * we are not going to want to use the built-in 10baseT port,
172 * since there will be another PHY on the MII wired up to the
173 * UTP connector. The parent indicates this to us by specifying
174 * the TLPHY_MEDIA_NO_10_T bit.
175 */
176 sc->sc_mii.mii_capabilities =
177 PHY_READ(&sc->sc_mii, MII_BMSR) & capmask /*ma->mii_capmask*/;
178
179#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
180
181 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
182 BMCR_ISO);
183
184 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
185 sc->sc_mii.mii_inst), BMCR_LOOP);
186
187#define PRINT(s) printf("%s%s", sep, s); sep = ", "
188
189 device_printf(dev, " ");
190 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->sc_mii.mii_inst), 0);
191 PRINT("10base2/BNC");
192 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, sc->sc_mii.mii_inst), 0);
193 PRINT("10base5/AUI");
194
195 if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
196 printf("%s", sep);
197 mii_add_media(&sc->sc_mii);
198 }
199
200 printf("\n");
201#undef ADD
202#undef PRINT
203 MIIBUS_MEDIAINIT(sc->sc_mii.mii_dev);
204 return (0);
205}
206
207static int
208tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd)
209{
210 struct tlphy_softc *sc = (struct tlphy_softc *)self;
211 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
212 int reg;
213
214 if (sc->sc_need_acomp)
215 tlphy_acomp(sc);
216
217 switch (cmd) {
218 case MII_POLLSTAT:
219 break;
220
221 case MII_MEDIACHG:
222 /*
223 * If the interface is not up, don't do anything.
224 */
225 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
226 break;
227
228 switch (IFM_SUBTYPE(ife->ifm_media)) {
229 case IFM_AUTO:
230 /*
231 * The ThunderLAN PHY doesn't self-configure after
232 * an autonegotiation cycle, so there's no such
233 * thing as "already in auto mode".
234 */
235 (void) tlphy_auto(sc);
236 break;
237 case IFM_10_2:
238 case IFM_10_5:
239 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
240 PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
241 DELAY(100000);
242 break;
243 default:
244 PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
245 DELAY(100000);
246 PHY_WRITE(&sc->sc_mii, MII_ANAR,
247 mii_anar(ife->ifm_media));
248 PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data);
249 }
250 break;
251
252 case MII_TICK:
253 /*
254 * Is the interface even up?
255 */
256 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
257 return (0);
258
259 /*
260 * Only used for autonegotiation.
261 */
262 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
263 break;
264
265 /*
266 * Check to see if we have link. If we do, we don't
267 * need to restart the autonegotiation process. Read
268 * the BMSR twice in case it's latched.
269 *
270 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
271 */
272 reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
273 PHY_READ(&sc->sc_mii, MII_BMSR);
274 if (reg & BMSR_LINK)
275 break;
276
277 /*
278 * Only retry autonegotiation every 5 seconds.
279 */
280 if (++sc->sc_mii.mii_ticks <= MII_ANEGTICKS)
281 break;
282
283 sc->sc_mii.mii_ticks = 0;
284 mii_phy_reset(&sc->sc_mii);
285 tlphy_auto(sc);
286 return (0);
287 }
288
289 /* Update the media status. */
290 tlphy_status(sc);
291
292 /* Callback if something changed. */
293 mii_phy_update(&sc->sc_mii, cmd);
294 return (0);
295}
296
297static void
298tlphy_status(struct tlphy_softc *sc)
299{
300 struct mii_data *mii = sc->sc_mii.mii_pdata;
301 int bmsr, bmcr, tlctrl;
302
303 mii->mii_media_status = IFM_AVALID;
304 mii->mii_media_active = IFM_ETHER;
305
306 bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
307 if (bmcr & BMCR_ISO) {
308 mii->mii_media_active |= IFM_NONE;
309 mii->mii_media_status = 0;
310 return;
311 }
312
313 tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
314 if (tlctrl & CTRL_AUISEL) {
315 mii->mii_media_status = 0;
316 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;
317 return;
318 }
319
320 bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
321 PHY_READ(&sc->sc_mii, MII_BMSR);
322 if (bmsr & BMSR_LINK)
323 mii->mii_media_status |= IFM_ACTIVE;
324
325 if (bmcr & BMCR_LOOP)
326 mii->mii_media_active |= IFM_LOOP;
327
328 /*
329 * Grr, braindead ThunderLAN PHY doesn't have any way to
330 * tell which media is actually active. (Note it also
331 * doesn't self-configure after autonegotiation.) We
332 * just have to report what's in the BMCR.
333 */
334 if (bmcr & BMCR_FDX)
335 mii->mii_media_active |= IFM_FDX;
336 else
337 mii->mii_media_active |= IFM_HDX;
338 mii->mii_media_active |= IFM_10_T;
339}
340
341static int
342tlphy_auto(struct tlphy_softc *sc)
343{
344 int error;
345
346 switch ((error = mii_phy_auto(&sc->sc_mii))) {
347 case EIO:
348 /*
349 * Just assume we're not in full-duplex mode.
350 * XXX Check link and try AUI/BNC?
351 */
352 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
353 break;
354
355 case EJUSTRETURN:
356 /* Flag that we need to program when it completes. */
357 sc->sc_need_acomp = 1;
358 break;
359
360 default:
361 tlphy_acomp(sc);
362 }
363
364 return (error);
365}
366
367static void
368tlphy_acomp(struct tlphy_softc *sc)
369{
370 int aner, anlpar;
371
372 sc->sc_need_acomp = 0;
373
374 /*
375 * Grr, braindead ThunderLAN PHY doesn't self-configure
376 * after autonegotiation. We have to do it ourselves
377 * based on the link partner status.
378 */
379
380 aner = PHY_READ(&sc->sc_mii, MII_ANER);
381 if (aner & ANER_LPAN) {
382 anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
383 PHY_READ(&sc->sc_mii, MII_ANAR);
384 if (anlpar & ANAR_10_FD) {
385 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
386 return;
387 }
388 }
389 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
390}