clock.c 24 KB

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