clk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * TI clock support
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. *
  6. * Tero Kristo <t-kristo@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/clkdev.h>
  20. #include <linux/clk/ti.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/list.h>
  24. #include <linux/regmap.h>
  25. #include <linux/bootmem.h>
  26. #include "clock.h"
  27. #undef pr_fmt
  28. #define pr_fmt(fmt) "%s: " fmt, __func__
  29. struct ti_clk_ll_ops *ti_clk_ll_ops;
  30. static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
  31. static struct ti_clk_features ti_clk_features;
  32. struct clk_iomap {
  33. struct regmap *regmap;
  34. void __iomem *mem;
  35. };
  36. static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
  37. static void clk_memmap_writel(u32 val, void __iomem *reg)
  38. {
  39. struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
  40. struct clk_iomap *io = clk_memmaps[r->index];
  41. if (io->regmap)
  42. regmap_write(io->regmap, r->offset, val);
  43. else
  44. writel_relaxed(val, io->mem + r->offset);
  45. }
  46. static u32 clk_memmap_readl(void __iomem *reg)
  47. {
  48. u32 val;
  49. struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
  50. struct clk_iomap *io = clk_memmaps[r->index];
  51. if (io->regmap)
  52. regmap_read(io->regmap, r->offset, &val);
  53. else
  54. val = readl_relaxed(io->mem + r->offset);
  55. return val;
  56. }
  57. /**
  58. * ti_clk_setup_ll_ops - setup low level clock operations
  59. * @ops: low level clock ops descriptor
  60. *
  61. * Sets up low level clock operations for TI clock driver. This is used
  62. * to provide various callbacks for the clock driver towards platform
  63. * specific code. Returns 0 on success, -EBUSY if ll_ops have been
  64. * registered already.
  65. */
  66. int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
  67. {
  68. if (ti_clk_ll_ops) {
  69. pr_err("Attempt to register ll_ops multiple times.\n");
  70. return -EBUSY;
  71. }
  72. ti_clk_ll_ops = ops;
  73. ops->clk_readl = clk_memmap_readl;
  74. ops->clk_writel = clk_memmap_writel;
  75. return 0;
  76. }
  77. /**
  78. * ti_dt_clocks_register - register DT alias clocks during boot
  79. * @oclks: list of clocks to register
  80. *
  81. * Register alias or non-standard DT clock entries during boot. By
  82. * default, DT clocks are found based on their node name. If any
  83. * additional con-id / dev-id -> clock mapping is required, use this
  84. * function to list these.
  85. */
  86. void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
  87. {
  88. struct ti_dt_clk *c;
  89. struct device_node *node;
  90. struct clk *clk;
  91. struct of_phandle_args clkspec;
  92. for (c = oclks; c->node_name != NULL; c++) {
  93. node = of_find_node_by_name(NULL, c->node_name);
  94. clkspec.np = node;
  95. clk = of_clk_get_from_provider(&clkspec);
  96. if (!IS_ERR(clk)) {
  97. c->lk.clk = clk;
  98. clkdev_add(&c->lk);
  99. } else {
  100. pr_warn("failed to lookup clock node %s\n",
  101. c->node_name);
  102. }
  103. }
  104. }
  105. struct clk_init_item {
  106. struct device_node *node;
  107. struct clk_hw *hw;
  108. ti_of_clk_init_cb_t func;
  109. struct list_head link;
  110. };
  111. static LIST_HEAD(retry_list);
  112. /**
  113. * ti_clk_retry_init - retries a failed clock init at later phase
  114. * @node: device not for the clock
  115. * @hw: partially initialized clk_hw struct for the clock
  116. * @func: init function to be called for the clock
  117. *
  118. * Adds a failed clock init to the retry list. The retry list is parsed
  119. * once all the other clocks have been initialized.
  120. */
  121. int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
  122. ti_of_clk_init_cb_t func)
  123. {
  124. struct clk_init_item *retry;
  125. pr_debug("%s: adding to retry list...\n", node->name);
  126. retry = kzalloc(sizeof(*retry), GFP_KERNEL);
  127. if (!retry)
  128. return -ENOMEM;
  129. retry->node = node;
  130. retry->func = func;
  131. retry->hw = hw;
  132. list_add(&retry->link, &retry_list);
  133. return 0;
  134. }
  135. /**
  136. * ti_clk_get_reg_addr - get register address for a clock register
  137. * @node: device node for the clock
  138. * @index: register index from the clock node
  139. *
  140. * Builds clock register address from device tree information. This
  141. * is a struct of type clk_omap_reg. Returns a pointer to the register
  142. * address, or a pointer error value in failure.
  143. */
  144. void __iomem *ti_clk_get_reg_addr(struct device_node *node, int index)
  145. {
  146. struct clk_omap_reg *reg;
  147. u32 val;
  148. u32 tmp;
  149. int i;
  150. reg = (struct clk_omap_reg *)&tmp;
  151. for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
  152. if (clocks_node_ptr[i] == node->parent)
  153. break;
  154. }
  155. if (i == CLK_MAX_MEMMAPS) {
  156. pr_err("clk-provider not found for %s!\n", node->name);
  157. return IOMEM_ERR_PTR(-ENOENT);
  158. }
  159. reg->index = i;
  160. if (of_property_read_u32_index(node, "reg", index, &val)) {
  161. pr_err("%s must have reg[%d]!\n", node->name, index);
  162. return IOMEM_ERR_PTR(-EINVAL);
  163. }
  164. reg->offset = val;
  165. return (__force void __iomem *)tmp;
  166. }
  167. /**
  168. * omap2_clk_provider_init - init master clock provider
  169. * @parent: master node
  170. * @index: internal index for clk_reg_ops
  171. * @syscon: syscon regmap pointer for accessing clock registers
  172. * @mem: iomem pointer for the clock provider memory area, only used if
  173. * syscon is not provided
  174. *
  175. * Initializes a master clock IP block. This basically sets up the
  176. * mapping from clocks node to the memory map index. All the clocks
  177. * are then initialized through the common of_clk_init call, and the
  178. * clocks will access their memory maps based on the node layout.
  179. * Returns 0 in success.
  180. */
  181. int __init omap2_clk_provider_init(struct device_node *parent, int index,
  182. struct regmap *syscon, void __iomem *mem)
  183. {
  184. struct device_node *clocks;
  185. struct clk_iomap *io;
  186. /* get clocks for this parent */
  187. clocks = of_get_child_by_name(parent, "clocks");
  188. if (!clocks) {
  189. pr_err("%s missing 'clocks' child node.\n", parent->name);
  190. return -EINVAL;
  191. }
  192. /* add clocks node info */
  193. clocks_node_ptr[index] = clocks;
  194. io = kzalloc(sizeof(*io), GFP_KERNEL);
  195. if (!io)
  196. return -ENOMEM;
  197. io->regmap = syscon;
  198. io->mem = mem;
  199. clk_memmaps[index] = io;
  200. return 0;
  201. }
  202. /**
  203. * omap2_clk_legacy_provider_init - initialize a legacy clock provider
  204. * @index: index for the clock provider
  205. * @mem: iomem pointer for the clock provider memory area
  206. *
  207. * Initializes a legacy clock provider memory mapping.
  208. */
  209. void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
  210. {
  211. struct clk_iomap *io;
  212. io = memblock_virt_alloc(sizeof(*io), 0);
  213. io->mem = mem;
  214. clk_memmaps[index] = io;
  215. }
  216. /**
  217. * ti_dt_clk_init_retry_clks - init clocks from the retry list
  218. *
  219. * Initializes any clocks that have failed to initialize before,
  220. * reasons being missing parent node(s) during earlier init. This
  221. * typically happens only for DPLLs which need to have both of their
  222. * parent clocks ready during init.
  223. */
  224. void ti_dt_clk_init_retry_clks(void)
  225. {
  226. struct clk_init_item *retry;
  227. struct clk_init_item *tmp;
  228. int retries = 5;
  229. while (!list_empty(&retry_list) && retries) {
  230. list_for_each_entry_safe(retry, tmp, &retry_list, link) {
  231. pr_debug("retry-init: %s\n", retry->node->name);
  232. retry->func(retry->hw, retry->node);
  233. list_del(&retry->link);
  234. kfree(retry);
  235. }
  236. retries--;
  237. }
  238. }
  239. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
  240. void __init ti_clk_patch_legacy_clks(struct ti_clk **patch)
  241. {
  242. while (*patch) {
  243. memcpy((*patch)->patch, *patch, sizeof(**patch));
  244. patch++;
  245. }
  246. }
  247. struct clk __init *ti_clk_register_clk(struct ti_clk *setup)
  248. {
  249. struct clk *clk;
  250. struct ti_clk_fixed *fixed;
  251. struct ti_clk_fixed_factor *fixed_factor;
  252. struct clk_hw *clk_hw;
  253. if (setup->clk)
  254. return setup->clk;
  255. switch (setup->type) {
  256. case TI_CLK_FIXED:
  257. fixed = setup->data;
  258. clk = clk_register_fixed_rate(NULL, setup->name, NULL, 0,
  259. fixed->frequency);
  260. break;
  261. case TI_CLK_MUX:
  262. clk = ti_clk_register_mux(setup);
  263. break;
  264. case TI_CLK_DIVIDER:
  265. clk = ti_clk_register_divider(setup);
  266. break;
  267. case TI_CLK_COMPOSITE:
  268. clk = ti_clk_register_composite(setup);
  269. break;
  270. case TI_CLK_FIXED_FACTOR:
  271. fixed_factor = setup->data;
  272. clk = clk_register_fixed_factor(NULL, setup->name,
  273. fixed_factor->parent,
  274. 0, fixed_factor->mult,
  275. fixed_factor->div);
  276. break;
  277. case TI_CLK_GATE:
  278. clk = ti_clk_register_gate(setup);
  279. break;
  280. case TI_CLK_DPLL:
  281. clk = ti_clk_register_dpll(setup);
  282. break;
  283. default:
  284. pr_err("bad type for %s!\n", setup->name);
  285. clk = ERR_PTR(-EINVAL);
  286. }
  287. if (!IS_ERR(clk)) {
  288. setup->clk = clk;
  289. if (setup->clkdm_name) {
  290. clk_hw = __clk_get_hw(clk);
  291. if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) {
  292. pr_warn("can't setup clkdm for basic clk %s\n",
  293. setup->name);
  294. } else {
  295. to_clk_hw_omap(clk_hw)->clkdm_name =
  296. setup->clkdm_name;
  297. omap2_init_clk_clkdm(clk_hw);
  298. }
  299. }
  300. }
  301. return clk;
  302. }
  303. int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
  304. {
  305. struct clk *clk;
  306. bool retry;
  307. struct ti_clk_alias *retry_clk;
  308. struct ti_clk_alias *tmp;
  309. while (clks->clk) {
  310. clk = ti_clk_register_clk(clks->clk);
  311. if (IS_ERR(clk)) {
  312. if (PTR_ERR(clk) == -EAGAIN) {
  313. list_add(&clks->link, &retry_list);
  314. } else {
  315. pr_err("register for %s failed: %ld\n",
  316. clks->clk->name, PTR_ERR(clk));
  317. return PTR_ERR(clk);
  318. }
  319. } else {
  320. clks->lk.clk = clk;
  321. clkdev_add(&clks->lk);
  322. }
  323. clks++;
  324. }
  325. retry = true;
  326. while (!list_empty(&retry_list) && retry) {
  327. retry = false;
  328. list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
  329. pr_debug("retry-init: %s\n", retry_clk->clk->name);
  330. clk = ti_clk_register_clk(retry_clk->clk);
  331. if (IS_ERR(clk)) {
  332. if (PTR_ERR(clk) == -EAGAIN) {
  333. continue;
  334. } else {
  335. pr_err("register for %s failed: %ld\n",
  336. retry_clk->clk->name,
  337. PTR_ERR(clk));
  338. return PTR_ERR(clk);
  339. }
  340. } else {
  341. retry = true;
  342. retry_clk->lk.clk = clk;
  343. clkdev_add(&retry_clk->lk);
  344. list_del(&retry_clk->link);
  345. }
  346. }
  347. }
  348. return 0;
  349. }
  350. #endif
  351. /**
  352. * ti_clk_setup_features - setup clock features flags
  353. * @features: features definition to use
  354. *
  355. * Initializes the clock driver features flags based on platform
  356. * provided data. No return value.
  357. */
  358. void __init ti_clk_setup_features(struct ti_clk_features *features)
  359. {
  360. memcpy(&ti_clk_features, features, sizeof(*features));
  361. }
  362. /**
  363. * ti_clk_get_features - get clock driver features flags
  364. *
  365. * Get TI clock driver features description. Returns a pointer
  366. * to the current feature setup.
  367. */
  368. const struct ti_clk_features *ti_clk_get_features(void)
  369. {
  370. return &ti_clk_features;
  371. }
  372. /**
  373. * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
  374. * @clk_names: ptr to an array of strings of clock names to enable
  375. * @num_clocks: number of clock names in @clk_names
  376. *
  377. * Prepare and enable a list of clocks, named by @clk_names. No
  378. * return value. XXX Deprecated; only needed until these clocks are
  379. * properly claimed and enabled by the drivers or core code that uses
  380. * them. XXX What code disables & calls clk_put on these clocks?
  381. */
  382. void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
  383. {
  384. struct clk *init_clk;
  385. int i;
  386. for (i = 0; i < num_clocks; i++) {
  387. init_clk = clk_get(NULL, clk_names[i]);
  388. if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
  389. clk_names[i]))
  390. continue;
  391. clk_prepare_enable(init_clk);
  392. }
  393. }