clock-sh7723.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <asm/clock.h>
  27. #include <asm/hwblk.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 DLLFRQ 0xa4150050
  37. /* Fixed 32 KHz root clock for RTC and Power Management purposes */
  38. static struct clk r_clk = {
  39. .rate = 32768,
  40. };
  41. /*
  42. * Default rate for the root input clock, reset this with clk_set_rate()
  43. * from the platform code.
  44. */
  45. struct clk extal_clk = {
  46. .rate = 33333333,
  47. };
  48. /* The dll multiplies the 32khz r_clk, may be used instead of extal */
  49. static unsigned long dll_recalc(struct clk *clk)
  50. {
  51. unsigned long mult;
  52. if (__raw_readl(PLLCR) & 0x1000)
  53. mult = __raw_readl(DLLFRQ);
  54. else
  55. mult = 0;
  56. return clk->parent->rate * mult;
  57. }
  58. static struct clk_ops dll_clk_ops = {
  59. .recalc = dll_recalc,
  60. };
  61. static struct clk dll_clk = {
  62. .ops = &dll_clk_ops,
  63. .parent = &r_clk,
  64. .flags = CLK_ENABLE_ON_INIT,
  65. };
  66. static unsigned long pll_recalc(struct clk *clk)
  67. {
  68. unsigned long mult = 1;
  69. unsigned long div = 1;
  70. if (__raw_readl(PLLCR) & 0x4000)
  71. mult = (((__raw_readl(FRQCR) >> 24) & 0x1f) + 1);
  72. else
  73. div = 2;
  74. return (clk->parent->rate * mult) / div;
  75. }
  76. static struct clk_ops pll_clk_ops = {
  77. .recalc = pll_recalc,
  78. };
  79. static struct clk pll_clk = {
  80. .ops = &pll_clk_ops,
  81. .flags = CLK_ENABLE_ON_INIT,
  82. };
  83. struct clk *main_clks[] = {
  84. &r_clk,
  85. &extal_clk,
  86. &dll_clk,
  87. &pll_clk,
  88. };
  89. static int multipliers[] = { 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  90. static int divisors[] = { 1, 3, 2, 5, 3, 4, 5, 6, 8, 10, 12, 16, 20 };
  91. static struct clk_div_mult_table div4_div_mult_table = {
  92. .divisors = divisors,
  93. .nr_divisors = ARRAY_SIZE(divisors),
  94. .multipliers = multipliers,
  95. .nr_multipliers = ARRAY_SIZE(multipliers),
  96. };
  97. static struct clk_div4_table div4_table = {
  98. .div_mult_table = &div4_div_mult_table,
  99. };
  100. enum { DIV4_I, DIV4_U, DIV4_SH, DIV4_B, DIV4_B3, DIV4_P, DIV4_NR };
  101. #define DIV4(_reg, _bit, _mask, _flags) \
  102. SH_CLK_DIV4(&pll_clk, _reg, _bit, _mask, _flags)
  103. struct clk div4_clks[DIV4_NR] = {
  104. [DIV4_I] = DIV4(FRQCR, 20, 0x0dbf, CLK_ENABLE_ON_INIT),
  105. [DIV4_U] = DIV4(FRQCR, 16, 0x0dbf, CLK_ENABLE_ON_INIT),
  106. [DIV4_SH] = DIV4(FRQCR, 12, 0x0dbf, CLK_ENABLE_ON_INIT),
  107. [DIV4_B] = DIV4(FRQCR, 8, 0x0dbf, CLK_ENABLE_ON_INIT),
  108. [DIV4_B3] = DIV4(FRQCR, 4, 0x0db4, CLK_ENABLE_ON_INIT),
  109. [DIV4_P] = DIV4(FRQCR, 0, 0x0dbf, 0),
  110. };
  111. enum { DIV4_IRDA, DIV4_ENABLE_NR };
  112. struct clk div4_enable_clks[DIV4_ENABLE_NR] = {
  113. [DIV4_IRDA] = DIV4(IRDACLKCR, 0, 0x0dbf, 0),
  114. };
  115. enum { DIV4_SIUA, DIV4_SIUB, DIV4_REPARENT_NR };
  116. struct clk div4_reparent_clks[DIV4_REPARENT_NR] = {
  117. [DIV4_SIUA] = DIV4(SCLKACR, 0, 0x0dbf, 0),
  118. [DIV4_SIUB] = DIV4(SCLKBCR, 0, 0x0dbf, 0),
  119. };
  120. enum { DIV6_V, DIV6_NR };
  121. struct clk div6_clks[DIV6_NR] = {
  122. [DIV6_V] = SH_CLK_DIV6(&pll_clk, VCLKCR, 0),
  123. };
  124. static struct clk mstp_clks[] = {
  125. /* See page 60 of Datasheet V1.0: Overview -> Block Diagram */
  126. SH_HWBLK_CLK(HWBLK_TLB, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  127. SH_HWBLK_CLK(HWBLK_IC, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  128. SH_HWBLK_CLK(HWBLK_OC, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  129. SH_HWBLK_CLK(HWBLK_L2C, &div4_clks[DIV4_SH], CLK_ENABLE_ON_INIT),
  130. SH_HWBLK_CLK(HWBLK_ILMEM, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  131. SH_HWBLK_CLK(HWBLK_FPU, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  132. SH_HWBLK_CLK(HWBLK_INTC, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  133. SH_HWBLK_CLK(HWBLK_DMAC0, &div4_clks[DIV4_B], 0),
  134. SH_HWBLK_CLK(HWBLK_SHYWAY, &div4_clks[DIV4_SH], CLK_ENABLE_ON_INIT),
  135. SH_HWBLK_CLK(HWBLK_HUDI, &div4_clks[DIV4_P], 0),
  136. SH_HWBLK_CLK(HWBLK_UBC, &div4_clks[DIV4_I], 0),
  137. SH_HWBLK_CLK(HWBLK_TMU0, &div4_clks[DIV4_P], 0),
  138. SH_HWBLK_CLK(HWBLK_CMT, &r_clk, 0),
  139. SH_HWBLK_CLK(HWBLK_RWDT, &r_clk, 0),
  140. SH_HWBLK_CLK(HWBLK_DMAC1, &div4_clks[DIV4_B], 0),
  141. SH_HWBLK_CLK(HWBLK_TMU1, &div4_clks[DIV4_P], 0),
  142. SH_HWBLK_CLK(HWBLK_FLCTL, &div4_clks[DIV4_P], 0),
  143. SH_HWBLK_CLK(HWBLK_SCIF0, &div4_clks[DIV4_P], 0),
  144. SH_HWBLK_CLK(HWBLK_SCIF1, &div4_clks[DIV4_P], 0),
  145. SH_HWBLK_CLK(HWBLK_SCIF2, &div4_clks[DIV4_P], 0),
  146. SH_HWBLK_CLK(HWBLK_SCIF3, &div4_clks[DIV4_B], 0),
  147. SH_HWBLK_CLK(HWBLK_SCIF4, &div4_clks[DIV4_B], 0),
  148. SH_HWBLK_CLK(HWBLK_SCIF5, &div4_clks[DIV4_B], 0),
  149. SH_HWBLK_CLK(HWBLK_MSIOF0, &div4_clks[DIV4_B], 0),
  150. SH_HWBLK_CLK(HWBLK_MSIOF1, &div4_clks[DIV4_B], 0),
  151. SH_HWBLK_CLK(HWBLK_MERAM, &div4_clks[DIV4_SH], 0),
  152. SH_HWBLK_CLK(HWBLK_IIC, &div4_clks[DIV4_P], 0),
  153. SH_HWBLK_CLK(HWBLK_RTC, &r_clk, 0),
  154. SH_HWBLK_CLK(HWBLK_ATAPI, &div4_clks[DIV4_SH], 0),
  155. SH_HWBLK_CLK(HWBLK_ADC, &div4_clks[DIV4_P], 0),
  156. SH_HWBLK_CLK(HWBLK_TPU, &div4_clks[DIV4_B], 0),
  157. SH_HWBLK_CLK(HWBLK_IRDA, &div4_clks[DIV4_P], 0),
  158. SH_HWBLK_CLK(HWBLK_TSIF, &div4_clks[DIV4_B], 0),
  159. SH_HWBLK_CLK(HWBLK_ICB, &div4_clks[DIV4_B], CLK_ENABLE_ON_INIT),
  160. SH_HWBLK_CLK(HWBLK_SDHI0, &div4_clks[DIV4_B], 0),
  161. SH_HWBLK_CLK(HWBLK_SDHI1, &div4_clks[DIV4_B], 0),
  162. SH_HWBLK_CLK(HWBLK_KEYSC, &r_clk, 0),
  163. SH_HWBLK_CLK(HWBLK_USB, &div4_clks[DIV4_B], 0),
  164. SH_HWBLK_CLK(HWBLK_2DG, &div4_clks[DIV4_B], 0),
  165. SH_HWBLK_CLK(HWBLK_SIU, &div4_clks[DIV4_B], 0),
  166. SH_HWBLK_CLK(HWBLK_VEU2H1, &div4_clks[DIV4_B], 0),
  167. SH_HWBLK_CLK(HWBLK_VOU, &div4_clks[DIV4_B], 0),
  168. SH_HWBLK_CLK(HWBLK_BEU, &div4_clks[DIV4_B], 0),
  169. SH_HWBLK_CLK(HWBLK_CEU, &div4_clks[DIV4_B], 0),
  170. SH_HWBLK_CLK(HWBLK_VEU2H0, &div4_clks[DIV4_B], 0),
  171. SH_HWBLK_CLK(HWBLK_VPU, &div4_clks[DIV4_B], 0),
  172. SH_HWBLK_CLK(HWBLK_LCDC, &div4_clks[DIV4_B], 0),
  173. };
  174. #define CLKDEV_CON_ID(_id, _clk) { .con_id = _id, .clk = _clk }
  175. static struct clk_lookup lookups[] = {
  176. /* main clocks */
  177. CLKDEV_CON_ID("rclk", &r_clk),
  178. CLKDEV_CON_ID("extal", &extal_clk),
  179. CLKDEV_CON_ID("dll_clk", &dll_clk),
  180. CLKDEV_CON_ID("pll_clk", &pll_clk),
  181. /* DIV4 clocks */
  182. CLKDEV_CON_ID("cpu_clk", &div4_clks[DIV4_I]),
  183. CLKDEV_CON_ID("umem_clk", &div4_clks[DIV4_U]),
  184. CLKDEV_CON_ID("shyway_clk", &div4_clks[DIV4_SH]),
  185. CLKDEV_CON_ID("bus_clk", &div4_clks[DIV4_B]),
  186. CLKDEV_CON_ID("b3_clk", &div4_clks[DIV4_B3]),
  187. CLKDEV_CON_ID("peripheral_clk", &div4_clks[DIV4_P]),
  188. CLKDEV_CON_ID("irda_clk", &div4_enable_clks[DIV4_IRDA]),
  189. CLKDEV_CON_ID("siua_clk", &div4_reparent_clks[DIV4_SIUA]),
  190. CLKDEV_CON_ID("siub_clk", &div4_reparent_clks[DIV4_SIUB]),
  191. /* DIV6 clocks */
  192. CLKDEV_CON_ID("video_clk", &div6_clks[DIV6_V]),
  193. /* MSTP clocks */
  194. CLKDEV_CON_ID("tlb0", &mstp_clks[HWBLK_TLB]),
  195. CLKDEV_CON_ID("ic0", &mstp_clks[HWBLK_IC]),
  196. CLKDEV_CON_ID("oc0", &mstp_clks[HWBLK_OC]),
  197. CLKDEV_CON_ID("l2c0", &mstp_clks[HWBLK_L2C]),
  198. CLKDEV_CON_ID("ilmem0", &mstp_clks[HWBLK_ILMEM]),
  199. CLKDEV_CON_ID("fpu0", &mstp_clks[HWBLK_FPU]),
  200. CLKDEV_CON_ID("intc0", &mstp_clks[HWBLK_INTC]),
  201. CLKDEV_CON_ID("dmac0", &mstp_clks[HWBLK_DMAC0]),
  202. CLKDEV_CON_ID("sh0", &mstp_clks[HWBLK_SHYWAY]),
  203. CLKDEV_CON_ID("hudi0", &mstp_clks[HWBLK_HUDI]),
  204. CLKDEV_CON_ID("ubc0", &mstp_clks[HWBLK_UBC]),
  205. {
  206. /* TMU0 */
  207. .dev_id = "sh_tmu.0",
  208. .con_id = "tmu_fck",
  209. .clk = &mstp_clks[HWBLK_TMU0],
  210. }, {
  211. /* TMU1 */
  212. .dev_id = "sh_tmu.1",
  213. .con_id = "tmu_fck",
  214. .clk = &mstp_clks[HWBLK_TMU0],
  215. }, {
  216. /* TMU2 */
  217. .dev_id = "sh_tmu.2",
  218. .con_id = "tmu_fck",
  219. .clk = &mstp_clks[HWBLK_TMU0],
  220. },
  221. CLKDEV_CON_ID("cmt_fck", &mstp_clks[HWBLK_CMT]),
  222. CLKDEV_CON_ID("rwdt0", &mstp_clks[HWBLK_RWDT]),
  223. CLKDEV_CON_ID("dmac1", &mstp_clks[HWBLK_DMAC1]),
  224. {
  225. /* TMU3 */
  226. .dev_id = "sh_tmu.3",
  227. .con_id = "tmu_fck",
  228. .clk = &mstp_clks[HWBLK_TMU1],
  229. }, {
  230. /* TMU4 */
  231. .dev_id = "sh_tmu.4",
  232. .con_id = "tmu_fck",
  233. .clk = &mstp_clks[HWBLK_TMU1],
  234. }, {
  235. /* TMU5 */
  236. .dev_id = "sh_tmu.5",
  237. .con_id = "tmu_fck",
  238. .clk = &mstp_clks[HWBLK_TMU1],
  239. },
  240. CLKDEV_CON_ID("flctl0", &mstp_clks[HWBLK_FLCTL]),
  241. {
  242. /* SCIF0 */
  243. .dev_id = "sh-sci.0",
  244. .con_id = "sci_fck",
  245. .clk = &mstp_clks[HWBLK_SCIF0],
  246. }, {
  247. /* SCIF1 */
  248. .dev_id = "sh-sci.1",
  249. .con_id = "sci_fck",
  250. .clk = &mstp_clks[HWBLK_SCIF1],
  251. }, {
  252. /* SCIF2 */
  253. .dev_id = "sh-sci.2",
  254. .con_id = "sci_fck",
  255. .clk = &mstp_clks[HWBLK_SCIF2],
  256. }, {
  257. /* SCIF3 */
  258. .dev_id = "sh-sci.3",
  259. .con_id = "sci_fck",
  260. .clk = &mstp_clks[HWBLK_SCIF3],
  261. }, {
  262. /* SCIF4 */
  263. .dev_id = "sh-sci.4",
  264. .con_id = "sci_fck",
  265. .clk = &mstp_clks[HWBLK_SCIF4],
  266. }, {
  267. /* SCIF5 */
  268. .dev_id = "sh-sci.5",
  269. .con_id = "sci_fck",
  270. .clk = &mstp_clks[HWBLK_SCIF5],
  271. },
  272. CLKDEV_CON_ID("msiof0", &mstp_clks[HWBLK_MSIOF0]),
  273. CLKDEV_CON_ID("msiof1", &mstp_clks[HWBLK_MSIOF1]),
  274. CLKDEV_CON_ID("meram0", &mstp_clks[HWBLK_MERAM]),
  275. CLKDEV_CON_ID("i2c0", &mstp_clks[HWBLK_IIC]),
  276. CLKDEV_CON_ID("rtc0", &mstp_clks[HWBLK_RTC]),
  277. CLKDEV_CON_ID("atapi0", &mstp_clks[HWBLK_ATAPI]),
  278. CLKDEV_CON_ID("adc0", &mstp_clks[HWBLK_ADC]),
  279. CLKDEV_CON_ID("tpu0", &mstp_clks[HWBLK_TPU]),
  280. CLKDEV_CON_ID("irda0", &mstp_clks[HWBLK_IRDA]),
  281. CLKDEV_CON_ID("tsif0", &mstp_clks[HWBLK_TSIF]),
  282. CLKDEV_CON_ID("icb0", &mstp_clks[HWBLK_ICB]),
  283. CLKDEV_CON_ID("sdhi0", &mstp_clks[HWBLK_SDHI0]),
  284. CLKDEV_CON_ID("sdhi1", &mstp_clks[HWBLK_SDHI1]),
  285. CLKDEV_CON_ID("keysc0", &mstp_clks[HWBLK_KEYSC]),
  286. CLKDEV_CON_ID("usb0", &mstp_clks[HWBLK_USB]),
  287. CLKDEV_CON_ID("2dg0", &mstp_clks[HWBLK_2DG]),
  288. CLKDEV_CON_ID("siu0", &mstp_clks[HWBLK_SIU]),
  289. CLKDEV_CON_ID("veu1", &mstp_clks[HWBLK_VEU2H1]),
  290. CLKDEV_CON_ID("vou0", &mstp_clks[HWBLK_VOU]),
  291. CLKDEV_CON_ID("beu0", &mstp_clks[HWBLK_BEU]),
  292. CLKDEV_CON_ID("ceu0", &mstp_clks[HWBLK_CEU]),
  293. CLKDEV_CON_ID("veu0", &mstp_clks[HWBLK_VEU2H0]),
  294. CLKDEV_CON_ID("vpu0", &mstp_clks[HWBLK_VPU]),
  295. CLKDEV_CON_ID("lcdc0", &mstp_clks[HWBLK_LCDC]),
  296. };
  297. int __init arch_clk_init(void)
  298. {
  299. int k, ret = 0;
  300. /* autodetect extal or dll configuration */
  301. if (__raw_readl(PLLCR) & 0x1000)
  302. pll_clk.parent = &dll_clk;
  303. else
  304. pll_clk.parent = &extal_clk;
  305. for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++)
  306. ret |= clk_register(main_clks[k]);
  307. clkdev_add_table(lookups, ARRAY_SIZE(lookups));
  308. if (!ret)
  309. ret = sh_clk_div4_register(div4_clks, DIV4_NR, &div4_table);
  310. if (!ret)
  311. ret = sh_clk_div4_enable_register(div4_enable_clks,
  312. DIV4_ENABLE_NR, &div4_table);
  313. if (!ret)
  314. ret = sh_clk_div4_reparent_register(div4_reparent_clks,
  315. DIV4_REPARENT_NR, &div4_table);
  316. if (!ret)
  317. ret = sh_clk_div6_register(div6_clks, DIV6_NR);
  318. if (!ret)
  319. ret = sh_hwblk_clk_register(mstp_clks, HWBLK_NR);
  320. return ret;
  321. }