clk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/clkdev.h>
  6. #include <linux/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/of.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/clk/tegra.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/reset-controller.h>
  16. #include <linux/string_helpers.h>
  17. #include <soc/tegra/fuse.h>
  18. #include "clk.h"
  19. /* Global data of Tegra CPU CAR ops */
  20. static struct device_node *tegra_car_np;
  21. static struct tegra_cpu_car_ops dummy_car_ops;
  22. struct tegra_cpu_car_ops *tegra_cpu_car_ops = &dummy_car_ops;
  23. int *periph_clk_enb_refcnt;
  24. static int periph_banks;
  25. static u32 *periph_state_ctx;
  26. static struct clk **clks;
  27. static int clk_num;
  28. static struct clk_onecell_data clk_data;
  29. /* Handlers for SoC-specific reset lines */
  30. static int (*special_reset_assert)(unsigned long);
  31. static int (*special_reset_deassert)(unsigned long);
  32. static unsigned int num_special_reset;
  33. static const struct tegra_clk_periph_regs periph_regs[] = {
  34. [0] = {
  35. .enb_reg = CLK_OUT_ENB_L,
  36. .enb_set_reg = CLK_OUT_ENB_SET_L,
  37. .enb_clr_reg = CLK_OUT_ENB_CLR_L,
  38. .rst_reg = RST_DEVICES_L,
  39. .rst_set_reg = RST_DEVICES_SET_L,
  40. .rst_clr_reg = RST_DEVICES_CLR_L,
  41. },
  42. [1] = {
  43. .enb_reg = CLK_OUT_ENB_H,
  44. .enb_set_reg = CLK_OUT_ENB_SET_H,
  45. .enb_clr_reg = CLK_OUT_ENB_CLR_H,
  46. .rst_reg = RST_DEVICES_H,
  47. .rst_set_reg = RST_DEVICES_SET_H,
  48. .rst_clr_reg = RST_DEVICES_CLR_H,
  49. },
  50. [2] = {
  51. .enb_reg = CLK_OUT_ENB_U,
  52. .enb_set_reg = CLK_OUT_ENB_SET_U,
  53. .enb_clr_reg = CLK_OUT_ENB_CLR_U,
  54. .rst_reg = RST_DEVICES_U,
  55. .rst_set_reg = RST_DEVICES_SET_U,
  56. .rst_clr_reg = RST_DEVICES_CLR_U,
  57. },
  58. [3] = {
  59. .enb_reg = CLK_OUT_ENB_V,
  60. .enb_set_reg = CLK_OUT_ENB_SET_V,
  61. .enb_clr_reg = CLK_OUT_ENB_CLR_V,
  62. .rst_reg = RST_DEVICES_V,
  63. .rst_set_reg = RST_DEVICES_SET_V,
  64. .rst_clr_reg = RST_DEVICES_CLR_V,
  65. },
  66. [4] = {
  67. .enb_reg = CLK_OUT_ENB_W,
  68. .enb_set_reg = CLK_OUT_ENB_SET_W,
  69. .enb_clr_reg = CLK_OUT_ENB_CLR_W,
  70. .rst_reg = RST_DEVICES_W,
  71. .rst_set_reg = RST_DEVICES_SET_W,
  72. .rst_clr_reg = RST_DEVICES_CLR_W,
  73. },
  74. [5] = {
  75. .enb_reg = CLK_OUT_ENB_X,
  76. .enb_set_reg = CLK_OUT_ENB_SET_X,
  77. .enb_clr_reg = CLK_OUT_ENB_CLR_X,
  78. .rst_reg = RST_DEVICES_X,
  79. .rst_set_reg = RST_DEVICES_SET_X,
  80. .rst_clr_reg = RST_DEVICES_CLR_X,
  81. },
  82. [6] = {
  83. .enb_reg = CLK_OUT_ENB_Y,
  84. .enb_set_reg = CLK_OUT_ENB_SET_Y,
  85. .enb_clr_reg = CLK_OUT_ENB_CLR_Y,
  86. .rst_reg = RST_DEVICES_Y,
  87. .rst_set_reg = RST_DEVICES_SET_Y,
  88. .rst_clr_reg = RST_DEVICES_CLR_Y,
  89. },
  90. };
  91. static void __iomem *clk_base;
  92. static int tegra_clk_rst_assert(struct reset_controller_dev *rcdev,
  93. unsigned long id)
  94. {
  95. /*
  96. * If peripheral is on the APB bus then we must read the APB bus to
  97. * flush the write operation in apb bus. This will avoid peripheral
  98. * access after disabling clock. Since the reset driver has no
  99. * knowledge of which reset IDs represent which devices, simply do
  100. * this all the time.
  101. */
  102. tegra_read_chipid();
  103. if (id < periph_banks * 32) {
  104. writel_relaxed(BIT(id % 32),
  105. clk_base + periph_regs[id / 32].rst_set_reg);
  106. return 0;
  107. } else if (id < periph_banks * 32 + num_special_reset) {
  108. return special_reset_assert(id);
  109. }
  110. return -EINVAL;
  111. }
  112. static int tegra_clk_rst_deassert(struct reset_controller_dev *rcdev,
  113. unsigned long id)
  114. {
  115. if (id < periph_banks * 32) {
  116. writel_relaxed(BIT(id % 32),
  117. clk_base + periph_regs[id / 32].rst_clr_reg);
  118. return 0;
  119. } else if (id < periph_banks * 32 + num_special_reset) {
  120. return special_reset_deassert(id);
  121. }
  122. return -EINVAL;
  123. }
  124. static int tegra_clk_rst_reset(struct reset_controller_dev *rcdev,
  125. unsigned long id)
  126. {
  127. int err;
  128. err = tegra_clk_rst_assert(rcdev, id);
  129. if (err)
  130. return err;
  131. udelay(1);
  132. return tegra_clk_rst_deassert(rcdev, id);
  133. }
  134. const struct tegra_clk_periph_regs *get_reg_bank(int clkid)
  135. {
  136. int reg_bank = clkid / 32;
  137. if (reg_bank < periph_banks)
  138. return &periph_regs[reg_bank];
  139. else {
  140. WARN_ON(1);
  141. return NULL;
  142. }
  143. }
  144. void tegra_clk_set_pllp_out_cpu(bool enable)
  145. {
  146. u32 val;
  147. val = readl_relaxed(clk_base + CLK_OUT_ENB_Y);
  148. if (enable)
  149. val |= CLK_ENB_PLLP_OUT_CPU;
  150. else
  151. val &= ~CLK_ENB_PLLP_OUT_CPU;
  152. writel_relaxed(val, clk_base + CLK_OUT_ENB_Y);
  153. }
  154. void tegra_clk_periph_suspend(void)
  155. {
  156. unsigned int i, idx;
  157. idx = 0;
  158. for (i = 0; i < periph_banks; i++, idx++)
  159. periph_state_ctx[idx] =
  160. readl_relaxed(clk_base + periph_regs[i].enb_reg);
  161. for (i = 0; i < periph_banks; i++, idx++)
  162. periph_state_ctx[idx] =
  163. readl_relaxed(clk_base + periph_regs[i].rst_reg);
  164. }
  165. void tegra_clk_periph_resume(void)
  166. {
  167. unsigned int i, idx;
  168. idx = 0;
  169. for (i = 0; i < periph_banks; i++, idx++)
  170. writel_relaxed(periph_state_ctx[idx],
  171. clk_base + periph_regs[i].enb_reg);
  172. /*
  173. * All non-boot peripherals will be in reset state on resume.
  174. * Wait for 5us of reset propagation delay before de-asserting
  175. * the peripherals based on the saved context.
  176. */
  177. fence_udelay(5, clk_base);
  178. for (i = 0; i < periph_banks; i++, idx++)
  179. writel_relaxed(periph_state_ctx[idx],
  180. clk_base + periph_regs[i].rst_reg);
  181. fence_udelay(2, clk_base);
  182. }
  183. static int tegra_clk_periph_ctx_init(int banks)
  184. {
  185. periph_state_ctx = kcalloc(2 * banks, sizeof(*periph_state_ctx),
  186. GFP_KERNEL);
  187. if (!periph_state_ctx)
  188. return -ENOMEM;
  189. return 0;
  190. }
  191. struct clk ** __init tegra_clk_init(void __iomem *regs, int num, int banks)
  192. {
  193. clk_base = regs;
  194. if (WARN_ON(banks > ARRAY_SIZE(periph_regs)))
  195. return NULL;
  196. periph_clk_enb_refcnt = kzalloc_objs(*periph_clk_enb_refcnt, 32 * banks);
  197. if (!periph_clk_enb_refcnt)
  198. return NULL;
  199. periph_banks = banks;
  200. clks = kzalloc_objs(struct clk *, num);
  201. if (!clks) {
  202. kfree(periph_clk_enb_refcnt);
  203. return NULL;
  204. }
  205. clk_num = num;
  206. if (IS_ENABLED(CONFIG_PM_SLEEP)) {
  207. if (tegra_clk_periph_ctx_init(banks)) {
  208. kfree(periph_clk_enb_refcnt);
  209. kfree(clks);
  210. return NULL;
  211. }
  212. }
  213. return clks;
  214. }
  215. void __init tegra_init_dup_clks(struct tegra_clk_duplicate *dup_list,
  216. struct clk *clks[], int clk_max)
  217. {
  218. struct clk *clk;
  219. for (; dup_list->clk_id < clk_max; dup_list++) {
  220. clk = clks[dup_list->clk_id];
  221. dup_list->lookup.clk = clk;
  222. clkdev_add(&dup_list->lookup);
  223. }
  224. }
  225. void tegra_init_from_table(struct tegra_clk_init_table *tbl,
  226. struct clk *clks[], int clk_max)
  227. {
  228. struct clk *clk;
  229. for (; tbl->clk_id < clk_max; tbl++) {
  230. clk = clks[tbl->clk_id];
  231. if (IS_ERR_OR_NULL(clk)) {
  232. pr_err("%s: invalid entry %ld in clks array for id %d\n",
  233. __func__, PTR_ERR(clk), tbl->clk_id);
  234. WARN_ON(1);
  235. continue;
  236. }
  237. if (tbl->parent_id < clk_max) {
  238. struct clk *parent = clks[tbl->parent_id];
  239. if (clk_set_parent(clk, parent)) {
  240. pr_err("%s: Failed to set parent %s of %s\n",
  241. __func__, __clk_get_name(parent),
  242. __clk_get_name(clk));
  243. WARN_ON(1);
  244. }
  245. }
  246. if (tbl->rate)
  247. if (clk_set_rate(clk, tbl->rate)) {
  248. pr_err("%s: Failed to set rate %lu of %s\n",
  249. __func__, tbl->rate,
  250. __clk_get_name(clk));
  251. WARN_ON(1);
  252. }
  253. if (tbl->state)
  254. if (clk_prepare_enable(clk)) {
  255. pr_err("%s: Failed to enable %s\n", __func__,
  256. __clk_get_name(clk));
  257. WARN_ON(1);
  258. }
  259. }
  260. }
  261. static const struct reset_control_ops rst_ops = {
  262. .assert = tegra_clk_rst_assert,
  263. .deassert = tegra_clk_rst_deassert,
  264. .reset = tegra_clk_rst_reset,
  265. };
  266. static struct reset_controller_dev rst_ctlr = {
  267. .ops = &rst_ops,
  268. .owner = THIS_MODULE,
  269. .of_reset_n_cells = 1,
  270. };
  271. void __init tegra_add_of_provider(struct device_node *np,
  272. void *clk_src_onecell_get)
  273. {
  274. int i;
  275. tegra_car_np = np;
  276. for (i = 0; i < clk_num; i++) {
  277. if (IS_ERR(clks[i])) {
  278. pr_err
  279. ("Tegra clk %d: register failed with %ld\n",
  280. i, PTR_ERR(clks[i]));
  281. }
  282. if (!clks[i])
  283. clks[i] = ERR_PTR(-EINVAL);
  284. }
  285. clk_data.clks = clks;
  286. clk_data.clk_num = clk_num;
  287. of_clk_add_provider(np, clk_src_onecell_get, &clk_data);
  288. rst_ctlr.of_node = np;
  289. rst_ctlr.nr_resets = periph_banks * 32 + num_special_reset;
  290. reset_controller_register(&rst_ctlr);
  291. }
  292. void __init tegra_init_special_resets(unsigned int num,
  293. int (*assert)(unsigned long),
  294. int (*deassert)(unsigned long))
  295. {
  296. num_special_reset = num;
  297. special_reset_assert = assert;
  298. special_reset_deassert = deassert;
  299. }
  300. void tegra_register_devclks(struct tegra_devclk *dev_clks, int num)
  301. {
  302. int i;
  303. for (i = 0; i < num; i++, dev_clks++)
  304. clk_register_clkdev(clks[dev_clks->dt_id], dev_clks->con_id,
  305. dev_clks->dev_id);
  306. for (i = 0; i < clk_num; i++) {
  307. if (!IS_ERR_OR_NULL(clks[i]))
  308. clk_register_clkdev(clks[i], __clk_get_name(clks[i]),
  309. "tegra-clk-debug");
  310. }
  311. }
  312. struct clk ** __init tegra_lookup_dt_id(int clk_id,
  313. struct tegra_clk *tegra_clk)
  314. {
  315. if (tegra_clk[clk_id].present)
  316. return &clks[tegra_clk[clk_id].dt_id];
  317. else
  318. return NULL;
  319. }
  320. static struct device_node *tegra_clk_get_of_node(struct clk_hw *hw)
  321. {
  322. struct device_node *np;
  323. char *node_name;
  324. node_name = kstrdup_and_replace(hw->init->name, '_', '-', GFP_KERNEL);
  325. if (!node_name)
  326. return NULL;
  327. for_each_child_of_node(tegra_car_np, np) {
  328. if (!strcmp(np->name, node_name))
  329. break;
  330. }
  331. kfree(node_name);
  332. return np;
  333. }
  334. struct clk *tegra_clk_dev_register(struct clk_hw *hw)
  335. {
  336. struct platform_device *pdev, *parent;
  337. const char *dev_name = NULL;
  338. struct device *dev = NULL;
  339. struct device_node *np;
  340. np = tegra_clk_get_of_node(hw);
  341. if (!of_device_is_available(np))
  342. goto put_node;
  343. dev_name = kasprintf(GFP_KERNEL, "tegra_clk_%s", hw->init->name);
  344. if (!dev_name)
  345. goto put_node;
  346. parent = of_find_device_by_node(tegra_car_np);
  347. if (parent) {
  348. pdev = of_platform_device_create(np, dev_name, &parent->dev);
  349. put_device(&parent->dev);
  350. if (!pdev) {
  351. pr_err("%s: failed to create device for %pOF\n",
  352. __func__, np);
  353. goto free_name;
  354. }
  355. dev = &pdev->dev;
  356. pm_runtime_enable(dev);
  357. } else {
  358. WARN(1, "failed to find device for %pOF\n", tegra_car_np);
  359. }
  360. free_name:
  361. kfree(dev_name);
  362. put_node:
  363. of_node_put(np);
  364. return clk_register(dev, hw);
  365. }
  366. tegra_clk_apply_init_table_func tegra_clk_apply_init_table;
  367. static int __init tegra_clocks_apply_init_table(void)
  368. {
  369. if (!tegra_clk_apply_init_table)
  370. return 0;
  371. tegra_clk_apply_init_table();
  372. return 0;
  373. }
  374. arch_initcall(tegra_clocks_apply_init_table);