clock.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 <linux/clk.h>
  39. #include <linux/mutex.h>
  40. #include <asm/hardware.h>
  41. #include <asm/irq.h>
  42. #include <asm/io.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 DEFINE_MUTEX(clocks_mutex);
  49. /* old functions */
  50. void inline s3c24xx_clk_enable(unsigned int clocks, unsigned int enable)
  51. {
  52. unsigned long clkcon;
  53. clkcon = __raw_readl(S3C2410_CLKCON);
  54. if (enable)
  55. clkcon |= clocks;
  56. else
  57. clkcon &= ~clocks;
  58. /* ensure none of the special function bits set */
  59. clkcon &= ~(S3C2410_CLKCON_IDLE|S3C2410_CLKCON_POWER);
  60. __raw_writel(clkcon, S3C2410_CLKCON);
  61. }
  62. /* enable and disable calls for use with the clk struct */
  63. static int clk_null_enable(struct clk *clk, int enable)
  64. {
  65. return 0;
  66. }
  67. int s3c24xx_clkcon_enable(struct clk *clk, int enable)
  68. {
  69. s3c24xx_clk_enable(clk->ctrlbit, enable);
  70. return 0;
  71. }
  72. /* Clock API calls */
  73. struct clk *clk_get(struct device *dev, const char *id)
  74. {
  75. struct clk *p;
  76. struct clk *clk = ERR_PTR(-ENOENT);
  77. int idno;
  78. if (dev == NULL || dev->bus != &platform_bus_type)
  79. idno = -1;
  80. else
  81. idno = to_platform_device(dev)->id;
  82. mutex_lock(&clocks_mutex);
  83. list_for_each_entry(p, &clocks, list) {
  84. if (p->id == idno &&
  85. strcmp(id, p->name) == 0 &&
  86. try_module_get(p->owner)) {
  87. clk = p;
  88. break;
  89. }
  90. }
  91. /* check for the case where a device was supplied, but the
  92. * clock that was being searched for is not device specific */
  93. if (IS_ERR(clk)) {
  94. list_for_each_entry(p, &clocks, list) {
  95. if (p->id == -1 && strcmp(id, p->name) == 0 &&
  96. try_module_get(p->owner)) {
  97. clk = p;
  98. break;
  99. }
  100. }
  101. }
  102. mutex_unlock(&clocks_mutex);
  103. return clk;
  104. }
  105. void clk_put(struct clk *clk)
  106. {
  107. module_put(clk->owner);
  108. }
  109. int clk_enable(struct clk *clk)
  110. {
  111. if (IS_ERR(clk) || clk == NULL)
  112. return -EINVAL;
  113. clk_enable(clk->parent);
  114. mutex_lock(&clocks_mutex);
  115. if ((clk->usage++) == 0)
  116. (clk->enable)(clk, 1);
  117. mutex_unlock(&clocks_mutex);
  118. return 0;
  119. }
  120. void clk_disable(struct clk *clk)
  121. {
  122. if (IS_ERR(clk) || clk == NULL)
  123. return;
  124. mutex_lock(&clocks_mutex);
  125. if ((--clk->usage) == 0)
  126. (clk->enable)(clk, 0);
  127. mutex_unlock(&clocks_mutex);
  128. clk_disable(clk->parent);
  129. }
  130. unsigned long clk_get_rate(struct clk *clk)
  131. {
  132. if (IS_ERR(clk))
  133. return 0;
  134. if (clk->rate != 0)
  135. return clk->rate;
  136. while (clk->parent != NULL && clk->rate == 0)
  137. clk = clk->parent;
  138. return clk->rate;
  139. }
  140. long clk_round_rate(struct clk *clk, unsigned long rate)
  141. {
  142. return rate;
  143. }
  144. int clk_set_rate(struct clk *clk, unsigned long rate)
  145. {
  146. return -EINVAL;
  147. }
  148. struct clk *clk_get_parent(struct clk *clk)
  149. {
  150. return clk->parent;
  151. }
  152. EXPORT_SYMBOL(clk_get);
  153. EXPORT_SYMBOL(clk_put);
  154. EXPORT_SYMBOL(clk_enable);
  155. EXPORT_SYMBOL(clk_disable);
  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. {
  213. .name = "nand",
  214. .id = -1,
  215. .parent = &clk_h,
  216. .enable = s3c24xx_clkcon_enable,
  217. .ctrlbit = S3C2410_CLKCON_NAND,
  218. }, {
  219. .name = "lcd",
  220. .id = -1,
  221. .parent = &clk_h,
  222. .enable = s3c24xx_clkcon_enable,
  223. .ctrlbit = S3C2410_CLKCON_LCDC,
  224. }, {
  225. .name = "usb-host",
  226. .id = -1,
  227. .parent = &clk_h,
  228. .enable = s3c24xx_clkcon_enable,
  229. .ctrlbit = S3C2410_CLKCON_USBH,
  230. }, {
  231. .name = "usb-device",
  232. .id = -1,
  233. .parent = &clk_h,
  234. .enable = s3c24xx_clkcon_enable,
  235. .ctrlbit = S3C2410_CLKCON_USBD,
  236. }, {
  237. .name = "timers",
  238. .id = -1,
  239. .parent = &clk_p,
  240. .enable = s3c24xx_clkcon_enable,
  241. .ctrlbit = S3C2410_CLKCON_PWMT,
  242. }, {
  243. .name = "sdi",
  244. .id = -1,
  245. .parent = &clk_p,
  246. .enable = s3c24xx_clkcon_enable,
  247. .ctrlbit = S3C2410_CLKCON_SDI,
  248. }, {
  249. .name = "uart",
  250. .id = 0,
  251. .parent = &clk_p,
  252. .enable = s3c24xx_clkcon_enable,
  253. .ctrlbit = S3C2410_CLKCON_UART0,
  254. }, {
  255. .name = "uart",
  256. .id = 1,
  257. .parent = &clk_p,
  258. .enable = s3c24xx_clkcon_enable,
  259. .ctrlbit = S3C2410_CLKCON_UART1,
  260. }, {
  261. .name = "uart",
  262. .id = 2,
  263. .parent = &clk_p,
  264. .enable = s3c24xx_clkcon_enable,
  265. .ctrlbit = S3C2410_CLKCON_UART2,
  266. }, {
  267. .name = "gpio",
  268. .id = -1,
  269. .parent = &clk_p,
  270. .enable = s3c24xx_clkcon_enable,
  271. .ctrlbit = S3C2410_CLKCON_GPIO,
  272. }, {
  273. .name = "rtc",
  274. .id = -1,
  275. .parent = &clk_p,
  276. .enable = s3c24xx_clkcon_enable,
  277. .ctrlbit = S3C2410_CLKCON_RTC,
  278. }, {
  279. .name = "adc",
  280. .id = -1,
  281. .parent = &clk_p,
  282. .enable = s3c24xx_clkcon_enable,
  283. .ctrlbit = S3C2410_CLKCON_ADC,
  284. }, {
  285. .name = "i2c",
  286. .id = -1,
  287. .parent = &clk_p,
  288. .enable = s3c24xx_clkcon_enable,
  289. .ctrlbit = S3C2410_CLKCON_IIC,
  290. }, {
  291. .name = "iis",
  292. .id = -1,
  293. .parent = &clk_p,
  294. .enable = s3c24xx_clkcon_enable,
  295. .ctrlbit = S3C2410_CLKCON_IIS,
  296. }, {
  297. .name = "spi",
  298. .id = -1,
  299. .parent = &clk_p,
  300. .enable = s3c24xx_clkcon_enable,
  301. .ctrlbit = S3C2410_CLKCON_SPI,
  302. }, {
  303. .name = "watchdog",
  304. .id = -1,
  305. .parent = &clk_p,
  306. .ctrlbit = 0,
  307. }
  308. };
  309. /* initialise the clock system */
  310. int s3c24xx_register_clock(struct clk *clk)
  311. {
  312. clk->owner = THIS_MODULE;
  313. if (clk->enable == NULL)
  314. clk->enable = clk_null_enable;
  315. /* if this is a standard clock, set the usage state */
  316. if (clk->ctrlbit) {
  317. unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
  318. clk->usage = (clkcon & clk->ctrlbit) ? 1 : 0;
  319. }
  320. /* add to the list of available clocks */
  321. mutex_lock(&clocks_mutex);
  322. list_add(&clk->list, &clocks);
  323. mutex_unlock(&clocks_mutex);
  324. return 0;
  325. }
  326. /* initalise all the clocks */
  327. int __init s3c24xx_setup_clocks(unsigned long xtal,
  328. unsigned long fclk,
  329. unsigned long hclk,
  330. unsigned long pclk)
  331. {
  332. unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
  333. struct clk *clkp = init_clocks;
  334. int ptr;
  335. int ret;
  336. printk(KERN_INFO "S3C2410 Clocks, (c) 2004 Simtec Electronics\n");
  337. /* initialise the main system clocks */
  338. clk_xtal.rate = xtal;
  339. clk_h.rate = hclk;
  340. clk_p.rate = pclk;
  341. clk_f.rate = fclk;
  342. /* We must be careful disabling the clocks we are not intending to
  343. * be using at boot time, as subsytems such as the LCD which do
  344. * their own DMA requests to the bus can cause the system to lockup
  345. * if they where in the middle of requesting bus access.
  346. *
  347. * Disabling the LCD clock if the LCD is active is very dangerous,
  348. * and therefore the bootloader should be careful to not enable
  349. * the LCD clock if it is not needed.
  350. */
  351. mutex_lock(&clocks_mutex);
  352. s3c24xx_clk_enable(S3C2410_CLKCON_NAND, 0);
  353. s3c24xx_clk_enable(S3C2410_CLKCON_USBH, 0);
  354. s3c24xx_clk_enable(S3C2410_CLKCON_USBD, 0);
  355. s3c24xx_clk_enable(S3C2410_CLKCON_ADC, 0);
  356. s3c24xx_clk_enable(S3C2410_CLKCON_IIC, 0);
  357. s3c24xx_clk_enable(S3C2410_CLKCON_SPI, 0);
  358. mutex_unlock(&clocks_mutex);
  359. /* assume uart clocks are correctly setup */
  360. /* register our clocks */
  361. if (s3c24xx_register_clock(&clk_xtal) < 0)
  362. printk(KERN_ERR "failed to register master xtal\n");
  363. if (s3c24xx_register_clock(&clk_f) < 0)
  364. printk(KERN_ERR "failed to register cpu fclk\n");
  365. if (s3c24xx_register_clock(&clk_h) < 0)
  366. printk(KERN_ERR "failed to register cpu hclk\n");
  367. if (s3c24xx_register_clock(&clk_p) < 0)
  368. printk(KERN_ERR "failed to register cpu pclk\n");
  369. /* register clocks from clock array */
  370. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
  371. ret = s3c24xx_register_clock(clkp);
  372. if (ret < 0) {
  373. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  374. clkp->name, ret);
  375. }
  376. }
  377. /* show the clock-slow value */
  378. printk("CLOCK: Slow mode (%ld.%ld MHz), %s, MPLL %s, UPLL %s\n",
  379. print_mhz(xtal / ( 2 * S3C2410_CLKSLOW_GET_SLOWVAL(clkslow))),
  380. (clkslow & S3C2410_CLKSLOW_SLOW) ? "slow" : "fast",
  381. (clkslow & S3C2410_CLKSLOW_MPLL_OFF) ? "off" : "on",
  382. (clkslow & S3C2410_CLKSLOW_UCLK_OFF) ? "off" : "on");
  383. return 0;
  384. }