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