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