dwmac-imx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dwmac-imx.c - DWMAC Specific Glue layer for NXP imx8
  4. *
  5. * Copyright 2020 NXP
  6. *
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_net.h>
  15. #include <linux/phy.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_wakeirq.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/stmmac.h>
  21. #include "stmmac_platform.h"
  22. #define GPR_ENET_QOS_INTF_MODE_MASK GENMASK(21, 16)
  23. #define GPR_ENET_QOS_INTF_SEL_MII (0x0 << 16)
  24. #define GPR_ENET_QOS_INTF_SEL_RMII (0x4 << 16)
  25. #define GPR_ENET_QOS_INTF_SEL_RGMII (0x1 << 16)
  26. #define GPR_ENET_QOS_CLK_GEN_EN (0x1 << 19)
  27. #define GPR_ENET_QOS_CLK_TX_CLK_SEL (0x1 << 20)
  28. #define GPR_ENET_QOS_RGMII_EN (0x1 << 21)
  29. #define MX93_GPR_ENET_QOS_INTF_MODE_MASK GENMASK(3, 0)
  30. #define MX93_GPR_ENET_QOS_INTF_MASK GENMASK(3, 1)
  31. #define MX93_GPR_ENET_QOS_INTF_SEL_MII (0x0 << 1)
  32. #define MX93_GPR_ENET_QOS_INTF_SEL_RMII (0x4 << 1)
  33. #define MX93_GPR_ENET_QOS_INTF_SEL_RGMII (0x1 << 1)
  34. #define MX93_GPR_ENET_QOS_CLK_GEN_EN (0x1 << 0)
  35. #define DMA_BUS_MODE 0x00001000
  36. #define DMA_BUS_MODE_SFT_RESET (0x1 << 0)
  37. #define RMII_RESET_SPEED (0x3 << 14)
  38. #define CTRL_SPEED_MASK GENMASK(15, 14)
  39. struct imx_dwmac_ops {
  40. u32 addr_width;
  41. u32 flags;
  42. bool mac_rgmii_txclk_auto_adj;
  43. int (*fix_soc_reset)(void *priv, void __iomem *ioaddr);
  44. int (*set_intf_mode)(struct plat_stmmacenet_data *plat_dat);
  45. void (*fix_mac_speed)(void *priv, unsigned int speed, unsigned int mode);
  46. };
  47. struct imx_priv_data {
  48. struct device *dev;
  49. struct clk *clk_tx;
  50. struct clk *clk_mem;
  51. struct regmap *intf_regmap;
  52. u32 intf_reg_off;
  53. bool rmii_refclk_ext;
  54. void __iomem *base_addr;
  55. const struct imx_dwmac_ops *ops;
  56. struct plat_stmmacenet_data *plat_dat;
  57. };
  58. static int imx8mp_set_intf_mode(struct plat_stmmacenet_data *plat_dat)
  59. {
  60. struct imx_priv_data *dwmac = plat_dat->bsp_priv;
  61. int val;
  62. switch (plat_dat->mac_interface) {
  63. case PHY_INTERFACE_MODE_MII:
  64. val = GPR_ENET_QOS_INTF_SEL_MII;
  65. break;
  66. case PHY_INTERFACE_MODE_RMII:
  67. val = GPR_ENET_QOS_INTF_SEL_RMII;
  68. val |= (dwmac->rmii_refclk_ext ? 0 : GPR_ENET_QOS_CLK_TX_CLK_SEL);
  69. break;
  70. case PHY_INTERFACE_MODE_RGMII:
  71. case PHY_INTERFACE_MODE_RGMII_ID:
  72. case PHY_INTERFACE_MODE_RGMII_RXID:
  73. case PHY_INTERFACE_MODE_RGMII_TXID:
  74. val = GPR_ENET_QOS_INTF_SEL_RGMII |
  75. GPR_ENET_QOS_RGMII_EN;
  76. break;
  77. default:
  78. pr_debug("imx dwmac doesn't support %d interface\n",
  79. plat_dat->mac_interface);
  80. return -EINVAL;
  81. }
  82. val |= GPR_ENET_QOS_CLK_GEN_EN;
  83. return regmap_update_bits(dwmac->intf_regmap, dwmac->intf_reg_off,
  84. GPR_ENET_QOS_INTF_MODE_MASK, val);
  85. };
  86. static int
  87. imx8dxl_set_intf_mode(struct plat_stmmacenet_data *plat_dat)
  88. {
  89. int ret = 0;
  90. /* TBD: depends on imx8dxl scu interfaces to be upstreamed */
  91. return ret;
  92. }
  93. static int imx93_set_intf_mode(struct plat_stmmacenet_data *plat_dat)
  94. {
  95. struct imx_priv_data *dwmac = plat_dat->bsp_priv;
  96. int val;
  97. switch (plat_dat->mac_interface) {
  98. case PHY_INTERFACE_MODE_MII:
  99. val = MX93_GPR_ENET_QOS_INTF_SEL_MII;
  100. break;
  101. case PHY_INTERFACE_MODE_RMII:
  102. val = MX93_GPR_ENET_QOS_INTF_SEL_RMII;
  103. break;
  104. case PHY_INTERFACE_MODE_RGMII:
  105. case PHY_INTERFACE_MODE_RGMII_ID:
  106. case PHY_INTERFACE_MODE_RGMII_RXID:
  107. case PHY_INTERFACE_MODE_RGMII_TXID:
  108. val = MX93_GPR_ENET_QOS_INTF_SEL_RGMII;
  109. break;
  110. default:
  111. dev_dbg(dwmac->dev, "imx dwmac doesn't support %d interface\n",
  112. plat_dat->mac_interface);
  113. return -EINVAL;
  114. }
  115. val |= MX93_GPR_ENET_QOS_CLK_GEN_EN;
  116. return regmap_update_bits(dwmac->intf_regmap, dwmac->intf_reg_off,
  117. MX93_GPR_ENET_QOS_INTF_MODE_MASK, val);
  118. };
  119. static int imx_dwmac_clks_config(void *priv, bool enabled)
  120. {
  121. struct imx_priv_data *dwmac = priv;
  122. int ret = 0;
  123. if (enabled) {
  124. ret = clk_prepare_enable(dwmac->clk_mem);
  125. if (ret) {
  126. dev_err(dwmac->dev, "mem clock enable failed\n");
  127. return ret;
  128. }
  129. ret = clk_prepare_enable(dwmac->clk_tx);
  130. if (ret) {
  131. dev_err(dwmac->dev, "tx clock enable failed\n");
  132. clk_disable_unprepare(dwmac->clk_mem);
  133. return ret;
  134. }
  135. } else {
  136. clk_disable_unprepare(dwmac->clk_tx);
  137. clk_disable_unprepare(dwmac->clk_mem);
  138. }
  139. return ret;
  140. }
  141. static int imx_dwmac_init(struct platform_device *pdev, void *priv)
  142. {
  143. struct plat_stmmacenet_data *plat_dat;
  144. struct imx_priv_data *dwmac = priv;
  145. int ret;
  146. plat_dat = dwmac->plat_dat;
  147. if (dwmac->ops->set_intf_mode) {
  148. ret = dwmac->ops->set_intf_mode(plat_dat);
  149. if (ret)
  150. return ret;
  151. }
  152. return 0;
  153. }
  154. static void imx_dwmac_exit(struct platform_device *pdev, void *priv)
  155. {
  156. /* nothing to do now */
  157. }
  158. static void imx_dwmac_fix_speed(void *priv, unsigned int speed, unsigned int mode)
  159. {
  160. struct plat_stmmacenet_data *plat_dat;
  161. struct imx_priv_data *dwmac = priv;
  162. unsigned long rate;
  163. int err;
  164. plat_dat = dwmac->plat_dat;
  165. if (dwmac->ops->mac_rgmii_txclk_auto_adj ||
  166. (plat_dat->mac_interface == PHY_INTERFACE_MODE_RMII) ||
  167. (plat_dat->mac_interface == PHY_INTERFACE_MODE_MII))
  168. return;
  169. switch (speed) {
  170. case SPEED_1000:
  171. rate = 125000000;
  172. break;
  173. case SPEED_100:
  174. rate = 25000000;
  175. break;
  176. case SPEED_10:
  177. rate = 2500000;
  178. break;
  179. default:
  180. dev_err(dwmac->dev, "invalid speed %u\n", speed);
  181. return;
  182. }
  183. err = clk_set_rate(dwmac->clk_tx, rate);
  184. if (err < 0)
  185. dev_err(dwmac->dev, "failed to set tx rate %lu\n", rate);
  186. }
  187. static void imx93_dwmac_fix_speed(void *priv, unsigned int speed, unsigned int mode)
  188. {
  189. struct imx_priv_data *dwmac = priv;
  190. unsigned int iface;
  191. int ctrl, old_ctrl;
  192. imx_dwmac_fix_speed(priv, speed, mode);
  193. if (!dwmac || mode != MLO_AN_FIXED)
  194. return;
  195. if (regmap_read(dwmac->intf_regmap, dwmac->intf_reg_off, &iface))
  196. return;
  197. iface &= MX93_GPR_ENET_QOS_INTF_MASK;
  198. if (iface != MX93_GPR_ENET_QOS_INTF_SEL_RGMII)
  199. return;
  200. old_ctrl = readl(dwmac->base_addr + MAC_CTRL_REG);
  201. ctrl = old_ctrl & ~CTRL_SPEED_MASK;
  202. regmap_update_bits(dwmac->intf_regmap, dwmac->intf_reg_off,
  203. MX93_GPR_ENET_QOS_INTF_MODE_MASK, 0);
  204. writel(ctrl, dwmac->base_addr + MAC_CTRL_REG);
  205. /* Ensure the settings for CTRL are applied. */
  206. readl(dwmac->base_addr + MAC_CTRL_REG);
  207. usleep_range(10, 20);
  208. iface |= MX93_GPR_ENET_QOS_CLK_GEN_EN;
  209. regmap_update_bits(dwmac->intf_regmap, dwmac->intf_reg_off,
  210. MX93_GPR_ENET_QOS_INTF_MODE_MASK, iface);
  211. writel(old_ctrl, dwmac->base_addr + MAC_CTRL_REG);
  212. }
  213. static int imx_dwmac_mx93_reset(void *priv, void __iomem *ioaddr)
  214. {
  215. struct plat_stmmacenet_data *plat_dat = priv;
  216. u32 value = readl(ioaddr + DMA_BUS_MODE);
  217. /* DMA SW reset */
  218. value |= DMA_BUS_MODE_SFT_RESET;
  219. writel(value, ioaddr + DMA_BUS_MODE);
  220. if (plat_dat->mac_interface == PHY_INTERFACE_MODE_RMII) {
  221. usleep_range(100, 200);
  222. writel(RMII_RESET_SPEED, ioaddr + MAC_CTRL_REG);
  223. }
  224. return readl_poll_timeout(ioaddr + DMA_BUS_MODE, value,
  225. !(value & DMA_BUS_MODE_SFT_RESET),
  226. 10000, 1000000);
  227. }
  228. static int
  229. imx_dwmac_parse_dt(struct imx_priv_data *dwmac, struct device *dev)
  230. {
  231. struct device_node *np = dev->of_node;
  232. int err = 0;
  233. dwmac->rmii_refclk_ext = of_property_read_bool(np, "snps,rmii_refclk_ext");
  234. dwmac->clk_tx = devm_clk_get(dev, "tx");
  235. if (IS_ERR(dwmac->clk_tx)) {
  236. dev_err(dev, "failed to get tx clock\n");
  237. return PTR_ERR(dwmac->clk_tx);
  238. }
  239. dwmac->clk_mem = NULL;
  240. if (of_machine_is_compatible("fsl,imx8dxl") ||
  241. of_machine_is_compatible("fsl,imx93")) {
  242. dwmac->clk_mem = devm_clk_get(dev, "mem");
  243. if (IS_ERR(dwmac->clk_mem)) {
  244. dev_err(dev, "failed to get mem clock\n");
  245. return PTR_ERR(dwmac->clk_mem);
  246. }
  247. }
  248. if (of_machine_is_compatible("fsl,imx8mp") ||
  249. of_machine_is_compatible("fsl,imx93")) {
  250. /* Binding doc describes the propety:
  251. * is required by i.MX8MP, i.MX93.
  252. * is optinoal for i.MX8DXL.
  253. */
  254. dwmac->intf_regmap = syscon_regmap_lookup_by_phandle(np, "intf_mode");
  255. if (IS_ERR(dwmac->intf_regmap))
  256. return PTR_ERR(dwmac->intf_regmap);
  257. err = of_property_read_u32_index(np, "intf_mode", 1, &dwmac->intf_reg_off);
  258. if (err) {
  259. dev_err(dev, "Can't get intf mode reg offset (%d)\n", err);
  260. return err;
  261. }
  262. }
  263. return err;
  264. }
  265. static int imx_dwmac_probe(struct platform_device *pdev)
  266. {
  267. struct plat_stmmacenet_data *plat_dat;
  268. struct stmmac_resources stmmac_res;
  269. struct imx_priv_data *dwmac;
  270. const struct imx_dwmac_ops *data;
  271. int ret;
  272. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  273. if (ret)
  274. return ret;
  275. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  276. if (!dwmac)
  277. return -ENOMEM;
  278. plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
  279. if (IS_ERR(plat_dat))
  280. return PTR_ERR(plat_dat);
  281. data = of_device_get_match_data(&pdev->dev);
  282. if (!data) {
  283. dev_err(&pdev->dev, "failed to get match data\n");
  284. return -EINVAL;
  285. }
  286. dwmac->ops = data;
  287. dwmac->dev = &pdev->dev;
  288. ret = imx_dwmac_parse_dt(dwmac, &pdev->dev);
  289. if (ret) {
  290. dev_err(&pdev->dev, "failed to parse OF data\n");
  291. return ret;
  292. }
  293. if (data->flags & STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY)
  294. plat_dat->flags |= STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY;
  295. /* Default TX Q0 to use TSO and rest TXQ for TBS */
  296. for (int i = 1; i < plat_dat->tx_queues_to_use; i++)
  297. plat_dat->tx_queues_cfg[i].tbs_en = 1;
  298. plat_dat->host_dma_width = dwmac->ops->addr_width;
  299. plat_dat->init = imx_dwmac_init;
  300. plat_dat->exit = imx_dwmac_exit;
  301. plat_dat->clks_config = imx_dwmac_clks_config;
  302. plat_dat->fix_mac_speed = imx_dwmac_fix_speed;
  303. plat_dat->bsp_priv = dwmac;
  304. dwmac->plat_dat = plat_dat;
  305. dwmac->base_addr = stmmac_res.addr;
  306. ret = imx_dwmac_clks_config(dwmac, true);
  307. if (ret)
  308. return ret;
  309. ret = imx_dwmac_init(pdev, dwmac);
  310. if (ret)
  311. goto err_dwmac_init;
  312. if (dwmac->ops->fix_mac_speed)
  313. plat_dat->fix_mac_speed = dwmac->ops->fix_mac_speed;
  314. dwmac->plat_dat->fix_soc_reset = dwmac->ops->fix_soc_reset;
  315. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  316. if (ret)
  317. goto err_drv_probe;
  318. return 0;
  319. err_drv_probe:
  320. imx_dwmac_exit(pdev, plat_dat->bsp_priv);
  321. err_dwmac_init:
  322. imx_dwmac_clks_config(dwmac, false);
  323. return ret;
  324. }
  325. static struct imx_dwmac_ops imx8mp_dwmac_data = {
  326. .addr_width = 34,
  327. .mac_rgmii_txclk_auto_adj = false,
  328. .set_intf_mode = imx8mp_set_intf_mode,
  329. .flags = STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY,
  330. };
  331. static struct imx_dwmac_ops imx8dxl_dwmac_data = {
  332. .addr_width = 32,
  333. .mac_rgmii_txclk_auto_adj = true,
  334. .set_intf_mode = imx8dxl_set_intf_mode,
  335. };
  336. static struct imx_dwmac_ops imx93_dwmac_data = {
  337. .addr_width = 32,
  338. .mac_rgmii_txclk_auto_adj = true,
  339. .set_intf_mode = imx93_set_intf_mode,
  340. .fix_soc_reset = imx_dwmac_mx93_reset,
  341. .fix_mac_speed = imx93_dwmac_fix_speed,
  342. };
  343. static const struct of_device_id imx_dwmac_match[] = {
  344. { .compatible = "nxp,imx8mp-dwmac-eqos", .data = &imx8mp_dwmac_data },
  345. { .compatible = "nxp,imx8dxl-dwmac-eqos", .data = &imx8dxl_dwmac_data },
  346. { .compatible = "nxp,imx93-dwmac-eqos", .data = &imx93_dwmac_data },
  347. { }
  348. };
  349. MODULE_DEVICE_TABLE(of, imx_dwmac_match);
  350. static struct platform_driver imx_dwmac_driver = {
  351. .probe = imx_dwmac_probe,
  352. .remove_new = stmmac_pltfr_remove,
  353. .driver = {
  354. .name = "imx-dwmac",
  355. .pm = &stmmac_pltfr_pm_ops,
  356. .of_match_table = imx_dwmac_match,
  357. },
  358. };
  359. module_platform_driver(imx_dwmac_driver);
  360. MODULE_AUTHOR("NXP");
  361. MODULE_DESCRIPTION("NXP imx8 DWMAC Specific Glue layer");
  362. MODULE_LICENSE("GPL v2");