2 * Copyright 2009-2010, 2013 Freescale Semiconductor, Inc.
3 * Jun-jie Zhang <b18070@freescale.com>
4 * Mingkai Hu <Mingkai.hu@freescale.com>
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/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);
24 while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--)
28 int tsec_local_mdio_read(struct tsec_mii_mng __iomem *phyregs, int port_addr,
29 int dev_addr, int regnum)
32 int timeout = 1000000;
34 /* Put the address of the phy, and the register
35 * 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);
42 /* Initiate a read command, and wait */
43 out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
46 /* Wait for the the indication that the read is done */
47 while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
51 /* Grab the value read from the PHY */
52 value = in_be32(&phyregs->miimstat);
57 static int fsl_pq_mdio_reset(struct mii_dev *bus)
59 struct tsec_mii_mng __iomem *regs =
60 (struct tsec_mii_mng __iomem *)bus->priv;
62 /* Reset MII (due to new addresses) */
63 out_be32(®s->miimcfg, MIIMCFG_RESET_MGMT);
65 out_be32(®s->miimcfg, MIIMCFG_INIT_VALUE);
67 while (in_be32(®s->miimind) & MIIMIND_BUSY)
73 int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
75 struct tsec_mii_mng __iomem *phyregs =
76 (struct tsec_mii_mng __iomem *)bus->priv;
78 return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum);
81 int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
84 struct tsec_mii_mng __iomem *phyregs =
85 (struct tsec_mii_mng __iomem *)bus->priv;
87 tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value);
92 int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
94 struct mii_dev *bus = mdio_alloc();
97 printf("Failed to allocate FSL MDIO bus\n");
101 bus->read = tsec_phy_read;
102 bus->write = tsec_phy_write;
103 bus->reset = fsl_pq_mdio_reset;
104 sprintf(bus->name, info->name);
106 bus->priv = (void *)info->regs;
108 return mdio_register(bus);