gianfar_mii.c 5.0 KB

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