2 * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Driver for the JMicron JMP211 10/100/1000, JMP202 10/100 PHY.
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>
43 #include <net/if_var.h>
44 #include <net/if_media.h>
46 #include <dev/mii/mii.h>
47 #include <dev/mii/miivar.h>
50 #include <dev/mii/jmphyreg.h>
52 #include "miibus_if.h"
54 static int jmphy_probe(device_t);
55 static int jmphy_attach(device_t);
56 static void jmphy_reset(struct mii_softc *);
57 static uint16_t jmphy_anar(struct ifmedia_entry *);
58 static int jmphy_setmedia(struct mii_softc *, struct ifmedia_entry *);
60 static device_method_t jmphy_methods[] = {
61 /* Device interface. */
62 DEVMETHOD(device_probe, jmphy_probe),
63 DEVMETHOD(device_attach, jmphy_attach),
64 DEVMETHOD(device_detach, mii_phy_detach),
65 DEVMETHOD(device_shutdown, bus_generic_shutdown),
69 static devclass_t jmphy_devclass;
70 static driver_t jmphy_driver = {
73 sizeof(struct mii_softc)
76 DRIVER_MODULE(jmphy, miibus, jmphy_driver, jmphy_devclass, 0, 0);
78 static int jmphy_service(struct mii_softc *, struct mii_data *, int);
79 static void jmphy_status(struct mii_softc *);
81 static const struct mii_phydesc jmphys[] = {
82 MII_PHY_DESC(JMICRON, JMP202),
83 MII_PHY_DESC(JMICRON, JMP211),
87 static const struct mii_phy_funcs jmphy_funcs = {
94 jmphy_probe(device_t dev)
97 return (mii_phy_dev_probe(dev, jmphys, BUS_PROBE_DEFAULT));
101 jmphy_attach(device_t dev)
103 struct mii_attach_args *ma;
106 ma = device_get_ivars(dev);
108 if (strcmp(ma->mii_data->mii_ifp->if_dname, "jme") == 0 &&
109 (miibus_get_flags(dev) & MIIF_MACPRIV0) != 0)
110 flags |= MIIF_PHYPRIV0;
111 mii_phy_dev_attach(dev, flags, &jmphy_funcs, 1);
116 jmphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
118 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
125 if (jmphy_setmedia(sc, ife) != EJUSTRETURN)
131 * Only used for autonegotiation.
133 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
138 /* Check for link. */
139 if ((PHY_READ(sc, JMPHY_SSR) & JMPHY_SSR_LINK_UP) != 0) {
144 /* Announce link loss right after it happens. */
145 if (sc->mii_ticks++ == 0)
147 if (sc->mii_ticks <= sc->mii_anegticks)
151 (void)jmphy_setmedia(sc, ife);
155 /* Update the media status. */
158 /* Callback if something changed. */
159 mii_phy_update(sc, cmd);
164 jmphy_status(struct mii_softc *sc)
166 struct mii_data *mii = sc->mii_pdata;
169 mii->mii_media_status = IFM_AVALID;
170 mii->mii_media_active = IFM_ETHER;
172 ssr = PHY_READ(sc, JMPHY_SSR);
173 if ((ssr & JMPHY_SSR_LINK_UP) != 0)
174 mii->mii_media_status |= IFM_ACTIVE;
176 bmcr = PHY_READ(sc, MII_BMCR);
177 if ((bmcr & BMCR_ISO) != 0) {
178 mii->mii_media_active |= IFM_NONE;
179 mii->mii_media_status = 0;
183 if ((bmcr & BMCR_LOOP) != 0)
184 mii->mii_media_active |= IFM_LOOP;
186 if ((ssr & JMPHY_SSR_SPD_DPLX_RESOLVED) == 0) {
187 /* Erg, still trying, I guess... */
188 mii->mii_media_active |= IFM_NONE;
192 switch ((ssr & JMPHY_SSR_SPEED_MASK)) {
193 case JMPHY_SSR_SPEED_1000:
194 mii->mii_media_active |= IFM_1000_T;
196 * jmphy(4) got a valid link so reset mii_ticks.
197 * Resetting mii_ticks is needed in order to
198 * detect link loss after auto-negotiation.
202 case JMPHY_SSR_SPEED_100:
203 mii->mii_media_active |= IFM_100_TX;
206 case JMPHY_SSR_SPEED_10:
207 mii->mii_media_active |= IFM_10_T;
211 mii->mii_media_active |= IFM_NONE;
215 if ((ssr & JMPHY_SSR_DUPLEX) != 0)
216 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
218 mii->mii_media_active |= IFM_HDX;
220 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
221 if ((PHY_READ(sc, MII_100T2SR) & GTSR_MS_RES) != 0)
222 mii->mii_media_active |= IFM_ETH_MASTER;
227 jmphy_reset(struct mii_softc *sc)
232 /* Disable sleep mode. */
233 PHY_WRITE(sc, JMPHY_TMCTL,
234 PHY_READ(sc, JMPHY_TMCTL) & ~JMPHY_TMCTL_SLEEP_ENB);
235 PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN);
237 for (i = 0; i < 1000; i++) {
239 if ((PHY_READ(sc, MII_BMCR) & BMCR_RESET) == 0)
242 /* Perform vendor recommended PHY calibration. */
243 if ((sc->mii_flags & MIIF_PHYPRIV0) != 0) {
244 /* Select PHY test mode 1. */
245 t2cr = PHY_READ(sc, MII_100T2CR);
246 t2cr &= ~GTCR_TEST_MASK;
248 PHY_WRITE(sc, MII_100T2CR, t2cr);
249 /* Apply calibration patch. */
250 PHY_WRITE(sc, JMPHY_SPEC_ADDR, JMPHY_SPEC_ADDR_READ |
252 val = PHY_READ(sc, JMPHY_SPEC_DATA);
254 val |= 0x0010 | 0x0001;
255 PHY_WRITE(sc, JMPHY_SPEC_DATA, val);
256 PHY_WRITE(sc, JMPHY_SPEC_ADDR, JMPHY_SPEC_ADDR_WRITE |
259 /* XXX 20ms to complete recalibration. */
262 PHY_READ(sc, MII_100T2CR);
263 PHY_WRITE(sc, JMPHY_SPEC_ADDR, JMPHY_SPEC_ADDR_READ |
265 val = PHY_READ(sc, JMPHY_SPEC_DATA);
266 val &= ~(0x0001 | 0x0002 | 0x0010);
267 PHY_WRITE(sc, JMPHY_SPEC_DATA, val);
268 PHY_WRITE(sc, JMPHY_SPEC_ADDR, JMPHY_SPEC_ADDR_WRITE |
270 /* Disable PHY test mode. */
271 PHY_READ(sc, MII_100T2CR);
272 t2cr &= ~GTCR_TEST_MASK;
273 PHY_WRITE(sc, MII_100T2CR, t2cr);
278 jmphy_anar(struct ifmedia_entry *ife)
283 switch (IFM_SUBTYPE(ife->ifm_media)) {
285 anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10;
290 anar |= ANAR_TX | ANAR_TX_FD;
293 anar |= ANAR_10 | ANAR_10_FD;
303 jmphy_setmedia(struct mii_softc *sc, struct ifmedia_entry *ife)
305 uint16_t anar, bmcr, gig;
308 bmcr = PHY_READ(sc, MII_BMCR);
309 switch (IFM_SUBTYPE(ife->ifm_media)) {
311 gig |= GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
314 gig |= GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
320 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO | BMCR_PDOWN);
321 return (EJUSTRETURN);
326 anar = jmphy_anar(ife);
327 if ((IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO ||
328 (ife->ifm_media & IFM_FDX) != 0) &&
329 ((ife->ifm_media & IFM_FLOW) != 0 ||
330 (sc->mii_flags & MIIF_FORCEPAUSE) != 0))
331 anar |= ANAR_PAUSE_TOWARDS;
333 if ((sc->mii_flags & MIIF_HAVE_GTCR) != 0) {
334 if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
336 if ((ife->ifm_media & IFM_ETH_MASTER) != 0)
339 PHY_WRITE(sc, MII_100T2CR, gig);
341 PHY_WRITE(sc, MII_ANAR, anar | ANAR_CSMA);
342 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
344 return (EJUSTRETURN);