1b97d2791SOlof Johansson /* 2b97d2791SOlof Johansson * Copyright (C) 2006-2007 PA Semi, Inc 3b97d2791SOlof Johansson * 4b97d2791SOlof Johansson * Author: Olof Johansson, PA Semi 5b97d2791SOlof Johansson * 6b97d2791SOlof Johansson * Maintained by: Olof Johansson <olof@lixom.net> 7b97d2791SOlof Johansson * 8b97d2791SOlof Johansson * Based on drivers/net/fs_enet/mii-bitbang.c. 9b97d2791SOlof Johansson * 10b97d2791SOlof Johansson * This program is free software; you can redistribute it and/or modify 11b97d2791SOlof Johansson * it under the terms of the GNU General Public License version 2 as 12b97d2791SOlof Johansson * published by the Free Software Foundation. 13b97d2791SOlof Johansson * 14b97d2791SOlof Johansson * This program is distributed in the hope that it will be useful, 15b97d2791SOlof Johansson * but WITHOUT ANY WARRANTY; without even the implied warranty of 16b97d2791SOlof Johansson * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17b97d2791SOlof Johansson * GNU General Public License for more details. 18b97d2791SOlof Johansson * 19b97d2791SOlof Johansson * You should have received a copy of the GNU General Public License 20b97d2791SOlof Johansson * along with this program; if not, write to the Free Software 21b97d2791SOlof Johansson * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22b97d2791SOlof Johansson */ 23b97d2791SOlof Johansson 24b97d2791SOlof Johansson #include <linux/io.h> 25b97d2791SOlof Johansson #include <linux/module.h> 26b97d2791SOlof Johansson #include <linux/types.h> 275a0e3ad6STejun Heo #include <linux/slab.h> 28b97d2791SOlof Johansson #include <linux/sched.h> 29b97d2791SOlof Johansson #include <linux/errno.h> 30b97d2791SOlof Johansson #include <linux/ioport.h> 31b97d2791SOlof Johansson #include <linux/interrupt.h> 32b97d2791SOlof Johansson #include <linux/phy.h> 331dd2d06cSGrant Likely #include <linux/of_mdio.h> 345f867dc7SJon Loeliger #include <linux/of_platform.h> 35b97d2791SOlof Johansson 36b97d2791SOlof Johansson #define DELAY 1 37b97d2791SOlof Johansson 38b97d2791SOlof Johansson static void __iomem *gpio_regs; 39b97d2791SOlof Johansson 40b97d2791SOlof Johansson struct gpio_priv { 41b97d2791SOlof Johansson int mdc_pin; 42b97d2791SOlof Johansson int mdio_pin; 431dd2d06cSGrant Likely int mdio_irqs[PHY_MAX_ADDR]; 44b97d2791SOlof Johansson }; 45b97d2791SOlof Johansson 46b97d2791SOlof Johansson #define MDC_PIN(bus) (((struct gpio_priv *)bus->priv)->mdc_pin) 47b97d2791SOlof Johansson #define MDIO_PIN(bus) (((struct gpio_priv *)bus->priv)->mdio_pin) 48b97d2791SOlof Johansson 49b97d2791SOlof Johansson static inline void mdio_lo(struct mii_bus *bus) 50b97d2791SOlof Johansson { 51b97d2791SOlof Johansson out_le32(gpio_regs+0x10, 1 << MDIO_PIN(bus)); 52b97d2791SOlof Johansson } 53b97d2791SOlof Johansson 54b97d2791SOlof Johansson static inline void mdio_hi(struct mii_bus *bus) 55b97d2791SOlof Johansson { 56b97d2791SOlof Johansson out_le32(gpio_regs, 1 << MDIO_PIN(bus)); 57b97d2791SOlof Johansson } 58b97d2791SOlof Johansson 59b97d2791SOlof Johansson static inline void mdc_lo(struct mii_bus *bus) 60b97d2791SOlof Johansson { 61b97d2791SOlof Johansson out_le32(gpio_regs+0x10, 1 << MDC_PIN(bus)); 62b97d2791SOlof Johansson } 63b97d2791SOlof Johansson 64b97d2791SOlof Johansson static inline void mdc_hi(struct mii_bus *bus) 65b97d2791SOlof Johansson { 66b97d2791SOlof Johansson out_le32(gpio_regs, 1 << MDC_PIN(bus)); 67b97d2791SOlof Johansson } 68b97d2791SOlof Johansson 69b97d2791SOlof Johansson static inline void mdio_active(struct mii_bus *bus) 70b97d2791SOlof Johansson { 71b97d2791SOlof Johansson out_le32(gpio_regs+0x20, (1 << MDC_PIN(bus)) | (1 << MDIO_PIN(bus))); 72b97d2791SOlof Johansson } 73b97d2791SOlof Johansson 74b97d2791SOlof Johansson static inline void mdio_tristate(struct mii_bus *bus) 75b97d2791SOlof Johansson { 76b97d2791SOlof Johansson out_le32(gpio_regs+0x30, (1 << MDIO_PIN(bus))); 77b97d2791SOlof Johansson } 78b97d2791SOlof Johansson 79b97d2791SOlof Johansson static inline int mdio_read(struct mii_bus *bus) 80b97d2791SOlof Johansson { 81b97d2791SOlof Johansson return !!(in_le32(gpio_regs+0x40) & (1 << MDIO_PIN(bus))); 82b97d2791SOlof Johansson } 83b97d2791SOlof Johansson 84b97d2791SOlof Johansson static void clock_out(struct mii_bus *bus, int bit) 85b97d2791SOlof Johansson { 86b97d2791SOlof Johansson if (bit) 87b97d2791SOlof Johansson mdio_hi(bus); 88b97d2791SOlof Johansson else 89b97d2791SOlof Johansson mdio_lo(bus); 90b97d2791SOlof Johansson udelay(DELAY); 91b97d2791SOlof Johansson mdc_hi(bus); 92b97d2791SOlof Johansson udelay(DELAY); 93b97d2791SOlof Johansson mdc_lo(bus); 94b97d2791SOlof Johansson } 95b97d2791SOlof Johansson 96b97d2791SOlof Johansson /* Utility to send the preamble, address, and register (common to read and write). */ 97b97d2791SOlof Johansson static void bitbang_pre(struct mii_bus *bus, int read, u8 addr, u8 reg) 98b97d2791SOlof Johansson { 99b97d2791SOlof Johansson int i; 100b97d2791SOlof Johansson 101b97d2791SOlof Johansson /* CFE uses a really long preamble (40 bits). We'll do the same. */ 102b97d2791SOlof Johansson mdio_active(bus); 103b97d2791SOlof Johansson for (i = 0; i < 40; i++) { 104b97d2791SOlof Johansson clock_out(bus, 1); 105b97d2791SOlof Johansson } 106b97d2791SOlof Johansson 107b97d2791SOlof Johansson /* send the start bit (01) and the read opcode (10) or write (10) */ 108b97d2791SOlof Johansson clock_out(bus, 0); 109b97d2791SOlof Johansson clock_out(bus, 1); 110b97d2791SOlof Johansson 111b97d2791SOlof Johansson clock_out(bus, read); 112b97d2791SOlof Johansson clock_out(bus, !read); 113b97d2791SOlof Johansson 114b97d2791SOlof Johansson /* send the PHY address */ 115b97d2791SOlof Johansson for (i = 0; i < 5; i++) { 116b97d2791SOlof Johansson clock_out(bus, (addr & 0x10) != 0); 117b97d2791SOlof Johansson addr <<= 1; 118b97d2791SOlof Johansson } 119b97d2791SOlof Johansson 120b97d2791SOlof Johansson /* send the register address */ 121b97d2791SOlof Johansson for (i = 0; i < 5; i++) { 122b97d2791SOlof Johansson clock_out(bus, (reg & 0x10) != 0); 123b97d2791SOlof Johansson reg <<= 1; 124b97d2791SOlof Johansson } 125b97d2791SOlof Johansson } 126b97d2791SOlof Johansson 127b97d2791SOlof Johansson static int gpio_mdio_read(struct mii_bus *bus, int phy_id, int location) 128b97d2791SOlof Johansson { 129b97d2791SOlof Johansson u16 rdreg; 130b97d2791SOlof Johansson int ret, i; 131b97d2791SOlof Johansson u8 addr = phy_id & 0xff; 132b97d2791SOlof Johansson u8 reg = location & 0xff; 133b97d2791SOlof Johansson 134b97d2791SOlof Johansson bitbang_pre(bus, 1, addr, reg); 135b97d2791SOlof Johansson 136b97d2791SOlof Johansson /* tri-state our MDIO I/O pin so we can read */ 137b97d2791SOlof Johansson mdio_tristate(bus); 138b97d2791SOlof Johansson udelay(DELAY); 139b97d2791SOlof Johansson mdc_hi(bus); 140b97d2791SOlof Johansson udelay(DELAY); 141b97d2791SOlof Johansson mdc_lo(bus); 142b97d2791SOlof Johansson 143b97d2791SOlof Johansson /* read 16 bits of register data, MSB first */ 144b97d2791SOlof Johansson rdreg = 0; 145b97d2791SOlof Johansson for (i = 0; i < 16; i++) { 146b97d2791SOlof Johansson mdc_lo(bus); 147b97d2791SOlof Johansson udelay(DELAY); 148b97d2791SOlof Johansson mdc_hi(bus); 149b97d2791SOlof Johansson udelay(DELAY); 150b97d2791SOlof Johansson mdc_lo(bus); 151b97d2791SOlof Johansson udelay(DELAY); 152b97d2791SOlof Johansson rdreg <<= 1; 153b97d2791SOlof Johansson rdreg |= mdio_read(bus); 154b97d2791SOlof Johansson } 155b97d2791SOlof Johansson 156b97d2791SOlof Johansson mdc_hi(bus); 157b97d2791SOlof Johansson udelay(DELAY); 158b97d2791SOlof Johansson mdc_lo(bus); 159b97d2791SOlof Johansson udelay(DELAY); 160b97d2791SOlof Johansson 161b97d2791SOlof Johansson ret = rdreg; 162b97d2791SOlof Johansson 163b97d2791SOlof Johansson return ret; 164b97d2791SOlof Johansson } 165b97d2791SOlof Johansson 166b97d2791SOlof Johansson static int gpio_mdio_write(struct mii_bus *bus, int phy_id, int location, u16 val) 167b97d2791SOlof Johansson { 168b97d2791SOlof Johansson int i; 169b97d2791SOlof Johansson 170b97d2791SOlof Johansson u8 addr = phy_id & 0xff; 171b97d2791SOlof Johansson u8 reg = location & 0xff; 172b97d2791SOlof Johansson u16 value = val & 0xffff; 173b97d2791SOlof Johansson 174b97d2791SOlof Johansson bitbang_pre(bus, 0, addr, reg); 175b97d2791SOlof Johansson 176b97d2791SOlof Johansson /* send the turnaround (10) */ 177b97d2791SOlof Johansson mdc_lo(bus); 178b97d2791SOlof Johansson mdio_hi(bus); 179b97d2791SOlof Johansson udelay(DELAY); 180b97d2791SOlof Johansson mdc_hi(bus); 181b97d2791SOlof Johansson udelay(DELAY); 182b97d2791SOlof Johansson mdc_lo(bus); 183b97d2791SOlof Johansson mdio_lo(bus); 184b97d2791SOlof Johansson udelay(DELAY); 185b97d2791SOlof Johansson mdc_hi(bus); 186b97d2791SOlof Johansson udelay(DELAY); 187b97d2791SOlof Johansson 188b97d2791SOlof Johansson /* write 16 bits of register data, MSB first */ 189b97d2791SOlof Johansson for (i = 0; i < 16; i++) { 190b97d2791SOlof Johansson mdc_lo(bus); 191b97d2791SOlof Johansson if (value & 0x8000) 192b97d2791SOlof Johansson mdio_hi(bus); 193b97d2791SOlof Johansson else 194b97d2791SOlof Johansson mdio_lo(bus); 195b97d2791SOlof Johansson udelay(DELAY); 196b97d2791SOlof Johansson mdc_hi(bus); 197b97d2791SOlof Johansson udelay(DELAY); 198b97d2791SOlof Johansson value <<= 1; 199b97d2791SOlof Johansson } 200b97d2791SOlof Johansson 201b97d2791SOlof Johansson /* 202b97d2791SOlof Johansson * Tri-state the MDIO line. 203b97d2791SOlof Johansson */ 204b97d2791SOlof Johansson mdio_tristate(bus); 205b97d2791SOlof Johansson mdc_lo(bus); 206b97d2791SOlof Johansson udelay(DELAY); 207b97d2791SOlof Johansson mdc_hi(bus); 208b97d2791SOlof Johansson udelay(DELAY); 209b97d2791SOlof Johansson return 0; 210b97d2791SOlof Johansson } 211b97d2791SOlof Johansson 212b97d2791SOlof Johansson static int gpio_mdio_reset(struct mii_bus *bus) 213b97d2791SOlof Johansson { 214b97d2791SOlof Johansson /*nothing here - dunno how to reset it*/ 215b97d2791SOlof Johansson return 0; 216b97d2791SOlof Johansson } 217b97d2791SOlof Johansson 218b97d2791SOlof Johansson 219*00006124SGrant Likely static int __devinit gpio_mdio_probe(struct platform_device *ofdev) 220b97d2791SOlof Johansson { 221b97d2791SOlof Johansson struct device *dev = &ofdev->dev; 22261c7a080SGrant Likely struct device_node *np = ofdev->dev.of_node; 223b97d2791SOlof Johansson struct mii_bus *new_bus; 224b97d2791SOlof Johansson struct gpio_priv *priv; 225b97d2791SOlof Johansson const unsigned int *prop; 2262dd3c001SOlof Johansson int err; 227b97d2791SOlof Johansson 2282dd3c001SOlof Johansson err = -ENOMEM; 229b97d2791SOlof Johansson priv = kzalloc(sizeof(struct gpio_priv), GFP_KERNEL); 2302dd3c001SOlof Johansson if (!priv) 2312dd3c001SOlof Johansson goto out; 232b97d2791SOlof Johansson 233ec2a5652SLennert Buytenhek new_bus = mdiobus_alloc(); 234b97d2791SOlof Johansson 2352dd3c001SOlof Johansson if (!new_bus) 2362dd3c001SOlof Johansson goto out_free_priv; 237b97d2791SOlof Johansson 2382dd3c001SOlof Johansson new_bus->name = "pasemi gpio mdio bus"; 2392dd3c001SOlof Johansson new_bus->read = &gpio_mdio_read; 2402dd3c001SOlof Johansson new_bus->write = &gpio_mdio_write; 2412dd3c001SOlof Johansson new_bus->reset = &gpio_mdio_reset; 242b97d2791SOlof Johansson 24312d371a6SStephen Rothwell prop = of_get_property(np, "reg", NULL); 2449d9326d3SAndy Fleming snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", *prop); 245b97d2791SOlof Johansson new_bus->priv = priv; 246b97d2791SOlof Johansson 2471dd2d06cSGrant Likely new_bus->irq = priv->mdio_irqs; 248b97d2791SOlof Johansson 24912d371a6SStephen Rothwell prop = of_get_property(np, "mdc-pin", NULL); 250b97d2791SOlof Johansson priv->mdc_pin = *prop; 251b97d2791SOlof Johansson 25212d371a6SStephen Rothwell prop = of_get_property(np, "mdio-pin", NULL); 253b97d2791SOlof Johansson priv->mdio_pin = *prop; 254b97d2791SOlof Johansson 25518ee49ddSLennert Buytenhek new_bus->parent = dev; 256b97d2791SOlof Johansson dev_set_drvdata(dev, new_bus); 257b97d2791SOlof Johansson 2581dd2d06cSGrant Likely err = of_mdiobus_register(new_bus, np); 259b97d2791SOlof Johansson 2602dd3c001SOlof Johansson if (err != 0) { 261b97d2791SOlof Johansson printk(KERN_ERR "%s: Cannot register as MDIO bus, err %d\n", 262b97d2791SOlof Johansson new_bus->name, err); 2632dd3c001SOlof Johansson goto out_free_irq; 264b97d2791SOlof Johansson } 265b97d2791SOlof Johansson 266b97d2791SOlof Johansson return 0; 267b97d2791SOlof Johansson 2682dd3c001SOlof Johansson out_free_irq: 269b97d2791SOlof Johansson kfree(new_bus); 2702dd3c001SOlof Johansson out_free_priv: 2712dd3c001SOlof Johansson kfree(priv); 2722dd3c001SOlof Johansson out: 273b97d2791SOlof Johansson return err; 274b97d2791SOlof Johansson } 275b97d2791SOlof Johansson 276b97d2791SOlof Johansson 277a454dc50SGrant Likely static int gpio_mdio_remove(struct platform_device *dev) 278b97d2791SOlof Johansson { 279b97d2791SOlof Johansson struct mii_bus *bus = dev_get_drvdata(&dev->dev); 280b97d2791SOlof Johansson 281b97d2791SOlof Johansson mdiobus_unregister(bus); 282b97d2791SOlof Johansson 283b97d2791SOlof Johansson dev_set_drvdata(&dev->dev, NULL); 284b97d2791SOlof Johansson 285b97d2791SOlof Johansson kfree(bus->priv); 286b97d2791SOlof Johansson bus->priv = NULL; 287ec2a5652SLennert Buytenhek mdiobus_free(bus); 288b97d2791SOlof Johansson 289b97d2791SOlof Johansson return 0; 290b97d2791SOlof Johansson } 291b97d2791SOlof Johansson 292b97d2791SOlof Johansson static struct of_device_id gpio_mdio_match[] = 293b97d2791SOlof Johansson { 294b97d2791SOlof Johansson { 295b97d2791SOlof Johansson .compatible = "gpio-mdio", 296b97d2791SOlof Johansson }, 297b97d2791SOlof Johansson {}, 298b97d2791SOlof Johansson }; 2995619965fSOlof Johansson MODULE_DEVICE_TABLE(of, gpio_mdio_match); 300b97d2791SOlof Johansson 301*00006124SGrant Likely static struct platform_driver gpio_mdio_driver = 302b97d2791SOlof Johansson { 303b97d2791SOlof Johansson .probe = gpio_mdio_probe, 304b97d2791SOlof Johansson .remove = gpio_mdio_remove, 30584dd4676SStephen Rothwell .driver = { 30684dd4676SStephen Rothwell .name = "gpio-mdio-bitbang", 3074018294bSGrant Likely .owner = THIS_MODULE, 3084018294bSGrant Likely .of_match_table = gpio_mdio_match, 30984dd4676SStephen Rothwell }, 310b97d2791SOlof Johansson }; 311b97d2791SOlof Johansson 312b97d2791SOlof Johansson int gpio_mdio_init(void) 313b97d2791SOlof Johansson { 3142dd3c001SOlof Johansson struct device_node *np; 3152dd3c001SOlof Johansson 3160d08a847SOlof Johansson np = of_find_compatible_node(NULL, NULL, "1682m-gpio"); 3170d08a847SOlof Johansson if (!np) 3180d08a847SOlof Johansson np = of_find_compatible_node(NULL, NULL, 3190d08a847SOlof Johansson "pasemi,pwrficient-gpio"); 3202dd3c001SOlof Johansson if (!np) 3212dd3c001SOlof Johansson return -ENODEV; 3222dd3c001SOlof Johansson gpio_regs = of_iomap(np, 0); 3232dd3c001SOlof Johansson of_node_put(np); 3242dd3c001SOlof Johansson 3252dd3c001SOlof Johansson if (!gpio_regs) 3262dd3c001SOlof Johansson return -ENODEV; 3272dd3c001SOlof Johansson 328*00006124SGrant Likely return platform_driver_register(&gpio_mdio_driver); 329b97d2791SOlof Johansson } 3302dd3c001SOlof Johansson module_init(gpio_mdio_init); 331b97d2791SOlof Johansson 332b97d2791SOlof Johansson void gpio_mdio_exit(void) 333b97d2791SOlof Johansson { 334*00006124SGrant Likely platform_driver_unregister(&gpio_mdio_driver); 3352dd3c001SOlof Johansson if (gpio_regs) 3362dd3c001SOlof Johansson iounmap(gpio_regs); 337b97d2791SOlof Johansson } 3382dd3c001SOlof Johansson module_exit(gpio_mdio_exit); 3395619965fSOlof Johansson 3405619965fSOlof Johansson MODULE_LICENSE("GPL"); 3415619965fSOlof Johansson MODULE_AUTHOR("Olof Johansson <olof@lixom.net>"); 3425619965fSOlof Johansson MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards"); 343