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.h>
  20. #include <linux/clk-provider.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #define HB_PLL_LOCK_500 0x20000000
  25. #define HB_PLL_LOCK 0x10000000
  26. #define HB_PLL_DIVF_SHIFT 20
  27. #define HB_PLL_DIVF_MASK 0x0ff00000
  28. #define HB_PLL_DIVQ_SHIFT 16
  29. #define HB_PLL_DIVQ_MASK 0x00070000
  30. #define HB_PLL_DIVR_SHIFT 8
  31. #define HB_PLL_DIVR_MASK 0x00001f00
  32. #define HB_PLL_RANGE_SHIFT 4
  33. #define HB_PLL_RANGE_MASK 0x00000070
  34. #define HB_PLL_BYPASS 0x00000008
  35. #define HB_PLL_RESET 0x00000004
  36. #define HB_PLL_EXT_BYPASS 0x00000002
  37. #define HB_PLL_EXT_ENA 0x00000001
  38. #define HB_PLL_VCO_MIN_FREQ 2133000000
  39. #define HB_PLL_MAX_FREQ HB_PLL_VCO_MIN_FREQ
  40. #define HB_PLL_MIN_FREQ (HB_PLL_VCO_MIN_FREQ / 64)
  41. #define HB_A9_BCLK_DIV_MASK 0x00000006
  42. #define HB_A9_BCLK_DIV_SHIFT 1
  43. #define HB_A9_PCLK_DIV 0x00000001
  44. struct hb_clk {
  45. struct clk_hw hw;
  46. void __iomem *reg;
  47. char *parent_name;
  48. };
  49. #define to_hb_clk(p) container_of(p, struct hb_clk, hw)
  50. static int clk_pll_prepare(struct clk_hw *hwclk)
  51. {
  52. struct hb_clk *hbclk = to_hb_clk(hwclk);
  53. u32 reg;
  54. reg = readl(hbclk->reg);
  55. reg &= ~HB_PLL_RESET;
  56. writel(reg, hbclk->reg);
  57. while ((readl(hbclk->reg) & HB_PLL_LOCK) == 0)
  58. ;
  59. while ((readl(hbclk->reg) & HB_PLL_LOCK_500) == 0)
  60. ;
  61. return 0;
  62. }
  63. static void clk_pll_unprepare(struct clk_hw *hwclk)
  64. {
  65. struct hb_clk *hbclk = to_hb_clk(hwclk);
  66. u32 reg;
  67. reg = readl(hbclk->reg);
  68. reg |= HB_PLL_RESET;
  69. writel(reg, hbclk->reg);
  70. }
  71. static int clk_pll_enable(struct clk_hw *hwclk)
  72. {
  73. struct hb_clk *hbclk = to_hb_clk(hwclk);
  74. u32 reg;
  75. reg = readl(hbclk->reg);
  76. reg |= HB_PLL_EXT_ENA;
  77. writel(reg, hbclk->reg);
  78. return 0;
  79. }
  80. static void clk_pll_disable(struct clk_hw *hwclk)
  81. {
  82. struct hb_clk *hbclk = to_hb_clk(hwclk);
  83. u32 reg;
  84. reg = readl(hbclk->reg);
  85. reg &= ~HB_PLL_EXT_ENA;
  86. writel(reg, hbclk->reg);
  87. }
  88. static unsigned long clk_pll_recalc_rate(struct clk_hw *hwclk,
  89. unsigned long parent_rate)
  90. {
  91. struct hb_clk *hbclk = to_hb_clk(hwclk);
  92. unsigned long divf, divq, vco_freq, reg;
  93. reg = readl(hbclk->reg);
  94. if (reg & HB_PLL_EXT_BYPASS)
  95. return parent_rate;
  96. divf = (reg & HB_PLL_DIVF_MASK) >> HB_PLL_DIVF_SHIFT;
  97. divq = (reg & HB_PLL_DIVQ_MASK) >> HB_PLL_DIVQ_SHIFT;
  98. vco_freq = parent_rate * (divf + 1);
  99. return vco_freq / (1 << divq);
  100. }
  101. static void clk_pll_calc(unsigned long rate, unsigned long ref_freq,
  102. u32 *pdivq, u32 *pdivf)
  103. {
  104. u32 divq, divf;
  105. unsigned long vco_freq;
  106. if (rate < HB_PLL_MIN_FREQ)
  107. rate = HB_PLL_MIN_FREQ;
  108. if (rate > HB_PLL_MAX_FREQ)
  109. rate = HB_PLL_MAX_FREQ;
  110. for (divq = 1; divq <= 6; divq++) {
  111. if ((rate * (1 << divq)) >= HB_PLL_VCO_MIN_FREQ)
  112. break;
  113. }
  114. vco_freq = rate * (1 << divq);
  115. divf = (vco_freq + (ref_freq / 2)) / ref_freq;
  116. divf--;
  117. *pdivq = divq;
  118. *pdivf = divf;
  119. }
  120. static long clk_pll_round_rate(struct clk_hw *hwclk, unsigned long rate,
  121. unsigned long *parent_rate)
  122. {
  123. u32 divq, divf;
  124. unsigned long ref_freq = *parent_rate;
  125. clk_pll_calc(rate, ref_freq, &divq, &divf);
  126. return (ref_freq * (divf + 1)) / (1 << divq);
  127. }
  128. static int clk_pll_set_rate(struct clk_hw *hwclk, unsigned long rate,
  129. unsigned long parent_rate)
  130. {
  131. struct hb_clk *hbclk = to_hb_clk(hwclk);
  132. u32 divq, divf;
  133. u32 reg;
  134. clk_pll_calc(rate, parent_rate, &divq, &divf);
  135. reg = readl(hbclk->reg);
  136. if (divf != ((reg & HB_PLL_DIVF_MASK) >> HB_PLL_DIVF_SHIFT)) {
  137. /* Need to re-lock PLL, so put it into bypass mode */
  138. reg |= HB_PLL_EXT_BYPASS;
  139. writel(reg | HB_PLL_EXT_BYPASS, hbclk->reg);
  140. writel(reg | HB_PLL_RESET, hbclk->reg);
  141. reg &= ~(HB_PLL_DIVF_MASK | HB_PLL_DIVQ_MASK);
  142. reg |= (divf << HB_PLL_DIVF_SHIFT) | (divq << HB_PLL_DIVQ_SHIFT);
  143. writel(reg | HB_PLL_RESET, hbclk->reg);
  144. writel(reg, hbclk->reg);
  145. while ((readl(hbclk->reg) & HB_PLL_LOCK) == 0)
  146. ;
  147. while ((readl(hbclk->reg) & HB_PLL_LOCK_500) == 0)
  148. ;
  149. reg |= HB_PLL_EXT_ENA;
  150. reg &= ~HB_PLL_EXT_BYPASS;
  151. } else {
  152. writel(reg | HB_PLL_EXT_BYPASS, hbclk->reg);
  153. reg &= ~HB_PLL_DIVQ_MASK;
  154. reg |= divq << HB_PLL_DIVQ_SHIFT;
  155. writel(reg | HB_PLL_EXT_BYPASS, hbclk->reg);
  156. }
  157. writel(reg, hbclk->reg);
  158. return 0;
  159. }
  160. static const struct clk_ops clk_pll_ops = {
  161. .prepare = clk_pll_prepare,
  162. .unprepare = clk_pll_unprepare,
  163. .enable = clk_pll_enable,
  164. .disable = clk_pll_disable,
  165. .recalc_rate = clk_pll_recalc_rate,
  166. .round_rate = clk_pll_round_rate,
  167. .set_rate = clk_pll_set_rate,
  168. };
  169. static unsigned long clk_cpu_periphclk_recalc_rate(struct clk_hw *hwclk,
  170. unsigned long parent_rate)
  171. {
  172. struct hb_clk *hbclk = to_hb_clk(hwclk);
  173. u32 div = (readl(hbclk->reg) & HB_A9_PCLK_DIV) ? 8 : 4;
  174. return parent_rate / div;
  175. }
  176. static const struct clk_ops a9periphclk_ops = {
  177. .recalc_rate = clk_cpu_periphclk_recalc_rate,
  178. };
  179. static unsigned long clk_cpu_a9bclk_recalc_rate(struct clk_hw *hwclk,
  180. unsigned long parent_rate)
  181. {
  182. struct hb_clk *hbclk = to_hb_clk(hwclk);
  183. u32 div = (readl(hbclk->reg) & HB_A9_BCLK_DIV_MASK) >> HB_A9_BCLK_DIV_SHIFT;
  184. return parent_rate / (div + 2);
  185. }
  186. static const struct clk_ops a9bclk_ops = {
  187. .recalc_rate = clk_cpu_a9bclk_recalc_rate,
  188. };
  189. static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk,
  190. unsigned long parent_rate)
  191. {
  192. struct hb_clk *hbclk = to_hb_clk(hwclk);
  193. u32 div;
  194. div = readl(hbclk->reg) & 0x1f;
  195. div++;
  196. div *= 2;
  197. return parent_rate / div;
  198. }
  199. static long clk_periclk_round_rate(struct clk_hw *hwclk, unsigned long rate,
  200. unsigned long *parent_rate)
  201. {
  202. u32 div;
  203. div = *parent_rate / rate;
  204. div++;
  205. div &= ~0x1;
  206. return *parent_rate / div;
  207. }
  208. static int clk_periclk_set_rate(struct clk_hw *hwclk, unsigned long rate,
  209. unsigned long parent_rate)
  210. {
  211. struct hb_clk *hbclk = to_hb_clk(hwclk);
  212. u32 div;
  213. div = parent_rate / rate;
  214. if (div & 0x1)
  215. return -EINVAL;
  216. writel(div >> 1, hbclk->reg);
  217. return 0;
  218. }
  219. static const struct clk_ops periclk_ops = {
  220. .recalc_rate = clk_periclk_recalc_rate,
  221. .round_rate = clk_periclk_round_rate,
  222. .set_rate = clk_periclk_set_rate,
  223. };
  224. static __init struct clk *hb_clk_init(struct device_node *node, const struct clk_ops *ops)
  225. {
  226. u32 reg;
  227. struct clk *clk;
  228. struct hb_clk *hb_clk;
  229. const char *clk_name = node->name;
  230. const char *parent_name;
  231. struct clk_init_data init;
  232. struct device_node *srnp;
  233. int rc;
  234. rc = of_property_read_u32(node, "reg", &reg);
  235. if (WARN_ON(rc))
  236. return NULL;
  237. hb_clk = kzalloc(sizeof(*hb_clk), GFP_KERNEL);
  238. if (WARN_ON(!hb_clk))
  239. return NULL;
  240. /* Map system registers */
  241. srnp = of_find_compatible_node(NULL, NULL, "calxeda,hb-sregs");
  242. hb_clk->reg = of_iomap(srnp, 0);
  243. BUG_ON(!hb_clk->reg);
  244. hb_clk->reg += reg;
  245. of_property_read_string(node, "clock-output-names", &clk_name);
  246. init.name = clk_name;
  247. init.ops = ops;
  248. init.flags = 0;
  249. parent_name = of_clk_get_parent_name(node, 0);
  250. init.parent_names = &parent_name;
  251. init.num_parents = 1;
  252. hb_clk->hw.init = &init;
  253. clk = clk_register(NULL, &hb_clk->hw);
  254. if (WARN_ON(IS_ERR(clk))) {
  255. kfree(hb_clk);
  256. return NULL;
  257. }
  258. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  259. return clk;
  260. }
  261. static void __init hb_pll_init(struct device_node *node)
  262. {
  263. hb_clk_init(node, &clk_pll_ops);
  264. }
  265. CLK_OF_DECLARE(hb_pll, "calxeda,hb-pll-clock", hb_pll_init);
  266. static void __init hb_a9periph_init(struct device_node *node)
  267. {
  268. hb_clk_init(node, &a9periphclk_ops);
  269. }
  270. CLK_OF_DECLARE(hb_a9periph, "calxeda,hb-a9periph-clock", hb_a9periph_init);
  271. static void __init hb_a9bus_init(struct device_node *node)
  272. {
  273. struct clk *clk = hb_clk_init(node, &a9bclk_ops);
  274. clk_prepare_enable(clk);
  275. }
  276. CLK_OF_DECLARE(hb_a9bus, "calxeda,hb-a9bus-clock", hb_a9bus_init);
  277. static void __init hb_emmc_init(struct device_node *node)
  278. {
  279. hb_clk_init(node, &periclk_ops);
  280. }
  281. CLK_OF_DECLARE(hb_emmc, "calxeda,hb-emmc-clock", hb_emmc_init);