clock.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/list.h>
  4. #include <linux/errno.h>
  5. #include <linux/err.h>
  6. #include <linux/string.h>
  7. #include <linux/clk.h>
  8. #include <linux/mutex.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/device.h>
  12. #include <linux/init.h>
  13. #include <linux/timer.h>
  14. #include <linux/io.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/clkdev.h>
  17. #include <asm/clocks.h>
  18. #define CGU0_CTL_DF (1 << 0)
  19. #define CGU0_CTL_MSEL_SHIFT 8
  20. #define CGU0_CTL_MSEL_MASK (0x7f << 8)
  21. #define CGU0_STAT_PLLEN (1 << 0)
  22. #define CGU0_STAT_PLLBP (1 << 1)
  23. #define CGU0_STAT_PLLLK (1 << 2)
  24. #define CGU0_STAT_CLKSALGN (1 << 3)
  25. #define CGU0_STAT_CCBF0 (1 << 4)
  26. #define CGU0_STAT_CCBF1 (1 << 5)
  27. #define CGU0_STAT_SCBF0 (1 << 6)
  28. #define CGU0_STAT_SCBF1 (1 << 7)
  29. #define CGU0_STAT_DCBF (1 << 8)
  30. #define CGU0_STAT_OCBF (1 << 9)
  31. #define CGU0_STAT_ADDRERR (1 << 16)
  32. #define CGU0_STAT_LWERR (1 << 17)
  33. #define CGU0_STAT_DIVERR (1 << 18)
  34. #define CGU0_STAT_WDFMSERR (1 << 19)
  35. #define CGU0_STAT_WDIVERR (1 << 20)
  36. #define CGU0_STAT_PLOCKERR (1 << 21)
  37. #define CGU0_DIV_CSEL_SHIFT 0
  38. #define CGU0_DIV_CSEL_MASK 0x0000001F
  39. #define CGU0_DIV_S0SEL_SHIFT 5
  40. #define CGU0_DIV_S0SEL_MASK (0x3 << CGU0_DIV_S0SEL_SHIFT)
  41. #define CGU0_DIV_SYSSEL_SHIFT 8
  42. #define CGU0_DIV_SYSSEL_MASK (0x1f << CGU0_DIV_SYSSEL_SHIFT)
  43. #define CGU0_DIV_S1SEL_SHIFT 13
  44. #define CGU0_DIV_S1SEL_MASK (0x3 << CGU0_DIV_S1SEL_SHIFT)
  45. #define CGU0_DIV_DSEL_SHIFT 16
  46. #define CGU0_DIV_DSEL_MASK (0x1f << CGU0_DIV_DSEL_SHIFT)
  47. #define CGU0_DIV_OSEL_SHIFT 22
  48. #define CGU0_DIV_OSEL_MASK (0x7f << CGU0_DIV_OSEL_SHIFT)
  49. #define CLK(_clk, _devname, _conname) \
  50. { \
  51. .clk = &_clk, \
  52. .dev_id = _devname, \
  53. .con_id = _conname, \
  54. }
  55. #define NEEDS_INITIALIZATION 0x11
  56. static LIST_HEAD(clk_list);
  57. static void clk_reg_write_mask(u32 reg, uint32_t val, uint32_t mask)
  58. {
  59. u32 val2;
  60. val2 = bfin_read32(reg);
  61. val2 &= ~mask;
  62. val2 |= val;
  63. bfin_write32(reg, val2);
  64. }
  65. int wait_for_pll_align(void)
  66. {
  67. int i = 10000;
  68. while (i-- && (bfin_read32(CGU0_STAT) & CGU0_STAT_CLKSALGN));
  69. if (bfin_read32(CGU0_STAT) & CGU0_STAT_CLKSALGN) {
  70. printk(KERN_CRIT "fail to align clk\n");
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. int clk_enable(struct clk *clk)
  76. {
  77. int ret = -EIO;
  78. if (clk->ops && clk->ops->enable)
  79. ret = clk->ops->enable(clk);
  80. return ret;
  81. }
  82. EXPORT_SYMBOL(clk_enable);
  83. void clk_disable(struct clk *clk)
  84. {
  85. if (clk->ops && clk->ops->disable)
  86. clk->ops->disable(clk);
  87. }
  88. EXPORT_SYMBOL(clk_disable);
  89. unsigned long clk_get_rate(struct clk *clk)
  90. {
  91. unsigned long ret = 0;
  92. if (clk->ops && clk->ops->get_rate)
  93. ret = clk->ops->get_rate(clk);
  94. return ret;
  95. }
  96. EXPORT_SYMBOL(clk_get_rate);
  97. long clk_round_rate(struct clk *clk, unsigned long rate)
  98. {
  99. long ret = 0;
  100. if (clk->ops && clk->ops->round_rate)
  101. ret = clk->ops->round_rate(clk, rate);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(clk_round_rate);
  105. int clk_set_rate(struct clk *clk, unsigned long rate)
  106. {
  107. int ret = -EIO;
  108. if (clk->ops && clk->ops->set_rate)
  109. ret = clk->ops->set_rate(clk, rate);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL(clk_set_rate);
  113. unsigned long vco_get_rate(struct clk *clk)
  114. {
  115. return clk->rate;
  116. }
  117. unsigned long pll_get_rate(struct clk *clk)
  118. {
  119. u32 df;
  120. u32 msel;
  121. u32 ctl = bfin_read32(CGU0_CTL);
  122. u32 stat = bfin_read32(CGU0_STAT);
  123. if (stat & CGU0_STAT_PLLBP)
  124. return 0;
  125. msel = (ctl & CGU0_CTL_MSEL_MASK) >> CGU0_CTL_MSEL_SHIFT;
  126. df = (ctl & CGU0_CTL_DF);
  127. clk->parent->rate = clk_get_rate(clk->parent);
  128. return clk->parent->rate / (df + 1) * msel * 2;
  129. }
  130. unsigned long pll_round_rate(struct clk *clk, unsigned long rate)
  131. {
  132. u32 div;
  133. div = rate / clk->parent->rate;
  134. return clk->parent->rate * div;
  135. }
  136. int pll_set_rate(struct clk *clk, unsigned long rate)
  137. {
  138. u32 msel;
  139. u32 stat = bfin_read32(CGU0_STAT);
  140. if (!(stat & CGU0_STAT_PLLEN))
  141. return -EBUSY;
  142. if (!(stat & CGU0_STAT_PLLLK))
  143. return -EBUSY;
  144. if (wait_for_pll_align())
  145. return -EBUSY;
  146. msel = rate / clk->parent->rate / 2;
  147. clk_reg_write_mask(CGU0_CTL, msel << CGU0_CTL_MSEL_SHIFT,
  148. CGU0_CTL_MSEL_MASK);
  149. clk->rate = rate;
  150. return 0;
  151. }
  152. unsigned long cclk_get_rate(struct clk *clk)
  153. {
  154. if (clk->parent)
  155. return clk->parent->rate;
  156. else
  157. return 0;
  158. }
  159. unsigned long sys_clk_get_rate(struct clk *clk)
  160. {
  161. unsigned long drate;
  162. u32 msel;
  163. u32 df;
  164. u32 ctl = bfin_read32(CGU0_CTL);
  165. u32 div = bfin_read32(CGU0_DIV);
  166. div = (div & clk->mask) >> clk->shift;
  167. msel = (ctl & CGU0_CTL_MSEL_MASK) >> CGU0_CTL_MSEL_SHIFT;
  168. df = (ctl & CGU0_CTL_DF);
  169. if (!strcmp(clk->parent->name, "SYS_CLKIN")) {
  170. drate = clk->parent->rate / (df + 1);
  171. drate *= msel;
  172. drate /= div;
  173. return drate;
  174. } else {
  175. clk->parent->rate = clk_get_rate(clk->parent);
  176. return clk->parent->rate / div;
  177. }
  178. }
  179. unsigned long dummy_get_rate(struct clk *clk)
  180. {
  181. clk->parent->rate = clk_get_rate(clk->parent);
  182. return clk->parent->rate;
  183. }
  184. unsigned long sys_clk_round_rate(struct clk *clk, unsigned long rate)
  185. {
  186. unsigned long max_rate;
  187. unsigned long drate;
  188. int i;
  189. u32 msel;
  190. u32 df;
  191. u32 ctl = bfin_read32(CGU0_CTL);
  192. msel = (ctl & CGU0_CTL_MSEL_MASK) >> CGU0_CTL_MSEL_SHIFT;
  193. df = (ctl & CGU0_CTL_DF);
  194. max_rate = clk->parent->rate / (df + 1) * msel;
  195. if (rate > max_rate)
  196. return 0;
  197. for (i = 1; i < clk->mask; i++) {
  198. drate = max_rate / i;
  199. if (rate >= drate)
  200. return drate;
  201. }
  202. return 0;
  203. }
  204. int sys_clk_set_rate(struct clk *clk, unsigned long rate)
  205. {
  206. u32 div = bfin_read32(CGU0_DIV);
  207. div = (div & clk->mask) >> clk->shift;
  208. rate = clk_round_rate(clk, rate);
  209. if (!rate)
  210. return -EINVAL;
  211. div = (clk_get_rate(clk) * div) / rate;
  212. if (wait_for_pll_align())
  213. return -EBUSY;
  214. clk_reg_write_mask(CGU0_DIV, div << clk->shift,
  215. clk->mask);
  216. clk->rate = rate;
  217. return 0;
  218. }
  219. static struct clk_ops vco_ops = {
  220. .get_rate = vco_get_rate,
  221. };
  222. static struct clk_ops pll_ops = {
  223. .get_rate = pll_get_rate,
  224. .set_rate = pll_set_rate,
  225. };
  226. static struct clk_ops cclk_ops = {
  227. .get_rate = cclk_get_rate,
  228. };
  229. static struct clk_ops sys_clk_ops = {
  230. .get_rate = sys_clk_get_rate,
  231. .set_rate = sys_clk_set_rate,
  232. .round_rate = sys_clk_round_rate,
  233. };
  234. static struct clk_ops dummy_clk_ops = {
  235. .get_rate = dummy_get_rate,
  236. };
  237. static struct clk sys_clkin = {
  238. .name = "SYS_CLKIN",
  239. .rate = CONFIG_CLKIN_HZ,
  240. .ops = &vco_ops,
  241. };
  242. static struct clk pll_clk = {
  243. .name = "PLLCLK",
  244. .rate = 500000000,
  245. .parent = &sys_clkin,
  246. .ops = &pll_ops,
  247. .flags = NEEDS_INITIALIZATION,
  248. };
  249. static struct clk cclk = {
  250. .name = "CCLK",
  251. .rate = 500000000,
  252. .mask = CGU0_DIV_CSEL_MASK,
  253. .shift = CGU0_DIV_CSEL_SHIFT,
  254. .parent = &sys_clkin,
  255. .ops = &sys_clk_ops,
  256. .flags = NEEDS_INITIALIZATION,
  257. };
  258. static struct clk cclk0 = {
  259. .name = "CCLK0",
  260. .parent = &cclk,
  261. .ops = &cclk_ops,
  262. };
  263. static struct clk cclk1 = {
  264. .name = "CCLK1",
  265. .parent = &cclk,
  266. .ops = &cclk_ops,
  267. };
  268. static struct clk sysclk = {
  269. .name = "SYSCLK",
  270. .rate = 500000000,
  271. .mask = CGU0_DIV_SYSSEL_MASK,
  272. .shift = CGU0_DIV_SYSSEL_SHIFT,
  273. .parent = &sys_clkin,
  274. .ops = &sys_clk_ops,
  275. .flags = NEEDS_INITIALIZATION,
  276. };
  277. static struct clk sclk0 = {
  278. .name = "SCLK0",
  279. .rate = 500000000,
  280. .mask = CGU0_DIV_S0SEL_MASK,
  281. .shift = CGU0_DIV_S0SEL_SHIFT,
  282. .parent = &sysclk,
  283. .ops = &sys_clk_ops,
  284. };
  285. static struct clk sclk1 = {
  286. .name = "SCLK1",
  287. .rate = 500000000,
  288. .mask = CGU0_DIV_S1SEL_MASK,
  289. .shift = CGU0_DIV_S1SEL_SHIFT,
  290. .parent = &sysclk,
  291. .ops = &sys_clk_ops,
  292. };
  293. static struct clk dclk = {
  294. .name = "DCLK",
  295. .rate = 500000000,
  296. .mask = CGU0_DIV_DSEL_MASK,
  297. .shift = CGU0_DIV_DSEL_SHIFT,
  298. .parent = &sys_clkin,
  299. .ops = &sys_clk_ops,
  300. };
  301. static struct clk oclk = {
  302. .name = "OCLK",
  303. .rate = 500000000,
  304. .mask = CGU0_DIV_OSEL_MASK,
  305. .shift = CGU0_DIV_OSEL_SHIFT,
  306. .parent = &pll_clk,
  307. };
  308. static struct clk ethclk = {
  309. .name = "stmmaceth",
  310. .parent = &sclk0,
  311. .ops = &dummy_clk_ops,
  312. };
  313. static struct clk ethpclk = {
  314. .name = "pclk",
  315. .parent = &sclk0,
  316. .ops = &dummy_clk_ops,
  317. };
  318. static struct clk spiclk = {
  319. .name = "spi",
  320. .parent = &sclk1,
  321. .ops = &dummy_clk_ops,
  322. };
  323. static struct clk_lookup bf609_clks[] = {
  324. CLK(sys_clkin, NULL, "SYS_CLKIN"),
  325. CLK(pll_clk, NULL, "PLLCLK"),
  326. CLK(cclk, NULL, "CCLK"),
  327. CLK(cclk0, NULL, "CCLK0"),
  328. CLK(cclk1, NULL, "CCLK1"),
  329. CLK(sysclk, NULL, "SYSCLK"),
  330. CLK(sclk0, NULL, "SCLK0"),
  331. CLK(sclk1, NULL, "SCLK1"),
  332. CLK(dclk, NULL, "DCLK"),
  333. CLK(oclk, NULL, "OCLK"),
  334. CLK(ethclk, NULL, "stmmaceth"),
  335. CLK(ethpclk, NULL, "pclk"),
  336. CLK(spiclk, NULL, "spi"),
  337. };
  338. int __init clk_init(void)
  339. {
  340. int i;
  341. struct clk *clkp;
  342. for (i = 0; i < ARRAY_SIZE(bf609_clks); i++) {
  343. clkp = bf609_clks[i].clk;
  344. if (clkp->flags & NEEDS_INITIALIZATION)
  345. clk_get_rate(clkp);
  346. }
  347. clkdev_add_table(bf609_clks, ARRAY_SIZE(bf609_clks));
  348. return 0;
  349. }