hwif.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * TC956X ethernet driver.
  3. *
  4. * hwif.c
  5. *
  6. * Copyright (C) 2018 Synopsys, Inc. and/or its affiliates.
  7. * Copyright (C) 2021 Toshiba Electronic Devices & Storage Corporation
  8. *
  9. * This file has been derived from the STMicro and Synopsys Linux driver,
  10. * and developed or modified for TC956X.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. /*! History:
  27. * 20 Jan 2021 : Initial Version
  28. * VERSION : 00-01
  29. *
  30. * 15 Mar 2021 : Base lined
  31. * VERSION : 01-00
  32. */
  33. #include "common.h"
  34. #include "tc956xmac.h"
  35. #include "tc956xmac_ptp.h"
  36. #ifndef TC956X_SRIOV_VF
  37. #ifdef TC956X
  38. #include "tc956x_xpcs.h"
  39. #include "tc956x_pma.h"
  40. #endif
  41. #endif
  42. static u32 tc956xmac_get_id(struct tc956xmac_priv *priv, u32 id_reg)
  43. {
  44. u32 reg = readl(priv->ioaddr + id_reg);
  45. if (!reg) {
  46. dev_info(priv->device, "Version ID not available\n");
  47. return 0x0;
  48. }
  49. dev_info(priv->device, "User ID: 0x%x, Synopsys ID: 0x%x\n",
  50. (unsigned int)(reg & GENMASK(15, 8)) >> 8,
  51. (unsigned int)(reg & GENMASK(7, 0)));
  52. return reg & GENMASK(7, 0);
  53. }
  54. #ifndef TC956X
  55. static void tc956xmac_dwmac_mode_quirk(struct tc956xmac_priv *priv)
  56. {
  57. struct mac_device_info *mac = priv->hw;
  58. if (priv->chain_mode) {
  59. dev_info(priv->device, "Chain mode enabled\n");
  60. priv->mode = TC956XMAC_CHAIN_MODE;
  61. mac->mode = &chain_mode_ops;
  62. } else {
  63. dev_info(priv->device, "Ring mode enabled\n");
  64. priv->mode = TC956XMAC_RING_MODE;
  65. mac->mode = &ring_mode_ops;
  66. }
  67. }
  68. static int tc956xmac_dwmac1_quirks(struct tc956xmac_priv *priv)
  69. {
  70. struct mac_device_info *mac = priv->hw;
  71. if (priv->plat->enh_desc) {
  72. dev_info(priv->device, "Enhanced/Alternate descriptors\n");
  73. /* GMAC older than 3.50 has no extended descriptors */
  74. if (priv->synopsys_id >= DWMAC_CORE_3_50) {
  75. dev_info(priv->device, "Enabled extended descriptors\n");
  76. priv->extend_desc = 1;
  77. } else {
  78. dev_warn(priv->device, "Extended descriptors not supported\n");
  79. }
  80. mac->desc = &enh_desc_ops;
  81. } else {
  82. dev_info(priv->device, "Normal descriptors\n");
  83. mac->desc = &ndesc_ops;
  84. }
  85. tc956xmac_dwmac_mode_quirk(priv);
  86. return 0;
  87. }
  88. static int tc956xmac_dwmac4_quirks(struct tc956xmac_priv *priv)
  89. {
  90. tc956xmac_dwmac_mode_quirk(priv);
  91. return 0;
  92. }
  93. #endif
  94. static const struct tc956xmac_hwif_entry {
  95. bool gmac;
  96. bool gmac4;
  97. bool xgmac;
  98. u32 min_id;
  99. const struct tc956xmac_regs_off regs;
  100. const void *desc;
  101. const void *dma;
  102. const void *mac;
  103. const void *hwtimestamp;
  104. const void *mode;
  105. const void *tc;
  106. const void *mmc;
  107. const void *pma;
  108. #if defined(TC956X_SRIOV_PF) | defined(TC956X_SRIOV_VF)
  109. const void *msi;
  110. const void *rsc;
  111. const void *mbx;
  112. const void *mbx_wrapper;
  113. #endif
  114. int (*setup)(struct tc956xmac_priv *priv);
  115. int (*quirks)(struct tc956xmac_priv *priv);
  116. } tc956xmac_hw[] = {
  117. /* NOTE: New HW versions shall go to the end of this table */
  118. #ifndef TC956X
  119. {
  120. .gmac = false,
  121. .gmac4 = false,
  122. .xgmac = false,
  123. .min_id = 0,
  124. .regs = {
  125. .ptp_off = PTP_GMAC3_X_OFFSET_BASE,
  126. .mmc_off = MMC_GMAC3_X_OFFSET_BASE,
  127. },
  128. .desc = NULL,
  129. .dma = &dwmac100_dma_ops,
  130. .mac = &dwmac100_ops,
  131. .hwtimestamp = &tc956xmac_ptp,
  132. .mode = NULL,
  133. .tc = NULL,
  134. .mmc = &dwmac_mmc_ops,
  135. .setup = dwmac100_setup,
  136. .quirks = tc956xmac_dwmac1_quirks,
  137. }, {
  138. .gmac = true,
  139. .gmac4 = false,
  140. .xgmac = false,
  141. .min_id = 0,
  142. .regs = {
  143. .ptp_off = PTP_GMAC3_X_OFFSET_BASE,
  144. .mmc_off = MMC_GMAC3_X_OFFSET_BASE,
  145. },
  146. .desc = NULL,
  147. .dma = &dwmac1000_dma_ops,
  148. .mac = &dwmac1000_ops,
  149. .hwtimestamp = &tc956xmac_ptp,
  150. .mode = NULL,
  151. .tc = NULL,
  152. .mmc = &dwmac_mmc_ops,
  153. .setup = dwmac1000_setup,
  154. .quirks = tc956xmac_dwmac1_quirks,
  155. }, {
  156. .gmac = false,
  157. .gmac4 = true,
  158. .xgmac = false,
  159. .min_id = 0,
  160. .regs = {
  161. .ptp_off = PTP_GMAC4_OFFSET_BASE,
  162. .mmc_off = MMC_GMAC4_OFFSET_BASE,
  163. },
  164. .desc = &dwmac4_desc_ops,
  165. .dma = &dwmac4_dma_ops,
  166. .mac = &dwmac4_ops,
  167. .hwtimestamp = &tc956xmac_ptp,
  168. .mode = NULL,
  169. .tc = &dwmac510_tc_ops,
  170. .mmc = &dwmac_mmc_ops,
  171. .setup = dwmac4_setup,
  172. .quirks = tc956xmac_dwmac4_quirks,
  173. }, {
  174. .gmac = false,
  175. .gmac4 = true,
  176. .xgmac = false,
  177. .min_id = DWMAC_CORE_4_00,
  178. .regs = {
  179. .ptp_off = PTP_GMAC4_OFFSET_BASE,
  180. .mmc_off = MMC_GMAC4_OFFSET_BASE,
  181. },
  182. .desc = &dwmac4_desc_ops,
  183. .dma = &dwmac4_dma_ops,
  184. .mac = &dwmac410_ops,
  185. .hwtimestamp = &tc956xmac_ptp,
  186. .mode = &dwmac4_ring_mode_ops,
  187. .tc = &dwmac510_tc_ops,
  188. .mmc = &dwmac_mmc_ops,
  189. .setup = dwmac4_setup,
  190. .quirks = NULL,
  191. }, {
  192. .gmac = false,
  193. .gmac4 = true,
  194. .xgmac = false,
  195. .min_id = DWMAC_CORE_4_10,
  196. .regs = {
  197. .ptp_off = PTP_GMAC4_OFFSET_BASE,
  198. .mmc_off = MMC_GMAC4_OFFSET_BASE,
  199. },
  200. .desc = &dwmac4_desc_ops,
  201. .dma = &dwmac410_dma_ops,
  202. .mac = &dwmac410_ops,
  203. .hwtimestamp = &tc956xmac_ptp,
  204. .mode = &dwmac4_ring_mode_ops,
  205. .tc = &dwmac510_tc_ops,
  206. .mmc = &dwmac_mmc_ops,
  207. .setup = dwmac4_setup,
  208. .quirks = NULL,
  209. }, {
  210. .gmac = false,
  211. .gmac4 = true,
  212. .xgmac = false,
  213. .min_id = DWMAC_CORE_5_10,
  214. .regs = {
  215. .ptp_off = PTP_GMAC4_OFFSET_BASE,
  216. .mmc_off = MMC_GMAC4_OFFSET_BASE,
  217. },
  218. .desc = &dwmac4_desc_ops,
  219. .dma = &dwmac410_dma_ops,
  220. .mac = &dwmac510_ops,
  221. .hwtimestamp = &tc956xmac_ptp,
  222. .mode = &dwmac4_ring_mode_ops,
  223. .tc = &dwmac510_tc_ops,
  224. .mmc = &dwmac_mmc_ops,
  225. .setup = dwmac4_setup,
  226. .quirks = NULL,
  227. }, {
  228. #endif
  229. {
  230. .gmac = false,
  231. .gmac4 = false,
  232. .xgmac = true,
  233. .min_id = DWXGMAC_CORE_3_01,
  234. .regs = {
  235. .ptp_off = PTP_XGMAC_OFFSET_BASE,
  236. .mmc_off = MMC_XGMAC_OFFSET_BASE,
  237. #ifndef TC956X_SRIOV_VF
  238. #ifdef TC956X
  239. .xpcs_off = XPCS_XGMAC_OFFSET,
  240. .pma_off = PMA_XGMAC_OFFSET,
  241. #endif
  242. #endif
  243. },
  244. .desc = &dwxgmac210_desc_ops,
  245. .dma = &dwxgmac210_dma_ops,
  246. .mac = &dwxgmac210_ops,
  247. .hwtimestamp = &tc956xmac_ptp,
  248. .mode = NULL,
  249. .tc = &dwmac510_tc_ops,
  250. .mmc = &dwxgmac_mmc_ops,
  251. #ifndef TC956X_SRIOV_VF
  252. #ifdef TC956X
  253. .pma = &tc956x_pma_ops,
  254. #endif
  255. #endif
  256. #if defined(TC956X_SRIOV_PF) | defined(TC956X_SRIOV_VF)
  257. .msi = &tc956x_msigen_ops,
  258. .rsc = &tc956xmac_rsc_mng_ops,
  259. #endif
  260. #if (defined(TC956X_SRIOV_PF) && !defined(TC956X_AUTOMOTIVE_CONFIG) && !defined(TC956X_ENABLE_MAC2MAC_BRIDGE)) | defined(TC956X_SRIOV_VF)
  261. .mbx = &tc956xmac_mbx_ops,
  262. .mbx_wrapper = &tc956xmac_mbx_wrapper_ops,
  263. #endif
  264. .setup = dwxgmac2_setup,
  265. .quirks = NULL,
  266. },
  267. };
  268. int tc956xmac_hwif_init(struct tc956xmac_priv *priv)
  269. {
  270. bool needs_xgmac = priv->plat->has_xgmac;
  271. bool needs_gmac4 = priv->plat->has_gmac4;
  272. bool needs_gmac = priv->plat->has_gmac;
  273. const struct tc956xmac_hwif_entry *entry;
  274. struct mac_device_info *mac;
  275. bool needs_setup = true;
  276. int i, ret;
  277. u32 id;
  278. u32 mac_offset_base = priv->port_num == RM_PF0_ID ?
  279. MAC0_BASE_OFFSET : MAC1_BASE_OFFSET;
  280. if (needs_gmac)
  281. id = tc956xmac_get_id(priv, GMAC_VERSION);
  282. else if (needs_gmac4 || needs_xgmac)
  283. id = tc956xmac_get_id(priv, GMAC4_VERSION);
  284. else
  285. id = 0;
  286. /* Save ID for later use */
  287. priv->synopsys_id = id;
  288. /* Lets assume some safe values first */
  289. priv->ptpaddr = priv->ioaddr +
  290. (needs_gmac4 ? PTP_GMAC4_OFFSET : PTP_GMAC3_X_OFFSET);
  291. priv->mmcaddr = priv->ioaddr +
  292. (needs_gmac4 ? MMC_GMAC4_OFFSET : MMC_GMAC3_X_OFFSET);
  293. #ifndef TC956X_SRIOV_VF
  294. #ifdef TC956X
  295. priv->xpcsaddr = priv->ioaddr + XPCS_XGMAC_OFFSET;
  296. priv->pmaaddr = priv->ioaddr + PMA_XGMAC_OFFSET;
  297. #endif
  298. #endif
  299. /* Check for HW specific setup first */
  300. if (priv->plat->setup) {
  301. mac = priv->plat->setup(priv);
  302. needs_setup = false;
  303. } else {
  304. mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL);
  305. }
  306. if (!mac)
  307. return -ENOMEM;
  308. /* Fallback to generic HW */
  309. for (i = ARRAY_SIZE(tc956xmac_hw) - 1; i >= 0; i--) {
  310. entry = &tc956xmac_hw[i];
  311. if (needs_gmac ^ entry->gmac)
  312. continue;
  313. if (needs_gmac4 ^ entry->gmac4)
  314. continue;
  315. if (needs_xgmac ^ entry->xgmac)
  316. continue;
  317. /* Use synopsys_id var because some setups can override this */
  318. if (priv->synopsys_id < entry->min_id)
  319. continue;
  320. /* Only use generic HW helpers if needed */
  321. mac->desc = mac->desc ? : entry->desc;
  322. mac->dma = mac->dma ? : entry->dma;
  323. mac->mac = mac->mac ? : entry->mac;
  324. mac->ptp = mac->ptp ? : entry->hwtimestamp;
  325. mac->mode = mac->mode ? : entry->mode;
  326. mac->tc = mac->tc ? : entry->tc;
  327. mac->mmc = mac->mmc ? : entry->mmc;
  328. #ifndef TC956X_SRIOV_VF
  329. mac->pma = mac->pma ? : entry->pma;
  330. #endif
  331. #if defined(TC956X_SRIOV_PF) | defined(TC956X_SRIOV_VF)
  332. mac->msi = mac->msi ? : entry->msi;
  333. mac->rsc = mac->rsc ? : entry->rsc;
  334. mac->mbx = mac->mbx ? : entry->mbx;
  335. mac->mbx_wrapper = mac->mbx_wrapper ? : entry->mbx_wrapper;
  336. #endif
  337. priv->hw = mac;
  338. priv->ptpaddr = priv->ioaddr + mac_offset_base + entry->regs.ptp_off;
  339. priv->mmcaddr = priv->ioaddr + mac_offset_base + entry->regs.mmc_off;
  340. #ifndef TC956X_SRIOV_VF
  341. #ifdef TC956X
  342. priv->xpcsaddr = priv->ioaddr + mac_offset_base + entry->regs.xpcs_off;
  343. priv->pmaaddr = priv->ioaddr + mac_offset_base + entry->regs.pma_off;
  344. #endif
  345. #endif
  346. /* Entry found */
  347. if (needs_setup) {
  348. ret = entry->setup(priv);
  349. if (ret)
  350. return ret;
  351. }
  352. /* Save quirks, if needed for posterior use */
  353. priv->hwif_quirks = entry->quirks;
  354. return 0;
  355. }
  356. dev_err(priv->device, "Failed to find HW IF (id=0x%x, gmac=%d/%d)\n",
  357. id, needs_gmac, needs_gmac4);
  358. return -EINVAL;
  359. }