blob: 0105fc5af1ea3a3fc012b689536617607002b5ed [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming9082eea2011-04-07 21:56:05 -05002/*
3 * Micrel PHY drivers
4 *
Andy Fleming9082eea2011-04-07 21:56:05 -05005 * Copyright 2010-2011 Freescale Semiconductor, Inc.
6 * author Andy Fleming
David Andrey62d7dba2013-02-06 22:18:37 +01007 * (C) 2012 NetModule AG, David Andrey, added KSZ9031
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -07008 * (C) Copyright 2017 Adaptrum, Inc.
9 * Written by Alexandru Gagniuc <alex.g@adaptrum.com> for Adaptrum, Inc.
Andy Fleming9082eea2011-04-07 21:56:05 -050010 */
Troy Kisky8682aba2012-02-07 14:08:48 +000011#include <common.h>
Marek Vasut22854bd2015-12-05 17:41:58 +010012#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -060013#include <env.h>
Marek Vasut22854bd2015-12-05 17:41:58 +010014#include <errno.h>
Troy Kisky8682aba2012-02-07 14:08:48 +000015#include <micrel.h>
Andy Fleming9082eea2011-04-07 21:56:05 -050016#include <phy.h>
17
Pavel Machek58ec63d2014-09-09 14:26:51 +020018/*
David Andrey62d7dba2013-02-06 22:18:37 +010019 * KSZ9021 - KSZ9031 common
20 */
21
22#define MII_KSZ90xx_PHY_CTL 0x1f
23#define MIIM_KSZ90xx_PHYCTL_1000 (1 << 6)
24#define MIIM_KSZ90xx_PHYCTL_100 (1 << 5)
25#define MIIM_KSZ90xx_PHYCTL_10 (1 << 4)
26#define MIIM_KSZ90xx_PHYCTL_DUPLEX (1 << 3)
27
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -070028/* KSZ9021 PHY Registers */
29#define MII_KSZ9021_EXTENDED_CTRL 0x0b
30#define MII_KSZ9021_EXTENDED_DATAW 0x0c
31#define MII_KSZ9021_EXTENDED_DATAR 0x0d
32
33#define CTRL1000_PREFER_MASTER (1 << 10)
34#define CTRL1000_CONFIG_MASTER (1 << 11)
35#define CTRL1000_MANUAL_CONFIG (1 << 12)
36
James Byrne83f71ef2019-03-04 17:40:33 +000037#define KSZ9021_PS_TO_REG 120
38
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -070039/* KSZ9031 PHY Registers */
40#define MII_KSZ9031_MMD_ACCES_CTRL 0x0d
41#define MII_KSZ9031_MMD_REG_DATA 0x0e
42
James Byrne83f71ef2019-03-04 17:40:33 +000043#define KSZ9031_PS_TO_REG 60
44
David Andrey62d7dba2013-02-06 22:18:37 +010045static int ksz90xx_startup(struct phy_device *phydev)
46{
47 unsigned phy_ctl;
Michal Simekb733c272016-05-18 12:46:12 +020048 int ret;
49
50 ret = genphy_update_link(phydev);
51 if (ret)
52 return ret;
53
David Andrey62d7dba2013-02-06 22:18:37 +010054 phy_ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_KSZ90xx_PHY_CTL);
55
56 if (phy_ctl & MIIM_KSZ90xx_PHYCTL_DUPLEX)
57 phydev->duplex = DUPLEX_FULL;
58 else
59 phydev->duplex = DUPLEX_HALF;
60
61 if (phy_ctl & MIIM_KSZ90xx_PHYCTL_1000)
62 phydev->speed = SPEED_1000;
63 else if (phy_ctl & MIIM_KSZ90xx_PHYCTL_100)
64 phydev->speed = SPEED_100;
65 else if (phy_ctl & MIIM_KSZ90xx_PHYCTL_10)
66 phydev->speed = SPEED_10;
67 return 0;
68}
David Andrey62d7dba2013-02-06 22:18:37 +010069
Marek Vasut22854bd2015-12-05 17:41:58 +010070/* Common OF config bits for KSZ9021 and KSZ9031 */
Marek Vasut22854bd2015-12-05 17:41:58 +010071#ifdef CONFIG_DM_ETH
72struct ksz90x1_reg_field {
73 const char *name;
74 const u8 size; /* Size of the bitfield, in bits */
75 const u8 off; /* Offset from bit 0 */
76 const u8 dflt; /* Default value */
77};
78
79struct ksz90x1_ofcfg {
80 const u16 reg;
81 const u16 devad;
82 const struct ksz90x1_reg_field *grp;
83 const u16 grpsz;
84};
85
86static const struct ksz90x1_reg_field ksz90x1_rxd_grp[] = {
87 { "rxd0-skew-ps", 4, 0, 0x7 }, { "rxd1-skew-ps", 4, 4, 0x7 },
88 { "rxd2-skew-ps", 4, 8, 0x7 }, { "rxd3-skew-ps", 4, 12, 0x7 }
89};
90
91static const struct ksz90x1_reg_field ksz90x1_txd_grp[] = {
92 { "txd0-skew-ps", 4, 0, 0x7 }, { "txd1-skew-ps", 4, 4, 0x7 },
93 { "txd2-skew-ps", 4, 8, 0x7 }, { "txd3-skew-ps", 4, 12, 0x7 },
94};
95
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -070096static const struct ksz90x1_reg_field ksz9021_clk_grp[] = {
97 { "txen-skew-ps", 4, 0, 0x7 }, { "txc-skew-ps", 4, 4, 0x7 },
98 { "rxdv-skew-ps", 4, 8, 0x7 }, { "rxc-skew-ps", 4, 12, 0x7 },
99};
100
101static const struct ksz90x1_reg_field ksz9031_ctl_grp[] = {
102 { "txen-skew-ps", 4, 0, 0x7 }, { "rxdv-skew-ps", 4, 4, 0x7 }
103};
104
105static const struct ksz90x1_reg_field ksz9031_clk_grp[] = {
106 { "rxc-skew-ps", 5, 0, 0xf }, { "txc-skew-ps", 5, 5, 0xf }
107};
108
Marek Vasut22854bd2015-12-05 17:41:58 +0100109static int ksz90x1_of_config_group(struct phy_device *phydev,
James Byrne83f71ef2019-03-04 17:40:33 +0000110 struct ksz90x1_ofcfg *ofcfg,
111 int ps_to_regval)
Marek Vasut22854bd2015-12-05 17:41:58 +0100112{
113 struct udevice *dev = phydev->dev;
114 struct phy_driver *drv = phydev->drv;
Marek Vasut22854bd2015-12-05 17:41:58 +0100115 int val[4];
116 int i, changed = 0, offset, max;
117 u16 regval = 0;
James Byrne6314d1c2019-03-04 17:40:34 +0000118 ofnode node;
Marek Vasut22854bd2015-12-05 17:41:58 +0100119
120 if (!drv || !drv->writeext)
121 return -EOPNOTSUPP;
122
James Byrne6314d1c2019-03-04 17:40:34 +0000123 /* Look for a PHY node under the Ethernet node */
124 node = dev_read_subnode(dev, "ethernet-phy");
125 if (!ofnode_valid(node)) {
126 /* No node found, look in the Ethernet node */
127 node = dev_ofnode(dev);
128 }
129
Marek Vasut22854bd2015-12-05 17:41:58 +0100130 for (i = 0; i < ofcfg->grpsz; i++) {
James Byrne6314d1c2019-03-04 17:40:34 +0000131 val[i] = ofnode_read_u32_default(node, ofcfg->grp[i].name, ~0);
Marek Vasut22854bd2015-12-05 17:41:58 +0100132 offset = ofcfg->grp[i].off;
133 if (val[i] == -1) {
134 /* Default register value for KSZ9021 */
135 regval |= ofcfg->grp[i].dflt << offset;
136 } else {
137 changed = 1; /* Value was changed in OF */
138 /* Calculate the register value and fix corner cases */
Andreas Pretzsch3b4cda32018-11-29 20:04:53 +0100139 max = (1 << ofcfg->grp[i].size) - 1;
140 if (val[i] > ps_to_regval * max) {
Marek Vasut22854bd2015-12-05 17:41:58 +0100141 regval |= max << offset;
142 } else {
143 regval |= (val[i] / ps_to_regval) << offset;
144 }
145 }
146 }
147
148 if (!changed)
149 return 0;
150
151 return drv->writeext(phydev, 0, ofcfg->devad, ofcfg->reg, regval);
152}
Marek Vasut22854bd2015-12-05 17:41:58 +0100153
154static int ksz9021_of_config(struct phy_device *phydev)
155{
156 struct ksz90x1_ofcfg ofcfg[] = {
157 { MII_KSZ9021_EXT_RGMII_RX_DATA_SKEW, 0, ksz90x1_rxd_grp, 4 },
158 { MII_KSZ9021_EXT_RGMII_TX_DATA_SKEW, 0, ksz90x1_txd_grp, 4 },
159 { MII_KSZ9021_EXT_RGMII_CLOCK_SKEW, 0, ksz9021_clk_grp, 4 },
160 };
161 int i, ret = 0;
162
Marek Vasut75c056d2016-11-14 15:08:42 +0100163 for (i = 0; i < ARRAY_SIZE(ofcfg); i++) {
James Byrne83f71ef2019-03-04 17:40:33 +0000164 ret = ksz90x1_of_config_group(phydev, &ofcfg[i],
165 KSZ9021_PS_TO_REG);
Marek Vasut22854bd2015-12-05 17:41:58 +0100166 if (ret)
167 return ret;
Marek Vasut75c056d2016-11-14 15:08:42 +0100168 }
Marek Vasut22854bd2015-12-05 17:41:58 +0100169
170 return 0;
171}
Marek Vasut22854bd2015-12-05 17:41:58 +0100172
173static int ksz9031_of_config(struct phy_device *phydev)
174{
175 struct ksz90x1_ofcfg ofcfg[] = {
176 { MII_KSZ9031_EXT_RGMII_CTRL_SIG_SKEW, 2, ksz9031_ctl_grp, 2 },
177 { MII_KSZ9031_EXT_RGMII_RX_DATA_SKEW, 2, ksz90x1_rxd_grp, 4 },
178 { MII_KSZ9031_EXT_RGMII_TX_DATA_SKEW, 2, ksz90x1_txd_grp, 4 },
179 { MII_KSZ9031_EXT_RGMII_CLOCK_SKEW, 2, ksz9031_clk_grp, 2 },
180 };
181 int i, ret = 0;
182
Marek Vasut75c056d2016-11-14 15:08:42 +0100183 for (i = 0; i < ARRAY_SIZE(ofcfg); i++) {
James Byrne83f71ef2019-03-04 17:40:33 +0000184 ret = ksz90x1_of_config_group(phydev, &ofcfg[i],
185 KSZ9031_PS_TO_REG);
Marek Vasut22854bd2015-12-05 17:41:58 +0100186 if (ret)
187 return ret;
Marek Vasut75c056d2016-11-14 15:08:42 +0100188 }
Marek Vasut22854bd2015-12-05 17:41:58 +0100189
190 return 0;
191}
Ash Charlesf0185452016-10-21 17:31:33 -0400192
193static int ksz9031_center_flp_timing(struct phy_device *phydev)
194{
195 struct phy_driver *drv = phydev->drv;
196 int ret = 0;
197
198 if (!drv || !drv->writeext)
199 return -EOPNOTSUPP;
200
201 ret = drv->writeext(phydev, 0, 0, MII_KSZ9031_FLP_BURST_TX_LO, 0x1A80);
202 if (ret)
203 return ret;
204
205 ret = drv->writeext(phydev, 0, 0, MII_KSZ9031_FLP_BURST_TX_HI, 0x6);
206 return ret;
207}
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700208
209#else /* !CONFIG_DM_ETH */
210static int ksz9021_of_config(struct phy_device *phydev)
211{
212 return 0;
213}
214
Marek Vasut22854bd2015-12-05 17:41:58 +0100215static int ksz9031_of_config(struct phy_device *phydev)
216{
217 return 0;
218}
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700219
Ash Charlesf0185452016-10-21 17:31:33 -0400220static int ksz9031_center_flp_timing(struct phy_device *phydev)
221{
222 return 0;
223}
Marek Vasut22854bd2015-12-05 17:41:58 +0100224#endif
225
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700226/*
227 * KSZ9021
228 */
229int ksz9021_phy_extended_write(struct phy_device *phydev, int regnum, u16 val)
230{
231 /* extended registers */
232 phy_write(phydev, MDIO_DEVAD_NONE,
233 MII_KSZ9021_EXTENDED_CTRL, regnum | 0x8000);
234 return phy_write(phydev, MDIO_DEVAD_NONE,
235 MII_KSZ9021_EXTENDED_DATAW, val);
236}
237
238int ksz9021_phy_extended_read(struct phy_device *phydev, int regnum)
239{
240 /* extended registers */
241 phy_write(phydev, MDIO_DEVAD_NONE, MII_KSZ9021_EXTENDED_CTRL, regnum);
242 return phy_read(phydev, MDIO_DEVAD_NONE, MII_KSZ9021_EXTENDED_DATAR);
243}
244
245
246static int ksz9021_phy_extread(struct phy_device *phydev, int addr, int devaddr,
247 int regnum)
248{
249 return ksz9021_phy_extended_read(phydev, regnum);
250}
251
252static int ksz9021_phy_extwrite(struct phy_device *phydev, int addr,
253 int devaddr, int regnum, u16 val)
254{
255 return ksz9021_phy_extended_write(phydev, regnum, val);
256}
257
258static int ksz9021_config(struct phy_device *phydev)
259{
260 unsigned ctrl1000 = 0;
261 const unsigned master = CTRL1000_PREFER_MASTER |
262 CTRL1000_CONFIG_MASTER | CTRL1000_MANUAL_CONFIG;
263 unsigned features = phydev->drv->features;
264 int ret;
265
266 ret = ksz9021_of_config(phydev);
267 if (ret)
268 return ret;
269
Simon Glass00caae62017-08-03 12:22:12 -0600270 if (env_get("disable_giga"))
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700271 features &= ~(SUPPORTED_1000baseT_Half |
272 SUPPORTED_1000baseT_Full);
273 /* force master mode for 1000BaseT due to chip errata */
274 if (features & SUPPORTED_1000baseT_Half)
275 ctrl1000 |= ADVERTISE_1000HALF | master;
276 if (features & SUPPORTED_1000baseT_Full)
277 ctrl1000 |= ADVERTISE_1000FULL | master;
278 phydev->advertising = features;
279 phydev->supported = features;
280 phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, ctrl1000);
281 genphy_config_aneg(phydev);
282 genphy_restart_aneg(phydev);
283 return 0;
284}
285
286static struct phy_driver ksz9021_driver = {
287 .name = "Micrel ksz9021",
288 .uid = 0x221610,
James Byrne77b508d2019-03-06 12:48:27 +0000289 .mask = 0xfffffe,
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700290 .features = PHY_GBIT_FEATURES,
291 .config = &ksz9021_config,
292 .startup = &ksz90xx_startup,
293 .shutdown = &genphy_shutdown,
294 .writeext = &ksz9021_phy_extwrite,
295 .readext = &ksz9021_phy_extread,
296};
297
298/*
299 * KSZ9031
300 */
SARTRE Leo42a7cb52013-04-30 16:57:25 +0200301int ksz9031_phy_extended_write(struct phy_device *phydev,
302 int devaddr, int regnum, u16 mode, u16 val)
303{
304 /*select register addr for mmd*/
305 phy_write(phydev, MDIO_DEVAD_NONE,
306 MII_KSZ9031_MMD_ACCES_CTRL, devaddr);
307 /*select register for mmd*/
308 phy_write(phydev, MDIO_DEVAD_NONE,
309 MII_KSZ9031_MMD_REG_DATA, regnum);
310 /*setup mode*/
311 phy_write(phydev, MDIO_DEVAD_NONE,
312 MII_KSZ9031_MMD_ACCES_CTRL, (mode | devaddr));
313 /*write the value*/
314 return phy_write(phydev, MDIO_DEVAD_NONE,
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700315 MII_KSZ9031_MMD_REG_DATA, val);
SARTRE Leo42a7cb52013-04-30 16:57:25 +0200316}
317
318int ksz9031_phy_extended_read(struct phy_device *phydev, int devaddr,
319 int regnum, u16 mode)
320{
321 phy_write(phydev, MDIO_DEVAD_NONE,
322 MII_KSZ9031_MMD_ACCES_CTRL, devaddr);
323 phy_write(phydev, MDIO_DEVAD_NONE,
324 MII_KSZ9031_MMD_REG_DATA, regnum);
325 phy_write(phydev, MDIO_DEVAD_NONE,
326 MII_KSZ9031_MMD_ACCES_CTRL, (devaddr | mode));
327 return phy_read(phydev, MDIO_DEVAD_NONE, MII_KSZ9031_MMD_REG_DATA);
328}
329
Stefano Babic9ced16f2013-09-02 15:42:31 +0200330static int ksz9031_phy_extread(struct phy_device *phydev, int addr, int devaddr,
331 int regnum)
332{
333 return ksz9031_phy_extended_read(phydev, devaddr, regnum,
334 MII_KSZ9031_MOD_DATA_NO_POST_INC);
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700335}
Stefano Babic9ced16f2013-09-02 15:42:31 +0200336
337static int ksz9031_phy_extwrite(struct phy_device *phydev, int addr,
338 int devaddr, int regnum, u16 val)
339{
340 return ksz9031_phy_extended_write(phydev, devaddr, regnum,
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700341 MII_KSZ9031_MOD_DATA_POST_INC_RW, val);
342}
Stefano Babic9ced16f2013-09-02 15:42:31 +0200343
Marek Vasut22854bd2015-12-05 17:41:58 +0100344static int ksz9031_config(struct phy_device *phydev)
345{
346 int ret;
Sebastien Bourdelinef1f61a2017-07-28 15:59:22 -0400347
Marek Vasut22854bd2015-12-05 17:41:58 +0100348 ret = ksz9031_of_config(phydev);
349 if (ret)
350 return ret;
Ash Charlesf0185452016-10-21 17:31:33 -0400351 ret = ksz9031_center_flp_timing(phydev);
352 if (ret)
353 return ret;
Sebastien Bourdelinef1f61a2017-07-28 15:59:22 -0400354
355 /* add an option to disable the gigabit feature of this PHY */
Simon Glass00caae62017-08-03 12:22:12 -0600356 if (env_get("disable_giga")) {
Sebastien Bourdelinef1f61a2017-07-28 15:59:22 -0400357 unsigned features;
358 unsigned bmcr;
359
360 /* disable speed 1000 in features supported by the PHY */
361 features = phydev->drv->features;
362 features &= ~(SUPPORTED_1000baseT_Half |
363 SUPPORTED_1000baseT_Full);
364 phydev->advertising = phydev->supported = features;
365
366 /* disable speed 1000 in Basic Control Register */
367 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
368 bmcr &= ~(1 << 6);
369 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, bmcr);
370
371 /* disable speed 1000 in 1000Base-T Control Register */
372 phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, 0);
373
374 /* start autoneg */
375 genphy_config_aneg(phydev);
376 genphy_restart_aneg(phydev);
377
378 return 0;
379 }
380
Marek Vasut22854bd2015-12-05 17:41:58 +0100381 return genphy_config(phydev);
382}
Stefano Babic9ced16f2013-09-02 15:42:31 +0200383
David Andrey62d7dba2013-02-06 22:18:37 +0100384static struct phy_driver ksz9031_driver = {
385 .name = "Micrel ksz9031",
386 .uid = 0x221620,
Stefano Babice8194d52013-09-02 15:42:29 +0200387 .mask = 0xfffff0,
David Andrey62d7dba2013-02-06 22:18:37 +0100388 .features = PHY_GBIT_FEATURES,
Marek Vasut22854bd2015-12-05 17:41:58 +0100389 .config = &ksz9031_config,
David Andrey62d7dba2013-02-06 22:18:37 +0100390 .startup = &ksz90xx_startup,
391 .shutdown = &genphy_shutdown,
Stefano Babic9ced16f2013-09-02 15:42:31 +0200392 .writeext = &ksz9031_phy_extwrite,
393 .readext = &ksz9031_phy_extread,
David Andrey62d7dba2013-02-06 22:18:37 +0100394};
395
Alexandru Gagniucd397f7c2017-07-07 11:36:57 -0700396int phy_micrel_ksz90x1_init(void)
Alexey Firago79887742016-05-26 16:28:44 +0300397{
Troy Kisky8682aba2012-02-07 14:08:48 +0000398 phy_register(&ksz9021_driver);
David Andrey62d7dba2013-02-06 22:18:37 +0100399 phy_register(&ksz9031_driver);
Andy Fleming9082eea2011-04-07 21:56:05 -0500400 return 0;
401}