clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Clock tree for CSR SiRFprimaII
  3. *
  4. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/bitops.h>
  10. #include <linux/err.h>
  11. #include <linux/errno.h>
  12. #include <linux/io.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/clk.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <asm/mach/map.h>
  19. #include <mach/map.h>
  20. #define SIRFSOC_CLKC_CLK_EN0 0x0000
  21. #define SIRFSOC_CLKC_CLK_EN1 0x0004
  22. #define SIRFSOC_CLKC_REF_CFG 0x0014
  23. #define SIRFSOC_CLKC_CPU_CFG 0x0018
  24. #define SIRFSOC_CLKC_MEM_CFG 0x001c
  25. #define SIRFSOC_CLKC_SYS_CFG 0x0020
  26. #define SIRFSOC_CLKC_IO_CFG 0x0024
  27. #define SIRFSOC_CLKC_DSP_CFG 0x0028
  28. #define SIRFSOC_CLKC_GFX_CFG 0x002c
  29. #define SIRFSOC_CLKC_MM_CFG 0x0030
  30. #define SIRFSOC_LKC_LCD_CFG 0x0034
  31. #define SIRFSOC_CLKC_MMC_CFG 0x0038
  32. #define SIRFSOC_CLKC_PLL1_CFG0 0x0040
  33. #define SIRFSOC_CLKC_PLL2_CFG0 0x0044
  34. #define SIRFSOC_CLKC_PLL3_CFG0 0x0048
  35. #define SIRFSOC_CLKC_PLL1_CFG1 0x004c
  36. #define SIRFSOC_CLKC_PLL2_CFG1 0x0050
  37. #define SIRFSOC_CLKC_PLL3_CFG1 0x0054
  38. #define SIRFSOC_CLKC_PLL1_CFG2 0x0058
  39. #define SIRFSOC_CLKC_PLL2_CFG2 0x005c
  40. #define SIRFSOC_CLKC_PLL3_CFG2 0x0060
  41. #define SIRFSOC_CLOCK_VA_BASE SIRFSOC_VA(0x005000)
  42. #define KHZ 1000
  43. #define MHZ (KHZ * KHZ)
  44. struct clk_ops {
  45. unsigned long (*get_rate)(struct clk *clk);
  46. long (*round_rate)(struct clk *clk, unsigned long rate);
  47. int (*set_rate)(struct clk *clk, unsigned long rate);
  48. int (*enable)(struct clk *clk);
  49. int (*disable)(struct clk *clk);
  50. struct clk *(*get_parent)(struct clk *clk);
  51. int (*set_parent)(struct clk *clk, struct clk *parent);
  52. };
  53. struct clk {
  54. struct clk *parent; /* parent clk */
  55. unsigned long rate; /* clock rate in Hz */
  56. signed char usage; /* clock enable count */
  57. signed char enable_bit; /* enable bit: 0 ~ 63 */
  58. unsigned short regofs; /* register offset */
  59. struct clk_ops *ops; /* clock operation */
  60. };
  61. static DEFINE_SPINLOCK(clocks_lock);
  62. static inline unsigned long clkc_readl(unsigned reg)
  63. {
  64. return readl(SIRFSOC_CLOCK_VA_BASE + reg);
  65. }
  66. static inline void clkc_writel(u32 val, unsigned reg)
  67. {
  68. writel(val, SIRFSOC_CLOCK_VA_BASE + reg);
  69. }
  70. /*
  71. * osc_rtc - real time oscillator - 32.768KHz
  72. * osc_sys - high speed oscillator - 26MHz
  73. */
  74. static struct clk clk_rtc = {
  75. .rate = 32768,
  76. };
  77. static struct clk clk_osc = {
  78. .rate = 26 * MHZ,
  79. };
  80. /*
  81. * std pll
  82. */
  83. static unsigned long std_pll_get_rate(struct clk *clk)
  84. {
  85. unsigned long fin = clk_get_rate(clk->parent);
  86. u32 regcfg2 = clk->regofs + SIRFSOC_CLKC_PLL1_CFG2 -
  87. SIRFSOC_CLKC_PLL1_CFG0;
  88. if (clkc_readl(regcfg2) & BIT(2)) {
  89. /* pll bypass mode */
  90. clk->rate = fin;
  91. } else {
  92. /* fout = fin * nf / nr / od */
  93. u32 cfg0 = clkc_readl(clk->regofs);
  94. u32 nf = (cfg0 & (BIT(13) - 1)) + 1;
  95. u32 nr = ((cfg0 >> 13) & (BIT(6) - 1)) + 1;
  96. u32 od = ((cfg0 >> 19) & (BIT(4) - 1)) + 1;
  97. WARN_ON(fin % MHZ);
  98. clk->rate = fin / MHZ * nf / nr / od * MHZ;
  99. }
  100. return clk->rate;
  101. }
  102. static int std_pll_set_rate(struct clk *clk, unsigned long rate)
  103. {
  104. unsigned long fin, nf, nr, od, reg;
  105. /*
  106. * fout = fin * nf / (nr * od);
  107. * set od = 1, nr = fin/MHz, so fout = nf * MHz
  108. */
  109. nf = rate / MHZ;
  110. if (unlikely((rate % MHZ) || nf > BIT(13) || nf < 1))
  111. return -EINVAL;
  112. fin = clk_get_rate(clk->parent);
  113. BUG_ON(fin < MHZ);
  114. nr = fin / MHZ;
  115. BUG_ON((fin % MHZ) || nr > BIT(6));
  116. od = 1;
  117. reg = (nf - 1) | ((nr - 1) << 13) | ((od - 1) << 19);
  118. clkc_writel(reg, clk->regofs);
  119. reg = clk->regofs + SIRFSOC_CLKC_PLL1_CFG1 - SIRFSOC_CLKC_PLL1_CFG0;
  120. clkc_writel((nf >> 1) - 1, reg);
  121. reg = clk->regofs + SIRFSOC_CLKC_PLL1_CFG2 - SIRFSOC_CLKC_PLL1_CFG0;
  122. while (!(clkc_readl(reg) & BIT(6)))
  123. cpu_relax();
  124. clk->rate = 0; /* set to zero will force recalculation */
  125. return 0;
  126. }
  127. static struct clk_ops std_pll_ops = {
  128. .get_rate = std_pll_get_rate,
  129. .set_rate = std_pll_set_rate,
  130. };
  131. static struct clk clk_pll1 = {
  132. .parent = &clk_osc,
  133. .regofs = SIRFSOC_CLKC_PLL1_CFG0,
  134. .ops = &std_pll_ops,
  135. };
  136. static struct clk clk_pll2 = {
  137. .parent = &clk_osc,
  138. .regofs = SIRFSOC_CLKC_PLL2_CFG0,
  139. .ops = &std_pll_ops,
  140. };
  141. static struct clk clk_pll3 = {
  142. .parent = &clk_osc,
  143. .regofs = SIRFSOC_CLKC_PLL3_CFG0,
  144. .ops = &std_pll_ops,
  145. };
  146. /*
  147. * clock domains - cpu, mem, sys/io
  148. */
  149. static struct clk clk_mem;
  150. static struct clk *dmn_get_parent(struct clk *clk)
  151. {
  152. struct clk *clks[] = {
  153. &clk_osc, &clk_rtc, &clk_pll1, &clk_pll2, &clk_pll3
  154. };
  155. u32 cfg = clkc_readl(clk->regofs);
  156. WARN_ON((cfg & (BIT(3) - 1)) > 4);
  157. return clks[cfg & (BIT(3) - 1)];
  158. }
  159. static int dmn_set_parent(struct clk *clk, struct clk *parent)
  160. {
  161. const struct clk *clks[] = {
  162. &clk_osc, &clk_rtc, &clk_pll1, &clk_pll2, &clk_pll3
  163. };
  164. u32 cfg = clkc_readl(clk->regofs);
  165. int i;
  166. for (i = 0; i < ARRAY_SIZE(clks); i++) {
  167. if (clks[i] == parent) {
  168. cfg &= ~(BIT(3) - 1);
  169. clkc_writel(cfg | i, clk->regofs);
  170. /* BIT(3) - switching status: 1 - busy, 0 - done */
  171. while (clkc_readl(clk->regofs) & BIT(3))
  172. cpu_relax();
  173. return 0;
  174. }
  175. }
  176. return -EINVAL;
  177. }
  178. static unsigned long dmn_get_rate(struct clk *clk)
  179. {
  180. unsigned long fin = clk_get_rate(clk->parent);
  181. u32 cfg = clkc_readl(clk->regofs);
  182. if (cfg & BIT(24)) {
  183. /* fcd bypass mode */
  184. clk->rate = fin;
  185. } else {
  186. /*
  187. * wait count: bit[19:16], hold count: bit[23:20]
  188. */
  189. u32 wait = (cfg >> 16) & (BIT(4) - 1);
  190. u32 hold = (cfg >> 20) & (BIT(4) - 1);
  191. clk->rate = fin / (wait + hold + 2);
  192. }
  193. return clk->rate;
  194. }
  195. static int dmn_set_rate(struct clk *clk, unsigned long rate)
  196. {
  197. unsigned long fin;
  198. unsigned ratio, wait, hold, reg;
  199. unsigned bits = (clk == &clk_mem) ? 3 : 4;
  200. fin = clk_get_rate(clk->parent);
  201. ratio = fin / rate;
  202. if (unlikely(ratio < 2 || ratio > BIT(bits + 1)))
  203. return -EINVAL;
  204. WARN_ON(fin % rate);
  205. wait = (ratio >> 1) - 1;
  206. hold = ratio - wait - 2;
  207. reg = clkc_readl(clk->regofs);
  208. reg &= ~(((BIT(bits) - 1) << 16) | ((BIT(bits) - 1) << 20));
  209. reg |= (wait << 16) | (hold << 20) | BIT(25);
  210. clkc_writel(reg, clk->regofs);
  211. /* waiting FCD been effective */
  212. while (clkc_readl(clk->regofs) & BIT(25))
  213. cpu_relax();
  214. clk->rate = 0; /* set to zero will force recalculation */
  215. return 0;
  216. }
  217. /*
  218. * cpu clock has no FCD register in Prima2, can only change pll
  219. */
  220. static int cpu_set_rate(struct clk *clk, unsigned long rate)
  221. {
  222. int ret1, ret2;
  223. struct clk *cur_parent, *tmp_parent;
  224. cur_parent = dmn_get_parent(clk);
  225. BUG_ON(cur_parent == NULL || cur_parent->usage > 1);
  226. /* switch to tmp pll before setting parent clock's rate */
  227. tmp_parent = cur_parent == &clk_pll1 ? &clk_pll2 : &clk_pll1;
  228. ret1 = dmn_set_parent(clk, tmp_parent);
  229. BUG_ON(ret1);
  230. ret2 = clk_set_rate(cur_parent, rate);
  231. ret1 = dmn_set_parent(clk, cur_parent);
  232. clk->rate = 0; /* set to zero will force recalculation */
  233. return ret2 ? ret2 : ret1;
  234. }
  235. static struct clk_ops cpu_ops = {
  236. .get_parent = dmn_get_parent,
  237. .set_parent = dmn_set_parent,
  238. .set_rate = cpu_set_rate,
  239. };
  240. static struct clk clk_cpu = {
  241. .parent = &clk_pll1,
  242. .regofs = SIRFSOC_CLKC_CPU_CFG,
  243. .ops = &cpu_ops,
  244. };
  245. static struct clk_ops msi_ops = {
  246. .set_rate = dmn_set_rate,
  247. .get_rate = dmn_get_rate,
  248. .set_parent = dmn_set_parent,
  249. .get_parent = dmn_get_parent,
  250. };
  251. static struct clk clk_mem = {
  252. .parent = &clk_pll2,
  253. .regofs = SIRFSOC_CLKC_MEM_CFG,
  254. .ops = &msi_ops,
  255. };
  256. static struct clk clk_sys = {
  257. .parent = &clk_pll3,
  258. .regofs = SIRFSOC_CLKC_SYS_CFG,
  259. .ops = &msi_ops,
  260. };
  261. static struct clk clk_io = {
  262. .parent = &clk_pll3,
  263. .regofs = SIRFSOC_CLKC_IO_CFG,
  264. .ops = &msi_ops,
  265. };
  266. /*
  267. * on-chip clock sets
  268. */
  269. static struct clk_lookup onchip_clks[] = {
  270. {
  271. .dev_id = "rtc",
  272. .clk = &clk_rtc,
  273. }, {
  274. .dev_id = "osc",
  275. .clk = &clk_osc,
  276. }, {
  277. .dev_id = "pll1",
  278. .clk = &clk_pll1,
  279. }, {
  280. .dev_id = "pll2",
  281. .clk = &clk_pll2,
  282. }, {
  283. .dev_id = "pll3",
  284. .clk = &clk_pll3,
  285. }, {
  286. .dev_id = "cpu",
  287. .clk = &clk_cpu,
  288. }, {
  289. .dev_id = "mem",
  290. .clk = &clk_mem,
  291. }, {
  292. .dev_id = "sys",
  293. .clk = &clk_sys,
  294. }, {
  295. .dev_id = "io",
  296. .clk = &clk_io,
  297. },
  298. };
  299. int clk_enable(struct clk *clk)
  300. {
  301. unsigned long flags;
  302. if (unlikely(IS_ERR_OR_NULL(clk)))
  303. return -EINVAL;
  304. if (clk->parent)
  305. clk_enable(clk->parent);
  306. spin_lock_irqsave(&clocks_lock, flags);
  307. if (!clk->usage++ && clk->ops && clk->ops->enable)
  308. clk->ops->enable(clk);
  309. spin_unlock_irqrestore(&clocks_lock, flags);
  310. return 0;
  311. }
  312. EXPORT_SYMBOL(clk_enable);
  313. void clk_disable(struct clk *clk)
  314. {
  315. unsigned long flags;
  316. if (unlikely(IS_ERR_OR_NULL(clk)))
  317. return;
  318. WARN_ON(!clk->usage);
  319. spin_lock_irqsave(&clocks_lock, flags);
  320. if (--clk->usage == 0 && clk->ops && clk->ops->disable)
  321. clk->ops->disable(clk);
  322. spin_unlock_irqrestore(&clocks_lock, flags);
  323. if (clk->parent)
  324. clk_disable(clk->parent);
  325. }
  326. EXPORT_SYMBOL(clk_disable);
  327. unsigned long clk_get_rate(struct clk *clk)
  328. {
  329. if (unlikely(IS_ERR_OR_NULL(clk)))
  330. return 0;
  331. if (clk->rate)
  332. return clk->rate;
  333. if (clk->ops && clk->ops->get_rate)
  334. return clk->ops->get_rate(clk);
  335. return clk_get_rate(clk->parent);
  336. }
  337. EXPORT_SYMBOL(clk_get_rate);
  338. long clk_round_rate(struct clk *clk, unsigned long rate)
  339. {
  340. if (unlikely(IS_ERR_OR_NULL(clk)))
  341. return 0;
  342. if (clk->ops && clk->ops->round_rate)
  343. return clk->ops->round_rate(clk, rate);
  344. return 0;
  345. }
  346. EXPORT_SYMBOL(clk_round_rate);
  347. int clk_set_rate(struct clk *clk, unsigned long rate)
  348. {
  349. if (unlikely(IS_ERR_OR_NULL(clk)))
  350. return -EINVAL;
  351. if (!clk->ops || !clk->ops->set_rate)
  352. return -EINVAL;
  353. return clk->ops->set_rate(clk, rate);
  354. }
  355. EXPORT_SYMBOL(clk_set_rate);
  356. int clk_set_parent(struct clk *clk, struct clk *parent)
  357. {
  358. int ret;
  359. unsigned long flags;
  360. if (unlikely(IS_ERR_OR_NULL(clk)))
  361. return -EINVAL;
  362. if (!clk->ops || !clk->ops->set_parent)
  363. return -EINVAL;
  364. spin_lock_irqsave(&clocks_lock, flags);
  365. ret = clk->ops->set_parent(clk, parent);
  366. if (!ret) {
  367. parent->usage += clk->usage;
  368. clk->parent->usage -= clk->usage;
  369. BUG_ON(clk->parent->usage < 0);
  370. clk->parent = parent;
  371. }
  372. spin_unlock_irqrestore(&clocks_lock, flags);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL(clk_set_parent);
  376. struct clk *clk_get_parent(struct clk *clk)
  377. {
  378. unsigned long flags;
  379. if (unlikely(IS_ERR_OR_NULL(clk)))
  380. return NULL;
  381. if (!clk->ops || !clk->ops->get_parent)
  382. return clk->parent;
  383. spin_lock_irqsave(&clocks_lock, flags);
  384. clk->parent = clk->ops->get_parent(clk);
  385. spin_unlock_irqrestore(&clocks_lock, flags);
  386. return clk->parent;
  387. }
  388. EXPORT_SYMBOL(clk_get_parent);
  389. static void __init sirfsoc_clk_init(void)
  390. {
  391. clkdev_add_table(onchip_clks, ARRAY_SIZE(onchip_clks));
  392. }
  393. static struct of_device_id clkc_ids[] = {
  394. { .compatible = "sirf,prima2-clkc" },
  395. {},
  396. };
  397. void __init sirfsoc_of_clk_init(void)
  398. {
  399. struct device_node *np;
  400. struct resource res;
  401. struct map_desc sirfsoc_clkc_iodesc = {
  402. .virtual = SIRFSOC_CLOCK_VA_BASE,
  403. .type = MT_DEVICE,
  404. };
  405. np = of_find_matching_node(NULL, clkc_ids);
  406. if (!np)
  407. panic("unable to find compatible clkc node in dtb\n");
  408. if (of_address_to_resource(np, 0, &res))
  409. panic("unable to find clkc range in dtb");
  410. of_node_put(np);
  411. sirfsoc_clkc_iodesc.pfn = __phys_to_pfn(res.start);
  412. sirfsoc_clkc_iodesc.length = 1 + res.end - res.start;
  413. iotable_init(&sirfsoc_clkc_iodesc, 1);
  414. sirfsoc_clk_init();
  415. }