clock-sh7722.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * arch/sh/kernel/cpu/sh4a/clock-sh7722.c
  3. *
  4. * SH7722 support for the clock framework
  5. *
  6. * Copyright (c) 2006-2007 Nomad Global Solutions Inc
  7. * Based on code for sh7343 by Paul Mundt
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/io.h>
  16. #include <linux/errno.h>
  17. #include <asm/clock.h>
  18. #include <asm/freq.h>
  19. #define SH7722_PLL_FREQ (32000000/8)
  20. #define N (-1)
  21. #define NM (-2)
  22. #define ROUND_NEAREST 0
  23. #define ROUND_DOWN -1
  24. #define ROUND_UP +1
  25. static int adjust_algos[][3] = {
  26. {}, /* NO_CHANGE */
  27. { NM, N, 1 }, /* N:1, N:1 */
  28. { 3, 2, 2 }, /* 3:2:2 */
  29. { 5, 2, 2 }, /* 5:2:2 */
  30. { N, 1, 1 }, /* N:1:1 */
  31. { N, 1 }, /* N:1 */
  32. { N, 1 }, /* N:1 */
  33. { 3, 2 },
  34. { 4, 3 },
  35. { 5, 4 },
  36. { N, 1 }
  37. };
  38. static unsigned long adjust_pair_of_clocks(unsigned long r1, unsigned long r2,
  39. int m1, int m2, int round_flag)
  40. {
  41. unsigned long rem, div;
  42. int the_one = 0;
  43. pr_debug( "Actual values: r1 = %ld\n", r1);
  44. pr_debug( "...............r2 = %ld\n", r2);
  45. if (m1 == m2) {
  46. r2 = r1;
  47. pr_debug( "setting equal rates: r2 now %ld\n", r2);
  48. } else if ((m2 == N && m1 == 1) ||
  49. (m2 == NM && m1 == N)) { /* N:1 or NM:N */
  50. pr_debug( "Setting rates as 1:N (N:N*M)\n");
  51. rem = r2 % r1;
  52. pr_debug( "...remainder = %ld\n", rem);
  53. if (rem) {
  54. div = r2 / r1;
  55. pr_debug( "...div = %ld\n", div);
  56. switch (round_flag) {
  57. case ROUND_NEAREST:
  58. the_one = rem >= r1/2 ? 1 : 0; break;
  59. case ROUND_UP:
  60. the_one = 1; break;
  61. case ROUND_DOWN:
  62. the_one = 0; break;
  63. }
  64. r2 = r1 * (div + the_one);
  65. pr_debug( "...setting r2 to %ld\n", r2);
  66. }
  67. } else if ((m2 == 1 && m1 == N) ||
  68. (m2 == N && m1 == NM)) { /* 1:N or N:NM */
  69. pr_debug( "Setting rates as N:1 (N*M:N)\n");
  70. rem = r1 % r2;
  71. pr_debug( "...remainder = %ld\n", rem);
  72. if (rem) {
  73. div = r1 / r2;
  74. pr_debug( "...div = %ld\n", div);
  75. switch (round_flag) {
  76. case ROUND_NEAREST:
  77. the_one = rem > r2/2 ? 1 : 0; break;
  78. case ROUND_UP:
  79. the_one = 0; break;
  80. case ROUND_DOWN:
  81. the_one = 1; break;
  82. }
  83. r2 = r1 / (div + the_one);
  84. pr_debug( "...setting r2 to %ld\n", r2);
  85. }
  86. } else { /* value:value */
  87. pr_debug( "Setting rates as %d:%d\n", m1, m2);
  88. div = r1 / m1;
  89. r2 = div * m2;
  90. pr_debug( "...div = %ld\n", div);
  91. pr_debug( "...setting r2 to %ld\n", r2);
  92. }
  93. return r2;
  94. }
  95. static void adjust_clocks(int originate, int *l, unsigned long v[],
  96. int n_in_line)
  97. {
  98. int x;
  99. pr_debug( "Go down from %d...\n", originate);
  100. /* go up recalculation clocks */
  101. for (x = originate; x>0; x -- )
  102. v[x-1] = adjust_pair_of_clocks(v[x], v[x-1],
  103. l[x], l[x-1],
  104. ROUND_UP);
  105. pr_debug( "Go up from %d...\n", originate);
  106. /* go down recalculation clocks */
  107. for (x = originate; x<n_in_line - 1; x ++ )
  108. v[x+1] = adjust_pair_of_clocks(v[x], v[x+1],
  109. l[x], l[x+1],
  110. ROUND_UP);
  111. }
  112. /*
  113. * SH7722 uses a common set of multipliers and divisors, so this
  114. * is quite simple..
  115. */
  116. /*
  117. * Instead of having two separate multipliers/divisors set, like this:
  118. *
  119. * static int multipliers[] = { 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  120. * static int divisors[] = { 1, 3, 2, 5, 3, 4, 5, 6, 8, 10, 12, 16, 20 };
  121. *
  122. * I created the divisors2 array, which is used to calculate rate like
  123. * rate = parent * 2 / divisors2[ divisor ];
  124. */
  125. static int divisors2[] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40 };
  126. static void master_clk_init(struct clk *clk)
  127. {
  128. clk_set_rate(clk, clk_get_rate(clk));
  129. }
  130. static void master_clk_recalc(struct clk *clk)
  131. {
  132. unsigned long frqcr = ctrl_inl(FRQCR);
  133. clk->rate = CONFIG_SH_PCLK_FREQ * (1 + (frqcr >> 24 & 0xF));
  134. }
  135. static int master_clk_setrate(struct clk *clk, unsigned long rate, int id)
  136. {
  137. int div = rate / SH7722_PLL_FREQ;
  138. int master_divs[] = { 2, 3, 4, 6, 8, 16 };
  139. int index;
  140. unsigned long frqcr;
  141. if (rate < SH7722_PLL_FREQ * 2)
  142. return -EINVAL;
  143. for (index = 1; index < ARRAY_SIZE(master_divs); index++)
  144. if (div >= master_divs[index - 1] && div < master_divs[index])
  145. break;
  146. if (index >= ARRAY_SIZE(master_divs))
  147. index = ARRAY_SIZE(master_divs);
  148. div = master_divs[index - 1];
  149. frqcr = ctrl_inl(FRQCR);
  150. frqcr &= ~(0xF << 24);
  151. frqcr |= ( (div-1) << 24);
  152. ctrl_outl(frqcr, FRQCR);
  153. return 0;
  154. }
  155. static struct clk_ops sh7722_master_clk_ops = {
  156. .init = master_clk_init,
  157. .recalc = master_clk_recalc,
  158. .set_rate = master_clk_setrate,
  159. };
  160. struct frqcr_context {
  161. unsigned mask;
  162. unsigned shift;
  163. };
  164. struct frqcr_context sh7722_get_clk_context(const char *name)
  165. {
  166. struct frqcr_context ctx = { 0, };
  167. if (!strcmp(name, "peripheral_clk")) {
  168. ctx.shift = 0;
  169. ctx.mask = 0xF;
  170. } else if (!strcmp(name, "sdram_clk")) {
  171. ctx.shift = 4;
  172. ctx.mask = 0xF;
  173. } else if (!strcmp(name, "bus_clk")) {
  174. ctx.shift = 8;
  175. ctx.mask = 0xF;
  176. } else if (!strcmp(name, "sh_clk")) {
  177. ctx.shift = 12;
  178. ctx.mask = 0xF;
  179. } else if (!strcmp(name, "umem_clk")) {
  180. ctx.shift = 16;
  181. ctx.mask = 0xF;
  182. } else if (!strcmp(name, "cpu_clk")) {
  183. ctx.shift = 20;
  184. ctx.mask = 7;
  185. }
  186. return ctx;
  187. }
  188. /**
  189. * sh7722_find_divisors - find divisor for setting rate
  190. *
  191. * All sh7722 clocks use the same set of multipliers/divisors. This function
  192. * chooses correct divisor to set the rate of clock with parent clock that
  193. * generates frequency of 'parent_rate'
  194. *
  195. * @parent_rate: rate of parent clock
  196. * @rate: requested rate to be set
  197. */
  198. static int sh7722_find_divisors(unsigned long parent_rate, unsigned rate)
  199. {
  200. unsigned div2 = parent_rate * 2 / rate;
  201. int index;
  202. if (rate > parent_rate)
  203. return -EINVAL;
  204. for (index = 1; index < ARRAY_SIZE(divisors2); index++) {
  205. if (div2 > divisors2[index] && div2 <= divisors2[index])
  206. break;
  207. }
  208. if (index >= ARRAY_SIZE(divisors2))
  209. index = ARRAY_SIZE(divisors2) - 1;
  210. return divisors2[index];
  211. }
  212. static void sh7722_frqcr_recalc(struct clk *clk)
  213. {
  214. struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
  215. unsigned long frqcr = ctrl_inl(FRQCR);
  216. int index;
  217. index = (frqcr >> ctx.shift) & ctx.mask;
  218. clk->rate = clk->parent->rate * 2 / divisors2[index];
  219. }
  220. static int sh7722_frqcr_set_rate(struct clk *clk, unsigned long rate,
  221. int algo_id)
  222. {
  223. struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
  224. unsigned long parent_rate = clk->parent->rate;
  225. int div;
  226. unsigned long frqcr;
  227. int err = 0;
  228. /* pretty invalid */
  229. if (parent_rate < rate)
  230. return -EINVAL;
  231. /* look for multiplier/divisor pair */
  232. div = sh7722_find_divisors(parent_rate, rate);
  233. if (div<0)
  234. return div;
  235. /* calculate new value of clock rate */
  236. clk->rate = parent_rate * 2 / div;
  237. frqcr = ctrl_inl(FRQCR);
  238. /* FIXME: adjust as algo_id specifies */
  239. if (algo_id != NO_CHANGE) {
  240. int originator;
  241. char *algo_group_1[] = { "cpu_clk", "umem_clk", "sh_clk" };
  242. char *algo_group_2[] = { "sh_clk", "bus_clk" };
  243. char *algo_group_3[] = { "sh_clk", "sdram_clk" };
  244. char *algo_group_4[] = { "bus_clk", "peripheral_clk" };
  245. char *algo_group_5[] = { "cpu_clk", "peripheral_clk" };
  246. char **algo_current = NULL;
  247. /* 3 is the maximum number of clocks in relation */
  248. struct clk *ck[3];
  249. unsigned long values[3]; /* the same comment as above */
  250. int part_length = -1;
  251. int i;
  252. /*
  253. * all the steps below only required if adjustion was
  254. * requested
  255. */
  256. if (algo_id == IUS_N1_N1 ||
  257. algo_id == IUS_322 ||
  258. algo_id == IUS_522 ||
  259. algo_id == IUS_N11) {
  260. algo_current = algo_group_1;
  261. part_length = 3;
  262. }
  263. if (algo_id == SB_N1) {
  264. algo_current = algo_group_2;
  265. part_length = 2;
  266. }
  267. if (algo_id == SB3_N1 ||
  268. algo_id == SB3_32 ||
  269. algo_id == SB3_43 ||
  270. algo_id == SB3_54) {
  271. algo_current = algo_group_3;
  272. part_length = 2;
  273. }
  274. if (algo_id == BP_N1) {
  275. algo_current = algo_group_4;
  276. part_length = 2;
  277. }
  278. if (algo_id == IP_N1) {
  279. algo_current = algo_group_5;
  280. part_length = 2;
  281. }
  282. if (!algo_current)
  283. goto incorrect_algo_id;
  284. originator = -1;
  285. for (i = 0; i < part_length; i ++ ) {
  286. if (originator >= 0 && !strcmp(clk->name,
  287. algo_current[i]))
  288. originator = i;
  289. ck[i] = clk_get(NULL, algo_current[i]);
  290. values[i] = clk_get_rate(ck[i]);
  291. }
  292. if (originator >= 0)
  293. adjust_clocks(originator, adjust_algos[algo_id],
  294. values, part_length);
  295. for (i = 0; i < part_length; i ++ ) {
  296. struct frqcr_context part_ctx;
  297. int part_div;
  298. if (likely(!err)) {
  299. part_div = sh7722_find_divisors(parent_rate,
  300. rate);
  301. if (part_div > 0) {
  302. part_ctx = sh7722_get_clk_context(
  303. ck[i]->name);
  304. frqcr &= ~(part_ctx.mask <<
  305. part_ctx.shift);
  306. frqcr |= part_div << part_ctx.shift;
  307. } else
  308. err = part_div;
  309. }
  310. ck[i]->ops->recalc(ck[i]);
  311. clk_put(ck[i]);
  312. }
  313. }
  314. /* was there any error during recalculation ? If so, bail out.. */
  315. if (unlikely(err!=0))
  316. goto out_err;
  317. /* clear FRQCR bits */
  318. frqcr &= ~(ctx.mask << ctx.shift);
  319. frqcr |= div << ctx.shift;
  320. /* ...and perform actual change */
  321. ctrl_outl(frqcr, FRQCR);
  322. return 0;
  323. incorrect_algo_id:
  324. return -EINVAL;
  325. out_err:
  326. return err;
  327. }
  328. static struct clk_ops sh7722_frqcr_clk_ops = {
  329. .recalc = sh7722_frqcr_recalc,
  330. .set_rate = sh7722_frqcr_set_rate,
  331. };
  332. /*
  333. * clock ops methods for SIU A/B and IrDA clock
  334. *
  335. */
  336. static int sh7722_siu_which(struct clk *clk)
  337. {
  338. if (!strcmp(clk->name, "siu_a_clk"))
  339. return 0;
  340. if (!strcmp(clk->name, "siu_b_clk"))
  341. return 1;
  342. if (!strcmp(clk->name, "irda_clk"))
  343. return 2;
  344. return -EINVAL;
  345. }
  346. static unsigned long sh7722_siu_regs[] = {
  347. [0] = SCLKACR,
  348. [1] = SCLKBCR,
  349. [2] = IrDACLKCR,
  350. };
  351. static int sh7722_siu_start_stop(struct clk *clk, int enable)
  352. {
  353. int siu = sh7722_siu_which(clk);
  354. unsigned long r;
  355. if (siu < 0)
  356. return siu;
  357. BUG_ON(siu > 2);
  358. r = ctrl_inl(sh7722_siu_regs[siu]);
  359. if (enable)
  360. ctrl_outl(r & ~(1 << 8), sh7722_siu_regs[siu]);
  361. else
  362. ctrl_outl(r | (1 << 8), sh7722_siu_regs[siu]);
  363. return 0;
  364. }
  365. static void sh7722_siu_enable(struct clk *clk)
  366. {
  367. sh7722_siu_start_stop(clk, 1);
  368. }
  369. static void sh7722_siu_disable(struct clk *clk)
  370. {
  371. sh7722_siu_start_stop(clk, 0);
  372. }
  373. static void sh7722_video_enable(struct clk *clk)
  374. {
  375. unsigned long r;
  376. r = ctrl_inl(VCLKCR);
  377. ctrl_outl( r & ~(1<<8), VCLKCR);
  378. }
  379. static void sh7722_video_disable(struct clk *clk)
  380. {
  381. unsigned long r;
  382. r = ctrl_inl(VCLKCR);
  383. ctrl_outl( r | (1<<8), VCLKCR);
  384. }
  385. static int sh7722_video_set_rate(struct clk *clk, unsigned long rate,
  386. int algo_id)
  387. {
  388. unsigned long r;
  389. r = ctrl_inl(VCLKCR);
  390. r &= ~0x3F;
  391. r |= ((clk->parent->rate / rate - 1) & 0x3F);
  392. ctrl_outl(r, VCLKCR);
  393. return 0;
  394. }
  395. static void sh7722_video_recalc(struct clk *clk)
  396. {
  397. unsigned long r;
  398. r = ctrl_inl(VCLKCR);
  399. clk->rate = clk->parent->rate / ((r & 0x3F) + 1);
  400. }
  401. static int sh7722_siu_set_rate(struct clk *clk, unsigned long rate, int algo_id)
  402. {
  403. int siu = sh7722_siu_which(clk);
  404. unsigned long r;
  405. int div;
  406. if (siu < 0)
  407. return siu;
  408. BUG_ON(siu > 2);
  409. r = ctrl_inl(sh7722_siu_regs[siu]);
  410. div = sh7722_find_divisors(clk->parent->rate, rate);
  411. if (div < 0)
  412. return div;
  413. r = (r & ~0xF) | div;
  414. ctrl_outl(r, sh7722_siu_regs[siu]);
  415. return 0;
  416. }
  417. static void sh7722_siu_recalc(struct clk *clk)
  418. {
  419. int siu = sh7722_siu_which(clk);
  420. unsigned long r;
  421. if (siu < 0)
  422. return /* siu */ ;
  423. BUG_ON(siu > 1);
  424. r = ctrl_inl(sh7722_siu_regs[siu]);
  425. clk->rate = clk->parent->rate * 2 / divisors2[r & 0xF];
  426. }
  427. static struct clk_ops sh7722_siu_clk_ops = {
  428. .recalc = sh7722_siu_recalc,
  429. .set_rate = sh7722_siu_set_rate,
  430. .enable = sh7722_siu_enable,
  431. .disable = sh7722_siu_disable,
  432. };
  433. static struct clk_ops sh7722_video_clk_ops = {
  434. .recalc = sh7722_video_recalc,
  435. .set_rate = sh7722_video_set_rate,
  436. .enable = sh7722_video_enable,
  437. .disable = sh7722_video_disable,
  438. };
  439. /*
  440. * and at last, clock definitions themselves
  441. */
  442. static struct clk sh7722_umem_clock = {
  443. .name = "umem_clk",
  444. .ops = &sh7722_frqcr_clk_ops,
  445. };
  446. static struct clk sh7722_sh_clock = {
  447. .name = "sh_clk",
  448. .ops = &sh7722_frqcr_clk_ops,
  449. };
  450. static struct clk sh7722_peripheral_clock = {
  451. .name = "peripheral_clk",
  452. .ops = &sh7722_frqcr_clk_ops,
  453. };
  454. static struct clk sh7722_sdram_clock = {
  455. .name = "sdram_clk",
  456. .ops = &sh7722_frqcr_clk_ops,
  457. };
  458. /*
  459. * these three clocks - SIU A, SIU B, IrDA - share the same clk_ops
  460. * methods of clk_ops determine which register they should access by
  461. * examining clk->name field
  462. */
  463. static struct clk sh7722_siu_a_clock = {
  464. .name = "siu_a_clk",
  465. .ops = &sh7722_siu_clk_ops,
  466. };
  467. static struct clk sh7722_siu_b_clock = {
  468. .name = "siu_b_clk",
  469. .ops = &sh7722_siu_clk_ops,
  470. };
  471. static struct clk sh7722_irda_clock = {
  472. .name = "irda_clk",
  473. .ops = &sh7722_siu_clk_ops,
  474. };
  475. static struct clk sh7722_video_clock = {
  476. .name = "video_clk",
  477. .ops = &sh7722_video_clk_ops,
  478. };
  479. static struct clk *sh7722_clocks[] = {
  480. &sh7722_umem_clock,
  481. &sh7722_sh_clock,
  482. &sh7722_peripheral_clock,
  483. &sh7722_sdram_clock,
  484. &sh7722_siu_a_clock,
  485. &sh7722_siu_b_clock,
  486. &sh7722_irda_clock,
  487. &sh7722_video_clock,
  488. };
  489. /*
  490. * init in order: master, module, bus, cpu
  491. */
  492. struct clk_ops *onchip_ops[] = {
  493. &sh7722_master_clk_ops,
  494. &sh7722_frqcr_clk_ops,
  495. &sh7722_frqcr_clk_ops,
  496. &sh7722_frqcr_clk_ops,
  497. };
  498. void __init
  499. arch_init_clk_ops(struct clk_ops **ops, int type)
  500. {
  501. BUG_ON(type < 0 || type > ARRAY_SIZE(onchip_ops));
  502. *ops = onchip_ops[type];
  503. }
  504. int __init sh7722_clock_init(void)
  505. {
  506. struct clk *master;
  507. int i;
  508. master = clk_get(NULL, "master_clk");
  509. for (i = 0; i < ARRAY_SIZE(sh7722_clocks); i++) {
  510. pr_debug( "Registering clock '%s'\n", sh7722_clocks[i]->name);
  511. sh7722_clocks[i]->parent = master;
  512. clk_register(sh7722_clocks[i]);
  513. }
  514. clk_put(master);
  515. return 0;
  516. }
  517. arch_initcall(sh7722_clock_init);