clock.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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 <linux/io.h>
  34. #include <asm/mach/map.h>
  35. #include <mach/hardware.h>
  36. #include <mach/regs-s3c2443-clock.h>
  37. #include <plat/s3c2443.h>
  38. #include <plat/clock.h>
  39. #include <plat/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_PCLKCON);
  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. /* armdiv
  198. *
  199. * this clock is sourced from msysclk and can have a number of
  200. * divider values applied to it to then be fed into armclk.
  201. */
  202. static struct clk clk_armdiv = {
  203. .name = "armdiv",
  204. .id = -1,
  205. .parent = &clk_msysclk,
  206. };
  207. /* armclk
  208. *
  209. * this is the clock fed into the ARM core itself, either from
  210. * armdiv or from hclk.
  211. */
  212. static int s3c2443_setparent_armclk(struct clk *clk, struct clk *parent)
  213. {
  214. unsigned long clkdiv0;
  215. clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
  216. if (parent == &clk_armdiv)
  217. clkdiv0 &= ~S3C2443_CLKDIV0_DVS;
  218. else if (parent == &clk_h)
  219. clkdiv0 |= S3C2443_CLKDIV0_DVS;
  220. else
  221. return -EINVAL;
  222. __raw_writel(clkdiv0, S3C2443_CLKDIV0);
  223. return 0;
  224. }
  225. static struct clk clk_arm = {
  226. .name = "armclk",
  227. .id = -1,
  228. .set_parent = s3c2443_setparent_armclk,
  229. };
  230. /* esysclk
  231. *
  232. * this is sourced from either the EPLL or the EPLLref clock
  233. */
  234. static int s3c2443_setparent_esysclk(struct clk *clk, struct clk *parent)
  235. {
  236. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  237. if (parent == &clk_epll)
  238. clksrc |= S3C2443_CLKSRC_ESYSCLK_EPLL;
  239. else if (parent == &clk_epllref)
  240. clksrc &= ~S3C2443_CLKSRC_ESYSCLK_EPLL;
  241. else
  242. return -EINVAL;
  243. __raw_writel(clksrc, S3C2443_CLKSRC);
  244. clk->parent = parent;
  245. return 0;
  246. }
  247. static struct clk clk_esysclk = {
  248. .name = "esysclk",
  249. .parent = &clk_epll,
  250. .id = -1,
  251. .set_parent = s3c2443_setparent_esysclk,
  252. };
  253. /* uartclk
  254. *
  255. * UART baud-rate clock sourced from esysclk via a divisor
  256. */
  257. static unsigned long s3c2443_getrate_uart(struct clk *clk)
  258. {
  259. unsigned long parent_rate = clk_get_rate(clk->parent);
  260. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  261. div &= S3C2443_CLKDIV1_UARTDIV_MASK;
  262. div >>= S3C2443_CLKDIV1_UARTDIV_SHIFT;
  263. return parent_rate / (div + 1);
  264. }
  265. static int s3c2443_setrate_uart(struct clk *clk, unsigned long rate)
  266. {
  267. unsigned long parent_rate = clk_get_rate(clk->parent);
  268. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  269. rate = s3c2443_roundrate_clksrc16(clk, rate);
  270. rate = parent_rate / rate;
  271. clkdivn &= ~S3C2443_CLKDIV1_UARTDIV_MASK;
  272. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_UARTDIV_SHIFT;
  273. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  274. return 0;
  275. }
  276. static struct clk clk_uart = {
  277. .name = "uartclk",
  278. .id = -1,
  279. .parent = &clk_esysclk,
  280. .get_rate = s3c2443_getrate_uart,
  281. .set_rate = s3c2443_setrate_uart,
  282. .round_rate = s3c2443_roundrate_clksrc16,
  283. };
  284. /* hsspi
  285. *
  286. * high-speed spi clock, sourced from esysclk
  287. */
  288. static unsigned long s3c2443_getrate_hsspi(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_HSSPIDIV_MASK;
  293. div >>= S3C2443_CLKDIV1_HSSPIDIV_SHIFT;
  294. return parent_rate / (div + 1);
  295. }
  296. static int s3c2443_setrate_hsspi(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_HSSPIDIV_MASK;
  303. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_HSSPIDIV_SHIFT;
  304. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  305. return 0;
  306. }
  307. static struct clk clk_hsspi = {
  308. .name = "hsspi",
  309. .id = -1,
  310. .parent = &clk_esysclk,
  311. .ctrlbit = S3C2443_SCLKCON_HSSPICLK,
  312. .enable = s3c2443_clkcon_enable_s,
  313. .get_rate = s3c2443_getrate_hsspi,
  314. .set_rate = s3c2443_setrate_hsspi,
  315. .round_rate = s3c2443_roundrate_clksrc4,
  316. };
  317. /* usbhost
  318. *
  319. * usb host bus-clock, usually 48MHz to provide USB bus clock timing
  320. */
  321. static unsigned long s3c2443_getrate_usbhost(struct clk *clk)
  322. {
  323. unsigned long parent_rate = clk_get_rate(clk->parent);
  324. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  325. div &= S3C2443_CLKDIV1_USBHOSTDIV_MASK;
  326. div >>= S3C2443_CLKDIV1_USBHOSTDIV_SHIFT;
  327. return parent_rate / (div + 1);
  328. }
  329. static int s3c2443_setrate_usbhost(struct clk *clk, unsigned long rate)
  330. {
  331. unsigned long parent_rate = clk_get_rate(clk->parent);
  332. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  333. rate = s3c2443_roundrate_clksrc4(clk, rate);
  334. rate = parent_rate / rate;
  335. clkdivn &= ~S3C2443_CLKDIV1_USBHOSTDIV_MASK;
  336. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_USBHOSTDIV_SHIFT;
  337. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  338. return 0;
  339. }
  340. static struct clk clk_usb_bus_host = {
  341. .name = "usb-bus-host-parent",
  342. .id = -1,
  343. .parent = &clk_esysclk,
  344. .ctrlbit = S3C2443_SCLKCON_USBHOST,
  345. .enable = s3c2443_clkcon_enable_s,
  346. .get_rate = s3c2443_getrate_usbhost,
  347. .set_rate = s3c2443_setrate_usbhost,
  348. .round_rate = s3c2443_roundrate_clksrc4,
  349. };
  350. /* clk_hsmcc_div
  351. *
  352. * this clock is sourced from epll, and is fed through a divider,
  353. * to a mux controlled by sclkcon where either it or a extclk can
  354. * be fed to the hsmmc block
  355. */
  356. static unsigned long s3c2443_getrate_hsmmc_div(struct clk *clk)
  357. {
  358. unsigned long parent_rate = clk_get_rate(clk->parent);
  359. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  360. div &= S3C2443_CLKDIV1_HSMMCDIV_MASK;
  361. div >>= S3C2443_CLKDIV1_HSMMCDIV_SHIFT;
  362. return parent_rate / (div + 1);
  363. }
  364. static int s3c2443_setrate_hsmmc_div(struct clk *clk, unsigned long rate)
  365. {
  366. unsigned long parent_rate = clk_get_rate(clk->parent);
  367. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  368. rate = s3c2443_roundrate_clksrc4(clk, rate);
  369. rate = parent_rate / rate;
  370. clkdivn &= ~S3C2443_CLKDIV1_HSMMCDIV_MASK;
  371. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_HSMMCDIV_SHIFT;
  372. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  373. return 0;
  374. }
  375. static struct clk clk_hsmmc_div = {
  376. .name = "hsmmc-div",
  377. .id = -1,
  378. .parent = &clk_esysclk,
  379. .get_rate = s3c2443_getrate_hsmmc_div,
  380. .set_rate = s3c2443_setrate_hsmmc_div,
  381. .round_rate = s3c2443_roundrate_clksrc4,
  382. };
  383. static int s3c2443_setparent_hsmmc(struct clk *clk, struct clk *parent)
  384. {
  385. unsigned long clksrc = __raw_readl(S3C2443_SCLKCON);
  386. clksrc &= ~(S3C2443_SCLKCON_HSMMCCLK_EXT |
  387. S3C2443_SCLKCON_HSMMCCLK_EPLL);
  388. if (parent == &clk_epll)
  389. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EPLL;
  390. else if (parent == &clk_ext)
  391. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EXT;
  392. else
  393. return -EINVAL;
  394. if (clk->usage > 0) {
  395. __raw_writel(clksrc, S3C2443_SCLKCON);
  396. }
  397. clk->parent = parent;
  398. return 0;
  399. }
  400. static int s3c2443_enable_hsmmc(struct clk *clk, int enable)
  401. {
  402. return s3c2443_setparent_hsmmc(clk, clk->parent);
  403. }
  404. static struct clk clk_hsmmc = {
  405. .name = "hsmmc-if",
  406. .id = -1,
  407. .parent = &clk_hsmmc_div,
  408. .enable = s3c2443_enable_hsmmc,
  409. .set_parent = s3c2443_setparent_hsmmc,
  410. };
  411. /* i2s_eplldiv
  412. *
  413. * this clock is the output from the i2s divisor of esysclk
  414. */
  415. static unsigned long s3c2443_getrate_i2s_eplldiv(struct clk *clk)
  416. {
  417. unsigned long parent_rate = clk_get_rate(clk->parent);
  418. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  419. div &= S3C2443_CLKDIV1_I2SDIV_MASK;
  420. div >>= S3C2443_CLKDIV1_I2SDIV_SHIFT;
  421. return parent_rate / (div + 1);
  422. }
  423. static int s3c2443_setrate_i2s_eplldiv(struct clk *clk, unsigned long rate)
  424. {
  425. unsigned long parent_rate = clk_get_rate(clk->parent);
  426. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  427. rate = s3c2443_roundrate_clksrc16(clk, rate);
  428. rate = parent_rate / rate;
  429. clkdivn &= ~S3C2443_CLKDIV1_I2SDIV_MASK;
  430. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_I2SDIV_SHIFT;
  431. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  432. return 0;
  433. }
  434. static struct clk clk_i2s_eplldiv = {
  435. .name = "i2s-eplldiv",
  436. .id = -1,
  437. .parent = &clk_esysclk,
  438. .get_rate = s3c2443_getrate_i2s_eplldiv,
  439. .set_rate = s3c2443_setrate_i2s_eplldiv,
  440. .round_rate = s3c2443_roundrate_clksrc16,
  441. };
  442. /* i2s-ref
  443. *
  444. * i2s bus reference clock, selectable from external, esysclk or epllref
  445. */
  446. static int s3c2443_setparent_i2s(struct clk *clk, struct clk *parent)
  447. {
  448. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  449. clksrc &= ~S3C2443_CLKSRC_I2S_MASK;
  450. if (parent == &clk_epllref)
  451. clksrc |= S3C2443_CLKSRC_I2S_EPLLREF;
  452. else if (parent == &clk_i2s_ext)
  453. clksrc |= S3C2443_CLKSRC_I2S_EXT;
  454. else if (parent != &clk_i2s_eplldiv)
  455. return -EINVAL;
  456. clk->parent = parent;
  457. __raw_writel(clksrc, S3C2443_CLKSRC);
  458. return 0;
  459. }
  460. static struct clk clk_i2s = {
  461. .name = "i2s-if",
  462. .id = -1,
  463. .parent = &clk_i2s_eplldiv,
  464. .ctrlbit = S3C2443_SCLKCON_I2SCLK,
  465. .enable = s3c2443_clkcon_enable_s,
  466. .set_parent = s3c2443_setparent_i2s,
  467. };
  468. /* cam-if
  469. *
  470. * camera interface bus-clock, divided down from esysclk
  471. */
  472. static unsigned long s3c2443_getrate_cam(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_CAMDIV_MASK;
  477. div >>= S3C2443_CLKDIV1_CAMDIV_SHIFT;
  478. return parent_rate / (div + 1);
  479. }
  480. static int s3c2443_setrate_cam(struct clk *clk, unsigned long rate)
  481. {
  482. unsigned long parent_rate = clk_get_rate(clk->parent);
  483. unsigned long clkdiv1 = __raw_readl(S3C2443_CLKDIV1);
  484. rate = s3c2443_roundrate_clksrc16(clk, rate);
  485. rate = parent_rate / rate;
  486. clkdiv1 &= ~S3C2443_CLKDIV1_CAMDIV_MASK;
  487. clkdiv1 |= (rate - 1) << S3C2443_CLKDIV1_CAMDIV_SHIFT;
  488. __raw_writel(clkdiv1, S3C2443_CLKDIV1);
  489. return 0;
  490. }
  491. static struct clk clk_cam = {
  492. .name = "camif-upll", /* same as 2440 name */
  493. .id = -1,
  494. .parent = &clk_esysclk,
  495. .ctrlbit = S3C2443_SCLKCON_CAMCLK,
  496. .enable = s3c2443_clkcon_enable_s,
  497. .get_rate = s3c2443_getrate_cam,
  498. .set_rate = s3c2443_setrate_cam,
  499. .round_rate = s3c2443_roundrate_clksrc16,
  500. };
  501. /* display-if
  502. *
  503. * display interface clock, divided from esysclk
  504. */
  505. static unsigned long s3c2443_getrate_display(struct clk *clk)
  506. {
  507. unsigned long parent_rate = clk_get_rate(clk->parent);
  508. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  509. div &= S3C2443_CLKDIV1_DISPDIV_MASK;
  510. div >>= S3C2443_CLKDIV1_DISPDIV_SHIFT;
  511. return parent_rate / (div + 1);
  512. }
  513. static int s3c2443_setrate_display(struct clk *clk, unsigned long rate)
  514. {
  515. unsigned long parent_rate = clk_get_rate(clk->parent);
  516. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  517. rate = s3c2443_roundrate_clksrc256(clk, rate);
  518. rate = parent_rate / rate;
  519. clkdivn &= ~S3C2443_CLKDIV1_UARTDIV_MASK;
  520. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_UARTDIV_SHIFT;
  521. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  522. return 0;
  523. }
  524. static struct clk clk_display = {
  525. .name = "display-if",
  526. .id = -1,
  527. .parent = &clk_esysclk,
  528. .ctrlbit = S3C2443_SCLKCON_DISPCLK,
  529. .enable = s3c2443_clkcon_enable_s,
  530. .get_rate = s3c2443_getrate_display,
  531. .set_rate = s3c2443_setrate_display,
  532. .round_rate = s3c2443_roundrate_clksrc256,
  533. };
  534. /* prediv
  535. *
  536. * this divides the msysclk down to pass to h/p/etc.
  537. */
  538. static unsigned long s3c2443_prediv_getrate(struct clk *clk)
  539. {
  540. unsigned long rate = clk_get_rate(clk->parent);
  541. unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
  542. clkdiv0 &= S3C2443_CLKDIV0_PREDIV_MASK;
  543. clkdiv0 >>= S3C2443_CLKDIV0_PREDIV_SHIFT;
  544. return rate / (clkdiv0 + 1);
  545. }
  546. static struct clk clk_prediv = {
  547. .name = "prediv",
  548. .id = -1,
  549. .parent = &clk_msysclk,
  550. .get_rate = s3c2443_prediv_getrate,
  551. };
  552. /* standard clock definitions */
  553. static struct clk init_clocks_disable[] = {
  554. {
  555. .name = "nand",
  556. .id = -1,
  557. .parent = &clk_h,
  558. }, {
  559. .name = "sdi",
  560. .id = -1,
  561. .parent = &clk_p,
  562. .enable = s3c2443_clkcon_enable_p,
  563. .ctrlbit = S3C2443_PCLKCON_SDI,
  564. }, {
  565. .name = "adc",
  566. .id = -1,
  567. .parent = &clk_p,
  568. .enable = s3c2443_clkcon_enable_p,
  569. .ctrlbit = S3C2443_PCLKCON_ADC,
  570. }, {
  571. .name = "i2c",
  572. .id = -1,
  573. .parent = &clk_p,
  574. .enable = s3c2443_clkcon_enable_p,
  575. .ctrlbit = S3C2443_PCLKCON_IIC,
  576. }, {
  577. .name = "iis",
  578. .id = -1,
  579. .parent = &clk_p,
  580. .enable = s3c2443_clkcon_enable_p,
  581. .ctrlbit = S3C2443_PCLKCON_IIS,
  582. }, {
  583. .name = "spi",
  584. .id = 0,
  585. .parent = &clk_p,
  586. .enable = s3c2443_clkcon_enable_p,
  587. .ctrlbit = S3C2443_PCLKCON_SPI0,
  588. }, {
  589. .name = "spi",
  590. .id = 1,
  591. .parent = &clk_p,
  592. .enable = s3c2443_clkcon_enable_p,
  593. .ctrlbit = S3C2443_PCLKCON_SPI1,
  594. }
  595. };
  596. static struct clk init_clocks[] = {
  597. {
  598. .name = "dma",
  599. .id = 0,
  600. .parent = &clk_h,
  601. .enable = s3c2443_clkcon_enable_h,
  602. .ctrlbit = S3C2443_HCLKCON_DMA0,
  603. }, {
  604. .name = "dma",
  605. .id = 1,
  606. .parent = &clk_h,
  607. .enable = s3c2443_clkcon_enable_h,
  608. .ctrlbit = S3C2443_HCLKCON_DMA1,
  609. }, {
  610. .name = "dma",
  611. .id = 2,
  612. .parent = &clk_h,
  613. .enable = s3c2443_clkcon_enable_h,
  614. .ctrlbit = S3C2443_HCLKCON_DMA2,
  615. }, {
  616. .name = "dma",
  617. .id = 3,
  618. .parent = &clk_h,
  619. .enable = s3c2443_clkcon_enable_h,
  620. .ctrlbit = S3C2443_HCLKCON_DMA3,
  621. }, {
  622. .name = "dma",
  623. .id = 4,
  624. .parent = &clk_h,
  625. .enable = s3c2443_clkcon_enable_h,
  626. .ctrlbit = S3C2443_HCLKCON_DMA4,
  627. }, {
  628. .name = "dma",
  629. .id = 5,
  630. .parent = &clk_h,
  631. .enable = s3c2443_clkcon_enable_h,
  632. .ctrlbit = S3C2443_HCLKCON_DMA5,
  633. }, {
  634. .name = "lcd",
  635. .id = -1,
  636. .parent = &clk_h,
  637. .enable = s3c2443_clkcon_enable_h,
  638. .ctrlbit = S3C2443_HCLKCON_LCDC,
  639. }, {
  640. .name = "gpio",
  641. .id = -1,
  642. .parent = &clk_p,
  643. .enable = s3c2443_clkcon_enable_p,
  644. .ctrlbit = S3C2443_PCLKCON_GPIO,
  645. }, {
  646. .name = "usb-host",
  647. .id = -1,
  648. .parent = &clk_h,
  649. .enable = s3c2443_clkcon_enable_h,
  650. .ctrlbit = S3C2443_HCLKCON_USBH,
  651. }, {
  652. .name = "usb-device",
  653. .id = -1,
  654. .parent = &clk_h,
  655. .enable = s3c2443_clkcon_enable_h,
  656. .ctrlbit = S3C2443_HCLKCON_USBD,
  657. }, {
  658. .name = "hsmmc",
  659. .id = -1,
  660. .parent = &clk_h,
  661. .enable = s3c2443_clkcon_enable_h,
  662. .ctrlbit = S3C2443_HCLKCON_HSMMC,
  663. }, {
  664. .name = "cfc",
  665. .id = -1,
  666. .parent = &clk_h,
  667. .enable = s3c2443_clkcon_enable_h,
  668. .ctrlbit = S3C2443_HCLKCON_CFC,
  669. }, {
  670. .name = "ssmc",
  671. .id = -1,
  672. .parent = &clk_h,
  673. .enable = s3c2443_clkcon_enable_h,
  674. .ctrlbit = S3C2443_HCLKCON_SSMC,
  675. }, {
  676. .name = "timers",
  677. .id = -1,
  678. .parent = &clk_p,
  679. .enable = s3c2443_clkcon_enable_p,
  680. .ctrlbit = S3C2443_PCLKCON_PWMT,
  681. }, {
  682. .name = "uart",
  683. .id = 0,
  684. .parent = &clk_p,
  685. .enable = s3c2443_clkcon_enable_p,
  686. .ctrlbit = S3C2443_PCLKCON_UART0,
  687. }, {
  688. .name = "uart",
  689. .id = 1,
  690. .parent = &clk_p,
  691. .enable = s3c2443_clkcon_enable_p,
  692. .ctrlbit = S3C2443_PCLKCON_UART1,
  693. }, {
  694. .name = "uart",
  695. .id = 2,
  696. .parent = &clk_p,
  697. .enable = s3c2443_clkcon_enable_p,
  698. .ctrlbit = S3C2443_PCLKCON_UART2,
  699. }, {
  700. .name = "uart",
  701. .id = 3,
  702. .parent = &clk_p,
  703. .enable = s3c2443_clkcon_enable_p,
  704. .ctrlbit = S3C2443_PCLKCON_UART3,
  705. }, {
  706. .name = "rtc",
  707. .id = -1,
  708. .parent = &clk_p,
  709. .enable = s3c2443_clkcon_enable_p,
  710. .ctrlbit = S3C2443_PCLKCON_RTC,
  711. }, {
  712. .name = "watchdog",
  713. .id = -1,
  714. .parent = &clk_p,
  715. .ctrlbit = S3C2443_PCLKCON_WDT,
  716. }, {
  717. .name = "usb-bus-host",
  718. .id = -1,
  719. .parent = &clk_usb_bus_host,
  720. }, {
  721. .name = "ac97",
  722. .id = -1,
  723. .parent = &clk_p,
  724. .ctrlbit = S3C2443_PCLKCON_AC97,
  725. }
  726. };
  727. /* clocks to add where we need to check their parentage */
  728. /* s3c2443_clk_initparents
  729. *
  730. * Initialise the parents for the clocks that we get at start-time
  731. */
  732. static int __init clk_init_set_parent(struct clk *clk, struct clk *parent)
  733. {
  734. printk(KERN_DEBUG "clock %s: parent %s\n", clk->name, parent->name);
  735. return clk_set_parent(clk, parent);
  736. }
  737. static void __init s3c2443_clk_initparents(void)
  738. {
  739. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  740. struct clk *parent;
  741. switch (clksrc & S3C2443_CLKSRC_EPLLREF_MASK) {
  742. case S3C2443_CLKSRC_EPLLREF_EXTCLK:
  743. parent = &clk_ext;
  744. break;
  745. case S3C2443_CLKSRC_EPLLREF_XTAL:
  746. default:
  747. parent = &clk_xtal;
  748. break;
  749. case S3C2443_CLKSRC_EPLLREF_MPLLREF:
  750. case S3C2443_CLKSRC_EPLLREF_MPLLREF2:
  751. parent = &clk_mpllref;
  752. break;
  753. }
  754. clk_init_set_parent(&clk_epllref, parent);
  755. switch (clksrc & S3C2443_CLKSRC_I2S_MASK) {
  756. case S3C2443_CLKSRC_I2S_EXT:
  757. parent = &clk_i2s_ext;
  758. break;
  759. case S3C2443_CLKSRC_I2S_EPLLDIV:
  760. default:
  761. parent = &clk_i2s_eplldiv;
  762. break;
  763. case S3C2443_CLKSRC_I2S_EPLLREF:
  764. case S3C2443_CLKSRC_I2S_EPLLREF3:
  765. parent = &clk_epllref;
  766. }
  767. clk_init_set_parent(&clk_i2s, &clk_epllref);
  768. /* esysclk source */
  769. parent = (clksrc & S3C2443_CLKSRC_ESYSCLK_EPLL) ?
  770. &clk_epll : &clk_epllref;
  771. clk_init_set_parent(&clk_esysclk, parent);
  772. /* msysclk source */
  773. if (clksrc & S3C2443_CLKSRC_MSYSCLK_MPLL) {
  774. parent = &clk_mpll;
  775. } else {
  776. parent = (clksrc & S3C2443_CLKSRC_EXTCLK_DIV) ?
  777. &clk_mdivclk : &clk_mpllref;
  778. }
  779. clk_init_set_parent(&clk_msysclk, parent);
  780. /* arm */
  781. if (__raw_readl(S3C2443_CLKDIV0) & S3C2443_CLKDIV0_DVS)
  782. parent = &clk_h;
  783. else
  784. parent = &clk_armdiv;
  785. clk_init_set_parent(&clk_arm, parent);
  786. }
  787. /* armdiv divisor table */
  788. static unsigned int armdiv[16] = {
  789. [S3C2443_CLKDIV0_ARMDIV_1 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 1,
  790. [S3C2443_CLKDIV0_ARMDIV_2 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 2,
  791. [S3C2443_CLKDIV0_ARMDIV_3 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 3,
  792. [S3C2443_CLKDIV0_ARMDIV_4 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 4,
  793. [S3C2443_CLKDIV0_ARMDIV_6 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 6,
  794. [S3C2443_CLKDIV0_ARMDIV_8 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 8,
  795. [S3C2443_CLKDIV0_ARMDIV_12 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 12,
  796. [S3C2443_CLKDIV0_ARMDIV_16 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 16,
  797. };
  798. static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0)
  799. {
  800. clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
  801. return armdiv[clkcon0 >> S3C2443_CLKDIV0_ARMDIV_SHIFT];
  802. }
  803. static inline unsigned long s3c2443_get_hdiv(unsigned long clkcon0)
  804. {
  805. clkcon0 &= S3C2443_CLKDIV0_HCLKDIV_MASK;
  806. return clkcon0 + 1;
  807. }
  808. /* clocks to add straight away */
  809. static struct clk *clks[] __initdata = {
  810. &clk_ext,
  811. &clk_epll,
  812. &clk_usb_bus_host,
  813. &clk_usb_bus,
  814. &clk_esysclk,
  815. &clk_epllref,
  816. &clk_mpllref,
  817. &clk_msysclk,
  818. &clk_uart,
  819. &clk_display,
  820. &clk_cam,
  821. &clk_i2s_eplldiv,
  822. &clk_i2s,
  823. &clk_hsspi,
  824. &clk_hsmmc_div,
  825. &clk_hsmmc,
  826. &clk_armdiv,
  827. &clk_arm,
  828. &clk_prediv,
  829. };
  830. void __init s3c2443_init_clocks(int xtal)
  831. {
  832. unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
  833. unsigned long mpllcon = __raw_readl(S3C2443_MPLLCON);
  834. unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
  835. unsigned long pll;
  836. unsigned long fclk;
  837. unsigned long hclk;
  838. unsigned long pclk;
  839. struct clk *clkp;
  840. int ret;
  841. int ptr;
  842. /* s3c2443 parents h and p clocks from prediv */
  843. clk_h.parent = &clk_prediv;
  844. clk_p.parent = &clk_prediv;
  845. pll = s3c2443_get_mpll(mpllcon, xtal);
  846. clk_msysclk.rate = pll;
  847. fclk = pll / s3c2443_fclk_div(clkdiv0);
  848. hclk = s3c2443_prediv_getrate(&clk_prediv);
  849. hclk /= s3c2443_get_hdiv(clkdiv0);
  850. pclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_PCLK) ? 2 : 1);
  851. s3c24xx_setup_clocks(xtal, fclk, hclk, pclk);
  852. printk("S3C2443: mpll %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
  853. (mpllcon & S3C2443_PLLCON_OFF) ? "off":"on",
  854. print_mhz(pll), print_mhz(fclk),
  855. print_mhz(hclk), print_mhz(pclk));
  856. s3c2443_clk_initparents();
  857. for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
  858. clkp = clks[ptr];
  859. ret = s3c24xx_register_clock(clkp);
  860. if (ret < 0) {
  861. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  862. clkp->name, ret);
  863. }
  864. }
  865. clk_epll.rate = s3c2443_get_epll(epllcon, xtal);
  866. clk_usb_bus.parent = &clk_usb_bus_host;
  867. /* ensure usb bus clock is within correct rate of 48MHz */
  868. if (clk_get_rate(&clk_usb_bus_host) != (48 * 1000 * 1000)) {
  869. printk(KERN_INFO "Warning: USB host bus not at 48MHz\n");
  870. clk_set_rate(&clk_usb_bus_host, 48*1000*1000);
  871. }
  872. printk("S3C2443: epll %s %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
  873. (epllcon & S3C2443_PLLCON_OFF) ? "off":"on",
  874. print_mhz(clk_get_rate(&clk_epll)),
  875. print_mhz(clk_get_rate(&clk_usb_bus)));
  876. /* register clocks from clock array */
  877. clkp = init_clocks;
  878. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
  879. ret = s3c24xx_register_clock(clkp);
  880. if (ret < 0) {
  881. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  882. clkp->name, ret);
  883. }
  884. }
  885. /* We must be careful disabling the clocks we are not intending to
  886. * be using at boot time, as subsystems such as the LCD which do
  887. * their own DMA requests to the bus can cause the system to lockup
  888. * if they where in the middle of requesting bus access.
  889. *
  890. * Disabling the LCD clock if the LCD is active is very dangerous,
  891. * and therefore the bootloader should be careful to not enable
  892. * the LCD clock if it is not needed.
  893. */
  894. /* install (and disable) the clocks we do not need immediately */
  895. clkp = init_clocks_disable;
  896. for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
  897. ret = s3c24xx_register_clock(clkp);
  898. if (ret < 0) {
  899. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  900. clkp->name, ret);
  901. }
  902. (clkp->enable)(clkp, 0);
  903. }
  904. }