reset-uniphier-glue.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // reset-uniphier-glue.c - Glue layer reset driver for UniPhier
  4. // Copyright 2018 Socionext Inc.
  5. // Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
  6. #include <linux/clk.h>
  7. #include <linux/module.h>
  8. #include <linux/of_device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/reset.h>
  11. #include <linux/reset/reset-simple.h>
  12. #define MAX_CLKS 2
  13. #define MAX_RSTS 2
  14. struct uniphier_glue_reset_soc_data {
  15. int nclks;
  16. const char * const *clock_names;
  17. int nrsts;
  18. const char * const *reset_names;
  19. };
  20. struct uniphier_glue_reset_priv {
  21. struct clk_bulk_data clk[MAX_CLKS];
  22. struct reset_control *rst[MAX_RSTS];
  23. struct reset_simple_data rdata;
  24. const struct uniphier_glue_reset_soc_data *data;
  25. };
  26. static int uniphier_glue_reset_probe(struct platform_device *pdev)
  27. {
  28. struct device *dev = &pdev->dev;
  29. struct uniphier_glue_reset_priv *priv;
  30. struct resource *res;
  31. resource_size_t size;
  32. const char *name;
  33. int i, ret, nr;
  34. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  35. if (!priv)
  36. return -ENOMEM;
  37. priv->data = of_device_get_match_data(dev);
  38. if (WARN_ON(!priv->data || priv->data->nclks > MAX_CLKS ||
  39. priv->data->nrsts > MAX_RSTS))
  40. return -EINVAL;
  41. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  42. size = resource_size(res);
  43. priv->rdata.membase = devm_ioremap_resource(dev, res);
  44. if (IS_ERR(priv->rdata.membase))
  45. return PTR_ERR(priv->rdata.membase);
  46. for (i = 0; i < priv->data->nclks; i++)
  47. priv->clk[i].id = priv->data->clock_names[i];
  48. ret = devm_clk_bulk_get(dev, priv->data->nclks, priv->clk);
  49. if (ret)
  50. return ret;
  51. for (i = 0; i < priv->data->nrsts; i++) {
  52. name = priv->data->reset_names[i];
  53. priv->rst[i] = devm_reset_control_get_shared(dev, name);
  54. if (IS_ERR(priv->rst[i]))
  55. return PTR_ERR(priv->rst[i]);
  56. }
  57. ret = clk_bulk_prepare_enable(priv->data->nclks, priv->clk);
  58. if (ret)
  59. return ret;
  60. for (nr = 0; nr < priv->data->nrsts; nr++) {
  61. ret = reset_control_deassert(priv->rst[nr]);
  62. if (ret)
  63. goto out_rst_assert;
  64. }
  65. spin_lock_init(&priv->rdata.lock);
  66. priv->rdata.rcdev.owner = THIS_MODULE;
  67. priv->rdata.rcdev.nr_resets = size * BITS_PER_BYTE;
  68. priv->rdata.rcdev.ops = &reset_simple_ops;
  69. priv->rdata.rcdev.of_node = dev->of_node;
  70. priv->rdata.active_low = true;
  71. platform_set_drvdata(pdev, priv);
  72. ret = devm_reset_controller_register(dev, &priv->rdata.rcdev);
  73. if (ret)
  74. goto out_rst_assert;
  75. return 0;
  76. out_rst_assert:
  77. while (nr--)
  78. reset_control_assert(priv->rst[nr]);
  79. clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
  80. return ret;
  81. }
  82. static int uniphier_glue_reset_remove(struct platform_device *pdev)
  83. {
  84. struct uniphier_glue_reset_priv *priv = platform_get_drvdata(pdev);
  85. int i;
  86. for (i = 0; i < priv->data->nrsts; i++)
  87. reset_control_assert(priv->rst[i]);
  88. clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
  89. return 0;
  90. }
  91. static const char * const uniphier_pro4_clock_reset_names[] = {
  92. "gio", "link",
  93. };
  94. static const struct uniphier_glue_reset_soc_data uniphier_pro4_data = {
  95. .nclks = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
  96. .clock_names = uniphier_pro4_clock_reset_names,
  97. .nrsts = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
  98. .reset_names = uniphier_pro4_clock_reset_names,
  99. };
  100. static const char * const uniphier_pxs2_clock_reset_names[] = {
  101. "link",
  102. };
  103. static const struct uniphier_glue_reset_soc_data uniphier_pxs2_data = {
  104. .nclks = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
  105. .clock_names = uniphier_pxs2_clock_reset_names,
  106. .nrsts = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
  107. .reset_names = uniphier_pxs2_clock_reset_names,
  108. };
  109. static const struct of_device_id uniphier_glue_reset_match[] = {
  110. {
  111. .compatible = "socionext,uniphier-pro4-usb3-reset",
  112. .data = &uniphier_pro4_data,
  113. },
  114. {
  115. .compatible = "socionext,uniphier-pro5-usb3-reset",
  116. .data = &uniphier_pro4_data,
  117. },
  118. {
  119. .compatible = "socionext,uniphier-pxs2-usb3-reset",
  120. .data = &uniphier_pxs2_data,
  121. },
  122. {
  123. .compatible = "socionext,uniphier-ld20-usb3-reset",
  124. .data = &uniphier_pxs2_data,
  125. },
  126. {
  127. .compatible = "socionext,uniphier-pxs3-usb3-reset",
  128. .data = &uniphier_pxs2_data,
  129. },
  130. {
  131. .compatible = "socionext,uniphier-pro4-ahci-reset",
  132. .data = &uniphier_pro4_data,
  133. },
  134. {
  135. .compatible = "socionext,uniphier-pxs2-ahci-reset",
  136. .data = &uniphier_pxs2_data,
  137. },
  138. {
  139. .compatible = "socionext,uniphier-pxs3-ahci-reset",
  140. .data = &uniphier_pxs2_data,
  141. },
  142. { /* Sentinel */ }
  143. };
  144. MODULE_DEVICE_TABLE(of, uniphier_glue_reset_match);
  145. static struct platform_driver uniphier_glue_reset_driver = {
  146. .probe = uniphier_glue_reset_probe,
  147. .remove = uniphier_glue_reset_remove,
  148. .driver = {
  149. .name = "uniphier-glue-reset",
  150. .of_match_table = uniphier_glue_reset_match,
  151. },
  152. };
  153. module_platform_driver(uniphier_glue_reset_driver);
  154. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  155. MODULE_DESCRIPTION("UniPhier Glue layer reset driver");
  156. MODULE_LICENSE("GPL");