clock-sh7722.c 14 KB

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