clk.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/of.h>
  19. #include <linux/clk/tegra.h>
  20. #include "clk.h"
  21. #define CLK_OUT_ENB_L 0x010
  22. #define CLK_OUT_ENB_H 0x014
  23. #define CLK_OUT_ENB_U 0x018
  24. #define CLK_OUT_ENB_V 0x360
  25. #define CLK_OUT_ENB_W 0x364
  26. #define CLK_OUT_ENB_X 0x280
  27. #define CLK_OUT_ENB_SET_L 0x320
  28. #define CLK_OUT_ENB_CLR_L 0x324
  29. #define CLK_OUT_ENB_SET_H 0x328
  30. #define CLK_OUT_ENB_CLR_H 0x32c
  31. #define CLK_OUT_ENB_SET_U 0x330
  32. #define CLK_OUT_ENB_CLR_U 0x334
  33. #define CLK_OUT_ENB_SET_V 0x440
  34. #define CLK_OUT_ENB_CLR_V 0x444
  35. #define CLK_OUT_ENB_SET_W 0x448
  36. #define CLK_OUT_ENB_CLR_W 0x44c
  37. #define CLK_OUT_ENB_SET_X 0x284
  38. #define CLK_OUT_ENB_CLR_X 0x288
  39. #define RST_DEVICES_L 0x004
  40. #define RST_DEVICES_H 0x008
  41. #define RST_DEVICES_U 0x00C
  42. #define RST_DFLL_DVCO 0x2F4
  43. #define RST_DEVICES_V 0x358
  44. #define RST_DEVICES_W 0x35C
  45. #define RST_DEVICES_X 0x28C
  46. #define RST_DEVICES_SET_L 0x300
  47. #define RST_DEVICES_CLR_L 0x304
  48. #define RST_DEVICES_SET_H 0x308
  49. #define RST_DEVICES_CLR_H 0x30c
  50. #define RST_DEVICES_SET_U 0x310
  51. #define RST_DEVICES_CLR_U 0x314
  52. #define RST_DEVICES_SET_V 0x430
  53. #define RST_DEVICES_CLR_V 0x434
  54. #define RST_DEVICES_SET_W 0x438
  55. #define RST_DEVICES_CLR_W 0x43c
  56. #define RST_DEVICES_SET_X 0x290
  57. #define RST_DEVICES_CLR_X 0x294
  58. /* Global data of Tegra CPU CAR ops */
  59. static struct tegra_cpu_car_ops dummy_car_ops;
  60. struct tegra_cpu_car_ops *tegra_cpu_car_ops = &dummy_car_ops;
  61. int *periph_clk_enb_refcnt;
  62. static int periph_banks;
  63. static struct clk **clks;
  64. static int clk_num;
  65. static struct clk_onecell_data clk_data;
  66. static struct tegra_clk_periph_regs periph_regs[] = {
  67. [0] = {
  68. .enb_reg = CLK_OUT_ENB_L,
  69. .enb_set_reg = CLK_OUT_ENB_SET_L,
  70. .enb_clr_reg = CLK_OUT_ENB_CLR_L,
  71. .rst_reg = RST_DEVICES_L,
  72. .rst_set_reg = RST_DEVICES_SET_L,
  73. .rst_clr_reg = RST_DEVICES_CLR_L,
  74. },
  75. [1] = {
  76. .enb_reg = CLK_OUT_ENB_H,
  77. .enb_set_reg = CLK_OUT_ENB_SET_H,
  78. .enb_clr_reg = CLK_OUT_ENB_CLR_H,
  79. .rst_reg = RST_DEVICES_H,
  80. .rst_set_reg = RST_DEVICES_SET_H,
  81. .rst_clr_reg = RST_DEVICES_CLR_H,
  82. },
  83. [2] = {
  84. .enb_reg = CLK_OUT_ENB_U,
  85. .enb_set_reg = CLK_OUT_ENB_SET_U,
  86. .enb_clr_reg = CLK_OUT_ENB_CLR_U,
  87. .rst_reg = RST_DEVICES_U,
  88. .rst_set_reg = RST_DEVICES_SET_U,
  89. .rst_clr_reg = RST_DEVICES_CLR_U,
  90. },
  91. [3] = {
  92. .enb_reg = CLK_OUT_ENB_V,
  93. .enb_set_reg = CLK_OUT_ENB_SET_V,
  94. .enb_clr_reg = CLK_OUT_ENB_CLR_V,
  95. .rst_reg = RST_DEVICES_V,
  96. .rst_set_reg = RST_DEVICES_SET_V,
  97. .rst_clr_reg = RST_DEVICES_CLR_V,
  98. },
  99. [4] = {
  100. .enb_reg = CLK_OUT_ENB_W,
  101. .enb_set_reg = CLK_OUT_ENB_SET_W,
  102. .enb_clr_reg = CLK_OUT_ENB_CLR_W,
  103. .rst_reg = RST_DEVICES_W,
  104. .rst_set_reg = RST_DEVICES_SET_W,
  105. .rst_clr_reg = RST_DEVICES_CLR_W,
  106. },
  107. [5] = {
  108. .enb_reg = CLK_OUT_ENB_X,
  109. .enb_set_reg = CLK_OUT_ENB_SET_X,
  110. .enb_clr_reg = CLK_OUT_ENB_CLR_X,
  111. .rst_reg = RST_DEVICES_X,
  112. .rst_set_reg = RST_DEVICES_SET_X,
  113. .rst_clr_reg = RST_DEVICES_CLR_X,
  114. },
  115. };
  116. struct tegra_clk_periph_regs *get_reg_bank(int clkid)
  117. {
  118. int reg_bank = clkid / 32;
  119. if (reg_bank < periph_banks)
  120. return &periph_regs[reg_bank];
  121. else {
  122. WARN_ON(1);
  123. return NULL;
  124. }
  125. }
  126. struct clk ** __init tegra_clk_init(int num, int banks)
  127. {
  128. if (WARN_ON(banks > ARRAY_SIZE(periph_regs)))
  129. return NULL;
  130. periph_clk_enb_refcnt = kzalloc(32 * banks *
  131. sizeof(*periph_clk_enb_refcnt), GFP_KERNEL);
  132. if (!periph_clk_enb_refcnt)
  133. return NULL;
  134. periph_banks = banks;
  135. clks = kzalloc(num * sizeof(struct clk *), GFP_KERNEL);
  136. if (!clks)
  137. kfree(periph_clk_enb_refcnt);
  138. clk_num = num;
  139. return clks;
  140. }
  141. void __init tegra_init_dup_clks(struct tegra_clk_duplicate *dup_list,
  142. struct clk *clks[], int clk_max)
  143. {
  144. struct clk *clk;
  145. for (; dup_list->clk_id < clk_max; dup_list++) {
  146. clk = clks[dup_list->clk_id];
  147. dup_list->lookup.clk = clk;
  148. clkdev_add(&dup_list->lookup);
  149. }
  150. }
  151. void __init tegra_init_from_table(struct tegra_clk_init_table *tbl,
  152. struct clk *clks[], int clk_max)
  153. {
  154. struct clk *clk;
  155. for (; tbl->clk_id < clk_max; tbl++) {
  156. clk = clks[tbl->clk_id];
  157. if (IS_ERR_OR_NULL(clk))
  158. return;
  159. if (tbl->parent_id < clk_max) {
  160. struct clk *parent = clks[tbl->parent_id];
  161. if (clk_set_parent(clk, parent)) {
  162. pr_err("%s: Failed to set parent %s of %s\n",
  163. __func__, __clk_get_name(parent),
  164. __clk_get_name(clk));
  165. WARN_ON(1);
  166. }
  167. }
  168. if (tbl->rate)
  169. if (clk_set_rate(clk, tbl->rate)) {
  170. pr_err("%s: Failed to set rate %lu of %s\n",
  171. __func__, tbl->rate,
  172. __clk_get_name(clk));
  173. WARN_ON(1);
  174. }
  175. if (tbl->state)
  176. if (clk_prepare_enable(clk)) {
  177. pr_err("%s: Failed to enable %s\n", __func__,
  178. __clk_get_name(clk));
  179. WARN_ON(1);
  180. }
  181. }
  182. }
  183. void __init tegra_add_of_provider(struct device_node *np)
  184. {
  185. int i;
  186. for (i = 0; i < clk_num; i++) {
  187. if (IS_ERR(clks[i])) {
  188. pr_err
  189. ("Tegra clk %d: register failed with %ld\n",
  190. i, PTR_ERR(clks[i]));
  191. }
  192. if (!clks[i])
  193. clks[i] = ERR_PTR(-EINVAL);
  194. }
  195. clk_data.clks = clks;
  196. clk_data.clk_num = clk_num;
  197. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
  198. }
  199. void __init tegra_register_devclks(struct tegra_devclk *dev_clks, int num)
  200. {
  201. int i;
  202. for (i = 0; i < num; i++, dev_clks++)
  203. clk_register_clkdev(clks[dev_clks->dt_id], dev_clks->con_id,
  204. dev_clks->dev_id);
  205. }
  206. struct clk ** __init tegra_lookup_dt_id(int clk_id,
  207. struct tegra_clk *tegra_clk)
  208. {
  209. if (tegra_clk[clk_id].present)
  210. return &clks[tegra_clk[clk_id].dt_id];
  211. else
  212. return NULL;
  213. }
  214. tegra_clk_apply_init_table_func tegra_clk_apply_init_table;
  215. void __init tegra_clocks_apply_init_table(void)
  216. {
  217. if (!tegra_clk_apply_init_table)
  218. return;
  219. tegra_clk_apply_init_table();
  220. }