2 * Copyright (C) 2006-2007 PA Semi, Inc
4 * Author: Olof Johansson, PA Semi
6 * Maintained by: Olof Johansson <olof@lixom.net>
8 * Based on drivers/net/fs_enet/mii-bitbang.c.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/sched.h>
28 #include <linux/errno.h>
29 #include <linux/ioport.h>
30 #include <linux/interrupt.h>
31 #include <linux/phy.h>
32 #include <linux/platform_device.h>
33 #include <asm/of_platform.h>
37 static void __iomem *gpio_regs;
44 #define MDC_PIN(bus) (((struct gpio_priv *)bus->priv)->mdc_pin)
45 #define MDIO_PIN(bus) (((struct gpio_priv *)bus->priv)->mdio_pin)
47 static inline void mdio_lo(struct mii_bus *bus)
49 out_le32(gpio_regs+0x10, 1 << MDIO_PIN(bus));
52 static inline void mdio_hi(struct mii_bus *bus)
54 out_le32(gpio_regs, 1 << MDIO_PIN(bus));
57 static inline void mdc_lo(struct mii_bus *bus)
59 out_le32(gpio_regs+0x10, 1 << MDC_PIN(bus));
62 static inline void mdc_hi(struct mii_bus *bus)
64 out_le32(gpio_regs, 1 << MDC_PIN(bus));
67 static inline void mdio_active(struct mii_bus *bus)
69 out_le32(gpio_regs+0x20, (1 << MDC_PIN(bus)) | (1 << MDIO_PIN(bus)));
72 static inline void mdio_tristate(struct mii_bus *bus)
74 out_le32(gpio_regs+0x30, (1 << MDIO_PIN(bus)));
77 static inline int mdio_read(struct mii_bus *bus)
79 return !!(in_le32(gpio_regs+0x40) & (1 << MDIO_PIN(bus)));
82 static void clock_out(struct mii_bus *bus, int bit)
94 /* Utility to send the preamble, address, and register (common to read and write). */
95 static void bitbang_pre(struct mii_bus *bus, int read, u8 addr, u8 reg)
99 /* CFE uses a really long preamble (40 bits). We'll do the same. */
101 for (i = 0; i < 40; i++) {
105 /* send the start bit (01) and the read opcode (10) or write (10) */
109 clock_out(bus, read);
110 clock_out(bus, !read);
112 /* send the PHY address */
113 for (i = 0; i < 5; i++) {
114 clock_out(bus, (addr & 0x10) != 0);
118 /* send the register address */
119 for (i = 0; i < 5; i++) {
120 clock_out(bus, (reg & 0x10) != 0);
125 static int gpio_mdio_read(struct mii_bus *bus, int phy_id, int location)
129 u8 addr = phy_id & 0xff;
130 u8 reg = location & 0xff;
132 bitbang_pre(bus, 1, addr, reg);
134 /* tri-state our MDIO I/O pin so we can read */
141 /* read 16 bits of register data, MSB first */
143 for (i = 0; i < 16; i++) {
151 rdreg |= mdio_read(bus);
164 static int gpio_mdio_write(struct mii_bus *bus, int phy_id, int location, u16 val)
168 u8 addr = phy_id & 0xff;
169 u8 reg = location & 0xff;
170 u16 value = val & 0xffff;
172 bitbang_pre(bus, 0, addr, reg);
174 /* send the turnaround (10) */
186 /* write 16 bits of register data, MSB first */
187 for (i = 0; i < 16; i++) {
200 * Tri-state the MDIO line.
210 static int gpio_mdio_reset(struct mii_bus *bus)
212 /*nothing here - dunno how to reset it*/
217 static int __devinit gpio_mdio_probe(struct of_device *ofdev,
218 const struct of_device_id *match)
220 struct device *dev = &ofdev->dev;
221 struct device_node *np = ofdev->node;
222 struct device_node *gpio_np;
223 struct mii_bus *new_bus;
225 struct gpio_priv *priv;
226 const unsigned int *prop;
230 gpio_np = of_find_compatible_node(NULL, "gpio", "1682m-gpio");
235 err = of_address_to_resource(gpio_np, 0, &res);
236 of_node_put(gpio_np);
242 gpio_regs = ioremap(res.start, 0x100);
247 priv = kzalloc(sizeof(struct gpio_priv), GFP_KERNEL);
251 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
256 new_bus->name = "pasemi gpio mdio bus",
257 new_bus->read = &gpio_mdio_read,
258 new_bus->write = &gpio_mdio_write,
259 new_bus->reset = &gpio_mdio_reset,
261 prop = of_get_property(np, "reg", NULL);
263 new_bus->priv = priv;
265 new_bus->phy_mask = 0;
267 new_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
268 for(i = 0; i < PHY_MAX_ADDR; ++i)
269 new_bus->irq[i] = irq_create_mapping(NULL, 10);
272 prop = of_get_property(np, "mdc-pin", NULL);
273 priv->mdc_pin = *prop;
275 prop = of_get_property(np, "mdio-pin", NULL);
276 priv->mdio_pin = *prop;
279 dev_set_drvdata(dev, new_bus);
281 err = mdiobus_register(new_bus);
284 printk(KERN_ERR "%s: Cannot register as MDIO bus, err %d\n",
286 goto bus_register_fail;
298 static int gpio_mdio_remove(struct of_device *dev)
300 struct mii_bus *bus = dev_get_drvdata(&dev->dev);
302 mdiobus_unregister(bus);
304 dev_set_drvdata(&dev->dev, NULL);
313 static struct of_device_id gpio_mdio_match[] =
316 .compatible = "gpio-mdio",
321 static struct of_platform_driver gpio_mdio_driver =
323 .match_table = gpio_mdio_match,
324 .probe = gpio_mdio_probe,
325 .remove = gpio_mdio_remove,
327 .name = "gpio-mdio-bitbang",
331 int gpio_mdio_init(void)
333 return of_register_platform_driver(&gpio_mdio_driver);
336 void gpio_mdio_exit(void)
338 of_unregister_platform_driver(&gpio_mdio_driver);
340 device_initcall(gpio_mdio_init);