clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* linux/arch/arm/plat-s3c24xx/clock.c
  2. *
  3. * Copyright (c) 2004-2005 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C24XX Core clock control support
  7. *
  8. * Based on, and code from linux/arch/arm/mach-versatile/clock.c
  9. **
  10. ** Copyright (C) 2004 ARM Limited.
  11. ** Written by Deep Blue Solutions Limited.
  12. *
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/list.h>
  32. #include <linux/errno.h>
  33. #include <linux/err.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/sysdev.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/ioport.h>
  38. #include <linux/clk.h>
  39. #include <linux/mutex.h>
  40. #include <linux/delay.h>
  41. #include <linux/io.h>
  42. #include <mach/hardware.h>
  43. #include <asm/irq.h>
  44. #include <mach/regs-clock.h>
  45. #include <mach/regs-gpio.h>
  46. #include <plat/clock.h>
  47. #include <plat/cpu.h>
  48. /* clock information */
  49. static LIST_HEAD(clocks);
  50. DEFINE_MUTEX(clocks_mutex);
  51. /* enable and disable calls for use with the clk struct */
  52. static int clk_null_enable(struct clk *clk, int enable)
  53. {
  54. return 0;
  55. }
  56. /* Clock API calls */
  57. struct clk *clk_get(struct device *dev, const char *id)
  58. {
  59. struct clk *p;
  60. struct clk *clk = ERR_PTR(-ENOENT);
  61. int idno;
  62. if (dev == NULL || dev->bus != &platform_bus_type)
  63. idno = -1;
  64. else
  65. idno = to_platform_device(dev)->id;
  66. mutex_lock(&clocks_mutex);
  67. list_for_each_entry(p, &clocks, list) {
  68. if (p->id == idno &&
  69. strcmp(id, p->name) == 0 &&
  70. try_module_get(p->owner)) {
  71. clk = p;
  72. break;
  73. }
  74. }
  75. /* check for the case where a device was supplied, but the
  76. * clock that was being searched for is not device specific */
  77. if (IS_ERR(clk)) {
  78. list_for_each_entry(p, &clocks, list) {
  79. if (p->id == -1 && strcmp(id, p->name) == 0 &&
  80. try_module_get(p->owner)) {
  81. clk = p;
  82. break;
  83. }
  84. }
  85. }
  86. mutex_unlock(&clocks_mutex);
  87. return clk;
  88. }
  89. void clk_put(struct clk *clk)
  90. {
  91. module_put(clk->owner);
  92. }
  93. int clk_enable(struct clk *clk)
  94. {
  95. if (IS_ERR(clk) || clk == NULL)
  96. return -EINVAL;
  97. clk_enable(clk->parent);
  98. mutex_lock(&clocks_mutex);
  99. if ((clk->usage++) == 0)
  100. (clk->enable)(clk, 1);
  101. mutex_unlock(&clocks_mutex);
  102. return 0;
  103. }
  104. void clk_disable(struct clk *clk)
  105. {
  106. if (IS_ERR(clk) || clk == NULL)
  107. return;
  108. mutex_lock(&clocks_mutex);
  109. if ((--clk->usage) == 0)
  110. (clk->enable)(clk, 0);
  111. mutex_unlock(&clocks_mutex);
  112. clk_disable(clk->parent);
  113. }
  114. unsigned long clk_get_rate(struct clk *clk)
  115. {
  116. if (IS_ERR(clk))
  117. return 0;
  118. if (clk->rate != 0)
  119. return clk->rate;
  120. if (clk->get_rate != NULL)
  121. return (clk->get_rate)(clk);
  122. if (clk->parent != NULL)
  123. return clk_get_rate(clk->parent);
  124. return clk->rate;
  125. }
  126. long clk_round_rate(struct clk *clk, unsigned long rate)
  127. {
  128. if (!IS_ERR(clk) && clk->round_rate)
  129. return (clk->round_rate)(clk, rate);
  130. return rate;
  131. }
  132. int clk_set_rate(struct clk *clk, unsigned long rate)
  133. {
  134. int ret;
  135. if (IS_ERR(clk))
  136. return -EINVAL;
  137. /* We do not default just do a clk->rate = rate as
  138. * the clock may have been made this way by choice.
  139. */
  140. WARN_ON(clk->set_rate == NULL);
  141. if (clk->set_rate == NULL)
  142. return -EINVAL;
  143. mutex_lock(&clocks_mutex);
  144. ret = (clk->set_rate)(clk, rate);
  145. mutex_unlock(&clocks_mutex);
  146. return ret;
  147. }
  148. struct clk *clk_get_parent(struct clk *clk)
  149. {
  150. return clk->parent;
  151. }
  152. int clk_set_parent(struct clk *clk, struct clk *parent)
  153. {
  154. int ret = 0;
  155. if (IS_ERR(clk))
  156. return -EINVAL;
  157. mutex_lock(&clocks_mutex);
  158. if (clk->set_parent)
  159. ret = (clk->set_parent)(clk, parent);
  160. mutex_unlock(&clocks_mutex);
  161. return ret;
  162. }
  163. EXPORT_SYMBOL(clk_get);
  164. EXPORT_SYMBOL(clk_put);
  165. EXPORT_SYMBOL(clk_enable);
  166. EXPORT_SYMBOL(clk_disable);
  167. EXPORT_SYMBOL(clk_get_rate);
  168. EXPORT_SYMBOL(clk_round_rate);
  169. EXPORT_SYMBOL(clk_set_rate);
  170. EXPORT_SYMBOL(clk_get_parent);
  171. EXPORT_SYMBOL(clk_set_parent);
  172. /* base clocks */
  173. static int clk_default_setrate(struct clk *clk, unsigned long rate)
  174. {
  175. clk->rate = rate;
  176. return 0;
  177. }
  178. struct clk clk_xtal = {
  179. .name = "xtal",
  180. .id = -1,
  181. .rate = 0,
  182. .parent = NULL,
  183. .ctrlbit = 0,
  184. };
  185. struct clk clk_mpll = {
  186. .name = "mpll",
  187. .id = -1,
  188. .set_rate = clk_default_setrate,
  189. };
  190. struct clk clk_upll = {
  191. .name = "upll",
  192. .id = -1,
  193. .parent = NULL,
  194. .ctrlbit = 0,
  195. };
  196. struct clk clk_f = {
  197. .name = "fclk",
  198. .id = -1,
  199. .rate = 0,
  200. .parent = &clk_mpll,
  201. .ctrlbit = 0,
  202. .set_rate = clk_default_setrate,
  203. };
  204. struct clk clk_h = {
  205. .name = "hclk",
  206. .id = -1,
  207. .rate = 0,
  208. .parent = NULL,
  209. .ctrlbit = 0,
  210. .set_rate = clk_default_setrate,
  211. };
  212. struct clk clk_p = {
  213. .name = "pclk",
  214. .id = -1,
  215. .rate = 0,
  216. .parent = NULL,
  217. .ctrlbit = 0,
  218. .set_rate = clk_default_setrate,
  219. };
  220. struct clk clk_usb_bus = {
  221. .name = "usb-bus",
  222. .id = -1,
  223. .rate = 0,
  224. .parent = &clk_upll,
  225. };
  226. /* clocks that could be registered by external code */
  227. static int s3c24xx_dclk_enable(struct clk *clk, int enable)
  228. {
  229. unsigned long dclkcon = __raw_readl(S3C24XX_DCLKCON);
  230. if (enable)
  231. dclkcon |= clk->ctrlbit;
  232. else
  233. dclkcon &= ~clk->ctrlbit;
  234. __raw_writel(dclkcon, S3C24XX_DCLKCON);
  235. return 0;
  236. }
  237. static int s3c24xx_dclk_setparent(struct clk *clk, struct clk *parent)
  238. {
  239. unsigned long dclkcon;
  240. unsigned int uclk;
  241. if (parent == &clk_upll)
  242. uclk = 1;
  243. else if (parent == &clk_p)
  244. uclk = 0;
  245. else
  246. return -EINVAL;
  247. clk->parent = parent;
  248. dclkcon = __raw_readl(S3C24XX_DCLKCON);
  249. if (clk->ctrlbit == S3C2410_DCLKCON_DCLK0EN) {
  250. if (uclk)
  251. dclkcon |= S3C2410_DCLKCON_DCLK0_UCLK;
  252. else
  253. dclkcon &= ~S3C2410_DCLKCON_DCLK0_UCLK;
  254. } else {
  255. if (uclk)
  256. dclkcon |= S3C2410_DCLKCON_DCLK1_UCLK;
  257. else
  258. dclkcon &= ~S3C2410_DCLKCON_DCLK1_UCLK;
  259. }
  260. __raw_writel(dclkcon, S3C24XX_DCLKCON);
  261. return 0;
  262. }
  263. static unsigned long s3c24xx_calc_div(struct clk *clk, unsigned long rate)
  264. {
  265. unsigned long div;
  266. if ((rate == 0) || !clk->parent)
  267. return 0;
  268. div = clk_get_rate(clk->parent) / rate;
  269. if (div < 2)
  270. div = 2;
  271. else if (div > 16)
  272. div = 16;
  273. return div;
  274. }
  275. static unsigned long s3c24xx_round_dclk_rate(struct clk *clk,
  276. unsigned long rate)
  277. {
  278. unsigned long div = s3c24xx_calc_div(clk, rate);
  279. if (div == 0)
  280. return 0;
  281. return clk_get_rate(clk->parent) / div;
  282. }
  283. static int s3c24xx_set_dclk_rate(struct clk *clk, unsigned long rate)
  284. {
  285. unsigned long mask, data, div = s3c24xx_calc_div(clk, rate);
  286. if (div == 0)
  287. return -EINVAL;
  288. if (clk == &s3c24xx_dclk0) {
  289. mask = S3C2410_DCLKCON_DCLK0_DIV_MASK |
  290. S3C2410_DCLKCON_DCLK0_CMP_MASK;
  291. data = S3C2410_DCLKCON_DCLK0_DIV(div) |
  292. S3C2410_DCLKCON_DCLK0_CMP((div + 1) / 2);
  293. } else if (clk == &s3c24xx_dclk1) {
  294. mask = S3C2410_DCLKCON_DCLK1_DIV_MASK |
  295. S3C2410_DCLKCON_DCLK1_CMP_MASK;
  296. data = S3C2410_DCLKCON_DCLK1_DIV(div) |
  297. S3C2410_DCLKCON_DCLK1_CMP((div + 1) / 2);
  298. } else
  299. return -EINVAL;
  300. clk->rate = clk_get_rate(clk->parent) / div;
  301. __raw_writel(((__raw_readl(S3C24XX_DCLKCON) & ~mask) | data),
  302. S3C24XX_DCLKCON);
  303. return clk->rate;
  304. }
  305. static int s3c24xx_clkout_setparent(struct clk *clk, struct clk *parent)
  306. {
  307. unsigned long mask;
  308. unsigned long source;
  309. /* calculate the MISCCR setting for the clock */
  310. if (parent == &clk_xtal)
  311. source = S3C2410_MISCCR_CLK0_MPLL;
  312. else if (parent == &clk_upll)
  313. source = S3C2410_MISCCR_CLK0_UPLL;
  314. else if (parent == &clk_f)
  315. source = S3C2410_MISCCR_CLK0_FCLK;
  316. else if (parent == &clk_h)
  317. source = S3C2410_MISCCR_CLK0_HCLK;
  318. else if (parent == &clk_p)
  319. source = S3C2410_MISCCR_CLK0_PCLK;
  320. else if (clk == &s3c24xx_clkout0 && parent == &s3c24xx_dclk0)
  321. source = S3C2410_MISCCR_CLK0_DCLK0;
  322. else if (clk == &s3c24xx_clkout1 && parent == &s3c24xx_dclk1)
  323. source = S3C2410_MISCCR_CLK0_DCLK0;
  324. else
  325. return -EINVAL;
  326. clk->parent = parent;
  327. if (clk == &s3c24xx_clkout0)
  328. mask = S3C2410_MISCCR_CLK0_MASK;
  329. else {
  330. source <<= 4;
  331. mask = S3C2410_MISCCR_CLK1_MASK;
  332. }
  333. s3c2410_modify_misccr(mask, source);
  334. return 0;
  335. }
  336. /* external clock definitions */
  337. struct clk s3c24xx_dclk0 = {
  338. .name = "dclk0",
  339. .id = -1,
  340. .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
  341. .enable = s3c24xx_dclk_enable,
  342. .set_parent = s3c24xx_dclk_setparent,
  343. .set_rate = s3c24xx_set_dclk_rate,
  344. .round_rate = s3c24xx_round_dclk_rate,
  345. };
  346. struct clk s3c24xx_dclk1 = {
  347. .name = "dclk1",
  348. .id = -1,
  349. .ctrlbit = S3C2410_DCLKCON_DCLK1EN,
  350. .enable = s3c24xx_dclk_enable,
  351. .set_parent = s3c24xx_dclk_setparent,
  352. .set_rate = s3c24xx_set_dclk_rate,
  353. .round_rate = s3c24xx_round_dclk_rate,
  354. };
  355. struct clk s3c24xx_clkout0 = {
  356. .name = "clkout0",
  357. .id = -1,
  358. .set_parent = s3c24xx_clkout_setparent,
  359. };
  360. struct clk s3c24xx_clkout1 = {
  361. .name = "clkout1",
  362. .id = -1,
  363. .set_parent = s3c24xx_clkout_setparent,
  364. };
  365. struct clk s3c24xx_uclk = {
  366. .name = "uclk",
  367. .id = -1,
  368. };
  369. /* initialise the clock system */
  370. int s3c24xx_register_clock(struct clk *clk)
  371. {
  372. clk->owner = THIS_MODULE;
  373. if (clk->enable == NULL)
  374. clk->enable = clk_null_enable;
  375. /* add to the list of available clocks */
  376. mutex_lock(&clocks_mutex);
  377. list_add(&clk->list, &clocks);
  378. mutex_unlock(&clocks_mutex);
  379. return 0;
  380. }
  381. int s3c24xx_register_clocks(struct clk **clks, int nr_clks)
  382. {
  383. int fails = 0;
  384. for (; nr_clks > 0; nr_clks--, clks++) {
  385. if (s3c24xx_register_clock(*clks) < 0)
  386. fails++;
  387. }
  388. return fails;
  389. }
  390. /* initalise all the clocks */
  391. int __init s3c24xx_setup_clocks(unsigned long xtal,
  392. unsigned long fclk,
  393. unsigned long hclk,
  394. unsigned long pclk)
  395. {
  396. printk(KERN_INFO "S3C24XX Clocks, (c) 2004 Simtec Electronics\n");
  397. /* initialise the main system clocks */
  398. clk_xtal.rate = xtal;
  399. clk_upll.rate = s3c2410_get_pll(__raw_readl(S3C2410_UPLLCON), xtal);
  400. clk_mpll.rate = fclk;
  401. clk_h.rate = hclk;
  402. clk_p.rate = pclk;
  403. clk_f.rate = fclk;
  404. /* assume uart clocks are correctly setup */
  405. /* register our clocks */
  406. if (s3c24xx_register_clock(&clk_xtal) < 0)
  407. printk(KERN_ERR "failed to register master xtal\n");
  408. if (s3c24xx_register_clock(&clk_mpll) < 0)
  409. printk(KERN_ERR "failed to register mpll clock\n");
  410. if (s3c24xx_register_clock(&clk_upll) < 0)
  411. printk(KERN_ERR "failed to register upll clock\n");
  412. if (s3c24xx_register_clock(&clk_f) < 0)
  413. printk(KERN_ERR "failed to register cpu fclk\n");
  414. if (s3c24xx_register_clock(&clk_h) < 0)
  415. printk(KERN_ERR "failed to register cpu hclk\n");
  416. if (s3c24xx_register_clock(&clk_p) < 0)
  417. printk(KERN_ERR "failed to register cpu pclk\n");
  418. return 0;
  419. }