gpio_mdio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2006-2007 PA Semi, Inc
  3. *
  4. * Author: Olof Johansson, PA Semi
  5. *
  6. * Maintained by: Olof Johansson <olof@lixom.net>
  7. *
  8. * Based on drivers/net/fs_enet/mii-bitbang.c.
  9. *
  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.
  13. *
  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.
  18. *
  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
  22. */
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/slab.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/of_mdio.h>
  33. #include <linux/of_platform.h>
  34. #define DELAY 1
  35. static void __iomem *gpio_regs;
  36. struct gpio_priv {
  37. int mdc_pin;
  38. int mdio_pin;
  39. int mdio_irqs[PHY_MAX_ADDR];
  40. };
  41. #define MDC_PIN(bus) (((struct gpio_priv *)bus->priv)->mdc_pin)
  42. #define MDIO_PIN(bus) (((struct gpio_priv *)bus->priv)->mdio_pin)
  43. static inline void mdio_lo(struct mii_bus *bus)
  44. {
  45. out_le32(gpio_regs+0x10, 1 << MDIO_PIN(bus));
  46. }
  47. static inline void mdio_hi(struct mii_bus *bus)
  48. {
  49. out_le32(gpio_regs, 1 << MDIO_PIN(bus));
  50. }
  51. static inline void mdc_lo(struct mii_bus *bus)
  52. {
  53. out_le32(gpio_regs+0x10, 1 << MDC_PIN(bus));
  54. }
  55. static inline void mdc_hi(struct mii_bus *bus)
  56. {
  57. out_le32(gpio_regs, 1 << MDC_PIN(bus));
  58. }
  59. static inline void mdio_active(struct mii_bus *bus)
  60. {
  61. out_le32(gpio_regs+0x20, (1 << MDC_PIN(bus)) | (1 << MDIO_PIN(bus)));
  62. }
  63. static inline void mdio_tristate(struct mii_bus *bus)
  64. {
  65. out_le32(gpio_regs+0x30, (1 << MDIO_PIN(bus)));
  66. }
  67. static inline int mdio_read(struct mii_bus *bus)
  68. {
  69. return !!(in_le32(gpio_regs+0x40) & (1 << MDIO_PIN(bus)));
  70. }
  71. static void clock_out(struct mii_bus *bus, int bit)
  72. {
  73. if (bit)
  74. mdio_hi(bus);
  75. else
  76. mdio_lo(bus);
  77. udelay(DELAY);
  78. mdc_hi(bus);
  79. udelay(DELAY);
  80. mdc_lo(bus);
  81. }
  82. /* Utility to send the preamble, address, and register (common to read and write). */
  83. static void bitbang_pre(struct mii_bus *bus, int read, u8 addr, u8 reg)
  84. {
  85. int i;
  86. /* CFE uses a really long preamble (40 bits). We'll do the same. */
  87. mdio_active(bus);
  88. for (i = 0; i < 40; i++) {
  89. clock_out(bus, 1);
  90. }
  91. /* send the start bit (01) and the read opcode (10) or write (10) */
  92. clock_out(bus, 0);
  93. clock_out(bus, 1);
  94. clock_out(bus, read);
  95. clock_out(bus, !read);
  96. /* send the PHY address */
  97. for (i = 0; i < 5; i++) {
  98. clock_out(bus, (addr & 0x10) != 0);
  99. addr <<= 1;
  100. }
  101. /* send the register address */
  102. for (i = 0; i < 5; i++) {
  103. clock_out(bus, (reg & 0x10) != 0);
  104. reg <<= 1;
  105. }
  106. }
  107. static int gpio_mdio_read(struct mii_bus *bus, int phy_id, int location)
  108. {
  109. u16 rdreg;
  110. int ret, i;
  111. u8 addr = phy_id & 0xff;
  112. u8 reg = location & 0xff;
  113. bitbang_pre(bus, 1, addr, reg);
  114. /* tri-state our MDIO I/O pin so we can read */
  115. mdio_tristate(bus);
  116. udelay(DELAY);
  117. mdc_hi(bus);
  118. udelay(DELAY);
  119. mdc_lo(bus);
  120. /* read 16 bits of register data, MSB first */
  121. rdreg = 0;
  122. for (i = 0; i < 16; i++) {
  123. mdc_lo(bus);
  124. udelay(DELAY);
  125. mdc_hi(bus);
  126. udelay(DELAY);
  127. mdc_lo(bus);
  128. udelay(DELAY);
  129. rdreg <<= 1;
  130. rdreg |= mdio_read(bus);
  131. }
  132. mdc_hi(bus);
  133. udelay(DELAY);
  134. mdc_lo(bus);
  135. udelay(DELAY);
  136. ret = rdreg;
  137. return ret;
  138. }
  139. static int gpio_mdio_write(struct mii_bus *bus, int phy_id, int location, u16 val)
  140. {
  141. int i;
  142. u8 addr = phy_id & 0xff;
  143. u8 reg = location & 0xff;
  144. u16 value = val & 0xffff;
  145. bitbang_pre(bus, 0, addr, reg);
  146. /* send the turnaround (10) */
  147. mdc_lo(bus);
  148. mdio_hi(bus);
  149. udelay(DELAY);
  150. mdc_hi(bus);
  151. udelay(DELAY);
  152. mdc_lo(bus);
  153. mdio_lo(bus);
  154. udelay(DELAY);
  155. mdc_hi(bus);
  156. udelay(DELAY);
  157. /* write 16 bits of register data, MSB first */
  158. for (i = 0; i < 16; i++) {
  159. mdc_lo(bus);
  160. if (value & 0x8000)
  161. mdio_hi(bus);
  162. else
  163. mdio_lo(bus);
  164. udelay(DELAY);
  165. mdc_hi(bus);
  166. udelay(DELAY);
  167. value <<= 1;
  168. }
  169. /*
  170. * Tri-state the MDIO line.
  171. */
  172. mdio_tristate(bus);
  173. mdc_lo(bus);
  174. udelay(DELAY);
  175. mdc_hi(bus);
  176. udelay(DELAY);
  177. return 0;
  178. }
  179. static int gpio_mdio_reset(struct mii_bus *bus)
  180. {
  181. /*nothing here - dunno how to reset it*/
  182. return 0;
  183. }
  184. static int __devinit gpio_mdio_probe(struct platform_device *ofdev)
  185. {
  186. struct device *dev = &ofdev->dev;
  187. struct device_node *np = ofdev->dev.of_node;
  188. struct mii_bus *new_bus;
  189. struct gpio_priv *priv;
  190. const unsigned int *prop;
  191. int err;
  192. err = -ENOMEM;
  193. priv = kzalloc(sizeof(struct gpio_priv), GFP_KERNEL);
  194. if (!priv)
  195. goto out;
  196. new_bus = mdiobus_alloc();
  197. if (!new_bus)
  198. goto out_free_priv;
  199. new_bus->name = "pasemi gpio mdio bus";
  200. new_bus->read = &gpio_mdio_read;
  201. new_bus->write = &gpio_mdio_write;
  202. new_bus->reset = &gpio_mdio_reset;
  203. prop = of_get_property(np, "reg", NULL);
  204. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", *prop);
  205. new_bus->priv = priv;
  206. new_bus->irq = priv->mdio_irqs;
  207. prop = of_get_property(np, "mdc-pin", NULL);
  208. priv->mdc_pin = *prop;
  209. prop = of_get_property(np, "mdio-pin", NULL);
  210. priv->mdio_pin = *prop;
  211. new_bus->parent = dev;
  212. dev_set_drvdata(dev, new_bus);
  213. err = of_mdiobus_register(new_bus, np);
  214. if (err != 0) {
  215. printk(KERN_ERR "%s: Cannot register as MDIO bus, err %d\n",
  216. new_bus->name, err);
  217. goto out_free_irq;
  218. }
  219. return 0;
  220. out_free_irq:
  221. kfree(new_bus);
  222. out_free_priv:
  223. kfree(priv);
  224. out:
  225. return err;
  226. }
  227. static int gpio_mdio_remove(struct platform_device *dev)
  228. {
  229. struct mii_bus *bus = dev_get_drvdata(&dev->dev);
  230. mdiobus_unregister(bus);
  231. dev_set_drvdata(&dev->dev, NULL);
  232. kfree(bus->priv);
  233. bus->priv = NULL;
  234. mdiobus_free(bus);
  235. return 0;
  236. }
  237. static struct of_device_id gpio_mdio_match[] =
  238. {
  239. {
  240. .compatible = "gpio-mdio",
  241. },
  242. {},
  243. };
  244. MODULE_DEVICE_TABLE(of, gpio_mdio_match);
  245. static struct platform_driver gpio_mdio_driver =
  246. {
  247. .probe = gpio_mdio_probe,
  248. .remove = gpio_mdio_remove,
  249. .driver = {
  250. .name = "gpio-mdio-bitbang",
  251. .owner = THIS_MODULE,
  252. .of_match_table = gpio_mdio_match,
  253. },
  254. };
  255. int gpio_mdio_init(void)
  256. {
  257. struct device_node *np;
  258. np = of_find_compatible_node(NULL, NULL, "1682m-gpio");
  259. if (!np)
  260. np = of_find_compatible_node(NULL, NULL,
  261. "pasemi,pwrficient-gpio");
  262. if (!np)
  263. return -ENODEV;
  264. gpio_regs = of_iomap(np, 0);
  265. of_node_put(np);
  266. if (!gpio_regs)
  267. return -ENODEV;
  268. return platform_driver_register(&gpio_mdio_driver);
  269. }
  270. module_init(gpio_mdio_init);
  271. void gpio_mdio_exit(void)
  272. {
  273. platform_driver_unregister(&gpio_mdio_driver);
  274. if (gpio_regs)
  275. iounmap(gpio_regs);
  276. }
  277. module_exit(gpio_mdio_exit);
  278. MODULE_LICENSE("GPL");
  279. MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
  280. MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards");