clock-sh7723.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * arch/sh/kernel/cpu/sh4a/clock-sh7723.c
  3. *
  4. * SH7723 clock framework support
  5. *
  6. * Copyright (C) 2009 Magnus Damm
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/io.h>
  24. #include <linux/clk.h>
  25. #include <linux/clkdev.h>
  26. #include <linux/sh_clk.h>
  27. #include <asm/clock.h>
  28. #include <cpu/sh7723.h>
  29. /* SH7723 registers */
  30. #define FRQCR 0xa4150000
  31. #define VCLKCR 0xa4150004
  32. #define SCLKACR 0xa4150008
  33. #define SCLKBCR 0xa415000c
  34. #define IRDACLKCR 0xa4150018
  35. #define PLLCR 0xa4150024
  36. #define MSTPCR0 0xa4150030
  37. #define MSTPCR1 0xa4150034
  38. #define MSTPCR2 0xa4150038
  39. #define DLLFRQ 0xa4150050
  40. /* Fixed 32 KHz root clock for RTC and Power Management purposes */
  41. static struct clk r_clk = {
  42. .rate = 32768,
  43. };
  44. /*
  45. * Default rate for the root input clock, reset this with clk_set_rate()
  46. * from the platform code.
  47. */
  48. struct clk extal_clk = {
  49. .rate = 33333333,
  50. };
  51. /* The dll multiplies the 32khz r_clk, may be used instead of extal */
  52. static unsigned long dll_recalc(struct clk *clk)
  53. {
  54. unsigned long mult;
  55. if (__raw_readl(PLLCR) & 0x1000)
  56. mult = __raw_readl(DLLFRQ);
  57. else
  58. mult = 0;
  59. return clk->parent->rate * mult;
  60. }
  61. static struct sh_clk_ops dll_clk_ops = {
  62. .recalc = dll_recalc,
  63. };
  64. static struct clk dll_clk = {
  65. .ops = &dll_clk_ops,
  66. .parent = &r_clk,
  67. .flags = CLK_ENABLE_ON_INIT,
  68. };
  69. static unsigned long pll_recalc(struct clk *clk)
  70. {
  71. unsigned long mult = 1;
  72. unsigned long div = 1;
  73. if (__raw_readl(PLLCR) & 0x4000)
  74. mult = (((__raw_readl(FRQCR) >> 24) & 0x1f) + 1);
  75. else
  76. div = 2;
  77. return (clk->parent->rate * mult) / div;
  78. }
  79. static struct sh_clk_ops pll_clk_ops = {
  80. .recalc = pll_recalc,
  81. };
  82. static struct clk pll_clk = {
  83. .ops = &pll_clk_ops,
  84. .flags = CLK_ENABLE_ON_INIT,
  85. };
  86. struct clk *main_clks[] = {
  87. &r_clk,
  88. &extal_clk,
  89. &dll_clk,
  90. &pll_clk,
  91. };
  92. static int multipliers[] = { 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  93. static int divisors[] = { 1, 3, 2, 5, 3, 4, 5, 6, 8, 10, 12, 16, 20 };
  94. static struct clk_div_mult_table div4_div_mult_table = {
  95. .divisors = divisors,
  96. .nr_divisors = ARRAY_SIZE(divisors),
  97. .multipliers = multipliers,
  98. .nr_multipliers = ARRAY_SIZE(multipliers),
  99. };
  100. static struct clk_div4_table div4_table = {
  101. .div_mult_table = &div4_div_mult_table,
  102. };
  103. enum { DIV4_I, DIV4_U, DIV4_SH, DIV4_B, DIV4_B3, DIV4_P, DIV4_NR };
  104. #define DIV4(_reg, _bit, _mask, _flags) \
  105. SH_CLK_DIV4(&pll_clk, _reg, _bit, _mask, _flags)
  106. struct clk div4_clks[DIV4_NR] = {
  107. [DIV4_I] = DIV4(FRQCR, 20, 0x0dbf, CLK_ENABLE_ON_INIT),
  108. [DIV4_U] = DIV4(FRQCR, 16, 0x0dbf, CLK_ENABLE_ON_INIT),
  109. [DIV4_SH] = DIV4(FRQCR, 12, 0x0dbf, CLK_ENABLE_ON_INIT),
  110. [DIV4_B] = DIV4(FRQCR, 8, 0x0dbf, CLK_ENABLE_ON_INIT),
  111. [DIV4_B3] = DIV4(FRQCR, 4, 0x0db4, CLK_ENABLE_ON_INIT),
  112. [DIV4_P] = DIV4(FRQCR, 0, 0x0dbf, 0),
  113. };
  114. enum { DIV4_IRDA, DIV4_ENABLE_NR };
  115. struct clk div4_enable_clks[DIV4_ENABLE_NR] = {
  116. [DIV4_IRDA] = DIV4(IRDACLKCR, 0, 0x0dbf, 0),
  117. };
  118. enum { DIV4_SIUA, DIV4_SIUB, DIV4_REPARENT_NR };
  119. struct clk div4_reparent_clks[DIV4_REPARENT_NR] = {
  120. [DIV4_SIUA] = DIV4(SCLKACR, 0, 0x0dbf, 0),
  121. [DIV4_SIUB] = DIV4(SCLKBCR, 0, 0x0dbf, 0),
  122. };
  123. enum { DIV6_V, DIV6_NR };
  124. struct clk div6_clks[DIV6_NR] = {
  125. [DIV6_V] = SH_CLK_DIV6(&pll_clk, VCLKCR, 0),
  126. };
  127. static struct clk mstp_clks[] = {
  128. /* See page 60 of Datasheet V1.0: Overview -> Block Diagram */
  129. [HWBLK_TLB] = SH_CLK_MSTP32(&div4_clks[DIV4_I], MSTPCR0, 31, CLK_ENABLE_ON_INIT),
  130. [HWBLK_IC] = SH_CLK_MSTP32(&div4_clks[DIV4_I], MSTPCR0, 30, CLK_ENABLE_ON_INIT),
  131. [HWBLK_OC] = SH_CLK_MSTP32(&div4_clks[DIV4_I], MSTPCR0, 29, CLK_ENABLE_ON_INIT),
  132. [HWBLK_L2C] = SH_CLK_MSTP32(&div4_clks[DIV4_SH], MSTPCR0, 28, CLK_ENABLE_ON_INIT),
  133. [HWBLK_ILMEM] = SH_CLK_MSTP32(&div4_clks[DIV4_I], MSTPCR0, 27, CLK_ENABLE_ON_INIT),
  134. [HWBLK_FPU] = SH_CLK_MSTP32(&div4_clks[DIV4_I], MSTPCR0, 24, CLK_ENABLE_ON_INIT),
  135. [HWBLK_INTC] = SH_CLK_MSTP32(&div4_clks[DIV4_I], MSTPCR0, 22, CLK_ENABLE_ON_INIT),
  136. [HWBLK_DMAC0] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR0, 21, 0),
  137. [HWBLK_SHYWAY] = SH_CLK_MSTP32(&div4_clks[DIV4_SH], MSTPCR0, 20, CLK_ENABLE_ON_INIT),
  138. [HWBLK_HUDI] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 19, 0),
  139. [HWBLK_UBC] = SH_CLK_MSTP32(&div4_clks[DIV4_I], MSTPCR0, 17, 0),
  140. [HWBLK_TMU0] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 15, 0),
  141. [HWBLK_CMT] = SH_CLK_MSTP32(&r_clk, MSTPCR0, 14, 0),
  142. [HWBLK_RWDT] = SH_CLK_MSTP32(&r_clk, MSTPCR0, 13, 0),
  143. [HWBLK_DMAC1] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR0, 12, 0),
  144. [HWBLK_TMU1] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 11, 0),
  145. [HWBLK_FLCTL] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 10, 0),
  146. [HWBLK_SCIF0] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 9, 0),
  147. [HWBLK_SCIF1] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 8, 0),
  148. [HWBLK_SCIF2] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR0, 7, 0),
  149. [HWBLK_SCIF3] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR0, 6, 0),
  150. [HWBLK_SCIF4] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR0, 5, 0),
  151. [HWBLK_SCIF5] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR0, 4, 0),
  152. [HWBLK_MSIOF0] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR0, 2, 0),
  153. [HWBLK_MSIOF1] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR0, 1, 0),
  154. [HWBLK_MERAM] = SH_CLK_MSTP32(&div4_clks[DIV4_SH], MSTPCR0, 0, 0),
  155. [HWBLK_IIC] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR1, 9, 0),
  156. [HWBLK_RTC] = SH_CLK_MSTP32(&r_clk, MSTPCR1, 8, 0),
  157. [HWBLK_ATAPI] = SH_CLK_MSTP32(&div4_clks[DIV4_SH], MSTPCR2, 28, 0),
  158. [HWBLK_ADC] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR2, 27, 0),
  159. [HWBLK_TPU] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 25, 0),
  160. [HWBLK_IRDA] = SH_CLK_MSTP32(&div4_clks[DIV4_P], MSTPCR2, 24, 0),
  161. [HWBLK_TSIF] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 22, 0),
  162. [HWBLK_ICB] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 21, CLK_ENABLE_ON_INIT),
  163. [HWBLK_SDHI0] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 18, 0),
  164. [HWBLK_SDHI1] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 17, 0),
  165. [HWBLK_KEYSC] = SH_CLK_MSTP32(&r_clk, MSTPCR2, 14, 0),
  166. [HWBLK_USB] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 11, 0),
  167. [HWBLK_2DG] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 10, 0),
  168. [HWBLK_SIU] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 8, 0),
  169. [HWBLK_VEU2H1] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 6, 0),
  170. [HWBLK_VOU] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 5, 0),
  171. [HWBLK_BEU] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 4, 0),
  172. [HWBLK_CEU] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 3, 0),
  173. [HWBLK_VEU2H0] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 2, 0),
  174. [HWBLK_VPU] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 1, 0),
  175. [HWBLK_LCDC] = SH_CLK_MSTP32(&div4_clks[DIV4_B], MSTPCR2, 0, 0),
  176. };
  177. static struct clk_lookup lookups[] = {
  178. /* main clocks */
  179. CLKDEV_CON_ID("rclk", &r_clk),
  180. CLKDEV_CON_ID("extal", &extal_clk),
  181. CLKDEV_CON_ID("dll_clk", &dll_clk),
  182. CLKDEV_CON_ID("pll_clk", &pll_clk),
  183. /* DIV4 clocks */
  184. CLKDEV_CON_ID("cpu_clk", &div4_clks[DIV4_I]),
  185. CLKDEV_CON_ID("umem_clk", &div4_clks[DIV4_U]),
  186. CLKDEV_CON_ID("shyway_clk", &div4_clks[DIV4_SH]),
  187. CLKDEV_CON_ID("bus_clk", &div4_clks[DIV4_B]),
  188. CLKDEV_CON_ID("b3_clk", &div4_clks[DIV4_B3]),
  189. CLKDEV_CON_ID("peripheral_clk", &div4_clks[DIV4_P]),
  190. CLKDEV_CON_ID("irda_clk", &div4_enable_clks[DIV4_IRDA]),
  191. CLKDEV_CON_ID("siua_clk", &div4_reparent_clks[DIV4_SIUA]),
  192. CLKDEV_CON_ID("siub_clk", &div4_reparent_clks[DIV4_SIUB]),
  193. /* DIV6 clocks */
  194. CLKDEV_CON_ID("video_clk", &div6_clks[DIV6_V]),
  195. /* MSTP clocks */
  196. CLKDEV_CON_ID("tlb0", &mstp_clks[HWBLK_TLB]),
  197. CLKDEV_CON_ID("ic0", &mstp_clks[HWBLK_IC]),
  198. CLKDEV_CON_ID("oc0", &mstp_clks[HWBLK_OC]),
  199. CLKDEV_CON_ID("l2c0", &mstp_clks[HWBLK_L2C]),
  200. CLKDEV_CON_ID("ilmem0", &mstp_clks[HWBLK_ILMEM]),
  201. CLKDEV_CON_ID("fpu0", &mstp_clks[HWBLK_FPU]),
  202. CLKDEV_CON_ID("intc0", &mstp_clks[HWBLK_INTC]),
  203. CLKDEV_DEV_ID("sh-dma-engine.0", &mstp_clks[HWBLK_DMAC0]),
  204. CLKDEV_CON_ID("sh0", &mstp_clks[HWBLK_SHYWAY]),
  205. CLKDEV_CON_ID("hudi0", &mstp_clks[HWBLK_HUDI]),
  206. CLKDEV_CON_ID("ubc0", &mstp_clks[HWBLK_UBC]),
  207. CLKDEV_ICK_ID("fck", "sh-cmt-32.0", &mstp_clks[HWBLK_CMT]),
  208. CLKDEV_DEV_ID("sh-wdt.0", &mstp_clks[HWBLK_RWDT]),
  209. CLKDEV_DEV_ID("sh-dma-engine.1", &mstp_clks[HWBLK_DMAC1]),
  210. CLKDEV_CON_ID("flctl0", &mstp_clks[HWBLK_FLCTL]),
  211. CLKDEV_DEV_ID("spi_sh_msiof.0", &mstp_clks[HWBLK_MSIOF0]),
  212. CLKDEV_DEV_ID("spi_sh_msiof.1", &mstp_clks[HWBLK_MSIOF1]),
  213. CLKDEV_DEV_ID("sh_mobile_meram.0", &mstp_clks[HWBLK_MERAM]),
  214. CLKDEV_DEV_ID("i2c-sh_mobile.0", &mstp_clks[HWBLK_IIC]),
  215. CLKDEV_CON_ID("rtc0", &mstp_clks[HWBLK_RTC]),
  216. CLKDEV_CON_ID("atapi0", &mstp_clks[HWBLK_ATAPI]),
  217. CLKDEV_CON_ID("adc0", &mstp_clks[HWBLK_ADC]),
  218. CLKDEV_CON_ID("tpu0", &mstp_clks[HWBLK_TPU]),
  219. CLKDEV_CON_ID("irda0", &mstp_clks[HWBLK_IRDA]),
  220. CLKDEV_CON_ID("tsif0", &mstp_clks[HWBLK_TSIF]),
  221. CLKDEV_CON_ID("icb0", &mstp_clks[HWBLK_ICB]),
  222. CLKDEV_DEV_ID("sh_mobile_sdhi.0", &mstp_clks[HWBLK_SDHI0]),
  223. CLKDEV_DEV_ID("sh_mobile_sdhi.1", &mstp_clks[HWBLK_SDHI1]),
  224. CLKDEV_DEV_ID("sh_keysc.0", &mstp_clks[HWBLK_KEYSC]),
  225. CLKDEV_CON_ID("usb0", &mstp_clks[HWBLK_USB]),
  226. CLKDEV_CON_ID("2dg0", &mstp_clks[HWBLK_2DG]),
  227. CLKDEV_DEV_ID("siu-pcm-audio", &mstp_clks[HWBLK_SIU]),
  228. CLKDEV_CON_ID("veu1", &mstp_clks[HWBLK_VEU2H1]),
  229. CLKDEV_DEV_ID("sh-vou.0", &mstp_clks[HWBLK_VOU]),
  230. CLKDEV_CON_ID("beu0", &mstp_clks[HWBLK_BEU]),
  231. CLKDEV_DEV_ID("sh_mobile_ceu.0", &mstp_clks[HWBLK_CEU]),
  232. CLKDEV_CON_ID("veu0", &mstp_clks[HWBLK_VEU2H0]),
  233. CLKDEV_CON_ID("vpu0", &mstp_clks[HWBLK_VPU]),
  234. CLKDEV_ICK_ID("fck", "sh-tmu.0", &mstp_clks[HWBLK_TMU0]),
  235. CLKDEV_ICK_ID("fck", "sh-tmu.1", &mstp_clks[HWBLK_TMU1]),
  236. CLKDEV_ICK_ID("sci_fck", "sh-sci.0", &mstp_clks[HWBLK_SCIF0]),
  237. CLKDEV_ICK_ID("sci_fck", "sh-sci.1", &mstp_clks[HWBLK_SCIF1]),
  238. CLKDEV_ICK_ID("sci_fck", "sh-sci.2", &mstp_clks[HWBLK_SCIF2]),
  239. CLKDEV_ICK_ID("sci_fck", "sh-sci.3", &mstp_clks[HWBLK_SCIF3]),
  240. CLKDEV_ICK_ID("sci_fck", "sh-sci.4", &mstp_clks[HWBLK_SCIF4]),
  241. CLKDEV_ICK_ID("sci_fck", "sh-sci.5", &mstp_clks[HWBLK_SCIF5]),
  242. CLKDEV_DEV_ID("sh_mobile_lcdc_fb.0", &mstp_clks[HWBLK_LCDC]),
  243. };
  244. int __init arch_clk_init(void)
  245. {
  246. int k, ret = 0;
  247. /* autodetect extal or dll configuration */
  248. if (__raw_readl(PLLCR) & 0x1000)
  249. pll_clk.parent = &dll_clk;
  250. else
  251. pll_clk.parent = &extal_clk;
  252. for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++)
  253. ret |= clk_register(main_clks[k]);
  254. clkdev_add_table(lookups, ARRAY_SIZE(lookups));
  255. if (!ret)
  256. ret = sh_clk_div4_register(div4_clks, DIV4_NR, &div4_table);
  257. if (!ret)
  258. ret = sh_clk_div4_enable_register(div4_enable_clks,
  259. DIV4_ENABLE_NR, &div4_table);
  260. if (!ret)
  261. ret = sh_clk_div4_reparent_register(div4_reparent_clks,
  262. DIV4_REPARENT_NR, &div4_table);
  263. if (!ret)
  264. ret = sh_clk_div6_register(div6_clks, DIV6_NR);
  265. if (!ret)
  266. ret = sh_clk_mstp_register(mstp_clks, HWBLK_NR);
  267. return ret;
  268. }