emac_rockchip.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. { .compatible = "rockchip,rk3036-emac", .data = &emac_rk3036_emac_data },
  75. { .compatible = "rockchip,rk3066-emac", .data = &emac_rk3066_emac_data },
  76. { .compatible = "rockchip,rk3188-emac", .data = &emac_rk3188_emac_data },
  77. { /* Sentinel */ }
  78. };
  79. MODULE_DEVICE_TABLE(of, emac_rockchip_dt_ids);
  80. static int emac_rockchip_probe(struct platform_device *pdev)
  81. {
  82. struct device *dev = &pdev->dev;
  83. struct net_device *ndev;
  84. struct rockchip_priv_data *priv;
  85. const struct of_device_id *match;
  86. u32 data;
  87. int err, interface;
  88. if (!pdev->dev.of_node)
  89. return -ENODEV;
  90. ndev = alloc_etherdev(sizeof(struct rockchip_priv_data));
  91. if (!ndev)
  92. return -ENOMEM;
  93. platform_set_drvdata(pdev, ndev);
  94. SET_NETDEV_DEV(ndev, dev);
  95. priv = netdev_priv(ndev);
  96. priv->emac.drv_name = DRV_NAME;
  97. priv->emac.drv_version = DRV_VERSION;
  98. priv->emac.set_mac_speed = emac_rockchip_set_mac_speed;
  99. interface = of_get_phy_mode(dev->of_node);
  100. /* RK3036/RK3066/RK3188 SoCs only support RMII */
  101. if (interface != PHY_INTERFACE_MODE_RMII) {
  102. dev_err(dev, "unsupported phy interface mode %d\n", interface);
  103. err = -ENOTSUPP;
  104. goto out_netdev;
  105. }
  106. priv->grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
  107. if (IS_ERR(priv->grf)) {
  108. dev_err(dev, "failed to retrieve global register file (%ld)\n", PTR_ERR(priv->grf));
  109. err = PTR_ERR(priv->grf);
  110. goto out_netdev;
  111. }
  112. match = of_match_node(emac_rockchip_dt_ids, dev->of_node);
  113. priv->soc_data = match->data;
  114. priv->emac.clk = devm_clk_get(dev, "hclk");
  115. if (IS_ERR(priv->emac.clk)) {
  116. dev_err(dev, "failed to retrieve host clock (%ld)\n", PTR_ERR(priv->emac.clk));
  117. err = PTR_ERR(priv->emac.clk);
  118. goto out_netdev;
  119. }
  120. priv->refclk = devm_clk_get(dev, "macref");
  121. if (IS_ERR(priv->refclk)) {
  122. dev_err(dev, "failed to retrieve reference clock (%ld)\n", PTR_ERR(priv->refclk));
  123. err = PTR_ERR(priv->refclk);
  124. goto out_netdev;
  125. }
  126. err = clk_prepare_enable(priv->refclk);
  127. if (err) {
  128. dev_err(dev, "failed to enable reference clock (%d)\n", err);
  129. goto out_netdev;
  130. }
  131. /* Optional regulator for PHY */
  132. priv->regulator = devm_regulator_get_optional(dev, "phy");
  133. if (IS_ERR(priv->regulator)) {
  134. if (PTR_ERR(priv->regulator) == -EPROBE_DEFER)
  135. return -EPROBE_DEFER;
  136. dev_err(dev, "no regulator found\n");
  137. priv->regulator = NULL;
  138. }
  139. if (priv->regulator) {
  140. err = regulator_enable(priv->regulator);
  141. if (err) {
  142. dev_err(dev, "failed to enable phy-supply (%d)\n", err);
  143. goto out_clk_disable;
  144. }
  145. }
  146. /* Set speed 100M */
  147. data = (1 << (priv->soc_data->grf_speed_offset + 16)) |
  148. (1 << priv->soc_data->grf_speed_offset);
  149. /* Set RMII mode */
  150. data |= (1 << (priv->soc_data->grf_mode_offset + 16)) |
  151. (0 << priv->soc_data->grf_mode_offset);
  152. err = regmap_write(priv->grf, priv->soc_data->grf_offset, data);
  153. if (err) {
  154. dev_err(dev, "unable to apply initial settings to grf (%d)\n", err);
  155. goto out_regulator_disable;
  156. }
  157. /* RMII interface needs always a rate of 50MHz */
  158. err = clk_set_rate(priv->refclk, 50000000);
  159. if (err)
  160. dev_err(dev, "failed to change reference clock rate (%d)\n", err);
  161. if (priv->soc_data->need_div_macclk) {
  162. priv->macclk = devm_clk_get(dev, "macclk");
  163. if (IS_ERR(priv->macclk)) {
  164. dev_err(dev, "failed to retrieve mac clock (%ld)\n", PTR_ERR(priv->macclk));
  165. err = PTR_ERR(priv->macclk);
  166. goto out_regulator_disable;
  167. }
  168. err = clk_prepare_enable(priv->macclk);
  169. if (err) {
  170. dev_err(dev, "failed to enable mac clock (%d)\n", err);
  171. goto out_regulator_disable;
  172. }
  173. /* RMII TX/RX needs always a rate of 25MHz */
  174. err = clk_set_rate(priv->macclk, 25000000);
  175. if (err)
  176. dev_err(dev, "failed to change mac clock rate (%d)\n", err);
  177. }
  178. err = arc_emac_probe(ndev, interface);
  179. if (err) {
  180. dev_err(dev, "failed to probe arc emac (%d)\n", err);
  181. goto out_regulator_disable;
  182. }
  183. return 0;
  184. out_regulator_disable:
  185. if (priv->regulator)
  186. regulator_disable(priv->regulator);
  187. out_clk_disable:
  188. clk_disable_unprepare(priv->refclk);
  189. out_netdev:
  190. free_netdev(ndev);
  191. return err;
  192. }
  193. static int emac_rockchip_remove(struct platform_device *pdev)
  194. {
  195. struct net_device *ndev = platform_get_drvdata(pdev);
  196. struct rockchip_priv_data *priv = netdev_priv(ndev);
  197. int err;
  198. err = arc_emac_remove(ndev);
  199. clk_disable_unprepare(priv->refclk);
  200. if (priv->regulator)
  201. regulator_disable(priv->regulator);
  202. free_netdev(ndev);
  203. return err;
  204. }
  205. static struct platform_driver emac_rockchip_driver = {
  206. .probe = emac_rockchip_probe,
  207. .remove = emac_rockchip_remove,
  208. .driver = {
  209. .name = DRV_NAME,
  210. .of_match_table = emac_rockchip_dt_ids,
  211. },
  212. };
  213. module_platform_driver(emac_rockchip_driver);
  214. MODULE_AUTHOR("Romain Perier <romain.perier@gmail.com>");
  215. MODULE_DESCRIPTION("Rockchip EMAC platform driver");
  216. MODULE_LICENSE("GPL");