Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver
0003  *
0004  * Copyright (C) 2007  Domen Puncer, Telargo, Inc.
0005  * Copyright (C) 2008  Wolfram Sang, Pengutronix
0006  *
0007  * This file is licensed under the terms of the GNU General Public License
0008  * version 2. This program is licensed "as is" without any warranty of any
0009  * kind, whether express or implied.
0010  */
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/netdevice.h>
0015 #include <linux/phy.h>
0016 #include <linux/of_platform.h>
0017 #include <linux/slab.h>
0018 #include <linux/of_address.h>
0019 #include <linux/of_mdio.h>
0020 #include <asm/io.h>
0021 #include <asm/mpc52xx.h>
0022 #include "fec_mpc52xx.h"
0023 
0024 struct mpc52xx_fec_mdio_priv {
0025     struct mpc52xx_fec __iomem *regs;
0026 };
0027 
0028 static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
0029         int reg, u32 value)
0030 {
0031     struct mpc52xx_fec_mdio_priv *priv = bus->priv;
0032     struct mpc52xx_fec __iomem *fec = priv->regs;
0033     int tries = 3;
0034 
0035     value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
0036     value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
0037 
0038     out_be32(&fec->ievent, FEC_IEVENT_MII);
0039     out_be32(&fec->mii_data, value);
0040 
0041     /* wait for it to finish, this takes about 23 us on lite5200b */
0042     while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
0043         msleep(1);
0044 
0045     if (!tries)
0046         return -ETIMEDOUT;
0047 
0048     return value & FEC_MII_DATA_OP_RD ?
0049         in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0;
0050 }
0051 
0052 static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
0053 {
0054     return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
0055 }
0056 
0057 static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
0058         u16 data)
0059 {
0060     return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
0061         data | FEC_MII_WRITE_FRAME);
0062 }
0063 
0064 static int mpc52xx_fec_mdio_probe(struct platform_device *of)
0065 {
0066     struct device *dev = &of->dev;
0067     struct device_node *np = of->dev.of_node;
0068     struct mii_bus *bus;
0069     struct mpc52xx_fec_mdio_priv *priv;
0070     struct resource res;
0071     int err;
0072 
0073     bus = mdiobus_alloc();
0074     if (bus == NULL)
0075         return -ENOMEM;
0076     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0077     if (priv == NULL) {
0078         err = -ENOMEM;
0079         goto out_free;
0080     }
0081 
0082     bus->name = "mpc52xx MII bus";
0083     bus->read = mpc52xx_fec_mdio_read;
0084     bus->write = mpc52xx_fec_mdio_write;
0085 
0086     /* setup registers */
0087     err = of_address_to_resource(np, 0, &res);
0088     if (err)
0089         goto out_free;
0090     priv->regs = ioremap(res.start, resource_size(&res));
0091     if (priv->regs == NULL) {
0092         err = -ENOMEM;
0093         goto out_free;
0094     }
0095 
0096     snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
0097     bus->priv = priv;
0098 
0099     bus->parent = dev;
0100     dev_set_drvdata(dev, bus);
0101 
0102     /* set MII speed */
0103     out_be32(&priv->regs->mii_speed, ((mpc5xxx_get_bus_frequency(dev) >> 20) / 5) << 1);
0104 
0105     err = of_mdiobus_register(bus, np);
0106     if (err)
0107         goto out_unmap;
0108 
0109     return 0;
0110 
0111  out_unmap:
0112     iounmap(priv->regs);
0113  out_free:
0114     kfree(priv);
0115     mdiobus_free(bus);
0116 
0117     return err;
0118 }
0119 
0120 static int mpc52xx_fec_mdio_remove(struct platform_device *of)
0121 {
0122     struct mii_bus *bus = platform_get_drvdata(of);
0123     struct mpc52xx_fec_mdio_priv *priv = bus->priv;
0124 
0125     mdiobus_unregister(bus);
0126     iounmap(priv->regs);
0127     kfree(priv);
0128     mdiobus_free(bus);
0129 
0130     return 0;
0131 }
0132 
0133 static const struct of_device_id mpc52xx_fec_mdio_match[] = {
0134     { .compatible = "fsl,mpc5200b-mdio", },
0135     { .compatible = "fsl,mpc5200-mdio", },
0136     { .compatible = "mpc5200b-fec-phy", },
0137     {}
0138 };
0139 MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
0140 
0141 struct platform_driver mpc52xx_fec_mdio_driver = {
0142     .driver = {
0143         .name = "mpc5200b-fec-phy",
0144         .owner = THIS_MODULE,
0145         .of_match_table = mpc52xx_fec_mdio_match,
0146     },
0147     .probe = mpc52xx_fec_mdio_probe,
0148     .remove = mpc52xx_fec_mdio_remove,
0149 };
0150 
0151 /* let fec driver call it, since this has to be registered before it */
0152 EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
0153 
0154 MODULE_LICENSE("Dual BSD/GPL");