clk.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <linux/reset-controller.h>
  21. #include <linux/tegra-soc.h>
  22. #include "clk.h"
  23. #define CLK_OUT_ENB_L 0x010
  24. #define CLK_OUT_ENB_H 0x014
  25. #define CLK_OUT_ENB_U 0x018
  26. #define CLK_OUT_ENB_V 0x360
  27. #define CLK_OUT_ENB_W 0x364
  28. #define CLK_OUT_ENB_X 0x280
  29. #define CLK_OUT_ENB_SET_L 0x320
  30. #define CLK_OUT_ENB_CLR_L 0x324
  31. #define CLK_OUT_ENB_SET_H 0x328
  32. #define CLK_OUT_ENB_CLR_H 0x32c
  33. #define CLK_OUT_ENB_SET_U 0x330
  34. #define CLK_OUT_ENB_CLR_U 0x334
  35. #define CLK_OUT_ENB_SET_V 0x440
  36. #define CLK_OUT_ENB_CLR_V 0x444
  37. #define CLK_OUT_ENB_SET_W 0x448
  38. #define CLK_OUT_ENB_CLR_W 0x44c
  39. #define CLK_OUT_ENB_SET_X 0x284
  40. #define CLK_OUT_ENB_CLR_X 0x288
  41. #define RST_DEVICES_L 0x004
  42. #define RST_DEVICES_H 0x008
  43. #define RST_DEVICES_U 0x00C
  44. #define RST_DFLL_DVCO 0x2F4
  45. #define RST_DEVICES_V 0x358
  46. #define RST_DEVICES_W 0x35C
  47. #define RST_DEVICES_X 0x28C
  48. #define RST_DEVICES_SET_L 0x300
  49. #define RST_DEVICES_CLR_L 0x304
  50. #define RST_DEVICES_SET_H 0x308
  51. #define RST_DEVICES_CLR_H 0x30c
  52. #define RST_DEVICES_SET_U 0x310
  53. #define RST_DEVICES_CLR_U 0x314
  54. #define RST_DEVICES_SET_V 0x430
  55. #define RST_DEVICES_CLR_V 0x434
  56. #define RST_DEVICES_SET_W 0x438
  57. #define RST_DEVICES_CLR_W 0x43c
  58. #define RST_DEVICES_SET_X 0x290
  59. #define RST_DEVICES_CLR_X 0x294
  60. /* Global data of Tegra CPU CAR ops */
  61. static struct tegra_cpu_car_ops dummy_car_ops;
  62. struct tegra_cpu_car_ops *tegra_cpu_car_ops = &dummy_car_ops;
  63. int *periph_clk_enb_refcnt;
  64. static int periph_banks;
  65. static struct clk **clks;
  66. static int clk_num;
  67. static struct clk_onecell_data clk_data;
  68. static struct tegra_clk_periph_regs periph_regs[] = {
  69. [0] = {
  70. .enb_reg = CLK_OUT_ENB_L,
  71. .enb_set_reg = CLK_OUT_ENB_SET_L,
  72. .enb_clr_reg = CLK_OUT_ENB_CLR_L,
  73. .rst_reg = RST_DEVICES_L,
  74. .rst_set_reg = RST_DEVICES_SET_L,
  75. .rst_clr_reg = RST_DEVICES_CLR_L,
  76. },
  77. [1] = {
  78. .enb_reg = CLK_OUT_ENB_H,
  79. .enb_set_reg = CLK_OUT_ENB_SET_H,
  80. .enb_clr_reg = CLK_OUT_ENB_CLR_H,
  81. .rst_reg = RST_DEVICES_H,
  82. .rst_set_reg = RST_DEVICES_SET_H,
  83. .rst_clr_reg = RST_DEVICES_CLR_H,
  84. },
  85. [2] = {
  86. .enb_reg = CLK_OUT_ENB_U,
  87. .enb_set_reg = CLK_OUT_ENB_SET_U,
  88. .enb_clr_reg = CLK_OUT_ENB_CLR_U,
  89. .rst_reg = RST_DEVICES_U,
  90. .rst_set_reg = RST_DEVICES_SET_U,
  91. .rst_clr_reg = RST_DEVICES_CLR_U,
  92. },
  93. [3] = {
  94. .enb_reg = CLK_OUT_ENB_V,
  95. .enb_set_reg = CLK_OUT_ENB_SET_V,
  96. .enb_clr_reg = CLK_OUT_ENB_CLR_V,
  97. .rst_reg = RST_DEVICES_V,
  98. .rst_set_reg = RST_DEVICES_SET_V,
  99. .rst_clr_reg = RST_DEVICES_CLR_V,
  100. },
  101. [4] = {
  102. .enb_reg = CLK_OUT_ENB_W,
  103. .enb_set_reg = CLK_OUT_ENB_SET_W,
  104. .enb_clr_reg = CLK_OUT_ENB_CLR_W,
  105. .rst_reg = RST_DEVICES_W,
  106. .rst_set_reg = RST_DEVICES_SET_W,
  107. .rst_clr_reg = RST_DEVICES_CLR_W,
  108. },
  109. [5] = {
  110. .enb_reg = CLK_OUT_ENB_X,
  111. .enb_set_reg = CLK_OUT_ENB_SET_X,
  112. .enb_clr_reg = CLK_OUT_ENB_CLR_X,
  113. .rst_reg = RST_DEVICES_X,
  114. .rst_set_reg = RST_DEVICES_SET_X,
  115. .rst_clr_reg = RST_DEVICES_CLR_X,
  116. },
  117. };
  118. static void __iomem *clk_base;
  119. static int tegra_clk_rst_assert(struct reset_controller_dev *rcdev,
  120. unsigned long id)
  121. {
  122. /*
  123. * If peripheral is on the APB bus then we must read the APB bus to
  124. * flush the write operation in apb bus. This will avoid peripheral
  125. * access after disabling clock. Since the reset driver has no
  126. * knowledge of which reset IDs represent which devices, simply do
  127. * this all the time.
  128. */
  129. tegra_read_chipid();
  130. writel_relaxed(BIT(id % 32),
  131. clk_base + periph_regs[id / 32].rst_set_reg);
  132. return 0;
  133. }
  134. static int tegra_clk_rst_deassert(struct reset_controller_dev *rcdev,
  135. unsigned long id)
  136. {
  137. writel_relaxed(BIT(id % 32),
  138. clk_base + periph_regs[id / 32].rst_clr_reg);
  139. return 0;
  140. }
  141. struct tegra_clk_periph_regs *get_reg_bank(int clkid)
  142. {
  143. int reg_bank = clkid / 32;
  144. if (reg_bank < periph_banks)
  145. return &periph_regs[reg_bank];
  146. else {
  147. WARN_ON(1);
  148. return NULL;
  149. }
  150. }
  151. struct clk ** __init tegra_clk_init(void __iomem *regs, int num, int banks)
  152. {
  153. clk_base = regs;
  154. if (WARN_ON(banks > ARRAY_SIZE(periph_regs)))
  155. return NULL;
  156. periph_clk_enb_refcnt = kzalloc(32 * banks *
  157. sizeof(*periph_clk_enb_refcnt), GFP_KERNEL);
  158. if (!periph_clk_enb_refcnt)
  159. return NULL;
  160. periph_banks = banks;
  161. clks = kzalloc(num * sizeof(struct clk *), GFP_KERNEL);
  162. if (!clks)
  163. kfree(periph_clk_enb_refcnt);
  164. clk_num = num;
  165. return clks;
  166. }
  167. void __init tegra_init_dup_clks(struct tegra_clk_duplicate *dup_list,
  168. struct clk *clks[], int clk_max)
  169. {
  170. struct clk *clk;
  171. for (; dup_list->clk_id < clk_max; dup_list++) {
  172. clk = clks[dup_list->clk_id];
  173. dup_list->lookup.clk = clk;
  174. clkdev_add(&dup_list->lookup);
  175. }
  176. }
  177. void __init tegra_init_from_table(struct tegra_clk_init_table *tbl,
  178. struct clk *clks[], int clk_max)
  179. {
  180. struct clk *clk;
  181. for (; tbl->clk_id < clk_max; tbl++) {
  182. clk = clks[tbl->clk_id];
  183. if (IS_ERR_OR_NULL(clk))
  184. return;
  185. if (tbl->parent_id < clk_max) {
  186. struct clk *parent = clks[tbl->parent_id];
  187. if (clk_set_parent(clk, parent)) {
  188. pr_err("%s: Failed to set parent %s of %s\n",
  189. __func__, __clk_get_name(parent),
  190. __clk_get_name(clk));
  191. WARN_ON(1);
  192. }
  193. }
  194. if (tbl->rate)
  195. if (clk_set_rate(clk, tbl->rate)) {
  196. pr_err("%s: Failed to set rate %lu of %s\n",
  197. __func__, tbl->rate,
  198. __clk_get_name(clk));
  199. WARN_ON(1);
  200. }
  201. if (tbl->state)
  202. if (clk_prepare_enable(clk)) {
  203. pr_err("%s: Failed to enable %s\n", __func__,
  204. __clk_get_name(clk));
  205. WARN_ON(1);
  206. }
  207. }
  208. }
  209. static struct reset_control_ops rst_ops = {
  210. .assert = tegra_clk_rst_assert,
  211. .deassert = tegra_clk_rst_deassert,
  212. };
  213. static struct reset_controller_dev rst_ctlr = {
  214. .ops = &rst_ops,
  215. .owner = THIS_MODULE,
  216. .of_reset_n_cells = 1,
  217. };
  218. void __init tegra_add_of_provider(struct device_node *np)
  219. {
  220. int i;
  221. for (i = 0; i < clk_num; i++) {
  222. if (IS_ERR(clks[i])) {
  223. pr_err
  224. ("Tegra clk %d: register failed with %ld\n",
  225. i, PTR_ERR(clks[i]));
  226. }
  227. if (!clks[i])
  228. clks[i] = ERR_PTR(-EINVAL);
  229. }
  230. clk_data.clks = clks;
  231. clk_data.clk_num = clk_num;
  232. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
  233. rst_ctlr.of_node = np;
  234. rst_ctlr.nr_resets = clk_num * 32;
  235. reset_controller_register(&rst_ctlr);
  236. }
  237. void __init tegra_register_devclks(struct tegra_devclk *dev_clks, int num)
  238. {
  239. int i;
  240. for (i = 0; i < num; i++, dev_clks++)
  241. clk_register_clkdev(clks[dev_clks->dt_id], dev_clks->con_id,
  242. dev_clks->dev_id);
  243. }
  244. struct clk ** __init tegra_lookup_dt_id(int clk_id,
  245. struct tegra_clk *tegra_clk)
  246. {
  247. if (tegra_clk[clk_id].present)
  248. return &clks[tegra_clk[clk_id].dt_id];
  249. else
  250. return NULL;
  251. }
  252. tegra_clk_apply_init_table_func tegra_clk_apply_init_table;
  253. void __init tegra_clocks_apply_init_table(void)
  254. {
  255. if (!tegra_clk_apply_init_table)
  256. return;
  257. tegra_clk_apply_init_table();
  258. }