dwmac-sti.c 10 KB

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