1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1997, 1998, 1999
5 *	Bill Paul <wpaul@ctr.columbia.edu>.  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, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: releng/12.0/sys/dev/mii/rlphy.c 325966 2017-11-18 14:26:50Z pfg $");
37
38/*
39 * driver for RealTek 8139 internal PHYs
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/module.h>
46#include <sys/socket.h>
47#include <sys/bus.h>
48#include <sys/taskqueue.h>	/* XXXGL: if_rlreg.h contamination */
49
50#include <net/if.h>
51#include <net/if_arp.h>
52#include <net/if_media.h>
53
54#include <dev/mii/mii.h>
55#include <dev/mii/miivar.h>
56#include "miidevs.h"
57
58#include <machine/bus.h>
59#include <dev/rl/if_rlreg.h>
60
61#include "miibus_if.h"
62
63static int rlphy_probe(device_t);
64static int rlphy_attach(device_t);
65
66static device_method_t rlphy_methods[] = {
67	/* device interface */
68	DEVMETHOD(device_probe,		rlphy_probe),
69	DEVMETHOD(device_attach,	rlphy_attach),
70	DEVMETHOD(device_detach,	mii_phy_detach),
71	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
72	DEVMETHOD_END
73};
74
75static devclass_t rlphy_devclass;
76
77static driver_t rlphy_driver = {
78	"rlphy",
79	rlphy_methods,
80	sizeof(struct mii_softc)
81};
82
83DRIVER_MODULE(rlphy, miibus, rlphy_driver, rlphy_devclass, 0, 0);
84
85static int	rlphy_service(struct mii_softc *, struct mii_data *, int);
86static void	rlphy_status(struct mii_softc *);
87
88/*
89 * RealTek internal PHYs don't have vendor/device ID registers;
90 * re(4) and rl(4) fake up a return value of all zeros.
91 */
92static const struct mii_phydesc rlintphys[] = {
93	{ 0, 0, "RealTek internal media interface" },
94	MII_PHY_END
95};
96
97static const struct mii_phydesc rlphys[] = {
98	MII_PHY_DESC(yyREALTEK, RTL8201L),
99	MII_PHY_DESC(REALTEK, RTL8201E),
100	MII_PHY_DESC(xxICPLUS, IP101),
101	MII_PHY_END
102};
103
104static const struct mii_phy_funcs rlphy_funcs = {
105	rlphy_service,
106	rlphy_status,
107	mii_phy_reset
108};
109
110static int
111rlphy_probe(device_t dev)
112{
113	int rv;
114
115	rv = mii_phy_dev_probe(dev, rlphys, BUS_PROBE_DEFAULT);
116#ifndef __HAIKU__
117	if (rv <= 0)
118		return (rv);
119#else /* __HAIKU__ */
120	/* our BUS_PROBE_* constants are > 0 since errors are < 0 */
121	if (rv > 0)
122		return (rv);
123#endif
124
125	if (mii_dev_mac_match(dev, "rl") || mii_dev_mac_match(dev, "re"))
126		return (mii_phy_dev_probe(dev, rlintphys, BUS_PROBE_DEFAULT));
127	return (ENXIO);
128}
129
130static int
131rlphy_attach(device_t dev)
132{
133
134	/*
135	 * The RealTek PHY can never be isolated.
136	 */
137	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
138	    &rlphy_funcs, 1);
139	return (0);
140}
141
142static int
143rlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
144{
145
146	switch (cmd) {
147	case MII_POLLSTAT:
148		break;
149
150	case MII_MEDIACHG:
151		mii_phy_setmedia(sc);
152		break;
153
154	case MII_TICK:
155		/*
156		 * The RealTek PHY's autonegotiation doesn't need to be
157		 * kicked; it continues in the background.
158		 */
159		break;
160	}
161
162	/* Update the media status. */
163	PHY_STATUS(sc);
164
165	/* Callback if something changed. */
166	mii_phy_update(sc, cmd);
167	return (0);
168}
169
170static void
171rlphy_status(struct mii_softc *phy)
172{
173	struct mii_data *mii = phy->mii_pdata;
174	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
175	int bmsr, bmcr, anlpar;
176
177	mii->mii_media_status = IFM_AVALID;
178	mii->mii_media_active = IFM_ETHER;
179
180	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
181	if (bmsr & BMSR_LINK)
182		mii->mii_media_status |= IFM_ACTIVE;
183
184	bmcr = PHY_READ(phy, MII_BMCR);
185	if (bmcr & BMCR_ISO) {
186		mii->mii_media_active |= IFM_NONE;
187		mii->mii_media_status = 0;
188		return;
189	}
190
191	if (bmcr & BMCR_LOOP)
192		mii->mii_media_active |= IFM_LOOP;
193
194	if (bmcr & BMCR_AUTOEN) {
195		/*
196		 * NWay autonegotiation takes the highest-order common
197		 * bit of the ANAR and ANLPAR (i.e. best media advertised
198		 * both by us and our link partner).
199		 */
200		if ((bmsr & BMSR_ACOMP) == 0) {
201			/* Erg, still trying, I guess... */
202			mii->mii_media_active |= IFM_NONE;
203			return;
204		}
205
206		if ((anlpar = PHY_READ(phy, MII_ANAR) &
207		    PHY_READ(phy, MII_ANLPAR))) {
208			if (anlpar & ANLPAR_TX_FD)
209				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
210			else if (anlpar & ANLPAR_T4)
211				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
212			else if (anlpar & ANLPAR_TX)
213				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
214			else if (anlpar & ANLPAR_10_FD)
215				mii->mii_media_active |= IFM_10_T|IFM_FDX;
216			else if (anlpar & ANLPAR_10)
217				mii->mii_media_active |= IFM_10_T|IFM_HDX;
218			else
219				mii->mii_media_active |= IFM_NONE;
220			if ((mii->mii_media_active & IFM_FDX) != 0)
221				mii->mii_media_active |=
222				    mii_phy_flowstatus(phy);
223			return;
224		}
225		/*
226		 * If the other side doesn't support NWAY, then the
227		 * best we can do is determine if we have a 10Mbps or
228		 * 100Mbps link. There's no way to know if the link
229		 * is full or half duplex, so we default to half duplex
230		 * and hope that the user is clever enough to manually
231		 * change the media settings if we're wrong.
232		 */
233
234		/*
235		 * The RealTek PHY supports non-NWAY link speed
236		 * detection, however it does not report the link
237		 * detection results via the ANLPAR or BMSR registers.
238		 * (What? RealTek doesn't do things the way everyone
239		 * else does? I'm just shocked, shocked I tell you.)
240		 * To determine the link speed, we have to do one
241		 * of two things:
242		 *
243		 * - If this is a standalone RealTek RTL8201(L) or
244		 *   workalike PHY, we can determine the link speed by
245		 *   testing bit 0 in the magic, vendor-specific register
246		 *   at offset 0x19.
247		 *
248		 * - If this is a RealTek MAC with integrated PHY, we
249		 *   can test the 'SPEED10' bit of the MAC's media status
250		 *   register.
251		 */
252		if (!(phy->mii_mpd_model == 0 && phy->mii_mpd_rev == 0)) {
253			if (PHY_READ(phy, 0x0019) & 0x01)
254				mii->mii_media_active |= IFM_100_TX;
255			else
256				mii->mii_media_active |= IFM_10_T;
257		} else {
258			if (PHY_READ(phy, RL_MEDIASTAT) &
259			    RL_MEDIASTAT_SPEED10)
260				mii->mii_media_active |= IFM_10_T;
261			else
262				mii->mii_media_active |= IFM_100_TX;
263		}
264		mii->mii_media_active |= IFM_HDX;
265	} else
266		mii->mii_media_active = ife->ifm_media;
267}
268