mdio-octeon.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2009 Cavium Networks
  7. */
  8. #include <linux/gfp.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/phy.h>
  13. #include <asm/octeon/octeon.h>
  14. #include <asm/octeon/cvmx-smix-defs.h>
  15. #define DRV_VERSION "1.0"
  16. #define DRV_DESCRIPTION "Cavium Networks Octeon SMI/MDIO driver"
  17. struct octeon_mdiobus {
  18. struct mii_bus *mii_bus;
  19. int unit;
  20. int phy_irq[PHY_MAX_ADDR];
  21. };
  22. static int octeon_mdiobus_read(struct mii_bus *bus, int phy_id, int regnum)
  23. {
  24. struct octeon_mdiobus *p = bus->priv;
  25. union cvmx_smix_cmd smi_cmd;
  26. union cvmx_smix_rd_dat smi_rd;
  27. int timeout = 1000;
  28. smi_cmd.u64 = 0;
  29. smi_cmd.s.phy_op = 1; /* MDIO_CLAUSE_22_READ */
  30. smi_cmd.s.phy_adr = phy_id;
  31. smi_cmd.s.reg_adr = regnum;
  32. cvmx_write_csr(CVMX_SMIX_CMD(p->unit), smi_cmd.u64);
  33. do {
  34. /*
  35. * Wait 1000 clocks so we don't saturate the RSL bus
  36. * doing reads.
  37. */
  38. cvmx_wait(1000);
  39. smi_rd.u64 = cvmx_read_csr(CVMX_SMIX_RD_DAT(p->unit));
  40. } while (smi_rd.s.pending && --timeout);
  41. if (smi_rd.s.val)
  42. return smi_rd.s.dat;
  43. else
  44. return -EIO;
  45. }
  46. static int octeon_mdiobus_write(struct mii_bus *bus, int phy_id,
  47. int regnum, u16 val)
  48. {
  49. struct octeon_mdiobus *p = bus->priv;
  50. union cvmx_smix_cmd smi_cmd;
  51. union cvmx_smix_wr_dat smi_wr;
  52. int timeout = 1000;
  53. smi_wr.u64 = 0;
  54. smi_wr.s.dat = val;
  55. cvmx_write_csr(CVMX_SMIX_WR_DAT(p->unit), smi_wr.u64);
  56. smi_cmd.u64 = 0;
  57. smi_cmd.s.phy_op = 0; /* MDIO_CLAUSE_22_WRITE */
  58. smi_cmd.s.phy_adr = phy_id;
  59. smi_cmd.s.reg_adr = regnum;
  60. cvmx_write_csr(CVMX_SMIX_CMD(p->unit), smi_cmd.u64);
  61. do {
  62. /*
  63. * Wait 1000 clocks so we don't saturate the RSL bus
  64. * doing reads.
  65. */
  66. cvmx_wait(1000);
  67. smi_wr.u64 = cvmx_read_csr(CVMX_SMIX_WR_DAT(p->unit));
  68. } while (smi_wr.s.pending && --timeout);
  69. if (timeout <= 0)
  70. return -EIO;
  71. return 0;
  72. }
  73. static int __devinit octeon_mdiobus_probe(struct platform_device *pdev)
  74. {
  75. struct octeon_mdiobus *bus;
  76. union cvmx_smix_en smi_en;
  77. int i;
  78. int err = -ENOENT;
  79. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  80. if (!bus)
  81. return -ENOMEM;
  82. /* The platform_device id is our unit number. */
  83. bus->unit = pdev->id;
  84. bus->mii_bus = mdiobus_alloc();
  85. if (!bus->mii_bus)
  86. goto err;
  87. smi_en.u64 = 0;
  88. smi_en.s.en = 1;
  89. cvmx_write_csr(CVMX_SMIX_EN(bus->unit), smi_en.u64);
  90. /*
  91. * Standard Octeon evaluation boards don't support phy
  92. * interrupts, we need to poll.
  93. */
  94. for (i = 0; i < PHY_MAX_ADDR; i++)
  95. bus->phy_irq[i] = PHY_POLL;
  96. bus->mii_bus->priv = bus;
  97. bus->mii_bus->irq = bus->phy_irq;
  98. bus->mii_bus->name = "mdio-octeon";
  99. snprintf(bus->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  100. bus->mii_bus->name, bus->unit);
  101. bus->mii_bus->parent = &pdev->dev;
  102. bus->mii_bus->read = octeon_mdiobus_read;
  103. bus->mii_bus->write = octeon_mdiobus_write;
  104. dev_set_drvdata(&pdev->dev, bus);
  105. err = mdiobus_register(bus->mii_bus);
  106. if (err)
  107. goto err_register;
  108. dev_info(&pdev->dev, "Version " DRV_VERSION "\n");
  109. return 0;
  110. err_register:
  111. mdiobus_free(bus->mii_bus);
  112. err:
  113. devm_kfree(&pdev->dev, bus);
  114. smi_en.u64 = 0;
  115. cvmx_write_csr(CVMX_SMIX_EN(bus->unit), smi_en.u64);
  116. return err;
  117. }
  118. static int __devexit octeon_mdiobus_remove(struct platform_device *pdev)
  119. {
  120. struct octeon_mdiobus *bus;
  121. union cvmx_smix_en smi_en;
  122. bus = dev_get_drvdata(&pdev->dev);
  123. mdiobus_unregister(bus->mii_bus);
  124. mdiobus_free(bus->mii_bus);
  125. smi_en.u64 = 0;
  126. cvmx_write_csr(CVMX_SMIX_EN(bus->unit), smi_en.u64);
  127. return 0;
  128. }
  129. static struct platform_driver octeon_mdiobus_driver = {
  130. .driver = {
  131. .name = "mdio-octeon",
  132. .owner = THIS_MODULE,
  133. },
  134. .probe = octeon_mdiobus_probe,
  135. .remove = __devexit_p(octeon_mdiobus_remove),
  136. };
  137. void octeon_mdiobus_force_mod_depencency(void)
  138. {
  139. /* Let ethernet drivers force us to be loaded. */
  140. }
  141. EXPORT_SYMBOL(octeon_mdiobus_force_mod_depencency);
  142. static int __init octeon_mdiobus_mod_init(void)
  143. {
  144. return platform_driver_register(&octeon_mdiobus_driver);
  145. }
  146. static void __exit octeon_mdiobus_mod_exit(void)
  147. {
  148. platform_driver_unregister(&octeon_mdiobus_driver);
  149. }
  150. module_init(octeon_mdiobus_mod_init);
  151. module_exit(octeon_mdiobus_mod_exit);
  152. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  153. MODULE_VERSION(DRV_VERSION);
  154. MODULE_AUTHOR("David Daney");
  155. MODULE_LICENSE("GPL");