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; 221b97d2791SOlof Johansson struct device_node *np = ofdev->node; 222b97d2791SOlof Johansson struct device_node *gpio_np; 223b97d2791SOlof Johansson struct mii_bus *new_bus; 224b97d2791SOlof Johansson struct resource res; 225b97d2791SOlof Johansson struct gpio_priv *priv; 226b97d2791SOlof Johansson const unsigned int *prop; 227b97d2791SOlof Johansson int err = 0; 228b97d2791SOlof Johansson int i; 229b97d2791SOlof Johansson 230b97d2791SOlof Johansson gpio_np = of_find_compatible_node(NULL, "gpio", "1682m-gpio"); 231b97d2791SOlof Johansson 232b97d2791SOlof Johansson if (!gpio_np) 233b97d2791SOlof Johansson return -ENODEV; 234b97d2791SOlof Johansson 235b97d2791SOlof Johansson err = of_address_to_resource(gpio_np, 0, &res); 236b97d2791SOlof Johansson of_node_put(gpio_np); 237b97d2791SOlof Johansson 238b97d2791SOlof Johansson if (err) 239b97d2791SOlof Johansson return -EINVAL; 240b97d2791SOlof Johansson 241b97d2791SOlof Johansson if (!gpio_regs) 242b97d2791SOlof Johansson gpio_regs = ioremap(res.start, 0x100); 243b97d2791SOlof Johansson 244b97d2791SOlof Johansson if (!gpio_regs) 245b97d2791SOlof Johansson return -EPERM; 246b97d2791SOlof Johansson 247b97d2791SOlof Johansson priv = kzalloc(sizeof(struct gpio_priv), GFP_KERNEL); 248b97d2791SOlof Johansson if (priv == NULL) 249b97d2791SOlof Johansson return -ENOMEM; 250b97d2791SOlof Johansson 251b97d2791SOlof Johansson new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL); 252b97d2791SOlof Johansson 253b97d2791SOlof Johansson if (new_bus == NULL) 254b97d2791SOlof Johansson return -ENOMEM; 255b97d2791SOlof Johansson 256b97d2791SOlof Johansson new_bus->name = "pasemi gpio mdio bus", 257b97d2791SOlof Johansson new_bus->read = &gpio_mdio_read, 258b97d2791SOlof Johansson new_bus->write = &gpio_mdio_write, 259b97d2791SOlof Johansson new_bus->reset = &gpio_mdio_reset, 260b97d2791SOlof Johansson 261*12d371a6SStephen Rothwell prop = of_get_property(np, "reg", NULL); 262b97d2791SOlof Johansson new_bus->id = *prop; 263b97d2791SOlof Johansson new_bus->priv = priv; 264b97d2791SOlof Johansson 265b97d2791SOlof Johansson new_bus->phy_mask = 0; 266b97d2791SOlof Johansson 267b97d2791SOlof Johansson new_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); 268b97d2791SOlof Johansson for(i = 0; i < PHY_MAX_ADDR; ++i) 269b97d2791SOlof Johansson new_bus->irq[i] = irq_create_mapping(NULL, 10); 270b97d2791SOlof Johansson 271b97d2791SOlof Johansson 272*12d371a6SStephen Rothwell prop = of_get_property(np, "mdc-pin", NULL); 273b97d2791SOlof Johansson priv->mdc_pin = *prop; 274b97d2791SOlof Johansson 275*12d371a6SStephen Rothwell prop = of_get_property(np, "mdio-pin", NULL); 276b97d2791SOlof Johansson priv->mdio_pin = *prop; 277b97d2791SOlof Johansson 278b97d2791SOlof Johansson new_bus->dev = dev; 279b97d2791SOlof Johansson dev_set_drvdata(dev, new_bus); 280b97d2791SOlof Johansson 281b97d2791SOlof Johansson err = mdiobus_register(new_bus); 282b97d2791SOlof Johansson 283b97d2791SOlof Johansson if (0 != err) { 284b97d2791SOlof Johansson printk(KERN_ERR "%s: Cannot register as MDIO bus, err %d\n", 285b97d2791SOlof Johansson new_bus->name, err); 286b97d2791SOlof Johansson goto bus_register_fail; 287b97d2791SOlof Johansson } 288b97d2791SOlof Johansson 289b97d2791SOlof Johansson return 0; 290b97d2791SOlof Johansson 291b97d2791SOlof Johansson bus_register_fail: 292b97d2791SOlof Johansson kfree(new_bus); 293b97d2791SOlof Johansson 294b97d2791SOlof Johansson return err; 295b97d2791SOlof Johansson } 296b97d2791SOlof Johansson 297b97d2791SOlof Johansson 298b97d2791SOlof Johansson static int gpio_mdio_remove(struct of_device *dev) 299b97d2791SOlof Johansson { 300b97d2791SOlof Johansson struct mii_bus *bus = dev_get_drvdata(&dev->dev); 301b97d2791SOlof Johansson 302b97d2791SOlof Johansson mdiobus_unregister(bus); 303b97d2791SOlof Johansson 304b97d2791SOlof Johansson dev_set_drvdata(&dev->dev, NULL); 305b97d2791SOlof Johansson 306b97d2791SOlof Johansson kfree(bus->priv); 307b97d2791SOlof Johansson bus->priv = NULL; 308b97d2791SOlof Johansson kfree(bus); 309b97d2791SOlof Johansson 310b97d2791SOlof Johansson return 0; 311b97d2791SOlof Johansson } 312b97d2791SOlof Johansson 313b97d2791SOlof Johansson static struct of_device_id gpio_mdio_match[] = 314b97d2791SOlof Johansson { 315b97d2791SOlof Johansson { 316b97d2791SOlof Johansson .compatible = "gpio-mdio", 317b97d2791SOlof Johansson }, 318b97d2791SOlof Johansson {}, 319b97d2791SOlof Johansson }; 320b97d2791SOlof Johansson 321b97d2791SOlof Johansson static struct of_platform_driver gpio_mdio_driver = 322b97d2791SOlof Johansson { 323b97d2791SOlof Johansson .name = "gpio-mdio-bitbang", 324b97d2791SOlof Johansson .match_table = gpio_mdio_match, 325b97d2791SOlof Johansson .probe = gpio_mdio_probe, 326b97d2791SOlof Johansson .remove = gpio_mdio_remove, 327b97d2791SOlof Johansson }; 328b97d2791SOlof Johansson 329b97d2791SOlof Johansson int gpio_mdio_init(void) 330b97d2791SOlof Johansson { 331b97d2791SOlof Johansson return of_register_platform_driver(&gpio_mdio_driver); 332b97d2791SOlof Johansson } 333b97d2791SOlof Johansson 334b97d2791SOlof Johansson void gpio_mdio_exit(void) 335b97d2791SOlof Johansson { 336b97d2791SOlof Johansson of_unregister_platform_driver(&gpio_mdio_driver); 337b97d2791SOlof Johansson } 338b97d2791SOlof Johansson device_initcall(gpio_mdio_init); 339b97d2791SOlof Johansson 340