clock.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. static 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 = "hsmmc",
  608. .id = -1,
  609. .parent = &clk_h,
  610. .enable = s3c2443_clkcon_enable_h,
  611. .ctrlbit = S3C2443_HCLKCON_HSMMC,
  612. }, {
  613. .name = "cfc",
  614. .id = -1,
  615. .parent = &clk_h,
  616. .enable = s3c2443_clkcon_enable_h,
  617. .ctrlbit = S3C2443_HCLKCON_CFC,
  618. }, {
  619. .name = "ssmc",
  620. .id = -1,
  621. .parent = &clk_h,
  622. .enable = s3c2443_clkcon_enable_h,
  623. .ctrlbit = S3C2443_HCLKCON_SSMC,
  624. }, {
  625. .name = "timers",
  626. .id = -1,
  627. .parent = &clk_p,
  628. .enable = s3c2443_clkcon_enable_p,
  629. .ctrlbit = S3C2443_PCLKCON_PWMT,
  630. }, {
  631. .name = "uart",
  632. .id = 0,
  633. .parent = &clk_p,
  634. .enable = s3c2443_clkcon_enable_p,
  635. .ctrlbit = S3C2443_PCLKCON_UART0,
  636. }, {
  637. .name = "uart",
  638. .id = 1,
  639. .parent = &clk_p,
  640. .enable = s3c2443_clkcon_enable_p,
  641. .ctrlbit = S3C2443_PCLKCON_UART1,
  642. }, {
  643. .name = "uart",
  644. .id = 2,
  645. .parent = &clk_p,
  646. .enable = s3c2443_clkcon_enable_p,
  647. .ctrlbit = S3C2443_PCLKCON_UART2,
  648. }, {
  649. .name = "uart",
  650. .id = 3,
  651. .parent = &clk_p,
  652. .enable = s3c2443_clkcon_enable_p,
  653. .ctrlbit = S3C2443_PCLKCON_UART3,
  654. }, {
  655. .name = "rtc",
  656. .id = -1,
  657. .parent = &clk_p,
  658. .enable = s3c2443_clkcon_enable_p,
  659. .ctrlbit = S3C2443_PCLKCON_RTC,
  660. }, {
  661. .name = "watchdog",
  662. .id = -1,
  663. .parent = &clk_p,
  664. .ctrlbit = S3C2443_PCLKCON_WDT,
  665. }, {
  666. .name = "usb-bus-host",
  667. .id = -1,
  668. .parent = &clk_usb_bus_host,
  669. }, {
  670. .name = "ac97",
  671. .id = -1,
  672. .parent = &clk_p,
  673. .ctrlbit = S3C2443_PCLKCON_AC97,
  674. }
  675. };
  676. /* clocks to add where we need to check their parentage */
  677. /* s3c2443_clk_initparents
  678. *
  679. * Initialise the parents for the clocks that we get at start-time
  680. */
  681. static int __init clk_init_set_parent(struct clk *clk, struct clk *parent)
  682. {
  683. printk(KERN_DEBUG "clock %s: parent %s\n", clk->name, parent->name);
  684. return clk_set_parent(clk, parent);
  685. }
  686. static void __init s3c2443_clk_initparents(void)
  687. {
  688. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  689. struct clk *parent;
  690. switch (clksrc & S3C2443_CLKSRC_EPLLREF_MASK) {
  691. case S3C2443_CLKSRC_EPLLREF_EXTCLK:
  692. parent = &clk_ext;
  693. break;
  694. case S3C2443_CLKSRC_EPLLREF_XTAL:
  695. default:
  696. parent = &clk_xtal;
  697. break;
  698. case S3C2443_CLKSRC_EPLLREF_MPLLREF:
  699. case S3C2443_CLKSRC_EPLLREF_MPLLREF2:
  700. parent = &clk_mpllref;
  701. break;
  702. }
  703. clk_init_set_parent(&clk_epllref, parent);
  704. switch (clksrc & S3C2443_CLKSRC_I2S_MASK) {
  705. case S3C2443_CLKSRC_I2S_EXT:
  706. parent = &clk_i2s_ext;
  707. break;
  708. case S3C2443_CLKSRC_I2S_EPLLDIV:
  709. default:
  710. parent = &clk_i2s_eplldiv;
  711. break;
  712. case S3C2443_CLKSRC_I2S_EPLLREF:
  713. case S3C2443_CLKSRC_I2S_EPLLREF3:
  714. parent = &clk_epllref;
  715. }
  716. clk_init_set_parent(&clk_i2s, &clk_epllref);
  717. /* esysclk source */
  718. parent = (clksrc & S3C2443_CLKSRC_ESYSCLK_EPLL) ?
  719. &clk_epll : &clk_epllref;
  720. clk_init_set_parent(&clk_esysclk, parent);
  721. /* msysclk source */
  722. if (clksrc & S3C2443_CLKSRC_MSYSCLK_MPLL) {
  723. parent = &clk_mpll;
  724. } else {
  725. parent = (clksrc & S3C2443_CLKSRC_EXTCLK_DIV) ?
  726. &clk_mdivclk : &clk_mpllref;
  727. }
  728. clk_init_set_parent(&clk_msysclk, parent);
  729. }
  730. /* armdiv divisor table */
  731. static unsigned int armdiv[16] = {
  732. [S3C2443_CLKDIV0_ARMDIV_1 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 1,
  733. [S3C2443_CLKDIV0_ARMDIV_2 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 2,
  734. [S3C2443_CLKDIV0_ARMDIV_3 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 3,
  735. [S3C2443_CLKDIV0_ARMDIV_4 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 4,
  736. [S3C2443_CLKDIV0_ARMDIV_6 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 6,
  737. [S3C2443_CLKDIV0_ARMDIV_8 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 8,
  738. [S3C2443_CLKDIV0_ARMDIV_12 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 12,
  739. [S3C2443_CLKDIV0_ARMDIV_16 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 16,
  740. };
  741. static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0)
  742. {
  743. clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
  744. return armdiv[clkcon0 >> S3C2443_CLKDIV0_ARMDIV_SHIFT];
  745. }
  746. static inline unsigned long s3c2443_get_prediv(unsigned long clkcon0)
  747. {
  748. clkcon0 &= S3C2443_CLKDIV0_PREDIV_MASK;
  749. clkcon0 >>= S3C2443_CLKDIV0_PREDIV_SHIFT;
  750. return clkcon0 + 1;
  751. }
  752. /* clocks to add straight away */
  753. static struct clk *clks[] __initdata = {
  754. &clk_ext,
  755. &clk_epll,
  756. &clk_usb_bus_host,
  757. &clk_usb_bus,
  758. &clk_esysclk,
  759. &clk_epllref,
  760. &clk_mpllref,
  761. &clk_msysclk,
  762. &clk_uart,
  763. &clk_display,
  764. &clk_cam,
  765. &clk_i2s_eplldiv,
  766. &clk_i2s,
  767. &clk_hsspi,
  768. &clk_hsmmc_div,
  769. &clk_hsmmc,
  770. };
  771. void __init s3c2443_init_clocks(int xtal)
  772. {
  773. unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
  774. unsigned long mpllcon = __raw_readl(S3C2443_MPLLCON);
  775. unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
  776. unsigned long pll;
  777. unsigned long fclk;
  778. unsigned long hclk;
  779. unsigned long pclk;
  780. struct clk *clkp;
  781. int ret;
  782. int ptr;
  783. pll = s3c2443_get_mpll(mpllcon, xtal);
  784. fclk = pll / s3c2443_fclk_div(clkdiv0);
  785. hclk = fclk / s3c2443_get_prediv(clkdiv0);
  786. hclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_HCLK) ? 2 : 1);
  787. pclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_PCLK) ? 2 : 1);
  788. s3c24xx_setup_clocks(xtal, fclk, hclk, pclk);
  789. printk("S3C2443: mpll %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
  790. (mpllcon & S3C2443_PLLCON_OFF) ? "off":"on",
  791. print_mhz(pll), print_mhz(fclk),
  792. print_mhz(hclk), print_mhz(pclk));
  793. s3c2443_clk_initparents();
  794. for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
  795. clkp = clks[ptr];
  796. ret = s3c24xx_register_clock(clkp);
  797. if (ret < 0) {
  798. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  799. clkp->name, ret);
  800. }
  801. }
  802. clk_epll.rate = s3c2443_get_epll(epllcon, xtal);
  803. clk_usb_bus.parent = &clk_usb_bus_host;
  804. /* ensure usb bus clock is within correct rate of 48MHz */
  805. if (clk_get_rate(&clk_usb_bus_host) != (48 * 1000 * 1000)) {
  806. printk(KERN_INFO "Warning: USB host bus not at 48MHz\n");
  807. clk_set_rate(&clk_usb_bus_host, 48*1000*1000);
  808. }
  809. printk("S3C2443: epll %s %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
  810. (epllcon & S3C2443_PLLCON_OFF) ? "off":"on",
  811. print_mhz(clk_get_rate(&clk_epll)),
  812. print_mhz(clk_get_rate(&clk_usb_bus)));
  813. /* register clocks from clock array */
  814. clkp = init_clocks;
  815. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
  816. ret = s3c24xx_register_clock(clkp);
  817. if (ret < 0) {
  818. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  819. clkp->name, ret);
  820. }
  821. }
  822. /* We must be careful disabling the clocks we are not intending to
  823. * be using at boot time, as subsystems such as the LCD which do
  824. * their own DMA requests to the bus can cause the system to lockup
  825. * if they where in the middle of requesting bus access.
  826. *
  827. * Disabling the LCD clock if the LCD is active is very dangerous,
  828. * and therefore the bootloader should be careful to not enable
  829. * the LCD clock if it is not needed.
  830. */
  831. /* install (and disable) the clocks we do not need immediately */
  832. clkp = init_clocks_disable;
  833. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
  834. ret = s3c24xx_register_clock(clkp);
  835. if (ret < 0) {
  836. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  837. clkp->name, ret);
  838. }
  839. (clkp->enable)(clkp, 0);
  840. }
  841. }