2 * drivers/net/gianfar_mii.c
4 * Gianfar Ethernet Driver -- MIIM bus implementation
5 * Provides Bus interface for MIIM regs
8 * Maintainer: Kumar Gala (kumar.gala@freescale.com)
10 * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/unistd.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
34 #include <linux/module.h>
35 #include <linux/platform_device.h>
37 #include <linux/crc32.h>
38 #include <linux/mii.h>
39 #include <linux/phy.h>
43 #include <asm/uaccess.h>
46 #include "gianfar_mii.h"
48 /* Write value to the PHY at mii_id at register regnum,
49 * on the bus, waiting until the write is done before returning.
50 * All PHY configuration is done through the TSEC1 MIIM regs */
51 int gfar_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
53 struct gfar_mii *regs = bus->priv;
55 /* Set the PHY address and the register address we want to write */
56 gfar_write(®s->miimadd, (mii_id << 8) | regnum);
58 /* Write out the value we want */
59 gfar_write(®s->miimcon, value);
61 /* Wait for the transaction to finish */
62 while (gfar_read(®s->miimind) & MIIMIND_BUSY)
68 /* Read the bus for PHY at addr mii_id, register regnum, and
69 * return the value. Clears miimcom first. All PHY
70 * configuration has to be done through the TSEC1 MIIM regs */
71 int gfar_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
73 struct gfar_mii *regs = bus->priv;
76 /* Set the PHY address and the register address we want to read */
77 gfar_write(®s->miimadd, (mii_id << 8) | regnum);
79 /* Clear miimcom, and then initiate a read */
80 gfar_write(®s->miimcom, 0);
81 gfar_write(®s->miimcom, MII_READ_COMMAND);
83 /* Wait for the transaction to finish */
84 while (gfar_read(®s->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
87 /* Grab the value of the register from miimstat */
88 value = gfar_read(®s->miimstat);
94 /* Reset the MIIM registers, and wait for the bus to free */
95 int gfar_mdio_reset(struct mii_bus *bus)
97 struct gfar_mii *regs = bus->priv;
98 unsigned int timeout = PHY_INIT_TIMEOUT;
100 spin_lock_bh(&bus->mdio_lock);
102 /* Reset the management interface */
103 gfar_write(®s->miimcfg, MIIMCFG_RESET);
105 /* Setup the MII Mgmt clock speed */
106 gfar_write(®s->miimcfg, MIIMCFG_INIT_VALUE);
108 /* Wait until the bus is free */
109 while ((gfar_read(®s->miimind) & MIIMIND_BUSY) &&
113 spin_unlock_bh(&bus->mdio_lock);
116 printk(KERN_ERR "%s: The MII Bus is stuck!\n",
125 int gfar_mdio_probe(struct device *dev)
127 struct platform_device *pdev = to_platform_device(dev);
128 struct gianfar_mdio_data *pdata;
129 struct gfar_mii *regs;
130 struct mii_bus *new_bus;
136 new_bus = kmalloc(sizeof(struct mii_bus), GFP_KERNEL);
141 new_bus->name = "Gianfar MII Bus",
142 new_bus->read = &gfar_mdio_read,
143 new_bus->write = &gfar_mdio_write,
144 new_bus->reset = &gfar_mdio_reset,
145 new_bus->id = pdev->id;
147 pdata = (struct gianfar_mdio_data *)pdev->dev.platform_data;
150 printk(KERN_ERR "gfar mdio %d: Missing platform data!\n", pdev->id);
154 /* Set the PHY base address */
155 regs = (struct gfar_mii *) ioremap(pdata->paddr,
156 sizeof (struct gfar_mii));
163 new_bus->priv = regs;
165 new_bus->irq = pdata->irq;
168 dev_set_drvdata(dev, new_bus);
170 err = mdiobus_register(new_bus);
173 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
175 goto bus_register_fail;
181 iounmap((void *) regs);
189 int gfar_mdio_remove(struct device *dev)
191 struct mii_bus *bus = dev_get_drvdata(dev);
193 mdiobus_unregister(bus);
195 dev_set_drvdata(dev, NULL);
197 iounmap((void *) (&bus->priv));
204 static struct device_driver gianfar_mdio_driver = {
205 .name = "fsl-gianfar_mdio",
206 .bus = &platform_bus_type,
207 .probe = gfar_mdio_probe,
208 .remove = gfar_mdio_remove,
211 int __init gfar_mdio_init(void)
213 return driver_register(&gianfar_mdio_driver);
216 void __exit gfar_mdio_exit(void)
218 driver_unregister(&gianfar_mdio_driver);