gpio_mdio.c 7.3 KB

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