hwif.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * Copyright (c) 2018 Synopsys, Inc. and/or its affiliates.
  4. * stmmac HW Interface Handling
  5. */
  6. #include "common.h"
  7. #include "stmmac.h"
  8. #include "stmmac_ptp.h"
  9. static u32 stmmac_get_id(struct stmmac_priv *priv, u32 id_reg)
  10. {
  11. u32 reg = readl(priv->ioaddr + id_reg);
  12. if (!reg) {
  13. dev_info(priv->device, "Version ID not available\n");
  14. return 0x0;
  15. }
  16. dev_info(priv->device, "User ID: 0x%x, Synopsys ID: 0x%x\n",
  17. (unsigned int)(reg & GENMASK(15, 8)) >> 8,
  18. (unsigned int)(reg & GENMASK(7, 0)));
  19. return reg & GENMASK(7, 0);
  20. }
  21. static void stmmac_dwmac_mode_quirk(struct stmmac_priv *priv)
  22. {
  23. struct mac_device_info *mac = priv->hw;
  24. if (priv->chain_mode) {
  25. dev_info(priv->device, "Chain mode enabled\n");
  26. priv->mode = STMMAC_CHAIN_MODE;
  27. mac->mode = &chain_mode_ops;
  28. } else {
  29. dev_info(priv->device, "Ring mode enabled\n");
  30. priv->mode = STMMAC_RING_MODE;
  31. mac->mode = &ring_mode_ops;
  32. }
  33. }
  34. static int stmmac_dwmac1_quirks(struct stmmac_priv *priv)
  35. {
  36. struct mac_device_info *mac = priv->hw;
  37. if (priv->plat->enh_desc) {
  38. dev_info(priv->device, "Enhanced/Alternate descriptors\n");
  39. /* GMAC older than 3.50 has no extended descriptors */
  40. if (priv->synopsys_id >= DWMAC_CORE_3_50) {
  41. dev_info(priv->device, "Enabled extended descriptors\n");
  42. priv->extend_desc = 1;
  43. } else {
  44. dev_warn(priv->device, "Extended descriptors not supported\n");
  45. }
  46. mac->desc = &enh_desc_ops;
  47. } else {
  48. dev_info(priv->device, "Normal descriptors\n");
  49. mac->desc = &ndesc_ops;
  50. }
  51. stmmac_dwmac_mode_quirk(priv);
  52. return 0;
  53. }
  54. static int stmmac_dwmac4_quirks(struct stmmac_priv *priv)
  55. {
  56. stmmac_dwmac_mode_quirk(priv);
  57. return 0;
  58. }
  59. static const struct stmmac_hwif_entry {
  60. bool gmac;
  61. bool gmac4;
  62. bool xgmac;
  63. u32 min_id;
  64. const struct stmmac_regs_off regs;
  65. const void *desc;
  66. const void *dma;
  67. const void *mac;
  68. const void *hwtimestamp;
  69. const void *mode;
  70. const void *tc;
  71. int (*setup)(struct stmmac_priv *priv);
  72. int (*quirks)(struct stmmac_priv *priv);
  73. } stmmac_hw[] = {
  74. /* NOTE: New HW versions shall go to the end of this table */
  75. {
  76. .gmac = false,
  77. .gmac4 = false,
  78. .xgmac = false,
  79. .min_id = 0,
  80. .regs = {
  81. .ptp_off = PTP_GMAC3_X_OFFSET,
  82. .mmc_off = MMC_GMAC3_X_OFFSET,
  83. },
  84. .desc = NULL,
  85. .dma = &dwmac100_dma_ops,
  86. .mac = &dwmac100_ops,
  87. .hwtimestamp = &stmmac_ptp,
  88. .mode = NULL,
  89. .tc = NULL,
  90. .setup = dwmac100_setup,
  91. .quirks = stmmac_dwmac1_quirks,
  92. }, {
  93. .gmac = true,
  94. .gmac4 = false,
  95. .xgmac = false,
  96. .min_id = 0,
  97. .regs = {
  98. .ptp_off = PTP_GMAC3_X_OFFSET,
  99. .mmc_off = MMC_GMAC3_X_OFFSET,
  100. },
  101. .desc = NULL,
  102. .dma = &dwmac1000_dma_ops,
  103. .mac = &dwmac1000_ops,
  104. .hwtimestamp = &stmmac_ptp,
  105. .mode = NULL,
  106. .tc = NULL,
  107. .setup = dwmac1000_setup,
  108. .quirks = stmmac_dwmac1_quirks,
  109. }, {
  110. .gmac = false,
  111. .gmac4 = true,
  112. .xgmac = false,
  113. .min_id = 0,
  114. .regs = {
  115. .ptp_off = PTP_GMAC4_OFFSET,
  116. .mmc_off = MMC_GMAC4_OFFSET,
  117. },
  118. .desc = &dwmac4_desc_ops,
  119. .dma = &dwmac4_dma_ops,
  120. .mac = &dwmac4_ops,
  121. .hwtimestamp = &stmmac_ptp,
  122. .mode = NULL,
  123. .tc = NULL,
  124. .setup = dwmac4_setup,
  125. .quirks = stmmac_dwmac4_quirks,
  126. }, {
  127. .gmac = false,
  128. .gmac4 = true,
  129. .xgmac = false,
  130. .min_id = DWMAC_CORE_4_00,
  131. .regs = {
  132. .ptp_off = PTP_GMAC4_OFFSET,
  133. .mmc_off = MMC_GMAC4_OFFSET,
  134. },
  135. .desc = &dwmac4_desc_ops,
  136. .dma = &dwmac4_dma_ops,
  137. .mac = &dwmac410_ops,
  138. .hwtimestamp = &stmmac_ptp,
  139. .mode = &dwmac4_ring_mode_ops,
  140. .tc = NULL,
  141. .setup = dwmac4_setup,
  142. .quirks = NULL,
  143. }, {
  144. .gmac = false,
  145. .gmac4 = true,
  146. .xgmac = false,
  147. .min_id = DWMAC_CORE_4_10,
  148. .regs = {
  149. .ptp_off = PTP_GMAC4_OFFSET,
  150. .mmc_off = MMC_GMAC4_OFFSET,
  151. },
  152. .desc = &dwmac4_desc_ops,
  153. .dma = &dwmac410_dma_ops,
  154. .mac = &dwmac410_ops,
  155. .hwtimestamp = &stmmac_ptp,
  156. .mode = &dwmac4_ring_mode_ops,
  157. .tc = NULL,
  158. .setup = dwmac4_setup,
  159. .quirks = NULL,
  160. }, {
  161. .gmac = false,
  162. .gmac4 = true,
  163. .xgmac = false,
  164. .min_id = DWMAC_CORE_5_10,
  165. .regs = {
  166. .ptp_off = PTP_GMAC4_OFFSET,
  167. .mmc_off = MMC_GMAC4_OFFSET,
  168. },
  169. .desc = &dwmac4_desc_ops,
  170. .dma = &dwmac410_dma_ops,
  171. .mac = &dwmac510_ops,
  172. .hwtimestamp = &stmmac_ptp,
  173. .mode = &dwmac4_ring_mode_ops,
  174. .tc = &dwmac510_tc_ops,
  175. .setup = dwmac4_setup,
  176. .quirks = NULL,
  177. }, {
  178. .gmac = false,
  179. .gmac4 = false,
  180. .xgmac = true,
  181. .min_id = DWXGMAC_CORE_2_10,
  182. .regs = {
  183. .ptp_off = PTP_XGMAC_OFFSET,
  184. .mmc_off = 0,
  185. },
  186. .desc = &dwxgmac210_desc_ops,
  187. .dma = &dwxgmac210_dma_ops,
  188. .mac = &dwxgmac210_ops,
  189. .hwtimestamp = &stmmac_ptp,
  190. .mode = NULL,
  191. .tc = NULL,
  192. .setup = dwxgmac2_setup,
  193. .quirks = NULL,
  194. },
  195. };
  196. int stmmac_hwif_init(struct stmmac_priv *priv)
  197. {
  198. bool needs_xgmac = priv->plat->has_xgmac;
  199. bool needs_gmac4 = priv->plat->has_gmac4;
  200. bool needs_gmac = priv->plat->has_gmac;
  201. const struct stmmac_hwif_entry *entry;
  202. struct mac_device_info *mac;
  203. bool needs_setup = true;
  204. int i, ret;
  205. u32 id;
  206. if (needs_gmac) {
  207. id = stmmac_get_id(priv, GMAC_VERSION);
  208. } else if (needs_gmac4 || needs_xgmac) {
  209. id = stmmac_get_id(priv, GMAC4_VERSION);
  210. } else {
  211. id = 0;
  212. }
  213. /* Save ID for later use */
  214. priv->synopsys_id = id;
  215. /* Lets assume some safe values first */
  216. priv->ptpaddr = priv->ioaddr +
  217. (needs_gmac4 ? PTP_GMAC4_OFFSET : PTP_GMAC3_X_OFFSET);
  218. priv->mmcaddr = priv->ioaddr +
  219. (needs_gmac4 ? MMC_GMAC4_OFFSET : MMC_GMAC3_X_OFFSET);
  220. /* Check for HW specific setup first */
  221. if (priv->plat->setup) {
  222. mac = priv->plat->setup(priv);
  223. needs_setup = false;
  224. } else {
  225. mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL);
  226. }
  227. if (!mac)
  228. return -ENOMEM;
  229. /* Fallback to generic HW */
  230. for (i = ARRAY_SIZE(stmmac_hw) - 1; i >= 0; i--) {
  231. entry = &stmmac_hw[i];
  232. if (needs_gmac ^ entry->gmac)
  233. continue;
  234. if (needs_gmac4 ^ entry->gmac4)
  235. continue;
  236. if (needs_xgmac ^ entry->xgmac)
  237. continue;
  238. /* Use synopsys_id var because some setups can override this */
  239. if (priv->synopsys_id < entry->min_id)
  240. continue;
  241. /* Only use generic HW helpers if needed */
  242. mac->desc = mac->desc ? : entry->desc;
  243. mac->dma = mac->dma ? : entry->dma;
  244. mac->mac = mac->mac ? : entry->mac;
  245. mac->ptp = mac->ptp ? : entry->hwtimestamp;
  246. mac->mode = mac->mode ? : entry->mode;
  247. mac->tc = mac->tc ? : entry->tc;
  248. priv->hw = mac;
  249. priv->ptpaddr = priv->ioaddr + entry->regs.ptp_off;
  250. priv->mmcaddr = priv->ioaddr + entry->regs.mmc_off;
  251. /* Entry found */
  252. if (needs_setup) {
  253. ret = entry->setup(priv);
  254. if (ret)
  255. return ret;
  256. }
  257. /* Save quirks, if needed for posterior use */
  258. priv->hwif_quirks = entry->quirks;
  259. return 0;
  260. }
  261. dev_err(priv->device, "Failed to find HW IF (id=0x%x, gmac=%d/%d)\n",
  262. id, needs_gmac, needs_gmac4);
  263. return -EINVAL;
  264. }