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