1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2009-2010, 2013 Freescale Semiconductor, Inc.
4 * Jun-jie Zhang <b18070@freescale.com>
5 * Mingkai Hu <Mingkai.hu@freescale.com>
13 #include <linux/errno.h>
16 struct tsec_mdio_priv {
17 struct tsec_mii_mng __iomem *regs;
21 void tsec_local_mdio_write(struct tsec_mii_mng __iomem *phyregs, int port_addr,
22 int dev_addr, int regnum, int value)
24 int timeout = 1000000;
26 out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
27 out_be32(&phyregs->miimcon, value);
31 while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--)
35 int tsec_local_mdio_read(struct tsec_mii_mng __iomem *phyregs, int port_addr,
36 int dev_addr, int regnum)
39 int timeout = 1000000;
41 /* Put the address of the phy, and the register number into MIIMADD */
42 out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
44 /* Clear the command register, and wait */
45 out_be32(&phyregs->miimcom, 0);
49 /* Initiate a read command, and wait */
50 out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
54 /* Wait for the the indication that the read is done */
55 while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
59 /* Grab the value read from the PHY */
60 value = in_be32(&phyregs->miimstat);
65 #if defined(CONFIG_PHYLIB)
66 static int fsl_pq_mdio_reset(struct mii_dev *bus)
68 struct tsec_mii_mng __iomem *regs;
69 #ifndef CONFIG_DM_MDIO
70 regs = (struct tsec_mii_mng __iomem *)bus->priv;
72 struct tsec_mdio_priv *priv;
77 priv = dev_get_priv(bus->priv);
81 /* Reset MII (due to new addresses) */
82 out_be32(®s->miimcfg, MIIMCFG_RESET_MGMT);
84 out_be32(®s->miimcfg, MIIMCFG_INIT_VALUE);
86 while (in_be32(®s->miimind) & MIIMIND_BUSY)
93 int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
95 struct tsec_mii_mng __iomem *phyregs;
96 #ifndef CONFIG_DM_MDIO
97 phyregs = (struct tsec_mii_mng __iomem *)bus->priv;
99 struct tsec_mdio_priv *priv;
104 priv = dev_get_priv(bus->priv);
105 phyregs = priv->regs;
108 return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum);
111 int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
114 struct tsec_mii_mng __iomem *phyregs;
115 #ifndef CONFIG_DM_MDIO
116 phyregs = (struct tsec_mii_mng __iomem *)bus->priv;
118 struct tsec_mdio_priv *priv;
123 priv = dev_get_priv(bus->priv);
124 phyregs = priv->regs;
127 tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value);
132 #ifndef CONFIG_DM_MDIO
133 int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
135 struct mii_dev *bus = mdio_alloc();
138 printf("Failed to allocate FSL MDIO bus\n");
142 bus->read = tsec_phy_read;
143 bus->write = tsec_phy_write;
144 bus->reset = fsl_pq_mdio_reset;
145 strcpy(bus->name, info->name);
147 bus->priv = (void *)info->regs;
149 return mdio_register(bus);
151 #else /* CONFIG_DM_MDIO */
152 #if defined(CONFIG_PHYLIB)
153 static int tsec_mdio_read(struct udevice *dev, int addr, int devad, int reg)
155 struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
158 if (pdata && pdata->mii_bus)
159 return tsec_phy_read(pdata->mii_bus, addr, devad, reg);
164 static int tsec_mdio_write(struct udevice *dev, int addr, int devad, int reg,
167 struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
170 if (pdata && pdata->mii_bus)
171 return tsec_phy_write(pdata->mii_bus, addr, devad, reg, val);
176 static int tsec_mdio_reset(struct udevice *dev)
178 struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
181 if (pdata && pdata->mii_bus)
182 return fsl_pq_mdio_reset(pdata->mii_bus);
187 static const struct mdio_ops tsec_mdio_ops = {
188 .read = tsec_mdio_read,
189 .write = tsec_mdio_write,
190 .reset = tsec_mdio_reset,
193 static const struct udevice_id tsec_mdio_ids[] = {
194 { .compatible = "fsl,gianfar-tbi" },
195 { .compatible = "fsl,gianfar-mdio" },
196 { .compatible = "fsl,etsec2-tbi" },
197 { .compatible = "fsl,etsec2-mdio" },
198 { .compatible = "fsl,fman-mdio" },
202 static int tsec_mdio_probe(struct udevice *dev)
204 struct tsec_mdio_priv *priv = (dev) ? dev_get_priv(dev) : NULL;
205 struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
209 printf("%s dev = NULL\n", __func__);
213 printf("dev_get_priv(dev %p) = NULL\n", dev);
216 priv->regs = (void *)(uintptr_t)dev_read_addr(dev);
217 debug("%s priv %p @ regs %p, pdata %p\n", __func__,
218 priv, priv->regs, pdata);
223 static int tsec_mdio_remove(struct udevice *dev)
228 U_BOOT_DRIVER(tsec_mdio) = {
231 .of_match = tsec_mdio_ids,
232 .probe = tsec_mdio_probe,
233 .remove = tsec_mdio_remove,
234 .ops = &tsec_mdio_ops,
235 .priv_auto_alloc_size = sizeof(struct tsec_mdio_priv),
236 .platdata_auto_alloc_size = sizeof(struct mdio_perdev_priv),
238 #endif /* CONFIG_PHYLIB */
239 #endif /* CONFIG_DM_MDIO */