clk-mstp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. 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. pr_err("%s: failed to allocate MSTP clock.\n", __func__);
  136. return ERR_PTR(-ENOMEM);
  137. }
  138. init.name = name;
  139. init.ops = &cpg_mstp_clock_ops;
  140. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  141. /* INTC-SYS is the module clock of the GIC, and must not be disabled */
  142. if (!strcmp(name, "intc-sys")) {
  143. pr_debug("MSTP %s setting CLK_IS_CRITICAL\n", name);
  144. init.flags |= CLK_IS_CRITICAL;
  145. }
  146. init.parent_names = &parent_name;
  147. init.num_parents = 1;
  148. clock->bit_index = index;
  149. clock->group = group;
  150. clock->hw.init = &init;
  151. clk = clk_register(NULL, &clock->hw);
  152. if (IS_ERR(clk))
  153. kfree(clock);
  154. return clk;
  155. }
  156. static void __init cpg_mstp_clocks_init(struct device_node *np)
  157. {
  158. struct mstp_clock_group *group;
  159. const char *idxname;
  160. struct clk **clks;
  161. unsigned int i;
  162. group = kzalloc(sizeof(*group), GFP_KERNEL);
  163. clks = kmalloc_array(MSTP_MAX_CLOCKS, sizeof(*clks), GFP_KERNEL);
  164. if (group == NULL || clks == NULL) {
  165. kfree(group);
  166. kfree(clks);
  167. pr_err("%s: failed to allocate group\n", __func__);
  168. return;
  169. }
  170. spin_lock_init(&group->lock);
  171. group->data.clks = clks;
  172. group->smstpcr = of_iomap(np, 0);
  173. group->mstpsr = of_iomap(np, 1);
  174. if (group->smstpcr == NULL) {
  175. pr_err("%s: failed to remap SMSTPCR\n", __func__);
  176. kfree(group);
  177. kfree(clks);
  178. return;
  179. }
  180. if (of_device_is_compatible(np, "renesas,r7s72100-mstp-clocks"))
  181. group->width_8bit = true;
  182. for (i = 0; i < MSTP_MAX_CLOCKS; ++i)
  183. clks[i] = ERR_PTR(-ENOENT);
  184. if (of_find_property(np, "clock-indices", &i))
  185. idxname = "clock-indices";
  186. else
  187. idxname = "renesas,clock-indices";
  188. for (i = 0; i < MSTP_MAX_CLOCKS; ++i) {
  189. const char *parent_name;
  190. const char *name;
  191. u32 clkidx;
  192. int ret;
  193. /* Skip clocks with no name. */
  194. ret = of_property_read_string_index(np, "clock-output-names",
  195. i, &name);
  196. if (ret < 0 || strlen(name) == 0)
  197. continue;
  198. parent_name = of_clk_get_parent_name(np, i);
  199. ret = of_property_read_u32_index(np, idxname, i, &clkidx);
  200. if (parent_name == NULL || ret < 0)
  201. break;
  202. if (clkidx >= MSTP_MAX_CLOCKS) {
  203. pr_err("%s: invalid clock %s %s index %u\n",
  204. __func__, np->name, name, clkidx);
  205. continue;
  206. }
  207. clks[clkidx] = cpg_mstp_clock_register(name, parent_name,
  208. clkidx, group);
  209. if (!IS_ERR(clks[clkidx])) {
  210. group->data.clk_num = max(group->data.clk_num,
  211. clkidx + 1);
  212. /*
  213. * Register a clkdev to let board code retrieve the
  214. * clock by name and register aliases for non-DT
  215. * devices.
  216. *
  217. * FIXME: Remove this when all devices that require a
  218. * clock will be instantiated from DT.
  219. */
  220. clk_register_clkdev(clks[clkidx], name, NULL);
  221. } else {
  222. pr_err("%s: failed to register %s %s clock (%ld)\n",
  223. __func__, np->name, name, PTR_ERR(clks[clkidx]));
  224. }
  225. }
  226. of_clk_add_provider(np, of_clk_src_onecell_get, &group->data);
  227. }
  228. CLK_OF_DECLARE(cpg_mstp_clks, "renesas,cpg-mstp-clocks", cpg_mstp_clocks_init);
  229. int cpg_mstp_attach_dev(struct generic_pm_domain *unused, struct device *dev)
  230. {
  231. struct device_node *np = dev->of_node;
  232. struct of_phandle_args clkspec;
  233. struct clk *clk;
  234. int i = 0;
  235. int error;
  236. while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
  237. &clkspec)) {
  238. if (of_device_is_compatible(clkspec.np,
  239. "renesas,cpg-mstp-clocks"))
  240. goto found;
  241. /* BSC on r8a73a4/sh73a0 uses zb_clk instead of an mstp clock */
  242. if (!strcmp(clkspec.np->name, "zb_clk"))
  243. goto found;
  244. of_node_put(clkspec.np);
  245. i++;
  246. }
  247. return 0;
  248. found:
  249. clk = of_clk_get_from_provider(&clkspec);
  250. of_node_put(clkspec.np);
  251. if (IS_ERR(clk))
  252. return PTR_ERR(clk);
  253. error = pm_clk_create(dev);
  254. if (error) {
  255. dev_err(dev, "pm_clk_create failed %d\n", error);
  256. goto fail_put;
  257. }
  258. error = pm_clk_add_clk(dev, clk);
  259. if (error) {
  260. dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error);
  261. goto fail_destroy;
  262. }
  263. return 0;
  264. fail_destroy:
  265. pm_clk_destroy(dev);
  266. fail_put:
  267. clk_put(clk);
  268. return error;
  269. }
  270. void cpg_mstp_detach_dev(struct generic_pm_domain *unused, struct device *dev)
  271. {
  272. if (!list_empty(&dev->power.subsys_data->clock_list))
  273. pm_clk_destroy(dev);
  274. }
  275. void __init cpg_mstp_add_clk_domain(struct device_node *np)
  276. {
  277. struct generic_pm_domain *pd;
  278. u32 ncells;
  279. if (of_property_read_u32(np, "#power-domain-cells", &ncells)) {
  280. pr_warn("%s lacks #power-domain-cells\n", np->full_name);
  281. return;
  282. }
  283. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  284. if (!pd)
  285. return;
  286. pd->name = np->name;
  287. pd->flags = GENPD_FLAG_PM_CLK;
  288. pd->attach_dev = cpg_mstp_attach_dev;
  289. pd->detach_dev = cpg_mstp_detach_dev;
  290. pm_genpd_init(pd, &pm_domain_always_on_gov, false);
  291. of_genpd_add_provider_simple(np, pd);
  292. }