ucc_geth_mii.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * drivers/net/ucc_geth_mii.c
  3. *
  4. * QE UCC Gigabit Ethernet Driver -- MII Management Bus Implementation
  5. * Provides Bus interface for MII Management regs in the UCC register space
  6. *
  7. * Copyright (C) 2007 Freescale Semiconductor, Inc.
  8. *
  9. * Authors: Li Yang <leoli@freescale.com>
  10. * Kim Phillips <kim.phillips@freescale.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/string.h>
  21. #include <linux/errno.h>
  22. #include <linux/unistd.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/mm.h>
  32. #include <linux/module.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/crc32.h>
  35. #include <linux/mii.h>
  36. #include <linux/phy.h>
  37. #include <linux/fsl_devices.h>
  38. #include <linux/of_platform.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/ucc.h>
  43. #include "ucc_geth_mii.h"
  44. #include "ucc_geth.h"
  45. #define DEBUG
  46. #ifdef DEBUG
  47. #define vdbg(format, arg...) printk(KERN_DEBUG , format "\n" , ## arg)
  48. #else
  49. #define vdbg(format, arg...) do {} while(0)
  50. #endif
  51. #define MII_DRV_DESC "QE UCC Ethernet Controller MII Bus"
  52. #define MII_DRV_NAME "fsl-uec_mdio"
  53. /* Write value to the PHY for this device to the register at regnum, */
  54. /* waiting until the write is done before it returns. All PHY */
  55. /* configuration has to be done through the master UEC MIIM regs */
  56. int uec_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
  57. {
  58. struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
  59. /* Setting up the MII Mangement Address Register */
  60. out_be32(&regs->miimadd,
  61. (mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | regnum);
  62. /* Setting up the MII Mangement Control Register with the value */
  63. out_be32(&regs->miimcon, value);
  64. /* Wait till MII management write is complete */
  65. while ((in_be32(&regs->miimind)) & MIIMIND_BUSY)
  66. cpu_relax();
  67. return 0;
  68. }
  69. /* Reads from register regnum in the PHY for device dev, */
  70. /* returning the value. Clears miimcom first. All PHY */
  71. /* configuration has to be done through the TSEC1 MIIM regs */
  72. int uec_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  73. {
  74. struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
  75. u16 value;
  76. /* Setting up the MII Mangement Address Register */
  77. out_be32(&regs->miimadd,
  78. (mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | regnum);
  79. /* Clear miimcom, perform an MII management read cycle */
  80. out_be32(&regs->miimcom, 0);
  81. out_be32(&regs->miimcom, MIIMCOM_READ_CYCLE);
  82. /* Wait till MII management write is complete */
  83. while ((in_be32(&regs->miimind)) & (MIIMIND_BUSY | MIIMIND_NOT_VALID))
  84. cpu_relax();
  85. /* Read MII management status */
  86. value = in_be32(&regs->miimstat);
  87. return value;
  88. }
  89. /* Reset the MIIM registers, and wait for the bus to free */
  90. static int uec_mdio_reset(struct mii_bus *bus)
  91. {
  92. struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
  93. unsigned int timeout = PHY_INIT_TIMEOUT;
  94. mutex_lock(&bus->mdio_lock);
  95. /* Reset the management interface */
  96. out_be32(&regs->miimcfg, MIIMCFG_RESET_MANAGEMENT);
  97. /* Setup the MII Mgmt clock speed */
  98. out_be32(&regs->miimcfg, MIIMCFG_MANAGEMENT_CLOCK_DIVIDE_BY_112);
  99. /* Wait until the bus is free */
  100. while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
  101. cpu_relax();
  102. mutex_unlock(&bus->mdio_lock);
  103. if (timeout <= 0) {
  104. printk(KERN_ERR "%s: The MII Bus is stuck!\n", bus->name);
  105. return -EBUSY;
  106. }
  107. return 0;
  108. }
  109. static int uec_mdio_probe(struct of_device *ofdev, const struct of_device_id *match)
  110. {
  111. struct device *device = &ofdev->dev;
  112. struct device_node *np = ofdev->node, *tempnp = NULL;
  113. struct device_node *child = NULL;
  114. struct ucc_mii_mng __iomem *regs;
  115. struct mii_bus *new_bus;
  116. struct resource res;
  117. int k, err = 0;
  118. new_bus = mdiobus_alloc();
  119. if (NULL == new_bus)
  120. return -ENOMEM;
  121. new_bus->name = "UCC Ethernet Controller MII Bus";
  122. new_bus->read = &uec_mdio_read;
  123. new_bus->write = &uec_mdio_write;
  124. new_bus->reset = &uec_mdio_reset;
  125. memset(&res, 0, sizeof(res));
  126. err = of_address_to_resource(np, 0, &res);
  127. if (err)
  128. goto reg_map_fail;
  129. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  130. new_bus->irq = kmalloc(32 * sizeof(int), GFP_KERNEL);
  131. if (NULL == new_bus->irq) {
  132. err = -ENOMEM;
  133. goto reg_map_fail;
  134. }
  135. for (k = 0; k < 32; k++)
  136. new_bus->irq[k] = PHY_POLL;
  137. while ((child = of_get_next_child(np, child)) != NULL) {
  138. int irq = irq_of_parse_and_map(child, 0);
  139. if (irq != NO_IRQ) {
  140. const u32 *id = of_get_property(child, "reg", NULL);
  141. new_bus->irq[*id] = irq;
  142. }
  143. }
  144. /* Set the base address */
  145. regs = ioremap(res.start, sizeof(struct ucc_mii_mng));
  146. if (NULL == regs) {
  147. err = -ENOMEM;
  148. goto ioremap_fail;
  149. }
  150. new_bus->priv = (void __force *)regs;
  151. new_bus->parent = device;
  152. dev_set_drvdata(device, new_bus);
  153. /* Read MII management master from device tree */
  154. while ((tempnp = of_find_compatible_node(tempnp, "network", "ucc_geth"))
  155. != NULL) {
  156. struct resource tempres;
  157. err = of_address_to_resource(tempnp, 0, &tempres);
  158. if (err)
  159. goto bus_register_fail;
  160. /* if our mdio regs fall within this UCC regs range */
  161. if ((res.start >= tempres.start) &&
  162. (res.end <= tempres.end)) {
  163. /* set this UCC to be the MII master */
  164. const u32 *id;
  165. id = of_get_property(tempnp, "cell-index", NULL);
  166. if (!id) {
  167. id = of_get_property(tempnp, "device-id", NULL);
  168. if (!id)
  169. goto bus_register_fail;
  170. }
  171. ucc_set_qe_mux_mii_mng(*id - 1);
  172. /* assign the TBI an address which won't
  173. * conflict with the PHYs */
  174. out_be32(&regs->utbipar, UTBIPAR_INIT_TBIPA);
  175. break;
  176. }
  177. }
  178. err = mdiobus_register(new_bus);
  179. if (0 != err) {
  180. printk(KERN_ERR "%s: Cannot register as MDIO bus\n",
  181. new_bus->name);
  182. goto bus_register_fail;
  183. }
  184. return 0;
  185. bus_register_fail:
  186. iounmap(regs);
  187. ioremap_fail:
  188. kfree(new_bus->irq);
  189. reg_map_fail:
  190. mdiobus_free(new_bus);
  191. return err;
  192. }
  193. static int uec_mdio_remove(struct of_device *ofdev)
  194. {
  195. struct device *device = &ofdev->dev;
  196. struct mii_bus *bus = dev_get_drvdata(device);
  197. mdiobus_unregister(bus);
  198. dev_set_drvdata(device, NULL);
  199. iounmap((void __iomem *)bus->priv);
  200. bus->priv = NULL;
  201. mdiobus_free(bus);
  202. return 0;
  203. }
  204. static struct of_device_id uec_mdio_match[] = {
  205. {
  206. .type = "mdio",
  207. .compatible = "ucc_geth_phy",
  208. },
  209. {
  210. .compatible = "fsl,ucc-mdio",
  211. },
  212. {},
  213. };
  214. static struct of_platform_driver uec_mdio_driver = {
  215. .name = MII_DRV_NAME,
  216. .probe = uec_mdio_probe,
  217. .remove = uec_mdio_remove,
  218. .match_table = uec_mdio_match,
  219. };
  220. int __init uec_mdio_init(void)
  221. {
  222. return of_register_platform_driver(&uec_mdio_driver);
  223. }
  224. /* called from __init ucc_geth_init, therefore can not be __exit */
  225. void uec_mdio_exit(void)
  226. {
  227. of_unregister_platform_driver(&uec_mdio_driver);
  228. }