clock-sh7722.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * arch/sh/kernel/cpu/sh4a/clock-sh7722.c
  3. *
  4. * SH7343, SH7722, SH7723 & SH7366 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 <linux/stringify.h>
  18. #include <asm/clock.h>
  19. #include <asm/freq.h>
  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. #if defined(CONFIG_CPU_SUBTYPE_SH7724)
  117. #define STCPLL(frqcr) ((((frqcr >> 24) & 0x3f) + 1) * 2)
  118. #else
  119. #define STCPLL(frqcr) (((frqcr >> 24) & 0x1f) + 1)
  120. #endif
  121. /*
  122. * Instead of having two separate multipliers/divisors set, like this:
  123. *
  124. * static int multipliers[] = { 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  125. * static int divisors[] = { 1, 3, 2, 5, 3, 4, 5, 6, 8, 10, 12, 16, 20 };
  126. *
  127. * I created the divisors2 array, which is used to calculate rate like
  128. * rate = parent * 2 / divisors2[ divisor ];
  129. */
  130. #if defined(CONFIG_CPU_SUBTYPE_SH7724)
  131. static int divisors2[] = { 4, 1, 8, 12, 16, 24, 32, 1, 48, 64, 72, 96, 1, 144 };
  132. #else
  133. static int divisors2[] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40 };
  134. #endif
  135. static unsigned long master_clk_recalc(struct clk *clk)
  136. {
  137. unsigned frqcr = ctrl_inl(FRQCR);
  138. return CONFIG_SH_PCLK_FREQ * STCPLL(frqcr);
  139. }
  140. static void master_clk_init(struct clk *clk)
  141. {
  142. clk->parent = NULL;
  143. clk->flags |= CLK_RATE_PROPAGATES;
  144. clk->rate = CONFIG_SH_PCLK_FREQ;
  145. master_clk_recalc(clk);
  146. }
  147. static unsigned long module_clk_recalc(struct clk *clk)
  148. {
  149. unsigned long frqcr = ctrl_inl(FRQCR);
  150. return clk->parent->rate / STCPLL(frqcr);
  151. }
  152. #if defined(CONFIG_CPU_SUBTYPE_SH7724)
  153. #define MASTERDIVS { 12, 16, 24, 30, 32, 36, 48 }
  154. #define STCMASK 0x3f
  155. #define DIVCALC(div) (div/2-1)
  156. #define FRQCRKICK 0x80000000
  157. #elif defined(CONFIG_CPU_SUBTYPE_SH7723)
  158. #define MASTERDIVS { 6, 8, 12, 16 }
  159. #define STCMASK 0x1f
  160. #define DIVCALC(div) (div-1)
  161. #define FRQCRKICK 0x00000000
  162. #else
  163. #define MASTERDIVS { 2, 3, 4, 6, 8, 16 }
  164. #define STCMASK 0x1f
  165. #define DIVCALC(div) (div-1)
  166. #define FRQCRKICK 0x00000000
  167. #endif
  168. static int master_clk_setrate(struct clk *clk, unsigned long rate, int id)
  169. {
  170. int div = rate / clk->rate;
  171. int master_divs[] = MASTERDIVS;
  172. int index;
  173. unsigned long frqcr;
  174. for (index = 1; index < ARRAY_SIZE(master_divs); index++)
  175. if (div >= master_divs[index - 1] && div < master_divs[index])
  176. break;
  177. if (index >= ARRAY_SIZE(master_divs))
  178. index = ARRAY_SIZE(master_divs);
  179. div = master_divs[index - 1];
  180. frqcr = ctrl_inl(FRQCR);
  181. frqcr &= ~(STCMASK << 24);
  182. frqcr |= (DIVCALC(div) << 24);
  183. frqcr |= FRQCRKICK;
  184. ctrl_outl(frqcr, FRQCR);
  185. return 0;
  186. }
  187. static struct clk_ops sh7722_master_clk_ops = {
  188. .init = master_clk_init,
  189. .recalc = master_clk_recalc,
  190. .set_rate = master_clk_setrate,
  191. };
  192. static struct clk_ops sh7722_module_clk_ops = {
  193. .recalc = module_clk_recalc,
  194. };
  195. struct frqcr_context {
  196. unsigned mask;
  197. unsigned shift;
  198. };
  199. struct frqcr_context sh7722_get_clk_context(const char *name)
  200. {
  201. struct frqcr_context ctx = { 0, };
  202. if (!strcmp(name, "peripheral_clk")) {
  203. ctx.shift = 0;
  204. ctx.mask = 0xF;
  205. } else if (!strcmp(name, "sdram_clk")) {
  206. ctx.shift = 4;
  207. ctx.mask = 0xF;
  208. } else if (!strcmp(name, "bus_clk")) {
  209. ctx.shift = 8;
  210. ctx.mask = 0xF;
  211. } else if (!strcmp(name, "sh_clk")) {
  212. ctx.shift = 12;
  213. ctx.mask = 0xF;
  214. } else if (!strcmp(name, "umem_clk")) {
  215. ctx.shift = 16;
  216. ctx.mask = 0xF;
  217. } else if (!strcmp(name, "cpu_clk")) {
  218. ctx.shift = 20;
  219. ctx.mask = 7;
  220. }
  221. return ctx;
  222. }
  223. /**
  224. * sh7722_find_div_index - find divisor for setting rate
  225. *
  226. * All sh7722 clocks use the same set of multipliers/divisors. This function
  227. * chooses correct divisor to set the rate of clock with parent clock that
  228. * generates frequency of 'parent_rate'
  229. *
  230. * @parent_rate: rate of parent clock
  231. * @rate: requested rate to be set
  232. */
  233. static int sh7722_find_div_index(unsigned long parent_rate, unsigned rate)
  234. {
  235. unsigned div2 = parent_rate * 2 / rate;
  236. int index;
  237. if (rate > parent_rate)
  238. return -EINVAL;
  239. for (index = 1; index < ARRAY_SIZE(divisors2); index++) {
  240. if (div2 > divisors2[index - 1] && div2 <= divisors2[index])
  241. break;
  242. }
  243. if (index >= ARRAY_SIZE(divisors2))
  244. index = ARRAY_SIZE(divisors2) - 1;
  245. return index;
  246. }
  247. static unsigned long sh7722_frqcr_recalc(struct clk *clk)
  248. {
  249. struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
  250. unsigned long frqcr = ctrl_inl(FRQCR);
  251. int index;
  252. index = (frqcr >> ctx.shift) & ctx.mask;
  253. return clk->parent->rate * 2 / divisors2[index];
  254. }
  255. static int sh7722_frqcr_set_rate(struct clk *clk, unsigned long rate,
  256. int algo_id)
  257. {
  258. struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
  259. unsigned long parent_rate = clk->parent->rate;
  260. int div;
  261. unsigned long frqcr;
  262. int err = 0;
  263. /* pretty invalid */
  264. if (parent_rate < rate)
  265. return -EINVAL;
  266. /* look for multiplier/divisor pair */
  267. div = sh7722_find_div_index(parent_rate, rate);
  268. if (div<0)
  269. return div;
  270. /* calculate new value of clock rate */
  271. clk->rate = parent_rate * 2 / divisors2[div];
  272. frqcr = ctrl_inl(FRQCR);
  273. /* FIXME: adjust as algo_id specifies */
  274. if (algo_id != NO_CHANGE) {
  275. int originator;
  276. char *algo_group_1[] = { "cpu_clk", "umem_clk", "sh_clk" };
  277. char *algo_group_2[] = { "sh_clk", "bus_clk" };
  278. char *algo_group_3[] = { "sh_clk", "sdram_clk" };
  279. char *algo_group_4[] = { "bus_clk", "peripheral_clk" };
  280. char *algo_group_5[] = { "cpu_clk", "peripheral_clk" };
  281. char **algo_current = NULL;
  282. /* 3 is the maximum number of clocks in relation */
  283. struct clk *ck[3];
  284. unsigned long values[3]; /* the same comment as above */
  285. int part_length = -1;
  286. int i;
  287. /*
  288. * all the steps below only required if adjustion was
  289. * requested
  290. */
  291. if (algo_id == IUS_N1_N1 ||
  292. algo_id == IUS_322 ||
  293. algo_id == IUS_522 ||
  294. algo_id == IUS_N11) {
  295. algo_current = algo_group_1;
  296. part_length = 3;
  297. }
  298. if (algo_id == SB_N1) {
  299. algo_current = algo_group_2;
  300. part_length = 2;
  301. }
  302. if (algo_id == SB3_N1 ||
  303. algo_id == SB3_32 ||
  304. algo_id == SB3_43 ||
  305. algo_id == SB3_54) {
  306. algo_current = algo_group_3;
  307. part_length = 2;
  308. }
  309. if (algo_id == BP_N1) {
  310. algo_current = algo_group_4;
  311. part_length = 2;
  312. }
  313. if (algo_id == IP_N1) {
  314. algo_current = algo_group_5;
  315. part_length = 2;
  316. }
  317. if (!algo_current)
  318. goto incorrect_algo_id;
  319. originator = -1;
  320. for (i = 0; i < part_length; i ++ ) {
  321. if (originator >= 0 && !strcmp(clk->name,
  322. algo_current[i]))
  323. originator = i;
  324. ck[i] = clk_get(NULL, algo_current[i]);
  325. values[i] = clk_get_rate(ck[i]);
  326. }
  327. if (originator >= 0)
  328. adjust_clocks(originator, adjust_algos[algo_id],
  329. values, part_length);
  330. for (i = 0; i < part_length; i ++ ) {
  331. struct frqcr_context part_ctx;
  332. int part_div;
  333. if (likely(!err)) {
  334. part_div = sh7722_find_div_index(parent_rate,
  335. rate);
  336. if (part_div > 0) {
  337. part_ctx = sh7722_get_clk_context(
  338. ck[i]->name);
  339. frqcr &= ~(part_ctx.mask <<
  340. part_ctx.shift);
  341. frqcr |= part_div << part_ctx.shift;
  342. } else
  343. err = part_div;
  344. }
  345. ck[i]->ops->recalc(ck[i]);
  346. clk_put(ck[i]);
  347. }
  348. }
  349. /* was there any error during recalculation ? If so, bail out.. */
  350. if (unlikely(err!=0))
  351. goto out_err;
  352. /* clear FRQCR bits */
  353. frqcr &= ~(ctx.mask << ctx.shift);
  354. frqcr |= div << ctx.shift;
  355. frqcr |= FRQCRKICK;
  356. /* ...and perform actual change */
  357. ctrl_outl(frqcr, FRQCR);
  358. return 0;
  359. incorrect_algo_id:
  360. return -EINVAL;
  361. out_err:
  362. return err;
  363. }
  364. static long sh7722_frqcr_round_rate(struct clk *clk, unsigned long rate)
  365. {
  366. unsigned long parent_rate = clk->parent->rate;
  367. int div;
  368. /* look for multiplier/divisor pair */
  369. div = sh7722_find_div_index(parent_rate, rate);
  370. if (div < 0)
  371. return clk->rate;
  372. /* calculate new value of clock rate */
  373. return parent_rate * 2 / divisors2[div];
  374. }
  375. static struct clk_ops sh7722_frqcr_clk_ops = {
  376. .recalc = sh7722_frqcr_recalc,
  377. .set_rate = sh7722_frqcr_set_rate,
  378. .round_rate = sh7722_frqcr_round_rate,
  379. };
  380. /*
  381. * clock ops methods for SIU A/B and IrDA clock
  382. */
  383. #ifndef CONFIG_CPU_SUBTYPE_SH7343
  384. static int sh7722_siu_set_rate(struct clk *clk, unsigned long rate, int algo_id)
  385. {
  386. unsigned long r;
  387. int div;
  388. r = ctrl_inl(clk->arch_flags);
  389. div = sh7722_find_div_index(clk->parent->rate, rate);
  390. if (div < 0)
  391. return div;
  392. r = (r & ~0xF) | div;
  393. ctrl_outl(r, clk->arch_flags);
  394. return 0;
  395. }
  396. static unsigned long sh7722_siu_recalc(struct clk *clk)
  397. {
  398. unsigned long r;
  399. r = ctrl_inl(clk->arch_flags);
  400. return clk->parent->rate * 2 / divisors2[r & 0xF];
  401. }
  402. static int sh7722_siu_start_stop(struct clk *clk, int enable)
  403. {
  404. unsigned long r;
  405. r = ctrl_inl(clk->arch_flags);
  406. if (enable)
  407. ctrl_outl(r & ~(1 << 8), clk->arch_flags);
  408. else
  409. ctrl_outl(r | (1 << 8), clk->arch_flags);
  410. return 0;
  411. }
  412. static void sh7722_siu_enable(struct clk *clk)
  413. {
  414. sh7722_siu_start_stop(clk, 1);
  415. }
  416. static void sh7722_siu_disable(struct clk *clk)
  417. {
  418. sh7722_siu_start_stop(clk, 0);
  419. }
  420. static struct clk_ops sh7722_siu_clk_ops = {
  421. .recalc = sh7722_siu_recalc,
  422. .set_rate = sh7722_siu_set_rate,
  423. .enable = sh7722_siu_enable,
  424. .disable = sh7722_siu_disable,
  425. };
  426. #endif /* CONFIG_CPU_SUBTYPE_SH7343 */
  427. static void sh7722_video_enable(struct clk *clk)
  428. {
  429. unsigned long r;
  430. r = ctrl_inl(VCLKCR);
  431. ctrl_outl( r & ~(1<<8), VCLKCR);
  432. }
  433. static void sh7722_video_disable(struct clk *clk)
  434. {
  435. unsigned long r;
  436. r = ctrl_inl(VCLKCR);
  437. ctrl_outl( r | (1<<8), VCLKCR);
  438. }
  439. static int sh7722_video_set_rate(struct clk *clk, unsigned long rate,
  440. int algo_id)
  441. {
  442. unsigned long r;
  443. r = ctrl_inl(VCLKCR);
  444. r &= ~0x3F;
  445. r |= ((clk->parent->rate / rate - 1) & 0x3F);
  446. ctrl_outl(r, VCLKCR);
  447. return 0;
  448. }
  449. static unsigned long sh7722_video_recalc(struct clk *clk)
  450. {
  451. unsigned long r;
  452. r = ctrl_inl(VCLKCR);
  453. return clk->parent->rate / ((r & 0x3F) + 1);
  454. }
  455. static struct clk_ops sh7722_video_clk_ops = {
  456. .recalc = sh7722_video_recalc,
  457. .set_rate = sh7722_video_set_rate,
  458. .enable = sh7722_video_enable,
  459. .disable = sh7722_video_disable,
  460. };
  461. /*
  462. * and at last, clock definitions themselves
  463. */
  464. static struct clk sh7722_umem_clock = {
  465. .name = "umem_clk",
  466. .ops = &sh7722_frqcr_clk_ops,
  467. .flags = CLK_RATE_PROPAGATES,
  468. };
  469. static struct clk sh7722_sh_clock = {
  470. .name = "sh_clk",
  471. .ops = &sh7722_frqcr_clk_ops,
  472. .flags = CLK_RATE_PROPAGATES,
  473. };
  474. static struct clk sh7722_peripheral_clock = {
  475. .name = "peripheral_clk",
  476. .ops = &sh7722_frqcr_clk_ops,
  477. .flags = CLK_RATE_PROPAGATES,
  478. };
  479. static struct clk sh7722_sdram_clock = {
  480. .name = "sdram_clk",
  481. .ops = &sh7722_frqcr_clk_ops,
  482. };
  483. static struct clk sh7722_r_clock = {
  484. .name = "r_clk",
  485. .rate = 32768,
  486. .flags = CLK_RATE_PROPAGATES,
  487. };
  488. #if !defined(CONFIG_CPU_SUBTYPE_SH7343) &&\
  489. !defined(CONFIG_CPU_SUBTYPE_SH7724)
  490. /*
  491. * these three clocks - SIU A, SIU B, IrDA - share the same clk_ops
  492. * methods of clk_ops determine which register they should access by
  493. * examining clk->name field
  494. */
  495. static struct clk sh7722_siu_a_clock = {
  496. .name = "siu_a_clk",
  497. .arch_flags = SCLKACR,
  498. .ops = &sh7722_siu_clk_ops,
  499. };
  500. static struct clk sh7722_siu_b_clock = {
  501. .name = "siu_b_clk",
  502. .arch_flags = SCLKBCR,
  503. .ops = &sh7722_siu_clk_ops,
  504. };
  505. #endif /* CONFIG_CPU_SUBTYPE_SH7343, SH7724 */
  506. #if defined(CONFIG_CPU_SUBTYPE_SH7722) ||\
  507. defined(CONFIG_CPU_SUBTYPE_SH7724)
  508. static struct clk sh7722_irda_clock = {
  509. .name = "irda_clk",
  510. .arch_flags = IrDACLKCR,
  511. .ops = &sh7722_siu_clk_ops,
  512. };
  513. #endif
  514. static struct clk sh7722_video_clock = {
  515. .name = "video_clk",
  516. .ops = &sh7722_video_clk_ops,
  517. };
  518. #define MSTPCR_ARCH_FLAGS(reg, bit) (((reg) << 8) | (bit))
  519. #define MSTPCR_ARCH_FLAGS_REG(value) ((value) >> 8)
  520. #define MSTPCR_ARCH_FLAGS_BIT(value) ((value) & 0xff)
  521. static int sh7722_mstpcr_start_stop(struct clk *clk, int enable)
  522. {
  523. unsigned long bit = MSTPCR_ARCH_FLAGS_BIT(clk->arch_flags);
  524. unsigned long reg;
  525. unsigned long r;
  526. switch(MSTPCR_ARCH_FLAGS_REG(clk->arch_flags)) {
  527. case 0:
  528. reg = MSTPCR0;
  529. break;
  530. case 1:
  531. reg = MSTPCR1;
  532. break;
  533. case 2:
  534. reg = MSTPCR2;
  535. break;
  536. default:
  537. return -EINVAL;
  538. }
  539. r = ctrl_inl(reg);
  540. if (enable)
  541. r &= ~(1 << bit);
  542. else
  543. r |= (1 << bit);
  544. ctrl_outl(r, reg);
  545. return 0;
  546. }
  547. static void sh7722_mstpcr_enable(struct clk *clk)
  548. {
  549. sh7722_mstpcr_start_stop(clk, 1);
  550. }
  551. static void sh7722_mstpcr_disable(struct clk *clk)
  552. {
  553. sh7722_mstpcr_start_stop(clk, 0);
  554. }
  555. static struct clk_ops sh7722_mstpcr_clk_ops = {
  556. .enable = sh7722_mstpcr_enable,
  557. .disable = sh7722_mstpcr_disable,
  558. .recalc = followparent_recalc,
  559. };
  560. #define MSTPCR(_name, _parent, regnr, bitnr) \
  561. { \
  562. .name = _name, \
  563. .arch_flags = MSTPCR_ARCH_FLAGS(regnr, bitnr), \
  564. .ops = (void *)_parent, \
  565. }
  566. static struct clk sh7722_mstpcr_clocks[] = {
  567. #if defined(CONFIG_CPU_SUBTYPE_SH7722)
  568. MSTPCR("uram0", "umem_clk", 0, 28),
  569. MSTPCR("xymem0", "bus_clk", 0, 26),
  570. MSTPCR("tmu0", "peripheral_clk", 0, 15),
  571. MSTPCR("cmt0", "r_clk", 0, 14),
  572. MSTPCR("rwdt0", "r_clk", 0, 13),
  573. MSTPCR("flctl0", "peripheral_clk", 0, 10),
  574. MSTPCR("scif0", "peripheral_clk", 0, 7),
  575. MSTPCR("scif1", "peripheral_clk", 0, 6),
  576. MSTPCR("scif2", "peripheral_clk", 0, 5),
  577. MSTPCR("i2c0", "peripheral_clk", 1, 9),
  578. MSTPCR("rtc0", "r_clk", 1, 8),
  579. MSTPCR("sdhi0", "peripheral_clk", 2, 18),
  580. MSTPCR("keysc0", "r_clk", 2, 14),
  581. MSTPCR("usbf0", "peripheral_clk", 2, 11),
  582. MSTPCR("2dg0", "bus_clk", 2, 9),
  583. MSTPCR("siu0", "bus_clk", 2, 8),
  584. MSTPCR("vou0", "bus_clk", 2, 5),
  585. MSTPCR("jpu0", "bus_clk", 2, 6),
  586. MSTPCR("beu0", "bus_clk", 2, 4),
  587. MSTPCR("ceu0", "bus_clk", 2, 3),
  588. MSTPCR("veu0", "bus_clk", 2, 2),
  589. MSTPCR("vpu0", "bus_clk", 2, 1),
  590. MSTPCR("lcdc0", "bus_clk", 2, 0),
  591. #endif
  592. #if defined(CONFIG_CPU_SUBTYPE_SH7723)
  593. /* See page 60 of Datasheet V1.0: Overview -> Block Diagram */
  594. MSTPCR("tlb0", "cpu_clk", 0, 31),
  595. MSTPCR("ic0", "cpu_clk", 0, 30),
  596. MSTPCR("oc0", "cpu_clk", 0, 29),
  597. MSTPCR("l2c0", "sh_clk", 0, 28),
  598. MSTPCR("ilmem0", "cpu_clk", 0, 27),
  599. MSTPCR("fpu0", "cpu_clk", 0, 24),
  600. MSTPCR("intc0", "cpu_clk", 0, 22),
  601. MSTPCR("dmac0", "bus_clk", 0, 21),
  602. MSTPCR("sh0", "sh_clk", 0, 20),
  603. MSTPCR("hudi0", "peripheral_clk", 0, 19),
  604. MSTPCR("ubc0", "cpu_clk", 0, 17),
  605. MSTPCR("tmu0", "peripheral_clk", 0, 15),
  606. MSTPCR("cmt0", "r_clk", 0, 14),
  607. MSTPCR("rwdt0", "r_clk", 0, 13),
  608. MSTPCR("dmac1", "bus_clk", 0, 12),
  609. MSTPCR("tmu1", "peripheral_clk", 0, 11),
  610. MSTPCR("flctl0", "peripheral_clk", 0, 10),
  611. MSTPCR("scif0", "peripheral_clk", 0, 9),
  612. MSTPCR("scif1", "peripheral_clk", 0, 8),
  613. MSTPCR("scif2", "peripheral_clk", 0, 7),
  614. MSTPCR("scif3", "bus_clk", 0, 6),
  615. MSTPCR("scif4", "bus_clk", 0, 5),
  616. MSTPCR("scif5", "bus_clk", 0, 4),
  617. MSTPCR("msiof0", "bus_clk", 0, 2),
  618. MSTPCR("msiof1", "bus_clk", 0, 1),
  619. MSTPCR("meram0", "sh_clk", 0, 0),
  620. MSTPCR("i2c0", "peripheral_clk", 1, 9),
  621. MSTPCR("rtc0", "r_clk", 1, 8),
  622. MSTPCR("atapi0", "sh_clk", 2, 28),
  623. MSTPCR("adc0", "peripheral_clk", 2, 28),
  624. MSTPCR("tpu0", "bus_clk", 2, 25),
  625. MSTPCR("irda0", "peripheral_clk", 2, 24),
  626. MSTPCR("tsif0", "bus_clk", 2, 22),
  627. MSTPCR("icb0", "bus_clk", 2, 21),
  628. MSTPCR("sdhi0", "bus_clk", 2, 18),
  629. MSTPCR("sdhi1", "bus_clk", 2, 17),
  630. MSTPCR("keysc0", "r_clk", 2, 14),
  631. MSTPCR("usb0", "bus_clk", 2, 11),
  632. MSTPCR("2dg0", "bus_clk", 2, 10),
  633. MSTPCR("siu0", "bus_clk", 2, 8),
  634. MSTPCR("veu1", "bus_clk", 2, 6),
  635. MSTPCR("vou0", "bus_clk", 2, 5),
  636. MSTPCR("beu0", "bus_clk", 2, 4),
  637. MSTPCR("ceu0", "bus_clk", 2, 3),
  638. MSTPCR("veu0", "bus_clk", 2, 2),
  639. MSTPCR("vpu0", "bus_clk", 2, 1),
  640. MSTPCR("lcdc0", "bus_clk", 2, 0),
  641. #endif
  642. #if defined(CONFIG_CPU_SUBTYPE_SH7724)
  643. /* See Datasheet : Overview -> Block Diagram */
  644. MSTPCR("tlb0", "cpu_clk", 0, 31),
  645. MSTPCR("ic0", "cpu_clk", 0, 30),
  646. MSTPCR("oc0", "cpu_clk", 0, 29),
  647. MSTPCR("rs0", "bus_clk", 0, 28),
  648. MSTPCR("ilmem0", "cpu_clk", 0, 27),
  649. MSTPCR("l2c0", "sh_clk", 0, 26),
  650. MSTPCR("fpu0", "cpu_clk", 0, 24),
  651. MSTPCR("intc0", "peripheral_clk", 0, 22),
  652. MSTPCR("dmac0", "bus_clk", 0, 21),
  653. MSTPCR("sh0", "sh_clk", 0, 20),
  654. MSTPCR("hudi0", "peripheral_clk", 0, 19),
  655. MSTPCR("ubc0", "cpu_clk", 0, 17),
  656. MSTPCR("tmu0", "peripheral_clk", 0, 15),
  657. MSTPCR("cmt0", "r_clk", 0, 14),
  658. MSTPCR("rwdt0", "r_clk", 0, 13),
  659. MSTPCR("dmac1", "bus_clk", 0, 12),
  660. MSTPCR("tmu1", "peripheral_clk", 0, 10),
  661. MSTPCR("scif0", "peripheral_clk", 0, 9),
  662. MSTPCR("scif1", "peripheral_clk", 0, 8),
  663. MSTPCR("scif2", "peripheral_clk", 0, 7),
  664. MSTPCR("scif3", "bus_clk", 0, 6),
  665. MSTPCR("scif4", "bus_clk", 0, 5),
  666. MSTPCR("scif5", "bus_clk", 0, 4),
  667. MSTPCR("msiof0", "bus_clk", 0, 2),
  668. MSTPCR("msiof1", "bus_clk", 0, 1),
  669. MSTPCR("keysc0", "r_clk", 1, 12),
  670. MSTPCR("rtc0", "r_clk", 1, 11),
  671. MSTPCR("i2c0", "peripheral_clk", 1, 9),
  672. MSTPCR("i2c1", "peripheral_clk", 1, 8),
  673. MSTPCR("mmc0", "bus_clk", 2, 29),
  674. MSTPCR("eth0", "bus_clk", 2, 28),
  675. MSTPCR("atapi0", "bus_clk", 2, 26),
  676. MSTPCR("tpu0", "bus_clk", 2, 25),
  677. MSTPCR("irda0", "peripheral_clk", 2, 24),
  678. MSTPCR("tsif0", "bus_clk", 2, 22),
  679. MSTPCR("usb1", "bus_clk", 2, 21),
  680. MSTPCR("usb0", "bus_clk", 2, 20),
  681. MSTPCR("2dg0", "bus_clk", 2, 19),
  682. MSTPCR("sdhi0", "bus_clk", 2, 18),
  683. MSTPCR("sdhi1", "bus_clk", 2, 17),
  684. MSTPCR("veu1", "bus_clk", 2, 15),
  685. MSTPCR("ceu1", "bus_clk", 2, 13),
  686. MSTPCR("beu1", "bus_clk", 2, 12),
  687. MSTPCR("2ddmac0", "sh_clk", 2, 10),
  688. MSTPCR("spu0", "bus_clk", 2, 9),
  689. MSTPCR("jpu0", "bus_clk", 2, 6),
  690. MSTPCR("vou0", "bus_clk", 2, 5),
  691. MSTPCR("beu0", "bus_clk", 2, 4),
  692. MSTPCR("ceu0", "bus_clk", 2, 3),
  693. MSTPCR("veu0", "bus_clk", 2, 2),
  694. MSTPCR("vpu0", "bus_clk", 2, 1),
  695. MSTPCR("lcdc0", "bus_clk", 2, 0),
  696. #endif
  697. #if defined(CONFIG_CPU_SUBTYPE_SH7343)
  698. MSTPCR("uram0", "umem_clk", 0, 28),
  699. MSTPCR("xymem0", "bus_clk", 0, 26),
  700. MSTPCR("tmu0", "peripheral_clk", 0, 15),
  701. MSTPCR("cmt0", "r_clk", 0, 14),
  702. MSTPCR("rwdt0", "r_clk", 0, 13),
  703. MSTPCR("scif0", "peripheral_clk", 0, 7),
  704. MSTPCR("scif1", "peripheral_clk", 0, 6),
  705. MSTPCR("scif2", "peripheral_clk", 0, 5),
  706. MSTPCR("scif3", "peripheral_clk", 0, 4),
  707. MSTPCR("i2c0", "peripheral_clk", 1, 9),
  708. MSTPCR("i2c1", "peripheral_clk", 1, 8),
  709. MSTPCR("sdhi0", "peripheral_clk", 2, 18),
  710. MSTPCR("keysc0", "r_clk", 2, 14),
  711. MSTPCR("usbf0", "peripheral_clk", 2, 11),
  712. MSTPCR("siu0", "bus_clk", 2, 8),
  713. MSTPCR("jpu0", "bus_clk", 2, 6),
  714. MSTPCR("vou0", "bus_clk", 2, 5),
  715. MSTPCR("beu0", "bus_clk", 2, 4),
  716. MSTPCR("ceu0", "bus_clk", 2, 3),
  717. MSTPCR("veu0", "bus_clk", 2, 2),
  718. MSTPCR("vpu0", "bus_clk", 2, 1),
  719. MSTPCR("lcdc0", "bus_clk", 2, 0),
  720. #endif
  721. #if defined(CONFIG_CPU_SUBTYPE_SH7366)
  722. /* See page 52 of Datasheet V0.40: Overview -> Block Diagram */
  723. MSTPCR("tlb0", "cpu_clk", 0, 31),
  724. MSTPCR("ic0", "cpu_clk", 0, 30),
  725. MSTPCR("oc0", "cpu_clk", 0, 29),
  726. MSTPCR("rsmem0", "sh_clk", 0, 28),
  727. MSTPCR("xymem0", "cpu_clk", 0, 26),
  728. MSTPCR("intc30", "peripheral_clk", 0, 23),
  729. MSTPCR("intc0", "peripheral_clk", 0, 22),
  730. MSTPCR("dmac0", "bus_clk", 0, 21),
  731. MSTPCR("sh0", "sh_clk", 0, 20),
  732. MSTPCR("hudi0", "peripheral_clk", 0, 19),
  733. MSTPCR("ubc0", "cpu_clk", 0, 17),
  734. MSTPCR("tmu0", "peripheral_clk", 0, 15),
  735. MSTPCR("cmt0", "r_clk", 0, 14),
  736. MSTPCR("rwdt0", "r_clk", 0, 13),
  737. MSTPCR("flctl0", "peripheral_clk", 0, 10),
  738. MSTPCR("scif0", "peripheral_clk", 0, 7),
  739. MSTPCR("scif1", "bus_clk", 0, 6),
  740. MSTPCR("scif2", "bus_clk", 0, 5),
  741. MSTPCR("msiof0", "peripheral_clk", 0, 2),
  742. MSTPCR("sbr0", "peripheral_clk", 0, 1),
  743. MSTPCR("i2c0", "peripheral_clk", 1, 9),
  744. MSTPCR("icb0", "bus_clk", 2, 27),
  745. MSTPCR("meram0", "sh_clk", 2, 26),
  746. MSTPCR("dacc0", "peripheral_clk", 2, 24),
  747. MSTPCR("dacy0", "peripheral_clk", 2, 23),
  748. MSTPCR("tsif0", "bus_clk", 2, 22),
  749. MSTPCR("sdhi0", "bus_clk", 2, 18),
  750. MSTPCR("mmcif0", "bus_clk", 2, 17),
  751. MSTPCR("usb0", "bus_clk", 2, 11),
  752. MSTPCR("siu0", "bus_clk", 2, 8),
  753. MSTPCR("veu1", "bus_clk", 2, 7),
  754. MSTPCR("vou0", "bus_clk", 2, 5),
  755. MSTPCR("beu0", "bus_clk", 2, 4),
  756. MSTPCR("ceu0", "bus_clk", 2, 3),
  757. MSTPCR("veu0", "bus_clk", 2, 2),
  758. MSTPCR("vpu0", "bus_clk", 2, 1),
  759. MSTPCR("lcdc0", "bus_clk", 2, 0),
  760. #endif
  761. };
  762. static struct clk *sh7722_clocks[] = {
  763. &sh7722_umem_clock,
  764. &sh7722_sh_clock,
  765. &sh7722_peripheral_clock,
  766. &sh7722_sdram_clock,
  767. #if !defined(CONFIG_CPU_SUBTYPE_SH7343) &&\
  768. !defined(CONFIG_CPU_SUBTYPE_SH7724)
  769. &sh7722_siu_a_clock,
  770. &sh7722_siu_b_clock,
  771. #endif
  772. /* 7724 should support FSI clock */
  773. #if defined(CONFIG_CPU_SUBTYPE_SH7722) || \
  774. defined(CONFIG_CPU_SUBTYPE_SH7724)
  775. &sh7722_irda_clock,
  776. #endif
  777. &sh7722_video_clock,
  778. };
  779. /*
  780. * init in order: master, module, bus, cpu
  781. */
  782. struct clk_ops *onchip_ops[] = {
  783. &sh7722_master_clk_ops,
  784. &sh7722_module_clk_ops,
  785. &sh7722_frqcr_clk_ops,
  786. &sh7722_frqcr_clk_ops,
  787. };
  788. void __init
  789. arch_init_clk_ops(struct clk_ops **ops, int type)
  790. {
  791. BUG_ON(type < 0 || type > ARRAY_SIZE(onchip_ops));
  792. *ops = onchip_ops[type];
  793. }
  794. int __init arch_clk_init(void)
  795. {
  796. struct clk *clk;
  797. int i;
  798. clk = clk_get(NULL, "master_clk");
  799. for (i = 0; i < ARRAY_SIZE(sh7722_clocks); i++) {
  800. pr_debug( "Registering clock '%s'\n", sh7722_clocks[i]->name);
  801. sh7722_clocks[i]->parent = clk;
  802. clk_register(sh7722_clocks[i]);
  803. }
  804. clk_put(clk);
  805. clk_register(&sh7722_r_clock);
  806. for (i = 0; i < ARRAY_SIZE(sh7722_mstpcr_clocks); i++) {
  807. pr_debug( "Registering mstpcr clock '%s'\n",
  808. sh7722_mstpcr_clocks[i].name);
  809. clk = clk_get(NULL, (void *) sh7722_mstpcr_clocks[i].ops);
  810. sh7722_mstpcr_clocks[i].parent = clk;
  811. sh7722_mstpcr_clocks[i].ops = &sh7722_mstpcr_clk_ops;
  812. clk_register(&sh7722_mstpcr_clocks[i]);
  813. clk_put(clk);
  814. }
  815. clk_recalc_rate(&sh7722_r_clock); /* make sure rate gets propagated */
  816. return 0;
  817. }