clk.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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-provider.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/clk/ti.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/list.h>
  23. #undef pr_fmt
  24. #define pr_fmt(fmt) "%s: " fmt, __func__
  25. struct ti_clk_ll_ops *ti_clk_ll_ops;
  26. static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
  27. /**
  28. * ti_dt_clocks_register - register DT alias clocks during boot
  29. * @oclks: list of clocks to register
  30. *
  31. * Register alias or non-standard DT clock entries during boot. By
  32. * default, DT clocks are found based on their node name. If any
  33. * additional con-id / dev-id -> clock mapping is required, use this
  34. * function to list these.
  35. */
  36. void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
  37. {
  38. struct ti_dt_clk *c;
  39. struct device_node *node;
  40. struct clk *clk;
  41. struct of_phandle_args clkspec;
  42. for (c = oclks; c->node_name != NULL; c++) {
  43. node = of_find_node_by_name(NULL, c->node_name);
  44. clkspec.np = node;
  45. clk = of_clk_get_from_provider(&clkspec);
  46. if (!IS_ERR(clk)) {
  47. c->lk.clk = clk;
  48. clkdev_add(&c->lk);
  49. } else {
  50. pr_warn("failed to lookup clock node %s\n",
  51. c->node_name);
  52. }
  53. }
  54. }
  55. struct clk_init_item {
  56. struct device_node *node;
  57. struct clk_hw *hw;
  58. ti_of_clk_init_cb_t func;
  59. struct list_head link;
  60. };
  61. static LIST_HEAD(retry_list);
  62. /**
  63. * ti_clk_retry_init - retries a failed clock init at later phase
  64. * @node: device not for the clock
  65. * @hw: partially initialized clk_hw struct for the clock
  66. * @func: init function to be called for the clock
  67. *
  68. * Adds a failed clock init to the retry list. The retry list is parsed
  69. * once all the other clocks have been initialized.
  70. */
  71. int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
  72. ti_of_clk_init_cb_t func)
  73. {
  74. struct clk_init_item *retry;
  75. pr_debug("%s: adding to retry list...\n", node->name);
  76. retry = kzalloc(sizeof(*retry), GFP_KERNEL);
  77. if (!retry)
  78. return -ENOMEM;
  79. retry->node = node;
  80. retry->func = func;
  81. retry->hw = hw;
  82. list_add(&retry->link, &retry_list);
  83. return 0;
  84. }
  85. /**
  86. * ti_clk_get_reg_addr - get register address for a clock register
  87. * @node: device node for the clock
  88. * @index: register index from the clock node
  89. *
  90. * Builds clock register address from device tree information. This
  91. * is a struct of type clk_omap_reg.
  92. */
  93. void __iomem *ti_clk_get_reg_addr(struct device_node *node, int index)
  94. {
  95. struct clk_omap_reg *reg;
  96. u32 val;
  97. u32 tmp;
  98. int i;
  99. reg = (struct clk_omap_reg *)&tmp;
  100. for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
  101. if (clocks_node_ptr[i] == node->parent)
  102. break;
  103. }
  104. if (i == CLK_MAX_MEMMAPS) {
  105. pr_err("clk-provider not found for %s!\n", node->name);
  106. return NULL;
  107. }
  108. reg->index = i;
  109. if (of_property_read_u32_index(node, "reg", index, &val)) {
  110. pr_err("%s must have reg[%d]!\n", node->name, index);
  111. return NULL;
  112. }
  113. reg->offset = val;
  114. return (void __iomem *)tmp;
  115. }
  116. /**
  117. * ti_dt_clk_init_provider - init master clock provider
  118. * @parent: master node
  119. * @index: internal index for clk_reg_ops
  120. *
  121. * Initializes a master clock IP block. This basically sets up the
  122. * mapping from clocks node to the memory map index. All the clocks
  123. * are then initialized through the common of_clk_init call, and the
  124. * clocks will access their memory maps based on the node layout.
  125. */
  126. void ti_dt_clk_init_provider(struct device_node *parent, int index)
  127. {
  128. struct device_node *clocks;
  129. /* get clocks for this parent */
  130. clocks = of_get_child_by_name(parent, "clocks");
  131. if (!clocks) {
  132. pr_err("%s missing 'clocks' child node.\n", parent->name);
  133. return;
  134. }
  135. /* add clocks node info */
  136. clocks_node_ptr[index] = clocks;
  137. }
  138. /**
  139. * ti_dt_clk_init_retry_clks - init clocks from the retry list
  140. *
  141. * Initializes any clocks that have failed to initialize before,
  142. * reasons being missing parent node(s) during earlier init. This
  143. * typically happens only for DPLLs which need to have both of their
  144. * parent clocks ready during init.
  145. */
  146. void ti_dt_clk_init_retry_clks(void)
  147. {
  148. struct clk_init_item *retry;
  149. struct clk_init_item *tmp;
  150. int retries = 5;
  151. while (!list_empty(&retry_list) && retries) {
  152. list_for_each_entry_safe(retry, tmp, &retry_list, link) {
  153. pr_debug("retry-init: %s\n", retry->node->name);
  154. retry->func(retry->hw, retry->node);
  155. list_del(&retry->link);
  156. kfree(retry);
  157. }
  158. retries--;
  159. }
  160. }