cpsw_sl.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments Ethernet Switch media-access-controller (MAC) submodule/
  4. * Ethernet MAC Sliver (CPGMAC_SL)
  5. *
  6. * Copyright (C) 2019 Texas Instruments
  7. *
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include "cpsw_sl.h"
  13. #define CPSW_SL_REG_NOTUSED U16_MAX
  14. static const u16 cpsw_sl_reg_map_cpsw[] = {
  15. [CPSW_SL_IDVER] = 0x00,
  16. [CPSW_SL_MACCONTROL] = 0x04,
  17. [CPSW_SL_MACSTATUS] = 0x08,
  18. [CPSW_SL_SOFT_RESET] = 0x0c,
  19. [CPSW_SL_RX_MAXLEN] = 0x10,
  20. [CPSW_SL_BOFFTEST] = 0x14,
  21. [CPSW_SL_RX_PAUSE] = 0x18,
  22. [CPSW_SL_TX_PAUSE] = 0x1c,
  23. [CPSW_SL_EMCONTROL] = 0x20,
  24. [CPSW_SL_RX_PRI_MAP] = 0x24,
  25. [CPSW_SL_TX_GAP] = 0x28,
  26. };
  27. static const u16 cpsw_sl_reg_map_66ak2hk[] = {
  28. [CPSW_SL_IDVER] = 0x00,
  29. [CPSW_SL_MACCONTROL] = 0x04,
  30. [CPSW_SL_MACSTATUS] = 0x08,
  31. [CPSW_SL_SOFT_RESET] = 0x0c,
  32. [CPSW_SL_RX_MAXLEN] = 0x10,
  33. [CPSW_SL_BOFFTEST] = CPSW_SL_REG_NOTUSED,
  34. [CPSW_SL_RX_PAUSE] = 0x18,
  35. [CPSW_SL_TX_PAUSE] = 0x1c,
  36. [CPSW_SL_EMCONTROL] = 0x20,
  37. [CPSW_SL_RX_PRI_MAP] = 0x24,
  38. [CPSW_SL_TX_GAP] = CPSW_SL_REG_NOTUSED,
  39. };
  40. static const u16 cpsw_sl_reg_map_66ak2x_xgbe[] = {
  41. [CPSW_SL_IDVER] = 0x00,
  42. [CPSW_SL_MACCONTROL] = 0x04,
  43. [CPSW_SL_MACSTATUS] = 0x08,
  44. [CPSW_SL_SOFT_RESET] = 0x0c,
  45. [CPSW_SL_RX_MAXLEN] = 0x10,
  46. [CPSW_SL_BOFFTEST] = CPSW_SL_REG_NOTUSED,
  47. [CPSW_SL_RX_PAUSE] = 0x18,
  48. [CPSW_SL_TX_PAUSE] = 0x1c,
  49. [CPSW_SL_EMCONTROL] = 0x20,
  50. [CPSW_SL_RX_PRI_MAP] = CPSW_SL_REG_NOTUSED,
  51. [CPSW_SL_TX_GAP] = 0x28,
  52. };
  53. static const u16 cpsw_sl_reg_map_66ak2elg_am65[] = {
  54. [CPSW_SL_IDVER] = CPSW_SL_REG_NOTUSED,
  55. [CPSW_SL_MACCONTROL] = 0x00,
  56. [CPSW_SL_MACSTATUS] = 0x04,
  57. [CPSW_SL_SOFT_RESET] = 0x08,
  58. [CPSW_SL_RX_MAXLEN] = CPSW_SL_REG_NOTUSED,
  59. [CPSW_SL_BOFFTEST] = 0x0c,
  60. [CPSW_SL_RX_PAUSE] = 0x10,
  61. [CPSW_SL_TX_PAUSE] = 0x40,
  62. [CPSW_SL_EMCONTROL] = 0x70,
  63. [CPSW_SL_RX_PRI_MAP] = CPSW_SL_REG_NOTUSED,
  64. [CPSW_SL_TX_GAP] = 0x74,
  65. };
  66. #define CPSW_SL_SOFT_RESET_BIT BIT(0)
  67. #define CPSW_SL_STATUS_PN_IDLE BIT(31)
  68. #define CPSW_SL_AM65_STATUS_PN_E_IDLE BIT(30)
  69. #define CPSW_SL_AM65_STATUS_PN_P_IDLE BIT(29)
  70. #define CPSW_SL_AM65_STATUS_PN_TX_IDLE BIT(28)
  71. #define CPSW_SL_STATUS_IDLE_MASK_BASE (CPSW_SL_STATUS_PN_IDLE)
  72. #define CPSW_SL_STATUS_IDLE_MASK_K3 \
  73. (CPSW_SL_STATUS_IDLE_MASK_BASE | CPSW_SL_AM65_STATUS_PN_E_IDLE | \
  74. CPSW_SL_AM65_STATUS_PN_P_IDLE | CPSW_SL_AM65_STATUS_PN_TX_IDLE)
  75. #define CPSW_SL_CTL_FUNC_BASE \
  76. (CPSW_SL_CTL_FULLDUPLEX |\
  77. CPSW_SL_CTL_LOOPBACK |\
  78. CPSW_SL_CTL_RX_FLOW_EN |\
  79. CPSW_SL_CTL_TX_FLOW_EN |\
  80. CPSW_SL_CTL_GMII_EN |\
  81. CPSW_SL_CTL_TX_PACE |\
  82. CPSW_SL_CTL_GIG |\
  83. CPSW_SL_CTL_CMD_IDLE |\
  84. CPSW_SL_CTL_IFCTL_A |\
  85. CPSW_SL_CTL_IFCTL_B |\
  86. CPSW_SL_CTL_GIG_FORCE |\
  87. CPSW_SL_CTL_EXT_EN |\
  88. CPSW_SL_CTL_RX_CEF_EN |\
  89. CPSW_SL_CTL_RX_CSF_EN |\
  90. CPSW_SL_CTL_RX_CMF_EN)
  91. struct cpsw_sl {
  92. struct device *dev;
  93. void __iomem *sl_base;
  94. const u16 *regs;
  95. u32 control_features;
  96. u32 idle_mask;
  97. };
  98. struct cpsw_sl_dev_id {
  99. const char *device_id;
  100. const u16 *regs;
  101. const u32 control_features;
  102. const u32 regs_offset;
  103. const u32 idle_mask;
  104. };
  105. static const struct cpsw_sl_dev_id cpsw_sl_id_match[] = {
  106. {
  107. .device_id = "cpsw",
  108. .regs = cpsw_sl_reg_map_cpsw,
  109. .control_features = CPSW_SL_CTL_FUNC_BASE |
  110. CPSW_SL_CTL_MTEST |
  111. CPSW_SL_CTL_TX_SHORT_GAP_EN |
  112. CPSW_SL_CTL_TX_SG_LIM_EN,
  113. .idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE,
  114. },
  115. {
  116. .device_id = "66ak2hk",
  117. .regs = cpsw_sl_reg_map_66ak2hk,
  118. .control_features = CPSW_SL_CTL_FUNC_BASE |
  119. CPSW_SL_CTL_TX_SHORT_GAP_EN,
  120. .idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE,
  121. },
  122. {
  123. .device_id = "66ak2x_xgbe",
  124. .regs = cpsw_sl_reg_map_66ak2x_xgbe,
  125. .control_features = CPSW_SL_CTL_FUNC_BASE |
  126. CPSW_SL_CTL_XGIG |
  127. CPSW_SL_CTL_TX_SHORT_GAP_EN |
  128. CPSW_SL_CTL_CRC_TYPE |
  129. CPSW_SL_CTL_XGMII_EN,
  130. .idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE,
  131. },
  132. {
  133. .device_id = "66ak2el",
  134. .regs = cpsw_sl_reg_map_66ak2elg_am65,
  135. .regs_offset = 0x330,
  136. .control_features = CPSW_SL_CTL_FUNC_BASE |
  137. CPSW_SL_CTL_MTEST |
  138. CPSW_SL_CTL_TX_SHORT_GAP_EN |
  139. CPSW_SL_CTL_CRC_TYPE |
  140. CPSW_SL_CTL_EXT_EN_RX_FLO |
  141. CPSW_SL_CTL_EXT_EN_TX_FLO |
  142. CPSW_SL_CTL_TX_SG_LIM_EN,
  143. .idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE,
  144. },
  145. {
  146. .device_id = "66ak2g",
  147. .regs = cpsw_sl_reg_map_66ak2elg_am65,
  148. .regs_offset = 0x330,
  149. .control_features = CPSW_SL_CTL_FUNC_BASE |
  150. CPSW_SL_CTL_MTEST |
  151. CPSW_SL_CTL_CRC_TYPE |
  152. CPSW_SL_CTL_EXT_EN_RX_FLO |
  153. CPSW_SL_CTL_EXT_EN_TX_FLO,
  154. },
  155. {
  156. .device_id = "am65",
  157. .regs = cpsw_sl_reg_map_66ak2elg_am65,
  158. .regs_offset = 0x330,
  159. .control_features = CPSW_SL_CTL_FUNC_BASE |
  160. CPSW_SL_CTL_MTEST |
  161. CPSW_SL_CTL_XGIG |
  162. CPSW_SL_CTL_TX_SHORT_GAP_EN |
  163. CPSW_SL_CTL_CRC_TYPE |
  164. CPSW_SL_CTL_XGMII_EN |
  165. CPSW_SL_CTL_EXT_EN_RX_FLO |
  166. CPSW_SL_CTL_EXT_EN_TX_FLO |
  167. CPSW_SL_CTL_TX_SG_LIM_EN |
  168. CPSW_SL_CTL_EXT_EN_XGIG,
  169. .idle_mask = CPSW_SL_STATUS_IDLE_MASK_K3,
  170. },
  171. { },
  172. };
  173. u32 cpsw_sl_reg_read(struct cpsw_sl *sl, enum cpsw_sl_regs reg)
  174. {
  175. int val;
  176. if (sl->regs[reg] == CPSW_SL_REG_NOTUSED) {
  177. dev_err(sl->dev, "cpsw_sl: not sup r reg: %04X\n",
  178. sl->regs[reg]);
  179. return 0;
  180. }
  181. val = readl(sl->sl_base + sl->regs[reg]);
  182. dev_dbg(sl->dev, "cpsw_sl: reg: %04X r 0x%08X\n", sl->regs[reg], val);
  183. return val;
  184. }
  185. void cpsw_sl_reg_write(struct cpsw_sl *sl, enum cpsw_sl_regs reg, u32 val)
  186. {
  187. if (sl->regs[reg] == CPSW_SL_REG_NOTUSED) {
  188. dev_err(sl->dev, "cpsw_sl: not sup w reg: %04X\n",
  189. sl->regs[reg]);
  190. return;
  191. }
  192. dev_dbg(sl->dev, "cpsw_sl: reg: %04X w 0x%08X\n", sl->regs[reg], val);
  193. writel(val, sl->sl_base + sl->regs[reg]);
  194. }
  195. static const struct cpsw_sl_dev_id *cpsw_sl_match_id(
  196. const struct cpsw_sl_dev_id *id,
  197. const char *device_id)
  198. {
  199. if (!id || !device_id)
  200. return NULL;
  201. while (id->device_id) {
  202. if (strcmp(device_id, id->device_id) == 0)
  203. return id;
  204. id++;
  205. }
  206. return NULL;
  207. }
  208. struct cpsw_sl *cpsw_sl_get(const char *device_id, struct device *dev,
  209. void __iomem *sl_base)
  210. {
  211. const struct cpsw_sl_dev_id *sl_dev_id;
  212. struct cpsw_sl *sl;
  213. sl = devm_kzalloc(dev, sizeof(struct cpsw_sl), GFP_KERNEL);
  214. if (!sl)
  215. return ERR_PTR(-ENOMEM);
  216. sl->dev = dev;
  217. sl->sl_base = sl_base;
  218. sl_dev_id = cpsw_sl_match_id(cpsw_sl_id_match, device_id);
  219. if (!sl_dev_id) {
  220. dev_err(sl->dev, "cpsw_sl: dev_id %s not found.\n", device_id);
  221. return ERR_PTR(-EINVAL);
  222. }
  223. sl->regs = sl_dev_id->regs;
  224. sl->control_features = sl_dev_id->control_features;
  225. sl->idle_mask = sl_dev_id->idle_mask;
  226. sl->sl_base += sl_dev_id->regs_offset;
  227. return sl;
  228. }
  229. void cpsw_sl_reset(struct cpsw_sl *sl, unsigned long tmo)
  230. {
  231. unsigned long timeout = jiffies + msecs_to_jiffies(tmo);
  232. /* Set the soft reset bit */
  233. cpsw_sl_reg_write(sl, CPSW_SL_SOFT_RESET, CPSW_SL_SOFT_RESET_BIT);
  234. /* Wait for the bit to clear */
  235. do {
  236. usleep_range(100, 200);
  237. } while ((cpsw_sl_reg_read(sl, CPSW_SL_SOFT_RESET) &
  238. CPSW_SL_SOFT_RESET_BIT) &&
  239. time_after(timeout, jiffies));
  240. if (cpsw_sl_reg_read(sl, CPSW_SL_SOFT_RESET) & CPSW_SL_SOFT_RESET_BIT)
  241. dev_err(sl->dev, "cpsw_sl failed to soft-reset.\n");
  242. }
  243. u32 cpsw_sl_ctl_set(struct cpsw_sl *sl, u32 ctl_funcs)
  244. {
  245. u32 val;
  246. if (ctl_funcs & ~sl->control_features) {
  247. dev_err(sl->dev, "cpsw_sl: unsupported func 0x%08X\n",
  248. ctl_funcs & (~sl->control_features));
  249. return -EINVAL;
  250. }
  251. val = cpsw_sl_reg_read(sl, CPSW_SL_MACCONTROL);
  252. val |= ctl_funcs;
  253. cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, val);
  254. return 0;
  255. }
  256. u32 cpsw_sl_ctl_clr(struct cpsw_sl *sl, u32 ctl_funcs)
  257. {
  258. u32 val;
  259. if (ctl_funcs & ~sl->control_features) {
  260. dev_err(sl->dev, "cpsw_sl: unsupported func 0x%08X\n",
  261. ctl_funcs & (~sl->control_features));
  262. return -EINVAL;
  263. }
  264. val = cpsw_sl_reg_read(sl, CPSW_SL_MACCONTROL);
  265. val &= ~ctl_funcs;
  266. cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, val);
  267. return 0;
  268. }
  269. void cpsw_sl_ctl_reset(struct cpsw_sl *sl)
  270. {
  271. cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, 0);
  272. }
  273. int cpsw_sl_wait_for_idle(struct cpsw_sl *sl, unsigned long tmo)
  274. {
  275. unsigned long timeout = jiffies + msecs_to_jiffies(tmo);
  276. do {
  277. usleep_range(100, 200);
  278. } while (!(cpsw_sl_reg_read(sl, CPSW_SL_MACSTATUS) &
  279. sl->idle_mask) && time_after(timeout, jiffies));
  280. if (!(cpsw_sl_reg_read(sl, CPSW_SL_MACSTATUS) & sl->idle_mask)) {
  281. dev_err(sl->dev, "cpsw_sl failed to soft-reset.\n");
  282. return -ETIMEDOUT;
  283. }
  284. return 0;
  285. }