clk-sun9i-mmc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2015 Chen-Yu Tsai
  3. *
  4. * Chen-Yu Tsai <wens@csie.org>
  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/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/reset.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/reset-controller.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #define SUN9I_MMC_WIDTH 4
  28. #define SUN9I_MMC_GATE_BIT 16
  29. #define SUN9I_MMC_RESET_BIT 18
  30. struct sun9i_mmc_clk_data {
  31. spinlock_t lock;
  32. void __iomem *membase;
  33. struct clk *clk;
  34. struct reset_control *reset;
  35. struct clk_onecell_data clk_data;
  36. struct reset_controller_dev rcdev;
  37. };
  38. static int sun9i_mmc_reset_assert(struct reset_controller_dev *rcdev,
  39. unsigned long id)
  40. {
  41. struct sun9i_mmc_clk_data *data = container_of(rcdev,
  42. struct sun9i_mmc_clk_data,
  43. rcdev);
  44. unsigned long flags;
  45. void __iomem *reg = data->membase + SUN9I_MMC_WIDTH * id;
  46. u32 val;
  47. clk_prepare_enable(data->clk);
  48. spin_lock_irqsave(&data->lock, flags);
  49. val = readl(reg);
  50. writel(val & ~BIT(SUN9I_MMC_RESET_BIT), reg);
  51. spin_unlock_irqrestore(&data->lock, flags);
  52. clk_disable_unprepare(data->clk);
  53. return 0;
  54. }
  55. static int sun9i_mmc_reset_deassert(struct reset_controller_dev *rcdev,
  56. unsigned long id)
  57. {
  58. struct sun9i_mmc_clk_data *data = container_of(rcdev,
  59. struct sun9i_mmc_clk_data,
  60. rcdev);
  61. unsigned long flags;
  62. void __iomem *reg = data->membase + SUN9I_MMC_WIDTH * id;
  63. u32 val;
  64. clk_prepare_enable(data->clk);
  65. spin_lock_irqsave(&data->lock, flags);
  66. val = readl(reg);
  67. writel(val | BIT(SUN9I_MMC_RESET_BIT), reg);
  68. spin_unlock_irqrestore(&data->lock, flags);
  69. clk_disable_unprepare(data->clk);
  70. return 0;
  71. }
  72. static int sun9i_mmc_reset_reset(struct reset_controller_dev *rcdev,
  73. unsigned long id)
  74. {
  75. sun9i_mmc_reset_assert(rcdev, id);
  76. udelay(10);
  77. sun9i_mmc_reset_deassert(rcdev, id);
  78. return 0;
  79. }
  80. static const struct reset_control_ops sun9i_mmc_reset_ops = {
  81. .assert = sun9i_mmc_reset_assert,
  82. .deassert = sun9i_mmc_reset_deassert,
  83. .reset = sun9i_mmc_reset_reset,
  84. };
  85. static int sun9i_a80_mmc_config_clk_probe(struct platform_device *pdev)
  86. {
  87. struct device_node *np = pdev->dev.of_node;
  88. struct sun9i_mmc_clk_data *data;
  89. struct clk_onecell_data *clk_data;
  90. const char *clk_name = np->name;
  91. const char *clk_parent;
  92. struct resource *r;
  93. int count, i, ret;
  94. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  95. if (!data)
  96. return -ENOMEM;
  97. spin_lock_init(&data->lock);
  98. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  99. /* one clock/reset pair per word */
  100. count = DIV_ROUND_UP((resource_size(r)), SUN9I_MMC_WIDTH);
  101. data->membase = devm_ioremap_resource(&pdev->dev, r);
  102. if (IS_ERR(data->membase))
  103. return PTR_ERR(data->membase);
  104. clk_data = &data->clk_data;
  105. clk_data->clk_num = count;
  106. clk_data->clks = devm_kcalloc(&pdev->dev, count, sizeof(struct clk *),
  107. GFP_KERNEL);
  108. if (!clk_data->clks)
  109. return -ENOMEM;
  110. data->clk = devm_clk_get(&pdev->dev, NULL);
  111. if (IS_ERR(data->clk)) {
  112. dev_err(&pdev->dev, "Could not get clock\n");
  113. return PTR_ERR(data->clk);
  114. }
  115. data->reset = devm_reset_control_get(&pdev->dev, NULL);
  116. if (IS_ERR(data->reset)) {
  117. dev_err(&pdev->dev, "Could not get reset control\n");
  118. return PTR_ERR(data->reset);
  119. }
  120. ret = reset_control_deassert(data->reset);
  121. if (ret) {
  122. dev_err(&pdev->dev, "Reset deassert err %d\n", ret);
  123. return ret;
  124. }
  125. clk_parent = __clk_get_name(data->clk);
  126. for (i = 0; i < count; i++) {
  127. of_property_read_string_index(np, "clock-output-names",
  128. i, &clk_name);
  129. clk_data->clks[i] = clk_register_gate(&pdev->dev, clk_name,
  130. clk_parent, 0,
  131. data->membase + SUN9I_MMC_WIDTH * i,
  132. SUN9I_MMC_GATE_BIT, 0,
  133. &data->lock);
  134. if (IS_ERR(clk_data->clks[i])) {
  135. ret = PTR_ERR(clk_data->clks[i]);
  136. goto err_clk_register;
  137. }
  138. }
  139. ret = of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  140. if (ret)
  141. goto err_clk_provider;
  142. data->rcdev.owner = THIS_MODULE;
  143. data->rcdev.nr_resets = count;
  144. data->rcdev.ops = &sun9i_mmc_reset_ops;
  145. data->rcdev.of_node = pdev->dev.of_node;
  146. ret = reset_controller_register(&data->rcdev);
  147. if (ret)
  148. goto err_rc_reg;
  149. platform_set_drvdata(pdev, data);
  150. return 0;
  151. err_rc_reg:
  152. of_clk_del_provider(np);
  153. err_clk_provider:
  154. for (i = 0; i < count; i++)
  155. clk_unregister(clk_data->clks[i]);
  156. err_clk_register:
  157. reset_control_assert(data->reset);
  158. return ret;
  159. }
  160. static const struct of_device_id sun9i_a80_mmc_config_clk_dt_ids[] = {
  161. { .compatible = "allwinner,sun9i-a80-mmc-config-clk" },
  162. { /* sentinel */ }
  163. };
  164. static struct platform_driver sun9i_a80_mmc_config_clk_driver = {
  165. .driver = {
  166. .name = "sun9i-a80-mmc-config-clk",
  167. .suppress_bind_attrs = true,
  168. .of_match_table = sun9i_a80_mmc_config_clk_dt_ids,
  169. },
  170. .probe = sun9i_a80_mmc_config_clk_probe,
  171. };
  172. builtin_platform_driver(sun9i_a80_mmc_config_clk_driver);