clock.c 13 KB

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