clk-mstp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * R-Car MSTP clocks
  3. *
  4. * Copyright (C) 2013 Ideas On Board SPRL
  5. * Copyright (C) 2015 Glider bvba
  6. *
  7. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/clkdev.h>
  16. #include <linux/clk/renesas.h>
  17. #include <linux/device.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/pm_clock.h>
  22. #include <linux/pm_domain.h>
  23. #include <linux/spinlock.h>
  24. /*
  25. * MSTP clocks. We can't use standard gate clocks as we need to poll on the
  26. * status register when enabling the clock.
  27. */
  28. #define MSTP_MAX_CLOCKS 32
  29. /**
  30. * struct mstp_clock_group - MSTP gating clocks group
  31. *
  32. * @data: clocks in this group
  33. * @smstpcr: module stop control register
  34. * @mstpsr: module stop status register (optional)
  35. * @lock: protects writes to SMSTPCR
  36. * @width_8bit: registers are 8-bit, not 32-bit
  37. */
  38. struct mstp_clock_group {
  39. struct clk_onecell_data data;
  40. void __iomem *smstpcr;
  41. void __iomem *mstpsr;
  42. spinlock_t lock;
  43. bool width_8bit;
  44. };
  45. /**
  46. * struct mstp_clock - MSTP gating clock
  47. * @hw: handle between common and hardware-specific interfaces
  48. * @bit_index: control bit index
  49. * @group: MSTP clocks group
  50. */
  51. struct mstp_clock {
  52. struct clk_hw hw;
  53. u32 bit_index;
  54. struct mstp_clock_group *group;
  55. };
  56. #define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw)
  57. static inline u32 cpg_mstp_read(struct mstp_clock_group *group,
  58. u32 __iomem *reg)
  59. {
  60. return group->width_8bit ? readb(reg) : clk_readl(reg);
  61. }
  62. static inline void cpg_mstp_write(struct mstp_clock_group *group, u32 val,
  63. u32 __iomem *reg)
  64. {
  65. group->width_8bit ? writeb(val, reg) : clk_writel(val, reg);
  66. }
  67. static int cpg_mstp_clock_endisable(struct clk_hw *hw, bool enable)
  68. {
  69. struct mstp_clock *clock = to_mstp_clock(hw);
  70. struct mstp_clock_group *group = clock->group;
  71. u32 bitmask = BIT(clock->bit_index);
  72. unsigned long flags;
  73. unsigned int i;
  74. u32 value;
  75. spin_lock_irqsave(&group->lock, flags);
  76. value = cpg_mstp_read(group, group->smstpcr);
  77. if (enable)
  78. value &= ~bitmask;
  79. else
  80. value |= bitmask;
  81. cpg_mstp_write(group, value, group->smstpcr);
  82. spin_unlock_irqrestore(&group->lock, flags);
  83. if (!enable || !group->mstpsr)
  84. return 0;
  85. for (i = 1000; i > 0; --i) {
  86. if (!(cpg_mstp_read(group, group->mstpsr) & bitmask))
  87. break;
  88. cpu_relax();
  89. }
  90. if (!i) {
  91. pr_err("%s: failed to enable %p[%d]\n", __func__,
  92. group->smstpcr, clock->bit_index);
  93. return -ETIMEDOUT;
  94. }
  95. return 0;
  96. }
  97. static int cpg_mstp_clock_enable(struct clk_hw *hw)
  98. {
  99. return cpg_mstp_clock_endisable(hw, true);
  100. }
  101. static void cpg_mstp_clock_disable(struct clk_hw *hw)
  102. {
  103. cpg_mstp_clock_endisable(hw, false);
  104. }
  105. static int cpg_mstp_clock_is_enabled(struct clk_hw *hw)
  106. {
  107. struct mstp_clock *clock = to_mstp_clock(hw);
  108. struct mstp_clock_group *group = clock->group;
  109. u32 value;
  110. if (group->mstpsr)
  111. value = cpg_mstp_read(group, group->mstpsr);
  112. else
  113. value = cpg_mstp_read(group, group->smstpcr);
  114. return !(value & BIT(clock->bit_index));
  115. }
  116. static const struct clk_ops cpg_mstp_clock_ops = {
  117. .enable = cpg_mstp_clock_enable,
  118. .disable = cpg_mstp_clock_disable,
  119. .is_enabled = cpg_mstp_clock_is_enabled,
  120. };
  121. static struct clk * __init
  122. cpg_mstp_clock_register(const char *name, const char *parent_name,
  123. unsigned int index, struct mstp_clock_group *group)
  124. {
  125. struct clk_init_data init;
  126. struct mstp_clock *clock;
  127. struct clk *clk;
  128. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  129. if (!clock) {
  130. pr_err("%s: failed to allocate MSTP clock.\n", __func__);
  131. return ERR_PTR(-ENOMEM);
  132. }
  133. init.name = name;
  134. init.ops = &cpg_mstp_clock_ops;
  135. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  136. init.parent_names = &parent_name;
  137. init.num_parents = 1;
  138. clock->bit_index = index;
  139. clock->group = group;
  140. clock->hw.init = &init;
  141. clk = clk_register(NULL, &clock->hw);
  142. if (IS_ERR(clk))
  143. kfree(clock);
  144. return clk;
  145. }
  146. static void __init cpg_mstp_clocks_init(struct device_node *np)
  147. {
  148. struct mstp_clock_group *group;
  149. const char *idxname;
  150. struct clk **clks;
  151. unsigned int i;
  152. group = kzalloc(sizeof(*group), GFP_KERNEL);
  153. clks = kmalloc_array(MSTP_MAX_CLOCKS, sizeof(*clks), GFP_KERNEL);
  154. if (group == NULL || clks == NULL) {
  155. kfree(group);
  156. kfree(clks);
  157. pr_err("%s: failed to allocate group\n", __func__);
  158. return;
  159. }
  160. spin_lock_init(&group->lock);
  161. group->data.clks = clks;
  162. group->smstpcr = of_iomap(np, 0);
  163. group->mstpsr = of_iomap(np, 1);
  164. if (group->smstpcr == NULL) {
  165. pr_err("%s: failed to remap SMSTPCR\n", __func__);
  166. kfree(group);
  167. kfree(clks);
  168. return;
  169. }
  170. if (of_device_is_compatible(np, "renesas,r7s72100-mstp-clocks"))
  171. group->width_8bit = true;
  172. for (i = 0; i < MSTP_MAX_CLOCKS; ++i)
  173. clks[i] = ERR_PTR(-ENOENT);
  174. if (of_find_property(np, "clock-indices", &i))
  175. idxname = "clock-indices";
  176. else
  177. idxname = "renesas,clock-indices";
  178. for (i = 0; i < MSTP_MAX_CLOCKS; ++i) {
  179. const char *parent_name;
  180. const char *name;
  181. u32 clkidx;
  182. int ret;
  183. /* Skip clocks with no name. */
  184. ret = of_property_read_string_index(np, "clock-output-names",
  185. i, &name);
  186. if (ret < 0 || strlen(name) == 0)
  187. continue;
  188. parent_name = of_clk_get_parent_name(np, i);
  189. ret = of_property_read_u32_index(np, idxname, i, &clkidx);
  190. if (parent_name == NULL || ret < 0)
  191. break;
  192. if (clkidx >= MSTP_MAX_CLOCKS) {
  193. pr_err("%s: invalid clock %s %s index %u\n",
  194. __func__, np->name, name, clkidx);
  195. continue;
  196. }
  197. clks[clkidx] = cpg_mstp_clock_register(name, parent_name,
  198. clkidx, group);
  199. if (!IS_ERR(clks[clkidx])) {
  200. group->data.clk_num = max(group->data.clk_num,
  201. clkidx + 1);
  202. /*
  203. * Register a clkdev to let board code retrieve the
  204. * clock by name and register aliases for non-DT
  205. * devices.
  206. *
  207. * FIXME: Remove this when all devices that require a
  208. * clock will be instantiated from DT.
  209. */
  210. clk_register_clkdev(clks[clkidx], name, NULL);
  211. } else {
  212. pr_err("%s: failed to register %s %s clock (%ld)\n",
  213. __func__, np->name, name, PTR_ERR(clks[clkidx]));
  214. }
  215. }
  216. of_clk_add_provider(np, of_clk_src_onecell_get, &group->data);
  217. }
  218. CLK_OF_DECLARE(cpg_mstp_clks, "renesas,cpg-mstp-clocks", cpg_mstp_clocks_init);
  219. int cpg_mstp_attach_dev(struct generic_pm_domain *unused, struct device *dev)
  220. {
  221. struct device_node *np = dev->of_node;
  222. struct of_phandle_args clkspec;
  223. struct clk *clk;
  224. int i = 0;
  225. int error;
  226. while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
  227. &clkspec)) {
  228. if (of_device_is_compatible(clkspec.np,
  229. "renesas,cpg-mstp-clocks"))
  230. goto found;
  231. /* BSC on r8a73a4/sh73a0 uses zb_clk instead of an mstp clock */
  232. if (!strcmp(clkspec.np->name, "zb_clk"))
  233. goto found;
  234. of_node_put(clkspec.np);
  235. i++;
  236. }
  237. return 0;
  238. found:
  239. clk = of_clk_get_from_provider(&clkspec);
  240. of_node_put(clkspec.np);
  241. if (IS_ERR(clk))
  242. return PTR_ERR(clk);
  243. error = pm_clk_create(dev);
  244. if (error) {
  245. dev_err(dev, "pm_clk_create failed %d\n", error);
  246. goto fail_put;
  247. }
  248. error = pm_clk_add_clk(dev, clk);
  249. if (error) {
  250. dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error);
  251. goto fail_destroy;
  252. }
  253. return 0;
  254. fail_destroy:
  255. pm_clk_destroy(dev);
  256. fail_put:
  257. clk_put(clk);
  258. return error;
  259. }
  260. void cpg_mstp_detach_dev(struct generic_pm_domain *unused, struct device *dev)
  261. {
  262. if (!list_empty(&dev->power.subsys_data->clock_list))
  263. pm_clk_destroy(dev);
  264. }
  265. void __init cpg_mstp_add_clk_domain(struct device_node *np)
  266. {
  267. struct generic_pm_domain *pd;
  268. u32 ncells;
  269. if (of_property_read_u32(np, "#power-domain-cells", &ncells)) {
  270. pr_warn("%s lacks #power-domain-cells\n", np->full_name);
  271. return;
  272. }
  273. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  274. if (!pd)
  275. return;
  276. pd->name = np->name;
  277. pd->flags = GENPD_FLAG_PM_CLK;
  278. pd->attach_dev = cpg_mstp_attach_dev;
  279. pd->detach_dev = cpg_mstp_detach_dev;
  280. pm_genpd_init(pd, &pm_domain_always_on_gov, false);
  281. of_genpd_add_provider_simple(np, pd);
  282. }