1 /* $NetBSD: smscphy.c,v 1.4 2021/08/25 21:50:29 andvar Exp $ */
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 2006 Benno Rice.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* $FreeBSD: head/sys/dev/mii/smscphy.c 326255 2017-11-27 14:52:40Z pfg $ */
30 
31 /*
32  * Driver for the SMSC LAN8710A
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: smscphy.c,v 1.4 2021/08/25 21:50:29 andvar Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/socket.h>
43 #include <sys/errno.h>
44 
45 #include <net/if.h>
46 #include <net/if_media.h>
47 
48 #include <dev/mii/mii.h>
49 #include <dev/mii/miivar.h>
50 #include <dev/mii/miidevs.h>
51 
52 /* PHY special control/status register */
53 #define SMSCPHY_SPCSR         0x1f
54 #define  SPCSR_SPDIND_10      0x0004
55 #define  SPCSR_SPDIND_100     0x0008
56 #define  SPCSR_SPDIND_SPDMASK 0x000c
57 #define  SPCSR_SPDIND_FDX     0x0010
58 
59 static int          smscphy_match(device_t, cfdata_t, void *);
60 static void         smscphy_attach(device_t, device_t, void *);
61 
62 CFATTACH_DECL_NEW(smscphy, sizeof (struct mii_softc),
63     smscphy_match, smscphy_attach, mii_phy_detach, mii_phy_activate);
64 
65 static void         smscphy_power(struct mii_softc *, bool);
66 static int          smscphy_service(struct mii_softc *, struct mii_data *, int);
67 static void         smscphy_auto(struct mii_softc *, int);
68 static void         smscphy_status(struct mii_softc *);
69 
70 static const struct mii_phydesc smscphys[] = {
71           MII_PHY_DESC(SMSC, LAN8700),
72           MII_PHY_DESC(SMSC, LAN8710_LAN8720),
73           MII_PHY_END
74 };
75 
76 static const struct mii_phy_funcs smscphy_funcs = {
77           smscphy_service,
78           smscphy_status,
79           mii_phy_reset
80 };
81 
82 static int
smscphy_match(device_t dev,cfdata_t match,void * aux)83 smscphy_match(device_t dev, cfdata_t match, void *aux)
84 {
85           struct mii_attach_args *ma = aux;
86 
87           if (mii_phy_match(ma, smscphys) != NULL)
88                     return 10;
89 
90           return 0;
91 }
92 
93 static void
smscphy_attach(device_t parent,device_t self,void * aux)94 smscphy_attach(device_t parent, device_t self, void *aux)
95 {
96           struct mii_softc *sc = device_private(self);
97           struct mii_attach_args *ma = aux;
98           struct mii_data *mii = ma->mii_data;
99           const struct mii_phydesc *mpd;
100 
101           mpd = mii_phy_match(ma, smscphys);
102           aprint_naive(": Media interface\n");
103           aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
104 
105           sc->mii_dev = self;
106           sc->mii_inst = mii->mii_instance;
107           sc->mii_phy = ma->mii_phyno;
108           sc->mii_funcs = &smscphy_funcs;
109           sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
110           sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
111           sc->mii_mpd_rev = MII_REV(ma->mii_id2);
112           sc->mii_pdata = mii;
113           sc->mii_flags = ma->mii_flags;
114 
115           mii_lock(mii);
116 
117           PHY_RESET(sc);
118 
119           PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
120           sc->mii_capabilities &= ma->mii_capmask;
121 
122           mii_unlock(mii);
123 
124           mii_phy_add_media(sc);
125 }
126 
127 static void
smscphy_power(struct mii_softc * sc,bool power)128 smscphy_power(struct mii_softc *sc, bool power)
129 {
130           uint16_t bmcr, new;
131 
132           PHY_READ(sc, MII_BMCR, &bmcr);
133           if (power)
134                     new = bmcr & ~BMCR_PDOWN;
135           else
136                     new = bmcr | BMCR_PDOWN;
137           if (bmcr != new)
138                     PHY_WRITE(sc, MII_BMCR, new);
139 }
140 
141 static int
smscphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)142 smscphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
143 {
144           struct    ifmedia_entry *ife;
145           uint16_t reg;
146 
147           KASSERT(mii_locked(mii));
148 
149           ife = mii->mii_media.ifm_cur;
150 
151           switch (cmd) {
152           case MII_POLLSTAT:
153                     break;
154 
155           case MII_MEDIACHG:
156                     /* Try to power up the PHY in case it's down */
157                     if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
158                               smscphy_power(sc, true);
159 
160                     switch (IFM_SUBTYPE(ife->ifm_media)) {
161                     case IFM_AUTO:
162                               smscphy_auto(sc, ife->ifm_media);
163                               break;
164 
165                     default:
166                               mii_phy_setmedia(sc);
167                               if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
168                                         smscphy_power(sc, false);
169                               break;
170                     }
171                     break;
172 
173           case MII_TICK:
174                     if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
175                               break;
176 
177                     PHY_READ(sc, MII_BMSR, &reg);
178                     PHY_READ(sc, MII_BMSR, &reg);
179                     if (reg & BMSR_LINK) {
180                               sc->mii_ticks = 0;
181                               break;
182                     }
183 
184                     if (++sc->mii_ticks <= MII_ANEGTICKS)
185                               break;
186 
187                     PHY_RESET(sc);
188                     smscphy_auto(sc, ife->ifm_media);
189                     break;
190           }
191 
192           /* Update the media status. */
193           PHY_STATUS(sc);
194 
195           /* Callback if something changed. */
196           mii_phy_update(sc, cmd);
197           return 0;
198 }
199 
200 static void
smscphy_auto(struct mii_softc * sc,int media)201 smscphy_auto(struct mii_softc *sc, int media)
202 {
203           uint16_t anar;
204 
205           sc->mii_ticks = 0;
206           anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
207           if ((media & IFM_FLOW) != 0)
208                     anar |= ANAR_FC;
209           PHY_WRITE(sc, MII_ANAR, anar);
210           /* Apparently this helps. */
211           PHY_READ(sc, MII_ANAR, &anar);
212           PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
213 }
214 
215 static void
smscphy_status(struct mii_softc * sc)216 smscphy_status(struct mii_softc *sc)
217 {
218           struct mii_data *mii = sc->mii_pdata;
219           uint16_t bmcr, bmsr, status;
220 
221           KASSERT(mii_locked(mii));
222 
223           mii->mii_media_status = IFM_AVALID;
224           mii->mii_media_active = IFM_ETHER;
225 
226           PHY_READ(sc, MII_BMSR, &bmsr);
227           PHY_READ(sc, MII_BMSR, &bmsr);
228           if ((bmsr & BMSR_LINK) != 0)
229                     mii->mii_media_status |= IFM_ACTIVE;
230 
231           PHY_READ(sc, MII_BMCR, &bmcr);
232           if ((bmcr & BMCR_ISO) != 0) {
233                     mii->mii_media_active |= IFM_NONE;
234                     mii->mii_media_status = 0;
235                     return;
236           }
237 
238           if ((bmcr & BMCR_LOOP) != 0)
239                     mii->mii_media_active |= IFM_LOOP;
240 
241           if ((bmcr & BMCR_AUTOEN) != 0) {
242                     if ((bmsr & BMSR_ACOMP) == 0) {
243                               /* Erg, still trying, I guess... */
244                               mii->mii_media_active |= IFM_NONE;
245                               return;
246                     }
247           }
248 
249           PHY_READ(sc, SMSCPHY_SPCSR, &status);
250           if ((status & SPCSR_SPDIND_SPDMASK) == SPCSR_SPDIND_100)
251                     mii->mii_media_active |= IFM_100_TX;
252           else
253                     mii->mii_media_active |= IFM_10_T;
254           if (status & SPCSR_SPDIND_FDX)
255                     mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
256           else
257                     mii->mii_media_active |= IFM_HDX;
258 }
259