clock.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  1. /*
  2. * arch/arm/mach-meson6/clock.c
  3. *
  4. * Copyright (C) 2011-2012 Amlogic, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. //#define DEBUG
  21. //#define CONFIG_CPU_FREQ_DEBUG 1
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/clk.h>
  25. #include <linux/init.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/delay.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/device.h>
  30. #include <linux/err.h>
  31. #include <linux/cpu.h>
  32. #include <linux/clkdev.h>
  33. #include <plat/io.h>
  34. #include <mach/clock.h>
  35. #include <mach/hardware.h>
  36. #include <mach/clk_set.h>
  37. #include <mach/power_gate.h>
  38. #include <plat/regops.h>
  39. #include <plat/cpufreq.h>
  40. #include <linux/printk.h>
  41. static DEFINE_SPINLOCK(mali_clk_lock);
  42. static DEFINE_SPINLOCK(clockfw_lock);
  43. static DEFINE_MUTEX(clock_ops_lock);
  44. static unsigned int mali_max = 333000;
  45. static unsigned int freq_limit = 1;
  46. static int set_sys_pll(struct clk *clk, unsigned long src, unsigned long dst, unsigned * scale_divn);
  47. #define IS_CLK_ERR(a) (IS_ERR(a) || a == 0)
  48. #if 0
  49. #ifdef CONFIG_INIT_A9_CLOCK_FREQ
  50. static unsigned long __initdata init_clock = CONFIG_INIT_A9_CLOCK;
  51. #else
  52. static unsigned long __initdata init_clock = 0;
  53. #endif
  54. #endif
  55. static unsigned int gpu_to_min_cpu(unsigned int gpu);
  56. static int _clk_set_rate_gpu(struct clk *clk, unsigned long gpu, unsigned long cpu);
  57. static unsigned long clk_get_rate_a9(struct clk * clkdev);
  58. #define CONFIG_MALI_MINIMUM_FREQ 0
  59. // -----------------------------------------
  60. // clk_util_clk_msr
  61. // -----------------------------------------
  62. // from twister_core.v
  63. /** .clk_to_msr_in ( { 13'h0, // [63:50]
  64. cts_mipi_phy_clk,
  65. am_ring_osc_out_mali[1], // [49]
  66. am_ring_osc_out_mali[0], // [48]
  67. am_ring_osc_out_a9[1], // [47]
  68. am_ring_osc_out_a9[0], // [46]
  69. cts_pwm_A_clk, // [45]
  70. cts_pwm_B_clk, // [44]
  71. cts_pwm_C_clk, // [43]
  72. cts_pwm_D_clk, // [42]
  73. cts_eth_tx_clk, // [41]
  74. cts_pcm_mclk, // [40]
  75. cts_pcm_sclk, // [39]
  76. cts_vdin_meas_clk, // [38]
  77. cts_vdac_clk[1], // [37]
  78. cts_hdmi_tx_pixel_clk, // [36]
  79. cts_mali_clk, // [35]
  80. cts_sdhc_clk1, // [34]
  81. cts_sdhc_clk0, // [33]
  82. cts_vdec_clk, // [32]
  83. 1'b0, // [31]
  84. cts_slow_ddr_clk, // [30]
  85. cts_vdac_clk[0], // [29]
  86. cts_sar_adc_clk, // [28]
  87. cts_enci_clk, // [27]
  88. sc_clk_int, // [26]
  89. sys_pll_div3, // [25]
  90. lvds_fifo_clk, // [24]
  91. HDMI_CH0_TMDSCLK, // [23]
  92. clk_rmii_from_pad, // [22]
  93. mod_audin_aoclk_i, // [21]
  94. rtc_osc_clk_out, // [20]
  95. cts_hdmi_sys_clk, // [19]
  96. cts_led_pll_clk, // [18]
  97. cts_vghl_pll_clk, // [17]
  98. cts_FEC_CLK_2, // [16]
  99. cts_FEC_CLK_1, // [15]
  100. cts_FEC_CLK_0, // [14]
  101. cts_amclk, // [13]
  102. vid2_pll_clk, // [12]
  103. cts_eth_rmii, // [11]
  104. cts_enct_clk, // [10]
  105. cts_encl_clk, // [9]
  106. cts_encp_clk, // [8]
  107. clk81, // [7]
  108. vid_pll_clk, // [6]
  109. usb1_clk_12mhz, // [5]
  110. usb0_clk_12mhz, // [4]
  111. ddr_pll_clk, // [3]
  112. 1'b0, // [2]
  113. am_ring_osc_clk_out_ee[1], // [1]
  114. am_ring_osc_clk_out_ee[0]} ), // [0]
  115. */
  116. //
  117. // For Example
  118. //
  119. // unsigend long clk81_clk = clk_util_clk_msr( 2, // mux select 2
  120. // 50 ); // measure for 50uS
  121. //
  122. // returns a value in "clk81_clk" in Hz
  123. //
  124. // The "uS_gate_time" can be anything between 1uS and 65535 uS, but the limitation is
  125. // the circuit will only count 65536 clocks. Therefore the uS_gate_time is limited by
  126. //
  127. // uS_gate_time <= 65535/(expect clock frequency in MHz)
  128. //
  129. // For example, if the expected frequency is 400Mhz, then the uS_gate_time should
  130. // be less than 163.
  131. //
  132. // Your measurement resolution is:
  133. //
  134. // 100% / (uS_gate_time * measure_val )
  135. //
  136. //
  137. // #define MSR_CLK_DUTY 0x21d6
  138. // #define MSR_CLK_REG0 0x21d7
  139. // #define MSR_CLK_REG1 0x21d8
  140. // #define MSR_CLK_REG2 0x21d9
  141. //
  142. /**
  143. unsigned long clk_util_clk_msr( unsigned long clk_mux, unsigned long uS_gate_time )
  144. {
  145. // Set the measurement gate to 100uS
  146. Wr(MSR_CLK_REG0, (Rd(MSR_CLK_REG0) & ~(0xFFFF << 0)) | ((uS_gate_time-1) << 0) );
  147. // Disable continuous measurement
  148. // disable interrupts
  149. Wr(MSR_CLK_REG0, (Rd(MSR_CLK_REG0) & ~((1 << 18) | (1 << 17))) );
  150. Wr(MSR_CLK_REG0, (Rd(MSR_CLK_REG0) & ~(0x1f << 20)) | ((clk_mux << 20) | // Select MUX
  151. (1 << 19) | // enable the clock
  152. (1 << 16)) ); // enable measuring
  153. // Delay
  154. Rd(MSR_CLK_REG0);
  155. // Wait for the measurement to be done
  156. while( (Rd(MSR_CLK_REG0) & (1 << 31)) ) {}
  157. // disable measuring
  158. Wr(MSR_CLK_REG0, (Rd(MSR_CLK_REG0) & ~(1 << 16)) | (0 << 16) );
  159. unsigned long measured_val = (Rd(MSR_CLK_REG2) & 0x000FFFFF); // only 20 bits
  160. if( measured_val == 0xFFFFF ) { // 20 bits max
  161. return(0);
  162. } else {
  163. // Return value in Hz
  164. return(measured_val*(1000000/uS_gate_time));
  165. }
  166. }
  167. **/
  168. #ifndef CONFIG_CLK_MSR_NEW
  169. static unsigned int clk_util_clk_msr(unsigned int clk_mux)
  170. {
  171. unsigned int msr;
  172. unsigned int regval = 0;
  173. aml_write_reg32(P_MSR_CLK_REG0,0);
  174. // Set the measurement gate to 64uS
  175. clrsetbits_le32(P_MSR_CLK_REG0,0xffff,64-1);
  176. // Disable continuous measurement
  177. // disable interrupts
  178. clrbits_le32(P_MSR_CLK_REG0,((1 << 18) | (1 << 17)));
  179. clrsetbits_le32(P_MSR_CLK_REG0,(0x1f<<20),(clk_mux<<20)|(1<<19)|(1<<16));
  180. aml_read_reg32(P_MSR_CLK_REG0);
  181. // Wait for the measurement to be done
  182. do {
  183. regval = aml_read_reg32(P_MSR_CLK_REG0);
  184. } while (regval & (1 << 31));
  185. // disable measuring
  186. clrbits_le32(P_MSR_CLK_REG0,(1 << 16));
  187. msr=(aml_read_reg32(P_MSR_CLK_REG2)+31)&0x000FFFFF;
  188. // Return value in MHz*measured_val
  189. return (msr>>6)*1000000;
  190. /*
  191. unsigned int regval = 0;
  192. /// Set the measurement gate to 64uS
  193. clrsetbits_le32(P_MSR_CLK_REG0,0xffff,63);
  194. // Disable continuous measurement
  195. // disable interrupts
  196. clrsetbits_le32(P_MSR_CLK_REG0,
  197. ((1 << 18) | (1 << 17)|(0x1f << 20)),///clrbits
  198. (clk_mux << 20) | /// Select MUX
  199. (1 << 19) | /// enable the clock
  200. (1 << 16));
  201. // Wait for the measurement to be done
  202. regval = aml_read_reg32(P_MSR_CLK_REG0);
  203. do {
  204. regval = aml_read_reg32(P_MSR_CLK_REG0);
  205. } while (regval & (1 << 31));
  206. // disable measuring
  207. clrbits_le32(P_MSR_CLK_REG0, (1 << 16));
  208. regval = (aml_read_reg32(P_MSR_CLK_REG2) + 31) & 0x000FFFFF;
  209. // Return value in MHz*measured_val
  210. return (regval >> 6)*1000000;
  211. */
  212. }
  213. #else
  214. static unsigned int clk_util_clk_msr(unsigned int clk_mux)
  215. {
  216. unsigned int regval = 0;
  217. /// Set the measurement gate to 64uS
  218. clrsetbits_le32(P_MSR_CLK_REG0,0xffff,121);///122us
  219. // Disable continuous measurement
  220. // disable interrupts
  221. clrsetbits_le32(P_MSR_CLK_REG0,
  222. ((1 << 18) | (1 << 17)|(0x1f << 20)),///clrbits
  223. (clk_mux << 20) | /// Select MUX
  224. (1 << 19) | /// enable the clock
  225. (1 << 16));
  226. // Wait for the measurement to be done
  227. regval = aml_read_reg32(P_MSR_CLK_REG0);
  228. do {
  229. regval = aml_read_reg32(P_MSR_CLK_REG0);
  230. } while (regval & (1 << 31));
  231. // disable measuring
  232. clrbits_le32(P_MSR_CLK_REG0, (1 << 16));
  233. regval = (aml_read_reg32(P_MSR_CLK_REG2)) & 0x000FFFFF;
  234. regval += (regval/10000) * 6;
  235. // Return value in MHz*measured_val
  236. return (regval << 13);
  237. }
  238. #endif
  239. unsigned long mali_clock_gating_lock(void)
  240. {
  241. unsigned long flags;
  242. spin_lock_irqsave(&mali_clk_lock, flags);
  243. return flags;
  244. }
  245. EXPORT_SYMBOL(mali_clock_gating_lock);
  246. void mali_clock_gating_unlock(unsigned long flags)
  247. {
  248. spin_unlock_irqrestore(&mali_clk_lock, flags);
  249. }
  250. EXPORT_SYMBOL(mali_clock_gating_unlock);
  251. int clk_measure(char index )
  252. {
  253. const char* clk_table[]={
  254. " CTS_MIPI_PHY_CLK(50)",
  255. " AM_RING_OSC_OUT_MALI[1](49)",
  256. " AM_RING_OSC_OUT_MALI[0](48)",
  257. " AM_RING_OSC_OUT_A9[1](47)",
  258. " AM_RING_OSC_OUT_A9[0](46)",
  259. " CTS_PWM_A_CLK(45)",
  260. " CTS_PWM_B_CLK(44)",
  261. " CTS_PWM_C_CLK(43)",
  262. " CTS_PWM_D_CLK(42)",
  263. " CTS_ETH_TX(41)",
  264. " CTS_PCM_MCLK(40)",
  265. " CTS_PCM_SCLK(39)",
  266. " CTS_VDIN_MEAS_CLK(38)",
  267. " CTS_VDAC_CLK1(37)",
  268. " CTS_HDMI_TX_PIXEL_CLK(36)",
  269. " CTS_MALI_CLK (35)",
  270. " CTS_SDHC_CLK1(34)",
  271. " CTS_SDHC_CLK0(33)",
  272. " CTS_VDAC_CLK(32)",
  273. " Reserved(31)",
  274. " CTS_SLOW_DDR_CLK(30)",
  275. " CTS_VDAC_CLK0(29)",
  276. " CTS_SAR_ADC_CLK(28)",
  277. " CTS_ENCI_CL(27)",
  278. " SC_CLK_INT(26)",
  279. " SYS_PLL_DIV3(25)",
  280. " LVDS_FIFO_CLK(24)",
  281. " HDMI_CH0_TMDSCLK(23)",
  282. " CLK_RMII_FROM_PAD (22)",
  283. " MOD_AUDIN_AMCLK_I(21)",
  284. " RTC_OSC_CLK_OUT (20)",
  285. " CTS_HDMI_SYS_CLK(19)",
  286. " CTS_LED_PLL_CLK(18)",
  287. " CTS_VGHL_PLL_CLK (17)",
  288. " CTS_FEC_CLK_2(16)",
  289. " CTS_FEC_CLK_1 (15)",
  290. " CTS_FEC_CLK_0 (14)",
  291. " CTS_AMCLK(13)",
  292. " VID2_PLL_CLK(12)",
  293. " CTS_ETH_RMII(11)",
  294. " CTS_ENCT_CLK(10)",
  295. " CTS_ENCL_CLK(9)",
  296. " CTS_ENCP_CLK(8)",
  297. " CLK81 (7)",
  298. " VID_PLL_CLK(6)",
  299. " USB1_CLK_12MHZ (5)",
  300. " USB0_CLK_12MHZ (4)",
  301. " DDR_PLL_CLK(3)",
  302. " Reserved(2)",
  303. " AM_RING_OSC_CLK_OUT1(1)",
  304. " AM_RING_OSC_CLK_OUT0(0)",
  305. };
  306. int i;
  307. int len = sizeof(clk_table)/sizeof(char*) - 1;
  308. if (index == 0xff)
  309. {
  310. for(i = 0;i < len;i++)
  311. {
  312. printk("[%10d]%s\n",clk_util_clk_msr(i),clk_table[len-i]);
  313. }
  314. return 0;
  315. }
  316. printk("[%10d]%s\n",clk_util_clk_msr(index),clk_table[len-index]);
  317. return 0;
  318. }
  319. long clk_round_rate(struct clk *clk, unsigned long rate)
  320. {
  321. if (clk == NULL || IS_ERR(clk))
  322. return -EINVAL;
  323. if (rate < clk->min)
  324. return clk->min;
  325. if (rate > clk->max)
  326. return clk->max;
  327. return rate;
  328. }
  329. EXPORT_SYMBOL(clk_round_rate);
  330. unsigned long clk_get_rate(struct clk *clk)
  331. {
  332. if (clk == NULL || IS_ERR(clk))
  333. return -EINVAL;
  334. if (clk->get_rate)
  335. return clk->get_rate(clk);
  336. else
  337. return clk->rate;
  338. }
  339. EXPORT_SYMBOL(clk_get_rate);
  340. int on_parent_changed(struct clk *clk, int rate, int before,int failed)
  341. {
  342. struct clk_ops* pops = clk->clk_ops;
  343. while(pops){
  344. if(before == 1){
  345. if(pops->clk_ratechange_before)
  346. pops->clk_ratechange_before(rate,pops->privdata);
  347. }
  348. else{
  349. if(pops->clk_ratechange_after)
  350. pops->clk_ratechange_after(rate,pops->privdata,failed);
  351. }
  352. pops = pops->next;
  353. }
  354. return 0;
  355. }
  356. int meson_notify_childs_changed(struct clk *clk,int before,int failed)
  357. {
  358. struct clk* p;
  359. if(IS_CLK_ERR(clk))
  360. return 0;
  361. p = (struct clk*)(clk->child.next);
  362. if (p) {
  363. unsigned long flags;
  364. int rate = clk_get_rate(p);
  365. spin_lock_irqsave(&clockfw_lock, flags);
  366. on_parent_changed(p,rate,before,failed);
  367. spin_unlock_irqrestore(&clockfw_lock, flags);
  368. meson_notify_childs_changed(p,before,failed);
  369. p = (struct clk*)p->sibling.next;
  370. while(p){
  371. spin_lock_irqsave(&clockfw_lock, flags);
  372. on_parent_changed(p,rate,before,failed);
  373. spin_unlock_irqrestore(&clockfw_lock, flags);
  374. meson_notify_childs_changed(p,before,failed);
  375. p = (struct clk*)p->sibling.next;
  376. }
  377. }
  378. return 0;
  379. }
  380. //flow. self -> child -> child slibling
  381. int meson_clk_set_rate(struct clk *clk, unsigned long rate)
  382. {
  383. unsigned long flags;
  384. int ret;
  385. int ops_run_count;
  386. struct clk_ops *p;
  387. if(clk->set_rate == NULL || IS_CLK_ERR(clk))
  388. return 0;
  389. //post message before clk change.
  390. {
  391. ret = 0;
  392. ops_run_count = 0;
  393. p = clk->clk_ops;
  394. while(p){
  395. ops_run_count++;
  396. if(p->clk_ratechange_before)
  397. ret = p->clk_ratechange_before(rate, p->privdata);
  398. if(ret != 0)
  399. break;
  400. p = p->next;
  401. }
  402. meson_notify_childs_changed(clk,1,ret);
  403. }
  404. if(ret == 0){
  405. if (!clk->open_irq)
  406. spin_lock_irqsave(&clockfw_lock, flags);
  407. //printk(KERN_INFO "%s() clk=%p rate=%lu\n", __FUNCTION__, clk, rate);
  408. if(clk->set_rate)
  409. ret = clk->set_rate(clk, rate) ;
  410. if (!clk->open_irq)
  411. spin_unlock_irqrestore(&clockfw_lock, flags);
  412. }
  413. //post message after clk change.
  414. {
  415. int idx = 0;
  416. p = clk->clk_ops;
  417. while(p){
  418. idx++;
  419. if(idx > ops_run_count)
  420. break;
  421. if(p->clk_ratechange_after)
  422. p->clk_ratechange_after(rate, p->privdata,ret);
  423. p = p->next;
  424. }
  425. }
  426. meson_notify_childs_changed(clk,0,ret);
  427. return ret;
  428. }
  429. int clk_set_rate(struct clk *clk, unsigned long rate)
  430. {
  431. int ret=0;
  432. int parent_rate = 0;
  433. if(IS_CLK_ERR(clk))
  434. return 0;
  435. if(clk_get_rate(clk) == rate){
  436. return 0;
  437. }
  438. if(clk->need_parent_changed){
  439. unsigned long flags;
  440. spin_lock_irqsave(&clockfw_lock, flags);
  441. parent_rate = clk->need_parent_changed(clk, rate);
  442. spin_unlock_irqrestore(&clockfw_lock, flags);
  443. }
  444. if(parent_rate != 0)
  445. clk_set_rate(clk->parent,parent_rate);
  446. else{
  447. mutex_lock(&clock_ops_lock);
  448. //printk(KERN_INFO "%s() clk=%p rate=%lu\n", __FUNCTION__, clk, rate);
  449. ret = meson_clk_set_rate(clk,rate);
  450. mutex_unlock(&clock_ops_lock);
  451. }
  452. return ret;
  453. }
  454. EXPORT_SYMBOL(clk_set_rate);
  455. unsigned long long clkparse(const char *ptr, char **retptr)
  456. {
  457. char *endptr; /* local pointer to end of parsed string */
  458. unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
  459. switch (*endptr) {
  460. case 'G':
  461. case 'g':
  462. ret *= 1000;
  463. case 'M':
  464. case 'm':
  465. ret *= 1000;
  466. case 'K':
  467. case 'k':
  468. ret *= 1000;
  469. endptr++;
  470. default:
  471. break;
  472. }
  473. if (retptr) {
  474. *retptr = endptr;
  475. }
  476. return ret;
  477. }
  478. int meson_enable(struct clk *clk)
  479. {
  480. if (IS_CLK_ERR(clk))
  481. return 0;
  482. if (clk_get_status(clk) == 1)
  483. return 0;
  484. if (meson_enable(clk->parent) == 0) {
  485. struct clk_ops *p;
  486. int idx;
  487. int ops_run_count = 0;
  488. int ret = 0;
  489. p = clk->clk_ops;
  490. while(p){
  491. ops_run_count++;
  492. if(p->clk_enable_before)
  493. ret = p->clk_enable_before(p->privdata);
  494. if(ret == 1)
  495. break;
  496. p = p->next;
  497. }
  498. if(ret == 0){
  499. if(clk->enable)
  500. ret = clk->enable(clk);
  501. else if(clk->clk_gate_reg_adr != 0)
  502. aml_set_reg32_mask(clk->clk_gate_reg_adr,clk->clk_gate_reg_mask);
  503. ret = 0;
  504. }
  505. p = clk->clk_ops;
  506. idx = 0;
  507. while(p){
  508. idx++;
  509. if(idx > ops_run_count)
  510. break;
  511. if(p->clk_enable_after)
  512. p->clk_enable_after(p->privdata,ret);
  513. p = p->next;
  514. }
  515. return ret;
  516. }
  517. else
  518. return 1;
  519. }
  520. int clk_enable(struct clk *clk)
  521. {
  522. int ret;
  523. mutex_lock(&clock_ops_lock);
  524. ret = meson_enable(clk);
  525. mutex_unlock(&clock_ops_lock);
  526. return ret;
  527. }
  528. EXPORT_SYMBOL(clk_enable);
  529. int meson_clk_disable(struct clk *clk)
  530. {
  531. int ret = 0;
  532. int ops_run_count = 0;
  533. if(IS_CLK_ERR(clk))
  534. return 0;
  535. if(clk_get_status(clk) == 0)
  536. return 0;
  537. if(clk->child.next){
  538. struct clk* pchild = (struct clk*)(clk->child.next);
  539. if(meson_clk_disable(pchild) != 0)
  540. return 1;
  541. pchild = (struct clk*)pchild->sibling.next;
  542. while(pchild){
  543. if(meson_clk_disable(pchild) != 0)
  544. return 1;
  545. pchild = (struct clk*)pchild->sibling.next;
  546. }
  547. }
  548. //do clk disable
  549. //post message before clk disable.
  550. {
  551. struct clk_ops *p;
  552. ret = 0;
  553. p = clk->clk_ops;
  554. while(p){
  555. ops_run_count++;
  556. if(p->clk_disable_before)
  557. ret = p->clk_disable_before(p->privdata);
  558. if(ret != 0)
  559. break;
  560. p = p->next;
  561. }
  562. }
  563. //do clock gate disable
  564. if(ret == 0){
  565. if(clk->disable)
  566. ret = clk->disable(clk);
  567. else if(clk->clk_gate_reg_adr != 0){
  568. aml_clr_reg32_mask(clk->clk_gate_reg_adr,clk->clk_gate_reg_mask);
  569. ret = 0;
  570. }
  571. }
  572. //post message after clk disable.
  573. {
  574. struct clk_ops *p;
  575. int idx = 0;
  576. p = clk->clk_ops;
  577. while(p){
  578. idx++;
  579. if(idx > ops_run_count)
  580. break;
  581. if(p->clk_disable_after)
  582. p->clk_disable_after(p->privdata,ret);
  583. p = p->next;
  584. }
  585. }
  586. return ret;
  587. }
  588. void clk_disable(struct clk *clk)
  589. {
  590. mutex_lock(&clock_ops_lock);
  591. meson_clk_disable(clk);
  592. mutex_unlock(&clock_ops_lock);
  593. }
  594. EXPORT_SYMBOL(clk_disable);
  595. /**
  596. * Section all get rate functions
  597. */
  598. static unsigned long clk_msr_get(struct clk * clk)
  599. {
  600. uint32_t temp;
  601. uint32_t cnt = 0;
  602. if(clk->rate>0)
  603. {
  604. return clk->rate;
  605. }
  606. if(clk->msr>0)
  607. {
  608. clk->rate = clk_util_clk_msr(clk->msr);
  609. }else if (clk->parent){
  610. cnt=clk_get_rate(clk->parent);
  611. cnt /= 1000000;
  612. clk->msr_mul=clk->msr_mul?clk->msr_mul:1;
  613. clk->msr_div=clk->msr_div?clk->msr_div:1;
  614. temp=cnt*clk->msr_mul;
  615. clk->rate=temp/clk->msr_div;
  616. clk->rate *= 1000000;
  617. }
  618. return clk->rate;
  619. }
  620. static unsigned long clk_get_rate_xtal(struct clk * clkdev)
  621. {
  622. unsigned long clk;
  623. clk = aml_get_reg32_bits(P_PREG_CTLREG0_ADDR, 4, 6);
  624. clk = clk * 1000 * 1000;
  625. return clk;
  626. }
  627. static unsigned long clk_get_rate_sys(struct clk * clkdev)
  628. {
  629. unsigned long clk;
  630. if (clkdev && clkdev->rate)
  631. clk = clkdev->rate;
  632. else {
  633. //using measure sys div3 to get sys pll clock. (25)
  634. clk = clk_util_clk_msr(25);
  635. clk *= 3;
  636. }
  637. return clk;
  638. }
  639. static unsigned long clk_get_rate_a9(struct clk * clkdev)
  640. {
  641. unsigned long clk = 0;
  642. unsigned int sysclk_cntl;;
  643. if (clkdev && clkdev->rate)
  644. return clkdev->rate;
  645. sysclk_cntl = aml_read_reg32(P_HHI_SYS_CPU_CLK_CNTL);
  646. if((sysclk_cntl & (1<<7)) == 0)
  647. clk = clk_get_rate_xtal(NULL);
  648. else{
  649. unsigned long parent_clk = 0;
  650. unsigned int pll_sel = sysclk_cntl&3;
  651. if(pll_sel == 0)
  652. parent_clk = clk_get_rate_xtal(NULL);
  653. else if(pll_sel == 1)
  654. parent_clk = clk_get_rate_sys(clkdev->parent);
  655. else if(pll_sel == 2)
  656. parent_clk = clk_util_clk_msr(3);//ddr clock
  657. else
  658. printk(KERN_INFO "Error : A9 parent pll selection incorrect!");
  659. if(parent_clk > 0){
  660. unsigned int N = (sysclk_cntl >> 8) & 0x3F;
  661. unsigned int div = 1;
  662. unsigned sel = (sysclk_cntl >> 2) & 3;
  663. if(sel == 1)
  664. div = 2;
  665. else if(sel == 2)
  666. div = 3;
  667. else if(sel == 3)
  668. div = 2 * N;
  669. clk = parent_clk / div;
  670. }
  671. }
  672. if (clk == 0) {
  673. pr_info("clk_get_rate_a9 measured clk=0 sysclk_cntl=%#x\n", sysclk_cntl);
  674. }
  675. return clk;
  676. }
  677. static unsigned long a9_cur_clk = 0;
  678. #ifdef CONFIG_HAVE_ARM_TWD
  679. static unsigned long clk_get_rate_smp_twd(struct clk * clkdev)
  680. {
  681. return (a9_cur_clk ? a9_cur_clk : clk_get_rate_a9(NULL)) / 4;
  682. }
  683. #endif /* CONFIG_HAVE_ARM_TWD */
  684. /**
  685. * udelay will delay depending on lpj. lpj is adjusted before|after
  686. * cpu freq is changed, so udelay could take longer or shorter than
  687. * expected. This function scales the udelay value to get a more
  688. * accurate delay during cpu freq changes.
  689. * lpj is adjust elsewhere, so drivers don't need to worry about this.
  690. */
  691. static inline void udelay_scaled(unsigned long usecs, unsigned int oldMHz,
  692. unsigned int newMHz)
  693. {
  694. udelay(usecs * newMHz / oldMHz);
  695. }
  696. /**
  697. * Internal CPU clock rate setting function.
  698. *
  699. * MUST be called with proper protection.
  700. */
  701. static int _clk_set_rate_cpu(struct clk *clk, unsigned long cpu, unsigned long gpu)
  702. {
  703. unsigned long parent = 0;
  704. unsigned long oldcpu = clk_get_rate_a9(clk);
  705. unsigned int cpu_clk_cntl = aml_read_reg32(P_HHI_SYS_CPU_CLK_CNTL);
  706. //printk(KERN_INFO "(CTS_CPU_CLK) %ldMHz --> %ldMHz (0x%x)\n", clk_get_rate_a9(clk) / 1000000, cpu / 1000000, cpu_clk_cntl);
  707. /**
  708. * CPU <-> GPU clock need to satisfy the following:
  709. *
  710. * CPU > GPU
  711. *
  712. * If GPU is busy then abort the scaling request.
  713. */
  714. if (aml_read_reg32(P_HHI_MALI_CLK_CNTL) & (1 << 8)) {
  715. if (!gpu)
  716. gpu = clk_util_clk_msr(35);
  717. if (cpu <= gpu) {
  718. unsigned int min_cpu = gpu_to_min_cpu(gpu / 1000) * 1000;
  719. // already at min
  720. if (oldcpu == min_cpu)
  721. return -EINVAL;
  722. // bring target cpu freq down only to minimum this gpu freq supports
  723. if (cpu < min_cpu)
  724. cpu = min_cpu;
  725. }
  726. }
  727. if ((cpu_clk_cntl & 3) == 1) {
  728. unsigned int n = 0;
  729. //unsigned char factor = 1;
  730. unsigned int scale_out = 0;
  731. parent = clk_get_rate_sys(clk->parent);
  732. // CPU switch to xtal
  733. aml_write_reg32(P_HHI_SYS_CPU_CLK_CNTL, cpu_clk_cntl & ~(1 << 7));
  734. if (oldcpu <= cpu) {
  735. // when increasing frequency, lpj has already been adjusted
  736. udelay_scaled(10, cpu / 1000000, 24 /*clk_get_rate_xtal*/);
  737. } else {
  738. // when decreasing frequency, lpj has not yet been adjusted
  739. udelay_scaled(10, oldcpu / 1000000, 24 /*clk_get_rate_xtal*/);
  740. }
  741. #if 0
  742. if (parent == cpu)
  743. scale_out = 0;
  744. else if ((parent >> 1) == cpu)
  745. scale_out = 1;
  746. else if (parent == ((cpu << 1) + cpu))
  747. scale_out = 2;
  748. else {
  749. parent = cpu;
  750. while (parent < 750000000) {
  751. switch (factor) {
  752. case 1:
  753. scale_out = 1;
  754. factor = 2;
  755. break;
  756. case 2:
  757. scale_out = 2;
  758. factor = 3;
  759. break;
  760. default:
  761. scale_out = 3;
  762. n++;
  763. factor = (n << 1);
  764. break;
  765. }
  766. parent = cpu * factor;
  767. }
  768. set_sys_pll(clk->parent, parent);
  769. }
  770. cpu_clk_cntl = ((cpu_clk_cntl & ~((3 << 2) | (0x3f << 8))) | (scale_out << 2) | (n << 8));
  771. printk(KERN_INFO "(CTS_CPU_CLK) syspll=%lu n=%d scale_out=%d cpu_clk_cntl=0x%x\n", parent, n, scale_out, cpu_clk_cntl);
  772. aml_write_reg32(P_HHI_SYS_CPU_CLK_CNTL, cpu_clk_cntl);
  773. #else
  774. #if 0
  775. //set_sys_pll(clk->parent, cpu);
  776. #else
  777. scale_out = set_sys_pll(clk->parent, oldcpu, cpu, &n);
  778. parent = clk_get_rate_sys(clk->parent);
  779. // update actual cpu freq
  780. cpu = parent;
  781. cpu_clk_cntl = ((cpu_clk_cntl & ~((3 << 2) | (0x3f << 8))) | (scale_out << 2) | (n << 8));
  782. #ifdef CONFIG_CPU_FREQ_DEBUG_DETAIL
  783. pr_debug("(CTS_CPU_CLK) syspll=%lu n=%d scale_out=%d cpu_clk_cntl=0x%x\n", parent, n, scale_out, cpu_clk_cntl);
  784. #endif /* CONFIG_CPU_FREQ_DEBUG_DETAIL */
  785. aml_write_reg32(P_HHI_SYS_CPU_CLK_CNTL, cpu_clk_cntl);
  786. #endif
  787. #endif
  788. //_clk_set_rate_gpu(clk_get_sys("mali", "pll_fixed"), gpu / 1000000, cpu);
  789. // cpu increased, adjust gpu
  790. if (cpu > a9_cur_clk)
  791. _clk_set_rate_gpu(NULL, 0, cpu);
  792. // Read CBUS for short delay, then CPU switch to sys pll
  793. cpu_clk_cntl = aml_read_reg32(P_HHI_SYS_CPU_CLK_CNTL);
  794. aml_write_reg32(P_HHI_SYS_CPU_CLK_CNTL, (cpu_clk_cntl) | (1 << 7));
  795. if (oldcpu <= cpu) {
  796. // when increasing frequency, lpj has already been adjusted
  797. udelay(100);
  798. } else {
  799. // when decreasing frequency, lpj has not yet been adjusted
  800. udelay_scaled(100, oldcpu / 1000000, cpu / 1000000);
  801. }
  802. // CPU switch to sys pll
  803. //cpu_clk_cntl = aml_read_reg32(P_HHI_SYS_CPU_CLK_CNTL);
  804. //aml_set_reg32_mask(P_HHI_SYS_CPU_CLK_CNTL, (1 << 7));
  805. }
  806. clk->rate = cpu;
  807. pr_info("(CTS_CPU_CLK) CPU %ld.%ldMHz\n", clk_get_rate_a9(clk) / 1000000,clk_get_rate_a9(clk)%1000000);
  808. return 0;
  809. }
  810. #ifdef CONFIG_SMP
  811. #define USE_ON_EACH_CPU 0
  812. struct clk_change_info{
  813. int cpu;
  814. struct clk * clk;
  815. unsigned long rate;
  816. int err;
  817. };
  818. #define MESON_CPU_CONTROL_REG (IO_AHB_BASE + 0x1ff80)
  819. #define MESON_CPU1_CONTROL_ADDR_REG (IO_AHB_BASE + 0x1ff84)
  820. #define MESON_CPU_STATUS_REG(cpu) (IO_AHB_BASE + 0x1ff90 +(cpu<<2))
  821. #define MESON_CPU_STATUS(cpu) aml_read_reg32(MESON_CPU_STATUS_REG(cpu))
  822. #define MESON_CPU_SET_STATUS(status) aml_write_reg32(MESON_CPU_STATUS_REG(smp_processor_id()),status)
  823. #define MESON_CPU_SLEEP 1
  824. #define MESON_CPU_WAKEUP 2
  825. inline void meson_set_cpu_ctrl_reg(int value)
  826. {
  827. spin_lock(&clockfw_lock);
  828. aml_write_reg32(MESON_CPU_CONTROL_REG, value);
  829. spin_unlock(&clockfw_lock);
  830. }
  831. static unsigned long cpu_sleep_max_count = 0;
  832. static unsigned long cpu_wait_max_count = 0;
  833. static unsigned tag_print=0;
  834. static inline unsigned long meson_smp_wait_others(unsigned status)
  835. {
  836. unsigned long count = 0;
  837. int mask;
  838. int cpu = 0, my = smp_processor_id();
  839. mask = (((1 << nr_cpu_ids) - 1) & (~(1 << my)));
  840. do {
  841. __asm__ __volatile__ ("wfe" : : : "memory");
  842. for_each_online_cpu(cpu) {
  843. if (cpu != my && MESON_CPU_STATUS(cpu) == status) {
  844. count++;
  845. mask &= ~(1 << cpu);
  846. }
  847. }
  848. } while (mask);
  849. return count;
  850. }
  851. static inline void meson_smp_init_transaction(void)
  852. {
  853. int cpu;
  854. aml_write_reg32(MESON_CPU_CONTROL_REG, 0);
  855. for_each_online_cpu(cpu) {
  856. aml_write_reg32(MESON_CPU_STATUS_REG(cpu), 0);
  857. }
  858. }
  859. static void smp_a9_clk_change(struct clk_change_info * info)
  860. {
  861. int cpu = smp_processor_id();
  862. #if USE_ON_EACH_CPU
  863. unsigned long count = 0;
  864. if (cpu != info->cpu) {
  865. unsigned long flags;
  866. MESON_CPU_SET_STATUS(MESON_CPU_SLEEP);
  867. pr_debug("CPU%u: Hey CPU %d, I am going to sleep\n", cpu, info->cpu);
  868. smp_wmb();
  869. dsb_sev();
  870. local_irq_save(flags);
  871. while ((aml_read_reg32(MESON_CPU_CONTROL_REG) & (1 << cpu)) == 0) {
  872. count++;
  873. __asm__ __volatile__ ("wfe" : : : "memory");
  874. }
  875. local_irq_restore(flags);
  876. MESON_CPU_SET_STATUS(MESON_CPU_WAKEUP);
  877. if (count > cpu_sleep_max_count) cpu_sleep_max_count = count;
  878. pr_debug("CPU%u: Hey CPU %d, I woke up (%lu %lu)\n", cpu, info->cpu, count, cpu_sleep_max_count);
  879. smp_wmb();
  880. dsb_sev();
  881. }
  882. else
  883. {
  884. pr_debug("CPU%u: Hey other CPU, I am waiting for you to sleep\n", cpu);
  885. count = meson_smp_wait_others(MESON_CPU_SLEEP);
  886. if (count > cpu_wait_max_count) cpu_wait_max_count = count;
  887. pr_debug("CPU%u: All other CPU in sleep (%lu %lu)\n", cpu, count, cpu_wait_max_count);
  888. info->err = _clk_set_rate_cpu(info->clk, info->rate, 0);
  889. aml_write_reg32(MESON_CPU_CONTROL_REG, 0xf);
  890. smp_wmb();
  891. dsb_sev();
  892. }
  893. #else
  894. if(cpu!=info->cpu){
  895. info->err = _clk_set_rate_cpu(info->clk, info->rate, 0);
  896. }
  897. #endif
  898. }
  899. #endif /* CONFIG_SMP */
  900. static int clk_set_rate_a9(struct clk *clk, unsigned long rate)
  901. {
  902. #ifdef CONFIG_SMP
  903. struct clk_change_info info;
  904. #endif /* CONFIG_SMP */
  905. int error = 0;
  906. if (rate < 1000)
  907. rate *= 1000000;
  908. if(freq_limit && rate > 1200000000)
  909. {
  910. rate = 1200000000;
  911. printk("cpu freq limited to %d \n", rate);
  912. }
  913. #ifdef CONFIG_SMP
  914. #if USE_ON_EACH_CPU
  915. if (aml_read_reg32(MESON_CPU_CONTROL_REG)) {
  916. #else
  917. if (num_online_cpus()>1) {
  918. #endif
  919. info.cpu = smp_processor_id();
  920. info.clk = clk;
  921. info.rate = rate;
  922. info.err = 0;
  923. #if USE_ON_EACH_CPU
  924. meson_smp_init_transaction();
  925. on_each_cpu((void (*) (void * info))smp_a9_clk_change, &info, 0);
  926. #else
  927. smp_call_function((void (*) (void * info))smp_a9_clk_change, &info, 1);
  928. #endif
  929. error = info.err;
  930. }
  931. else {
  932. error = _clk_set_rate_cpu(clk, rate, 0);
  933. }
  934. if (error == 0)
  935. a9_cur_clk = clk->rate;
  936. #else
  937. error = _clk_set_rate_cpu(clk, rate, 0);
  938. #endif /* CONFIG_SMP */
  939. return error;
  940. }
  941. static int clk81_target_rate = 0;
  942. static int set_clk81_clock(int rate)
  943. {
  944. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 0, 8, 1); //switch to xtal
  945. if (rate <= 100000000) {//100M
  946. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 3, 0, 7); //div 4
  947. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 7, 12, 3); //switch to fclk_div5
  948. } else if (rate > 100000000 && rate <= 118000000) {//111M
  949. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 5, 0, 7); //div 6
  950. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 6, 12, 3); //switch to fclk_div3
  951. } else if (rate > 118000000 && rate <= 132000000) {//125M
  952. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 7, 0, 7); //div 8
  953. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 5, 12, 3); //switch to fclk_div2
  954. } else if (rate > 132000000 && rate <= 180000000) {//167M
  955. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 3, 0, 7); //div 8
  956. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 6, 12, 3); //switch to fclk_div3
  957. } else if (rate > 180000000) {//200M
  958. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 1, 0, 7); //div 2
  959. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 7, 12, 3); //switch to fclk_div5
  960. }
  961. aml_read_reg32(P_HHI_MPEG_CLK_CNTL);
  962. aml_set_reg32_bits(P_HHI_MPEG_CLK_CNTL, 1, 8, 1);
  963. }
  964. int check_and_set_clk81(void)
  965. {
  966. if (clk81_target_rate > 0) {
  967. set_clk81_clock(clk81_target_rate);
  968. clk81_target_rate = 0;
  969. }
  970. return 0;
  971. }
  972. static int cal_final_clk81_clk(int rate)
  973. {
  974. int ret;
  975. if (rate <= 100000000) {//100M
  976. ret = 100000000;
  977. } else if (rate > 100000000 && rate <= 118000000) {//111M
  978. ret = 111000000;
  979. } else if (rate > 118000000 && rate <= 132000000) {//125M
  980. ret = 125000000;
  981. } else if (rate > 132000000 && rate <= 180000000) {//167M
  982. ret = 167000000;
  983. } else if (rate > 180000000) {//200M
  984. ret = 200000000;
  985. }
  986. return ret;
  987. }
  988. static int clk_set_rate_clk81(struct clk *clk, unsigned long rate)
  989. {
  990. int clk81_rate;
  991. clk81_rate = clk_get_rate(clk);
  992. if (cal_final_clk81_clk(rate) == clk81_rate)
  993. return 0;
  994. printk("pre clk81 rate is %d\n", clk81_rate);
  995. printk("new clk81 rate is %d\n", rate);
  996. clk81_target_rate = rate;
  997. while(clk81_target_rate >0)
  998. msleep(2);
  999. clk->rate = clk_util_clk_msr(7); //mesure current clk81 clock
  1000. clk81_rate = clk_get_rate(clk);
  1001. aml_clr_reg32_mask(P_UART0_CONTROL, (1 << 19) | 0xFFF);
  1002. aml_set_reg32_mask(P_UART0_CONTROL, (((clk81_rate / (115200 * 4)) - 1) & 0xfff));
  1003. aml_clr_reg32_mask(P_UART1_CONTROL, (1 << 19) | 0xFFF);
  1004. aml_set_reg32_mask(P_UART1_CONTROL, (((clk81_rate / (115200 * 4)) - 1) & 0xfff));
  1005. aml_clr_reg32_mask(P_AO_UART_CONTROL, (1 << 19) | 0xFFF);
  1006. aml_set_reg32_bits(P_AO_UART_CONTROL, ((clk81_rate / (115200 * 4)) - 1) & 0xfff, 0, 12);
  1007. printk(" \n");
  1008. printk("clk81 switch to %d\n", clk81_rate);
  1009. }
  1010. static unsigned long clk_get_rate_gpu(struct clk * clkdev)
  1011. {
  1012. unsigned long clk = 0;
  1013. unsigned int gpu_clk_cntl = aml_read_reg32(P_HHI_MALI_CLK_CNTL);
  1014. int src = (gpu_clk_cntl >> 9) & 7;
  1015. int N = (gpu_clk_cntl & 0x7F) + 1;
  1016. //printk(KERN_INFO "%s() cntl=0x%x src=%d N=%d\n", __FUNCTION__, gpu_clk_cntl, src, N);
  1017. if (src == 7)
  1018. clk = 2000000000 / (5 * N);
  1019. else if (src == 6)
  1020. clk = 2000000000 / (3 * N);
  1021. else if (src == 5)
  1022. clk = 2000000000 / (2 * N);
  1023. else
  1024. clk = 0;
  1025. return clk;
  1026. }
  1027. static unsigned int gpu_pll_cntl_lookup[] = {
  1028. 0x0E00, // DPLL / 1 = N/A (400)
  1029. 0x0E00, // DPLL / 2 = N/A (400)
  1030. 0x0E00, // DPLL / 3 = N/A (400)
  1031. 0x0E00, // DPLL / 4 = N/A (400)
  1032. 0x0E00, // DPLL / 5 = 400
  1033. 0x0C01, // DPLL / 6 = 333
  1034. 0x0A03, // DPLL / 7 = N/A (250)
  1035. 0x0A03, // DPLL / 8 = 250
  1036. 0x0C02, // DPLL / 9 = 222
  1037. 0x0E01, // DPLL / 10 = 200
  1038. 0x0C03, // DPLL / 11 = N/A (167)
  1039. 0x0C03, // DPLL / 12 = 167
  1040. 0x0A06, // DPLL / 13 = N/A (143)
  1041. 0x0A06, // DPLL / 14 = 143
  1042. 0x0C04, // DPLL / 15 = 133
  1043. 0x0A07, // DPLL / 16 = 125
  1044. 0x0C05, // DPLL / 17 = N/A (111)
  1045. 0x0C05, // DPLL / 18 = 111
  1046. 0x0E03, // DPLL / 19 = N/A (100)
  1047. 0x0E03, // DPLL / 20 = 100
  1048. 0x0C06, // DPLL / 21 = 95
  1049. 0x0C07, // DPLL / 22 = N/A (83)
  1050. 0x0C07, // DPLL / 23 = N/A (83)
  1051. 0x0C07, // DPLL / 24 = 83
  1052. 0x0C09, // DPLL / 25 = N/A (66)
  1053. 0x0C09, // DPLL / 26 = N/A (66)
  1054. 0x0C09, // DPLL / 27 = N/A (66)
  1055. 0x0C09, // DPLL / 28 = N/A (66)
  1056. 0x0C09, // DPLL / 29 = N/A (66)
  1057. 0x0C09, // DPLL / 30 = 66
  1058. };
  1059. static unsigned int gpu_to_min_cpu(unsigned int gpu)
  1060. {
  1061. if (gpu == 0)
  1062. gpu = mali_max;
  1063. //NOTICE cpu rates must be rounded to final rate
  1064. if (gpu <= 83000) return 96000;
  1065. if (gpu <= 111000) return 120000;
  1066. if (gpu <= 167000) return 168000;
  1067. if (gpu <= 250000) return 264000;
  1068. if (gpu <= 333000) return 336000;
  1069. return 408000;
  1070. }
  1071. /**
  1072. * Internal GPU clock rate setting function.
  1073. *
  1074. * MUST be called with proper protection.
  1075. */
  1076. static int _clk_set_rate_gpu(struct clk *clk, unsigned long gpu, unsigned long cpu)
  1077. {
  1078. unsigned long mali_flags;
  1079. int enabled;
  1080. cpu /= 1000000;
  1081. if (!gpu) {
  1082. if (cpu >= 400)
  1083. gpu = 400;
  1084. else if (cpu > 333)
  1085. gpu = 333;
  1086. else if (cpu > 250)
  1087. gpu = 250;
  1088. else if (cpu > 166)
  1089. gpu = 166;
  1090. else if (cpu > 111)
  1091. gpu = 111;
  1092. else if (cpu > 83)
  1093. gpu = 83;
  1094. else
  1095. gpu = 66;
  1096. } else if (gpu > 400)
  1097. gpu = 400;
  1098. if (gpu > (mali_max / 1000))
  1099. gpu = mali_max / 1000;
  1100. if (gpu == (clk_get_rate_gpu(NULL) / 1000000))
  1101. return 0;
  1102. mali_flags = mali_clock_gating_lock();
  1103. enabled = (aml_read_reg32(P_HHI_MALI_CLK_CNTL) & (1 << 8));
  1104. if (enabled)
  1105. aml_clr_reg32_mask((P_HHI_MALI_CLK_CNTL), (1 << 8));
  1106. aml_clr_reg32_mask(P_HHI_MALI_CLK_CNTL, 0x7F | (0x7 << 9));
  1107. aml_set_reg32_mask(P_HHI_MALI_CLK_CNTL,
  1108. gpu_pll_cntl_lookup[2000 / gpu - 1] | (enabled ? (1 << 8) : 0));
  1109. mali_clock_gating_unlock(mali_flags);
  1110. if (enabled) {
  1111. pr_debug("%s() cpu=%luMHz gpu=%luMHz idx=%ld cntl=0x%x\n", __FUNCTION__, cpu, gpu, 2000 / gpu - 1, gpu_pll_cntl_lookup[2000 / gpu - 1]);
  1112. pr_debug("%s() gpu=%luMHz\n", __FUNCTION__, clk_get_rate_gpu(NULL) / 1000000);
  1113. }
  1114. return 0;
  1115. }
  1116. static int clk_set_rate_mali(struct clk *clk, unsigned long rate)
  1117. {
  1118. pr_debug("%s() GPU %luMHz --> %luMHz\n", __FUNCTION__, clk_get_rate_gpu(NULL) / 1000000, rate / 1000000);
  1119. if (clk->priv)
  1120. _clk_set_rate_gpu(clk, 0, clk_get_rate_a9(clk->priv));
  1121. pr_debug("%s() %luMHz\n", __FUNCTION__, clk_get_rate_gpu(NULL) / 1000000);
  1122. return 0;
  1123. }
  1124. static int clk_enable_mali(struct clk *clk)
  1125. {
  1126. /* Turn on mali clock */
  1127. unsigned long cpu = 0;
  1128. unsigned int min_cpu;
  1129. unsigned long flags;
  1130. unsigned long mali_flags;
  1131. if (!clk->priv) {
  1132. return -1;
  1133. }
  1134. #ifdef CONFIG_CPU_FREQ
  1135. cpu = clk_get_rate_a9(clk->priv);
  1136. min_cpu = gpu_to_min_cpu(mali_max);
  1137. if ((cpu / 1000) < min_cpu) {
  1138. /* bump CPU up to get Mali enabled at higher freq. Using cpufreq
  1139. * to do so, which will properly adjust voltage and jiffies
  1140. */
  1141. meson_cpufreq_boost(min_cpu);
  1142. }
  1143. #endif
  1144. spin_lock_irqsave(&clockfw_lock, flags);
  1145. cpu = clk_get_rate_a9(clk->priv);
  1146. _clk_set_rate_gpu(NULL, 0, cpu);
  1147. mali_flags = mali_clock_gating_lock();
  1148. aml_set_reg32_mask(P_HHI_MALI_CLK_CNTL, (1 << 8));
  1149. mali_clock_gating_unlock(mali_flags);
  1150. #ifdef CONFIG_CPU_FREQ_DEBUG
  1151. printk(KERN_INFO "%s() GPU=%luMHz CPU=%luMhz\n", __FUNCTION__, clk_get_rate_gpu(NULL) / 1000000, clk_get_rate_a9(clk->priv) / 1000000);
  1152. #endif /* CONFIG_CPU_FREQ_DEBUG */
  1153. spin_unlock_irqrestore(&clockfw_lock, flags);
  1154. return 0;
  1155. }
  1156. static int clk_disable_mali(struct clk *clk)
  1157. {
  1158. /* Turn off mali clock */
  1159. unsigned long flags;
  1160. unsigned long mali_flags;
  1161. spin_lock_irqsave(&clockfw_lock, flags);
  1162. #ifdef CONFIG_CPU_FREQ_DEBUG
  1163. printk(KERN_INFO "%s() GPU=%luMHz CPU=%luMhz\n", __FUNCTION__, clk_get_rate_gpu(NULL) / 1000000, clk_get_rate_a9(clk->priv) / 1000000);
  1164. #endif /* CONFIG_CPU_FREQ_DEBUG */
  1165. mali_flags = mali_clock_gating_lock();
  1166. aml_clr_reg32_mask(P_HHI_MALI_CLK_CNTL, (1 << 8));
  1167. mali_clock_gating_unlock(mali_flags);
  1168. spin_unlock_irqrestore(&clockfw_lock, flags);
  1169. return 0;
  1170. }
  1171. #if 0
  1172. static int clk_status_mali(struct clk *clk)
  1173. {
  1174. /* Check mali clock status */
  1175. unsigned long flags;
  1176. spin_lock_irqsave(&clockfw_lock, flags);
  1177. return aml_read_reg32(P_HHI_MALI_CLK_CNTL) & (1 << 8)
  1178. spin_unlock_irqrestore(&clockfw_lock, flags);
  1179. }
  1180. #endif
  1181. static unsigned long clk_get_rate_vid2(struct clk * clkdev)
  1182. {
  1183. unsigned long clk;
  1184. unsigned int viid_cntl = aml_read_reg32(P_HHI_VIID_PLL_CNTL);
  1185. unsigned long parent_clk;
  1186. unsigned od,M,N;
  1187. parent_clk = clk_get_rate(clkdev->parent);
  1188. parent_clk /= 1000000;
  1189. od = (viid_cntl>>16)&3;
  1190. M = viid_cntl&0x1FF;
  1191. N = (viid_cntl>>9)&0x1F;
  1192. if(od == 0)
  1193. od = 1;
  1194. else if(od == 1)
  1195. od = 2;
  1196. else if(od == 2)
  1197. od = 4;
  1198. clk = parent_clk * M / N;
  1199. clk /= od;
  1200. clk *= 1000000;
  1201. return clk;
  1202. }
  1203. static unsigned long clk_get_rate_hpll(struct clk * clkdev)
  1204. {
  1205. unsigned long clk;
  1206. unsigned int vid_cntl = aml_read_reg32(P_HHI_VID_PLL_CNTL);
  1207. unsigned long parent_clk;
  1208. unsigned od_fb,od_hdmi,od_ldvs,M,N;
  1209. parent_clk = clk_get_rate(clkdev->parent);
  1210. parent_clk /= 1000000;
  1211. od_ldvs = (vid_cntl>>16)&3;
  1212. od_hdmi = (vid_cntl>>18)&3;
  1213. od_fb = (vid_cntl>>20)&3;
  1214. M = vid_cntl&0x3FF;
  1215. N = (vid_cntl>>10)&0x1F;
  1216. if(od_hdmi == 0)
  1217. od_hdmi = 1;
  1218. else if(od_hdmi == 1)
  1219. od_hdmi = 2;
  1220. else if(od_hdmi == 2)
  1221. od_hdmi = 4;
  1222. if(od_fb == 0)
  1223. od_fb = 1;
  1224. else if(od_fb == 1)
  1225. od_fb = 2;
  1226. else if(od_fb == 2)
  1227. od_fb = 4;
  1228. clk = parent_clk * M * od_fb / N;
  1229. clk /= od_hdmi;
  1230. clk *= 1000000;
  1231. return clk;
  1232. }
  1233. #define CLK_DEFINE(devid,conid,msr_id,setrate,getrate,en,dis,privdata) \
  1234. static struct clk clk_##devid={ \
  1235. .set_rate=setrate,.get_rate=getrate,.enable=en,.disable=dis, \
  1236. .priv=privdata,.parent=&clk_##conid ,.msr=msr_id \
  1237. }; \
  1238. static struct clk_lookup clk_lookup_##devid={ \
  1239. .dev_id=#devid,.con_id=#conid,.clk=&clk_##devid \
  1240. };clkdev_add(&clk_lookup_##devid)
  1241. ///TOP level
  1242. static struct clk clk_xtal = {
  1243. .rate = -1,
  1244. .get_rate = clk_get_rate_xtal,
  1245. };
  1246. static struct clk_lookup clk_lookup_xtal = {
  1247. .dev_id = "xtal",
  1248. .con_id = NULL,
  1249. .clk = &clk_xtal
  1250. };
  1251. #if 1
  1252. #define SYS_PLL_TABLE_MIN 48000000
  1253. #define SYS_PLL_TABLE_MAX 1512000000
  1254. struct sys_pll_s {
  1255. unsigned cntl;
  1256. unsigned cntl2;
  1257. unsigned cntl3;
  1258. unsigned cntl4;
  1259. unsigned scan;
  1260. unsigned scale_divn;
  1261. };
  1262. static unsigned sys_pll_settings[][6] = {
  1263. {0x20220, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 3, 2}, // 48 = 192 / 2 / 2
  1264. {0x20224, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 2}, // 72 = 216 / 3
  1265. {0x20220, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 96
  1266. {0x20228, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 120
  1267. {0x20230, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 144
  1268. {0x20238, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 168
  1269. {0x20220, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 192
  1270. {0x20224, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 216
  1271. {0x20228, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 240
  1272. {0x2022C, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 264
  1273. {0x20230, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 288
  1274. {0x20234, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 312
  1275. {0x20238, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 336
  1276. {0x2023C, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 360
  1277. {0x00220, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 384
  1278. {0x00222, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 408
  1279. {0x00224, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 432
  1280. {0x00226, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 456
  1281. {0x00228, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 480
  1282. {0x0022A, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 504
  1283. {0x0022C, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 528
  1284. {0x0022E, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 552
  1285. {0x00230, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 576
  1286. {0x00232, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 600
  1287. {0x00234, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 624
  1288. {0x00236, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 648
  1289. {0x00238, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 672
  1290. {0x0023A, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 696
  1291. {0x0023C, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 720
  1292. {0x0023E, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 1}, // 744
  1293. {0x00220, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 768
  1294. {0x00221, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 792
  1295. {0x00222, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 816
  1296. {0x00223, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 840
  1297. {0x00224, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 864
  1298. {0x00225, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 888
  1299. {0x00226, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 912
  1300. {0x00227, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 936
  1301. {0x00228, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 960
  1302. {0x00229, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 984
  1303. {0x0022A, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1008
  1304. {0x0022B, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1032
  1305. {0x0022C, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1056
  1306. {0x0022D, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1080
  1307. {0x0022E, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1104
  1308. {0x0022F, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1128
  1309. {0x00230, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1152
  1310. {0x00231, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1176
  1311. {0x00232, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1200
  1312. {0x00233, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1224
  1313. {0x00234, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1248
  1314. {0x00235, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1272
  1315. {0x00236, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1296
  1316. {0x00237, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1320
  1317. {0x00238, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1344
  1318. {0x00239, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1368
  1319. {0x0023a, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1392
  1320. {0x0023b, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1416
  1321. {0x0023c, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1440
  1322. {0x0023d, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1464
  1323. {0x0023e, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1488
  1324. {0x0023f, M6_SYS_PLL_CNTL_2, M6_SYS_PLL_CNTL_3, M6_SYS_PLL_CNTL_4, 0}, // 1512
  1325. };
  1326. static unsigned setup_a9_clk_max=1200000000;
  1327. static unsigned setup_a9_clk_min=48000000;
  1328. static int __init a9_clk_max(char *str)
  1329. {
  1330. unsigned long clk=clkparse(str, 0);
  1331. if(clk<SYS_PLL_TABLE_MIN || clk>SYS_PLL_TABLE_MAX)
  1332. return 0;
  1333. setup_a9_clk_max=clk-(clk%24000000);
  1334. BUG_ON(setup_a9_clk_min>setup_a9_clk_max);
  1335. return 0;
  1336. }
  1337. early_param("a9_clk_max", a9_clk_max);
  1338. static int __init a9_clk_min(char *str)
  1339. {
  1340. unsigned long clk = clkparse(str, 0);
  1341. if (clk < SYS_PLL_TABLE_MIN || clk > SYS_PLL_TABLE_MAX)
  1342. return 0;
  1343. setup_a9_clk_min = clk - (clk % 24000000);
  1344. BUG_ON(setup_a9_clk_min>setup_a9_clk_max);
  1345. return 0;
  1346. }
  1347. early_param("a9_clk_min", a9_clk_min);
  1348. static int set_sys_pll(struct clk *clk, unsigned long src, unsigned long dst, unsigned * scale_divn)
  1349. {
  1350. int idx;
  1351. unsigned int curr_cntl = aml_read_reg32(P_HHI_SYS_PLL_CNTL);
  1352. unsigned int cpu_clk_cntl = 0;
  1353. if (dst < SYS_PLL_TABLE_MIN) dst = SYS_PLL_TABLE_MIN;
  1354. if (dst > SYS_PLL_TABLE_MAX) dst = SYS_PLL_TABLE_MAX;
  1355. idx = ((dst - SYS_PLL_TABLE_MIN) / 1000000) / 24;
  1356. cpu_clk_cntl = sys_pll_settings[idx][0];
  1357. #ifdef CONFIG_CPU_FREQ_DEBUG_DETAIL
  1358. pr_debug("CTS_CPU_CLK %ldMHz idx=%d\n", dst / 1000000, idx);
  1359. pr_debug("CTS_CPU_CLK sys_pll_cntl=0x%x cur_cntl=0x%x\n", cpu_clk_cntl, curr_cntl);
  1360. #endif /* CONFIG_CPU_FREQ_DEBUG_DETAIL */
  1361. if (cpu_clk_cntl != curr_cntl) {
  1362. aml_write_reg32(P_HHI_SYS_PLL_CNTL, sys_pll_settings[idx][0] | (1 << 29));
  1363. aml_write_reg32(P_HHI_SYS_PLL_CNTL2, sys_pll_settings[idx][1]);
  1364. aml_write_reg32(P_HHI_SYS_PLL_CNTL3, sys_pll_settings[idx][2]);
  1365. aml_write_reg32(P_HHI_SYS_PLL_CNTL4, sys_pll_settings[idx][3]);
  1366. aml_write_reg32(P_HHI_SYS_PLL_CNTL, sys_pll_settings[idx][0]);
  1367. #if 1
  1368. if (src <= dst) {
  1369. // when increasing frequency, lpj has already been adjusted
  1370. do {
  1371. udelay_scaled(100, dst / 1000000, 24 /*clk_get_rate_xtal*/);
  1372. } while ((aml_read_reg32(P_HHI_SYS_PLL_CNTL) & 0x80000000) == 0);
  1373. } else {
  1374. // when decreasing frequency, lpj has not yet been adjusted
  1375. do {
  1376. udelay_scaled(100, src / 1000000, 24 /*clk_get_rate_xtal*/);
  1377. } while ((aml_read_reg32(P_HHI_SYS_PLL_CNTL) & 0x80000000) == 0);
  1378. }
  1379. #else
  1380. M6_PLL_WAIT_FOR_LOCK(P_HHI_SYS_PLL_CNTL);
  1381. #endif
  1382. }
  1383. else {
  1384. //printk(KERN_INFO "(CTS_CPU_CLK) No Change (0x%x)\n", cpu_clk_cntl);
  1385. }
  1386. *scale_divn = sys_pll_settings[idx][5];
  1387. if (clk)
  1388. clk->rate = (idx * 24000000) + SYS_PLL_TABLE_MIN;
  1389. #ifdef CONFIG_CPU_FREQ_DEBUG_DETAIL
  1390. pr_debug("CTS_CPU_CLK %ldMHz idx=%d 0x%x scale_out=%d\n", dst / 1000000, idx, cpu_clk_cntl, sys_pll_settings[idx][4]);
  1391. #endif /* CONFIG_CPU_FREQ_DEBUG_DETAIL */
  1392. return sys_pll_settings[idx][4];
  1393. }
  1394. #else
  1395. static int set_sys_pll(struct clk *clk, unsigned long src, unsigned long dst)
  1396. {
  1397. unsigned int od, M, N;
  1398. unsigned long parent_clk = 0, rate;
  1399. rate = clk_get_rate(clk);
  1400. if (dst > clk->max || dst < clk->min) {
  1401. printk(KERN_ERR "SYS PLL rate (%lu) out of range [%lu, %lu]\n", dst, clk->min, clk->max);
  1402. return -EINVAL;
  1403. }
  1404. if (dst != rate) {
  1405. unsigned long vco;
  1406. int found = 0;
  1407. if (clk->parent)
  1408. parent_clk = clk_get_rate(clk->parent);
  1409. else
  1410. printk(KERN_ERR "sys pll: no parent clock assigned!\n");
  1411. parent_clk /= 1000000;
  1412. dst /= 1000000;
  1413. if (dst > 750)
  1414. od = 1;
  1415. else if ((dst << 1) > 750)
  1416. od = 2;
  1417. else
  1418. od = 4;
  1419. vco = dst * od;
  1420. // vco 750M ~ 1.5G
  1421. for (N = 1; N < 0x1F; N++) {
  1422. for (M = 0x1FF; M > 0; M--) {
  1423. rate = parent_clk * M / N;
  1424. if( rate == vco) {
  1425. found = 1;
  1426. break;
  1427. }
  1428. else if (rate > vco)
  1429. continue;
  1430. else
  1431. break;
  1432. }
  1433. if (found)
  1434. break;
  1435. }
  1436. if (found) {
  1437. unsigned int sys_clk_cntl = 0;
  1438. if (od == 4)
  1439. od = 2;
  1440. else if (od == 2)
  1441. od = 1;
  1442. else
  1443. od = 0;
  1444. sys_clk_cntl = (M) | (N << 9) | (od << 16);
  1445. M6_PLL_RESET(P_HHI_SYS_PLL_CNTL);
  1446. aml_write_reg32(P_HHI_SYS_PLL_CNTL2, M6_SYS_PLL_CNTL_2);
  1447. aml_write_reg32(P_HHI_SYS_PLL_CNTL3, M6_SYS_PLL_CNTL_3);
  1448. aml_write_reg32(P_HHI_SYS_PLL_CNTL4, M6_SYS_PLL_CNTL_4);
  1449. aml_write_reg32(P_HHI_SYS_PLL_CNTL, sys_clk_cntl);
  1450. #if 1
  1451. do {
  1452. udelay(100);
  1453. } while ((aml_read_reg32(P_HHI_SYS_PLL_CNTL) & 0x80000000) == 0);
  1454. #else
  1455. M6_PLL_WAIT_FOR_LOCK(P_HHI_SYS_PLL_CNTL);
  1456. #endif
  1457. printk(KERN_INFO "(CTS_CPU_CLK) M=%d N=%d OD=%d sys_clk_cntl=0x%x\n", M, N, od, sys_clk_cntl);
  1458. }
  1459. else
  1460. {
  1461. printk(KERN_ERR "sys pll: no clock setting matched.\n");
  1462. return -1;
  1463. }
  1464. }
  1465. return 1;
  1466. }
  1467. #endif
  1468. static int set_hpll_pll(struct clk * clk, unsigned long dst)
  1469. {
  1470. unsigned od,M,N;
  1471. unsigned long parent_clk = 0, rate;
  1472. rate = clk_get_rate(clk);
  1473. if(dst > clk->max || dst < clk->min){
  1474. printk("vid pll: invalid rate : %lu [%lu ~ %lu]\n",dst,clk->min,clk->max);
  1475. return -1;
  1476. }
  1477. if(dst != rate){
  1478. unsigned vco;
  1479. unsigned od_fb = 0;
  1480. int found = 0;
  1481. if(clk->parent)
  1482. parent_clk = clk_get_rate(clk->parent);
  1483. else
  1484. printk("vid2 pll: no parent clock assigned!\n");
  1485. parent_clk /= 1000000;
  1486. dst /= 1000000;
  1487. if(dst > 750)
  1488. od = 1;
  1489. else if( (dst * 2) > 750)
  1490. od = 2;
  1491. else
  1492. od = 4;
  1493. vco = dst * od;
  1494. //vco 750M ~1.5G
  1495. for(N = 1; N < 0x1F; N++){
  1496. for(M = 0x1FF * 4; M > 0; M--){
  1497. rate = parent_clk * M / N;
  1498. if(rate == vco){
  1499. if(M > 0x1FF){
  1500. if(((M / 2) < 0x1FF) && ((M%2) == 0)){
  1501. od_fb = 2;
  1502. M = M / od_fb;
  1503. found = 1;
  1504. }else if((M%4) == 0){
  1505. od_fb = 4;
  1506. M = M/od_fb;
  1507. found = 1;
  1508. }
  1509. else
  1510. break;
  1511. }
  1512. else{
  1513. od_fb = 1;
  1514. found = 1;
  1515. break;
  1516. }
  1517. }
  1518. else if(rate > vco)
  1519. continue;
  1520. else
  1521. break;
  1522. }
  1523. if(found)
  1524. break;
  1525. }
  1526. if(found){
  1527. unsigned vid_cntl = 0;
  1528. if(od == 4)
  1529. od = 2;
  1530. else if(od == 2)
  1531. od = 1;
  1532. else
  1533. od = 0;
  1534. if(od_fb == 4)
  1535. od_fb = 2;
  1536. else if(od_fb == 2)
  1537. od_fb = 1;
  1538. else
  1539. od_fb = 0;
  1540. vid_cntl = (M) | (N <<10) | (od << 18) | (od_fb <<20);
  1541. //VID PLL
  1542. M6_PLL_RESET(P_HHI_VID_PLL_CNTL);
  1543. aml_write_reg32(P_HHI_VID_PLL_CNTL2, M6_VID_PLL_CNTL_2 );
  1544. aml_write_reg32(P_HHI_VID_PLL_CNTL3, M6_VID_PLL_CNTL_3 );
  1545. aml_write_reg32(P_HHI_VID_PLL_CNTL4, M6_VID_PLL_CNTL_4 );
  1546. aml_write_reg32(P_HHI_VID_PLL_CNTL, vid_cntl );
  1547. M6_PLL_WAIT_FOR_LOCK(P_HHI_VID_PLL_CNTL);
  1548. }
  1549. else
  1550. {
  1551. printk("vid pll: no clock setting matched.\n");
  1552. return -1;
  1553. }
  1554. }
  1555. return 1;
  1556. }
  1557. static int set_fixed_pll(struct clk * clk, unsigned long dst)
  1558. {
  1559. if(dst == 2000000000){
  1560. //fixed pll = xtal * M(0:8) * OD_FB(4) /N(9:13) /OD(16:17)
  1561. //M: 0~511 OD_FB:0~1 + 1, N:0~32 + 1 OD:0~3 + 1
  1562. //recommend this pll is fixed as 2G.
  1563. unsigned long xtal = 24000000;
  1564. unsigned cntl = aml_read_reg32(P_HHI_MPLL_CNTL);
  1565. unsigned m = cntl&0x1FF;
  1566. unsigned n = ((cntl>>9)&0x1F);
  1567. unsigned od = ((cntl >>16)&3) + 1;
  1568. unsigned od_fb = ((aml_read_reg32(P_HHI_MPLL_CNTL4)>>4)&1) + 1;
  1569. unsigned long rate;
  1570. if(clk->parent)
  1571. xtal = clk_get_rate(clk->parent);
  1572. xtal /= 1000000;
  1573. rate = xtal * m * od_fb;
  1574. rate /= n;
  1575. rate /= od;
  1576. rate *= 1000000;
  1577. if(dst != rate){
  1578. M6_PLL_RESET(P_HHI_MPLL_CNTL);
  1579. aml_write_reg32(P_HHI_MPLL_CNTL2,M6_MPLL_CNTL_2);
  1580. aml_write_reg32(P_HHI_MPLL_CNTL2, M6_MPLL_CNTL_2 );
  1581. aml_write_reg32(P_HHI_MPLL_CNTL3, M6_MPLL_CNTL_3 );
  1582. aml_write_reg32(P_HHI_MPLL_CNTL4, M6_MPLL_CNTL_4 );
  1583. aml_write_reg32(P_HHI_MPLL_CNTL5, M6_MPLL_CNTL_5 );
  1584. aml_write_reg32(P_HHI_MPLL_CNTL6, M6_MPLL_CNTL_6 );
  1585. aml_write_reg32(P_HHI_MPLL_CNTL7, M6_MPLL_CNTL_7 );
  1586. aml_write_reg32(P_HHI_MPLL_CNTL8, M6_MPLL_CNTL_8 );
  1587. aml_write_reg32(P_HHI_MPLL_CNTL9, M6_MPLL_CNTL_9 );
  1588. aml_write_reg32(P_HHI_MPLL_CNTL10,M6_MPLL_CNTL_10);
  1589. aml_write_reg32(P_HHI_MPLL_CNTL, 0x67d );
  1590. M6_PLL_WAIT_FOR_LOCK(P_HHI_MPLL_CNTL);
  1591. }
  1592. }
  1593. else
  1594. return -1;
  1595. return 0;
  1596. }
  1597. static int set_vid2_pll(struct clk * clk, unsigned long dst)
  1598. {
  1599. unsigned od,M,N;
  1600. unsigned long parent_clk = 0, rate;
  1601. rate = clk_get_rate(clk);
  1602. if(dst > clk->max || dst < clk->min){
  1603. printk("vid2 pll: invalid rate : %lu [%lu ~ %lu]\n",dst,clk->min,clk->max);
  1604. return -1;
  1605. }
  1606. if(dst != rate){
  1607. unsigned vco;
  1608. int found = 0;
  1609. if(clk->parent)
  1610. parent_clk = clk_get_rate(clk->parent);
  1611. else
  1612. printk("vid2 pll: no parent clock assigned!\n");
  1613. parent_clk /= 1000000;
  1614. dst /= 1000000;
  1615. if(dst > 750)
  1616. od = 1;
  1617. else if( (dst * 2) > 750)
  1618. od = 2;
  1619. else
  1620. od = 4;
  1621. vco = dst * od;
  1622. //vco 750M ~1.5G
  1623. for(N = 1; N < 0x1F; N++){
  1624. for(M = 0x1FF; M > 0; M--){
  1625. rate = parent_clk * M / N;
  1626. if(rate == vco){
  1627. found = 1;
  1628. break;
  1629. }
  1630. else if(rate > vco)
  1631. continue;
  1632. else
  1633. break;
  1634. }
  1635. if(found)
  1636. break;
  1637. }
  1638. if(found){
  1639. unsigned viid_cntl = 0;
  1640. if(od == 4)
  1641. od = 2;
  1642. else if(od == 2)
  1643. od = 1;
  1644. else
  1645. od = 0;
  1646. viid_cntl = (M) | (N <<9) | (od << 16);
  1647. //VIID PLL
  1648. M6_PLL_RESET(P_HHI_VIID_PLL_CNTL);
  1649. aml_write_reg32(P_HHI_VIID_PLL_CNTL2, M6_VIID_PLL_CNTL_2 );
  1650. aml_write_reg32(P_HHI_VIID_PLL_CNTL3, M6_VIID_PLL_CNTL_3 );
  1651. aml_write_reg32(P_HHI_VIID_PLL_CNTL4, M6_VIID_PLL_CNTL_4 );
  1652. aml_write_reg32(P_HHI_VIID_PLL_CNTL, viid_cntl);
  1653. M6_PLL_WAIT_FOR_LOCK(P_HHI_VIID_PLL_CNTL);
  1654. }
  1655. else
  1656. {
  1657. printk("vid2 pll: no clock setting matched.\n");
  1658. return -1;
  1659. }
  1660. }
  1661. return 1;
  1662. }
  1663. //------------------------------------
  1664. //return 0:not in the clock tree, 1:in the clock tree
  1665. static int clk_in_clocktree(struct clk *clktree, struct clk *clk)
  1666. {
  1667. struct clk *p;
  1668. int ret = 0;
  1669. if(IS_CLK_ERR(clk) || IS_CLK_ERR(clktree))
  1670. return 0;
  1671. if(clktree == clk)
  1672. return 1;
  1673. p = (struct clk*)clktree->sibling.next;
  1674. while(p){
  1675. if(p == clk){
  1676. ret = 1;
  1677. break;
  1678. }
  1679. p = (struct clk*)p->sibling.next;
  1680. }
  1681. if(ret == 1)
  1682. return ret;
  1683. return clk_in_clocktree((struct clk*)clktree->child.next, clk);
  1684. }
  1685. //return 0:ok, 1:fail
  1686. static int meson_clk_register(struct clk* clk, struct clk* parent)
  1687. {
  1688. if (clk_in_clocktree(parent,clk))
  1689. return 0;
  1690. mutex_lock(&clock_ops_lock);
  1691. clk->parent = parent;
  1692. if (parent->child.next == NULL) {
  1693. parent->child.next = clk;
  1694. clk->sibling.next = NULL;
  1695. clk->sibling.prev = NULL;
  1696. }
  1697. else {
  1698. struct clk* p = parent->child.next;
  1699. while(p->sibling.next != NULL)
  1700. p = p->sibling.next;
  1701. p->sibling.next = clk;
  1702. clk->sibling.prev = p;
  1703. clk->sibling.next = NULL;
  1704. }
  1705. mutex_unlock(&clock_ops_lock);
  1706. return 0;
  1707. }
  1708. int clk_register(struct clk *clk,const char *parent)
  1709. {
  1710. struct clk* p = clk_get_sys(parent,0);
  1711. if(!IS_CLK_ERR(p))
  1712. return meson_clk_register(clk,p);
  1713. return 1;
  1714. }
  1715. EXPORT_SYMBOL(clk_register);
  1716. void clk_unregister(struct clk *clk)
  1717. {
  1718. if(IS_CLK_ERR(clk))
  1719. return;
  1720. mutex_lock(&clock_ops_lock);
  1721. if(clk->sibling.next){
  1722. struct clk* pnext = (struct clk*)(clk->sibling.next);
  1723. pnext->sibling.prev = clk->sibling.prev;
  1724. if(clk->sibling.prev)
  1725. ((struct clk*)(clk->sibling.prev))->sibling.next = pnext;
  1726. else
  1727. clk->parent->child.next = pnext;
  1728. }
  1729. else if(clk->sibling.prev){
  1730. struct clk* prev = (struct clk*)(clk->sibling.prev);
  1731. prev->sibling.next = clk->sibling.next;
  1732. if(clk->sibling.next)
  1733. ((struct clk*)(clk->sibling.next))->sibling.prev = prev;
  1734. }
  1735. else{
  1736. struct clk* parent = clk->parent;
  1737. if(parent)
  1738. parent->child.next = NULL;
  1739. }
  1740. clk->sibling.next = NULL;
  1741. clk->sibling.prev = NULL;
  1742. mutex_unlock(&clock_ops_lock);
  1743. }
  1744. EXPORT_SYMBOL(clk_unregister);
  1745. /**
  1746. * Check clock status.
  1747. *
  1748. * 0 -- Disabled
  1749. * 1 -- Enabled
  1750. * 2 -- Unknown
  1751. */
  1752. int clk_get_status(struct clk *clk)
  1753. {
  1754. int ret = 2;
  1755. unsigned long flags;
  1756. spin_lock_irqsave(&clockfw_lock, flags);
  1757. if (clk->status)
  1758. ret = clk->status(clk);
  1759. else if (clk->clk_gate_reg_adr != 0)
  1760. ret = ((aml_read_reg32(clk->clk_gate_reg_adr) & clk->clk_gate_reg_mask) ? 1 : 0);
  1761. spin_unlock_irqrestore(&clockfw_lock, flags);
  1762. return ret;
  1763. }
  1764. EXPORT_SYMBOL(clk_get_status);
  1765. //return: 0:success 1: fail
  1766. int clk_ops_register(struct clk *clk, struct clk_ops *ops)
  1767. {
  1768. int found = 0;
  1769. struct clk_ops *p;
  1770. mutex_lock(&clock_ops_lock);
  1771. ops->next = NULL;
  1772. p = clk->clk_ops;
  1773. while(p != NULL){
  1774. if(p == ops){
  1775. found = 1;
  1776. break;
  1777. }
  1778. p = p->next;
  1779. }
  1780. if(found == 0){
  1781. if(clk->clk_ops == NULL)
  1782. clk->clk_ops = ops;
  1783. else{
  1784. struct clk_ops* p = clk->clk_ops;
  1785. while(p->next)
  1786. p = p->next;
  1787. p->next = ops;
  1788. }
  1789. }
  1790. mutex_unlock(&clock_ops_lock);
  1791. return 0;
  1792. }
  1793. EXPORT_SYMBOL(clk_ops_register);
  1794. //return: 0:success 1: fail
  1795. int clk_ops_unregister(struct clk *clk, struct clk_ops *ops)
  1796. {
  1797. if(ops == NULL || IS_CLK_ERR(clk))
  1798. return 0;
  1799. mutex_lock(&clock_ops_lock);
  1800. if(clk->clk_ops == ops){
  1801. if(clk->clk_ops->next == NULL)
  1802. clk->clk_ops = NULL;
  1803. else
  1804. clk->clk_ops = clk->clk_ops->next;
  1805. }
  1806. else if(clk->clk_ops){
  1807. struct clk_ops *p, *p1;
  1808. p = clk->clk_ops->next;
  1809. p1 = clk->clk_ops;
  1810. while(p != NULL && p != ops){
  1811. p1 = p;
  1812. p = p->next;
  1813. }
  1814. if(p == ops)
  1815. p1->next = p->next;
  1816. }
  1817. ops->next = NULL;
  1818. mutex_unlock(&clock_ops_lock);
  1819. return 0;
  1820. }
  1821. EXPORT_SYMBOL(clk_ops_unregister);
  1822. ///FIXME add data later
  1823. #define PLL_CLK_DEFINE(name,msr) \
  1824. static unsigned pll_##name##_data[10]; \
  1825. CLK_DEFINE(pll_##name,xtal,msr,set_##name##_pll, \
  1826. clk_msr_get,NULL,NULL,&pll_##name##_data)
  1827. _Pragma("GCC diagnostic ignored \"-Wdeclaration-after-statement\"");
  1828. #define PLL_RELATION_DEF(child,parent) meson_clk_register(&clk_pll_##child,&clk_##parent)
  1829. #define CLK_PLL_CHILD_DEF(child,parent) meson_clk_register(&clk_##child,&clk_pll_##parent)
  1830. #if 0
  1831. typedef struct {
  1832. unsigned int a9_clk_cntl ;
  1833. unsigned char mali_clk_cntl;
  1834. }clk_cntl_t;
  1835. static int A9_ratechange_before(unsigned long newrate,void* privdata)
  1836. {
  1837. //switch A9 input to xtal
  1838. clk_cntl_t * pdata = (clk_cntl_t*)privdata;
  1839. pdata->mali_clk_cntl=aml_read_reg32(P_HHI_MALI_CLK_CNTL);
  1840. aml_clr_reg32_mask(P_HHI_MALI_CLK_CNTL,(7<<9));//mali switch to crystal
  1841. pdata->a9_clk_cntl = aml_read_reg32(P_HHI_SYS_CPU_CLK_CNTL);
  1842. aml_clr_reg32_mask(P_HHI_SYS_CPU_CLK_CNTL,(1<<7));
  1843. return 0;
  1844. }
  1845. static int A9_ratechange_after(unsigned long newrate,void* privdata,int failed)
  1846. {
  1847. //recovery input pll.
  1848. clk_cntl_t * pdata = (clk_cntl_t *)privdata;
  1849. if((pdata->a9_clk_cntl & (1<<7)) != 0)
  1850. aml_set_reg32_mask(P_HHI_SYS_CPU_CLK_CNTL,(1<<7));
  1851. aml_set_reg32_mask(P_HHI_MALI_CLK_CNTL,(7<<9));//mali switch to fclk_div5
  1852. return 0;
  1853. }
  1854. static int Mali_ratechange_before(unsigned long newrate,void* privdata)
  1855. {
  1856. //switch A9 input to xtal
  1857. clk_cntl_t * pdata = (clk_cntl_t*)privdata;
  1858. aml_clr_reg32_mask(P_HHI_MALI_CLK_CNTL,(7<<9));//mali switch to crystal
  1859. return 0;
  1860. }
  1861. static int Mali_ratechange_after(unsigned long newrate,void* privdata,int failed)
  1862. {
  1863. //recovery input pll.
  1864. clk_cntl_t * pdata = (clk_cntl_t *)privdata;
  1865. #if defined(CONFIG_MALI_CLK_333M)
  1866. aml_set_reg32_mask(P_HHI_MALI_CLK_CNTL,(1<<8)|(6<<9));//mali switch to fclk_div3
  1867. #elif defined(CONFIG_MALI_CLK_400M)
  1868. aml_set_reg32_mask(P_HHI_MALI_CLK_CNTL,(1<<8)|(7<<9));//mali switch to fclk_div5
  1869. #elif defined(CONFIG_MALI_CLK_250M)
  1870. aml_set_reg32_mask(P_HHI_MALI_CLK_CNTL,(1<<8)|(5<<9));//mali switch to fclk_div2
  1871. #endif
  1872. return 0;
  1873. }
  1874. static clk_cntl_t a9_clk_cntl;
  1875. static struct clk_ops a9_clk_ops={
  1876. //.clk_ratechange_before = A9_ratechange_before,
  1877. //.clk_ratechange_after = A9_ratechange_after,
  1878. .privdata = &a9_clk_cntl,
  1879. };
  1880. static clk_cntl_t mali_clk_cntl;
  1881. static struct clk_ops mali_clk_ops={
  1882. //.clk_ratechange_before = Mali_ratechange_before,
  1883. //.clk_ratechange_after = Mali_ratechange_after,
  1884. .privdata = &mali_clk_cntl,
  1885. };
  1886. #endif
  1887. #ifdef CONFIG_CLKTREE_DEBUG
  1888. extern struct clk_lookup * lookup_clk(struct clk* clk);
  1889. void print_clk_name(struct clk* clk)
  1890. {
  1891. struct clk_lookup * p = lookup_clk(clk);
  1892. if(p)
  1893. printk(" %s \n",p->dev_id);
  1894. else
  1895. printk(" unknown \n");
  1896. }
  1897. void dump_child(int nlevel, struct clk* clk)
  1898. {
  1899. if(!IS_CLK_ERR(clk)){
  1900. int i;
  1901. for(i = 0; i < nlevel; i++)
  1902. printk(" ");
  1903. print_clk_name(clk);
  1904. dump_child(nlevel+6,(struct clk*)(clk->child.next));
  1905. {
  1906. struct clk * p = (struct clk*)(clk->sibling.prev);
  1907. while(p){
  1908. for(i = 0; i < nlevel; i++)
  1909. printk(" ");
  1910. print_clk_name(p);
  1911. dump_child(nlevel+6,(struct clk*)(p->child.next));
  1912. p = (struct clk*)(p->sibling.prev);
  1913. }
  1914. p = (struct clk*)(clk->sibling.next);
  1915. while(p){
  1916. for(i = 0; i < nlevel; i++)
  1917. printk(" ");
  1918. print_clk_name(p);
  1919. dump_child(nlevel+6,(struct clk*)(p->child.next));
  1920. p = (struct clk*)(p->sibling.next);
  1921. }
  1922. }
  1923. }
  1924. }
  1925. void dump_clock_tree(struct clk* clk)
  1926. {
  1927. printk("========= dump clock tree==============\n");
  1928. mutex_lock(&clock_ops_lock);
  1929. int nlevel = 0;
  1930. if(!IS_CLK_ERR(clk)){
  1931. print_clk_name(clk);
  1932. dump_child(nlevel + 6,(struct clk*)(clk->child.next));
  1933. { int i;
  1934. struct clk * p = (struct clk*)clk->sibling.prev;
  1935. while(p){
  1936. for(i = 0; i < nlevel; i++)
  1937. printk(" ");
  1938. print_clk_name(p);
  1939. dump_child(nlevel+6,(struct clk*)(p->child.next));
  1940. p = (struct clk*)clk->sibling.prev;
  1941. }
  1942. p = (struct clk*)clk->sibling.next;
  1943. while(p){
  1944. for(i = 0; i < nlevel; i++)
  1945. printk(" ");
  1946. print_clk_name(p);
  1947. dump_child(nlevel+6,(struct clk*)(p->child.next));
  1948. p = (struct clk*)clk->sibling.next;
  1949. }
  1950. }
  1951. }
  1952. mutex_unlock(&clock_ops_lock);
  1953. printk("========= dump clock tree end ==============\n");
  1954. }
  1955. static ssize_t clock_tree_store(struct class *cla, struct class_attribute *attr, char *buf,size_t count)
  1956. {
  1957. char* p = buf;
  1958. char cmd;
  1959. char name[20];
  1960. unsigned long rate = 0;
  1961. int idx = 0;
  1962. if(count < 1)
  1963. return -1;
  1964. while((idx < count) && ((*p == ' ') || (*p == '\t')|| (*p == '\r') || (*p == '\n'))){
  1965. p++;
  1966. idx++;
  1967. }
  1968. if(idx <= count){
  1969. int i;
  1970. cmd = *p;
  1971. p++;
  1972. while((idx < count) && ((*p == ' ') || (*p == '\t')|| (*p == '\r') || (*p == '\n'))){
  1973. p++;
  1974. idx++;
  1975. }
  1976. i = 0;
  1977. while((idx < count) && (*p != ' ') && (*p != '\t') && (*p != '\r') && (*p != '\n')){
  1978. name[i++] = *p;
  1979. p++;
  1980. idx++;
  1981. }
  1982. name[i] = '\0';
  1983. p++;
  1984. while((idx < count) && ((*p == ' ') || (*p == '\t')|| (*p == '\r') || (*p == '\n'))){
  1985. p++;
  1986. idx++;
  1987. }
  1988. if(idx < count){
  1989. int val;
  1990. sscanf(p, "%d", &val);
  1991. rate = val;
  1992. }
  1993. if(cmd == 'r'){
  1994. if(strcmp(name,"tree") == 0){
  1995. struct clk* clk = clk_get_sys("xtal",NULL);
  1996. if(!IS_CLK_ERR(clk))
  1997. dump_clock_tree(clk);
  1998. }
  1999. else{
  2000. struct clk* clk = clk_get_sys(name,NULL);
  2001. if(!IS_CLK_ERR(clk)){
  2002. clk->rate = 0; //enforce update rate
  2003. printk("%s : %lu\n",name,clk_get_rate(clk));
  2004. }
  2005. else
  2006. printk("no %s in tree.\n",name);
  2007. }
  2008. }
  2009. else if(cmd == 'w'){
  2010. struct clk* clk = clk_get_sys(name,NULL);
  2011. if(!IS_CLK_ERR(clk)){
  2012. if(rate < 1000000 || rate >1512000000)
  2013. printk("Invalid rate : %lu\n",rate);
  2014. else{
  2015. if(clk_set_rate(clk,rate) ==0)
  2016. printk("%s = %lu\n",name,rate);
  2017. else
  2018. printk("set %s = %lu failed.\n",name,rate);
  2019. }
  2020. }
  2021. else
  2022. printk("no %s in tree.\n",name);
  2023. }
  2024. else if(cmd == 'o'){
  2025. struct clk* clk = clk_get_sys(name,NULL);
  2026. if(!IS_CLK_ERR(clk)){
  2027. if(clk_enable(clk) ==0)
  2028. printk("%s gate on\n",name);
  2029. else
  2030. printk("gate on %s failed.\n",name);
  2031. }
  2032. else
  2033. printk("no %s in tree.\n",name);
  2034. }
  2035. else if(cmd == 'f'){
  2036. struct clk* clk = clk_get_sys(name,NULL);
  2037. if(!IS_CLK_ERR(clk)){
  2038. clk_disable(clk);
  2039. printk("gate off %s.\n",name);
  2040. }
  2041. else
  2042. printk("no %s in tree.\n",name);
  2043. }
  2044. else
  2045. printk("command:%c invalid.\n",cmd);
  2046. }
  2047. return count;
  2048. }
  2049. static ssize_t clock_tree_show(struct class *cla, struct class_attribute *attr, char *buf)
  2050. {
  2051. printk("Usage:\n");
  2052. printk("1. echo r tree >clkTree ,display the clock tree.\n");
  2053. printk("2. echo r clockname >clkTree ,display the clock rate.\n");
  2054. printk("3. echo w clockname rate >clkTree ,modify the clock rate.\n");
  2055. printk("4. echo o clockname >clkTree ,gate on clock.\n");
  2056. printk("5. echo f clockname >clkTree ,gate off clock.\n");
  2057. printk("Example:\n");
  2058. printk("1. display the clock tree.\n");
  2059. printk(" echo r tree >clkTree\n");
  2060. printk("2. display clk81 rate.\n");
  2061. printk(" echo r clk81 >clkTree\n");
  2062. printk("3. modify sys pll as 792M.\n");
  2063. printk(" echo w pll_sys 792000000 >clkTree\n");
  2064. return 0;
  2065. }
  2066. static struct class_attribute clktree_class_attrs[] = {
  2067. __ATTR(clkTree, S_IRWXU, clock_tree_show, clock_tree_store),
  2068. __ATTR_NULL,
  2069. };
  2070. static struct class meson_clktree_class = {
  2071. .name = "meson_clocktree",
  2072. .class_attrs = clktree_class_attrs,
  2073. };
  2074. #endif
  2075. // -------------------- mali_max sysfs ---------------------
  2076. static ssize_t mali_max_store(struct class *cla, struct class_attribute *attr, char *buf, size_t count)
  2077. {
  2078. unsigned int input;
  2079. int ret;
  2080. ret = sscanf(buf, "%u", &input);
  2081. if (ret != 1 || input > 400000 || input < 83000)
  2082. return -EINVAL;
  2083. mali_max = input;
  2084. return count;
  2085. }
  2086. static ssize_t mali_max_show(struct class *cla, struct class_attribute *attr, char *buf)
  2087. {
  2088. printk("%u\n", mali_max);
  2089. return sprintf(buf, "%d\n", mali_max);
  2090. }
  2091. // -------------------- frequency limit sysfs ---------------------
  2092. static ssize_t freq_limit_store(struct class *cla, struct class_attribute *attr, char *buf, size_t count)
  2093. {
  2094. unsigned int input;
  2095. int ret;
  2096. ret = sscanf(buf, "%u", &input);
  2097. if (ret != 1)
  2098. return -EINVAL;
  2099. freq_limit = input;
  2100. return count;
  2101. }
  2102. static ssize_t freq_limit_show(struct class *cla, struct class_attribute *attr, char *buf)
  2103. {
  2104. printk("%u\n", freq_limit);
  2105. return sprintf(buf, "%d\n", freq_limit);
  2106. }
  2107. static struct class_attribute mali_freq_class_attrs[] = {
  2108. __ATTR(max, S_IRWXU, mali_max_show, mali_max_store),
  2109. __ATTR_NULL,
  2110. };
  2111. static struct class meson_mali_freq_class = {
  2112. .name = "mali_freq",
  2113. .class_attrs = mali_freq_class_attrs,
  2114. };
  2115. static struct class_attribute freq_limit_class_attrs[] = {
  2116. __ATTR(limit, S_IRWXU, freq_limit_show, freq_limit_store),
  2117. __ATTR_NULL,
  2118. };
  2119. static struct class meson_freq_limit_class = {
  2120. .name = "freq_limit",
  2121. .class_attrs = freq_limit_class_attrs,
  2122. };
  2123. // ------------------- /mali_max sysfs ---------------------
  2124. static int __init meson_clock_init(void)
  2125. {
  2126. clkdev_add(&clk_lookup_xtal);
  2127. CLK_DEFINE(pll_ddr,xtal,3,NULL,clk_msr_get,NULL,NULL,NULL);
  2128. PLL_CLK_DEFINE(sys,-1);
  2129. PLL_CLK_DEFINE(vid2,12);
  2130. PLL_CLK_DEFINE(fixed,-1);
  2131. PLL_CLK_DEFINE(hpll,-1);///@todo unknown now
  2132. clk_pll_fixed.msr_mul = 125 *2;
  2133. clk_pll_fixed.msr_div = 3;
  2134. clk_pll_sys.get_rate = clk_get_rate_sys;
  2135. clk_pll_vid2.get_rate = clk_get_rate_vid2;
  2136. clk_pll_hpll.get_rate = clk_get_rate_hpll;
  2137. clk_pll_vid2.max = 1512000000;//1.5G
  2138. clk_pll_vid2.min = 187500000;//187M
  2139. clk_pll_hpll.max = 1512000000;//1.5G
  2140. clk_pll_hpll.min = 187500000;//187M
  2141. clk_pll_sys.max = 1512000000;//1.5G
  2142. clk_pll_sys.min = 187500000;//187M
  2143. clk_pll_ddr.max = 1512000000;//1.5G
  2144. clk_pll_ddr.min = 187500000;//187M
  2145. clk_pll_fixed.max = 2000000000; //2G
  2146. clk_pll_fixed.min = 250000000;//250M
  2147. //create pll tree
  2148. PLL_RELATION_DEF(sys,xtal);
  2149. PLL_RELATION_DEF(ddr,xtal);
  2150. PLL_RELATION_DEF(vid2,xtal);
  2151. PLL_RELATION_DEF(fixed,xtal);
  2152. PLL_RELATION_DEF(hpll,xtal);
  2153. // Add clk81
  2154. CLK_DEFINE(clk81, pll_fixed, 7, clk_set_rate_clk81, clk_msr_get, NULL, NULL, NULL);
  2155. // Add clk81 as pll_fixed's child
  2156. CLK_PLL_CHILD_DEF(clk81, fixed);
  2157. clk_clk81.clk_gate_reg_adr = P_HHI_MPEG_CLK_CNTL;
  2158. clk_clk81.clk_gate_reg_mask = (1<<7);
  2159. clk_clk81.open_irq = 1;
  2160. // Add CPU clock
  2161. CLK_DEFINE(a9_clk, pll_sys, -1, clk_set_rate_a9, clk_get_rate_a9, NULL, NULL, NULL);
  2162. clk_a9_clk.min = setup_a9_clk_min;
  2163. clk_a9_clk.max = setup_a9_clk_max;
  2164. CLK_PLL_CHILD_DEF(a9_clk,sys);
  2165. #ifdef CONFIG_HAVE_ARM_TWD
  2166. static struct clk clk_smp_twd = {
  2167. .set_rate =((void *)0),
  2168. .get_rate = clk_get_rate_smp_twd,
  2169. .enable = NULL,
  2170. .disable = NULL,
  2171. .priv = NULL,
  2172. .parent = NULL,
  2173. .msr = -1
  2174. };
  2175. static struct clk_lookup clk_lookup_smp_twd = {
  2176. .dev_id = "smp_twd",
  2177. .con_id = NULL,
  2178. .clk = &clk_smp_twd
  2179. };
  2180. clkdev_add(&clk_lookup_smp_twd);
  2181. #endif /* CONFIG_HAVE_ARM_TWD */
  2182. // Add GPU clock
  2183. CLK_DEFINE(mali, pll_fixed, 35, clk_set_rate_mali, clk_msr_get, NULL, NULL, &clk_a9_clk);
  2184. clk_mali.min = 111000000;
  2185. clk_mali.max = 400000000;
  2186. clk_mali.enable = clk_enable_mali;
  2187. clk_mali.disable = clk_disable_mali;
  2188. //clk_mali.status = clk_status_mali;
  2189. CLK_PLL_CHILD_DEF(mali, fixed);
  2190. //clk_ops_register(&clk_mali, &mali_clk_ops);
  2191. // Add clk usb0
  2192. CLK_DEFINE(usb0,xtal,4,NULL,clk_msr_get,NULL,NULL,NULL);
  2193. meson_clk_register(&clk_usb0,&clk_xtal);
  2194. clk_usb0.clk_gate_reg_adr = P_USB_ADDR0;
  2195. clk_usb0.clk_gate_reg_mask = (1<<0);
  2196. // Add clk usb1
  2197. CLK_DEFINE(usb1,xtal,5,NULL,clk_msr_get,NULL,NULL,NULL);
  2198. meson_clk_register(&clk_usb1,&clk_xtal);
  2199. clk_usb1.clk_gate_reg_adr = P_USB_ADDR8;
  2200. clk_usb1.clk_gate_reg_mask = (1<<0);
  2201. {
  2202. // Dump clocks
  2203. char *clks[] = {
  2204. "xtal",
  2205. "pll_sys",
  2206. "pll_fixed",
  2207. "pll_vid2",
  2208. "pll_hpll",
  2209. "pll_ddr",
  2210. "a9_clk",
  2211. "clk81",
  2212. "usb0",
  2213. "usb1",
  2214. "smp_twd"
  2215. };
  2216. int i;
  2217. int count = ARRAY_SIZE(clks);
  2218. struct clk *clk;
  2219. for (i = 0; i < count; i++) {
  2220. char *clk_name = clks[i];
  2221. clk = clk_get_sys(clk_name, NULL);
  2222. if (!IS_CLK_ERR(clk))
  2223. printk("clkrate [ %s ] : %lu\n", clk_name, clk_get_rate(clk));
  2224. }
  2225. }
  2226. #ifdef CONFIG_CLKTREE_DEBUG
  2227. class_register(&meson_clktree_class);
  2228. #endif
  2229. class_register(&meson_mali_freq_class);
  2230. class_register(&meson_freq_limit_class);
  2231. return 0;
  2232. }
  2233. /* initialize clocking early to be available later in the boot */
  2234. core_initcall(meson_clock_init);