dwmac-rk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /**
  2. * dwmac-rk.c - Rockchip RK3288 DWMAC specific glue layer
  3. *
  4. * Copyright (C) 2014 Chen-Zhi (Roger Chen)
  5. *
  6. * Chen-Zhi (Roger Chen) <roger.chen@rock-chips.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. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/stmmac.h>
  19. #include <linux/bitops.h>
  20. #include <linux/clk.h>
  21. #include <linux/phy.h>
  22. #include <linux/of_net.h>
  23. #include <linux/gpio.h>
  24. #include <linux/module.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/of_device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/delay.h>
  30. #include <linux/mfd/syscon.h>
  31. #include <linux/regmap.h>
  32. #include "stmmac_platform.h"
  33. struct rk_priv_data;
  34. struct rk_gmac_ops {
  35. void (*set_to_rgmii)(struct rk_priv_data *bsp_priv,
  36. int tx_delay, int rx_delay);
  37. void (*set_to_rmii)(struct rk_priv_data *bsp_priv);
  38. void (*set_rgmii_speed)(struct rk_priv_data *bsp_priv, int speed);
  39. void (*set_rmii_speed)(struct rk_priv_data *bsp_priv, int speed);
  40. };
  41. struct rk_priv_data {
  42. struct platform_device *pdev;
  43. int phy_iface;
  44. struct regulator *regulator;
  45. const struct rk_gmac_ops *ops;
  46. bool clk_enabled;
  47. bool clock_input;
  48. struct clk *clk_mac;
  49. struct clk *gmac_clkin;
  50. struct clk *mac_clk_rx;
  51. struct clk *mac_clk_tx;
  52. struct clk *clk_mac_ref;
  53. struct clk *clk_mac_refout;
  54. struct clk *aclk_mac;
  55. struct clk *pclk_mac;
  56. int tx_delay;
  57. int rx_delay;
  58. struct regmap *grf;
  59. };
  60. #define HIWORD_UPDATE(val, mask, shift) \
  61. ((val) << (shift) | (mask) << ((shift) + 16))
  62. #define GRF_BIT(nr) (BIT(nr) | BIT(nr+16))
  63. #define GRF_CLR_BIT(nr) (BIT(nr+16))
  64. #define RK3288_GRF_SOC_CON1 0x0248
  65. #define RK3288_GRF_SOC_CON3 0x0250
  66. /*RK3288_GRF_SOC_CON1*/
  67. #define RK3288_GMAC_PHY_INTF_SEL_RGMII (GRF_BIT(6) | GRF_CLR_BIT(7) | \
  68. GRF_CLR_BIT(8))
  69. #define RK3288_GMAC_PHY_INTF_SEL_RMII (GRF_CLR_BIT(6) | GRF_CLR_BIT(7) | \
  70. GRF_BIT(8))
  71. #define RK3288_GMAC_FLOW_CTRL GRF_BIT(9)
  72. #define RK3288_GMAC_FLOW_CTRL_CLR GRF_CLR_BIT(9)
  73. #define RK3288_GMAC_SPEED_10M GRF_CLR_BIT(10)
  74. #define RK3288_GMAC_SPEED_100M GRF_BIT(10)
  75. #define RK3288_GMAC_RMII_CLK_25M GRF_BIT(11)
  76. #define RK3288_GMAC_RMII_CLK_2_5M GRF_CLR_BIT(11)
  77. #define RK3288_GMAC_CLK_125M (GRF_CLR_BIT(12) | GRF_CLR_BIT(13))
  78. #define RK3288_GMAC_CLK_25M (GRF_BIT(12) | GRF_BIT(13))
  79. #define RK3288_GMAC_CLK_2_5M (GRF_CLR_BIT(12) | GRF_BIT(13))
  80. #define RK3288_GMAC_RMII_MODE GRF_BIT(14)
  81. #define RK3288_GMAC_RMII_MODE_CLR GRF_CLR_BIT(14)
  82. /*RK3288_GRF_SOC_CON3*/
  83. #define RK3288_GMAC_TXCLK_DLY_ENABLE GRF_BIT(14)
  84. #define RK3288_GMAC_TXCLK_DLY_DISABLE GRF_CLR_BIT(14)
  85. #define RK3288_GMAC_RXCLK_DLY_ENABLE GRF_BIT(15)
  86. #define RK3288_GMAC_RXCLK_DLY_DISABLE GRF_CLR_BIT(15)
  87. #define RK3288_GMAC_CLK_RX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 7)
  88. #define RK3288_GMAC_CLK_TX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 0)
  89. static void rk3288_set_to_rgmii(struct rk_priv_data *bsp_priv,
  90. int tx_delay, int rx_delay)
  91. {
  92. struct device *dev = &bsp_priv->pdev->dev;
  93. if (IS_ERR(bsp_priv->grf)) {
  94. dev_err(dev, "Missing rockchip,grf property\n");
  95. return;
  96. }
  97. regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
  98. RK3288_GMAC_PHY_INTF_SEL_RGMII |
  99. RK3288_GMAC_RMII_MODE_CLR);
  100. regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON3,
  101. RK3288_GMAC_RXCLK_DLY_ENABLE |
  102. RK3288_GMAC_TXCLK_DLY_ENABLE |
  103. RK3288_GMAC_CLK_RX_DL_CFG(rx_delay) |
  104. RK3288_GMAC_CLK_TX_DL_CFG(tx_delay));
  105. }
  106. static void rk3288_set_to_rmii(struct rk_priv_data *bsp_priv)
  107. {
  108. struct device *dev = &bsp_priv->pdev->dev;
  109. if (IS_ERR(bsp_priv->grf)) {
  110. dev_err(dev, "Missing rockchip,grf property\n");
  111. return;
  112. }
  113. regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
  114. RK3288_GMAC_PHY_INTF_SEL_RMII | RK3288_GMAC_RMII_MODE);
  115. }
  116. static void rk3288_set_rgmii_speed(struct rk_priv_data *bsp_priv, int speed)
  117. {
  118. struct device *dev = &bsp_priv->pdev->dev;
  119. if (IS_ERR(bsp_priv->grf)) {
  120. dev_err(dev, "Missing rockchip,grf property\n");
  121. return;
  122. }
  123. if (speed == 10)
  124. regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
  125. RK3288_GMAC_CLK_2_5M);
  126. else if (speed == 100)
  127. regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
  128. RK3288_GMAC_CLK_25M);
  129. else if (speed == 1000)
  130. regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
  131. RK3288_GMAC_CLK_125M);
  132. else
  133. dev_err(dev, "unknown speed value for RGMII! speed=%d", speed);
  134. }
  135. static void rk3288_set_rmii_speed(struct rk_priv_data *bsp_priv, int speed)
  136. {
  137. struct device *dev = &bsp_priv->pdev->dev;
  138. if (IS_ERR(bsp_priv->grf)) {
  139. dev_err(dev, "Missing rockchip,grf property\n");
  140. return;
  141. }
  142. if (speed == 10) {
  143. regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
  144. RK3288_GMAC_RMII_CLK_2_5M |
  145. RK3288_GMAC_SPEED_10M);
  146. } else if (speed == 100) {
  147. regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
  148. RK3288_GMAC_RMII_CLK_25M |
  149. RK3288_GMAC_SPEED_100M);
  150. } else {
  151. dev_err(dev, "unknown speed value for RMII! speed=%d", speed);
  152. }
  153. }
  154. static const struct rk_gmac_ops rk3288_ops = {
  155. .set_to_rgmii = rk3288_set_to_rgmii,
  156. .set_to_rmii = rk3288_set_to_rmii,
  157. .set_rgmii_speed = rk3288_set_rgmii_speed,
  158. .set_rmii_speed = rk3288_set_rmii_speed,
  159. };
  160. #define RK3368_GRF_SOC_CON15 0x043c
  161. #define RK3368_GRF_SOC_CON16 0x0440
  162. /* RK3368_GRF_SOC_CON15 */
  163. #define RK3368_GMAC_PHY_INTF_SEL_RGMII (GRF_BIT(9) | GRF_CLR_BIT(10) | \
  164. GRF_CLR_BIT(11))
  165. #define RK3368_GMAC_PHY_INTF_SEL_RMII (GRF_CLR_BIT(9) | GRF_CLR_BIT(10) | \
  166. GRF_BIT(11))
  167. #define RK3368_GMAC_FLOW_CTRL GRF_BIT(8)
  168. #define RK3368_GMAC_FLOW_CTRL_CLR GRF_CLR_BIT(8)
  169. #define RK3368_GMAC_SPEED_10M GRF_CLR_BIT(7)
  170. #define RK3368_GMAC_SPEED_100M GRF_BIT(7)
  171. #define RK3368_GMAC_RMII_CLK_25M GRF_BIT(3)
  172. #define RK3368_GMAC_RMII_CLK_2_5M GRF_CLR_BIT(3)
  173. #define RK3368_GMAC_CLK_125M (GRF_CLR_BIT(4) | GRF_CLR_BIT(5))
  174. #define RK3368_GMAC_CLK_25M (GRF_BIT(4) | GRF_BIT(5))
  175. #define RK3368_GMAC_CLK_2_5M (GRF_CLR_BIT(4) | GRF_BIT(5))
  176. #define RK3368_GMAC_RMII_MODE GRF_BIT(6)
  177. #define RK3368_GMAC_RMII_MODE_CLR GRF_CLR_BIT(6)
  178. /* RK3368_GRF_SOC_CON16 */
  179. #define RK3368_GMAC_TXCLK_DLY_ENABLE GRF_BIT(7)
  180. #define RK3368_GMAC_TXCLK_DLY_DISABLE GRF_CLR_BIT(7)
  181. #define RK3368_GMAC_RXCLK_DLY_ENABLE GRF_BIT(15)
  182. #define RK3368_GMAC_RXCLK_DLY_DISABLE GRF_CLR_BIT(15)
  183. #define RK3368_GMAC_CLK_RX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 8)
  184. #define RK3368_GMAC_CLK_TX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 0)
  185. static void rk3368_set_to_rgmii(struct rk_priv_data *bsp_priv,
  186. int tx_delay, int rx_delay)
  187. {
  188. struct device *dev = &bsp_priv->pdev->dev;
  189. if (IS_ERR(bsp_priv->grf)) {
  190. dev_err(dev, "%s: Missing rockchip,grf property\n", __func__);
  191. return;
  192. }
  193. regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15,
  194. RK3368_GMAC_PHY_INTF_SEL_RGMII |
  195. RK3368_GMAC_RMII_MODE_CLR);
  196. regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON16,
  197. RK3368_GMAC_RXCLK_DLY_ENABLE |
  198. RK3368_GMAC_TXCLK_DLY_ENABLE |
  199. RK3368_GMAC_CLK_RX_DL_CFG(rx_delay) |
  200. RK3368_GMAC_CLK_TX_DL_CFG(tx_delay));
  201. }
  202. static void rk3368_set_to_rmii(struct rk_priv_data *bsp_priv)
  203. {
  204. struct device *dev = &bsp_priv->pdev->dev;
  205. if (IS_ERR(bsp_priv->grf)) {
  206. dev_err(dev, "%s: Missing rockchip,grf property\n", __func__);
  207. return;
  208. }
  209. regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15,
  210. RK3368_GMAC_PHY_INTF_SEL_RMII | RK3368_GMAC_RMII_MODE);
  211. }
  212. static void rk3368_set_rgmii_speed(struct rk_priv_data *bsp_priv, int speed)
  213. {
  214. struct device *dev = &bsp_priv->pdev->dev;
  215. if (IS_ERR(bsp_priv->grf)) {
  216. dev_err(dev, "%s: Missing rockchip,grf property\n", __func__);
  217. return;
  218. }
  219. if (speed == 10)
  220. regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15,
  221. RK3368_GMAC_CLK_2_5M);
  222. else if (speed == 100)
  223. regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15,
  224. RK3368_GMAC_CLK_25M);
  225. else if (speed == 1000)
  226. regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15,
  227. RK3368_GMAC_CLK_125M);
  228. else
  229. dev_err(dev, "unknown speed value for RGMII! speed=%d", speed);
  230. }
  231. static void rk3368_set_rmii_speed(struct rk_priv_data *bsp_priv, int speed)
  232. {
  233. struct device *dev = &bsp_priv->pdev->dev;
  234. if (IS_ERR(bsp_priv->grf)) {
  235. dev_err(dev, "%s: Missing rockchip,grf property\n", __func__);
  236. return;
  237. }
  238. if (speed == 10) {
  239. regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15,
  240. RK3368_GMAC_RMII_CLK_2_5M |
  241. RK3368_GMAC_SPEED_10M);
  242. } else if (speed == 100) {
  243. regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15,
  244. RK3368_GMAC_RMII_CLK_25M |
  245. RK3368_GMAC_SPEED_100M);
  246. } else {
  247. dev_err(dev, "unknown speed value for RMII! speed=%d", speed);
  248. }
  249. }
  250. static const struct rk_gmac_ops rk3368_ops = {
  251. .set_to_rgmii = rk3368_set_to_rgmii,
  252. .set_to_rmii = rk3368_set_to_rmii,
  253. .set_rgmii_speed = rk3368_set_rgmii_speed,
  254. .set_rmii_speed = rk3368_set_rmii_speed,
  255. };
  256. static int gmac_clk_init(struct rk_priv_data *bsp_priv)
  257. {
  258. struct device *dev = &bsp_priv->pdev->dev;
  259. bsp_priv->clk_enabled = false;
  260. bsp_priv->mac_clk_rx = devm_clk_get(dev, "mac_clk_rx");
  261. if (IS_ERR(bsp_priv->mac_clk_rx))
  262. dev_err(dev, "cannot get clock %s\n",
  263. "mac_clk_rx");
  264. bsp_priv->mac_clk_tx = devm_clk_get(dev, "mac_clk_tx");
  265. if (IS_ERR(bsp_priv->mac_clk_tx))
  266. dev_err(dev, "cannot get clock %s\n",
  267. "mac_clk_tx");
  268. bsp_priv->aclk_mac = devm_clk_get(dev, "aclk_mac");
  269. if (IS_ERR(bsp_priv->aclk_mac))
  270. dev_err(dev, "cannot get clock %s\n",
  271. "aclk_mac");
  272. bsp_priv->pclk_mac = devm_clk_get(dev, "pclk_mac");
  273. if (IS_ERR(bsp_priv->pclk_mac))
  274. dev_err(dev, "cannot get clock %s\n",
  275. "pclk_mac");
  276. bsp_priv->clk_mac = devm_clk_get(dev, "stmmaceth");
  277. if (IS_ERR(bsp_priv->clk_mac))
  278. dev_err(dev, "cannot get clock %s\n",
  279. "stmmaceth");
  280. if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) {
  281. bsp_priv->clk_mac_ref = devm_clk_get(dev, "clk_mac_ref");
  282. if (IS_ERR(bsp_priv->clk_mac_ref))
  283. dev_err(dev, "cannot get clock %s\n",
  284. "clk_mac_ref");
  285. if (!bsp_priv->clock_input) {
  286. bsp_priv->clk_mac_refout =
  287. devm_clk_get(dev, "clk_mac_refout");
  288. if (IS_ERR(bsp_priv->clk_mac_refout))
  289. dev_err(dev, "cannot get clock %s\n",
  290. "clk_mac_refout");
  291. }
  292. }
  293. if (bsp_priv->clock_input) {
  294. dev_info(dev, "clock input from PHY\n");
  295. } else {
  296. if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII)
  297. clk_set_rate(bsp_priv->clk_mac, 50000000);
  298. }
  299. return 0;
  300. }
  301. static int gmac_clk_enable(struct rk_priv_data *bsp_priv, bool enable)
  302. {
  303. int phy_iface = bsp_priv->phy_iface;
  304. if (enable) {
  305. if (!bsp_priv->clk_enabled) {
  306. if (phy_iface == PHY_INTERFACE_MODE_RMII) {
  307. if (!IS_ERR(bsp_priv->mac_clk_rx))
  308. clk_prepare_enable(
  309. bsp_priv->mac_clk_rx);
  310. if (!IS_ERR(bsp_priv->clk_mac_ref))
  311. clk_prepare_enable(
  312. bsp_priv->clk_mac_ref);
  313. if (!IS_ERR(bsp_priv->clk_mac_refout))
  314. clk_prepare_enable(
  315. bsp_priv->clk_mac_refout);
  316. }
  317. if (!IS_ERR(bsp_priv->aclk_mac))
  318. clk_prepare_enable(bsp_priv->aclk_mac);
  319. if (!IS_ERR(bsp_priv->pclk_mac))
  320. clk_prepare_enable(bsp_priv->pclk_mac);
  321. if (!IS_ERR(bsp_priv->mac_clk_tx))
  322. clk_prepare_enable(bsp_priv->mac_clk_tx);
  323. /**
  324. * if (!IS_ERR(bsp_priv->clk_mac))
  325. * clk_prepare_enable(bsp_priv->clk_mac);
  326. */
  327. mdelay(5);
  328. bsp_priv->clk_enabled = true;
  329. }
  330. } else {
  331. if (bsp_priv->clk_enabled) {
  332. if (phy_iface == PHY_INTERFACE_MODE_RMII) {
  333. if (!IS_ERR(bsp_priv->mac_clk_rx))
  334. clk_disable_unprepare(
  335. bsp_priv->mac_clk_rx);
  336. if (!IS_ERR(bsp_priv->clk_mac_ref))
  337. clk_disable_unprepare(
  338. bsp_priv->clk_mac_ref);
  339. if (!IS_ERR(bsp_priv->clk_mac_refout))
  340. clk_disable_unprepare(
  341. bsp_priv->clk_mac_refout);
  342. }
  343. if (!IS_ERR(bsp_priv->aclk_mac))
  344. clk_disable_unprepare(bsp_priv->aclk_mac);
  345. if (!IS_ERR(bsp_priv->pclk_mac))
  346. clk_disable_unprepare(bsp_priv->pclk_mac);
  347. if (!IS_ERR(bsp_priv->mac_clk_tx))
  348. clk_disable_unprepare(bsp_priv->mac_clk_tx);
  349. /**
  350. * if (!IS_ERR(bsp_priv->clk_mac))
  351. * clk_disable_unprepare(bsp_priv->clk_mac);
  352. */
  353. bsp_priv->clk_enabled = false;
  354. }
  355. }
  356. return 0;
  357. }
  358. static int phy_power_on(struct rk_priv_data *bsp_priv, bool enable)
  359. {
  360. struct regulator *ldo = bsp_priv->regulator;
  361. int ret;
  362. struct device *dev = &bsp_priv->pdev->dev;
  363. if (!ldo) {
  364. dev_err(dev, "no regulator found\n");
  365. return -1;
  366. }
  367. if (enable) {
  368. ret = regulator_enable(ldo);
  369. if (ret)
  370. dev_err(dev, "fail to enable phy-supply\n");
  371. } else {
  372. ret = regulator_disable(ldo);
  373. if (ret)
  374. dev_err(dev, "fail to disable phy-supply\n");
  375. }
  376. return 0;
  377. }
  378. static struct rk_priv_data *rk_gmac_setup(struct platform_device *pdev,
  379. const struct rk_gmac_ops *ops)
  380. {
  381. struct rk_priv_data *bsp_priv;
  382. struct device *dev = &pdev->dev;
  383. int ret;
  384. const char *strings = NULL;
  385. int value;
  386. bsp_priv = devm_kzalloc(dev, sizeof(*bsp_priv), GFP_KERNEL);
  387. if (!bsp_priv)
  388. return ERR_PTR(-ENOMEM);
  389. bsp_priv->phy_iface = of_get_phy_mode(dev->of_node);
  390. bsp_priv->ops = ops;
  391. bsp_priv->regulator = devm_regulator_get_optional(dev, "phy");
  392. if (IS_ERR(bsp_priv->regulator)) {
  393. if (PTR_ERR(bsp_priv->regulator) == -EPROBE_DEFER) {
  394. dev_err(dev, "phy regulator is not available yet, deferred probing\n");
  395. return ERR_PTR(-EPROBE_DEFER);
  396. }
  397. dev_err(dev, "no regulator found\n");
  398. bsp_priv->regulator = NULL;
  399. }
  400. ret = of_property_read_string(dev->of_node, "clock_in_out", &strings);
  401. if (ret) {
  402. dev_err(dev, "Can not read property: clock_in_out.\n");
  403. bsp_priv->clock_input = true;
  404. } else {
  405. dev_info(dev, "clock input or output? (%s).\n",
  406. strings);
  407. if (!strcmp(strings, "input"))
  408. bsp_priv->clock_input = true;
  409. else
  410. bsp_priv->clock_input = false;
  411. }
  412. ret = of_property_read_u32(dev->of_node, "tx_delay", &value);
  413. if (ret) {
  414. bsp_priv->tx_delay = 0x30;
  415. dev_err(dev, "Can not read property: tx_delay.");
  416. dev_err(dev, "set tx_delay to 0x%x\n",
  417. bsp_priv->tx_delay);
  418. } else {
  419. dev_info(dev, "TX delay(0x%x).\n", value);
  420. bsp_priv->tx_delay = value;
  421. }
  422. ret = of_property_read_u32(dev->of_node, "rx_delay", &value);
  423. if (ret) {
  424. bsp_priv->rx_delay = 0x10;
  425. dev_err(dev, "Can not read property: rx_delay.");
  426. dev_err(dev, "set rx_delay to 0x%x\n",
  427. bsp_priv->rx_delay);
  428. } else {
  429. dev_info(dev, "RX delay(0x%x).\n", value);
  430. bsp_priv->rx_delay = value;
  431. }
  432. bsp_priv->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
  433. "rockchip,grf");
  434. bsp_priv->pdev = pdev;
  435. /*rmii or rgmii*/
  436. if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RGMII) {
  437. dev_info(dev, "init for RGMII\n");
  438. bsp_priv->ops->set_to_rgmii(bsp_priv, bsp_priv->tx_delay,
  439. bsp_priv->rx_delay);
  440. } else if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) {
  441. dev_info(dev, "init for RMII\n");
  442. bsp_priv->ops->set_to_rmii(bsp_priv);
  443. } else {
  444. dev_err(dev, "NO interface defined!\n");
  445. }
  446. gmac_clk_init(bsp_priv);
  447. return bsp_priv;
  448. }
  449. static int rk_gmac_init(struct platform_device *pdev, void *priv)
  450. {
  451. struct rk_priv_data *bsp_priv = priv;
  452. int ret;
  453. ret = phy_power_on(bsp_priv, true);
  454. if (ret) {
  455. gmac_clk_enable(bsp_priv, false);
  456. return ret;
  457. }
  458. ret = gmac_clk_enable(bsp_priv, true);
  459. if (ret)
  460. return ret;
  461. return 0;
  462. }
  463. static void rk_gmac_exit(struct platform_device *pdev, void *priv)
  464. {
  465. struct rk_priv_data *gmac = priv;
  466. phy_power_on(gmac, false);
  467. gmac_clk_enable(gmac, false);
  468. }
  469. static void rk_fix_speed(void *priv, unsigned int speed)
  470. {
  471. struct rk_priv_data *bsp_priv = priv;
  472. struct device *dev = &bsp_priv->pdev->dev;
  473. if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RGMII)
  474. bsp_priv->ops->set_rgmii_speed(bsp_priv, speed);
  475. else if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII)
  476. bsp_priv->ops->set_rmii_speed(bsp_priv, speed);
  477. else
  478. dev_err(dev, "unsupported interface %d", bsp_priv->phy_iface);
  479. }
  480. static int rk_gmac_probe(struct platform_device *pdev)
  481. {
  482. struct plat_stmmacenet_data *plat_dat;
  483. struct stmmac_resources stmmac_res;
  484. const struct rk_gmac_ops *data;
  485. int ret;
  486. data = of_device_get_match_data(&pdev->dev);
  487. if (!data) {
  488. dev_err(&pdev->dev, "no of match data provided\n");
  489. return -EINVAL;
  490. }
  491. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  492. if (ret)
  493. return ret;
  494. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  495. if (IS_ERR(plat_dat))
  496. return PTR_ERR(plat_dat);
  497. plat_dat->has_gmac = true;
  498. plat_dat->init = rk_gmac_init;
  499. plat_dat->exit = rk_gmac_exit;
  500. plat_dat->fix_mac_speed = rk_fix_speed;
  501. plat_dat->bsp_priv = rk_gmac_setup(pdev, data);
  502. if (IS_ERR(plat_dat->bsp_priv))
  503. return PTR_ERR(plat_dat->bsp_priv);
  504. ret = rk_gmac_init(pdev, plat_dat->bsp_priv);
  505. if (ret)
  506. return ret;
  507. return stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  508. }
  509. static const struct of_device_id rk_gmac_dwmac_match[] = {
  510. { .compatible = "rockchip,rk3288-gmac", .data = &rk3288_ops },
  511. { .compatible = "rockchip,rk3368-gmac", .data = &rk3368_ops },
  512. { }
  513. };
  514. MODULE_DEVICE_TABLE(of, rk_gmac_dwmac_match);
  515. static struct platform_driver rk_gmac_dwmac_driver = {
  516. .probe = rk_gmac_probe,
  517. .remove = stmmac_pltfr_remove,
  518. .driver = {
  519. .name = "rk_gmac-dwmac",
  520. .pm = &stmmac_pltfr_pm_ops,
  521. .of_match_table = rk_gmac_dwmac_match,
  522. },
  523. };
  524. module_platform_driver(rk_gmac_dwmac_driver);
  525. MODULE_AUTHOR("Chen-Zhi (Roger Chen) <roger.chen@rock-chips.com>");
  526. MODULE_DESCRIPTION("Rockchip RK3288 DWMAC specific glue layer");
  527. MODULE_LICENSE("GPL");