dwmac-anarion.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Adaptrum Anarion DWMAC glue layer
  3. *
  4. * Copyright (C) 2017, Adaptrum, Inc.
  5. * (Written by Alexandru Gagniuc <alex.g at adaptrum.com> for Adaptrum, Inc.)
  6. * Licensed under the GPLv2 or (at your option) any later version.
  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. int phy_mode;
  51. struct resource *res;
  52. void __iomem *ctl_block;
  53. struct anarion_gmac *gmac;
  54. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  55. ctl_block = devm_ioremap_resource(&pdev->dev, res);
  56. if (IS_ERR(ctl_block)) {
  57. dev_err(&pdev->dev, "Cannot get reset region (%ld)!\n",
  58. PTR_ERR(ctl_block));
  59. return ctl_block;
  60. }
  61. gmac = devm_kzalloc(&pdev->dev, sizeof(*gmac), GFP_KERNEL);
  62. if (!gmac)
  63. return ERR_PTR(-ENOMEM);
  64. gmac->ctl_block = (uintptr_t)ctl_block;
  65. phy_mode = of_get_phy_mode(pdev->dev.of_node);
  66. switch (phy_mode) {
  67. case PHY_INTERFACE_MODE_RGMII: /* Fall through */
  68. case PHY_INTERFACE_MODE_RGMII_ID /* Fall through */:
  69. case PHY_INTERFACE_MODE_RGMII_RXID: /* Fall through */
  70. case PHY_INTERFACE_MODE_RGMII_TXID:
  71. gmac->phy_intf_sel = GMAC_CONFIG_INTF_RGMII;
  72. break;
  73. default:
  74. dev_err(&pdev->dev, "Unsupported phy-mode (%d)\n",
  75. phy_mode);
  76. return ERR_PTR(-ENOTSUPP);
  77. }
  78. return gmac;
  79. }
  80. static int anarion_dwmac_probe(struct platform_device *pdev)
  81. {
  82. int ret;
  83. struct anarion_gmac *gmac;
  84. struct plat_stmmacenet_data *plat_dat;
  85. struct stmmac_resources stmmac_res;
  86. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  87. if (ret)
  88. return ret;
  89. gmac = anarion_config_dt(pdev);
  90. if (IS_ERR(gmac))
  91. return PTR_ERR(gmac);
  92. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  93. if (IS_ERR(plat_dat))
  94. return PTR_ERR(plat_dat);
  95. plat_dat->init = anarion_gmac_init;
  96. plat_dat->exit = anarion_gmac_exit;
  97. anarion_gmac_init(pdev, gmac);
  98. plat_dat->bsp_priv = gmac;
  99. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  100. if (ret) {
  101. stmmac_remove_config_dt(pdev, plat_dat);
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. static const struct of_device_id anarion_dwmac_match[] = {
  107. { .compatible = "adaptrum,anarion-gmac" },
  108. { }
  109. };
  110. MODULE_DEVICE_TABLE(of, anarion_dwmac_match);
  111. static struct platform_driver anarion_dwmac_driver = {
  112. .probe = anarion_dwmac_probe,
  113. .remove = stmmac_pltfr_remove,
  114. .driver = {
  115. .name = "anarion-dwmac",
  116. .pm = &stmmac_pltfr_pm_ops,
  117. .of_match_table = anarion_dwmac_match,
  118. },
  119. };
  120. module_platform_driver(anarion_dwmac_driver);
  121. MODULE_DESCRIPTION("Adaptrum Anarion DWMAC specific glue layer");
  122. MODULE_AUTHOR("Alexandru Gagniuc <mr.nuke.me@gmail.com>");
  123. MODULE_LICENSE("GPL v2");