clock.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* linux/arch/arm/plat-s3c24xx/clock.c
  2. *
  3. * Copyright 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/device.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/ioport.h>
  38. #include <linux/clk.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/io.h>
  41. #if defined(CONFIG_DEBUG_FS)
  42. #include <linux/debugfs.h>
  43. #endif
  44. #include <mach/hardware.h>
  45. #include <asm/irq.h>
  46. #include <plat/cpu-freq.h>
  47. #include <plat/clock.h>
  48. #include <plat/cpu.h>
  49. #include <linux/serial_core.h>
  50. #include <plat/regs-serial.h> /* for s3c24xx_uart_devs */
  51. /* clock information */
  52. static LIST_HEAD(clocks);
  53. /* We originally used an mutex here, but some contexts (see resume)
  54. * are calling functions such as clk_set_parent() with IRQs disabled
  55. * causing an BUG to be triggered.
  56. */
  57. DEFINE_SPINLOCK(clocks_lock);
  58. /* Global watchdog clock used by arch_wtd_reset() callback */
  59. struct clk *s3c2410_wdtclk;
  60. static int __init s3c_wdt_reset_init(void)
  61. {
  62. s3c2410_wdtclk = clk_get(NULL, "watchdog");
  63. if (IS_ERR(s3c2410_wdtclk))
  64. printk(KERN_WARNING "%s: warning: cannot get watchdog clock\n", __func__);
  65. return 0;
  66. }
  67. arch_initcall(s3c_wdt_reset_init);
  68. /* enable and disable calls for use with the clk struct */
  69. static int clk_null_enable(struct clk *clk, int enable)
  70. {
  71. return 0;
  72. }
  73. int clk_enable(struct clk *clk)
  74. {
  75. if (IS_ERR(clk) || clk == NULL)
  76. return -EINVAL;
  77. clk_enable(clk->parent);
  78. spin_lock(&clocks_lock);
  79. if ((clk->usage++) == 0)
  80. (clk->enable)(clk, 1);
  81. spin_unlock(&clocks_lock);
  82. return 0;
  83. }
  84. void clk_disable(struct clk *clk)
  85. {
  86. if (IS_ERR(clk) || clk == NULL)
  87. return;
  88. spin_lock(&clocks_lock);
  89. if ((--clk->usage) == 0)
  90. (clk->enable)(clk, 0);
  91. spin_unlock(&clocks_lock);
  92. clk_disable(clk->parent);
  93. }
  94. unsigned long clk_get_rate(struct clk *clk)
  95. {
  96. if (IS_ERR(clk))
  97. return 0;
  98. if (clk->rate != 0)
  99. return clk->rate;
  100. if (clk->ops != NULL && clk->ops->get_rate != NULL)
  101. return (clk->ops->get_rate)(clk);
  102. if (clk->parent != NULL)
  103. return clk_get_rate(clk->parent);
  104. return clk->rate;
  105. }
  106. long clk_round_rate(struct clk *clk, unsigned long rate)
  107. {
  108. if (!IS_ERR(clk) && clk->ops && clk->ops->round_rate)
  109. return (clk->ops->round_rate)(clk, rate);
  110. return rate;
  111. }
  112. int clk_set_rate(struct clk *clk, unsigned long rate)
  113. {
  114. int ret;
  115. if (IS_ERR(clk))
  116. return -EINVAL;
  117. /* We do not default just do a clk->rate = rate as
  118. * the clock may have been made this way by choice.
  119. */
  120. WARN_ON(clk->ops == NULL);
  121. WARN_ON(clk->ops && clk->ops->set_rate == NULL);
  122. if (clk->ops == NULL || clk->ops->set_rate == NULL)
  123. return -EINVAL;
  124. spin_lock(&clocks_lock);
  125. ret = (clk->ops->set_rate)(clk, rate);
  126. spin_unlock(&clocks_lock);
  127. return ret;
  128. }
  129. struct clk *clk_get_parent(struct clk *clk)
  130. {
  131. return clk->parent;
  132. }
  133. int clk_set_parent(struct clk *clk, struct clk *parent)
  134. {
  135. int ret = 0;
  136. if (IS_ERR(clk))
  137. return -EINVAL;
  138. spin_lock(&clocks_lock);
  139. if (clk->ops && clk->ops->set_parent)
  140. ret = (clk->ops->set_parent)(clk, parent);
  141. spin_unlock(&clocks_lock);
  142. return ret;
  143. }
  144. EXPORT_SYMBOL(clk_enable);
  145. EXPORT_SYMBOL(clk_disable);
  146. EXPORT_SYMBOL(clk_get_rate);
  147. EXPORT_SYMBOL(clk_round_rate);
  148. EXPORT_SYMBOL(clk_set_rate);
  149. EXPORT_SYMBOL(clk_get_parent);
  150. EXPORT_SYMBOL(clk_set_parent);
  151. /* base clocks */
  152. int clk_default_setrate(struct clk *clk, unsigned long rate)
  153. {
  154. clk->rate = rate;
  155. return 0;
  156. }
  157. struct clk_ops clk_ops_def_setrate = {
  158. .set_rate = clk_default_setrate,
  159. };
  160. struct clk clk_xtal = {
  161. .name = "xtal",
  162. .rate = 0,
  163. .parent = NULL,
  164. .ctrlbit = 0,
  165. };
  166. struct clk clk_ext = {
  167. .name = "ext",
  168. };
  169. struct clk clk_epll = {
  170. .name = "epll",
  171. };
  172. struct clk clk_mpll = {
  173. .name = "mpll",
  174. .ops = &clk_ops_def_setrate,
  175. };
  176. struct clk clk_upll = {
  177. .name = "upll",
  178. .parent = NULL,
  179. .ctrlbit = 0,
  180. };
  181. struct clk clk_f = {
  182. .name = "fclk",
  183. .rate = 0,
  184. .parent = &clk_mpll,
  185. .ctrlbit = 0,
  186. };
  187. struct clk clk_h = {
  188. .name = "hclk",
  189. .rate = 0,
  190. .parent = NULL,
  191. .ctrlbit = 0,
  192. .ops = &clk_ops_def_setrate,
  193. };
  194. struct clk clk_p = {
  195. .name = "pclk",
  196. .rate = 0,
  197. .parent = NULL,
  198. .ctrlbit = 0,
  199. .ops = &clk_ops_def_setrate,
  200. };
  201. struct clk clk_usb_bus = {
  202. .name = "usb-bus",
  203. .rate = 0,
  204. .parent = &clk_upll,
  205. };
  206. struct clk s3c24xx_uclk = {
  207. .name = "uclk",
  208. };
  209. /* initialise the clock system */
  210. /**
  211. * s3c24xx_register_clock() - register a clock
  212. * @clk: The clock to register
  213. *
  214. * Add the specified clock to the list of clocks known by the system.
  215. */
  216. int s3c24xx_register_clock(struct clk *clk)
  217. {
  218. if (clk->enable == NULL)
  219. clk->enable = clk_null_enable;
  220. /* fill up the clk_lookup structure and register it*/
  221. clk->lookup.dev_id = clk->devname;
  222. clk->lookup.con_id = clk->name;
  223. clk->lookup.clk = clk;
  224. clkdev_add(&clk->lookup);
  225. return 0;
  226. }
  227. /**
  228. * s3c24xx_register_clocks() - register an array of clock pointers
  229. * @clks: Pointer to an array of struct clk pointers
  230. * @nr_clks: The number of clocks in the @clks array.
  231. *
  232. * Call s3c24xx_register_clock() for all the clock pointers contained
  233. * in the @clks list. Returns the number of failures.
  234. */
  235. int s3c24xx_register_clocks(struct clk **clks, int nr_clks)
  236. {
  237. int fails = 0;
  238. for (; nr_clks > 0; nr_clks--, clks++) {
  239. if (s3c24xx_register_clock(*clks) < 0) {
  240. struct clk *clk = *clks;
  241. printk(KERN_ERR "%s: failed to register %p: %s\n",
  242. __func__, clk, clk->name);
  243. fails++;
  244. }
  245. }
  246. return fails;
  247. }
  248. /**
  249. * s3c_register_clocks() - register an array of clocks
  250. * @clkp: Pointer to the first clock in the array.
  251. * @nr_clks: Number of clocks to register.
  252. *
  253. * Call s3c24xx_register_clock() on the @clkp array given, printing an
  254. * error if it fails to register the clock (unlikely).
  255. */
  256. void __init s3c_register_clocks(struct clk *clkp, int nr_clks)
  257. {
  258. int ret;
  259. for (; nr_clks > 0; nr_clks--, clkp++) {
  260. ret = s3c24xx_register_clock(clkp);
  261. if (ret < 0) {
  262. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  263. clkp->name, ret);
  264. }
  265. }
  266. }
  267. /**
  268. * s3c_disable_clocks() - disable an array of clocks
  269. * @clkp: Pointer to the first clock in the array.
  270. * @nr_clks: Number of clocks to register.
  271. *
  272. * for internal use only at initialisation time. disable the clocks in the
  273. * @clkp array.
  274. */
  275. void __init s3c_disable_clocks(struct clk *clkp, int nr_clks)
  276. {
  277. for (; nr_clks > 0; nr_clks--, clkp++)
  278. (clkp->enable)(clkp, 0);
  279. }
  280. /* initialise all the clocks */
  281. int __init s3c24xx_register_baseclocks(unsigned long xtal)
  282. {
  283. printk(KERN_INFO "S3C24XX Clocks, Copyright 2004 Simtec Electronics\n");
  284. clk_xtal.rate = xtal;
  285. /* register our clocks */
  286. if (s3c24xx_register_clock(&clk_xtal) < 0)
  287. printk(KERN_ERR "failed to register master xtal\n");
  288. if (s3c24xx_register_clock(&clk_mpll) < 0)
  289. printk(KERN_ERR "failed to register mpll clock\n");
  290. if (s3c24xx_register_clock(&clk_upll) < 0)
  291. printk(KERN_ERR "failed to register upll clock\n");
  292. if (s3c24xx_register_clock(&clk_f) < 0)
  293. printk(KERN_ERR "failed to register cpu fclk\n");
  294. if (s3c24xx_register_clock(&clk_h) < 0)
  295. printk(KERN_ERR "failed to register cpu hclk\n");
  296. if (s3c24xx_register_clock(&clk_p) < 0)
  297. printk(KERN_ERR "failed to register cpu pclk\n");
  298. return 0;
  299. }
  300. #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
  301. /* debugfs support to trace clock tree hierarchy and attributes */
  302. static struct dentry *clk_debugfs_root;
  303. static int clk_debugfs_register_one(struct clk *c)
  304. {
  305. int err;
  306. struct dentry *d;
  307. struct clk *pa = c->parent;
  308. char s[255];
  309. char *p = s;
  310. p += sprintf(p, "%s", c->devname);
  311. d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
  312. if (!d)
  313. return -ENOMEM;
  314. c->dent = d;
  315. d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usage);
  316. if (!d) {
  317. err = -ENOMEM;
  318. goto err_out;
  319. }
  320. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  321. if (!d) {
  322. err = -ENOMEM;
  323. goto err_out;
  324. }
  325. return 0;
  326. err_out:
  327. debugfs_remove_recursive(c->dent);
  328. return err;
  329. }
  330. static int clk_debugfs_register(struct clk *c)
  331. {
  332. int err;
  333. struct clk *pa = c->parent;
  334. if (pa && !pa->dent) {
  335. err = clk_debugfs_register(pa);
  336. if (err)
  337. return err;
  338. }
  339. if (!c->dent) {
  340. err = clk_debugfs_register_one(c);
  341. if (err)
  342. return err;
  343. }
  344. return 0;
  345. }
  346. static int __init clk_debugfs_init(void)
  347. {
  348. struct clk *c;
  349. struct dentry *d;
  350. int err;
  351. d = debugfs_create_dir("clock", NULL);
  352. if (!d)
  353. return -ENOMEM;
  354. clk_debugfs_root = d;
  355. list_for_each_entry(c, &clocks, list) {
  356. err = clk_debugfs_register(c);
  357. if (err)
  358. goto err_out;
  359. }
  360. return 0;
  361. err_out:
  362. debugfs_remove_recursive(clk_debugfs_root);
  363. return err;
  364. }
  365. late_initcall(clk_debugfs_init);
  366. #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */