dwmac-socfpga.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright Altera Corporation (C) 2014. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License, version 2,
  5. * as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Adopted from dwmac-sti.c
  16. */
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/of.h>
  19. #include <linux/of_net.h>
  20. #include <linux/phy.h>
  21. #include <linux/regmap.h>
  22. #include <linux/stmmac.h>
  23. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0x0
  24. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII 0x1
  25. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII 0x2
  26. #define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
  27. #define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK 0x00000003
  28. struct socfpga_dwmac {
  29. int interface;
  30. u32 reg_offset;
  31. u32 reg_shift;
  32. struct device *dev;
  33. struct regmap *sys_mgr_base_addr;
  34. };
  35. static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev)
  36. {
  37. struct device_node *np = dev->of_node;
  38. struct regmap *sys_mgr_base_addr;
  39. u32 reg_offset, reg_shift;
  40. int ret;
  41. dwmac->interface = of_get_phy_mode(np);
  42. sys_mgr_base_addr = syscon_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
  43. if (IS_ERR(sys_mgr_base_addr)) {
  44. dev_info(dev, "No sysmgr-syscon node found\n");
  45. return PTR_ERR(sys_mgr_base_addr);
  46. }
  47. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
  48. if (ret) {
  49. dev_info(dev, "Could not read reg_offset from sysmgr-syscon!\n");
  50. return -EINVAL;
  51. }
  52. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
  53. if (ret) {
  54. dev_info(dev, "Could not read reg_shift from sysmgr-syscon!\n");
  55. return -EINVAL;
  56. }
  57. dwmac->reg_offset = reg_offset;
  58. dwmac->reg_shift = reg_shift;
  59. dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
  60. dwmac->dev = dev;
  61. return 0;
  62. }
  63. static int socfpga_dwmac_setup(struct socfpga_dwmac *dwmac)
  64. {
  65. struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
  66. int phymode = dwmac->interface;
  67. u32 reg_offset = dwmac->reg_offset;
  68. u32 reg_shift = dwmac->reg_shift;
  69. u32 ctrl, val;
  70. switch (phymode) {
  71. case PHY_INTERFACE_MODE_RGMII:
  72. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
  73. break;
  74. case PHY_INTERFACE_MODE_MII:
  75. case PHY_INTERFACE_MODE_GMII:
  76. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  77. break;
  78. default:
  79. dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
  80. return -EINVAL;
  81. }
  82. regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
  83. ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << reg_shift);
  84. ctrl |= val << reg_shift;
  85. regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
  86. return 0;
  87. }
  88. static void *socfpga_dwmac_probe(struct platform_device *pdev)
  89. {
  90. struct device *dev = &pdev->dev;
  91. int ret;
  92. struct socfpga_dwmac *dwmac;
  93. dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
  94. if (!dwmac)
  95. return ERR_PTR(-ENOMEM);
  96. ret = socfpga_dwmac_parse_data(dwmac, dev);
  97. if (ret) {
  98. dev_err(dev, "Unable to parse OF data\n");
  99. return ERR_PTR(ret);
  100. }
  101. ret = socfpga_dwmac_setup(dwmac);
  102. if (ret) {
  103. dev_err(dev, "couldn't setup SoC glue (%d)\n", ret);
  104. return ERR_PTR(ret);
  105. }
  106. return dwmac;
  107. }
  108. const struct stmmac_of_data socfpga_gmac_data = {
  109. .setup = socfpga_dwmac_probe,
  110. };