clock-sh7724.c 11 KB

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