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>
15 void tsec_local_mdio_write(struct tsec_mii_mng __iomem *phyregs, int port_addr,
16 int dev_addr, int regnum, int value)
18 int timeout = 1000000;
20 out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
21 out_be32(&phyregs->miimcon, value);
25 while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--)
29 int tsec_local_mdio_read(struct tsec_mii_mng __iomem *phyregs, int port_addr,
30 int dev_addr, int regnum)
33 int timeout = 1000000;
35 /* Put the address of the phy, and the register number into MIIMADD */
36 out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
38 /* Clear the command register, and wait */
39 out_be32(&phyregs->miimcom, 0);
43 /* Initiate a read command, and wait */
44 out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
48 /* Wait for the the indication that the read is done */
49 while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
53 /* Grab the value read from the PHY */
54 value = in_be32(&phyregs->miimstat);
59 static int fsl_pq_mdio_reset(struct mii_dev *bus)
61 struct tsec_mii_mng __iomem *regs =
62 (struct tsec_mii_mng __iomem *)bus->priv;
64 /* Reset MII (due to new addresses) */
65 out_be32(®s->miimcfg, MIIMCFG_RESET_MGMT);
67 out_be32(®s->miimcfg, MIIMCFG_INIT_VALUE);
69 while (in_be32(®s->miimind) & MIIMIND_BUSY)
75 int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
77 struct tsec_mii_mng __iomem *phyregs =
78 (struct tsec_mii_mng __iomem *)bus->priv;
80 return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum);
83 int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
86 struct tsec_mii_mng __iomem *phyregs =
87 (struct tsec_mii_mng __iomem *)bus->priv;
89 tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value);
94 int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
96 struct mii_dev *bus = mdio_alloc();
99 printf("Failed to allocate FSL MDIO bus\n");
103 bus->read = tsec_phy_read;
104 bus->write = tsec_phy_write;
105 bus->reset = fsl_pq_mdio_reset;
106 strcpy(bus->name, info->name);
108 bus->priv = (void *)info->regs;
110 return mdio_register(bus);