clock.c 25 KB

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