gianfar_mii.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * drivers/net/gianfar_mii.c
  3. *
  4. * Gianfar Ethernet Driver -- MIIM bus implementation
  5. * Provides Bus interface for MIIM regs
  6. *
  7. * Author: Andy Fleming
  8. * Maintainer: Kumar Gala
  9. *
  10. * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
  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/config.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/string.h>
  22. #include <linux/errno.h>
  23. #include <linux/unistd.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/mm.h>
  33. #include <linux/module.h>
  34. #include <linux/platform_device.h>
  35. #include <asm/ocp.h>
  36. #include <linux/crc32.h>
  37. #include <linux/mii.h>
  38. #include <linux/phy.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/uaccess.h>
  42. #include "gianfar.h"
  43. #include "gianfar_mii.h"
  44. /* Write value to the PHY at mii_id at register regnum,
  45. * on the bus, waiting until the write is done before returning.
  46. * All PHY configuration is done through the TSEC1 MIIM regs */
  47. int gfar_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
  48. {
  49. struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;
  50. /* Set the PHY address and the register address we want to write */
  51. gfar_write(&regs->miimadd, (mii_id << 8) | regnum);
  52. /* Write out the value we want */
  53. gfar_write(&regs->miimcon, value);
  54. /* Wait for the transaction to finish */
  55. while (gfar_read(&regs->miimind) & MIIMIND_BUSY)
  56. cpu_relax();
  57. return 0;
  58. }
  59. /* Read the bus for PHY at addr mii_id, register regnum, and
  60. * return the value. Clears miimcom first. All PHY
  61. * configuration has to be done through the TSEC1 MIIM regs */
  62. int gfar_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  63. {
  64. struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;
  65. u16 value;
  66. /* Set the PHY address and the register address we want to read */
  67. gfar_write(&regs->miimadd, (mii_id << 8) | regnum);
  68. /* Clear miimcom, and then initiate a read */
  69. gfar_write(&regs->miimcom, 0);
  70. gfar_write(&regs->miimcom, MII_READ_COMMAND);
  71. /* Wait for the transaction to finish */
  72. while (gfar_read(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
  73. cpu_relax();
  74. /* Grab the value of the register from miimstat */
  75. value = gfar_read(&regs->miimstat);
  76. return value;
  77. }
  78. /* Reset the MIIM registers, and wait for the bus to free */
  79. int gfar_mdio_reset(struct mii_bus *bus)
  80. {
  81. struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;
  82. unsigned int timeout = PHY_INIT_TIMEOUT;
  83. spin_lock_bh(&bus->mdio_lock);
  84. /* Reset the management interface */
  85. gfar_write(&regs->miimcfg, MIIMCFG_RESET);
  86. /* Setup the MII Mgmt clock speed */
  87. gfar_write(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  88. /* Wait until the bus is free */
  89. while ((gfar_read(&regs->miimind) & MIIMIND_BUSY) &&
  90. timeout--)
  91. cpu_relax();
  92. spin_unlock_bh(&bus->mdio_lock);
  93. if(timeout <= 0) {
  94. printk(KERN_ERR "%s: The MII Bus is stuck!\n",
  95. bus->name);
  96. return -EBUSY;
  97. }
  98. return 0;
  99. }
  100. int gfar_mdio_probe(struct device *dev)
  101. {
  102. struct platform_device *pdev = to_platform_device(dev);
  103. struct gianfar_mdio_data *pdata;
  104. struct gfar_mii __iomem *regs;
  105. struct mii_bus *new_bus;
  106. struct resource *r;
  107. int err = 0;
  108. if (NULL == dev)
  109. return -EINVAL;
  110. new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
  111. if (NULL == new_bus)
  112. return -ENOMEM;
  113. new_bus->name = "Gianfar MII Bus",
  114. new_bus->read = &gfar_mdio_read,
  115. new_bus->write = &gfar_mdio_write,
  116. new_bus->reset = &gfar_mdio_reset,
  117. new_bus->id = pdev->id;
  118. pdata = (struct gianfar_mdio_data *)pdev->dev.platform_data;
  119. if (NULL == pdata) {
  120. printk(KERN_ERR "gfar mdio %d: Missing platform data!\n", pdev->id);
  121. return -ENODEV;
  122. }
  123. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  124. /* Set the PHY base address */
  125. regs = ioremap(r->start, sizeof (struct gfar_mii));
  126. if (NULL == regs) {
  127. err = -ENOMEM;
  128. goto reg_map_fail;
  129. }
  130. new_bus->priv = (void __force *)regs;
  131. new_bus->irq = pdata->irq;
  132. new_bus->dev = dev;
  133. dev_set_drvdata(dev, new_bus);
  134. err = mdiobus_register(new_bus);
  135. if (0 != err) {
  136. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  137. new_bus->name);
  138. goto bus_register_fail;
  139. }
  140. return 0;
  141. bus_register_fail:
  142. iounmap(regs);
  143. reg_map_fail:
  144. kfree(new_bus);
  145. return err;
  146. }
  147. int gfar_mdio_remove(struct device *dev)
  148. {
  149. struct mii_bus *bus = dev_get_drvdata(dev);
  150. mdiobus_unregister(bus);
  151. dev_set_drvdata(dev, NULL);
  152. iounmap((void __iomem *)bus->priv);
  153. bus->priv = NULL;
  154. kfree(bus);
  155. return 0;
  156. }
  157. static struct device_driver gianfar_mdio_driver = {
  158. .name = "fsl-gianfar_mdio",
  159. .bus = &platform_bus_type,
  160. .probe = gfar_mdio_probe,
  161. .remove = gfar_mdio_remove,
  162. };
  163. int __init gfar_mdio_init(void)
  164. {
  165. return driver_register(&gianfar_mdio_driver);
  166. }
  167. void __exit gfar_mdio_exit(void)
  168. {
  169. driver_unregister(&gianfar_mdio_driver);
  170. }