clk-highbank.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #include <linux/of_address.h>
  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. writel(reg | HB_PLL_EXT_BYPASS, hbclk->reg);
  152. reg &= ~HB_PLL_DIVQ_MASK;
  153. reg |= divq << HB_PLL_DIVQ_SHIFT;
  154. writel(reg | HB_PLL_EXT_BYPASS, hbclk->reg);
  155. }
  156. writel(reg, hbclk->reg);
  157. return 0;
  158. }
  159. static const struct clk_ops clk_pll_ops = {
  160. .prepare = clk_pll_prepare,
  161. .unprepare = clk_pll_unprepare,
  162. .enable = clk_pll_enable,
  163. .disable = clk_pll_disable,
  164. .recalc_rate = clk_pll_recalc_rate,
  165. .round_rate = clk_pll_round_rate,
  166. .set_rate = clk_pll_set_rate,
  167. };
  168. static unsigned long clk_cpu_periphclk_recalc_rate(struct clk_hw *hwclk,
  169. unsigned long parent_rate)
  170. {
  171. struct hb_clk *hbclk = to_hb_clk(hwclk);
  172. u32 div = (readl(hbclk->reg) & HB_A9_PCLK_DIV) ? 8 : 4;
  173. return parent_rate / div;
  174. }
  175. static const struct clk_ops a9periphclk_ops = {
  176. .recalc_rate = clk_cpu_periphclk_recalc_rate,
  177. };
  178. static unsigned long clk_cpu_a9bclk_recalc_rate(struct clk_hw *hwclk,
  179. unsigned long parent_rate)
  180. {
  181. struct hb_clk *hbclk = to_hb_clk(hwclk);
  182. u32 div = (readl(hbclk->reg) & HB_A9_BCLK_DIV_MASK) >> HB_A9_BCLK_DIV_SHIFT;
  183. return parent_rate / (div + 2);
  184. }
  185. static const struct clk_ops a9bclk_ops = {
  186. .recalc_rate = clk_cpu_a9bclk_recalc_rate,
  187. };
  188. static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk,
  189. unsigned long parent_rate)
  190. {
  191. struct hb_clk *hbclk = to_hb_clk(hwclk);
  192. u32 div;
  193. div = readl(hbclk->reg) & 0x1f;
  194. div++;
  195. div *= 2;
  196. return parent_rate / div;
  197. }
  198. static long clk_periclk_round_rate(struct clk_hw *hwclk, unsigned long rate,
  199. unsigned long *parent_rate)
  200. {
  201. u32 div;
  202. div = *parent_rate / rate;
  203. div++;
  204. div &= ~0x1;
  205. return *parent_rate / div;
  206. }
  207. static int clk_periclk_set_rate(struct clk_hw *hwclk, unsigned long rate,
  208. unsigned long parent_rate)
  209. {
  210. struct hb_clk *hbclk = to_hb_clk(hwclk);
  211. u32 div;
  212. div = parent_rate / rate;
  213. if (div & 0x1)
  214. return -EINVAL;
  215. writel(div >> 1, hbclk->reg);
  216. return 0;
  217. }
  218. static const struct clk_ops periclk_ops = {
  219. .recalc_rate = clk_periclk_recalc_rate,
  220. .round_rate = clk_periclk_round_rate,
  221. .set_rate = clk_periclk_set_rate,
  222. };
  223. static __init struct clk *hb_clk_init(struct device_node *node, const struct clk_ops *ops)
  224. {
  225. u32 reg;
  226. struct clk *clk;
  227. struct hb_clk *hb_clk;
  228. const char *clk_name = node->name;
  229. const char *parent_name;
  230. struct clk_init_data init;
  231. struct device_node *srnp;
  232. int rc;
  233. rc = of_property_read_u32(node, "reg", &reg);
  234. if (WARN_ON(rc))
  235. return NULL;
  236. hb_clk = kzalloc(sizeof(*hb_clk), GFP_KERNEL);
  237. if (WARN_ON(!hb_clk))
  238. return NULL;
  239. /* Map system registers */
  240. srnp = of_find_compatible_node(NULL, NULL, "calxeda,hb-sregs");
  241. hb_clk->reg = of_iomap(srnp, 0);
  242. BUG_ON(!hb_clk->reg);
  243. hb_clk->reg += reg;
  244. of_property_read_string(node, "clock-output-names", &clk_name);
  245. init.name = clk_name;
  246. init.ops = ops;
  247. init.flags = 0;
  248. parent_name = of_clk_get_parent_name(node, 0);
  249. init.parent_names = &parent_name;
  250. init.num_parents = 1;
  251. hb_clk->hw.init = &init;
  252. clk = clk_register(NULL, &hb_clk->hw);
  253. if (WARN_ON(IS_ERR(clk))) {
  254. kfree(hb_clk);
  255. return NULL;
  256. }
  257. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  258. return clk;
  259. }
  260. static void __init hb_pll_init(struct device_node *node)
  261. {
  262. hb_clk_init(node, &clk_pll_ops);
  263. }
  264. CLK_OF_DECLARE(hb_pll, "calxeda,hb-pll-clock", hb_pll_init);
  265. static void __init hb_a9periph_init(struct device_node *node)
  266. {
  267. hb_clk_init(node, &a9periphclk_ops);
  268. }
  269. CLK_OF_DECLARE(hb_a9periph, "calxeda,hb-a9periph-clock", hb_a9periph_init);
  270. static void __init hb_a9bus_init(struct device_node *node)
  271. {
  272. struct clk *clk = hb_clk_init(node, &a9bclk_ops);
  273. clk_prepare_enable(clk);
  274. }
  275. CLK_OF_DECLARE(hb_a9bus, "calxeda,hb-a9bus-clock", hb_a9bus_init);
  276. static void __init hb_emmc_init(struct device_node *node)
  277. {
  278. hb_clk_init(node, &periclk_ops);
  279. }
  280. CLK_OF_DECLARE(hb_emmc, "calxeda,hb-emmc-clock", hb_emmc_init);