ucc_geth_mii.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <asm/ocp.h>
  35. #include <linux/crc32.h>
  36. #include <linux/mii.h>
  37. #include <linux/phy.h>
  38. #include <linux/fsl_devices.h>
  39. #include <asm/of_platform.h>
  40. #include <asm/io.h>
  41. #include <asm/irq.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/ucc.h>
  44. #include "ucc_geth_mii.h"
  45. #include "ucc_geth.h"
  46. #define DEBUG
  47. #ifdef DEBUG
  48. #define vdbg(format, arg...) printk(KERN_DEBUG , format "\n" , ## arg)
  49. #else
  50. #define vdbg(format, arg...) do {} while(0)
  51. #endif
  52. #define DRV_DESC "QE UCC Ethernet Controller MII Bus"
  53. #define DRV_NAME "fsl-uec_mdio"
  54. /* Write value to the PHY for this device to the register at regnum, */
  55. /* waiting until the write is done before it returns. All PHY */
  56. /* configuration has to be done through the master UEC MIIM regs */
  57. int uec_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
  58. {
  59. struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
  60. /* Setting up the MII Mangement Address Register */
  61. out_be32(&regs->miimadd,
  62. (mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | regnum);
  63. /* Setting up the MII Mangement Control Register with the value */
  64. out_be32(&regs->miimcon, value);
  65. /* Wait till MII management write is complete */
  66. while ((in_be32(&regs->miimind)) & MIIMIND_BUSY)
  67. cpu_relax();
  68. return 0;
  69. }
  70. /* Reads from register regnum in the PHY for device dev, */
  71. /* returning the value. Clears miimcom first. All PHY */
  72. /* configuration has to be done through the TSEC1 MIIM regs */
  73. int uec_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  74. {
  75. struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
  76. u16 value;
  77. /* Setting up the MII Mangement Address Register */
  78. out_be32(&regs->miimadd,
  79. (mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | regnum);
  80. /* Clear miimcom, perform an MII management read cycle */
  81. out_be32(&regs->miimcom, 0);
  82. out_be32(&regs->miimcom, MIIMCOM_READ_CYCLE);
  83. /* Wait till MII management write is complete */
  84. while ((in_be32(&regs->miimind)) & (MIIMIND_BUSY | MIIMIND_NOT_VALID))
  85. cpu_relax();
  86. /* Read MII management status */
  87. value = in_be32(&regs->miimstat);
  88. return value;
  89. }
  90. /* Reset the MIIM registers, and wait for the bus to free */
  91. int uec_mdio_reset(struct mii_bus *bus)
  92. {
  93. struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
  94. unsigned int timeout = PHY_INIT_TIMEOUT;
  95. spin_lock_bh(&bus->mdio_lock);
  96. /* Reset the management interface */
  97. out_be32(&regs->miimcfg, MIIMCFG_RESET_MANAGEMENT);
  98. /* Setup the MII Mgmt clock speed */
  99. out_be32(&regs->miimcfg, MIIMCFG_MANAGEMENT_CLOCK_DIVIDE_BY_112);
  100. /* Wait until the bus is free */
  101. while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
  102. cpu_relax();
  103. spin_unlock_bh(&bus->mdio_lock);
  104. if (timeout <= 0) {
  105. printk(KERN_ERR "%s: The MII Bus is stuck!\n", bus->name);
  106. return -EBUSY;
  107. }
  108. return 0;
  109. }
  110. static int uec_mdio_probe(struct of_device *ofdev, const struct of_device_id *match)
  111. {
  112. struct device *device = &ofdev->dev;
  113. struct device_node *np = ofdev->node, *tempnp = NULL;
  114. struct device_node *child = NULL;
  115. struct ucc_mii_mng __iomem *regs;
  116. struct mii_bus *new_bus;
  117. struct resource res;
  118. int k, err = 0;
  119. new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
  120. if (NULL == new_bus)
  121. return -ENOMEM;
  122. new_bus->name = "UCC Ethernet Controller MII Bus";
  123. new_bus->read = &uec_mdio_read;
  124. new_bus->write = &uec_mdio_write;
  125. new_bus->reset = &uec_mdio_reset;
  126. memset(&res, 0, sizeof(res));
  127. err = of_address_to_resource(np, 0, &res);
  128. if (err)
  129. goto reg_map_fail;
  130. new_bus->id = res.start;
  131. new_bus->irq = kmalloc(32 * sizeof(int), GFP_KERNEL);
  132. if (NULL == new_bus->irq) {
  133. err = -ENOMEM;
  134. goto reg_map_fail;
  135. }
  136. for (k = 0; k < 32; k++)
  137. new_bus->irq[k] = PHY_POLL;
  138. while ((child = of_get_next_child(np, child)) != NULL) {
  139. int irq = irq_of_parse_and_map(child, 0);
  140. if (irq != NO_IRQ) {
  141. const u32 *id = of_get_property(child, "reg", NULL);
  142. new_bus->irq[*id] = irq;
  143. }
  144. }
  145. /* Set the base address */
  146. regs = ioremap(res.start, sizeof(struct ucc_mii_mng));
  147. if (NULL == regs) {
  148. err = -ENOMEM;
  149. goto ioremap_fail;
  150. }
  151. new_bus->priv = (void __force *)regs;
  152. new_bus->dev = device;
  153. dev_set_drvdata(device, new_bus);
  154. /* Read MII management master from device tree */
  155. while ((tempnp = of_find_compatible_node(tempnp, "network", "ucc_geth"))
  156. != NULL) {
  157. struct resource tempres;
  158. err = of_address_to_resource(tempnp, 0, &tempres);
  159. if (err)
  160. goto bus_register_fail;
  161. /* if our mdio regs fall within this UCC regs range */
  162. if ((res.start >= tempres.start) &&
  163. (res.end <= tempres.end)) {
  164. /* set this UCC to be the MII master */
  165. const u32 *id = of_get_property(tempnp, "device-id", NULL);
  166. if (id == NULL)
  167. goto bus_register_fail;
  168. ucc_set_qe_mux_mii_mng(*id - 1);
  169. /* assign the TBI an address which won't
  170. * conflict with the PHYs */
  171. out_be32(&regs->utbipar, UTBIPAR_INIT_TBIPA);
  172. break;
  173. }
  174. }
  175. err = mdiobus_register(new_bus);
  176. if (0 != err) {
  177. printk(KERN_ERR "%s: Cannot register as MDIO bus\n",
  178. new_bus->name);
  179. goto bus_register_fail;
  180. }
  181. return 0;
  182. bus_register_fail:
  183. iounmap(regs);
  184. ioremap_fail:
  185. kfree(new_bus->irq);
  186. reg_map_fail:
  187. kfree(new_bus);
  188. return err;
  189. }
  190. int uec_mdio_remove(struct of_device *ofdev)
  191. {
  192. struct device *device = &ofdev->dev;
  193. struct mii_bus *bus = dev_get_drvdata(device);
  194. mdiobus_unregister(bus);
  195. dev_set_drvdata(device, NULL);
  196. iounmap((void __iomem *)bus->priv);
  197. bus->priv = NULL;
  198. kfree(bus);
  199. return 0;
  200. }
  201. static struct of_device_id uec_mdio_match[] = {
  202. {
  203. .type = "mdio",
  204. .compatible = "ucc_geth_phy",
  205. },
  206. {},
  207. };
  208. static struct of_platform_driver uec_mdio_driver = {
  209. .name = DRV_NAME,
  210. .probe = uec_mdio_probe,
  211. .remove = uec_mdio_remove,
  212. .match_table = uec_mdio_match,
  213. };
  214. int __init uec_mdio_init(void)
  215. {
  216. return of_register_platform_driver(&uec_mdio_driver);
  217. }
  218. void __exit uec_mdio_exit(void)
  219. {
  220. of_unregister_platform_driver(&uec_mdio_driver);
  221. }