dwmac-sti.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. *
  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/regmap.h>
  20. #include <linux/clk.h>
  21. #include <linux/of.h>
  22. #include <linux/of_net.h>
  23. /**
  24. * STi GMAC glue logic.
  25. * --------------------
  26. *
  27. * _
  28. * | \
  29. * --------|0 \ ETH_SEL_INTERNAL_NOTEXT_PHYCLK
  30. * phyclk | |___________________________________________
  31. * | | | (phyclk-in)
  32. * --------|1 / |
  33. * int-clk |_ / |
  34. * | _
  35. * | | \
  36. * |_______|1 \ ETH_SEL_TX_RETIME_CLK
  37. * | |___________________________
  38. * | | (tx-retime-clk)
  39. * _______|0 /
  40. * | |_ /
  41. * _ |
  42. * | \ |
  43. * --------|0 \ |
  44. * clk_125 | |__|
  45. * | | ETH_SEL_TXCLK_NOT_CLK125
  46. * --------|1 /
  47. * txclk |_ /
  48. *
  49. *
  50. * ETH_SEL_INTERNAL_NOTEXT_PHYCLK is valid only for RMII where PHY can
  51. * generate 50MHz clock or MAC can generate it.
  52. * This bit is configured by "st,ext-phyclk" property.
  53. *
  54. * ETH_SEL_TXCLK_NOT_CLK125 is only valid for gigabit modes, where the 125Mhz
  55. * clock either comes from clk-125 pin or txclk pin. This configuration is
  56. * totally driven by the board wiring. This bit is configured by
  57. * "st,tx-retime-src" property.
  58. *
  59. * TXCLK configuration is different for different phy interface modes
  60. * and changes according to link speed in modes like RGMII.
  61. *
  62. * Below table summarizes the clock requirement and clock sources for
  63. * supported phy interface modes with link speeds.
  64. * ________________________________________________
  65. *| PHY_MODE | 1000 Mbit Link | 100 Mbit Link |
  66. * ------------------------------------------------
  67. *| MII | n/a | 25Mhz |
  68. *| | | txclk |
  69. * ------------------------------------------------
  70. *| GMII | 125Mhz | 25Mhz |
  71. *| | clk-125/txclk | txclk |
  72. * ------------------------------------------------
  73. *| RGMII | 125Mhz | 25Mhz |
  74. *| | clk-125/txclk | clkgen |
  75. * ------------------------------------------------
  76. *| RMII | n/a | 25Mhz |
  77. *| | |clkgen/phyclk-in |
  78. * ------------------------------------------------
  79. *
  80. * TX lines are always retimed with a clk, which can vary depending
  81. * on the board configuration. Below is the table of these bits
  82. * in eth configuration register depending on source of retime clk.
  83. *
  84. *---------------------------------------------------------------
  85. * src | tx_rt_clk | int_not_ext_phyclk | txclk_n_clk125|
  86. *---------------------------------------------------------------
  87. * txclk | 0 | n/a | 1 |
  88. *---------------------------------------------------------------
  89. * ck_125| 0 | n/a | 0 |
  90. *---------------------------------------------------------------
  91. * phyclk| 1 | 0 | n/a |
  92. *---------------------------------------------------------------
  93. * clkgen| 1 | 1 | n/a |
  94. *---------------------------------------------------------------
  95. */
  96. /* Register definition */
  97. /* 3 bits [8:6]
  98. * [6:6] ETH_SEL_TXCLK_NOT_CLK125
  99. * [7:7] ETH_SEL_INTERNAL_NOTEXT_PHYCLK
  100. * [8:8] ETH_SEL_TX_RETIME_CLK
  101. *
  102. */
  103. #define TX_RETIME_SRC_MASK GENMASK(8, 6)
  104. #define ETH_SEL_TX_RETIME_CLK BIT(8)
  105. #define ETH_SEL_INTERNAL_NOTEXT_PHYCLK BIT(7)
  106. #define ETH_SEL_TXCLK_NOT_CLK125 BIT(6)
  107. #define ENMII_MASK GENMASK(5, 5)
  108. #define ENMII BIT(5)
  109. /**
  110. * 3 bits [4:2]
  111. * 000-GMII/MII
  112. * 001-RGMII
  113. * 010-SGMII
  114. * 100-RMII
  115. */
  116. #define MII_PHY_SEL_MASK GENMASK(4, 2)
  117. #define ETH_PHY_SEL_RMII BIT(4)
  118. #define ETH_PHY_SEL_SGMII BIT(3)
  119. #define ETH_PHY_SEL_RGMII BIT(2)
  120. #define ETH_PHY_SEL_GMII 0x0
  121. #define ETH_PHY_SEL_MII 0x0
  122. #define IS_PHY_IF_MODE_RGMII(iface) (iface == PHY_INTERFACE_MODE_RGMII || \
  123. iface == PHY_INTERFACE_MODE_RGMII_ID || \
  124. iface == PHY_INTERFACE_MODE_RGMII_RXID || \
  125. iface == PHY_INTERFACE_MODE_RGMII_TXID)
  126. #define IS_PHY_IF_MODE_GBIT(iface) (IS_PHY_IF_MODE_RGMII(iface) || \
  127. iface == PHY_INTERFACE_MODE_GMII)
  128. struct sti_dwmac {
  129. int interface;
  130. bool ext_phyclk;
  131. bool is_tx_retime_src_clk_125;
  132. struct clk *clk;
  133. int reg;
  134. struct device *dev;
  135. struct regmap *regmap;
  136. };
  137. static u32 phy_intf_sels[] = {
  138. [PHY_INTERFACE_MODE_MII] = ETH_PHY_SEL_MII,
  139. [PHY_INTERFACE_MODE_GMII] = ETH_PHY_SEL_GMII,
  140. [PHY_INTERFACE_MODE_RGMII] = ETH_PHY_SEL_RGMII,
  141. [PHY_INTERFACE_MODE_RGMII_ID] = ETH_PHY_SEL_RGMII,
  142. [PHY_INTERFACE_MODE_SGMII] = ETH_PHY_SEL_SGMII,
  143. [PHY_INTERFACE_MODE_RMII] = ETH_PHY_SEL_RMII,
  144. };
  145. enum {
  146. TX_RETIME_SRC_NA = 0,
  147. TX_RETIME_SRC_TXCLK = 1,
  148. TX_RETIME_SRC_CLK_125,
  149. TX_RETIME_SRC_PHYCLK,
  150. TX_RETIME_SRC_CLKGEN,
  151. };
  152. static const char *const tx_retime_srcs[] = {
  153. [TX_RETIME_SRC_NA] = "",
  154. [TX_RETIME_SRC_TXCLK] = "txclk",
  155. [TX_RETIME_SRC_CLK_125] = "clk_125",
  156. [TX_RETIME_SRC_PHYCLK] = "phyclk",
  157. [TX_RETIME_SRC_CLKGEN] = "clkgen",
  158. };
  159. static u32 tx_retime_val[] = {
  160. [TX_RETIME_SRC_TXCLK] = ETH_SEL_TXCLK_NOT_CLK125,
  161. [TX_RETIME_SRC_CLK_125] = 0x0,
  162. [TX_RETIME_SRC_PHYCLK] = ETH_SEL_TX_RETIME_CLK,
  163. [TX_RETIME_SRC_CLKGEN] = ETH_SEL_TX_RETIME_CLK |
  164. ETH_SEL_INTERNAL_NOTEXT_PHYCLK,
  165. };
  166. static void setup_retime_src(struct sti_dwmac *dwmac, u32 spd)
  167. {
  168. u32 src = 0, freq = 0;
  169. if (spd == SPEED_100) {
  170. if (dwmac->interface == PHY_INTERFACE_MODE_MII ||
  171. dwmac->interface == PHY_INTERFACE_MODE_GMII) {
  172. src = TX_RETIME_SRC_TXCLK;
  173. } else if (dwmac->interface == PHY_INTERFACE_MODE_RMII) {
  174. if (dwmac->ext_phyclk) {
  175. src = TX_RETIME_SRC_PHYCLK;
  176. } else {
  177. src = TX_RETIME_SRC_CLKGEN;
  178. freq = 50000000;
  179. }
  180. } else if (IS_PHY_IF_MODE_RGMII(dwmac->interface)) {
  181. src = TX_RETIME_SRC_CLKGEN;
  182. freq = 25000000;
  183. }
  184. if (src == TX_RETIME_SRC_CLKGEN && dwmac->clk)
  185. clk_set_rate(dwmac->clk, freq);
  186. } else if (spd == SPEED_1000) {
  187. if (dwmac->is_tx_retime_src_clk_125)
  188. src = TX_RETIME_SRC_CLK_125;
  189. else
  190. src = TX_RETIME_SRC_TXCLK;
  191. }
  192. regmap_update_bits(dwmac->regmap, dwmac->reg,
  193. TX_RETIME_SRC_MASK, tx_retime_val[src]);
  194. }
  195. static void sti_dwmac_exit(struct platform_device *pdev, void *priv)
  196. {
  197. struct sti_dwmac *dwmac = priv;
  198. if (dwmac->clk)
  199. clk_disable_unprepare(dwmac->clk);
  200. }
  201. static void sti_fix_mac_speed(void *priv, unsigned int spd)
  202. {
  203. struct sti_dwmac *dwmac = priv;
  204. setup_retime_src(dwmac, spd);
  205. return;
  206. }
  207. static int sti_dwmac_parse_data(struct sti_dwmac *dwmac,
  208. struct platform_device *pdev)
  209. {
  210. struct resource *res;
  211. struct device *dev = &pdev->dev;
  212. struct device_node *np = dev->of_node;
  213. struct regmap *regmap;
  214. int err;
  215. if (!np)
  216. return -EINVAL;
  217. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sti-ethconf");
  218. if (!res)
  219. return -ENODATA;
  220. regmap = syscon_regmap_lookup_by_phandle(np, "st,syscon");
  221. if (IS_ERR(regmap))
  222. return PTR_ERR(regmap);
  223. dwmac->dev = dev;
  224. dwmac->interface = of_get_phy_mode(np);
  225. dwmac->regmap = regmap;
  226. dwmac->reg = res->start;
  227. dwmac->ext_phyclk = of_property_read_bool(np, "st,ext-phyclk");
  228. dwmac->is_tx_retime_src_clk_125 = false;
  229. if (IS_PHY_IF_MODE_GBIT(dwmac->interface)) {
  230. const char *rs;
  231. err = of_property_read_string(np, "st,tx-retime-src", &rs);
  232. if (err < 0) {
  233. dev_err(dev, "st,tx-retime-src not specified\n");
  234. return err;
  235. }
  236. if (!strcasecmp(rs, "clk_125"))
  237. dwmac->is_tx_retime_src_clk_125 = true;
  238. }
  239. dwmac->clk = devm_clk_get(dev, "sti-ethclk");
  240. if (IS_ERR(dwmac->clk))
  241. dwmac->clk = NULL;
  242. return 0;
  243. }
  244. static int sti_dwmac_init(struct platform_device *pdev, void *priv)
  245. {
  246. struct sti_dwmac *dwmac = priv;
  247. struct regmap *regmap = dwmac->regmap;
  248. int iface = dwmac->interface;
  249. u32 reg = dwmac->reg;
  250. u32 val, spd;
  251. if (dwmac->clk)
  252. clk_prepare_enable(dwmac->clk);
  253. regmap_update_bits(regmap, reg, MII_PHY_SEL_MASK, phy_intf_sels[iface]);
  254. val = (iface == PHY_INTERFACE_MODE_REVMII) ? 0 : ENMII;
  255. regmap_update_bits(regmap, reg, ENMII_MASK, val);
  256. if (IS_PHY_IF_MODE_GBIT(iface))
  257. spd = SPEED_1000;
  258. else
  259. spd = SPEED_100;
  260. setup_retime_src(dwmac, spd);
  261. return 0;
  262. }
  263. static void *sti_dwmac_setup(struct platform_device *pdev)
  264. {
  265. struct sti_dwmac *dwmac;
  266. int ret;
  267. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  268. if (!dwmac)
  269. return ERR_PTR(-ENOMEM);
  270. ret = sti_dwmac_parse_data(dwmac, pdev);
  271. if (ret) {
  272. dev_err(&pdev->dev, "Unable to parse OF data\n");
  273. return ERR_PTR(ret);
  274. }
  275. return dwmac;
  276. }
  277. const struct stmmac_of_data sti_gmac_data = {
  278. .fix_mac_speed = sti_fix_mac_speed,
  279. .setup = sti_dwmac_setup,
  280. .init = sti_dwmac_init,
  281. .exit = sti_dwmac_exit,
  282. };