clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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_OR_NULL(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_OR_NULL(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. unsigned long flags;
  117. int ret;
  118. if (IS_ERR_OR_NULL(clk))
  119. return -EINVAL;
  120. /* We do not default just do a clk->rate = rate as
  121. * the clock may have been made this way by choice.
  122. */
  123. WARN_ON(clk->ops == NULL);
  124. WARN_ON(clk->ops && clk->ops->set_rate == NULL);
  125. if (clk->ops == NULL || clk->ops->set_rate == NULL)
  126. return -EINVAL;
  127. spin_lock_irqsave(&clocks_lock, flags);
  128. ret = (clk->ops->set_rate)(clk, rate);
  129. spin_unlock_irqrestore(&clocks_lock, flags);
  130. return ret;
  131. }
  132. struct clk *clk_get_parent(struct clk *clk)
  133. {
  134. return clk->parent;
  135. }
  136. int clk_set_parent(struct clk *clk, struct clk *parent)
  137. {
  138. unsigned long flags;
  139. int ret = 0;
  140. if (IS_ERR_OR_NULL(clk) || IS_ERR_OR_NULL(parent))
  141. return -EINVAL;
  142. spin_lock_irqsave(&clocks_lock, flags);
  143. if (clk->ops && clk->ops->set_parent)
  144. ret = (clk->ops->set_parent)(clk, parent);
  145. spin_unlock_irqrestore(&clocks_lock, flags);
  146. return ret;
  147. }
  148. EXPORT_SYMBOL(clk_enable);
  149. EXPORT_SYMBOL(clk_disable);
  150. EXPORT_SYMBOL(clk_get_rate);
  151. EXPORT_SYMBOL(clk_round_rate);
  152. EXPORT_SYMBOL(clk_set_rate);
  153. EXPORT_SYMBOL(clk_get_parent);
  154. EXPORT_SYMBOL(clk_set_parent);
  155. /* base clocks */
  156. int clk_default_setrate(struct clk *clk, unsigned long rate)
  157. {
  158. clk->rate = rate;
  159. return 0;
  160. }
  161. struct clk_ops clk_ops_def_setrate = {
  162. .set_rate = clk_default_setrate,
  163. };
  164. struct clk clk_xtal = {
  165. .name = "xtal",
  166. .rate = 0,
  167. .parent = NULL,
  168. .ctrlbit = 0,
  169. };
  170. struct clk clk_ext = {
  171. .name = "ext",
  172. };
  173. struct clk clk_epll = {
  174. .name = "epll",
  175. };
  176. struct clk clk_mpll = {
  177. .name = "mpll",
  178. .ops = &clk_ops_def_setrate,
  179. };
  180. struct clk clk_upll = {
  181. .name = "upll",
  182. .parent = NULL,
  183. .ctrlbit = 0,
  184. };
  185. struct clk clk_f = {
  186. .name = "fclk",
  187. .rate = 0,
  188. .parent = &clk_mpll,
  189. .ctrlbit = 0,
  190. };
  191. struct clk clk_h = {
  192. .name = "hclk",
  193. .rate = 0,
  194. .parent = NULL,
  195. .ctrlbit = 0,
  196. .ops = &clk_ops_def_setrate,
  197. };
  198. struct clk clk_p = {
  199. .name = "pclk",
  200. .rate = 0,
  201. .parent = NULL,
  202. .ctrlbit = 0,
  203. .ops = &clk_ops_def_setrate,
  204. };
  205. struct clk clk_usb_bus = {
  206. .name = "usb-bus",
  207. .rate = 0,
  208. .parent = &clk_upll,
  209. };
  210. struct clk s3c24xx_uclk = {
  211. .name = "uclk",
  212. };
  213. /* initialise the clock system */
  214. /**
  215. * s3c24xx_register_clock() - register a clock
  216. * @clk: The clock to register
  217. *
  218. * Add the specified clock to the list of clocks known by the system.
  219. */
  220. int s3c24xx_register_clock(struct clk *clk)
  221. {
  222. if (clk->enable == NULL)
  223. clk->enable = clk_null_enable;
  224. /* fill up the clk_lookup structure and register it*/
  225. clk->lookup.dev_id = clk->devname;
  226. clk->lookup.con_id = clk->name;
  227. clk->lookup.clk = clk;
  228. clkdev_add(&clk->lookup);
  229. return 0;
  230. }
  231. /**
  232. * s3c24xx_register_clocks() - register an array of clock pointers
  233. * @clks: Pointer to an array of struct clk pointers
  234. * @nr_clks: The number of clocks in the @clks array.
  235. *
  236. * Call s3c24xx_register_clock() for all the clock pointers contained
  237. * in the @clks list. Returns the number of failures.
  238. */
  239. int s3c24xx_register_clocks(struct clk **clks, int nr_clks)
  240. {
  241. int fails = 0;
  242. for (; nr_clks > 0; nr_clks--, clks++) {
  243. if (s3c24xx_register_clock(*clks) < 0) {
  244. struct clk *clk = *clks;
  245. printk(KERN_ERR "%s: failed to register %p: %s\n",
  246. __func__, clk, clk->name);
  247. fails++;
  248. }
  249. }
  250. return fails;
  251. }
  252. /**
  253. * s3c_register_clocks() - register an array of clocks
  254. * @clkp: Pointer to the first clock in the array.
  255. * @nr_clks: Number of clocks to register.
  256. *
  257. * Call s3c24xx_register_clock() on the @clkp array given, printing an
  258. * error if it fails to register the clock (unlikely).
  259. */
  260. void __init s3c_register_clocks(struct clk *clkp, int nr_clks)
  261. {
  262. int ret;
  263. for (; nr_clks > 0; nr_clks--, clkp++) {
  264. ret = s3c24xx_register_clock(clkp);
  265. if (ret < 0) {
  266. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  267. clkp->name, ret);
  268. }
  269. }
  270. }
  271. /**
  272. * s3c_disable_clocks() - disable an array of clocks
  273. * @clkp: Pointer to the first clock in the array.
  274. * @nr_clks: Number of clocks to register.
  275. *
  276. * for internal use only at initialisation time. disable the clocks in the
  277. * @clkp array.
  278. */
  279. void __init s3c_disable_clocks(struct clk *clkp, int nr_clks)
  280. {
  281. for (; nr_clks > 0; nr_clks--, clkp++)
  282. (clkp->enable)(clkp, 0);
  283. }
  284. /* initialise all the clocks */
  285. int __init s3c24xx_register_baseclocks(unsigned long xtal)
  286. {
  287. printk(KERN_INFO "S3C24XX Clocks, Copyright 2004 Simtec Electronics\n");
  288. clk_xtal.rate = xtal;
  289. /* register our clocks */
  290. if (s3c24xx_register_clock(&clk_xtal) < 0)
  291. printk(KERN_ERR "failed to register master xtal\n");
  292. if (s3c24xx_register_clock(&clk_mpll) < 0)
  293. printk(KERN_ERR "failed to register mpll clock\n");
  294. if (s3c24xx_register_clock(&clk_upll) < 0)
  295. printk(KERN_ERR "failed to register upll clock\n");
  296. if (s3c24xx_register_clock(&clk_f) < 0)
  297. printk(KERN_ERR "failed to register cpu fclk\n");
  298. if (s3c24xx_register_clock(&clk_h) < 0)
  299. printk(KERN_ERR "failed to register cpu hclk\n");
  300. if (s3c24xx_register_clock(&clk_p) < 0)
  301. printk(KERN_ERR "failed to register cpu pclk\n");
  302. return 0;
  303. }
  304. #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
  305. /* debugfs support to trace clock tree hierarchy and attributes */
  306. static struct dentry *clk_debugfs_root;
  307. static void clock_tree_show_one(struct seq_file *s, struct clk *c, int level)
  308. {
  309. struct clk *child;
  310. const char *state;
  311. char buf[255] = { 0 };
  312. int n = 0;
  313. if (c->name)
  314. n = snprintf(buf, sizeof(buf) - 1, "%s", c->name);
  315. if (c->devname)
  316. n += snprintf(buf + n, sizeof(buf) - 1 - n, ":%s", c->devname);
  317. state = (c->usage > 0) ? "on" : "off";
  318. seq_printf(s, "%*s%-*s %-6s %-3d %-10lu\n",
  319. level * 3 + 1, "",
  320. 50 - level * 3, buf,
  321. state, c->usage, clk_get_rate(c));
  322. list_for_each_entry(child, &clocks, list) {
  323. if (child->parent != c)
  324. continue;
  325. clock_tree_show_one(s, child, level + 1);
  326. }
  327. }
  328. static int clock_tree_show(struct seq_file *s, void *data)
  329. {
  330. struct clk *c;
  331. unsigned long flags;
  332. seq_printf(s, " clock state ref rate\n");
  333. seq_printf(s, "----------------------------------------------------\n");
  334. spin_lock_irqsave(&clocks_lock, flags);
  335. list_for_each_entry(c, &clocks, list)
  336. if (c->parent == NULL)
  337. clock_tree_show_one(s, c, 0);
  338. spin_unlock_irqrestore(&clocks_lock, flags);
  339. return 0;
  340. }
  341. static int clock_tree_open(struct inode *inode, struct file *file)
  342. {
  343. return single_open(file, clock_tree_show, inode->i_private);
  344. }
  345. static const struct file_operations clock_tree_fops = {
  346. .open = clock_tree_open,
  347. .read = seq_read,
  348. .llseek = seq_lseek,
  349. .release = single_release,
  350. };
  351. static int clock_rate_show(void *data, u64 *val)
  352. {
  353. struct clk *c = data;
  354. *val = clk_get_rate(c);
  355. return 0;
  356. }
  357. DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_rate_show, NULL, "%llu\n");
  358. static int clk_debugfs_register_one(struct clk *c)
  359. {
  360. int err;
  361. struct dentry *d;
  362. struct clk *pa = c->parent;
  363. char s[255];
  364. char *p = s;
  365. p += sprintf(p, "%s", c->devname);
  366. d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
  367. if (!d)
  368. return -ENOMEM;
  369. c->dent = d;
  370. d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usage);
  371. if (!d) {
  372. err = -ENOMEM;
  373. goto err_out;
  374. }
  375. d = debugfs_create_file("rate", S_IRUGO, c->dent, c, &clock_rate_fops);
  376. if (!d) {
  377. err = -ENOMEM;
  378. goto err_out;
  379. }
  380. return 0;
  381. err_out:
  382. debugfs_remove_recursive(c->dent);
  383. return err;
  384. }
  385. static int clk_debugfs_register(struct clk *c)
  386. {
  387. int err;
  388. struct clk *pa = c->parent;
  389. if (pa && !pa->dent) {
  390. err = clk_debugfs_register(pa);
  391. if (err)
  392. return err;
  393. }
  394. if (!c->dent) {
  395. err = clk_debugfs_register_one(c);
  396. if (err)
  397. return err;
  398. }
  399. return 0;
  400. }
  401. static int __init clk_debugfs_init(void)
  402. {
  403. struct clk *c;
  404. struct dentry *d;
  405. int err = -ENOMEM;
  406. d = debugfs_create_dir("clock", NULL);
  407. if (!d)
  408. return -ENOMEM;
  409. clk_debugfs_root = d;
  410. d = debugfs_create_file("clock_tree", S_IRUGO, clk_debugfs_root, NULL,
  411. &clock_tree_fops);
  412. if (!d)
  413. goto err_out;
  414. list_for_each_entry(c, &clocks, list) {
  415. err = clk_debugfs_register(c);
  416. if (err)
  417. goto err_out;
  418. }
  419. return 0;
  420. err_out:
  421. debugfs_remove_recursive(clk_debugfs_root);
  422. return err;
  423. }
  424. late_initcall(clk_debugfs_init);
  425. #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */