clock.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * linux/arch/arm/mach-omap/clock.c
  3. *
  4. * Copyright (C) 2004 Nokia corporation
  5. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <asm/semaphore.h>
  17. #include <asm/hardware/clock.h>
  18. #include <asm/arch/board.h>
  19. #include <asm/arch/usb.h>
  20. #include "clock.h"
  21. static LIST_HEAD(clocks);
  22. static DECLARE_MUTEX(clocks_sem);
  23. static DEFINE_SPINLOCK(clockfw_lock);
  24. static void propagate_rate(struct clk * clk);
  25. /* External clock (MCLK & BCLK) functions */
  26. static int set_ext_clk_rate(struct clk * clk, unsigned long rate);
  27. static long round_ext_clk_rate(struct clk * clk, unsigned long rate);
  28. static void init_ext_clk(struct clk * clk);
  29. /* MPU virtual clock functions */
  30. static int select_table_rate(struct clk * clk, unsigned long rate);
  31. static long round_to_table_rate(struct clk * clk, unsigned long rate);
  32. void clk_setdpll(__u16, __u16);
  33. struct mpu_rate rate_table[] = {
  34. /* MPU MHz, xtal MHz, dpll1 MHz, CKCTL, DPLL_CTL
  35. * armdiv, dspdiv, dspmmu, tcdiv, perdiv, lcddiv
  36. */
  37. #if defined(CONFIG_OMAP_ARM_216MHZ)
  38. { 216000000, 12000000, 216000000, 0x050d, 0x2910 }, /* 1/1/2/2/2/8 */
  39. #endif
  40. #if defined(CONFIG_OMAP_ARM_195MHZ)
  41. { 195000000, 13000000, 195000000, 0x050e, 0x2790 }, /* 1/1/2/2/4/8 */
  42. #endif
  43. #if defined(CONFIG_OMAP_ARM_192MHZ)
  44. { 192000000, 19200000, 192000000, 0x050f, 0x2510 }, /* 1/1/2/2/8/8 */
  45. { 192000000, 12000000, 192000000, 0x050f, 0x2810 }, /* 1/1/2/2/8/8 */
  46. { 96000000, 12000000, 192000000, 0x055f, 0x2810 }, /* 2/2/2/2/8/8 */
  47. { 48000000, 12000000, 192000000, 0x0ccf, 0x2810 }, /* 4/4/4/4/8/8 */
  48. { 24000000, 12000000, 192000000, 0x0fff, 0x2810 }, /* 8/8/8/8/8/8 */
  49. #endif
  50. #if defined(CONFIG_OMAP_ARM_182MHZ)
  51. { 182000000, 13000000, 182000000, 0x050e, 0x2710 }, /* 1/1/2/2/4/8 */
  52. #endif
  53. #if defined(CONFIG_OMAP_ARM_168MHZ)
  54. { 168000000, 12000000, 168000000, 0x010f, 0x2710 }, /* 1/1/1/2/8/8 */
  55. #endif
  56. #if defined(CONFIG_OMAP_ARM_150MHZ)
  57. { 150000000, 12000000, 150000000, 0x150a, 0x2cb0 }, /* 0/0/1/1/2/2 */
  58. #endif
  59. #if defined(CONFIG_OMAP_ARM_120MHZ)
  60. { 120000000, 12000000, 120000000, 0x010a, 0x2510 }, /* 1/1/1/2/4/4 */
  61. #endif
  62. #if defined(CONFIG_OMAP_ARM_96MHZ)
  63. { 96000000, 12000000, 96000000, 0x0005, 0x2410 }, /* 1/1/1/1/2/2 */
  64. #endif
  65. #if defined(CONFIG_OMAP_ARM_60MHZ)
  66. { 60000000, 12000000, 60000000, 0x0005, 0x2290 }, /* 1/1/1/1/2/2 */
  67. #endif
  68. #if defined(CONFIG_OMAP_ARM_30MHZ)
  69. { 30000000, 12000000, 60000000, 0x0555, 0x2290 }, /* 2/2/2/2/2/2 */
  70. #endif
  71. { 0, 0, 0, 0, 0 },
  72. };
  73. static void ckctl_recalc(struct clk * clk)
  74. {
  75. int dsor;
  76. /* Calculate divisor encoded as 2-bit exponent */
  77. dsor = 1 << (3 & (omap_readw(ARM_CKCTL) >> clk->rate_offset));
  78. if (unlikely(clk->rate == clk->parent->rate / dsor))
  79. return; /* No change, quick exit */
  80. clk->rate = clk->parent->rate / dsor;
  81. if (unlikely(clk->flags & RATE_PROPAGATES))
  82. propagate_rate(clk);
  83. }
  84. static void followparent_recalc(struct clk * clk)
  85. {
  86. clk->rate = clk->parent->rate;
  87. }
  88. static void watchdog_recalc(struct clk * clk)
  89. {
  90. clk->rate = clk->parent->rate / 14;
  91. }
  92. static struct clk ck_ref = {
  93. .name = "ck_ref",
  94. .rate = 12000000,
  95. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  96. ALWAYS_ENABLED,
  97. };
  98. static struct clk ck_dpll1 = {
  99. .name = "ck_dpll1",
  100. .parent = &ck_ref,
  101. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  102. RATE_PROPAGATES | ALWAYS_ENABLED,
  103. };
  104. static struct clk ck_dpll1out = {
  105. .name = "ck_dpll1out",
  106. .parent = &ck_dpll1,
  107. .flags = CLOCK_IN_OMAP16XX,
  108. .enable_reg = ARM_IDLECT2,
  109. .enable_bit = EN_CKOUT_ARM,
  110. .recalc = &followparent_recalc,
  111. };
  112. static struct clk arm_ck = {
  113. .name = "arm_ck",
  114. .parent = &ck_dpll1,
  115. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  116. RATE_CKCTL | RATE_PROPAGATES | ALWAYS_ENABLED,
  117. .rate_offset = CKCTL_ARMDIV_OFFSET,
  118. .recalc = &ckctl_recalc,
  119. };
  120. static struct clk armper_ck = {
  121. .name = "armper_ck",
  122. .parent = &ck_dpll1,
  123. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  124. RATE_CKCTL,
  125. .enable_reg = ARM_IDLECT2,
  126. .enable_bit = EN_PERCK,
  127. .rate_offset = CKCTL_PERDIV_OFFSET,
  128. .recalc = &ckctl_recalc,
  129. };
  130. static struct clk arm_gpio_ck = {
  131. .name = "arm_gpio_ck",
  132. .parent = &ck_dpll1,
  133. .flags = CLOCK_IN_OMAP1510,
  134. .enable_reg = ARM_IDLECT2,
  135. .enable_bit = EN_GPIOCK,
  136. .recalc = &followparent_recalc,
  137. };
  138. static struct clk armxor_ck = {
  139. .name = "armxor_ck",
  140. .parent = &ck_ref,
  141. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
  142. .enable_reg = ARM_IDLECT2,
  143. .enable_bit = EN_XORPCK,
  144. .recalc = &followparent_recalc,
  145. };
  146. static struct clk armtim_ck = {
  147. .name = "armtim_ck",
  148. .parent = &ck_ref,
  149. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
  150. .enable_reg = ARM_IDLECT2,
  151. .enable_bit = EN_TIMCK,
  152. .recalc = &followparent_recalc,
  153. };
  154. static struct clk armwdt_ck = {
  155. .name = "armwdt_ck",
  156. .parent = &ck_ref,
  157. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
  158. .enable_reg = ARM_IDLECT2,
  159. .enable_bit = EN_WDTCK,
  160. .recalc = &watchdog_recalc,
  161. };
  162. static struct clk arminth_ck16xx = {
  163. .name = "arminth_ck",
  164. .parent = &arm_ck,
  165. .flags = CLOCK_IN_OMAP16XX,
  166. .recalc = &followparent_recalc,
  167. /* Note: On 16xx the frequency can be divided by 2 by programming
  168. * ARM_CKCTL:ARM_INTHCK_SEL(14) to 1
  169. *
  170. * 1510 version is in TC clocks.
  171. */
  172. };
  173. static struct clk dsp_ck = {
  174. .name = "dsp_ck",
  175. .parent = &ck_dpll1,
  176. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  177. RATE_CKCTL,
  178. .enable_reg = ARM_CKCTL,
  179. .enable_bit = EN_DSPCK,
  180. .rate_offset = CKCTL_DSPDIV_OFFSET,
  181. .recalc = &ckctl_recalc,
  182. };
  183. static struct clk dspmmu_ck = {
  184. .name = "dspmmu_ck",
  185. .parent = &ck_dpll1,
  186. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  187. RATE_CKCTL | ALWAYS_ENABLED,
  188. .rate_offset = CKCTL_DSPMMUDIV_OFFSET,
  189. .recalc = &ckctl_recalc,
  190. };
  191. static struct clk tc_ck = {
  192. .name = "tc_ck",
  193. .parent = &ck_dpll1,
  194. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX | CLOCK_IN_OMAP730 |
  195. RATE_CKCTL | RATE_PROPAGATES | ALWAYS_ENABLED,
  196. .rate_offset = CKCTL_TCDIV_OFFSET,
  197. .recalc = &ckctl_recalc,
  198. };
  199. static struct clk arminth_ck1510 = {
  200. .name = "arminth_ck",
  201. .parent = &tc_ck,
  202. .flags = CLOCK_IN_OMAP1510,
  203. .recalc = &followparent_recalc,
  204. /* Note: On 1510 the frequency follows TC_CK
  205. *
  206. * 16xx version is in MPU clocks.
  207. */
  208. };
  209. static struct clk tipb_ck = {
  210. .name = "tibp_ck",
  211. .parent = &tc_ck,
  212. .flags = CLOCK_IN_OMAP1510,
  213. .recalc = &followparent_recalc,
  214. };
  215. static struct clk l3_ocpi_ck = {
  216. .name = "l3_ocpi_ck",
  217. .parent = &tc_ck,
  218. .flags = CLOCK_IN_OMAP16XX,
  219. .enable_reg = ARM_IDLECT3,
  220. .enable_bit = EN_OCPI_CK,
  221. .recalc = &followparent_recalc,
  222. };
  223. static struct clk tc1_ck = {
  224. .name = "tc1_ck",
  225. .parent = &tc_ck,
  226. .flags = CLOCK_IN_OMAP16XX,
  227. .enable_reg = ARM_IDLECT3,
  228. .enable_bit = EN_TC1_CK,
  229. .recalc = &followparent_recalc,
  230. };
  231. static struct clk tc2_ck = {
  232. .name = "tc2_ck",
  233. .parent = &tc_ck,
  234. .flags = CLOCK_IN_OMAP16XX,
  235. .enable_reg = ARM_IDLECT3,
  236. .enable_bit = EN_TC2_CK,
  237. .recalc = &followparent_recalc,
  238. };
  239. static struct clk dma_ck = {
  240. .name = "dma_ck",
  241. .parent = &tc_ck,
  242. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
  243. .recalc = &followparent_recalc,
  244. };
  245. static struct clk dma_lcdfree_ck = {
  246. .name = "dma_lcdfree_ck",
  247. .parent = &tc_ck,
  248. .flags = CLOCK_IN_OMAP16XX,
  249. .recalc = &followparent_recalc,
  250. };
  251. static struct clk api_ck = {
  252. .name = "api_ck",
  253. .parent = &tc_ck,
  254. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
  255. .enable_reg = ARM_IDLECT2,
  256. .enable_bit = EN_APICK,
  257. .recalc = &followparent_recalc,
  258. };
  259. static struct clk lb_ck = {
  260. .name = "lb_ck",
  261. .parent = &tc_ck,
  262. .flags = CLOCK_IN_OMAP1510,
  263. .enable_reg = ARM_IDLECT2,
  264. .enable_bit = EN_LBCK,
  265. .recalc = &followparent_recalc,
  266. };
  267. static struct clk rhea1_ck = {
  268. .name = "rhea1_ck",
  269. .parent = &tc_ck,
  270. .flags = CLOCK_IN_OMAP16XX,
  271. .recalc = &followparent_recalc,
  272. };
  273. static struct clk rhea2_ck = {
  274. .name = "rhea2_ck",
  275. .parent = &tc_ck,
  276. .flags = CLOCK_IN_OMAP16XX,
  277. .recalc = &followparent_recalc,
  278. };
  279. static struct clk lcd_ck = {
  280. .name = "lcd_ck",
  281. .parent = &ck_dpll1,
  282. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX | CLOCK_IN_OMAP730 |
  283. RATE_CKCTL,
  284. .enable_reg = ARM_IDLECT2,
  285. .enable_bit = EN_LCDCK,
  286. .rate_offset = CKCTL_LCDDIV_OFFSET,
  287. .recalc = &ckctl_recalc,
  288. };
  289. static struct clk uart1_ck = {
  290. .name = "uart1_ck",
  291. /* Direct from ULPD, no parent */
  292. .rate = 48000000,
  293. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  294. RATE_FIXED | ENABLE_REG_32BIT,
  295. .enable_reg = MOD_CONF_CTRL_0,
  296. .enable_bit = 29,
  297. /* (Only on 1510)
  298. * The "enable bit" actually chooses between 48MHz and 12MHz.
  299. */
  300. };
  301. static struct clk uart2_ck = {
  302. .name = "uart2_ck",
  303. /* Direct from ULPD, no parent */
  304. .rate = 48000000,
  305. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  306. RATE_FIXED | ENABLE_REG_32BIT,
  307. .enable_reg = MOD_CONF_CTRL_0,
  308. .enable_bit = 30,
  309. /* (for both 1510 and 16xx)
  310. * The "enable bit" actually chooses between 48MHz and 12MHz/32kHz.
  311. */
  312. };
  313. static struct clk uart3_ck = {
  314. .name = "uart3_ck",
  315. /* Direct from ULPD, no parent */
  316. .rate = 48000000,
  317. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  318. RATE_FIXED | ENABLE_REG_32BIT,
  319. .enable_reg = MOD_CONF_CTRL_0,
  320. .enable_bit = 31,
  321. /* (Only on 1510)
  322. * The "enable bit" actually chooses between 48MHz and 12MHz.
  323. */
  324. };
  325. static struct clk usb_clko = { /* 6 MHz output on W4_USB_CLKO */
  326. .name = "usb_clko",
  327. /* Direct from ULPD, no parent */
  328. .rate = 6000000,
  329. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  330. RATE_FIXED | ENABLE_REG_32BIT,
  331. .enable_reg = ULPD_CLOCK_CTRL,
  332. .enable_bit = USB_MCLK_EN_BIT,
  333. };
  334. static struct clk usb_hhc_ck1510 = {
  335. .name = "usb_hhc_ck",
  336. /* Direct from ULPD, no parent */
  337. .rate = 48000000, /* Actually 2 clocks, 12MHz and 48MHz */
  338. .flags = CLOCK_IN_OMAP1510 |
  339. RATE_FIXED | ENABLE_REG_32BIT,
  340. .enable_reg = MOD_CONF_CTRL_0,
  341. .enable_bit = USB_HOST_HHC_UHOST_EN,
  342. };
  343. static struct clk usb_hhc_ck16xx = {
  344. .name = "usb_hhc_ck",
  345. /* Direct from ULPD, no parent */
  346. .rate = 48000000,
  347. /* OTG_SYSCON_2.OTG_PADEN == 0 (not 1510-compatible) */
  348. .flags = CLOCK_IN_OMAP16XX |
  349. RATE_FIXED | ENABLE_REG_32BIT,
  350. .enable_reg = OTG_BASE + 0x08 /* OTG_SYSCON_2 */,
  351. .enable_bit = 8 /* UHOST_EN */,
  352. };
  353. static struct clk mclk_1510 = {
  354. .name = "mclk",
  355. /* Direct from ULPD, no parent. May be enabled by ext hardware. */
  356. .rate = 12000000,
  357. .flags = CLOCK_IN_OMAP1510 | RATE_FIXED,
  358. };
  359. static struct clk mclk_16xx = {
  360. .name = "mclk",
  361. /* Direct from ULPD, no parent. May be enabled by ext hardware. */
  362. .flags = CLOCK_IN_OMAP16XX,
  363. .enable_reg = COM_CLK_DIV_CTRL_SEL,
  364. .enable_bit = COM_ULPD_PLL_CLK_REQ,
  365. .set_rate = &set_ext_clk_rate,
  366. .round_rate = &round_ext_clk_rate,
  367. .init = &init_ext_clk,
  368. };
  369. static struct clk bclk_1510 = {
  370. .name = "bclk",
  371. /* Direct from ULPD, no parent. May be enabled by ext hardware. */
  372. .rate = 12000000,
  373. .flags = CLOCK_IN_OMAP1510 | RATE_FIXED,
  374. };
  375. static struct clk bclk_16xx = {
  376. .name = "bclk",
  377. /* Direct from ULPD, no parent. May be enabled by ext hardware. */
  378. .flags = CLOCK_IN_OMAP16XX,
  379. .enable_reg = SWD_CLK_DIV_CTRL_SEL,
  380. .enable_bit = SWD_ULPD_PLL_CLK_REQ,
  381. .set_rate = &set_ext_clk_rate,
  382. .round_rate = &round_ext_clk_rate,
  383. .init = &init_ext_clk,
  384. };
  385. static struct clk mmc1_ck = {
  386. .name = "mmc1_ck",
  387. /* Functional clock is direct from ULPD, interface clock is ARMPER */
  388. .parent = &armper_ck,
  389. .rate = 48000000,
  390. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  391. RATE_FIXED | ENABLE_REG_32BIT,
  392. .enable_reg = MOD_CONF_CTRL_0,
  393. .enable_bit = 23,
  394. };
  395. static struct clk mmc2_ck = {
  396. .name = "mmc2_ck",
  397. /* Functional clock is direct from ULPD, interface clock is ARMPER */
  398. .parent = &armper_ck,
  399. .rate = 48000000,
  400. .flags = CLOCK_IN_OMAP16XX |
  401. RATE_FIXED | ENABLE_REG_32BIT,
  402. .enable_reg = MOD_CONF_CTRL_0,
  403. .enable_bit = 20,
  404. };
  405. static struct clk virtual_ck_mpu = {
  406. .name = "mpu",
  407. .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
  408. VIRTUAL_CLOCK | ALWAYS_ENABLED,
  409. .parent = &arm_ck, /* Is smarter alias for */
  410. .recalc = &followparent_recalc,
  411. .set_rate = &select_table_rate,
  412. .round_rate = &round_to_table_rate,
  413. };
  414. static struct clk * onchip_clks[] = {
  415. /* non-ULPD clocks */
  416. &ck_ref,
  417. &ck_dpll1,
  418. /* CK_GEN1 clocks */
  419. &ck_dpll1out,
  420. &arm_ck,
  421. &armper_ck,
  422. &arm_gpio_ck,
  423. &armxor_ck,
  424. &armtim_ck,
  425. &armwdt_ck,
  426. &arminth_ck1510, &arminth_ck16xx,
  427. /* CK_GEN2 clocks */
  428. &dsp_ck,
  429. &dspmmu_ck,
  430. /* CK_GEN3 clocks */
  431. &tc_ck,
  432. &tipb_ck,
  433. &l3_ocpi_ck,
  434. &tc1_ck,
  435. &tc2_ck,
  436. &dma_ck,
  437. &dma_lcdfree_ck,
  438. &api_ck,
  439. &lb_ck,
  440. &rhea1_ck,
  441. &rhea2_ck,
  442. &lcd_ck,
  443. /* ULPD clocks */
  444. &uart1_ck,
  445. &uart2_ck,
  446. &uart3_ck,
  447. &usb_clko,
  448. &usb_hhc_ck1510, &usb_hhc_ck16xx,
  449. &mclk_1510, &mclk_16xx,
  450. &bclk_1510, &bclk_16xx,
  451. &mmc1_ck,
  452. &mmc2_ck,
  453. /* Virtual clocks */
  454. &virtual_ck_mpu,
  455. };
  456. struct clk *clk_get(struct device *dev, const char *id)
  457. {
  458. struct clk *p, *clk = ERR_PTR(-ENOENT);
  459. down(&clocks_sem);
  460. list_for_each_entry(p, &clocks, node) {
  461. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  462. clk = p;
  463. break;
  464. }
  465. }
  466. up(&clocks_sem);
  467. return clk;
  468. }
  469. EXPORT_SYMBOL(clk_get);
  470. void clk_put(struct clk *clk)
  471. {
  472. if (clk && !IS_ERR(clk))
  473. module_put(clk->owner);
  474. }
  475. EXPORT_SYMBOL(clk_put);
  476. int __clk_enable(struct clk *clk)
  477. {
  478. __u16 regval16;
  479. __u32 regval32;
  480. if (clk->flags & ALWAYS_ENABLED)
  481. return 0;
  482. if (unlikely(clk->enable_reg == 0)) {
  483. printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
  484. clk->name);
  485. return 0;
  486. }
  487. if (clk->flags & ENABLE_REG_32BIT) {
  488. regval32 = omap_readl(clk->enable_reg);
  489. regval32 |= (1 << clk->enable_bit);
  490. omap_writel(regval32, clk->enable_reg);
  491. } else {
  492. regval16 = omap_readw(clk->enable_reg);
  493. regval16 |= (1 << clk->enable_bit);
  494. omap_writew(regval16, clk->enable_reg);
  495. }
  496. return 0;
  497. }
  498. void __clk_disable(struct clk *clk)
  499. {
  500. __u16 regval16;
  501. __u32 regval32;
  502. if (clk->enable_reg == 0)
  503. return;
  504. if (clk->flags & ENABLE_REG_32BIT) {
  505. regval32 = omap_readl(clk->enable_reg);
  506. regval32 &= ~(1 << clk->enable_bit);
  507. omap_writel(regval32, clk->enable_reg);
  508. } else {
  509. regval16 = omap_readw(clk->enable_reg);
  510. regval16 &= ~(1 << clk->enable_bit);
  511. omap_writew(regval16, clk->enable_reg);
  512. }
  513. }
  514. void __clk_unuse(struct clk *clk)
  515. {
  516. if (clk->usecount > 0 && !(--clk->usecount)) {
  517. __clk_disable(clk);
  518. if (likely(clk->parent))
  519. __clk_unuse(clk->parent);
  520. }
  521. }
  522. int __clk_use(struct clk *clk)
  523. {
  524. int ret = 0;
  525. if (clk->usecount++ == 0) {
  526. if (likely(clk->parent))
  527. ret = __clk_use(clk->parent);
  528. if (unlikely(ret != 0)) {
  529. clk->usecount--;
  530. return ret;
  531. }
  532. ret = __clk_enable(clk);
  533. if (unlikely(ret != 0) && clk->parent) {
  534. __clk_unuse(clk->parent);
  535. clk->usecount--;
  536. }
  537. }
  538. return ret;
  539. }
  540. int clk_enable(struct clk *clk)
  541. {
  542. unsigned long flags;
  543. int ret;
  544. spin_lock_irqsave(&clockfw_lock, flags);
  545. ret = __clk_enable(clk);
  546. spin_unlock_irqrestore(&clockfw_lock, flags);
  547. return ret;
  548. }
  549. EXPORT_SYMBOL(clk_enable);
  550. void clk_disable(struct clk *clk)
  551. {
  552. unsigned long flags;
  553. spin_lock_irqsave(&clockfw_lock, flags);
  554. __clk_disable(clk);
  555. spin_unlock_irqrestore(&clockfw_lock, flags);
  556. }
  557. EXPORT_SYMBOL(clk_disable);
  558. int clk_use(struct clk *clk)
  559. {
  560. unsigned long flags;
  561. int ret = 0;
  562. spin_lock_irqsave(&clockfw_lock, flags);
  563. ret = __clk_use(clk);
  564. spin_unlock_irqrestore(&clockfw_lock, flags);
  565. return ret;
  566. }
  567. EXPORT_SYMBOL(clk_use);
  568. void clk_unuse(struct clk *clk)
  569. {
  570. unsigned long flags;
  571. spin_lock_irqsave(&clockfw_lock, flags);
  572. __clk_unuse(clk);
  573. spin_unlock_irqrestore(&clockfw_lock, flags);
  574. }
  575. EXPORT_SYMBOL(clk_unuse);
  576. int clk_get_usecount(struct clk *clk)
  577. {
  578. return clk->usecount;
  579. }
  580. EXPORT_SYMBOL(clk_get_usecount);
  581. unsigned long clk_get_rate(struct clk *clk)
  582. {
  583. return clk->rate;
  584. }
  585. EXPORT_SYMBOL(clk_get_rate);
  586. static __u16 verify_ckctl_value(__u16 newval)
  587. {
  588. /* This function checks for following limitations set
  589. * by the hardware (all conditions must be true):
  590. * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
  591. * ARM_CK >= TC_CK
  592. * DSP_CK >= TC_CK
  593. * DSPMMU_CK >= TC_CK
  594. *
  595. * In addition following rules are enforced:
  596. * LCD_CK <= TC_CK
  597. * ARMPER_CK <= TC_CK
  598. *
  599. * However, maximum frequencies are not checked for!
  600. */
  601. __u8 per_exp;
  602. __u8 lcd_exp;
  603. __u8 arm_exp;
  604. __u8 dsp_exp;
  605. __u8 tc_exp;
  606. __u8 dspmmu_exp;
  607. per_exp = (newval >> CKCTL_PERDIV_OFFSET) & 3;
  608. lcd_exp = (newval >> CKCTL_LCDDIV_OFFSET) & 3;
  609. arm_exp = (newval >> CKCTL_ARMDIV_OFFSET) & 3;
  610. dsp_exp = (newval >> CKCTL_DSPDIV_OFFSET) & 3;
  611. tc_exp = (newval >> CKCTL_TCDIV_OFFSET) & 3;
  612. dspmmu_exp = (newval >> CKCTL_DSPMMUDIV_OFFSET) & 3;
  613. if (dspmmu_exp < dsp_exp)
  614. dspmmu_exp = dsp_exp;
  615. if (dspmmu_exp > dsp_exp+1)
  616. dspmmu_exp = dsp_exp+1;
  617. if (tc_exp < arm_exp)
  618. tc_exp = arm_exp;
  619. if (tc_exp < dspmmu_exp)
  620. tc_exp = dspmmu_exp;
  621. if (tc_exp > lcd_exp)
  622. lcd_exp = tc_exp;
  623. if (tc_exp > per_exp)
  624. per_exp = tc_exp;
  625. newval &= 0xf000;
  626. newval |= per_exp << CKCTL_PERDIV_OFFSET;
  627. newval |= lcd_exp << CKCTL_LCDDIV_OFFSET;
  628. newval |= arm_exp << CKCTL_ARMDIV_OFFSET;
  629. newval |= dsp_exp << CKCTL_DSPDIV_OFFSET;
  630. newval |= tc_exp << CKCTL_TCDIV_OFFSET;
  631. newval |= dspmmu_exp << CKCTL_DSPMMUDIV_OFFSET;
  632. return newval;
  633. }
  634. static int calc_dsor_exp(struct clk *clk, unsigned long rate)
  635. {
  636. /* Note: If target frequency is too low, this function will return 4,
  637. * which is invalid value. Caller must check for this value and act
  638. * accordingly.
  639. *
  640. * Note: This function does not check for following limitations set
  641. * by the hardware (all conditions must be true):
  642. * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
  643. * ARM_CK >= TC_CK
  644. * DSP_CK >= TC_CK
  645. * DSPMMU_CK >= TC_CK
  646. */
  647. unsigned long realrate;
  648. struct clk * parent;
  649. unsigned dsor_exp;
  650. if (unlikely(!(clk->flags & RATE_CKCTL)))
  651. return -EINVAL;
  652. parent = clk->parent;
  653. if (unlikely(parent == 0))
  654. return -EIO;
  655. realrate = parent->rate;
  656. for (dsor_exp=0; dsor_exp<4; dsor_exp++) {
  657. if (realrate <= rate)
  658. break;
  659. realrate /= 2;
  660. }
  661. return dsor_exp;
  662. }
  663. long clk_round_rate(struct clk *clk, unsigned long rate)
  664. {
  665. int dsor_exp;
  666. if (clk->flags & RATE_FIXED)
  667. return clk->rate;
  668. if (clk->flags & RATE_CKCTL) {
  669. dsor_exp = calc_dsor_exp(clk, rate);
  670. if (dsor_exp < 0)
  671. return dsor_exp;
  672. if (dsor_exp > 3)
  673. dsor_exp = 3;
  674. return clk->parent->rate / (1 << dsor_exp);
  675. }
  676. if(clk->round_rate != 0)
  677. return clk->round_rate(clk, rate);
  678. return clk->rate;
  679. }
  680. EXPORT_SYMBOL(clk_round_rate);
  681. static void propagate_rate(struct clk * clk)
  682. {
  683. struct clk ** clkp;
  684. for (clkp = onchip_clks; clkp < onchip_clks+ARRAY_SIZE(onchip_clks); clkp++) {
  685. if (likely((*clkp)->parent != clk)) continue;
  686. if (likely((*clkp)->recalc))
  687. (*clkp)->recalc(*clkp);
  688. }
  689. }
  690. static int select_table_rate(struct clk * clk, unsigned long rate)
  691. {
  692. /* Find the highest supported frequency <= rate and switch to it */
  693. struct mpu_rate * ptr;
  694. if (clk != &virtual_ck_mpu)
  695. return -EINVAL;
  696. for (ptr = rate_table; ptr->rate; ptr++) {
  697. if (ptr->xtal != ck_ref.rate)
  698. continue;
  699. /* DPLL1 cannot be reprogrammed without risking system crash */
  700. if (likely(ck_dpll1.rate!=0) && ptr->pll_rate != ck_dpll1.rate)
  701. continue;
  702. /* Can check only after xtal frequency check */
  703. if (ptr->rate <= rate)
  704. break;
  705. }
  706. if (!ptr->rate)
  707. return -EINVAL;
  708. if (unlikely(ck_dpll1.rate == 0)) {
  709. omap_writew(ptr->dpllctl_val, DPLL_CTL);
  710. ck_dpll1.rate = ptr->pll_rate;
  711. }
  712. omap_writew(ptr->ckctl_val, ARM_CKCTL);
  713. propagate_rate(&ck_dpll1);
  714. return 0;
  715. }
  716. static long round_to_table_rate(struct clk * clk, unsigned long rate)
  717. {
  718. /* Find the highest supported frequency <= rate */
  719. struct mpu_rate * ptr;
  720. long highest_rate;
  721. if (clk != &virtual_ck_mpu)
  722. return -EINVAL;
  723. highest_rate = -EINVAL;
  724. for (ptr = rate_table; ptr->rate; ptr++) {
  725. if (ptr->xtal != ck_ref.rate)
  726. continue;
  727. highest_rate = ptr->rate;
  728. /* Can check only after xtal frequency check */
  729. if (ptr->rate <= rate)
  730. break;
  731. }
  732. return highest_rate;
  733. }
  734. int clk_set_rate(struct clk *clk, unsigned long rate)
  735. {
  736. int ret = -EINVAL;
  737. int dsor_exp;
  738. __u16 regval;
  739. unsigned long flags;
  740. if (clk->flags & RATE_CKCTL) {
  741. dsor_exp = calc_dsor_exp(clk, rate);
  742. if (dsor_exp > 3)
  743. dsor_exp = -EINVAL;
  744. if (dsor_exp < 0)
  745. return dsor_exp;
  746. spin_lock_irqsave(&clockfw_lock, flags);
  747. regval = omap_readw(ARM_CKCTL);
  748. regval &= ~(3 << clk->rate_offset);
  749. regval |= dsor_exp << clk->rate_offset;
  750. regval = verify_ckctl_value(regval);
  751. omap_writew(regval, ARM_CKCTL);
  752. clk->rate = clk->parent->rate / (1 << dsor_exp);
  753. spin_unlock_irqrestore(&clockfw_lock, flags);
  754. ret = 0;
  755. } else if(clk->set_rate != 0) {
  756. spin_lock_irqsave(&clockfw_lock, flags);
  757. ret = clk->set_rate(clk, rate);
  758. spin_unlock_irqrestore(&clockfw_lock, flags);
  759. }
  760. if (unlikely(ret == 0 && (clk->flags & RATE_PROPAGATES)))
  761. propagate_rate(clk);
  762. return ret;
  763. }
  764. EXPORT_SYMBOL(clk_set_rate);
  765. static unsigned calc_ext_dsor(unsigned long rate)
  766. {
  767. unsigned dsor;
  768. /* MCLK and BCLK divisor selection is not linear:
  769. * freq = 96MHz / dsor
  770. *
  771. * RATIO_SEL range: dsor <-> RATIO_SEL
  772. * 0..6: (RATIO_SEL+2) <-> (dsor-2)
  773. * 6..48: (8+(RATIO_SEL-6)*2) <-> ((dsor-8)/2+6)
  774. * Minimum dsor is 2 and maximum is 96. Odd divisors starting from 9
  775. * can not be used.
  776. */
  777. for (dsor = 2; dsor < 96; ++dsor) {
  778. if ((dsor & 1) && dsor > 8)
  779. continue;
  780. if (rate >= 96000000 / dsor)
  781. break;
  782. }
  783. return dsor;
  784. }
  785. static int set_ext_clk_rate(struct clk * clk, unsigned long rate)
  786. {
  787. unsigned dsor;
  788. __u16 ratio_bits;
  789. dsor = calc_ext_dsor(rate);
  790. clk->rate = 96000000 / dsor;
  791. if (dsor > 8)
  792. ratio_bits = ((dsor - 8) / 2 + 6) << 2;
  793. else
  794. ratio_bits = (dsor - 2) << 2;
  795. ratio_bits |= omap_readw(clk->enable_reg) & ~0xfd;
  796. omap_writew(ratio_bits, clk->enable_reg);
  797. return 0;
  798. }
  799. static long round_ext_clk_rate(struct clk * clk, unsigned long rate)
  800. {
  801. return 96000000 / calc_ext_dsor(rate);
  802. }
  803. static void init_ext_clk(struct clk * clk)
  804. {
  805. unsigned dsor;
  806. __u16 ratio_bits;
  807. /* Determine current rate and ensure clock is based on 96MHz APLL */
  808. ratio_bits = omap_readw(clk->enable_reg) & ~1;
  809. omap_writew(ratio_bits, clk->enable_reg);
  810. ratio_bits = (ratio_bits & 0xfc) >> 2;
  811. if (ratio_bits > 6)
  812. dsor = (ratio_bits - 6) * 2 + 8;
  813. else
  814. dsor = ratio_bits + 2;
  815. clk-> rate = 96000000 / dsor;
  816. }
  817. int clk_register(struct clk *clk)
  818. {
  819. down(&clocks_sem);
  820. list_add(&clk->node, &clocks);
  821. if (clk->init)
  822. clk->init(clk);
  823. up(&clocks_sem);
  824. return 0;
  825. }
  826. EXPORT_SYMBOL(clk_register);
  827. void clk_unregister(struct clk *clk)
  828. {
  829. down(&clocks_sem);
  830. list_del(&clk->node);
  831. up(&clocks_sem);
  832. }
  833. EXPORT_SYMBOL(clk_unregister);
  834. int __init clk_init(void)
  835. {
  836. struct clk ** clkp;
  837. const struct omap_clock_config *info;
  838. int crystal_type = 0; /* Default 12 MHz */
  839. for (clkp = onchip_clks; clkp < onchip_clks+ARRAY_SIZE(onchip_clks); clkp++) {
  840. if (((*clkp)->flags &CLOCK_IN_OMAP1510) && cpu_is_omap1510()) {
  841. clk_register(*clkp);
  842. continue;
  843. }
  844. if (((*clkp)->flags &CLOCK_IN_OMAP16XX) && cpu_is_omap16xx()) {
  845. clk_register(*clkp);
  846. continue;
  847. }
  848. if (((*clkp)->flags &CLOCK_IN_OMAP730) && cpu_is_omap730()) {
  849. clk_register(*clkp);
  850. continue;
  851. }
  852. }
  853. info = omap_get_config(OMAP_TAG_CLOCK, struct omap_clock_config);
  854. if (info != NULL) {
  855. if (!cpu_is_omap1510())
  856. crystal_type = info->system_clock_type;
  857. }
  858. #if defined(CONFIG_ARCH_OMAP730)
  859. ck_ref.rate = 13000000;
  860. #elif defined(CONFIG_ARCH_OMAP16XX)
  861. if (crystal_type == 2)
  862. ck_ref.rate = 19200000;
  863. #endif
  864. /* We want to be in syncronous scalable mode */
  865. omap_writew(0x1000, ARM_SYSST);
  866. /* Find the highest supported frequency and enable it */
  867. if (select_table_rate(&virtual_ck_mpu, ~0)) {
  868. printk(KERN_ERR "System frequencies not set. Check your config.\n");
  869. /* Guess sane values (60MHz) */
  870. omap_writew(0x2290, DPLL_CTL);
  871. omap_writew(0x1005, ARM_CKCTL);
  872. ck_dpll1.rate = 60000000;
  873. propagate_rate(&ck_dpll1);
  874. printk(KERN_INFO "Clocking rate (xtal/DPLL1/MPU): %ld/%ld/%ld\n",
  875. ck_ref.rate, ck_dpll1.rate, arm_ck.rate);
  876. }
  877. /* Cache rates for clocks connected to ck_ref (not dpll1) */
  878. propagate_rate(&ck_ref);
  879. #ifdef CONFIG_MACH_OMAP_PERSEUS2
  880. /* Select slicer output as OMAP input clock */
  881. omap_writew(omap_readw(OMAP730_PCC_UPLD_CTRL) & ~0x1, OMAP730_PCC_UPLD_CTRL);
  882. #endif
  883. /* Turn off DSP and ARM_TIMXO. Make sure ARM_INTHCK is not divided */
  884. omap_writew(omap_readw(ARM_CKCTL) & 0x0fff, ARM_CKCTL);
  885. /* Put DSP/MPUI into reset until needed */
  886. omap_writew(0, ARM_RSTCT1);
  887. omap_writew(1, ARM_RSTCT2);
  888. omap_writew(0x400, ARM_IDLECT1);
  889. /*
  890. * According to OMAP5910 Erratum SYS_DMA_1, bit DMACK_REQ (bit 8)
  891. * of the ARM_IDLECT2 register must be set to zero. The power-on
  892. * default value of this bit is one.
  893. */
  894. omap_writew(0x0000, ARM_IDLECT2); /* Turn LCD clock off also */
  895. /*
  896. * Only enable those clocks we will need, let the drivers
  897. * enable other clocks as necessary
  898. */
  899. clk_use(&armper_ck);
  900. clk_use(&armxor_ck);
  901. clk_use(&armtim_ck);
  902. if (cpu_is_omap1510())
  903. clk_enable(&arm_gpio_ck);
  904. return 0;
  905. }