Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / phy / marvell10g.c
blob8a0bd98fdec77400e729683a4aec6381a4c57051
1 /*
2 * Marvell 10G 88x3310 PHY driver
4 * Based upon the ID registers, this PHY appears to be a mixture of IPs
5 * from two different companies.
7 * There appears to be several different data paths through the PHY which
8 * are automatically managed by the PHY. The following has been determined
9 * via observation and experimentation for a setup using single-lane Serdes:
11 * SGMII PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for <= 1G)
12 * 10GBASE-KR PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for 10G)
13 * 10GBASE-KR PHYXS -- BASE-R PCS -- Fiber
15 * With XAUI, observation shows:
17 * XAUI PHYXS -- <appropriate PCS as above>
19 * and no switching of the host interface mode occurs.
21 * If both the fiber and copper ports are connected, the first to gain
22 * link takes priority and the other port is completely locked out.
24 #include <linux/phy.h>
25 #include <linux/marvell_phy.h>
27 enum {
28 MV_PCS_BASE_T = 0x0000,
29 MV_PCS_BASE_R = 0x1000,
30 MV_PCS_1000BASEX = 0x2000,
32 MV_PCS_PAIRSWAP = 0x8182,
33 MV_PCS_PAIRSWAP_MASK = 0x0003,
34 MV_PCS_PAIRSWAP_AB = 0x0002,
35 MV_PCS_PAIRSWAP_NONE = 0x0003,
37 /* These registers appear at 0x800X and 0xa00X - the 0xa00X control
38 * registers appear to set themselves to the 0x800X when AN is
39 * restarted, but status registers appear readable from either.
41 MV_AN_CTRL1000 = 0x8000, /* 1000base-T control register */
42 MV_AN_STAT1000 = 0x8001, /* 1000base-T status register */
45 static int mv3310_modify(struct phy_device *phydev, int devad, u16 reg,
46 u16 mask, u16 bits)
48 int old, val, ret;
50 old = phy_read_mmd(phydev, devad, reg);
51 if (old < 0)
52 return old;
54 val = (old & ~mask) | (bits & mask);
55 if (val == old)
56 return 0;
58 ret = phy_write_mmd(phydev, devad, reg, val);
60 return ret < 0 ? ret : 1;
63 static int mv3310_probe(struct phy_device *phydev)
65 u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
67 if (!phydev->is_c45 ||
68 (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
69 return -ENODEV;
71 return 0;
75 * Resetting the MV88X3310 causes it to become non-responsive. Avoid
76 * setting the reset bit(s).
78 static int mv3310_soft_reset(struct phy_device *phydev)
80 return 0;
83 static int mv3310_config_init(struct phy_device *phydev)
85 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported) = { 0, };
86 u32 mask;
87 int val;
89 /* Check that the PHY interface type is compatible */
90 if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
91 phydev->interface != PHY_INTERFACE_MODE_XAUI &&
92 phydev->interface != PHY_INTERFACE_MODE_RXAUI &&
93 phydev->interface != PHY_INTERFACE_MODE_10GKR)
94 return -ENODEV;
96 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
97 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, supported);
99 if (phydev->c45_ids.devices_in_package & MDIO_DEVS_AN) {
100 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
101 if (val < 0)
102 return val;
104 if (val & MDIO_AN_STAT1_ABLE)
105 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, supported);
108 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_STAT2);
109 if (val < 0)
110 return val;
112 /* Ethtool does not support the WAN mode bits */
113 if (val & (MDIO_PMA_STAT2_10GBSR | MDIO_PMA_STAT2_10GBLR |
114 MDIO_PMA_STAT2_10GBER | MDIO_PMA_STAT2_10GBLX4 |
115 MDIO_PMA_STAT2_10GBSW | MDIO_PMA_STAT2_10GBLW |
116 MDIO_PMA_STAT2_10GBEW))
117 __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
118 if (val & MDIO_PMA_STAT2_10GBSR)
119 __set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, supported);
120 if (val & MDIO_PMA_STAT2_10GBLR)
121 __set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, supported);
122 if (val & MDIO_PMA_STAT2_10GBER)
123 __set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, supported);
125 if (val & MDIO_PMA_STAT2_EXTABLE) {
126 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_EXTABLE);
127 if (val < 0)
128 return val;
130 if (val & (MDIO_PMA_EXTABLE_10GBT | MDIO_PMA_EXTABLE_1000BT |
131 MDIO_PMA_EXTABLE_100BTX | MDIO_PMA_EXTABLE_10BT))
132 __set_bit(ETHTOOL_LINK_MODE_TP_BIT, supported);
133 if (val & MDIO_PMA_EXTABLE_10GBLRM)
134 __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
135 if (val & (MDIO_PMA_EXTABLE_10GBKX4 | MDIO_PMA_EXTABLE_10GBKR |
136 MDIO_PMA_EXTABLE_1000BKX))
137 __set_bit(ETHTOOL_LINK_MODE_Backplane_BIT, supported);
138 if (val & MDIO_PMA_EXTABLE_10GBLRM)
139 __set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT,
140 supported);
141 if (val & MDIO_PMA_EXTABLE_10GBT)
142 __set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
143 supported);
144 if (val & MDIO_PMA_EXTABLE_10GBKX4)
145 __set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
146 supported);
147 if (val & MDIO_PMA_EXTABLE_10GBKR)
148 __set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
149 supported);
150 if (val & MDIO_PMA_EXTABLE_1000BT)
151 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
152 supported);
153 if (val & MDIO_PMA_EXTABLE_1000BKX)
154 __set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
155 supported);
156 if (val & MDIO_PMA_EXTABLE_100BTX) {
157 __set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
158 supported);
159 __set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
160 supported);
162 if (val & MDIO_PMA_EXTABLE_10BT) {
163 __set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
164 supported);
165 __set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
166 supported);
170 if (!ethtool_convert_link_mode_to_legacy_u32(&mask, supported))
171 dev_warn(&phydev->mdio.dev,
172 "PHY supports (%*pb) more modes than phylib supports, some modes not supported.\n",
173 __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
175 phydev->supported &= mask;
176 phydev->advertising &= phydev->supported;
178 return 0;
181 static int mv3310_config_aneg(struct phy_device *phydev)
183 bool changed = false;
184 u32 advertising;
185 int ret;
187 /* We don't support manual MDI control */
188 phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
190 if (phydev->autoneg == AUTONEG_DISABLE) {
191 ret = genphy_c45_pma_setup_forced(phydev);
192 if (ret < 0)
193 return ret;
195 return genphy_c45_an_disable_aneg(phydev);
198 phydev->advertising &= phydev->supported;
199 advertising = phydev->advertising;
201 ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE,
202 ADVERTISE_ALL | ADVERTISE_100BASE4 |
203 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
204 ethtool_adv_to_mii_adv_t(advertising));
205 if (ret < 0)
206 return ret;
207 if (ret > 0)
208 changed = true;
210 ret = mv3310_modify(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
211 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
212 ethtool_adv_to_mii_ctrl1000_t(advertising));
213 if (ret < 0)
214 return ret;
215 if (ret > 0)
216 changed = true;
218 /* 10G control register */
219 ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
220 MDIO_AN_10GBT_CTRL_ADV10G,
221 advertising & ADVERTISED_10000baseT_Full ?
222 MDIO_AN_10GBT_CTRL_ADV10G : 0);
223 if (ret < 0)
224 return ret;
225 if (ret > 0)
226 changed = true;
228 if (changed)
229 ret = genphy_c45_restart_aneg(phydev);
231 return ret;
234 static int mv3310_aneg_done(struct phy_device *phydev)
236 int val;
238 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
239 if (val < 0)
240 return val;
242 if (val & MDIO_STAT1_LSTATUS)
243 return 1;
245 return genphy_c45_aneg_done(phydev);
248 static void mv3310_update_interface(struct phy_device *phydev)
250 if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
251 phydev->interface == PHY_INTERFACE_MODE_10GKR) && phydev->link) {
252 /* The PHY automatically switches its serdes interface (and
253 * active PHYXS instance) between Cisco SGMII and 10GBase-KR
254 * modes according to the speed. Florian suggests setting
255 * phydev->interface to communicate this to the MAC. Only do
256 * this if we are already in either SGMII or 10GBase-KR mode.
258 if (phydev->speed == SPEED_10000)
259 phydev->interface = PHY_INTERFACE_MODE_10GKR;
260 else if (phydev->speed >= SPEED_10 &&
261 phydev->speed < SPEED_10000)
262 phydev->interface = PHY_INTERFACE_MODE_SGMII;
266 /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
267 static int mv3310_read_10gbr_status(struct phy_device *phydev)
269 phydev->link = 1;
270 phydev->speed = SPEED_10000;
271 phydev->duplex = DUPLEX_FULL;
273 mv3310_update_interface(phydev);
275 return 0;
278 static int mv3310_read_status(struct phy_device *phydev)
280 u32 mmd_mask = phydev->c45_ids.devices_in_package;
281 int val;
283 /* The vendor devads do not report link status. Avoid the PHYXS
284 * instance as there are three, and its status depends on the MAC
285 * being appropriately configured for the negotiated speed.
287 mmd_mask &= ~(BIT(MDIO_MMD_VEND1) | BIT(MDIO_MMD_VEND2) |
288 BIT(MDIO_MMD_PHYXS));
290 phydev->speed = SPEED_UNKNOWN;
291 phydev->duplex = DUPLEX_UNKNOWN;
292 phydev->lp_advertising = 0;
293 phydev->link = 0;
294 phydev->pause = 0;
295 phydev->asym_pause = 0;
296 phydev->mdix = 0;
298 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
299 if (val < 0)
300 return val;
302 if (val & MDIO_STAT1_LSTATUS)
303 return mv3310_read_10gbr_status(phydev);
305 val = genphy_c45_read_link(phydev, mmd_mask);
306 if (val < 0)
307 return val;
309 phydev->link = val > 0 ? 1 : 0;
311 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
312 if (val < 0)
313 return val;
315 if (val & MDIO_AN_STAT1_COMPLETE) {
316 val = genphy_c45_read_lpa(phydev);
317 if (val < 0)
318 return val;
320 /* Read the link partner's 1G advertisment */
321 val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
322 if (val < 0)
323 return val;
325 phydev->lp_advertising |= mii_stat1000_to_ethtool_lpa_t(val);
327 if (phydev->autoneg == AUTONEG_ENABLE)
328 phy_resolve_aneg_linkmode(phydev);
331 if (phydev->autoneg != AUTONEG_ENABLE) {
332 val = genphy_c45_read_pma(phydev);
333 if (val < 0)
334 return val;
337 if (phydev->speed == SPEED_10000) {
338 val = genphy_c45_read_mdix(phydev);
339 if (val < 0)
340 return val;
341 } else {
342 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_PAIRSWAP);
343 if (val < 0)
344 return val;
346 switch (val & MV_PCS_PAIRSWAP_MASK) {
347 case MV_PCS_PAIRSWAP_AB:
348 phydev->mdix = ETH_TP_MDI_X;
349 break;
350 case MV_PCS_PAIRSWAP_NONE:
351 phydev->mdix = ETH_TP_MDI;
352 break;
353 default:
354 phydev->mdix = ETH_TP_MDI_INVALID;
355 break;
359 mv3310_update_interface(phydev);
361 return 0;
364 static struct phy_driver mv3310_drivers[] = {
366 .phy_id = 0x002b09aa,
367 .phy_id_mask = MARVELL_PHY_ID_MASK,
368 .name = "mv88x3310",
369 .features = SUPPORTED_10baseT_Full |
370 SUPPORTED_10baseT_Half |
371 SUPPORTED_100baseT_Full |
372 SUPPORTED_100baseT_Half |
373 SUPPORTED_1000baseT_Full |
374 SUPPORTED_Autoneg |
375 SUPPORTED_TP |
376 SUPPORTED_FIBRE |
377 SUPPORTED_10000baseT_Full |
378 SUPPORTED_Backplane,
379 .probe = mv3310_probe,
380 .soft_reset = mv3310_soft_reset,
381 .config_init = mv3310_config_init,
382 .config_aneg = mv3310_config_aneg,
383 .aneg_done = mv3310_aneg_done,
384 .read_status = mv3310_read_status,
388 module_phy_driver(mv3310_drivers);
390 static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
391 { 0x002b09aa, MARVELL_PHY_ID_MASK },
392 { },
394 MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
395 MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
396 MODULE_LICENSE("GPL");