2 * Copyright (c) 2006, Pyun YongHyeon <yongari@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Driver for the IC Plus IP1000A/IP1001 10/100/1000 PHY.
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
44 #include <net/if_media.h>
46 #include <dev/mii/mii.h>
47 #include <dev/mii/miivar.h>
50 #include <dev/mii/ip1000phyreg.h>
52 #include "miibus_if.h"
54 #include <machine/bus.h>
55 #include <dev/stge/if_stgereg.h>
57 static int ip1000phy_probe(device_t);
58 static int ip1000phy_attach(device_t);
60 struct ip1000phy_softc {
61 struct mii_softc mii_sc;
66 static device_method_t ip1000phy_methods[] = {
67 /* device interface */
68 DEVMETHOD(device_probe, ip1000phy_probe),
69 DEVMETHOD(device_attach, ip1000phy_attach),
70 DEVMETHOD(device_detach, mii_phy_detach),
71 DEVMETHOD(device_shutdown, bus_generic_shutdown),
75 static devclass_t ip1000phy_devclass;
76 static driver_t ip1000phy_driver = {
79 sizeof (struct mii_softc)
82 DRIVER_MODULE(ip1000phy, miibus, ip1000phy_driver, ip1000phy_devclass, 0, 0);
84 static int ip1000phy_service(struct mii_softc *, struct mii_data *, int);
85 static void ip1000phy_status(struct mii_softc *);
86 static void ip1000phy_reset(struct mii_softc *);
87 static int ip1000phy_mii_phy_auto(struct mii_softc *);
89 static const struct mii_phydesc ip1000phys[] = {
90 MII_PHY_DESC(ICPLUS, IP1000A),
91 MII_PHY_DESC(ICPLUS, IP1001),
96 ip1000phy_probe(device_t dev)
99 return (mii_phy_dev_probe(dev, ip1000phys, BUS_PROBE_DEFAULT));
103 ip1000phy_attach(device_t dev)
105 struct ip1000phy_softc *isc;
106 struct mii_softc *sc;
107 struct mii_attach_args *ma;
108 struct mii_data *mii;
110 isc = device_get_softc(dev);
112 ma = device_get_ivars(dev);
113 sc->mii_dev = device_get_parent(dev);
114 mii = device_get_softc(sc->mii_dev);
115 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
117 sc->mii_inst = mii->mii_instance;
118 sc->mii_phy = ma->mii_phyno;
119 sc->mii_service = ip1000phy_service;
121 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
122 sc->mii_flags |= MIIF_NOISOLATE;
126 isc->model = MII_MODEL(ma->mii_id2);
127 isc->revision = MII_REV(ma->mii_id2);
129 device_printf(dev, " ");
131 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
133 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
136 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
139 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
140 IP1000PHY_BMCR_10 | IP1000PHY_BMCR_FDX);
141 printf("10baseT-FDX, ");
142 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
144 printf("100baseTX, ");
145 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
146 IP1000PHY_BMCR_100 | IP1000PHY_BMCR_FDX);
147 printf("100baseTX-FDX, ");
148 /* 1000baseT half-duplex, really supported? */
149 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
150 IP1000PHY_BMCR_1000);
151 printf("1000baseTX, ");
152 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst),
153 IP1000PHY_BMCR_1000 | IP1000PHY_BMCR_FDX);
154 printf("1000baseTX-FDX, ");
155 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
161 MIIBUS_MEDIAINIT(sc->mii_dev);
166 ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
168 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
169 uint32_t gig, reg, speed;
174 * If we're not polling our PHY instance, just return.
176 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
182 * If the media indicates a different PHY instance,
185 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
186 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
187 PHY_WRITE(sc, IP1000PHY_MII_BMCR,
188 reg | IP1000PHY_BMCR_ISO);
193 * If the interface is not up, don't do anything.
195 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
200 switch (IFM_SUBTYPE(ife->ifm_media)) {
202 (void)ip1000phy_mii_phy_auto(sc);
209 * Manual 1000baseT setting doesn't seem to work.
211 speed = IP1000PHY_BMCR_1000;
215 speed = IP1000PHY_BMCR_100;
219 speed = IP1000PHY_BMCR_10;
226 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
227 speed |= IP1000PHY_BMCR_FDX;
228 gig = IP1000PHY_1000CR_1000T_FDX;
230 gig = IP1000PHY_1000CR_1000T;
232 PHY_WRITE(sc, IP1000PHY_MII_1000CR, 0);
233 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
235 if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)
238 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
239 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
242 * When setting the link manually, one side must
243 * be the master and the other the slave. However
244 * ifmedia doesn't give us a good way to specify
245 * this, so we fake it by using one of the LINK
246 * flags. If LINK0 is set, we program the PHY to
247 * be a master, otherwise it's a slave.
249 if ((mii->mii_ifp->if_flags & IFF_LINK0))
250 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
251 IP1000PHY_1000CR_MASTER |
252 IP1000PHY_1000CR_MMASTER |
253 IP1000PHY_1000CR_MANUAL);
255 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
256 IP1000PHY_1000CR_MASTER |
257 IP1000PHY_1000CR_MANUAL);
264 * If we're not currently selected, just return.
266 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
269 * Is the interface even up?
271 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
275 * Only used for autonegotiation.
277 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
285 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
286 if (reg & BMSR_LINK) {
291 /* Announce link loss right after it happens */
292 if (sc->mii_ticks++ == 0)
296 * Only retry autonegotiation every mii_anegticks seconds.
298 if (sc->mii_ticks <= sc->mii_anegticks)
302 ip1000phy_mii_phy_auto(sc);
306 /* Update the media status. */
307 ip1000phy_status(sc);
309 /* Callback if something changed. */
310 mii_phy_update(sc, cmd);
315 ip1000phy_status(struct mii_softc *sc)
317 struct ip1000phy_softc *isc;
318 struct mii_data *mii = sc->mii_pdata;
319 uint32_t bmsr, bmcr, stat;
322 isc = (struct ip1000phy_softc *)sc;
324 mii->mii_media_status = IFM_AVALID;
325 mii->mii_media_active = IFM_ETHER;
327 bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) |
328 PHY_READ(sc, IP1000PHY_MII_BMSR);
329 if ((bmsr & IP1000PHY_BMSR_LINK) != 0)
330 mii->mii_media_status |= IFM_ACTIVE;
332 bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
333 if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
334 mii->mii_media_active |= IFM_LOOP;
336 if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) {
337 if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) {
338 /* Erg, still trying, I guess... */
339 mii->mii_media_active |= IFM_NONE;
344 if (isc->model == MII_MODEL_ICPLUS_IP1001) {
345 stat = PHY_READ(sc, IP1000PHY_LSR);
346 switch (stat & IP1000PHY_LSR_SPEED_MASK) {
347 case IP1000PHY_LSR_SPEED_10:
348 mii->mii_media_active |= IFM_10_T;
350 case IP1000PHY_LSR_SPEED_100:
351 mii->mii_media_active |= IFM_100_TX;
353 case IP1000PHY_LSR_SPEED_1000:
354 mii->mii_media_active |= IFM_1000_T;
357 if ((stat & IP1000PHY_LSR_FULL_DUPLEX) != 0)
358 mii->mii_media_active |= IFM_FDX;
360 mii->mii_media_active |= IFM_HDX;
362 stat = PHY_READ(sc, STGE_PhyCtrl);
363 switch (PC_LinkSpeed(stat)) {
364 case PC_LinkSpeed_Down:
365 mii->mii_media_active |= IFM_NONE;
367 case PC_LinkSpeed_10:
368 mii->mii_media_active |= IFM_10_T;
370 case PC_LinkSpeed_100:
371 mii->mii_media_active |= IFM_100_TX;
373 case PC_LinkSpeed_1000:
374 mii->mii_media_active |= IFM_1000_T;
377 if ((stat & PC_PhyDuplexStatus) != 0)
378 mii->mii_media_active |= IFM_FDX;
380 mii->mii_media_active |= IFM_HDX;
383 ar = PHY_READ(sc, IP1000PHY_MII_ANAR);
384 lpar = PHY_READ(sc, IP1000PHY_MII_ANLPAR);
387 * FLAG0 : Rx flow-control
388 * FLAG1 : Tx flow-control
390 if ((ar & IP1000PHY_ANAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_PAUSE))
391 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
392 else if (!(ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
393 (lpar & IP1000PHY_ANLPAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_APAUSE))
394 mii->mii_media_active |= IFM_FLAG1;
395 else if ((ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
396 !(lpar & IP1000PHY_ANLPAR_PAUSE) &&
397 (lpar & IP1000PHY_ANLPAR_APAUSE)) {
398 mii->mii_media_active |= IFM_FLAG0;
402 * FLAG2 : local PHY resolved to MASTER
404 if ((mii->mii_media_active & IFM_1000_T) != 0) {
405 stat = PHY_READ(sc, IP1000PHY_MII_1000SR);
406 if ((stat & IP1000PHY_1000SR_MASTER) != 0)
407 mii->mii_media_active |= IFM_FLAG2;
412 ip1000phy_mii_phy_auto(struct mii_softc *mii)
416 PHY_WRITE(mii, IP1000PHY_MII_ANAR,
417 IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX |
418 IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX |
419 IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE);
420 reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX;
421 reg |= IP1000PHY_1000CR_MASTER;
422 PHY_WRITE(mii, IP1000PHY_MII_1000CR, reg);
423 PHY_WRITE(mii, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX |
424 IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG));
426 return (EJUSTRETURN);
430 ip1000phy_load_dspcode(struct mii_softc *sc)
433 PHY_WRITE(sc, 31, 0x0001);
434 PHY_WRITE(sc, 27, 0x01e0);
435 PHY_WRITE(sc, 31, 0x0002);
436 PHY_WRITE(sc, 27, 0xeb8e);
437 PHY_WRITE(sc, 31, 0x0000);
438 PHY_WRITE(sc, 30, 0x005e);
439 PHY_WRITE(sc, 9, 0x0700);
445 ip1000phy_reset(struct mii_softc *sc)
447 struct ip1000phy_softc *isc;
448 struct stge_softc *stge_sc;
449 struct mii_data *mii;
452 isc = (struct ip1000phy_softc *)sc;
455 /* clear autoneg/full-duplex as we don't want it after reset */
456 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
457 reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX);
458 PHY_WRITE(sc, MII_BMCR, reg);
462 * XXX There should be more general way to pass PHY specific
463 * data via mii interface.
465 if (isc->model == MII_MODEL_ICPLUS_IP1000A &&
466 strcmp(mii->mii_ifp->if_dname, "stge") == 0) {
467 stge_sc = mii->mii_ifp->if_softc;
468 if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
469 ip1000phy_load_dspcode(sc);