dwmac-sti.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 device *dev;
  118. struct regmap *regmap;
  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 && dwmac->clk && 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 (dwmac->clk && 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_init(struct platform_device *pdev, void *priv)
  207. {
  208. struct sti_dwmac *dwmac = priv;
  209. struct regmap *regmap = dwmac->regmap;
  210. int iface = dwmac->interface;
  211. struct device *dev = dwmac->dev;
  212. struct device_node *np = dev->of_node;
  213. u32 reg = dwmac->ctrl_reg;
  214. u32 val;
  215. if (dwmac->clk)
  216. clk_prepare_enable(dwmac->clk);
  217. if (of_property_read_bool(np, "st,gmac_en"))
  218. regmap_update_bits(regmap, reg, EN_MASK, EN);
  219. regmap_update_bits(regmap, reg, MII_PHY_SEL_MASK, phy_intf_sels[iface]);
  220. val = (iface == PHY_INTERFACE_MODE_REVMII) ? 0 : ENMII;
  221. regmap_update_bits(regmap, reg, ENMII_MASK, val);
  222. dwmac->fix_retime_src(priv, dwmac->speed);
  223. return 0;
  224. }
  225. static void sti_dwmac_exit(struct platform_device *pdev, void *priv)
  226. {
  227. struct sti_dwmac *dwmac = priv;
  228. if (dwmac->clk)
  229. clk_disable_unprepare(dwmac->clk);
  230. }
  231. static int sti_dwmac_parse_data(struct sti_dwmac *dwmac,
  232. struct platform_device *pdev)
  233. {
  234. struct resource *res;
  235. struct device *dev = &pdev->dev;
  236. struct device_node *np = dev->of_node;
  237. struct regmap *regmap;
  238. int err;
  239. if (!np)
  240. return -EINVAL;
  241. /* clk selection from extra syscfg register */
  242. dwmac->clk_sel_reg = -ENXIO;
  243. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sti-clkconf");
  244. if (res)
  245. dwmac->clk_sel_reg = res->start;
  246. regmap = syscon_regmap_lookup_by_phandle(np, "st,syscon");
  247. if (IS_ERR(regmap))
  248. return PTR_ERR(regmap);
  249. err = of_property_read_u32_index(np, "st,syscon", 1, &dwmac->ctrl_reg);
  250. if (err) {
  251. dev_err(dev, "Can't get sysconfig ctrl offset (%d)\n", err);
  252. return err;
  253. }
  254. dwmac->dev = dev;
  255. dwmac->interface = of_get_phy_mode(np);
  256. dwmac->regmap = regmap;
  257. dwmac->ext_phyclk = of_property_read_bool(np, "st,ext-phyclk");
  258. dwmac->tx_retime_src = TX_RETIME_SRC_NA;
  259. dwmac->speed = SPEED_100;
  260. if (IS_PHY_IF_MODE_GBIT(dwmac->interface)) {
  261. const char *rs;
  262. dwmac->tx_retime_src = TX_RETIME_SRC_CLKGEN;
  263. err = of_property_read_string(np, "st,tx-retime-src", &rs);
  264. if (err < 0) {
  265. dev_warn(dev, "Use internal clock source\n");
  266. } else {
  267. if (!strcasecmp(rs, "clk_125"))
  268. dwmac->tx_retime_src = TX_RETIME_SRC_CLK_125;
  269. else if (!strcasecmp(rs, "txclk"))
  270. dwmac->tx_retime_src = TX_RETIME_SRC_TXCLK;
  271. }
  272. dwmac->speed = SPEED_1000;
  273. }
  274. dwmac->clk = devm_clk_get(dev, "sti-ethclk");
  275. if (IS_ERR(dwmac->clk)) {
  276. dev_warn(dev, "No phy clock provided...\n");
  277. dwmac->clk = NULL;
  278. }
  279. return 0;
  280. }
  281. static int sti_dwmac_probe(struct platform_device *pdev)
  282. {
  283. struct plat_stmmacenet_data *plat_dat;
  284. const struct sti_dwmac_of_data *data;
  285. struct stmmac_resources stmmac_res;
  286. struct sti_dwmac *dwmac;
  287. int ret;
  288. data = of_device_get_match_data(&pdev->dev);
  289. if (!data) {
  290. dev_err(&pdev->dev, "No OF match data provided\n");
  291. return -EINVAL;
  292. }
  293. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  294. if (ret)
  295. return ret;
  296. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  297. if (IS_ERR(plat_dat))
  298. return PTR_ERR(plat_dat);
  299. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  300. if (!dwmac)
  301. return -ENOMEM;
  302. ret = sti_dwmac_parse_data(dwmac, pdev);
  303. if (ret) {
  304. dev_err(&pdev->dev, "Unable to parse OF data\n");
  305. return ret;
  306. }
  307. dwmac->fix_retime_src = data->fix_retime_src;
  308. plat_dat->bsp_priv = dwmac;
  309. plat_dat->init = sti_dwmac_init;
  310. plat_dat->exit = sti_dwmac_exit;
  311. plat_dat->fix_mac_speed = data->fix_retime_src;
  312. ret = sti_dwmac_init(pdev, plat_dat->bsp_priv);
  313. if (ret)
  314. return ret;
  315. return stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  316. }
  317. static const struct sti_dwmac_of_data stih4xx_dwmac_data = {
  318. .fix_retime_src = stih4xx_fix_retime_src,
  319. };
  320. static const struct sti_dwmac_of_data stid127_dwmac_data = {
  321. .fix_retime_src = stid127_fix_retime_src,
  322. };
  323. static const struct of_device_id sti_dwmac_match[] = {
  324. { .compatible = "st,stih415-dwmac", .data = &stih4xx_dwmac_data},
  325. { .compatible = "st,stih416-dwmac", .data = &stih4xx_dwmac_data},
  326. { .compatible = "st,stid127-dwmac", .data = &stid127_dwmac_data},
  327. { .compatible = "st,stih407-dwmac", .data = &stih4xx_dwmac_data},
  328. { }
  329. };
  330. MODULE_DEVICE_TABLE(of, sti_dwmac_match);
  331. static struct platform_driver sti_dwmac_driver = {
  332. .probe = sti_dwmac_probe,
  333. .remove = stmmac_pltfr_remove,
  334. .driver = {
  335. .name = "sti-dwmac",
  336. .pm = &stmmac_pltfr_pm_ops,
  337. .of_match_table = sti_dwmac_match,
  338. },
  339. };
  340. module_platform_driver(sti_dwmac_driver);
  341. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@st.com>");
  342. MODULE_DESCRIPTION("STMicroelectronics DWMAC Specific Glue layer");
  343. MODULE_LICENSE("GPL");