clock.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /* linux/arch/arm/mach-s3c2443/clock.c
  2. *
  3. * Copyright (c) 2007 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2443 Clock control support
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/errno.h>
  27. #include <linux/err.h>
  28. #include <linux/sysdev.h>
  29. #include <linux/clk.h>
  30. #include <linux/mutex.h>
  31. #include <linux/delay.h>
  32. #include <linux/serial_core.h>
  33. #include <asm/mach/map.h>
  34. #include <asm/hardware.h>
  35. #include <asm/io.h>
  36. #include <asm/arch/regs-s3c2443-clock.h>
  37. #include <asm/plat-s3c24xx/s3c2443.h>
  38. #include <asm/plat-s3c24xx/clock.h>
  39. #include <asm/plat-s3c24xx/cpu.h>
  40. /* We currently have to assume that the system is running
  41. * from the XTPll input, and that all ***REFCLKs are being
  42. * fed from it, as we cannot read the state of OM[4] from
  43. * software.
  44. *
  45. * It would be possible for each board initialisation to
  46. * set the correct muxing at initialisation
  47. */
  48. static int s3c2443_clkcon_enable_h(struct clk *clk, int enable)
  49. {
  50. unsigned int clocks = clk->ctrlbit;
  51. unsigned long clkcon;
  52. clkcon = __raw_readl(S3C2443_HCLKCON);
  53. if (enable)
  54. clkcon |= clocks;
  55. else
  56. clkcon &= ~clocks;
  57. __raw_writel(clkcon, S3C2443_HCLKCON);
  58. return 0;
  59. }
  60. static int s3c2443_clkcon_enable_p(struct clk *clk, int enable)
  61. {
  62. unsigned int clocks = clk->ctrlbit;
  63. unsigned long clkcon;
  64. clkcon = __raw_readl(S3C2443_PCLKCON);
  65. if (enable)
  66. clkcon |= clocks;
  67. else
  68. clkcon &= ~clocks;
  69. __raw_writel(clkcon, S3C2443_HCLKCON);
  70. return 0;
  71. }
  72. static int s3c2443_clkcon_enable_s(struct clk *clk, int enable)
  73. {
  74. unsigned int clocks = clk->ctrlbit;
  75. unsigned long clkcon;
  76. clkcon = __raw_readl(S3C2443_SCLKCON);
  77. if (enable)
  78. clkcon |= clocks;
  79. else
  80. clkcon &= ~clocks;
  81. __raw_writel(clkcon, S3C2443_SCLKCON);
  82. return 0;
  83. }
  84. static unsigned long s3c2443_roundrate_clksrc(struct clk *clk,
  85. unsigned long rate,
  86. unsigned int max)
  87. {
  88. unsigned long parent_rate = clk_get_rate(clk->parent);
  89. int div;
  90. if (rate > parent_rate)
  91. return parent_rate;
  92. /* note, we remove the +/- 1 calculations as they cancel out */
  93. div = (rate / parent_rate);
  94. if (div < 1)
  95. div = 1;
  96. else if (div > max)
  97. div = max;
  98. return parent_rate / div;
  99. }
  100. static unsigned long s3c2443_roundrate_clksrc4(struct clk *clk,
  101. unsigned long rate)
  102. {
  103. return s3c2443_roundrate_clksrc(clk, rate, 4);
  104. }
  105. static unsigned long s3c2443_roundrate_clksrc16(struct clk *clk,
  106. unsigned long rate)
  107. {
  108. return s3c2443_roundrate_clksrc(clk, rate, 16);
  109. }
  110. static unsigned long s3c2443_roundrate_clksrc256(struct clk *clk,
  111. unsigned long rate)
  112. {
  113. return s3c2443_roundrate_clksrc(clk, rate, 256);
  114. }
  115. /* clock selections */
  116. /* CPU EXTCLK input */
  117. static struct clk clk_ext = {
  118. .name = "ext",
  119. .id = -1,
  120. };
  121. static struct clk clk_mpllref = {
  122. .name = "mpllref",
  123. .parent = &clk_xtal,
  124. .id = -1,
  125. };
  126. #if 0
  127. static struct clk clk_mpll = {
  128. .name = "mpll",
  129. .parent = &clk_mpllref,
  130. .id = -1,
  131. };
  132. #endif
  133. static struct clk clk_epllref;
  134. static struct clk clk_epll = {
  135. .name = "epll",
  136. .parent = &clk_epllref,
  137. .id = -1,
  138. };
  139. static struct clk clk_i2s_ext = {
  140. .name = "i2s-ext",
  141. .id = -1,
  142. };
  143. static int s3c2443_setparent_epllref(struct clk *clk, struct clk *parent)
  144. {
  145. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  146. clksrc &= ~S3C2443_CLKSRC_EPLLREF_MASK;
  147. if (parent == &clk_xtal)
  148. clksrc |= S3C2443_CLKSRC_EPLLREF_XTAL;
  149. else if (parent == &clk_ext)
  150. clksrc |= S3C2443_CLKSRC_EPLLREF_EXTCLK;
  151. else if (parent != &clk_mpllref)
  152. return -EINVAL;
  153. __raw_writel(clksrc, S3C2443_CLKSRC);
  154. clk->parent = parent;
  155. return 0;
  156. }
  157. static struct clk clk_epllref = {
  158. .name = "epllref",
  159. .id = -1,
  160. .set_parent = s3c2443_setparent_epllref,
  161. };
  162. static unsigned long s3c2443_getrate_mdivclk(struct clk *clk)
  163. {
  164. unsigned long parent_rate = clk_get_rate(clk->parent);
  165. unsigned long div = __raw_readl(S3C2443_CLKDIV0);
  166. div &= S3C2443_CLKDIV0_EXTDIV_MASK;
  167. div >>= (S3C2443_CLKDIV0_EXTDIV_SHIFT-1); /* x2 */
  168. return parent_rate / (div + 1);
  169. }
  170. static struct clk clk_mdivclk = {
  171. .name = "mdivclk",
  172. .parent = &clk_mpllref,
  173. .id = -1,
  174. .get_rate = s3c2443_getrate_mdivclk,
  175. };
  176. static int s3c2443_setparent_msysclk(struct clk *clk, struct clk *parent)
  177. {
  178. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  179. clksrc &= ~(S3C2443_CLKSRC_MSYSCLK_MPLL |
  180. S3C2443_CLKSRC_EXTCLK_DIV);
  181. if (parent == &clk_mpll)
  182. clksrc |= S3C2443_CLKSRC_MSYSCLK_MPLL;
  183. else if (parent == &clk_mdivclk)
  184. clksrc |= S3C2443_CLKSRC_EXTCLK_DIV;
  185. else if (parent != &clk_mpllref)
  186. return -EINVAL;
  187. __raw_writel(clksrc, S3C2443_CLKSRC);
  188. clk->parent = parent;
  189. return 0;
  190. }
  191. static struct clk clk_msysclk = {
  192. .name = "msysclk",
  193. .parent = &clk_xtal,
  194. .id = -1,
  195. .set_parent = s3c2443_setparent_msysclk,
  196. };
  197. /* esysclk
  198. *
  199. * this is sourced from either the EPLL or the EPLLref clock
  200. */
  201. static int s3c2443_setparent_esysclk(struct clk *clk, struct clk *parent)
  202. {
  203. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  204. if (parent == &clk_epll)
  205. clksrc |= S3C2443_CLKSRC_ESYSCLK_EPLL;
  206. else if (parent == &clk_epllref)
  207. clksrc &= ~S3C2443_CLKSRC_ESYSCLK_EPLL;
  208. else
  209. return -EINVAL;
  210. __raw_writel(clksrc, S3C2443_CLKSRC);
  211. clk->parent = parent;
  212. return 0;
  213. }
  214. static struct clk clk_esysclk = {
  215. .name = "esysclk",
  216. .parent = &clk_epll,
  217. .id = -1,
  218. .set_parent = s3c2443_setparent_esysclk,
  219. };
  220. /* uartclk
  221. *
  222. * UART baud-rate clock sourced from esysclk via a divisor
  223. */
  224. static unsigned long s3c2443_getrate_uart(struct clk *clk)
  225. {
  226. unsigned long parent_rate = clk_get_rate(clk->parent);
  227. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  228. div &= S3C2443_CLKDIV1_UARTDIV_MASK;
  229. div >>= S3C2443_CLKDIV1_UARTDIV_SHIFT;
  230. return parent_rate / (div + 1);
  231. }
  232. static int s3c2443_setrate_uart(struct clk *clk, unsigned long rate)
  233. {
  234. unsigned long parent_rate = clk_get_rate(clk->parent);
  235. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  236. rate = s3c2443_roundrate_clksrc16(clk, rate);
  237. rate = parent_rate / rate;
  238. clkdivn &= ~S3C2443_CLKDIV1_UARTDIV_MASK;
  239. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_UARTDIV_SHIFT;
  240. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  241. return 0;
  242. }
  243. static struct clk clk_uart = {
  244. .name = "uartclk",
  245. .id = -1,
  246. .parent = &clk_esysclk,
  247. .get_rate = s3c2443_getrate_uart,
  248. .set_rate = s3c2443_setrate_uart,
  249. .round_rate = s3c2443_roundrate_clksrc16,
  250. };
  251. /* hsspi
  252. *
  253. * high-speed spi clock, sourced from esysclk
  254. */
  255. static unsigned long s3c2443_getrate_hsspi(struct clk *clk)
  256. {
  257. unsigned long parent_rate = clk_get_rate(clk->parent);
  258. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  259. div &= S3C2443_CLKDIV1_HSSPIDIV_MASK;
  260. div >>= S3C2443_CLKDIV1_HSSPIDIV_SHIFT;
  261. return parent_rate / (div + 1);
  262. }
  263. static int s3c2443_setrate_hsspi(struct clk *clk, unsigned long rate)
  264. {
  265. unsigned long parent_rate = clk_get_rate(clk->parent);
  266. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  267. rate = s3c2443_roundrate_clksrc4(clk, rate);
  268. rate = parent_rate / rate;
  269. clkdivn &= ~S3C2443_CLKDIV1_HSSPIDIV_MASK;
  270. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_HSSPIDIV_SHIFT;
  271. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  272. return 0;
  273. }
  274. static struct clk clk_hsspi = {
  275. .name = "hsspi",
  276. .id = -1,
  277. .parent = &clk_esysclk,
  278. .ctrlbit = S3C2443_SCLKCON_HSSPICLK,
  279. .enable = s3c2443_clkcon_enable_s,
  280. .get_rate = s3c2443_getrate_hsspi,
  281. .set_rate = s3c2443_setrate_hsspi,
  282. .round_rate = s3c2443_roundrate_clksrc4,
  283. };
  284. /* usbhost
  285. *
  286. * usb host bus-clock, usually 48MHz to provide USB bus clock timing
  287. */
  288. static unsigned long s3c2443_getrate_usbhost(struct clk *clk)
  289. {
  290. unsigned long parent_rate = clk_get_rate(clk->parent);
  291. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  292. div &= S3C2443_CLKDIV1_USBHOSTDIV_MASK;
  293. div >>= S3C2443_CLKDIV1_USBHOSTDIV_SHIFT;
  294. return parent_rate / (div + 1);
  295. }
  296. static int s3c2443_setrate_usbhost(struct clk *clk, unsigned long rate)
  297. {
  298. unsigned long parent_rate = clk_get_rate(clk->parent);
  299. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  300. rate = s3c2443_roundrate_clksrc4(clk, rate);
  301. rate = parent_rate / rate;
  302. clkdivn &= ~S3C2443_CLKDIV1_USBHOSTDIV_MASK;
  303. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_USBHOSTDIV_SHIFT;
  304. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  305. return 0;
  306. }
  307. struct clk clk_usb_bus_host = {
  308. .name = "usb-bus-host-parent",
  309. .id = -1,
  310. .parent = &clk_esysclk,
  311. .ctrlbit = S3C2443_SCLKCON_USBHOST,
  312. .enable = s3c2443_clkcon_enable_s,
  313. .get_rate = s3c2443_getrate_usbhost,
  314. .set_rate = s3c2443_setrate_usbhost,
  315. .round_rate = s3c2443_roundrate_clksrc4,
  316. };
  317. /* clk_hsmcc_div
  318. *
  319. * this clock is sourced from epll, and is fed through a divider,
  320. * to a mux controlled by sclkcon where either it or a extclk can
  321. * be fed to the hsmmc block
  322. */
  323. static unsigned long s3c2443_getrate_hsmmc_div(struct clk *clk)
  324. {
  325. unsigned long parent_rate = clk_get_rate(clk->parent);
  326. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  327. div &= S3C2443_CLKDIV1_HSMMCDIV_MASK;
  328. div >>= S3C2443_CLKDIV1_HSMMCDIV_SHIFT;
  329. return parent_rate / (div + 1);
  330. }
  331. static int s3c2443_setrate_hsmmc_div(struct clk *clk, unsigned long rate)
  332. {
  333. unsigned long parent_rate = clk_get_rate(clk->parent);
  334. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  335. rate = s3c2443_roundrate_clksrc4(clk, rate);
  336. rate = parent_rate / rate;
  337. clkdivn &= ~S3C2443_CLKDIV1_HSMMCDIV_MASK;
  338. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_HSMMCDIV_SHIFT;
  339. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  340. return 0;
  341. }
  342. static struct clk clk_hsmmc_div = {
  343. .name = "hsmmc-div",
  344. .id = -1,
  345. .parent = &clk_esysclk,
  346. .get_rate = s3c2443_getrate_hsmmc_div,
  347. .set_rate = s3c2443_setrate_hsmmc_div,
  348. .round_rate = s3c2443_roundrate_clksrc4,
  349. };
  350. static int s3c2443_setparent_hsmmc(struct clk *clk, struct clk *parent)
  351. {
  352. unsigned long clksrc = __raw_readl(S3C2443_SCLKCON);
  353. clksrc &= ~(S3C2443_SCLKCON_HSMMCCLK_EXT |
  354. S3C2443_SCLKCON_HSMMCCLK_EPLL);
  355. if (parent == &clk_epll)
  356. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EPLL;
  357. else if (parent == &clk_ext)
  358. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EXT;
  359. else
  360. return -EINVAL;
  361. if (clk->usage > 0) {
  362. __raw_writel(clksrc, S3C2443_SCLKCON);
  363. }
  364. clk->parent = parent;
  365. return 0;
  366. }
  367. static int s3c2443_enable_hsmmc(struct clk *clk, int enable)
  368. {
  369. return s3c2443_setparent_hsmmc(clk, clk->parent);
  370. }
  371. static struct clk clk_hsmmc = {
  372. .name = "hsmmc-if",
  373. .id = -1,
  374. .parent = &clk_hsmmc_div,
  375. .enable = s3c2443_enable_hsmmc,
  376. .set_parent = s3c2443_setparent_hsmmc,
  377. };
  378. /* i2s_eplldiv
  379. *
  380. * this clock is the output from the i2s divisor of esysclk
  381. */
  382. static unsigned long s3c2443_getrate_i2s_eplldiv(struct clk *clk)
  383. {
  384. unsigned long parent_rate = clk_get_rate(clk->parent);
  385. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  386. div &= S3C2443_CLKDIV1_I2SDIV_MASK;
  387. div >>= S3C2443_CLKDIV1_I2SDIV_SHIFT;
  388. return parent_rate / (div + 1);
  389. }
  390. static int s3c2443_setrate_i2s_eplldiv(struct clk *clk, unsigned long rate)
  391. {
  392. unsigned long parent_rate = clk_get_rate(clk->parent);
  393. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  394. rate = s3c2443_roundrate_clksrc16(clk, rate);
  395. rate = parent_rate / rate;
  396. clkdivn &= ~S3C2443_CLKDIV1_I2SDIV_MASK;
  397. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_I2SDIV_SHIFT;
  398. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  399. return 0;
  400. }
  401. static struct clk clk_i2s_eplldiv = {
  402. .name = "i2s-eplldiv",
  403. .id = -1,
  404. .parent = &clk_esysclk,
  405. .get_rate = s3c2443_getrate_i2s_eplldiv,
  406. .set_rate = s3c2443_setrate_i2s_eplldiv,
  407. .round_rate = s3c2443_roundrate_clksrc16,
  408. };
  409. /* i2s-ref
  410. *
  411. * i2s bus reference clock, selectable from external, esysclk or epllref
  412. */
  413. static int s3c2443_setparent_i2s(struct clk *clk, struct clk *parent)
  414. {
  415. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  416. clksrc &= ~S3C2443_CLKSRC_I2S_MASK;
  417. if (parent == &clk_epllref)
  418. clksrc |= S3C2443_CLKSRC_I2S_EPLLREF;
  419. else if (parent == &clk_i2s_ext)
  420. clksrc |= S3C2443_CLKSRC_I2S_EXT;
  421. else if (parent != &clk_i2s_eplldiv)
  422. return -EINVAL;
  423. clk->parent = parent;
  424. __raw_writel(clksrc, S3C2443_CLKSRC);
  425. return 0;
  426. }
  427. static struct clk clk_i2s = {
  428. .name = "i2s-if",
  429. .id = -1,
  430. .parent = &clk_i2s_eplldiv,
  431. .ctrlbit = S3C2443_SCLKCON_I2SCLK,
  432. .enable = s3c2443_clkcon_enable_s,
  433. .set_parent = s3c2443_setparent_i2s,
  434. };
  435. /* cam-if
  436. *
  437. * camera interface bus-clock, divided down from esysclk
  438. */
  439. static unsigned long s3c2443_getrate_cam(struct clk *clk)
  440. {
  441. unsigned long parent_rate = clk_get_rate(clk->parent);
  442. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  443. div &= S3C2443_CLKDIV1_CAMDIV_MASK;
  444. div >>= S3C2443_CLKDIV1_CAMDIV_SHIFT;
  445. return parent_rate / (div + 1);
  446. }
  447. static int s3c2443_setrate_cam(struct clk *clk, unsigned long rate)
  448. {
  449. unsigned long parent_rate = clk_get_rate(clk->parent);
  450. unsigned long clkdiv1 = __raw_readl(S3C2443_CLKDIV1);
  451. rate = s3c2443_roundrate_clksrc16(clk, rate);
  452. rate = parent_rate / rate;
  453. clkdiv1 &= ~S3C2443_CLKDIV1_CAMDIV_MASK;
  454. clkdiv1 |= (rate - 1) << S3C2443_CLKDIV1_CAMDIV_SHIFT;
  455. __raw_writel(clkdiv1, S3C2443_CLKDIV1);
  456. return 0;
  457. }
  458. static struct clk clk_cam = {
  459. .name = "camif-upll", /* same as 2440 name */
  460. .id = -1,
  461. .parent = &clk_esysclk,
  462. .ctrlbit = S3C2443_SCLKCON_CAMCLK,
  463. .enable = s3c2443_clkcon_enable_s,
  464. .get_rate = s3c2443_getrate_cam,
  465. .set_rate = s3c2443_setrate_cam,
  466. .round_rate = s3c2443_roundrate_clksrc16,
  467. };
  468. /* display-if
  469. *
  470. * display interface clock, divided from esysclk
  471. */
  472. static unsigned long s3c2443_getrate_display(struct clk *clk)
  473. {
  474. unsigned long parent_rate = clk_get_rate(clk->parent);
  475. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  476. div &= S3C2443_CLKDIV1_DISPDIV_MASK;
  477. div >>= S3C2443_CLKDIV1_DISPDIV_SHIFT;
  478. return parent_rate / (div + 1);
  479. }
  480. static int s3c2443_setrate_display(struct clk *clk, unsigned long rate)
  481. {
  482. unsigned long parent_rate = clk_get_rate(clk->parent);
  483. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  484. rate = s3c2443_roundrate_clksrc256(clk, rate);
  485. rate = parent_rate / rate;
  486. clkdivn &= ~S3C2443_CLKDIV1_UARTDIV_MASK;
  487. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_UARTDIV_SHIFT;
  488. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  489. return 0;
  490. }
  491. static struct clk clk_display = {
  492. .name = "display-if",
  493. .id = -1,
  494. .parent = &clk_esysclk,
  495. .ctrlbit = S3C2443_SCLKCON_DISPCLK,
  496. .enable = s3c2443_clkcon_enable_s,
  497. .get_rate = s3c2443_getrate_display,
  498. .set_rate = s3c2443_setrate_display,
  499. .round_rate = s3c2443_roundrate_clksrc256,
  500. };
  501. /* standard clock definitions */
  502. static struct clk init_clocks_disable[] = {
  503. {
  504. .name = "nand",
  505. .id = -1,
  506. .parent = &clk_h,
  507. }, {
  508. .name = "sdi",
  509. .id = -1,
  510. .parent = &clk_p,
  511. .enable = s3c2443_clkcon_enable_p,
  512. .ctrlbit = S3C2443_PCLKCON_SDI,
  513. }, {
  514. .name = "adc",
  515. .id = -1,
  516. .parent = &clk_p,
  517. .enable = s3c2443_clkcon_enable_p,
  518. .ctrlbit = S3C2443_PCLKCON_ADC,
  519. }, {
  520. .name = "i2c",
  521. .id = -1,
  522. .parent = &clk_p,
  523. .enable = s3c2443_clkcon_enable_p,
  524. .ctrlbit = S3C2443_PCLKCON_IIC,
  525. }, {
  526. .name = "iis",
  527. .id = -1,
  528. .parent = &clk_p,
  529. .enable = s3c2443_clkcon_enable_p,
  530. .ctrlbit = S3C2443_PCLKCON_IIS,
  531. }, {
  532. .name = "spi",
  533. .id = 0,
  534. .parent = &clk_p,
  535. .enable = s3c2443_clkcon_enable_p,
  536. .ctrlbit = S3C2443_PCLKCON_SPI0,
  537. }, {
  538. .name = "spi",
  539. .id = 1,
  540. .parent = &clk_p,
  541. .enable = s3c2443_clkcon_enable_p,
  542. .ctrlbit = S3C2443_PCLKCON_SPI1,
  543. }
  544. };
  545. static struct clk init_clocks[] = {
  546. {
  547. .name = "dma",
  548. .id = 0,
  549. .parent = &clk_h,
  550. .enable = s3c2443_clkcon_enable_h,
  551. .ctrlbit = S3C2443_HCLKCON_DMA0,
  552. }, {
  553. .name = "dma",
  554. .id = 1,
  555. .parent = &clk_h,
  556. .enable = s3c2443_clkcon_enable_h,
  557. .ctrlbit = S3C2443_HCLKCON_DMA1,
  558. }, {
  559. .name = "dma",
  560. .id = 2,
  561. .parent = &clk_h,
  562. .enable = s3c2443_clkcon_enable_h,
  563. .ctrlbit = S3C2443_HCLKCON_DMA2,
  564. }, {
  565. .name = "dma",
  566. .id = 3,
  567. .parent = &clk_h,
  568. .enable = s3c2443_clkcon_enable_h,
  569. .ctrlbit = S3C2443_HCLKCON_DMA3,
  570. }, {
  571. .name = "dma",
  572. .id = 4,
  573. .parent = &clk_h,
  574. .enable = s3c2443_clkcon_enable_h,
  575. .ctrlbit = S3C2443_HCLKCON_DMA4,
  576. }, {
  577. .name = "dma",
  578. .id = 5,
  579. .parent = &clk_h,
  580. .enable = s3c2443_clkcon_enable_h,
  581. .ctrlbit = S3C2443_HCLKCON_DMA5,
  582. }, {
  583. .name = "lcd",
  584. .id = -1,
  585. .parent = &clk_h,
  586. .enable = s3c2443_clkcon_enable_h,
  587. .ctrlbit = S3C2443_HCLKCON_LCDC,
  588. }, {
  589. .name = "gpio",
  590. .id = -1,
  591. .parent = &clk_p,
  592. .enable = s3c2443_clkcon_enable_p,
  593. .ctrlbit = S3C2443_PCLKCON_GPIO,
  594. }, {
  595. .name = "usb-host",
  596. .id = -1,
  597. .parent = &clk_h,
  598. .enable = s3c2443_clkcon_enable_h,
  599. .ctrlbit = S3C2443_HCLKCON_USBH,
  600. }, {
  601. .name = "usb-device",
  602. .id = -1,
  603. .parent = &clk_h,
  604. .enable = s3c2443_clkcon_enable_h,
  605. .ctrlbit = S3C2443_HCLKCON_USBD,
  606. }, {
  607. .name = "timers",
  608. .id = -1,
  609. .parent = &clk_p,
  610. .enable = s3c2443_clkcon_enable_p,
  611. .ctrlbit = S3C2443_PCLKCON_PWMT,
  612. }, {
  613. .name = "uart",
  614. .id = 0,
  615. .parent = &clk_p,
  616. .enable = s3c2443_clkcon_enable_p,
  617. .ctrlbit = S3C2443_PCLKCON_UART0,
  618. }, {
  619. .name = "uart",
  620. .id = 1,
  621. .parent = &clk_p,
  622. .enable = s3c2443_clkcon_enable_p,
  623. .ctrlbit = S3C2443_PCLKCON_UART1,
  624. }, {
  625. .name = "uart",
  626. .id = 2,
  627. .parent = &clk_p,
  628. .enable = s3c2443_clkcon_enable_p,
  629. .ctrlbit = S3C2443_PCLKCON_UART2,
  630. }, {
  631. .name = "uart",
  632. .id = 3,
  633. .parent = &clk_p,
  634. .enable = s3c2443_clkcon_enable_p,
  635. .ctrlbit = S3C2443_PCLKCON_UART3,
  636. }, {
  637. .name = "rtc",
  638. .id = -1,
  639. .parent = &clk_p,
  640. .enable = s3c2443_clkcon_enable_p,
  641. .ctrlbit = S3C2443_PCLKCON_RTC,
  642. }, {
  643. .name = "watchdog",
  644. .id = -1,
  645. .parent = &clk_p,
  646. .ctrlbit = S3C2443_PCLKCON_WDT,
  647. }, {
  648. .name = "usb-bus-host",
  649. .id = -1,
  650. .parent = &clk_usb_bus_host,
  651. }, { .name = "ac97",
  652. .id = -1,
  653. .parent = &clk_p,
  654. .ctrlbit = S3C2443_PCLKCON_AC97,
  655. }
  656. };
  657. /* clocks to add where we need to check their parentage */
  658. /* s3c2443_clk_initparents
  659. *
  660. * Initialise the parents for the clocks that we get at start-time
  661. */
  662. static int __init clk_init_set_parent(struct clk *clk, struct clk *parent)
  663. {
  664. printk(KERN_DEBUG "clock %s: parent %s\n", clk->name, parent->name);
  665. return clk_set_parent(clk, parent);
  666. }
  667. static void __init s3c2443_clk_initparents(void)
  668. {
  669. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  670. struct clk *parent;
  671. switch (clksrc & S3C2443_CLKSRC_EPLLREF_MASK) {
  672. case S3C2443_CLKSRC_EPLLREF_EXTCLK:
  673. parent = &clk_ext;
  674. break;
  675. case S3C2443_CLKSRC_EPLLREF_XTAL:
  676. default:
  677. parent = &clk_xtal;
  678. break;
  679. case S3C2443_CLKSRC_EPLLREF_MPLLREF:
  680. case S3C2443_CLKSRC_EPLLREF_MPLLREF2:
  681. parent = &clk_mpllref;
  682. break;
  683. }
  684. clk_init_set_parent(&clk_epllref, parent);
  685. switch (clksrc & S3C2443_CLKSRC_I2S_MASK) {
  686. case S3C2443_CLKSRC_I2S_EXT:
  687. parent = &clk_i2s_ext;
  688. break;
  689. case S3C2443_CLKSRC_I2S_EPLLDIV:
  690. default:
  691. parent = &clk_i2s_eplldiv;
  692. break;
  693. case S3C2443_CLKSRC_I2S_EPLLREF:
  694. case S3C2443_CLKSRC_I2S_EPLLREF3:
  695. parent = &clk_epllref;
  696. }
  697. clk_init_set_parent(&clk_i2s, &clk_epllref);
  698. /* esysclk source */
  699. parent = (clksrc & S3C2443_CLKSRC_ESYSCLK_EPLL) ?
  700. &clk_epll : &clk_epllref;
  701. clk_init_set_parent(&clk_esysclk, parent);
  702. /* msysclk source */
  703. if (clksrc & S3C2443_CLKSRC_MSYSCLK_MPLL) {
  704. parent = &clk_mpll;
  705. } else {
  706. parent = (clksrc & S3C2443_CLKSRC_EXTCLK_DIV) ?
  707. &clk_mdivclk : &clk_mpllref;
  708. }
  709. clk_init_set_parent(&clk_msysclk, parent);
  710. }
  711. /* armdiv divisor table */
  712. static unsigned int armdiv[16] = {
  713. [S3C2443_CLKDIV0_ARMDIV_1 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 1,
  714. [S3C2443_CLKDIV0_ARMDIV_2 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 2,
  715. [S3C2443_CLKDIV0_ARMDIV_3 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 3,
  716. [S3C2443_CLKDIV0_ARMDIV_4 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 4,
  717. [S3C2443_CLKDIV0_ARMDIV_6 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 6,
  718. [S3C2443_CLKDIV0_ARMDIV_8 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 8,
  719. [S3C2443_CLKDIV0_ARMDIV_12 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 12,
  720. [S3C2443_CLKDIV0_ARMDIV_16 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 16,
  721. };
  722. static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0)
  723. {
  724. clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
  725. return armdiv[clkcon0 >> S3C2443_CLKDIV0_ARMDIV_SHIFT];
  726. }
  727. static inline unsigned long s3c2443_get_prediv(unsigned long clkcon0)
  728. {
  729. clkcon0 &= S3C2443_CLKDIV0_PREDIV_MASK;
  730. clkcon0 >>= S3C2443_CLKDIV0_PREDIV_SHIFT;
  731. return clkcon0 + 1;
  732. }
  733. /* clocks to add straight away */
  734. static struct clk *clks[] __initdata = {
  735. &clk_ext,
  736. &clk_epll,
  737. &clk_usb_bus_host,
  738. &clk_usb_bus,
  739. &clk_esysclk,
  740. &clk_epllref,
  741. &clk_mpllref,
  742. &clk_msysclk,
  743. &clk_uart,
  744. &clk_display,
  745. &clk_cam,
  746. &clk_i2s_eplldiv,
  747. &clk_i2s,
  748. &clk_hsspi,
  749. &clk_hsmmc_div,
  750. &clk_hsmmc,
  751. };
  752. void __init s3c2443_init_clocks(int xtal)
  753. {
  754. unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
  755. unsigned long mpllcon = __raw_readl(S3C2443_MPLLCON);
  756. unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
  757. unsigned long pll;
  758. unsigned long fclk;
  759. unsigned long hclk;
  760. unsigned long pclk;
  761. struct clk *clkp;
  762. int ret;
  763. int ptr;
  764. pll = s3c2443_get_mpll(mpllcon, xtal);
  765. fclk = pll / s3c2443_fclk_div(clkdiv0);
  766. hclk = fclk / s3c2443_get_prediv(clkdiv0);
  767. hclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_HCLK) ? 2 : 1);
  768. pclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_PCLK) ? 2 : 1);
  769. s3c24xx_setup_clocks(xtal, fclk, hclk, pclk);
  770. printk("S3C2443: mpll %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
  771. (mpllcon & S3C2443_PLLCON_OFF) ? "off":"on",
  772. print_mhz(pll), print_mhz(fclk),
  773. print_mhz(hclk), print_mhz(pclk));
  774. s3c2443_clk_initparents();
  775. for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
  776. clkp = clks[ptr];
  777. ret = s3c24xx_register_clock(clkp);
  778. if (ret < 0) {
  779. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  780. clkp->name, ret);
  781. }
  782. }
  783. clk_epll.rate = s3c2443_get_epll(epllcon, xtal);
  784. clk_usb_bus.parent = &clk_usb_bus_host;
  785. /* ensure usb bus clock is within correct rate of 48MHz */
  786. if (clk_get_rate(&clk_usb_bus_host) != (48 * 1000 * 1000)) {
  787. printk(KERN_INFO "Warning: USB host bus not at 48MHz\n");
  788. clk_set_rate(&clk_usb_bus_host, 48*1000*1000);
  789. }
  790. printk("S3C2443: epll %s %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
  791. (epllcon & S3C2443_PLLCON_OFF) ? "off":"on",
  792. print_mhz(clk_get_rate(&clk_epll)),
  793. print_mhz(clk_get_rate(&clk_usb_bus)));
  794. /* register clocks from clock array */
  795. clkp = init_clocks;
  796. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
  797. ret = s3c24xx_register_clock(clkp);
  798. if (ret < 0) {
  799. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  800. clkp->name, ret);
  801. }
  802. }
  803. /* We must be careful disabling the clocks we are not intending to
  804. * be using at boot time, as subsytems such as the LCD which do
  805. * their own DMA requests to the bus can cause the system to lockup
  806. * if they where in the middle of requesting bus access.
  807. *
  808. * Disabling the LCD clock if the LCD is active is very dangerous,
  809. * and therefore the bootloader should be careful to not enable
  810. * the LCD clock if it is not needed.
  811. */
  812. /* install (and disable) the clocks we do not need immediately */
  813. clkp = init_clocks_disable;
  814. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
  815. ret = s3c24xx_register_clock(clkp);
  816. if (ret < 0) {
  817. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  818. clkp->name, ret);
  819. }
  820. (clkp->enable)(clkp, 0);
  821. }
  822. }