blob: 25f1332ca98b6ef0328a3b1dcecd07779f132977 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -07002/*
3 * TI PHY drivers
4 *
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -07005 */
6#include <common.h>
7#include <phy.h>
Dan Murphy83fbd0a2016-05-02 15:45:59 -05008#include <linux/compat.h>
9#include <malloc.h>
10
Dan Murphy83fbd0a2016-05-02 15:45:59 -050011#include <dm.h>
12#include <dt-bindings/net/ti-dp83867.h>
13
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -070014
15/* TI DP83867 */
16#define DP83867_DEVADDR 0x1f
17
18#define MII_DP83867_PHYCTRL 0x10
19#define MII_DP83867_MICR 0x12
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +053020#define MII_DP83867_CFG2 0x14
21#define MII_DP83867_BISCR 0x16
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -070022#define DP83867_CTRL 0x1f
23
24/* Extended Registers */
Murali Karicheri9b050762018-06-28 14:26:34 -050025#define DP83867_CFG4 0x0031
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -070026#define DP83867_RGMIICTL 0x0032
Janine Hagemannb3dd2ed2018-08-28 08:25:38 +020027#define DP83867_STRAP_STS1 0x006E
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -070028#define DP83867_RGMIIDCTL 0x0086
Mugunthan V N5b6b18e2017-01-24 11:15:40 -060029#define DP83867_IO_MUX_CFG 0x0170
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -070030
31#define DP83867_SW_RESET BIT(15)
32#define DP83867_SW_RESTART BIT(14)
33
34/* MICR Interrupt bits */
35#define MII_DP83867_MICR_AN_ERR_INT_EN BIT(15)
36#define MII_DP83867_MICR_SPEED_CHNG_INT_EN BIT(14)
37#define MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN BIT(13)
38#define MII_DP83867_MICR_PAGE_RXD_INT_EN BIT(12)
39#define MII_DP83867_MICR_AUTONEG_COMP_INT_EN BIT(11)
40#define MII_DP83867_MICR_LINK_STS_CHNG_INT_EN BIT(10)
41#define MII_DP83867_MICR_FALSE_CARRIER_INT_EN BIT(8)
42#define MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN BIT(4)
43#define MII_DP83867_MICR_WOL_INT_EN BIT(3)
44#define MII_DP83867_MICR_XGMII_ERR_INT_EN BIT(2)
45#define MII_DP83867_MICR_POL_CHNG_INT_EN BIT(1)
46#define MII_DP83867_MICR_JABBER_INT_EN BIT(0)
47
48/* RGMIICTL bits */
49#define DP83867_RGMII_TX_CLK_DELAY_EN BIT(1)
50#define DP83867_RGMII_RX_CLK_DELAY_EN BIT(0)
51
Janine Hagemannb3dd2ed2018-08-28 08:25:38 +020052/* STRAP_STS1 bits */
53#define DP83867_STRAP_STS1_RESERVED BIT(11)
54
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -070055/* PHY CTRL bits */
56#define DP83867_PHYCR_FIFO_DEPTH_SHIFT 14
Janine Hagemannb3dd2ed2018-08-28 08:25:38 +020057#define DP83867_PHYCR_RESERVED_MASK BIT(11)
Michal Simekf6459152015-10-19 10:43:30 +020058#define DP83867_MDI_CROSSOVER 5
59#define DP83867_MDI_CROSSOVER_AUTO 2
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +053060#define DP83867_MDI_CROSSOVER_MDIX 2
61#define DP83867_PHYCTRL_SGMIIEN 0x0800
62#define DP83867_PHYCTRL_RXFIFO_SHIFT 12
63#define DP83867_PHYCTRL_TXFIFO_SHIFT 14
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -070064
65/* RGMIIDCTL bits */
66#define DP83867_RGMII_TX_CLK_DELAY_SHIFT 4
67
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +053068/* CFG2 bits */
69#define MII_DP83867_CFG2_SPEEDOPT_10EN 0x0040
70#define MII_DP83867_CFG2_SGMII_AUTONEGEN 0x0080
71#define MII_DP83867_CFG2_SPEEDOPT_ENH 0x0100
72#define MII_DP83867_CFG2_SPEEDOPT_CNT 0x0800
73#define MII_DP83867_CFG2_SPEEDOPT_INTLOW 0x2000
74#define MII_DP83867_CFG2_MASK 0x003F
75
Dan Murphy83fbd0a2016-05-02 15:45:59 -050076/* User setting - can be taken from DTS */
77#define DEFAULT_RX_ID_DELAY DP83867_RGMIIDCTL_2_25_NS
78#define DEFAULT_TX_ID_DELAY DP83867_RGMIIDCTL_2_75_NS
79#define DEFAULT_FIFO_DEPTH DP83867_PHYCR_FIFO_DEPTH_4_B_NIB
80
Mugunthan V N5b6b18e2017-01-24 11:15:40 -060081/* IO_MUX_CFG bits */
82#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL 0x1f
83
84#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX 0x0
85#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN 0x1f
Janine Hagemann1c2ba092018-08-28 08:25:39 +020086#define DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT 8
87#define DP83867_IO_MUX_CFG_CLK_O_SEL_MASK \
88 GENMASK(0x1f, DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT)
Mugunthan V N5b6b18e2017-01-24 11:15:40 -060089
Janine Hagemanned51bfc2018-08-28 08:25:37 +020090/* CFG4 bits */
91#define DP83867_CFG4_PORT_MIRROR_EN BIT(0)
92
93enum {
94 DP83867_PORT_MIRRORING_KEEP,
95 DP83867_PORT_MIRRORING_EN,
96 DP83867_PORT_MIRRORING_DIS,
97};
98
Dan Murphy83fbd0a2016-05-02 15:45:59 -050099struct dp83867_private {
100 int rx_id_delay;
101 int tx_id_delay;
102 int fifo_depth;
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600103 int io_impedance;
Murali Karicheri9b050762018-06-28 14:26:34 -0500104 bool rxctrl_strap_quirk;
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200105 int port_mirroring;
Janine Hagemann1c2ba092018-08-28 08:25:39 +0200106 int clk_output_sel;
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500107};
108
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200109static int dp83867_config_port_mirroring(struct phy_device *phydev)
110{
111 struct dp83867_private *dp83867 =
112 (struct dp83867_private *)phydev->priv;
113 u16 val;
114
Carlo Caionea8abcff2019-02-08 17:25:07 +0000115 val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4);
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200116
117 if (dp83867->port_mirroring == DP83867_PORT_MIRRORING_EN)
118 val |= DP83867_CFG4_PORT_MIRROR_EN;
119 else
120 val &= ~DP83867_CFG4_PORT_MIRROR_EN;
121
Carlo Caionea8abcff2019-02-08 17:25:07 +0000122 phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4, val);
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200123
124 return 0;
125}
126
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500127#if defined(CONFIG_DM_ETH)
128/**
129 * dp83867_data_init - Convenience function for setting PHY specific data
130 *
131 * @phydev: the phy_device struct
132 */
133static int dp83867_of_init(struct phy_device *phydev)
134{
135 struct dp83867_private *dp83867 = phydev->priv;
Grygorii Strashko00e2c242018-07-05 12:02:49 -0500136 ofnode node;
Janine Hagemann1c2ba092018-08-28 08:25:39 +0200137 u16 val;
138
139 /* Optional configuration */
140
Michal Simek8e102a82019-03-16 12:43:17 +0100141 node = phy_get_ofnode(phydev);
142 if (!ofnode_valid(node))
143 return -EINVAL;
144
Janine Hagemann1c2ba092018-08-28 08:25:39 +0200145 /*
146 * Keep the default value if ti,clk-output-sel is not set
147 * or to high
148 */
149
150 dp83867->clk_output_sel =
151 ofnode_read_u32_default(node, "ti,clk-output-sel",
152 DP83867_CLK_O_SEL_REF_CLK);
Grygorii Strashko00e2c242018-07-05 12:02:49 -0500153
Grygorii Strashko9df35052018-06-28 14:26:35 -0500154 if (ofnode_read_bool(node, "ti,max-output-impedance"))
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600155 dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX;
Grygorii Strashko9df35052018-06-28 14:26:35 -0500156 else if (ofnode_read_bool(node, "ti,min-output-impedance"))
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600157 dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN;
158 else
159 dp83867->io_impedance = -EINVAL;
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500160
Grygorii Strashko9df35052018-06-28 14:26:35 -0500161 if (ofnode_read_bool(node, "ti,dp83867-rxctrl-strap-quirk"))
Murali Karicheri9b050762018-06-28 14:26:34 -0500162 dp83867->rxctrl_strap_quirk = true;
Grygorii Strashko9df35052018-06-28 14:26:35 -0500163 dp83867->rx_id_delay = ofnode_read_u32_default(node,
164 "ti,rx-internal-delay",
165 -1);
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500166
Grygorii Strashko9df35052018-06-28 14:26:35 -0500167 dp83867->tx_id_delay = ofnode_read_u32_default(node,
168 "ti,tx-internal-delay",
169 -1);
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500170
Grygorii Strashko9df35052018-06-28 14:26:35 -0500171 dp83867->fifo_depth = ofnode_read_u32_default(node, "ti,fifo-depth",
172 -1);
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200173 if (ofnode_read_bool(node, "enet-phy-lane-swap"))
174 dp83867->port_mirroring = DP83867_PORT_MIRRORING_EN;
175
176 if (ofnode_read_bool(node, "enet-phy-lane-no-swap"))
177 dp83867->port_mirroring = DP83867_PORT_MIRRORING_DIS;
178
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500179
Janine Hagemann1c2ba092018-08-28 08:25:39 +0200180 /* Clock output selection if muxing property is set */
181 if (dp83867->clk_output_sel != DP83867_CLK_O_SEL_REF_CLK) {
Carlo Caionea8abcff2019-02-08 17:25:07 +0000182 val = phy_read_mmd(phydev, DP83867_DEVADDR,
183 DP83867_IO_MUX_CFG);
Janine Hagemann1c2ba092018-08-28 08:25:39 +0200184 val &= ~DP83867_IO_MUX_CFG_CLK_O_SEL_MASK;
185 val |= (dp83867->clk_output_sel <<
186 DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT);
Carlo Caionea8abcff2019-02-08 17:25:07 +0000187 phy_write_mmd(phydev, DP83867_DEVADDR,
188 DP83867_IO_MUX_CFG, val);
Janine Hagemann1c2ba092018-08-28 08:25:39 +0200189 }
190
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500191 return 0;
192}
193#else
194static int dp83867_of_init(struct phy_device *phydev)
195{
196 struct dp83867_private *dp83867 = phydev->priv;
197
198 dp83867->rx_id_delay = DEFAULT_RX_ID_DELAY;
199 dp83867->tx_id_delay = DEFAULT_TX_ID_DELAY;
200 dp83867->fifo_depth = DEFAULT_FIFO_DEPTH;
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600201 dp83867->io_impedance = -EINVAL;
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500202
203 return 0;
204}
205#endif
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700206
207static int dp83867_config(struct phy_device *phydev)
208{
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500209 struct dp83867_private *dp83867;
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +0530210 unsigned int val, delay, cfg2;
Janine Hagemannb3dd2ed2018-08-28 08:25:38 +0200211 int ret, bs;
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700212
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500213 if (!phydev->priv) {
214 dp83867 = kzalloc(sizeof(*dp83867), GFP_KERNEL);
215 if (!dp83867)
216 return -ENOMEM;
217
218 phydev->priv = dp83867;
219 ret = dp83867_of_init(phydev);
220 if (ret)
221 goto err_out;
222 } else {
223 dp83867 = (struct dp83867_private *)phydev->priv;
224 }
225
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700226 /* Restart the PHY. */
227 val = phy_read(phydev, MDIO_DEVAD_NONE, DP83867_CTRL);
228 phy_write(phydev, MDIO_DEVAD_NONE, DP83867_CTRL,
229 val | DP83867_SW_RESTART);
230
Murali Karicheri9b050762018-06-28 14:26:34 -0500231 /* Mode 1 or 2 workaround */
232 if (dp83867->rxctrl_strap_quirk) {
Carlo Caionea8abcff2019-02-08 17:25:07 +0000233 val = phy_read_mmd(phydev, DP83867_DEVADDR,
234 DP83867_CFG4);
Murali Karicheri9b050762018-06-28 14:26:34 -0500235 val &= ~BIT(7);
Carlo Caionea8abcff2019-02-08 17:25:07 +0000236 phy_write_mmd(phydev, DP83867_DEVADDR,
237 DP83867_CFG4, val);
Murali Karicheri9b050762018-06-28 14:26:34 -0500238 }
239
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700240 if (phy_interface_is_rgmii(phydev)) {
241 ret = phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_PHYCTRL,
Michal Simekf6459152015-10-19 10:43:30 +0200242 (DP83867_MDI_CROSSOVER_AUTO << DP83867_MDI_CROSSOVER) |
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500243 (dp83867->fifo_depth << DP83867_PHYCR_FIFO_DEPTH_SHIFT));
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700244 if (ret)
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500245 goto err_out;
Janine Hagemannb3dd2ed2018-08-28 08:25:38 +0200246
247 /* The code below checks if "port mirroring" N/A MODE4 has been
248 * enabled during power on bootstrap.
249 *
250 * Such N/A mode enabled by mistake can put PHY IC in some
251 * internal testing mode and disable RGMII transmission.
252 *
253 * In this particular case one needs to check STRAP_STS1
254 * register's bit 11 (marked as RESERVED).
255 */
256
Carlo Caionea8abcff2019-02-08 17:25:07 +0000257 bs = phy_read_mmd(phydev, DP83867_DEVADDR,
258 DP83867_STRAP_STS1);
Janine Hagemannb3dd2ed2018-08-28 08:25:38 +0200259 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_DP83867_PHYCTRL);
260 if (bs & DP83867_STRAP_STS1_RESERVED) {
261 val &= ~DP83867_PHYCR_RESERVED_MASK;
262 phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_PHYCTRL,
263 val);
264 }
265
Dan Murphy4f4e9622016-05-02 15:46:02 -0500266 } else if (phy_interface_is_sgmii(phydev)) {
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +0530267 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR,
268 (BMCR_ANENABLE | BMCR_FULLDPLX | BMCR_SPEED1000));
269
270 cfg2 = phy_read(phydev, phydev->addr, MII_DP83867_CFG2);
271 cfg2 &= MII_DP83867_CFG2_MASK;
272 cfg2 |= (MII_DP83867_CFG2_SPEEDOPT_10EN |
273 MII_DP83867_CFG2_SGMII_AUTONEGEN |
274 MII_DP83867_CFG2_SPEEDOPT_ENH |
275 MII_DP83867_CFG2_SPEEDOPT_CNT |
276 MII_DP83867_CFG2_SPEEDOPT_INTLOW);
277 phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_CFG2, cfg2);
278
Carlo Caionea8abcff2019-02-08 17:25:07 +0000279 phy_write_mmd(phydev, DP83867_DEVADDR,
280 DP83867_RGMIICTL, 0x0);
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +0530281
282 phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_PHYCTRL,
283 DP83867_PHYCTRL_SGMIIEN |
284 (DP83867_MDI_CROSSOVER_MDIX <<
285 DP83867_MDI_CROSSOVER) |
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500286 (dp83867->fifo_depth << DP83867_PHYCTRL_RXFIFO_SHIFT) |
287 (dp83867->fifo_depth << DP83867_PHYCTRL_TXFIFO_SHIFT));
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +0530288 phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_BISCR, 0x0);
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700289 }
290
Phil Edworthyc6119802016-12-09 10:46:02 +0000291 if (phy_interface_is_rgmii(phydev)) {
Carlo Caionea8abcff2019-02-08 17:25:07 +0000292 val = phy_read_mmd(phydev, DP83867_DEVADDR,
293 DP83867_RGMIICTL);
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700294
295 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
296 val |= (DP83867_RGMII_TX_CLK_DELAY_EN |
297 DP83867_RGMII_RX_CLK_DELAY_EN);
298
299 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
300 val |= DP83867_RGMII_TX_CLK_DELAY_EN;
301
302 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
303 val |= DP83867_RGMII_RX_CLK_DELAY_EN;
304
Carlo Caionea8abcff2019-02-08 17:25:07 +0000305 phy_write_mmd(phydev, DP83867_DEVADDR,
306 DP83867_RGMIICTL, val);
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700307
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500308 delay = (dp83867->rx_id_delay |
309 (dp83867->tx_id_delay << DP83867_RGMII_TX_CLK_DELAY_SHIFT));
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700310
Carlo Caionea8abcff2019-02-08 17:25:07 +0000311 phy_write_mmd(phydev, DP83867_DEVADDR,
312 DP83867_RGMIIDCTL, delay);
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600313
314 if (dp83867->io_impedance >= 0) {
Carlo Caionea8abcff2019-02-08 17:25:07 +0000315 val = phy_read_mmd(phydev,
316 DP83867_DEVADDR,
317 DP83867_IO_MUX_CFG);
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600318 val &= ~DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL;
319 val |= dp83867->io_impedance &
320 DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL;
Carlo Caionea8abcff2019-02-08 17:25:07 +0000321 phy_write_mmd(phydev, DP83867_DEVADDR,
322 DP83867_IO_MUX_CFG, val);
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600323 }
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700324 }
325
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200326 if (dp83867->port_mirroring != DP83867_PORT_MIRRORING_KEEP)
327 dp83867_config_port_mirroring(phydev);
328
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700329 genphy_config_aneg(phydev);
330 return 0;
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500331
332err_out:
333 kfree(dp83867);
334 return ret;
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700335}
336
337static struct phy_driver DP83867_driver = {
338 .name = "TI DP83867",
339 .uid = 0x2000a231,
340 .mask = 0xfffffff0,
341 .features = PHY_GBIT_FEATURES,
342 .config = &dp83867_config,
343 .startup = &genphy_startup,
344 .shutdown = &genphy_shutdown,
345};
346
347int phy_ti_init(void)
348{
349 phy_register(&DP83867_driver);
350 return 0;
351}