gianfar_mii.c 5.0 KB

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