clk-mstp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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) : 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) : 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. if (!group->mstpsr) {
  83. /* dummy read to ensure write has completed */
  84. cpg_mstp_read(group, group->smstpcr);
  85. barrier_data(group->smstpcr);
  86. }
  87. spin_unlock_irqrestore(&group->lock, flags);
  88. if (!enable || !group->mstpsr)
  89. return 0;
  90. for (i = 1000; i > 0; --i) {
  91. if (!(cpg_mstp_read(group, group->mstpsr) & bitmask))
  92. break;
  93. cpu_relax();
  94. }
  95. if (!i) {
  96. pr_err("%s: failed to enable %p[%d]\n", __func__,
  97. group->smstpcr, clock->bit_index);
  98. return -ETIMEDOUT;
  99. }
  100. return 0;
  101. }
  102. static int cpg_mstp_clock_enable(struct clk_hw *hw)
  103. {
  104. return cpg_mstp_clock_endisable(hw, true);
  105. }
  106. static void cpg_mstp_clock_disable(struct clk_hw *hw)
  107. {
  108. cpg_mstp_clock_endisable(hw, false);
  109. }
  110. static int cpg_mstp_clock_is_enabled(struct clk_hw *hw)
  111. {
  112. struct mstp_clock *clock = to_mstp_clock(hw);
  113. struct mstp_clock_group *group = clock->group;
  114. u32 value;
  115. if (group->mstpsr)
  116. value = cpg_mstp_read(group, group->mstpsr);
  117. else
  118. value = cpg_mstp_read(group, group->smstpcr);
  119. return !(value & BIT(clock->bit_index));
  120. }
  121. static const struct clk_ops cpg_mstp_clock_ops = {
  122. .enable = cpg_mstp_clock_enable,
  123. .disable = cpg_mstp_clock_disable,
  124. .is_enabled = cpg_mstp_clock_is_enabled,
  125. };
  126. static struct clk * __init cpg_mstp_clock_register(const char *name,
  127. const char *parent_name, unsigned int index,
  128. struct mstp_clock_group *group)
  129. {
  130. struct clk_init_data init;
  131. struct mstp_clock *clock;
  132. struct clk *clk;
  133. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  134. if (!clock)
  135. return ERR_PTR(-ENOMEM);
  136. init.name = name;
  137. init.ops = &cpg_mstp_clock_ops;
  138. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  139. /* INTC-SYS is the module clock of the GIC, and must not be disabled */
  140. if (!strcmp(name, "intc-sys")) {
  141. pr_debug("MSTP %s setting CLK_IS_CRITICAL\n", name);
  142. init.flags |= CLK_IS_CRITICAL;
  143. }
  144. init.parent_names = &parent_name;
  145. init.num_parents = 1;
  146. clock->bit_index = index;
  147. clock->group = group;
  148. clock->hw.init = &init;
  149. clk = clk_register(NULL, &clock->hw);
  150. if (IS_ERR(clk))
  151. kfree(clock);
  152. return clk;
  153. }
  154. static void __init cpg_mstp_clocks_init(struct device_node *np)
  155. {
  156. struct mstp_clock_group *group;
  157. const char *idxname;
  158. struct clk **clks;
  159. unsigned int i;
  160. group = kzalloc(sizeof(*group), GFP_KERNEL);
  161. clks = kmalloc_array(MSTP_MAX_CLOCKS, sizeof(*clks), GFP_KERNEL);
  162. if (group == NULL || clks == NULL) {
  163. kfree(group);
  164. kfree(clks);
  165. return;
  166. }
  167. spin_lock_init(&group->lock);
  168. group->data.clks = clks;
  169. group->smstpcr = of_iomap(np, 0);
  170. group->mstpsr = of_iomap(np, 1);
  171. if (group->smstpcr == NULL) {
  172. pr_err("%s: failed to remap SMSTPCR\n", __func__);
  173. kfree(group);
  174. kfree(clks);
  175. return;
  176. }
  177. if (of_device_is_compatible(np, "renesas,r7s72100-mstp-clocks"))
  178. group->width_8bit = true;
  179. for (i = 0; i < MSTP_MAX_CLOCKS; ++i)
  180. clks[i] = ERR_PTR(-ENOENT);
  181. if (of_find_property(np, "clock-indices", &i))
  182. idxname = "clock-indices";
  183. else
  184. idxname = "renesas,clock-indices";
  185. for (i = 0; i < MSTP_MAX_CLOCKS; ++i) {
  186. const char *parent_name;
  187. const char *name;
  188. u32 clkidx;
  189. int ret;
  190. /* Skip clocks with no name. */
  191. ret = of_property_read_string_index(np, "clock-output-names",
  192. i, &name);
  193. if (ret < 0 || strlen(name) == 0)
  194. continue;
  195. parent_name = of_clk_get_parent_name(np, i);
  196. ret = of_property_read_u32_index(np, idxname, i, &clkidx);
  197. if (parent_name == NULL || ret < 0)
  198. break;
  199. if (clkidx >= MSTP_MAX_CLOCKS) {
  200. pr_err("%s: invalid clock %s %s index %u\n",
  201. __func__, np->name, name, clkidx);
  202. continue;
  203. }
  204. clks[clkidx] = cpg_mstp_clock_register(name, parent_name,
  205. clkidx, group);
  206. if (!IS_ERR(clks[clkidx])) {
  207. group->data.clk_num = max(group->data.clk_num,
  208. clkidx + 1);
  209. /*
  210. * Register a clkdev to let board code retrieve the
  211. * clock by name and register aliases for non-DT
  212. * devices.
  213. *
  214. * FIXME: Remove this when all devices that require a
  215. * clock will be instantiated from DT.
  216. */
  217. clk_register_clkdev(clks[clkidx], name, NULL);
  218. } else {
  219. pr_err("%s: failed to register %s %s clock (%ld)\n",
  220. __func__, np->name, name, PTR_ERR(clks[clkidx]));
  221. }
  222. }
  223. of_clk_add_provider(np, of_clk_src_onecell_get, &group->data);
  224. }
  225. CLK_OF_DECLARE(cpg_mstp_clks, "renesas,cpg-mstp-clocks", cpg_mstp_clocks_init);
  226. int cpg_mstp_attach_dev(struct generic_pm_domain *unused, struct device *dev)
  227. {
  228. struct device_node *np = dev->of_node;
  229. struct of_phandle_args clkspec;
  230. struct clk *clk;
  231. int i = 0;
  232. int error;
  233. while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
  234. &clkspec)) {
  235. if (of_device_is_compatible(clkspec.np,
  236. "renesas,cpg-mstp-clocks"))
  237. goto found;
  238. /* BSC on r8a73a4/sh73a0 uses zb_clk instead of an mstp clock */
  239. if (!strcmp(clkspec.np->name, "zb_clk"))
  240. goto found;
  241. of_node_put(clkspec.np);
  242. i++;
  243. }
  244. return 0;
  245. found:
  246. clk = of_clk_get_from_provider(&clkspec);
  247. of_node_put(clkspec.np);
  248. if (IS_ERR(clk))
  249. return PTR_ERR(clk);
  250. error = pm_clk_create(dev);
  251. if (error) {
  252. dev_err(dev, "pm_clk_create failed %d\n", error);
  253. goto fail_put;
  254. }
  255. error = pm_clk_add_clk(dev, clk);
  256. if (error) {
  257. dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error);
  258. goto fail_destroy;
  259. }
  260. return 0;
  261. fail_destroy:
  262. pm_clk_destroy(dev);
  263. fail_put:
  264. clk_put(clk);
  265. return error;
  266. }
  267. void cpg_mstp_detach_dev(struct generic_pm_domain *unused, struct device *dev)
  268. {
  269. if (!pm_clk_no_clocks(dev))
  270. pm_clk_destroy(dev);
  271. }
  272. void __init cpg_mstp_add_clk_domain(struct device_node *np)
  273. {
  274. struct generic_pm_domain *pd;
  275. u32 ncells;
  276. if (of_property_read_u32(np, "#power-domain-cells", &ncells)) {
  277. pr_warn("%pOF lacks #power-domain-cells\n", np);
  278. return;
  279. }
  280. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  281. if (!pd)
  282. return;
  283. pd->name = np->name;
  284. pd->flags = GENPD_FLAG_PM_CLK | GENPD_FLAG_ALWAYS_ON |
  285. GENPD_FLAG_ACTIVE_WAKEUP;
  286. pd->attach_dev = cpg_mstp_attach_dev;
  287. pd->detach_dev = cpg_mstp_detach_dev;
  288. pm_genpd_init(pd, &pm_domain_always_on_gov, false);
  289. of_genpd_add_provider_simple(np, pd);
  290. }