clk.c 17 KB

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