xref: /freebsd/sys/dev/mii/rdcphy.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010, Pyun YongHyeon <yongari@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 /*
32  * Driver for the RDC Semiconductor R6040 10/100 PHY.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/socket.h>
40 #include <sys/bus.h>
41 
42 #include <net/if.h>
43 #include <net/if_media.h>
44 
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
47 #include "miidevs.h"
48 
49 #include <dev/mii/rdcphyreg.h>
50 
51 #include "miibus_if.h"
52 
53 static device_probe_t	rdcphy_probe;
54 static device_attach_t	rdcphy_attach;
55 
56 struct rdcphy_softc {
57 	struct mii_softc mii_sc;
58 	int mii_link_tick;
59 #define	RDCPHY_MANNEG_TICK	3
60 };
61 
62 static device_method_t rdcphy_methods[] = {
63 	/* device interface */
64 	DEVMETHOD(device_probe,		rdcphy_probe),
65 	DEVMETHOD(device_attach,	rdcphy_attach),
66 	DEVMETHOD(device_detach,	mii_phy_detach),
67 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
68 	DEVMETHOD_END
69 };
70 
71 static driver_t rdcphy_driver = {
72 	"rdcphy",
73 	rdcphy_methods,
74 	sizeof(struct rdcphy_softc)
75 };
76 
77 DRIVER_MODULE(rdcphy, miibus, rdcphy_driver, 0, 0);
78 
79 static int	rdcphy_service(struct mii_softc *, struct mii_data *, int);
80 static void	rdcphy_status(struct mii_softc *);
81 
82 static const struct mii_phydesc rdcphys[] = {
83 	MII_PHY_DESC(RDC, R6040),
84 	MII_PHY_DESC(RDC, R6040_2),
85 	MII_PHY_END
86 };
87 
88 static const struct mii_phy_funcs rdcphy_funcs = {
89 	rdcphy_service,
90 	rdcphy_status,
91 	mii_phy_reset
92 };
93 
94 static int
95 rdcphy_probe(device_t dev)
96 {
97 
98 	return (mii_phy_dev_probe(dev, rdcphys, BUS_PROBE_DEFAULT));
99 }
100 
101 static int
102 rdcphy_attach(device_t dev)
103 {
104 
105 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &rdcphy_funcs, 1);
106 	return (0);
107 }
108 
109 static int
110 rdcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
111 {
112 	struct rdcphy_softc *rsc;
113 	struct ifmedia_entry *ife;
114 
115 	rsc = (struct rdcphy_softc *)sc;
116 	ife = mii->mii_media.ifm_cur;
117 
118 	switch (cmd) {
119 	case MII_POLLSTAT:
120 		break;
121 
122 	case MII_MEDIACHG:
123 		mii_phy_setmedia(sc);
124 		switch (IFM_SUBTYPE(ife->ifm_media)) {
125 		case IFM_100_TX:
126 		case IFM_10_T:
127 			/*
128 			 * Report fake lost link event to parent
129 			 * driver.  This will stop MAC of parent
130 			 * driver and make it possible to reconfigure
131 			 * MAC after completion of link establishment.
132 			 * Note, the parent MAC seems to require
133 			 * restarting MAC when underlying any PHY
134 			 * configuration was changed even if the
135 			 * resolved speed/duplex was not changed at
136 			 * all.
137 			 */
138 			mii->mii_media_status = 0;
139 			mii->mii_media_active = IFM_ETHER | IFM_NONE;
140 			rsc->mii_link_tick = RDCPHY_MANNEG_TICK;
141 			/* Immediately report link down. */
142 			mii_phy_update(sc, MII_MEDIACHG);
143 			return (0);
144 		default:
145 			break;
146 		}
147 		break;
148 
149 	case MII_TICK:
150 		if (mii_phy_tick(sc) == EJUSTRETURN)
151 			return (0);
152 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
153 			/*
154 			 * It seems the PHY hardware does not correctly
155 			 * report link status changes when manual link
156 			 * configuration is in progress.  It is also
157 			 * possible for the PHY to complete establishing
158 			 * a link within one second such that mii(4)
159 			 * did not notice the link change.  To workaround
160 			 * the issue, emulate lost link event and wait
161 			 * for 3 seconds when manual link configuration
162 			 * is in progress.  3 seconds would be long
163 			 * enough to absorb transient link flips.
164 			 */
165 			if (rsc->mii_link_tick > 0) {
166 				rsc->mii_link_tick--;
167 				return (0);
168 			}
169 		}
170 		break;
171 	}
172 
173 	/* Update the media status. */
174 	PHY_STATUS(sc);
175 
176 	/* Callback if something changed. */
177 	mii_phy_update(sc, cmd);
178 	return (0);
179 }
180 
181 static void
182 rdcphy_status(struct mii_softc *sc)
183 {
184 	struct mii_data *mii;
185 	int bmsr, bmcr, physts;
186 
187 	mii = sc->mii_pdata;
188 
189 	mii->mii_media_status = IFM_AVALID;
190 	mii->mii_media_active = IFM_ETHER;
191 
192 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
193 	physts = PHY_READ(sc, MII_RDCPHY_STATUS);
194 
195 	if ((physts & STATUS_LINK_UP) != 0)
196 		mii->mii_media_status |= IFM_ACTIVE;
197 
198 	bmcr = PHY_READ(sc, MII_BMCR);
199 	if ((bmcr & BMCR_ISO) != 0) {
200 		mii->mii_media_active |= IFM_NONE;
201 		mii->mii_media_status = 0;
202 		return;
203 	}
204 
205 	if ((bmcr & BMCR_LOOP) != 0)
206 		mii->mii_media_active |= IFM_LOOP;
207 
208 	if ((bmcr & BMCR_AUTOEN) != 0) {
209 		if ((bmsr & BMSR_ACOMP) == 0) {
210 			/* Erg, still trying, I guess... */
211 			mii->mii_media_active |= IFM_NONE;
212 			return;
213 		}
214 	}
215 
216 	switch (physts & STATUS_SPEED_MASK) {
217 	case STATUS_SPEED_100:
218 		mii->mii_media_active |= IFM_100_TX;
219 		break;
220 	case STATUS_SPEED_10:
221 		mii->mii_media_active |= IFM_10_T;
222 		break;
223 	default:
224 		mii->mii_media_active |= IFM_NONE;
225 		return;
226 	}
227 	if ((physts & STATUS_FULL_DUPLEX) != 0)
228 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
229 	else
230 		mii->mii_media_active |= IFM_HDX;
231 }
232