mdio-thunder.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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-2016 Cavium, Inc.
  7. */
  8. #include <linux/of_address.h>
  9. #include <linux/of_mdio.h>
  10. #include <linux/module.h>
  11. #include <linux/gfp.h>
  12. #include <linux/phy.h>
  13. #include <linux/io.h>
  14. #include <linux/acpi.h>
  15. #include <linux/pci.h>
  16. #include "mdio-cavium.h"
  17. struct thunder_mdiobus_nexus {
  18. void __iomem *bar0;
  19. struct cavium_mdiobus *buses[4];
  20. };
  21. static int thunder_mdiobus_pci_probe(struct pci_dev *pdev,
  22. const struct pci_device_id *ent)
  23. {
  24. struct device_node *node;
  25. struct fwnode_handle *fwn;
  26. struct thunder_mdiobus_nexus *nexus;
  27. int err;
  28. int i;
  29. nexus = devm_kzalloc(&pdev->dev, sizeof(*nexus), GFP_KERNEL);
  30. if (!nexus)
  31. return -ENOMEM;
  32. pci_set_drvdata(pdev, nexus);
  33. err = pcim_enable_device(pdev);
  34. if (err) {
  35. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  36. pci_set_drvdata(pdev, NULL);
  37. return err;
  38. }
  39. err = pci_request_regions(pdev, KBUILD_MODNAME);
  40. if (err) {
  41. dev_err(&pdev->dev, "pci_request_regions failed\n");
  42. goto err_disable_device;
  43. }
  44. nexus->bar0 = pcim_iomap(pdev, 0, pci_resource_len(pdev, 0));
  45. if (!nexus->bar0) {
  46. err = -ENOMEM;
  47. goto err_release_regions;
  48. }
  49. i = 0;
  50. device_for_each_child_node(&pdev->dev, fwn) {
  51. struct resource r;
  52. struct mii_bus *mii_bus;
  53. struct cavium_mdiobus *bus;
  54. union cvmx_smix_en smi_en;
  55. /* If it is not an OF node we cannot handle it yet, so
  56. * exit the loop.
  57. */
  58. node = to_of_node(fwn);
  59. if (!node)
  60. break;
  61. err = of_address_to_resource(node, 0, &r);
  62. if (err) {
  63. dev_err(&pdev->dev,
  64. "Couldn't translate address for \"%s\"\n",
  65. node->name);
  66. break;
  67. }
  68. mii_bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(*bus));
  69. if (!mii_bus)
  70. break;
  71. bus = mii_bus->priv;
  72. bus->mii_bus = mii_bus;
  73. nexus->buses[i] = bus;
  74. i++;
  75. bus->register_base = (u64)nexus->bar0 +
  76. r.start - pci_resource_start(pdev, 0);
  77. smi_en.u64 = 0;
  78. smi_en.s.en = 1;
  79. oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN);
  80. bus->mii_bus->name = KBUILD_MODNAME;
  81. snprintf(bus->mii_bus->id, MII_BUS_ID_SIZE, "%llx", r.start);
  82. bus->mii_bus->parent = &pdev->dev;
  83. bus->mii_bus->read = cavium_mdiobus_read;
  84. bus->mii_bus->write = cavium_mdiobus_write;
  85. err = of_mdiobus_register(bus->mii_bus, node);
  86. if (err)
  87. dev_err(&pdev->dev, "of_mdiobus_register failed\n");
  88. dev_info(&pdev->dev, "Added bus at %llx\n", r.start);
  89. if (i >= ARRAY_SIZE(nexus->buses))
  90. break;
  91. }
  92. return 0;
  93. err_release_regions:
  94. pci_release_regions(pdev);
  95. err_disable_device:
  96. pci_set_drvdata(pdev, NULL);
  97. return err;
  98. }
  99. static void thunder_mdiobus_pci_remove(struct pci_dev *pdev)
  100. {
  101. int i;
  102. struct thunder_mdiobus_nexus *nexus = pci_get_drvdata(pdev);
  103. for (i = 0; i < ARRAY_SIZE(nexus->buses); i++) {
  104. struct cavium_mdiobus *bus = nexus->buses[i];
  105. if (!bus)
  106. continue;
  107. mdiobus_unregister(bus->mii_bus);
  108. mdiobus_free(bus->mii_bus);
  109. oct_mdio_writeq(0, bus->register_base + SMI_EN);
  110. }
  111. pci_set_drvdata(pdev, NULL);
  112. }
  113. static const struct pci_device_id thunder_mdiobus_id_table[] = {
  114. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xa02b) },
  115. { 0, } /* End of table. */
  116. };
  117. MODULE_DEVICE_TABLE(pci, thunder_mdiobus_id_table);
  118. static struct pci_driver thunder_mdiobus_driver = {
  119. .name = KBUILD_MODNAME,
  120. .id_table = thunder_mdiobus_id_table,
  121. .probe = thunder_mdiobus_pci_probe,
  122. .remove = thunder_mdiobus_pci_remove,
  123. };
  124. module_pci_driver(thunder_mdiobus_driver);
  125. MODULE_DESCRIPTION("Cavium ThunderX MDIO bus driver");
  126. MODULE_LICENSE("GPL");