clock.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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/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/io.h>
  41. #include <mach/hardware.h>
  42. #include <asm/irq.h>
  43. #include <plat/cpu-freq.h>
  44. #include <plat/clock.h>
  45. #include <plat/cpu.h>
  46. #include <linux/serial_core.h>
  47. #include <plat/regs-serial.h> /* for s3c24xx_uart_devs */
  48. /* clock information */
  49. static LIST_HEAD(clocks);
  50. /* We originally used an mutex here, but some contexts (see resume)
  51. * are calling functions such as clk_set_parent() with IRQs disabled
  52. * causing an BUG to be triggered.
  53. */
  54. DEFINE_SPINLOCK(clocks_lock);
  55. /* enable and disable calls for use with the clk struct */
  56. static int clk_null_enable(struct clk *clk, int enable)
  57. {
  58. return 0;
  59. }
  60. static int dev_is_s3c_uart(struct device *dev)
  61. {
  62. struct platform_device **pdev = s3c24xx_uart_devs;
  63. int i;
  64. for (i = 0; i < ARRAY_SIZE(s3c24xx_uart_devs); i++, pdev++)
  65. if (*pdev && dev == &(*pdev)->dev)
  66. return 1;
  67. return 0;
  68. }
  69. /*
  70. * Serial drivers call get_clock() very early, before platform bus
  71. * has been set up, this requires a special check to let them get
  72. * a proper clock
  73. */
  74. static int dev_is_platform_device(struct device *dev)
  75. {
  76. return dev->bus == &platform_bus_type ||
  77. (dev->bus == NULL && dev_is_s3c_uart(dev));
  78. }
  79. /* Clock API calls */
  80. struct clk *clk_get(struct device *dev, const char *id)
  81. {
  82. struct clk *p;
  83. struct clk *clk = ERR_PTR(-ENOENT);
  84. int idno;
  85. if (dev == NULL || !dev_is_platform_device(dev))
  86. idno = -1;
  87. else
  88. idno = to_platform_device(dev)->id;
  89. spin_lock(&clocks_lock);
  90. list_for_each_entry(p, &clocks, list) {
  91. if (p->id == idno &&
  92. strcmp(id, p->name) == 0 &&
  93. try_module_get(p->owner)) {
  94. clk = p;
  95. break;
  96. }
  97. }
  98. /* check for the case where a device was supplied, but the
  99. * clock that was being searched for is not device specific */
  100. if (IS_ERR(clk)) {
  101. list_for_each_entry(p, &clocks, list) {
  102. if (p->id == -1 && strcmp(id, p->name) == 0 &&
  103. try_module_get(p->owner)) {
  104. clk = p;
  105. break;
  106. }
  107. }
  108. }
  109. spin_unlock(&clocks_lock);
  110. return clk;
  111. }
  112. void clk_put(struct clk *clk)
  113. {
  114. module_put(clk->owner);
  115. }
  116. int clk_enable(struct clk *clk)
  117. {
  118. if (IS_ERR(clk) || clk == NULL)
  119. return -EINVAL;
  120. clk_enable(clk->parent);
  121. spin_lock(&clocks_lock);
  122. if ((clk->usage++) == 0)
  123. (clk->enable)(clk, 1);
  124. spin_unlock(&clocks_lock);
  125. return 0;
  126. }
  127. void clk_disable(struct clk *clk)
  128. {
  129. if (IS_ERR(clk) || clk == NULL)
  130. return;
  131. spin_lock(&clocks_lock);
  132. if ((--clk->usage) == 0)
  133. (clk->enable)(clk, 0);
  134. spin_unlock(&clocks_lock);
  135. clk_disable(clk->parent);
  136. }
  137. unsigned long clk_get_rate(struct clk *clk)
  138. {
  139. if (IS_ERR(clk))
  140. return 0;
  141. if (clk->rate != 0)
  142. return clk->rate;
  143. if (clk->ops != NULL && clk->ops->get_rate != NULL)
  144. return (clk->ops->get_rate)(clk);
  145. if (clk->parent != NULL)
  146. return clk_get_rate(clk->parent);
  147. return clk->rate;
  148. }
  149. long clk_round_rate(struct clk *clk, unsigned long rate)
  150. {
  151. if (!IS_ERR(clk) && clk->ops && clk->ops->round_rate)
  152. return (clk->ops->round_rate)(clk, rate);
  153. return rate;
  154. }
  155. int clk_set_rate(struct clk *clk, unsigned long rate)
  156. {
  157. int ret;
  158. if (IS_ERR(clk))
  159. return -EINVAL;
  160. /* We do not default just do a clk->rate = rate as
  161. * the clock may have been made this way by choice.
  162. */
  163. WARN_ON(clk->ops == NULL);
  164. WARN_ON(clk->ops && clk->ops->set_rate == NULL);
  165. if (clk->ops == NULL || clk->ops->set_rate == NULL)
  166. return -EINVAL;
  167. spin_lock(&clocks_lock);
  168. ret = (clk->ops->set_rate)(clk, rate);
  169. spin_unlock(&clocks_lock);
  170. return ret;
  171. }
  172. struct clk *clk_get_parent(struct clk *clk)
  173. {
  174. return clk->parent;
  175. }
  176. int clk_set_parent(struct clk *clk, struct clk *parent)
  177. {
  178. int ret = 0;
  179. if (IS_ERR(clk))
  180. return -EINVAL;
  181. spin_lock(&clocks_lock);
  182. if (clk->ops && clk->ops->set_parent)
  183. ret = (clk->ops->set_parent)(clk, parent);
  184. spin_unlock(&clocks_lock);
  185. return ret;
  186. }
  187. EXPORT_SYMBOL(clk_get);
  188. EXPORT_SYMBOL(clk_put);
  189. EXPORT_SYMBOL(clk_enable);
  190. EXPORT_SYMBOL(clk_disable);
  191. EXPORT_SYMBOL(clk_get_rate);
  192. EXPORT_SYMBOL(clk_round_rate);
  193. EXPORT_SYMBOL(clk_set_rate);
  194. EXPORT_SYMBOL(clk_get_parent);
  195. EXPORT_SYMBOL(clk_set_parent);
  196. /* base clocks */
  197. int clk_default_setrate(struct clk *clk, unsigned long rate)
  198. {
  199. clk->rate = rate;
  200. return 0;
  201. }
  202. struct clk_ops clk_ops_def_setrate = {
  203. .set_rate = clk_default_setrate,
  204. };
  205. struct clk clk_xtal = {
  206. .name = "xtal",
  207. .id = -1,
  208. .rate = 0,
  209. .parent = NULL,
  210. .ctrlbit = 0,
  211. };
  212. struct clk clk_ext = {
  213. .name = "ext",
  214. .id = -1,
  215. };
  216. struct clk clk_epll = {
  217. .name = "epll",
  218. .id = -1,
  219. };
  220. struct clk clk_mpll = {
  221. .name = "mpll",
  222. .id = -1,
  223. .ops = &clk_ops_def_setrate,
  224. };
  225. struct clk clk_upll = {
  226. .name = "upll",
  227. .id = -1,
  228. .parent = NULL,
  229. .ctrlbit = 0,
  230. };
  231. struct clk clk_f = {
  232. .name = "fclk",
  233. .id = -1,
  234. .rate = 0,
  235. .parent = &clk_mpll,
  236. .ctrlbit = 0,
  237. };
  238. struct clk clk_h = {
  239. .name = "hclk",
  240. .id = -1,
  241. .rate = 0,
  242. .parent = NULL,
  243. .ctrlbit = 0,
  244. .ops = &clk_ops_def_setrate,
  245. };
  246. struct clk clk_p = {
  247. .name = "pclk",
  248. .id = -1,
  249. .rate = 0,
  250. .parent = NULL,
  251. .ctrlbit = 0,
  252. .ops = &clk_ops_def_setrate,
  253. };
  254. struct clk clk_usb_bus = {
  255. .name = "usb-bus",
  256. .id = -1,
  257. .rate = 0,
  258. .parent = &clk_upll,
  259. };
  260. struct clk s3c24xx_uclk = {
  261. .name = "uclk",
  262. .id = -1,
  263. };
  264. /* initialise the clock system */
  265. /**
  266. * s3c24xx_register_clock() - register a clock
  267. * @clk: The clock to register
  268. *
  269. * Add the specified clock to the list of clocks known by the system.
  270. */
  271. int s3c24xx_register_clock(struct clk *clk)
  272. {
  273. if (clk->enable == NULL)
  274. clk->enable = clk_null_enable;
  275. /* add to the list of available clocks */
  276. /* Quick check to see if this clock has already been registered. */
  277. BUG_ON(clk->list.prev != clk->list.next);
  278. spin_lock(&clocks_lock);
  279. list_add(&clk->list, &clocks);
  280. spin_unlock(&clocks_lock);
  281. return 0;
  282. }
  283. /**
  284. * s3c24xx_register_clocks() - register an array of clock pointers
  285. * @clks: Pointer to an array of struct clk pointers
  286. * @nr_clks: The number of clocks in the @clks array.
  287. *
  288. * Call s3c24xx_register_clock() for all the clock pointers contained
  289. * in the @clks list. Returns the number of failures.
  290. */
  291. int s3c24xx_register_clocks(struct clk **clks, int nr_clks)
  292. {
  293. int fails = 0;
  294. for (; nr_clks > 0; nr_clks--, clks++) {
  295. if (s3c24xx_register_clock(*clks) < 0) {
  296. struct clk *clk = *clks;
  297. printk(KERN_ERR "%s: failed to register %p: %s\n",
  298. __func__, clk, clk->name);
  299. fails++;
  300. }
  301. }
  302. return fails;
  303. }
  304. /**
  305. * s3c_register_clocks() - register an array of clocks
  306. * @clkp: Pointer to the first clock in the array.
  307. * @nr_clks: Number of clocks to register.
  308. *
  309. * Call s3c24xx_register_clock() on the @clkp array given, printing an
  310. * error if it fails to register the clock (unlikely).
  311. */
  312. void __init s3c_register_clocks(struct clk *clkp, int nr_clks)
  313. {
  314. int ret;
  315. for (; nr_clks > 0; nr_clks--, clkp++) {
  316. ret = s3c24xx_register_clock(clkp);
  317. if (ret < 0) {
  318. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  319. clkp->name, ret);
  320. }
  321. }
  322. }
  323. /**
  324. * s3c_disable_clocks() - disable an array of clocks
  325. * @clkp: Pointer to the first clock in the array.
  326. * @nr_clks: Number of clocks to register.
  327. *
  328. * for internal use only at initialisation time. disable the clocks in the
  329. * @clkp array.
  330. */
  331. void __init s3c_disable_clocks(struct clk *clkp, int nr_clks)
  332. {
  333. for (; nr_clks > 0; nr_clks--, clkp++)
  334. (clkp->enable)(clkp, 0);
  335. }
  336. /* initialise all the clocks */
  337. int __init s3c24xx_register_baseclocks(unsigned long xtal)
  338. {
  339. printk(KERN_INFO "S3C24XX Clocks, Copyright 2004 Simtec Electronics\n");
  340. clk_xtal.rate = xtal;
  341. /* register our clocks */
  342. if (s3c24xx_register_clock(&clk_xtal) < 0)
  343. printk(KERN_ERR "failed to register master xtal\n");
  344. if (s3c24xx_register_clock(&clk_mpll) < 0)
  345. printk(KERN_ERR "failed to register mpll clock\n");
  346. if (s3c24xx_register_clock(&clk_upll) < 0)
  347. printk(KERN_ERR "failed to register upll clock\n");
  348. if (s3c24xx_register_clock(&clk_f) < 0)
  349. printk(KERN_ERR "failed to register cpu fclk\n");
  350. if (s3c24xx_register_clock(&clk_h) < 0)
  351. printk(KERN_ERR "failed to register cpu hclk\n");
  352. if (s3c24xx_register_clock(&clk_p) < 0)
  353. printk(KERN_ERR "failed to register cpu pclk\n");
  354. return 0;
  355. }