clock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. * S3C2410 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/device.h>
  35. #include <linux/sysdev.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/ioport.h>
  38. #include <asm/hardware.h>
  39. #include <asm/atomic.h>
  40. #include <asm/irq.h>
  41. #include <asm/io.h>
  42. #include <asm/hardware/clock.h>
  43. #include <asm/arch/regs-clock.h>
  44. #include "clock.h"
  45. #include "cpu.h"
  46. /* clock information */
  47. static LIST_HEAD(clocks);
  48. static DECLARE_MUTEX(clocks_sem);
  49. /* old functions */
  50. void inline s3c24xx_clk_enable(unsigned int clocks, unsigned int enable)
  51. {
  52. unsigned long clkcon;
  53. unsigned long flags;
  54. local_irq_save(flags);
  55. clkcon = __raw_readl(S3C2410_CLKCON);
  56. clkcon &= ~clocks;
  57. if (enable)
  58. clkcon |= clocks;
  59. /* ensure none of the special function bits set */
  60. clkcon &= ~(S3C2410_CLKCON_IDLE|S3C2410_CLKCON_POWER);
  61. __raw_writel(clkcon, S3C2410_CLKCON);
  62. local_irq_restore(flags);
  63. }
  64. /* enable and disable calls for use with the clk struct */
  65. static int clk_null_enable(struct clk *clk, int enable)
  66. {
  67. return 0;
  68. }
  69. int s3c24xx_clkcon_enable(struct clk *clk, int enable)
  70. {
  71. s3c24xx_clk_enable(clk->ctrlbit, enable);
  72. return 0;
  73. }
  74. /* Clock API calls */
  75. struct clk *clk_get(struct device *dev, const char *id)
  76. {
  77. struct clk *p;
  78. struct clk *clk = ERR_PTR(-ENOENT);
  79. int idno;
  80. idno = (dev == NULL) ? -1 : to_platform_device(dev)->id;
  81. down(&clocks_sem);
  82. list_for_each_entry(p, &clocks, list) {
  83. if (p->id == idno &&
  84. strcmp(id, p->name) == 0 &&
  85. try_module_get(p->owner)) {
  86. clk = p;
  87. break;
  88. }
  89. }
  90. /* check for the case where a device was supplied, but the
  91. * clock that was being searched for is not device specific */
  92. if (IS_ERR(clk)) {
  93. list_for_each_entry(p, &clocks, list) {
  94. if (p->id == -1 && strcmp(id, p->name) == 0 &&
  95. try_module_get(p->owner)) {
  96. clk = p;
  97. break;
  98. }
  99. }
  100. }
  101. up(&clocks_sem);
  102. return clk;
  103. }
  104. void clk_put(struct clk *clk)
  105. {
  106. module_put(clk->owner);
  107. }
  108. int clk_enable(struct clk *clk)
  109. {
  110. if (IS_ERR(clk))
  111. return -EINVAL;
  112. return (clk->enable)(clk, 1);
  113. }
  114. void clk_disable(struct clk *clk)
  115. {
  116. if (!IS_ERR(clk))
  117. (clk->enable)(clk, 0);
  118. }
  119. int clk_use(struct clk *clk)
  120. {
  121. atomic_inc(&clk->used);
  122. return 0;
  123. }
  124. void clk_unuse(struct clk *clk)
  125. {
  126. atomic_dec(&clk->used);
  127. }
  128. unsigned long clk_get_rate(struct clk *clk)
  129. {
  130. if (IS_ERR(clk))
  131. return 0;
  132. if (clk->rate != 0)
  133. return clk->rate;
  134. while (clk->parent != NULL && clk->rate == 0)
  135. clk = clk->parent;
  136. return clk->rate;
  137. }
  138. long clk_round_rate(struct clk *clk, unsigned long rate)
  139. {
  140. return rate;
  141. }
  142. int clk_set_rate(struct clk *clk, unsigned long rate)
  143. {
  144. return -EINVAL;
  145. }
  146. struct clk *clk_get_parent(struct clk *clk)
  147. {
  148. return clk->parent;
  149. }
  150. EXPORT_SYMBOL(clk_get);
  151. EXPORT_SYMBOL(clk_put);
  152. EXPORT_SYMBOL(clk_enable);
  153. EXPORT_SYMBOL(clk_disable);
  154. EXPORT_SYMBOL(clk_use);
  155. EXPORT_SYMBOL(clk_unuse);
  156. EXPORT_SYMBOL(clk_get_rate);
  157. EXPORT_SYMBOL(clk_round_rate);
  158. EXPORT_SYMBOL(clk_set_rate);
  159. EXPORT_SYMBOL(clk_get_parent);
  160. /* base clocks */
  161. static struct clk clk_xtal = {
  162. .name = "xtal",
  163. .id = -1,
  164. .rate = 0,
  165. .parent = NULL,
  166. .ctrlbit = 0,
  167. };
  168. static struct clk clk_f = {
  169. .name = "fclk",
  170. .id = -1,
  171. .rate = 0,
  172. .parent = NULL,
  173. .ctrlbit = 0,
  174. };
  175. static struct clk clk_h = {
  176. .name = "hclk",
  177. .id = -1,
  178. .rate = 0,
  179. .parent = NULL,
  180. .ctrlbit = 0,
  181. };
  182. static struct clk clk_p = {
  183. .name = "pclk",
  184. .id = -1,
  185. .rate = 0,
  186. .parent = NULL,
  187. .ctrlbit = 0,
  188. };
  189. /* clocks that could be registered by external code */
  190. struct clk s3c24xx_dclk0 = {
  191. .name = "dclk0",
  192. .id = -1,
  193. };
  194. struct clk s3c24xx_dclk1 = {
  195. .name = "dclk1",
  196. .id = -1,
  197. };
  198. struct clk s3c24xx_clkout0 = {
  199. .name = "clkout0",
  200. .id = -1,
  201. };
  202. struct clk s3c24xx_clkout1 = {
  203. .name = "clkout1",
  204. .id = -1,
  205. };
  206. struct clk s3c24xx_uclk = {
  207. .name = "uclk",
  208. .id = -1,
  209. };
  210. /* clock definitions */
  211. static struct clk init_clocks[] = {
  212. { .name = "nand",
  213. .id = -1,
  214. .parent = &clk_h,
  215. .enable = s3c24xx_clkcon_enable,
  216. .ctrlbit = S3C2410_CLKCON_NAND
  217. },
  218. { .name = "lcd",
  219. .id = -1,
  220. .parent = &clk_h,
  221. .enable = s3c24xx_clkcon_enable,
  222. .ctrlbit = S3C2410_CLKCON_LCDC
  223. },
  224. { .name = "usb-host",
  225. .id = -1,
  226. .parent = &clk_h,
  227. .enable = s3c24xx_clkcon_enable,
  228. .ctrlbit = S3C2410_CLKCON_USBH
  229. },
  230. { .name = "usb-device",
  231. .id = -1,
  232. .parent = &clk_h,
  233. .enable = s3c24xx_clkcon_enable,
  234. .ctrlbit = S3C2410_CLKCON_USBD
  235. },
  236. { .name = "timers",
  237. .id = -1,
  238. .parent = &clk_p,
  239. .enable = s3c24xx_clkcon_enable,
  240. .ctrlbit = S3C2410_CLKCON_PWMT
  241. },
  242. { .name = "sdi",
  243. .id = -1,
  244. .parent = &clk_p,
  245. .enable = s3c24xx_clkcon_enable,
  246. .ctrlbit = S3C2410_CLKCON_SDI
  247. },
  248. { .name = "uart",
  249. .id = 0,
  250. .parent = &clk_p,
  251. .enable = s3c24xx_clkcon_enable,
  252. .ctrlbit = S3C2410_CLKCON_UART0
  253. },
  254. { .name = "uart",
  255. .id = 1,
  256. .parent = &clk_p,
  257. .enable = s3c24xx_clkcon_enable,
  258. .ctrlbit = S3C2410_CLKCON_UART1
  259. },
  260. { .name = "uart",
  261. .id = 2,
  262. .parent = &clk_p,
  263. .enable = s3c24xx_clkcon_enable,
  264. .ctrlbit = S3C2410_CLKCON_UART2
  265. },
  266. { .name = "gpio",
  267. .id = -1,
  268. .parent = &clk_p,
  269. .enable = s3c24xx_clkcon_enable,
  270. .ctrlbit = S3C2410_CLKCON_GPIO
  271. },
  272. { .name = "rtc",
  273. .id = -1,
  274. .parent = &clk_p,
  275. .enable = s3c24xx_clkcon_enable,
  276. .ctrlbit = S3C2410_CLKCON_RTC
  277. },
  278. { .name = "adc",
  279. .id = -1,
  280. .parent = &clk_p,
  281. .enable = s3c24xx_clkcon_enable,
  282. .ctrlbit = S3C2410_CLKCON_ADC
  283. },
  284. { .name = "i2c",
  285. .id = -1,
  286. .parent = &clk_p,
  287. .enable = s3c24xx_clkcon_enable,
  288. .ctrlbit = S3C2410_CLKCON_IIC
  289. },
  290. { .name = "iis",
  291. .id = -1,
  292. .parent = &clk_p,
  293. .enable = s3c24xx_clkcon_enable,
  294. .ctrlbit = S3C2410_CLKCON_IIS
  295. },
  296. { .name = "spi",
  297. .id = -1,
  298. .parent = &clk_p,
  299. .enable = s3c24xx_clkcon_enable,
  300. .ctrlbit = S3C2410_CLKCON_SPI
  301. },
  302. { .name = "watchdog",
  303. .id = -1,
  304. .parent = &clk_p,
  305. .ctrlbit = 0
  306. }
  307. };
  308. /* initialise the clock system */
  309. int s3c24xx_register_clock(struct clk *clk)
  310. {
  311. clk->owner = THIS_MODULE;
  312. atomic_set(&clk->used, 0);
  313. if (clk->enable == NULL)
  314. clk->enable = clk_null_enable;
  315. /* add to the list of available clocks */
  316. down(&clocks_sem);
  317. list_add(&clk->list, &clocks);
  318. up(&clocks_sem);
  319. return 0;
  320. }
  321. /* initalise all the clocks */
  322. int __init s3c24xx_setup_clocks(unsigned long xtal,
  323. unsigned long fclk,
  324. unsigned long hclk,
  325. unsigned long pclk)
  326. {
  327. struct clk *clkp = init_clocks;
  328. int ptr;
  329. int ret;
  330. printk(KERN_INFO "S3C2410 Clocks, (c) 2004 Simtec Electronics\n");
  331. /* initialise the main system clocks */
  332. clk_xtal.rate = xtal;
  333. clk_h.rate = hclk;
  334. clk_p.rate = pclk;
  335. clk_f.rate = fclk;
  336. /* it looks like just setting the register here is not good
  337. * enough, and causes the odd hang at initial boot time, so
  338. * do all of them indivdually.
  339. *
  340. * I think disabling the LCD clock if the LCD is active is
  341. * very dangerous, and therefore the bootloader should be
  342. * careful to not enable the LCD clock if it is not needed.
  343. *
  344. * and of course, this looks neater
  345. */
  346. s3c24xx_clk_enable(S3C2410_CLKCON_NAND, 0);
  347. s3c24xx_clk_enable(S3C2410_CLKCON_USBH, 0);
  348. s3c24xx_clk_enable(S3C2410_CLKCON_USBD, 0);
  349. s3c24xx_clk_enable(S3C2410_CLKCON_ADC, 0);
  350. s3c24xx_clk_enable(S3C2410_CLKCON_IIC, 0);
  351. s3c24xx_clk_enable(S3C2410_CLKCON_SPI, 0);
  352. /* assume uart clocks are correctly setup */
  353. /* register our clocks */
  354. if (s3c24xx_register_clock(&clk_xtal) < 0)
  355. printk(KERN_ERR "failed to register master xtal\n");
  356. if (s3c24xx_register_clock(&clk_f) < 0)
  357. printk(KERN_ERR "failed to register cpu fclk\n");
  358. if (s3c24xx_register_clock(&clk_h) < 0)
  359. printk(KERN_ERR "failed to register cpu hclk\n");
  360. if (s3c24xx_register_clock(&clk_p) < 0)
  361. printk(KERN_ERR "failed to register cpu pclk\n");
  362. /* register clocks from clock array */
  363. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
  364. ret = s3c24xx_register_clock(clkp);
  365. if (ret < 0) {
  366. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  367. clkp->name, ret);
  368. }
  369. }
  370. return 0;
  371. }
  372. /* S3C2440 extended clock support */
  373. #ifdef CONFIG_CPU_S3C2440
  374. static struct clk s3c2440_clk_upll = {
  375. .name = "upll",
  376. .id = -1,
  377. };
  378. static struct clk s3c2440_clk_cam = {
  379. .name = "camif",
  380. .parent = &clk_h,
  381. .id = -1,
  382. .enable = s3c24xx_clkcon_enable,
  383. .ctrlbit = S3C2440_CLKCON_CAMERA,
  384. };
  385. static struct clk s3c2440_clk_ac97 = {
  386. .name = "ac97",
  387. .parent = &clk_p,
  388. .id = -1,
  389. .enable = s3c24xx_clkcon_enable,
  390. .ctrlbit = S3C2440_CLKCON_CAMERA,
  391. };
  392. static int s3c2440_clk_add(struct sys_device *sysdev)
  393. {
  394. unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
  395. s3c2440_clk_upll.rate = s3c2410_get_pll(upllcon, clk_xtal.rate);
  396. printk("S3C2440: Clock Support, UPLL %ld.%03ld MHz\n",
  397. print_mhz(s3c2440_clk_upll.rate));
  398. s3c24xx_register_clock(&s3c2440_clk_ac97);
  399. s3c24xx_register_clock(&s3c2440_clk_cam);
  400. s3c24xx_register_clock(&s3c2440_clk_upll);
  401. clk_disable(&s3c2440_clk_ac97);
  402. clk_disable(&s3c2440_clk_cam);
  403. return 0;
  404. }
  405. static struct sysdev_driver s3c2440_clk_driver = {
  406. .add = s3c2440_clk_add,
  407. };
  408. static int s3c24xx_clk_driver(void)
  409. {
  410. return sysdev_driver_register(&s3c2440_sysclass, &s3c2440_clk_driver);
  411. }
  412. arch_initcall(s3c24xx_clk_driver);
  413. #endif /* CONFIG_CPU_S3C2440 */