blob: aa37e8547cd01efc72f672ad6c3ee09f4e885624 [file] [log] [blame]
Raju Lakkarajud50736a2016-08-05 17:54:21 +05301/*
2 * Driver for Microsemi VSC85xx PHYs
3 *
4 * Author: Nagaraju Lakkaraju
5 * License: Dual MIT/GPL
6 * Copyright (c) 2016 Microsemi Corporation
7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/mdio.h>
12#include <linux/mii.h>
13#include <linux/phy.h>
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +053014#include <linux/of.h>
Raju Lakkaraju0a55c122016-10-05 14:19:27 +053015#include <linux/netdevice.h>
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +053016#include <dt-bindings/net/mscc-phy-vsc8531.h>
Raju Lakkarajud50736a2016-08-05 17:54:21 +053017
18enum rgmii_rx_clock_delay {
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +053019 RGMII_RX_CLK_DELAY_0_2_NS = 0,
20 RGMII_RX_CLK_DELAY_0_8_NS = 1,
21 RGMII_RX_CLK_DELAY_1_1_NS = 2,
22 RGMII_RX_CLK_DELAY_1_7_NS = 3,
23 RGMII_RX_CLK_DELAY_2_0_NS = 4,
24 RGMII_RX_CLK_DELAY_2_3_NS = 5,
25 RGMII_RX_CLK_DELAY_2_6_NS = 6,
26 RGMII_RX_CLK_DELAY_3_4_NS = 7
Raju Lakkarajud50736a2016-08-05 17:54:21 +053027};
28
Raju Lakkaraju1a211012016-09-19 15:33:54 +053029/* Microsemi VSC85xx PHY registers */
30/* IEEE 802. Std Registers */
Raju Lakkaraju233275e2016-11-29 15:16:48 +053031#define MSCC_PHY_BYPASS_CONTROL 18
32#define DISABLE_HP_AUTO_MDIX_MASK 0x0080
33#define DISABLE_PAIR_SWAP_CORR_MASK 0x0020
34#define DISABLE_POLARITY_CORR_MASK 0x0010
35
Raju Lakkaraju1a211012016-09-19 15:33:54 +053036#define MSCC_PHY_EXT_PHY_CNTL_1 23
37#define MAC_IF_SELECTION_MASK 0x1800
38#define MAC_IF_SELECTION_GMII 0
39#define MAC_IF_SELECTION_RMII 1
40#define MAC_IF_SELECTION_RGMII 2
41#define MAC_IF_SELECTION_POS 11
42#define FAR_END_LOOPBACK_MODE_MASK 0x0008
43
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +053044#define MII_VSC85XX_INT_MASK 25
45#define MII_VSC85XX_INT_MASK_MASK 0xa000
Raju Lakkaraju0a55c122016-10-05 14:19:27 +053046#define MII_VSC85XX_INT_MASK_WOL 0x0040
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +053047#define MII_VSC85XX_INT_STATUS 26
Raju Lakkarajud50736a2016-08-05 17:54:21 +053048
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +053049#define MSCC_PHY_WOL_MAC_CONTROL 27
50#define EDGE_RATE_CNTL_POS 5
51#define EDGE_RATE_CNTL_MASK 0x00E0
52
Raju Lakkaraju233275e2016-11-29 15:16:48 +053053#define MSCC_PHY_DEV_AUX_CNTL 28
54#define HP_AUTO_MDIX_X_OVER_IND_MASK 0x2000
55
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +053056#define MSCC_PHY_LED_MODE_SEL 29
Quentin Schulz11bfdab2018-09-03 10:48:47 +020057#define LED_MODE_SEL_POS(x) ((x) * 4)
58#define LED_MODE_SEL_MASK(x) (GENMASK(3, 0) << LED_MODE_SEL_POS(x))
59#define LED_MODE_SEL(x, mode) (((mode) << LED_MODE_SEL_POS(x)) & LED_MODE_SEL_MASK(x))
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +053060
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +053061#define MSCC_EXT_PAGE_ACCESS 31
62#define MSCC_PHY_PAGE_STANDARD 0x0000 /* Standard registers */
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +010063#define MSCC_PHY_PAGE_EXTENDED 0x0001 /* Extended registers */
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +053064#define MSCC_PHY_PAGE_EXTENDED_2 0x0002 /* Extended reg - page 2 */
Raju Lakkarajud50736a2016-08-05 17:54:21 +053065
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +010066/* Extended Page 1 Registers */
Raju Lakkaraju233275e2016-11-29 15:16:48 +053067#define MSCC_PHY_EXT_MODE_CNTL 19
68#define FORCE_MDI_CROSSOVER_MASK 0x000C
69#define FORCE_MDI_CROSSOVER_MDIX 0x000C
70#define FORCE_MDI_CROSSOVER_MDI 0x0008
71
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +010072#define MSCC_PHY_ACTIPHY_CNTL 20
73#define DOWNSHIFT_CNTL_MASK 0x001C
74#define DOWNSHIFT_EN 0x0010
75#define DOWNSHIFT_CNTL_POS 2
76
Raju Lakkarajud50736a2016-08-05 17:54:21 +053077/* Extended Page 2 Registers */
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +053078#define MSCC_PHY_RGMII_CNTL 20
79#define RGMII_RX_CLK_DELAY_MASK 0x0070
80#define RGMII_RX_CLK_DELAY_POS 4
Raju Lakkarajud50736a2016-08-05 17:54:21 +053081
Raju Lakkaraju0a55c122016-10-05 14:19:27 +053082#define MSCC_PHY_WOL_LOWER_MAC_ADDR 21
83#define MSCC_PHY_WOL_MID_MAC_ADDR 22
84#define MSCC_PHY_WOL_UPPER_MAC_ADDR 23
85#define MSCC_PHY_WOL_LOWER_PASSWD 24
86#define MSCC_PHY_WOL_MID_PASSWD 25
87#define MSCC_PHY_WOL_UPPER_PASSWD 26
88
89#define MSCC_PHY_WOL_MAC_CONTROL 27
90#define SECURE_ON_ENABLE 0x8000
91#define SECURE_ON_PASSWD_LEN_4 0x4000
92
Raju Lakkarajud50736a2016-08-05 17:54:21 +053093/* Microsemi PHY ID's */
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +020094#define PHY_ID_VSC8530 0x00070560
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +053095#define PHY_ID_VSC8531 0x00070570
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +020096#define PHY_ID_VSC8540 0x00070760
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +053097#define PHY_ID_VSC8541 0x00070770
Raju Lakkarajud50736a2016-08-05 17:54:21 +053098
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +020099#define MSCC_VDDMAC_1500 1500
100#define MSCC_VDDMAC_1800 1800
101#define MSCC_VDDMAC_2500 2500
102#define MSCC_VDDMAC_3300 3300
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530103
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100104#define DOWNSHIFT_COUNT_MAX 5
105
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200106#define MAX_LEDS 4
Quentin Schulz0969aba2018-09-03 10:48:48 +0200107#define VSC85XX_SUPP_LED_MODES (BIT(VSC8531_LINK_ACTIVITY) | \
108 BIT(VSC8531_LINK_1000_ACTIVITY) | \
109 BIT(VSC8531_LINK_100_ACTIVITY) | \
110 BIT(VSC8531_LINK_10_ACTIVITY) | \
111 BIT(VSC8531_LINK_100_1000_ACTIVITY) | \
112 BIT(VSC8531_LINK_10_1000_ACTIVITY) | \
113 BIT(VSC8531_LINK_10_100_ACTIVITY) | \
114 BIT(VSC8531_DUPLEX_COLLISION) | \
115 BIT(VSC8531_COLLISION) | \
116 BIT(VSC8531_ACTIVITY) | \
117 BIT(VSC8531_AUTONEG_FAULT) | \
118 BIT(VSC8531_SERIAL_MODE) | \
119 BIT(VSC8531_FORCE_LED_OFF) | \
120 BIT(VSC8531_FORCE_LED_ON))
121
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530122struct vsc8531_private {
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200123 int rate_magic;
Quentin Schulz0969aba2018-09-03 10:48:48 +0200124 u16 supp_led_modes;
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200125 u8 leds_mode[MAX_LEDS];
126 u8 nleds;
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530127};
128
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200129#ifdef CONFIG_OF_MDIO
130struct vsc8531_edge_rate_table {
131 u16 vddmac;
132 u8 slowdown[8];
133};
134
135static const struct vsc8531_edge_rate_table edge_table[] = {
136 {MSCC_VDDMAC_3300, { 0, 2, 4, 7, 10, 17, 29, 53} },
137 {MSCC_VDDMAC_2500, { 0, 3, 6, 10, 14, 23, 37, 63} },
138 {MSCC_VDDMAC_1800, { 0, 5, 9, 16, 23, 35, 52, 76} },
139 {MSCC_VDDMAC_1500, { 0, 6, 14, 21, 29, 42, 58, 77} },
140};
141#endif /* CONFIG_OF_MDIO */
142
Quentin Schulz86ff7362018-07-30 14:53:13 +0200143static int vsc85xx_phy_page_set(struct phy_device *phydev, u16 page)
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530144{
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530145 int rc;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530146
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530147 rc = phy_write(phydev, MSCC_EXT_PAGE_ACCESS, page);
148 return rc;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530149}
150
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +0530151static int vsc85xx_led_cntl_set(struct phy_device *phydev,
152 u8 led_num,
153 u8 mode)
154{
155 int rc;
156 u16 reg_val;
157
158 mutex_lock(&phydev->lock);
159 reg_val = phy_read(phydev, MSCC_PHY_LED_MODE_SEL);
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200160 reg_val &= ~LED_MODE_SEL_MASK(led_num);
161 reg_val |= LED_MODE_SEL(led_num, (u16)mode);
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +0530162 rc = phy_write(phydev, MSCC_PHY_LED_MODE_SEL, reg_val);
163 mutex_unlock(&phydev->lock);
164
165 return rc;
166}
167
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530168static int vsc85xx_mdix_get(struct phy_device *phydev, u8 *mdix)
169{
170 u16 reg_val;
171
172 reg_val = phy_read(phydev, MSCC_PHY_DEV_AUX_CNTL);
173 if (reg_val & HP_AUTO_MDIX_X_OVER_IND_MASK)
174 *mdix = ETH_TP_MDI_X;
175 else
176 *mdix = ETH_TP_MDI;
177
178 return 0;
179}
180
181static int vsc85xx_mdix_set(struct phy_device *phydev, u8 mdix)
182{
183 int rc;
184 u16 reg_val;
185
186 reg_val = phy_read(phydev, MSCC_PHY_BYPASS_CONTROL);
187 if ((mdix == ETH_TP_MDI) || (mdix == ETH_TP_MDI_X)) {
188 reg_val |= (DISABLE_PAIR_SWAP_CORR_MASK |
189 DISABLE_POLARITY_CORR_MASK |
190 DISABLE_HP_AUTO_MDIX_MASK);
191 } else {
192 reg_val &= ~(DISABLE_PAIR_SWAP_CORR_MASK |
193 DISABLE_POLARITY_CORR_MASK |
194 DISABLE_HP_AUTO_MDIX_MASK);
195 }
196 rc = phy_write(phydev, MSCC_PHY_BYPASS_CONTROL, reg_val);
197 if (rc != 0)
198 return rc;
199
200 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED);
201 if (rc != 0)
202 return rc;
203
204 reg_val = phy_read(phydev, MSCC_PHY_EXT_MODE_CNTL);
205 reg_val &= ~(FORCE_MDI_CROSSOVER_MASK);
206 if (mdix == ETH_TP_MDI)
207 reg_val |= FORCE_MDI_CROSSOVER_MDI;
208 else if (mdix == ETH_TP_MDI_X)
209 reg_val |= FORCE_MDI_CROSSOVER_MDIX;
210 rc = phy_write(phydev, MSCC_PHY_EXT_MODE_CNTL, reg_val);
211 if (rc != 0)
212 return rc;
213
214 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD);
215 if (rc != 0)
216 return rc;
217
218 return genphy_restart_aneg(phydev);
219}
220
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100221static int vsc85xx_downshift_get(struct phy_device *phydev, u8 *count)
222{
223 int rc;
224 u16 reg_val;
225
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100226 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED);
227 if (rc != 0)
Florian Fainelli4b652462016-11-22 13:55:31 -0800228 goto out;
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100229
230 reg_val = phy_read(phydev, MSCC_PHY_ACTIPHY_CNTL);
231 reg_val &= DOWNSHIFT_CNTL_MASK;
232 if (!(reg_val & DOWNSHIFT_EN))
233 *count = DOWNSHIFT_DEV_DISABLE;
234 else
235 *count = ((reg_val & ~DOWNSHIFT_EN) >> DOWNSHIFT_CNTL_POS) + 2;
236 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD);
237
Florian Fainelli4b652462016-11-22 13:55:31 -0800238out:
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100239 return rc;
240}
241
242static int vsc85xx_downshift_set(struct phy_device *phydev, u8 count)
243{
244 int rc;
245 u16 reg_val;
246
247 if (count == DOWNSHIFT_DEV_DEFAULT_COUNT) {
248 /* Default downshift count 3 (i.e. Bit3:2 = 0b01) */
249 count = ((1 << DOWNSHIFT_CNTL_POS) | DOWNSHIFT_EN);
250 } else if (count > DOWNSHIFT_COUNT_MAX || count == 1) {
251 phydev_err(phydev, "Downshift count should be 2,3,4 or 5\n");
252 return -ERANGE;
253 } else if (count) {
254 /* Downshift count is either 2,3,4 or 5 */
255 count = (((count - 2) << DOWNSHIFT_CNTL_POS) | DOWNSHIFT_EN);
256 }
257
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100258 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED);
259 if (rc != 0)
Florian Fainelli4b652462016-11-22 13:55:31 -0800260 goto out;
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100261
262 reg_val = phy_read(phydev, MSCC_PHY_ACTIPHY_CNTL);
263 reg_val &= ~(DOWNSHIFT_CNTL_MASK);
264 reg_val |= count;
265 rc = phy_write(phydev, MSCC_PHY_ACTIPHY_CNTL, reg_val);
266 if (rc != 0)
Florian Fainelli4b652462016-11-22 13:55:31 -0800267 goto out;
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100268
269 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD);
270
Florian Fainelli4b652462016-11-22 13:55:31 -0800271out:
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100272 return rc;
273}
274
Raju Lakkaraju0a55c122016-10-05 14:19:27 +0530275static int vsc85xx_wol_set(struct phy_device *phydev,
276 struct ethtool_wolinfo *wol)
277{
278 int rc;
279 u16 reg_val;
280 u8 i;
281 u16 pwd[3] = {0, 0, 0};
282 struct ethtool_wolinfo *wol_conf = wol;
283 u8 *mac_addr = phydev->attached_dev->dev_addr;
284
285 mutex_lock(&phydev->lock);
286 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2);
287 if (rc != 0)
288 goto out_unlock;
289
290 if (wol->wolopts & WAKE_MAGIC) {
291 /* Store the device address for the magic packet */
292 for (i = 0; i < ARRAY_SIZE(pwd); i++)
293 pwd[i] = mac_addr[5 - (i * 2 + 1)] << 8 |
294 mac_addr[5 - i * 2];
295 phy_write(phydev, MSCC_PHY_WOL_LOWER_MAC_ADDR, pwd[0]);
296 phy_write(phydev, MSCC_PHY_WOL_MID_MAC_ADDR, pwd[1]);
297 phy_write(phydev, MSCC_PHY_WOL_UPPER_MAC_ADDR, pwd[2]);
298 } else {
299 phy_write(phydev, MSCC_PHY_WOL_LOWER_MAC_ADDR, 0);
300 phy_write(phydev, MSCC_PHY_WOL_MID_MAC_ADDR, 0);
301 phy_write(phydev, MSCC_PHY_WOL_UPPER_MAC_ADDR, 0);
302 }
303
304 if (wol_conf->wolopts & WAKE_MAGICSECURE) {
305 for (i = 0; i < ARRAY_SIZE(pwd); i++)
306 pwd[i] = wol_conf->sopass[5 - (i * 2 + 1)] << 8 |
307 wol_conf->sopass[5 - i * 2];
308 phy_write(phydev, MSCC_PHY_WOL_LOWER_PASSWD, pwd[0]);
309 phy_write(phydev, MSCC_PHY_WOL_MID_PASSWD, pwd[1]);
310 phy_write(phydev, MSCC_PHY_WOL_UPPER_PASSWD, pwd[2]);
311 } else {
312 phy_write(phydev, MSCC_PHY_WOL_LOWER_PASSWD, 0);
313 phy_write(phydev, MSCC_PHY_WOL_MID_PASSWD, 0);
314 phy_write(phydev, MSCC_PHY_WOL_UPPER_PASSWD, 0);
315 }
316
317 reg_val = phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL);
318 if (wol_conf->wolopts & WAKE_MAGICSECURE)
319 reg_val |= SECURE_ON_ENABLE;
320 else
321 reg_val &= ~SECURE_ON_ENABLE;
322 phy_write(phydev, MSCC_PHY_WOL_MAC_CONTROL, reg_val);
323
324 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD);
325 if (rc != 0)
326 goto out_unlock;
327
328 if (wol->wolopts & WAKE_MAGIC) {
329 /* Enable the WOL interrupt */
330 reg_val = phy_read(phydev, MII_VSC85XX_INT_MASK);
331 reg_val |= MII_VSC85XX_INT_MASK_WOL;
332 rc = phy_write(phydev, MII_VSC85XX_INT_MASK, reg_val);
333 if (rc != 0)
334 goto out_unlock;
335 } else {
336 /* Disable the WOL interrupt */
337 reg_val = phy_read(phydev, MII_VSC85XX_INT_MASK);
338 reg_val &= (~MII_VSC85XX_INT_MASK_WOL);
339 rc = phy_write(phydev, MII_VSC85XX_INT_MASK, reg_val);
340 if (rc != 0)
341 goto out_unlock;
342 }
343 /* Clear WOL iterrupt status */
344 reg_val = phy_read(phydev, MII_VSC85XX_INT_STATUS);
345
346out_unlock:
347 mutex_unlock(&phydev->lock);
348
349 return rc;
350}
351
352static void vsc85xx_wol_get(struct phy_device *phydev,
353 struct ethtool_wolinfo *wol)
354{
355 int rc;
356 u16 reg_val;
357 u8 i;
358 u16 pwd[3] = {0, 0, 0};
359 struct ethtool_wolinfo *wol_conf = wol;
360
361 mutex_lock(&phydev->lock);
362 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2);
363 if (rc != 0)
364 goto out_unlock;
365
366 reg_val = phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL);
367 if (reg_val & SECURE_ON_ENABLE)
368 wol_conf->wolopts |= WAKE_MAGICSECURE;
369 if (wol_conf->wolopts & WAKE_MAGICSECURE) {
370 pwd[0] = phy_read(phydev, MSCC_PHY_WOL_LOWER_PASSWD);
371 pwd[1] = phy_read(phydev, MSCC_PHY_WOL_MID_PASSWD);
372 pwd[2] = phy_read(phydev, MSCC_PHY_WOL_UPPER_PASSWD);
373 for (i = 0; i < ARRAY_SIZE(pwd); i++) {
374 wol_conf->sopass[5 - i * 2] = pwd[i] & 0x00ff;
375 wol_conf->sopass[5 - (i * 2 + 1)] = (pwd[i] & 0xff00)
376 >> 8;
377 }
378 }
379
380 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD);
381
382out_unlock:
383 mutex_unlock(&phydev->lock);
384}
385
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200386#ifdef CONFIG_OF_MDIO
387static int vsc85xx_edge_rate_magic_get(struct phy_device *phydev)
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530388{
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530389 u8 sd;
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200390 u16 vdd;
391 int rc, i, j;
392 struct device *dev = &phydev->mdio.dev;
393 struct device_node *of_node = dev->of_node;
394 u8 sd_array_size = ARRAY_SIZE(edge_table[0].slowdown);
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530395
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200396 if (!of_node)
397 return -ENODEV;
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530398
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200399 rc = of_property_read_u16(of_node, "vsc8531,vddmac", &vdd);
400 if (rc != 0)
401 vdd = MSCC_VDDMAC_3300;
402
403 rc = of_property_read_u8(of_node, "vsc8531,edge-slowdown", &sd);
404 if (rc != 0)
405 sd = 0;
406
407 for (i = 0; i < ARRAY_SIZE(edge_table); i++)
408 if (edge_table[i].vddmac == vdd)
409 for (j = 0; j < sd_array_size; j++)
410 if (edge_table[i].slowdown[j] == sd)
411 return (sd_array_size - j - 1);
412
413 return -EINVAL;
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530414}
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +0530415
416static int vsc85xx_dt_led_mode_get(struct phy_device *phydev,
417 char *led,
418 u8 default_mode)
419{
Quentin Schulz0969aba2018-09-03 10:48:48 +0200420 struct vsc8531_private *priv = phydev->priv;
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +0530421 struct device *dev = &phydev->mdio.dev;
422 struct device_node *of_node = dev->of_node;
423 u8 led_mode;
424 int err;
425
426 if (!of_node)
427 return -ENODEV;
428
429 led_mode = default_mode;
430 err = of_property_read_u8(of_node, led, &led_mode);
Quentin Schulz0969aba2018-09-03 10:48:48 +0200431 if (!err && !(BIT(led_mode) & priv->supp_led_modes)) {
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +0530432 phydev_err(phydev, "DT %s invalid\n", led);
433 return -EINVAL;
434 }
435
436 return led_mode;
437}
438
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200439#else
440static int vsc85xx_edge_rate_magic_get(struct phy_device *phydev)
441{
442 return 0;
443}
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +0530444
445static int vsc85xx_dt_led_mode_get(struct phy_device *phydev,
446 char *led,
447 u8 default_mode)
448{
449 return default_mode;
450}
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200451#endif /* CONFIG_OF_MDIO */
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530452
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200453static int vsc85xx_dt_led_modes_get(struct phy_device *phydev, u8 *default_mode)
454{
455 struct vsc8531_private *priv = phydev->priv;
456 char led_dt_prop[19];
457 int i, ret;
458
459 for (i = 0; i < priv->nleds; i++) {
460 ret = sprintf(led_dt_prop, "vsc8531,led-%d-mode", i);
461 if (ret < 0)
462 return ret;
463
464 ret = vsc85xx_dt_led_mode_get(phydev, led_dt_prop,
465 default_mode[i]);
466 if (ret < 0)
467 return ret;
468 priv->leds_mode[i] = ret;
469 }
470
471 return 0;
472}
473
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200474static int vsc85xx_edge_rate_cntl_set(struct phy_device *phydev, u8 edge_rate)
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530475{
476 int rc;
477 u16 reg_val;
478
479 mutex_lock(&phydev->lock);
480 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2);
481 if (rc != 0)
482 goto out_unlock;
483 reg_val = phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL);
484 reg_val &= ~(EDGE_RATE_CNTL_MASK);
485 reg_val |= (edge_rate << EDGE_RATE_CNTL_POS);
486 rc = phy_write(phydev, MSCC_PHY_WOL_MAC_CONTROL, reg_val);
487 if (rc != 0)
488 goto out_unlock;
489 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD);
490
491out_unlock:
492 mutex_unlock(&phydev->lock);
493
494 return rc;
495}
496
Raju Lakkaraju1a211012016-09-19 15:33:54 +0530497static int vsc85xx_mac_if_set(struct phy_device *phydev,
498 phy_interface_t interface)
499{
500 int rc;
501 u16 reg_val;
502
503 mutex_lock(&phydev->lock);
504 reg_val = phy_read(phydev, MSCC_PHY_EXT_PHY_CNTL_1);
505 reg_val &= ~(MAC_IF_SELECTION_MASK);
506 switch (interface) {
507 case PHY_INTERFACE_MODE_RGMII:
508 reg_val |= (MAC_IF_SELECTION_RGMII << MAC_IF_SELECTION_POS);
509 break;
510 case PHY_INTERFACE_MODE_RMII:
511 reg_val |= (MAC_IF_SELECTION_RMII << MAC_IF_SELECTION_POS);
512 break;
513 case PHY_INTERFACE_MODE_MII:
514 case PHY_INTERFACE_MODE_GMII:
515 reg_val |= (MAC_IF_SELECTION_GMII << MAC_IF_SELECTION_POS);
516 break;
517 default:
518 rc = -EINVAL;
519 goto out_unlock;
520 }
521 rc = phy_write(phydev, MSCC_PHY_EXT_PHY_CNTL_1, reg_val);
522 if (rc != 0)
523 goto out_unlock;
524
525 rc = genphy_soft_reset(phydev);
526
527out_unlock:
528 mutex_unlock(&phydev->lock);
529
530 return rc;
531}
532
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530533static int vsc85xx_default_config(struct phy_device *phydev)
534{
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530535 int rc;
536 u16 reg_val;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530537
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530538 phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530539 mutex_lock(&phydev->lock);
540 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2);
541 if (rc != 0)
542 goto out_unlock;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530543
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530544 reg_val = phy_read(phydev, MSCC_PHY_RGMII_CNTL);
545 reg_val &= ~(RGMII_RX_CLK_DELAY_MASK);
546 reg_val |= (RGMII_RX_CLK_DELAY_1_1_NS << RGMII_RX_CLK_DELAY_POS);
547 phy_write(phydev, MSCC_PHY_RGMII_CNTL, reg_val);
548 rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD);
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530549
550out_unlock:
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530551 mutex_unlock(&phydev->lock);
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530552
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530553 return rc;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530554}
555
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100556static int vsc85xx_get_tunable(struct phy_device *phydev,
557 struct ethtool_tunable *tuna, void *data)
558{
559 switch (tuna->id) {
560 case ETHTOOL_PHY_DOWNSHIFT:
561 return vsc85xx_downshift_get(phydev, (u8 *)data);
562 default:
563 return -EINVAL;
564 }
565}
566
567static int vsc85xx_set_tunable(struct phy_device *phydev,
568 struct ethtool_tunable *tuna,
569 const void *data)
570{
571 switch (tuna->id) {
572 case ETHTOOL_PHY_DOWNSHIFT:
573 return vsc85xx_downshift_set(phydev, *(u8 *)data);
574 default:
575 return -EINVAL;
576 }
577}
578
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530579static int vsc85xx_config_init(struct phy_device *phydev)
580{
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200581 int rc, i;
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530582 struct vsc8531_private *vsc8531 = phydev->priv;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530583
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530584 rc = vsc85xx_default_config(phydev);
585 if (rc)
586 return rc;
Raju Lakkaraju1a211012016-09-19 15:33:54 +0530587
588 rc = vsc85xx_mac_if_set(phydev, phydev->interface);
589 if (rc)
590 return rc;
591
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200592 rc = vsc85xx_edge_rate_cntl_set(phydev, vsc8531->rate_magic);
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530593 if (rc)
594 return rc;
595
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200596 for (i = 0; i < vsc8531->nleds; i++) {
597 rc = vsc85xx_led_cntl_set(phydev, i, vsc8531->leds_mode[i]);
598 if (rc)
599 return rc;
600 }
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +0530601
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530602 rc = genphy_config_init(phydev);
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530603
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530604 return rc;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530605}
606
607static int vsc85xx_ack_interrupt(struct phy_device *phydev)
608{
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530609 int rc = 0;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530610
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530611 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
612 rc = phy_read(phydev, MII_VSC85XX_INT_STATUS);
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530613
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530614 return (rc < 0) ? rc : 0;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530615}
616
617static int vsc85xx_config_intr(struct phy_device *phydev)
618{
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530619 int rc;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530620
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530621 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
622 rc = phy_write(phydev, MII_VSC85XX_INT_MASK,
623 MII_VSC85XX_INT_MASK_MASK);
624 } else {
625 rc = phy_write(phydev, MII_VSC85XX_INT_MASK, 0);
626 if (rc < 0)
627 return rc;
628 rc = phy_read(phydev, MII_VSC85XX_INT_STATUS);
629 }
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530630
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530631 return rc;
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530632}
633
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530634static int vsc85xx_config_aneg(struct phy_device *phydev)
635{
636 int rc;
637
638 rc = vsc85xx_mdix_set(phydev, phydev->mdix_ctrl);
639 if (rc < 0)
640 return rc;
641
642 return genphy_config_aneg(phydev);
643}
644
645static int vsc85xx_read_status(struct phy_device *phydev)
646{
647 int rc;
648
649 rc = vsc85xx_mdix_get(phydev, &phydev->mdix);
650 if (rc < 0)
651 return rc;
652
653 return genphy_read_status(phydev);
654}
655
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530656static int vsc85xx_probe(struct phy_device *phydev)
657{
658 struct vsc8531_private *vsc8531;
Raju Lakkaraju04d8a0a2017-02-07 19:10:26 +0530659 int rate_magic;
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200660 u8 default_mode[2] = {VSC8531_LINK_1000_ACTIVITY,
661 VSC8531_LINK_100_ACTIVITY};
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530662
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200663 rate_magic = vsc85xx_edge_rate_magic_get(phydev);
664 if (rate_magic < 0)
665 return rate_magic;
666
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530667 vsc8531 = devm_kzalloc(&phydev->mdio.dev, sizeof(*vsc8531), GFP_KERNEL);
668 if (!vsc8531)
669 return -ENOMEM;
670
671 phydev->priv = vsc8531;
672
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200673 vsc8531->rate_magic = rate_magic;
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200674 vsc8531->nleds = 2;
Quentin Schulz0969aba2018-09-03 10:48:48 +0200675 vsc8531->supp_led_modes = VSC85XX_SUPP_LED_MODES;
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200676
Quentin Schulz11bfdab2018-09-03 10:48:47 +0200677 return vsc85xx_dt_led_modes_get(phydev, default_mode);
Raju Lakkarajua4cc96d2016-10-03 12:53:13 +0530678}
679
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530680/* Microsemi VSC85xx PHYs */
681static struct phy_driver vsc85xx_driver[] = {
682{
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200683 .phy_id = PHY_ID_VSC8530,
684 .name = "Microsemi FE VSC8530",
685 .phy_id_mask = 0xfffffff0,
686 .features = PHY_BASIC_FEATURES,
687 .flags = PHY_HAS_INTERRUPT,
688 .soft_reset = &genphy_soft_reset,
689 .config_init = &vsc85xx_config_init,
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530690 .config_aneg = &vsc85xx_config_aneg,
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200691 .aneg_done = &genphy_aneg_done,
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530692 .read_status = &vsc85xx_read_status,
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200693 .ack_interrupt = &vsc85xx_ack_interrupt,
694 .config_intr = &vsc85xx_config_intr,
695 .suspend = &genphy_suspend,
696 .resume = &genphy_resume,
697 .probe = &vsc85xx_probe,
698 .set_wol = &vsc85xx_wol_set,
699 .get_wol = &vsc85xx_wol_get,
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100700 .get_tunable = &vsc85xx_get_tunable,
701 .set_tunable = &vsc85xx_set_tunable,
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200702},
703{
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530704 .phy_id = PHY_ID_VSC8531,
705 .name = "Microsemi VSC8531",
706 .phy_id_mask = 0xfffffff0,
707 .features = PHY_GBIT_FEATURES,
708 .flags = PHY_HAS_INTERRUPT,
709 .soft_reset = &genphy_soft_reset,
710 .config_init = &vsc85xx_config_init,
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530711 .config_aneg = &vsc85xx_config_aneg,
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530712 .aneg_done = &genphy_aneg_done,
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530713 .read_status = &vsc85xx_read_status,
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530714 .ack_interrupt = &vsc85xx_ack_interrupt,
715 .config_intr = &vsc85xx_config_intr,
716 .suspend = &genphy_suspend,
717 .resume = &genphy_resume,
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200718 .probe = &vsc85xx_probe,
719 .set_wol = &vsc85xx_wol_set,
720 .get_wol = &vsc85xx_wol_get,
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100721 .get_tunable = &vsc85xx_get_tunable,
722 .set_tunable = &vsc85xx_set_tunable,
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530723},
724{
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200725 .phy_id = PHY_ID_VSC8540,
726 .name = "Microsemi FE VSC8540 SyncE",
727 .phy_id_mask = 0xfffffff0,
728 .features = PHY_BASIC_FEATURES,
729 .flags = PHY_HAS_INTERRUPT,
730 .soft_reset = &genphy_soft_reset,
731 .config_init = &vsc85xx_config_init,
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530732 .config_aneg = &vsc85xx_config_aneg,
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200733 .aneg_done = &genphy_aneg_done,
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530734 .read_status = &vsc85xx_read_status,
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200735 .ack_interrupt = &vsc85xx_ack_interrupt,
736 .config_intr = &vsc85xx_config_intr,
737 .suspend = &genphy_suspend,
738 .resume = &genphy_resume,
739 .probe = &vsc85xx_probe,
740 .set_wol = &vsc85xx_wol_set,
741 .get_wol = &vsc85xx_wol_get,
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100742 .get_tunable = &vsc85xx_get_tunable,
743 .set_tunable = &vsc85xx_set_tunable,
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200744},
745{
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530746 .phy_id = PHY_ID_VSC8541,
747 .name = "Microsemi VSC8541 SyncE",
748 .phy_id_mask = 0xfffffff0,
749 .features = PHY_GBIT_FEATURES,
750 .flags = PHY_HAS_INTERRUPT,
751 .soft_reset = &genphy_soft_reset,
752 .config_init = &vsc85xx_config_init,
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530753 .config_aneg = &vsc85xx_config_aneg,
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530754 .aneg_done = &genphy_aneg_done,
Raju Lakkaraju233275e2016-11-29 15:16:48 +0530755 .read_status = &vsc85xx_read_status,
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530756 .ack_interrupt = &vsc85xx_ack_interrupt,
757 .config_intr = &vsc85xx_config_intr,
758 .suspend = &genphy_suspend,
759 .resume = &genphy_resume,
Allan W. Nielsen4f58e6d2016-10-12 15:47:51 +0200760 .probe = &vsc85xx_probe,
761 .set_wol = &vsc85xx_wol_set,
762 .get_wol = &vsc85xx_wol_get,
Raju Lakkaraju310d9ad2016-11-17 13:07:24 +0100763 .get_tunable = &vsc85xx_get_tunable,
764 .set_tunable = &vsc85xx_set_tunable,
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530765}
766
767};
768
769module_phy_driver(vsc85xx_driver);
770
771static struct mdio_device_id __maybe_unused vsc85xx_tbl[] = {
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200772 { PHY_ID_VSC8530, 0xfffffff0, },
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530773 { PHY_ID_VSC8531, 0xfffffff0, },
Raju Lakkarajuaf1fee92016-10-28 12:10:11 +0200774 { PHY_ID_VSC8540, 0xfffffff0, },
Raju Lakkaraju4ffd03f2016-09-08 14:09:31 +0530775 { PHY_ID_VSC8541, 0xfffffff0, },
776 { }
Raju Lakkarajud50736a2016-08-05 17:54:21 +0530777};
778
779MODULE_DEVICE_TABLE(mdio, vsc85xx_tbl);
780
781MODULE_DESCRIPTION("Microsemi VSC85xx PHY driver");
782MODULE_AUTHOR("Nagaraju Lakkaraju");
783MODULE_LICENSE("Dual MIT/GPL");