clock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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_h)
  288. source = S3C2410_MISCCR_CLK0_HCLK;
  289. else if (parent == &clk_p)
  290. source = S3C2410_MISCCR_CLK0_PCLK;
  291. else if (clk == &s3c24xx_clkout0 && parent == &s3c24xx_dclk0)
  292. source = S3C2410_MISCCR_CLK0_DCLK0;
  293. else if (clk == &s3c24xx_clkout1 && parent == &s3c24xx_dclk1)
  294. source = S3C2410_MISCCR_CLK0_DCLK0;
  295. else
  296. return -EINVAL;
  297. clk->parent = parent;
  298. if (clk == &s3c24xx_dclk0)
  299. mask = S3C2410_MISCCR_CLK0_MASK;
  300. else {
  301. source <<= 4;
  302. mask = S3C2410_MISCCR_CLK1_MASK;
  303. }
  304. s3c2410_modify_misccr(mask, source);
  305. return 0;
  306. }
  307. /* external clock definitions */
  308. struct clk s3c24xx_dclk0 = {
  309. .name = "dclk0",
  310. .id = -1,
  311. .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
  312. .enable = s3c24xx_dclk_enable,
  313. .set_parent = s3c24xx_dclk_setparent,
  314. };
  315. struct clk s3c24xx_dclk1 = {
  316. .name = "dclk1",
  317. .id = -1,
  318. .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
  319. .enable = s3c24xx_dclk_enable,
  320. .set_parent = s3c24xx_dclk_setparent,
  321. };
  322. struct clk s3c24xx_clkout0 = {
  323. .name = "clkout0",
  324. .id = -1,
  325. .set_parent = s3c24xx_clkout_setparent,
  326. };
  327. struct clk s3c24xx_clkout1 = {
  328. .name = "clkout1",
  329. .id = -1,
  330. .set_parent = s3c24xx_clkout_setparent,
  331. };
  332. struct clk s3c24xx_uclk = {
  333. .name = "uclk",
  334. .id = -1,
  335. };
  336. /* standard clock definitions */
  337. static struct clk init_clocks[] = {
  338. {
  339. .name = "nand",
  340. .id = -1,
  341. .parent = &clk_h,
  342. .enable = s3c24xx_clkcon_enable,
  343. .ctrlbit = S3C2410_CLKCON_NAND,
  344. }, {
  345. .name = "lcd",
  346. .id = -1,
  347. .parent = &clk_h,
  348. .enable = s3c24xx_clkcon_enable,
  349. .ctrlbit = S3C2410_CLKCON_LCDC,
  350. }, {
  351. .name = "usb-host",
  352. .id = -1,
  353. .parent = &clk_h,
  354. .enable = s3c24xx_clkcon_enable,
  355. .ctrlbit = S3C2410_CLKCON_USBH,
  356. }, {
  357. .name = "usb-device",
  358. .id = -1,
  359. .parent = &clk_h,
  360. .enable = s3c24xx_clkcon_enable,
  361. .ctrlbit = S3C2410_CLKCON_USBD,
  362. }, {
  363. .name = "timers",
  364. .id = -1,
  365. .parent = &clk_p,
  366. .enable = s3c24xx_clkcon_enable,
  367. .ctrlbit = S3C2410_CLKCON_PWMT,
  368. }, {
  369. .name = "sdi",
  370. .id = -1,
  371. .parent = &clk_p,
  372. .enable = s3c24xx_clkcon_enable,
  373. .ctrlbit = S3C2410_CLKCON_SDI,
  374. }, {
  375. .name = "uart",
  376. .id = 0,
  377. .parent = &clk_p,
  378. .enable = s3c24xx_clkcon_enable,
  379. .ctrlbit = S3C2410_CLKCON_UART0,
  380. }, {
  381. .name = "uart",
  382. .id = 1,
  383. .parent = &clk_p,
  384. .enable = s3c24xx_clkcon_enable,
  385. .ctrlbit = S3C2410_CLKCON_UART1,
  386. }, {
  387. .name = "uart",
  388. .id = 2,
  389. .parent = &clk_p,
  390. .enable = s3c24xx_clkcon_enable,
  391. .ctrlbit = S3C2410_CLKCON_UART2,
  392. }, {
  393. .name = "gpio",
  394. .id = -1,
  395. .parent = &clk_p,
  396. .enable = s3c24xx_clkcon_enable,
  397. .ctrlbit = S3C2410_CLKCON_GPIO,
  398. }, {
  399. .name = "rtc",
  400. .id = -1,
  401. .parent = &clk_p,
  402. .enable = s3c24xx_clkcon_enable,
  403. .ctrlbit = S3C2410_CLKCON_RTC,
  404. }, {
  405. .name = "adc",
  406. .id = -1,
  407. .parent = &clk_p,
  408. .enable = s3c24xx_clkcon_enable,
  409. .ctrlbit = S3C2410_CLKCON_ADC,
  410. }, {
  411. .name = "i2c",
  412. .id = -1,
  413. .parent = &clk_p,
  414. .enable = s3c24xx_clkcon_enable,
  415. .ctrlbit = S3C2410_CLKCON_IIC,
  416. }, {
  417. .name = "iis",
  418. .id = -1,
  419. .parent = &clk_p,
  420. .enable = s3c24xx_clkcon_enable,
  421. .ctrlbit = S3C2410_CLKCON_IIS,
  422. }, {
  423. .name = "spi",
  424. .id = -1,
  425. .parent = &clk_p,
  426. .enable = s3c24xx_clkcon_enable,
  427. .ctrlbit = S3C2410_CLKCON_SPI,
  428. }, {
  429. .name = "watchdog",
  430. .id = -1,
  431. .parent = &clk_p,
  432. .ctrlbit = 0,
  433. }
  434. };
  435. /* initialise the clock system */
  436. int s3c24xx_register_clock(struct clk *clk)
  437. {
  438. clk->owner = THIS_MODULE;
  439. if (clk->enable == NULL)
  440. clk->enable = clk_null_enable;
  441. /* if this is a standard clock, set the usage state */
  442. if (clk->ctrlbit && clk->enable == s3c24xx_clkcon_enable) {
  443. unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
  444. clk->usage = (clkcon & clk->ctrlbit) ? 1 : 0;
  445. }
  446. /* add to the list of available clocks */
  447. mutex_lock(&clocks_mutex);
  448. list_add(&clk->list, &clocks);
  449. mutex_unlock(&clocks_mutex);
  450. return 0;
  451. }
  452. /* initalise all the clocks */
  453. int __init s3c24xx_setup_clocks(unsigned long xtal,
  454. unsigned long fclk,
  455. unsigned long hclk,
  456. unsigned long pclk)
  457. {
  458. unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
  459. unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
  460. struct clk *clkp = init_clocks;
  461. int ptr;
  462. int ret;
  463. printk(KERN_INFO "S3C2410 Clocks, (c) 2004 Simtec Electronics\n");
  464. /* initialise the main system clocks */
  465. clk_xtal.rate = xtal;
  466. clk_upll.rate = s3c2410_get_pll(upllcon, xtal);
  467. clk_h.rate = hclk;
  468. clk_p.rate = pclk;
  469. clk_f.rate = fclk;
  470. /* We must be careful disabling the clocks we are not intending to
  471. * be using at boot time, as subsytems such as the LCD which do
  472. * their own DMA requests to the bus can cause the system to lockup
  473. * if they where in the middle of requesting bus access.
  474. *
  475. * Disabling the LCD clock if the LCD is active is very dangerous,
  476. * and therefore the bootloader should be careful to not enable
  477. * the LCD clock if it is not needed.
  478. */
  479. mutex_lock(&clocks_mutex);
  480. s3c24xx_clk_enable(S3C2410_CLKCON_NAND, 0);
  481. s3c24xx_clk_enable(S3C2410_CLKCON_USBH, 0);
  482. s3c24xx_clk_enable(S3C2410_CLKCON_USBD, 0);
  483. s3c24xx_clk_enable(S3C2410_CLKCON_ADC, 0);
  484. s3c24xx_clk_enable(S3C2410_CLKCON_IIC, 0);
  485. s3c24xx_clk_enable(S3C2410_CLKCON_SPI, 0);
  486. mutex_unlock(&clocks_mutex);
  487. /* assume uart clocks are correctly setup */
  488. /* register our clocks */
  489. if (s3c24xx_register_clock(&clk_xtal) < 0)
  490. printk(KERN_ERR "failed to register master xtal\n");
  491. if (s3c24xx_register_clock(&clk_upll) < 0)
  492. printk(KERN_ERR "failed to register upll clock\n");
  493. if (s3c24xx_register_clock(&clk_f) < 0)
  494. printk(KERN_ERR "failed to register cpu fclk\n");
  495. if (s3c24xx_register_clock(&clk_h) < 0)
  496. printk(KERN_ERR "failed to register cpu hclk\n");
  497. if (s3c24xx_register_clock(&clk_p) < 0)
  498. printk(KERN_ERR "failed to register cpu pclk\n");
  499. if (s3c24xx_register_clock(&clk_usb_bus) < 0)
  500. printk(KERN_ERR "failed to register usb bus clock\n");
  501. /* register clocks from clock array */
  502. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
  503. ret = s3c24xx_register_clock(clkp);
  504. if (ret < 0) {
  505. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  506. clkp->name, ret);
  507. }
  508. }
  509. /* show the clock-slow value */
  510. printk("CLOCK: Slow mode (%ld.%ld MHz), %s, MPLL %s, UPLL %s\n",
  511. print_mhz(xtal / ( 2 * S3C2410_CLKSLOW_GET_SLOWVAL(clkslow))),
  512. (clkslow & S3C2410_CLKSLOW_SLOW) ? "slow" : "fast",
  513. (clkslow & S3C2410_CLKSLOW_MPLL_OFF) ? "off" : "on",
  514. (clkslow & S3C2410_CLKSLOW_UCLK_OFF) ? "off" : "on");
  515. return 0;
  516. }