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_flags |= MIIF_NOISOLATE;
125 isc->model = MII_MODEL(ma->mii_id2);
126 isc->revision = MII_REV(ma->mii_id2);
128 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
129 if (sc->mii_capabilities & BMSR_EXTSTAT)
130 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
131 device_printf(dev, " ");
134 mii_phy_add_media(sc);
137 MIIBUS_MEDIAINIT(sc->mii_dev);
142 ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
144 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
145 uint32_t gig, reg, speed;
150 * If we're not polling our PHY instance, just return.
152 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
158 * If the media indicates a different PHY instance,
161 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
162 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
163 PHY_WRITE(sc, IP1000PHY_MII_BMCR,
164 reg | IP1000PHY_BMCR_ISO);
169 * If the interface is not up, don't do anything.
171 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
176 switch (IFM_SUBTYPE(ife->ifm_media)) {
178 (void)ip1000phy_mii_phy_auto(sc);
185 * Manual 1000baseT setting doesn't seem to work.
187 speed = IP1000PHY_BMCR_1000;
191 speed = IP1000PHY_BMCR_100;
195 speed = IP1000PHY_BMCR_10;
202 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
203 speed |= IP1000PHY_BMCR_FDX;
204 gig = IP1000PHY_1000CR_1000T_FDX;
206 gig = IP1000PHY_1000CR_1000T;
208 PHY_WRITE(sc, IP1000PHY_MII_1000CR, 0);
209 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
211 if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)
214 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
215 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
218 * When setting the link manually, one side must
219 * be the master and the other the slave. However
220 * ifmedia doesn't give us a good way to specify
221 * this, so we fake it by using one of the LINK
222 * flags. If LINK0 is set, we program the PHY to
223 * be a master, otherwise it's a slave.
225 if ((mii->mii_ifp->if_flags & IFF_LINK0))
226 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
227 IP1000PHY_1000CR_MASTER |
228 IP1000PHY_1000CR_MMASTER |
229 IP1000PHY_1000CR_MANUAL);
231 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
232 IP1000PHY_1000CR_MASTER |
233 IP1000PHY_1000CR_MANUAL);
240 * If we're not currently selected, just return.
242 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
245 * Is the interface even up?
247 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
251 * Only used for autonegotiation.
253 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
261 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
262 if (reg & BMSR_LINK) {
267 /* Announce link loss right after it happens */
268 if (sc->mii_ticks++ == 0)
272 * Only retry autonegotiation every mii_anegticks seconds.
274 if (sc->mii_ticks <= sc->mii_anegticks)
278 ip1000phy_mii_phy_auto(sc);
282 /* Update the media status. */
283 ip1000phy_status(sc);
285 /* Callback if something changed. */
286 mii_phy_update(sc, cmd);
291 ip1000phy_status(struct mii_softc *sc)
293 struct ip1000phy_softc *isc;
294 struct mii_data *mii = sc->mii_pdata;
295 uint32_t bmsr, bmcr, stat;
298 isc = (struct ip1000phy_softc *)sc;
300 mii->mii_media_status = IFM_AVALID;
301 mii->mii_media_active = IFM_ETHER;
303 bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) |
304 PHY_READ(sc, IP1000PHY_MII_BMSR);
305 if ((bmsr & IP1000PHY_BMSR_LINK) != 0)
306 mii->mii_media_status |= IFM_ACTIVE;
308 bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
309 if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
310 mii->mii_media_active |= IFM_LOOP;
312 if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) {
313 if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) {
314 /* Erg, still trying, I guess... */
315 mii->mii_media_active |= IFM_NONE;
320 if (isc->model == MII_MODEL_ICPLUS_IP1001) {
321 stat = PHY_READ(sc, IP1000PHY_LSR);
322 switch (stat & IP1000PHY_LSR_SPEED_MASK) {
323 case IP1000PHY_LSR_SPEED_10:
324 mii->mii_media_active |= IFM_10_T;
326 case IP1000PHY_LSR_SPEED_100:
327 mii->mii_media_active |= IFM_100_TX;
329 case IP1000PHY_LSR_SPEED_1000:
330 mii->mii_media_active |= IFM_1000_T;
333 mii->mii_media_active |= IFM_NONE;
336 if ((stat & IP1000PHY_LSR_FULL_DUPLEX) != 0)
337 mii->mii_media_active |= IFM_FDX;
339 mii->mii_media_active |= IFM_HDX;
341 stat = PHY_READ(sc, STGE_PhyCtrl);
342 switch (PC_LinkSpeed(stat)) {
343 case PC_LinkSpeed_Down:
344 mii->mii_media_active |= IFM_NONE;
346 case PC_LinkSpeed_10:
347 mii->mii_media_active |= IFM_10_T;
349 case PC_LinkSpeed_100:
350 mii->mii_media_active |= IFM_100_TX;
352 case PC_LinkSpeed_1000:
353 mii->mii_media_active |= IFM_1000_T;
356 mii->mii_media_active |= IFM_NONE;
359 if ((stat & PC_PhyDuplexStatus) != 0)
360 mii->mii_media_active |= IFM_FDX;
362 mii->mii_media_active |= IFM_HDX;
365 ar = PHY_READ(sc, IP1000PHY_MII_ANAR);
366 lpar = PHY_READ(sc, IP1000PHY_MII_ANLPAR);
369 * FLAG0 : Rx flow-control
370 * FLAG1 : Tx flow-control
372 if ((ar & IP1000PHY_ANAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_PAUSE))
373 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
374 else if (!(ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
375 (lpar & IP1000PHY_ANLPAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_APAUSE))
376 mii->mii_media_active |= IFM_FLAG1;
377 else if ((ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
378 !(lpar & IP1000PHY_ANLPAR_PAUSE) &&
379 (lpar & IP1000PHY_ANLPAR_APAUSE)) {
380 mii->mii_media_active |= IFM_FLAG0;
384 * FLAG2 : local PHY resolved to MASTER
386 if ((mii->mii_media_active & IFM_1000_T) != 0) {
387 stat = PHY_READ(sc, IP1000PHY_MII_1000SR);
388 if ((stat & IP1000PHY_1000SR_MASTER) != 0)
389 mii->mii_media_active |= IFM_FLAG2;
394 ip1000phy_mii_phy_auto(struct mii_softc *sc)
396 struct ip1000phy_softc *isc;
399 isc = (struct ip1000phy_softc *)sc;
401 if (isc->model == MII_MODEL_ICPLUS_IP1001)
402 reg = PHY_READ(sc, IP1000PHY_MII_ANAR);
403 reg |= IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX |
404 IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX |
405 IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE;
406 PHY_WRITE(sc, IP1000PHY_MII_ANAR, reg | IP1000PHY_ANAR_CSMA);
408 reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX;
409 reg |= IP1000PHY_1000CR_MASTER;
410 PHY_WRITE(sc, IP1000PHY_MII_1000CR, reg);
411 PHY_WRITE(sc, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX |
412 IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG));
414 return (EJUSTRETURN);
418 ip1000phy_load_dspcode(struct mii_softc *sc)
421 PHY_WRITE(sc, 31, 0x0001);
422 PHY_WRITE(sc, 27, 0x01e0);
423 PHY_WRITE(sc, 31, 0x0002);
424 PHY_WRITE(sc, 27, 0xeb8e);
425 PHY_WRITE(sc, 31, 0x0000);
426 PHY_WRITE(sc, 30, 0x005e);
427 PHY_WRITE(sc, 9, 0x0700);
433 ip1000phy_reset(struct mii_softc *sc)
435 struct ip1000phy_softc *isc;
436 struct stge_softc *stge_sc;
437 struct mii_data *mii;
440 isc = (struct ip1000phy_softc *)sc;
443 /* clear autoneg/full-duplex as we don't want it after reset */
444 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
445 reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX);
446 PHY_WRITE(sc, MII_BMCR, reg);
450 * XXX There should be more general way to pass PHY specific
451 * data via mii interface.
453 if (isc->model == MII_MODEL_ICPLUS_IP1000A &&
454 strcmp(mii->mii_ifp->if_dname, "stge") == 0) {
455 stge_sc = mii->mii_ifp->if_softc;
456 if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
457 ip1000phy_load_dspcode(sc);