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. |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public License |
| 7 | * version 2. This program is licensed "as is" without any warranty of any |
| 8 | * kind, whether express or implied. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/phy.h> |
| 15 | #include <linux/of_platform.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <asm/mpc52xx.h> |
| 18 | #include "fec_mpc52xx.h" |
| 19 | |
| 20 | struct mpc52xx_fec_mdio_priv { |
| 21 | struct mpc52xx_fec __iomem *regs; |
| 22 | }; |
| 23 | |
| 24 | static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg) |
| 25 | { |
| 26 | struct mpc52xx_fec_mdio_priv *priv = bus->priv; |
| 27 | struct mpc52xx_fec __iomem *fec; |
| 28 | int tries = 100; |
| 29 | u32 request = FEC_MII_READ_FRAME; |
| 30 | |
| 31 | fec = priv->regs; |
| 32 | out_be32(&fec->ievent, FEC_IEVENT_MII); |
| 33 | |
| 34 | request |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK; |
| 35 | request |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK; |
| 36 | |
| 37 | out_be32(&priv->regs->mii_data, request); |
| 38 | |
| 39 | /* wait for it to finish, this takes about 23 us on lite5200b */ |
| 40 | while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries) |
| 41 | udelay(5); |
| 42 | |
| 43 | if (tries == 0) |
| 44 | return -ETIMEDOUT; |
| 45 | |
| 46 | return in_be32(&priv->regs->mii_data) & FEC_MII_DATA_DATAMSK; |
| 47 | } |
| 48 | |
| 49 | static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 data) |
| 50 | { |
| 51 | struct mpc52xx_fec_mdio_priv *priv = bus->priv; |
| 52 | struct mpc52xx_fec __iomem *fec; |
| 53 | u32 value = data; |
| 54 | int tries = 100; |
| 55 | |
| 56 | fec = priv->regs; |
| 57 | out_be32(&fec->ievent, FEC_IEVENT_MII); |
| 58 | |
| 59 | value |= FEC_MII_WRITE_FRAME; |
| 60 | value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK; |
| 61 | value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK; |
| 62 | |
| 63 | out_be32(&priv->regs->mii_data, value); |
| 64 | |
| 65 | /* wait for request to finish */ |
| 66 | while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries) |
| 67 | udelay(5); |
| 68 | |
| 69 | if (tries == 0) |
| 70 | return -ETIMEDOUT; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int mpc52xx_fec_mdio_probe(struct of_device *of, const struct of_device_id *match) |
| 76 | { |
| 77 | struct device *dev = &of->dev; |
| 78 | struct device_node *np = of->node; |
| 79 | struct device_node *child = NULL; |
| 80 | struct mii_bus *bus; |
| 81 | struct mpc52xx_fec_mdio_priv *priv; |
| 82 | struct resource res = {}; |
| 83 | int err; |
| 84 | int i; |
| 85 | |
Lennert Buytenhek | 298cf9be | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 86 | bus = mdiobus_alloc(); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 87 | if (bus == NULL) |
| 88 | return -ENOMEM; |
| 89 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 90 | if (priv == NULL) { |
| 91 | err = -ENOMEM; |
| 92 | goto out_free; |
| 93 | } |
| 94 | |
| 95 | bus->name = "mpc52xx MII bus"; |
| 96 | bus->read = mpc52xx_fec_mdio_read; |
| 97 | bus->write = mpc52xx_fec_mdio_write; |
| 98 | |
| 99 | /* setup irqs */ |
| 100 | bus->irq = kmalloc(sizeof(bus->irq[0]) * PHY_MAX_ADDR, GFP_KERNEL); |
| 101 | if (bus->irq == NULL) { |
| 102 | err = -ENOMEM; |
| 103 | goto out_free; |
| 104 | } |
| 105 | for (i=0; i<PHY_MAX_ADDR; i++) |
| 106 | bus->irq[i] = PHY_POLL; |
| 107 | |
| 108 | while ((child = of_get_next_child(np, child)) != NULL) { |
| 109 | int irq = irq_of_parse_and_map(child, 0); |
| 110 | if (irq != NO_IRQ) { |
| 111 | const u32 *id = of_get_property(child, "reg", NULL); |
Grant Likely | b8c19eb | 2008-03-22 14:20:29 +1100 | [diff] [blame] | 112 | if (id) |
| 113 | bus->irq[*id] = irq; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | /* setup registers */ |
| 118 | err = of_address_to_resource(np, 0, &res); |
| 119 | if (err) |
| 120 | goto out_free; |
| 121 | priv->regs = ioremap(res.start, res.end - res.start + 1); |
| 122 | if (priv->regs == NULL) { |
| 123 | err = -ENOMEM; |
| 124 | goto out_free; |
| 125 | } |
| 126 | |
Andy Fleming | 9d9326d | 2008-04-09 19:38:13 -0500 | [diff] [blame] | 127 | snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 128 | bus->priv = priv; |
| 129 | |
Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 130 | bus->parent = dev; |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 131 | dev_set_drvdata(dev, bus); |
| 132 | |
| 133 | /* set MII speed */ |
| 134 | out_be32(&priv->regs->mii_speed, ((mpc52xx_find_ipb_freq(of->node) >> 20) / 5) << 1); |
| 135 | |
| 136 | /* enable MII interrupt */ |
| 137 | out_be32(&priv->regs->imask, in_be32(&priv->regs->imask) | FEC_IMASK_MII); |
| 138 | |
| 139 | err = mdiobus_register(bus); |
| 140 | if (err) |
| 141 | goto out_unmap; |
| 142 | |
| 143 | return 0; |
| 144 | |
| 145 | out_unmap: |
| 146 | iounmap(priv->regs); |
| 147 | out_free: |
| 148 | for (i=0; i<PHY_MAX_ADDR; i++) |
| 149 | if (bus->irq[i] != PHY_POLL) |
| 150 | irq_dispose_mapping(bus->irq[i]); |
| 151 | kfree(bus->irq); |
| 152 | kfree(priv); |
Lennert Buytenhek | 298cf9be | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 153 | mdiobus_free(bus); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 154 | |
| 155 | return err; |
| 156 | } |
| 157 | |
| 158 | static int mpc52xx_fec_mdio_remove(struct of_device *of) |
| 159 | { |
| 160 | struct device *dev = &of->dev; |
| 161 | struct mii_bus *bus = dev_get_drvdata(dev); |
| 162 | struct mpc52xx_fec_mdio_priv *priv = bus->priv; |
| 163 | int i; |
| 164 | |
| 165 | mdiobus_unregister(bus); |
| 166 | dev_set_drvdata(dev, NULL); |
| 167 | |
| 168 | iounmap(priv->regs); |
| 169 | for (i=0; i<PHY_MAX_ADDR; i++) |
| 170 | if (bus->irq[i]) |
| 171 | irq_dispose_mapping(bus->irq[i]); |
| 172 | kfree(priv); |
| 173 | kfree(bus->irq); |
Lennert Buytenhek | 298cf9be | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 174 | mdiobus_free(bus); |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static struct of_device_id mpc52xx_fec_mdio_match[] = { |
Grant Likely | 66ffbe4 | 2008-01-24 22:25:31 -0700 | [diff] [blame] | 181 | { .compatible = "fsl,mpc5200b-mdio", }, |
René Bürgel | 8d81394 | 2008-04-03 19:58:37 +1100 | [diff] [blame] | 182 | { .compatible = "fsl,mpc5200-mdio", }, |
Grant Likely | 66ffbe4 | 2008-01-24 22:25:31 -0700 | [diff] [blame] | 183 | { .compatible = "mpc5200b-fec-phy", }, |
| 184 | {} |
Domen Puncer | 5d031e9 | 2007-10-26 18:07:49 +0200 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | struct of_platform_driver mpc52xx_fec_mdio_driver = { |
| 188 | .name = "mpc5200b-fec-phy", |
| 189 | .probe = mpc52xx_fec_mdio_probe, |
| 190 | .remove = mpc52xx_fec_mdio_remove, |
| 191 | .match_table = mpc52xx_fec_mdio_match, |
| 192 | }; |
| 193 | |
| 194 | /* let fec driver call it, since this has to be registered before it */ |
| 195 | EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver); |
| 196 | |
| 197 | |
| 198 | MODULE_LICENSE("Dual BSD/GPL"); |