Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 1 | /* |
| 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 Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 14 | #include <linux/of.h> |
Raju Lakkaraju | 0a55c12 | 2016-10-05 14:19:27 +0530 | [diff] [blame] | 15 | #include <linux/netdevice.h> |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 16 | |
| 17 | enum rgmii_rx_clock_delay { |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 18 | RGMII_RX_CLK_DELAY_0_2_NS = 0, |
| 19 | RGMII_RX_CLK_DELAY_0_8_NS = 1, |
| 20 | RGMII_RX_CLK_DELAY_1_1_NS = 2, |
| 21 | RGMII_RX_CLK_DELAY_1_7_NS = 3, |
| 22 | RGMII_RX_CLK_DELAY_2_0_NS = 4, |
| 23 | RGMII_RX_CLK_DELAY_2_3_NS = 5, |
| 24 | RGMII_RX_CLK_DELAY_2_6_NS = 6, |
| 25 | RGMII_RX_CLK_DELAY_3_4_NS = 7 |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 26 | }; |
| 27 | |
Raju Lakkaraju | 1a21101 | 2016-09-19 15:33:54 +0530 | [diff] [blame] | 28 | /* Microsemi VSC85xx PHY registers */ |
| 29 | /* IEEE 802. Std Registers */ |
| 30 | #define MSCC_PHY_EXT_PHY_CNTL_1 23 |
| 31 | #define MAC_IF_SELECTION_MASK 0x1800 |
| 32 | #define MAC_IF_SELECTION_GMII 0 |
| 33 | #define MAC_IF_SELECTION_RMII 1 |
| 34 | #define MAC_IF_SELECTION_RGMII 2 |
| 35 | #define MAC_IF_SELECTION_POS 11 |
| 36 | #define FAR_END_LOOPBACK_MODE_MASK 0x0008 |
| 37 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 38 | #define MII_VSC85XX_INT_MASK 25 |
| 39 | #define MII_VSC85XX_INT_MASK_MASK 0xa000 |
Raju Lakkaraju | 0a55c12 | 2016-10-05 14:19:27 +0530 | [diff] [blame] | 40 | #define MII_VSC85XX_INT_MASK_WOL 0x0040 |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 41 | #define MII_VSC85XX_INT_STATUS 26 |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 42 | |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 43 | #define MSCC_PHY_WOL_MAC_CONTROL 27 |
| 44 | #define EDGE_RATE_CNTL_POS 5 |
| 45 | #define EDGE_RATE_CNTL_MASK 0x00E0 |
| 46 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 47 | #define MSCC_EXT_PAGE_ACCESS 31 |
| 48 | #define MSCC_PHY_PAGE_STANDARD 0x0000 /* Standard registers */ |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 49 | #define MSCC_PHY_PAGE_EXTENDED 0x0001 /* Extended registers */ |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 50 | #define MSCC_PHY_PAGE_EXTENDED_2 0x0002 /* Extended reg - page 2 */ |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 51 | |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 52 | /* Extended Page 1 Registers */ |
| 53 | #define MSCC_PHY_ACTIPHY_CNTL 20 |
| 54 | #define DOWNSHIFT_CNTL_MASK 0x001C |
| 55 | #define DOWNSHIFT_EN 0x0010 |
| 56 | #define DOWNSHIFT_CNTL_POS 2 |
| 57 | |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 58 | /* Extended Page 2 Registers */ |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 59 | #define MSCC_PHY_RGMII_CNTL 20 |
| 60 | #define RGMII_RX_CLK_DELAY_MASK 0x0070 |
| 61 | #define RGMII_RX_CLK_DELAY_POS 4 |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 62 | |
Raju Lakkaraju | 0a55c12 | 2016-10-05 14:19:27 +0530 | [diff] [blame] | 63 | #define MSCC_PHY_WOL_LOWER_MAC_ADDR 21 |
| 64 | #define MSCC_PHY_WOL_MID_MAC_ADDR 22 |
| 65 | #define MSCC_PHY_WOL_UPPER_MAC_ADDR 23 |
| 66 | #define MSCC_PHY_WOL_LOWER_PASSWD 24 |
| 67 | #define MSCC_PHY_WOL_MID_PASSWD 25 |
| 68 | #define MSCC_PHY_WOL_UPPER_PASSWD 26 |
| 69 | |
| 70 | #define MSCC_PHY_WOL_MAC_CONTROL 27 |
| 71 | #define SECURE_ON_ENABLE 0x8000 |
| 72 | #define SECURE_ON_PASSWD_LEN_4 0x4000 |
| 73 | |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 74 | /* Microsemi PHY ID's */ |
Raju Lakkaraju | af1fee9 | 2016-10-28 12:10:11 +0200 | [diff] [blame] | 75 | #define PHY_ID_VSC8530 0x00070560 |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 76 | #define PHY_ID_VSC8531 0x00070570 |
Raju Lakkaraju | af1fee9 | 2016-10-28 12:10:11 +0200 | [diff] [blame] | 77 | #define PHY_ID_VSC8540 0x00070760 |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 78 | #define PHY_ID_VSC8541 0x00070770 |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 79 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 80 | #define MSCC_VDDMAC_1500 1500 |
| 81 | #define MSCC_VDDMAC_1800 1800 |
| 82 | #define MSCC_VDDMAC_2500 2500 |
| 83 | #define MSCC_VDDMAC_3300 3300 |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 84 | |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 85 | #define DOWNSHIFT_COUNT_MAX 5 |
| 86 | |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 87 | struct vsc8531_private { |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 88 | int rate_magic; |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 89 | }; |
| 90 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 91 | #ifdef CONFIG_OF_MDIO |
| 92 | struct vsc8531_edge_rate_table { |
| 93 | u16 vddmac; |
| 94 | u8 slowdown[8]; |
| 95 | }; |
| 96 | |
| 97 | static const struct vsc8531_edge_rate_table edge_table[] = { |
| 98 | {MSCC_VDDMAC_3300, { 0, 2, 4, 7, 10, 17, 29, 53} }, |
| 99 | {MSCC_VDDMAC_2500, { 0, 3, 6, 10, 14, 23, 37, 63} }, |
| 100 | {MSCC_VDDMAC_1800, { 0, 5, 9, 16, 23, 35, 52, 76} }, |
| 101 | {MSCC_VDDMAC_1500, { 0, 6, 14, 21, 29, 42, 58, 77} }, |
| 102 | }; |
| 103 | #endif /* CONFIG_OF_MDIO */ |
| 104 | |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 105 | static int vsc85xx_phy_page_set(struct phy_device *phydev, u8 page) |
| 106 | { |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 107 | int rc; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 108 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 109 | rc = phy_write(phydev, MSCC_EXT_PAGE_ACCESS, page); |
| 110 | return rc; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 111 | } |
| 112 | |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 113 | static int vsc85xx_downshift_get(struct phy_device *phydev, u8 *count) |
| 114 | { |
| 115 | int rc; |
| 116 | u16 reg_val; |
| 117 | |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 118 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED); |
| 119 | if (rc != 0) |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame^] | 120 | goto out; |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 121 | |
| 122 | reg_val = phy_read(phydev, MSCC_PHY_ACTIPHY_CNTL); |
| 123 | reg_val &= DOWNSHIFT_CNTL_MASK; |
| 124 | if (!(reg_val & DOWNSHIFT_EN)) |
| 125 | *count = DOWNSHIFT_DEV_DISABLE; |
| 126 | else |
| 127 | *count = ((reg_val & ~DOWNSHIFT_EN) >> DOWNSHIFT_CNTL_POS) + 2; |
| 128 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD); |
| 129 | |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame^] | 130 | out: |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 131 | return rc; |
| 132 | } |
| 133 | |
| 134 | static int vsc85xx_downshift_set(struct phy_device *phydev, u8 count) |
| 135 | { |
| 136 | int rc; |
| 137 | u16 reg_val; |
| 138 | |
| 139 | if (count == DOWNSHIFT_DEV_DEFAULT_COUNT) { |
| 140 | /* Default downshift count 3 (i.e. Bit3:2 = 0b01) */ |
| 141 | count = ((1 << DOWNSHIFT_CNTL_POS) | DOWNSHIFT_EN); |
| 142 | } else if (count > DOWNSHIFT_COUNT_MAX || count == 1) { |
| 143 | phydev_err(phydev, "Downshift count should be 2,3,4 or 5\n"); |
| 144 | return -ERANGE; |
| 145 | } else if (count) { |
| 146 | /* Downshift count is either 2,3,4 or 5 */ |
| 147 | count = (((count - 2) << DOWNSHIFT_CNTL_POS) | DOWNSHIFT_EN); |
| 148 | } |
| 149 | |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 150 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED); |
| 151 | if (rc != 0) |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame^] | 152 | goto out; |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 153 | |
| 154 | reg_val = phy_read(phydev, MSCC_PHY_ACTIPHY_CNTL); |
| 155 | reg_val &= ~(DOWNSHIFT_CNTL_MASK); |
| 156 | reg_val |= count; |
| 157 | rc = phy_write(phydev, MSCC_PHY_ACTIPHY_CNTL, reg_val); |
| 158 | if (rc != 0) |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame^] | 159 | goto out; |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 160 | |
| 161 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD); |
| 162 | |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame^] | 163 | out: |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 164 | return rc; |
| 165 | } |
| 166 | |
Raju Lakkaraju | 0a55c12 | 2016-10-05 14:19:27 +0530 | [diff] [blame] | 167 | static int vsc85xx_wol_set(struct phy_device *phydev, |
| 168 | struct ethtool_wolinfo *wol) |
| 169 | { |
| 170 | int rc; |
| 171 | u16 reg_val; |
| 172 | u8 i; |
| 173 | u16 pwd[3] = {0, 0, 0}; |
| 174 | struct ethtool_wolinfo *wol_conf = wol; |
| 175 | u8 *mac_addr = phydev->attached_dev->dev_addr; |
| 176 | |
| 177 | mutex_lock(&phydev->lock); |
| 178 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2); |
| 179 | if (rc != 0) |
| 180 | goto out_unlock; |
| 181 | |
| 182 | if (wol->wolopts & WAKE_MAGIC) { |
| 183 | /* Store the device address for the magic packet */ |
| 184 | for (i = 0; i < ARRAY_SIZE(pwd); i++) |
| 185 | pwd[i] = mac_addr[5 - (i * 2 + 1)] << 8 | |
| 186 | mac_addr[5 - i * 2]; |
| 187 | phy_write(phydev, MSCC_PHY_WOL_LOWER_MAC_ADDR, pwd[0]); |
| 188 | phy_write(phydev, MSCC_PHY_WOL_MID_MAC_ADDR, pwd[1]); |
| 189 | phy_write(phydev, MSCC_PHY_WOL_UPPER_MAC_ADDR, pwd[2]); |
| 190 | } else { |
| 191 | phy_write(phydev, MSCC_PHY_WOL_LOWER_MAC_ADDR, 0); |
| 192 | phy_write(phydev, MSCC_PHY_WOL_MID_MAC_ADDR, 0); |
| 193 | phy_write(phydev, MSCC_PHY_WOL_UPPER_MAC_ADDR, 0); |
| 194 | } |
| 195 | |
| 196 | if (wol_conf->wolopts & WAKE_MAGICSECURE) { |
| 197 | for (i = 0; i < ARRAY_SIZE(pwd); i++) |
| 198 | pwd[i] = wol_conf->sopass[5 - (i * 2 + 1)] << 8 | |
| 199 | wol_conf->sopass[5 - i * 2]; |
| 200 | phy_write(phydev, MSCC_PHY_WOL_LOWER_PASSWD, pwd[0]); |
| 201 | phy_write(phydev, MSCC_PHY_WOL_MID_PASSWD, pwd[1]); |
| 202 | phy_write(phydev, MSCC_PHY_WOL_UPPER_PASSWD, pwd[2]); |
| 203 | } else { |
| 204 | phy_write(phydev, MSCC_PHY_WOL_LOWER_PASSWD, 0); |
| 205 | phy_write(phydev, MSCC_PHY_WOL_MID_PASSWD, 0); |
| 206 | phy_write(phydev, MSCC_PHY_WOL_UPPER_PASSWD, 0); |
| 207 | } |
| 208 | |
| 209 | reg_val = phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL); |
| 210 | if (wol_conf->wolopts & WAKE_MAGICSECURE) |
| 211 | reg_val |= SECURE_ON_ENABLE; |
| 212 | else |
| 213 | reg_val &= ~SECURE_ON_ENABLE; |
| 214 | phy_write(phydev, MSCC_PHY_WOL_MAC_CONTROL, reg_val); |
| 215 | |
| 216 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD); |
| 217 | if (rc != 0) |
| 218 | goto out_unlock; |
| 219 | |
| 220 | if (wol->wolopts & WAKE_MAGIC) { |
| 221 | /* Enable the WOL interrupt */ |
| 222 | reg_val = phy_read(phydev, MII_VSC85XX_INT_MASK); |
| 223 | reg_val |= MII_VSC85XX_INT_MASK_WOL; |
| 224 | rc = phy_write(phydev, MII_VSC85XX_INT_MASK, reg_val); |
| 225 | if (rc != 0) |
| 226 | goto out_unlock; |
| 227 | } else { |
| 228 | /* Disable the WOL interrupt */ |
| 229 | reg_val = phy_read(phydev, MII_VSC85XX_INT_MASK); |
| 230 | reg_val &= (~MII_VSC85XX_INT_MASK_WOL); |
| 231 | rc = phy_write(phydev, MII_VSC85XX_INT_MASK, reg_val); |
| 232 | if (rc != 0) |
| 233 | goto out_unlock; |
| 234 | } |
| 235 | /* Clear WOL iterrupt status */ |
| 236 | reg_val = phy_read(phydev, MII_VSC85XX_INT_STATUS); |
| 237 | |
| 238 | out_unlock: |
| 239 | mutex_unlock(&phydev->lock); |
| 240 | |
| 241 | return rc; |
| 242 | } |
| 243 | |
| 244 | static void vsc85xx_wol_get(struct phy_device *phydev, |
| 245 | struct ethtool_wolinfo *wol) |
| 246 | { |
| 247 | int rc; |
| 248 | u16 reg_val; |
| 249 | u8 i; |
| 250 | u16 pwd[3] = {0, 0, 0}; |
| 251 | struct ethtool_wolinfo *wol_conf = wol; |
| 252 | |
| 253 | mutex_lock(&phydev->lock); |
| 254 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2); |
| 255 | if (rc != 0) |
| 256 | goto out_unlock; |
| 257 | |
| 258 | reg_val = phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL); |
| 259 | if (reg_val & SECURE_ON_ENABLE) |
| 260 | wol_conf->wolopts |= WAKE_MAGICSECURE; |
| 261 | if (wol_conf->wolopts & WAKE_MAGICSECURE) { |
| 262 | pwd[0] = phy_read(phydev, MSCC_PHY_WOL_LOWER_PASSWD); |
| 263 | pwd[1] = phy_read(phydev, MSCC_PHY_WOL_MID_PASSWD); |
| 264 | pwd[2] = phy_read(phydev, MSCC_PHY_WOL_UPPER_PASSWD); |
| 265 | for (i = 0; i < ARRAY_SIZE(pwd); i++) { |
| 266 | wol_conf->sopass[5 - i * 2] = pwd[i] & 0x00ff; |
| 267 | wol_conf->sopass[5 - (i * 2 + 1)] = (pwd[i] & 0xff00) |
| 268 | >> 8; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD); |
| 273 | |
| 274 | out_unlock: |
| 275 | mutex_unlock(&phydev->lock); |
| 276 | } |
| 277 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 278 | #ifdef CONFIG_OF_MDIO |
| 279 | static int vsc85xx_edge_rate_magic_get(struct phy_device *phydev) |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 280 | { |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 281 | u8 sd; |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 282 | u16 vdd; |
| 283 | int rc, i, j; |
| 284 | struct device *dev = &phydev->mdio.dev; |
| 285 | struct device_node *of_node = dev->of_node; |
| 286 | u8 sd_array_size = ARRAY_SIZE(edge_table[0].slowdown); |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 287 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 288 | if (!of_node) |
| 289 | return -ENODEV; |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 290 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 291 | rc = of_property_read_u16(of_node, "vsc8531,vddmac", &vdd); |
| 292 | if (rc != 0) |
| 293 | vdd = MSCC_VDDMAC_3300; |
| 294 | |
| 295 | rc = of_property_read_u8(of_node, "vsc8531,edge-slowdown", &sd); |
| 296 | if (rc != 0) |
| 297 | sd = 0; |
| 298 | |
| 299 | for (i = 0; i < ARRAY_SIZE(edge_table); i++) |
| 300 | if (edge_table[i].vddmac == vdd) |
| 301 | for (j = 0; j < sd_array_size; j++) |
| 302 | if (edge_table[i].slowdown[j] == sd) |
| 303 | return (sd_array_size - j - 1); |
| 304 | |
| 305 | return -EINVAL; |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 306 | } |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 307 | #else |
| 308 | static int vsc85xx_edge_rate_magic_get(struct phy_device *phydev) |
| 309 | { |
| 310 | return 0; |
| 311 | } |
| 312 | #endif /* CONFIG_OF_MDIO */ |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 313 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 314 | static int vsc85xx_edge_rate_cntl_set(struct phy_device *phydev, u8 edge_rate) |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 315 | { |
| 316 | int rc; |
| 317 | u16 reg_val; |
| 318 | |
| 319 | mutex_lock(&phydev->lock); |
| 320 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2); |
| 321 | if (rc != 0) |
| 322 | goto out_unlock; |
| 323 | reg_val = phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL); |
| 324 | reg_val &= ~(EDGE_RATE_CNTL_MASK); |
| 325 | reg_val |= (edge_rate << EDGE_RATE_CNTL_POS); |
| 326 | rc = phy_write(phydev, MSCC_PHY_WOL_MAC_CONTROL, reg_val); |
| 327 | if (rc != 0) |
| 328 | goto out_unlock; |
| 329 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD); |
| 330 | |
| 331 | out_unlock: |
| 332 | mutex_unlock(&phydev->lock); |
| 333 | |
| 334 | return rc; |
| 335 | } |
| 336 | |
Raju Lakkaraju | 1a21101 | 2016-09-19 15:33:54 +0530 | [diff] [blame] | 337 | static int vsc85xx_mac_if_set(struct phy_device *phydev, |
| 338 | phy_interface_t interface) |
| 339 | { |
| 340 | int rc; |
| 341 | u16 reg_val; |
| 342 | |
| 343 | mutex_lock(&phydev->lock); |
| 344 | reg_val = phy_read(phydev, MSCC_PHY_EXT_PHY_CNTL_1); |
| 345 | reg_val &= ~(MAC_IF_SELECTION_MASK); |
| 346 | switch (interface) { |
| 347 | case PHY_INTERFACE_MODE_RGMII: |
| 348 | reg_val |= (MAC_IF_SELECTION_RGMII << MAC_IF_SELECTION_POS); |
| 349 | break; |
| 350 | case PHY_INTERFACE_MODE_RMII: |
| 351 | reg_val |= (MAC_IF_SELECTION_RMII << MAC_IF_SELECTION_POS); |
| 352 | break; |
| 353 | case PHY_INTERFACE_MODE_MII: |
| 354 | case PHY_INTERFACE_MODE_GMII: |
| 355 | reg_val |= (MAC_IF_SELECTION_GMII << MAC_IF_SELECTION_POS); |
| 356 | break; |
| 357 | default: |
| 358 | rc = -EINVAL; |
| 359 | goto out_unlock; |
| 360 | } |
| 361 | rc = phy_write(phydev, MSCC_PHY_EXT_PHY_CNTL_1, reg_val); |
| 362 | if (rc != 0) |
| 363 | goto out_unlock; |
| 364 | |
| 365 | rc = genphy_soft_reset(phydev); |
| 366 | |
| 367 | out_unlock: |
| 368 | mutex_unlock(&phydev->lock); |
| 369 | |
| 370 | return rc; |
| 371 | } |
| 372 | |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 373 | static int vsc85xx_default_config(struct phy_device *phydev) |
| 374 | { |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 375 | int rc; |
| 376 | u16 reg_val; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 377 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 378 | mutex_lock(&phydev->lock); |
| 379 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_EXTENDED_2); |
| 380 | if (rc != 0) |
| 381 | goto out_unlock; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 382 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 383 | reg_val = phy_read(phydev, MSCC_PHY_RGMII_CNTL); |
| 384 | reg_val &= ~(RGMII_RX_CLK_DELAY_MASK); |
| 385 | reg_val |= (RGMII_RX_CLK_DELAY_1_1_NS << RGMII_RX_CLK_DELAY_POS); |
| 386 | phy_write(phydev, MSCC_PHY_RGMII_CNTL, reg_val); |
| 387 | rc = vsc85xx_phy_page_set(phydev, MSCC_PHY_PAGE_STANDARD); |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 388 | |
| 389 | out_unlock: |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 390 | mutex_unlock(&phydev->lock); |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 391 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 392 | return rc; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 393 | } |
| 394 | |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 395 | static int vsc85xx_get_tunable(struct phy_device *phydev, |
| 396 | struct ethtool_tunable *tuna, void *data) |
| 397 | { |
| 398 | switch (tuna->id) { |
| 399 | case ETHTOOL_PHY_DOWNSHIFT: |
| 400 | return vsc85xx_downshift_get(phydev, (u8 *)data); |
| 401 | default: |
| 402 | return -EINVAL; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static int vsc85xx_set_tunable(struct phy_device *phydev, |
| 407 | struct ethtool_tunable *tuna, |
| 408 | const void *data) |
| 409 | { |
| 410 | switch (tuna->id) { |
| 411 | case ETHTOOL_PHY_DOWNSHIFT: |
| 412 | return vsc85xx_downshift_set(phydev, *(u8 *)data); |
| 413 | default: |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | } |
| 417 | |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 418 | static int vsc85xx_config_init(struct phy_device *phydev) |
| 419 | { |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 420 | int rc; |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 421 | struct vsc8531_private *vsc8531 = phydev->priv; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 422 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 423 | rc = vsc85xx_default_config(phydev); |
| 424 | if (rc) |
| 425 | return rc; |
Raju Lakkaraju | 1a21101 | 2016-09-19 15:33:54 +0530 | [diff] [blame] | 426 | |
| 427 | rc = vsc85xx_mac_if_set(phydev, phydev->interface); |
| 428 | if (rc) |
| 429 | return rc; |
| 430 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 431 | rc = vsc85xx_edge_rate_cntl_set(phydev, vsc8531->rate_magic); |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 432 | if (rc) |
| 433 | return rc; |
| 434 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 435 | rc = genphy_config_init(phydev); |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 436 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 437 | return rc; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static int vsc85xx_ack_interrupt(struct phy_device *phydev) |
| 441 | { |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 442 | int rc = 0; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 443 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 444 | if (phydev->interrupts == PHY_INTERRUPT_ENABLED) |
| 445 | rc = phy_read(phydev, MII_VSC85XX_INT_STATUS); |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 446 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 447 | return (rc < 0) ? rc : 0; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static int vsc85xx_config_intr(struct phy_device *phydev) |
| 451 | { |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 452 | int rc; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 453 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 454 | if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { |
| 455 | rc = phy_write(phydev, MII_VSC85XX_INT_MASK, |
| 456 | MII_VSC85XX_INT_MASK_MASK); |
| 457 | } else { |
| 458 | rc = phy_write(phydev, MII_VSC85XX_INT_MASK, 0); |
| 459 | if (rc < 0) |
| 460 | return rc; |
| 461 | rc = phy_read(phydev, MII_VSC85XX_INT_STATUS); |
| 462 | } |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 463 | |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 464 | return rc; |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 465 | } |
| 466 | |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 467 | static int vsc85xx_probe(struct phy_device *phydev) |
| 468 | { |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 469 | int rate_magic; |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 470 | struct vsc8531_private *vsc8531; |
| 471 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 472 | rate_magic = vsc85xx_edge_rate_magic_get(phydev); |
| 473 | if (rate_magic < 0) |
| 474 | return rate_magic; |
| 475 | |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 476 | vsc8531 = devm_kzalloc(&phydev->mdio.dev, sizeof(*vsc8531), GFP_KERNEL); |
| 477 | if (!vsc8531) |
| 478 | return -ENOMEM; |
| 479 | |
| 480 | phydev->priv = vsc8531; |
| 481 | |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 482 | vsc8531->rate_magic = rate_magic; |
| 483 | |
Raju Lakkaraju | a4cc96d | 2016-10-03 12:53:13 +0530 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 487 | /* Microsemi VSC85xx PHYs */ |
| 488 | static struct phy_driver vsc85xx_driver[] = { |
| 489 | { |
Raju Lakkaraju | af1fee9 | 2016-10-28 12:10:11 +0200 | [diff] [blame] | 490 | .phy_id = PHY_ID_VSC8530, |
| 491 | .name = "Microsemi FE VSC8530", |
| 492 | .phy_id_mask = 0xfffffff0, |
| 493 | .features = PHY_BASIC_FEATURES, |
| 494 | .flags = PHY_HAS_INTERRUPT, |
| 495 | .soft_reset = &genphy_soft_reset, |
| 496 | .config_init = &vsc85xx_config_init, |
| 497 | .config_aneg = &genphy_config_aneg, |
| 498 | .aneg_done = &genphy_aneg_done, |
| 499 | .read_status = &genphy_read_status, |
| 500 | .ack_interrupt = &vsc85xx_ack_interrupt, |
| 501 | .config_intr = &vsc85xx_config_intr, |
| 502 | .suspend = &genphy_suspend, |
| 503 | .resume = &genphy_resume, |
| 504 | .probe = &vsc85xx_probe, |
| 505 | .set_wol = &vsc85xx_wol_set, |
| 506 | .get_wol = &vsc85xx_wol_get, |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 507 | .get_tunable = &vsc85xx_get_tunable, |
| 508 | .set_tunable = &vsc85xx_set_tunable, |
Raju Lakkaraju | af1fee9 | 2016-10-28 12:10:11 +0200 | [diff] [blame] | 509 | }, |
| 510 | { |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 511 | .phy_id = PHY_ID_VSC8531, |
| 512 | .name = "Microsemi VSC8531", |
| 513 | .phy_id_mask = 0xfffffff0, |
| 514 | .features = PHY_GBIT_FEATURES, |
| 515 | .flags = PHY_HAS_INTERRUPT, |
| 516 | .soft_reset = &genphy_soft_reset, |
| 517 | .config_init = &vsc85xx_config_init, |
| 518 | .config_aneg = &genphy_config_aneg, |
| 519 | .aneg_done = &genphy_aneg_done, |
| 520 | .read_status = &genphy_read_status, |
| 521 | .ack_interrupt = &vsc85xx_ack_interrupt, |
| 522 | .config_intr = &vsc85xx_config_intr, |
| 523 | .suspend = &genphy_suspend, |
| 524 | .resume = &genphy_resume, |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 525 | .probe = &vsc85xx_probe, |
| 526 | .set_wol = &vsc85xx_wol_set, |
| 527 | .get_wol = &vsc85xx_wol_get, |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 528 | .get_tunable = &vsc85xx_get_tunable, |
| 529 | .set_tunable = &vsc85xx_set_tunable, |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 530 | }, |
| 531 | { |
Raju Lakkaraju | af1fee9 | 2016-10-28 12:10:11 +0200 | [diff] [blame] | 532 | .phy_id = PHY_ID_VSC8540, |
| 533 | .name = "Microsemi FE VSC8540 SyncE", |
| 534 | .phy_id_mask = 0xfffffff0, |
| 535 | .features = PHY_BASIC_FEATURES, |
| 536 | .flags = PHY_HAS_INTERRUPT, |
| 537 | .soft_reset = &genphy_soft_reset, |
| 538 | .config_init = &vsc85xx_config_init, |
| 539 | .config_aneg = &genphy_config_aneg, |
| 540 | .aneg_done = &genphy_aneg_done, |
| 541 | .read_status = &genphy_read_status, |
| 542 | .ack_interrupt = &vsc85xx_ack_interrupt, |
| 543 | .config_intr = &vsc85xx_config_intr, |
| 544 | .suspend = &genphy_suspend, |
| 545 | .resume = &genphy_resume, |
| 546 | .probe = &vsc85xx_probe, |
| 547 | .set_wol = &vsc85xx_wol_set, |
| 548 | .get_wol = &vsc85xx_wol_get, |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 549 | .get_tunable = &vsc85xx_get_tunable, |
| 550 | .set_tunable = &vsc85xx_set_tunable, |
Raju Lakkaraju | af1fee9 | 2016-10-28 12:10:11 +0200 | [diff] [blame] | 551 | }, |
| 552 | { |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 553 | .phy_id = PHY_ID_VSC8541, |
| 554 | .name = "Microsemi VSC8541 SyncE", |
| 555 | .phy_id_mask = 0xfffffff0, |
| 556 | .features = PHY_GBIT_FEATURES, |
| 557 | .flags = PHY_HAS_INTERRUPT, |
| 558 | .soft_reset = &genphy_soft_reset, |
| 559 | .config_init = &vsc85xx_config_init, |
| 560 | .config_aneg = &genphy_config_aneg, |
| 561 | .aneg_done = &genphy_aneg_done, |
| 562 | .read_status = &genphy_read_status, |
| 563 | .ack_interrupt = &vsc85xx_ack_interrupt, |
| 564 | .config_intr = &vsc85xx_config_intr, |
| 565 | .suspend = &genphy_suspend, |
| 566 | .resume = &genphy_resume, |
Allan W. Nielsen | 4f58e6d | 2016-10-12 15:47:51 +0200 | [diff] [blame] | 567 | .probe = &vsc85xx_probe, |
| 568 | .set_wol = &vsc85xx_wol_set, |
| 569 | .get_wol = &vsc85xx_wol_get, |
Raju Lakkaraju | 310d9ad | 2016-11-17 13:07:24 +0100 | [diff] [blame] | 570 | .get_tunable = &vsc85xx_get_tunable, |
| 571 | .set_tunable = &vsc85xx_set_tunable, |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | }; |
| 575 | |
| 576 | module_phy_driver(vsc85xx_driver); |
| 577 | |
| 578 | static struct mdio_device_id __maybe_unused vsc85xx_tbl[] = { |
Raju Lakkaraju | af1fee9 | 2016-10-28 12:10:11 +0200 | [diff] [blame] | 579 | { PHY_ID_VSC8530, 0xfffffff0, }, |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 580 | { PHY_ID_VSC8531, 0xfffffff0, }, |
Raju Lakkaraju | af1fee9 | 2016-10-28 12:10:11 +0200 | [diff] [blame] | 581 | { PHY_ID_VSC8540, 0xfffffff0, }, |
Raju Lakkaraju | 4ffd03f | 2016-09-08 14:09:31 +0530 | [diff] [blame] | 582 | { PHY_ID_VSC8541, 0xfffffff0, }, |
| 583 | { } |
Raju Lakkaraju | d50736a | 2016-08-05 17:54:21 +0530 | [diff] [blame] | 584 | }; |
| 585 | |
| 586 | MODULE_DEVICE_TABLE(mdio, vsc85xx_tbl); |
| 587 | |
| 588 | MODULE_DESCRIPTION("Microsemi VSC85xx PHY driver"); |
| 589 | MODULE_AUTHOR("Nagaraju Lakkaraju"); |
| 590 | MODULE_LICENSE("Dual MIT/GPL"); |