mdio-thunder.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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;
  81. bus->mii_bus->write = cavium_mdiobus_write;
  82. err = of_mdiobus_register(bus->mii_bus, node);
  83. if (err)
  84. dev_err(&pdev->dev, "of_mdiobus_register failed\n");
  85. dev_info(&pdev->dev, "Added bus at %llx\n", r.start);
  86. if (i >= ARRAY_SIZE(nexus->buses))
  87. break;
  88. }
  89. fwnode_handle_put(fwn);
  90. return 0;
  91. err_release_regions:
  92. pci_release_regions(pdev);
  93. err_disable_device:
  94. pci_set_drvdata(pdev, NULL);
  95. return err;
  96. }
  97. static void thunder_mdiobus_pci_remove(struct pci_dev *pdev)
  98. {
  99. int i;
  100. struct thunder_mdiobus_nexus *nexus = pci_get_drvdata(pdev);
  101. for (i = 0; i < ARRAY_SIZE(nexus->buses); i++) {
  102. struct cavium_mdiobus *bus = nexus->buses[i];
  103. if (!bus)
  104. continue;
  105. mdiobus_unregister(bus->mii_bus);
  106. oct_mdio_writeq(0, bus->register_base + SMI_EN);
  107. }
  108. pci_release_regions(pdev);
  109. pci_set_drvdata(pdev, NULL);
  110. }
  111. static const struct pci_device_id thunder_mdiobus_id_table[] = {
  112. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xa02b) },
  113. { 0, } /* End of table. */
  114. };
  115. MODULE_DEVICE_TABLE(pci, thunder_mdiobus_id_table);
  116. static struct pci_driver thunder_mdiobus_driver = {
  117. .name = KBUILD_MODNAME,
  118. .id_table = thunder_mdiobus_id_table,
  119. .probe = thunder_mdiobus_pci_probe,
  120. .remove = thunder_mdiobus_pci_remove,
  121. };
  122. module_pci_driver(thunder_mdiobus_driver);
  123. MODULE_DESCRIPTION("Cavium ThunderX MDIO bus driver");
  124. MODULE_LICENSE("GPL v2");