2 * Copyright 2009-2011 Freescale Semiconductor, Inc.
3 * Andy Fleming <afleming@freescale.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * Some part is taken from tsec.c
25 #include <asm/fsl_tgec.h>
29 * Write value to the PHY for this device to the register at regnum, waiting
30 * until the write is done before it returns. All PHY configuration has to be
31 * done through the TSEC1 MIIM regs
33 int tgec_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr,
34 int regnum, u16 value)
38 struct tgec_mdio_controller *regs = bus->priv;
40 if (dev_addr == MDIO_DEVAD_NONE)
43 /* Wait till the bus is free */
44 stat_val = MDIO_STAT_CLKDIV(100);
45 out_be32(®s->mdio_stat, stat_val);
46 while ((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY)
49 /* Set the port and dev addr */
50 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
51 out_be32(®s->mdio_ctl, mdio_ctl);
53 /* Set the register address */
54 out_be32(®s->mdio_addr, regnum & 0xffff);
56 /* Wait till the bus is free */
57 while ((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY)
60 /* Write the value to the register */
61 out_be32(®s->mdio_data, MDIO_DATA(value));
63 /* Wait till the MDIO write is complete */
64 while ((in_be32(®s->mdio_data)) & MDIO_DATA_BSY)
71 * Reads from register regnum in the PHY for device dev, returning the value.
72 * Clears miimcom first. All PHY configuration has to be done through the
75 int tgec_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
80 struct tgec_mdio_controller *regs = bus->priv;
82 if (dev_addr == MDIO_DEVAD_NONE)
85 stat_val = MDIO_STAT_CLKDIV(100);
86 out_be32(®s->mdio_stat, stat_val);
87 /* Wait till the bus is free */
88 while ((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY)
91 /* Set the Port and Device Addrs */
92 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
93 out_be32(®s->mdio_ctl, mdio_ctl);
95 /* Set the register address */
96 out_be32(®s->mdio_addr, regnum & 0xffff);
98 /* Wait till the bus is free */
99 while ((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY)
102 /* Initiate the read */
103 mdio_ctl |= MDIO_CTL_READ;
104 out_be32(®s->mdio_ctl, mdio_ctl);
106 /* Wait till the MDIO write is complete */
107 while ((in_be32(®s->mdio_data)) & MDIO_DATA_BSY)
110 /* Return all Fs if nothing was there */
111 if (in_be32(®s->mdio_stat) & MDIO_STAT_RD_ER)
114 return in_be32(®s->mdio_data) & 0xffff;
117 int tgec_mdio_reset(struct mii_dev *bus)
122 int fm_tgec_mdio_init(bd_t *bis, struct tgec_mdio_info *info)
124 struct mii_dev *bus = mdio_alloc();
127 printf("Failed to allocate FM TGEC MDIO bus\n");
131 bus->read = tgec_mdio_read;
132 bus->write = tgec_mdio_write;
133 bus->reset = tgec_mdio_reset;
134 sprintf(bus->name, info->name);
136 bus->priv = info->regs;
138 return mdio_register(bus);