clock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* linux/arch/arm/mach-s3c2410/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/mutex.h>
  40. #include <linux/delay.h>
  41. #include <asm/hardware.h>
  42. #include <asm/irq.h>
  43. #include <asm/io.h>
  44. #include <asm/arch/regs-clock.h>
  45. #include <asm/arch/regs-gpio.h>
  46. #include "clock.h"
  47. #include "cpu.h"
  48. /* clock information */
  49. static LIST_HEAD(clocks);
  50. DEFINE_MUTEX(clocks_mutex);
  51. /* enable and disable calls for use with the clk struct */
  52. static int clk_null_enable(struct clk *clk, int enable)
  53. {
  54. return 0;
  55. }
  56. /* Clock API calls */
  57. struct clk *clk_get(struct device *dev, const char *id)
  58. {
  59. struct clk *p;
  60. struct clk *clk = ERR_PTR(-ENOENT);
  61. int idno;
  62. if (dev == NULL || dev->bus != &platform_bus_type)
  63. idno = -1;
  64. else
  65. idno = to_platform_device(dev)->id;
  66. mutex_lock(&clocks_mutex);
  67. list_for_each_entry(p, &clocks, list) {
  68. if (p->id == idno &&
  69. strcmp(id, p->name) == 0 &&
  70. try_module_get(p->owner)) {
  71. clk = p;
  72. break;
  73. }
  74. }
  75. /* check for the case where a device was supplied, but the
  76. * clock that was being searched for is not device specific */
  77. if (IS_ERR(clk)) {
  78. list_for_each_entry(p, &clocks, list) {
  79. if (p->id == -1 && strcmp(id, p->name) == 0 &&
  80. try_module_get(p->owner)) {
  81. clk = p;
  82. break;
  83. }
  84. }
  85. }
  86. mutex_unlock(&clocks_mutex);
  87. return clk;
  88. }
  89. void clk_put(struct clk *clk)
  90. {
  91. module_put(clk->owner);
  92. }
  93. int clk_enable(struct clk *clk)
  94. {
  95. if (IS_ERR(clk) || clk == NULL)
  96. return -EINVAL;
  97. clk_enable(clk->parent);
  98. mutex_lock(&clocks_mutex);
  99. if ((clk->usage++) == 0)
  100. (clk->enable)(clk, 1);
  101. mutex_unlock(&clocks_mutex);
  102. return 0;
  103. }
  104. void clk_disable(struct clk *clk)
  105. {
  106. if (IS_ERR(clk) || clk == NULL)
  107. return;
  108. mutex_lock(&clocks_mutex);
  109. if ((--clk->usage) == 0)
  110. (clk->enable)(clk, 0);
  111. mutex_unlock(&clocks_mutex);
  112. clk_disable(clk->parent);
  113. }
  114. unsigned long clk_get_rate(struct clk *clk)
  115. {
  116. if (IS_ERR(clk))
  117. return 0;
  118. if (clk->rate != 0)
  119. return clk->rate;
  120. if (clk->get_rate != NULL)
  121. return (clk->get_rate)(clk);
  122. if (clk->parent != NULL)
  123. return clk_get_rate(clk->parent);
  124. return clk->rate;
  125. }
  126. long clk_round_rate(struct clk *clk, unsigned long rate)
  127. {
  128. if (!IS_ERR(clk) && clk->round_rate)
  129. return (clk->round_rate)(clk, rate);
  130. return rate;
  131. }
  132. int clk_set_rate(struct clk *clk, unsigned long rate)
  133. {
  134. int ret;
  135. if (IS_ERR(clk))
  136. return -EINVAL;
  137. mutex_lock(&clocks_mutex);
  138. ret = (clk->set_rate)(clk, rate);
  139. mutex_unlock(&clocks_mutex);
  140. return ret;
  141. }
  142. struct clk *clk_get_parent(struct clk *clk)
  143. {
  144. return clk->parent;
  145. }
  146. int clk_set_parent(struct clk *clk, struct clk *parent)
  147. {
  148. int ret = 0;
  149. if (IS_ERR(clk))
  150. return -EINVAL;
  151. mutex_lock(&clocks_mutex);
  152. if (clk->set_parent)
  153. ret = (clk->set_parent)(clk, parent);
  154. mutex_unlock(&clocks_mutex);
  155. return ret;
  156. }
  157. EXPORT_SYMBOL(clk_get);
  158. EXPORT_SYMBOL(clk_put);
  159. EXPORT_SYMBOL(clk_enable);
  160. EXPORT_SYMBOL(clk_disable);
  161. EXPORT_SYMBOL(clk_get_rate);
  162. EXPORT_SYMBOL(clk_round_rate);
  163. EXPORT_SYMBOL(clk_set_rate);
  164. EXPORT_SYMBOL(clk_get_parent);
  165. EXPORT_SYMBOL(clk_set_parent);
  166. /* base clocks */
  167. struct clk clk_xtal = {
  168. .name = "xtal",
  169. .id = -1,
  170. .rate = 0,
  171. .parent = NULL,
  172. .ctrlbit = 0,
  173. };
  174. struct clk clk_mpll = {
  175. .name = "mpll",
  176. .id = -1,
  177. };
  178. struct clk clk_upll = {
  179. .name = "upll",
  180. .id = -1,
  181. .parent = NULL,
  182. .ctrlbit = 0,
  183. };
  184. struct clk clk_f = {
  185. .name = "fclk",
  186. .id = -1,
  187. .rate = 0,
  188. .parent = &clk_mpll,
  189. .ctrlbit = 0,
  190. };
  191. struct clk clk_h = {
  192. .name = "hclk",
  193. .id = -1,
  194. .rate = 0,
  195. .parent = NULL,
  196. .ctrlbit = 0,
  197. };
  198. struct clk clk_p = {
  199. .name = "pclk",
  200. .id = -1,
  201. .rate = 0,
  202. .parent = NULL,
  203. .ctrlbit = 0,
  204. };
  205. struct clk clk_usb_bus = {
  206. .name = "usb-bus",
  207. .id = -1,
  208. .rate = 0,
  209. .parent = &clk_upll,
  210. };
  211. /* clocks that could be registered by external code */
  212. static int s3c24xx_dclk_enable(struct clk *clk, int enable)
  213. {
  214. unsigned long dclkcon = __raw_readl(S3C24XX_DCLKCON);
  215. if (enable)
  216. dclkcon |= clk->ctrlbit;
  217. else
  218. dclkcon &= ~clk->ctrlbit;
  219. __raw_writel(dclkcon, S3C24XX_DCLKCON);
  220. return 0;
  221. }
  222. static int s3c24xx_dclk_setparent(struct clk *clk, struct clk *parent)
  223. {
  224. unsigned long dclkcon;
  225. unsigned int uclk;
  226. if (parent == &clk_upll)
  227. uclk = 1;
  228. else if (parent == &clk_p)
  229. uclk = 0;
  230. else
  231. return -EINVAL;
  232. clk->parent = parent;
  233. dclkcon = __raw_readl(S3C24XX_DCLKCON);
  234. if (clk->ctrlbit == S3C2410_DCLKCON_DCLK0EN) {
  235. if (uclk)
  236. dclkcon |= S3C2410_DCLKCON_DCLK0_UCLK;
  237. else
  238. dclkcon &= ~S3C2410_DCLKCON_DCLK0_UCLK;
  239. } else {
  240. if (uclk)
  241. dclkcon |= S3C2410_DCLKCON_DCLK1_UCLK;
  242. else
  243. dclkcon &= ~S3C2410_DCLKCON_DCLK1_UCLK;
  244. }
  245. __raw_writel(dclkcon, S3C24XX_DCLKCON);
  246. return 0;
  247. }
  248. static int s3c24xx_clkout_setparent(struct clk *clk, struct clk *parent)
  249. {
  250. unsigned long mask;
  251. unsigned long source;
  252. /* calculate the MISCCR setting for the clock */
  253. if (parent == &clk_xtal)
  254. source = S3C2410_MISCCR_CLK0_MPLL;
  255. else if (parent == &clk_upll)
  256. source = S3C2410_MISCCR_CLK0_UPLL;
  257. else if (parent == &clk_f)
  258. source = S3C2410_MISCCR_CLK0_FCLK;
  259. else if (parent == &clk_h)
  260. source = S3C2410_MISCCR_CLK0_HCLK;
  261. else if (parent == &clk_p)
  262. source = S3C2410_MISCCR_CLK0_PCLK;
  263. else if (clk == &s3c24xx_clkout0 && parent == &s3c24xx_dclk0)
  264. source = S3C2410_MISCCR_CLK0_DCLK0;
  265. else if (clk == &s3c24xx_clkout1 && parent == &s3c24xx_dclk1)
  266. source = S3C2410_MISCCR_CLK0_DCLK0;
  267. else
  268. return -EINVAL;
  269. clk->parent = parent;
  270. if (clk == &s3c24xx_dclk0)
  271. mask = S3C2410_MISCCR_CLK0_MASK;
  272. else {
  273. source <<= 4;
  274. mask = S3C2410_MISCCR_CLK1_MASK;
  275. }
  276. s3c2410_modify_misccr(mask, source);
  277. return 0;
  278. }
  279. /* external clock definitions */
  280. struct clk s3c24xx_dclk0 = {
  281. .name = "dclk0",
  282. .id = -1,
  283. .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
  284. .enable = s3c24xx_dclk_enable,
  285. .set_parent = s3c24xx_dclk_setparent,
  286. };
  287. struct clk s3c24xx_dclk1 = {
  288. .name = "dclk1",
  289. .id = -1,
  290. .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
  291. .enable = s3c24xx_dclk_enable,
  292. .set_parent = s3c24xx_dclk_setparent,
  293. };
  294. struct clk s3c24xx_clkout0 = {
  295. .name = "clkout0",
  296. .id = -1,
  297. .set_parent = s3c24xx_clkout_setparent,
  298. };
  299. struct clk s3c24xx_clkout1 = {
  300. .name = "clkout1",
  301. .id = -1,
  302. .set_parent = s3c24xx_clkout_setparent,
  303. };
  304. struct clk s3c24xx_uclk = {
  305. .name = "uclk",
  306. .id = -1,
  307. };
  308. /* initialise the clock system */
  309. int s3c24xx_register_clock(struct clk *clk)
  310. {
  311. clk->owner = THIS_MODULE;
  312. if (clk->enable == NULL)
  313. clk->enable = clk_null_enable;
  314. /* add to the list of available clocks */
  315. mutex_lock(&clocks_mutex);
  316. list_add(&clk->list, &clocks);
  317. mutex_unlock(&clocks_mutex);
  318. return 0;
  319. }
  320. /* initalise all the clocks */
  321. int __init s3c24xx_setup_clocks(unsigned long xtal,
  322. unsigned long fclk,
  323. unsigned long hclk,
  324. unsigned long pclk)
  325. {
  326. printk(KERN_INFO "S3C24XX Clocks, (c) 2004 Simtec Electronics\n");
  327. /* initialise the main system clocks */
  328. clk_xtal.rate = xtal;
  329. clk_upll.rate = s3c2410_get_pll(__raw_readl(S3C2410_UPLLCON), xtal);
  330. clk_mpll.rate = fclk;
  331. clk_h.rate = hclk;
  332. clk_p.rate = pclk;
  333. clk_f.rate = fclk;
  334. /* assume uart clocks are correctly setup */
  335. /* register our clocks */
  336. if (s3c24xx_register_clock(&clk_xtal) < 0)
  337. printk(KERN_ERR "failed to register master xtal\n");
  338. if (s3c24xx_register_clock(&clk_mpll) < 0)
  339. printk(KERN_ERR "failed to register mpll clock\n");
  340. if (s3c24xx_register_clock(&clk_upll) < 0)
  341. printk(KERN_ERR "failed to register upll clock\n");
  342. if (s3c24xx_register_clock(&clk_f) < 0)
  343. printk(KERN_ERR "failed to register cpu fclk\n");
  344. if (s3c24xx_register_clock(&clk_h) < 0)
  345. printk(KERN_ERR "failed to register cpu hclk\n");
  346. if (s3c24xx_register_clock(&clk_p) < 0)
  347. printk(KERN_ERR "failed to register cpu pclk\n");
  348. return 0;
  349. }