clk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_device.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.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 = kcalloc(32 * banks,
  197. sizeof(*periph_clk_enb_refcnt),
  198. GFP_KERNEL);
  199. if (!periph_clk_enb_refcnt)
  200. return NULL;
  201. periph_banks = banks;
  202. clks = kcalloc(num, sizeof(struct clk *), GFP_KERNEL);
  203. if (!clks) {
  204. kfree(periph_clk_enb_refcnt);
  205. return NULL;
  206. }
  207. clk_num = num;
  208. if (IS_ENABLED(CONFIG_PM_SLEEP)) {
  209. if (tegra_clk_periph_ctx_init(banks)) {
  210. kfree(periph_clk_enb_refcnt);
  211. kfree(clks);
  212. return NULL;
  213. }
  214. }
  215. return clks;
  216. }
  217. void __init tegra_init_dup_clks(struct tegra_clk_duplicate *dup_list,
  218. struct clk *clks[], int clk_max)
  219. {
  220. struct clk *clk;
  221. for (; dup_list->clk_id < clk_max; dup_list++) {
  222. clk = clks[dup_list->clk_id];
  223. dup_list->lookup.clk = clk;
  224. clkdev_add(&dup_list->lookup);
  225. }
  226. }
  227. void tegra_init_from_table(struct tegra_clk_init_table *tbl,
  228. struct clk *clks[], int clk_max)
  229. {
  230. struct clk *clk;
  231. for (; tbl->clk_id < clk_max; tbl++) {
  232. clk = clks[tbl->clk_id];
  233. if (IS_ERR_OR_NULL(clk)) {
  234. pr_err("%s: invalid entry %ld in clks array for id %d\n",
  235. __func__, PTR_ERR(clk), tbl->clk_id);
  236. WARN_ON(1);
  237. continue;
  238. }
  239. if (tbl->parent_id < clk_max) {
  240. struct clk *parent = clks[tbl->parent_id];
  241. if (clk_set_parent(clk, parent)) {
  242. pr_err("%s: Failed to set parent %s of %s\n",
  243. __func__, __clk_get_name(parent),
  244. __clk_get_name(clk));
  245. WARN_ON(1);
  246. }
  247. }
  248. if (tbl->rate)
  249. if (clk_set_rate(clk, tbl->rate)) {
  250. pr_err("%s: Failed to set rate %lu of %s\n",
  251. __func__, tbl->rate,
  252. __clk_get_name(clk));
  253. WARN_ON(1);
  254. }
  255. if (tbl->state)
  256. if (clk_prepare_enable(clk)) {
  257. pr_err("%s: Failed to enable %s\n", __func__,
  258. __clk_get_name(clk));
  259. WARN_ON(1);
  260. }
  261. }
  262. }
  263. static const struct reset_control_ops rst_ops = {
  264. .assert = tegra_clk_rst_assert,
  265. .deassert = tegra_clk_rst_deassert,
  266. .reset = tegra_clk_rst_reset,
  267. };
  268. static struct reset_controller_dev rst_ctlr = {
  269. .ops = &rst_ops,
  270. .owner = THIS_MODULE,
  271. .of_reset_n_cells = 1,
  272. };
  273. void __init tegra_add_of_provider(struct device_node *np,
  274. void *clk_src_onecell_get)
  275. {
  276. int i;
  277. tegra_car_np = np;
  278. for (i = 0; i < clk_num; i++) {
  279. if (IS_ERR(clks[i])) {
  280. pr_err
  281. ("Tegra clk %d: register failed with %ld\n",
  282. i, PTR_ERR(clks[i]));
  283. }
  284. if (!clks[i])
  285. clks[i] = ERR_PTR(-EINVAL);
  286. }
  287. clk_data.clks = clks;
  288. clk_data.clk_num = clk_num;
  289. of_clk_add_provider(np, clk_src_onecell_get, &clk_data);
  290. rst_ctlr.of_node = np;
  291. rst_ctlr.nr_resets = periph_banks * 32 + num_special_reset;
  292. reset_controller_register(&rst_ctlr);
  293. }
  294. void __init tegra_init_special_resets(unsigned int num,
  295. int (*assert)(unsigned long),
  296. int (*deassert)(unsigned long))
  297. {
  298. num_special_reset = num;
  299. special_reset_assert = assert;
  300. special_reset_deassert = deassert;
  301. }
  302. void tegra_register_devclks(struct tegra_devclk *dev_clks, int num)
  303. {
  304. int i;
  305. for (i = 0; i < num; i++, dev_clks++)
  306. clk_register_clkdev(clks[dev_clks->dt_id], dev_clks->con_id,
  307. dev_clks->dev_id);
  308. for (i = 0; i < clk_num; i++) {
  309. if (!IS_ERR_OR_NULL(clks[i]))
  310. clk_register_clkdev(clks[i], __clk_get_name(clks[i]),
  311. "tegra-clk-debug");
  312. }
  313. }
  314. struct clk ** __init tegra_lookup_dt_id(int clk_id,
  315. struct tegra_clk *tegra_clk)
  316. {
  317. if (tegra_clk[clk_id].present)
  318. return &clks[tegra_clk[clk_id].dt_id];
  319. else
  320. return NULL;
  321. }
  322. static struct device_node *tegra_clk_get_of_node(struct clk_hw *hw)
  323. {
  324. struct device_node *np;
  325. char *node_name;
  326. node_name = kstrdup(hw->init->name, GFP_KERNEL);
  327. if (!node_name)
  328. return NULL;
  329. strreplace(node_name, '_', '-');
  330. for_each_child_of_node(tegra_car_np, np) {
  331. if (!strcmp(np->name, node_name))
  332. break;
  333. }
  334. kfree(node_name);
  335. return np;
  336. }
  337. struct clk *tegra_clk_dev_register(struct clk_hw *hw)
  338. {
  339. struct platform_device *pdev, *parent;
  340. const char *dev_name = NULL;
  341. struct device *dev = NULL;
  342. struct device_node *np;
  343. np = tegra_clk_get_of_node(hw);
  344. if (!of_device_is_available(np))
  345. goto put_node;
  346. dev_name = kasprintf(GFP_KERNEL, "tegra_clk_%s", hw->init->name);
  347. if (!dev_name)
  348. goto put_node;
  349. parent = of_find_device_by_node(tegra_car_np);
  350. if (parent) {
  351. pdev = of_platform_device_create(np, dev_name, &parent->dev);
  352. put_device(&parent->dev);
  353. if (!pdev) {
  354. pr_err("%s: failed to create device for %pOF\n",
  355. __func__, np);
  356. goto free_name;
  357. }
  358. dev = &pdev->dev;
  359. pm_runtime_enable(dev);
  360. } else {
  361. WARN(1, "failed to find device for %pOF\n", tegra_car_np);
  362. }
  363. free_name:
  364. kfree(dev_name);
  365. put_node:
  366. of_node_put(np);
  367. return clk_register(dev, hw);
  368. }
  369. tegra_clk_apply_init_table_func tegra_clk_apply_init_table;
  370. static int __init tegra_clocks_apply_init_table(void)
  371. {
  372. if (!tegra_clk_apply_init_table)
  373. return 0;
  374. tegra_clk_apply_init_table();
  375. return 0;
  376. }
  377. arch_initcall(tegra_clocks_apply_init_table);