clk.c 13 KB

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