clock.c 9.6 KB

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