Andrew Lunn | 5f85757 | 2019-01-21 19:10:19 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dan Murphy | 753c66e | 2020-09-03 14:51:12 -0500 | [diff] [blame] | 2 | /* Driver for the Texas Instruments DP83867 PHY |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2015 Texas Instruments Inc. |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/ethtool.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/mii.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/phy.h> |
Max Uvarov | 72a7d45 | 2019-02-25 12:15:10 +0300 | [diff] [blame] | 13 | #include <linux/delay.h> |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/etherdevice.h> |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 16 | #include <linux/bitfield.h> |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 17 | |
| 18 | #include <dt-bindings/net/ti-dp83867.h> |
| 19 | |
| 20 | #define DP83867_PHY_ID 0x2000a231 |
| 21 | #define DP83867_DEVADDR 0x1f |
| 22 | |
| 23 | #define MII_DP83867_PHYCTRL 0x10 |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 24 | #define MII_DP83867_PHYSTS 0x11 |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 25 | #define MII_DP83867_MICR 0x12 |
| 26 | #define MII_DP83867_ISR 0x13 |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 27 | #define DP83867_CFG2 0x14 |
Grygorii Strashko | 5ca7d1c | 2017-01-05 14:48:07 -0600 | [diff] [blame] | 28 | #define DP83867_CFG3 0x1e |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 29 | #define DP83867_CTRL 0x1f |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 30 | |
| 31 | /* Extended Registers */ |
Grygorii Strashko | 749f6f6 | 2020-03-17 20:04:54 +0200 | [diff] [blame] | 32 | #define DP83867_FLD_THR_CFG 0x002e |
| 33 | #define DP83867_CFG4 0x0031 |
Max Uvarov | 1a97a47 | 2019-05-28 13:00:50 +0300 | [diff] [blame] | 34 | #define DP83867_CFG4_SGMII_ANEG_MASK (BIT(5) | BIT(6)) |
| 35 | #define DP83867_CFG4_SGMII_ANEG_TIMER_11MS (3 << 5) |
| 36 | #define DP83867_CFG4_SGMII_ANEG_TIMER_800US (2 << 5) |
| 37 | #define DP83867_CFG4_SGMII_ANEG_TIMER_2US (1 << 5) |
| 38 | #define DP83867_CFG4_SGMII_ANEG_TIMER_16MS (0 << 5) |
| 39 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 40 | #define DP83867_RGMIICTL 0x0032 |
Lukasz Majewski | ac6e058 | 2017-02-07 06:20:24 +0100 | [diff] [blame] | 41 | #define DP83867_STRAP_STS1 0x006E |
Trent Piepho | c11669a | 2019-05-22 18:43:23 +0000 | [diff] [blame] | 42 | #define DP83867_STRAP_STS2 0x006f |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 43 | #define DP83867_RGMIIDCTL 0x0086 |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 44 | #define DP83867_RXFCFG 0x0134 |
| 45 | #define DP83867_RXFPMD1 0x0136 |
| 46 | #define DP83867_RXFPMD2 0x0137 |
| 47 | #define DP83867_RXFPMD3 0x0138 |
| 48 | #define DP83867_RXFSOP1 0x0139 |
| 49 | #define DP83867_RXFSOP2 0x013A |
| 50 | #define DP83867_RXFSOP3 0x013B |
Mugunthan V N | ed838fe | 2016-10-18 16:50:18 +0530 | [diff] [blame] | 51 | #define DP83867_IO_MUX_CFG 0x0170 |
Vitaly Gaiduk | 507ddd5 | 2019-09-09 20:19:24 +0300 | [diff] [blame] | 52 | #define DP83867_SGMIICTL 0x00D3 |
Max Uvarov | 333061b | 2019-05-28 13:00:49 +0300 | [diff] [blame] | 53 | #define DP83867_10M_SGMII_CFG 0x016F |
| 54 | #define DP83867_10M_SGMII_RATE_ADAPT_MASK BIT(7) |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 55 | |
| 56 | #define DP83867_SW_RESET BIT(15) |
| 57 | #define DP83867_SW_RESTART BIT(14) |
| 58 | |
| 59 | /* MICR Interrupt bits */ |
| 60 | #define MII_DP83867_MICR_AN_ERR_INT_EN BIT(15) |
| 61 | #define MII_DP83867_MICR_SPEED_CHNG_INT_EN BIT(14) |
| 62 | #define MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN BIT(13) |
| 63 | #define MII_DP83867_MICR_PAGE_RXD_INT_EN BIT(12) |
| 64 | #define MII_DP83867_MICR_AUTONEG_COMP_INT_EN BIT(11) |
| 65 | #define MII_DP83867_MICR_LINK_STS_CHNG_INT_EN BIT(10) |
| 66 | #define MII_DP83867_MICR_FALSE_CARRIER_INT_EN BIT(8) |
| 67 | #define MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN BIT(4) |
| 68 | #define MII_DP83867_MICR_WOL_INT_EN BIT(3) |
| 69 | #define MII_DP83867_MICR_XGMII_ERR_INT_EN BIT(2) |
| 70 | #define MII_DP83867_MICR_POL_CHNG_INT_EN BIT(1) |
| 71 | #define MII_DP83867_MICR_JABBER_INT_EN BIT(0) |
| 72 | |
| 73 | /* RGMIICTL bits */ |
| 74 | #define DP83867_RGMII_TX_CLK_DELAY_EN BIT(1) |
| 75 | #define DP83867_RGMII_RX_CLK_DELAY_EN BIT(0) |
| 76 | |
Vitaly Gaiduk | 507ddd5 | 2019-09-09 20:19:24 +0300 | [diff] [blame] | 77 | /* SGMIICTL bits */ |
| 78 | #define DP83867_SGMII_TYPE BIT(14) |
| 79 | |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 80 | /* RXFCFG bits*/ |
| 81 | #define DP83867_WOL_MAGIC_EN BIT(0) |
| 82 | #define DP83867_WOL_BCAST_EN BIT(2) |
| 83 | #define DP83867_WOL_UCAST_EN BIT(4) |
| 84 | #define DP83867_WOL_SEC_EN BIT(5) |
| 85 | #define DP83867_WOL_ENH_MAC BIT(7) |
| 86 | |
Lukasz Majewski | ac6e058 | 2017-02-07 06:20:24 +0100 | [diff] [blame] | 87 | /* STRAP_STS1 bits */ |
| 88 | #define DP83867_STRAP_STS1_RESERVED BIT(11) |
| 89 | |
Trent Piepho | c11669a | 2019-05-22 18:43:23 +0000 | [diff] [blame] | 90 | /* STRAP_STS2 bits */ |
| 91 | #define DP83867_STRAP_STS2_CLK_SKEW_TX_MASK GENMASK(6, 4) |
| 92 | #define DP83867_STRAP_STS2_CLK_SKEW_TX_SHIFT 4 |
| 93 | #define DP83867_STRAP_STS2_CLK_SKEW_RX_MASK GENMASK(2, 0) |
| 94 | #define DP83867_STRAP_STS2_CLK_SKEW_RX_SHIFT 0 |
| 95 | #define DP83867_STRAP_STS2_CLK_SKEW_NONE BIT(2) |
Grygorii Strashko | 749f6f6 | 2020-03-17 20:04:54 +0200 | [diff] [blame] | 96 | #define DP83867_STRAP_STS2_STRAP_FLD BIT(10) |
Trent Piepho | c11669a | 2019-05-22 18:43:23 +0000 | [diff] [blame] | 97 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 98 | /* PHY CTRL bits */ |
Dan Murphy | e02d181 | 2019-12-09 14:10:25 -0600 | [diff] [blame] | 99 | #define DP83867_PHYCR_TX_FIFO_DEPTH_SHIFT 14 |
| 100 | #define DP83867_PHYCR_RX_FIFO_DEPTH_SHIFT 12 |
Trent Piepho | f8bbf41 | 2019-05-22 18:43:26 +0000 | [diff] [blame] | 101 | #define DP83867_PHYCR_FIFO_DEPTH_MAX 0x03 |
Dan Murphy | e02d181 | 2019-12-09 14:10:25 -0600 | [diff] [blame] | 102 | #define DP83867_PHYCR_TX_FIFO_DEPTH_MASK GENMASK(15, 14) |
| 103 | #define DP83867_PHYCR_RX_FIFO_DEPTH_MASK GENMASK(13, 12) |
Lukasz Majewski | ac6e058 | 2017-02-07 06:20:24 +0100 | [diff] [blame] | 104 | #define DP83867_PHYCR_RESERVED_MASK BIT(11) |
Michael Grzeschik | 86ffe92 | 2020-01-16 14:16:31 +0100 | [diff] [blame] | 105 | #define DP83867_PHYCR_FORCE_LINK_GOOD BIT(10) |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 106 | |
| 107 | /* RGMIIDCTL bits */ |
Trent Piepho | c11669a | 2019-05-22 18:43:23 +0000 | [diff] [blame] | 108 | #define DP83867_RGMII_TX_CLK_DELAY_MAX 0xf |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 109 | #define DP83867_RGMII_TX_CLK_DELAY_SHIFT 4 |
Grygorii Strashko | fafc5db | 2019-12-06 14:34:32 +0200 | [diff] [blame] | 110 | #define DP83867_RGMII_TX_CLK_DELAY_INV (DP83867_RGMII_TX_CLK_DELAY_MAX + 1) |
Trent Piepho | c11669a | 2019-05-22 18:43:23 +0000 | [diff] [blame] | 111 | #define DP83867_RGMII_RX_CLK_DELAY_MAX 0xf |
| 112 | #define DP83867_RGMII_RX_CLK_DELAY_SHIFT 0 |
Grygorii Strashko | fafc5db | 2019-12-06 14:34:32 +0200 | [diff] [blame] | 113 | #define DP83867_RGMII_RX_CLK_DELAY_INV (DP83867_RGMII_RX_CLK_DELAY_MAX + 1) |
| 114 | |
Mugunthan V N | ed838fe | 2016-10-18 16:50:18 +0530 | [diff] [blame] | 115 | /* IO_MUX_CFG bits */ |
Trent Piepho | 27708eb | 2019-05-22 18:43:25 +0000 | [diff] [blame] | 116 | #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MASK 0x1f |
Mugunthan V N | ed838fe | 2016-10-18 16:50:18 +0530 | [diff] [blame] | 117 | #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX 0x0 |
| 118 | #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN 0x1f |
Trent Piepho | 13c83cf | 2019-05-22 18:43:22 +0000 | [diff] [blame] | 119 | #define DP83867_IO_MUX_CFG_CLK_O_DISABLE BIT(6) |
Wadim Egorov | 9708fb6 | 2018-02-14 17:07:11 +0100 | [diff] [blame] | 120 | #define DP83867_IO_MUX_CFG_CLK_O_SEL_MASK (0x1f << 8) |
| 121 | #define DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT 8 |
Mugunthan V N | ed838fe | 2016-10-18 16:50:18 +0530 | [diff] [blame] | 122 | |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 123 | /* PHY STS bits */ |
| 124 | #define DP83867_PHYSTS_1000 BIT(15) |
| 125 | #define DP83867_PHYSTS_100 BIT(14) |
| 126 | #define DP83867_PHYSTS_DUPLEX BIT(13) |
| 127 | #define DP83867_PHYSTS_LINK BIT(10) |
| 128 | |
| 129 | /* CFG2 bits */ |
| 130 | #define DP83867_DOWNSHIFT_EN (BIT(8) | BIT(9)) |
| 131 | #define DP83867_DOWNSHIFT_ATTEMPT_MASK (BIT(10) | BIT(11)) |
| 132 | #define DP83867_DOWNSHIFT_1_COUNT_VAL 0 |
| 133 | #define DP83867_DOWNSHIFT_2_COUNT_VAL 1 |
| 134 | #define DP83867_DOWNSHIFT_4_COUNT_VAL 2 |
| 135 | #define DP83867_DOWNSHIFT_8_COUNT_VAL 3 |
| 136 | #define DP83867_DOWNSHIFT_1_COUNT 1 |
| 137 | #define DP83867_DOWNSHIFT_2_COUNT 2 |
| 138 | #define DP83867_DOWNSHIFT_4_COUNT 4 |
| 139 | #define DP83867_DOWNSHIFT_8_COUNT 8 |
| 140 | |
Grygorii Strashko | 5a7f08c | 2019-10-23 17:48:45 +0300 | [diff] [blame] | 141 | /* CFG3 bits */ |
| 142 | #define DP83867_CFG3_INT_OE BIT(7) |
| 143 | #define DP83867_CFG3_ROBUST_AUTO_MDIX BIT(9) |
| 144 | |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 145 | /* CFG4 bits */ |
| 146 | #define DP83867_CFG4_PORT_MIRROR_EN BIT(0) |
| 147 | |
Grygorii Strashko | 749f6f6 | 2020-03-17 20:04:54 +0200 | [diff] [blame] | 148 | /* FLD_THR_CFG */ |
| 149 | #define DP83867_FLD_THR_CFG_ENERGY_LOST_THR_MASK 0x7 |
| 150 | |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 151 | enum { |
| 152 | DP83867_PORT_MIRROING_KEEP, |
| 153 | DP83867_PORT_MIRROING_EN, |
| 154 | DP83867_PORT_MIRROING_DIS, |
| 155 | }; |
| 156 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 157 | struct dp83867_private { |
Trent Piepho | 1b9b295 | 2019-05-22 18:43:24 +0000 | [diff] [blame] | 158 | u32 rx_id_delay; |
| 159 | u32 tx_id_delay; |
Dan Murphy | e02d181 | 2019-12-09 14:10:25 -0600 | [diff] [blame] | 160 | u32 tx_fifo_depth; |
| 161 | u32 rx_fifo_depth; |
Mugunthan V N | ed838fe | 2016-10-18 16:50:18 +0530 | [diff] [blame] | 162 | int io_impedance; |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 163 | int port_mirroring; |
Murali Karicheri | 3714447 | 2017-07-04 16:23:24 +0530 | [diff] [blame] | 164 | bool rxctrl_strap_quirk; |
Trent Piepho | 13c83cf | 2019-05-22 18:43:22 +0000 | [diff] [blame] | 165 | bool set_clk_output; |
| 166 | u32 clk_output_sel; |
Vitaly Gaiduk | 507ddd5 | 2019-09-09 20:19:24 +0300 | [diff] [blame] | 167 | bool sgmii_ref_clk_en; |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | static int dp83867_ack_interrupt(struct phy_device *phydev) |
| 171 | { |
| 172 | int err = phy_read(phydev, MII_DP83867_ISR); |
| 173 | |
| 174 | if (err < 0) |
| 175 | return err; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 180 | static int dp83867_set_wol(struct phy_device *phydev, |
| 181 | struct ethtool_wolinfo *wol) |
| 182 | { |
| 183 | struct net_device *ndev = phydev->attached_dev; |
| 184 | u16 val_rxcfg, val_micr; |
Jakub Kicinski | 86466cb | 2021-10-22 16:20:58 -0700 | [diff] [blame] | 185 | const u8 *mac; |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 186 | |
| 187 | val_rxcfg = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_RXFCFG); |
| 188 | val_micr = phy_read(phydev, MII_DP83867_MICR); |
| 189 | |
| 190 | if (wol->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_UCAST | |
| 191 | WAKE_BCAST)) { |
| 192 | val_rxcfg |= DP83867_WOL_ENH_MAC; |
| 193 | val_micr |= MII_DP83867_MICR_WOL_INT_EN; |
| 194 | |
| 195 | if (wol->wolopts & WAKE_MAGIC) { |
Jakub Kicinski | 86466cb | 2021-10-22 16:20:58 -0700 | [diff] [blame] | 196 | mac = (const u8 *)ndev->dev_addr; |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 197 | |
| 198 | if (!is_valid_ether_addr(mac)) |
| 199 | return -EINVAL; |
| 200 | |
| 201 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RXFPMD1, |
| 202 | (mac[1] << 8 | mac[0])); |
| 203 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RXFPMD2, |
| 204 | (mac[3] << 8 | mac[2])); |
| 205 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RXFPMD3, |
| 206 | (mac[5] << 8 | mac[4])); |
| 207 | |
| 208 | val_rxcfg |= DP83867_WOL_MAGIC_EN; |
| 209 | } else { |
| 210 | val_rxcfg &= ~DP83867_WOL_MAGIC_EN; |
| 211 | } |
| 212 | |
| 213 | if (wol->wolopts & WAKE_MAGICSECURE) { |
| 214 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RXFSOP1, |
| 215 | (wol->sopass[1] << 8) | wol->sopass[0]); |
Dan Murphy | 8b4a11c | 2020-09-02 14:27:04 -0500 | [diff] [blame] | 216 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RXFSOP2, |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 217 | (wol->sopass[3] << 8) | wol->sopass[2]); |
Dan Murphy | 8b4a11c | 2020-09-02 14:27:04 -0500 | [diff] [blame] | 218 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RXFSOP3, |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 219 | (wol->sopass[5] << 8) | wol->sopass[4]); |
| 220 | |
| 221 | val_rxcfg |= DP83867_WOL_SEC_EN; |
| 222 | } else { |
| 223 | val_rxcfg &= ~DP83867_WOL_SEC_EN; |
| 224 | } |
| 225 | |
| 226 | if (wol->wolopts & WAKE_UCAST) |
| 227 | val_rxcfg |= DP83867_WOL_UCAST_EN; |
| 228 | else |
| 229 | val_rxcfg &= ~DP83867_WOL_UCAST_EN; |
| 230 | |
| 231 | if (wol->wolopts & WAKE_BCAST) |
| 232 | val_rxcfg |= DP83867_WOL_BCAST_EN; |
| 233 | else |
| 234 | val_rxcfg &= ~DP83867_WOL_BCAST_EN; |
| 235 | } else { |
| 236 | val_rxcfg &= ~DP83867_WOL_ENH_MAC; |
| 237 | val_micr &= ~MII_DP83867_MICR_WOL_INT_EN; |
| 238 | } |
| 239 | |
| 240 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RXFCFG, val_rxcfg); |
| 241 | phy_write(phydev, MII_DP83867_MICR, val_micr); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static void dp83867_get_wol(struct phy_device *phydev, |
| 247 | struct ethtool_wolinfo *wol) |
| 248 | { |
| 249 | u16 value, sopass_val; |
| 250 | |
| 251 | wol->supported = (WAKE_UCAST | WAKE_BCAST | WAKE_MAGIC | |
| 252 | WAKE_MAGICSECURE); |
| 253 | wol->wolopts = 0; |
| 254 | |
| 255 | value = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_RXFCFG); |
| 256 | |
| 257 | if (value & DP83867_WOL_UCAST_EN) |
| 258 | wol->wolopts |= WAKE_UCAST; |
| 259 | |
| 260 | if (value & DP83867_WOL_BCAST_EN) |
| 261 | wol->wolopts |= WAKE_BCAST; |
| 262 | |
| 263 | if (value & DP83867_WOL_MAGIC_EN) |
| 264 | wol->wolopts |= WAKE_MAGIC; |
| 265 | |
| 266 | if (value & DP83867_WOL_SEC_EN) { |
| 267 | sopass_val = phy_read_mmd(phydev, DP83867_DEVADDR, |
| 268 | DP83867_RXFSOP1); |
| 269 | wol->sopass[0] = (sopass_val & 0xff); |
| 270 | wol->sopass[1] = (sopass_val >> 8); |
| 271 | |
| 272 | sopass_val = phy_read_mmd(phydev, DP83867_DEVADDR, |
| 273 | DP83867_RXFSOP2); |
| 274 | wol->sopass[2] = (sopass_val & 0xff); |
| 275 | wol->sopass[3] = (sopass_val >> 8); |
| 276 | |
| 277 | sopass_val = phy_read_mmd(phydev, DP83867_DEVADDR, |
| 278 | DP83867_RXFSOP3); |
| 279 | wol->sopass[4] = (sopass_val & 0xff); |
| 280 | wol->sopass[5] = (sopass_val >> 8); |
| 281 | |
| 282 | wol->wolopts |= WAKE_MAGICSECURE; |
| 283 | } |
| 284 | |
| 285 | if (!(value & DP83867_WOL_ENH_MAC)) |
| 286 | wol->wolopts = 0; |
| 287 | } |
| 288 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 289 | static int dp83867_config_intr(struct phy_device *phydev) |
| 290 | { |
Ioana Ciornei | aa2d603 | 2020-11-23 17:38:14 +0200 | [diff] [blame] | 291 | int micr_status, err; |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 292 | |
| 293 | if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { |
Ioana Ciornei | aa2d603 | 2020-11-23 17:38:14 +0200 | [diff] [blame] | 294 | err = dp83867_ack_interrupt(phydev); |
| 295 | if (err) |
| 296 | return err; |
| 297 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 298 | micr_status = phy_read(phydev, MII_DP83867_MICR); |
| 299 | if (micr_status < 0) |
| 300 | return micr_status; |
| 301 | |
| 302 | micr_status |= |
| 303 | (MII_DP83867_MICR_AN_ERR_INT_EN | |
| 304 | MII_DP83867_MICR_SPEED_CHNG_INT_EN | |
Grygorii Strashko | 5ca7d1c | 2017-01-05 14:48:07 -0600 | [diff] [blame] | 305 | MII_DP83867_MICR_AUTONEG_COMP_INT_EN | |
| 306 | MII_DP83867_MICR_LINK_STS_CHNG_INT_EN | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 307 | MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN | |
| 308 | MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN); |
| 309 | |
Ioana Ciornei | aa2d603 | 2020-11-23 17:38:14 +0200 | [diff] [blame] | 310 | err = phy_write(phydev, MII_DP83867_MICR, micr_status); |
| 311 | } else { |
| 312 | micr_status = 0x0; |
| 313 | err = phy_write(phydev, MII_DP83867_MICR, micr_status); |
| 314 | if (err) |
| 315 | return err; |
| 316 | |
| 317 | err = dp83867_ack_interrupt(phydev); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Ioana Ciornei | aa2d603 | 2020-11-23 17:38:14 +0200 | [diff] [blame] | 320 | return err; |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 321 | } |
| 322 | |
Ioana Ciornei | 1d1ae3c | 2020-11-23 17:38:13 +0200 | [diff] [blame] | 323 | static irqreturn_t dp83867_handle_interrupt(struct phy_device *phydev) |
| 324 | { |
| 325 | int irq_status, irq_enabled; |
| 326 | |
| 327 | irq_status = phy_read(phydev, MII_DP83867_ISR); |
| 328 | if (irq_status < 0) { |
| 329 | phy_error(phydev); |
| 330 | return IRQ_NONE; |
| 331 | } |
| 332 | |
| 333 | irq_enabled = phy_read(phydev, MII_DP83867_MICR); |
| 334 | if (irq_enabled < 0) { |
| 335 | phy_error(phydev); |
| 336 | return IRQ_NONE; |
| 337 | } |
| 338 | |
| 339 | if (!(irq_status & irq_enabled)) |
| 340 | return IRQ_NONE; |
| 341 | |
| 342 | phy_trigger_machine(phydev); |
| 343 | |
| 344 | return IRQ_HANDLED; |
| 345 | } |
| 346 | |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 347 | static int dp83867_read_status(struct phy_device *phydev) |
| 348 | { |
| 349 | int status = phy_read(phydev, MII_DP83867_PHYSTS); |
| 350 | int ret; |
| 351 | |
| 352 | ret = genphy_read_status(phydev); |
| 353 | if (ret) |
| 354 | return ret; |
| 355 | |
| 356 | if (status < 0) |
| 357 | return status; |
| 358 | |
| 359 | if (status & DP83867_PHYSTS_DUPLEX) |
| 360 | phydev->duplex = DUPLEX_FULL; |
| 361 | else |
| 362 | phydev->duplex = DUPLEX_HALF; |
| 363 | |
| 364 | if (status & DP83867_PHYSTS_1000) |
| 365 | phydev->speed = SPEED_1000; |
| 366 | else if (status & DP83867_PHYSTS_100) |
| 367 | phydev->speed = SPEED_100; |
| 368 | else |
| 369 | phydev->speed = SPEED_10; |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int dp83867_get_downshift(struct phy_device *phydev, u8 *data) |
| 375 | { |
| 376 | int val, cnt, enable, count; |
| 377 | |
| 378 | val = phy_read(phydev, DP83867_CFG2); |
| 379 | if (val < 0) |
| 380 | return val; |
| 381 | |
| 382 | enable = FIELD_GET(DP83867_DOWNSHIFT_EN, val); |
| 383 | cnt = FIELD_GET(DP83867_DOWNSHIFT_ATTEMPT_MASK, val); |
| 384 | |
| 385 | switch (cnt) { |
| 386 | case DP83867_DOWNSHIFT_1_COUNT_VAL: |
| 387 | count = DP83867_DOWNSHIFT_1_COUNT; |
| 388 | break; |
| 389 | case DP83867_DOWNSHIFT_2_COUNT_VAL: |
| 390 | count = DP83867_DOWNSHIFT_2_COUNT; |
| 391 | break; |
| 392 | case DP83867_DOWNSHIFT_4_COUNT_VAL: |
| 393 | count = DP83867_DOWNSHIFT_4_COUNT; |
| 394 | break; |
| 395 | case DP83867_DOWNSHIFT_8_COUNT_VAL: |
| 396 | count = DP83867_DOWNSHIFT_8_COUNT; |
| 397 | break; |
| 398 | default: |
| 399 | return -EINVAL; |
Zheng Bin | dce38b7 | 2020-04-24 17:08:50 +0800 | [diff] [blame] | 400 | } |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 401 | |
| 402 | *data = enable ? count : DOWNSHIFT_DEV_DISABLE; |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int dp83867_set_downshift(struct phy_device *phydev, u8 cnt) |
| 408 | { |
| 409 | int val, count; |
| 410 | |
| 411 | if (cnt > DP83867_DOWNSHIFT_8_COUNT) |
| 412 | return -E2BIG; |
| 413 | |
| 414 | if (!cnt) |
| 415 | return phy_clear_bits(phydev, DP83867_CFG2, |
| 416 | DP83867_DOWNSHIFT_EN); |
| 417 | |
| 418 | switch (cnt) { |
Dan Murphy | 753c66e | 2020-09-03 14:51:12 -0500 | [diff] [blame] | 419 | case DP83867_DOWNSHIFT_1_COUNT: |
| 420 | count = DP83867_DOWNSHIFT_1_COUNT_VAL; |
| 421 | break; |
| 422 | case DP83867_DOWNSHIFT_2_COUNT: |
| 423 | count = DP83867_DOWNSHIFT_2_COUNT_VAL; |
| 424 | break; |
| 425 | case DP83867_DOWNSHIFT_4_COUNT: |
| 426 | count = DP83867_DOWNSHIFT_4_COUNT_VAL; |
| 427 | break; |
| 428 | case DP83867_DOWNSHIFT_8_COUNT: |
| 429 | count = DP83867_DOWNSHIFT_8_COUNT_VAL; |
| 430 | break; |
| 431 | default: |
| 432 | phydev_err(phydev, |
| 433 | "Downshift count must be 1, 2, 4 or 8\n"); |
| 434 | return -EINVAL; |
Zheng Bin | dce38b7 | 2020-04-24 17:08:50 +0800 | [diff] [blame] | 435 | } |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 436 | |
| 437 | val = DP83867_DOWNSHIFT_EN; |
| 438 | val |= FIELD_PREP(DP83867_DOWNSHIFT_ATTEMPT_MASK, count); |
| 439 | |
| 440 | return phy_modify(phydev, DP83867_CFG2, |
| 441 | DP83867_DOWNSHIFT_EN | DP83867_DOWNSHIFT_ATTEMPT_MASK, |
| 442 | val); |
| 443 | } |
| 444 | |
| 445 | static int dp83867_get_tunable(struct phy_device *phydev, |
Dan Murphy | 753c66e | 2020-09-03 14:51:12 -0500 | [diff] [blame] | 446 | struct ethtool_tunable *tuna, void *data) |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 447 | { |
| 448 | switch (tuna->id) { |
| 449 | case ETHTOOL_PHY_DOWNSHIFT: |
| 450 | return dp83867_get_downshift(phydev, data); |
| 451 | default: |
| 452 | return -EOPNOTSUPP; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | static int dp83867_set_tunable(struct phy_device *phydev, |
Dan Murphy | 753c66e | 2020-09-03 14:51:12 -0500 | [diff] [blame] | 457 | struct ethtool_tunable *tuna, const void *data) |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 458 | { |
| 459 | switch (tuna->id) { |
| 460 | case ETHTOOL_PHY_DOWNSHIFT: |
| 461 | return dp83867_set_downshift(phydev, *(const u8 *)data); |
| 462 | default: |
| 463 | return -EOPNOTSUPP; |
| 464 | } |
| 465 | } |
| 466 | |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 467 | static int dp83867_config_port_mirroring(struct phy_device *phydev) |
| 468 | { |
| 469 | struct dp83867_private *dp83867 = |
| 470 | (struct dp83867_private *)phydev->priv; |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 471 | |
| 472 | if (dp83867->port_mirroring == DP83867_PORT_MIRROING_EN) |
Heiner Kallweit | b52c018 | 2019-02-06 07:38:43 +0100 | [diff] [blame] | 473 | phy_set_bits_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4, |
| 474 | DP83867_CFG4_PORT_MIRROR_EN); |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 475 | else |
Heiner Kallweit | b52c018 | 2019-02-06 07:38:43 +0100 | [diff] [blame] | 476 | phy_clear_bits_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4, |
| 477 | DP83867_CFG4_PORT_MIRROR_EN); |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Grygorii Strashko | fafc5db | 2019-12-06 14:34:32 +0200 | [diff] [blame] | 481 | static int dp83867_verify_rgmii_cfg(struct phy_device *phydev) |
| 482 | { |
| 483 | struct dp83867_private *dp83867 = phydev->priv; |
| 484 | |
| 485 | /* Existing behavior was to use default pin strapping delay in rgmii |
| 486 | * mode, but rgmii should have meant no delay. Warn existing users. |
| 487 | */ |
| 488 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII) { |
| 489 | const u16 val = phy_read_mmd(phydev, DP83867_DEVADDR, |
| 490 | DP83867_STRAP_STS2); |
| 491 | const u16 txskew = (val & DP83867_STRAP_STS2_CLK_SKEW_TX_MASK) >> |
| 492 | DP83867_STRAP_STS2_CLK_SKEW_TX_SHIFT; |
| 493 | const u16 rxskew = (val & DP83867_STRAP_STS2_CLK_SKEW_RX_MASK) >> |
| 494 | DP83867_STRAP_STS2_CLK_SKEW_RX_SHIFT; |
| 495 | |
| 496 | if (txskew != DP83867_STRAP_STS2_CLK_SKEW_NONE || |
| 497 | rxskew != DP83867_STRAP_STS2_CLK_SKEW_NONE) |
| 498 | phydev_warn(phydev, |
| 499 | "PHY has delays via pin strapping, but phy-mode = 'rgmii'\n" |
| 500 | "Should be 'rgmii-id' to use internal delays txskew:%x rxskew:%x\n", |
| 501 | txskew, rxskew); |
| 502 | } |
| 503 | |
| 504 | /* RX delay *must* be specified if internal delay of RX is used. */ |
| 505 | if ((phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || |
| 506 | phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) && |
| 507 | dp83867->rx_id_delay == DP83867_RGMII_RX_CLK_DELAY_INV) { |
| 508 | phydev_err(phydev, "ti,rx-internal-delay must be specified\n"); |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
| 512 | /* TX delay *must* be specified if internal delay of TX is used. */ |
| 513 | if ((phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || |
| 514 | phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) && |
| 515 | dp83867->tx_id_delay == DP83867_RGMII_TX_CLK_DELAY_INV) { |
| 516 | phydev_err(phydev, "ti,tx-internal-delay must be specified\n"); |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
Dan Murphy | 506de00 | 2020-06-05 09:01:05 -0500 | [diff] [blame] | 523 | #if IS_ENABLED(CONFIG_OF_MDIO) |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 524 | static int dp83867_of_init(struct phy_device *phydev) |
| 525 | { |
| 526 | struct dp83867_private *dp83867 = phydev->priv; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 527 | struct device *dev = &phydev->mdio.dev; |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 528 | struct device_node *of_node = dev->of_node; |
| 529 | int ret; |
| 530 | |
Andrew Lunn | 7bf9ae0 | 2015-12-07 04:38:58 +0100 | [diff] [blame] | 531 | if (!of_node) |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 532 | return -ENODEV; |
| 533 | |
Mugunthan V N | ed838fe | 2016-10-18 16:50:18 +0530 | [diff] [blame] | 534 | /* Optional configuration */ |
Wadim Egorov | 9708fb6 | 2018-02-14 17:07:11 +0100 | [diff] [blame] | 535 | ret = of_property_read_u32(of_node, "ti,clk-output-sel", |
| 536 | &dp83867->clk_output_sel); |
Trent Piepho | 13c83cf | 2019-05-22 18:43:22 +0000 | [diff] [blame] | 537 | /* If not set, keep default */ |
| 538 | if (!ret) { |
| 539 | dp83867->set_clk_output = true; |
| 540 | /* Valid values are 0 to DP83867_CLK_O_SEL_REF_CLK or |
| 541 | * DP83867_CLK_O_SEL_OFF. |
Wadim Egorov | 9708fb6 | 2018-02-14 17:07:11 +0100 | [diff] [blame] | 542 | */ |
Trent Piepho | 13c83cf | 2019-05-22 18:43:22 +0000 | [diff] [blame] | 543 | if (dp83867->clk_output_sel > DP83867_CLK_O_SEL_REF_CLK && |
| 544 | dp83867->clk_output_sel != DP83867_CLK_O_SEL_OFF) { |
| 545 | phydev_err(phydev, "ti,clk-output-sel value %u out of range\n", |
| 546 | dp83867->clk_output_sel); |
| 547 | return -EINVAL; |
| 548 | } |
| 549 | } |
Wadim Egorov | 9708fb6 | 2018-02-14 17:07:11 +0100 | [diff] [blame] | 550 | |
Mugunthan V N | ed838fe | 2016-10-18 16:50:18 +0530 | [diff] [blame] | 551 | if (of_property_read_bool(of_node, "ti,max-output-impedance")) |
| 552 | dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX; |
| 553 | else if (of_property_read_bool(of_node, "ti,min-output-impedance")) |
| 554 | dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN; |
Trent Piepho | 27708eb | 2019-05-22 18:43:25 +0000 | [diff] [blame] | 555 | else |
| 556 | dp83867->io_impedance = -1; /* leave at default */ |
Mugunthan V N | ed838fe | 2016-10-18 16:50:18 +0530 | [diff] [blame] | 557 | |
Murali Karicheri | 3714447 | 2017-07-04 16:23:24 +0530 | [diff] [blame] | 558 | dp83867->rxctrl_strap_quirk = of_property_read_bool(of_node, |
Dan Murphy | 753c66e | 2020-09-03 14:51:12 -0500 | [diff] [blame] | 559 | "ti,dp83867-rxctrl-strap-quirk"); |
Murali Karicheri | 3714447 | 2017-07-04 16:23:24 +0530 | [diff] [blame] | 560 | |
Vitaly Gaiduk | 507ddd5 | 2019-09-09 20:19:24 +0300 | [diff] [blame] | 561 | dp83867->sgmii_ref_clk_en = of_property_read_bool(of_node, |
Dan Murphy | 753c66e | 2020-09-03 14:51:12 -0500 | [diff] [blame] | 562 | "ti,sgmii-ref-clock-output-enable"); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 563 | |
Grygorii Strashko | fafc5db | 2019-12-06 14:34:32 +0200 | [diff] [blame] | 564 | dp83867->rx_id_delay = DP83867_RGMII_RX_CLK_DELAY_INV; |
| 565 | ret = of_property_read_u32(of_node, "ti,rx-internal-delay", |
| 566 | &dp83867->rx_id_delay); |
| 567 | if (!ret && dp83867->rx_id_delay > DP83867_RGMII_RX_CLK_DELAY_MAX) { |
| 568 | phydev_err(phydev, |
| 569 | "ti,rx-internal-delay value of %u out of range\n", |
| 570 | dp83867->rx_id_delay); |
| 571 | return -EINVAL; |
Trent Piepho | c11669a | 2019-05-22 18:43:23 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Grygorii Strashko | fafc5db | 2019-12-06 14:34:32 +0200 | [diff] [blame] | 574 | dp83867->tx_id_delay = DP83867_RGMII_TX_CLK_DELAY_INV; |
| 575 | ret = of_property_read_u32(of_node, "ti,tx-internal-delay", |
| 576 | &dp83867->tx_id_delay); |
| 577 | if (!ret && dp83867->tx_id_delay > DP83867_RGMII_TX_CLK_DELAY_MAX) { |
| 578 | phydev_err(phydev, |
| 579 | "ti,tx-internal-delay value of %u out of range\n", |
| 580 | dp83867->tx_id_delay); |
| 581 | return -EINVAL; |
Trent Piepho | c11669a | 2019-05-22 18:43:23 +0000 | [diff] [blame] | 582 | } |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 583 | |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 584 | if (of_property_read_bool(of_node, "enet-phy-lane-swap")) |
| 585 | dp83867->port_mirroring = DP83867_PORT_MIRROING_EN; |
| 586 | |
| 587 | if (of_property_read_bool(of_node, "enet-phy-lane-no-swap")) |
| 588 | dp83867->port_mirroring = DP83867_PORT_MIRROING_DIS; |
| 589 | |
Trent Piepho | f8bbf41 | 2019-05-22 18:43:26 +0000 | [diff] [blame] | 590 | ret = of_property_read_u32(of_node, "ti,fifo-depth", |
Dan Murphy | e02d181 | 2019-12-09 14:10:25 -0600 | [diff] [blame] | 591 | &dp83867->tx_fifo_depth); |
Trent Piepho | f8bbf41 | 2019-05-22 18:43:26 +0000 | [diff] [blame] | 592 | if (ret) { |
Dan Murphy | e02d181 | 2019-12-09 14:10:25 -0600 | [diff] [blame] | 593 | ret = of_property_read_u32(of_node, "tx-fifo-depth", |
| 594 | &dp83867->tx_fifo_depth); |
| 595 | if (ret) |
| 596 | dp83867->tx_fifo_depth = |
| 597 | DP83867_PHYCR_FIFO_DEPTH_4_B_NIB; |
Trent Piepho | f8bbf41 | 2019-05-22 18:43:26 +0000 | [diff] [blame] | 598 | } |
Dan Murphy | e02d181 | 2019-12-09 14:10:25 -0600 | [diff] [blame] | 599 | |
| 600 | if (dp83867->tx_fifo_depth > DP83867_PHYCR_FIFO_DEPTH_MAX) { |
| 601 | phydev_err(phydev, "tx-fifo-depth value %u out of range\n", |
| 602 | dp83867->tx_fifo_depth); |
Trent Piepho | f8bbf41 | 2019-05-22 18:43:26 +0000 | [diff] [blame] | 603 | return -EINVAL; |
| 604 | } |
Dan Murphy | e02d181 | 2019-12-09 14:10:25 -0600 | [diff] [blame] | 605 | |
| 606 | ret = of_property_read_u32(of_node, "rx-fifo-depth", |
| 607 | &dp83867->rx_fifo_depth); |
| 608 | if (ret) |
| 609 | dp83867->rx_fifo_depth = DP83867_PHYCR_FIFO_DEPTH_4_B_NIB; |
| 610 | |
| 611 | if (dp83867->rx_fifo_depth > DP83867_PHYCR_FIFO_DEPTH_MAX) { |
| 612 | phydev_err(phydev, "rx-fifo-depth value %u out of range\n", |
| 613 | dp83867->rx_fifo_depth); |
| 614 | return -EINVAL; |
| 615 | } |
| 616 | |
Trent Piepho | f8bbf41 | 2019-05-22 18:43:26 +0000 | [diff] [blame] | 617 | return 0; |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 618 | } |
| 619 | #else |
| 620 | static int dp83867_of_init(struct phy_device *phydev) |
| 621 | { |
Lay, Kuan Loon | 4dc08dc | 2021-10-13 14:59:41 +0800 | [diff] [blame] | 622 | struct dp83867_private *dp83867 = phydev->priv; |
| 623 | u16 delay; |
| 624 | |
| 625 | /* For non-OF device, the RX and TX ID values are either strapped |
| 626 | * or take from default value. So, we init RX & TX ID values here |
| 627 | * so that the RGMIIDCTL is configured correctly later in |
| 628 | * dp83867_config_init(); |
| 629 | */ |
| 630 | delay = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIIDCTL); |
| 631 | dp83867->rx_id_delay = delay & DP83867_RGMII_RX_CLK_DELAY_MAX; |
| 632 | dp83867->tx_id_delay = (delay >> DP83867_RGMII_TX_CLK_DELAY_SHIFT) & |
| 633 | DP83867_RGMII_TX_CLK_DELAY_MAX; |
| 634 | |
| 635 | /* Per datasheet, IO impedance is default to 50-ohm, so we set the |
| 636 | * same here or else the default '0' means highest IO impedance |
| 637 | * which is wrong. |
| 638 | */ |
| 639 | dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN / 2; |
| 640 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | #endif /* CONFIG_OF_MDIO */ |
| 644 | |
Trent Piepho | 565d9d2 | 2019-05-22 18:43:27 +0000 | [diff] [blame] | 645 | static int dp83867_probe(struct phy_device *phydev) |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 646 | { |
| 647 | struct dp83867_private *dp83867; |
Trent Piepho | 565d9d2 | 2019-05-22 18:43:27 +0000 | [diff] [blame] | 648 | |
| 649 | dp83867 = devm_kzalloc(&phydev->mdio.dev, sizeof(*dp83867), |
| 650 | GFP_KERNEL); |
| 651 | if (!dp83867) |
| 652 | return -ENOMEM; |
| 653 | |
| 654 | phydev->priv = dp83867; |
| 655 | |
Grygorii Strashko | ef87f7d | 2019-10-23 17:48:46 +0300 | [diff] [blame] | 656 | return dp83867_of_init(phydev); |
Trent Piepho | 565d9d2 | 2019-05-22 18:43:27 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static int dp83867_config_init(struct phy_device *phydev) |
| 660 | { |
| 661 | struct dp83867_private *dp83867 = phydev->priv; |
Lukasz Majewski | ac6e058 | 2017-02-07 06:20:24 +0100 | [diff] [blame] | 662 | int ret, val, bs; |
Stefan Hauser | b291c41 | 2016-07-01 22:35:03 +0200 | [diff] [blame] | 663 | u16 delay; |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 664 | |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 665 | /* Force speed optimization for the PHY even if it strapped */ |
| 666 | ret = phy_modify(phydev, DP83867_CFG2, DP83867_DOWNSHIFT_EN, |
| 667 | DP83867_DOWNSHIFT_EN); |
| 668 | if (ret) |
| 669 | return ret; |
| 670 | |
Grygorii Strashko | fafc5db | 2019-12-06 14:34:32 +0200 | [diff] [blame] | 671 | ret = dp83867_verify_rgmii_cfg(phydev); |
| 672 | if (ret) |
| 673 | return ret; |
| 674 | |
Murali Karicheri | 3714447 | 2017-07-04 16:23:24 +0530 | [diff] [blame] | 675 | /* RX_DV/RX_CTRL strapped in mode 1 or mode 2 workaround */ |
Heiner Kallweit | b52c018 | 2019-02-06 07:38:43 +0100 | [diff] [blame] | 676 | if (dp83867->rxctrl_strap_quirk) |
| 677 | phy_clear_bits_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4, |
| 678 | BIT(7)); |
Murali Karicheri | 3714447 | 2017-07-04 16:23:24 +0530 | [diff] [blame] | 679 | |
Grygorii Strashko | 749f6f6 | 2020-03-17 20:04:54 +0200 | [diff] [blame] | 680 | bs = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_STRAP_STS2); |
| 681 | if (bs & DP83867_STRAP_STS2_STRAP_FLD) { |
| 682 | /* When using strap to enable FLD, the ENERGY_LOST_FLD_THR will |
| 683 | * be set to 0x2. This may causes the PHY link to be unstable - |
| 684 | * the default value 0x1 need to be restored. |
| 685 | */ |
| 686 | ret = phy_modify_mmd(phydev, DP83867_DEVADDR, |
| 687 | DP83867_FLD_THR_CFG, |
| 688 | DP83867_FLD_THR_CFG_ENERGY_LOST_THR_MASK, |
| 689 | 0x1); |
| 690 | if (ret) |
| 691 | return ret; |
| 692 | } |
| 693 | |
Dan Murphy | e02d181 | 2019-12-09 14:10:25 -0600 | [diff] [blame] | 694 | if (phy_interface_is_rgmii(phydev) || |
| 695 | phydev->interface == PHY_INTERFACE_MODE_SGMII) { |
| 696 | val = phy_read(phydev, MII_DP83867_PHYCTRL); |
| 697 | if (val < 0) |
| 698 | return val; |
| 699 | |
| 700 | val &= ~DP83867_PHYCR_TX_FIFO_DEPTH_MASK; |
| 701 | val |= (dp83867->tx_fifo_depth << |
| 702 | DP83867_PHYCR_TX_FIFO_DEPTH_SHIFT); |
| 703 | |
| 704 | if (phydev->interface == PHY_INTERFACE_MODE_SGMII) { |
| 705 | val &= ~DP83867_PHYCR_RX_FIFO_DEPTH_MASK; |
| 706 | val |= (dp83867->rx_fifo_depth << |
| 707 | DP83867_PHYCR_RX_FIFO_DEPTH_SHIFT); |
| 708 | } |
| 709 | |
| 710 | ret = phy_write(phydev, MII_DP83867_PHYCTRL, val); |
| 711 | if (ret) |
| 712 | return ret; |
| 713 | } |
| 714 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 715 | if (phy_interface_is_rgmii(phydev)) { |
Stefan Hauser | b291c41 | 2016-07-01 22:35:03 +0200 | [diff] [blame] | 716 | val = phy_read(phydev, MII_DP83867_PHYCTRL); |
| 717 | if (val < 0) |
| 718 | return val; |
Lukasz Majewski | ac6e058 | 2017-02-07 06:20:24 +0100 | [diff] [blame] | 719 | |
| 720 | /* The code below checks if "port mirroring" N/A MODE4 has been |
| 721 | * enabled during power on bootstrap. |
| 722 | * |
| 723 | * Such N/A mode enabled by mistake can put PHY IC in some |
| 724 | * internal testing mode and disable RGMII transmission. |
| 725 | * |
| 726 | * In this particular case one needs to check STRAP_STS1 |
| 727 | * register's bit 11 (marked as RESERVED). |
| 728 | */ |
| 729 | |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 730 | bs = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_STRAP_STS1); |
Lukasz Majewski | ac6e058 | 2017-02-07 06:20:24 +0100 | [diff] [blame] | 731 | if (bs & DP83867_STRAP_STS1_RESERVED) |
| 732 | val &= ~DP83867_PHYCR_RESERVED_MASK; |
| 733 | |
Stefan Hauser | b291c41 | 2016-07-01 22:35:03 +0200 | [diff] [blame] | 734 | ret = phy_write(phydev, MII_DP83867_PHYCTRL, val); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 735 | if (ret) |
| 736 | return ret; |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 737 | |
David S. Miller | b4b12b0 | 2019-05-31 10:49:43 -0700 | [diff] [blame] | 738 | /* If rgmii mode with no internal delay is selected, we do NOT use |
| 739 | * aligned mode as one might expect. Instead we use the PHY's default |
| 740 | * based on pin strapping. And the "mode 0" default is to *use* |
| 741 | * internal delay with a value of 7 (2.00 ns). |
| 742 | * |
| 743 | * Set up RGMII delays |
| 744 | */ |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 745 | val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIICTL); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 746 | |
Trent Piepho | c11669a | 2019-05-22 18:43:23 +0000 | [diff] [blame] | 747 | val &= ~(DP83867_RGMII_TX_CLK_DELAY_EN | DP83867_RGMII_RX_CLK_DELAY_EN); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 748 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) |
| 749 | val |= (DP83867_RGMII_TX_CLK_DELAY_EN | DP83867_RGMII_RX_CLK_DELAY_EN); |
| 750 | |
| 751 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) |
| 752 | val |= DP83867_RGMII_TX_CLK_DELAY_EN; |
| 753 | |
| 754 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) |
| 755 | val |= DP83867_RGMII_RX_CLK_DELAY_EN; |
| 756 | |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 757 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIICTL, val); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 758 | |
Grygorii Strashko | fafc5db | 2019-12-06 14:34:32 +0200 | [diff] [blame] | 759 | delay = 0; |
| 760 | if (dp83867->rx_id_delay != DP83867_RGMII_RX_CLK_DELAY_INV) |
| 761 | delay |= dp83867->rx_id_delay; |
| 762 | if (dp83867->tx_id_delay != DP83867_RGMII_TX_CLK_DELAY_INV) |
| 763 | delay |= dp83867->tx_id_delay << |
| 764 | DP83867_RGMII_TX_CLK_DELAY_SHIFT; |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 765 | |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 766 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIIDCTL, |
| 767 | delay); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 768 | } |
| 769 | |
Trent Piepho | 27708eb | 2019-05-22 18:43:25 +0000 | [diff] [blame] | 770 | /* If specified, set io impedance */ |
| 771 | if (dp83867->io_impedance >= 0) |
| 772 | phy_modify_mmd(phydev, DP83867_DEVADDR, DP83867_IO_MUX_CFG, |
| 773 | DP83867_IO_MUX_CFG_IO_IMPEDANCE_MASK, |
| 774 | dp83867->io_impedance); |
| 775 | |
Max Uvarov | 333061b | 2019-05-28 13:00:49 +0300 | [diff] [blame] | 776 | if (phydev->interface == PHY_INTERFACE_MODE_SGMII) { |
| 777 | /* For support SPEED_10 in SGMII mode |
| 778 | * DP83867_10M_SGMII_RATE_ADAPT bit |
| 779 | * has to be cleared by software. That |
| 780 | * does not affect SPEED_100 and |
| 781 | * SPEED_1000. |
| 782 | */ |
| 783 | ret = phy_modify_mmd(phydev, DP83867_DEVADDR, |
| 784 | DP83867_10M_SGMII_CFG, |
| 785 | DP83867_10M_SGMII_RATE_ADAPT_MASK, |
| 786 | 0); |
| 787 | if (ret) |
| 788 | return ret; |
Max Uvarov | 1a97a47 | 2019-05-28 13:00:50 +0300 | [diff] [blame] | 789 | |
| 790 | /* After reset SGMII Autoneg timer is set to 2us (bits 6 and 5 |
| 791 | * are 01). That is not enough to finalize autoneg on some |
| 792 | * devices. Increase this timer duration to maximum 16ms. |
| 793 | */ |
| 794 | ret = phy_modify_mmd(phydev, DP83867_DEVADDR, |
| 795 | DP83867_CFG4, |
| 796 | DP83867_CFG4_SGMII_ANEG_MASK, |
| 797 | DP83867_CFG4_SGMII_ANEG_TIMER_16MS); |
| 798 | |
| 799 | if (ret) |
| 800 | return ret; |
Vitaly Gaiduk | 507ddd5 | 2019-09-09 20:19:24 +0300 | [diff] [blame] | 801 | |
| 802 | val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_SGMIICTL); |
| 803 | /* SGMII type is set to 4-wire mode by default. |
| 804 | * If we place appropriate property in dts (see above) |
| 805 | * switch on 6-wire mode. |
| 806 | */ |
| 807 | if (dp83867->sgmii_ref_clk_en) |
| 808 | val |= DP83867_SGMII_TYPE; |
| 809 | else |
| 810 | val &= ~DP83867_SGMII_TYPE; |
| 811 | phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_SGMIICTL, val); |
Max Uvarov | 333061b | 2019-05-28 13:00:49 +0300 | [diff] [blame] | 812 | } |
| 813 | |
Grygorii Strashko | 5a7f08c | 2019-10-23 17:48:45 +0300 | [diff] [blame] | 814 | val = phy_read(phydev, DP83867_CFG3); |
Grygorii Strashko | 5ca7d1c | 2017-01-05 14:48:07 -0600 | [diff] [blame] | 815 | /* Enable Interrupt output INT_OE in CFG3 register */ |
Grygorii Strashko | 5a7f08c | 2019-10-23 17:48:45 +0300 | [diff] [blame] | 816 | if (phy_interrupt_is_valid(phydev)) |
| 817 | val |= DP83867_CFG3_INT_OE; |
| 818 | |
| 819 | val |= DP83867_CFG3_ROBUST_AUTO_MDIX; |
| 820 | phy_write(phydev, DP83867_CFG3, val); |
Grygorii Strashko | 5ca7d1c | 2017-01-05 14:48:07 -0600 | [diff] [blame] | 821 | |
Lukasz Majewski | fc6d39c | 2017-02-07 06:20:23 +0100 | [diff] [blame] | 822 | if (dp83867->port_mirroring != DP83867_PORT_MIRROING_KEEP) |
| 823 | dp83867_config_port_mirroring(phydev); |
| 824 | |
Wadim Egorov | 9708fb6 | 2018-02-14 17:07:11 +0100 | [diff] [blame] | 825 | /* Clock output selection if muxing property is set */ |
Trent Piepho | 13c83cf | 2019-05-22 18:43:22 +0000 | [diff] [blame] | 826 | if (dp83867->set_clk_output) { |
| 827 | u16 mask = DP83867_IO_MUX_CFG_CLK_O_DISABLE; |
| 828 | |
| 829 | if (dp83867->clk_output_sel == DP83867_CLK_O_SEL_OFF) { |
| 830 | val = DP83867_IO_MUX_CFG_CLK_O_DISABLE; |
| 831 | } else { |
| 832 | mask |= DP83867_IO_MUX_CFG_CLK_O_SEL_MASK; |
| 833 | val = dp83867->clk_output_sel << |
| 834 | DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT; |
| 835 | } |
| 836 | |
Heiner Kallweit | b52c018 | 2019-02-06 07:38:43 +0100 | [diff] [blame] | 837 | phy_modify_mmd(phydev, DP83867_DEVADDR, DP83867_IO_MUX_CFG, |
Trent Piepho | 13c83cf | 2019-05-22 18:43:22 +0000 | [diff] [blame] | 838 | mask, val); |
| 839 | } |
Wadim Egorov | 9708fb6 | 2018-02-14 17:07:11 +0100 | [diff] [blame] | 840 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | static int dp83867_phy_reset(struct phy_device *phydev) |
| 845 | { |
| 846 | int err; |
| 847 | |
Praneeth Bajjuri | da9ef50 | 2021-06-09 19:43:42 -0500 | [diff] [blame] | 848 | err = phy_write(phydev, DP83867_CTRL, DP83867_SW_RESTART); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 849 | if (err < 0) |
| 850 | return err; |
| 851 | |
Max Uvarov | 72a7d45 | 2019-02-25 12:15:10 +0300 | [diff] [blame] | 852 | usleep_range(10, 20); |
| 853 | |
Michael Grzeschik | 86ffe92 | 2020-01-16 14:16:31 +0100 | [diff] [blame] | 854 | return phy_modify(phydev, MII_DP83867_PHYCTRL, |
| 855 | DP83867_PHYCR_FORCE_LINK_GOOD, 0); |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static struct phy_driver dp83867_driver[] = { |
| 859 | { |
| 860 | .phy_id = DP83867_PHY_ID, |
| 861 | .phy_id_mask = 0xfffffff0, |
| 862 | .name = "TI DP83867", |
Heiner Kallweit | dcdecdc | 2019-04-12 20:47:03 +0200 | [diff] [blame] | 863 | /* PHY_GBIT_FEATURES */ |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 864 | |
Trent Piepho | 565d9d2 | 2019-05-22 18:43:27 +0000 | [diff] [blame] | 865 | .probe = dp83867_probe, |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 866 | .config_init = dp83867_config_init, |
| 867 | .soft_reset = dp83867_phy_reset, |
| 868 | |
Dan Murphy | cd26d72 | 2020-02-18 08:11:30 -0600 | [diff] [blame] | 869 | .read_status = dp83867_read_status, |
| 870 | .get_tunable = dp83867_get_tunable, |
| 871 | .set_tunable = dp83867_set_tunable, |
| 872 | |
Thomas Haemmerle | caabee5 | 2019-10-28 08:08:14 +0000 | [diff] [blame] | 873 | .get_wol = dp83867_get_wol, |
| 874 | .set_wol = dp83867_set_wol, |
| 875 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 876 | /* IRQ related */ |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 877 | .config_intr = dp83867_config_intr, |
Ioana Ciornei | 1d1ae3c | 2020-11-23 17:38:13 +0200 | [diff] [blame] | 878 | .handle_interrupt = dp83867_handle_interrupt, |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 879 | |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 880 | .suspend = genphy_suspend, |
| 881 | .resume = genphy_resume, |
Dan Murphy | 2a10154 | 2015-06-02 09:34:37 -0500 | [diff] [blame] | 882 | }, |
| 883 | }; |
| 884 | module_phy_driver(dp83867_driver); |
| 885 | |
| 886 | static struct mdio_device_id __maybe_unused dp83867_tbl[] = { |
| 887 | { DP83867_PHY_ID, 0xfffffff0 }, |
| 888 | { } |
| 889 | }; |
| 890 | |
| 891 | MODULE_DEVICE_TABLE(mdio, dp83867_tbl); |
| 892 | |
| 893 | MODULE_DESCRIPTION("Texas Instruments DP83867 PHY driver"); |
| 894 | MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com"); |
Andrew Lunn | 5f85757 | 2019-01-21 19:10:19 +0100 | [diff] [blame] | 895 | MODULE_LICENSE("GPL v2"); |