clk.c 12 KB

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