2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006, Pyun YongHyeon <yongari@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * Driver for the IC Plus IP1000A/IP1001 10/100/1000 PHY.
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>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
50 #include <dev/mii/mii.h>
51 #include <dev/mii/miivar.h>
54 #include <dev/mii/ip1000phyreg.h>
56 #include "miibus_if.h"
58 #include <machine/bus.h>
59 #include <dev/stge/if_stgereg.h>
61 static int ip1000phy_probe(device_t);
62 static int ip1000phy_attach(device_t);
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),
73 static devclass_t ip1000phy_devclass;
74 static driver_t ip1000phy_driver = {
77 sizeof(struct mii_softc)
80 DRIVER_MODULE(ip1000phy, miibus, ip1000phy_driver, ip1000phy_devclass, 0, 0);
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);
87 static const struct mii_phydesc ip1000phys[] = {
88 MII_PHY_DESC(xxICPLUS, IP1000A),
89 MII_PHY_DESC(xxICPLUS, IP1001),
93 static const struct mii_phy_funcs ip1000phy_funcs = {
100 ip1000phy_probe(device_t dev)
103 return (mii_phy_dev_probe(dev, ip1000phys, BUS_PROBE_DEFAULT));
107 ip1000phy_attach(device_t dev)
109 struct mii_attach_args *ma;
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);
123 ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
125 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
126 uint32_t gig, reg, speed;
134 switch (IFM_SUBTYPE(ife->ifm_media)) {
136 (void)ip1000phy_mii_phy_auto(sc, ife->ifm_media);
142 * Manual 1000baseT setting doesn't seem to work.
144 speed = IP1000PHY_BMCR_1000;
148 speed = IP1000PHY_BMCR_100;
152 speed = IP1000PHY_BMCR_10;
159 if ((ife->ifm_media & IFM_FDX) != 0) {
160 speed |= IP1000PHY_BMCR_FDX;
161 gig = IP1000PHY_1000CR_1000T_FDX;
163 gig = IP1000PHY_1000CR_1000T;
165 if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
167 IP1000PHY_1000CR_MASTER | IP1000PHY_1000CR_MANUAL;
168 if ((ife->ifm_media & IFM_ETH_MASTER) != 0)
169 gig |= IP1000PHY_1000CR_MMASTER;
172 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
173 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
180 * Only used for autonegotiation.
182 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
190 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
191 if (reg & BMSR_LINK) {
196 /* Announce link loss right after it happens */
197 if (sc->mii_ticks++ == 0)
201 * Only retry autonegotiation every mii_anegticks seconds.
203 if (sc->mii_ticks <= sc->mii_anegticks)
207 ip1000phy_mii_phy_auto(sc, ife->ifm_media);
211 /* Update the media status. */
214 /* Callback if something changed. */
215 mii_phy_update(sc, cmd);
220 ip1000phy_status(struct mii_softc *sc)
222 struct mii_data *mii = sc->mii_pdata;
223 uint32_t bmsr, bmcr, stat;
225 mii->mii_media_status = IFM_AVALID;
226 mii->mii_media_active = IFM_ETHER;
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;
233 bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
234 if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
235 mii->mii_media_active |= IFM_LOOP;
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;
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;
251 case IP1000PHY_LSR_SPEED_100:
252 mii->mii_media_active |= IFM_100_TX;
254 case IP1000PHY_LSR_SPEED_1000:
255 mii->mii_media_active |= IFM_1000_T;
258 mii->mii_media_active |= IFM_NONE;
261 if ((stat & IP1000PHY_LSR_FULL_DUPLEX) != 0)
262 mii->mii_media_active |= IFM_FDX;
264 mii->mii_media_active |= IFM_HDX;
266 stat = PHY_READ(sc, STGE_PhyCtrl);
267 switch (PC_LinkSpeed(stat)) {
268 case PC_LinkSpeed_Down:
269 mii->mii_media_active |= IFM_NONE;
271 case PC_LinkSpeed_10:
272 mii->mii_media_active |= IFM_10_T;
274 case PC_LinkSpeed_100:
275 mii->mii_media_active |= IFM_100_TX;
277 case PC_LinkSpeed_1000:
278 mii->mii_media_active |= IFM_1000_T;
281 mii->mii_media_active |= IFM_NONE;
284 if ((stat & PC_PhyDuplexStatus) != 0)
285 mii->mii_media_active |= IFM_FDX;
287 mii->mii_media_active |= IFM_HDX;
290 if ((mii->mii_media_active & IFM_FDX) != 0)
291 mii->mii_media_active |= mii_phy_flowstatus(sc);
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;
301 ip1000phy_mii_phy_auto(struct mii_softc *sc, int media)
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;
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);
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));
324 return (EJUSTRETURN);
328 ip1000phy_load_dspcode(struct mii_softc *sc)
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);
343 ip1000phy_reset(struct mii_softc *sc)
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);
354 if ((sc->mii_flags & MIIF_PHYPRIV0) != 0)
355 ip1000phy_load_dspcode(sc);