1694c6518SBenno Rice /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4694c6518SBenno Rice * Copyright (c) 2006 Benno Rice. All rights reserved.
5694c6518SBenno Rice *
6694c6518SBenno Rice * Redistribution and use in source and binary forms, with or without
7694c6518SBenno Rice * modification, are permitted provided that the following conditions
8694c6518SBenno Rice * are met:
9694c6518SBenno Rice * 1. Redistributions of source code must retain the above copyright
10694c6518SBenno Rice * notice, this list of conditions and the following disclaimer.
11694c6518SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright
12694c6518SBenno Rice * notice, this list of conditions and the following disclaimer in the
13694c6518SBenno Rice * documentation and/or other materials provided with the distribution.
14694c6518SBenno Rice *
15694c6518SBenno Rice * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16694c6518SBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17694c6518SBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18694c6518SBenno Rice * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19694c6518SBenno Rice * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20694c6518SBenno Rice * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21694c6518SBenno Rice * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22694c6518SBenno Rice * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23694c6518SBenno Rice * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24694c6518SBenno Rice * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25694c6518SBenno Rice */
26694c6518SBenno Rice
27694c6518SBenno Rice #include <sys/cdefs.h>
28694c6518SBenno Rice /*
29b011f8c4SOleksandr Tymoshenko * Driver for the SEEQ 80220 and 84220.
30b011f8c4SOleksandr Tymoshenko * (Originally developed for the internal PHY on the SMSC LAN91C111.)
31694c6518SBenno Rice */
32694c6518SBenno Rice
33694c6518SBenno Rice #include <sys/param.h>
34694c6518SBenno Rice #include <sys/systm.h>
35694c6518SBenno Rice #include <sys/kernel.h>
36694c6518SBenno Rice #include <sys/socket.h>
37694c6518SBenno Rice #include <sys/errno.h>
38694c6518SBenno Rice #include <sys/module.h>
39694c6518SBenno Rice #include <sys/bus.h>
40694c6518SBenno Rice #include <sys/malloc.h>
41694c6518SBenno Rice
42694c6518SBenno Rice #include <machine/bus.h>
43694c6518SBenno Rice
44694c6518SBenno Rice #include <net/if.h>
45694c6518SBenno Rice #include <net/if_media.h>
46694c6518SBenno Rice
47694c6518SBenno Rice #include <dev/mii/mii.h>
48694c6518SBenno Rice #include <dev/mii/miivar.h>
49694c6518SBenno Rice #include "miidevs.h"
50694c6518SBenno Rice
51694c6518SBenno Rice #include "miibus_if.h"
52694c6518SBenno Rice
53694c6518SBenno Rice static int smcphy_probe(device_t);
54694c6518SBenno Rice static int smcphy_attach(device_t);
55694c6518SBenno Rice
56694c6518SBenno Rice static int smcphy_service(struct mii_softc *, struct mii_data *, int);
573fcb7a53SMarius Strobl static void smcphy_reset(struct mii_softc *);
58efd4fc3fSMarius Strobl static void smcphy_auto(struct mii_softc *, int);
5934fe3828SPyun YongHyeon static void smcphy_status(struct mii_softc *);
60694c6518SBenno Rice
61694c6518SBenno Rice static device_method_t smcphy_methods[] = {
62694c6518SBenno Rice /* device interface */
63694c6518SBenno Rice DEVMETHOD(device_probe, smcphy_probe),
64694c6518SBenno Rice DEVMETHOD(device_attach, smcphy_attach),
65694c6518SBenno Rice DEVMETHOD(device_detach, mii_phy_detach),
66694c6518SBenno Rice DEVMETHOD(device_shutdown, bus_generic_shutdown),
67604f5f1fSMarius Strobl DEVMETHOD_END
68694c6518SBenno Rice };
69694c6518SBenno Rice
70694c6518SBenno Rice static driver_t smcphy_driver = {
71694c6518SBenno Rice "smcphy",
72694c6518SBenno Rice smcphy_methods,
73694c6518SBenno Rice sizeof(struct mii_softc)
74694c6518SBenno Rice };
75694c6518SBenno Rice
76f438c2ffSJohn Baldwin DRIVER_MODULE(smcphy, miibus, smcphy_driver, 0, 0);
77694c6518SBenno Rice
78f6613debSMarius Strobl static const struct mii_phydesc smcphys[] = {
7934fe3828SPyun YongHyeon MII_PHY_DESC(SEEQ, 80220),
803fcb7a53SMarius Strobl MII_PHY_DESC(SEEQ, 84220),
81f6613debSMarius Strobl MII_PHY_END
82f6613debSMarius Strobl };
83f6613debSMarius Strobl
8434fe3828SPyun YongHyeon static const struct mii_phy_funcs smcphy80220_funcs = {
8534fe3828SPyun YongHyeon smcphy_service,
8634fe3828SPyun YongHyeon smcphy_status,
8734fe3828SPyun YongHyeon mii_phy_reset
8834fe3828SPyun YongHyeon };
8934fe3828SPyun YongHyeon
903fcb7a53SMarius Strobl static const struct mii_phy_funcs smcphy_funcs = {
913fcb7a53SMarius Strobl smcphy_service,
9234fe3828SPyun YongHyeon smcphy_status,
933fcb7a53SMarius Strobl smcphy_reset
943fcb7a53SMarius Strobl };
953fcb7a53SMarius Strobl
96694c6518SBenno Rice static int
smcphy_probe(device_t dev)97694c6518SBenno Rice smcphy_probe(device_t dev)
98694c6518SBenno Rice {
99694c6518SBenno Rice
100f6613debSMarius Strobl return (mii_phy_dev_probe(dev, smcphys, BUS_PROBE_DEFAULT));
101694c6518SBenno Rice }
102694c6518SBenno Rice
103694c6518SBenno Rice static int
smcphy_attach(device_t dev)104694c6518SBenno Rice smcphy_attach(device_t dev)
105694c6518SBenno Rice {
106694c6518SBenno Rice struct mii_softc *sc;
10734fe3828SPyun YongHyeon struct mii_attach_args *ma;
10834fe3828SPyun YongHyeon const struct mii_phy_funcs *mpf;
109694c6518SBenno Rice
110694c6518SBenno Rice sc = device_get_softc(dev);
11134fe3828SPyun YongHyeon ma = device_get_ivars(dev);
11234fe3828SPyun YongHyeon if (MII_MODEL(ma->mii_id2) == MII_MODEL_SEEQ_80220)
11334fe3828SPyun YongHyeon mpf = &smcphy80220_funcs;
11434fe3828SPyun YongHyeon else
11534fe3828SPyun YongHyeon mpf = &smcphy_funcs;
11634fe3828SPyun YongHyeon mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE, mpf, 1);
117694c6518SBenno Rice mii_phy_setmedia(sc);
118694c6518SBenno Rice
119694c6518SBenno Rice return (0);
120694c6518SBenno Rice }
121694c6518SBenno Rice
122694c6518SBenno Rice static int
smcphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)123694c6518SBenno Rice smcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
124694c6518SBenno Rice {
125694c6518SBenno Rice struct ifmedia_entry *ife;
126694c6518SBenno Rice int reg;
127694c6518SBenno Rice
128694c6518SBenno Rice ife = mii->mii_media.ifm_cur;
129694c6518SBenno Rice
130694c6518SBenno Rice switch (cmd) {
131694c6518SBenno Rice case MII_POLLSTAT:
132694c6518SBenno Rice break;
133694c6518SBenno Rice
134694c6518SBenno Rice case MII_MEDIACHG:
135694c6518SBenno Rice switch (IFM_SUBTYPE(ife->ifm_media)) {
136694c6518SBenno Rice case IFM_AUTO:
137efd4fc3fSMarius Strobl smcphy_auto(sc, ife->ifm_media);
138694c6518SBenno Rice break;
139694c6518SBenno Rice
140694c6518SBenno Rice default:
141694c6518SBenno Rice mii_phy_setmedia(sc);
142694c6518SBenno Rice break;
143694c6518SBenno Rice }
144694c6518SBenno Rice
145694c6518SBenno Rice break;
146694c6518SBenno Rice
147694c6518SBenno Rice case MII_TICK:
148694c6518SBenno Rice if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
149694c6518SBenno Rice break;
150694c6518SBenno Rice }
151694c6518SBenno Rice
152694c6518SBenno Rice /* I have no idea why BMCR_ISO gets set. */
153694c6518SBenno Rice reg = PHY_READ(sc, MII_BMCR);
154694c6518SBenno Rice if (reg & BMCR_ISO) {
155694c6518SBenno Rice PHY_WRITE(sc, MII_BMCR, reg & ~BMCR_ISO);
156694c6518SBenno Rice }
157694c6518SBenno Rice
158694c6518SBenno Rice reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
159694c6518SBenno Rice if (reg & BMSR_LINK) {
160694c6518SBenno Rice sc->mii_ticks = 0;
161694c6518SBenno Rice break;
162694c6518SBenno Rice }
163694c6518SBenno Rice
164694c6518SBenno Rice if (++sc->mii_ticks <= MII_ANEGTICKS) {
165694c6518SBenno Rice break;
166694c6518SBenno Rice }
167694c6518SBenno Rice
168694c6518SBenno Rice sc->mii_ticks = 0;
1693fcb7a53SMarius Strobl PHY_RESET(sc);
170efd4fc3fSMarius Strobl smcphy_auto(sc, ife->ifm_media);
171694c6518SBenno Rice break;
172694c6518SBenno Rice }
173694c6518SBenno Rice
174694c6518SBenno Rice /* Update the media status. */
1753fcb7a53SMarius Strobl PHY_STATUS(sc);
176694c6518SBenno Rice
177694c6518SBenno Rice /* Callback if something changed. */
178694c6518SBenno Rice mii_phy_update(sc, cmd);
179694c6518SBenno Rice return (0);
180694c6518SBenno Rice }
181694c6518SBenno Rice
1823fcb7a53SMarius Strobl static void
smcphy_reset(struct mii_softc * sc)183694c6518SBenno Rice smcphy_reset(struct mii_softc *sc)
184694c6518SBenno Rice {
185694c6518SBenno Rice u_int bmcr;
186694c6518SBenno Rice int timeout;
187694c6518SBenno Rice
188694c6518SBenno Rice PHY_WRITE(sc, MII_BMCR, BMCR_RESET);
189694c6518SBenno Rice
190694c6518SBenno Rice for (timeout = 2; timeout > 0; timeout--) {
191694c6518SBenno Rice DELAY(50000);
192694c6518SBenno Rice bmcr = PHY_READ(sc, MII_BMCR);
193694c6518SBenno Rice if ((bmcr & BMCR_RESET) == 0)
194694c6518SBenno Rice break;
195694c6518SBenno Rice }
196694c6518SBenno Rice
1973fcb7a53SMarius Strobl if (bmcr & BMCR_RESET)
1983fcb7a53SMarius Strobl device_printf(sc->mii_dev, "reset failed\n");
199694c6518SBenno Rice
200694c6518SBenno Rice PHY_WRITE(sc, MII_BMCR, 0x3000);
2013fcb7a53SMarius Strobl
2023fcb7a53SMarius Strobl /* Mask interrupts, we poll instead. */
2033fcb7a53SMarius Strobl PHY_WRITE(sc, 0x1e, 0xffc0);
204694c6518SBenno Rice }
205694c6518SBenno Rice
206694c6518SBenno Rice static void
smcphy_auto(struct mii_softc * sc,int media)207efd4fc3fSMarius Strobl smcphy_auto(struct mii_softc *sc, int media)
208694c6518SBenno Rice {
209694c6518SBenno Rice uint16_t anar;
210694c6518SBenno Rice
211604f5f1fSMarius Strobl anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
212efd4fc3fSMarius Strobl if ((media & IFM_FLOW) != 0 || (sc->mii_flags & MIIF_FORCEPAUSE) != 0)
213694c6518SBenno Rice anar |= ANAR_FC;
214694c6518SBenno Rice PHY_WRITE(sc, MII_ANAR, anar);
215694c6518SBenno Rice /* Apparently this helps. */
216694c6518SBenno Rice anar = PHY_READ(sc, MII_ANAR);
217694c6518SBenno Rice PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
218694c6518SBenno Rice }
21934fe3828SPyun YongHyeon
22034fe3828SPyun YongHyeon static void
smcphy_status(struct mii_softc * sc)22134fe3828SPyun YongHyeon smcphy_status(struct mii_softc *sc)
22234fe3828SPyun YongHyeon {
22334fe3828SPyun YongHyeon struct mii_data *mii;
22434fe3828SPyun YongHyeon uint32_t bmcr, bmsr, status;
22534fe3828SPyun YongHyeon
22634fe3828SPyun YongHyeon mii = sc->mii_pdata;
22734fe3828SPyun YongHyeon mii->mii_media_status = IFM_AVALID;
22834fe3828SPyun YongHyeon mii->mii_media_active = IFM_ETHER;
22934fe3828SPyun YongHyeon
23034fe3828SPyun YongHyeon bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
23134fe3828SPyun YongHyeon if ((bmsr & BMSR_LINK) != 0)
23234fe3828SPyun YongHyeon mii->mii_media_status |= IFM_ACTIVE;
23334fe3828SPyun YongHyeon
23434fe3828SPyun YongHyeon bmcr = PHY_READ(sc, MII_BMCR);
23534fe3828SPyun YongHyeon if ((bmcr & BMCR_ISO) != 0) {
23634fe3828SPyun YongHyeon mii->mii_media_active |= IFM_NONE;
23734fe3828SPyun YongHyeon mii->mii_media_status = 0;
23834fe3828SPyun YongHyeon return;
23934fe3828SPyun YongHyeon }
24034fe3828SPyun YongHyeon
24134fe3828SPyun YongHyeon if ((bmcr & BMCR_LOOP) != 0)
24234fe3828SPyun YongHyeon mii->mii_media_active |= IFM_LOOP;
24334fe3828SPyun YongHyeon
24434fe3828SPyun YongHyeon if ((bmcr & BMCR_AUTOEN) != 0) {
24534fe3828SPyun YongHyeon if ((bmsr & BMSR_ACOMP) == 0) {
24634fe3828SPyun YongHyeon /* Erg, still trying, I guess... */
24734fe3828SPyun YongHyeon mii->mii_media_active |= IFM_NONE;
24834fe3828SPyun YongHyeon return;
24934fe3828SPyun YongHyeon }
25034fe3828SPyun YongHyeon }
25134fe3828SPyun YongHyeon
25234fe3828SPyun YongHyeon status = PHY_READ(sc, 0x12);
25334fe3828SPyun YongHyeon if (status & 0x0080)
25434fe3828SPyun YongHyeon mii->mii_media_active |= IFM_100_TX;
25534fe3828SPyun YongHyeon else
25634fe3828SPyun YongHyeon mii->mii_media_active |= IFM_10_T;
25734fe3828SPyun YongHyeon if (status & 0x0040)
25834fe3828SPyun YongHyeon mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
25934fe3828SPyun YongHyeon else
26034fe3828SPyun YongHyeon mii->mii_media_active |= IFM_HDX;
26134fe3828SPyun YongHyeon }
262