clock.c 25 KB

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