blob: 37fceaf9fa10f43a5344a162ad8e38d86e6cd204 [file] [log] [blame]
Andrew Lunn5f857572019-01-21 19:10:19 +01001// SPDX-License-Identifier: GPL-2.0
Dan Murphy2a101542015-06-02 09:34:37 -05002/*
3 * Driver for the Texas Instruments DP83867 PHY
4 *
5 * Copyright (C) 2015 Texas Instruments Inc.
Dan Murphy2a101542015-06-02 09:34:37 -05006 */
7
8#include <linux/ethtool.h>
9#include <linux/kernel.h>
10#include <linux/mii.h>
11#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/phy.h>
Max Uvarov72a7d452019-02-25 12:15:10 +030014#include <linux/delay.h>
Dan Murphy2a101542015-06-02 09:34:37 -050015
16#include <dt-bindings/net/ti-dp83867.h>
17
18#define DP83867_PHY_ID 0x2000a231
19#define DP83867_DEVADDR 0x1f
20
21#define MII_DP83867_PHYCTRL 0x10
22#define MII_DP83867_MICR 0x12
23#define MII_DP83867_ISR 0x13
24#define DP83867_CTRL 0x1f
Grygorii Strashko5ca7d1c2017-01-05 14:48:07 -060025#define DP83867_CFG3 0x1e
Dan Murphy2a101542015-06-02 09:34:37 -050026
27/* Extended Registers */
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +010028#define DP83867_CFG4 0x0031
Max Uvarov1a97a472019-05-28 13:00:50 +030029#define DP83867_CFG4_SGMII_ANEG_MASK (BIT(5) | BIT(6))
30#define DP83867_CFG4_SGMII_ANEG_TIMER_11MS (3 << 5)
31#define DP83867_CFG4_SGMII_ANEG_TIMER_800US (2 << 5)
32#define DP83867_CFG4_SGMII_ANEG_TIMER_2US (1 << 5)
33#define DP83867_CFG4_SGMII_ANEG_TIMER_16MS (0 << 5)
34
Dan Murphy2a101542015-06-02 09:34:37 -050035#define DP83867_RGMIICTL 0x0032
Lukasz Majewskiac6e0582017-02-07 06:20:24 +010036#define DP83867_STRAP_STS1 0x006E
Trent Piephoc11669a2019-05-22 18:43:23 +000037#define DP83867_STRAP_STS2 0x006f
Dan Murphy2a101542015-06-02 09:34:37 -050038#define DP83867_RGMIIDCTL 0x0086
Mugunthan V Ned838fe2016-10-18 16:50:18 +053039#define DP83867_IO_MUX_CFG 0x0170
Vitaly Gaiduk507ddd52019-09-09 20:19:24 +030040#define DP83867_SGMIICTL 0x00D3
Max Uvarov333061b2019-05-28 13:00:49 +030041#define DP83867_10M_SGMII_CFG 0x016F
42#define DP83867_10M_SGMII_RATE_ADAPT_MASK BIT(7)
Dan Murphy2a101542015-06-02 09:34:37 -050043
44#define DP83867_SW_RESET BIT(15)
45#define DP83867_SW_RESTART BIT(14)
46
47/* MICR Interrupt bits */
48#define MII_DP83867_MICR_AN_ERR_INT_EN BIT(15)
49#define MII_DP83867_MICR_SPEED_CHNG_INT_EN BIT(14)
50#define MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN BIT(13)
51#define MII_DP83867_MICR_PAGE_RXD_INT_EN BIT(12)
52#define MII_DP83867_MICR_AUTONEG_COMP_INT_EN BIT(11)
53#define MII_DP83867_MICR_LINK_STS_CHNG_INT_EN BIT(10)
54#define MII_DP83867_MICR_FALSE_CARRIER_INT_EN BIT(8)
55#define MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN BIT(4)
56#define MII_DP83867_MICR_WOL_INT_EN BIT(3)
57#define MII_DP83867_MICR_XGMII_ERR_INT_EN BIT(2)
58#define MII_DP83867_MICR_POL_CHNG_INT_EN BIT(1)
59#define MII_DP83867_MICR_JABBER_INT_EN BIT(0)
60
61/* RGMIICTL bits */
62#define DP83867_RGMII_TX_CLK_DELAY_EN BIT(1)
63#define DP83867_RGMII_RX_CLK_DELAY_EN BIT(0)
64
Vitaly Gaiduk507ddd52019-09-09 20:19:24 +030065/* SGMIICTL bits */
66#define DP83867_SGMII_TYPE BIT(14)
67
Lukasz Majewskiac6e0582017-02-07 06:20:24 +010068/* STRAP_STS1 bits */
69#define DP83867_STRAP_STS1_RESERVED BIT(11)
70
Trent Piephoc11669a2019-05-22 18:43:23 +000071/* STRAP_STS2 bits */
72#define DP83867_STRAP_STS2_CLK_SKEW_TX_MASK GENMASK(6, 4)
73#define DP83867_STRAP_STS2_CLK_SKEW_TX_SHIFT 4
74#define DP83867_STRAP_STS2_CLK_SKEW_RX_MASK GENMASK(2, 0)
75#define DP83867_STRAP_STS2_CLK_SKEW_RX_SHIFT 0
76#define DP83867_STRAP_STS2_CLK_SKEW_NONE BIT(2)
77
Dan Murphy2a101542015-06-02 09:34:37 -050078/* PHY CTRL bits */
79#define DP83867_PHYCR_FIFO_DEPTH_SHIFT 14
Trent Piephof8bbf412019-05-22 18:43:26 +000080#define DP83867_PHYCR_FIFO_DEPTH_MAX 0x03
81#define DP83867_PHYCR_FIFO_DEPTH_MASK GENMASK(15, 14)
Lukasz Majewskiac6e0582017-02-07 06:20:24 +010082#define DP83867_PHYCR_RESERVED_MASK BIT(11)
Dan Murphy2a101542015-06-02 09:34:37 -050083
84/* RGMIIDCTL bits */
Trent Piephoc11669a2019-05-22 18:43:23 +000085#define DP83867_RGMII_TX_CLK_DELAY_MAX 0xf
Dan Murphy2a101542015-06-02 09:34:37 -050086#define DP83867_RGMII_TX_CLK_DELAY_SHIFT 4
Trent Piephoc11669a2019-05-22 18:43:23 +000087#define DP83867_RGMII_RX_CLK_DELAY_MAX 0xf
88#define DP83867_RGMII_RX_CLK_DELAY_SHIFT 0
Dan Murphy2a101542015-06-02 09:34:37 -050089
Mugunthan V Ned838fe2016-10-18 16:50:18 +053090/* IO_MUX_CFG bits */
Trent Piepho27708eb2019-05-22 18:43:25 +000091#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MASK 0x1f
Mugunthan V Ned838fe2016-10-18 16:50:18 +053092#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX 0x0
93#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN 0x1f
Trent Piepho13c83cf2019-05-22 18:43:22 +000094#define DP83867_IO_MUX_CFG_CLK_O_DISABLE BIT(6)
Wadim Egorov9708fb62018-02-14 17:07:11 +010095#define DP83867_IO_MUX_CFG_CLK_O_SEL_MASK (0x1f << 8)
96#define DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT 8
Mugunthan V Ned838fe2016-10-18 16:50:18 +053097
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +010098/* CFG4 bits */
99#define DP83867_CFG4_PORT_MIRROR_EN BIT(0)
100
101enum {
102 DP83867_PORT_MIRROING_KEEP,
103 DP83867_PORT_MIRROING_EN,
104 DP83867_PORT_MIRROING_DIS,
105};
106
Dan Murphy2a101542015-06-02 09:34:37 -0500107struct dp83867_private {
Trent Piepho1b9b2952019-05-22 18:43:24 +0000108 u32 rx_id_delay;
109 u32 tx_id_delay;
110 u32 fifo_depth;
Mugunthan V Ned838fe2016-10-18 16:50:18 +0530111 int io_impedance;
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100112 int port_mirroring;
Murali Karicheri37144472017-07-04 16:23:24 +0530113 bool rxctrl_strap_quirk;
Trent Piepho13c83cf2019-05-22 18:43:22 +0000114 bool set_clk_output;
115 u32 clk_output_sel;
Vitaly Gaiduk507ddd52019-09-09 20:19:24 +0300116 bool sgmii_ref_clk_en;
Dan Murphy2a101542015-06-02 09:34:37 -0500117};
118
119static int dp83867_ack_interrupt(struct phy_device *phydev)
120{
121 int err = phy_read(phydev, MII_DP83867_ISR);
122
123 if (err < 0)
124 return err;
125
126 return 0;
127}
128
129static int dp83867_config_intr(struct phy_device *phydev)
130{
131 int micr_status;
132
133 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
134 micr_status = phy_read(phydev, MII_DP83867_MICR);
135 if (micr_status < 0)
136 return micr_status;
137
138 micr_status |=
139 (MII_DP83867_MICR_AN_ERR_INT_EN |
140 MII_DP83867_MICR_SPEED_CHNG_INT_EN |
Grygorii Strashko5ca7d1c2017-01-05 14:48:07 -0600141 MII_DP83867_MICR_AUTONEG_COMP_INT_EN |
142 MII_DP83867_MICR_LINK_STS_CHNG_INT_EN |
Dan Murphy2a101542015-06-02 09:34:37 -0500143 MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN |
144 MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN);
145
146 return phy_write(phydev, MII_DP83867_MICR, micr_status);
147 }
148
149 micr_status = 0x0;
150 return phy_write(phydev, MII_DP83867_MICR, micr_status);
151}
152
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100153static int dp83867_config_port_mirroring(struct phy_device *phydev)
154{
155 struct dp83867_private *dp83867 =
156 (struct dp83867_private *)phydev->priv;
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100157
158 if (dp83867->port_mirroring == DP83867_PORT_MIRROING_EN)
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100159 phy_set_bits_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4,
160 DP83867_CFG4_PORT_MIRROR_EN);
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100161 else
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100162 phy_clear_bits_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4,
163 DP83867_CFG4_PORT_MIRROR_EN);
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100164 return 0;
165}
166
Dan Murphy2a101542015-06-02 09:34:37 -0500167#ifdef CONFIG_OF_MDIO
168static int dp83867_of_init(struct phy_device *phydev)
169{
170 struct dp83867_private *dp83867 = phydev->priv;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100171 struct device *dev = &phydev->mdio.dev;
Dan Murphy2a101542015-06-02 09:34:37 -0500172 struct device_node *of_node = dev->of_node;
173 int ret;
174
Andrew Lunn7bf9ae02015-12-07 04:38:58 +0100175 if (!of_node)
Dan Murphy2a101542015-06-02 09:34:37 -0500176 return -ENODEV;
177
Mugunthan V Ned838fe2016-10-18 16:50:18 +0530178 /* Optional configuration */
Wadim Egorov9708fb62018-02-14 17:07:11 +0100179 ret = of_property_read_u32(of_node, "ti,clk-output-sel",
180 &dp83867->clk_output_sel);
Trent Piepho13c83cf2019-05-22 18:43:22 +0000181 /* If not set, keep default */
182 if (!ret) {
183 dp83867->set_clk_output = true;
184 /* Valid values are 0 to DP83867_CLK_O_SEL_REF_CLK or
185 * DP83867_CLK_O_SEL_OFF.
Wadim Egorov9708fb62018-02-14 17:07:11 +0100186 */
Trent Piepho13c83cf2019-05-22 18:43:22 +0000187 if (dp83867->clk_output_sel > DP83867_CLK_O_SEL_REF_CLK &&
188 dp83867->clk_output_sel != DP83867_CLK_O_SEL_OFF) {
189 phydev_err(phydev, "ti,clk-output-sel value %u out of range\n",
190 dp83867->clk_output_sel);
191 return -EINVAL;
192 }
193 }
Wadim Egorov9708fb62018-02-14 17:07:11 +0100194
Mugunthan V Ned838fe2016-10-18 16:50:18 +0530195 if (of_property_read_bool(of_node, "ti,max-output-impedance"))
196 dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX;
197 else if (of_property_read_bool(of_node, "ti,min-output-impedance"))
198 dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN;
Trent Piepho27708eb2019-05-22 18:43:25 +0000199 else
200 dp83867->io_impedance = -1; /* leave at default */
Mugunthan V Ned838fe2016-10-18 16:50:18 +0530201
Murali Karicheri37144472017-07-04 16:23:24 +0530202 dp83867->rxctrl_strap_quirk = of_property_read_bool(of_node,
203 "ti,dp83867-rxctrl-strap-quirk");
204
Vitaly Gaiduk507ddd52019-09-09 20:19:24 +0300205 dp83867->sgmii_ref_clk_en = of_property_read_bool(of_node,
206 "ti,sgmii-ref-clock-output-enable");
207
Trent Piephoc11669a2019-05-22 18:43:23 +0000208 /* Existing behavior was to use default pin strapping delay in rgmii
209 * mode, but rgmii should have meant no delay. Warn existing users.
210 */
211 if (phydev->interface == PHY_INTERFACE_MODE_RGMII) {
212 const u16 val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_STRAP_STS2);
213 const u16 txskew = (val & DP83867_STRAP_STS2_CLK_SKEW_TX_MASK) >>
214 DP83867_STRAP_STS2_CLK_SKEW_TX_SHIFT;
215 const u16 rxskew = (val & DP83867_STRAP_STS2_CLK_SKEW_RX_MASK) >>
216 DP83867_STRAP_STS2_CLK_SKEW_RX_SHIFT;
Dan Murphy2a101542015-06-02 09:34:37 -0500217
Trent Piephoc11669a2019-05-22 18:43:23 +0000218 if (txskew != DP83867_STRAP_STS2_CLK_SKEW_NONE ||
219 rxskew != DP83867_STRAP_STS2_CLK_SKEW_NONE)
220 phydev_warn(phydev,
221 "PHY has delays via pin strapping, but phy-mode = 'rgmii'\n"
222 "Should be 'rgmii-id' to use internal delays\n");
223 }
224
225 /* RX delay *must* be specified if internal delay of RX is used. */
226 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
227 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
228 ret = of_property_read_u32(of_node, "ti,rx-internal-delay",
229 &dp83867->rx_id_delay);
230 if (ret) {
231 phydev_err(phydev, "ti,rx-internal-delay must be specified\n");
232 return ret;
233 }
234 if (dp83867->rx_id_delay > DP83867_RGMII_RX_CLK_DELAY_MAX) {
235 phydev_err(phydev,
236 "ti,rx-internal-delay value of %u out of range\n",
237 dp83867->rx_id_delay);
238 return -EINVAL;
239 }
240 }
241
242 /* TX delay *must* be specified if internal delay of RX is used. */
243 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
244 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
245 ret = of_property_read_u32(of_node, "ti,tx-internal-delay",
246 &dp83867->tx_id_delay);
247 if (ret) {
248 phydev_err(phydev, "ti,tx-internal-delay must be specified\n");
249 return ret;
250 }
251 if (dp83867->tx_id_delay > DP83867_RGMII_TX_CLK_DELAY_MAX) {
252 phydev_err(phydev,
253 "ti,tx-internal-delay value of %u out of range\n",
254 dp83867->tx_id_delay);
255 return -EINVAL;
256 }
257 }
Dan Murphy2a101542015-06-02 09:34:37 -0500258
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100259 if (of_property_read_bool(of_node, "enet-phy-lane-swap"))
260 dp83867->port_mirroring = DP83867_PORT_MIRROING_EN;
261
262 if (of_property_read_bool(of_node, "enet-phy-lane-no-swap"))
263 dp83867->port_mirroring = DP83867_PORT_MIRROING_DIS;
264
Trent Piephof8bbf412019-05-22 18:43:26 +0000265 ret = of_property_read_u32(of_node, "ti,fifo-depth",
Dan Murphy2a101542015-06-02 09:34:37 -0500266 &dp83867->fifo_depth);
Trent Piephof8bbf412019-05-22 18:43:26 +0000267 if (ret) {
268 phydev_err(phydev,
269 "ti,fifo-depth property is required\n");
270 return ret;
271 }
272 if (dp83867->fifo_depth > DP83867_PHYCR_FIFO_DEPTH_MAX) {
273 phydev_err(phydev,
274 "ti,fifo-depth value %u out of range\n",
275 dp83867->fifo_depth);
276 return -EINVAL;
277 }
278 return 0;
Dan Murphy2a101542015-06-02 09:34:37 -0500279}
280#else
281static int dp83867_of_init(struct phy_device *phydev)
282{
283 return 0;
284}
285#endif /* CONFIG_OF_MDIO */
286
Trent Piepho565d9d22019-05-22 18:43:27 +0000287static int dp83867_probe(struct phy_device *phydev)
Dan Murphy2a101542015-06-02 09:34:37 -0500288{
289 struct dp83867_private *dp83867;
Trent Piepho565d9d22019-05-22 18:43:27 +0000290
291 dp83867 = devm_kzalloc(&phydev->mdio.dev, sizeof(*dp83867),
292 GFP_KERNEL);
293 if (!dp83867)
294 return -ENOMEM;
295
296 phydev->priv = dp83867;
297
298 return 0;
299}
300
301static int dp83867_config_init(struct phy_device *phydev)
302{
303 struct dp83867_private *dp83867 = phydev->priv;
Lukasz Majewskiac6e0582017-02-07 06:20:24 +0100304 int ret, val, bs;
Stefan Hauserb291c412016-07-01 22:35:03 +0200305 u16 delay;
Dan Murphy2a101542015-06-02 09:34:37 -0500306
Trent Piepho565d9d22019-05-22 18:43:27 +0000307 ret = dp83867_of_init(phydev);
308 if (ret)
309 return ret;
Dan Murphy2a101542015-06-02 09:34:37 -0500310
Murali Karicheri37144472017-07-04 16:23:24 +0530311 /* RX_DV/RX_CTRL strapped in mode 1 or mode 2 workaround */
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100312 if (dp83867->rxctrl_strap_quirk)
313 phy_clear_bits_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4,
314 BIT(7));
Murali Karicheri37144472017-07-04 16:23:24 +0530315
Dan Murphy2a101542015-06-02 09:34:37 -0500316 if (phy_interface_is_rgmii(phydev)) {
Stefan Hauserb291c412016-07-01 22:35:03 +0200317 val = phy_read(phydev, MII_DP83867_PHYCTRL);
318 if (val < 0)
319 return val;
320 val &= ~DP83867_PHYCR_FIFO_DEPTH_MASK;
321 val |= (dp83867->fifo_depth << DP83867_PHYCR_FIFO_DEPTH_SHIFT);
Lukasz Majewskiac6e0582017-02-07 06:20:24 +0100322
323 /* The code below checks if "port mirroring" N/A MODE4 has been
324 * enabled during power on bootstrap.
325 *
326 * Such N/A mode enabled by mistake can put PHY IC in some
327 * internal testing mode and disable RGMII transmission.
328 *
329 * In this particular case one needs to check STRAP_STS1
330 * register's bit 11 (marked as RESERVED).
331 */
332
Russell Kinga6d99fc2017-03-21 16:36:53 +0000333 bs = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_STRAP_STS1);
Lukasz Majewskiac6e0582017-02-07 06:20:24 +0100334 if (bs & DP83867_STRAP_STS1_RESERVED)
335 val &= ~DP83867_PHYCR_RESERVED_MASK;
336
Stefan Hauserb291c412016-07-01 22:35:03 +0200337 ret = phy_write(phydev, MII_DP83867_PHYCTRL, val);
Dan Murphy2a101542015-06-02 09:34:37 -0500338 if (ret)
339 return ret;
Dan Murphy2a101542015-06-02 09:34:37 -0500340
David S. Millerb4b12b02019-05-31 10:49:43 -0700341 /* If rgmii mode with no internal delay is selected, we do NOT use
342 * aligned mode as one might expect. Instead we use the PHY's default
343 * based on pin strapping. And the "mode 0" default is to *use*
344 * internal delay with a value of 7 (2.00 ns).
345 *
346 * Set up RGMII delays
347 */
Russell Kinga6d99fc2017-03-21 16:36:53 +0000348 val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIICTL);
Dan Murphy2a101542015-06-02 09:34:37 -0500349
Trent Piephoc11669a2019-05-22 18:43:23 +0000350 val &= ~(DP83867_RGMII_TX_CLK_DELAY_EN | DP83867_RGMII_RX_CLK_DELAY_EN);
Dan Murphy2a101542015-06-02 09:34:37 -0500351 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
352 val |= (DP83867_RGMII_TX_CLK_DELAY_EN | DP83867_RGMII_RX_CLK_DELAY_EN);
353
354 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
355 val |= DP83867_RGMII_TX_CLK_DELAY_EN;
356
357 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
358 val |= DP83867_RGMII_RX_CLK_DELAY_EN;
359
Russell Kinga6d99fc2017-03-21 16:36:53 +0000360 phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIICTL, val);
Dan Murphy2a101542015-06-02 09:34:37 -0500361
362 delay = (dp83867->rx_id_delay |
363 (dp83867->tx_id_delay << DP83867_RGMII_TX_CLK_DELAY_SHIFT));
364
Russell Kinga6d99fc2017-03-21 16:36:53 +0000365 phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIIDCTL,
366 delay);
Dan Murphy2a101542015-06-02 09:34:37 -0500367 }
368
Trent Piepho27708eb2019-05-22 18:43:25 +0000369 /* If specified, set io impedance */
370 if (dp83867->io_impedance >= 0)
371 phy_modify_mmd(phydev, DP83867_DEVADDR, DP83867_IO_MUX_CFG,
372 DP83867_IO_MUX_CFG_IO_IMPEDANCE_MASK,
373 dp83867->io_impedance);
374
Max Uvarov333061b2019-05-28 13:00:49 +0300375 if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
376 /* For support SPEED_10 in SGMII mode
377 * DP83867_10M_SGMII_RATE_ADAPT bit
378 * has to be cleared by software. That
379 * does not affect SPEED_100 and
380 * SPEED_1000.
381 */
382 ret = phy_modify_mmd(phydev, DP83867_DEVADDR,
383 DP83867_10M_SGMII_CFG,
384 DP83867_10M_SGMII_RATE_ADAPT_MASK,
385 0);
386 if (ret)
387 return ret;
Max Uvarov1a97a472019-05-28 13:00:50 +0300388
389 /* After reset SGMII Autoneg timer is set to 2us (bits 6 and 5
390 * are 01). That is not enough to finalize autoneg on some
391 * devices. Increase this timer duration to maximum 16ms.
392 */
393 ret = phy_modify_mmd(phydev, DP83867_DEVADDR,
394 DP83867_CFG4,
395 DP83867_CFG4_SGMII_ANEG_MASK,
396 DP83867_CFG4_SGMII_ANEG_TIMER_16MS);
397
398 if (ret)
399 return ret;
Vitaly Gaiduk507ddd52019-09-09 20:19:24 +0300400
401 val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_SGMIICTL);
402 /* SGMII type is set to 4-wire mode by default.
403 * If we place appropriate property in dts (see above)
404 * switch on 6-wire mode.
405 */
406 if (dp83867->sgmii_ref_clk_en)
407 val |= DP83867_SGMII_TYPE;
408 else
409 val &= ~DP83867_SGMII_TYPE;
410 phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_SGMIICTL, val);
Max Uvarov333061b2019-05-28 13:00:49 +0300411 }
412
Grygorii Strashko5ca7d1c2017-01-05 14:48:07 -0600413 /* Enable Interrupt output INT_OE in CFG3 register */
414 if (phy_interrupt_is_valid(phydev)) {
415 val = phy_read(phydev, DP83867_CFG3);
416 val |= BIT(7);
417 phy_write(phydev, DP83867_CFG3, val);
418 }
419
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100420 if (dp83867->port_mirroring != DP83867_PORT_MIRROING_KEEP)
421 dp83867_config_port_mirroring(phydev);
422
Wadim Egorov9708fb62018-02-14 17:07:11 +0100423 /* Clock output selection if muxing property is set */
Trent Piepho13c83cf2019-05-22 18:43:22 +0000424 if (dp83867->set_clk_output) {
425 u16 mask = DP83867_IO_MUX_CFG_CLK_O_DISABLE;
426
427 if (dp83867->clk_output_sel == DP83867_CLK_O_SEL_OFF) {
428 val = DP83867_IO_MUX_CFG_CLK_O_DISABLE;
429 } else {
430 mask |= DP83867_IO_MUX_CFG_CLK_O_SEL_MASK;
431 val = dp83867->clk_output_sel <<
432 DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT;
433 }
434
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100435 phy_modify_mmd(phydev, DP83867_DEVADDR, DP83867_IO_MUX_CFG,
Trent Piepho13c83cf2019-05-22 18:43:22 +0000436 mask, val);
437 }
Wadim Egorov9708fb62018-02-14 17:07:11 +0100438
Dan Murphy2a101542015-06-02 09:34:37 -0500439 return 0;
440}
441
442static int dp83867_phy_reset(struct phy_device *phydev)
443{
444 int err;
445
446 err = phy_write(phydev, DP83867_CTRL, DP83867_SW_RESET);
447 if (err < 0)
448 return err;
449
Max Uvarov72a7d452019-02-25 12:15:10 +0300450 usleep_range(10, 20);
451
Max Uvarovc8081fc2019-05-28 13:00:51 +0300452 return 0;
Dan Murphy2a101542015-06-02 09:34:37 -0500453}
454
455static struct phy_driver dp83867_driver[] = {
456 {
457 .phy_id = DP83867_PHY_ID,
458 .phy_id_mask = 0xfffffff0,
459 .name = "TI DP83867",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200460 /* PHY_GBIT_FEATURES */
Dan Murphy2a101542015-06-02 09:34:37 -0500461
Trent Piepho565d9d22019-05-22 18:43:27 +0000462 .probe = dp83867_probe,
Dan Murphy2a101542015-06-02 09:34:37 -0500463 .config_init = dp83867_config_init,
464 .soft_reset = dp83867_phy_reset,
465
466 /* IRQ related */
467 .ack_interrupt = dp83867_ack_interrupt,
468 .config_intr = dp83867_config_intr,
469
Dan Murphy2a101542015-06-02 09:34:37 -0500470 .suspend = genphy_suspend,
471 .resume = genphy_resume,
Dan Murphy2a101542015-06-02 09:34:37 -0500472 },
473};
474module_phy_driver(dp83867_driver);
475
476static struct mdio_device_id __maybe_unused dp83867_tbl[] = {
477 { DP83867_PHY_ID, 0xfffffff0 },
478 { }
479};
480
481MODULE_DEVICE_TABLE(mdio, dp83867_tbl);
482
483MODULE_DESCRIPTION("Texas Instruments DP83867 PHY driver");
484MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com");
Andrew Lunn5f857572019-01-21 19:10:19 +0100485MODULE_LICENSE("GPL v2");