gianfar_mii.c 4.9 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 (kumar.gala@freescale.com)
  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/version.h>
  35. #include <linux/platform_device.h>
  36. #include <asm/ocp.h>
  37. #include <linux/crc32.h>
  38. #include <linux/mii.h>
  39. #include <linux/phy.h>
  40. #include <asm/io.h>
  41. #include <asm/irq.h>
  42. #include <asm/uaccess.h>
  43. #include "gianfar.h"
  44. #include "gianfar_mii.h"
  45. /* Write value to the PHY at mii_id at register regnum,
  46. * on the bus, waiting until the write is done before returning.
  47. * All PHY configuration is done through the TSEC1 MIIM regs */
  48. int gfar_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
  49. {
  50. struct gfar_mii *regs = bus->priv;
  51. /* Set the PHY address and the register address we want to write */
  52. gfar_write(&regs->miimadd, (mii_id << 8) | regnum);
  53. /* Write out the value we want */
  54. gfar_write(&regs->miimcon, value);
  55. /* Wait for the transaction to finish */
  56. while (gfar_read(&regs->miimind) & MIIMIND_BUSY)
  57. cpu_relax();
  58. return 0;
  59. }
  60. /* Read the bus for PHY at addr mii_id, register regnum, and
  61. * return the value. Clears miimcom first. All PHY
  62. * configuration has to be done through the TSEC1 MIIM regs */
  63. int gfar_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  64. {
  65. struct gfar_mii *regs = bus->priv;
  66. u16 value;
  67. /* Set the PHY address and the register address we want to read */
  68. gfar_write(&regs->miimadd, (mii_id << 8) | regnum);
  69. /* Clear miimcom, and then initiate a read */
  70. gfar_write(&regs->miimcom, 0);
  71. gfar_write(&regs->miimcom, MII_READ_COMMAND);
  72. /* Wait for the transaction to finish */
  73. while (gfar_read(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
  74. cpu_relax();
  75. /* Grab the value of the register from miimstat */
  76. value = gfar_read(&regs->miimstat);
  77. return value;
  78. }
  79. /* Reset the MIIM registers, and wait for the bus to free */
  80. int gfar_mdio_reset(struct mii_bus *bus)
  81. {
  82. struct gfar_mii *regs = bus->priv;
  83. unsigned int timeout = PHY_INIT_TIMEOUT;
  84. spin_lock_bh(&bus->mdio_lock);
  85. /* Reset the management interface */
  86. gfar_write(&regs->miimcfg, MIIMCFG_RESET);
  87. /* Setup the MII Mgmt clock speed */
  88. gfar_write(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  89. /* Wait until the bus is free */
  90. while ((gfar_read(&regs->miimind) & MIIMIND_BUSY) &&
  91. timeout--)
  92. cpu_relax();
  93. spin_unlock_bh(&bus->mdio_lock);
  94. if(timeout <= 0) {
  95. printk(KERN_ERR "%s: The MII Bus is stuck!\n",
  96. bus->name);
  97. return -EBUSY;
  98. }
  99. return 0;
  100. }
  101. int gfar_mdio_probe(struct device *dev)
  102. {
  103. struct platform_device *pdev = to_platform_device(dev);
  104. struct gianfar_mdio_data *pdata;
  105. struct gfar_mii *regs;
  106. struct mii_bus *new_bus;
  107. int err = 0;
  108. if (NULL == dev)
  109. return -EINVAL;
  110. new_bus = kmalloc(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. /* Set the PHY base address */
  124. regs = (struct gfar_mii *) ioremap(pdata->paddr,
  125. sizeof (struct gfar_mii));
  126. if (NULL == regs) {
  127. err = -ENOMEM;
  128. goto reg_map_fail;
  129. }
  130. new_bus->priv = 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((void *) 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 *) (&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. }