clk.c 9.1 KB

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