Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver |
| 3 | * |
| 4 | * Copyright (C) 2007 Domen Puncer, Telargo, Inc. |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 5 | * Copyright (C) 2008 Wolfram Sang, Pengutronix |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 6 | * |
| 7 | * This file is licensed under the terms of the GNU General Public License |
| 8 | * version 2. This program is licensed "as is" without any warranty of any |
| 9 | * kind, whether express or implied. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/phy.h> |
| 16 | #include <linux/of_platform.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Grant Likely | ca816d9 | 2009-04-25 12:53:02 +0000 | [diff] [blame] | 18 | #include <linux/of_mdio.h> |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | #include <asm/mpc52xx.h> |
| 21 | #include "fec_mpc52xx.h" |
| 22 | |
| 23 | struct mpc52xx_fec_mdio_priv { |
| 24 | struct mpc52xx_fec __iomem *regs; |
Grant Likely | ca816d9 | 2009-04-25 12:53:02 +0000 | [diff] [blame] | 25 | int mdio_irqs[PHY_MAX_ADDR]; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 28 | static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id, |
| 29 | int reg, u32 value) |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 30 | { |
| 31 | struct mpc52xx_fec_mdio_priv *priv = bus->priv; |
Wolfram Sang | e950955 | 2010-06-02 03:45:22 -0700 | [diff] [blame] | 32 | struct mpc52xx_fec __iomem *fec = priv->regs; |
Grant Likely | ca816d9 | 2009-04-25 12:53:02 +0000 | [diff] [blame] | 33 | int tries = 3; |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 34 | |
| 35 | value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK; |
| 36 | value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 37 | |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 38 | out_be32(&fec->ievent, FEC_IEVENT_MII); |
Wolfram Sang | e950955 | 2010-06-02 03:45:22 -0700 | [diff] [blame] | 39 | out_be32(&fec->mii_data, value); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 40 | |
| 41 | /* wait for it to finish, this takes about 23 us on lite5200b */ |
| 42 | while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries) |
Grant Likely | ca816d9 | 2009-04-25 12:53:02 +0000 | [diff] [blame] | 43 | msleep(1); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 44 | |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 45 | if (!tries) |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 46 | return -ETIMEDOUT; |
| 47 | |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 48 | return value & FEC_MII_DATA_OP_RD ? |
Wolfram Sang | e950955 | 2010-06-02 03:45:22 -0700 | [diff] [blame] | 49 | in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 52 | static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg) |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 53 | { |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 54 | return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 57 | static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg, |
| 58 | u16 data) |
| 59 | { |
| 60 | return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, |
| 61 | data | FEC_MII_WRITE_FRAME); |
| 62 | } |
| 63 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 64 | static int mpc52xx_fec_mdio_probe(struct platform_device *of, |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 65 | const struct of_device_id *match) |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 66 | { |
| 67 | struct device *dev = &of->dev; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 68 | struct device_node *np = of->dev.of_node; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 69 | struct mii_bus *bus; |
| 70 | struct mpc52xx_fec_mdio_priv *priv; |
Wolfram Sang | e950955 | 2010-06-02 03:45:22 -0700 | [diff] [blame] | 71 | struct resource res; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 72 | int err; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 73 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 74 | bus = mdiobus_alloc(); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 75 | if (bus == NULL) |
| 76 | return -ENOMEM; |
| 77 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 78 | if (priv == NULL) { |
| 79 | err = -ENOMEM; |
| 80 | goto out_free; |
| 81 | } |
| 82 | |
| 83 | bus->name = "mpc52xx MII bus"; |
| 84 | bus->read = mpc52xx_fec_mdio_read; |
| 85 | bus->write = mpc52xx_fec_mdio_write; |
| 86 | |
| 87 | /* setup irqs */ |
Grant Likely | ca816d9 | 2009-04-25 12:53:02 +0000 | [diff] [blame] | 88 | bus->irq = priv->mdio_irqs; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 89 | |
| 90 | /* setup registers */ |
| 91 | err = of_address_to_resource(np, 0, &res); |
| 92 | if (err) |
| 93 | goto out_free; |
Wolfram Sang | e950955 | 2010-06-02 03:45:22 -0700 | [diff] [blame] | 94 | priv->regs = ioremap(res.start, resource_size(&res)); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 95 | if (priv->regs == NULL) { |
| 96 | err = -ENOMEM; |
| 97 | goto out_free; |
| 98 | } |
| 99 | |
Andy Fleming | 9d9326d3 | 2008-04-09 19:38:13 -0500 | [diff] [blame] | 100 | snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 101 | bus->priv = priv; |
| 102 | |
Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 103 | bus->parent = dev; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 104 | dev_set_drvdata(dev, bus); |
| 105 | |
| 106 | /* set MII speed */ |
Wolfram Sang | 51d9861 | 2008-10-15 11:10:00 -0600 | [diff] [blame] | 107 | out_be32(&priv->regs->mii_speed, |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 108 | ((mpc5xxx_get_bus_frequency(of->dev.of_node) >> 20) / 5) << 1); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 109 | |
Grant Likely | ca816d9 | 2009-04-25 12:53:02 +0000 | [diff] [blame] | 110 | err = of_mdiobus_register(bus, np); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 111 | if (err) |
| 112 | goto out_unmap; |
| 113 | |
| 114 | return 0; |
| 115 | |
| 116 | out_unmap: |
| 117 | iounmap(priv->regs); |
| 118 | out_free: |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 119 | kfree(priv); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 120 | mdiobus_free(bus); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 121 | |
| 122 | return err; |
| 123 | } |
| 124 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 125 | static int mpc52xx_fec_mdio_remove(struct platform_device *of) |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 126 | { |
| 127 | struct device *dev = &of->dev; |
| 128 | struct mii_bus *bus = dev_get_drvdata(dev); |
| 129 | struct mpc52xx_fec_mdio_priv *priv = bus->priv; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 130 | |
| 131 | mdiobus_unregister(bus); |
| 132 | dev_set_drvdata(dev, NULL); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 133 | iounmap(priv->regs); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 134 | kfree(priv); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 135 | mdiobus_free(bus); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 140 | static struct of_device_id mpc52xx_fec_mdio_match[] = { |
Grant Likely | 66ffbe4 | 2008-01-24 22:25:31 -0700 | [diff] [blame] | 141 | { .compatible = "fsl,mpc5200b-mdio", }, |
René Bürgel | 8d81394 | 2008-04-03 19:58:37 +1100 | [diff] [blame] | 142 | { .compatible = "fsl,mpc5200-mdio", }, |
Grant Likely | 66ffbe4 | 2008-01-24 22:25:31 -0700 | [diff] [blame] | 143 | { .compatible = "mpc5200b-fec-phy", }, |
| 144 | {} |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 145 | }; |
Anton Vorontsov | e72701a | 2009-10-14 14:54:52 -0700 | [diff] [blame] | 146 | MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 147 | |
| 148 | struct of_platform_driver mpc52xx_fec_mdio_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 149 | .driver = { |
| 150 | .name = "mpc5200b-fec-phy", |
| 151 | .owner = THIS_MODULE, |
| 152 | .of_match_table = mpc52xx_fec_mdio_match, |
| 153 | }, |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 154 | .probe = mpc52xx_fec_mdio_probe, |
| 155 | .remove = mpc52xx_fec_mdio_remove, |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /* let fec driver call it, since this has to be registered before it */ |
| 159 | EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver); |
| 160 | |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 161 | MODULE_LICENSE("Dual BSD/GPL"); |