clock.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /* linux/arch/arm/mach-s3c2443/clock.c
  2. *
  3. * Copyright (c) 2007 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2443 Clock control support
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/errno.h>
  27. #include <linux/err.h>
  28. #include <linux/sysdev.h>
  29. #include <linux/clk.h>
  30. #include <linux/mutex.h>
  31. #include <linux/delay.h>
  32. #include <linux/serial_core.h>
  33. #include <linux/io.h>
  34. #include <asm/mach/map.h>
  35. #include <mach/hardware.h>
  36. #include <mach/regs-s3c2443-clock.h>
  37. #include <asm/plat-s3c24xx/s3c2443.h>
  38. #include <asm/plat-s3c24xx/clock.h>
  39. #include <asm/plat-s3c24xx/cpu.h>
  40. /* We currently have to assume that the system is running
  41. * from the XTPll input, and that all ***REFCLKs are being
  42. * fed from it, as we cannot read the state of OM[4] from
  43. * software.
  44. *
  45. * It would be possible for each board initialisation to
  46. * set the correct muxing at initialisation
  47. */
  48. static int s3c2443_clkcon_enable_h(struct clk *clk, int enable)
  49. {
  50. unsigned int clocks = clk->ctrlbit;
  51. unsigned long clkcon;
  52. clkcon = __raw_readl(S3C2443_HCLKCON);
  53. if (enable)
  54. clkcon |= clocks;
  55. else
  56. clkcon &= ~clocks;
  57. __raw_writel(clkcon, S3C2443_HCLKCON);
  58. return 0;
  59. }
  60. static int s3c2443_clkcon_enable_p(struct clk *clk, int enable)
  61. {
  62. unsigned int clocks = clk->ctrlbit;
  63. unsigned long clkcon;
  64. clkcon = __raw_readl(S3C2443_PCLKCON);
  65. if (enable)
  66. clkcon |= clocks;
  67. else
  68. clkcon &= ~clocks;
  69. __raw_writel(clkcon, S3C2443_PCLKCON);
  70. return 0;
  71. }
  72. static int s3c2443_clkcon_enable_s(struct clk *clk, int enable)
  73. {
  74. unsigned int clocks = clk->ctrlbit;
  75. unsigned long clkcon;
  76. clkcon = __raw_readl(S3C2443_SCLKCON);
  77. if (enable)
  78. clkcon |= clocks;
  79. else
  80. clkcon &= ~clocks;
  81. __raw_writel(clkcon, S3C2443_SCLKCON);
  82. return 0;
  83. }
  84. static unsigned long s3c2443_roundrate_clksrc(struct clk *clk,
  85. unsigned long rate,
  86. unsigned int max)
  87. {
  88. unsigned long parent_rate = clk_get_rate(clk->parent);
  89. int div;
  90. if (rate > parent_rate)
  91. return parent_rate;
  92. /* note, we remove the +/- 1 calculations as they cancel out */
  93. div = (rate / parent_rate);
  94. if (div < 1)
  95. div = 1;
  96. else if (div > max)
  97. div = max;
  98. return parent_rate / div;
  99. }
  100. static unsigned long s3c2443_roundrate_clksrc4(struct clk *clk,
  101. unsigned long rate)
  102. {
  103. return s3c2443_roundrate_clksrc(clk, rate, 4);
  104. }
  105. static unsigned long s3c2443_roundrate_clksrc16(struct clk *clk,
  106. unsigned long rate)
  107. {
  108. return s3c2443_roundrate_clksrc(clk, rate, 16);
  109. }
  110. static unsigned long s3c2443_roundrate_clksrc256(struct clk *clk,
  111. unsigned long rate)
  112. {
  113. return s3c2443_roundrate_clksrc(clk, rate, 256);
  114. }
  115. /* clock selections */
  116. /* CPU EXTCLK input */
  117. static struct clk clk_ext = {
  118. .name = "ext",
  119. .id = -1,
  120. };
  121. static struct clk clk_mpllref = {
  122. .name = "mpllref",
  123. .parent = &clk_xtal,
  124. .id = -1,
  125. };
  126. #if 0
  127. static struct clk clk_mpll = {
  128. .name = "mpll",
  129. .parent = &clk_mpllref,
  130. .id = -1,
  131. };
  132. #endif
  133. static struct clk clk_epllref;
  134. static struct clk clk_epll = {
  135. .name = "epll",
  136. .parent = &clk_epllref,
  137. .id = -1,
  138. };
  139. static struct clk clk_i2s_ext = {
  140. .name = "i2s-ext",
  141. .id = -1,
  142. };
  143. static int s3c2443_setparent_epllref(struct clk *clk, struct clk *parent)
  144. {
  145. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  146. clksrc &= ~S3C2443_CLKSRC_EPLLREF_MASK;
  147. if (parent == &clk_xtal)
  148. clksrc |= S3C2443_CLKSRC_EPLLREF_XTAL;
  149. else if (parent == &clk_ext)
  150. clksrc |= S3C2443_CLKSRC_EPLLREF_EXTCLK;
  151. else if (parent != &clk_mpllref)
  152. return -EINVAL;
  153. __raw_writel(clksrc, S3C2443_CLKSRC);
  154. clk->parent = parent;
  155. return 0;
  156. }
  157. static struct clk clk_epllref = {
  158. .name = "epllref",
  159. .id = -1,
  160. .set_parent = s3c2443_setparent_epllref,
  161. };
  162. static unsigned long s3c2443_getrate_mdivclk(struct clk *clk)
  163. {
  164. unsigned long parent_rate = clk_get_rate(clk->parent);
  165. unsigned long div = __raw_readl(S3C2443_CLKDIV0);
  166. div &= S3C2443_CLKDIV0_EXTDIV_MASK;
  167. div >>= (S3C2443_CLKDIV0_EXTDIV_SHIFT-1); /* x2 */
  168. return parent_rate / (div + 1);
  169. }
  170. static struct clk clk_mdivclk = {
  171. .name = "mdivclk",
  172. .parent = &clk_mpllref,
  173. .id = -1,
  174. .get_rate = s3c2443_getrate_mdivclk,
  175. };
  176. static int s3c2443_setparent_msysclk(struct clk *clk, struct clk *parent)
  177. {
  178. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  179. clksrc &= ~(S3C2443_CLKSRC_MSYSCLK_MPLL |
  180. S3C2443_CLKSRC_EXTCLK_DIV);
  181. if (parent == &clk_mpll)
  182. clksrc |= S3C2443_CLKSRC_MSYSCLK_MPLL;
  183. else if (parent == &clk_mdivclk)
  184. clksrc |= S3C2443_CLKSRC_EXTCLK_DIV;
  185. else if (parent != &clk_mpllref)
  186. return -EINVAL;
  187. __raw_writel(clksrc, S3C2443_CLKSRC);
  188. clk->parent = parent;
  189. return 0;
  190. }
  191. static struct clk clk_msysclk = {
  192. .name = "msysclk",
  193. .parent = &clk_xtal,
  194. .id = -1,
  195. .set_parent = s3c2443_setparent_msysclk,
  196. };
  197. /* armdiv
  198. *
  199. * this clock is sourced from msysclk and can have a number of
  200. * divider values applied to it to then be fed into armclk.
  201. */
  202. static struct clk clk_armdiv = {
  203. .name = "armdiv",
  204. .id = -1,
  205. .parent = &clk_msysclk,
  206. };
  207. /* armclk
  208. *
  209. * this is the clock fed into the ARM core itself, either from
  210. * armdiv or from hclk.
  211. */
  212. static int s3c2443_setparent_armclk(struct clk *clk, struct clk *parent)
  213. {
  214. unsigned long clkdiv0;
  215. clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
  216. if (parent == &clk_armdiv)
  217. clkdiv0 &= ~S3C2443_CLKDIV0_DVS;
  218. else if (parent == &clk_h)
  219. clkdiv0 |= S3C2443_CLKDIV0_DVS;
  220. else
  221. return -EINVAL;
  222. __raw_writel(clkdiv0, S3C2443_CLKDIV0);
  223. return 0;
  224. }
  225. static struct clk clk_arm = {
  226. .name = "armclk",
  227. .id = -1,
  228. .set_parent = s3c2443_setparent_armclk,
  229. };
  230. /* esysclk
  231. *
  232. * this is sourced from either the EPLL or the EPLLref clock
  233. */
  234. static int s3c2443_setparent_esysclk(struct clk *clk, struct clk *parent)
  235. {
  236. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  237. if (parent == &clk_epll)
  238. clksrc |= S3C2443_CLKSRC_ESYSCLK_EPLL;
  239. else if (parent == &clk_epllref)
  240. clksrc &= ~S3C2443_CLKSRC_ESYSCLK_EPLL;
  241. else
  242. return -EINVAL;
  243. __raw_writel(clksrc, S3C2443_CLKSRC);
  244. clk->parent = parent;
  245. return 0;
  246. }
  247. static struct clk clk_esysclk = {
  248. .name = "esysclk",
  249. .parent = &clk_epll,
  250. .id = -1,
  251. .set_parent = s3c2443_setparent_esysclk,
  252. };
  253. /* uartclk
  254. *
  255. * UART baud-rate clock sourced from esysclk via a divisor
  256. */
  257. static unsigned long s3c2443_getrate_uart(struct clk *clk)
  258. {
  259. unsigned long parent_rate = clk_get_rate(clk->parent);
  260. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  261. div &= S3C2443_CLKDIV1_UARTDIV_MASK;
  262. div >>= S3C2443_CLKDIV1_UARTDIV_SHIFT;
  263. return parent_rate / (div + 1);
  264. }
  265. static int s3c2443_setrate_uart(struct clk *clk, unsigned long rate)
  266. {
  267. unsigned long parent_rate = clk_get_rate(clk->parent);
  268. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  269. rate = s3c2443_roundrate_clksrc16(clk, rate);
  270. rate = parent_rate / rate;
  271. clkdivn &= ~S3C2443_CLKDIV1_UARTDIV_MASK;
  272. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_UARTDIV_SHIFT;
  273. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  274. return 0;
  275. }
  276. static struct clk clk_uart = {
  277. .name = "uartclk",
  278. .id = -1,
  279. .parent = &clk_esysclk,
  280. .get_rate = s3c2443_getrate_uart,
  281. .set_rate = s3c2443_setrate_uart,
  282. .round_rate = s3c2443_roundrate_clksrc16,
  283. };
  284. /* hsspi
  285. *
  286. * high-speed spi clock, sourced from esysclk
  287. */
  288. static unsigned long s3c2443_getrate_hsspi(struct clk *clk)
  289. {
  290. unsigned long parent_rate = clk_get_rate(clk->parent);
  291. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  292. div &= S3C2443_CLKDIV1_HSSPIDIV_MASK;
  293. div >>= S3C2443_CLKDIV1_HSSPIDIV_SHIFT;
  294. return parent_rate / (div + 1);
  295. }
  296. static int s3c2443_setrate_hsspi(struct clk *clk, unsigned long rate)
  297. {
  298. unsigned long parent_rate = clk_get_rate(clk->parent);
  299. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  300. rate = s3c2443_roundrate_clksrc4(clk, rate);
  301. rate = parent_rate / rate;
  302. clkdivn &= ~S3C2443_CLKDIV1_HSSPIDIV_MASK;
  303. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_HSSPIDIV_SHIFT;
  304. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  305. return 0;
  306. }
  307. static struct clk clk_hsspi = {
  308. .name = "hsspi",
  309. .id = -1,
  310. .parent = &clk_esysclk,
  311. .ctrlbit = S3C2443_SCLKCON_HSSPICLK,
  312. .enable = s3c2443_clkcon_enable_s,
  313. .get_rate = s3c2443_getrate_hsspi,
  314. .set_rate = s3c2443_setrate_hsspi,
  315. .round_rate = s3c2443_roundrate_clksrc4,
  316. };
  317. /* usbhost
  318. *
  319. * usb host bus-clock, usually 48MHz to provide USB bus clock timing
  320. */
  321. static unsigned long s3c2443_getrate_usbhost(struct clk *clk)
  322. {
  323. unsigned long parent_rate = clk_get_rate(clk->parent);
  324. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  325. div &= S3C2443_CLKDIV1_USBHOSTDIV_MASK;
  326. div >>= S3C2443_CLKDIV1_USBHOSTDIV_SHIFT;
  327. return parent_rate / (div + 1);
  328. }
  329. static int s3c2443_setrate_usbhost(struct clk *clk, unsigned long rate)
  330. {
  331. unsigned long parent_rate = clk_get_rate(clk->parent);
  332. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  333. rate = s3c2443_roundrate_clksrc4(clk, rate);
  334. rate = parent_rate / rate;
  335. clkdivn &= ~S3C2443_CLKDIV1_USBHOSTDIV_MASK;
  336. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_USBHOSTDIV_SHIFT;
  337. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  338. return 0;
  339. }
  340. static struct clk clk_usb_bus_host = {
  341. .name = "usb-bus-host-parent",
  342. .id = -1,
  343. .parent = &clk_esysclk,
  344. .ctrlbit = S3C2443_SCLKCON_USBHOST,
  345. .enable = s3c2443_clkcon_enable_s,
  346. .get_rate = s3c2443_getrate_usbhost,
  347. .set_rate = s3c2443_setrate_usbhost,
  348. .round_rate = s3c2443_roundrate_clksrc4,
  349. };
  350. /* clk_hsmcc_div
  351. *
  352. * this clock is sourced from epll, and is fed through a divider,
  353. * to a mux controlled by sclkcon where either it or a extclk can
  354. * be fed to the hsmmc block
  355. */
  356. static unsigned long s3c2443_getrate_hsmmc_div(struct clk *clk)
  357. {
  358. unsigned long parent_rate = clk_get_rate(clk->parent);
  359. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  360. div &= S3C2443_CLKDIV1_HSMMCDIV_MASK;
  361. div >>= S3C2443_CLKDIV1_HSMMCDIV_SHIFT;
  362. return parent_rate / (div + 1);
  363. }
  364. static int s3c2443_setrate_hsmmc_div(struct clk *clk, unsigned long rate)
  365. {
  366. unsigned long parent_rate = clk_get_rate(clk->parent);
  367. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  368. rate = s3c2443_roundrate_clksrc4(clk, rate);
  369. rate = parent_rate / rate;
  370. clkdivn &= ~S3C2443_CLKDIV1_HSMMCDIV_MASK;
  371. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_HSMMCDIV_SHIFT;
  372. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  373. return 0;
  374. }
  375. static struct clk clk_hsmmc_div = {
  376. .name = "hsmmc-div",
  377. .id = -1,
  378. .parent = &clk_esysclk,
  379. .get_rate = s3c2443_getrate_hsmmc_div,
  380. .set_rate = s3c2443_setrate_hsmmc_div,
  381. .round_rate = s3c2443_roundrate_clksrc4,
  382. };
  383. static int s3c2443_setparent_hsmmc(struct clk *clk, struct clk *parent)
  384. {
  385. unsigned long clksrc = __raw_readl(S3C2443_SCLKCON);
  386. clksrc &= ~(S3C2443_SCLKCON_HSMMCCLK_EXT |
  387. S3C2443_SCLKCON_HSMMCCLK_EPLL);
  388. if (parent == &clk_epll)
  389. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EPLL;
  390. else if (parent == &clk_ext)
  391. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EXT;
  392. else
  393. return -EINVAL;
  394. if (clk->usage > 0) {
  395. __raw_writel(clksrc, S3C2443_SCLKCON);
  396. }
  397. clk->parent = parent;
  398. return 0;
  399. }
  400. static int s3c2443_enable_hsmmc(struct clk *clk, int enable)
  401. {
  402. return s3c2443_setparent_hsmmc(clk, clk->parent);
  403. }
  404. static struct clk clk_hsmmc = {
  405. .name = "hsmmc-if",
  406. .id = -1,
  407. .parent = &clk_hsmmc_div,
  408. .enable = s3c2443_enable_hsmmc,
  409. .set_parent = s3c2443_setparent_hsmmc,
  410. };
  411. /* i2s_eplldiv
  412. *
  413. * this clock is the output from the i2s divisor of esysclk
  414. */
  415. static unsigned long s3c2443_getrate_i2s_eplldiv(struct clk *clk)
  416. {
  417. unsigned long parent_rate = clk_get_rate(clk->parent);
  418. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  419. div &= S3C2443_CLKDIV1_I2SDIV_MASK;
  420. div >>= S3C2443_CLKDIV1_I2SDIV_SHIFT;
  421. return parent_rate / (div + 1);
  422. }
  423. static int s3c2443_setrate_i2s_eplldiv(struct clk *clk, unsigned long rate)
  424. {
  425. unsigned long parent_rate = clk_get_rate(clk->parent);
  426. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  427. rate = s3c2443_roundrate_clksrc16(clk, rate);
  428. rate = parent_rate / rate;
  429. clkdivn &= ~S3C2443_CLKDIV1_I2SDIV_MASK;
  430. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_I2SDIV_SHIFT;
  431. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  432. return 0;
  433. }
  434. static struct clk clk_i2s_eplldiv = {
  435. .name = "i2s-eplldiv",
  436. .id = -1,
  437. .parent = &clk_esysclk,
  438. .get_rate = s3c2443_getrate_i2s_eplldiv,
  439. .set_rate = s3c2443_setrate_i2s_eplldiv,
  440. .round_rate = s3c2443_roundrate_clksrc16,
  441. };
  442. /* i2s-ref
  443. *
  444. * i2s bus reference clock, selectable from external, esysclk or epllref
  445. */
  446. static int s3c2443_setparent_i2s(struct clk *clk, struct clk *parent)
  447. {
  448. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  449. clksrc &= ~S3C2443_CLKSRC_I2S_MASK;
  450. if (parent == &clk_epllref)
  451. clksrc |= S3C2443_CLKSRC_I2S_EPLLREF;
  452. else if (parent == &clk_i2s_ext)
  453. clksrc |= S3C2443_CLKSRC_I2S_EXT;
  454. else if (parent != &clk_i2s_eplldiv)
  455. return -EINVAL;
  456. clk->parent = parent;
  457. __raw_writel(clksrc, S3C2443_CLKSRC);
  458. return 0;
  459. }
  460. static struct clk clk_i2s = {
  461. .name = "i2s-if",
  462. .id = -1,
  463. .parent = &clk_i2s_eplldiv,
  464. .ctrlbit = S3C2443_SCLKCON_I2SCLK,
  465. .enable = s3c2443_clkcon_enable_s,
  466. .set_parent = s3c2443_setparent_i2s,
  467. };
  468. /* cam-if
  469. *
  470. * camera interface bus-clock, divided down from esysclk
  471. */
  472. static unsigned long s3c2443_getrate_cam(struct clk *clk)
  473. {
  474. unsigned long parent_rate = clk_get_rate(clk->parent);
  475. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  476. div &= S3C2443_CLKDIV1_CAMDIV_MASK;
  477. div >>= S3C2443_CLKDIV1_CAMDIV_SHIFT;
  478. return parent_rate / (div + 1);
  479. }
  480. static int s3c2443_setrate_cam(struct clk *clk, unsigned long rate)
  481. {
  482. unsigned long parent_rate = clk_get_rate(clk->parent);
  483. unsigned long clkdiv1 = __raw_readl(S3C2443_CLKDIV1);
  484. rate = s3c2443_roundrate_clksrc16(clk, rate);
  485. rate = parent_rate / rate;
  486. clkdiv1 &= ~S3C2443_CLKDIV1_CAMDIV_MASK;
  487. clkdiv1 |= (rate - 1) << S3C2443_CLKDIV1_CAMDIV_SHIFT;
  488. __raw_writel(clkdiv1, S3C2443_CLKDIV1);
  489. return 0;
  490. }
  491. static struct clk clk_cam = {
  492. .name = "camif-upll", /* same as 2440 name */
  493. .id = -1,
  494. .parent = &clk_esysclk,
  495. .ctrlbit = S3C2443_SCLKCON_CAMCLK,
  496. .enable = s3c2443_clkcon_enable_s,
  497. .get_rate = s3c2443_getrate_cam,
  498. .set_rate = s3c2443_setrate_cam,
  499. .round_rate = s3c2443_roundrate_clksrc16,
  500. };
  501. /* display-if
  502. *
  503. * display interface clock, divided from esysclk
  504. */
  505. static unsigned long s3c2443_getrate_display(struct clk *clk)
  506. {
  507. unsigned long parent_rate = clk_get_rate(clk->parent);
  508. unsigned long div = __raw_readl(S3C2443_CLKDIV1);
  509. div &= S3C2443_CLKDIV1_DISPDIV_MASK;
  510. div >>= S3C2443_CLKDIV1_DISPDIV_SHIFT;
  511. return parent_rate / (div + 1);
  512. }
  513. static int s3c2443_setrate_display(struct clk *clk, unsigned long rate)
  514. {
  515. unsigned long parent_rate = clk_get_rate(clk->parent);
  516. unsigned long clkdivn = __raw_readl(S3C2443_CLKDIV1);
  517. rate = s3c2443_roundrate_clksrc256(clk, rate);
  518. rate = parent_rate / rate;
  519. clkdivn &= ~S3C2443_CLKDIV1_UARTDIV_MASK;
  520. clkdivn |= (rate - 1) << S3C2443_CLKDIV1_UARTDIV_SHIFT;
  521. __raw_writel(clkdivn, S3C2443_CLKDIV1);
  522. return 0;
  523. }
  524. static struct clk clk_display = {
  525. .name = "display-if",
  526. .id = -1,
  527. .parent = &clk_esysclk,
  528. .ctrlbit = S3C2443_SCLKCON_DISPCLK,
  529. .enable = s3c2443_clkcon_enable_s,
  530. .get_rate = s3c2443_getrate_display,
  531. .set_rate = s3c2443_setrate_display,
  532. .round_rate = s3c2443_roundrate_clksrc256,
  533. };
  534. /* prediv
  535. *
  536. * this divides the msysclk down to pass to h/p/etc.
  537. */
  538. static unsigned long s3c2443_prediv_getrate(struct clk *clk)
  539. {
  540. unsigned long rate = clk_get_rate(clk->parent);
  541. unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
  542. clkdiv0 &= S3C2443_CLKDIV0_PREDIV_MASK;
  543. clkdiv0 >>= S3C2443_CLKDIV0_PREDIV_SHIFT;
  544. return rate / (clkdiv0 + 1);
  545. }
  546. static struct clk clk_prediv = {
  547. .name = "prediv",
  548. .id = -1,
  549. .parent = &clk_msysclk,
  550. .get_rate = s3c2443_prediv_getrate,
  551. };
  552. /* standard clock definitions */
  553. static struct clk init_clocks_disable[] = {
  554. {
  555. .name = "nand",
  556. .id = -1,
  557. .parent = &clk_h,
  558. }, {
  559. .name = "sdi",
  560. .id = -1,
  561. .parent = &clk_p,
  562. .enable = s3c2443_clkcon_enable_p,
  563. .ctrlbit = S3C2443_PCLKCON_SDI,
  564. }, {
  565. .name = "adc",
  566. .id = -1,
  567. .parent = &clk_p,
  568. .enable = s3c2443_clkcon_enable_p,
  569. .ctrlbit = S3C2443_PCLKCON_ADC,
  570. }, {
  571. .name = "i2c",
  572. .id = -1,
  573. .parent = &clk_p,
  574. .enable = s3c2443_clkcon_enable_p,
  575. .ctrlbit = S3C2443_PCLKCON_IIC,
  576. }, {
  577. .name = "iis",
  578. .id = -1,
  579. .parent = &clk_p,
  580. .enable = s3c2443_clkcon_enable_p,
  581. .ctrlbit = S3C2443_PCLKCON_IIS,
  582. }, {
  583. .name = "spi",
  584. .id = 0,
  585. .parent = &clk_p,
  586. .enable = s3c2443_clkcon_enable_p,
  587. .ctrlbit = S3C2443_PCLKCON_SPI0,
  588. }, {
  589. .name = "spi",
  590. .id = 1,
  591. .parent = &clk_p,
  592. .enable = s3c2443_clkcon_enable_p,
  593. .ctrlbit = S3C2443_PCLKCON_SPI1,
  594. }
  595. };
  596. static struct clk init_clocks[] = {
  597. {
  598. .name = "dma",
  599. .id = 0,
  600. .parent = &clk_h,
  601. .enable = s3c2443_clkcon_enable_h,
  602. .ctrlbit = S3C2443_HCLKCON_DMA0,
  603. }, {
  604. .name = "dma",
  605. .id = 1,
  606. .parent = &clk_h,
  607. .enable = s3c2443_clkcon_enable_h,
  608. .ctrlbit = S3C2443_HCLKCON_DMA1,
  609. }, {
  610. .name = "dma",
  611. .id = 2,
  612. .parent = &clk_h,
  613. .enable = s3c2443_clkcon_enable_h,
  614. .ctrlbit = S3C2443_HCLKCON_DMA2,
  615. }, {
  616. .name = "dma",
  617. .id = 3,
  618. .parent = &clk_h,
  619. .enable = s3c2443_clkcon_enable_h,
  620. .ctrlbit = S3C2443_HCLKCON_DMA3,
  621. }, {
  622. .name = "dma",
  623. .id = 4,
  624. .parent = &clk_h,
  625. .enable = s3c2443_clkcon_enable_h,
  626. .ctrlbit = S3C2443_HCLKCON_DMA4,
  627. }, {
  628. .name = "dma",
  629. .id = 5,
  630. .parent = &clk_h,
  631. .enable = s3c2443_clkcon_enable_h,
  632. .ctrlbit = S3C2443_HCLKCON_DMA5,
  633. }, {
  634. .name = "lcd",
  635. .id = -1,
  636. .parent = &clk_h,
  637. .enable = s3c2443_clkcon_enable_h,
  638. .ctrlbit = S3C2443_HCLKCON_LCDC,
  639. }, {
  640. .name = "gpio",
  641. .id = -1,
  642. .parent = &clk_p,
  643. .enable = s3c2443_clkcon_enable_p,
  644. .ctrlbit = S3C2443_PCLKCON_GPIO,
  645. }, {
  646. .name = "usb-host",
  647. .id = -1,
  648. .parent = &clk_h,
  649. .enable = s3c2443_clkcon_enable_h,
  650. .ctrlbit = S3C2443_HCLKCON_USBH,
  651. }, {
  652. .name = "usb-device",
  653. .id = -1,
  654. .parent = &clk_h,
  655. .enable = s3c2443_clkcon_enable_h,
  656. .ctrlbit = S3C2443_HCLKCON_USBD,
  657. }, {
  658. .name = "hsmmc",
  659. .id = -1,
  660. .parent = &clk_h,
  661. .enable = s3c2443_clkcon_enable_h,
  662. .ctrlbit = S3C2443_HCLKCON_HSMMC,
  663. }, {
  664. .name = "cfc",
  665. .id = -1,
  666. .parent = &clk_h,
  667. .enable = s3c2443_clkcon_enable_h,
  668. .ctrlbit = S3C2443_HCLKCON_CFC,
  669. }, {
  670. .name = "ssmc",
  671. .id = -1,
  672. .parent = &clk_h,
  673. .enable = s3c2443_clkcon_enable_h,
  674. .ctrlbit = S3C2443_HCLKCON_SSMC,
  675. }, {
  676. .name = "timers",
  677. .id = -1,
  678. .parent = &clk_p,
  679. .enable = s3c2443_clkcon_enable_p,
  680. .ctrlbit = S3C2443_PCLKCON_PWMT,
  681. }, {
  682. .name = "uart",
  683. .id = 0,
  684. .parent = &clk_p,
  685. .enable = s3c2443_clkcon_enable_p,
  686. .ctrlbit = S3C2443_PCLKCON_UART0,
  687. }, {
  688. .name = "uart",
  689. .id = 1,
  690. .parent = &clk_p,
  691. .enable = s3c2443_clkcon_enable_p,
  692. .ctrlbit = S3C2443_PCLKCON_UART1,
  693. }, {
  694. .name = "uart",
  695. .id = 2,
  696. .parent = &clk_p,
  697. .enable = s3c2443_clkcon_enable_p,
  698. .ctrlbit = S3C2443_PCLKCON_UART2,
  699. }, {
  700. .name = "uart",
  701. .id = 3,
  702. .parent = &clk_p,
  703. .enable = s3c2443_clkcon_enable_p,
  704. .ctrlbit = S3C2443_PCLKCON_UART3,
  705. }, {
  706. .name = "rtc",
  707. .id = -1,
  708. .parent = &clk_p,
  709. .enable = s3c2443_clkcon_enable_p,
  710. .ctrlbit = S3C2443_PCLKCON_RTC,
  711. }, {
  712. .name = "watchdog",
  713. .id = -1,
  714. .parent = &clk_p,
  715. .ctrlbit = S3C2443_PCLKCON_WDT,
  716. }, {
  717. .name = "usb-bus-host",
  718. .id = -1,
  719. .parent = &clk_usb_bus_host,
  720. }, {
  721. .name = "ac97",
  722. .id = -1,
  723. .parent = &clk_p,
  724. .ctrlbit = S3C2443_PCLKCON_AC97,
  725. }
  726. };
  727. /* clocks to add where we need to check their parentage */
  728. /* s3c2443_clk_initparents
  729. *
  730. * Initialise the parents for the clocks that we get at start-time
  731. */
  732. static int __init clk_init_set_parent(struct clk *clk, struct clk *parent)
  733. {
  734. printk(KERN_DEBUG "clock %s: parent %s\n", clk->name, parent->name);
  735. return clk_set_parent(clk, parent);
  736. }
  737. static void __init s3c2443_clk_initparents(void)
  738. {
  739. unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
  740. struct clk *parent;
  741. switch (clksrc & S3C2443_CLKSRC_EPLLREF_MASK) {
  742. case S3C2443_CLKSRC_EPLLREF_EXTCLK:
  743. parent = &clk_ext;
  744. break;
  745. case S3C2443_CLKSRC_EPLLREF_XTAL:
  746. default:
  747. parent = &clk_xtal;
  748. break;
  749. case S3C2443_CLKSRC_EPLLREF_MPLLREF:
  750. case S3C2443_CLKSRC_EPLLREF_MPLLREF2:
  751. parent = &clk_mpllref;
  752. break;
  753. }
  754. clk_init_set_parent(&clk_epllref, parent);
  755. switch (clksrc & S3C2443_CLKSRC_I2S_MASK) {
  756. case S3C2443_CLKSRC_I2S_EXT:
  757. parent = &clk_i2s_ext;
  758. break;
  759. case S3C2443_CLKSRC_I2S_EPLLDIV:
  760. default:
  761. parent = &clk_i2s_eplldiv;
  762. break;
  763. case S3C2443_CLKSRC_I2S_EPLLREF:
  764. case S3C2443_CLKSRC_I2S_EPLLREF3:
  765. parent = &clk_epllref;
  766. }
  767. clk_init_set_parent(&clk_i2s, &clk_epllref);
  768. /* esysclk source */
  769. parent = (clksrc & S3C2443_CLKSRC_ESYSCLK_EPLL) ?
  770. &clk_epll : &clk_epllref;
  771. clk_init_set_parent(&clk_esysclk, parent);
  772. /* msysclk source */
  773. if (clksrc & S3C2443_CLKSRC_MSYSCLK_MPLL) {
  774. parent = &clk_mpll;
  775. } else {
  776. parent = (clksrc & S3C2443_CLKSRC_EXTCLK_DIV) ?
  777. &clk_mdivclk : &clk_mpllref;
  778. }
  779. clk_init_set_parent(&clk_msysclk, parent);
  780. /* arm */
  781. if (__raw_readl(S3C2443_CLKDIV0) & S3C2443_CLKDIV0_DVS)
  782. parent = &clk_h;
  783. else
  784. parent = &clk_armdiv;
  785. clk_init_set_parent(&clk_arm, parent);
  786. }
  787. /* armdiv divisor table */
  788. static unsigned int armdiv[16] = {
  789. [S3C2443_CLKDIV0_ARMDIV_1 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 1,
  790. [S3C2443_CLKDIV0_ARMDIV_2 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 2,
  791. [S3C2443_CLKDIV0_ARMDIV_3 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 3,
  792. [S3C2443_CLKDIV0_ARMDIV_4 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 4,
  793. [S3C2443_CLKDIV0_ARMDIV_6 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 6,
  794. [S3C2443_CLKDIV0_ARMDIV_8 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 8,
  795. [S3C2443_CLKDIV0_ARMDIV_12 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 12,
  796. [S3C2443_CLKDIV0_ARMDIV_16 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 16,
  797. };
  798. static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0)
  799. {
  800. clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
  801. return armdiv[clkcon0 >> S3C2443_CLKDIV0_ARMDIV_SHIFT];
  802. }
  803. static inline unsigned long s3c2443_get_hdiv(unsigned long clkcon0)
  804. {
  805. clkcon0 &= S3C2443_CLKDIV0_HCLKDIV_MASK;
  806. return clkcon0 + 1;
  807. }
  808. /* clocks to add straight away */
  809. static struct clk *clks[] __initdata = {
  810. &clk_ext,
  811. &clk_epll,
  812. &clk_usb_bus_host,
  813. &clk_usb_bus,
  814. &clk_esysclk,
  815. &clk_epllref,
  816. &clk_mpllref,
  817. &clk_msysclk,
  818. &clk_uart,
  819. &clk_display,
  820. &clk_cam,
  821. &clk_i2s_eplldiv,
  822. &clk_i2s,
  823. &clk_hsspi,
  824. &clk_hsmmc_div,
  825. &clk_hsmmc,
  826. &clk_armdiv,
  827. &clk_arm,
  828. &clk_prediv,
  829. };
  830. void __init s3c2443_init_clocks(int xtal)
  831. {
  832. unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
  833. unsigned long mpllcon = __raw_readl(S3C2443_MPLLCON);
  834. unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
  835. unsigned long pll;
  836. unsigned long fclk;
  837. unsigned long hclk;
  838. unsigned long pclk;
  839. struct clk *clkp;
  840. int ret;
  841. int ptr;
  842. /* s3c2443 parents h and p clocks from prediv */
  843. clk_h.parent = &clk_prediv;
  844. clk_p.parent = &clk_prediv;
  845. pll = s3c2443_get_mpll(mpllcon, xtal);
  846. clk_msysclk.rate = pll;
  847. fclk = pll / s3c2443_fclk_div(clkdiv0);
  848. hclk = s3c2443_prediv_getrate(&clk_prediv);
  849. hclk = hclk / s3c2443_get_hdiv(clkdiv0);
  850. hclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_HCLK) ? 2 : 1);
  851. pclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_PCLK) ? 2 : 1);
  852. s3c24xx_setup_clocks(xtal, fclk, hclk, pclk);
  853. printk("S3C2443: mpll %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
  854. (mpllcon & S3C2443_PLLCON_OFF) ? "off":"on",
  855. print_mhz(pll), print_mhz(fclk),
  856. print_mhz(hclk), print_mhz(pclk));
  857. s3c2443_clk_initparents();
  858. for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
  859. clkp = clks[ptr];
  860. ret = s3c24xx_register_clock(clkp);
  861. if (ret < 0) {
  862. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  863. clkp->name, ret);
  864. }
  865. }
  866. clk_epll.rate = s3c2443_get_epll(epllcon, xtal);
  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. }