clk.c 12 KB

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