1/*-
2 * Copyright (c) 2006 Benno Rice.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include <sys/cdefs.h>
26__FBSDID("$FreeBSD$");
27
28/*
29 * Driver for the internal PHY on the SMSC LAN91C111.
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/socket.h>
36#include <sys/errno.h>
37#include <sys/module.h>
38#include <sys/bus.h>
39#include <sys/malloc.h>
40
41#include <machine/bus.h>
42
43#include <net/if.h>
44#include <net/if_media.h>
45
46#include <dev/mii/mii.h>
47#include <dev/mii/miivar.h>
48#include "miidevs.h"
49
50#include "miibus_if.h"
51
52static int	smcphy_probe(device_t);
53static int	smcphy_attach(device_t);
54
55static int	smcphy_service(struct mii_softc *, struct mii_data *, int);
56static void	smcphy_reset(struct mii_softc *);
57static void	smcphy_auto(struct mii_softc *, int);
58static void	smcphy_status(struct mii_softc *);
59
60static device_method_t smcphy_methods[] = {
61	/* device interface */
62	DEVMETHOD(device_probe,		smcphy_probe),
63	DEVMETHOD(device_attach,	smcphy_attach),
64	DEVMETHOD(device_detach,	mii_phy_detach),
65	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
66	DEVMETHOD_END
67};
68
69static devclass_t smcphy_devclass;
70
71static driver_t smcphy_driver = {
72	"smcphy",
73	smcphy_methods,
74	sizeof(struct mii_softc)
75};
76
77DRIVER_MODULE(smcphy, miibus, smcphy_driver, smcphy_devclass, 0, 0);
78
79static const struct mii_phydesc smcphys[] = {
80	MII_PHY_DESC(SEEQ, 80220),
81	MII_PHY_DESC(SEEQ, 84220),
82	MII_PHY_END
83};
84
85static const struct mii_phy_funcs smcphy80220_funcs = {
86	smcphy_service,
87	smcphy_status,
88	mii_phy_reset
89};
90
91static const struct mii_phy_funcs smcphy_funcs = {
92	smcphy_service,
93	smcphy_status,
94	smcphy_reset
95};
96
97static int
98smcphy_probe(device_t dev)
99{
100
101	return (mii_phy_dev_probe(dev, smcphys, BUS_PROBE_DEFAULT));
102}
103
104static int
105smcphy_attach(device_t dev)
106{
107	struct mii_softc *sc;
108	struct mii_attach_args *ma;
109	const struct mii_phy_funcs *mpf;
110
111	sc = device_get_softc(dev);
112	ma = device_get_ivars(dev);
113	if (MII_MODEL(ma->mii_id2) == MII_MODEL_SEEQ_80220)
114		mpf = &smcphy80220_funcs;
115	else
116		mpf = &smcphy_funcs;
117	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE, mpf, 1);
118	mii_phy_setmedia(sc);
119
120	return (0);
121}
122
123static int
124smcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
125{
126        struct	ifmedia_entry *ife;
127        int	reg;
128
129	ife = mii->mii_media.ifm_cur;
130
131        switch (cmd) {
132        case MII_POLLSTAT:
133                break;
134
135        case MII_MEDIACHG:
136                /*
137                 * If the interface is not up, don't do anything.
138                 */
139                if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
140                        break;
141
142		switch (IFM_SUBTYPE(ife->ifm_media)) {
143		case IFM_AUTO:
144			smcphy_auto(sc, ife->ifm_media);
145			break;
146
147		default:
148                	mii_phy_setmedia(sc);
149			break;
150		}
151
152                break;
153
154        case MII_TICK:
155		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
156			return (0);
157		}
158
159		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
160			break;
161		}
162
163		/* I have no idea why BMCR_ISO gets set. */
164		reg = PHY_READ(sc, MII_BMCR);
165		if (reg & BMCR_ISO) {
166			PHY_WRITE(sc, MII_BMCR, reg & ~BMCR_ISO);
167		}
168
169		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
170		if (reg & BMSR_LINK) {
171			sc->mii_ticks = 0;
172			break;
173		}
174
175		if (++sc->mii_ticks <= MII_ANEGTICKS) {
176			break;
177		}
178
179		sc->mii_ticks = 0;
180		PHY_RESET(sc);
181		smcphy_auto(sc, ife->ifm_media);
182                break;
183        }
184
185        /* Update the media status. */
186        PHY_STATUS(sc);
187
188        /* Callback if something changed. */
189        mii_phy_update(sc, cmd);
190        return (0);
191}
192
193static void
194smcphy_reset(struct mii_softc *sc)
195{
196	u_int	bmcr;
197	int	timeout;
198
199	PHY_WRITE(sc, MII_BMCR, BMCR_RESET);
200
201	for (timeout = 2; timeout > 0; timeout--) {
202		DELAY(50000);
203		bmcr = PHY_READ(sc, MII_BMCR);
204		if ((bmcr & BMCR_RESET) == 0)
205			break;
206	}
207
208	if (bmcr & BMCR_RESET)
209		device_printf(sc->mii_dev, "reset failed\n");
210
211	PHY_WRITE(sc, MII_BMCR, 0x3000);
212
213	/* Mask interrupts, we poll instead. */
214	PHY_WRITE(sc, 0x1e, 0xffc0);
215}
216
217static void
218smcphy_auto(struct mii_softc *sc, int media)
219{
220	uint16_t	anar;
221
222	anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
223	if ((media & IFM_FLOW) != 0 || (sc->mii_flags & MIIF_FORCEPAUSE) != 0)
224		anar |= ANAR_FC;
225	PHY_WRITE(sc, MII_ANAR, anar);
226	/* Apparently this helps. */
227	anar = PHY_READ(sc, MII_ANAR);
228	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
229}
230
231static void
232smcphy_status(struct mii_softc *sc)
233{
234	struct mii_data *mii;
235	uint32_t bmcr, bmsr, status;
236
237	mii = sc->mii_pdata;
238	mii->mii_media_status = IFM_AVALID;
239	mii->mii_media_active = IFM_ETHER;
240
241	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
242	if ((bmsr & BMSR_LINK) != 0)
243		mii->mii_media_status |= IFM_ACTIVE;
244
245	bmcr = PHY_READ(sc, MII_BMCR);
246	if ((bmcr & BMCR_ISO) != 0) {
247		mii->mii_media_active |= IFM_NONE;
248		mii->mii_media_status = 0;
249		return;
250	}
251
252	if ((bmcr & BMCR_LOOP) != 0)
253		mii->mii_media_active |= IFM_LOOP;
254
255	if ((bmcr & BMCR_AUTOEN) != 0) {
256		if ((bmsr & BMSR_ACOMP) == 0) {
257			/* Erg, still trying, I guess... */
258			mii->mii_media_active |= IFM_NONE;
259			return;
260		}
261	}
262
263	status = PHY_READ(sc, 0x12);
264	if (status & 0x0080)
265		mii->mii_media_active |= IFM_100_TX;
266	else
267		mii->mii_media_active |= IFM_10_T;
268	if (status & 0x0040)
269		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
270	else
271		mii->mii_media_active |= IFM_HDX;
272}
273