blob: 5816a06a94395beb693ee47c740989feb036a52a [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
Grygorii Strashko5a7f08c2019-10-23 17:48:45 +030098/* CFG3 bits */
99#define DP83867_CFG3_INT_OE BIT(7)
100#define DP83867_CFG3_ROBUST_AUTO_MDIX BIT(9)
101
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100102/* CFG4 bits */
103#define DP83867_CFG4_PORT_MIRROR_EN BIT(0)
104
105enum {
106 DP83867_PORT_MIRROING_KEEP,
107 DP83867_PORT_MIRROING_EN,
108 DP83867_PORT_MIRROING_DIS,
109};
110
Dan Murphy2a101542015-06-02 09:34:37 -0500111struct dp83867_private {
Trent Piepho1b9b2952019-05-22 18:43:24 +0000112 u32 rx_id_delay;
113 u32 tx_id_delay;
114 u32 fifo_depth;
Mugunthan V Ned838fe2016-10-18 16:50:18 +0530115 int io_impedance;
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100116 int port_mirroring;
Murali Karicheri37144472017-07-04 16:23:24 +0530117 bool rxctrl_strap_quirk;
Trent Piepho13c83cf2019-05-22 18:43:22 +0000118 bool set_clk_output;
119 u32 clk_output_sel;
Vitaly Gaiduk507ddd52019-09-09 20:19:24 +0300120 bool sgmii_ref_clk_en;
Dan Murphy2a101542015-06-02 09:34:37 -0500121};
122
123static int dp83867_ack_interrupt(struct phy_device *phydev)
124{
125 int err = phy_read(phydev, MII_DP83867_ISR);
126
127 if (err < 0)
128 return err;
129
130 return 0;
131}
132
133static int dp83867_config_intr(struct phy_device *phydev)
134{
135 int micr_status;
136
137 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
138 micr_status = phy_read(phydev, MII_DP83867_MICR);
139 if (micr_status < 0)
140 return micr_status;
141
142 micr_status |=
143 (MII_DP83867_MICR_AN_ERR_INT_EN |
144 MII_DP83867_MICR_SPEED_CHNG_INT_EN |
Grygorii Strashko5ca7d1c2017-01-05 14:48:07 -0600145 MII_DP83867_MICR_AUTONEG_COMP_INT_EN |
146 MII_DP83867_MICR_LINK_STS_CHNG_INT_EN |
Dan Murphy2a101542015-06-02 09:34:37 -0500147 MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN |
148 MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN);
149
150 return phy_write(phydev, MII_DP83867_MICR, micr_status);
151 }
152
153 micr_status = 0x0;
154 return phy_write(phydev, MII_DP83867_MICR, micr_status);
155}
156
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100157static int dp83867_config_port_mirroring(struct phy_device *phydev)
158{
159 struct dp83867_private *dp83867 =
160 (struct dp83867_private *)phydev->priv;
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100161
162 if (dp83867->port_mirroring == DP83867_PORT_MIRROING_EN)
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100163 phy_set_bits_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4,
164 DP83867_CFG4_PORT_MIRROR_EN);
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100165 else
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100166 phy_clear_bits_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4,
167 DP83867_CFG4_PORT_MIRROR_EN);
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100168 return 0;
169}
170
Dan Murphy2a101542015-06-02 09:34:37 -0500171#ifdef CONFIG_OF_MDIO
172static int dp83867_of_init(struct phy_device *phydev)
173{
174 struct dp83867_private *dp83867 = phydev->priv;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100175 struct device *dev = &phydev->mdio.dev;
Dan Murphy2a101542015-06-02 09:34:37 -0500176 struct device_node *of_node = dev->of_node;
177 int ret;
178
Andrew Lunn7bf9ae02015-12-07 04:38:58 +0100179 if (!of_node)
Dan Murphy2a101542015-06-02 09:34:37 -0500180 return -ENODEV;
181
Mugunthan V Ned838fe2016-10-18 16:50:18 +0530182 /* Optional configuration */
Wadim Egorov9708fb62018-02-14 17:07:11 +0100183 ret = of_property_read_u32(of_node, "ti,clk-output-sel",
184 &dp83867->clk_output_sel);
Trent Piepho13c83cf2019-05-22 18:43:22 +0000185 /* If not set, keep default */
186 if (!ret) {
187 dp83867->set_clk_output = true;
188 /* Valid values are 0 to DP83867_CLK_O_SEL_REF_CLK or
189 * DP83867_CLK_O_SEL_OFF.
Wadim Egorov9708fb62018-02-14 17:07:11 +0100190 */
Trent Piepho13c83cf2019-05-22 18:43:22 +0000191 if (dp83867->clk_output_sel > DP83867_CLK_O_SEL_REF_CLK &&
192 dp83867->clk_output_sel != DP83867_CLK_O_SEL_OFF) {
193 phydev_err(phydev, "ti,clk-output-sel value %u out of range\n",
194 dp83867->clk_output_sel);
195 return -EINVAL;
196 }
197 }
Wadim Egorov9708fb62018-02-14 17:07:11 +0100198
Mugunthan V Ned838fe2016-10-18 16:50:18 +0530199 if (of_property_read_bool(of_node, "ti,max-output-impedance"))
200 dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX;
201 else if (of_property_read_bool(of_node, "ti,min-output-impedance"))
202 dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN;
Trent Piepho27708eb2019-05-22 18:43:25 +0000203 else
204 dp83867->io_impedance = -1; /* leave at default */
Mugunthan V Ned838fe2016-10-18 16:50:18 +0530205
Murali Karicheri37144472017-07-04 16:23:24 +0530206 dp83867->rxctrl_strap_quirk = of_property_read_bool(of_node,
207 "ti,dp83867-rxctrl-strap-quirk");
208
Vitaly Gaiduk507ddd52019-09-09 20:19:24 +0300209 dp83867->sgmii_ref_clk_en = of_property_read_bool(of_node,
210 "ti,sgmii-ref-clock-output-enable");
211
Trent Piephoc11669a2019-05-22 18:43:23 +0000212 /* Existing behavior was to use default pin strapping delay in rgmii
213 * mode, but rgmii should have meant no delay. Warn existing users.
214 */
215 if (phydev->interface == PHY_INTERFACE_MODE_RGMII) {
216 const u16 val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_STRAP_STS2);
217 const u16 txskew = (val & DP83867_STRAP_STS2_CLK_SKEW_TX_MASK) >>
218 DP83867_STRAP_STS2_CLK_SKEW_TX_SHIFT;
219 const u16 rxskew = (val & DP83867_STRAP_STS2_CLK_SKEW_RX_MASK) >>
220 DP83867_STRAP_STS2_CLK_SKEW_RX_SHIFT;
Dan Murphy2a101542015-06-02 09:34:37 -0500221
Trent Piephoc11669a2019-05-22 18:43:23 +0000222 if (txskew != DP83867_STRAP_STS2_CLK_SKEW_NONE ||
223 rxskew != DP83867_STRAP_STS2_CLK_SKEW_NONE)
224 phydev_warn(phydev,
225 "PHY has delays via pin strapping, but phy-mode = 'rgmii'\n"
226 "Should be 'rgmii-id' to use internal delays\n");
227 }
228
229 /* RX delay *must* be specified if internal delay of RX is used. */
230 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
231 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
232 ret = of_property_read_u32(of_node, "ti,rx-internal-delay",
233 &dp83867->rx_id_delay);
234 if (ret) {
235 phydev_err(phydev, "ti,rx-internal-delay must be specified\n");
236 return ret;
237 }
238 if (dp83867->rx_id_delay > DP83867_RGMII_RX_CLK_DELAY_MAX) {
239 phydev_err(phydev,
240 "ti,rx-internal-delay value of %u out of range\n",
241 dp83867->rx_id_delay);
242 return -EINVAL;
243 }
244 }
245
246 /* TX delay *must* be specified if internal delay of RX is used. */
247 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
248 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
249 ret = of_property_read_u32(of_node, "ti,tx-internal-delay",
250 &dp83867->tx_id_delay);
251 if (ret) {
252 phydev_err(phydev, "ti,tx-internal-delay must be specified\n");
253 return ret;
254 }
255 if (dp83867->tx_id_delay > DP83867_RGMII_TX_CLK_DELAY_MAX) {
256 phydev_err(phydev,
257 "ti,tx-internal-delay value of %u out of range\n",
258 dp83867->tx_id_delay);
259 return -EINVAL;
260 }
261 }
Dan Murphy2a101542015-06-02 09:34:37 -0500262
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100263 if (of_property_read_bool(of_node, "enet-phy-lane-swap"))
264 dp83867->port_mirroring = DP83867_PORT_MIRROING_EN;
265
266 if (of_property_read_bool(of_node, "enet-phy-lane-no-swap"))
267 dp83867->port_mirroring = DP83867_PORT_MIRROING_DIS;
268
Trent Piephof8bbf412019-05-22 18:43:26 +0000269 ret = of_property_read_u32(of_node, "ti,fifo-depth",
Dan Murphy2a101542015-06-02 09:34:37 -0500270 &dp83867->fifo_depth);
Trent Piephof8bbf412019-05-22 18:43:26 +0000271 if (ret) {
272 phydev_err(phydev,
273 "ti,fifo-depth property is required\n");
274 return ret;
275 }
276 if (dp83867->fifo_depth > DP83867_PHYCR_FIFO_DEPTH_MAX) {
277 phydev_err(phydev,
278 "ti,fifo-depth value %u out of range\n",
279 dp83867->fifo_depth);
280 return -EINVAL;
281 }
282 return 0;
Dan Murphy2a101542015-06-02 09:34:37 -0500283}
284#else
285static int dp83867_of_init(struct phy_device *phydev)
286{
287 return 0;
288}
289#endif /* CONFIG_OF_MDIO */
290
Trent Piepho565d9d22019-05-22 18:43:27 +0000291static int dp83867_probe(struct phy_device *phydev)
Dan Murphy2a101542015-06-02 09:34:37 -0500292{
293 struct dp83867_private *dp83867;
Trent Piepho565d9d22019-05-22 18:43:27 +0000294
295 dp83867 = devm_kzalloc(&phydev->mdio.dev, sizeof(*dp83867),
296 GFP_KERNEL);
297 if (!dp83867)
298 return -ENOMEM;
299
300 phydev->priv = dp83867;
301
Grygorii Strashkoef87f7d2019-10-23 17:48:46 +0300302 return dp83867_of_init(phydev);
Trent Piepho565d9d22019-05-22 18:43:27 +0000303}
304
305static int dp83867_config_init(struct phy_device *phydev)
306{
307 struct dp83867_private *dp83867 = phydev->priv;
Lukasz Majewskiac6e0582017-02-07 06:20:24 +0100308 int ret, val, bs;
Stefan Hauserb291c412016-07-01 22:35:03 +0200309 u16 delay;
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 Strashko5a7f08c2019-10-23 17:48:45 +0300413 val = phy_read(phydev, DP83867_CFG3);
Grygorii Strashko5ca7d1c2017-01-05 14:48:07 -0600414 /* Enable Interrupt output INT_OE in CFG3 register */
Grygorii Strashko5a7f08c2019-10-23 17:48:45 +0300415 if (phy_interrupt_is_valid(phydev))
416 val |= DP83867_CFG3_INT_OE;
417
418 val |= DP83867_CFG3_ROBUST_AUTO_MDIX;
419 phy_write(phydev, DP83867_CFG3, val);
Grygorii Strashko5ca7d1c2017-01-05 14:48:07 -0600420
Lukasz Majewskifc6d39c2017-02-07 06:20:23 +0100421 if (dp83867->port_mirroring != DP83867_PORT_MIRROING_KEEP)
422 dp83867_config_port_mirroring(phydev);
423
Wadim Egorov9708fb62018-02-14 17:07:11 +0100424 /* Clock output selection if muxing property is set */
Trent Piepho13c83cf2019-05-22 18:43:22 +0000425 if (dp83867->set_clk_output) {
426 u16 mask = DP83867_IO_MUX_CFG_CLK_O_DISABLE;
427
428 if (dp83867->clk_output_sel == DP83867_CLK_O_SEL_OFF) {
429 val = DP83867_IO_MUX_CFG_CLK_O_DISABLE;
430 } else {
431 mask |= DP83867_IO_MUX_CFG_CLK_O_SEL_MASK;
432 val = dp83867->clk_output_sel <<
433 DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT;
434 }
435
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100436 phy_modify_mmd(phydev, DP83867_DEVADDR, DP83867_IO_MUX_CFG,
Trent Piepho13c83cf2019-05-22 18:43:22 +0000437 mask, val);
438 }
Wadim Egorov9708fb62018-02-14 17:07:11 +0100439
Dan Murphy2a101542015-06-02 09:34:37 -0500440 return 0;
441}
442
443static int dp83867_phy_reset(struct phy_device *phydev)
444{
445 int err;
446
447 err = phy_write(phydev, DP83867_CTRL, DP83867_SW_RESET);
448 if (err < 0)
449 return err;
450
Max Uvarov72a7d452019-02-25 12:15:10 +0300451 usleep_range(10, 20);
452
Max Uvarovc8081fc2019-05-28 13:00:51 +0300453 return 0;
Dan Murphy2a101542015-06-02 09:34:37 -0500454}
455
456static struct phy_driver dp83867_driver[] = {
457 {
458 .phy_id = DP83867_PHY_ID,
459 .phy_id_mask = 0xfffffff0,
460 .name = "TI DP83867",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200461 /* PHY_GBIT_FEATURES */
Dan Murphy2a101542015-06-02 09:34:37 -0500462
Trent Piepho565d9d22019-05-22 18:43:27 +0000463 .probe = dp83867_probe,
Dan Murphy2a101542015-06-02 09:34:37 -0500464 .config_init = dp83867_config_init,
465 .soft_reset = dp83867_phy_reset,
466
467 /* IRQ related */
468 .ack_interrupt = dp83867_ack_interrupt,
469 .config_intr = dp83867_config_intr,
470
Dan Murphy2a101542015-06-02 09:34:37 -0500471 .suspend = genphy_suspend,
472 .resume = genphy_resume,
Dan Murphy2a101542015-06-02 09:34:37 -0500473 },
474};
475module_phy_driver(dp83867_driver);
476
477static struct mdio_device_id __maybe_unused dp83867_tbl[] = {
478 { DP83867_PHY_ID, 0xfffffff0 },
479 { }
480};
481
482MODULE_DEVICE_TABLE(mdio, dp83867_tbl);
483
484MODULE_DESCRIPTION("Texas Instruments DP83867 PHY driver");
485MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com");
Andrew Lunn5f857572019-01-21 19:10:19 +0100486MODULE_LICENSE("GPL v2");