blob: fa7245a573e2cc94d5faeb3ed4c6b482662def69 [file] [log] [blame]
Russell King20b2af32017-06-05 12:23:16 +01001/*
2 * Marvell 10G 88x3310 PHY driver
3 *
4 * Based upon the ID registers, this PHY appears to be a mixture of IPs
5 * from two different companies.
6 *
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
Russell King05ca1b32017-12-29 12:46:22 +00009 * via observation and experimentation for a setup using single-lane Serdes:
Russell King20b2af32017-06-05 12:23:16 +010010 *
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
14 *
Russell King05ca1b32017-12-29 12:46:22 +000015 * With XAUI, observation shows:
16 *
17 * XAUI PHYXS -- <appropriate PCS as above>
18 *
19 * and no switching of the host interface mode occurs.
20 *
Russell King20b2af32017-06-05 12:23:16 +010021 * 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.
23 */
24#include <linux/phy.h>
Antoine Tenart952b6b32017-11-28 14:26:30 +010025#include <linux/marvell_phy.h>
Russell King20b2af32017-06-05 12:23:16 +010026
27enum {
28 MV_PCS_BASE_T = 0x0000,
29 MV_PCS_BASE_R = 0x1000,
30 MV_PCS_1000BASEX = 0x2000,
31
Russell Kingea4efe22017-12-29 12:46:27 +000032 MV_PCS_PAIRSWAP = 0x8182,
33 MV_PCS_PAIRSWAP_MASK = 0x0003,
34 MV_PCS_PAIRSWAP_AB = 0x0002,
35 MV_PCS_PAIRSWAP_NONE = 0x0003,
36
Russell King20b2af32017-06-05 12:23:16 +010037 /* 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.
40 */
41 MV_AN_CTRL1000 = 0x8000, /* 1000base-T control register */
42 MV_AN_STAT1000 = 0x8001, /* 1000base-T status register */
43
44 /* This register appears to reflect the copper status */
45 MV_AN_RESULT = 0xa016,
46 MV_AN_RESULT_SPD_10 = BIT(12),
47 MV_AN_RESULT_SPD_100 = BIT(13),
48 MV_AN_RESULT_SPD_1000 = BIT(14),
49 MV_AN_RESULT_SPD_10000 = BIT(15),
50};
51
52static int mv3310_modify(struct phy_device *phydev, int devad, u16 reg,
53 u16 mask, u16 bits)
54{
55 int old, val, ret;
56
57 old = phy_read_mmd(phydev, devad, reg);
58 if (old < 0)
59 return old;
60
61 val = (old & ~mask) | (bits & mask);
62 if (val == old)
63 return 0;
64
65 ret = phy_write_mmd(phydev, devad, reg, val);
66
67 return ret < 0 ? ret : 1;
68}
69
70static int mv3310_probe(struct phy_device *phydev)
71{
72 u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
73
74 if (!phydev->is_c45 ||
75 (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
76 return -ENODEV;
77
78 return 0;
79}
80
81/*
82 * Resetting the MV88X3310 causes it to become non-responsive. Avoid
83 * setting the reset bit(s).
84 */
85static int mv3310_soft_reset(struct phy_device *phydev)
86{
87 return 0;
88}
89
90static int mv3310_config_init(struct phy_device *phydev)
91{
92 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported) = { 0, };
93 u32 mask;
94 int val;
95
96 /* Check that the PHY interface type is compatible */
97 if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
Russell King20b2af32017-06-05 12:23:16 +010098 phydev->interface != PHY_INTERFACE_MODE_XAUI &&
99 phydev->interface != PHY_INTERFACE_MODE_RXAUI &&
100 phydev->interface != PHY_INTERFACE_MODE_10GKR)
101 return -ENODEV;
102
103 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
104 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, supported);
105
106 if (phydev->c45_ids.devices_in_package & MDIO_DEVS_AN) {
107 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
108 if (val < 0)
109 return val;
110
111 if (val & MDIO_AN_STAT1_ABLE)
112 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, supported);
113 }
114
115 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_STAT2);
116 if (val < 0)
117 return val;
118
119 /* Ethtool does not support the WAN mode bits */
120 if (val & (MDIO_PMA_STAT2_10GBSR | MDIO_PMA_STAT2_10GBLR |
121 MDIO_PMA_STAT2_10GBER | MDIO_PMA_STAT2_10GBLX4 |
122 MDIO_PMA_STAT2_10GBSW | MDIO_PMA_STAT2_10GBLW |
123 MDIO_PMA_STAT2_10GBEW))
124 __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
125 if (val & MDIO_PMA_STAT2_10GBSR)
126 __set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, supported);
127 if (val & MDIO_PMA_STAT2_10GBLR)
128 __set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, supported);
129 if (val & MDIO_PMA_STAT2_10GBER)
130 __set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, supported);
131
132 if (val & MDIO_PMA_STAT2_EXTABLE) {
133 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_EXTABLE);
134 if (val < 0)
135 return val;
136
137 if (val & (MDIO_PMA_EXTABLE_10GBT | MDIO_PMA_EXTABLE_1000BT |
138 MDIO_PMA_EXTABLE_100BTX | MDIO_PMA_EXTABLE_10BT))
139 __set_bit(ETHTOOL_LINK_MODE_TP_BIT, supported);
140 if (val & MDIO_PMA_EXTABLE_10GBLRM)
141 __set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
142 if (val & (MDIO_PMA_EXTABLE_10GBKX4 | MDIO_PMA_EXTABLE_10GBKR |
143 MDIO_PMA_EXTABLE_1000BKX))
144 __set_bit(ETHTOOL_LINK_MODE_Backplane_BIT, supported);
145 if (val & MDIO_PMA_EXTABLE_10GBLRM)
146 __set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT,
147 supported);
148 if (val & MDIO_PMA_EXTABLE_10GBT)
149 __set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
150 supported);
151 if (val & MDIO_PMA_EXTABLE_10GBKX4)
152 __set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
153 supported);
154 if (val & MDIO_PMA_EXTABLE_10GBKR)
155 __set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
156 supported);
157 if (val & MDIO_PMA_EXTABLE_1000BT)
158 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
159 supported);
160 if (val & MDIO_PMA_EXTABLE_1000BKX)
161 __set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
162 supported);
163 if (val & MDIO_PMA_EXTABLE_100BTX)
164 __set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
165 supported);
166 if (val & MDIO_PMA_EXTABLE_10BT)
167 __set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
168 supported);
169 }
170
171 if (!ethtool_convert_link_mode_to_legacy_u32(&mask, supported))
172 dev_warn(&phydev->mdio.dev,
173 "PHY supports (%*pb) more modes than phylib supports, some modes not supported.\n",
174 __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
175
176 phydev->supported &= mask;
177 phydev->advertising &= phydev->supported;
178
179 return 0;
180}
181
182static int mv3310_config_aneg(struct phy_device *phydev)
183{
184 bool changed = false;
185 u32 advertising;
186 int ret;
187
Russell Kingea4efe22017-12-29 12:46:27 +0000188 /* We don't support manual MDI control */
189 phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
190
Russell King20b2af32017-06-05 12:23:16 +0100191 if (phydev->autoneg == AUTONEG_DISABLE) {
192 ret = genphy_c45_pma_setup_forced(phydev);
193 if (ret < 0)
194 return ret;
195
196 return genphy_c45_an_disable_aneg(phydev);
197 }
198
199 phydev->advertising &= phydev->supported;
200 advertising = phydev->advertising;
201
202 ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE,
203 ADVERTISE_ALL | ADVERTISE_100BASE4 |
204 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
205 ethtool_adv_to_mii_adv_t(advertising));
206 if (ret < 0)
207 return ret;
208 if (ret > 0)
209 changed = true;
210
211 ret = mv3310_modify(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
212 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
213 ethtool_adv_to_mii_ctrl1000_t(advertising));
214 if (ret < 0)
215 return ret;
216 if (ret > 0)
217 changed = true;
218
219 /* 10G control register */
220 ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
221 MDIO_AN_10GBT_CTRL_ADV10G,
222 advertising & ADVERTISED_10000baseT_Full ?
223 MDIO_AN_10GBT_CTRL_ADV10G : 0);
224 if (ret < 0)
225 return ret;
226 if (ret > 0)
227 changed = true;
228
229 if (changed)
230 ret = genphy_c45_restart_aneg(phydev);
231
232 return ret;
233}
234
235static int mv3310_aneg_done(struct phy_device *phydev)
236{
237 int val;
238
239 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
240 if (val < 0)
241 return val;
242
243 if (val & MDIO_STAT1_LSTATUS)
244 return 1;
245
246 return genphy_c45_aneg_done(phydev);
247}
248
Russell King36c44492017-12-29 12:46:32 +0000249static void mv3310_update_interface(struct phy_device *phydev)
250{
251 if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
252 phydev->interface == PHY_INTERFACE_MODE_10GKR) && phydev->link) {
253 /* The PHY automatically switches its serdes interface (and
254 * active PHYXS instance) between Cisco SGMII and 10GBase-KR
255 * modes according to the speed. Florian suggests setting
256 * phydev->interface to communicate this to the MAC. Only do
257 * this if we are already in either SGMII or 10GBase-KR mode.
258 */
259 if (phydev->speed == SPEED_10000)
260 phydev->interface = PHY_INTERFACE_MODE_10GKR;
261 else if (phydev->speed >= SPEED_10 &&
262 phydev->speed < SPEED_10000)
263 phydev->interface = PHY_INTERFACE_MODE_SGMII;
264 }
265}
266
Russell King20b2af32017-06-05 12:23:16 +0100267/* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
268static int mv3310_read_10gbr_status(struct phy_device *phydev)
269{
270 phydev->link = 1;
271 phydev->speed = SPEED_10000;
272 phydev->duplex = DUPLEX_FULL;
273
Russell King36c44492017-12-29 12:46:32 +0000274 mv3310_update_interface(phydev);
Russell King20b2af32017-06-05 12:23:16 +0100275
276 return 0;
277}
278
279static int mv3310_read_status(struct phy_device *phydev)
280{
281 u32 mmd_mask = phydev->c45_ids.devices_in_package;
282 int val;
283
284 /* The vendor devads do not report link status. Avoid the PHYXS
285 * instance as there are three, and its status depends on the MAC
286 * being appropriately configured for the negotiated speed.
287 */
288 mmd_mask &= ~(BIT(MDIO_MMD_VEND1) | BIT(MDIO_MMD_VEND2) |
289 BIT(MDIO_MMD_PHYXS));
290
291 phydev->speed = SPEED_UNKNOWN;
292 phydev->duplex = DUPLEX_UNKNOWN;
293 phydev->lp_advertising = 0;
294 phydev->link = 0;
295 phydev->pause = 0;
296 phydev->asym_pause = 0;
Russell Kingea4efe22017-12-29 12:46:27 +0000297 phydev->mdix = 0;
Russell King20b2af32017-06-05 12:23:16 +0100298
299 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
300 if (val < 0)
301 return val;
302
303 if (val & MDIO_STAT1_LSTATUS)
304 return mv3310_read_10gbr_status(phydev);
305
306 val = genphy_c45_read_link(phydev, mmd_mask);
307 if (val < 0)
308 return val;
309
310 phydev->link = val > 0 ? 1 : 0;
311
312 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
313 if (val < 0)
314 return val;
315
316 if (val & MDIO_AN_STAT1_COMPLETE) {
317 val = genphy_c45_read_lpa(phydev);
318 if (val < 0)
319 return val;
320
321 /* Read the link partner's 1G advertisment */
322 val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
323 if (val < 0)
324 return val;
325
326 phydev->lp_advertising |= mii_stat1000_to_ethtool_lpa_t(val);
327
328 if (phydev->autoneg == AUTONEG_ENABLE) {
329 val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_RESULT);
330 if (val < 0)
331 return val;
332
333 if (val & MV_AN_RESULT_SPD_10000)
334 phydev->speed = SPEED_10000;
335 else if (val & MV_AN_RESULT_SPD_1000)
336 phydev->speed = SPEED_1000;
337 else if (val & MV_AN_RESULT_SPD_100)
338 phydev->speed = SPEED_100;
339 else if (val & MV_AN_RESULT_SPD_10)
340 phydev->speed = SPEED_10;
341
342 phydev->duplex = DUPLEX_FULL;
343 }
344 }
345
346 if (phydev->autoneg != AUTONEG_ENABLE) {
347 val = genphy_c45_read_pma(phydev);
348 if (val < 0)
349 return val;
350 }
351
Russell Kingea4efe22017-12-29 12:46:27 +0000352 if (phydev->speed == SPEED_10000) {
353 val = genphy_c45_read_mdix(phydev);
354 if (val < 0)
355 return val;
356 } else {
357 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_PAIRSWAP);
358 if (val < 0)
359 return val;
360
361 switch (val & MV_PCS_PAIRSWAP_MASK) {
362 case MV_PCS_PAIRSWAP_AB:
363 phydev->mdix = ETH_TP_MDI_X;
364 break;
365 case MV_PCS_PAIRSWAP_NONE:
366 phydev->mdix = ETH_TP_MDI;
367 break;
368 default:
369 phydev->mdix = ETH_TP_MDI_INVALID;
370 break;
371 }
372 }
373
Russell King36c44492017-12-29 12:46:32 +0000374 mv3310_update_interface(phydev);
Russell King20b2af32017-06-05 12:23:16 +0100375
376 return 0;
377}
378
379static struct phy_driver mv3310_drivers[] = {
380 {
381 .phy_id = 0x002b09aa,
Antoine Tenart952b6b32017-11-28 14:26:30 +0100382 .phy_id_mask = MARVELL_PHY_ID_MASK,
Russell King20b2af32017-06-05 12:23:16 +0100383 .name = "mv88x3310",
384 .features = SUPPORTED_10baseT_Full |
385 SUPPORTED_100baseT_Full |
386 SUPPORTED_1000baseT_Full |
387 SUPPORTED_Autoneg |
388 SUPPORTED_TP |
389 SUPPORTED_FIBRE |
390 SUPPORTED_10000baseT_Full |
391 SUPPORTED_Backplane,
392 .probe = mv3310_probe,
393 .soft_reset = mv3310_soft_reset,
394 .config_init = mv3310_config_init,
395 .config_aneg = mv3310_config_aneg,
396 .aneg_done = mv3310_aneg_done,
397 .read_status = mv3310_read_status,
398 },
399};
400
401module_phy_driver(mv3310_drivers);
402
403static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
Antoine Tenart952b6b32017-11-28 14:26:30 +0100404 { 0x002b09aa, MARVELL_PHY_ID_MASK },
Russell King20b2af32017-06-05 12:23:16 +0100405 { },
406};
407MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
408MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
409MODULE_LICENSE("GPL");