hwif.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include "stmmac_est.h"
  10. static u32 stmmac_get_id(struct stmmac_priv *priv, u32 id_reg)
  11. {
  12. u32 reg = readl(priv->ioaddr + id_reg);
  13. if (!reg) {
  14. dev_info(priv->device, "Version ID not available\n");
  15. return 0x0;
  16. }
  17. dev_info(priv->device, "User ID: 0x%x, Synopsys ID: 0x%x\n",
  18. (unsigned int)(reg & GENMASK(15, 8)) >> 8,
  19. (unsigned int)(reg & GENMASK(7, 0)));
  20. return reg & GENMASK(7, 0);
  21. }
  22. static u32 stmmac_get_dev_id(struct stmmac_priv *priv, u32 id_reg)
  23. {
  24. u32 reg = readl(priv->ioaddr + id_reg);
  25. if (!reg) {
  26. dev_info(priv->device, "Version ID not available\n");
  27. return 0x0;
  28. }
  29. return (reg & GENMASK(15, 8)) >> 8;
  30. }
  31. static void stmmac_dwmac_mode_quirk(struct stmmac_priv *priv)
  32. {
  33. struct mac_device_info *mac = priv->hw;
  34. if (priv->chain_mode) {
  35. dev_info(priv->device, "Chain mode enabled\n");
  36. priv->mode = STMMAC_CHAIN_MODE;
  37. mac->mode = &chain_mode_ops;
  38. } else {
  39. dev_info(priv->device, "Ring mode enabled\n");
  40. priv->mode = STMMAC_RING_MODE;
  41. mac->mode = &ring_mode_ops;
  42. }
  43. }
  44. static int stmmac_dwmac1_quirks(struct stmmac_priv *priv)
  45. {
  46. struct mac_device_info *mac = priv->hw;
  47. if (priv->plat->enh_desc) {
  48. dev_info(priv->device, "Enhanced/Alternate descriptors\n");
  49. /* GMAC older than 3.50 has no extended descriptors */
  50. if (priv->synopsys_id >= DWMAC_CORE_3_50) {
  51. dev_info(priv->device, "Enabled extended descriptors\n");
  52. priv->extend_desc = 1;
  53. } else {
  54. dev_warn(priv->device, "Extended descriptors not supported\n");
  55. }
  56. mac->desc = &enh_desc_ops;
  57. } else {
  58. dev_info(priv->device, "Normal descriptors\n");
  59. mac->desc = &ndesc_ops;
  60. }
  61. stmmac_dwmac_mode_quirk(priv);
  62. return 0;
  63. }
  64. static int stmmac_dwmac4_quirks(struct stmmac_priv *priv)
  65. {
  66. stmmac_dwmac_mode_quirk(priv);
  67. return 0;
  68. }
  69. static int stmmac_dwxlgmac_quirks(struct stmmac_priv *priv)
  70. {
  71. priv->hw->xlgmac = true;
  72. return 0;
  73. }
  74. int stmmac_reset(struct stmmac_priv *priv, void __iomem *ioaddr)
  75. {
  76. struct plat_stmmacenet_data *plat = priv ? priv->plat : NULL;
  77. if (!priv)
  78. return -EINVAL;
  79. if (plat && plat->fix_soc_reset)
  80. return plat->fix_soc_reset(plat, ioaddr);
  81. return stmmac_do_callback(priv, dma, reset, ioaddr);
  82. }
  83. static const struct stmmac_hwif_entry {
  84. bool gmac;
  85. bool gmac4;
  86. bool xgmac;
  87. u32 min_id;
  88. u32 dev_id;
  89. const struct stmmac_regs_off regs;
  90. const void *desc;
  91. const void *dma;
  92. const void *mac;
  93. const void *hwtimestamp;
  94. const void *mode;
  95. const void *tc;
  96. const void *mmc;
  97. const void *est;
  98. int (*setup)(struct stmmac_priv *priv);
  99. int (*quirks)(struct stmmac_priv *priv);
  100. } stmmac_hw[] = {
  101. /* NOTE: New HW versions shall go to the end of this table */
  102. {
  103. .gmac = false,
  104. .gmac4 = false,
  105. .xgmac = false,
  106. .min_id = 0,
  107. .regs = {
  108. .ptp_off = PTP_GMAC3_X_OFFSET,
  109. .mmc_off = MMC_GMAC3_X_OFFSET,
  110. },
  111. .desc = NULL,
  112. .dma = &dwmac100_dma_ops,
  113. .mac = &dwmac100_ops,
  114. .hwtimestamp = &stmmac_ptp,
  115. .mode = NULL,
  116. .tc = NULL,
  117. .mmc = &dwmac_mmc_ops,
  118. .setup = dwmac100_setup,
  119. .quirks = stmmac_dwmac1_quirks,
  120. }, {
  121. .gmac = true,
  122. .gmac4 = false,
  123. .xgmac = false,
  124. .min_id = 0,
  125. .regs = {
  126. .ptp_off = PTP_GMAC3_X_OFFSET,
  127. .mmc_off = MMC_GMAC3_X_OFFSET,
  128. },
  129. .desc = NULL,
  130. .dma = &dwmac1000_dma_ops,
  131. .mac = &dwmac1000_ops,
  132. .hwtimestamp = &stmmac_ptp,
  133. .mode = NULL,
  134. .tc = NULL,
  135. .mmc = &dwmac_mmc_ops,
  136. .setup = dwmac1000_setup,
  137. .quirks = stmmac_dwmac1_quirks,
  138. }, {
  139. .gmac = false,
  140. .gmac4 = true,
  141. .xgmac = false,
  142. .min_id = 0,
  143. .regs = {
  144. .ptp_off = PTP_GMAC4_OFFSET,
  145. .mmc_off = MMC_GMAC4_OFFSET,
  146. .est_off = EST_GMAC4_OFFSET,
  147. },
  148. .desc = &dwmac4_desc_ops,
  149. .dma = &dwmac4_dma_ops,
  150. .mac = &dwmac4_ops,
  151. .hwtimestamp = &stmmac_ptp,
  152. .mode = NULL,
  153. .tc = &dwmac4_tc_ops,
  154. .mmc = &dwmac_mmc_ops,
  155. .est = &dwmac510_est_ops,
  156. .setup = dwmac4_setup,
  157. .quirks = stmmac_dwmac4_quirks,
  158. }, {
  159. .gmac = false,
  160. .gmac4 = true,
  161. .xgmac = false,
  162. .min_id = DWMAC_CORE_4_00,
  163. .regs = {
  164. .ptp_off = PTP_GMAC4_OFFSET,
  165. .mmc_off = MMC_GMAC4_OFFSET,
  166. .est_off = EST_GMAC4_OFFSET,
  167. },
  168. .desc = &dwmac4_desc_ops,
  169. .dma = &dwmac4_dma_ops,
  170. .mac = &dwmac410_ops,
  171. .hwtimestamp = &stmmac_ptp,
  172. .mode = &dwmac4_ring_mode_ops,
  173. .tc = &dwmac510_tc_ops,
  174. .mmc = &dwmac_mmc_ops,
  175. .est = &dwmac510_est_ops,
  176. .setup = dwmac4_setup,
  177. .quirks = NULL,
  178. }, {
  179. .gmac = false,
  180. .gmac4 = true,
  181. .xgmac = false,
  182. .min_id = DWMAC_CORE_4_10,
  183. .regs = {
  184. .ptp_off = PTP_GMAC4_OFFSET,
  185. .mmc_off = MMC_GMAC4_OFFSET,
  186. .est_off = EST_GMAC4_OFFSET,
  187. },
  188. .desc = &dwmac4_desc_ops,
  189. .dma = &dwmac410_dma_ops,
  190. .mac = &dwmac410_ops,
  191. .hwtimestamp = &stmmac_ptp,
  192. .mode = &dwmac4_ring_mode_ops,
  193. .tc = &dwmac510_tc_ops,
  194. .mmc = &dwmac_mmc_ops,
  195. .est = &dwmac510_est_ops,
  196. .setup = dwmac4_setup,
  197. .quirks = NULL,
  198. }, {
  199. .gmac = false,
  200. .gmac4 = true,
  201. .xgmac = false,
  202. .min_id = DWMAC_CORE_5_10,
  203. .regs = {
  204. .ptp_off = PTP_GMAC4_OFFSET,
  205. .mmc_off = MMC_GMAC4_OFFSET,
  206. .est_off = EST_GMAC4_OFFSET,
  207. },
  208. .desc = &dwmac4_desc_ops,
  209. .dma = &dwmac410_dma_ops,
  210. .mac = &dwmac510_ops,
  211. .hwtimestamp = &stmmac_ptp,
  212. .mode = &dwmac4_ring_mode_ops,
  213. .tc = &dwmac510_tc_ops,
  214. .mmc = &dwmac_mmc_ops,
  215. .est = &dwmac510_est_ops,
  216. .setup = dwmac4_setup,
  217. .quirks = NULL,
  218. }, {
  219. .gmac = false,
  220. .gmac4 = false,
  221. .xgmac = true,
  222. .min_id = DWXGMAC_CORE_2_10,
  223. .dev_id = DWXGMAC_ID,
  224. .regs = {
  225. .ptp_off = PTP_XGMAC_OFFSET,
  226. .mmc_off = MMC_XGMAC_OFFSET,
  227. .est_off = EST_XGMAC_OFFSET,
  228. },
  229. .desc = &dwxgmac210_desc_ops,
  230. .dma = &dwxgmac210_dma_ops,
  231. .mac = &dwxgmac210_ops,
  232. .hwtimestamp = &stmmac_ptp,
  233. .mode = NULL,
  234. .tc = &dwxgmac_tc_ops,
  235. .mmc = &dwxgmac_mmc_ops,
  236. .est = &dwmac510_est_ops,
  237. .setup = dwxgmac2_setup,
  238. .quirks = NULL,
  239. }, {
  240. .gmac = false,
  241. .gmac4 = false,
  242. .xgmac = true,
  243. .min_id = DWXLGMAC_CORE_2_00,
  244. .dev_id = DWXLGMAC_ID,
  245. .regs = {
  246. .ptp_off = PTP_XGMAC_OFFSET,
  247. .mmc_off = MMC_XGMAC_OFFSET,
  248. .est_off = EST_XGMAC_OFFSET,
  249. },
  250. .desc = &dwxgmac210_desc_ops,
  251. .dma = &dwxgmac210_dma_ops,
  252. .mac = &dwxlgmac2_ops,
  253. .hwtimestamp = &stmmac_ptp,
  254. .mode = NULL,
  255. .tc = &dwxgmac_tc_ops,
  256. .mmc = &dwxgmac_mmc_ops,
  257. .est = &dwmac510_est_ops,
  258. .setup = dwxlgmac2_setup,
  259. .quirks = stmmac_dwxlgmac_quirks,
  260. },
  261. };
  262. int stmmac_hwif_init(struct stmmac_priv *priv)
  263. {
  264. bool needs_xgmac = priv->plat->has_xgmac;
  265. bool needs_gmac4 = priv->plat->has_gmac4;
  266. bool needs_gmac = priv->plat->has_gmac;
  267. const struct stmmac_hwif_entry *entry;
  268. struct mac_device_info *mac;
  269. bool needs_setup = true;
  270. u32 id, dev_id = 0;
  271. int i, ret;
  272. if (needs_gmac) {
  273. id = stmmac_get_id(priv, GMAC_VERSION);
  274. } else if (needs_gmac4 || needs_xgmac) {
  275. id = stmmac_get_id(priv, GMAC4_VERSION);
  276. if (needs_xgmac)
  277. dev_id = stmmac_get_dev_id(priv, GMAC4_VERSION);
  278. } else {
  279. id = 0;
  280. }
  281. /* Save ID for later use */
  282. priv->synopsys_id = id;
  283. /* Lets assume some safe values first */
  284. priv->ptpaddr = priv->ioaddr +
  285. (needs_gmac4 ? PTP_GMAC4_OFFSET : PTP_GMAC3_X_OFFSET);
  286. priv->mmcaddr = priv->ioaddr +
  287. (needs_gmac4 ? MMC_GMAC4_OFFSET : MMC_GMAC3_X_OFFSET);
  288. if (needs_gmac4)
  289. priv->estaddr = priv->ioaddr + EST_GMAC4_OFFSET;
  290. else if (needs_xgmac)
  291. priv->estaddr = priv->ioaddr + EST_XGMAC_OFFSET;
  292. /* Check for HW specific setup first */
  293. if (priv->plat->setup) {
  294. mac = priv->plat->setup(priv);
  295. needs_setup = false;
  296. } else {
  297. mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL);
  298. }
  299. if (!mac)
  300. return -ENOMEM;
  301. /* Fallback to generic HW */
  302. for (i = ARRAY_SIZE(stmmac_hw) - 1; i >= 0; i--) {
  303. entry = &stmmac_hw[i];
  304. if (needs_gmac ^ entry->gmac)
  305. continue;
  306. if (needs_gmac4 ^ entry->gmac4)
  307. continue;
  308. if (needs_xgmac ^ entry->xgmac)
  309. continue;
  310. /* Use synopsys_id var because some setups can override this */
  311. if (priv->synopsys_id < entry->min_id)
  312. continue;
  313. if (needs_xgmac && (dev_id ^ entry->dev_id))
  314. continue;
  315. /* Only use generic HW helpers if needed */
  316. mac->desc = mac->desc ? : entry->desc;
  317. mac->dma = mac->dma ? : entry->dma;
  318. mac->mac = mac->mac ? : entry->mac;
  319. mac->ptp = mac->ptp ? : entry->hwtimestamp;
  320. mac->mode = mac->mode ? : entry->mode;
  321. mac->tc = mac->tc ? : entry->tc;
  322. mac->mmc = mac->mmc ? : entry->mmc;
  323. mac->est = mac->est ? : entry->est;
  324. priv->hw = mac;
  325. priv->ptpaddr = priv->ioaddr + entry->regs.ptp_off;
  326. priv->mmcaddr = priv->ioaddr + entry->regs.mmc_off;
  327. if (entry->est)
  328. priv->estaddr = priv->ioaddr + entry->regs.est_off;
  329. /* Entry found */
  330. if (needs_setup) {
  331. ret = entry->setup(priv);
  332. if (ret)
  333. return ret;
  334. }
  335. /* Save quirks, if needed for posterior use */
  336. priv->hwif_quirks = entry->quirks;
  337. return 0;
  338. }
  339. dev_err(priv->device, "Failed to find HW IF (id=0x%x, gmac=%d/%d)\n",
  340. id, needs_gmac, needs_gmac4);
  341. return -EINVAL;
  342. }