mdio-thunder.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009-2016 Cavium, Inc.
  4. */
  5. #include <linux/acpi.h>
  6. #include <linux/gfp.h>
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_mdio.h>
  11. #include <linux/pci.h>
  12. #include <linux/phy.h>
  13. #include "mdio-cavium.h"
  14. struct thunder_mdiobus_nexus {
  15. void __iomem *bar0;
  16. struct cavium_mdiobus *buses[4];
  17. };
  18. static int thunder_mdiobus_pci_probe(struct pci_dev *pdev,
  19. const struct pci_device_id *ent)
  20. {
  21. struct device_node *node;
  22. struct fwnode_handle *fwn;
  23. struct thunder_mdiobus_nexus *nexus;
  24. int err;
  25. int i;
  26. nexus = devm_kzalloc(&pdev->dev, sizeof(*nexus), GFP_KERNEL);
  27. if (!nexus)
  28. return -ENOMEM;
  29. pci_set_drvdata(pdev, nexus);
  30. err = pcim_enable_device(pdev);
  31. if (err) {
  32. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  33. pci_set_drvdata(pdev, NULL);
  34. return err;
  35. }
  36. err = pci_request_regions(pdev, KBUILD_MODNAME);
  37. if (err) {
  38. dev_err(&pdev->dev, "pci_request_regions failed\n");
  39. goto err_disable_device;
  40. }
  41. nexus->bar0 = pcim_iomap(pdev, 0, pci_resource_len(pdev, 0));
  42. if (!nexus->bar0) {
  43. err = -ENOMEM;
  44. goto err_release_regions;
  45. }
  46. i = 0;
  47. device_for_each_child_node(&pdev->dev, fwn) {
  48. struct resource r;
  49. struct mii_bus *mii_bus;
  50. struct cavium_mdiobus *bus;
  51. union cvmx_smix_en smi_en;
  52. /* If it is not an OF node we cannot handle it yet, so
  53. * exit the loop.
  54. */
  55. node = to_of_node(fwn);
  56. if (!node)
  57. break;
  58. err = of_address_to_resource(node, 0, &r);
  59. if (err) {
  60. dev_err(&pdev->dev,
  61. "Couldn't translate address for \"%pOFn\"\n",
  62. node);
  63. break;
  64. }
  65. mii_bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(*bus));
  66. if (!mii_bus)
  67. break;
  68. bus = mii_bus->priv;
  69. bus->mii_bus = mii_bus;
  70. nexus->buses[i] = bus;
  71. i++;
  72. bus->register_base = nexus->bar0 +
  73. r.start - pci_resource_start(pdev, 0);
  74. smi_en.u64 = 0;
  75. smi_en.s.en = 1;
  76. oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN);
  77. bus->mii_bus->name = KBUILD_MODNAME;
  78. snprintf(bus->mii_bus->id, MII_BUS_ID_SIZE, "%llx", r.start);
  79. bus->mii_bus->parent = &pdev->dev;
  80. bus->mii_bus->read = cavium_mdiobus_read_c22;
  81. bus->mii_bus->write = cavium_mdiobus_write_c22;
  82. bus->mii_bus->read_c45 = cavium_mdiobus_read_c45;
  83. bus->mii_bus->write_c45 = cavium_mdiobus_write_c45;
  84. err = of_mdiobus_register(bus->mii_bus, node);
  85. if (err)
  86. dev_err(&pdev->dev, "of_mdiobus_register failed\n");
  87. dev_info(&pdev->dev, "Added bus at %llx\n", r.start);
  88. if (i >= ARRAY_SIZE(nexus->buses))
  89. break;
  90. }
  91. fwnode_handle_put(fwn);
  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. oct_mdio_writeq(0, bus->register_base + SMI_EN);
  109. }
  110. pci_release_regions(pdev);
  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 v2");