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> 27b97d2791SOlof Johansson #include <linux/sched.h> 28b97d2791SOlof Johansson #include <linux/errno.h> 29b97d2791SOlof Johansson #include <linux/ioport.h> 30b97d2791SOlof Johansson #include <linux/interrupt.h> 31b97d2791SOlof Johansson #include <linux/phy.h> 32b97d2791SOlof Johansson #include <linux/platform_device.h> 33b97d2791SOlof Johansson #include <asm/of_platform.h> 34b97d2791SOlof Johansson 35b97d2791SOlof Johansson #define DELAY 1 36b97d2791SOlof Johansson 37b97d2791SOlof Johansson static void __iomem *gpio_regs; 38b97d2791SOlof Johansson 39b97d2791SOlof Johansson struct gpio_priv { 40b97d2791SOlof Johansson int mdc_pin; 41b97d2791SOlof Johansson int mdio_pin; 42b97d2791SOlof Johansson }; 43b97d2791SOlof Johansson 44b97d2791SOlof Johansson #define MDC_PIN(bus) (((struct gpio_priv *)bus->priv)->mdc_pin) 45b97d2791SOlof Johansson #define MDIO_PIN(bus) (((struct gpio_priv *)bus->priv)->mdio_pin) 46b97d2791SOlof Johansson 47b97d2791SOlof Johansson static inline void mdio_lo(struct mii_bus *bus) 48b97d2791SOlof Johansson { 49b97d2791SOlof Johansson out_le32(gpio_regs+0x10, 1 << MDIO_PIN(bus)); 50b97d2791SOlof Johansson } 51b97d2791SOlof Johansson 52b97d2791SOlof Johansson static inline void mdio_hi(struct mii_bus *bus) 53b97d2791SOlof Johansson { 54b97d2791SOlof Johansson out_le32(gpio_regs, 1 << MDIO_PIN(bus)); 55b97d2791SOlof Johansson } 56b97d2791SOlof Johansson 57b97d2791SOlof Johansson static inline void mdc_lo(struct mii_bus *bus) 58b97d2791SOlof Johansson { 59b97d2791SOlof Johansson out_le32(gpio_regs+0x10, 1 << MDC_PIN(bus)); 60b97d2791SOlof Johansson } 61b97d2791SOlof Johansson 62b97d2791SOlof Johansson static inline void mdc_hi(struct mii_bus *bus) 63b97d2791SOlof Johansson { 64b97d2791SOlof Johansson out_le32(gpio_regs, 1 << MDC_PIN(bus)); 65b97d2791SOlof Johansson } 66b97d2791SOlof Johansson 67b97d2791SOlof Johansson static inline void mdio_active(struct mii_bus *bus) 68b97d2791SOlof Johansson { 69b97d2791SOlof Johansson out_le32(gpio_regs+0x20, (1 << MDC_PIN(bus)) | (1 << MDIO_PIN(bus))); 70b97d2791SOlof Johansson } 71b97d2791SOlof Johansson 72b97d2791SOlof Johansson static inline void mdio_tristate(struct mii_bus *bus) 73b97d2791SOlof Johansson { 74b97d2791SOlof Johansson out_le32(gpio_regs+0x30, (1 << MDIO_PIN(bus))); 75b97d2791SOlof Johansson } 76b97d2791SOlof Johansson 77b97d2791SOlof Johansson static inline int mdio_read(struct mii_bus *bus) 78b97d2791SOlof Johansson { 79b97d2791SOlof Johansson return !!(in_le32(gpio_regs+0x40) & (1 << MDIO_PIN(bus))); 80b97d2791SOlof Johansson } 81b97d2791SOlof Johansson 82b97d2791SOlof Johansson static void clock_out(struct mii_bus *bus, int bit) 83b97d2791SOlof Johansson { 84b97d2791SOlof Johansson if (bit) 85b97d2791SOlof Johansson mdio_hi(bus); 86b97d2791SOlof Johansson else 87b97d2791SOlof Johansson mdio_lo(bus); 88b97d2791SOlof Johansson udelay(DELAY); 89b97d2791SOlof Johansson mdc_hi(bus); 90b97d2791SOlof Johansson udelay(DELAY); 91b97d2791SOlof Johansson mdc_lo(bus); 92b97d2791SOlof Johansson } 93b97d2791SOlof Johansson 94b97d2791SOlof Johansson /* Utility to send the preamble, address, and register (common to read and write). */ 95b97d2791SOlof Johansson static void bitbang_pre(struct mii_bus *bus, int read, u8 addr, u8 reg) 96b97d2791SOlof Johansson { 97b97d2791SOlof Johansson int i; 98b97d2791SOlof Johansson 99b97d2791SOlof Johansson /* CFE uses a really long preamble (40 bits). We'll do the same. */ 100b97d2791SOlof Johansson mdio_active(bus); 101b97d2791SOlof Johansson for (i = 0; i < 40; i++) { 102b97d2791SOlof Johansson clock_out(bus, 1); 103b97d2791SOlof Johansson } 104b97d2791SOlof Johansson 105b97d2791SOlof Johansson /* send the start bit (01) and the read opcode (10) or write (10) */ 106b97d2791SOlof Johansson clock_out(bus, 0); 107b97d2791SOlof Johansson clock_out(bus, 1); 108b97d2791SOlof Johansson 109b97d2791SOlof Johansson clock_out(bus, read); 110b97d2791SOlof Johansson clock_out(bus, !read); 111b97d2791SOlof Johansson 112b97d2791SOlof Johansson /* send the PHY address */ 113b97d2791SOlof Johansson for (i = 0; i < 5; i++) { 114b97d2791SOlof Johansson clock_out(bus, (addr & 0x10) != 0); 115b97d2791SOlof Johansson addr <<= 1; 116b97d2791SOlof Johansson } 117b97d2791SOlof Johansson 118b97d2791SOlof Johansson /* send the register address */ 119b97d2791SOlof Johansson for (i = 0; i < 5; i++) { 120b97d2791SOlof Johansson clock_out(bus, (reg & 0x10) != 0); 121b97d2791SOlof Johansson reg <<= 1; 122b97d2791SOlof Johansson } 123b97d2791SOlof Johansson } 124b97d2791SOlof Johansson 125b97d2791SOlof Johansson static int gpio_mdio_read(struct mii_bus *bus, int phy_id, int location) 126b97d2791SOlof Johansson { 127b97d2791SOlof Johansson u16 rdreg; 128b97d2791SOlof Johansson int ret, i; 129b97d2791SOlof Johansson u8 addr = phy_id & 0xff; 130b97d2791SOlof Johansson u8 reg = location & 0xff; 131b97d2791SOlof Johansson 132b97d2791SOlof Johansson bitbang_pre(bus, 1, addr, reg); 133b97d2791SOlof Johansson 134b97d2791SOlof Johansson /* tri-state our MDIO I/O pin so we can read */ 135b97d2791SOlof Johansson mdio_tristate(bus); 136b97d2791SOlof Johansson udelay(DELAY); 137b97d2791SOlof Johansson mdc_hi(bus); 138b97d2791SOlof Johansson udelay(DELAY); 139b97d2791SOlof Johansson mdc_lo(bus); 140b97d2791SOlof Johansson 141b97d2791SOlof Johansson /* read 16 bits of register data, MSB first */ 142b97d2791SOlof Johansson rdreg = 0; 143b97d2791SOlof Johansson for (i = 0; i < 16; i++) { 144b97d2791SOlof Johansson mdc_lo(bus); 145b97d2791SOlof Johansson udelay(DELAY); 146b97d2791SOlof Johansson mdc_hi(bus); 147b97d2791SOlof Johansson udelay(DELAY); 148b97d2791SOlof Johansson mdc_lo(bus); 149b97d2791SOlof Johansson udelay(DELAY); 150b97d2791SOlof Johansson rdreg <<= 1; 151b97d2791SOlof Johansson rdreg |= mdio_read(bus); 152b97d2791SOlof Johansson } 153b97d2791SOlof Johansson 154b97d2791SOlof Johansson mdc_hi(bus); 155b97d2791SOlof Johansson udelay(DELAY); 156b97d2791SOlof Johansson mdc_lo(bus); 157b97d2791SOlof Johansson udelay(DELAY); 158b97d2791SOlof Johansson 159b97d2791SOlof Johansson ret = rdreg; 160b97d2791SOlof Johansson 161b97d2791SOlof Johansson return ret; 162b97d2791SOlof Johansson } 163b97d2791SOlof Johansson 164b97d2791SOlof Johansson static int gpio_mdio_write(struct mii_bus *bus, int phy_id, int location, u16 val) 165b97d2791SOlof Johansson { 166b97d2791SOlof Johansson int i; 167b97d2791SOlof Johansson 168b97d2791SOlof Johansson u8 addr = phy_id & 0xff; 169b97d2791SOlof Johansson u8 reg = location & 0xff; 170b97d2791SOlof Johansson u16 value = val & 0xffff; 171b97d2791SOlof Johansson 172b97d2791SOlof Johansson bitbang_pre(bus, 0, addr, reg); 173b97d2791SOlof Johansson 174b97d2791SOlof Johansson /* send the turnaround (10) */ 175b97d2791SOlof Johansson mdc_lo(bus); 176b97d2791SOlof Johansson mdio_hi(bus); 177b97d2791SOlof Johansson udelay(DELAY); 178b97d2791SOlof Johansson mdc_hi(bus); 179b97d2791SOlof Johansson udelay(DELAY); 180b97d2791SOlof Johansson mdc_lo(bus); 181b97d2791SOlof Johansson mdio_lo(bus); 182b97d2791SOlof Johansson udelay(DELAY); 183b97d2791SOlof Johansson mdc_hi(bus); 184b97d2791SOlof Johansson udelay(DELAY); 185b97d2791SOlof Johansson 186b97d2791SOlof Johansson /* write 16 bits of register data, MSB first */ 187b97d2791SOlof Johansson for (i = 0; i < 16; i++) { 188b97d2791SOlof Johansson mdc_lo(bus); 189b97d2791SOlof Johansson if (value & 0x8000) 190b97d2791SOlof Johansson mdio_hi(bus); 191b97d2791SOlof Johansson else 192b97d2791SOlof Johansson mdio_lo(bus); 193b97d2791SOlof Johansson udelay(DELAY); 194b97d2791SOlof Johansson mdc_hi(bus); 195b97d2791SOlof Johansson udelay(DELAY); 196b97d2791SOlof Johansson value <<= 1; 197b97d2791SOlof Johansson } 198b97d2791SOlof Johansson 199b97d2791SOlof Johansson /* 200b97d2791SOlof Johansson * Tri-state the MDIO line. 201b97d2791SOlof Johansson */ 202b97d2791SOlof Johansson mdio_tristate(bus); 203b97d2791SOlof Johansson mdc_lo(bus); 204b97d2791SOlof Johansson udelay(DELAY); 205b97d2791SOlof Johansson mdc_hi(bus); 206b97d2791SOlof Johansson udelay(DELAY); 207b97d2791SOlof Johansson return 0; 208b97d2791SOlof Johansson } 209b97d2791SOlof Johansson 210b97d2791SOlof Johansson static int gpio_mdio_reset(struct mii_bus *bus) 211b97d2791SOlof Johansson { 212b97d2791SOlof Johansson /*nothing here - dunno how to reset it*/ 213b97d2791SOlof Johansson return 0; 214b97d2791SOlof Johansson } 215b97d2791SOlof Johansson 216b97d2791SOlof Johansson 217b97d2791SOlof Johansson static int __devinit gpio_mdio_probe(struct of_device *ofdev, 218b97d2791SOlof Johansson const struct of_device_id *match) 219b97d2791SOlof Johansson { 220b97d2791SOlof Johansson struct device *dev = &ofdev->dev; 2212dd3c001SOlof Johansson struct device_node *phy_dn, *np = ofdev->node; 222b97d2791SOlof Johansson struct mii_bus *new_bus; 223b97d2791SOlof Johansson struct gpio_priv *priv; 224b97d2791SOlof Johansson const unsigned int *prop; 2252dd3c001SOlof Johansson int err; 226b97d2791SOlof Johansson int i; 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 233b97d2791SOlof Johansson new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL); 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); 244b97d2791SOlof Johansson new_bus->id = *prop; 245b97d2791SOlof Johansson new_bus->priv = priv; 246b97d2791SOlof Johansson 247b97d2791SOlof Johansson new_bus->phy_mask = 0; 248b97d2791SOlof Johansson 249b97d2791SOlof Johansson new_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); 250b97d2791SOlof Johansson 2512dd3c001SOlof Johansson if (!new_bus->irq) 2522dd3c001SOlof Johansson goto out_free_bus; 2532dd3c001SOlof Johansson 2542dd3c001SOlof Johansson for (i = 0; i < PHY_MAX_ADDR; i++) 2552dd3c001SOlof Johansson new_bus->irq[i] = NO_IRQ; 2562dd3c001SOlof Johansson 2572dd3c001SOlof Johansson for (phy_dn = of_get_next_child(np, NULL); 2582dd3c001SOlof Johansson phy_dn != NULL; 2592dd3c001SOlof Johansson phy_dn = of_get_next_child(np, phy_dn)) { 2602dd3c001SOlof Johansson const unsigned int *ip, *regp; 2612dd3c001SOlof Johansson 2622dd3c001SOlof Johansson ip = of_get_property(phy_dn, "interrupts", NULL); 2632dd3c001SOlof Johansson regp = of_get_property(phy_dn, "reg", NULL); 2642dd3c001SOlof Johansson if (!ip || !regp || *regp >= PHY_MAX_ADDR) 2652dd3c001SOlof Johansson continue; 2662dd3c001SOlof Johansson new_bus->irq[*regp] = irq_create_mapping(NULL, *ip); 2672dd3c001SOlof Johansson } 268b97d2791SOlof Johansson 26912d371a6SStephen Rothwell prop = of_get_property(np, "mdc-pin", NULL); 270b97d2791SOlof Johansson priv->mdc_pin = *prop; 271b97d2791SOlof Johansson 27212d371a6SStephen Rothwell prop = of_get_property(np, "mdio-pin", NULL); 273b97d2791SOlof Johansson priv->mdio_pin = *prop; 274b97d2791SOlof Johansson 275b97d2791SOlof Johansson new_bus->dev = dev; 276b97d2791SOlof Johansson dev_set_drvdata(dev, new_bus); 277b97d2791SOlof Johansson 278b97d2791SOlof Johansson err = mdiobus_register(new_bus); 279b97d2791SOlof Johansson 2802dd3c001SOlof Johansson if (err != 0) { 281b97d2791SOlof Johansson printk(KERN_ERR "%s: Cannot register as MDIO bus, err %d\n", 282b97d2791SOlof Johansson new_bus->name, err); 2832dd3c001SOlof Johansson goto out_free_irq; 284b97d2791SOlof Johansson } 285b97d2791SOlof Johansson 286b97d2791SOlof Johansson return 0; 287b97d2791SOlof Johansson 2882dd3c001SOlof Johansson out_free_irq: 2892dd3c001SOlof Johansson kfree(new_bus->irq); 2902dd3c001SOlof Johansson out_free_bus: 291b97d2791SOlof Johansson kfree(new_bus); 2922dd3c001SOlof Johansson out_free_priv: 2932dd3c001SOlof Johansson kfree(priv); 2942dd3c001SOlof Johansson out: 295b97d2791SOlof Johansson return err; 296b97d2791SOlof Johansson } 297b97d2791SOlof Johansson 298b97d2791SOlof Johansson 299b97d2791SOlof Johansson static int gpio_mdio_remove(struct of_device *dev) 300b97d2791SOlof Johansson { 301b97d2791SOlof Johansson struct mii_bus *bus = dev_get_drvdata(&dev->dev); 302b97d2791SOlof Johansson 303b97d2791SOlof Johansson mdiobus_unregister(bus); 304b97d2791SOlof Johansson 305b97d2791SOlof Johansson dev_set_drvdata(&dev->dev, NULL); 306b97d2791SOlof Johansson 307b97d2791SOlof Johansson kfree(bus->priv); 308b97d2791SOlof Johansson bus->priv = NULL; 309b97d2791SOlof Johansson kfree(bus); 310b97d2791SOlof Johansson 311b97d2791SOlof Johansson return 0; 312b97d2791SOlof Johansson } 313b97d2791SOlof Johansson 314b97d2791SOlof Johansson static struct of_device_id gpio_mdio_match[] = 315b97d2791SOlof Johansson { 316b97d2791SOlof Johansson { 317b97d2791SOlof Johansson .compatible = "gpio-mdio", 318b97d2791SOlof Johansson }, 319b97d2791SOlof Johansson {}, 320b97d2791SOlof Johansson }; 321*5619965fSOlof Johansson MODULE_DEVICE_TABLE(of, gpio_mdio_match); 322b97d2791SOlof Johansson 323b97d2791SOlof Johansson static struct of_platform_driver gpio_mdio_driver = 324b97d2791SOlof Johansson { 325b97d2791SOlof Johansson .match_table = gpio_mdio_match, 326b97d2791SOlof Johansson .probe = gpio_mdio_probe, 327b97d2791SOlof Johansson .remove = gpio_mdio_remove, 32884dd4676SStephen Rothwell .driver = { 32984dd4676SStephen Rothwell .name = "gpio-mdio-bitbang", 33084dd4676SStephen Rothwell }, 331b97d2791SOlof Johansson }; 332b97d2791SOlof Johansson 333b97d2791SOlof Johansson int gpio_mdio_init(void) 334b97d2791SOlof Johansson { 3352dd3c001SOlof Johansson struct device_node *np; 3362dd3c001SOlof Johansson 3370d08a847SOlof Johansson np = of_find_compatible_node(NULL, NULL, "1682m-gpio"); 3380d08a847SOlof Johansson if (!np) 3390d08a847SOlof Johansson np = of_find_compatible_node(NULL, NULL, 3400d08a847SOlof Johansson "pasemi,pwrficient-gpio"); 3412dd3c001SOlof Johansson if (!np) 3422dd3c001SOlof Johansson return -ENODEV; 3432dd3c001SOlof Johansson gpio_regs = of_iomap(np, 0); 3442dd3c001SOlof Johansson of_node_put(np); 3452dd3c001SOlof Johansson 3462dd3c001SOlof Johansson if (!gpio_regs) 3472dd3c001SOlof Johansson return -ENODEV; 3482dd3c001SOlof Johansson 349b97d2791SOlof Johansson return of_register_platform_driver(&gpio_mdio_driver); 350b97d2791SOlof Johansson } 3512dd3c001SOlof Johansson module_init(gpio_mdio_init); 352b97d2791SOlof Johansson 353b97d2791SOlof Johansson void gpio_mdio_exit(void) 354b97d2791SOlof Johansson { 355b97d2791SOlof Johansson of_unregister_platform_driver(&gpio_mdio_driver); 3562dd3c001SOlof Johansson if (gpio_regs) 3572dd3c001SOlof Johansson iounmap(gpio_regs); 358b97d2791SOlof Johansson } 3592dd3c001SOlof Johansson module_exit(gpio_mdio_exit); 360*5619965fSOlof Johansson 361*5619965fSOlof Johansson MODULE_LICENSE("GPL"); 362*5619965fSOlof Johansson MODULE_AUTHOR("Olof Johansson <olof@lixom.net>"); 363*5619965fSOlof Johansson MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards"); 364