clk-programmable.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/io.h>
  16. #include <linux/wait.h>
  17. #include <linux/sched.h>
  18. #include "pmc.h"
  19. #define PROG_SOURCE_MAX 5
  20. #define PROG_ID_MAX 7
  21. #define PROG_STATUS_MASK(id) (1 << ((id) + 8))
  22. #define PROG_PRES_MASK 0x7
  23. #define PROG_MAX_RM9200_CSS 3
  24. struct clk_programmable_layout {
  25. u8 pres_shift;
  26. u8 css_mask;
  27. u8 have_slck_mck;
  28. };
  29. struct clk_programmable {
  30. struct clk_hw hw;
  31. struct at91_pmc *pmc;
  32. u8 id;
  33. const struct clk_programmable_layout *layout;
  34. };
  35. #define to_clk_programmable(hw) container_of(hw, struct clk_programmable, hw)
  36. static unsigned long clk_programmable_recalc_rate(struct clk_hw *hw,
  37. unsigned long parent_rate)
  38. {
  39. u32 pres;
  40. struct clk_programmable *prog = to_clk_programmable(hw);
  41. struct at91_pmc *pmc = prog->pmc;
  42. const struct clk_programmable_layout *layout = prog->layout;
  43. pres = (pmc_read(pmc, AT91_PMC_PCKR(prog->id)) >> layout->pres_shift) &
  44. PROG_PRES_MASK;
  45. return parent_rate >> pres;
  46. }
  47. static long clk_programmable_determine_rate(struct clk_hw *hw,
  48. unsigned long rate,
  49. unsigned long *best_parent_rate,
  50. struct clk **best_parent_clk)
  51. {
  52. struct clk *parent = NULL;
  53. long best_rate = -EINVAL;
  54. unsigned long parent_rate;
  55. unsigned long tmp_rate;
  56. int shift;
  57. int i;
  58. for (i = 0; i < __clk_get_num_parents(hw->clk); i++) {
  59. parent = clk_get_parent_by_index(hw->clk, i);
  60. if (!parent)
  61. continue;
  62. parent_rate = __clk_get_rate(parent);
  63. for (shift = 0; shift < PROG_PRES_MASK; shift++) {
  64. tmp_rate = parent_rate >> shift;
  65. if (tmp_rate <= rate)
  66. break;
  67. }
  68. if (tmp_rate > rate)
  69. continue;
  70. if (best_rate < 0 || (rate - tmp_rate) < (rate - best_rate)) {
  71. best_rate = tmp_rate;
  72. *best_parent_rate = parent_rate;
  73. *best_parent_clk = parent;
  74. }
  75. if (!best_rate)
  76. break;
  77. }
  78. return best_rate;
  79. }
  80. static int clk_programmable_set_parent(struct clk_hw *hw, u8 index)
  81. {
  82. struct clk_programmable *prog = to_clk_programmable(hw);
  83. const struct clk_programmable_layout *layout = prog->layout;
  84. struct at91_pmc *pmc = prog->pmc;
  85. u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) & ~layout->css_mask;
  86. if (layout->have_slck_mck)
  87. tmp &= AT91_PMC_CSSMCK_MCK;
  88. if (index > layout->css_mask) {
  89. if (index > PROG_MAX_RM9200_CSS && layout->have_slck_mck) {
  90. tmp |= AT91_PMC_CSSMCK_MCK;
  91. return 0;
  92. } else {
  93. return -EINVAL;
  94. }
  95. }
  96. pmc_write(pmc, AT91_PMC_PCKR(prog->id), tmp | index);
  97. return 0;
  98. }
  99. static u8 clk_programmable_get_parent(struct clk_hw *hw)
  100. {
  101. u32 tmp;
  102. u8 ret;
  103. struct clk_programmable *prog = to_clk_programmable(hw);
  104. struct at91_pmc *pmc = prog->pmc;
  105. const struct clk_programmable_layout *layout = prog->layout;
  106. tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id));
  107. ret = tmp & layout->css_mask;
  108. if (layout->have_slck_mck && (tmp & AT91_PMC_CSSMCK_MCK) && !ret)
  109. ret = PROG_MAX_RM9200_CSS + 1;
  110. return ret;
  111. }
  112. static int clk_programmable_set_rate(struct clk_hw *hw, unsigned long rate,
  113. unsigned long parent_rate)
  114. {
  115. struct clk_programmable *prog = to_clk_programmable(hw);
  116. struct at91_pmc *pmc = prog->pmc;
  117. const struct clk_programmable_layout *layout = prog->layout;
  118. unsigned long div = parent_rate / rate;
  119. int shift = 0;
  120. u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) &
  121. ~(PROG_PRES_MASK << layout->pres_shift);
  122. if (!div)
  123. return -EINVAL;
  124. shift = fls(div) - 1;
  125. if (div != (1<<shift))
  126. return -EINVAL;
  127. if (shift >= PROG_PRES_MASK)
  128. return -EINVAL;
  129. pmc_write(pmc, AT91_PMC_PCKR(prog->id),
  130. tmp | (shift << layout->pres_shift));
  131. return 0;
  132. }
  133. static const struct clk_ops programmable_ops = {
  134. .recalc_rate = clk_programmable_recalc_rate,
  135. .determine_rate = clk_programmable_determine_rate,
  136. .get_parent = clk_programmable_get_parent,
  137. .set_parent = clk_programmable_set_parent,
  138. .set_rate = clk_programmable_set_rate,
  139. };
  140. static struct clk * __init
  141. at91_clk_register_programmable(struct at91_pmc *pmc,
  142. const char *name, const char **parent_names,
  143. u8 num_parents, u8 id,
  144. const struct clk_programmable_layout *layout)
  145. {
  146. struct clk_programmable *prog;
  147. struct clk *clk = NULL;
  148. struct clk_init_data init;
  149. if (id > PROG_ID_MAX)
  150. return ERR_PTR(-EINVAL);
  151. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  152. if (!prog)
  153. return ERR_PTR(-ENOMEM);
  154. init.name = name;
  155. init.ops = &programmable_ops;
  156. init.parent_names = parent_names;
  157. init.num_parents = num_parents;
  158. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  159. prog->id = id;
  160. prog->layout = layout;
  161. prog->hw.init = &init;
  162. prog->pmc = pmc;
  163. clk = clk_register(NULL, &prog->hw);
  164. if (IS_ERR(clk))
  165. kfree(prog);
  166. return clk;
  167. }
  168. static const struct clk_programmable_layout at91rm9200_programmable_layout = {
  169. .pres_shift = 2,
  170. .css_mask = 0x3,
  171. .have_slck_mck = 0,
  172. };
  173. static const struct clk_programmable_layout at91sam9g45_programmable_layout = {
  174. .pres_shift = 2,
  175. .css_mask = 0x3,
  176. .have_slck_mck = 1,
  177. };
  178. static const struct clk_programmable_layout at91sam9x5_programmable_layout = {
  179. .pres_shift = 4,
  180. .css_mask = 0x7,
  181. .have_slck_mck = 0,
  182. };
  183. static void __init
  184. of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  185. const struct clk_programmable_layout *layout)
  186. {
  187. int num;
  188. u32 id;
  189. int i;
  190. struct clk *clk;
  191. int num_parents;
  192. const char *parent_names[PROG_SOURCE_MAX];
  193. const char *name;
  194. struct device_node *progclknp;
  195. num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  196. if (num_parents <= 0 || num_parents > PROG_SOURCE_MAX)
  197. return;
  198. for (i = 0; i < num_parents; ++i) {
  199. parent_names[i] = of_clk_get_parent_name(np, i);
  200. if (!parent_names[i])
  201. return;
  202. }
  203. num = of_get_child_count(np);
  204. if (!num || num > (PROG_ID_MAX + 1))
  205. return;
  206. for_each_child_of_node(np, progclknp) {
  207. if (of_property_read_u32(progclknp, "reg", &id))
  208. continue;
  209. if (of_property_read_string(np, "clock-output-names", &name))
  210. name = progclknp->name;
  211. clk = at91_clk_register_programmable(pmc, name,
  212. parent_names, num_parents,
  213. id, layout);
  214. if (IS_ERR(clk))
  215. continue;
  216. of_clk_add_provider(progclknp, of_clk_src_simple_get, clk);
  217. }
  218. }
  219. void __init of_at91rm9200_clk_prog_setup(struct device_node *np,
  220. struct at91_pmc *pmc)
  221. {
  222. of_at91_clk_prog_setup(np, pmc, &at91rm9200_programmable_layout);
  223. }
  224. void __init of_at91sam9g45_clk_prog_setup(struct device_node *np,
  225. struct at91_pmc *pmc)
  226. {
  227. of_at91_clk_prog_setup(np, pmc, &at91sam9g45_programmable_layout);
  228. }
  229. void __init of_at91sam9x5_clk_prog_setup(struct device_node *np,
  230. struct at91_pmc *pmc)
  231. {
  232. of_at91_clk_prog_setup(np, pmc, &at91sam9x5_programmable_layout);
  233. }