clk-emc.c 13 KB

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