clock.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/spinlock.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. #include <plat/pll.h>
  49. /* clock information */
  50. static LIST_HEAD(clocks);
  51. /* We originally used an mutex here, but some contexts (see resume)
  52. * are calling functions such as clk_set_parent() with IRQs disabled
  53. * causing an BUG to be triggered.
  54. */
  55. DEFINE_SPINLOCK(clocks_lock);
  56. /* enable and disable calls for use with the clk struct */
  57. static int clk_null_enable(struct clk *clk, int enable)
  58. {
  59. return 0;
  60. }
  61. /* Clock API calls */
  62. struct clk *clk_get(struct device *dev, const char *id)
  63. {
  64. struct clk *p;
  65. struct clk *clk = ERR_PTR(-ENOENT);
  66. int idno;
  67. if (dev == NULL || dev->bus != &platform_bus_type)
  68. idno = -1;
  69. else
  70. idno = to_platform_device(dev)->id;
  71. spin_lock(&clocks_lock);
  72. list_for_each_entry(p, &clocks, list) {
  73. if (p->id == idno &&
  74. strcmp(id, p->name) == 0 &&
  75. try_module_get(p->owner)) {
  76. clk = p;
  77. break;
  78. }
  79. }
  80. /* check for the case where a device was supplied, but the
  81. * clock that was being searched for is not device specific */
  82. if (IS_ERR(clk)) {
  83. list_for_each_entry(p, &clocks, list) {
  84. if (p->id == -1 && strcmp(id, p->name) == 0 &&
  85. try_module_get(p->owner)) {
  86. clk = p;
  87. break;
  88. }
  89. }
  90. }
  91. spin_unlock(&clocks_lock);
  92. return clk;
  93. }
  94. void clk_put(struct clk *clk)
  95. {
  96. module_put(clk->owner);
  97. }
  98. int clk_enable(struct clk *clk)
  99. {
  100. if (IS_ERR(clk) || clk == NULL)
  101. return -EINVAL;
  102. clk_enable(clk->parent);
  103. spin_lock(&clocks_lock);
  104. if ((clk->usage++) == 0)
  105. (clk->enable)(clk, 1);
  106. spin_unlock(&clocks_lock);
  107. return 0;
  108. }
  109. void clk_disable(struct clk *clk)
  110. {
  111. if (IS_ERR(clk) || clk == NULL)
  112. return;
  113. spin_lock(&clocks_lock);
  114. if ((--clk->usage) == 0)
  115. (clk->enable)(clk, 0);
  116. spin_unlock(&clocks_lock);
  117. clk_disable(clk->parent);
  118. }
  119. unsigned long clk_get_rate(struct clk *clk)
  120. {
  121. if (IS_ERR(clk))
  122. return 0;
  123. if (clk->rate != 0)
  124. return clk->rate;
  125. if (clk->get_rate != NULL)
  126. return (clk->get_rate)(clk);
  127. if (clk->parent != NULL)
  128. return clk_get_rate(clk->parent);
  129. return clk->rate;
  130. }
  131. long clk_round_rate(struct clk *clk, unsigned long rate)
  132. {
  133. if (!IS_ERR(clk) && clk->round_rate)
  134. return (clk->round_rate)(clk, rate);
  135. return rate;
  136. }
  137. int clk_set_rate(struct clk *clk, unsigned long rate)
  138. {
  139. int ret;
  140. if (IS_ERR(clk))
  141. return -EINVAL;
  142. /* We do not default just do a clk->rate = rate as
  143. * the clock may have been made this way by choice.
  144. */
  145. WARN_ON(clk->set_rate == NULL);
  146. if (clk->set_rate == NULL)
  147. return -EINVAL;
  148. spin_lock(&clocks_lock);
  149. ret = (clk->set_rate)(clk, rate);
  150. spin_unlock(&clocks_lock);
  151. return ret;
  152. }
  153. struct clk *clk_get_parent(struct clk *clk)
  154. {
  155. return clk->parent;
  156. }
  157. int clk_set_parent(struct clk *clk, struct clk *parent)
  158. {
  159. int ret = 0;
  160. if (IS_ERR(clk))
  161. return -EINVAL;
  162. spin_lock(&clocks_lock);
  163. if (clk->set_parent)
  164. ret = (clk->set_parent)(clk, parent);
  165. spin_unlock(&clocks_lock);
  166. return ret;
  167. }
  168. EXPORT_SYMBOL(clk_get);
  169. EXPORT_SYMBOL(clk_put);
  170. EXPORT_SYMBOL(clk_enable);
  171. EXPORT_SYMBOL(clk_disable);
  172. EXPORT_SYMBOL(clk_get_rate);
  173. EXPORT_SYMBOL(clk_round_rate);
  174. EXPORT_SYMBOL(clk_set_rate);
  175. EXPORT_SYMBOL(clk_get_parent);
  176. EXPORT_SYMBOL(clk_set_parent);
  177. /* base clocks */
  178. static int clk_default_setrate(struct clk *clk, unsigned long rate)
  179. {
  180. clk->rate = rate;
  181. return 0;
  182. }
  183. struct clk clk_xtal = {
  184. .name = "xtal",
  185. .id = -1,
  186. .rate = 0,
  187. .parent = NULL,
  188. .ctrlbit = 0,
  189. };
  190. struct clk clk_mpll = {
  191. .name = "mpll",
  192. .id = -1,
  193. .set_rate = clk_default_setrate,
  194. };
  195. struct clk clk_upll = {
  196. .name = "upll",
  197. .id = -1,
  198. .parent = NULL,
  199. .ctrlbit = 0,
  200. };
  201. struct clk clk_f = {
  202. .name = "fclk",
  203. .id = -1,
  204. .rate = 0,
  205. .parent = &clk_mpll,
  206. .ctrlbit = 0,
  207. .set_rate = clk_default_setrate,
  208. };
  209. struct clk clk_h = {
  210. .name = "hclk",
  211. .id = -1,
  212. .rate = 0,
  213. .parent = NULL,
  214. .ctrlbit = 0,
  215. .set_rate = clk_default_setrate,
  216. };
  217. struct clk clk_p = {
  218. .name = "pclk",
  219. .id = -1,
  220. .rate = 0,
  221. .parent = NULL,
  222. .ctrlbit = 0,
  223. .set_rate = clk_default_setrate,
  224. };
  225. struct clk clk_usb_bus = {
  226. .name = "usb-bus",
  227. .id = -1,
  228. .rate = 0,
  229. .parent = &clk_upll,
  230. };
  231. struct clk s3c24xx_uclk = {
  232. .name = "uclk",
  233. .id = -1,
  234. };
  235. /* initialise the clock system */
  236. int s3c24xx_register_clock(struct clk *clk)
  237. {
  238. clk->owner = THIS_MODULE;
  239. if (clk->enable == NULL)
  240. clk->enable = clk_null_enable;
  241. /* add to the list of available clocks */
  242. spin_lock(&clocks_lock);
  243. list_add(&clk->list, &clocks);
  244. spin_unlock(&clocks_lock);
  245. return 0;
  246. }
  247. int s3c24xx_register_clocks(struct clk **clks, int nr_clks)
  248. {
  249. int fails = 0;
  250. for (; nr_clks > 0; nr_clks--, clks++) {
  251. if (s3c24xx_register_clock(*clks) < 0)
  252. fails++;
  253. }
  254. return fails;
  255. }
  256. /* initalise all the clocks */
  257. int __init s3c24xx_setup_clocks(unsigned long xtal,
  258. unsigned long fclk,
  259. unsigned long hclk,
  260. unsigned long pclk)
  261. {
  262. printk(KERN_INFO "S3C24XX Clocks, (c) 2004 Simtec Electronics\n");
  263. /* initialise the main system clocks */
  264. clk_xtal.rate = xtal;
  265. clk_upll.rate = s3c24xx_get_pll(__raw_readl(S3C2410_UPLLCON), xtal);
  266. clk_mpll.rate = fclk;
  267. clk_h.rate = hclk;
  268. clk_p.rate = pclk;
  269. clk_f.rate = fclk;
  270. /* assume uart clocks are correctly setup */
  271. /* register our clocks */
  272. if (s3c24xx_register_clock(&clk_xtal) < 0)
  273. printk(KERN_ERR "failed to register master xtal\n");
  274. if (s3c24xx_register_clock(&clk_mpll) < 0)
  275. printk(KERN_ERR "failed to register mpll clock\n");
  276. if (s3c24xx_register_clock(&clk_upll) < 0)
  277. printk(KERN_ERR "failed to register upll clock\n");
  278. if (s3c24xx_register_clock(&clk_f) < 0)
  279. printk(KERN_ERR "failed to register cpu fclk\n");
  280. if (s3c24xx_register_clock(&clk_h) < 0)
  281. printk(KERN_ERR "failed to register cpu hclk\n");
  282. if (s3c24xx_register_clock(&clk_p) < 0)
  283. printk(KERN_ERR "failed to register cpu pclk\n");
  284. return 0;
  285. }