clk.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI clock support
  4. *
  5. * Copyright (C) 2013 Texas Instruments, Inc.
  6. *
  7. * Tero Kristo <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/ti.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/list.h>
  17. #include <linux/regmap.h>
  18. #include <linux/memblock.h>
  19. #include <linux/device.h>
  20. #include "clock.h"
  21. #undef pr_fmt
  22. #define pr_fmt(fmt) "%s: " fmt, __func__
  23. static LIST_HEAD(clk_hw_omap_clocks);
  24. struct ti_clk_ll_ops *ti_clk_ll_ops;
  25. static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
  26. struct ti_clk_features ti_clk_features;
  27. struct clk_iomap {
  28. struct regmap *regmap;
  29. void __iomem *mem;
  30. };
  31. static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
  32. static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
  33. {
  34. struct clk_iomap *io = clk_memmaps[reg->index];
  35. if (reg->ptr)
  36. writel_relaxed(val, reg->ptr);
  37. else if (io->regmap)
  38. regmap_write(io->regmap, reg->offset, val);
  39. else
  40. writel_relaxed(val, io->mem + reg->offset);
  41. }
  42. static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
  43. {
  44. u32 v;
  45. v = readl_relaxed(ptr);
  46. v &= ~mask;
  47. v |= val;
  48. writel_relaxed(v, ptr);
  49. }
  50. static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
  51. {
  52. struct clk_iomap *io = clk_memmaps[reg->index];
  53. if (reg->ptr) {
  54. _clk_rmw(val, mask, reg->ptr);
  55. } else if (io->regmap) {
  56. regmap_update_bits(io->regmap, reg->offset, mask, val);
  57. } else {
  58. _clk_rmw(val, mask, io->mem + reg->offset);
  59. }
  60. }
  61. static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
  62. {
  63. u32 val;
  64. struct clk_iomap *io = clk_memmaps[reg->index];
  65. if (reg->ptr)
  66. val = readl_relaxed(reg->ptr);
  67. else if (io->regmap)
  68. regmap_read(io->regmap, reg->offset, &val);
  69. else
  70. val = readl_relaxed(io->mem + reg->offset);
  71. return val;
  72. }
  73. /**
  74. * ti_clk_setup_ll_ops - setup low level clock operations
  75. * @ops: low level clock ops descriptor
  76. *
  77. * Sets up low level clock operations for TI clock driver. This is used
  78. * to provide various callbacks for the clock driver towards platform
  79. * specific code. Returns 0 on success, -EBUSY if ll_ops have been
  80. * registered already.
  81. */
  82. int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
  83. {
  84. if (ti_clk_ll_ops) {
  85. pr_err("Attempt to register ll_ops multiple times.\n");
  86. return -EBUSY;
  87. }
  88. ti_clk_ll_ops = ops;
  89. ops->clk_readl = clk_memmap_readl;
  90. ops->clk_writel = clk_memmap_writel;
  91. ops->clk_rmw = clk_memmap_rmw;
  92. return 0;
  93. }
  94. /*
  95. * Eventually we could standardize to using '_' for clk-*.c files to follow the
  96. * TRM naming and leave out the tmp name here.
  97. */
  98. static struct device_node *ti_find_clock_provider(struct device_node *from,
  99. const char *name)
  100. {
  101. struct device_node *np;
  102. bool found = false;
  103. const char *n;
  104. char *tmp;
  105. tmp = kstrdup(name, GFP_KERNEL);
  106. if (!tmp)
  107. return NULL;
  108. strreplace(tmp, '-', '_');
  109. /* Node named "clock" with "clock-output-names" */
  110. for_each_of_allnodes_from(from, np) {
  111. if (of_property_read_string_index(np, "clock-output-names",
  112. 0, &n))
  113. continue;
  114. if (!strncmp(n, tmp, strlen(tmp))) {
  115. of_node_get(np);
  116. found = true;
  117. break;
  118. }
  119. }
  120. kfree(tmp);
  121. if (found) {
  122. of_node_put(from);
  123. return np;
  124. }
  125. /* Fall back to using old node name base provider name */
  126. return of_find_node_by_name(from, name);
  127. }
  128. /**
  129. * ti_dt_clocks_register - register DT alias clocks during boot
  130. * @oclks: list of clocks to register
  131. *
  132. * Register alias or non-standard DT clock entries during boot. By
  133. * default, DT clocks are found based on their clock-output-names
  134. * property, or the clock node name for legacy cases. If any
  135. * additional con-id / dev-id -> clock mapping is required, use this
  136. * function to list these.
  137. */
  138. void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
  139. {
  140. struct ti_dt_clk *c;
  141. struct device_node *node, *parent, *child;
  142. struct clk *clk;
  143. struct of_phandle_args clkspec;
  144. char buf[64];
  145. char *ptr;
  146. char *tags[2];
  147. int i;
  148. int num_args;
  149. int ret;
  150. static bool clkctrl_nodes_missing;
  151. static bool has_clkctrl_data;
  152. static bool compat_mode;
  153. compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
  154. for (c = oclks; c->node_name != NULL; c++) {
  155. strcpy(buf, c->node_name);
  156. ptr = buf;
  157. for (i = 0; i < 2; i++)
  158. tags[i] = NULL;
  159. num_args = 0;
  160. while (*ptr) {
  161. if (*ptr == ':') {
  162. if (num_args >= 2) {
  163. pr_warn("Bad number of tags on %s\n",
  164. c->node_name);
  165. return;
  166. }
  167. tags[num_args++] = ptr + 1;
  168. *ptr = 0;
  169. }
  170. ptr++;
  171. }
  172. if (num_args && clkctrl_nodes_missing)
  173. continue;
  174. node = ti_find_clock_provider(NULL, buf);
  175. if (num_args && compat_mode) {
  176. parent = node;
  177. child = of_get_child_by_name(parent, "clock");
  178. if (!child)
  179. child = of_get_child_by_name(parent, "clk");
  180. if (child) {
  181. of_node_put(parent);
  182. node = child;
  183. }
  184. }
  185. clkspec.np = node;
  186. clkspec.args_count = num_args;
  187. for (i = 0; i < num_args; i++) {
  188. ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
  189. if (ret) {
  190. pr_warn("Bad tag in %s at %d: %s\n",
  191. c->node_name, i, tags[i]);
  192. of_node_put(node);
  193. return;
  194. }
  195. }
  196. clk = of_clk_get_from_provider(&clkspec);
  197. of_node_put(node);
  198. if (!IS_ERR(clk)) {
  199. c->lk.clk = clk;
  200. clkdev_add(&c->lk);
  201. } else {
  202. if (num_args && !has_clkctrl_data) {
  203. struct device_node *np;
  204. np = of_find_compatible_node(NULL, NULL,
  205. "ti,clkctrl");
  206. if (np) {
  207. has_clkctrl_data = true;
  208. of_node_put(np);
  209. } else {
  210. clkctrl_nodes_missing = true;
  211. pr_warn("missing clkctrl nodes, please update your dts.\n");
  212. continue;
  213. }
  214. }
  215. pr_warn("failed to lookup clock node %s, ret=%ld\n",
  216. c->node_name, PTR_ERR(clk));
  217. }
  218. }
  219. }
  220. struct clk_init_item {
  221. struct device_node *node;
  222. void *user;
  223. ti_of_clk_init_cb_t func;
  224. struct list_head link;
  225. };
  226. static LIST_HEAD(retry_list);
  227. /**
  228. * ti_clk_retry_init - retries a failed clock init at later phase
  229. * @node: device not for the clock
  230. * @user: user data pointer
  231. * @func: init function to be called for the clock
  232. *
  233. * Adds a failed clock init to the retry list. The retry list is parsed
  234. * once all the other clocks have been initialized.
  235. */
  236. int __init ti_clk_retry_init(struct device_node *node, void *user,
  237. ti_of_clk_init_cb_t func)
  238. {
  239. struct clk_init_item *retry;
  240. pr_debug("%pOFn: adding to retry list...\n", node);
  241. retry = kzalloc(sizeof(*retry), GFP_KERNEL);
  242. if (!retry)
  243. return -ENOMEM;
  244. retry->node = node;
  245. retry->func = func;
  246. retry->user = user;
  247. list_add(&retry->link, &retry_list);
  248. return 0;
  249. }
  250. /**
  251. * ti_clk_get_reg_addr - get register address for a clock register
  252. * @node: device node for the clock
  253. * @index: register index from the clock node
  254. * @reg: pointer to target register struct
  255. *
  256. * Builds clock register address from device tree information, and returns
  257. * the data via the provided output pointer @reg. Returns 0 on success,
  258. * negative error value on failure.
  259. */
  260. int ti_clk_get_reg_addr(struct device_node *node, int index,
  261. struct clk_omap_reg *reg)
  262. {
  263. u32 val;
  264. int i;
  265. for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
  266. if (clocks_node_ptr[i] == node->parent)
  267. break;
  268. if (clocks_node_ptr[i] == node->parent->parent)
  269. break;
  270. }
  271. if (i == CLK_MAX_MEMMAPS) {
  272. pr_err("clk-provider not found for %pOFn!\n", node);
  273. return -ENOENT;
  274. }
  275. reg->index = i;
  276. if (of_property_read_u32_index(node, "reg", index, &val)) {
  277. if (of_property_read_u32_index(node->parent, "reg",
  278. index, &val)) {
  279. pr_err("%pOFn or parent must have reg[%d]!\n",
  280. node, index);
  281. return -EINVAL;
  282. }
  283. }
  284. reg->offset = val;
  285. reg->ptr = NULL;
  286. return 0;
  287. }
  288. void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
  289. {
  290. u32 latch;
  291. if (shift < 0)
  292. return;
  293. latch = 1 << shift;
  294. ti_clk_ll_ops->clk_rmw(latch, latch, reg);
  295. ti_clk_ll_ops->clk_rmw(0, latch, reg);
  296. ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
  297. }
  298. /**
  299. * omap2_clk_provider_init - init master clock provider
  300. * @parent: master node
  301. * @index: internal index for clk_reg_ops
  302. * @syscon: syscon regmap pointer for accessing clock registers
  303. * @mem: iomem pointer for the clock provider memory area, only used if
  304. * syscon is not provided
  305. *
  306. * Initializes a master clock IP block. This basically sets up the
  307. * mapping from clocks node to the memory map index. All the clocks
  308. * are then initialized through the common of_clk_init call, and the
  309. * clocks will access their memory maps based on the node layout.
  310. * Returns 0 in success.
  311. */
  312. int __init omap2_clk_provider_init(struct device_node *parent, int index,
  313. struct regmap *syscon, void __iomem *mem)
  314. {
  315. struct device_node *clocks;
  316. struct clk_iomap *io;
  317. /* get clocks for this parent */
  318. clocks = of_get_child_by_name(parent, "clocks");
  319. if (!clocks) {
  320. pr_err("%pOFn missing 'clocks' child node.\n", parent);
  321. return -EINVAL;
  322. }
  323. /* add clocks node info */
  324. clocks_node_ptr[index] = clocks;
  325. io = kzalloc(sizeof(*io), GFP_KERNEL);
  326. if (!io)
  327. return -ENOMEM;
  328. io->regmap = syscon;
  329. io->mem = mem;
  330. clk_memmaps[index] = io;
  331. return 0;
  332. }
  333. /**
  334. * omap2_clk_legacy_provider_init - initialize a legacy clock provider
  335. * @index: index for the clock provider
  336. * @mem: iomem pointer for the clock provider memory area
  337. *
  338. * Initializes a legacy clock provider memory mapping.
  339. */
  340. void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
  341. {
  342. struct clk_iomap *io;
  343. io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
  344. if (!io)
  345. panic("%s: Failed to allocate %zu bytes\n", __func__,
  346. sizeof(*io));
  347. io->mem = mem;
  348. clk_memmaps[index] = io;
  349. }
  350. /**
  351. * ti_dt_clk_init_retry_clks - init clocks from the retry list
  352. *
  353. * Initializes any clocks that have failed to initialize before,
  354. * reasons being missing parent node(s) during earlier init. This
  355. * typically happens only for DPLLs which need to have both of their
  356. * parent clocks ready during init.
  357. */
  358. void ti_dt_clk_init_retry_clks(void)
  359. {
  360. struct clk_init_item *retry;
  361. struct clk_init_item *tmp;
  362. int retries = 5;
  363. while (!list_empty(&retry_list) && retries) {
  364. list_for_each_entry_safe(retry, tmp, &retry_list, link) {
  365. pr_debug("retry-init: %pOFn\n", retry->node);
  366. retry->func(retry->user, retry->node);
  367. list_del(&retry->link);
  368. kfree(retry);
  369. }
  370. retries--;
  371. }
  372. }
  373. static const struct of_device_id simple_clk_match_table[] __initconst = {
  374. { .compatible = "fixed-clock" },
  375. { .compatible = "fixed-factor-clock" },
  376. { }
  377. };
  378. /**
  379. * ti_dt_clk_name - init clock name from first output name or node name
  380. * @np: device node
  381. *
  382. * Use the first clock-output-name for the clock name if found. Fall back
  383. * to legacy naming based on node name.
  384. */
  385. const char *ti_dt_clk_name(struct device_node *np)
  386. {
  387. const char *name;
  388. if (!of_property_read_string_index(np, "clock-output-names", 0,
  389. &name))
  390. return name;
  391. return np->name;
  392. }
  393. /**
  394. * ti_clk_add_aliases - setup clock aliases
  395. *
  396. * Sets up any missing clock aliases. No return value.
  397. */
  398. void __init ti_clk_add_aliases(void)
  399. {
  400. struct device_node *np;
  401. struct clk *clk;
  402. for_each_matching_node(np, simple_clk_match_table) {
  403. struct of_phandle_args clkspec;
  404. clkspec.np = np;
  405. clk = of_clk_get_from_provider(&clkspec);
  406. ti_clk_add_alias(clk, ti_dt_clk_name(np));
  407. }
  408. }
  409. /**
  410. * ti_clk_setup_features - setup clock features flags
  411. * @features: features definition to use
  412. *
  413. * Initializes the clock driver features flags based on platform
  414. * provided data. No return value.
  415. */
  416. void __init ti_clk_setup_features(struct ti_clk_features *features)
  417. {
  418. memcpy(&ti_clk_features, features, sizeof(*features));
  419. }
  420. /**
  421. * ti_clk_get_features - get clock driver features flags
  422. *
  423. * Get TI clock driver features description. Returns a pointer
  424. * to the current feature setup.
  425. */
  426. const struct ti_clk_features *ti_clk_get_features(void)
  427. {
  428. return &ti_clk_features;
  429. }
  430. /**
  431. * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
  432. * @clk_names: ptr to an array of strings of clock names to enable
  433. * @num_clocks: number of clock names in @clk_names
  434. *
  435. * Prepare and enable a list of clocks, named by @clk_names. No
  436. * return value. XXX Deprecated; only needed until these clocks are
  437. * properly claimed and enabled by the drivers or core code that uses
  438. * them. XXX What code disables & calls clk_put on these clocks?
  439. */
  440. void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
  441. {
  442. struct clk *init_clk;
  443. int i;
  444. for (i = 0; i < num_clocks; i++) {
  445. init_clk = clk_get(NULL, clk_names[i]);
  446. if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
  447. clk_names[i]))
  448. continue;
  449. clk_prepare_enable(init_clk);
  450. }
  451. }
  452. /**
  453. * ti_clk_add_alias - add a clock alias for a TI clock
  454. * @clk: clock handle to create alias for
  455. * @con: connection ID for this clock
  456. *
  457. * Creates a clock alias for a TI clock. Allocates the clock lookup entry
  458. * and assigns the data to it. Returns 0 if successful, negative error
  459. * value otherwise.
  460. */
  461. int ti_clk_add_alias(struct clk *clk, const char *con)
  462. {
  463. struct clk_lookup *cl;
  464. if (!clk)
  465. return 0;
  466. if (IS_ERR(clk))
  467. return PTR_ERR(clk);
  468. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  469. if (!cl)
  470. return -ENOMEM;
  471. cl->con_id = con;
  472. cl->clk = clk;
  473. clkdev_add(cl);
  474. return 0;
  475. }
  476. /**
  477. * of_ti_clk_register - register a TI clock to the common clock framework
  478. * @node: device node for this clock
  479. * @hw: hardware clock handle
  480. * @con: connection ID for this clock
  481. *
  482. * Registers a TI clock to the common clock framework, and adds a clock
  483. * alias for it. Returns a handle to the registered clock if successful,
  484. * ERR_PTR value in failure.
  485. */
  486. struct clk *of_ti_clk_register(struct device_node *node, struct clk_hw *hw,
  487. const char *con)
  488. {
  489. struct clk *clk;
  490. int ret;
  491. ret = of_clk_hw_register(node, hw);
  492. if (ret)
  493. return ERR_PTR(ret);
  494. clk = hw->clk;
  495. ret = ti_clk_add_alias(clk, con);
  496. if (ret) {
  497. clk_unregister(clk);
  498. return ERR_PTR(ret);
  499. }
  500. return clk;
  501. }
  502. /**
  503. * of_ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
  504. * @node: device node for this clock
  505. * @hw: hardware clock handle
  506. * @con: connection ID for this clock
  507. *
  508. * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
  509. * for it, and adds the list to the available clk_hw_omap type clocks.
  510. * Returns a handle to the registered clock if successful, ERR_PTR value
  511. * in failure.
  512. */
  513. struct clk *of_ti_clk_register_omap_hw(struct device_node *node,
  514. struct clk_hw *hw, const char *con)
  515. {
  516. struct clk *clk;
  517. struct clk_hw_omap *oclk;
  518. clk = of_ti_clk_register(node, hw, con);
  519. if (IS_ERR(clk))
  520. return clk;
  521. oclk = to_clk_hw_omap(hw);
  522. list_add(&oclk->node, &clk_hw_omap_clocks);
  523. return clk;
  524. }
  525. /**
  526. * omap2_clk_for_each - call function for each registered clk_hw_omap
  527. * @fn: pointer to a callback function
  528. *
  529. * Call @fn for each registered clk_hw_omap, passing @hw to each
  530. * function. @fn must return 0 for success or any other value for
  531. * failure. If @fn returns non-zero, the iteration across clocks
  532. * will stop and the non-zero return value will be passed to the
  533. * caller of omap2_clk_for_each().
  534. */
  535. int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
  536. {
  537. int ret;
  538. struct clk_hw_omap *hw;
  539. list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
  540. ret = (*fn)(hw);
  541. if (ret)
  542. break;
  543. }
  544. return ret;
  545. }
  546. /**
  547. * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
  548. * @hw: clk_hw to check if it is an omap clock or not
  549. *
  550. * Checks if the provided clk_hw is OMAP clock or not. Returns true if
  551. * it is, false otherwise.
  552. */
  553. bool omap2_clk_is_hw_omap(struct clk_hw *hw)
  554. {
  555. struct clk_hw_omap *oclk;
  556. list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {
  557. if (&oclk->hw == hw)
  558. return true;
  559. }
  560. return false;
  561. }