emac_rockchip.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * emac-rockchip.c - Rockchip EMAC specific glue layer
  3. *
  4. * Copyright (C) 2014 Romain Perier <romain.perier@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/etherdevice.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of_net.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/consumer.h>
  23. #include "emac.h"
  24. #define DRV_NAME "rockchip_emac"
  25. #define DRV_VERSION "1.1"
  26. struct emac_rockchip_soc_data {
  27. unsigned int grf_offset;
  28. unsigned int grf_mode_offset;
  29. unsigned int grf_speed_offset;
  30. bool need_div_macclk;
  31. };
  32. struct rockchip_priv_data {
  33. struct arc_emac_priv emac;
  34. struct regmap *grf;
  35. const struct emac_rockchip_soc_data *soc_data;
  36. struct regulator *regulator;
  37. struct clk *refclk;
  38. struct clk *macclk;
  39. };
  40. static void emac_rockchip_set_mac_speed(void *priv, unsigned int speed)
  41. {
  42. struct rockchip_priv_data *emac = priv;
  43. u32 speed_offset = emac->soc_data->grf_speed_offset;
  44. u32 data;
  45. int err = 0;
  46. switch (speed) {
  47. case 10:
  48. data = (1 << (speed_offset + 16)) | (0 << speed_offset);
  49. break;
  50. case 100:
  51. data = (1 << (speed_offset + 16)) | (1 << speed_offset);
  52. break;
  53. default:
  54. pr_err("speed %u not supported\n", speed);
  55. return;
  56. }
  57. err = regmap_write(emac->grf, emac->soc_data->grf_offset, data);
  58. if (err)
  59. pr_err("unable to apply speed %u to grf (%d)\n", speed, err);
  60. }
  61. static const struct emac_rockchip_soc_data emac_rk3036_emac_data = {
  62. .grf_offset = 0x140, .grf_mode_offset = 8,
  63. .grf_speed_offset = 9, .need_div_macclk = 1,
  64. };
  65. static const struct emac_rockchip_soc_data emac_rk3066_emac_data = {
  66. .grf_offset = 0x154, .grf_mode_offset = 0,
  67. .grf_speed_offset = 1, .need_div_macclk = 0,
  68. };
  69. static const struct emac_rockchip_soc_data emac_rk3188_emac_data = {
  70. .grf_offset = 0x0a4, .grf_mode_offset = 0,
  71. .grf_speed_offset = 1, .need_div_macclk = 0,
  72. };
  73. static const struct of_device_id emac_rockchip_dt_ids[] = {
  74. {
  75. .compatible = "rockchip,rk3036-emac",
  76. .data = &emac_rk3036_emac_data,
  77. },
  78. {
  79. .compatible = "rockchip,rk3066-emac",
  80. .data = &emac_rk3066_emac_data,
  81. },
  82. {
  83. .compatible = "rockchip,rk3188-emac",
  84. .data = &emac_rk3188_emac_data,
  85. },
  86. { /* Sentinel */ }
  87. };
  88. MODULE_DEVICE_TABLE(of, emac_rockchip_dt_ids);
  89. static int emac_rockchip_probe(struct platform_device *pdev)
  90. {
  91. struct device *dev = &pdev->dev;
  92. struct net_device *ndev;
  93. struct rockchip_priv_data *priv;
  94. const struct of_device_id *match;
  95. u32 data;
  96. int err, interface;
  97. if (!pdev->dev.of_node)
  98. return -ENODEV;
  99. ndev = alloc_etherdev(sizeof(struct rockchip_priv_data));
  100. if (!ndev)
  101. return -ENOMEM;
  102. platform_set_drvdata(pdev, ndev);
  103. SET_NETDEV_DEV(ndev, dev);
  104. priv = netdev_priv(ndev);
  105. priv->emac.drv_name = DRV_NAME;
  106. priv->emac.drv_version = DRV_VERSION;
  107. priv->emac.set_mac_speed = emac_rockchip_set_mac_speed;
  108. interface = of_get_phy_mode(dev->of_node);
  109. /* RK3036/RK3066/RK3188 SoCs only support RMII */
  110. if (interface != PHY_INTERFACE_MODE_RMII) {
  111. dev_err(dev, "unsupported phy interface mode %d\n", interface);
  112. err = -ENOTSUPP;
  113. goto out_netdev;
  114. }
  115. priv->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
  116. "rockchip,grf");
  117. if (IS_ERR(priv->grf)) {
  118. dev_err(dev, "failed to retrieve global register file (%ld)\n",
  119. PTR_ERR(priv->grf));
  120. err = PTR_ERR(priv->grf);
  121. goto out_netdev;
  122. }
  123. match = of_match_node(emac_rockchip_dt_ids, dev->of_node);
  124. priv->soc_data = match->data;
  125. priv->emac.clk = devm_clk_get(dev, "hclk");
  126. if (IS_ERR(priv->emac.clk)) {
  127. dev_err(dev, "failed to retrieve host clock (%ld)\n",
  128. PTR_ERR(priv->emac.clk));
  129. err = PTR_ERR(priv->emac.clk);
  130. goto out_netdev;
  131. }
  132. priv->refclk = devm_clk_get(dev, "macref");
  133. if (IS_ERR(priv->refclk)) {
  134. dev_err(dev, "failed to retrieve reference clock (%ld)\n",
  135. PTR_ERR(priv->refclk));
  136. err = PTR_ERR(priv->refclk);
  137. goto out_netdev;
  138. }
  139. err = clk_prepare_enable(priv->refclk);
  140. if (err) {
  141. dev_err(dev, "failed to enable reference clock (%d)\n", err);
  142. goto out_netdev;
  143. }
  144. /* Optional regulator for PHY */
  145. priv->regulator = devm_regulator_get_optional(dev, "phy");
  146. if (IS_ERR(priv->regulator)) {
  147. if (PTR_ERR(priv->regulator) == -EPROBE_DEFER) {
  148. err = -EPROBE_DEFER;
  149. goto out_clk_disable;
  150. }
  151. dev_err(dev, "no regulator found\n");
  152. priv->regulator = NULL;
  153. }
  154. if (priv->regulator) {
  155. err = regulator_enable(priv->regulator);
  156. if (err) {
  157. dev_err(dev, "failed to enable phy-supply (%d)\n", err);
  158. goto out_clk_disable;
  159. }
  160. }
  161. /* Set speed 100M */
  162. data = (1 << (priv->soc_data->grf_speed_offset + 16)) |
  163. (1 << priv->soc_data->grf_speed_offset);
  164. /* Set RMII mode */
  165. data |= (1 << (priv->soc_data->grf_mode_offset + 16)) |
  166. (0 << priv->soc_data->grf_mode_offset);
  167. err = regmap_write(priv->grf, priv->soc_data->grf_offset, data);
  168. if (err) {
  169. dev_err(dev, "unable to apply initial settings to grf (%d)\n",
  170. err);
  171. goto out_regulator_disable;
  172. }
  173. /* RMII interface needs always a rate of 50MHz */
  174. err = clk_set_rate(priv->refclk, 50000000);
  175. if (err)
  176. dev_err(dev,
  177. "failed to change reference clock rate (%d)\n", err);
  178. if (priv->soc_data->need_div_macclk) {
  179. priv->macclk = devm_clk_get(dev, "macclk");
  180. if (IS_ERR(priv->macclk)) {
  181. dev_err(dev, "failed to retrieve mac clock (%ld)\n",
  182. PTR_ERR(priv->macclk));
  183. err = PTR_ERR(priv->macclk);
  184. goto out_regulator_disable;
  185. }
  186. err = clk_prepare_enable(priv->macclk);
  187. if (err) {
  188. dev_err(dev, "failed to enable mac clock (%d)\n", err);
  189. goto out_regulator_disable;
  190. }
  191. /* RMII TX/RX needs always a rate of 25MHz */
  192. err = clk_set_rate(priv->macclk, 25000000);
  193. if (err) {
  194. dev_err(dev,
  195. "failed to change mac clock rate (%d)\n", err);
  196. goto out_clk_disable_macclk;
  197. }
  198. }
  199. err = arc_emac_probe(ndev, interface);
  200. if (err) {
  201. dev_err(dev, "failed to probe arc emac (%d)\n", err);
  202. goto out_regulator_disable;
  203. }
  204. return 0;
  205. out_clk_disable_macclk:
  206. clk_disable_unprepare(priv->macclk);
  207. out_regulator_disable:
  208. if (priv->regulator)
  209. regulator_disable(priv->regulator);
  210. out_clk_disable:
  211. clk_disable_unprepare(priv->refclk);
  212. out_netdev:
  213. free_netdev(ndev);
  214. return err;
  215. }
  216. static int emac_rockchip_remove(struct platform_device *pdev)
  217. {
  218. struct net_device *ndev = platform_get_drvdata(pdev);
  219. struct rockchip_priv_data *priv = netdev_priv(ndev);
  220. int err;
  221. err = arc_emac_remove(ndev);
  222. clk_disable_unprepare(priv->refclk);
  223. if (priv->regulator)
  224. regulator_disable(priv->regulator);
  225. free_netdev(ndev);
  226. return err;
  227. }
  228. static struct platform_driver emac_rockchip_driver = {
  229. .probe = emac_rockchip_probe,
  230. .remove = emac_rockchip_remove,
  231. .driver = {
  232. .name = DRV_NAME,
  233. .of_match_table = emac_rockchip_dt_ids,
  234. },
  235. };
  236. module_platform_driver(emac_rockchip_driver);
  237. MODULE_AUTHOR("Romain Perier <romain.perier@gmail.com>");
  238. MODULE_DESCRIPTION("Rockchip EMAC platform driver");
  239. MODULE_LICENSE("GPL");