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>
14 #include <asm/fsl_enet.h>
16 void tsec_local_mdio_write(struct tsec_mii_mng __iomem *phyregs, int port_addr,
17 int dev_addr, int regnum, int value)
19 int timeout = 1000000;
21 out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
22 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
36 * number into MIIMADD */
37 out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
39 /* Clear the command register, and wait */
40 out_be32(&phyregs->miimcom, 0);
43 /* Initiate a read command, and wait */
44 out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
47 /* Wait for the the indication that the read is done */
48 while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
52 /* Grab the value read from the PHY */
53 value = in_be32(&phyregs->miimstat);
58 static int fsl_pq_mdio_reset(struct mii_dev *bus)
60 struct tsec_mii_mng __iomem *regs =
61 (struct tsec_mii_mng __iomem *)bus->priv;
63 /* Reset MII (due to new addresses) */
64 out_be32(®s->miimcfg, MIIMCFG_RESET_MGMT);
66 out_be32(®s->miimcfg, MIIMCFG_INIT_VALUE);
68 while (in_be32(®s->miimind) & MIIMIND_BUSY)
74 int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
76 struct tsec_mii_mng __iomem *phyregs =
77 (struct tsec_mii_mng __iomem *)bus->priv;
79 return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum);
82 int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
85 struct tsec_mii_mng __iomem *phyregs =
86 (struct tsec_mii_mng __iomem *)bus->priv;
88 tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value);
93 int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
95 struct mii_dev *bus = mdio_alloc();
98 printf("Failed to allocate FSL MDIO bus\n");
102 bus->read = tsec_phy_read;
103 bus->write = tsec_phy_write;
104 bus->reset = fsl_pq_mdio_reset;
105 sprintf(bus->name, info->name);
107 bus->priv = (void *)info->regs;
109 return mdio_register(bus);