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
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39/*
40 * driver for Micro Linear 6692 PHYs
41 *
42 * The Micro Linear 6692 is a strange beast, and dealing with it using
43 * this code framework is tricky. The 6692 is actually a 100Mbps-only
44 * device, which means that a second PHY is required to support 10Mbps
45 * modes. However, even though the 6692 does not support 10Mbps modes,
46 * it can still advertise them when performing autonegotiation. If a
47 * 10Mbps mode is negotiated, we must program the registers of the
48 * companion PHY accordingly in addition to programming the registers
49 * of the 6692.
50 *
51 * This device also does not have vendor/device ID registers.
52 */
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/kernel.h>
57#include <sys/socket.h>
58#include <sys/module.h>
59#include <sys/bus.h>
60#include <sys/malloc.h>
61
62#include <net/if.h>
63#include <net/if_media.h>
64
65#include <dev/mii/mii.h>
66#include <dev/mii/miivar.h>
67
68#include "miibus_if.h"
69
70#define ML_STATE_AUTO_SELF	1
71#define ML_STATE_AUTO_OTHER	2
72
73struct mlphy_softc	{
74	struct mii_softc	ml_mii;
75	device_t		ml_dev;
76	int			ml_state;
77	int			ml_linked;
78};
79
80static int mlphy_probe(device_t);
81static int mlphy_attach(device_t);
82
83static device_method_t mlphy_methods[] = {
84	/* device interface */
85	DEVMETHOD(device_probe,		mlphy_probe),
86	DEVMETHOD(device_attach,	mlphy_attach),
87	DEVMETHOD(device_detach,	mii_phy_detach),
88	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
89	DEVMETHOD_END
90};
91
92static devclass_t mlphy_devclass;
93
94static driver_t mlphy_driver = {
95	"mlphy",
96	mlphy_methods,
97	sizeof(struct mlphy_softc)
98};
99
100DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0);
101
102static struct mii_softc *mlphy_find_other(struct mlphy_softc *);
103static int	mlphy_service(struct mii_softc *, struct mii_data *, int);
104static void	mlphy_reset(struct mii_softc *);
105static void	mlphy_status(struct mii_softc *);
106
107static const struct mii_phy_funcs mlphy_funcs = {
108	mlphy_service,
109	mlphy_status,
110	mlphy_reset
111};
112
113static int
114mlphy_probe(dev)
115	device_t		dev;
116{
117	struct mii_attach_args	*ma;
118
119	ma = device_get_ivars(dev);
120
121	/*
122	 * Micro Linear PHY reports oui == 0 model == 0
123	 */
124	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
125	    MII_MODEL(ma->mii_id2) != 0)
126		return (ENXIO);
127
128	/*
129	 * Make sure the parent is a `tl'. So far, I have only
130	 * encountered the 6692 on an Olicom card with a ThunderLAN
131	 * controller chip.
132	 */
133	if (!mii_dev_mac_match(dev, "tl"))
134		return (ENXIO);
135
136	device_set_desc(dev, "Micro Linear 6692 media interface");
137
138	return (BUS_PROBE_DEFAULT);
139}
140
141static int
142mlphy_attach(dev)
143	device_t		dev;
144{
145	struct mlphy_softc *msc;
146	struct mii_softc *sc;
147
148	msc = device_get_softc(dev);
149	sc = &msc->ml_mii;
150	msc->ml_dev = dev;
151	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &mlphy_funcs, 0);
152
153	PHY_RESET(sc);
154
155	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask;
156	/* Let the companion PHY (if any) only handle the media we don't. */
157	sc->mii_capmask = ~sc->mii_capabilities;
158	device_printf(dev, " ");
159	mii_phy_add_media(sc);
160	printf("\n");
161
162	MIIBUS_MEDIAINIT(sc->mii_dev);
163	return (0);
164}
165
166static struct mii_softc *
167mlphy_find_other(struct mlphy_softc *msc)
168{
169	device_t		*devlist;
170	struct mii_softc *retval;
171	int i, devs;
172
173	retval = NULL;
174	if (device_get_children(msc->ml_mii.mii_dev, &devlist, &devs) != 0)
175		return (NULL);
176	for (i = 0; i < devs; i++) {
177		if (devlist[i] != msc->ml_dev) {
178			retval = device_get_softc(devlist[i]);
179			break;
180		}
181	}
182	free(devlist, M_TEMP);
183	return (retval);
184}
185
186static int
187mlphy_service(xsc, mii, cmd)
188	struct mii_softc *xsc;
189	struct mii_data *mii;
190	int cmd;
191{
192	struct ifmedia_entry	*ife = mii->mii_media.ifm_cur;
193	struct mii_softc	*other = NULL;
194	struct mlphy_softc	*msc = (struct mlphy_softc *)xsc;
195	struct mii_softc	*sc = (struct mii_softc *)&msc->ml_mii;
196	int			other_inst, reg;
197
198	/*
199	 * See if there's another PHY on this bus with us.
200	 * If so, we may need it for 10Mbps modes.
201	 */
202	other = mlphy_find_other(msc);
203
204	switch (cmd) {
205	case MII_POLLSTAT:
206		break;
207
208	case MII_MEDIACHG:
209		switch (IFM_SUBTYPE(ife->ifm_media)) {
210		case IFM_AUTO:
211			/*
212			 * For autonegotiation, reset and isolate the
213			 * companion PHY (if any) and then do NWAY
214			 * autonegotiation ourselves.
215			 */
216			msc->ml_state = ML_STATE_AUTO_SELF;
217			if (other != NULL) {
218				PHY_RESET(other);
219				PHY_WRITE(other, MII_BMCR, BMCR_ISO);
220			}
221			(void)mii_phy_auto(sc);
222			msc->ml_linked = 0;
223			return (0);
224		case IFM_10_T:
225		case IFM_100_TX:
226			/*
227			 * For 10baseT and 100baseTX modes, reset and isolate
228			 * the companion PHY (if any), then program ourselves
229			 * accordingly.
230			 */
231			if (other != NULL) {
232				PHY_RESET(other);
233				PHY_WRITE(other, MII_BMCR, BMCR_ISO);
234			}
235			mii_phy_setmedia(sc);
236			msc->ml_state = 0;
237			break;
238		default:
239			return (EINVAL);
240		}
241		break;
242
243	case MII_TICK:
244		/*
245		 * Only used for autonegotiation.
246		 */
247		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
248			break;
249
250		/*
251		 * Check to see if we have link.  If we do, we don't
252		 * need to restart the autonegotiation process.  Read
253		 * the BMSR twice in case it's latched.
254		 * If we're in a 10Mbps mode, check the link of the
255		 * 10Mbps PHY. Sometimes the Micro Linear PHY's
256		 * linkstat bit will clear while the linkstat bit of
257		 * the companion PHY will remain set.
258		 */
259		if (msc->ml_state == ML_STATE_AUTO_OTHER) {
260			reg = PHY_READ(other, MII_BMSR) |
261			    PHY_READ(other, MII_BMSR);
262		} else {
263			reg = PHY_READ(sc, MII_BMSR) |
264			    PHY_READ(sc, MII_BMSR);
265		}
266
267		if (reg & BMSR_LINK) {
268			if (!msc->ml_linked) {
269				msc->ml_linked = 1;
270				PHY_STATUS(sc);
271			}
272			break;
273		}
274
275		/*
276		 * Only retry autonegotiation every 5 seconds.
277		 */
278		if (++sc->mii_ticks <= MII_ANEGTICKS)
279			break;
280
281		sc->mii_ticks = 0;
282		msc->ml_linked = 0;
283		mii->mii_media_active = IFM_NONE;
284		PHY_RESET(sc);
285		msc->ml_state = ML_STATE_AUTO_SELF;
286		if (other != NULL) {
287			PHY_RESET(other);
288			PHY_WRITE(other, MII_BMCR, BMCR_ISO);
289		}
290		mii_phy_auto(sc);
291		return (0);
292	}
293
294	/* Update the media status. */
295
296	if (msc->ml_state == ML_STATE_AUTO_OTHER) {
297		other_inst = other->mii_inst;
298		other->mii_inst = sc->mii_inst;
299		if (IFM_INST(ife->ifm_media) == other->mii_inst)
300			(void)PHY_SERVICE(other, mii, MII_POLLSTAT);
301		other->mii_inst = other_inst;
302		sc->mii_media_active = other->mii_media_active;
303		sc->mii_media_status = other->mii_media_status;
304	} else
305		ukphy_status(sc);
306
307	/* Callback if something changed. */
308	mii_phy_update(sc, cmd);
309	return (0);
310}
311
312/*
313 * The Micro Linear PHY comes out of reset with the 'autoneg
314 * enable' bit set, which we don't want.
315 */
316static void
317mlphy_reset(sc)
318	struct mii_softc	*sc;
319{
320	int			reg;
321
322	mii_phy_reset(sc);
323	reg = PHY_READ(sc, MII_BMCR);
324	reg &= ~BMCR_AUTOEN;
325	PHY_WRITE(sc, MII_BMCR, reg);
326}
327
328/*
329 * If we negotiate a 10Mbps mode, we need to check for an alternate
330 * PHY and make sure it's enabled and set correctly.
331 */
332static void
333mlphy_status(sc)
334	struct mii_softc	*sc;
335{
336	struct mlphy_softc	*msc = (struct mlphy_softc *)sc;
337	struct mii_data		*mii = msc->ml_mii.mii_pdata;
338	struct mii_softc	*other = NULL;
339
340	/* See if there's another PHY on the bus with us. */
341	other = mlphy_find_other(msc);
342	if (other == NULL)
343		return;
344
345	ukphy_status(sc);
346
347	if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
348		msc->ml_state = ML_STATE_AUTO_SELF;
349		PHY_RESET(other);
350		PHY_WRITE(other, MII_BMCR, BMCR_ISO);
351	}
352
353	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
354		msc->ml_state = ML_STATE_AUTO_OTHER;
355		PHY_RESET(&msc->ml_mii);
356		PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO);
357		PHY_RESET(other);
358		mii_phy_auto(other);
359	}
360}
361