clk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * TI clock support
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. *
  6. * Tero Kristo <t-kristo@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/clkdev.h>
  20. #include <linux/clk/ti.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/list.h>
  24. #include <linux/regmap.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/device.h>
  27. #include "clock.h"
  28. #undef pr_fmt
  29. #define pr_fmt(fmt) "%s: " fmt, __func__
  30. struct ti_clk_ll_ops *ti_clk_ll_ops;
  31. static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
  32. static struct ti_clk_features ti_clk_features;
  33. struct clk_iomap {
  34. struct regmap *regmap;
  35. void __iomem *mem;
  36. };
  37. static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
  38. static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
  39. {
  40. struct clk_iomap *io = clk_memmaps[reg->index];
  41. if (reg->ptr)
  42. writel_relaxed(val, reg->ptr);
  43. else if (io->regmap)
  44. regmap_write(io->regmap, reg->offset, val);
  45. else
  46. writel_relaxed(val, io->mem + reg->offset);
  47. }
  48. static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
  49. {
  50. u32 v;
  51. v = readl_relaxed(ptr);
  52. v &= ~mask;
  53. v |= val;
  54. writel_relaxed(v, ptr);
  55. }
  56. static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
  57. {
  58. struct clk_iomap *io = clk_memmaps[reg->index];
  59. if (reg->ptr) {
  60. _clk_rmw(val, mask, reg->ptr);
  61. } else if (io->regmap) {
  62. regmap_update_bits(io->regmap, reg->offset, mask, val);
  63. } else {
  64. _clk_rmw(val, mask, io->mem + reg->offset);
  65. }
  66. }
  67. static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
  68. {
  69. u32 val;
  70. struct clk_iomap *io = clk_memmaps[reg->index];
  71. if (reg->ptr)
  72. val = readl_relaxed(reg->ptr);
  73. else if (io->regmap)
  74. regmap_read(io->regmap, reg->offset, &val);
  75. else
  76. val = readl_relaxed(io->mem + reg->offset);
  77. return val;
  78. }
  79. /**
  80. * ti_clk_setup_ll_ops - setup low level clock operations
  81. * @ops: low level clock ops descriptor
  82. *
  83. * Sets up low level clock operations for TI clock driver. This is used
  84. * to provide various callbacks for the clock driver towards platform
  85. * specific code. Returns 0 on success, -EBUSY if ll_ops have been
  86. * registered already.
  87. */
  88. int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
  89. {
  90. if (ti_clk_ll_ops) {
  91. pr_err("Attempt to register ll_ops multiple times.\n");
  92. return -EBUSY;
  93. }
  94. ti_clk_ll_ops = ops;
  95. ops->clk_readl = clk_memmap_readl;
  96. ops->clk_writel = clk_memmap_writel;
  97. ops->clk_rmw = clk_memmap_rmw;
  98. return 0;
  99. }
  100. /**
  101. * ti_dt_clocks_register - register DT alias clocks during boot
  102. * @oclks: list of clocks to register
  103. *
  104. * Register alias or non-standard DT clock entries during boot. By
  105. * default, DT clocks are found based on their node name. If any
  106. * additional con-id / dev-id -> clock mapping is required, use this
  107. * function to list these.
  108. */
  109. void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
  110. {
  111. struct ti_dt_clk *c;
  112. struct device_node *node;
  113. struct clk *clk;
  114. struct of_phandle_args clkspec;
  115. for (c = oclks; c->node_name != NULL; c++) {
  116. node = of_find_node_by_name(NULL, c->node_name);
  117. clkspec.np = node;
  118. clk = of_clk_get_from_provider(&clkspec);
  119. if (!IS_ERR(clk)) {
  120. c->lk.clk = clk;
  121. clkdev_add(&c->lk);
  122. } else {
  123. pr_warn("failed to lookup clock node %s\n",
  124. c->node_name);
  125. }
  126. }
  127. }
  128. struct clk_init_item {
  129. struct device_node *node;
  130. struct clk_hw *hw;
  131. ti_of_clk_init_cb_t func;
  132. struct list_head link;
  133. };
  134. static LIST_HEAD(retry_list);
  135. /**
  136. * ti_clk_retry_init - retries a failed clock init at later phase
  137. * @node: device not for the clock
  138. * @hw: partially initialized clk_hw struct for the clock
  139. * @func: init function to be called for the clock
  140. *
  141. * Adds a failed clock init to the retry list. The retry list is parsed
  142. * once all the other clocks have been initialized.
  143. */
  144. int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
  145. ti_of_clk_init_cb_t func)
  146. {
  147. struct clk_init_item *retry;
  148. pr_debug("%s: adding to retry list...\n", node->name);
  149. retry = kzalloc(sizeof(*retry), GFP_KERNEL);
  150. if (!retry)
  151. return -ENOMEM;
  152. retry->node = node;
  153. retry->func = func;
  154. retry->hw = hw;
  155. list_add(&retry->link, &retry_list);
  156. return 0;
  157. }
  158. /**
  159. * ti_clk_get_reg_addr - get register address for a clock register
  160. * @node: device node for the clock
  161. * @index: register index from the clock node
  162. * @reg: pointer to target register struct
  163. *
  164. * Builds clock register address from device tree information, and returns
  165. * the data via the provided output pointer @reg. Returns 0 on success,
  166. * negative error value on failure.
  167. */
  168. int ti_clk_get_reg_addr(struct device_node *node, int index,
  169. struct clk_omap_reg *reg)
  170. {
  171. u32 val;
  172. int i;
  173. for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
  174. if (clocks_node_ptr[i] == node->parent)
  175. break;
  176. }
  177. if (i == CLK_MAX_MEMMAPS) {
  178. pr_err("clk-provider not found for %s!\n", node->name);
  179. return -ENOENT;
  180. }
  181. reg->index = i;
  182. if (of_property_read_u32_index(node, "reg", index, &val)) {
  183. pr_err("%s must have reg[%d]!\n", node->name, index);
  184. return -EINVAL;
  185. }
  186. reg->offset = val;
  187. reg->ptr = NULL;
  188. return 0;
  189. }
  190. void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
  191. {
  192. u32 latch;
  193. if (shift < 0)
  194. return;
  195. latch = 1 << shift;
  196. ti_clk_ll_ops->clk_rmw(latch, latch, reg);
  197. ti_clk_ll_ops->clk_rmw(0, latch, reg);
  198. ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
  199. }
  200. /**
  201. * omap2_clk_provider_init - init master clock provider
  202. * @parent: master node
  203. * @index: internal index for clk_reg_ops
  204. * @syscon: syscon regmap pointer for accessing clock registers
  205. * @mem: iomem pointer for the clock provider memory area, only used if
  206. * syscon is not provided
  207. *
  208. * Initializes a master clock IP block. This basically sets up the
  209. * mapping from clocks node to the memory map index. All the clocks
  210. * are then initialized through the common of_clk_init call, and the
  211. * clocks will access their memory maps based on the node layout.
  212. * Returns 0 in success.
  213. */
  214. int __init omap2_clk_provider_init(struct device_node *parent, int index,
  215. struct regmap *syscon, void __iomem *mem)
  216. {
  217. struct device_node *clocks;
  218. struct clk_iomap *io;
  219. /* get clocks for this parent */
  220. clocks = of_get_child_by_name(parent, "clocks");
  221. if (!clocks) {
  222. pr_err("%s missing 'clocks' child node.\n", parent->name);
  223. return -EINVAL;
  224. }
  225. /* add clocks node info */
  226. clocks_node_ptr[index] = clocks;
  227. io = kzalloc(sizeof(*io), GFP_KERNEL);
  228. if (!io)
  229. return -ENOMEM;
  230. io->regmap = syscon;
  231. io->mem = mem;
  232. clk_memmaps[index] = io;
  233. return 0;
  234. }
  235. /**
  236. * omap2_clk_legacy_provider_init - initialize a legacy clock provider
  237. * @index: index for the clock provider
  238. * @mem: iomem pointer for the clock provider memory area
  239. *
  240. * Initializes a legacy clock provider memory mapping.
  241. */
  242. void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
  243. {
  244. struct clk_iomap *io;
  245. io = memblock_virt_alloc(sizeof(*io), 0);
  246. io->mem = mem;
  247. clk_memmaps[index] = io;
  248. }
  249. /**
  250. * ti_dt_clk_init_retry_clks - init clocks from the retry list
  251. *
  252. * Initializes any clocks that have failed to initialize before,
  253. * reasons being missing parent node(s) during earlier init. This
  254. * typically happens only for DPLLs which need to have both of their
  255. * parent clocks ready during init.
  256. */
  257. void ti_dt_clk_init_retry_clks(void)
  258. {
  259. struct clk_init_item *retry;
  260. struct clk_init_item *tmp;
  261. int retries = 5;
  262. while (!list_empty(&retry_list) && retries) {
  263. list_for_each_entry_safe(retry, tmp, &retry_list, link) {
  264. pr_debug("retry-init: %s\n", retry->node->name);
  265. retry->func(retry->hw, retry->node);
  266. list_del(&retry->link);
  267. kfree(retry);
  268. }
  269. retries--;
  270. }
  271. }
  272. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
  273. void __init ti_clk_patch_legacy_clks(struct ti_clk **patch)
  274. {
  275. while (*patch) {
  276. memcpy((*patch)->patch, *patch, sizeof(**patch));
  277. patch++;
  278. }
  279. }
  280. struct clk __init *ti_clk_register_clk(struct ti_clk *setup)
  281. {
  282. struct clk *clk;
  283. struct ti_clk_fixed *fixed;
  284. struct ti_clk_fixed_factor *fixed_factor;
  285. struct clk_hw *clk_hw;
  286. int ret;
  287. if (setup->clk)
  288. return setup->clk;
  289. switch (setup->type) {
  290. case TI_CLK_FIXED:
  291. fixed = setup->data;
  292. clk = clk_register_fixed_rate(NULL, setup->name, NULL, 0,
  293. fixed->frequency);
  294. if (!IS_ERR(clk)) {
  295. ret = ti_clk_add_alias(NULL, clk, setup->name);
  296. if (ret) {
  297. clk_unregister(clk);
  298. clk = ERR_PTR(ret);
  299. }
  300. }
  301. break;
  302. case TI_CLK_MUX:
  303. clk = ti_clk_register_mux(setup);
  304. break;
  305. case TI_CLK_DIVIDER:
  306. clk = ti_clk_register_divider(setup);
  307. break;
  308. case TI_CLK_COMPOSITE:
  309. clk = ti_clk_register_composite(setup);
  310. break;
  311. case TI_CLK_FIXED_FACTOR:
  312. fixed_factor = setup->data;
  313. clk = clk_register_fixed_factor(NULL, setup->name,
  314. fixed_factor->parent,
  315. 0, fixed_factor->mult,
  316. fixed_factor->div);
  317. if (!IS_ERR(clk)) {
  318. ret = ti_clk_add_alias(NULL, clk, setup->name);
  319. if (ret) {
  320. clk_unregister(clk);
  321. clk = ERR_PTR(ret);
  322. }
  323. }
  324. break;
  325. case TI_CLK_GATE:
  326. clk = ti_clk_register_gate(setup);
  327. break;
  328. case TI_CLK_DPLL:
  329. clk = ti_clk_register_dpll(setup);
  330. break;
  331. default:
  332. pr_err("bad type for %s!\n", setup->name);
  333. clk = ERR_PTR(-EINVAL);
  334. }
  335. if (!IS_ERR(clk)) {
  336. setup->clk = clk;
  337. if (setup->clkdm_name) {
  338. clk_hw = __clk_get_hw(clk);
  339. if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) {
  340. pr_warn("can't setup clkdm for basic clk %s\n",
  341. setup->name);
  342. } else {
  343. to_clk_hw_omap(clk_hw)->clkdm_name =
  344. setup->clkdm_name;
  345. omap2_init_clk_clkdm(clk_hw);
  346. }
  347. }
  348. }
  349. return clk;
  350. }
  351. int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
  352. {
  353. struct clk *clk;
  354. bool retry;
  355. struct ti_clk_alias *retry_clk;
  356. struct ti_clk_alias *tmp;
  357. while (clks->clk) {
  358. clk = ti_clk_register_clk(clks->clk);
  359. if (IS_ERR(clk)) {
  360. if (PTR_ERR(clk) == -EAGAIN) {
  361. list_add(&clks->link, &retry_list);
  362. } else {
  363. pr_err("register for %s failed: %ld\n",
  364. clks->clk->name, PTR_ERR(clk));
  365. return PTR_ERR(clk);
  366. }
  367. }
  368. clks++;
  369. }
  370. retry = true;
  371. while (!list_empty(&retry_list) && retry) {
  372. retry = false;
  373. list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
  374. pr_debug("retry-init: %s\n", retry_clk->clk->name);
  375. clk = ti_clk_register_clk(retry_clk->clk);
  376. if (IS_ERR(clk)) {
  377. if (PTR_ERR(clk) == -EAGAIN) {
  378. continue;
  379. } else {
  380. pr_err("register for %s failed: %ld\n",
  381. retry_clk->clk->name,
  382. PTR_ERR(clk));
  383. return PTR_ERR(clk);
  384. }
  385. } else {
  386. retry = true;
  387. list_del(&retry_clk->link);
  388. }
  389. }
  390. }
  391. return 0;
  392. }
  393. #endif
  394. static const struct of_device_id simple_clk_match_table[] __initconst = {
  395. { .compatible = "fixed-clock" },
  396. { .compatible = "fixed-factor-clock" },
  397. { }
  398. };
  399. /**
  400. * ti_clk_add_aliases - setup clock aliases
  401. *
  402. * Sets up any missing clock aliases. No return value.
  403. */
  404. void __init ti_clk_add_aliases(void)
  405. {
  406. struct device_node *np;
  407. struct clk *clk;
  408. for_each_matching_node(np, simple_clk_match_table) {
  409. struct of_phandle_args clkspec;
  410. clkspec.np = np;
  411. clk = of_clk_get_from_provider(&clkspec);
  412. ti_clk_add_alias(NULL, clk, np->name);
  413. }
  414. }
  415. /**
  416. * ti_clk_setup_features - setup clock features flags
  417. * @features: features definition to use
  418. *
  419. * Initializes the clock driver features flags based on platform
  420. * provided data. No return value.
  421. */
  422. void __init ti_clk_setup_features(struct ti_clk_features *features)
  423. {
  424. memcpy(&ti_clk_features, features, sizeof(*features));
  425. }
  426. /**
  427. * ti_clk_get_features - get clock driver features flags
  428. *
  429. * Get TI clock driver features description. Returns a pointer
  430. * to the current feature setup.
  431. */
  432. const struct ti_clk_features *ti_clk_get_features(void)
  433. {
  434. return &ti_clk_features;
  435. }
  436. /**
  437. * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
  438. * @clk_names: ptr to an array of strings of clock names to enable
  439. * @num_clocks: number of clock names in @clk_names
  440. *
  441. * Prepare and enable a list of clocks, named by @clk_names. No
  442. * return value. XXX Deprecated; only needed until these clocks are
  443. * properly claimed and enabled by the drivers or core code that uses
  444. * them. XXX What code disables & calls clk_put on these clocks?
  445. */
  446. void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
  447. {
  448. struct clk *init_clk;
  449. int i;
  450. for (i = 0; i < num_clocks; i++) {
  451. init_clk = clk_get(NULL, clk_names[i]);
  452. if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
  453. clk_names[i]))
  454. continue;
  455. clk_prepare_enable(init_clk);
  456. }
  457. }
  458. /**
  459. * ti_clk_add_alias - add a clock alias for a TI clock
  460. * @dev: device alias for this clock
  461. * @clk: clock handle to create alias for
  462. * @con: connection ID for this clock
  463. *
  464. * Creates a clock alias for a TI clock. Allocates the clock lookup entry
  465. * and assigns the data to it. Returns 0 if successful, negative error
  466. * value otherwise.
  467. */
  468. int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
  469. {
  470. struct clk_lookup *cl;
  471. if (!clk)
  472. return 0;
  473. if (IS_ERR(clk))
  474. return PTR_ERR(clk);
  475. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  476. if (!cl)
  477. return -ENOMEM;
  478. if (dev)
  479. cl->dev_id = dev_name(dev);
  480. cl->con_id = con;
  481. cl->clk = clk;
  482. clkdev_add(cl);
  483. return 0;
  484. }
  485. /**
  486. * ti_clk_register - register a TI clock to the common clock framework
  487. * @dev: device for this clock
  488. * @hw: hardware clock handle
  489. * @con: connection ID for this clock
  490. *
  491. * Registers a TI clock to the common clock framework, and adds a clock
  492. * alias for it. Returns a handle to the registered clock if successful,
  493. * ERR_PTR value in failure.
  494. */
  495. struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
  496. const char *con)
  497. {
  498. struct clk *clk;
  499. int ret;
  500. clk = clk_register(dev, hw);
  501. if (IS_ERR(clk))
  502. return clk;
  503. ret = ti_clk_add_alias(dev, clk, con);
  504. if (ret) {
  505. clk_unregister(clk);
  506. return ERR_PTR(ret);
  507. }
  508. return clk;
  509. }