clk-highbank.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright 2011-2012 Calxeda, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/io.h>
  21. #include <linux/of.h>
  22. extern void __iomem *sregs_base;
  23. #define HB_PLL_LOCK_500 0x20000000
  24. #define HB_PLL_LOCK 0x10000000
  25. #define HB_PLL_DIVF_SHIFT 20
  26. #define HB_PLL_DIVF_MASK 0x0ff00000
  27. #define HB_PLL_DIVQ_SHIFT 16
  28. #define HB_PLL_DIVQ_MASK 0x00070000
  29. #define HB_PLL_DIVR_SHIFT 8
  30. #define HB_PLL_DIVR_MASK 0x00001f00
  31. #define HB_PLL_RANGE_SHIFT 4
  32. #define HB_PLL_RANGE_MASK 0x00000070
  33. #define HB_PLL_BYPASS 0x00000008
  34. #define HB_PLL_RESET 0x00000004
  35. #define HB_PLL_EXT_BYPASS 0x00000002
  36. #define HB_PLL_EXT_ENA 0x00000001
  37. #define HB_PLL_VCO_MIN_FREQ 2133000000
  38. #define HB_PLL_MAX_FREQ HB_PLL_VCO_MIN_FREQ
  39. #define HB_PLL_MIN_FREQ (HB_PLL_VCO_MIN_FREQ / 64)
  40. #define HB_A9_BCLK_DIV_MASK 0x00000006
  41. #define HB_A9_BCLK_DIV_SHIFT 1
  42. #define HB_A9_PCLK_DIV 0x00000001
  43. struct hb_clk {
  44. struct clk_hw hw;
  45. void __iomem *reg;
  46. char *parent_name;
  47. };
  48. #define to_hb_clk(p) container_of(p, struct hb_clk, hw)
  49. static int clk_pll_prepare(struct clk_hw *hwclk)
  50. {
  51. struct hb_clk *hbclk = to_hb_clk(hwclk);
  52. u32 reg;
  53. reg = readl(hbclk->reg);
  54. reg &= ~HB_PLL_RESET;
  55. writel(reg, hbclk->reg);
  56. while ((readl(hbclk->reg) & HB_PLL_LOCK) == 0)
  57. ;
  58. while ((readl(hbclk->reg) & HB_PLL_LOCK_500) == 0)
  59. ;
  60. return 0;
  61. }
  62. static void clk_pll_unprepare(struct clk_hw *hwclk)
  63. {
  64. struct hb_clk *hbclk = to_hb_clk(hwclk);
  65. u32 reg;
  66. reg = readl(hbclk->reg);
  67. reg |= HB_PLL_RESET;
  68. writel(reg, hbclk->reg);
  69. }
  70. static int clk_pll_enable(struct clk_hw *hwclk)
  71. {
  72. struct hb_clk *hbclk = to_hb_clk(hwclk);
  73. u32 reg;
  74. reg = readl(hbclk->reg);
  75. reg |= HB_PLL_EXT_ENA;
  76. writel(reg, hbclk->reg);
  77. return 0;
  78. }
  79. static void clk_pll_disable(struct clk_hw *hwclk)
  80. {
  81. struct hb_clk *hbclk = to_hb_clk(hwclk);
  82. u32 reg;
  83. reg = readl(hbclk->reg);
  84. reg &= ~HB_PLL_EXT_ENA;
  85. writel(reg, hbclk->reg);
  86. }
  87. static unsigned long clk_pll_recalc_rate(struct clk_hw *hwclk,
  88. unsigned long parent_rate)
  89. {
  90. struct hb_clk *hbclk = to_hb_clk(hwclk);
  91. unsigned long divf, divq, vco_freq, reg;
  92. reg = readl(hbclk->reg);
  93. if (reg & HB_PLL_EXT_BYPASS)
  94. return parent_rate;
  95. divf = (reg & HB_PLL_DIVF_MASK) >> HB_PLL_DIVF_SHIFT;
  96. divq = (reg & HB_PLL_DIVQ_MASK) >> HB_PLL_DIVQ_SHIFT;
  97. vco_freq = parent_rate * (divf + 1);
  98. return vco_freq / (1 << divq);
  99. }
  100. static void clk_pll_calc(unsigned long rate, unsigned long ref_freq,
  101. u32 *pdivq, u32 *pdivf)
  102. {
  103. u32 divq, divf;
  104. unsigned long vco_freq;
  105. if (rate < HB_PLL_MIN_FREQ)
  106. rate = HB_PLL_MIN_FREQ;
  107. if (rate > HB_PLL_MAX_FREQ)
  108. rate = HB_PLL_MAX_FREQ;
  109. for (divq = 1; divq <= 6; divq++) {
  110. if ((rate * (1 << divq)) >= HB_PLL_VCO_MIN_FREQ)
  111. break;
  112. }
  113. vco_freq = rate * (1 << divq);
  114. divf = (vco_freq + (ref_freq / 2)) / ref_freq;
  115. divf--;
  116. *pdivq = divq;
  117. *pdivf = divf;
  118. }
  119. static long clk_pll_round_rate(struct clk_hw *hwclk, unsigned long rate,
  120. unsigned long *parent_rate)
  121. {
  122. u32 divq, divf;
  123. unsigned long ref_freq = *parent_rate;
  124. clk_pll_calc(rate, ref_freq, &divq, &divf);
  125. return (ref_freq * (divf + 1)) / (1 << divq);
  126. }
  127. static int clk_pll_set_rate(struct clk_hw *hwclk, unsigned long rate,
  128. unsigned long parent_rate)
  129. {
  130. struct hb_clk *hbclk = to_hb_clk(hwclk);
  131. u32 divq, divf;
  132. u32 reg;
  133. clk_pll_calc(rate, parent_rate, &divq, &divf);
  134. reg = readl(hbclk->reg);
  135. if (divf != ((reg & HB_PLL_DIVF_MASK) >> HB_PLL_DIVF_SHIFT)) {
  136. /* Need to re-lock PLL, so put it into bypass mode */
  137. reg |= HB_PLL_EXT_BYPASS;
  138. writel(reg | HB_PLL_EXT_BYPASS, hbclk->reg);
  139. writel(reg | HB_PLL_RESET, hbclk->reg);
  140. reg &= ~(HB_PLL_DIVF_MASK | HB_PLL_DIVQ_MASK);
  141. reg |= (divf << HB_PLL_DIVF_SHIFT) | (divq << HB_PLL_DIVQ_SHIFT);
  142. writel(reg | HB_PLL_RESET, hbclk->reg);
  143. writel(reg, hbclk->reg);
  144. while ((readl(hbclk->reg) & HB_PLL_LOCK) == 0)
  145. ;
  146. while ((readl(hbclk->reg) & HB_PLL_LOCK_500) == 0)
  147. ;
  148. reg |= HB_PLL_EXT_ENA;
  149. reg &= ~HB_PLL_EXT_BYPASS;
  150. } else {
  151. reg &= ~HB_PLL_DIVQ_MASK;
  152. reg |= divq << HB_PLL_DIVQ_SHIFT;
  153. }
  154. writel(reg, hbclk->reg);
  155. return 0;
  156. }
  157. static const struct clk_ops clk_pll_ops = {
  158. .prepare = clk_pll_prepare,
  159. .unprepare = clk_pll_unprepare,
  160. .enable = clk_pll_enable,
  161. .disable = clk_pll_disable,
  162. .recalc_rate = clk_pll_recalc_rate,
  163. .round_rate = clk_pll_round_rate,
  164. .set_rate = clk_pll_set_rate,
  165. };
  166. static unsigned long clk_cpu_periphclk_recalc_rate(struct clk_hw *hwclk,
  167. unsigned long parent_rate)
  168. {
  169. struct hb_clk *hbclk = to_hb_clk(hwclk);
  170. u32 div = (readl(hbclk->reg) & HB_A9_PCLK_DIV) ? 8 : 4;
  171. return parent_rate / div;
  172. }
  173. static const struct clk_ops a9periphclk_ops = {
  174. .recalc_rate = clk_cpu_periphclk_recalc_rate,
  175. };
  176. static unsigned long clk_cpu_a9bclk_recalc_rate(struct clk_hw *hwclk,
  177. unsigned long parent_rate)
  178. {
  179. struct hb_clk *hbclk = to_hb_clk(hwclk);
  180. u32 div = (readl(hbclk->reg) & HB_A9_BCLK_DIV_MASK) >> HB_A9_BCLK_DIV_SHIFT;
  181. return parent_rate / (div + 2);
  182. }
  183. static const struct clk_ops a9bclk_ops = {
  184. .recalc_rate = clk_cpu_a9bclk_recalc_rate,
  185. };
  186. static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk,
  187. unsigned long parent_rate)
  188. {
  189. struct hb_clk *hbclk = to_hb_clk(hwclk);
  190. u32 div;
  191. div = readl(hbclk->reg) & 0x1f;
  192. div++;
  193. div *= 2;
  194. return parent_rate / div;
  195. }
  196. static long clk_periclk_round_rate(struct clk_hw *hwclk, unsigned long rate,
  197. unsigned long *parent_rate)
  198. {
  199. u32 div;
  200. div = *parent_rate / rate;
  201. div++;
  202. div &= ~0x1;
  203. return *parent_rate / div;
  204. }
  205. static int clk_periclk_set_rate(struct clk_hw *hwclk, unsigned long rate,
  206. unsigned long parent_rate)
  207. {
  208. struct hb_clk *hbclk = to_hb_clk(hwclk);
  209. u32 div;
  210. div = parent_rate / rate;
  211. if (div & 0x1)
  212. return -EINVAL;
  213. writel(div >> 1, hbclk->reg);
  214. return 0;
  215. }
  216. static const struct clk_ops periclk_ops = {
  217. .recalc_rate = clk_periclk_recalc_rate,
  218. .round_rate = clk_periclk_round_rate,
  219. .set_rate = clk_periclk_set_rate,
  220. };
  221. static __init struct clk *hb_clk_init(struct device_node *node, const struct clk_ops *ops)
  222. {
  223. u32 reg;
  224. struct clk *clk;
  225. struct hb_clk *hb_clk;
  226. const char *clk_name = node->name;
  227. const char *parent_name;
  228. struct clk_init_data init;
  229. int rc;
  230. rc = of_property_read_u32(node, "reg", &reg);
  231. if (WARN_ON(rc))
  232. return NULL;
  233. hb_clk = kzalloc(sizeof(*hb_clk), GFP_KERNEL);
  234. if (WARN_ON(!hb_clk))
  235. return NULL;
  236. hb_clk->reg = sregs_base + reg;
  237. of_property_read_string(node, "clock-output-names", &clk_name);
  238. init.name = clk_name;
  239. init.ops = ops;
  240. init.flags = 0;
  241. parent_name = of_clk_get_parent_name(node, 0);
  242. init.parent_names = &parent_name;
  243. init.num_parents = 1;
  244. hb_clk->hw.init = &init;
  245. clk = clk_register(NULL, &hb_clk->hw);
  246. if (WARN_ON(IS_ERR(clk))) {
  247. kfree(hb_clk);
  248. return NULL;
  249. }
  250. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  251. return clk;
  252. }
  253. static void __init hb_pll_init(struct device_node *node)
  254. {
  255. hb_clk_init(node, &clk_pll_ops);
  256. }
  257. static void __init hb_a9periph_init(struct device_node *node)
  258. {
  259. hb_clk_init(node, &a9periphclk_ops);
  260. }
  261. static void __init hb_a9bus_init(struct device_node *node)
  262. {
  263. struct clk *clk = hb_clk_init(node, &a9bclk_ops);
  264. clk_prepare_enable(clk);
  265. }
  266. static void __init hb_emmc_init(struct device_node *node)
  267. {
  268. hb_clk_init(node, &periclk_ops);
  269. }
  270. static const __initconst struct of_device_id clk_match[] = {
  271. { .compatible = "fixed-clock", .data = of_fixed_clk_setup, },
  272. { .compatible = "calxeda,hb-pll-clock", .data = hb_pll_init, },
  273. { .compatible = "calxeda,hb-a9periph-clock", .data = hb_a9periph_init, },
  274. { .compatible = "calxeda,hb-a9bus-clock", .data = hb_a9bus_init, },
  275. { .compatible = "calxeda,hb-emmc-clock", .data = hb_emmc_init, },
  276. {}
  277. };
  278. void __init highbank_clocks_init(void)
  279. {
  280. of_clk_init(clk_match);
  281. }