1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006, Pyun YongHyeon <yongari@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * 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 AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /*
35 * Driver for the IC Plus IP1000A/IP1001 10/100/1000 PHY.
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/socket.h>
43 #include <sys/taskqueue.h>
44 #include <sys/bus.h>
45
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
49
50 #include <dev/mii/mii.h>
51 #include <dev/mii/miivar.h>
52 #include "miidevs.h"
53
54 #include <dev/mii/ip1000phyreg.h>
55
56 #include "miibus_if.h"
57
58 #include <machine/bus.h>
59 #include <dev/stge/if_stgereg.h>
60
61 static int ip1000phy_probe(device_t);
62 static int ip1000phy_attach(device_t);
63
64 static device_method_t ip1000phy_methods[] = {
65 /* device interface */
66 DEVMETHOD(device_probe, ip1000phy_probe),
67 DEVMETHOD(device_attach, ip1000phy_attach),
68 DEVMETHOD(device_detach, mii_phy_detach),
69 DEVMETHOD(device_shutdown, bus_generic_shutdown),
70 DEVMETHOD_END
71 };
72
73 static devclass_t ip1000phy_devclass;
74 static driver_t ip1000phy_driver = {
75 "ip1000phy",
76 ip1000phy_methods,
77 sizeof(struct mii_softc)
78 };
79
80 DRIVER_MODULE(ip1000phy, miibus, ip1000phy_driver, ip1000phy_devclass, 0, 0);
81
82 static int ip1000phy_service(struct mii_softc *, struct mii_data *, int);
83 static void ip1000phy_status(struct mii_softc *);
84 static void ip1000phy_reset(struct mii_softc *);
85 static int ip1000phy_mii_phy_auto(struct mii_softc *, int);
86
87 static const struct mii_phydesc ip1000phys[] = {
88 MII_PHY_DESC(xxICPLUS, IP1000A),
89 MII_PHY_DESC(xxICPLUS, IP1001),
90 MII_PHY_END
91 };
92
93 static const struct mii_phy_funcs ip1000phy_funcs = {
94 ip1000phy_service,
95 ip1000phy_status,
96 ip1000phy_reset
97 };
98
99 static int
100 ip1000phy_probe(device_t dev)
101 {
102
103 return (mii_phy_dev_probe(dev, ip1000phys, BUS_PROBE_DEFAULT));
104 }
105
106 static int
107 ip1000phy_attach(device_t dev)
108 {
109 struct mii_attach_args *ma;
110 u_int flags;
111
112 ma = device_get_ivars(dev);
113 flags = MIIF_NOISOLATE | MIIF_NOMANPAUSE;
114 if (MII_MODEL(ma->mii_id2) == MII_MODEL_xxICPLUS_IP1000A &&
115 mii_dev_mac_match(dev, "stge") &&
116 (miibus_get_flags(dev) & MIIF_MACPRIV0) != 0)
117 flags |= MIIF_PHYPRIV0;
118 mii_phy_dev_attach(dev, flags, &ip1000phy_funcs, 1);
119 return (0);
120 }
121
122 static int
123 ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
124 {
125 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
126 uint32_t gig, reg, speed;
127
128 switch (cmd) {
129 case MII_POLLSTAT:
130 break;
131
132 case MII_MEDIACHG:
133 PHY_RESET(sc);
134 switch (IFM_SUBTYPE(ife->ifm_media)) {
135 case IFM_AUTO:
136 (void)ip1000phy_mii_phy_auto(sc, ife->ifm_media);
137 goto done;
138
139 case IFM_1000_T:
140 /*
141 * XXX
142 * Manual 1000baseT setting doesn't seem to work.
143 */
144 speed = IP1000PHY_BMCR_1000;
145 break;
146
147 case IFM_100_TX:
148 speed = IP1000PHY_BMCR_100;
149 break;
150
151 case IFM_10_T:
152 speed = IP1000PHY_BMCR_10;
153 break;
154
155 default:
156 return (EINVAL);
157 }
158
159 if ((ife->ifm_media & IFM_FDX) != 0) {
160 speed |= IP1000PHY_BMCR_FDX;
161 gig = IP1000PHY_1000CR_1000T_FDX;
162 } else
163 gig = IP1000PHY_1000CR_1000T;
164
165 if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
166 gig |=
167 IP1000PHY_1000CR_MASTER | IP1000PHY_1000CR_MANUAL;
168 if ((ife->ifm_media & IFM_ETH_MASTER) != 0)
169 gig |= IP1000PHY_1000CR_MMASTER;
170 } else
171 gig = 0;
172 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
173 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
174
175 done:
176 break;
177
178 case MII_TICK:
179 /*
180 * Only used for autonegotiation.
181 */
182 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
183 sc->mii_ticks = 0;
184 break;
185 }
186
187 /*
188 * check for link.
189 */
190 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
191 if (reg & BMSR_LINK) {
192 sc->mii_ticks = 0;
193 break;
194 }
195
196 /* Announce link loss right after it happens */
197 if (sc->mii_ticks++ == 0)
198 break;
199
200 /*
201 * Only retry autonegotiation every mii_anegticks seconds.
202 */
203 if (sc->mii_ticks <= sc->mii_anegticks)
204 break;
205
206 sc->mii_ticks = 0;
207 ip1000phy_mii_phy_auto(sc, ife->ifm_media);
208 break;
209 }
210
211 /* Update the media status. */
212 PHY_STATUS(sc);
213
214 /* Callback if something changed. */
215 mii_phy_update(sc, cmd);
216 return (0);
217 }
218
219 static void
220 ip1000phy_status(struct mii_softc *sc)
221 {
222 struct mii_data *mii = sc->mii_pdata;
223 uint32_t bmsr, bmcr, stat;
224
225 mii->mii_media_status = IFM_AVALID;
226 mii->mii_media_active = IFM_ETHER;
227
228 bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) |
229 PHY_READ(sc, IP1000PHY_MII_BMSR);
230 if ((bmsr & IP1000PHY_BMSR_LINK) != 0)
231 mii->mii_media_status |= IFM_ACTIVE;
232
233 bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
234 if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
235 mii->mii_media_active |= IFM_LOOP;
236
237 if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) {
238 if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) {
239 /* Erg, still trying, I guess... */
240 mii->mii_media_active |= IFM_NONE;
241 return;
242 }
243 }
244
245 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1001) {
246 stat = PHY_READ(sc, IP1000PHY_LSR);
247 switch (stat & IP1000PHY_LSR_SPEED_MASK) {
248 case IP1000PHY_LSR_SPEED_10:
249 mii->mii_media_active |= IFM_10_T;
250 break;
251 case IP1000PHY_LSR_SPEED_100:
252 mii->mii_media_active |= IFM_100_TX;
253 break;
254 case IP1000PHY_LSR_SPEED_1000:
255 mii->mii_media_active |= IFM_1000_T;
256 break;
257 default:
258 mii->mii_media_active |= IFM_NONE;
259 return;
260 }
261 if ((stat & IP1000PHY_LSR_FULL_DUPLEX) != 0)
262 mii->mii_media_active |= IFM_FDX;
263 else
264 mii->mii_media_active |= IFM_HDX;
265 } else {
266 stat = PHY_READ(sc, STGE_PhyCtrl);
267 switch (PC_LinkSpeed(stat)) {
268 case PC_LinkSpeed_Down:
269 mii->mii_media_active |= IFM_NONE;
270 return;
271 case PC_LinkSpeed_10:
272 mii->mii_media_active |= IFM_10_T;
273 break;
274 case PC_LinkSpeed_100:
275 mii->mii_media_active |= IFM_100_TX;
276 break;
277 case PC_LinkSpeed_1000:
278 mii->mii_media_active |= IFM_1000_T;
279 break;
280 default:
281 mii->mii_media_active |= IFM_NONE;
282 return;
283 }
284 if ((stat & PC_PhyDuplexStatus) != 0)
285 mii->mii_media_active |= IFM_FDX;
286 else
287 mii->mii_media_active |= IFM_HDX;
288 }
289
290 if ((mii->mii_media_active & IFM_FDX) != 0)
291 mii->mii_media_active |= mii_phy_flowstatus(sc);
292
293 if ((mii->mii_media_active & IFM_1000_T) != 0) {
294 stat = PHY_READ(sc, IP1000PHY_MII_1000SR);
295 if ((stat & IP1000PHY_1000SR_MASTER) != 0)
296 mii->mii_media_active |= IFM_ETH_MASTER;
297 }
298 }
299
300 static int
301 ip1000phy_mii_phy_auto(struct mii_softc *sc, int media)
302 {
303 uint32_t reg;
304
305 reg = 0;
306 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1001) {
307 reg = PHY_READ(sc, IP1000PHY_MII_ANAR);
308 reg &= ~(IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE);
309 reg |= IP1000PHY_ANAR_NP;
310 }
311 reg |= IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX |
312 IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX;
313 if ((media & IFM_FLOW) != 0 || (sc->mii_flags & MIIF_FORCEPAUSE) != 0)
314 reg |= IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE;
315 PHY_WRITE(sc, IP1000PHY_MII_ANAR, reg | IP1000PHY_ANAR_CSMA);
316
317 reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX;
318 if (sc->mii_mpd_model != MII_MODEL_xxICPLUS_IP1001)
319 reg |= IP1000PHY_1000CR_MASTER;
320 PHY_WRITE(sc, IP1000PHY_MII_1000CR, reg);
321 PHY_WRITE(sc, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX |
322 IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG));
323
324 return (EJUSTRETURN);
325 }
326
327 static void
328 ip1000phy_load_dspcode(struct mii_softc *sc)
329 {
330
331 PHY_WRITE(sc, 31, 0x0001);
332 PHY_WRITE(sc, 27, 0x01e0);
333 PHY_WRITE(sc, 31, 0x0002);
334 PHY_WRITE(sc, 27, 0xeb8e);
335 PHY_WRITE(sc, 31, 0x0000);
336 PHY_WRITE(sc, 30, 0x005e);
337 PHY_WRITE(sc, 9, 0x0700);
338
339 DELAY(50);
340 }
341
342 static void
343 ip1000phy_reset(struct mii_softc *sc)
344 {
345 uint32_t reg;
346
347 mii_phy_reset(sc);
348
349 /* clear autoneg/full-duplex as we don't want it after reset */
350 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
351 reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX);
352 PHY_WRITE(sc, MII_BMCR, reg);
353
354 if ((sc->mii_flags & MIIF_PHYPRIV0) != 0)
355 ip1000phy_load_dspcode(sc);
356 }
Cache object: 4edc11c61f60d32fd69926dc3e9d1d52
|