blob: e0b62b9449092350931b774ec6fac148f0374f9c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Roy Zangbafd8032012-10-08 07:44:21 +00002/*
3 * Copyright 2012 Freescale Semiconductor, Inc.
Andy Flemingb36a4d42014-07-25 17:39:08 -05004 * Andy Fleming <afleming@gmail.com>
Roy Zangbafd8032012-10-08 07:44:21 +00005 * Roy Zang <tie-fei.zang@freescale.com>
Roy Zangbafd8032012-10-08 07:44:21 +00006 * Some part is taken from tsec.c
7 */
8#include <common.h>
9#include <miiphy.h>
10#include <phy.h>
11#include <asm/io.h>
Shaohui Xie835c72b2015-03-20 19:28:19 -070012#include <fsl_memac.h>
Roy Zangbafd8032012-10-08 07:44:21 +000013#include <fm_eth.h>
14
Shaohui Xie835c72b2015-03-20 19:28:19 -070015#ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN
16#define memac_out_32(a, v) out_le32(a, v)
17#define memac_clrbits_32(a, v) clrbits_le32(a, v)
18#define memac_setbits_32(a, v) setbits_le32(a, v)
19#else
20#define memac_out_32(a, v) out_be32(a, v)
21#define memac_clrbits_32(a, v) clrbits_be32(a, v)
22#define memac_setbits_32(a, v) setbits_be32(a, v)
23#endif
24
Madalin Bucur9d327b02020-04-23 16:25:18 +030025struct fm_mdio_priv {
26 struct memac_mdio_controller *regs;
27};
Madalin Bucur9d327b02020-04-23 16:25:18 +030028
Ioana Ciorneif78f21d2020-12-09 13:31:58 +020029#define MAX_NUM_RETRIES 1000
30
Shaohui Xie835c72b2015-03-20 19:28:19 -070031static u32 memac_in_32(u32 *reg)
32{
33#ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN
34 return in_le32(reg);
35#else
36 return in_be32(reg);
37#endif
38}
39
Roy Zangbafd8032012-10-08 07:44:21 +000040/*
Ioana Ciorneif78f21d2020-12-09 13:31:58 +020041 * Wait until the MDIO bus is free
42 */
43static int memac_wait_until_free(struct memac_mdio_controller *regs)
44{
45 unsigned int timeout = MAX_NUM_RETRIES;
46
47 while ((memac_in_32(&regs->mdio_stat) & MDIO_STAT_BSY) && timeout--)
48 ;
49
50 if (!timeout) {
51 printf("timeout waiting for MDIO bus to be free\n");
52 return -ETIMEDOUT;
53 }
54
55 return 0;
56}
57
58/*
59 * Wait till the MDIO read or write operation is complete
60 */
61static int memac_wait_until_done(struct memac_mdio_controller *regs)
62{
63 unsigned int timeout = MAX_NUM_RETRIES;
64
Markus Koch9c878992022-01-11 19:22:54 +010065 while ((memac_in_32(&regs->mdio_stat) & MDIO_STAT_BSY) && timeout--)
Ioana Ciorneif78f21d2020-12-09 13:31:58 +020066 ;
67
68 if (!timeout) {
69 printf("timeout waiting for MDIO operation to complete\n");
70 return -ETIMEDOUT;
71 }
72
73 return 0;
74}
75
76/*
Roy Zangbafd8032012-10-08 07:44:21 +000077 * Write value to the PHY for this device to the register at regnum, waiting
78 * until the write is done before it returns. All PHY configuration has to be
79 * done through the TSEC1 MIIM regs
80 */
81int memac_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr,
82 int regnum, u16 value)
83{
Madalin Bucur9d327b02020-04-23 16:25:18 +030084 struct memac_mdio_controller *regs;
Roy Zangbafd8032012-10-08 07:44:21 +000085 u32 mdio_ctl;
Roy Zangbafd8032012-10-08 07:44:21 +000086 u32 c45 = 1; /* Default to 10G interface */
Ioana Ciorneif78f21d2020-12-09 13:31:58 +020087 int err;
Roy Zangbafd8032012-10-08 07:44:21 +000088
Madalin Bucur9d327b02020-04-23 16:25:18 +030089 struct fm_mdio_priv *priv;
90
91 if (!bus->priv)
92 return -EINVAL;
93 priv = dev_get_priv(bus->priv);
94 regs = priv->regs;
95 debug("memac_mdio_write(regs %p, port %d, dev %d, reg %d, val %#x)\n",
96 regs, port_addr, dev_addr, regnum, value);
Madalin Bucur9d327b02020-04-23 16:25:18 +030097
Roy Zangbafd8032012-10-08 07:44:21 +000098 if (dev_addr == MDIO_DEVAD_NONE) {
99 c45 = 0; /* clause 22 */
100 dev_addr = regnum & 0x1f;
Shaohui Xie835c72b2015-03-20 19:28:19 -0700101 memac_clrbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
Shaohui Xieadbcb9c2014-04-22 18:21:37 +0800102 } else
Shaohui Xie835c72b2015-03-20 19:28:19 -0700103 memac_setbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
Roy Zangbafd8032012-10-08 07:44:21 +0000104
Ioana Ciorneif78f21d2020-12-09 13:31:58 +0200105 err = memac_wait_until_free(regs);
106 if (err)
107 return err;
Roy Zangbafd8032012-10-08 07:44:21 +0000108
109 /* Set the port and dev addr */
110 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
Shaohui Xie835c72b2015-03-20 19:28:19 -0700111 memac_out_32(&regs->mdio_ctl, mdio_ctl);
Roy Zangbafd8032012-10-08 07:44:21 +0000112
113 /* Set the register address */
114 if (c45)
Shaohui Xie835c72b2015-03-20 19:28:19 -0700115 memac_out_32(&regs->mdio_addr, regnum & 0xffff);
Roy Zangbafd8032012-10-08 07:44:21 +0000116
Ioana Ciorneif78f21d2020-12-09 13:31:58 +0200117 err = memac_wait_until_free(regs);
118 if (err)
119 return err;
Roy Zangbafd8032012-10-08 07:44:21 +0000120
121 /* Write the value to the register */
Shaohui Xie835c72b2015-03-20 19:28:19 -0700122 memac_out_32(&regs->mdio_data, MDIO_DATA(value));
Roy Zangbafd8032012-10-08 07:44:21 +0000123
Ioana Ciorneif78f21d2020-12-09 13:31:58 +0200124 err = memac_wait_until_done(regs);
125 if (err)
126 return err;
Roy Zangbafd8032012-10-08 07:44:21 +0000127
128 return 0;
129}
130
131/*
132 * Reads from register regnum in the PHY for device dev, returning the value.
133 * Clears miimcom first. All PHY configuration has to be done through the
134 * TSEC1 MIIM regs
135 */
136int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
137 int regnum)
138{
Madalin Bucur9d327b02020-04-23 16:25:18 +0300139 struct memac_mdio_controller *regs;
Roy Zangbafd8032012-10-08 07:44:21 +0000140 u32 mdio_ctl;
Roy Zangbafd8032012-10-08 07:44:21 +0000141 u32 c45 = 1;
Ioana Ciorneif78f21d2020-12-09 13:31:58 +0200142 int err;
Roy Zangbafd8032012-10-08 07:44:21 +0000143
Madalin Bucur9d327b02020-04-23 16:25:18 +0300144 struct fm_mdio_priv *priv;
145
146 if (!bus->priv)
147 return -EINVAL;
148 priv = dev_get_priv(bus->priv);
149 regs = priv->regs;
Madalin Bucur9d327b02020-04-23 16:25:18 +0300150
Roy Zangbafd8032012-10-08 07:44:21 +0000151 if (dev_addr == MDIO_DEVAD_NONE) {
152 c45 = 0; /* clause 22 */
153 dev_addr = regnum & 0x1f;
Shaohui Xie835c72b2015-03-20 19:28:19 -0700154 memac_clrbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
Shaohui Xieadbcb9c2014-04-22 18:21:37 +0800155 } else
Shaohui Xie835c72b2015-03-20 19:28:19 -0700156 memac_setbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
Roy Zangbafd8032012-10-08 07:44:21 +0000157
Ioana Ciorneif78f21d2020-12-09 13:31:58 +0200158 err = memac_wait_until_free(regs);
159 if (err)
160 return err;
Roy Zangbafd8032012-10-08 07:44:21 +0000161
162 /* Set the Port and Device Addrs */
163 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
Shaohui Xie835c72b2015-03-20 19:28:19 -0700164 memac_out_32(&regs->mdio_ctl, mdio_ctl);
Roy Zangbafd8032012-10-08 07:44:21 +0000165
166 /* Set the register address */
167 if (c45)
Shaohui Xie835c72b2015-03-20 19:28:19 -0700168 memac_out_32(&regs->mdio_addr, regnum & 0xffff);
Roy Zangbafd8032012-10-08 07:44:21 +0000169
Ioana Ciorneif78f21d2020-12-09 13:31:58 +0200170 err = memac_wait_until_free(regs);
171 if (err)
172 return err;
Roy Zangbafd8032012-10-08 07:44:21 +0000173
174 /* Initiate the read */
175 mdio_ctl |= MDIO_CTL_READ;
Shaohui Xie835c72b2015-03-20 19:28:19 -0700176 memac_out_32(&regs->mdio_ctl, mdio_ctl);
Roy Zangbafd8032012-10-08 07:44:21 +0000177
Ioana Ciorneif78f21d2020-12-09 13:31:58 +0200178 err = memac_wait_until_done(regs);
179 if (err)
180 return err;
Roy Zangbafd8032012-10-08 07:44:21 +0000181
182 /* Return all Fs if nothing was there */
Shaohui Xie835c72b2015-03-20 19:28:19 -0700183 if (memac_in_32(&regs->mdio_stat) & MDIO_STAT_RD_ER)
Roy Zangbafd8032012-10-08 07:44:21 +0000184 return 0xffff;
185
Shaohui Xie835c72b2015-03-20 19:28:19 -0700186 return memac_in_32(&regs->mdio_data) & 0xffff;
Roy Zangbafd8032012-10-08 07:44:21 +0000187}
188
189int memac_mdio_reset(struct mii_dev *bus)
190{
191 return 0;
192}
193
Madalin Bucur9d327b02020-04-23 16:25:18 +0300194#if defined(CONFIG_PHYLIB) && defined(CONFIG_DM_MDIO)
195static int fm_mdio_read(struct udevice *dev, int addr, int devad, int reg)
196{
197 struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
198 NULL;
199
200 if (pdata && pdata->mii_bus)
201 return memac_mdio_read(pdata->mii_bus, addr, devad, reg);
202
203 return -1;
204}
205
206static int fm_mdio_write(struct udevice *dev, int addr, int devad, int reg,
207 u16 val)
208{
209 struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
210 NULL;
211
212 if (pdata && pdata->mii_bus)
213 return memac_mdio_write(pdata->mii_bus, addr, devad, reg, val);
214
215 return -1;
216}
217
218static int fm_mdio_reset(struct udevice *dev)
219{
220 struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
221 NULL;
222
223 if (pdata && pdata->mii_bus)
224 return memac_mdio_reset(pdata->mii_bus);
225
226 return -1;
227}
228
229static const struct mdio_ops fm_mdio_ops = {
230 .read = fm_mdio_read,
231 .write = fm_mdio_write,
232 .reset = fm_mdio_reset,
233};
234
235static const struct udevice_id fm_mdio_ids[] = {
236 { .compatible = "fsl,fman-memac-mdio" },
237 {}
238};
239
240static int fm_mdio_probe(struct udevice *dev)
241{
242 struct fm_mdio_priv *priv = (dev) ? dev_get_priv(dev) : NULL;
243 struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
244 NULL;
245
246 if (!dev) {
247 printf("%s dev = NULL\n", __func__);
248 return -1;
249 }
250 if (!priv) {
251 printf("dev_get_priv(dev %p) = NULL\n", dev);
252 return -1;
253 }
254 priv->regs = (void *)(uintptr_t)dev_read_addr(dev);
255 debug("%s priv %p @ regs %p, pdata %p\n", __func__,
256 priv, priv->regs, pdata);
257
258 /*
259 * On some platforms like B4860, default value of MDIO_CLK_DIV bits
260 * in mdio_stat(mdio_cfg) register generates MDIO clock too high
261 * (much higher than 2.5MHz), violating the IEEE specs.
262 * On other platforms like T1040, default value of MDIO_CLK_DIV bits
263 * is zero, so MDIO clock is disabled.
264 * So, for proper functioning of MDIO, MDIO_CLK_DIV bits needs to
265 * be properly initialized.
266 * The default NEG bit should be '1' as per FMANv3 RM, but on platforms
267 * like T2080QDS, this bit default is '0', which leads to MDIO failure
268 * on XAUI PHY, so set this bit definitely.
269 */
270 if (priv && priv->regs && priv->regs->mdio_stat)
271 memac_setbits_32(&priv->regs->mdio_stat,
272 MDIO_STAT_CLKDIV(258) | MDIO_STAT_NEG);
273
274 return 0;
275}
276
277static int fm_mdio_remove(struct udevice *dev)
278{
279 return 0;
280}
281
282U_BOOT_DRIVER(fman_mdio) = {
283 .name = "fman_mdio",
284 .id = UCLASS_MDIO,
285 .of_match = fm_mdio_ids,
286 .probe = fm_mdio_probe,
287 .remove = fm_mdio_remove,
288 .ops = &fm_mdio_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700289 .priv_auto = sizeof(struct fm_mdio_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700290 .plat_auto = sizeof(struct mdio_perdev_priv),
Madalin Bucur9d327b02020-04-23 16:25:18 +0300291};
292#endif /* CONFIG_PHYLIB && CONFIG_DM_MDIO */