clock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 <linux/delay.h>
  41. #include <asm/hardware.h>
  42. #include <asm/irq.h>
  43. #include <asm/io.h>
  44. #include <asm/arch/regs-clock.h>
  45. #include <asm/arch/regs-gpio.h>
  46. #include "clock.h"
  47. #include "cpu.h"
  48. /* clock information */
  49. static LIST_HEAD(clocks);
  50. static DEFINE_MUTEX(clocks_mutex);
  51. /* old functions */
  52. void inline s3c24xx_clk_enable(unsigned int clocks, unsigned int enable)
  53. {
  54. unsigned long clkcon;
  55. clkcon = __raw_readl(S3C2410_CLKCON);
  56. if (enable)
  57. clkcon |= clocks;
  58. else
  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. }
  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. mutex_lock(&clocks_mutex);
  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. mutex_unlock(&clocks_mutex);
  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) || clk == NULL)
  114. return -EINVAL;
  115. clk_enable(clk->parent);
  116. mutex_lock(&clocks_mutex);
  117. if ((clk->usage++) == 0)
  118. (clk->enable)(clk, 1);
  119. mutex_unlock(&clocks_mutex);
  120. return 0;
  121. }
  122. void clk_disable(struct clk *clk)
  123. {
  124. if (IS_ERR(clk) || clk == NULL)
  125. return;
  126. mutex_lock(&clocks_mutex);
  127. if ((--clk->usage) == 0)
  128. (clk->enable)(clk, 0);
  129. mutex_unlock(&clocks_mutex);
  130. clk_disable(clk->parent);
  131. }
  132. unsigned long clk_get_rate(struct clk *clk)
  133. {
  134. if (IS_ERR(clk))
  135. return 0;
  136. if (clk->rate != 0)
  137. return clk->rate;
  138. while (clk->parent != NULL && clk->rate == 0)
  139. clk = clk->parent;
  140. return clk->rate;
  141. }
  142. long clk_round_rate(struct clk *clk, unsigned long rate)
  143. {
  144. return rate;
  145. }
  146. int clk_set_rate(struct clk *clk, unsigned long rate)
  147. {
  148. return -EINVAL;
  149. }
  150. struct clk *clk_get_parent(struct clk *clk)
  151. {
  152. return clk->parent;
  153. }
  154. int clk_set_parent(struct clk *clk, struct clk *parent)
  155. {
  156. int ret = 0;
  157. if (IS_ERR(clk))
  158. return -EINVAL;
  159. mutex_lock(&clocks_mutex);
  160. if (clk->set_parent)
  161. ret = (clk->set_parent)(clk, parent);
  162. mutex_unlock(&clocks_mutex);
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(clk_get);
  166. EXPORT_SYMBOL(clk_put);
  167. EXPORT_SYMBOL(clk_enable);
  168. EXPORT_SYMBOL(clk_disable);
  169. EXPORT_SYMBOL(clk_get_rate);
  170. EXPORT_SYMBOL(clk_round_rate);
  171. EXPORT_SYMBOL(clk_set_rate);
  172. EXPORT_SYMBOL(clk_get_parent);
  173. EXPORT_SYMBOL(clk_set_parent);
  174. /* base clock enable */
  175. static int s3c24xx_upll_enable(struct clk *clk, int enable)
  176. {
  177. unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
  178. unsigned long orig = clkslow;
  179. if (enable)
  180. clkslow &= ~S3C2410_CLKSLOW_UCLK_OFF;
  181. else
  182. clkslow |= S3C2410_CLKSLOW_UCLK_OFF;
  183. __raw_writel(clkslow, S3C2410_CLKSLOW);
  184. /* if we started the UPLL, then allow to settle */
  185. if (enable && !(orig & S3C2410_CLKSLOW_UCLK_OFF))
  186. udelay(200);
  187. return 0;
  188. }
  189. /* base clocks */
  190. static struct clk clk_xtal = {
  191. .name = "xtal",
  192. .id = -1,
  193. .rate = 0,
  194. .parent = NULL,
  195. .ctrlbit = 0,
  196. };
  197. static struct clk clk_upll = {
  198. .name = "upll",
  199. .id = -1,
  200. .parent = NULL,
  201. .enable = s3c24xx_upll_enable,
  202. .ctrlbit = 0,
  203. };
  204. static struct clk clk_f = {
  205. .name = "fclk",
  206. .id = -1,
  207. .rate = 0,
  208. .parent = NULL,
  209. .ctrlbit = 0,
  210. };
  211. static struct clk clk_h = {
  212. .name = "hclk",
  213. .id = -1,
  214. .rate = 0,
  215. .parent = NULL,
  216. .ctrlbit = 0,
  217. };
  218. static struct clk clk_p = {
  219. .name = "pclk",
  220. .id = -1,
  221. .rate = 0,
  222. .parent = NULL,
  223. .ctrlbit = 0,
  224. };
  225. /* clocks that could be registered by external code */
  226. static int s3c24xx_dclk_enable(struct clk *clk, int enable)
  227. {
  228. unsigned long dclkcon = __raw_readl(S3C2410_DCLKCON);
  229. if (enable)
  230. dclkcon |= clk->ctrlbit;
  231. else
  232. dclkcon &= ~clk->ctrlbit;
  233. __raw_writel(dclkcon, S3C2410_DCLKCON);
  234. return 0;
  235. }
  236. static int s3c24xx_dclk_setparent(struct clk *clk, struct clk *parent)
  237. {
  238. unsigned long dclkcon;
  239. unsigned int uclk;
  240. if (parent == &clk_upll)
  241. uclk = 1;
  242. else if (parent == &clk_p)
  243. uclk = 0;
  244. else
  245. return -EINVAL;
  246. clk->parent = parent;
  247. dclkcon = __raw_readl(S3C2410_DCLKCON);
  248. if (clk->ctrlbit == S3C2410_DCLKCON_DCLK0EN) {
  249. if (uclk)
  250. dclkcon |= S3C2410_DCLKCON_DCLK0_UCLK;
  251. else
  252. dclkcon &= ~S3C2410_DCLKCON_DCLK0_UCLK;
  253. } else {
  254. if (uclk)
  255. dclkcon |= S3C2410_DCLKCON_DCLK1_UCLK;
  256. else
  257. dclkcon &= ~S3C2410_DCLKCON_DCLK1_UCLK;
  258. }
  259. __raw_writel(dclkcon, S3C2410_DCLKCON);
  260. return 0;
  261. }
  262. static int s3c24xx_clkout_setparent(struct clk *clk, struct clk *parent)
  263. {
  264. unsigned long mask;
  265. unsigned long source;
  266. /* calculate the MISCCR setting for the clock */
  267. if (parent == &clk_xtal)
  268. source = S3C2410_MISCCR_CLK0_MPLL;
  269. else if (parent == &clk_upll)
  270. source = S3C2410_MISCCR_CLK0_UPLL;
  271. else if (parent == &clk_f)
  272. source = S3C2410_MISCCR_CLK0_FCLK;
  273. else if (parent == &clk_p)
  274. source = S3C2410_MISCCR_CLK0_PCLK;
  275. else if (clk == &s3c24xx_clkout0 && parent == &s3c24xx_dclk0)
  276. source = S3C2410_MISCCR_CLK0_DCLK0;
  277. else if (clk == &s3c24xx_clkout1 && parent == &s3c24xx_dclk1)
  278. source = S3C2410_MISCCR_CLK0_DCLK0;
  279. else
  280. return -EINVAL;
  281. if (clk == &s3c24xx_dclk0)
  282. mask = S3C2410_MISCCR_CLK0_MASK;
  283. else {
  284. source <<= 4;
  285. mask = S3C2410_MISCCR_CLK1_MASK;
  286. }
  287. s3c2410_modify_misccr(mask, source);
  288. return 0;
  289. }
  290. /* external clock definitions */
  291. struct clk s3c24xx_dclk0 = {
  292. .name = "dclk0",
  293. .id = -1,
  294. .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
  295. .enable = s3c24xx_dclk_enable,
  296. .set_parent = s3c24xx_dclk_setparent,
  297. };
  298. struct clk s3c24xx_dclk1 = {
  299. .name = "dclk1",
  300. .id = -1,
  301. .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
  302. .enable = s3c24xx_dclk_enable,
  303. .set_parent = s3c24xx_dclk_setparent,
  304. };
  305. struct clk s3c24xx_clkout0 = {
  306. .name = "clkout0",
  307. .id = -1,
  308. .set_parent = s3c24xx_clkout_setparent,
  309. };
  310. struct clk s3c24xx_clkout1 = {
  311. .name = "clkout1",
  312. .id = -1,
  313. .set_parent = s3c24xx_clkout_setparent,
  314. };
  315. struct clk s3c24xx_uclk = {
  316. .name = "uclk",
  317. .id = -1,
  318. };
  319. /* standard clock definitions */
  320. static struct clk init_clocks[] = {
  321. {
  322. .name = "nand",
  323. .id = -1,
  324. .parent = &clk_h,
  325. .enable = s3c24xx_clkcon_enable,
  326. .ctrlbit = S3C2410_CLKCON_NAND,
  327. }, {
  328. .name = "lcd",
  329. .id = -1,
  330. .parent = &clk_h,
  331. .enable = s3c24xx_clkcon_enable,
  332. .ctrlbit = S3C2410_CLKCON_LCDC,
  333. }, {
  334. .name = "usb-host",
  335. .id = -1,
  336. .parent = &clk_h,
  337. .enable = s3c24xx_clkcon_enable,
  338. .ctrlbit = S3C2410_CLKCON_USBH,
  339. }, {
  340. .name = "usb-device",
  341. .id = -1,
  342. .parent = &clk_h,
  343. .enable = s3c24xx_clkcon_enable,
  344. .ctrlbit = S3C2410_CLKCON_USBD,
  345. }, {
  346. .name = "timers",
  347. .id = -1,
  348. .parent = &clk_p,
  349. .enable = s3c24xx_clkcon_enable,
  350. .ctrlbit = S3C2410_CLKCON_PWMT,
  351. }, {
  352. .name = "sdi",
  353. .id = -1,
  354. .parent = &clk_p,
  355. .enable = s3c24xx_clkcon_enable,
  356. .ctrlbit = S3C2410_CLKCON_SDI,
  357. }, {
  358. .name = "uart",
  359. .id = 0,
  360. .parent = &clk_p,
  361. .enable = s3c24xx_clkcon_enable,
  362. .ctrlbit = S3C2410_CLKCON_UART0,
  363. }, {
  364. .name = "uart",
  365. .id = 1,
  366. .parent = &clk_p,
  367. .enable = s3c24xx_clkcon_enable,
  368. .ctrlbit = S3C2410_CLKCON_UART1,
  369. }, {
  370. .name = "uart",
  371. .id = 2,
  372. .parent = &clk_p,
  373. .enable = s3c24xx_clkcon_enable,
  374. .ctrlbit = S3C2410_CLKCON_UART2,
  375. }, {
  376. .name = "gpio",
  377. .id = -1,
  378. .parent = &clk_p,
  379. .enable = s3c24xx_clkcon_enable,
  380. .ctrlbit = S3C2410_CLKCON_GPIO,
  381. }, {
  382. .name = "rtc",
  383. .id = -1,
  384. .parent = &clk_p,
  385. .enable = s3c24xx_clkcon_enable,
  386. .ctrlbit = S3C2410_CLKCON_RTC,
  387. }, {
  388. .name = "adc",
  389. .id = -1,
  390. .parent = &clk_p,
  391. .enable = s3c24xx_clkcon_enable,
  392. .ctrlbit = S3C2410_CLKCON_ADC,
  393. }, {
  394. .name = "i2c",
  395. .id = -1,
  396. .parent = &clk_p,
  397. .enable = s3c24xx_clkcon_enable,
  398. .ctrlbit = S3C2410_CLKCON_IIC,
  399. }, {
  400. .name = "iis",
  401. .id = -1,
  402. .parent = &clk_p,
  403. .enable = s3c24xx_clkcon_enable,
  404. .ctrlbit = S3C2410_CLKCON_IIS,
  405. }, {
  406. .name = "spi",
  407. .id = -1,
  408. .parent = &clk_p,
  409. .enable = s3c24xx_clkcon_enable,
  410. .ctrlbit = S3C2410_CLKCON_SPI,
  411. }, {
  412. .name = "watchdog",
  413. .id = -1,
  414. .parent = &clk_p,
  415. .ctrlbit = 0,
  416. }
  417. };
  418. /* initialise the clock system */
  419. int s3c24xx_register_clock(struct clk *clk)
  420. {
  421. clk->owner = THIS_MODULE;
  422. if (clk->enable == NULL)
  423. clk->enable = clk_null_enable;
  424. /* if this is a standard clock, set the usage state */
  425. if (clk->ctrlbit && clk->enable == s3c24xx_clkcon_enable) {
  426. unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
  427. clk->usage = (clkcon & clk->ctrlbit) ? 1 : 0;
  428. }
  429. /* add to the list of available clocks */
  430. mutex_lock(&clocks_mutex);
  431. list_add(&clk->list, &clocks);
  432. mutex_unlock(&clocks_mutex);
  433. return 0;
  434. }
  435. /* initalise all the clocks */
  436. int __init s3c24xx_setup_clocks(unsigned long xtal,
  437. unsigned long fclk,
  438. unsigned long hclk,
  439. unsigned long pclk)
  440. {
  441. unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
  442. unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
  443. struct clk *clkp = init_clocks;
  444. int ptr;
  445. int ret;
  446. printk(KERN_INFO "S3C2410 Clocks, (c) 2004 Simtec Electronics\n");
  447. /* initialise the main system clocks */
  448. clk_xtal.rate = xtal;
  449. clk_upll.rate = s3c2410_get_pll(upllcon, xtal);
  450. clk_h.rate = hclk;
  451. clk_p.rate = pclk;
  452. clk_f.rate = fclk;
  453. /* We must be careful disabling the clocks we are not intending to
  454. * be using at boot time, as subsytems such as the LCD which do
  455. * their own DMA requests to the bus can cause the system to lockup
  456. * if they where in the middle of requesting bus access.
  457. *
  458. * Disabling the LCD clock if the LCD is active is very dangerous,
  459. * and therefore the bootloader should be careful to not enable
  460. * the LCD clock if it is not needed.
  461. */
  462. mutex_lock(&clocks_mutex);
  463. s3c24xx_clk_enable(S3C2410_CLKCON_NAND, 0);
  464. s3c24xx_clk_enable(S3C2410_CLKCON_USBH, 0);
  465. s3c24xx_clk_enable(S3C2410_CLKCON_USBD, 0);
  466. s3c24xx_clk_enable(S3C2410_CLKCON_ADC, 0);
  467. s3c24xx_clk_enable(S3C2410_CLKCON_IIC, 0);
  468. s3c24xx_clk_enable(S3C2410_CLKCON_SPI, 0);
  469. mutex_unlock(&clocks_mutex);
  470. /* assume uart clocks are correctly setup */
  471. /* register our clocks */
  472. if (s3c24xx_register_clock(&clk_xtal) < 0)
  473. printk(KERN_ERR "failed to register master xtal\n");
  474. if (s3c24xx_register_clock(&clk_upll) < 0)
  475. printk(KERN_ERR "failed to register upll clock\n");
  476. if (s3c24xx_register_clock(&clk_f) < 0)
  477. printk(KERN_ERR "failed to register cpu fclk\n");
  478. if (s3c24xx_register_clock(&clk_h) < 0)
  479. printk(KERN_ERR "failed to register cpu hclk\n");
  480. if (s3c24xx_register_clock(&clk_p) < 0)
  481. printk(KERN_ERR "failed to register cpu pclk\n");
  482. /* register clocks from clock array */
  483. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
  484. ret = s3c24xx_register_clock(clkp);
  485. if (ret < 0) {
  486. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  487. clkp->name, ret);
  488. }
  489. }
  490. /* show the clock-slow value */
  491. printk("CLOCK: Slow mode (%ld.%ld MHz), %s, MPLL %s, UPLL %s\n",
  492. print_mhz(xtal / ( 2 * S3C2410_CLKSLOW_GET_SLOWVAL(clkslow))),
  493. (clkslow & S3C2410_CLKSLOW_SLOW) ? "slow" : "fast",
  494. (clkslow & S3C2410_CLKSLOW_MPLL_OFF) ? "off" : "on",
  495. (clkslow & S3C2410_CLKSLOW_UCLK_OFF) ? "off" : "on");
  496. return 0;
  497. }