blob: e27ee32bd131f3e6d8b117a7233a1ae0b330304a [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
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -070076#define MII_MMD_CTRL 0x0d /* MMD Access Control Register */
77#define MII_MMD_DATA 0x0e /* MMD Access Data Register */
78
79/* MMD Access Control register fields */
80#define MII_MMD_CTRL_DEVAD_MASK 0x1f /* Mask MMD DEVAD*/
81#define MII_MMD_CTRL_ADDR 0x0000 /* Address */
82#define MII_MMD_CTRL_NOINCR 0x4000 /* no post increment */
83#define MII_MMD_CTRL_INCR_RDWT 0x8000 /* post increment on reads & writes */
84#define MII_MMD_CTRL_INCR_ON_WT 0xC000 /* post increment on writes only */
85
Dan Murphy83fbd0a2016-05-02 15:45:59 -050086/* User setting - can be taken from DTS */
87#define DEFAULT_RX_ID_DELAY DP83867_RGMIIDCTL_2_25_NS
88#define DEFAULT_TX_ID_DELAY DP83867_RGMIIDCTL_2_75_NS
89#define DEFAULT_FIFO_DEPTH DP83867_PHYCR_FIFO_DEPTH_4_B_NIB
90
Mugunthan V N5b6b18e2017-01-24 11:15:40 -060091/* IO_MUX_CFG bits */
92#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL 0x1f
93
94#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX 0x0
95#define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN 0x1f
96
Janine Hagemanned51bfc2018-08-28 08:25:37 +020097/* CFG4 bits */
98#define DP83867_CFG4_PORT_MIRROR_EN BIT(0)
99
100enum {
101 DP83867_PORT_MIRRORING_KEEP,
102 DP83867_PORT_MIRRORING_EN,
103 DP83867_PORT_MIRRORING_DIS,
104};
105
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500106struct dp83867_private {
107 int rx_id_delay;
108 int tx_id_delay;
109 int fifo_depth;
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600110 int io_impedance;
Murali Karicheri9b050762018-06-28 14:26:34 -0500111 bool rxctrl_strap_quirk;
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200112 int port_mirroring;
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500113};
114
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700115/**
116 * phy_read_mmd_indirect - reads data from the MMD registers
117 * @phydev: The PHY device bus
118 * @prtad: MMD Address
119 * @devad: MMD DEVAD
120 * @addr: PHY address on the MII bus
121 *
122 * Description: it reads data from the MMD registers (clause 22 to access to
123 * clause 45) of the specified phy address.
124 * To read these registers we have:
125 * 1) Write reg 13 // DEVAD
126 * 2) Write reg 14 // MMD Address
127 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
128 * 3) Read reg 14 // Read MMD data
129 */
130int phy_read_mmd_indirect(struct phy_device *phydev, int prtad,
131 int devad, int addr)
132{
133 int value = -1;
134
135 /* Write the desired MMD Devad */
136 phy_write(phydev, addr, MII_MMD_CTRL, devad);
137
138 /* Write the desired MMD register address */
139 phy_write(phydev, addr, MII_MMD_DATA, prtad);
140
141 /* Select the Function : DATA with no post increment */
142 phy_write(phydev, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
143
144 /* Read the content of the MMD's selected register */
145 value = phy_read(phydev, addr, MII_MMD_DATA);
146 return value;
147}
148
149/**
150 * phy_write_mmd_indirect - writes data to the MMD registers
151 * @phydev: The PHY device
152 * @prtad: MMD Address
153 * @devad: MMD DEVAD
154 * @addr: PHY address on the MII bus
155 * @data: data to write in the MMD register
156 *
157 * Description: Write data from the MMD registers of the specified
158 * phy address.
159 * To write these registers we have:
160 * 1) Write reg 13 // DEVAD
161 * 2) Write reg 14 // MMD Address
162 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
163 * 3) Write reg 14 // Write MMD data
164 */
165void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
166 int devad, int addr, u32 data)
167{
168 /* Write the desired MMD Devad */
169 phy_write(phydev, addr, MII_MMD_CTRL, devad);
170
171 /* Write the desired MMD register address */
172 phy_write(phydev, addr, MII_MMD_DATA, prtad);
173
174 /* Select the Function : DATA with no post increment */
175 phy_write(phydev, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
176
177 /* Write the data into MMD's selected register */
178 phy_write(phydev, addr, MII_MMD_DATA, data);
179}
180
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200181static int dp83867_config_port_mirroring(struct phy_device *phydev)
182{
183 struct dp83867_private *dp83867 =
184 (struct dp83867_private *)phydev->priv;
185 u16 val;
186
187 val = phy_read_mmd_indirect(phydev, DP83867_CFG4, DP83867_DEVADDR,
188 phydev->addr);
189
190 if (dp83867->port_mirroring == DP83867_PORT_MIRRORING_EN)
191 val |= DP83867_CFG4_PORT_MIRROR_EN;
192 else
193 val &= ~DP83867_CFG4_PORT_MIRROR_EN;
194
195 phy_write_mmd_indirect(phydev, DP83867_CFG4, DP83867_DEVADDR,
196 phydev->addr, val);
197
198 return 0;
199}
200
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500201#if defined(CONFIG_DM_ETH)
202/**
203 * dp83867_data_init - Convenience function for setting PHY specific data
204 *
205 * @phydev: the phy_device struct
206 */
207static int dp83867_of_init(struct phy_device *phydev)
208{
209 struct dp83867_private *dp83867 = phydev->priv;
Grygorii Strashko00e2c242018-07-05 12:02:49 -0500210 ofnode node;
211
212 node = phy_get_ofnode(phydev);
213 if (!ofnode_valid(node))
214 return -EINVAL;
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600215
Grygorii Strashko9df35052018-06-28 14:26:35 -0500216 if (ofnode_read_bool(node, "ti,max-output-impedance"))
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600217 dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX;
Grygorii Strashko9df35052018-06-28 14:26:35 -0500218 else if (ofnode_read_bool(node, "ti,min-output-impedance"))
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600219 dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN;
220 else
221 dp83867->io_impedance = -EINVAL;
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500222
Grygorii Strashko9df35052018-06-28 14:26:35 -0500223 if (ofnode_read_bool(node, "ti,dp83867-rxctrl-strap-quirk"))
Murali Karicheri9b050762018-06-28 14:26:34 -0500224 dp83867->rxctrl_strap_quirk = true;
Grygorii Strashko9df35052018-06-28 14:26:35 -0500225 dp83867->rx_id_delay = ofnode_read_u32_default(node,
226 "ti,rx-internal-delay",
227 -1);
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500228
Grygorii Strashko9df35052018-06-28 14:26:35 -0500229 dp83867->tx_id_delay = ofnode_read_u32_default(node,
230 "ti,tx-internal-delay",
231 -1);
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500232
Grygorii Strashko9df35052018-06-28 14:26:35 -0500233 dp83867->fifo_depth = ofnode_read_u32_default(node, "ti,fifo-depth",
234 -1);
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200235 if (ofnode_read_bool(node, "enet-phy-lane-swap"))
236 dp83867->port_mirroring = DP83867_PORT_MIRRORING_EN;
237
238 if (ofnode_read_bool(node, "enet-phy-lane-no-swap"))
239 dp83867->port_mirroring = DP83867_PORT_MIRRORING_DIS;
240
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500241
242 return 0;
243}
244#else
245static int dp83867_of_init(struct phy_device *phydev)
246{
247 struct dp83867_private *dp83867 = phydev->priv;
248
249 dp83867->rx_id_delay = DEFAULT_RX_ID_DELAY;
250 dp83867->tx_id_delay = DEFAULT_TX_ID_DELAY;
251 dp83867->fifo_depth = DEFAULT_FIFO_DEPTH;
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600252 dp83867->io_impedance = -EINVAL;
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500253
254 return 0;
255}
256#endif
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700257
258static int dp83867_config(struct phy_device *phydev)
259{
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500260 struct dp83867_private *dp83867;
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +0530261 unsigned int val, delay, cfg2;
Janine Hagemannb3dd2ed2018-08-28 08:25:38 +0200262 int ret, bs;
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700263
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500264 if (!phydev->priv) {
265 dp83867 = kzalloc(sizeof(*dp83867), GFP_KERNEL);
266 if (!dp83867)
267 return -ENOMEM;
268
269 phydev->priv = dp83867;
270 ret = dp83867_of_init(phydev);
271 if (ret)
272 goto err_out;
273 } else {
274 dp83867 = (struct dp83867_private *)phydev->priv;
275 }
276
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700277 /* Restart the PHY. */
278 val = phy_read(phydev, MDIO_DEVAD_NONE, DP83867_CTRL);
279 phy_write(phydev, MDIO_DEVAD_NONE, DP83867_CTRL,
280 val | DP83867_SW_RESTART);
281
Murali Karicheri9b050762018-06-28 14:26:34 -0500282 /* Mode 1 or 2 workaround */
283 if (dp83867->rxctrl_strap_quirk) {
284 val = phy_read_mmd_indirect(phydev, DP83867_CFG4,
285 DP83867_DEVADDR, phydev->addr);
286 val &= ~BIT(7);
287 phy_write_mmd_indirect(phydev, DP83867_CFG4,
288 DP83867_DEVADDR, phydev->addr, val);
289 }
290
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700291 if (phy_interface_is_rgmii(phydev)) {
292 ret = phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_PHYCTRL,
Michal Simekf6459152015-10-19 10:43:30 +0200293 (DP83867_MDI_CROSSOVER_AUTO << DP83867_MDI_CROSSOVER) |
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500294 (dp83867->fifo_depth << DP83867_PHYCR_FIFO_DEPTH_SHIFT));
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700295 if (ret)
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500296 goto err_out;
Janine Hagemannb3dd2ed2018-08-28 08:25:38 +0200297
298 /* The code below checks if "port mirroring" N/A MODE4 has been
299 * enabled during power on bootstrap.
300 *
301 * Such N/A mode enabled by mistake can put PHY IC in some
302 * internal testing mode and disable RGMII transmission.
303 *
304 * In this particular case one needs to check STRAP_STS1
305 * register's bit 11 (marked as RESERVED).
306 */
307
308 bs = phy_read_mmd_indirect(phydev, DP83867_STRAP_STS1,
309 DP83867_DEVADDR, phydev->addr);
310 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_DP83867_PHYCTRL);
311 if (bs & DP83867_STRAP_STS1_RESERVED) {
312 val &= ~DP83867_PHYCR_RESERVED_MASK;
313 phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_PHYCTRL,
314 val);
315 }
316
Dan Murphy4f4e9622016-05-02 15:46:02 -0500317 } else if (phy_interface_is_sgmii(phydev)) {
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +0530318 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR,
319 (BMCR_ANENABLE | BMCR_FULLDPLX | BMCR_SPEED1000));
320
321 cfg2 = phy_read(phydev, phydev->addr, MII_DP83867_CFG2);
322 cfg2 &= MII_DP83867_CFG2_MASK;
323 cfg2 |= (MII_DP83867_CFG2_SPEEDOPT_10EN |
324 MII_DP83867_CFG2_SGMII_AUTONEGEN |
325 MII_DP83867_CFG2_SPEEDOPT_ENH |
326 MII_DP83867_CFG2_SPEEDOPT_CNT |
327 MII_DP83867_CFG2_SPEEDOPT_INTLOW);
328 phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_CFG2, cfg2);
329
330 phy_write_mmd_indirect(phydev, DP83867_RGMIICTL,
331 DP83867_DEVADDR, phydev->addr, 0x0);
332
333 phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_PHYCTRL,
334 DP83867_PHYCTRL_SGMIIEN |
335 (DP83867_MDI_CROSSOVER_MDIX <<
336 DP83867_MDI_CROSSOVER) |
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500337 (dp83867->fifo_depth << DP83867_PHYCTRL_RXFIFO_SHIFT) |
338 (dp83867->fifo_depth << DP83867_PHYCTRL_TXFIFO_SHIFT));
Siva Durga Prasad Paladugu02c7be62016-03-25 12:53:43 +0530339 phy_write(phydev, MDIO_DEVAD_NONE, MII_DP83867_BISCR, 0x0);
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700340 }
341
Phil Edworthyc6119802016-12-09 10:46:02 +0000342 if (phy_interface_is_rgmii(phydev)) {
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700343 val = phy_read_mmd_indirect(phydev, DP83867_RGMIICTL,
344 DP83867_DEVADDR, phydev->addr);
345
346 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
347 val |= (DP83867_RGMII_TX_CLK_DELAY_EN |
348 DP83867_RGMII_RX_CLK_DELAY_EN);
349
350 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
351 val |= DP83867_RGMII_TX_CLK_DELAY_EN;
352
353 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
354 val |= DP83867_RGMII_RX_CLK_DELAY_EN;
355
356 phy_write_mmd_indirect(phydev, DP83867_RGMIICTL,
357 DP83867_DEVADDR, phydev->addr, val);
358
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500359 delay = (dp83867->rx_id_delay |
360 (dp83867->tx_id_delay << DP83867_RGMII_TX_CLK_DELAY_SHIFT));
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700361
362 phy_write_mmd_indirect(phydev, DP83867_RGMIIDCTL,
363 DP83867_DEVADDR, phydev->addr, delay);
Mugunthan V N5b6b18e2017-01-24 11:15:40 -0600364
365 if (dp83867->io_impedance >= 0) {
366 val = phy_read_mmd_indirect(phydev,
367 DP83867_IO_MUX_CFG,
368 DP83867_DEVADDR,
369 phydev->addr);
370 val &= ~DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL;
371 val |= dp83867->io_impedance &
372 DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL;
373 phy_write_mmd_indirect(phydev, DP83867_IO_MUX_CFG,
374 DP83867_DEVADDR, phydev->addr,
375 val);
376 }
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700377 }
378
Janine Hagemanned51bfc2018-08-28 08:25:37 +0200379 if (dp83867->port_mirroring != DP83867_PORT_MIRRORING_KEEP)
380 dp83867_config_port_mirroring(phydev);
381
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700382 genphy_config_aneg(phydev);
383 return 0;
Dan Murphy83fbd0a2016-05-02 15:45:59 -0500384
385err_out:
386 kfree(dp83867);
387 return ret;
Edgar E. Iglesias8d3ce682015-09-25 23:46:08 -0700388}
389
390static struct phy_driver DP83867_driver = {
391 .name = "TI DP83867",
392 .uid = 0x2000a231,
393 .mask = 0xfffffff0,
394 .features = PHY_GBIT_FEATURES,
395 .config = &dp83867_config,
396 .startup = &genphy_startup,
397 .shutdown = &genphy_shutdown,
398};
399
400int phy_ti_init(void)
401{
402 phy_register(&DP83867_driver);
403 return 0;
404}