emac_rockchip.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. return -EPROBE_DEFER;
  149. dev_err(dev, "no regulator found\n");
  150. priv->regulator = NULL;
  151. }
  152. if (priv->regulator) {
  153. err = regulator_enable(priv->regulator);
  154. if (err) {
  155. dev_err(dev, "failed to enable phy-supply (%d)\n", err);
  156. goto out_clk_disable;
  157. }
  158. }
  159. /* Set speed 100M */
  160. data = (1 << (priv->soc_data->grf_speed_offset + 16)) |
  161. (1 << priv->soc_data->grf_speed_offset);
  162. /* Set RMII mode */
  163. data |= (1 << (priv->soc_data->grf_mode_offset + 16)) |
  164. (0 << priv->soc_data->grf_mode_offset);
  165. err = regmap_write(priv->grf, priv->soc_data->grf_offset, data);
  166. if (err) {
  167. dev_err(dev, "unable to apply initial settings to grf (%d)\n",
  168. err);
  169. goto out_regulator_disable;
  170. }
  171. /* RMII interface needs always a rate of 50MHz */
  172. err = clk_set_rate(priv->refclk, 50000000);
  173. if (err)
  174. dev_err(dev,
  175. "failed to change reference clock rate (%d)\n", err);
  176. if (priv->soc_data->need_div_macclk) {
  177. priv->macclk = devm_clk_get(dev, "macclk");
  178. if (IS_ERR(priv->macclk)) {
  179. dev_err(dev, "failed to retrieve mac clock (%ld)\n",
  180. PTR_ERR(priv->macclk));
  181. err = PTR_ERR(priv->macclk);
  182. goto out_regulator_disable;
  183. }
  184. err = clk_prepare_enable(priv->macclk);
  185. if (err) {
  186. dev_err(dev, "failed to enable mac clock (%d)\n", err);
  187. goto out_regulator_disable;
  188. }
  189. /* RMII TX/RX needs always a rate of 25MHz */
  190. err = clk_set_rate(priv->macclk, 25000000);
  191. if (err)
  192. dev_err(dev,
  193. "failed to change mac clock rate (%d)\n", err);
  194. }
  195. err = arc_emac_probe(ndev, interface);
  196. if (err) {
  197. dev_err(dev, "failed to probe arc emac (%d)\n", err);
  198. goto out_regulator_disable;
  199. }
  200. return 0;
  201. out_regulator_disable:
  202. if (priv->regulator)
  203. regulator_disable(priv->regulator);
  204. out_clk_disable:
  205. clk_disable_unprepare(priv->refclk);
  206. out_netdev:
  207. free_netdev(ndev);
  208. return err;
  209. }
  210. static int emac_rockchip_remove(struct platform_device *pdev)
  211. {
  212. struct net_device *ndev = platform_get_drvdata(pdev);
  213. struct rockchip_priv_data *priv = netdev_priv(ndev);
  214. int err;
  215. err = arc_emac_remove(ndev);
  216. clk_disable_unprepare(priv->refclk);
  217. if (priv->regulator)
  218. regulator_disable(priv->regulator);
  219. free_netdev(ndev);
  220. return err;
  221. }
  222. static struct platform_driver emac_rockchip_driver = {
  223. .probe = emac_rockchip_probe,
  224. .remove = emac_rockchip_remove,
  225. .driver = {
  226. .name = DRV_NAME,
  227. .of_match_table = emac_rockchip_dt_ids,
  228. },
  229. };
  230. module_platform_driver(emac_rockchip_driver);
  231. MODULE_AUTHOR("Romain Perier <romain.perier@gmail.com>");
  232. MODULE_DESCRIPTION("Rockchip EMAC platform driver");
  233. MODULE_LICENSE("GPL");