dwmac-sti.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * dwmac-sti.c - STMicroelectronics DWMAC Specific Glue layer
  3. *
  4. * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
  5. * Author: Srinivas Kandagatla <srinivas.kandagatla@st.com>
  6. * Contributors: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/stmmac.h>
  17. #include <linux/phy.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/module.h>
  20. #include <linux/regmap.h>
  21. #include <linux/clk.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_net.h>
  25. #include "stmmac_platform.h"
  26. #define DWMAC_125MHZ 125000000
  27. #define DWMAC_50MHZ 50000000
  28. #define DWMAC_25MHZ 25000000
  29. #define DWMAC_2_5MHZ 2500000
  30. #define IS_PHY_IF_MODE_RGMII(iface) (iface == PHY_INTERFACE_MODE_RGMII || \
  31. iface == PHY_INTERFACE_MODE_RGMII_ID || \
  32. iface == PHY_INTERFACE_MODE_RGMII_RXID || \
  33. iface == PHY_INTERFACE_MODE_RGMII_TXID)
  34. #define IS_PHY_IF_MODE_GBIT(iface) (IS_PHY_IF_MODE_RGMII(iface) || \
  35. iface == PHY_INTERFACE_MODE_GMII)
  36. /* STiH4xx register definitions (STiH415/STiH416/STiH407/STiH410 families)
  37. *
  38. * Below table summarizes the clock requirement and clock sources for
  39. * supported phy interface modes with link speeds.
  40. * ________________________________________________
  41. *| PHY_MODE | 1000 Mbit Link | 100 Mbit Link |
  42. * ------------------------------------------------
  43. *| MII | n/a | 25Mhz |
  44. *| | | txclk |
  45. * ------------------------------------------------
  46. *| GMII | 125Mhz | 25Mhz |
  47. *| | clk-125/txclk | txclk |
  48. * ------------------------------------------------
  49. *| RGMII | 125Mhz | 25Mhz |
  50. *| | clk-125/txclk | clkgen |
  51. *| | clkgen | |
  52. * ------------------------------------------------
  53. *| RMII | n/a | 25Mhz |
  54. *| | |clkgen/phyclk-in |
  55. * ------------------------------------------------
  56. *
  57. * Register Configuration
  58. *-------------------------------
  59. * src |BIT(8)| BIT(7)| BIT(6)|
  60. *-------------------------------
  61. * txclk | 0 | n/a | 1 |
  62. *-------------------------------
  63. * ck_125| 0 | n/a | 0 |
  64. *-------------------------------
  65. * phyclk| 1 | 0 | n/a |
  66. *-------------------------------
  67. * clkgen| 1 | 1 | n/a |
  68. *-------------------------------
  69. */
  70. #define STIH4XX_RETIME_SRC_MASK GENMASK(8, 6)
  71. #define STIH4XX_ETH_SEL_TX_RETIME_CLK BIT(8)
  72. #define STIH4XX_ETH_SEL_INTERNAL_NOTEXT_PHYCLK BIT(7)
  73. #define STIH4XX_ETH_SEL_TXCLK_NOT_CLK125 BIT(6)
  74. /* STiD127 register definitions
  75. *-----------------------
  76. * src |BIT(6)| BIT(7)|
  77. *-----------------------
  78. * MII | 1 | n/a |
  79. *-----------------------
  80. * RMII | n/a | 1 |
  81. * clkgen| | |
  82. *-----------------------
  83. * RMII | n/a | 0 |
  84. * phyclk| | |
  85. *-----------------------
  86. * RGMII | 1 | n/a |
  87. * clkgen| | |
  88. *-----------------------
  89. */
  90. #define STID127_RETIME_SRC_MASK GENMASK(7, 6)
  91. #define STID127_ETH_SEL_INTERNAL_NOTEXT_PHYCLK BIT(7)
  92. #define STID127_ETH_SEL_INTERNAL_NOTEXT_TXCLK BIT(6)
  93. #define ENMII_MASK GENMASK(5, 5)
  94. #define ENMII BIT(5)
  95. #define EN_MASK GENMASK(1, 1)
  96. #define EN BIT(1)
  97. /*
  98. * 3 bits [4:2]
  99. * 000-GMII/MII
  100. * 001-RGMII
  101. * 010-SGMII
  102. * 100-RMII
  103. */
  104. #define MII_PHY_SEL_MASK GENMASK(4, 2)
  105. #define ETH_PHY_SEL_RMII BIT(4)
  106. #define ETH_PHY_SEL_SGMII BIT(3)
  107. #define ETH_PHY_SEL_RGMII BIT(2)
  108. #define ETH_PHY_SEL_GMII 0x0
  109. #define ETH_PHY_SEL_MII 0x0
  110. struct sti_dwmac {
  111. int interface; /* MII interface */
  112. bool ext_phyclk; /* Clock from external PHY */
  113. u32 tx_retime_src; /* TXCLK Retiming*/
  114. struct clk *clk; /* PHY clock */
  115. u32 ctrl_reg; /* GMAC glue-logic control register */
  116. int clk_sel_reg; /* GMAC ext clk selection register */
  117. struct regmap *regmap;
  118. bool gmac_en;
  119. u32 speed;
  120. void (*fix_retime_src)(void *priv, unsigned int speed);
  121. };
  122. struct sti_dwmac_of_data {
  123. void (*fix_retime_src)(void *priv, unsigned int speed);
  124. };
  125. static u32 phy_intf_sels[] = {
  126. [PHY_INTERFACE_MODE_MII] = ETH_PHY_SEL_MII,
  127. [PHY_INTERFACE_MODE_GMII] = ETH_PHY_SEL_GMII,
  128. [PHY_INTERFACE_MODE_RGMII] = ETH_PHY_SEL_RGMII,
  129. [PHY_INTERFACE_MODE_RGMII_ID] = ETH_PHY_SEL_RGMII,
  130. [PHY_INTERFACE_MODE_SGMII] = ETH_PHY_SEL_SGMII,
  131. [PHY_INTERFACE_MODE_RMII] = ETH_PHY_SEL_RMII,
  132. };
  133. enum {
  134. TX_RETIME_SRC_NA = 0,
  135. TX_RETIME_SRC_TXCLK = 1,
  136. TX_RETIME_SRC_CLK_125,
  137. TX_RETIME_SRC_PHYCLK,
  138. TX_RETIME_SRC_CLKGEN,
  139. };
  140. static u32 stih4xx_tx_retime_val[] = {
  141. [TX_RETIME_SRC_TXCLK] = STIH4XX_ETH_SEL_TXCLK_NOT_CLK125,
  142. [TX_RETIME_SRC_CLK_125] = 0x0,
  143. [TX_RETIME_SRC_PHYCLK] = STIH4XX_ETH_SEL_TX_RETIME_CLK,
  144. [TX_RETIME_SRC_CLKGEN] = STIH4XX_ETH_SEL_TX_RETIME_CLK
  145. | STIH4XX_ETH_SEL_INTERNAL_NOTEXT_PHYCLK,
  146. };
  147. static void stih4xx_fix_retime_src(void *priv, u32 spd)
  148. {
  149. struct sti_dwmac *dwmac = priv;
  150. u32 src = dwmac->tx_retime_src;
  151. u32 reg = dwmac->ctrl_reg;
  152. u32 freq = 0;
  153. if (dwmac->interface == PHY_INTERFACE_MODE_MII) {
  154. src = TX_RETIME_SRC_TXCLK;
  155. } else if (dwmac->interface == PHY_INTERFACE_MODE_RMII) {
  156. if (dwmac->ext_phyclk) {
  157. src = TX_RETIME_SRC_PHYCLK;
  158. } else {
  159. src = TX_RETIME_SRC_CLKGEN;
  160. freq = DWMAC_50MHZ;
  161. }
  162. } else if (IS_PHY_IF_MODE_RGMII(dwmac->interface)) {
  163. /* On GiGa clk source can be either ext or from clkgen */
  164. if (spd == SPEED_1000) {
  165. freq = DWMAC_125MHZ;
  166. } else {
  167. /* Switch to clkgen for these speeds */
  168. src = TX_RETIME_SRC_CLKGEN;
  169. if (spd == SPEED_100)
  170. freq = DWMAC_25MHZ;
  171. else if (spd == SPEED_10)
  172. freq = DWMAC_2_5MHZ;
  173. }
  174. }
  175. if (src == TX_RETIME_SRC_CLKGEN && freq)
  176. clk_set_rate(dwmac->clk, freq);
  177. regmap_update_bits(dwmac->regmap, reg, STIH4XX_RETIME_SRC_MASK,
  178. stih4xx_tx_retime_val[src]);
  179. }
  180. static void stid127_fix_retime_src(void *priv, u32 spd)
  181. {
  182. struct sti_dwmac *dwmac = priv;
  183. u32 reg = dwmac->ctrl_reg;
  184. u32 freq = 0;
  185. u32 val = 0;
  186. if (dwmac->interface == PHY_INTERFACE_MODE_MII) {
  187. val = STID127_ETH_SEL_INTERNAL_NOTEXT_TXCLK;
  188. } else if (dwmac->interface == PHY_INTERFACE_MODE_RMII) {
  189. if (!dwmac->ext_phyclk) {
  190. val = STID127_ETH_SEL_INTERNAL_NOTEXT_PHYCLK;
  191. freq = DWMAC_50MHZ;
  192. }
  193. } else if (IS_PHY_IF_MODE_RGMII(dwmac->interface)) {
  194. val = STID127_ETH_SEL_INTERNAL_NOTEXT_TXCLK;
  195. if (spd == SPEED_1000)
  196. freq = DWMAC_125MHZ;
  197. else if (spd == SPEED_100)
  198. freq = DWMAC_25MHZ;
  199. else if (spd == SPEED_10)
  200. freq = DWMAC_2_5MHZ;
  201. }
  202. if (freq)
  203. clk_set_rate(dwmac->clk, freq);
  204. regmap_update_bits(dwmac->regmap, reg, STID127_RETIME_SRC_MASK, val);
  205. }
  206. static int sti_dwmac_set_mode(struct sti_dwmac *dwmac)
  207. {
  208. struct regmap *regmap = dwmac->regmap;
  209. int iface = dwmac->interface;
  210. u32 reg = dwmac->ctrl_reg;
  211. u32 val;
  212. if (dwmac->gmac_en)
  213. regmap_update_bits(regmap, reg, EN_MASK, EN);
  214. regmap_update_bits(regmap, reg, MII_PHY_SEL_MASK, phy_intf_sels[iface]);
  215. val = (iface == PHY_INTERFACE_MODE_REVMII) ? 0 : ENMII;
  216. regmap_update_bits(regmap, reg, ENMII_MASK, val);
  217. dwmac->fix_retime_src(dwmac, dwmac->speed);
  218. return 0;
  219. }
  220. static int sti_dwmac_parse_data(struct sti_dwmac *dwmac,
  221. struct platform_device *pdev)
  222. {
  223. struct resource *res;
  224. struct device *dev = &pdev->dev;
  225. struct device_node *np = dev->of_node;
  226. struct regmap *regmap;
  227. int err;
  228. /* clk selection from extra syscfg register */
  229. dwmac->clk_sel_reg = -ENXIO;
  230. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sti-clkconf");
  231. if (res)
  232. dwmac->clk_sel_reg = res->start;
  233. regmap = syscon_regmap_lookup_by_phandle(np, "st,syscon");
  234. if (IS_ERR(regmap))
  235. return PTR_ERR(regmap);
  236. err = of_property_read_u32_index(np, "st,syscon", 1, &dwmac->ctrl_reg);
  237. if (err) {
  238. dev_err(dev, "Can't get sysconfig ctrl offset (%d)\n", err);
  239. return err;
  240. }
  241. dwmac->interface = of_get_phy_mode(np);
  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 <srinivas.kandagatla@st.com>");
  365. MODULE_DESCRIPTION("STMicroelectronics DWMAC Specific Glue layer");
  366. MODULE_LICENSE("GPL");