dwmac-sti.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dwmac-sti.c - STMicroelectronics DWMAC Specific Glue layer
  4. *
  5. * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
  6. * Author: Srinivas Kandagatla <[email protected]>
  7. * Contributors: Giuseppe Cavallaro <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/stmmac.h>
  13. #include <linux/phy.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/regmap.h>
  17. #include <linux/clk.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_net.h>
  21. #include "stmmac_platform.h"
  22. #define DWMAC_125MHZ 125000000
  23. #define DWMAC_50MHZ 50000000
  24. #define DWMAC_25MHZ 25000000
  25. #define DWMAC_2_5MHZ 2500000
  26. #define IS_PHY_IF_MODE_RGMII(iface) (iface == PHY_INTERFACE_MODE_RGMII || \
  27. iface == PHY_INTERFACE_MODE_RGMII_ID || \
  28. iface == PHY_INTERFACE_MODE_RGMII_RXID || \
  29. iface == PHY_INTERFACE_MODE_RGMII_TXID)
  30. #define IS_PHY_IF_MODE_GBIT(iface) (IS_PHY_IF_MODE_RGMII(iface) || \
  31. iface == PHY_INTERFACE_MODE_GMII)
  32. /* STiH4xx register definitions (STiH415/STiH416/STiH407/STiH410 families)
  33. *
  34. * Below table summarizes the clock requirement and clock sources for
  35. * supported phy interface modes with link speeds.
  36. * ________________________________________________
  37. *| PHY_MODE | 1000 Mbit Link | 100 Mbit Link |
  38. * ------------------------------------------------
  39. *| MII | n/a | 25Mhz |
  40. *| | | txclk |
  41. * ------------------------------------------------
  42. *| GMII | 125Mhz | 25Mhz |
  43. *| | clk-125/txclk | txclk |
  44. * ------------------------------------------------
  45. *| RGMII | 125Mhz | 25Mhz |
  46. *| | clk-125/txclk | clkgen |
  47. *| | clkgen | |
  48. * ------------------------------------------------
  49. *| RMII | n/a | 25Mhz |
  50. *| | |clkgen/phyclk-in |
  51. * ------------------------------------------------
  52. *
  53. * Register Configuration
  54. *-------------------------------
  55. * src |BIT(8)| BIT(7)| BIT(6)|
  56. *-------------------------------
  57. * txclk | 0 | n/a | 1 |
  58. *-------------------------------
  59. * ck_125| 0 | n/a | 0 |
  60. *-------------------------------
  61. * phyclk| 1 | 0 | n/a |
  62. *-------------------------------
  63. * clkgen| 1 | 1 | n/a |
  64. *-------------------------------
  65. */
  66. #define STIH4XX_RETIME_SRC_MASK GENMASK(8, 6)
  67. #define STIH4XX_ETH_SEL_TX_RETIME_CLK BIT(8)
  68. #define STIH4XX_ETH_SEL_INTERNAL_NOTEXT_PHYCLK BIT(7)
  69. #define STIH4XX_ETH_SEL_TXCLK_NOT_CLK125 BIT(6)
  70. /* STiD127 register definitions
  71. *-----------------------
  72. * src |BIT(6)| BIT(7)|
  73. *-----------------------
  74. * MII | 1 | n/a |
  75. *-----------------------
  76. * RMII | n/a | 1 |
  77. * clkgen| | |
  78. *-----------------------
  79. * RMII | n/a | 0 |
  80. * phyclk| | |
  81. *-----------------------
  82. * RGMII | 1 | n/a |
  83. * clkgen| | |
  84. *-----------------------
  85. */
  86. #define STID127_RETIME_SRC_MASK GENMASK(7, 6)
  87. #define STID127_ETH_SEL_INTERNAL_NOTEXT_PHYCLK BIT(7)
  88. #define STID127_ETH_SEL_INTERNAL_NOTEXT_TXCLK BIT(6)
  89. #define ENMII_MASK GENMASK(5, 5)
  90. #define ENMII BIT(5)
  91. #define EN_MASK GENMASK(1, 1)
  92. #define EN BIT(1)
  93. /*
  94. * 3 bits [4:2]
  95. * 000-GMII/MII
  96. * 001-RGMII
  97. * 010-SGMII
  98. * 100-RMII
  99. */
  100. #define MII_PHY_SEL_MASK GENMASK(4, 2)
  101. #define ETH_PHY_SEL_RMII BIT(4)
  102. #define ETH_PHY_SEL_SGMII BIT(3)
  103. #define ETH_PHY_SEL_RGMII BIT(2)
  104. #define ETH_PHY_SEL_GMII 0x0
  105. #define ETH_PHY_SEL_MII 0x0
  106. struct sti_dwmac {
  107. phy_interface_t interface; /* MII interface */
  108. bool ext_phyclk; /* Clock from external PHY */
  109. u32 tx_retime_src; /* TXCLK Retiming*/
  110. struct clk *clk; /* PHY clock */
  111. u32 ctrl_reg; /* GMAC glue-logic control register */
  112. int clk_sel_reg; /* GMAC ext clk selection register */
  113. struct regmap *regmap;
  114. bool gmac_en;
  115. u32 speed;
  116. void (*fix_retime_src)(void *priv, unsigned int speed);
  117. };
  118. struct sti_dwmac_of_data {
  119. void (*fix_retime_src)(void *priv, unsigned int speed);
  120. };
  121. static u32 phy_intf_sels[] = {
  122. [PHY_INTERFACE_MODE_MII] = ETH_PHY_SEL_MII,
  123. [PHY_INTERFACE_MODE_GMII] = ETH_PHY_SEL_GMII,
  124. [PHY_INTERFACE_MODE_RGMII] = ETH_PHY_SEL_RGMII,
  125. [PHY_INTERFACE_MODE_RGMII_ID] = ETH_PHY_SEL_RGMII,
  126. [PHY_INTERFACE_MODE_SGMII] = ETH_PHY_SEL_SGMII,
  127. [PHY_INTERFACE_MODE_RMII] = ETH_PHY_SEL_RMII,
  128. };
  129. enum {
  130. TX_RETIME_SRC_NA = 0,
  131. TX_RETIME_SRC_TXCLK = 1,
  132. TX_RETIME_SRC_CLK_125,
  133. TX_RETIME_SRC_PHYCLK,
  134. TX_RETIME_SRC_CLKGEN,
  135. };
  136. static u32 stih4xx_tx_retime_val[] = {
  137. [TX_RETIME_SRC_TXCLK] = STIH4XX_ETH_SEL_TXCLK_NOT_CLK125,
  138. [TX_RETIME_SRC_CLK_125] = 0x0,
  139. [TX_RETIME_SRC_PHYCLK] = STIH4XX_ETH_SEL_TX_RETIME_CLK,
  140. [TX_RETIME_SRC_CLKGEN] = STIH4XX_ETH_SEL_TX_RETIME_CLK
  141. | STIH4XX_ETH_SEL_INTERNAL_NOTEXT_PHYCLK,
  142. };
  143. static void stih4xx_fix_retime_src(void *priv, u32 spd)
  144. {
  145. struct sti_dwmac *dwmac = priv;
  146. u32 src = dwmac->tx_retime_src;
  147. u32 reg = dwmac->ctrl_reg;
  148. u32 freq = 0;
  149. if (dwmac->interface == PHY_INTERFACE_MODE_MII) {
  150. src = TX_RETIME_SRC_TXCLK;
  151. } else if (dwmac->interface == PHY_INTERFACE_MODE_RMII) {
  152. if (dwmac->ext_phyclk) {
  153. src = TX_RETIME_SRC_PHYCLK;
  154. } else {
  155. src = TX_RETIME_SRC_CLKGEN;
  156. freq = DWMAC_50MHZ;
  157. }
  158. } else if (IS_PHY_IF_MODE_RGMII(dwmac->interface)) {
  159. /* On GiGa clk source can be either ext or from clkgen */
  160. if (spd == SPEED_1000) {
  161. freq = DWMAC_125MHZ;
  162. } else {
  163. /* Switch to clkgen for these speeds */
  164. src = TX_RETIME_SRC_CLKGEN;
  165. if (spd == SPEED_100)
  166. freq = DWMAC_25MHZ;
  167. else if (spd == SPEED_10)
  168. freq = DWMAC_2_5MHZ;
  169. }
  170. }
  171. if (src == TX_RETIME_SRC_CLKGEN && freq)
  172. clk_set_rate(dwmac->clk, freq);
  173. regmap_update_bits(dwmac->regmap, reg, STIH4XX_RETIME_SRC_MASK,
  174. stih4xx_tx_retime_val[src]);
  175. }
  176. static void stid127_fix_retime_src(void *priv, u32 spd)
  177. {
  178. struct sti_dwmac *dwmac = priv;
  179. u32 reg = dwmac->ctrl_reg;
  180. u32 freq = 0;
  181. u32 val = 0;
  182. if (dwmac->interface == PHY_INTERFACE_MODE_MII) {
  183. val = STID127_ETH_SEL_INTERNAL_NOTEXT_TXCLK;
  184. } else if (dwmac->interface == PHY_INTERFACE_MODE_RMII) {
  185. if (!dwmac->ext_phyclk) {
  186. val = STID127_ETH_SEL_INTERNAL_NOTEXT_PHYCLK;
  187. freq = DWMAC_50MHZ;
  188. }
  189. } else if (IS_PHY_IF_MODE_RGMII(dwmac->interface)) {
  190. val = STID127_ETH_SEL_INTERNAL_NOTEXT_TXCLK;
  191. if (spd == SPEED_1000)
  192. freq = DWMAC_125MHZ;
  193. else if (spd == SPEED_100)
  194. freq = DWMAC_25MHZ;
  195. else if (spd == SPEED_10)
  196. freq = DWMAC_2_5MHZ;
  197. }
  198. if (freq)
  199. clk_set_rate(dwmac->clk, freq);
  200. regmap_update_bits(dwmac->regmap, reg, STID127_RETIME_SRC_MASK, val);
  201. }
  202. static int sti_dwmac_set_mode(struct sti_dwmac *dwmac)
  203. {
  204. struct regmap *regmap = dwmac->regmap;
  205. int iface = dwmac->interface;
  206. u32 reg = dwmac->ctrl_reg;
  207. u32 val;
  208. if (dwmac->gmac_en)
  209. regmap_update_bits(regmap, reg, EN_MASK, EN);
  210. regmap_update_bits(regmap, reg, MII_PHY_SEL_MASK, phy_intf_sels[iface]);
  211. val = (iface == PHY_INTERFACE_MODE_REVMII) ? 0 : ENMII;
  212. regmap_update_bits(regmap, reg, ENMII_MASK, val);
  213. dwmac->fix_retime_src(dwmac, dwmac->speed);
  214. return 0;
  215. }
  216. static int sti_dwmac_parse_data(struct sti_dwmac *dwmac,
  217. struct platform_device *pdev)
  218. {
  219. struct resource *res;
  220. struct device *dev = &pdev->dev;
  221. struct device_node *np = dev->of_node;
  222. struct regmap *regmap;
  223. int err;
  224. /* clk selection from extra syscfg register */
  225. dwmac->clk_sel_reg = -ENXIO;
  226. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sti-clkconf");
  227. if (res)
  228. dwmac->clk_sel_reg = res->start;
  229. regmap = syscon_regmap_lookup_by_phandle(np, "st,syscon");
  230. if (IS_ERR(regmap))
  231. return PTR_ERR(regmap);
  232. err = of_property_read_u32_index(np, "st,syscon", 1, &dwmac->ctrl_reg);
  233. if (err) {
  234. dev_err(dev, "Can't get sysconfig ctrl offset (%d)\n", err);
  235. return err;
  236. }
  237. err = of_get_phy_mode(np, &dwmac->interface);
  238. if (err && err != -ENODEV) {
  239. dev_err(dev, "Can't get phy-mode\n");
  240. return err;
  241. }
  242. dwmac->regmap = regmap;
  243. dwmac->gmac_en = of_property_read_bool(np, "st,gmac_en");
  244. dwmac->ext_phyclk = of_property_read_bool(np, "st,ext-phyclk");
  245. dwmac->tx_retime_src = TX_RETIME_SRC_NA;
  246. dwmac->speed = SPEED_100;
  247. if (IS_PHY_IF_MODE_GBIT(dwmac->interface)) {
  248. const char *rs;
  249. dwmac->tx_retime_src = TX_RETIME_SRC_CLKGEN;
  250. err = of_property_read_string(np, "st,tx-retime-src", &rs);
  251. if (err < 0) {
  252. dev_warn(dev, "Use internal clock source\n");
  253. } else {
  254. if (!strcasecmp(rs, "clk_125"))
  255. dwmac->tx_retime_src = TX_RETIME_SRC_CLK_125;
  256. else if (!strcasecmp(rs, "txclk"))
  257. dwmac->tx_retime_src = TX_RETIME_SRC_TXCLK;
  258. }
  259. dwmac->speed = SPEED_1000;
  260. }
  261. dwmac->clk = devm_clk_get(dev, "sti-ethclk");
  262. if (IS_ERR(dwmac->clk)) {
  263. dev_warn(dev, "No phy clock provided...\n");
  264. dwmac->clk = NULL;
  265. }
  266. return 0;
  267. }
  268. static int sti_dwmac_probe(struct platform_device *pdev)
  269. {
  270. struct plat_stmmacenet_data *plat_dat;
  271. const struct sti_dwmac_of_data *data;
  272. struct stmmac_resources stmmac_res;
  273. struct sti_dwmac *dwmac;
  274. int ret;
  275. data = of_device_get_match_data(&pdev->dev);
  276. if (!data) {
  277. dev_err(&pdev->dev, "No OF match data provided\n");
  278. return -EINVAL;
  279. }
  280. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  281. if (ret)
  282. return ret;
  283. plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
  284. if (IS_ERR(plat_dat))
  285. return PTR_ERR(plat_dat);
  286. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  287. if (!dwmac) {
  288. ret = -ENOMEM;
  289. goto err_remove_config_dt;
  290. }
  291. ret = sti_dwmac_parse_data(dwmac, pdev);
  292. if (ret) {
  293. dev_err(&pdev->dev, "Unable to parse OF data\n");
  294. goto err_remove_config_dt;
  295. }
  296. dwmac->fix_retime_src = data->fix_retime_src;
  297. plat_dat->bsp_priv = dwmac;
  298. plat_dat->fix_mac_speed = data->fix_retime_src;
  299. ret = clk_prepare_enable(dwmac->clk);
  300. if (ret)
  301. goto err_remove_config_dt;
  302. ret = sti_dwmac_set_mode(dwmac);
  303. if (ret)
  304. goto disable_clk;
  305. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  306. if (ret)
  307. goto disable_clk;
  308. return 0;
  309. disable_clk:
  310. clk_disable_unprepare(dwmac->clk);
  311. err_remove_config_dt:
  312. stmmac_remove_config_dt(pdev, plat_dat);
  313. return ret;
  314. }
  315. static int sti_dwmac_remove(struct platform_device *pdev)
  316. {
  317. struct sti_dwmac *dwmac = get_stmmac_bsp_priv(&pdev->dev);
  318. int ret = stmmac_dvr_remove(&pdev->dev);
  319. clk_disable_unprepare(dwmac->clk);
  320. return ret;
  321. }
  322. #ifdef CONFIG_PM_SLEEP
  323. static int sti_dwmac_suspend(struct device *dev)
  324. {
  325. struct sti_dwmac *dwmac = get_stmmac_bsp_priv(dev);
  326. int ret = stmmac_suspend(dev);
  327. clk_disable_unprepare(dwmac->clk);
  328. return ret;
  329. }
  330. static int sti_dwmac_resume(struct device *dev)
  331. {
  332. struct sti_dwmac *dwmac = get_stmmac_bsp_priv(dev);
  333. clk_prepare_enable(dwmac->clk);
  334. sti_dwmac_set_mode(dwmac);
  335. return stmmac_resume(dev);
  336. }
  337. #endif /* CONFIG_PM_SLEEP */
  338. static SIMPLE_DEV_PM_OPS(sti_dwmac_pm_ops, sti_dwmac_suspend,
  339. sti_dwmac_resume);
  340. static const struct sti_dwmac_of_data stih4xx_dwmac_data = {
  341. .fix_retime_src = stih4xx_fix_retime_src,
  342. };
  343. static const struct sti_dwmac_of_data stid127_dwmac_data = {
  344. .fix_retime_src = stid127_fix_retime_src,
  345. };
  346. static const struct of_device_id sti_dwmac_match[] = {
  347. { .compatible = "st,stih415-dwmac", .data = &stih4xx_dwmac_data},
  348. { .compatible = "st,stih416-dwmac", .data = &stih4xx_dwmac_data},
  349. { .compatible = "st,stid127-dwmac", .data = &stid127_dwmac_data},
  350. { .compatible = "st,stih407-dwmac", .data = &stih4xx_dwmac_data},
  351. { }
  352. };
  353. MODULE_DEVICE_TABLE(of, sti_dwmac_match);
  354. static struct platform_driver sti_dwmac_driver = {
  355. .probe = sti_dwmac_probe,
  356. .remove = sti_dwmac_remove,
  357. .driver = {
  358. .name = "sti-dwmac",
  359. .pm = &sti_dwmac_pm_ops,
  360. .of_match_table = sti_dwmac_match,
  361. },
  362. };
  363. module_platform_driver(sti_dwmac_driver);
  364. MODULE_AUTHOR("Srinivas Kandagatla <[email protected]>");
  365. MODULE_DESCRIPTION("STMicroelectronics DWMAC Specific Glue layer");
  366. MODULE_LICENSE("GPL");