clock-sh7724.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * arch/sh/kernel/cpu/sh4a/clock-sh7724.c
  3. *
  4. * SH7724 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 <asm/clkdev.h>
  26. #include <asm/clock.h>
  27. #include <asm/hwblk.h>
  28. #include <cpu/sh7724.h>
  29. /* SH7724 registers */
  30. #define FRQCRA 0xa4150000
  31. #define FRQCRB 0xa4150004
  32. #define VCLKCR 0xa4150048
  33. #define FCLKACR 0xa4150008
  34. #define FCLKBCR 0xa415000c
  35. #define IRDACLKCR 0xa4150018
  36. #define PLLCR 0xa4150024
  37. #define SPUCLKCR 0xa415003c
  38. #define FLLFRQ 0xa4150050
  39. #define LSTATS 0xa4150060
  40. /* Fixed 32 KHz root clock for RTC and Power Management purposes */
  41. static struct clk r_clk = {
  42. .name = "rclk",
  43. .id = -1,
  44. .rate = 32768,
  45. };
  46. /*
  47. * Default rate for the root input clock, reset this with clk_set_rate()
  48. * from the platform code.
  49. */
  50. struct clk extal_clk = {
  51. .name = "extal",
  52. .id = -1,
  53. .rate = 33333333,
  54. };
  55. /* The fll multiplies the 32khz r_clk, may be used instead of extal */
  56. static unsigned long fll_recalc(struct clk *clk)
  57. {
  58. unsigned long mult = 0;
  59. unsigned long div = 1;
  60. if (__raw_readl(PLLCR) & 0x1000)
  61. mult = __raw_readl(FLLFRQ) & 0x3ff;
  62. if (__raw_readl(FLLFRQ) & 0x4000)
  63. div = 2;
  64. return (clk->parent->rate * mult) / div;
  65. }
  66. static struct clk_ops fll_clk_ops = {
  67. .recalc = fll_recalc,
  68. };
  69. static struct clk fll_clk = {
  70. .name = "fll_clk",
  71. .id = -1,
  72. .ops = &fll_clk_ops,
  73. .parent = &r_clk,
  74. .flags = CLK_ENABLE_ON_INIT,
  75. };
  76. static unsigned long pll_recalc(struct clk *clk)
  77. {
  78. unsigned long mult = 1;
  79. if (__raw_readl(PLLCR) & 0x4000)
  80. mult = (((__raw_readl(FRQCRA) >> 24) & 0x3f) + 1) * 2;
  81. return clk->parent->rate * mult;
  82. }
  83. static struct clk_ops pll_clk_ops = {
  84. .recalc = pll_recalc,
  85. };
  86. static struct clk pll_clk = {
  87. .name = "pll_clk",
  88. .id = -1,
  89. .ops = &pll_clk_ops,
  90. .flags = CLK_ENABLE_ON_INIT,
  91. };
  92. /* A fixed divide-by-3 block use by the div6 clocks */
  93. static unsigned long div3_recalc(struct clk *clk)
  94. {
  95. return clk->parent->rate / 3;
  96. }
  97. static struct clk_ops div3_clk_ops = {
  98. .recalc = div3_recalc,
  99. };
  100. static struct clk div3_clk = {
  101. .name = "div3_clk",
  102. .id = -1,
  103. .ops = &div3_clk_ops,
  104. .parent = &pll_clk,
  105. };
  106. struct clk *main_clks[] = {
  107. &r_clk,
  108. &extal_clk,
  109. &fll_clk,
  110. &pll_clk,
  111. &div3_clk,
  112. };
  113. static void div4_kick(struct clk *clk)
  114. {
  115. unsigned long value;
  116. /* set KICK bit in FRQCRA to update hardware setting */
  117. value = __raw_readl(FRQCRA);
  118. value |= (1 << 31);
  119. __raw_writel(value, FRQCRA);
  120. }
  121. static int divisors[] = { 2, 3, 4, 6, 8, 12, 16, 0, 24, 32, 36, 48, 0, 72 };
  122. static struct clk_div_mult_table div4_div_mult_table = {
  123. .divisors = divisors,
  124. .nr_divisors = ARRAY_SIZE(divisors),
  125. };
  126. static struct clk_div4_table div4_table = {
  127. .div_mult_table = &div4_div_mult_table,
  128. .kick = div4_kick,
  129. };
  130. enum { DIV4_I, DIV4_SH, DIV4_B, DIV4_P, DIV4_M1, DIV4_NR };
  131. #define DIV4(_str, _reg, _bit, _mask, _flags) \
  132. SH_CLK_DIV4(_str, &pll_clk, _reg, _bit, _mask, _flags)
  133. struct clk div4_clks[DIV4_NR] = {
  134. [DIV4_I] = DIV4("cpu_clk", FRQCRA, 20, 0x2f7d, CLK_ENABLE_ON_INIT),
  135. [DIV4_SH] = DIV4("shyway_clk", FRQCRA, 12, 0x2f7c, CLK_ENABLE_ON_INIT),
  136. [DIV4_B] = DIV4("bus_clk", FRQCRA, 8, 0x2f7c, CLK_ENABLE_ON_INIT),
  137. [DIV4_P] = DIV4("peripheral_clk", FRQCRA, 0, 0x2f7c, 0),
  138. [DIV4_M1] = DIV4("vpu_clk", FRQCRB, 4, 0x2f7c, CLK_ENABLE_ON_INIT),
  139. };
  140. enum { DIV6_V, DIV6_FA, DIV6_FB, DIV6_I, DIV6_S, DIV6_NR };
  141. struct clk div6_clks[DIV6_NR] = {
  142. [DIV6_V] = SH_CLK_DIV6(&div3_clk, VCLKCR, 0),
  143. [DIV6_FA] = SH_CLK_DIV6(&div3_clk, FCLKACR, 0),
  144. [DIV6_FB] = SH_CLK_DIV6(&div3_clk, FCLKBCR, 0),
  145. [DIV6_I] = SH_CLK_DIV6(&div3_clk, IRDACLKCR, 0),
  146. [DIV6_S] = SH_CLK_DIV6(&div3_clk, SPUCLKCR, CLK_ENABLE_ON_INIT),
  147. };
  148. static struct clk mstp_clks[HWBLK_NR] = {
  149. SH_HWBLK_CLK(HWBLK_TLB, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  150. SH_HWBLK_CLK(HWBLK_IC, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  151. SH_HWBLK_CLK(HWBLK_OC, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  152. SH_HWBLK_CLK(HWBLK_RSMEM, &div4_clks[DIV4_B], CLK_ENABLE_ON_INIT),
  153. SH_HWBLK_CLK(HWBLK_ILMEM, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  154. SH_HWBLK_CLK(HWBLK_L2C, &div4_clks[DIV4_SH], CLK_ENABLE_ON_INIT),
  155. SH_HWBLK_CLK(HWBLK_FPU, &div4_clks[DIV4_I], CLK_ENABLE_ON_INIT),
  156. SH_HWBLK_CLK(HWBLK_INTC, &div4_clks[DIV4_P], CLK_ENABLE_ON_INIT),
  157. SH_HWBLK_CLK(HWBLK_DMAC0, &div4_clks[DIV4_B], 0),
  158. SH_HWBLK_CLK(HWBLK_SHYWAY, &div4_clks[DIV4_SH], CLK_ENABLE_ON_INIT),
  159. SH_HWBLK_CLK(HWBLK_HUDI, &div4_clks[DIV4_P], 0),
  160. SH_HWBLK_CLK(HWBLK_UBC, &div4_clks[DIV4_I], 0),
  161. SH_HWBLK_CLK(HWBLK_TMU0, &div4_clks[DIV4_P], 0),
  162. SH_HWBLK_CLK(HWBLK_CMT, &r_clk, 0),
  163. SH_HWBLK_CLK(HWBLK_RWDT, &r_clk, 0),
  164. SH_HWBLK_CLK(HWBLK_DMAC1, &div4_clks[DIV4_B], 0),
  165. SH_HWBLK_CLK(HWBLK_TMU1, &div4_clks[DIV4_P], 0),
  166. SH_HWBLK_CLK(HWBLK_SCIF0, &div4_clks[DIV4_P], 0),
  167. SH_HWBLK_CLK(HWBLK_SCIF1, &div4_clks[DIV4_P], 0),
  168. SH_HWBLK_CLK(HWBLK_SCIF2, &div4_clks[DIV4_P], 0),
  169. SH_HWBLK_CLK(HWBLK_SCIF3, &div4_clks[DIV4_B], 0),
  170. SH_HWBLK_CLK(HWBLK_SCIF4, &div4_clks[DIV4_B], 0),
  171. SH_HWBLK_CLK(HWBLK_SCIF5, &div4_clks[DIV4_B], 0),
  172. SH_HWBLK_CLK(HWBLK_MSIOF0, &div4_clks[DIV4_B], 0),
  173. SH_HWBLK_CLK(HWBLK_MSIOF1, &div4_clks[DIV4_B], 0),
  174. SH_HWBLK_CLK(HWBLK_KEYSC, &r_clk, 0),
  175. SH_HWBLK_CLK(HWBLK_RTC, &r_clk, 0),
  176. SH_HWBLK_CLK(HWBLK_IIC0, &div4_clks[DIV4_P], 0),
  177. SH_HWBLK_CLK(HWBLK_IIC1, &div4_clks[DIV4_P], 0),
  178. SH_HWBLK_CLK(HWBLK_MMC, &div4_clks[DIV4_B], 0),
  179. SH_HWBLK_CLK(HWBLK_ETHER, &div4_clks[DIV4_B], 0),
  180. SH_HWBLK_CLK(HWBLK_ATAPI, &div4_clks[DIV4_B], 0),
  181. SH_HWBLK_CLK(HWBLK_TPU, &div4_clks[DIV4_B], 0),
  182. SH_HWBLK_CLK(HWBLK_IRDA, &div4_clks[DIV4_P], 0),
  183. SH_HWBLK_CLK(HWBLK_TSIF, &div4_clks[DIV4_B], 0),
  184. SH_HWBLK_CLK(HWBLK_USB1, &div4_clks[DIV4_B], 0),
  185. SH_HWBLK_CLK(HWBLK_USB0, &div4_clks[DIV4_B], 0),
  186. SH_HWBLK_CLK(HWBLK_2DG, &div4_clks[DIV4_B], 0),
  187. SH_HWBLK_CLK(HWBLK_SDHI0, &div4_clks[DIV4_B], 0),
  188. SH_HWBLK_CLK(HWBLK_SDHI1, &div4_clks[DIV4_B], 0),
  189. SH_HWBLK_CLK(HWBLK_VEU1, &div4_clks[DIV4_B], 0),
  190. SH_HWBLK_CLK(HWBLK_CEU1, &div4_clks[DIV4_B], 0),
  191. SH_HWBLK_CLK(HWBLK_BEU1, &div4_clks[DIV4_B], 0),
  192. SH_HWBLK_CLK(HWBLK_2DDMAC, &div4_clks[DIV4_SH], 0),
  193. SH_HWBLK_CLK(HWBLK_SPU, &div4_clks[DIV4_B], 0),
  194. SH_HWBLK_CLK(HWBLK_JPU, &div4_clks[DIV4_B], 0),
  195. SH_HWBLK_CLK(HWBLK_VOU, &div4_clks[DIV4_B], 0),
  196. SH_HWBLK_CLK(HWBLK_BEU0, &div4_clks[DIV4_B], 0),
  197. SH_HWBLK_CLK(HWBLK_CEU0, &div4_clks[DIV4_B], 0),
  198. SH_HWBLK_CLK(HWBLK_VEU0, &div4_clks[DIV4_B], 0),
  199. SH_HWBLK_CLK(HWBLK_VPU, &div4_clks[DIV4_B], 0),
  200. SH_HWBLK_CLK(HWBLK_LCDC, &div4_clks[DIV4_B], 0),
  201. };
  202. #define CLKDEV_CON_ID(_id, _clk) { .con_id = _id, .clk = _clk }
  203. static struct clk_lookup lookups[] = {
  204. /* DIV4 clocks */
  205. CLKDEV_CON_ID("cpu_clk", &div4_clks[DIV4_I]),
  206. CLKDEV_CON_ID("shyway_clk", &div4_clks[DIV4_SH]),
  207. CLKDEV_CON_ID("bus_clk", &div4_clks[DIV4_B]),
  208. CLKDEV_CON_ID("peripheral_clk", &div4_clks[DIV4_P]),
  209. CLKDEV_CON_ID("vpu_clk", &div4_clks[DIV4_M1]),
  210. /* DIV6 clocks */
  211. CLKDEV_CON_ID("video_clk", &div6_clks[DIV6_V]),
  212. CLKDEV_CON_ID("fsia_clk", &div6_clks[DIV6_FA]),
  213. CLKDEV_CON_ID("fsib_clk", &div6_clks[DIV6_FB]),
  214. CLKDEV_CON_ID("irda_clk", &div6_clks[DIV6_I]),
  215. CLKDEV_CON_ID("spu_clk", &div6_clks[DIV6_S]),
  216. /* MSTP clocks */
  217. CLKDEV_CON_ID("tlb0", &mstp_clks[HWBLK_TLB]),
  218. CLKDEV_CON_ID("ic0", &mstp_clks[HWBLK_IC]),
  219. CLKDEV_CON_ID("oc0", &mstp_clks[HWBLK_OC]),
  220. CLKDEV_CON_ID("rs0", &mstp_clks[HWBLK_RSMEM]),
  221. CLKDEV_CON_ID("ilmem0", &mstp_clks[HWBLK_ILMEM]),
  222. CLKDEV_CON_ID("l2c0", &mstp_clks[HWBLK_L2C]),
  223. CLKDEV_CON_ID("fpu0", &mstp_clks[HWBLK_FPU]),
  224. CLKDEV_CON_ID("intc0", &mstp_clks[HWBLK_INTC]),
  225. CLKDEV_CON_ID("dmac0", &mstp_clks[HWBLK_DMAC0]),
  226. CLKDEV_CON_ID("sh0", &mstp_clks[HWBLK_SHYWAY]),
  227. CLKDEV_CON_ID("hudi0", &mstp_clks[HWBLK_HUDI]),
  228. CLKDEV_CON_ID("ubc0", &mstp_clks[HWBLK_UBC]),
  229. {
  230. /* TMU0 */
  231. .dev_id = "sh_tmu.0",
  232. .con_id = "tmu_fck",
  233. .clk = &mstp_clks[HWBLK_TMU0],
  234. }, {
  235. /* TMU1 */
  236. .dev_id = "sh_tmu.1",
  237. .con_id = "tmu_fck",
  238. .clk = &mstp_clks[HWBLK_TMU0],
  239. }, {
  240. /* TMU2 */
  241. .dev_id = "sh_tmu.2",
  242. .con_id = "tmu_fck",
  243. .clk = &mstp_clks[HWBLK_TMU0],
  244. }, {
  245. /* TMU3 */
  246. .dev_id = "sh_tmu.3",
  247. .con_id = "tmu_fck",
  248. .clk = &mstp_clks[HWBLK_TMU1],
  249. },
  250. CLKDEV_CON_ID("cmt_fck", &mstp_clks[HWBLK_CMT]),
  251. CLKDEV_CON_ID("rwdt0", &mstp_clks[HWBLK_RWDT]),
  252. CLKDEV_CON_ID("dmac1", &mstp_clks[HWBLK_DMAC1]),
  253. {
  254. /* TMU4 */
  255. .dev_id = "sh_tmu.4",
  256. .con_id = "tmu_fck",
  257. .clk = &mstp_clks[HWBLK_TMU1],
  258. }, {
  259. /* TMU5 */
  260. .dev_id = "sh_tmu.5",
  261. .con_id = "tmu_fck",
  262. .clk = &mstp_clks[HWBLK_TMU1],
  263. }, {
  264. /* SCIF0 */
  265. .dev_id = "sh-sci.0",
  266. .con_id = "sci_fck",
  267. .clk = &mstp_clks[HWBLK_SCIF0],
  268. }, {
  269. /* SCIF1 */
  270. .dev_id = "sh-sci.1",
  271. .con_id = "sci_fck",
  272. .clk = &mstp_clks[HWBLK_SCIF1],
  273. }, {
  274. /* SCIF2 */
  275. .dev_id = "sh-sci.2",
  276. .con_id = "sci_fck",
  277. .clk = &mstp_clks[HWBLK_SCIF2],
  278. }, {
  279. /* SCIF3 */
  280. .dev_id = "sh-sci.3",
  281. .con_id = "sci_fck",
  282. .clk = &mstp_clks[HWBLK_SCIF3],
  283. }, {
  284. /* SCIF4 */
  285. .dev_id = "sh-sci.4",
  286. .con_id = "sci_fck",
  287. .clk = &mstp_clks[HWBLK_SCIF4],
  288. }, {
  289. /* SCIF5 */
  290. .dev_id = "sh-sci.5",
  291. .con_id = "sci_fck",
  292. .clk = &mstp_clks[HWBLK_SCIF5],
  293. },
  294. CLKDEV_CON_ID("msiof0", &mstp_clks[HWBLK_MSIOF0]),
  295. CLKDEV_CON_ID("msiof1", &mstp_clks[HWBLK_MSIOF1]),
  296. CLKDEV_CON_ID("keysc0", &mstp_clks[HWBLK_KEYSC]),
  297. CLKDEV_CON_ID("rtc0", &mstp_clks[HWBLK_RTC]),
  298. CLKDEV_CON_ID("i2c0", &mstp_clks[HWBLK_IIC0]),
  299. CLKDEV_CON_ID("i2c1", &mstp_clks[HWBLK_IIC1]),
  300. CLKDEV_CON_ID("mmc0", &mstp_clks[HWBLK_MMC]),
  301. CLKDEV_CON_ID("eth0", &mstp_clks[HWBLK_ETHER]),
  302. CLKDEV_CON_ID("atapi0", &mstp_clks[HWBLK_ATAPI]),
  303. CLKDEV_CON_ID("tpu0", &mstp_clks[HWBLK_TPU]),
  304. CLKDEV_CON_ID("irda0", &mstp_clks[HWBLK_IRDA]),
  305. CLKDEV_CON_ID("tsif0", &mstp_clks[HWBLK_TSIF]),
  306. CLKDEV_CON_ID("usb1", &mstp_clks[HWBLK_USB1]),
  307. CLKDEV_CON_ID("usb0", &mstp_clks[HWBLK_USB0]),
  308. CLKDEV_CON_ID("2dg0", &mstp_clks[HWBLK_2DG]),
  309. CLKDEV_CON_ID("sdhi0", &mstp_clks[HWBLK_SDHI0]),
  310. CLKDEV_CON_ID("sdhi1", &mstp_clks[HWBLK_SDHI1]),
  311. CLKDEV_CON_ID("veu1", &mstp_clks[HWBLK_VEU1]),
  312. CLKDEV_CON_ID("ceu1", &mstp_clks[HWBLK_CEU1]),
  313. CLKDEV_CON_ID("beu1", &mstp_clks[HWBLK_BEU1]),
  314. CLKDEV_CON_ID("2ddmac0", &mstp_clks[HWBLK_2DDMAC]),
  315. CLKDEV_CON_ID("spu0", &mstp_clks[HWBLK_SPU]),
  316. CLKDEV_CON_ID("jpu0", &mstp_clks[HWBLK_JPU]),
  317. CLKDEV_CON_ID("vou0", &mstp_clks[HWBLK_VOU]),
  318. CLKDEV_CON_ID("beu0", &mstp_clks[HWBLK_BEU0]),
  319. CLKDEV_CON_ID("ceu0", &mstp_clks[HWBLK_CEU0]),
  320. CLKDEV_CON_ID("veu0", &mstp_clks[HWBLK_VEU0]),
  321. CLKDEV_CON_ID("vpu0", &mstp_clks[HWBLK_VPU]),
  322. CLKDEV_CON_ID("lcdc0", &mstp_clks[HWBLK_LCDC]),
  323. };
  324. int __init arch_clk_init(void)
  325. {
  326. int k, ret = 0;
  327. /* autodetect extal or fll configuration */
  328. if (__raw_readl(PLLCR) & 0x1000)
  329. pll_clk.parent = &fll_clk;
  330. else
  331. pll_clk.parent = &extal_clk;
  332. for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++)
  333. ret = clk_register(main_clks[k]);
  334. clkdev_add_table(lookups, ARRAY_SIZE(lookups));
  335. if (!ret)
  336. ret = sh_clk_div4_register(div4_clks, DIV4_NR, &div4_table);
  337. if (!ret)
  338. ret = sh_clk_div6_register(div6_clks, DIV6_NR);
  339. if (!ret)
  340. ret = sh_hwblk_clk_register(mstp_clks, HWBLK_NR);
  341. return ret;
  342. }