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> 33*26a2056eSRob Herring #include <linux/of_address.h> 341dd2d06cSGrant Likely #include <linux/of_mdio.h> 355f867dc7SJon Loeliger #include <linux/of_platform.h> 36b97d2791SOlof Johansson 37b97d2791SOlof Johansson #define DELAY 1 38b97d2791SOlof Johansson 39b97d2791SOlof Johansson static void __iomem *gpio_regs; 40b97d2791SOlof Johansson 41b97d2791SOlof Johansson struct gpio_priv { 42b97d2791SOlof Johansson int mdc_pin; 43b97d2791SOlof Johansson int mdio_pin; 441dd2d06cSGrant Likely int mdio_irqs[PHY_MAX_ADDR]; 45b97d2791SOlof Johansson }; 46b97d2791SOlof Johansson 47b97d2791SOlof Johansson #define MDC_PIN(bus) (((struct gpio_priv *)bus->priv)->mdc_pin) 48b97d2791SOlof Johansson #define MDIO_PIN(bus) (((struct gpio_priv *)bus->priv)->mdio_pin) 49b97d2791SOlof Johansson 50b97d2791SOlof Johansson static inline void mdio_lo(struct mii_bus *bus) 51b97d2791SOlof Johansson { 52b97d2791SOlof Johansson out_le32(gpio_regs+0x10, 1 << MDIO_PIN(bus)); 53b97d2791SOlof Johansson } 54b97d2791SOlof Johansson 55b97d2791SOlof Johansson static inline void mdio_hi(struct mii_bus *bus) 56b97d2791SOlof Johansson { 57b97d2791SOlof Johansson out_le32(gpio_regs, 1 << MDIO_PIN(bus)); 58b97d2791SOlof Johansson } 59b97d2791SOlof Johansson 60b97d2791SOlof Johansson static inline void mdc_lo(struct mii_bus *bus) 61b97d2791SOlof Johansson { 62b97d2791SOlof Johansson out_le32(gpio_regs+0x10, 1 << MDC_PIN(bus)); 63b97d2791SOlof Johansson } 64b97d2791SOlof Johansson 65b97d2791SOlof Johansson static inline void mdc_hi(struct mii_bus *bus) 66b97d2791SOlof Johansson { 67b97d2791SOlof Johansson out_le32(gpio_regs, 1 << MDC_PIN(bus)); 68b97d2791SOlof Johansson } 69b97d2791SOlof Johansson 70b97d2791SOlof Johansson static inline void mdio_active(struct mii_bus *bus) 71b97d2791SOlof Johansson { 72b97d2791SOlof Johansson out_le32(gpio_regs+0x20, (1 << MDC_PIN(bus)) | (1 << MDIO_PIN(bus))); 73b97d2791SOlof Johansson } 74b97d2791SOlof Johansson 75b97d2791SOlof Johansson static inline void mdio_tristate(struct mii_bus *bus) 76b97d2791SOlof Johansson { 77b97d2791SOlof Johansson out_le32(gpio_regs+0x30, (1 << MDIO_PIN(bus))); 78b97d2791SOlof Johansson } 79b97d2791SOlof Johansson 80b97d2791SOlof Johansson static inline int mdio_read(struct mii_bus *bus) 81b97d2791SOlof Johansson { 82b97d2791SOlof Johansson return !!(in_le32(gpio_regs+0x40) & (1 << MDIO_PIN(bus))); 83b97d2791SOlof Johansson } 84b97d2791SOlof Johansson 85b97d2791SOlof Johansson static void clock_out(struct mii_bus *bus, int bit) 86b97d2791SOlof Johansson { 87b97d2791SOlof Johansson if (bit) 88b97d2791SOlof Johansson mdio_hi(bus); 89b97d2791SOlof Johansson else 90b97d2791SOlof Johansson mdio_lo(bus); 91b97d2791SOlof Johansson udelay(DELAY); 92b97d2791SOlof Johansson mdc_hi(bus); 93b97d2791SOlof Johansson udelay(DELAY); 94b97d2791SOlof Johansson mdc_lo(bus); 95b97d2791SOlof Johansson } 96b97d2791SOlof Johansson 97b97d2791SOlof Johansson /* Utility to send the preamble, address, and register (common to read and write). */ 98b97d2791SOlof Johansson static void bitbang_pre(struct mii_bus *bus, int read, u8 addr, u8 reg) 99b97d2791SOlof Johansson { 100b97d2791SOlof Johansson int i; 101b97d2791SOlof Johansson 102b97d2791SOlof Johansson /* CFE uses a really long preamble (40 bits). We'll do the same. */ 103b97d2791SOlof Johansson mdio_active(bus); 104b97d2791SOlof Johansson for (i = 0; i < 40; i++) { 105b97d2791SOlof Johansson clock_out(bus, 1); 106b97d2791SOlof Johansson } 107b97d2791SOlof Johansson 108b97d2791SOlof Johansson /* send the start bit (01) and the read opcode (10) or write (10) */ 109b97d2791SOlof Johansson clock_out(bus, 0); 110b97d2791SOlof Johansson clock_out(bus, 1); 111b97d2791SOlof Johansson 112b97d2791SOlof Johansson clock_out(bus, read); 113b97d2791SOlof Johansson clock_out(bus, !read); 114b97d2791SOlof Johansson 115b97d2791SOlof Johansson /* send the PHY address */ 116b97d2791SOlof Johansson for (i = 0; i < 5; i++) { 117b97d2791SOlof Johansson clock_out(bus, (addr & 0x10) != 0); 118b97d2791SOlof Johansson addr <<= 1; 119b97d2791SOlof Johansson } 120b97d2791SOlof Johansson 121b97d2791SOlof Johansson /* send the register address */ 122b97d2791SOlof Johansson for (i = 0; i < 5; i++) { 123b97d2791SOlof Johansson clock_out(bus, (reg & 0x10) != 0); 124b97d2791SOlof Johansson reg <<= 1; 125b97d2791SOlof Johansson } 126b97d2791SOlof Johansson } 127b97d2791SOlof Johansson 128b97d2791SOlof Johansson static int gpio_mdio_read(struct mii_bus *bus, int phy_id, int location) 129b97d2791SOlof Johansson { 130b97d2791SOlof Johansson u16 rdreg; 131b97d2791SOlof Johansson int ret, i; 132b97d2791SOlof Johansson u8 addr = phy_id & 0xff; 133b97d2791SOlof Johansson u8 reg = location & 0xff; 134b97d2791SOlof Johansson 135b97d2791SOlof Johansson bitbang_pre(bus, 1, addr, reg); 136b97d2791SOlof Johansson 137b97d2791SOlof Johansson /* tri-state our MDIO I/O pin so we can read */ 138b97d2791SOlof Johansson mdio_tristate(bus); 139b97d2791SOlof Johansson udelay(DELAY); 140b97d2791SOlof Johansson mdc_hi(bus); 141b97d2791SOlof Johansson udelay(DELAY); 142b97d2791SOlof Johansson mdc_lo(bus); 143b97d2791SOlof Johansson 144b97d2791SOlof Johansson /* read 16 bits of register data, MSB first */ 145b97d2791SOlof Johansson rdreg = 0; 146b97d2791SOlof Johansson for (i = 0; i < 16; i++) { 147b97d2791SOlof Johansson mdc_lo(bus); 148b97d2791SOlof Johansson udelay(DELAY); 149b97d2791SOlof Johansson mdc_hi(bus); 150b97d2791SOlof Johansson udelay(DELAY); 151b97d2791SOlof Johansson mdc_lo(bus); 152b97d2791SOlof Johansson udelay(DELAY); 153b97d2791SOlof Johansson rdreg <<= 1; 154b97d2791SOlof Johansson rdreg |= mdio_read(bus); 155b97d2791SOlof Johansson } 156b97d2791SOlof Johansson 157b97d2791SOlof Johansson mdc_hi(bus); 158b97d2791SOlof Johansson udelay(DELAY); 159b97d2791SOlof Johansson mdc_lo(bus); 160b97d2791SOlof Johansson udelay(DELAY); 161b97d2791SOlof Johansson 162b97d2791SOlof Johansson ret = rdreg; 163b97d2791SOlof Johansson 164b97d2791SOlof Johansson return ret; 165b97d2791SOlof Johansson } 166b97d2791SOlof Johansson 167b97d2791SOlof Johansson static int gpio_mdio_write(struct mii_bus *bus, int phy_id, int location, u16 val) 168b97d2791SOlof Johansson { 169b97d2791SOlof Johansson int i; 170b97d2791SOlof Johansson 171b97d2791SOlof Johansson u8 addr = phy_id & 0xff; 172b97d2791SOlof Johansson u8 reg = location & 0xff; 173b97d2791SOlof Johansson u16 value = val & 0xffff; 174b97d2791SOlof Johansson 175b97d2791SOlof Johansson bitbang_pre(bus, 0, addr, reg); 176b97d2791SOlof Johansson 177b97d2791SOlof Johansson /* send the turnaround (10) */ 178b97d2791SOlof Johansson mdc_lo(bus); 179b97d2791SOlof Johansson mdio_hi(bus); 180b97d2791SOlof Johansson udelay(DELAY); 181b97d2791SOlof Johansson mdc_hi(bus); 182b97d2791SOlof Johansson udelay(DELAY); 183b97d2791SOlof Johansson mdc_lo(bus); 184b97d2791SOlof Johansson mdio_lo(bus); 185b97d2791SOlof Johansson udelay(DELAY); 186b97d2791SOlof Johansson mdc_hi(bus); 187b97d2791SOlof Johansson udelay(DELAY); 188b97d2791SOlof Johansson 189b97d2791SOlof Johansson /* write 16 bits of register data, MSB first */ 190b97d2791SOlof Johansson for (i = 0; i < 16; i++) { 191b97d2791SOlof Johansson mdc_lo(bus); 192b97d2791SOlof Johansson if (value & 0x8000) 193b97d2791SOlof Johansson mdio_hi(bus); 194b97d2791SOlof Johansson else 195b97d2791SOlof Johansson mdio_lo(bus); 196b97d2791SOlof Johansson udelay(DELAY); 197b97d2791SOlof Johansson mdc_hi(bus); 198b97d2791SOlof Johansson udelay(DELAY); 199b97d2791SOlof Johansson value <<= 1; 200b97d2791SOlof Johansson } 201b97d2791SOlof Johansson 202b97d2791SOlof Johansson /* 203b97d2791SOlof Johansson * Tri-state the MDIO line. 204b97d2791SOlof Johansson */ 205b97d2791SOlof Johansson mdio_tristate(bus); 206b97d2791SOlof Johansson mdc_lo(bus); 207b97d2791SOlof Johansson udelay(DELAY); 208b97d2791SOlof Johansson mdc_hi(bus); 209b97d2791SOlof Johansson udelay(DELAY); 210b97d2791SOlof Johansson return 0; 211b97d2791SOlof Johansson } 212b97d2791SOlof Johansson 213b97d2791SOlof Johansson static int gpio_mdio_reset(struct mii_bus *bus) 214b97d2791SOlof Johansson { 215b97d2791SOlof Johansson /*nothing here - dunno how to reset it*/ 216b97d2791SOlof Johansson return 0; 217b97d2791SOlof Johansson } 218b97d2791SOlof Johansson 219b97d2791SOlof Johansson 220cad5cef6SGreg Kroah-Hartman static int gpio_mdio_probe(struct platform_device *ofdev) 221b97d2791SOlof Johansson { 222b97d2791SOlof Johansson struct device *dev = &ofdev->dev; 22361c7a080SGrant Likely struct device_node *np = ofdev->dev.of_node; 224b97d2791SOlof Johansson struct mii_bus *new_bus; 225b97d2791SOlof Johansson struct gpio_priv *priv; 226b97d2791SOlof Johansson const unsigned int *prop; 2272dd3c001SOlof Johansson int err; 228b97d2791SOlof Johansson 2292dd3c001SOlof Johansson err = -ENOMEM; 230b97d2791SOlof Johansson priv = kzalloc(sizeof(struct gpio_priv), GFP_KERNEL); 2312dd3c001SOlof Johansson if (!priv) 2322dd3c001SOlof Johansson goto out; 233b97d2791SOlof Johansson 234ec2a5652SLennert Buytenhek new_bus = mdiobus_alloc(); 235b97d2791SOlof Johansson 2362dd3c001SOlof Johansson if (!new_bus) 2372dd3c001SOlof Johansson goto out_free_priv; 238b97d2791SOlof Johansson 2392dd3c001SOlof Johansson new_bus->name = "pasemi gpio mdio bus"; 2402dd3c001SOlof Johansson new_bus->read = &gpio_mdio_read; 2412dd3c001SOlof Johansson new_bus->write = &gpio_mdio_write; 2422dd3c001SOlof Johansson new_bus->reset = &gpio_mdio_reset; 243b97d2791SOlof Johansson 24412d371a6SStephen Rothwell prop = of_get_property(np, "reg", NULL); 2459d9326d3SAndy Fleming snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", *prop); 246b97d2791SOlof Johansson new_bus->priv = priv; 247b97d2791SOlof Johansson 2481dd2d06cSGrant Likely new_bus->irq = priv->mdio_irqs; 249b97d2791SOlof Johansson 25012d371a6SStephen Rothwell prop = of_get_property(np, "mdc-pin", NULL); 251b97d2791SOlof Johansson priv->mdc_pin = *prop; 252b97d2791SOlof Johansson 25312d371a6SStephen Rothwell prop = of_get_property(np, "mdio-pin", NULL); 254b97d2791SOlof Johansson priv->mdio_pin = *prop; 255b97d2791SOlof Johansson 25618ee49ddSLennert Buytenhek new_bus->parent = dev; 257b97d2791SOlof Johansson dev_set_drvdata(dev, new_bus); 258b97d2791SOlof Johansson 2591dd2d06cSGrant Likely err = of_mdiobus_register(new_bus, np); 260b97d2791SOlof Johansson 2612dd3c001SOlof Johansson if (err != 0) { 262b97d2791SOlof Johansson printk(KERN_ERR "%s: Cannot register as MDIO bus, err %d\n", 263b97d2791SOlof Johansson new_bus->name, err); 2642dd3c001SOlof Johansson goto out_free_irq; 265b97d2791SOlof Johansson } 266b97d2791SOlof Johansson 267b97d2791SOlof Johansson return 0; 268b97d2791SOlof Johansson 2692dd3c001SOlof Johansson out_free_irq: 270b97d2791SOlof Johansson kfree(new_bus); 2712dd3c001SOlof Johansson out_free_priv: 2722dd3c001SOlof Johansson kfree(priv); 2732dd3c001SOlof Johansson out: 274b97d2791SOlof Johansson return err; 275b97d2791SOlof Johansson } 276b97d2791SOlof Johansson 277b97d2791SOlof Johansson 278a454dc50SGrant Likely static int gpio_mdio_remove(struct platform_device *dev) 279b97d2791SOlof Johansson { 280b97d2791SOlof Johansson struct mii_bus *bus = dev_get_drvdata(&dev->dev); 281b97d2791SOlof Johansson 282b97d2791SOlof Johansson mdiobus_unregister(bus); 283b97d2791SOlof Johansson 284b97d2791SOlof Johansson dev_set_drvdata(&dev->dev, NULL); 285b97d2791SOlof Johansson 286b97d2791SOlof Johansson kfree(bus->priv); 287b97d2791SOlof Johansson bus->priv = NULL; 288ec2a5652SLennert Buytenhek mdiobus_free(bus); 289b97d2791SOlof Johansson 290b97d2791SOlof Johansson return 0; 291b97d2791SOlof Johansson } 292b97d2791SOlof Johansson 293b97d2791SOlof Johansson static struct of_device_id gpio_mdio_match[] = 294b97d2791SOlof Johansson { 295b97d2791SOlof Johansson { 296b97d2791SOlof Johansson .compatible = "gpio-mdio", 297b97d2791SOlof Johansson }, 298b97d2791SOlof Johansson {}, 299b97d2791SOlof Johansson }; 3005619965fSOlof Johansson MODULE_DEVICE_TABLE(of, gpio_mdio_match); 301b97d2791SOlof Johansson 30200006124SGrant Likely static struct platform_driver gpio_mdio_driver = 303b97d2791SOlof Johansson { 304b97d2791SOlof Johansson .probe = gpio_mdio_probe, 305b97d2791SOlof Johansson .remove = gpio_mdio_remove, 30684dd4676SStephen Rothwell .driver = { 30784dd4676SStephen Rothwell .name = "gpio-mdio-bitbang", 3084018294bSGrant Likely .owner = THIS_MODULE, 3094018294bSGrant Likely .of_match_table = gpio_mdio_match, 31084dd4676SStephen Rothwell }, 311b97d2791SOlof Johansson }; 312b97d2791SOlof Johansson 313b97d2791SOlof Johansson int gpio_mdio_init(void) 314b97d2791SOlof Johansson { 3152dd3c001SOlof Johansson struct device_node *np; 3162dd3c001SOlof Johansson 3170d08a847SOlof Johansson np = of_find_compatible_node(NULL, NULL, "1682m-gpio"); 3180d08a847SOlof Johansson if (!np) 3190d08a847SOlof Johansson np = of_find_compatible_node(NULL, NULL, 3200d08a847SOlof Johansson "pasemi,pwrficient-gpio"); 3212dd3c001SOlof Johansson if (!np) 3222dd3c001SOlof Johansson return -ENODEV; 3232dd3c001SOlof Johansson gpio_regs = of_iomap(np, 0); 3242dd3c001SOlof Johansson of_node_put(np); 3252dd3c001SOlof Johansson 3262dd3c001SOlof Johansson if (!gpio_regs) 3272dd3c001SOlof Johansson return -ENODEV; 3282dd3c001SOlof Johansson 32900006124SGrant Likely return platform_driver_register(&gpio_mdio_driver); 330b97d2791SOlof Johansson } 3312dd3c001SOlof Johansson module_init(gpio_mdio_init); 332b97d2791SOlof Johansson 333b97d2791SOlof Johansson void gpio_mdio_exit(void) 334b97d2791SOlof Johansson { 33500006124SGrant Likely platform_driver_unregister(&gpio_mdio_driver); 3362dd3c001SOlof Johansson if (gpio_regs) 3372dd3c001SOlof Johansson iounmap(gpio_regs); 338b97d2791SOlof Johansson } 3392dd3c001SOlof Johansson module_exit(gpio_mdio_exit); 3405619965fSOlof Johansson 3415619965fSOlof Johansson MODULE_LICENSE("GPL"); 3425619965fSOlof Johansson MODULE_AUTHOR("Olof Johansson <olof@lixom.net>"); 3435619965fSOlof Johansson MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards"); 344