clock.c 9.2 KB

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