linux/drivers/net/fs_enet/mii-bitbang.c
<<
>>
Prefs
   1/*
   2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
   3 *
   4 * Copyright (c) 2003 Intracom S.A.
   5 *  by Pantelis Antoniou <panto@intracom.gr>
   6 *
   7 * 2005 (c) MontaVista Software, Inc.
   8 * Vitaly Bordug <vbordug@ru.mvista.com>
   9 *
  10 * This file is licensed under the terms of the GNU General Public License
  11 * version 2. This program is licensed "as is" without any warranty of any
  12 * kind, whether express or implied.
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/ioport.h>
  17#include <linux/slab.h>
  18#include <linux/init.h>
  19#include <linux/interrupt.h>
  20#include <linux/netdevice.h>
  21#include <linux/etherdevice.h>
  22#include <linux/mii.h>
  23#include <linux/platform_device.h>
  24#include <linux/mdio-bitbang.h>
  25#include <linux/of_mdio.h>
  26#include <linux/of_platform.h>
  27
  28#include "fs_enet.h"
  29
  30struct bb_info {
  31        struct mdiobb_ctrl ctrl;
  32        __be32 __iomem *dir;
  33        __be32 __iomem *dat;
  34        u32 mdio_msk;
  35        u32 mdc_msk;
  36};
  37
  38/* FIXME: If any other users of GPIO crop up, then these will have to
  39 * have some sort of global synchronization to avoid races with other
  40 * pins on the same port.  The ideal solution would probably be to
  41 * bind the ports to a GPIO driver, and have this be a client of it.
  42 */
  43static inline void bb_set(u32 __iomem *p, u32 m)
  44{
  45        out_be32(p, in_be32(p) | m);
  46}
  47
  48static inline void bb_clr(u32 __iomem *p, u32 m)
  49{
  50        out_be32(p, in_be32(p) & ~m);
  51}
  52
  53static inline int bb_read(u32 __iomem *p, u32 m)
  54{
  55        return (in_be32(p) & m) != 0;
  56}
  57
  58static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
  59{
  60        struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  61
  62        if (dir)
  63                bb_set(bitbang->dir, bitbang->mdio_msk);
  64        else
  65                bb_clr(bitbang->dir, bitbang->mdio_msk);
  66
  67        /* Read back to flush the write. */
  68        in_be32(bitbang->dir);
  69}
  70
  71static inline int mdio_read(struct mdiobb_ctrl *ctrl)
  72{
  73        struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  74        return bb_read(bitbang->dat, bitbang->mdio_msk);
  75}
  76
  77static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
  78{
  79        struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  80
  81        if (what)
  82                bb_set(bitbang->dat, bitbang->mdio_msk);
  83        else
  84                bb_clr(bitbang->dat, bitbang->mdio_msk);
  85
  86        /* Read back to flush the write. */
  87        in_be32(bitbang->dat);
  88}
  89
  90static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
  91{
  92        struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  93
  94        if (what)
  95                bb_set(bitbang->dat, bitbang->mdc_msk);
  96        else
  97                bb_clr(bitbang->dat, bitbang->mdc_msk);
  98
  99        /* Read back to flush the write. */
 100        in_be32(bitbang->dat);
 101}
 102
 103static struct mdiobb_ops bb_ops = {
 104        .owner = THIS_MODULE,
 105        .set_mdc = mdc,
 106        .set_mdio_dir = mdio_dir,
 107        .set_mdio_data = mdio,
 108        .get_mdio_data = mdio_read,
 109};
 110
 111static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
 112                                         struct device_node *np)
 113{
 114        struct resource res;
 115        const u32 *data;
 116        int mdio_pin, mdc_pin, len;
 117        struct bb_info *bitbang = bus->priv;
 118
 119        int ret = of_address_to_resource(np, 0, &res);
 120        if (ret)
 121                return ret;
 122
 123        if (res.end - res.start < 13)
 124                return -ENODEV;
 125
 126        /* This should really encode the pin number as well, but all
 127         * we get is an int, and the odds of multiple bitbang mdio buses
 128         * is low enough that it's not worth going too crazy.
 129         */
 130        snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
 131
 132        data = of_get_property(np, "fsl,mdio-pin", &len);
 133        if (!data || len != 4)
 134                return -ENODEV;
 135        mdio_pin = *data;
 136
 137        data = of_get_property(np, "fsl,mdc-pin", &len);
 138        if (!data || len != 4)
 139                return -ENODEV;
 140        mdc_pin = *data;
 141
 142        bitbang->dir = ioremap(res.start, res.end - res.start + 1);
 143        if (!bitbang->dir)
 144                return -ENOMEM;
 145
 146        bitbang->dat = bitbang->dir + 4;
 147        bitbang->mdio_msk = 1 << (31 - mdio_pin);
 148        bitbang->mdc_msk = 1 << (31 - mdc_pin);
 149
 150        return 0;
 151}
 152
 153static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
 154                                        const struct of_device_id *match)
 155{
 156        struct mii_bus *new_bus;
 157        struct bb_info *bitbang;
 158        int ret = -ENOMEM;
 159
 160        bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
 161        if (!bitbang)
 162                goto out;
 163
 164        bitbang->ctrl.ops = &bb_ops;
 165
 166        new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
 167        if (!new_bus)
 168                goto out_free_priv;
 169
 170        new_bus->name = "CPM2 Bitbanged MII",
 171
 172        ret = fs_mii_bitbang_init(new_bus, ofdev->node);
 173        if (ret)
 174                goto out_free_bus;
 175
 176        new_bus->phy_mask = ~0;
 177        new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
 178        if (!new_bus->irq)
 179                goto out_unmap_regs;
 180
 181        new_bus->parent = &ofdev->dev;
 182        dev_set_drvdata(&ofdev->dev, new_bus);
 183
 184        ret = of_mdiobus_register(new_bus, ofdev->node);
 185        if (ret)
 186                goto out_free_irqs;
 187
 188        return 0;
 189
 190out_free_irqs:
 191        dev_set_drvdata(&ofdev->dev, NULL);
 192        kfree(new_bus->irq);
 193out_unmap_regs:
 194        iounmap(bitbang->dir);
 195out_free_bus:
 196        free_mdio_bitbang(new_bus);
 197out_free_priv:
 198        kfree(bitbang);
 199out:
 200        return ret;
 201}
 202
 203static int fs_enet_mdio_remove(struct of_device *ofdev)
 204{
 205        struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
 206        struct bb_info *bitbang = bus->priv;
 207
 208        mdiobus_unregister(bus);
 209        dev_set_drvdata(&ofdev->dev, NULL);
 210        kfree(bus->irq);
 211        free_mdio_bitbang(bus);
 212        iounmap(bitbang->dir);
 213        kfree(bitbang);
 214
 215        return 0;
 216}
 217
 218static struct of_device_id fs_enet_mdio_bb_match[] = {
 219        {
 220                .compatible = "fsl,cpm2-mdio-bitbang",
 221        },
 222        {},
 223};
 224MODULE_DEVICE_TABLE(of, fs_enet_mdio_bb_match);
 225
 226static struct of_platform_driver fs_enet_bb_mdio_driver = {
 227        .name = "fsl-bb-mdio",
 228        .match_table = fs_enet_mdio_bb_match,
 229        .probe = fs_enet_mdio_probe,
 230        .remove = fs_enet_mdio_remove,
 231};
 232
 233static int fs_enet_mdio_bb_init(void)
 234{
 235        return of_register_platform_driver(&fs_enet_bb_mdio_driver);
 236}
 237
 238static void fs_enet_mdio_bb_exit(void)
 239{
 240        of_unregister_platform_driver(&fs_enet_bb_mdio_driver);
 241}
 242
 243module_init(fs_enet_mdio_bb_init);
 244module_exit(fs_enet_mdio_bb_exit);
 245