clock.c 26 KB

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