clk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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/memblock.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. 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, *parent;
  113. struct clk *clk;
  114. struct of_phandle_args clkspec;
  115. char buf[64];
  116. char *ptr;
  117. char *tags[2];
  118. int i;
  119. int num_args;
  120. int ret;
  121. static bool clkctrl_nodes_missing;
  122. static bool has_clkctrl_data;
  123. static bool compat_mode;
  124. compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
  125. for (c = oclks; c->node_name != NULL; c++) {
  126. strcpy(buf, c->node_name);
  127. ptr = buf;
  128. for (i = 0; i < 2; i++)
  129. tags[i] = NULL;
  130. num_args = 0;
  131. while (*ptr) {
  132. if (*ptr == ':') {
  133. if (num_args >= 2) {
  134. pr_warn("Bad number of tags on %s\n",
  135. c->node_name);
  136. return;
  137. }
  138. tags[num_args++] = ptr + 1;
  139. *ptr = 0;
  140. }
  141. ptr++;
  142. }
  143. if (num_args && clkctrl_nodes_missing)
  144. continue;
  145. node = of_find_node_by_name(NULL, buf);
  146. if (num_args && compat_mode) {
  147. parent = node;
  148. node = of_get_child_by_name(parent, "clk");
  149. of_node_put(parent);
  150. }
  151. clkspec.np = node;
  152. clkspec.args_count = num_args;
  153. for (i = 0; i < num_args; i++) {
  154. ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
  155. if (ret) {
  156. pr_warn("Bad tag in %s at %d: %s\n",
  157. c->node_name, i, tags[i]);
  158. of_node_put(node);
  159. return;
  160. }
  161. }
  162. clk = of_clk_get_from_provider(&clkspec);
  163. of_node_put(node);
  164. if (!IS_ERR(clk)) {
  165. c->lk.clk = clk;
  166. clkdev_add(&c->lk);
  167. } else {
  168. if (num_args && !has_clkctrl_data) {
  169. if (of_find_compatible_node(NULL, NULL,
  170. "ti,clkctrl")) {
  171. has_clkctrl_data = true;
  172. } else {
  173. clkctrl_nodes_missing = true;
  174. pr_warn("missing clkctrl nodes, please update your dts.\n");
  175. continue;
  176. }
  177. }
  178. pr_warn("failed to lookup clock node %s, ret=%ld\n",
  179. c->node_name, PTR_ERR(clk));
  180. }
  181. }
  182. }
  183. struct clk_init_item {
  184. struct device_node *node;
  185. void *user;
  186. ti_of_clk_init_cb_t func;
  187. struct list_head link;
  188. };
  189. static LIST_HEAD(retry_list);
  190. /**
  191. * ti_clk_retry_init - retries a failed clock init at later phase
  192. * @node: device not for the clock
  193. * @user: user data pointer
  194. * @func: init function to be called for the clock
  195. *
  196. * Adds a failed clock init to the retry list. The retry list is parsed
  197. * once all the other clocks have been initialized.
  198. */
  199. int __init ti_clk_retry_init(struct device_node *node, void *user,
  200. ti_of_clk_init_cb_t func)
  201. {
  202. struct clk_init_item *retry;
  203. pr_debug("%pOFn: adding to retry list...\n", node);
  204. retry = kzalloc(sizeof(*retry), GFP_KERNEL);
  205. if (!retry)
  206. return -ENOMEM;
  207. retry->node = node;
  208. retry->func = func;
  209. retry->user = user;
  210. list_add(&retry->link, &retry_list);
  211. return 0;
  212. }
  213. /**
  214. * ti_clk_get_reg_addr - get register address for a clock register
  215. * @node: device node for the clock
  216. * @index: register index from the clock node
  217. * @reg: pointer to target register struct
  218. *
  219. * Builds clock register address from device tree information, and returns
  220. * the data via the provided output pointer @reg. Returns 0 on success,
  221. * negative error value on failure.
  222. */
  223. int ti_clk_get_reg_addr(struct device_node *node, int index,
  224. struct clk_omap_reg *reg)
  225. {
  226. u32 val;
  227. int i;
  228. for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
  229. if (clocks_node_ptr[i] == node->parent)
  230. break;
  231. }
  232. if (i == CLK_MAX_MEMMAPS) {
  233. pr_err("clk-provider not found for %pOFn!\n", node);
  234. return -ENOENT;
  235. }
  236. reg->index = i;
  237. if (of_property_read_u32_index(node, "reg", index, &val)) {
  238. pr_err("%pOFn must have reg[%d]!\n", node, index);
  239. return -EINVAL;
  240. }
  241. reg->offset = val;
  242. reg->ptr = NULL;
  243. return 0;
  244. }
  245. void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
  246. {
  247. u32 latch;
  248. if (shift < 0)
  249. return;
  250. latch = 1 << shift;
  251. ti_clk_ll_ops->clk_rmw(latch, latch, reg);
  252. ti_clk_ll_ops->clk_rmw(0, latch, reg);
  253. ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
  254. }
  255. /**
  256. * omap2_clk_provider_init - init master clock provider
  257. * @parent: master node
  258. * @index: internal index for clk_reg_ops
  259. * @syscon: syscon regmap pointer for accessing clock registers
  260. * @mem: iomem pointer for the clock provider memory area, only used if
  261. * syscon is not provided
  262. *
  263. * Initializes a master clock IP block. This basically sets up the
  264. * mapping from clocks node to the memory map index. All the clocks
  265. * are then initialized through the common of_clk_init call, and the
  266. * clocks will access their memory maps based on the node layout.
  267. * Returns 0 in success.
  268. */
  269. int __init omap2_clk_provider_init(struct device_node *parent, int index,
  270. struct regmap *syscon, void __iomem *mem)
  271. {
  272. struct device_node *clocks;
  273. struct clk_iomap *io;
  274. /* get clocks for this parent */
  275. clocks = of_get_child_by_name(parent, "clocks");
  276. if (!clocks) {
  277. pr_err("%pOFn missing 'clocks' child node.\n", parent);
  278. return -EINVAL;
  279. }
  280. /* add clocks node info */
  281. clocks_node_ptr[index] = clocks;
  282. io = kzalloc(sizeof(*io), GFP_KERNEL);
  283. if (!io)
  284. return -ENOMEM;
  285. io->regmap = syscon;
  286. io->mem = mem;
  287. clk_memmaps[index] = io;
  288. return 0;
  289. }
  290. /**
  291. * omap2_clk_legacy_provider_init - initialize a legacy clock provider
  292. * @index: index for the clock provider
  293. * @mem: iomem pointer for the clock provider memory area
  294. *
  295. * Initializes a legacy clock provider memory mapping.
  296. */
  297. void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
  298. {
  299. struct clk_iomap *io;
  300. io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
  301. io->mem = mem;
  302. clk_memmaps[index] = io;
  303. }
  304. /**
  305. * ti_dt_clk_init_retry_clks - init clocks from the retry list
  306. *
  307. * Initializes any clocks that have failed to initialize before,
  308. * reasons being missing parent node(s) during earlier init. This
  309. * typically happens only for DPLLs which need to have both of their
  310. * parent clocks ready during init.
  311. */
  312. void ti_dt_clk_init_retry_clks(void)
  313. {
  314. struct clk_init_item *retry;
  315. struct clk_init_item *tmp;
  316. int retries = 5;
  317. while (!list_empty(&retry_list) && retries) {
  318. list_for_each_entry_safe(retry, tmp, &retry_list, link) {
  319. pr_debug("retry-init: %pOFn\n", retry->node);
  320. retry->func(retry->user, retry->node);
  321. list_del(&retry->link);
  322. kfree(retry);
  323. }
  324. retries--;
  325. }
  326. }
  327. static const struct of_device_id simple_clk_match_table[] __initconst = {
  328. { .compatible = "fixed-clock" },
  329. { .compatible = "fixed-factor-clock" },
  330. { }
  331. };
  332. /**
  333. * ti_clk_add_aliases - setup clock aliases
  334. *
  335. * Sets up any missing clock aliases. No return value.
  336. */
  337. void __init ti_clk_add_aliases(void)
  338. {
  339. struct device_node *np;
  340. struct clk *clk;
  341. for_each_matching_node(np, simple_clk_match_table) {
  342. struct of_phandle_args clkspec;
  343. clkspec.np = np;
  344. clk = of_clk_get_from_provider(&clkspec);
  345. ti_clk_add_alias(NULL, clk, np->name);
  346. }
  347. }
  348. /**
  349. * ti_clk_setup_features - setup clock features flags
  350. * @features: features definition to use
  351. *
  352. * Initializes the clock driver features flags based on platform
  353. * provided data. No return value.
  354. */
  355. void __init ti_clk_setup_features(struct ti_clk_features *features)
  356. {
  357. memcpy(&ti_clk_features, features, sizeof(*features));
  358. }
  359. /**
  360. * ti_clk_get_features - get clock driver features flags
  361. *
  362. * Get TI clock driver features description. Returns a pointer
  363. * to the current feature setup.
  364. */
  365. const struct ti_clk_features *ti_clk_get_features(void)
  366. {
  367. return &ti_clk_features;
  368. }
  369. /**
  370. * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
  371. * @clk_names: ptr to an array of strings of clock names to enable
  372. * @num_clocks: number of clock names in @clk_names
  373. *
  374. * Prepare and enable a list of clocks, named by @clk_names. No
  375. * return value. XXX Deprecated; only needed until these clocks are
  376. * properly claimed and enabled by the drivers or core code that uses
  377. * them. XXX What code disables & calls clk_put on these clocks?
  378. */
  379. void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
  380. {
  381. struct clk *init_clk;
  382. int i;
  383. for (i = 0; i < num_clocks; i++) {
  384. init_clk = clk_get(NULL, clk_names[i]);
  385. if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
  386. clk_names[i]))
  387. continue;
  388. clk_prepare_enable(init_clk);
  389. }
  390. }
  391. /**
  392. * ti_clk_add_alias - add a clock alias for a TI clock
  393. * @dev: device alias for this clock
  394. * @clk: clock handle to create alias for
  395. * @con: connection ID for this clock
  396. *
  397. * Creates a clock alias for a TI clock. Allocates the clock lookup entry
  398. * and assigns the data to it. Returns 0 if successful, negative error
  399. * value otherwise.
  400. */
  401. int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
  402. {
  403. struct clk_lookup *cl;
  404. if (!clk)
  405. return 0;
  406. if (IS_ERR(clk))
  407. return PTR_ERR(clk);
  408. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  409. if (!cl)
  410. return -ENOMEM;
  411. if (dev)
  412. cl->dev_id = dev_name(dev);
  413. cl->con_id = con;
  414. cl->clk = clk;
  415. clkdev_add(cl);
  416. return 0;
  417. }
  418. /**
  419. * ti_clk_register - register a TI clock to the common clock framework
  420. * @dev: device for this clock
  421. * @hw: hardware clock handle
  422. * @con: connection ID for this clock
  423. *
  424. * Registers a TI clock to the common clock framework, and adds a clock
  425. * alias for it. Returns a handle to the registered clock if successful,
  426. * ERR_PTR value in failure.
  427. */
  428. struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
  429. const char *con)
  430. {
  431. struct clk *clk;
  432. int ret;
  433. clk = clk_register(dev, hw);
  434. if (IS_ERR(clk))
  435. return clk;
  436. ret = ti_clk_add_alias(dev, clk, con);
  437. if (ret) {
  438. clk_unregister(clk);
  439. return ERR_PTR(ret);
  440. }
  441. return clk;
  442. }