mdio-thunder.c 3.4 KB

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