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
27 * $FreeBSD: src/sys/dev/mii/ip1000phy.c,v 1.1 2006/07/25 00:16:09 yongari Exp $
31 * Driver for the IC Plus IP1000A 10/100/1000 PHY.
34 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
42 #include <net/if_arp.h>
43 #include <net/if_media.h>
45 #include <dev/netif/mii_layer/mii.h>
46 #include <dev/netif/mii_layer/miivar.h>
49 #include <dev/netif/mii_layer/ip1000phyreg.h>
51 #include "miibus_if.h"
53 #include <dev/netif/stge/if_stgereg.h>
54 #include <dev/netif/stge/if_stgevar.h>
56 static int ip1000phy_probe(device_t);
57 static int ip1000phy_attach(device_t);
58 static int ip1000phy_service(struct mii_softc *, struct mii_data *, int);
59 static void ip1000phy_status(struct mii_softc *);
60 static void ip1000phy_reset(struct mii_softc *);
61 static int ip1000phy_mii_phy_auto(struct mii_softc *);
63 static device_method_t ip1000phy_methods[] = {
64 /* device interface */
65 DEVMETHOD(device_probe, ip1000phy_probe),
66 DEVMETHOD(device_attach, ip1000phy_attach),
67 DEVMETHOD(device_detach, ukphy_detach),
68 DEVMETHOD(device_shutdown, bus_generic_shutdown),
72 static devclass_t ip1000phy_devclass;
73 static driver_t ip1000phy_driver = {
76 sizeof (struct mii_softc)
79 DRIVER_MODULE(ip1000phy, miibus, ip1000phy_driver, ip1000phy_devclass, NULL, NULL);
81 static const struct mii_phydesc ip1000phys[] = {
82 MII_PHYDESC(ICPLUS, IP1000A),
87 ip1000phy_probe(device_t dev)
89 struct mii_attach_args *ma;
90 const struct mii_phydesc *mpd;
92 ma = device_get_ivars(dev);
93 mpd = mii_phy_match(ma, ip1000phys);
95 device_set_desc(dev, mpd->mpd_name);
103 ip1000phy_attach(device_t dev)
105 struct mii_softc *sc;
106 struct mii_attach_args *ma;
107 struct mii_data *mii;
109 sc = device_get_softc(dev);
110 ma = device_get_ivars(dev);
111 mii_softc_init(sc, ma);
112 sc->mii_dev = device_get_parent(dev);
113 mii = device_get_softc(sc->mii_dev);
114 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
116 sc->mii_inst = mii->mii_instance;
117 sc->mii_service = ip1000phy_service;
120 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
121 sc->mii_flags |= MIIF_NOISOLATE;
125 device_printf(dev, " ");
127 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
129 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
132 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
134 kprintf("10baseT, ");
135 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
136 IP1000PHY_BMCR_10 | IP1000PHY_BMCR_FDX);
137 kprintf("10baseT-FDX, ");
138 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
140 kprintf("100baseTX, ");
141 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
142 IP1000PHY_BMCR_100 | IP1000PHY_BMCR_FDX);
143 kprintf("100baseTX-FDX, ");
144 /* 1000baseT half-duplex, really supported? */
145 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
146 IP1000PHY_BMCR_1000);
147 kprintf("1000baseT, ");
148 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst),
149 IP1000PHY_BMCR_1000 | IP1000PHY_BMCR_FDX);
150 kprintf("1000baseT-FDX, ");
151 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
157 MIIBUS_MEDIAINIT(sc->mii_dev);
162 ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
164 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
165 uint32_t gig, reg, speed;
170 * If we're not polling our PHY instance, just return.
172 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
178 * If the media indicates a different PHY instance,
181 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
182 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
183 PHY_WRITE(sc, IP1000PHY_MII_BMCR,
184 reg | IP1000PHY_BMCR_ISO);
189 * If the interface is not up, don't do anything.
191 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
195 switch (IFM_SUBTYPE(ife->ifm_media)) {
197 ip1000phy_mii_phy_auto(sc);
203 * Manual 1000baseT setting doesn't seem to work.
205 speed = IP1000PHY_BMCR_1000;
209 speed = IP1000PHY_BMCR_100;
213 speed = IP1000PHY_BMCR_10;
220 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
221 speed |= IP1000PHY_BMCR_FDX;
222 gig = IP1000PHY_1000CR_1000T_FDX;
224 gig = IP1000PHY_1000CR_1000T;
227 PHY_WRITE(sc, IP1000PHY_MII_1000CR, 0);
228 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
230 if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)
233 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
234 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
237 * When settning the link manually, one side must
238 * be the master and the other the slave. However
239 * ifmedia doesn't give us a good way to specify
240 * this, so we fake it by using one of the LINK
241 * flags. If LINK0 is set, we program the PHY to
242 * be a master, otherwise it's a slave.
244 if ((mii->mii_ifp->if_flags & IFF_LINK0)) {
245 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
246 IP1000PHY_1000CR_MASTER |
247 IP1000PHY_1000CR_MMASTER |
248 IP1000PHY_1000CR_MANUAL);
250 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
251 IP1000PHY_1000CR_MASTER |
252 IP1000PHY_1000CR_MANUAL);
260 * If we're not currently selected, just return.
262 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
265 * Is the interface even up?
267 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
271 * Only used for autonegotiation.
273 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
279 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
280 if (reg & BMSR_LINK) {
285 /* Announce link loss right after it happens */
286 if (sc->mii_ticks++ == 0)
290 * Only retry autonegotiation every mii_anegticks seconds.
292 if (sc->mii_ticks <= sc->mii_anegticks)
296 ip1000phy_mii_phy_auto(sc);
300 /* Update the media status. */
301 ip1000phy_status(sc);
303 /* Callback if something changed. */
304 mii_phy_update(sc, cmd);
309 ip1000phy_status(struct mii_softc *sc)
311 struct mii_data *mii = sc->mii_pdata;
312 uint32_t bmsr, bmcr, stat;
315 mii->mii_media_status = IFM_AVALID;
316 mii->mii_media_active = IFM_ETHER;
318 bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) |
319 PHY_READ(sc, IP1000PHY_MII_BMSR);
320 if ((bmsr & IP1000PHY_BMSR_LINK) != 0)
321 mii->mii_media_status |= IFM_ACTIVE;
323 bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
324 if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
325 mii->mii_media_active |= IFM_LOOP;
327 if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) {
328 if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) {
329 /* Erg, still trying, I guess... */
330 mii->mii_media_active |= IFM_NONE;
335 stat = PHY_READ(sc, STGE_PhyCtrl);
336 switch (PC_LinkSpeed(stat)) {
337 case PC_LinkSpeed_Down:
338 mii->mii_media_active |= IFM_NONE;
340 case PC_LinkSpeed_10:
341 mii->mii_media_active |= IFM_10_T;
343 case PC_LinkSpeed_100:
344 mii->mii_media_active |= IFM_100_TX;
346 case PC_LinkSpeed_1000:
347 mii->mii_media_active |= IFM_1000_T;
350 if ((stat & PC_PhyDuplexStatus) != 0)
351 mii->mii_media_active |= IFM_FDX;
353 mii->mii_media_active |= IFM_HDX;
355 ar = PHY_READ(sc, IP1000PHY_MII_ANAR);
356 lpar = PHY_READ(sc, IP1000PHY_MII_ANLPAR);
359 * FLAG0 : Rx flow-control
360 * FLAG1 : Tx flow-control
362 if ((ar & IP1000PHY_ANAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_PAUSE))
363 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
364 else if (!(ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
365 (lpar & IP1000PHY_ANLPAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_APAUSE))
366 mii->mii_media_active |= IFM_FLAG1;
367 else if ((ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
368 !(lpar & IP1000PHY_ANLPAR_PAUSE) &&
369 (lpar & IP1000PHY_ANLPAR_APAUSE)) {
370 mii->mii_media_active |= IFM_FLAG0;
374 * FLAG2 : local PHY resolved to MASTER
376 if ((mii->mii_media_active & IFM_1000_T) != 0) {
377 stat = PHY_READ(sc, IP1000PHY_MII_1000SR);
378 if ((stat & IP1000PHY_1000SR_MASTER) != 0)
379 mii->mii_media_active |= IFM_FLAG2;
384 ip1000phy_mii_phy_auto(struct mii_softc *mii)
388 PHY_WRITE(mii, IP1000PHY_MII_ANAR,
389 IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX |
390 IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX |
391 IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE);
392 reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX;
393 reg |= IP1000PHY_1000CR_MASTER;
394 PHY_WRITE(mii, IP1000PHY_MII_1000CR, reg);
395 PHY_WRITE(mii, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX |
396 IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG));
398 return (EJUSTRETURN);
402 ip1000phy_load_dspcode(struct mii_softc *sc)
405 PHY_WRITE(sc, 31, 0x0001);
406 PHY_WRITE(sc, 27, 0x01e0);
407 PHY_WRITE(sc, 31, 0x0002);
408 PHY_WRITE(sc, 27, 0xeb8e);
409 PHY_WRITE(sc, 31, 0x0000);
410 PHY_WRITE(sc, 30, 0x005e);
411 PHY_WRITE(sc, 9, 0x0700);
417 ip1000phy_reset(struct mii_softc *sc)
419 struct stge_softc *stge_sc;
420 struct mii_data *mii;
426 /* clear autoneg/full-duplex as we don't want it after reset */
427 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
428 reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX);
429 PHY_WRITE(sc, MII_BMCR, reg);
433 * XXX There should be more general way to pass PHY specific
434 * data via mii interface.
436 parent = device_get_parent(sc->mii_dev);
437 if (strncmp(device_get_name(parent), "stge", 4) == 0) {
438 stge_sc = device_get_softc(parent);
439 if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
440 ip1000phy_load_dspcode(sc);