dwmac-anarion.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Adaptrum Anarion DWMAC glue layer
  4. *
  5. * Copyright (C) 2017, Adaptrum, Inc.
  6. * (Written by Alexandru Gagniuc <alex.g at adaptrum.com> for Adaptrum, Inc.)
  7. */
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/of_net.h>
  11. #include <linux/stmmac.h>
  12. #include "stmmac.h"
  13. #include "stmmac_platform.h"
  14. #define GMAC_RESET_CONTROL_REG 0
  15. #define GMAC_SW_CONFIG_REG 4
  16. #define GMAC_CONFIG_INTF_SEL_MASK (0x7 << 0)
  17. #define GMAC_CONFIG_INTF_RGMII (0x1 << 0)
  18. struct anarion_gmac {
  19. uintptr_t ctl_block;
  20. uint32_t phy_intf_sel;
  21. };
  22. static uint32_t gmac_read_reg(struct anarion_gmac *gmac, uint8_t reg)
  23. {
  24. return readl((void *)(gmac->ctl_block + reg));
  25. };
  26. static void gmac_write_reg(struct anarion_gmac *gmac, uint8_t reg, uint32_t val)
  27. {
  28. writel(val, (void *)(gmac->ctl_block + reg));
  29. }
  30. static int anarion_gmac_init(struct platform_device *pdev, void *priv)
  31. {
  32. uint32_t sw_config;
  33. struct anarion_gmac *gmac = priv;
  34. /* Reset logic, configure interface mode, then release reset. SIMPLE! */
  35. gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 1);
  36. sw_config = gmac_read_reg(gmac, GMAC_SW_CONFIG_REG);
  37. sw_config &= ~GMAC_CONFIG_INTF_SEL_MASK;
  38. sw_config |= (gmac->phy_intf_sel & GMAC_CONFIG_INTF_SEL_MASK);
  39. gmac_write_reg(gmac, GMAC_SW_CONFIG_REG, sw_config);
  40. gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 0);
  41. return 0;
  42. }
  43. static void anarion_gmac_exit(struct platform_device *pdev, void *priv)
  44. {
  45. struct anarion_gmac *gmac = priv;
  46. gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 1);
  47. }
  48. static struct anarion_gmac *anarion_config_dt(struct platform_device *pdev)
  49. {
  50. struct anarion_gmac *gmac;
  51. phy_interface_t phy_mode;
  52. void __iomem *ctl_block;
  53. int err;
  54. ctl_block = devm_platform_ioremap_resource(pdev, 1);
  55. if (IS_ERR(ctl_block)) {
  56. dev_err(&pdev->dev, "Cannot get reset region (%ld)!\n",
  57. PTR_ERR(ctl_block));
  58. return ctl_block;
  59. }
  60. gmac = devm_kzalloc(&pdev->dev, sizeof(*gmac), GFP_KERNEL);
  61. if (!gmac)
  62. return ERR_PTR(-ENOMEM);
  63. gmac->ctl_block = (uintptr_t)ctl_block;
  64. err = of_get_phy_mode(pdev->dev.of_node, &phy_mode);
  65. if (err)
  66. return ERR_PTR(err);
  67. switch (phy_mode) {
  68. case PHY_INTERFACE_MODE_RGMII:
  69. fallthrough;
  70. case PHY_INTERFACE_MODE_RGMII_ID:
  71. case PHY_INTERFACE_MODE_RGMII_RXID:
  72. case PHY_INTERFACE_MODE_RGMII_TXID:
  73. gmac->phy_intf_sel = GMAC_CONFIG_INTF_RGMII;
  74. break;
  75. default:
  76. dev_err(&pdev->dev, "Unsupported phy-mode (%d)\n",
  77. phy_mode);
  78. return ERR_PTR(-ENOTSUPP);
  79. }
  80. return gmac;
  81. }
  82. static int anarion_dwmac_probe(struct platform_device *pdev)
  83. {
  84. int ret;
  85. struct anarion_gmac *gmac;
  86. struct plat_stmmacenet_data *plat_dat;
  87. struct stmmac_resources stmmac_res;
  88. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  89. if (ret)
  90. return ret;
  91. gmac = anarion_config_dt(pdev);
  92. if (IS_ERR(gmac))
  93. return PTR_ERR(gmac);
  94. plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
  95. if (IS_ERR(plat_dat))
  96. return PTR_ERR(plat_dat);
  97. plat_dat->init = anarion_gmac_init;
  98. plat_dat->exit = anarion_gmac_exit;
  99. anarion_gmac_init(pdev, gmac);
  100. plat_dat->bsp_priv = gmac;
  101. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  102. if (ret) {
  103. stmmac_remove_config_dt(pdev, plat_dat);
  104. return ret;
  105. }
  106. return 0;
  107. }
  108. static const struct of_device_id anarion_dwmac_match[] = {
  109. { .compatible = "adaptrum,anarion-gmac" },
  110. { }
  111. };
  112. MODULE_DEVICE_TABLE(of, anarion_dwmac_match);
  113. static struct platform_driver anarion_dwmac_driver = {
  114. .probe = anarion_dwmac_probe,
  115. .remove = stmmac_pltfr_remove,
  116. .driver = {
  117. .name = "anarion-dwmac",
  118. .pm = &stmmac_pltfr_pm_ops,
  119. .of_match_table = anarion_dwmac_match,
  120. },
  121. };
  122. module_platform_driver(anarion_dwmac_driver);
  123. MODULE_DESCRIPTION("Adaptrum Anarion DWMAC specific glue layer");
  124. MODULE_AUTHOR("Alexandru Gagniuc <[email protected]>");
  125. MODULE_LICENSE("GPL v2");