xref: /netbsd-src/sys/dev/mii/rdcphy.c (revision 114be2d1a237fa9f60812a1c5a60e1722073d847)
1 /*      $NetBSD: rdcphy.c,v 1.8 2020/04/08 03:01:58 msaitoh Exp $        */
2 
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 /* FreeBSD: src/sys/dev/mii/rdcphy.c,v 1.1 2010/12/30 23:50:25 yongari Exp */
31 
32 /*
33  * Driver for the RDC Semiconductor R6040 10/100 PHY.
34  */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: rdcphy.c,v 1.8 2020/04/08 03:01:58 msaitoh Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/socket.h>
43 #include <sys/errno.h>
44 
45 #include <sys/bus.h>
46 
47 #include <net/if.h>
48 #include <net/if_media.h>
49 
50 #include <dev/mii/mii.h>
51 #include <dev/mii/miivar.h>
52 #include <dev/mii/miidevs.h>
53 
54 #include <dev/mii/rdcphyreg.h>
55 
56 struct rdcphy_softc {
57 	struct mii_softc sc_mii;
58 	int		 sc_mii_link_tick;
59 #define	RDCPHY_MANNEG_TICK	3
60 };
61 
62 static int	rdcphymatch(device_t, cfdata_t, void *);
63 static void	rdcphyattach(device_t, device_t, void *);
64 
65 CFATTACH_DECL_NEW(rdcphy, sizeof(struct rdcphy_softc),
66     rdcphymatch, rdcphyattach, mii_phy_detach, mii_phy_activate);
67 
68 
69 static int	rdcphy_service(struct mii_softc *, struct mii_data *, int);
70 static void	rdcphy_status(struct mii_softc *);
71 
72 static const struct mii_phy_funcs rdcphy_funcs = {
73 	rdcphy_service, rdcphy_status, mii_phy_reset,
74 };
75 
76 static const struct mii_phydesc rdcphys[] = {
77 	MII_PHY_DESC(xxRDC, R6040),
78 	MII_PHY_DESC(xxRDC, R6040_2),
79 	MII_PHY_DESC(xxRDC, R6040_3),
80 	MII_PHY_END,
81 };
82 
83 static int
rdcphymatch(device_t parent,cfdata_t match,void * aux)84 rdcphymatch(device_t parent, cfdata_t match, void *aux)
85 {
86 	struct mii_attach_args *ma = aux;
87 
88 	if (mii_phy_match(ma, rdcphys) != NULL)
89 		return 10;
90 
91 	return 0;
92 }
93 
94 static void
rdcphyattach(device_t parent,device_t self,void * aux)95 rdcphyattach(device_t parent, device_t self, void *aux)
96 {
97 	struct rdcphy_softc *rsc = device_private(self);
98 	struct mii_softc *sc = &rsc->sc_mii;
99 	struct mii_attach_args *ma = aux;
100 	struct mii_data *mii = ma->mii_data;
101 
102 	const struct mii_phydesc *mpd;
103 
104 	mpd = mii_phy_match(ma, rdcphys);
105 	aprint_naive(": Media interface\n");
106 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
107 
108 	sc->mii_dev = self;
109 	sc->mii_inst = mii->mii_instance;
110 	sc->mii_phy = ma->mii_phyno;
111 	sc->mii_funcs = &rdcphy_funcs;
112 	sc->mii_pdata = mii;
113 	sc->mii_flags = ma->mii_flags;
114 
115 	mii_lock(mii);
116 
117 	PHY_RESET(sc);
118 
119 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
120 	sc->mii_capabilities &= ma->mii_capmask;
121 	if (sc->mii_capabilities & BMSR_EXTSTAT)
122 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
123 
124 	mii_unlock(mii);
125 
126 	mii_phy_add_media(sc);
127 }
128 
129 static int
rdcphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)130 rdcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
131 {
132 	struct rdcphy_softc *rsc = (struct rdcphy_softc *)sc;
133 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
134 
135 	KASSERT(mii_locked(mii));
136 
137 	switch (cmd) {
138 	case MII_POLLSTAT:
139 		break;
140 
141 	case MII_MEDIACHG:
142 		/* If the interface is not up, don't do anything. */
143 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
144 			break;
145 
146 		mii_phy_setmedia(sc);
147 		switch (IFM_SUBTYPE(ife->ifm_media)) {
148 		case IFM_100_TX:
149 		case IFM_10_T:
150 			/*
151 			 * Report fake lost link event to parent driver.  This
152 			 * will stop MAC of parent driver and make it possible
153 			 * to reconfigure MAC after completion of link
154 			 * establishment.
155 			 * Note, the parent MAC seems to require restarting MAC
156 			 * when underlying any PHY configuration was changed
157 			 * even if the resolved speed/duplex was not changed at
158 			 * all.
159 			 */
160 			mii->mii_media_status = 0;
161 			mii->mii_media_active = IFM_ETHER | IFM_NONE;
162 			rsc->sc_mii_link_tick = RDCPHY_MANNEG_TICK;
163 			/* Immediately report link down. */
164 			mii_phy_update(sc, MII_MEDIACHG);
165 			return 0;
166 		default:
167 			break;
168 		}
169 		break;
170 
171 	case MII_TICK:
172 		if (mii_phy_tick(sc) == EJUSTRETURN)
173 			return 0;
174 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
175 			/*
176 			 * It seems the PHY hardware does not correctly report
177 			 * link status changes when manual link configuration
178 			 * is in progress.  It is also possible for the PHY to
179 			 * complete establishing a link within one second such
180 			 * that mii(4) did not notice the link change.
181 			 * To workaround the issue, emulate lost link event and
182 			 * wait for 3 seconds when manual link configuration
183 			 * is in progress.  3 seconds would be long enough to
184 			 * absorb transient link flips.
185 			 */
186 			if (rsc->sc_mii_link_tick > 0) {
187 				rsc->sc_mii_link_tick--;
188 				return 0;
189 			}
190 		}
191 		break;
192 	}
193 
194 	/* Update the media status. */
195 	rdcphy_status(sc);
196 
197 	/* Callback if something changed. */
198 	mii_phy_update(sc, cmd);
199 	return 0;
200 }
201 
202 static void
rdcphy_status(struct mii_softc * sc)203 rdcphy_status(struct mii_softc *sc)
204 {
205 	struct mii_data *mii = sc->mii_pdata;
206 	uint16_t bmsr, bmcr, physts;
207 
208 	KASSERT(mii_locked(mii));
209 
210 	mii->mii_media_status = IFM_AVALID;
211 	mii->mii_media_active = IFM_ETHER;
212 
213 	PHY_READ(sc, MII_BMSR, &bmsr);
214 	PHY_READ(sc, MII_BMSR, &bmsr);
215 	PHY_READ(sc, MII_RDCPHY_STATUS, &physts);
216 
217 	if ((physts & STATUS_LINK_UP) != 0)
218 		mii->mii_media_status |= IFM_ACTIVE;
219 
220 	PHY_READ(sc, MII_BMCR, &bmcr);
221 	if ((bmcr & BMCR_ISO) != 0) {
222 		mii->mii_media_active |= IFM_NONE;
223 		mii->mii_media_status = 0;
224 		return;
225 	}
226 
227 	if ((bmcr & BMCR_LOOP) != 0)
228 		mii->mii_media_active |= IFM_LOOP;
229 
230 	if ((bmcr & BMCR_AUTOEN) != 0) {
231 		if ((bmsr & BMSR_ACOMP) == 0) {
232 			/* Erg, still trying, I guess... */
233 			mii->mii_media_active |= IFM_NONE;
234 			return;
235 		}
236 	}
237 
238 	switch (physts & STATUS_SPEED_MASK) {
239 	case STATUS_SPEED_100:
240 		mii->mii_media_active |= IFM_100_TX;
241 		break;
242 	case STATUS_SPEED_10:
243 		mii->mii_media_active |= IFM_10_T;
244 		break;
245 	default:
246 		mii->mii_media_active |= IFM_NONE;
247 		return;
248 	}
249 	if ((physts & STATUS_FULL_DUPLEX) != 0)
250 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
251 	else
252 		mii->mii_media_active |= IFM_HDX;
253 }
254