clock-r8a73a4.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * r8a73a4 clock framework support
  3. *
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. * Copyright (C) 2013 Magnus Damm
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sh_clk.h>
  24. #include <linux/clkdev.h>
  25. #include <mach/clock.h>
  26. #include <mach/common.h>
  27. #define CPG_BASE 0xe6150000
  28. #define CPG_LEN 0x270
  29. #define MPCKCR 0xe6150080
  30. #define SMSTPCR2 0xe6150138
  31. #define SMSTPCR5 0xe6150144
  32. #define FRQCRA 0xE6150000
  33. #define FRQCRB 0xE6150004
  34. #define CKSCR 0xE61500C0
  35. #define PLLECR 0xE61500D0
  36. #define PLL1CR 0xE6150028
  37. #define PLL2CR 0xE615002C
  38. #define PLL2SCR 0xE61501F4
  39. #define PLL2HCR 0xE61501E4
  40. #define CPG_MAP(o) ((o - CPG_BASE) + cpg_mapping.base)
  41. static struct clk_mapping cpg_mapping = {
  42. .phys = CPG_BASE,
  43. .len = CPG_LEN,
  44. };
  45. static struct clk extalr_clk = {
  46. .rate = 32768,
  47. .mapping = &cpg_mapping,
  48. };
  49. static struct clk extal1_clk = {
  50. .rate = 26000000,
  51. .mapping = &cpg_mapping,
  52. };
  53. static struct clk extal2_clk = {
  54. .rate = 48000000,
  55. .mapping = &cpg_mapping,
  56. };
  57. static struct sh_clk_ops followparent_clk_ops = {
  58. .recalc = followparent_recalc,
  59. };
  60. static struct clk main_clk = {
  61. /* .parent will be set r8a73a4_clock_init */
  62. .ops = &followparent_clk_ops,
  63. };
  64. SH_CLK_RATIO(div2, 1, 2);
  65. SH_CLK_RATIO(div4, 1, 4);
  66. SH_FIXED_RATIO_CLK(main_div2_clk, main_clk, div2);
  67. SH_FIXED_RATIO_CLK(extal1_div2_clk, extal1_clk, div2);
  68. SH_FIXED_RATIO_CLK(extal2_div2_clk, extal2_clk, div2);
  69. SH_FIXED_RATIO_CLK(extal2_div4_clk, extal2_clk, div4);
  70. /*
  71. * PLL clocks
  72. */
  73. static struct clk *pll_parent_main[] = {
  74. [0] = &main_clk,
  75. [1] = &main_div2_clk
  76. };
  77. static struct clk *pll_parent_main_extal[8] = {
  78. [0] = &main_div2_clk,
  79. [1] = &extal2_div2_clk,
  80. [3] = &extal2_div4_clk,
  81. [4] = &main_clk,
  82. [5] = &extal2_clk,
  83. };
  84. static unsigned long pll_recalc(struct clk *clk)
  85. {
  86. unsigned long mult = 1;
  87. if (ioread32(CPG_MAP(PLLECR)) & (1 << clk->enable_bit))
  88. mult = (((ioread32(clk->mapped_reg) >> 24) & 0x7f) + 1);
  89. return clk->parent->rate * mult;
  90. }
  91. static int pll_set_parent(struct clk *clk, struct clk *parent)
  92. {
  93. u32 val;
  94. int i, ret;
  95. if (!clk->parent_table || !clk->parent_num)
  96. return -EINVAL;
  97. /* Search the parent */
  98. for (i = 0; i < clk->parent_num; i++)
  99. if (clk->parent_table[i] == parent)
  100. break;
  101. if (i == clk->parent_num)
  102. return -ENODEV;
  103. ret = clk_reparent(clk, parent);
  104. if (ret < 0)
  105. return ret;
  106. val = ioread32(clk->mapped_reg) &
  107. ~(((1 << clk->src_width) - 1) << clk->src_shift);
  108. iowrite32(val | i << clk->src_shift, clk->mapped_reg);
  109. return 0;
  110. }
  111. static struct sh_clk_ops pll_clk_ops = {
  112. .recalc = pll_recalc,
  113. .set_parent = pll_set_parent,
  114. };
  115. #define PLL_CLOCK(name, p, pt, w, s, reg, e) \
  116. static struct clk name = { \
  117. .ops = &pll_clk_ops, \
  118. .flags = CLK_ENABLE_ON_INIT, \
  119. .parent = p, \
  120. .parent_table = pt, \
  121. .parent_num = ARRAY_SIZE(pt), \
  122. .src_width = w, \
  123. .src_shift = s, \
  124. .enable_reg = (void __iomem *)reg, \
  125. .enable_bit = e, \
  126. .mapping = &cpg_mapping, \
  127. }
  128. PLL_CLOCK(pll1_clk, &main_clk, pll_parent_main, 1, 7, PLL1CR, 1);
  129. PLL_CLOCK(pll2_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2CR, 2);
  130. PLL_CLOCK(pll2s_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2SCR, 4);
  131. PLL_CLOCK(pll2h_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2HCR, 5);
  132. SH_FIXED_RATIO_CLK(pll1_div2_clk, pll1_clk, div2);
  133. static struct clk *main_clks[] = {
  134. &extalr_clk,
  135. &extal1_clk,
  136. &extal1_div2_clk,
  137. &extal2_clk,
  138. &extal2_div2_clk,
  139. &extal2_div4_clk,
  140. &main_clk,
  141. &main_div2_clk,
  142. &pll1_clk,
  143. &pll1_div2_clk,
  144. &pll2_clk,
  145. &pll2s_clk,
  146. &pll2h_clk,
  147. };
  148. /* DIV4 */
  149. static void div4_kick(struct clk *clk)
  150. {
  151. unsigned long value;
  152. /* set KICK bit in FRQCRB to update hardware setting */
  153. value = ioread32(CPG_MAP(FRQCRB));
  154. value |= (1 << 31);
  155. iowrite32(value, CPG_MAP(FRQCRB));
  156. }
  157. static int divisors[] = { 2, 3, 4, 6, 8, 12, 16, 18, 24, 0, 36, 48, 10};
  158. static struct clk_div_mult_table div4_div_mult_table = {
  159. .divisors = divisors,
  160. .nr_divisors = ARRAY_SIZE(divisors),
  161. };
  162. static struct clk_div4_table div4_table = {
  163. .div_mult_table = &div4_div_mult_table,
  164. .kick = div4_kick,
  165. };
  166. enum {
  167. DIV4_I, DIV4_M3, DIV4_B, DIV4_M1, DIV4_M2,
  168. DIV4_ZX, DIV4_ZS, DIV4_HP,
  169. DIV4_NR };
  170. static struct clk div4_clks[DIV4_NR] = {
  171. [DIV4_I] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 20, 0x0dff, CLK_ENABLE_ON_INIT),
  172. [DIV4_M3] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 12, 0x1dff, CLK_ENABLE_ON_INIT),
  173. [DIV4_B] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 8, 0x0dff, CLK_ENABLE_ON_INIT),
  174. [DIV4_M1] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 4, 0x1dff, 0),
  175. [DIV4_M2] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 0, 0x1dff, 0),
  176. [DIV4_ZX] = SH_CLK_DIV4(&pll1_clk, FRQCRB, 12, 0x0dff, 0),
  177. [DIV4_ZS] = SH_CLK_DIV4(&pll1_clk, FRQCRB, 8, 0x0dff, 0),
  178. [DIV4_HP] = SH_CLK_DIV4(&pll1_clk, FRQCRB, 4, 0x0dff, 0),
  179. };
  180. /* MSTP */
  181. enum {
  182. MSTP217, MSTP216, MSTP207, MSTP206, MSTP204, MSTP203,
  183. MSTP522,
  184. MSTP_NR
  185. };
  186. static struct clk mstp_clks[MSTP_NR] = {
  187. [MSTP204] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 4, 0), /* SCIFA0 */
  188. [MSTP203] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 3, 0), /* SCIFA1 */
  189. [MSTP206] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 6, 0), /* SCIFB0 */
  190. [MSTP207] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 7, 0), /* SCIFB1 */
  191. [MSTP216] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 16, 0), /* SCIFB2 */
  192. [MSTP217] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 17, 0), /* SCIFB3 */
  193. [MSTP522] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR5, 22, 0), /* Thermal */
  194. };
  195. static struct clk_lookup lookups[] = {
  196. /* main clock */
  197. CLKDEV_CON_ID("extal1", &extal1_clk),
  198. CLKDEV_CON_ID("extal1_div2", &extal1_div2_clk),
  199. CLKDEV_CON_ID("extal2", &extal2_clk),
  200. CLKDEV_CON_ID("extal2_div2", &extal2_div2_clk),
  201. CLKDEV_CON_ID("extal2_div4", &extal2_div4_clk),
  202. /* pll clock */
  203. CLKDEV_CON_ID("pll1", &pll1_clk),
  204. CLKDEV_CON_ID("pll1_div2", &pll1_div2_clk),
  205. CLKDEV_CON_ID("pll2", &pll2_clk),
  206. CLKDEV_CON_ID("pll2s", &pll2s_clk),
  207. CLKDEV_CON_ID("pll2h", &pll2h_clk),
  208. /* MSTP */
  209. CLKDEV_DEV_ID("sh-sci.0", &mstp_clks[MSTP204]),
  210. CLKDEV_DEV_ID("sh-sci.1", &mstp_clks[MSTP203]),
  211. CLKDEV_DEV_ID("sh-sci.2", &mstp_clks[MSTP206]),
  212. CLKDEV_DEV_ID("sh-sci.3", &mstp_clks[MSTP207]),
  213. CLKDEV_DEV_ID("sh-sci.4", &mstp_clks[MSTP216]),
  214. CLKDEV_DEV_ID("sh-sci.5", &mstp_clks[MSTP217]),
  215. CLKDEV_DEV_ID("rcar_thermal", &mstp_clks[MSTP522]),
  216. /* for DT */
  217. CLKDEV_DEV_ID("e61f0000.thermal", &mstp_clks[MSTP522]),
  218. };
  219. void __init r8a73a4_clock_init(void)
  220. {
  221. void __iomem *cpg_base, *reg;
  222. int k, ret = 0;
  223. u32 ckscr;
  224. /* fix MPCLK to EXTAL2 for now.
  225. * this is needed until more detailed clock topology is supported
  226. */
  227. cpg_base = ioremap_nocache(CPG_BASE, CPG_LEN);
  228. BUG_ON(!cpg_base);
  229. reg = cpg_base + (MPCKCR - CPG_BASE);
  230. iowrite32(ioread32(reg) | 1 << 7 | 0x0c, reg); /* set CKSEL */
  231. iounmap(cpg_base);
  232. reg = ioremap_nocache(CKSCR, PAGE_SIZE);
  233. BUG_ON(!reg);
  234. ckscr = ioread32(reg);
  235. iounmap(reg);
  236. switch ((ckscr >> 28) & 0x3) {
  237. case 0:
  238. main_clk.parent = &extal1_clk;
  239. break;
  240. case 1:
  241. main_clk.parent = &extal1_div2_clk;
  242. break;
  243. case 2:
  244. main_clk.parent = &extal2_clk;
  245. break;
  246. case 3:
  247. main_clk.parent = &extal2_div2_clk;
  248. break;
  249. }
  250. for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++)
  251. ret = clk_register(main_clks[k]);
  252. if (!ret)
  253. ret = sh_clk_div4_register(div4_clks, DIV4_NR, &div4_table);
  254. if (!ret)
  255. ret = sh_clk_mstp_register(mstp_clks, MSTP_NR);
  256. clkdev_add_table(lookups, ARRAY_SIZE(lookups));
  257. if (!ret)
  258. shmobile_clk_init();
  259. else
  260. panic("failed to setup r8a73a4 clocks\n");
  261. }