clk-tegra124-emc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/clk/tegra/clk-emc.c
  4. *
  5. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * Author:
  8. * Mikko Perttunen <[email protected]>
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clk.h>
  12. #include <linux/clkdev.h>
  13. #include <linux/clk/tegra.h>
  14. #include <linux/delay.h>
  15. #include <linux/export.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sort.h>
  22. #include <linux/string.h>
  23. #include <soc/tegra/fuse.h>
  24. #include "clk.h"
  25. #define CLK_SOURCE_EMC 0x19c
  26. #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_SHIFT 0
  27. #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK 0xff
  28. #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(x) (((x) & CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK) << \
  29. CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_SHIFT)
  30. #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT 29
  31. #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK 0x7
  32. #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC(x) (((x) & CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK) << \
  33. CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT)
  34. static const char * const emc_parent_clk_names[] = {
  35. "pll_m", "pll_c", "pll_p", "clk_m", "pll_m_ud",
  36. "pll_c2", "pll_c3", "pll_c_ud"
  37. };
  38. /*
  39. * List of clock sources for various parents the EMC clock can have.
  40. * When we change the timing to a timing with a parent that has the same
  41. * clock source as the current parent, we must first change to a backup
  42. * timing that has a different clock source.
  43. */
  44. #define EMC_SRC_PLL_M 0
  45. #define EMC_SRC_PLL_C 1
  46. #define EMC_SRC_PLL_P 2
  47. #define EMC_SRC_CLK_M 3
  48. #define EMC_SRC_PLL_C2 4
  49. #define EMC_SRC_PLL_C3 5
  50. static const char emc_parent_clk_sources[] = {
  51. EMC_SRC_PLL_M, EMC_SRC_PLL_C, EMC_SRC_PLL_P, EMC_SRC_CLK_M,
  52. EMC_SRC_PLL_M, EMC_SRC_PLL_C2, EMC_SRC_PLL_C3, EMC_SRC_PLL_C
  53. };
  54. struct emc_timing {
  55. unsigned long rate, parent_rate;
  56. u8 parent_index;
  57. struct clk *parent;
  58. u32 ram_code;
  59. };
  60. struct tegra_clk_emc {
  61. struct clk_hw hw;
  62. void __iomem *clk_regs;
  63. struct clk *prev_parent;
  64. bool changing_timing;
  65. struct device_node *emc_node;
  66. struct tegra_emc *emc;
  67. int num_timings;
  68. struct emc_timing *timings;
  69. spinlock_t *lock;
  70. tegra124_emc_prepare_timing_change_cb *prepare_timing_change;
  71. tegra124_emc_complete_timing_change_cb *complete_timing_change;
  72. };
  73. /* Common clock framework callback implementations */
  74. static unsigned long emc_recalc_rate(struct clk_hw *hw,
  75. unsigned long parent_rate)
  76. {
  77. struct tegra_clk_emc *tegra;
  78. u32 val, div;
  79. tegra = container_of(hw, struct tegra_clk_emc, hw);
  80. /*
  81. * CCF wrongly assumes that the parent won't change during set_rate,
  82. * so get the parent rate explicitly.
  83. */
  84. parent_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
  85. val = readl(tegra->clk_regs + CLK_SOURCE_EMC);
  86. div = val & CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK;
  87. return parent_rate / (div + 2) * 2;
  88. }
  89. /*
  90. * Rounds up unless no higher rate exists, in which case down. This way is
  91. * safer since things have EMC rate floors. Also don't touch parent_rate
  92. * since we don't want the CCF to play with our parent clocks.
  93. */
  94. static int emc_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  95. {
  96. struct tegra_clk_emc *tegra;
  97. u8 ram_code = tegra_read_ram_code();
  98. struct emc_timing *timing = NULL;
  99. int i, k, t;
  100. tegra = container_of(hw, struct tegra_clk_emc, hw);
  101. for (k = 0; k < tegra->num_timings; k++) {
  102. if (tegra->timings[k].ram_code == ram_code)
  103. break;
  104. }
  105. for (t = k; t < tegra->num_timings; t++) {
  106. if (tegra->timings[t].ram_code != ram_code)
  107. break;
  108. }
  109. for (i = k; i < t; i++) {
  110. timing = tegra->timings + i;
  111. if (timing->rate < req->rate && i != t - 1)
  112. continue;
  113. if (timing->rate > req->max_rate) {
  114. i = max(i, k + 1);
  115. req->rate = tegra->timings[i - 1].rate;
  116. return 0;
  117. }
  118. if (timing->rate < req->min_rate)
  119. continue;
  120. req->rate = timing->rate;
  121. return 0;
  122. }
  123. if (timing) {
  124. req->rate = timing->rate;
  125. return 0;
  126. }
  127. req->rate = clk_hw_get_rate(hw);
  128. return 0;
  129. }
  130. static u8 emc_get_parent(struct clk_hw *hw)
  131. {
  132. struct tegra_clk_emc *tegra;
  133. u32 val;
  134. tegra = container_of(hw, struct tegra_clk_emc, hw);
  135. val = readl(tegra->clk_regs + CLK_SOURCE_EMC);
  136. return (val >> CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT)
  137. & CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK;
  138. }
  139. static struct tegra_emc *emc_ensure_emc_driver(struct tegra_clk_emc *tegra)
  140. {
  141. struct platform_device *pdev;
  142. if (tegra->emc)
  143. return tegra->emc;
  144. if (!tegra->prepare_timing_change || !tegra->complete_timing_change)
  145. return NULL;
  146. if (!tegra->emc_node)
  147. return NULL;
  148. pdev = of_find_device_by_node(tegra->emc_node);
  149. if (!pdev) {
  150. pr_err("%s: could not get external memory controller\n",
  151. __func__);
  152. return NULL;
  153. }
  154. of_node_put(tegra->emc_node);
  155. tegra->emc_node = NULL;
  156. tegra->emc = platform_get_drvdata(pdev);
  157. if (!tegra->emc) {
  158. put_device(&pdev->dev);
  159. pr_err("%s: cannot find EMC driver\n", __func__);
  160. return NULL;
  161. }
  162. return tegra->emc;
  163. }
  164. static int emc_set_timing(struct tegra_clk_emc *tegra,
  165. struct emc_timing *timing)
  166. {
  167. int err;
  168. u8 div;
  169. u32 car_value;
  170. unsigned long flags = 0;
  171. struct tegra_emc *emc = emc_ensure_emc_driver(tegra);
  172. if (!emc)
  173. return -ENOENT;
  174. pr_debug("going to rate %ld prate %ld p %s\n", timing->rate,
  175. timing->parent_rate, __clk_get_name(timing->parent));
  176. if (emc_get_parent(&tegra->hw) == timing->parent_index &&
  177. clk_get_rate(timing->parent) != timing->parent_rate) {
  178. WARN_ONCE(1, "parent %s rate mismatch %lu %lu\n",
  179. __clk_get_name(timing->parent),
  180. clk_get_rate(timing->parent),
  181. timing->parent_rate);
  182. return -EINVAL;
  183. }
  184. tegra->changing_timing = true;
  185. err = clk_set_rate(timing->parent, timing->parent_rate);
  186. if (err) {
  187. pr_err("cannot change parent %s rate to %ld: %d\n",
  188. __clk_get_name(timing->parent), timing->parent_rate,
  189. err);
  190. return err;
  191. }
  192. err = clk_prepare_enable(timing->parent);
  193. if (err) {
  194. pr_err("cannot enable parent clock: %d\n", err);
  195. return err;
  196. }
  197. div = timing->parent_rate / (timing->rate / 2) - 2;
  198. err = tegra->prepare_timing_change(emc, timing->rate);
  199. if (err) {
  200. clk_disable_unprepare(timing->parent);
  201. return err;
  202. }
  203. spin_lock_irqsave(tegra->lock, flags);
  204. car_value = readl(tegra->clk_regs + CLK_SOURCE_EMC);
  205. car_value &= ~CLK_SOURCE_EMC_EMC_2X_CLK_SRC(~0);
  206. car_value |= CLK_SOURCE_EMC_EMC_2X_CLK_SRC(timing->parent_index);
  207. car_value &= ~CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(~0);
  208. car_value |= CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(div);
  209. writel(car_value, tegra->clk_regs + CLK_SOURCE_EMC);
  210. spin_unlock_irqrestore(tegra->lock, flags);
  211. tegra->complete_timing_change(emc, timing->rate);
  212. clk_hw_reparent(&tegra->hw, __clk_get_hw(timing->parent));
  213. clk_disable_unprepare(tegra->prev_parent);
  214. tegra->prev_parent = timing->parent;
  215. tegra->changing_timing = false;
  216. return 0;
  217. }
  218. /*
  219. * Get backup timing to use as an intermediate step when a change between
  220. * two timings with the same clock source has been requested. First try to
  221. * find a timing with a higher clock rate to avoid a rate below any set rate
  222. * floors. If that is not possible, find a lower rate.
  223. */
  224. static struct emc_timing *get_backup_timing(struct tegra_clk_emc *tegra,
  225. int timing_index)
  226. {
  227. int i;
  228. u32 ram_code = tegra_read_ram_code();
  229. struct emc_timing *timing;
  230. for (i = timing_index+1; i < tegra->num_timings; i++) {
  231. timing = tegra->timings + i;
  232. if (timing->ram_code != ram_code)
  233. break;
  234. if (emc_parent_clk_sources[timing->parent_index] !=
  235. emc_parent_clk_sources[
  236. tegra->timings[timing_index].parent_index])
  237. return timing;
  238. }
  239. for (i = timing_index-1; i >= 0; --i) {
  240. timing = tegra->timings + i;
  241. if (timing->ram_code != ram_code)
  242. break;
  243. if (emc_parent_clk_sources[timing->parent_index] !=
  244. emc_parent_clk_sources[
  245. tegra->timings[timing_index].parent_index])
  246. return timing;
  247. }
  248. return NULL;
  249. }
  250. static int emc_set_rate(struct clk_hw *hw, unsigned long rate,
  251. unsigned long parent_rate)
  252. {
  253. struct tegra_clk_emc *tegra;
  254. struct emc_timing *timing = NULL;
  255. int i, err;
  256. u32 ram_code = tegra_read_ram_code();
  257. tegra = container_of(hw, struct tegra_clk_emc, hw);
  258. if (clk_hw_get_rate(hw) == rate)
  259. return 0;
  260. /*
  261. * When emc_set_timing changes the parent rate, CCF will propagate
  262. * that downward to us, so ignore any set_rate calls while a rate
  263. * change is already going on.
  264. */
  265. if (tegra->changing_timing)
  266. return 0;
  267. for (i = 0; i < tegra->num_timings; i++) {
  268. if (tegra->timings[i].rate == rate &&
  269. tegra->timings[i].ram_code == ram_code) {
  270. timing = tegra->timings + i;
  271. break;
  272. }
  273. }
  274. if (!timing) {
  275. pr_err("cannot switch to rate %ld without emc table\n", rate);
  276. return -EINVAL;
  277. }
  278. if (emc_parent_clk_sources[emc_get_parent(hw)] ==
  279. emc_parent_clk_sources[timing->parent_index] &&
  280. clk_get_rate(timing->parent) != timing->parent_rate) {
  281. /*
  282. * Parent clock source not changed but parent rate has changed,
  283. * need to temporarily switch to another parent
  284. */
  285. struct emc_timing *backup_timing;
  286. backup_timing = get_backup_timing(tegra, i);
  287. if (!backup_timing) {
  288. pr_err("cannot find backup timing\n");
  289. return -EINVAL;
  290. }
  291. pr_debug("using %ld as backup rate when going to %ld\n",
  292. backup_timing->rate, rate);
  293. err = emc_set_timing(tegra, backup_timing);
  294. if (err) {
  295. pr_err("cannot set backup timing: %d\n", err);
  296. return err;
  297. }
  298. }
  299. return emc_set_timing(tegra, timing);
  300. }
  301. /* Initialization and deinitialization */
  302. static int load_one_timing_from_dt(struct tegra_clk_emc *tegra,
  303. struct emc_timing *timing,
  304. struct device_node *node)
  305. {
  306. int err, i;
  307. u32 tmp;
  308. err = of_property_read_u32(node, "clock-frequency", &tmp);
  309. if (err) {
  310. pr_err("timing %pOF: failed to read rate\n", node);
  311. return err;
  312. }
  313. timing->rate = tmp;
  314. err = of_property_read_u32(node, "nvidia,parent-clock-frequency", &tmp);
  315. if (err) {
  316. pr_err("timing %pOF: failed to read parent rate\n", node);
  317. return err;
  318. }
  319. timing->parent_rate = tmp;
  320. timing->parent = of_clk_get_by_name(node, "emc-parent");
  321. if (IS_ERR(timing->parent)) {
  322. pr_err("timing %pOF: failed to get parent clock\n", node);
  323. return PTR_ERR(timing->parent);
  324. }
  325. timing->parent_index = 0xff;
  326. i = match_string(emc_parent_clk_names, ARRAY_SIZE(emc_parent_clk_names),
  327. __clk_get_name(timing->parent));
  328. if (i < 0) {
  329. pr_err("timing %pOF: %s is not a valid parent\n",
  330. node, __clk_get_name(timing->parent));
  331. clk_put(timing->parent);
  332. return -EINVAL;
  333. }
  334. timing->parent_index = i;
  335. return 0;
  336. }
  337. static int cmp_timings(const void *_a, const void *_b)
  338. {
  339. const struct emc_timing *a = _a;
  340. const struct emc_timing *b = _b;
  341. if (a->rate < b->rate)
  342. return -1;
  343. else if (a->rate == b->rate)
  344. return 0;
  345. else
  346. return 1;
  347. }
  348. static int load_timings_from_dt(struct tegra_clk_emc *tegra,
  349. struct device_node *node,
  350. u32 ram_code)
  351. {
  352. struct emc_timing *timings_ptr;
  353. struct device_node *child;
  354. int child_count = of_get_child_count(node);
  355. int i = 0, err;
  356. size_t size;
  357. size = (tegra->num_timings + child_count) * sizeof(struct emc_timing);
  358. tegra->timings = krealloc(tegra->timings, size, GFP_KERNEL);
  359. if (!tegra->timings)
  360. return -ENOMEM;
  361. timings_ptr = tegra->timings + tegra->num_timings;
  362. tegra->num_timings += child_count;
  363. for_each_child_of_node(node, child) {
  364. struct emc_timing *timing = timings_ptr + (i++);
  365. err = load_one_timing_from_dt(tegra, timing, child);
  366. if (err) {
  367. of_node_put(child);
  368. kfree(tegra->timings);
  369. return err;
  370. }
  371. timing->ram_code = ram_code;
  372. }
  373. sort(timings_ptr, child_count, sizeof(struct emc_timing),
  374. cmp_timings, NULL);
  375. return 0;
  376. }
  377. static const struct clk_ops tegra_clk_emc_ops = {
  378. .recalc_rate = emc_recalc_rate,
  379. .determine_rate = emc_determine_rate,
  380. .set_rate = emc_set_rate,
  381. .get_parent = emc_get_parent,
  382. };
  383. struct clk *tegra124_clk_register_emc(void __iomem *base, struct device_node *np,
  384. spinlock_t *lock)
  385. {
  386. struct tegra_clk_emc *tegra;
  387. struct clk_init_data init;
  388. struct device_node *node;
  389. u32 node_ram_code;
  390. struct clk *clk;
  391. int err;
  392. tegra = kcalloc(1, sizeof(*tegra), GFP_KERNEL);
  393. if (!tegra)
  394. return ERR_PTR(-ENOMEM);
  395. tegra->clk_regs = base;
  396. tegra->lock = lock;
  397. tegra->num_timings = 0;
  398. for_each_child_of_node(np, node) {
  399. err = of_property_read_u32(node, "nvidia,ram-code",
  400. &node_ram_code);
  401. if (err)
  402. continue;
  403. /*
  404. * Store timings for all ram codes as we cannot read the
  405. * fuses until the apbmisc driver is loaded.
  406. */
  407. err = load_timings_from_dt(tegra, node, node_ram_code);
  408. if (err) {
  409. of_node_put(node);
  410. kfree(tegra);
  411. return ERR_PTR(err);
  412. }
  413. }
  414. if (tegra->num_timings == 0)
  415. pr_warn("%s: no memory timings registered\n", __func__);
  416. tegra->emc_node = of_parse_phandle(np,
  417. "nvidia,external-memory-controller", 0);
  418. if (!tegra->emc_node)
  419. pr_warn("%s: couldn't find node for EMC driver\n", __func__);
  420. init.name = "emc";
  421. init.ops = &tegra_clk_emc_ops;
  422. init.flags = CLK_IS_CRITICAL;
  423. init.parent_names = emc_parent_clk_names;
  424. init.num_parents = ARRAY_SIZE(emc_parent_clk_names);
  425. tegra->hw.init = &init;
  426. clk = clk_register(NULL, &tegra->hw);
  427. if (IS_ERR(clk))
  428. return clk;
  429. tegra->prev_parent = clk_hw_get_parent_by_index(
  430. &tegra->hw, emc_get_parent(&tegra->hw))->clk;
  431. tegra->changing_timing = false;
  432. /* Allow debugging tools to see the EMC clock */
  433. clk_register_clkdev(clk, "emc", "tegra-clk-debug");
  434. return clk;
  435. };
  436. void tegra124_clk_set_emc_callbacks(tegra124_emc_prepare_timing_change_cb *prep_cb,
  437. tegra124_emc_complete_timing_change_cb *complete_cb)
  438. {
  439. struct clk *clk = __clk_lookup("emc");
  440. struct tegra_clk_emc *tegra;
  441. struct clk_hw *hw;
  442. if (clk) {
  443. hw = __clk_get_hw(clk);
  444. tegra = container_of(hw, struct tegra_clk_emc, hw);
  445. tegra->prepare_timing_change = prep_cb;
  446. tegra->complete_timing_change = complete_cb;
  447. }
  448. }
  449. EXPORT_SYMBOL_GPL(tegra124_clk_set_emc_callbacks);
  450. bool tegra124_clk_emc_driver_available(struct clk_hw *hw)
  451. {
  452. struct tegra_clk_emc *tegra = container_of(hw, struct tegra_clk_emc, hw);
  453. return tegra->prepare_timing_change && tegra->complete_timing_change;
  454. }