gpio_mdio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/sched.h>
  27. #include <linux/errno.h>
  28. #include <linux/ioport.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/phy.h>
  31. #include <linux/of_mdio.h>
  32. #include <linux/of_platform.h>
  33. #define DELAY 1
  34. static void __iomem *gpio_regs;
  35. struct gpio_priv {
  36. int mdc_pin;
  37. int mdio_pin;
  38. int mdio_irqs[PHY_MAX_ADDR];
  39. };
  40. #define MDC_PIN(bus) (((struct gpio_priv *)bus->priv)->mdc_pin)
  41. #define MDIO_PIN(bus) (((struct gpio_priv *)bus->priv)->mdio_pin)
  42. static inline void mdio_lo(struct mii_bus *bus)
  43. {
  44. out_le32(gpio_regs+0x10, 1 << MDIO_PIN(bus));
  45. }
  46. static inline void mdio_hi(struct mii_bus *bus)
  47. {
  48. out_le32(gpio_regs, 1 << MDIO_PIN(bus));
  49. }
  50. static inline void mdc_lo(struct mii_bus *bus)
  51. {
  52. out_le32(gpio_regs+0x10, 1 << MDC_PIN(bus));
  53. }
  54. static inline void mdc_hi(struct mii_bus *bus)
  55. {
  56. out_le32(gpio_regs, 1 << MDC_PIN(bus));
  57. }
  58. static inline void mdio_active(struct mii_bus *bus)
  59. {
  60. out_le32(gpio_regs+0x20, (1 << MDC_PIN(bus)) | (1 << MDIO_PIN(bus)));
  61. }
  62. static inline void mdio_tristate(struct mii_bus *bus)
  63. {
  64. out_le32(gpio_regs+0x30, (1 << MDIO_PIN(bus)));
  65. }
  66. static inline int mdio_read(struct mii_bus *bus)
  67. {
  68. return !!(in_le32(gpio_regs+0x40) & (1 << MDIO_PIN(bus)));
  69. }
  70. static void clock_out(struct mii_bus *bus, int bit)
  71. {
  72. if (bit)
  73. mdio_hi(bus);
  74. else
  75. mdio_lo(bus);
  76. udelay(DELAY);
  77. mdc_hi(bus);
  78. udelay(DELAY);
  79. mdc_lo(bus);
  80. }
  81. /* Utility to send the preamble, address, and register (common to read and write). */
  82. static void bitbang_pre(struct mii_bus *bus, int read, u8 addr, u8 reg)
  83. {
  84. int i;
  85. /* CFE uses a really long preamble (40 bits). We'll do the same. */
  86. mdio_active(bus);
  87. for (i = 0; i < 40; i++) {
  88. clock_out(bus, 1);
  89. }
  90. /* send the start bit (01) and the read opcode (10) or write (10) */
  91. clock_out(bus, 0);
  92. clock_out(bus, 1);
  93. clock_out(bus, read);
  94. clock_out(bus, !read);
  95. /* send the PHY address */
  96. for (i = 0; i < 5; i++) {
  97. clock_out(bus, (addr & 0x10) != 0);
  98. addr <<= 1;
  99. }
  100. /* send the register address */
  101. for (i = 0; i < 5; i++) {
  102. clock_out(bus, (reg & 0x10) != 0);
  103. reg <<= 1;
  104. }
  105. }
  106. static int gpio_mdio_read(struct mii_bus *bus, int phy_id, int location)
  107. {
  108. u16 rdreg;
  109. int ret, i;
  110. u8 addr = phy_id & 0xff;
  111. u8 reg = location & 0xff;
  112. bitbang_pre(bus, 1, addr, reg);
  113. /* tri-state our MDIO I/O pin so we can read */
  114. mdio_tristate(bus);
  115. udelay(DELAY);
  116. mdc_hi(bus);
  117. udelay(DELAY);
  118. mdc_lo(bus);
  119. /* read 16 bits of register data, MSB first */
  120. rdreg = 0;
  121. for (i = 0; i < 16; i++) {
  122. mdc_lo(bus);
  123. udelay(DELAY);
  124. mdc_hi(bus);
  125. udelay(DELAY);
  126. mdc_lo(bus);
  127. udelay(DELAY);
  128. rdreg <<= 1;
  129. rdreg |= mdio_read(bus);
  130. }
  131. mdc_hi(bus);
  132. udelay(DELAY);
  133. mdc_lo(bus);
  134. udelay(DELAY);
  135. ret = rdreg;
  136. return ret;
  137. }
  138. static int gpio_mdio_write(struct mii_bus *bus, int phy_id, int location, u16 val)
  139. {
  140. int i;
  141. u8 addr = phy_id & 0xff;
  142. u8 reg = location & 0xff;
  143. u16 value = val & 0xffff;
  144. bitbang_pre(bus, 0, addr, reg);
  145. /* send the turnaround (10) */
  146. mdc_lo(bus);
  147. mdio_hi(bus);
  148. udelay(DELAY);
  149. mdc_hi(bus);
  150. udelay(DELAY);
  151. mdc_lo(bus);
  152. mdio_lo(bus);
  153. udelay(DELAY);
  154. mdc_hi(bus);
  155. udelay(DELAY);
  156. /* write 16 bits of register data, MSB first */
  157. for (i = 0; i < 16; i++) {
  158. mdc_lo(bus);
  159. if (value & 0x8000)
  160. mdio_hi(bus);
  161. else
  162. mdio_lo(bus);
  163. udelay(DELAY);
  164. mdc_hi(bus);
  165. udelay(DELAY);
  166. value <<= 1;
  167. }
  168. /*
  169. * Tri-state the MDIO line.
  170. */
  171. mdio_tristate(bus);
  172. mdc_lo(bus);
  173. udelay(DELAY);
  174. mdc_hi(bus);
  175. udelay(DELAY);
  176. return 0;
  177. }
  178. static int gpio_mdio_reset(struct mii_bus *bus)
  179. {
  180. /*nothing here - dunno how to reset it*/
  181. return 0;
  182. }
  183. static int __devinit gpio_mdio_probe(struct of_device *ofdev,
  184. const struct of_device_id *match)
  185. {
  186. struct device *dev = &ofdev->dev;
  187. struct device_node *np = ofdev->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 of_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 of_platform_driver gpio_mdio_driver =
  246. {
  247. .match_table = gpio_mdio_match,
  248. .probe = gpio_mdio_probe,
  249. .remove = gpio_mdio_remove,
  250. .driver = {
  251. .name = "gpio-mdio-bitbang",
  252. },
  253. };
  254. int gpio_mdio_init(void)
  255. {
  256. struct device_node *np;
  257. np = of_find_compatible_node(NULL, NULL, "1682m-gpio");
  258. if (!np)
  259. np = of_find_compatible_node(NULL, NULL,
  260. "pasemi,pwrficient-gpio");
  261. if (!np)
  262. return -ENODEV;
  263. gpio_regs = of_iomap(np, 0);
  264. of_node_put(np);
  265. if (!gpio_regs)
  266. return -ENODEV;
  267. return of_register_platform_driver(&gpio_mdio_driver);
  268. }
  269. module_init(gpio_mdio_init);
  270. void gpio_mdio_exit(void)
  271. {
  272. of_unregister_platform_driver(&gpio_mdio_driver);
  273. if (gpio_regs)
  274. iounmap(gpio_regs);
  275. }
  276. module_exit(gpio_mdio_exit);
  277. MODULE_LICENSE("GPL");
  278. MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
  279. MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards");