mdio-octeon.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009-2015 Cavium, Inc.
  4. */
  5. #include <linux/platform_device.h>
  6. #include <linux/of_address.h>
  7. #include <linux/of_mdio.h>
  8. #include <linux/module.h>
  9. #include <linux/gfp.h>
  10. #include <linux/phy.h>
  11. #include <linux/io.h>
  12. #include "mdio-cavium.h"
  13. static int octeon_mdiobus_probe(struct platform_device *pdev)
  14. {
  15. struct cavium_mdiobus *bus;
  16. struct mii_bus *mii_bus;
  17. struct resource *res_mem;
  18. resource_size_t mdio_phys;
  19. resource_size_t regsize;
  20. union cvmx_smix_en smi_en;
  21. int err = -ENOENT;
  22. mii_bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(*bus));
  23. if (!mii_bus)
  24. return -ENOMEM;
  25. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  26. if (res_mem == NULL) {
  27. dev_err(&pdev->dev, "found no memory resource\n");
  28. return -ENXIO;
  29. }
  30. bus = mii_bus->priv;
  31. bus->mii_bus = mii_bus;
  32. mdio_phys = res_mem->start;
  33. regsize = resource_size(res_mem);
  34. if (!devm_request_mem_region(&pdev->dev, mdio_phys, regsize,
  35. res_mem->name)) {
  36. dev_err(&pdev->dev, "request_mem_region failed\n");
  37. return -ENXIO;
  38. }
  39. bus->register_base =
  40. (u64)devm_ioremap(&pdev->dev, mdio_phys, regsize);
  41. if (!bus->register_base) {
  42. dev_err(&pdev->dev, "dev_ioremap failed\n");
  43. return -ENOMEM;
  44. }
  45. smi_en.u64 = 0;
  46. smi_en.s.en = 1;
  47. oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN);
  48. bus->mii_bus->name = KBUILD_MODNAME;
  49. snprintf(bus->mii_bus->id, MII_BUS_ID_SIZE, "%llx", bus->register_base);
  50. bus->mii_bus->parent = &pdev->dev;
  51. bus->mii_bus->read = cavium_mdiobus_read;
  52. bus->mii_bus->write = cavium_mdiobus_write;
  53. platform_set_drvdata(pdev, bus);
  54. err = of_mdiobus_register(bus->mii_bus, pdev->dev.of_node);
  55. if (err)
  56. goto fail_register;
  57. dev_info(&pdev->dev, "Probed\n");
  58. return 0;
  59. fail_register:
  60. smi_en.u64 = 0;
  61. oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN);
  62. return err;
  63. }
  64. static int octeon_mdiobus_remove(struct platform_device *pdev)
  65. {
  66. struct cavium_mdiobus *bus;
  67. union cvmx_smix_en smi_en;
  68. bus = platform_get_drvdata(pdev);
  69. mdiobus_unregister(bus->mii_bus);
  70. smi_en.u64 = 0;
  71. oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN);
  72. return 0;
  73. }
  74. static const struct of_device_id octeon_mdiobus_match[] = {
  75. {
  76. .compatible = "cavium,octeon-3860-mdio",
  77. },
  78. {},
  79. };
  80. MODULE_DEVICE_TABLE(of, octeon_mdiobus_match);
  81. static struct platform_driver octeon_mdiobus_driver = {
  82. .driver = {
  83. .name = KBUILD_MODNAME,
  84. .of_match_table = octeon_mdiobus_match,
  85. },
  86. .probe = octeon_mdiobus_probe,
  87. .remove = octeon_mdiobus_remove,
  88. };
  89. void octeon_mdiobus_force_mod_depencency(void)
  90. {
  91. /* Let ethernet drivers force us to be loaded. */
  92. }
  93. EXPORT_SYMBOL(octeon_mdiobus_force_mod_depencency);
  94. module_platform_driver(octeon_mdiobus_driver);
  95. MODULE_DESCRIPTION("Cavium OCTEON MDIO bus driver");
  96. MODULE_AUTHOR("David Daney");
  97. MODULE_LICENSE("GPL v2");