blob: 0105fc5af1ea3a3fc012b689536617607002b5ed [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming60ca78b2011-04-07 21:56:05 -05002/*
3 * Micrel PHY drivers
4 *
Andy Fleming60ca78b2011-04-07 21:56:05 -05005 * Copyright 2010-2011 Freescale Semiconductor, Inc.
6 * author Andy Fleming
David Andreyf0d83c42013-02-06 22:18:37 +01007 * (C) 2012 NetModule AG, David Andrey, added KSZ9031
Alexandru Gagniuc757bb672017-07-07 11:36:57 -07008 * (C) Copyright 2017 Adaptrum, Inc.
9 * Written by Alexandru Gagniuc <alex.g@adaptrum.com> for Adaptrum, Inc.
Andy Fleming60ca78b2011-04-07 21:56:05 -050010 */
Troy Kisky80b6b092012-02-07 14:08:48 +000011#include <common.h>
Marek Vasut1005ce52015-12-05 17:41:58 +010012#include <dm.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060013#include <env.h>
Marek Vasut1005ce52015-12-05 17:41:58 +010014#include <errno.h>
Troy Kisky80b6b092012-02-07 14:08:48 +000015#include <micrel.h>
Andy Fleming60ca78b2011-04-07 21:56:05 -050016#include <phy.h>
17
Pavel Machek5f022112014-09-09 14:26:51 +020018/*
David Andreyf0d83c42013-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 Gagniuc757bb672017-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 Byrne457107f2019-03-04 17:40:33 +000037#define KSZ9021_PS_TO_REG 120
38
Alexandru Gagniuc757bb672017-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 Byrne457107f2019-03-04 17:40:33 +000043#define KSZ9031_PS_TO_REG 60
44
David Andreyf0d83c42013-02-06 22:18:37 +010045static int ksz90xx_startup(struct phy_device *phydev)
46{
47 unsigned phy_ctl;
Michal Simek5ff89662016-05-18 12:46:12 +020048 int ret;
49
50 ret = genphy_update_link(phydev);
51 if (ret)
52 return ret;
53
David Andreyf0d83c42013-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 Andreyf0d83c42013-02-06 22:18:37 +010069
Marek Vasut1005ce52015-12-05 17:41:58 +010070/* Common OF config bits for KSZ9021 and KSZ9031 */
Marek Vasut1005ce52015-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 Gagniuc757bb672017-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 Vasut1005ce52015-12-05 17:41:58 +0100109static int ksz90x1_of_config_group(struct phy_device *phydev,
James Byrne457107f2019-03-04 17:40:33 +0000110 struct ksz90x1_ofcfg *ofcfg,
111 int ps_to_regval)
Marek Vasut1005ce52015-12-05 17:41:58 +0100112{
113 struct udevice *dev = phydev->dev;
114 struct phy_driver *drv = phydev->drv;
Marek Vasut1005ce52015-12-05 17:41:58 +0100115 int val[4];
116 int i, changed = 0, offset, max;
117 u16 regval = 0;
James Byrne9e791442019-03-04 17:40:34 +0000118 ofnode node;
Marek Vasut1005ce52015-12-05 17:41:58 +0100119
120 if (!drv || !drv->writeext)
121 return -EOPNOTSUPP;
122
James Byrne9e791442019-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 Vasut1005ce52015-12-05 17:41:58 +0100130 for (i = 0; i < ofcfg->grpsz; i++) {
James Byrne9e791442019-03-04 17:40:34 +0000131 val[i] = ofnode_read_u32_default(node, ofcfg->grp[i].name, ~0);
Marek Vasut1005ce52015-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 Pretzschf7c689d2018-11-29 20:04:53 +0100139 max = (1 << ofcfg->grp[i].size) - 1;
140 if (val[i] > ps_to_regval * max) {
Marek Vasut1005ce52015-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 Vasut1005ce52015-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 Vasut0c766302016-11-14 15:08:42 +0100163 for (i = 0; i < ARRAY_SIZE(ofcfg); i++) {
James Byrne457107f2019-03-04 17:40:33 +0000164 ret = ksz90x1_of_config_group(phydev, &ofcfg[i],
165 KSZ9021_PS_TO_REG);
Marek Vasut1005ce52015-12-05 17:41:58 +0100166 if (ret)
167 return ret;
Marek Vasut0c766302016-11-14 15:08:42 +0100168 }
Marek Vasut1005ce52015-12-05 17:41:58 +0100169
170 return 0;
171}
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700172
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
183 for (i = 0; i < ARRAY_SIZE(ofcfg); i++) {
James Byrne457107f2019-03-04 17:40:33 +0000184 ret = ksz90x1_of_config_group(phydev, &ofcfg[i],
185 KSZ9031_PS_TO_REG);
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700186 if (ret)
187 return ret;
188 }
189
190 return 0;
191}
192
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}
208
209#else /* !CONFIG_DM_ETH */
Marek Vasut1005ce52015-12-05 17:41:58 +0100210static int ksz9021_of_config(struct phy_device *phydev)
211{
212 return 0;
213}
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700214
215static int ksz9031_of_config(struct phy_device *phydev)
216{
217 return 0;
218}
219
220static int ksz9031_center_flp_timing(struct phy_device *phydev)
221{
222 return 0;
223}
Marek Vasut1005ce52015-12-05 17:41:58 +0100224#endif
225
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700226/*
227 * KSZ9021
228 */
Troy Kisky80b6b092012-02-07 14:08:48 +0000229int ksz9021_phy_extended_write(struct phy_device *phydev, int regnum, u16 val)
230{
231 /* extended registers */
232 phy_write(phydev, MDIO_DEVAD_NONE,
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700233 MII_KSZ9021_EXTENDED_CTRL, regnum | 0x8000);
Troy Kisky80b6b092012-02-07 14:08:48 +0000234 return phy_write(phydev, MDIO_DEVAD_NONE,
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700235 MII_KSZ9021_EXTENDED_DATAW, val);
Troy Kisky80b6b092012-02-07 14:08:48 +0000236}
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
Stefano Babica8aa2992013-09-02 15:42:31 +0200245
246static int ksz9021_phy_extread(struct phy_device *phydev, int addr, int devaddr,
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700247 int regnum)
Stefano Babica8aa2992013-09-02 15:42:31 +0200248{
249 return ksz9021_phy_extended_read(phydev, regnum);
250}
251
252static int ksz9021_phy_extwrite(struct phy_device *phydev, int addr,
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700253 int devaddr, int regnum, u16 val)
Stefano Babica8aa2992013-09-02 15:42:31 +0200254{
255 return ksz9021_phy_extended_write(phydev, regnum, val);
256}
257
Troy Kisky80b6b092012-02-07 14:08:48 +0000258static int ksz9021_config(struct phy_device *phydev)
259{
260 unsigned ctrl1000 = 0;
261 const unsigned master = CTRL1000_PREFER_MASTER |
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700262 CTRL1000_CONFIG_MASTER | CTRL1000_MANUAL_CONFIG;
Troy Kisky80b6b092012-02-07 14:08:48 +0000263 unsigned features = phydev->drv->features;
Marek Vasut1005ce52015-12-05 17:41:58 +0100264 int ret;
265
266 ret = ksz9021_of_config(phydev);
267 if (ret)
268 return ret;
Troy Kisky80b6b092012-02-07 14:08:48 +0000269
Simon Glass64b723f2017-08-03 12:22:12 -0600270 if (env_get("disable_giga"))
Troy Kisky80b6b092012-02-07 14:08:48 +0000271 features &= ~(SUPPORTED_1000baseT_Half |
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700272 SUPPORTED_1000baseT_Full);
Troy Kisky80b6b092012-02-07 14:08:48 +0000273 /* 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;
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700278 phydev->advertising = features;
279 phydev->supported = features;
Troy Kisky80b6b092012-02-07 14:08:48 +0000280 phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, ctrl1000);
281 genphy_config_aneg(phydev);
282 genphy_restart_aneg(phydev);
283 return 0;
284}
285
Troy Kisky80b6b092012-02-07 14:08:48 +0000286static struct phy_driver ksz9021_driver = {
287 .name = "Micrel ksz9021",
288 .uid = 0x221610,
James Byrnebc292c22019-03-06 12:48:27 +0000289 .mask = 0xfffffe,
Troy Kisky80b6b092012-02-07 14:08:48 +0000290 .features = PHY_GBIT_FEATURES,
291 .config = &ksz9021_config,
David Andreyf0d83c42013-02-06 22:18:37 +0100292 .startup = &ksz90xx_startup,
Troy Kisky80b6b092012-02-07 14:08:48 +0000293 .shutdown = &genphy_shutdown,
Stefano Babica8aa2992013-09-02 15:42:31 +0200294 .writeext = &ksz9021_phy_extwrite,
295 .readext = &ksz9021_phy_extread,
Troy Kisky80b6b092012-02-07 14:08:48 +0000296};
297
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700298/*
David Andreyf0d83c42013-02-06 22:18:37 +0100299 * KSZ9031
300 */
SARTRE Leoeaf68ac2013-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 Gagniuc757bb672017-07-07 11:36:57 -0700315 MII_KSZ9031_MMD_REG_DATA, val);
SARTRE Leoeaf68ac2013-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 Babica8aa2992013-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 Gagniuc757bb672017-07-07 11:36:57 -0700335}
Stefano Babica8aa2992013-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 Gagniuc757bb672017-07-07 11:36:57 -0700341 MII_KSZ9031_MOD_DATA_POST_INC_RW, val);
342}
Stefano Babica8aa2992013-09-02 15:42:31 +0200343
Marek Vasut1005ce52015-12-05 17:41:58 +0100344static int ksz9031_config(struct phy_device *phydev)
345{
346 int ret;
Sebastien Bourdelin3a6e0332017-07-28 15:59:22 -0400347
Marek Vasut1005ce52015-12-05 17:41:58 +0100348 ret = ksz9031_of_config(phydev);
349 if (ret)
350 return ret;
Ash Charles3f55bb62016-10-21 17:31:33 -0400351 ret = ksz9031_center_flp_timing(phydev);
352 if (ret)
353 return ret;
Sebastien Bourdelin3a6e0332017-07-28 15:59:22 -0400354
355 /* add an option to disable the gigabit feature of this PHY */
Simon Glass64b723f2017-08-03 12:22:12 -0600356 if (env_get("disable_giga")) {
Sebastien Bourdelin3a6e0332017-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 Vasut1005ce52015-12-05 17:41:58 +0100381 return genphy_config(phydev);
382}
Stefano Babica8aa2992013-09-02 15:42:31 +0200383
David Andreyf0d83c42013-02-06 22:18:37 +0100384static struct phy_driver ksz9031_driver = {
385 .name = "Micrel ksz9031",
386 .uid = 0x221620,
Stefano Babicd9e36ad2013-09-02 15:42:29 +0200387 .mask = 0xfffff0,
David Andreyf0d83c42013-02-06 22:18:37 +0100388 .features = PHY_GBIT_FEATURES,
Marek Vasut1005ce52015-12-05 17:41:58 +0100389 .config = &ksz9031_config,
David Andreyf0d83c42013-02-06 22:18:37 +0100390 .startup = &ksz90xx_startup,
391 .shutdown = &genphy_shutdown,
Stefano Babica8aa2992013-09-02 15:42:31 +0200392 .writeext = &ksz9031_phy_extwrite,
393 .readext = &ksz9031_phy_extread,
David Andreyf0d83c42013-02-06 22:18:37 +0100394};
395
Alexandru Gagniuc757bb672017-07-07 11:36:57 -0700396int phy_micrel_ksz90x1_init(void)
Andy Fleming60ca78b2011-04-07 21:56:05 -0500397{
Troy Kisky80b6b092012-02-07 14:08:48 +0000398 phy_register(&ksz9021_driver);
David Andreyf0d83c42013-02-06 22:18:37 +0100399 phy_register(&ksz9031_driver);
Andy Fleming60ca78b2011-04-07 21:56:05 -0500400 return 0;
401}