clock-emev2.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Emma Mobile EV2 clock framework support
  3. *
  4. * Copyright (C) 2012 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/io.h>
  22. #include <linux/sh_clk.h>
  23. #include <linux/clkdev.h>
  24. #include <mach/common.h>
  25. #define EMEV2_SMU_BASE 0xe0110000
  26. /* EMEV2 SMU registers */
  27. #define USIAU0_RSTCTRL 0x094
  28. #define USIBU1_RSTCTRL 0x0ac
  29. #define USIBU2_RSTCTRL 0x0b0
  30. #define USIBU3_RSTCTRL 0x0b4
  31. #define STI_RSTCTRL 0x124
  32. #define USIAU0GCLKCTRL 0x4a0
  33. #define USIBU1GCLKCTRL 0x4b8
  34. #define USIBU2GCLKCTRL 0x4bc
  35. #define USIBU3GCLKCTRL 0x04c0
  36. #define STIGCLKCTRL 0x528
  37. #define USIAU0SCLKDIV 0x61c
  38. #define USIB2SCLKDIV 0x65c
  39. #define USIB3SCLKDIV 0x660
  40. #define STI_CLKSEL 0x688
  41. #define SMU_GENERAL_REG0 0x7c0
  42. /* not pretty, but hey */
  43. static void __iomem *smu_base;
  44. static void emev2_smu_write(unsigned long value, int offs)
  45. {
  46. BUG_ON(!smu_base || (offs >= PAGE_SIZE));
  47. iowrite32(value, smu_base + offs);
  48. }
  49. void emev2_set_boot_vector(unsigned long value)
  50. {
  51. emev2_smu_write(value, SMU_GENERAL_REG0);
  52. }
  53. static struct clk_mapping smu_mapping = {
  54. .phys = EMEV2_SMU_BASE,
  55. .len = PAGE_SIZE,
  56. };
  57. /* Fixed 32 KHz root clock from C32K pin */
  58. static struct clk c32k_clk = {
  59. .rate = 32768,
  60. .mapping = &smu_mapping,
  61. };
  62. /* PLL3 multiplies C32K with 7000 */
  63. static unsigned long pll3_recalc(struct clk *clk)
  64. {
  65. return clk->parent->rate * 7000;
  66. }
  67. static struct sh_clk_ops pll3_clk_ops = {
  68. .recalc = pll3_recalc,
  69. };
  70. static struct clk pll3_clk = {
  71. .ops = &pll3_clk_ops,
  72. .parent = &c32k_clk,
  73. };
  74. static struct clk *main_clks[] = {
  75. &c32k_clk,
  76. &pll3_clk,
  77. };
  78. enum { SCLKDIV_USIAU0, SCLKDIV_USIBU2, SCLKDIV_USIBU1, SCLKDIV_USIBU3,
  79. SCLKDIV_NR };
  80. #define SCLKDIV(_reg, _shift) \
  81. { \
  82. .parent = &pll3_clk, \
  83. .enable_reg = IOMEM(EMEV2_SMU_BASE + (_reg)), \
  84. .enable_bit = _shift, \
  85. }
  86. static struct clk sclkdiv_clks[SCLKDIV_NR] = {
  87. [SCLKDIV_USIAU0] = SCLKDIV(USIAU0SCLKDIV, 0),
  88. [SCLKDIV_USIBU2] = SCLKDIV(USIB2SCLKDIV, 16),
  89. [SCLKDIV_USIBU1] = SCLKDIV(USIB2SCLKDIV, 0),
  90. [SCLKDIV_USIBU3] = SCLKDIV(USIB3SCLKDIV, 0),
  91. };
  92. enum { GCLK_USIAU0_SCLK, GCLK_USIBU1_SCLK, GCLK_USIBU2_SCLK, GCLK_USIBU3_SCLK,
  93. GCLK_STI_SCLK,
  94. GCLK_NR };
  95. #define GCLK_SCLK(_parent, _reg) \
  96. { \
  97. .parent = _parent, \
  98. .enable_reg = IOMEM(EMEV2_SMU_BASE + (_reg)), \
  99. .enable_bit = 1, /* SCLK_GCC */ \
  100. }
  101. static struct clk gclk_clks[GCLK_NR] = {
  102. [GCLK_USIAU0_SCLK] = GCLK_SCLK(&sclkdiv_clks[SCLKDIV_USIAU0],
  103. USIAU0GCLKCTRL),
  104. [GCLK_USIBU1_SCLK] = GCLK_SCLK(&sclkdiv_clks[SCLKDIV_USIBU1],
  105. USIBU1GCLKCTRL),
  106. [GCLK_USIBU2_SCLK] = GCLK_SCLK(&sclkdiv_clks[SCLKDIV_USIBU2],
  107. USIBU2GCLKCTRL),
  108. [GCLK_USIBU3_SCLK] = GCLK_SCLK(&sclkdiv_clks[SCLKDIV_USIBU3],
  109. USIBU3GCLKCTRL),
  110. [GCLK_STI_SCLK] = GCLK_SCLK(&c32k_clk, STIGCLKCTRL),
  111. };
  112. static int emev2_gclk_enable(struct clk *clk)
  113. {
  114. iowrite32(ioread32(clk->mapped_reg) | (1 << clk->enable_bit),
  115. clk->mapped_reg);
  116. return 0;
  117. }
  118. static void emev2_gclk_disable(struct clk *clk)
  119. {
  120. iowrite32(ioread32(clk->mapped_reg) & ~(1 << clk->enable_bit),
  121. clk->mapped_reg);
  122. }
  123. static struct sh_clk_ops emev2_gclk_clk_ops = {
  124. .enable = emev2_gclk_enable,
  125. .disable = emev2_gclk_disable,
  126. .recalc = followparent_recalc,
  127. };
  128. static int __init emev2_gclk_register(struct clk *clks, int nr)
  129. {
  130. struct clk *clkp;
  131. int ret = 0;
  132. int k;
  133. for (k = 0; !ret && (k < nr); k++) {
  134. clkp = clks + k;
  135. clkp->ops = &emev2_gclk_clk_ops;
  136. ret |= clk_register(clkp);
  137. }
  138. return ret;
  139. }
  140. static unsigned long emev2_sclkdiv_recalc(struct clk *clk)
  141. {
  142. unsigned int sclk_div;
  143. sclk_div = (ioread32(clk->mapped_reg) >> clk->enable_bit) & 0xff;
  144. return clk->parent->rate / (sclk_div + 1);
  145. }
  146. static struct sh_clk_ops emev2_sclkdiv_clk_ops = {
  147. .recalc = emev2_sclkdiv_recalc,
  148. };
  149. static int __init emev2_sclkdiv_register(struct clk *clks, int nr)
  150. {
  151. struct clk *clkp;
  152. int ret = 0;
  153. int k;
  154. for (k = 0; !ret && (k < nr); k++) {
  155. clkp = clks + k;
  156. clkp->ops = &emev2_sclkdiv_clk_ops;
  157. ret |= clk_register(clkp);
  158. }
  159. return ret;
  160. }
  161. static struct clk_lookup lookups[] = {
  162. CLKDEV_DEV_ID("serial8250-em.0", &gclk_clks[GCLK_USIAU0_SCLK]),
  163. CLKDEV_DEV_ID("e1020000.uart", &gclk_clks[GCLK_USIAU0_SCLK]),
  164. CLKDEV_DEV_ID("serial8250-em.1", &gclk_clks[GCLK_USIBU1_SCLK]),
  165. CLKDEV_DEV_ID("e1030000.uart", &gclk_clks[GCLK_USIBU1_SCLK]),
  166. CLKDEV_DEV_ID("serial8250-em.2", &gclk_clks[GCLK_USIBU2_SCLK]),
  167. CLKDEV_DEV_ID("e1040000.uart", &gclk_clks[GCLK_USIBU2_SCLK]),
  168. CLKDEV_DEV_ID("serial8250-em.3", &gclk_clks[GCLK_USIBU3_SCLK]),
  169. CLKDEV_DEV_ID("e1050000.uart", &gclk_clks[GCLK_USIBU3_SCLK]),
  170. CLKDEV_DEV_ID("em_sti.0", &gclk_clks[GCLK_STI_SCLK]),
  171. CLKDEV_DEV_ID("e0180000.sti", &gclk_clks[GCLK_STI_SCLK]),
  172. };
  173. void __init emev2_clock_init(void)
  174. {
  175. int k, ret = 0;
  176. static int is_setup;
  177. /* yuck, this is ugly as hell, but the non-smp case of clocks
  178. * code is now designed to rely on ioremap() instead of static
  179. * entity maps. in the case of smp we need access to the SMU
  180. * register earlier than ioremap() is actually working without
  181. * any static maps. to enable SMP in ugly but with dynamic
  182. * mappings we have to call emev2_clock_init() from different
  183. * places depending on UP and SMP...
  184. */
  185. if (is_setup++)
  186. return;
  187. smu_base = ioremap(EMEV2_SMU_BASE, PAGE_SIZE);
  188. BUG_ON(!smu_base);
  189. /* setup STI timer to run on 37.768 kHz and deassert reset */
  190. emev2_smu_write(0, STI_CLKSEL);
  191. emev2_smu_write(1, STI_RSTCTRL);
  192. /* deassert reset for UART0->UART3 */
  193. emev2_smu_write(2, USIAU0_RSTCTRL);
  194. emev2_smu_write(2, USIBU1_RSTCTRL);
  195. emev2_smu_write(2, USIBU2_RSTCTRL);
  196. emev2_smu_write(2, USIBU3_RSTCTRL);
  197. for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++)
  198. ret = clk_register(main_clks[k]);
  199. if (!ret)
  200. ret = emev2_sclkdiv_register(sclkdiv_clks, SCLKDIV_NR);
  201. if (!ret)
  202. ret = emev2_gclk_register(gclk_clks, GCLK_NR);
  203. clkdev_add_table(lookups, ARRAY_SIZE(lookups));
  204. if (!ret)
  205. shmobile_clk_init();
  206. else
  207. panic("failed to setup emev2 clocks\n");
  208. }