clock.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. if (dev == NULL || dev->bus != &platform_bus_type)
  81. idno = -1;
  82. else
  83. idno = to_platform_device(dev)->id;
  84. down(&clocks_sem);
  85. list_for_each_entry(p, &clocks, list) {
  86. if (p->id == idno &&
  87. strcmp(id, p->name) == 0 &&
  88. try_module_get(p->owner)) {
  89. clk = p;
  90. break;
  91. }
  92. }
  93. /* check for the case where a device was supplied, but the
  94. * clock that was being searched for is not device specific */
  95. if (IS_ERR(clk)) {
  96. list_for_each_entry(p, &clocks, list) {
  97. if (p->id == -1 && strcmp(id, p->name) == 0 &&
  98. try_module_get(p->owner)) {
  99. clk = p;
  100. break;
  101. }
  102. }
  103. }
  104. up(&clocks_sem);
  105. return clk;
  106. }
  107. void clk_put(struct clk *clk)
  108. {
  109. module_put(clk->owner);
  110. }
  111. int clk_enable(struct clk *clk)
  112. {
  113. if (IS_ERR(clk))
  114. return -EINVAL;
  115. return (clk->enable)(clk, 1);
  116. }
  117. void clk_disable(struct clk *clk)
  118. {
  119. if (!IS_ERR(clk))
  120. (clk->enable)(clk, 0);
  121. }
  122. int clk_use(struct clk *clk)
  123. {
  124. atomic_inc(&clk->used);
  125. return 0;
  126. }
  127. void clk_unuse(struct clk *clk)
  128. {
  129. atomic_dec(&clk->used);
  130. }
  131. unsigned long clk_get_rate(struct clk *clk)
  132. {
  133. if (IS_ERR(clk))
  134. return 0;
  135. if (clk->rate != 0)
  136. return clk->rate;
  137. while (clk->parent != NULL && clk->rate == 0)
  138. clk = clk->parent;
  139. return clk->rate;
  140. }
  141. long clk_round_rate(struct clk *clk, unsigned long rate)
  142. {
  143. return rate;
  144. }
  145. int clk_set_rate(struct clk *clk, unsigned long rate)
  146. {
  147. return -EINVAL;
  148. }
  149. struct clk *clk_get_parent(struct clk *clk)
  150. {
  151. return clk->parent;
  152. }
  153. EXPORT_SYMBOL(clk_get);
  154. EXPORT_SYMBOL(clk_put);
  155. EXPORT_SYMBOL(clk_enable);
  156. EXPORT_SYMBOL(clk_disable);
  157. EXPORT_SYMBOL(clk_use);
  158. EXPORT_SYMBOL(clk_unuse);
  159. EXPORT_SYMBOL(clk_get_rate);
  160. EXPORT_SYMBOL(clk_round_rate);
  161. EXPORT_SYMBOL(clk_set_rate);
  162. EXPORT_SYMBOL(clk_get_parent);
  163. /* base clocks */
  164. static struct clk clk_xtal = {
  165. .name = "xtal",
  166. .id = -1,
  167. .rate = 0,
  168. .parent = NULL,
  169. .ctrlbit = 0,
  170. };
  171. static struct clk clk_f = {
  172. .name = "fclk",
  173. .id = -1,
  174. .rate = 0,
  175. .parent = NULL,
  176. .ctrlbit = 0,
  177. };
  178. static struct clk clk_h = {
  179. .name = "hclk",
  180. .id = -1,
  181. .rate = 0,
  182. .parent = NULL,
  183. .ctrlbit = 0,
  184. };
  185. static struct clk clk_p = {
  186. .name = "pclk",
  187. .id = -1,
  188. .rate = 0,
  189. .parent = NULL,
  190. .ctrlbit = 0,
  191. };
  192. /* clocks that could be registered by external code */
  193. struct clk s3c24xx_dclk0 = {
  194. .name = "dclk0",
  195. .id = -1,
  196. };
  197. struct clk s3c24xx_dclk1 = {
  198. .name = "dclk1",
  199. .id = -1,
  200. };
  201. struct clk s3c24xx_clkout0 = {
  202. .name = "clkout0",
  203. .id = -1,
  204. };
  205. struct clk s3c24xx_clkout1 = {
  206. .name = "clkout1",
  207. .id = -1,
  208. };
  209. struct clk s3c24xx_uclk = {
  210. .name = "uclk",
  211. .id = -1,
  212. };
  213. /* clock definitions */
  214. static struct clk init_clocks[] = {
  215. { .name = "nand",
  216. .id = -1,
  217. .parent = &clk_h,
  218. .enable = s3c24xx_clkcon_enable,
  219. .ctrlbit = S3C2410_CLKCON_NAND
  220. },
  221. { .name = "lcd",
  222. .id = -1,
  223. .parent = &clk_h,
  224. .enable = s3c24xx_clkcon_enable,
  225. .ctrlbit = S3C2410_CLKCON_LCDC
  226. },
  227. { .name = "usb-host",
  228. .id = -1,
  229. .parent = &clk_h,
  230. .enable = s3c24xx_clkcon_enable,
  231. .ctrlbit = S3C2410_CLKCON_USBH
  232. },
  233. { .name = "usb-device",
  234. .id = -1,
  235. .parent = &clk_h,
  236. .enable = s3c24xx_clkcon_enable,
  237. .ctrlbit = S3C2410_CLKCON_USBD
  238. },
  239. { .name = "timers",
  240. .id = -1,
  241. .parent = &clk_p,
  242. .enable = s3c24xx_clkcon_enable,
  243. .ctrlbit = S3C2410_CLKCON_PWMT
  244. },
  245. { .name = "sdi",
  246. .id = -1,
  247. .parent = &clk_p,
  248. .enable = s3c24xx_clkcon_enable,
  249. .ctrlbit = S3C2410_CLKCON_SDI
  250. },
  251. { .name = "uart",
  252. .id = 0,
  253. .parent = &clk_p,
  254. .enable = s3c24xx_clkcon_enable,
  255. .ctrlbit = S3C2410_CLKCON_UART0
  256. },
  257. { .name = "uart",
  258. .id = 1,
  259. .parent = &clk_p,
  260. .enable = s3c24xx_clkcon_enable,
  261. .ctrlbit = S3C2410_CLKCON_UART1
  262. },
  263. { .name = "uart",
  264. .id = 2,
  265. .parent = &clk_p,
  266. .enable = s3c24xx_clkcon_enable,
  267. .ctrlbit = S3C2410_CLKCON_UART2
  268. },
  269. { .name = "gpio",
  270. .id = -1,
  271. .parent = &clk_p,
  272. .enable = s3c24xx_clkcon_enable,
  273. .ctrlbit = S3C2410_CLKCON_GPIO
  274. },
  275. { .name = "rtc",
  276. .id = -1,
  277. .parent = &clk_p,
  278. .enable = s3c24xx_clkcon_enable,
  279. .ctrlbit = S3C2410_CLKCON_RTC
  280. },
  281. { .name = "adc",
  282. .id = -1,
  283. .parent = &clk_p,
  284. .enable = s3c24xx_clkcon_enable,
  285. .ctrlbit = S3C2410_CLKCON_ADC
  286. },
  287. { .name = "i2c",
  288. .id = -1,
  289. .parent = &clk_p,
  290. .enable = s3c24xx_clkcon_enable,
  291. .ctrlbit = S3C2410_CLKCON_IIC
  292. },
  293. { .name = "iis",
  294. .id = -1,
  295. .parent = &clk_p,
  296. .enable = s3c24xx_clkcon_enable,
  297. .ctrlbit = S3C2410_CLKCON_IIS
  298. },
  299. { .name = "spi",
  300. .id = -1,
  301. .parent = &clk_p,
  302. .enable = s3c24xx_clkcon_enable,
  303. .ctrlbit = S3C2410_CLKCON_SPI
  304. },
  305. { .name = "watchdog",
  306. .id = -1,
  307. .parent = &clk_p,
  308. .ctrlbit = 0
  309. }
  310. };
  311. /* initialise the clock system */
  312. int s3c24xx_register_clock(struct clk *clk)
  313. {
  314. clk->owner = THIS_MODULE;
  315. atomic_set(&clk->used, 0);
  316. if (clk->enable == NULL)
  317. clk->enable = clk_null_enable;
  318. /* add to the list of available clocks */
  319. down(&clocks_sem);
  320. list_add(&clk->list, &clocks);
  321. up(&clocks_sem);
  322. return 0;
  323. }
  324. /* initalise all the clocks */
  325. int __init s3c24xx_setup_clocks(unsigned long xtal,
  326. unsigned long fclk,
  327. unsigned long hclk,
  328. unsigned long pclk)
  329. {
  330. unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
  331. struct clk *clkp = init_clocks;
  332. int ptr;
  333. int ret;
  334. printk(KERN_INFO "S3C2410 Clocks, (c) 2004 Simtec Electronics\n");
  335. /* initialise the main system clocks */
  336. clk_xtal.rate = xtal;
  337. clk_h.rate = hclk;
  338. clk_p.rate = pclk;
  339. clk_f.rate = fclk;
  340. /* it looks like just setting the register here is not good
  341. * enough, and causes the odd hang at initial boot time, so
  342. * do all of them indivdually.
  343. *
  344. * I think disabling the LCD clock if the LCD is active is
  345. * very dangerous, and therefore the bootloader should be
  346. * careful to not enable the LCD clock if it is not needed.
  347. *
  348. * and of course, this looks neater
  349. */
  350. s3c24xx_clk_enable(S3C2410_CLKCON_NAND, 0);
  351. s3c24xx_clk_enable(S3C2410_CLKCON_USBH, 0);
  352. s3c24xx_clk_enable(S3C2410_CLKCON_USBD, 0);
  353. s3c24xx_clk_enable(S3C2410_CLKCON_ADC, 0);
  354. s3c24xx_clk_enable(S3C2410_CLKCON_IIC, 0);
  355. s3c24xx_clk_enable(S3C2410_CLKCON_SPI, 0);
  356. /* assume uart clocks are correctly setup */
  357. /* register our clocks */
  358. if (s3c24xx_register_clock(&clk_xtal) < 0)
  359. printk(KERN_ERR "failed to register master xtal\n");
  360. if (s3c24xx_register_clock(&clk_f) < 0)
  361. printk(KERN_ERR "failed to register cpu fclk\n");
  362. if (s3c24xx_register_clock(&clk_h) < 0)
  363. printk(KERN_ERR "failed to register cpu hclk\n");
  364. if (s3c24xx_register_clock(&clk_p) < 0)
  365. printk(KERN_ERR "failed to register cpu pclk\n");
  366. /* register clocks from clock array */
  367. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
  368. ret = s3c24xx_register_clock(clkp);
  369. if (ret < 0) {
  370. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  371. clkp->name, ret);
  372. }
  373. }
  374. /* show the clock-slow value */
  375. printk("CLOCK: Slow mode (%ld.%ld MHz), %s, MPLL %s, UPLL %s\n",
  376. print_mhz(xtal / ( 2 * S3C2410_CLKSLOW_GET_SLOWVAL(clkslow))),
  377. (clkslow & S3C2410_CLKSLOW_SLOW) ? "slow" : "fast",
  378. (clkslow & S3C2410_CLKSLOW_MPLL_OFF) ? "off" : "on",
  379. (clkslow & S3C2410_CLKSLOW_UCLK_OFF) ? "off" : "on");
  380. return 0;
  381. }