tegra30_clocks.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  1. /*
  2. * arch/arm/mach-tegra/tegra30_clocks.c
  3. *
  4. * Copyright (c) 2010-2012 NVIDIA CORPORATION. All rights reserved.
  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; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/list.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/delay.h>
  25. #include <linux/err.h>
  26. #include <linux/io.h>
  27. #include <linux/clk.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/syscore_ops.h>
  30. #include <asm/clkdev.h>
  31. #include "clock.h"
  32. #include "fuse.h"
  33. #include "iomap.h"
  34. #include "tegra_cpu_car.h"
  35. #define USE_PLL_LOCK_BITS 0
  36. #define RST_DEVICES_L 0x004
  37. #define RST_DEVICES_H 0x008
  38. #define RST_DEVICES_U 0x00C
  39. #define RST_DEVICES_V 0x358
  40. #define RST_DEVICES_W 0x35C
  41. #define RST_DEVICES_SET_L 0x300
  42. #define RST_DEVICES_CLR_L 0x304
  43. #define RST_DEVICES_SET_V 0x430
  44. #define RST_DEVICES_CLR_V 0x434
  45. #define RST_DEVICES_NUM 5
  46. #define CLK_OUT_ENB_L 0x010
  47. #define CLK_OUT_ENB_H 0x014
  48. #define CLK_OUT_ENB_U 0x018
  49. #define CLK_OUT_ENB_V 0x360
  50. #define CLK_OUT_ENB_W 0x364
  51. #define CLK_OUT_ENB_SET_L 0x320
  52. #define CLK_OUT_ENB_CLR_L 0x324
  53. #define CLK_OUT_ENB_SET_V 0x440
  54. #define CLK_OUT_ENB_CLR_V 0x444
  55. #define CLK_OUT_ENB_NUM 5
  56. #define RST_DEVICES_V_SWR_CPULP_RST_DIS (0x1 << 1)
  57. #define CLK_OUT_ENB_V_CLK_ENB_CPULP_EN (0x1 << 1)
  58. #define PERIPH_CLK_TO_BIT(c) (1 << (c->u.periph.clk_num % 32))
  59. #define PERIPH_CLK_TO_RST_REG(c) \
  60. periph_clk_to_reg((c), RST_DEVICES_L, RST_DEVICES_V, 4)
  61. #define PERIPH_CLK_TO_RST_SET_REG(c) \
  62. periph_clk_to_reg((c), RST_DEVICES_SET_L, RST_DEVICES_SET_V, 8)
  63. #define PERIPH_CLK_TO_RST_CLR_REG(c) \
  64. periph_clk_to_reg((c), RST_DEVICES_CLR_L, RST_DEVICES_CLR_V, 8)
  65. #define PERIPH_CLK_TO_ENB_REG(c) \
  66. periph_clk_to_reg((c), CLK_OUT_ENB_L, CLK_OUT_ENB_V, 4)
  67. #define PERIPH_CLK_TO_ENB_SET_REG(c) \
  68. periph_clk_to_reg((c), CLK_OUT_ENB_SET_L, CLK_OUT_ENB_SET_V, 8)
  69. #define PERIPH_CLK_TO_ENB_CLR_REG(c) \
  70. periph_clk_to_reg((c), CLK_OUT_ENB_CLR_L, CLK_OUT_ENB_CLR_V, 8)
  71. #define CLK_MASK_ARM 0x44
  72. #define MISC_CLK_ENB 0x48
  73. #define OSC_CTRL 0x50
  74. #define OSC_CTRL_OSC_FREQ_MASK (0xF<<28)
  75. #define OSC_CTRL_OSC_FREQ_13MHZ (0x0<<28)
  76. #define OSC_CTRL_OSC_FREQ_19_2MHZ (0x4<<28)
  77. #define OSC_CTRL_OSC_FREQ_12MHZ (0x8<<28)
  78. #define OSC_CTRL_OSC_FREQ_26MHZ (0xC<<28)
  79. #define OSC_CTRL_OSC_FREQ_16_8MHZ (0x1<<28)
  80. #define OSC_CTRL_OSC_FREQ_38_4MHZ (0x5<<28)
  81. #define OSC_CTRL_OSC_FREQ_48MHZ (0x9<<28)
  82. #define OSC_CTRL_MASK (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
  83. #define OSC_CTRL_PLL_REF_DIV_MASK (3<<26)
  84. #define OSC_CTRL_PLL_REF_DIV_1 (0<<26)
  85. #define OSC_CTRL_PLL_REF_DIV_2 (1<<26)
  86. #define OSC_CTRL_PLL_REF_DIV_4 (2<<26)
  87. #define OSC_FREQ_DET 0x58
  88. #define OSC_FREQ_DET_TRIG (1<<31)
  89. #define OSC_FREQ_DET_STATUS 0x5C
  90. #define OSC_FREQ_DET_BUSY (1<<31)
  91. #define OSC_FREQ_DET_CNT_MASK 0xFFFF
  92. #define PERIPH_CLK_SOURCE_I2S1 0x100
  93. #define PERIPH_CLK_SOURCE_EMC 0x19c
  94. #define PERIPH_CLK_SOURCE_OSC 0x1fc
  95. #define PERIPH_CLK_SOURCE_NUM1 \
  96. ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
  97. #define PERIPH_CLK_SOURCE_G3D2 0x3b0
  98. #define PERIPH_CLK_SOURCE_SE 0x42c
  99. #define PERIPH_CLK_SOURCE_NUM2 \
  100. ((PERIPH_CLK_SOURCE_SE - PERIPH_CLK_SOURCE_G3D2) / 4 + 1)
  101. #define AUDIO_DLY_CLK 0x49c
  102. #define AUDIO_SYNC_CLK_SPDIF 0x4b4
  103. #define PERIPH_CLK_SOURCE_NUM3 \
  104. ((AUDIO_SYNC_CLK_SPDIF - AUDIO_DLY_CLK) / 4 + 1)
  105. #define PERIPH_CLK_SOURCE_NUM (PERIPH_CLK_SOURCE_NUM1 + \
  106. PERIPH_CLK_SOURCE_NUM2 + \
  107. PERIPH_CLK_SOURCE_NUM3)
  108. #define CPU_SOFTRST_CTRL 0x380
  109. #define PERIPH_CLK_SOURCE_DIVU71_MASK 0xFF
  110. #define PERIPH_CLK_SOURCE_DIVU16_MASK 0xFFFF
  111. #define PERIPH_CLK_SOURCE_DIV_SHIFT 0
  112. #define PERIPH_CLK_SOURCE_DIVIDLE_SHIFT 8
  113. #define PERIPH_CLK_SOURCE_DIVIDLE_VAL 50
  114. #define PERIPH_CLK_UART_DIV_ENB (1<<24)
  115. #define PERIPH_CLK_VI_SEL_EX_SHIFT 24
  116. #define PERIPH_CLK_VI_SEL_EX_MASK (0x3<<PERIPH_CLK_VI_SEL_EX_SHIFT)
  117. #define PERIPH_CLK_NAND_DIV_EX_ENB (1<<8)
  118. #define PERIPH_CLK_DTV_POLARITY_INV (1<<25)
  119. #define AUDIO_SYNC_SOURCE_MASK 0x0F
  120. #define AUDIO_SYNC_DISABLE_BIT 0x10
  121. #define AUDIO_SYNC_TAP_NIBBLE_SHIFT(c) ((c->reg_shift - 24) * 4)
  122. #define PLL_BASE 0x0
  123. #define PLL_BASE_BYPASS (1<<31)
  124. #define PLL_BASE_ENABLE (1<<30)
  125. #define PLL_BASE_REF_ENABLE (1<<29)
  126. #define PLL_BASE_OVERRIDE (1<<28)
  127. #define PLL_BASE_LOCK (1<<27)
  128. #define PLL_BASE_DIVP_MASK (0x7<<20)
  129. #define PLL_BASE_DIVP_SHIFT 20
  130. #define PLL_BASE_DIVN_MASK (0x3FF<<8)
  131. #define PLL_BASE_DIVN_SHIFT 8
  132. #define PLL_BASE_DIVM_MASK (0x1F)
  133. #define PLL_BASE_DIVM_SHIFT 0
  134. #define PLL_OUT_RATIO_MASK (0xFF<<8)
  135. #define PLL_OUT_RATIO_SHIFT 8
  136. #define PLL_OUT_OVERRIDE (1<<2)
  137. #define PLL_OUT_CLKEN (1<<1)
  138. #define PLL_OUT_RESET_DISABLE (1<<0)
  139. #define PLL_MISC(c) \
  140. (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
  141. #define PLL_MISC_LOCK_ENABLE(c) \
  142. (((c)->flags & (PLLU | PLLD)) ? (1<<22) : (1<<18))
  143. #define PLL_MISC_DCCON_SHIFT 20
  144. #define PLL_MISC_CPCON_SHIFT 8
  145. #define PLL_MISC_CPCON_MASK (0xF<<PLL_MISC_CPCON_SHIFT)
  146. #define PLL_MISC_LFCON_SHIFT 4
  147. #define PLL_MISC_LFCON_MASK (0xF<<PLL_MISC_LFCON_SHIFT)
  148. #define PLL_MISC_VCOCON_SHIFT 0
  149. #define PLL_MISC_VCOCON_MASK (0xF<<PLL_MISC_VCOCON_SHIFT)
  150. #define PLLD_MISC_CLKENABLE (1<<30)
  151. #define PLLU_BASE_POST_DIV (1<<20)
  152. #define PLLD_BASE_DSIB_MUX_SHIFT 25
  153. #define PLLD_BASE_DSIB_MUX_MASK (1<<PLLD_BASE_DSIB_MUX_SHIFT)
  154. #define PLLD_BASE_CSI_CLKENABLE (1<<26)
  155. #define PLLD_MISC_DSI_CLKENABLE (1<<30)
  156. #define PLLD_MISC_DIV_RST (1<<23)
  157. #define PLLD_MISC_DCCON_SHIFT 12
  158. #define PLLDU_LFCON_SET_DIVN 600
  159. /* FIXME: OUT_OF_TABLE_CPCON per pll */
  160. #define OUT_OF_TABLE_CPCON 0x8
  161. #define SUPER_CLK_MUX 0x00
  162. #define SUPER_STATE_SHIFT 28
  163. #define SUPER_STATE_MASK (0xF << SUPER_STATE_SHIFT)
  164. #define SUPER_STATE_STANDBY (0x0 << SUPER_STATE_SHIFT)
  165. #define SUPER_STATE_IDLE (0x1 << SUPER_STATE_SHIFT)
  166. #define SUPER_STATE_RUN (0x2 << SUPER_STATE_SHIFT)
  167. #define SUPER_STATE_IRQ (0x3 << SUPER_STATE_SHIFT)
  168. #define SUPER_STATE_FIQ (0x4 << SUPER_STATE_SHIFT)
  169. #define SUPER_LP_DIV2_BYPASS (0x1 << 16)
  170. #define SUPER_SOURCE_MASK 0xF
  171. #define SUPER_FIQ_SOURCE_SHIFT 12
  172. #define SUPER_IRQ_SOURCE_SHIFT 8
  173. #define SUPER_RUN_SOURCE_SHIFT 4
  174. #define SUPER_IDLE_SOURCE_SHIFT 0
  175. #define SUPER_CLK_DIVIDER 0x04
  176. #define SUPER_CLOCK_DIV_U71_SHIFT 16
  177. #define SUPER_CLOCK_DIV_U71_MASK (0xff << SUPER_CLOCK_DIV_U71_SHIFT)
  178. /* guarantees safe cpu backup */
  179. #define SUPER_CLOCK_DIV_U71_MIN 0x2
  180. #define BUS_CLK_DISABLE (1<<3)
  181. #define BUS_CLK_DIV_MASK 0x3
  182. #define PMC_CTRL 0x0
  183. #define PMC_CTRL_BLINK_ENB (1 << 7)
  184. #define PMC_DPD_PADS_ORIDE 0x1c
  185. #define PMC_DPD_PADS_ORIDE_BLINK_ENB (1 << 20)
  186. #define PMC_BLINK_TIMER_DATA_ON_SHIFT 0
  187. #define PMC_BLINK_TIMER_DATA_ON_MASK 0x7fff
  188. #define PMC_BLINK_TIMER_ENB (1 << 15)
  189. #define PMC_BLINK_TIMER_DATA_OFF_SHIFT 16
  190. #define PMC_BLINK_TIMER_DATA_OFF_MASK 0xffff
  191. #define PMC_PLLP_WB0_OVERRIDE 0xf8
  192. #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE (1 << 12)
  193. #define UTMIP_PLL_CFG2 0x488
  194. #define UTMIP_PLL_CFG2_STABLE_COUNT(x) (((x) & 0xfff) << 6)
  195. #define UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(x) (((x) & 0x3f) << 18)
  196. #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN (1 << 0)
  197. #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN (1 << 2)
  198. #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN (1 << 4)
  199. #define UTMIP_PLL_CFG1 0x484
  200. #define UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
  201. #define UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
  202. #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN (1 << 14)
  203. #define UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN (1 << 12)
  204. #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN (1 << 16)
  205. #define PLLE_BASE_CML_ENABLE (1<<31)
  206. #define PLLE_BASE_ENABLE (1<<30)
  207. #define PLLE_BASE_DIVCML_SHIFT 24
  208. #define PLLE_BASE_DIVCML_MASK (0xf<<PLLE_BASE_DIVCML_SHIFT)
  209. #define PLLE_BASE_DIVP_SHIFT 16
  210. #define PLLE_BASE_DIVP_MASK (0x3f<<PLLE_BASE_DIVP_SHIFT)
  211. #define PLLE_BASE_DIVN_SHIFT 8
  212. #define PLLE_BASE_DIVN_MASK (0xFF<<PLLE_BASE_DIVN_SHIFT)
  213. #define PLLE_BASE_DIVM_SHIFT 0
  214. #define PLLE_BASE_DIVM_MASK (0xFF<<PLLE_BASE_DIVM_SHIFT)
  215. #define PLLE_BASE_DIV_MASK \
  216. (PLLE_BASE_DIVCML_MASK | PLLE_BASE_DIVP_MASK | \
  217. PLLE_BASE_DIVN_MASK | PLLE_BASE_DIVM_MASK)
  218. #define PLLE_BASE_DIV(m, n, p, cml) \
  219. (((cml)<<PLLE_BASE_DIVCML_SHIFT) | ((p)<<PLLE_BASE_DIVP_SHIFT) | \
  220. ((n)<<PLLE_BASE_DIVN_SHIFT) | ((m)<<PLLE_BASE_DIVM_SHIFT))
  221. #define PLLE_MISC_SETUP_BASE_SHIFT 16
  222. #define PLLE_MISC_SETUP_BASE_MASK (0xFFFF<<PLLE_MISC_SETUP_BASE_SHIFT)
  223. #define PLLE_MISC_READY (1<<15)
  224. #define PLLE_MISC_LOCK (1<<11)
  225. #define PLLE_MISC_LOCK_ENABLE (1<<9)
  226. #define PLLE_MISC_SETUP_EX_SHIFT 2
  227. #define PLLE_MISC_SETUP_EX_MASK (0x3<<PLLE_MISC_SETUP_EX_SHIFT)
  228. #define PLLE_MISC_SETUP_MASK \
  229. (PLLE_MISC_SETUP_BASE_MASK | PLLE_MISC_SETUP_EX_MASK)
  230. #define PLLE_MISC_SETUP_VALUE \
  231. ((0x7<<PLLE_MISC_SETUP_BASE_SHIFT) | (0x0<<PLLE_MISC_SETUP_EX_SHIFT))
  232. #define PLLE_SS_CTRL 0x68
  233. #define PLLE_SS_INCINTRV_SHIFT 24
  234. #define PLLE_SS_INCINTRV_MASK (0x3f<<PLLE_SS_INCINTRV_SHIFT)
  235. #define PLLE_SS_INC_SHIFT 16
  236. #define PLLE_SS_INC_MASK (0xff<<PLLE_SS_INC_SHIFT)
  237. #define PLLE_SS_MAX_SHIFT 0
  238. #define PLLE_SS_MAX_MASK (0x1ff<<PLLE_SS_MAX_SHIFT)
  239. #define PLLE_SS_COEFFICIENTS_MASK \
  240. (PLLE_SS_INCINTRV_MASK | PLLE_SS_INC_MASK | PLLE_SS_MAX_MASK)
  241. #define PLLE_SS_COEFFICIENTS_12MHZ \
  242. ((0x18<<PLLE_SS_INCINTRV_SHIFT) | (0x1<<PLLE_SS_INC_SHIFT) | \
  243. (0x24<<PLLE_SS_MAX_SHIFT))
  244. #define PLLE_SS_DISABLE ((1<<12) | (1<<11) | (1<<10))
  245. #define PLLE_AUX 0x48c
  246. #define PLLE_AUX_PLLP_SEL (1<<2)
  247. #define PLLE_AUX_CML_SATA_ENABLE (1<<1)
  248. #define PLLE_AUX_CML_PCIE_ENABLE (1<<0)
  249. #define PMC_SATA_PWRGT 0x1ac
  250. #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE (1<<5)
  251. #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL (1<<4)
  252. #define ROUND_DIVIDER_UP 0
  253. #define ROUND_DIVIDER_DOWN 1
  254. /* FIXME: recommended safety delay after lock is detected */
  255. #define PLL_POST_LOCK_DELAY 100
  256. /* Tegra CPU clock and reset control regs */
  257. #define TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX 0x4c
  258. #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET 0x340
  259. #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR 0x344
  260. #define TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR 0x34c
  261. #define TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS 0x470
  262. #define CPU_CLOCK(cpu) (0x1 << (8 + cpu))
  263. #define CPU_RESET(cpu) (0x1111ul << (cpu))
  264. /**
  265. * Structure defining the fields for USB UTMI clocks Parameters.
  266. */
  267. struct utmi_clk_param {
  268. /* Oscillator Frequency in KHz */
  269. u32 osc_frequency;
  270. /* UTMIP PLL Enable Delay Count */
  271. u8 enable_delay_count;
  272. /* UTMIP PLL Stable count */
  273. u8 stable_count;
  274. /* UTMIP PLL Active delay count */
  275. u8 active_delay_count;
  276. /* UTMIP PLL Xtal frequency count */
  277. u8 xtal_freq_count;
  278. };
  279. static const struct utmi_clk_param utmi_parameters[] = {
  280. {
  281. .osc_frequency = 13000000,
  282. .enable_delay_count = 0x02,
  283. .stable_count = 0x33,
  284. .active_delay_count = 0x05,
  285. .xtal_freq_count = 0x7F
  286. },
  287. {
  288. .osc_frequency = 19200000,
  289. .enable_delay_count = 0x03,
  290. .stable_count = 0x4B,
  291. .active_delay_count = 0x06,
  292. .xtal_freq_count = 0xBB},
  293. {
  294. .osc_frequency = 12000000,
  295. .enable_delay_count = 0x02,
  296. .stable_count = 0x2F,
  297. .active_delay_count = 0x04,
  298. .xtal_freq_count = 0x76
  299. },
  300. {
  301. .osc_frequency = 26000000,
  302. .enable_delay_count = 0x04,
  303. .stable_count = 0x66,
  304. .active_delay_count = 0x09,
  305. .xtal_freq_count = 0xFE
  306. },
  307. {
  308. .osc_frequency = 16800000,
  309. .enable_delay_count = 0x03,
  310. .stable_count = 0x41,
  311. .active_delay_count = 0x0A,
  312. .xtal_freq_count = 0xA4
  313. },
  314. };
  315. static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
  316. static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
  317. static void __iomem *misc_gp_hidrev_base = IO_ADDRESS(TEGRA_APB_MISC_BASE);
  318. #define MISC_GP_HIDREV 0x804
  319. /*
  320. * Some peripheral clocks share an enable bit, so refcount the enable bits
  321. * in registers CLK_ENABLE_L, ... CLK_ENABLE_W
  322. */
  323. static int tegra_periph_clk_enable_refcount[CLK_OUT_ENB_NUM * 32];
  324. #define clk_writel(value, reg) \
  325. __raw_writel(value, reg_clk_base + (reg))
  326. #define clk_readl(reg) \
  327. __raw_readl(reg_clk_base + (reg))
  328. #define pmc_writel(value, reg) \
  329. __raw_writel(value, reg_pmc_base + (reg))
  330. #define pmc_readl(reg) \
  331. __raw_readl(reg_pmc_base + (reg))
  332. #define chipid_readl() \
  333. __raw_readl(misc_gp_hidrev_base + MISC_GP_HIDREV)
  334. #define clk_writel_delay(value, reg) \
  335. do { \
  336. __raw_writel((value), reg_clk_base + (reg)); \
  337. udelay(2); \
  338. } while (0)
  339. static inline int clk_set_div(struct clk_tegra *c, u32 n)
  340. {
  341. struct clk *clk = c->hw.clk;
  342. return clk_set_rate(clk,
  343. (__clk_get_rate(__clk_get_parent(clk)) + n - 1) / n);
  344. }
  345. static inline u32 periph_clk_to_reg(
  346. struct clk_tegra *c, u32 reg_L, u32 reg_V, int offs)
  347. {
  348. u32 reg = c->u.periph.clk_num / 32;
  349. BUG_ON(reg >= RST_DEVICES_NUM);
  350. if (reg < 3)
  351. reg = reg_L + (reg * offs);
  352. else
  353. reg = reg_V + ((reg - 3) * offs);
  354. return reg;
  355. }
  356. static unsigned long clk_measure_input_freq(void)
  357. {
  358. u32 clock_autodetect;
  359. clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
  360. do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
  361. clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
  362. if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
  363. return 12000000;
  364. } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
  365. return 13000000;
  366. } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
  367. return 19200000;
  368. } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
  369. return 26000000;
  370. } else if (clock_autodetect >= 1025 - 3 && clock_autodetect <= 1025 + 3) {
  371. return 16800000;
  372. } else if (clock_autodetect >= 2344 - 3 && clock_autodetect <= 2344 + 3) {
  373. return 38400000;
  374. } else if (clock_autodetect >= 2928 - 3 && clock_autodetect <= 2928 + 3) {
  375. return 48000000;
  376. } else {
  377. pr_err("%s: Unexpected clock autodetect value %d", __func__,
  378. clock_autodetect);
  379. BUG();
  380. return 0;
  381. }
  382. }
  383. static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
  384. u32 flags, u32 round_mode)
  385. {
  386. s64 divider_u71 = parent_rate;
  387. if (!rate)
  388. return -EINVAL;
  389. if (!(flags & DIV_U71_INT))
  390. divider_u71 *= 2;
  391. if (round_mode == ROUND_DIVIDER_UP)
  392. divider_u71 += rate - 1;
  393. do_div(divider_u71, rate);
  394. if (flags & DIV_U71_INT)
  395. divider_u71 *= 2;
  396. if (divider_u71 - 2 < 0)
  397. return 0;
  398. if (divider_u71 - 2 > 255)
  399. return -EINVAL;
  400. return divider_u71 - 2;
  401. }
  402. static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
  403. {
  404. s64 divider_u16;
  405. divider_u16 = parent_rate;
  406. if (!rate)
  407. return -EINVAL;
  408. divider_u16 += rate - 1;
  409. do_div(divider_u16, rate);
  410. if (divider_u16 - 1 < 0)
  411. return 0;
  412. if (divider_u16 - 1 > 0xFFFF)
  413. return -EINVAL;
  414. return divider_u16 - 1;
  415. }
  416. static unsigned long tegra30_clk_fixed_recalc_rate(struct clk_hw *hw,
  417. unsigned long parent_rate)
  418. {
  419. return to_clk_tegra(hw)->fixed_rate;
  420. }
  421. struct clk_ops tegra30_clk_32k_ops = {
  422. .recalc_rate = tegra30_clk_fixed_recalc_rate,
  423. };
  424. /* clk_m functions */
  425. static unsigned long tegra30_clk_m_recalc_rate(struct clk_hw *hw,
  426. unsigned long parent_rate)
  427. {
  428. if (!to_clk_tegra(hw)->fixed_rate)
  429. to_clk_tegra(hw)->fixed_rate = clk_measure_input_freq();
  430. return to_clk_tegra(hw)->fixed_rate;
  431. }
  432. static void tegra30_clk_m_init(struct clk_hw *hw)
  433. {
  434. u32 osc_ctrl = clk_readl(OSC_CTRL);
  435. u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
  436. u32 pll_ref_div = osc_ctrl & OSC_CTRL_PLL_REF_DIV_MASK;
  437. switch (to_clk_tegra(hw)->fixed_rate) {
  438. case 12000000:
  439. auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
  440. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  441. break;
  442. case 13000000:
  443. auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
  444. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  445. break;
  446. case 19200000:
  447. auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
  448. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  449. break;
  450. case 26000000:
  451. auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
  452. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  453. break;
  454. case 16800000:
  455. auto_clock_control |= OSC_CTRL_OSC_FREQ_16_8MHZ;
  456. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  457. break;
  458. case 38400000:
  459. auto_clock_control |= OSC_CTRL_OSC_FREQ_38_4MHZ;
  460. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_2);
  461. break;
  462. case 48000000:
  463. auto_clock_control |= OSC_CTRL_OSC_FREQ_48MHZ;
  464. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_4);
  465. break;
  466. default:
  467. pr_err("%s: Unexpected clock rate %ld", __func__,
  468. to_clk_tegra(hw)->fixed_rate);
  469. BUG();
  470. }
  471. clk_writel(auto_clock_control, OSC_CTRL);
  472. }
  473. struct clk_ops tegra30_clk_m_ops = {
  474. .init = tegra30_clk_m_init,
  475. .recalc_rate = tegra30_clk_m_recalc_rate,
  476. };
  477. static unsigned long tegra30_clk_m_div_recalc_rate(struct clk_hw *hw,
  478. unsigned long parent_rate)
  479. {
  480. struct clk_tegra *c = to_clk_tegra(hw);
  481. u64 rate = parent_rate;
  482. if (c->mul != 0 && c->div != 0) {
  483. rate *= c->mul;
  484. rate += c->div - 1; /* round up */
  485. do_div(rate, c->div);
  486. }
  487. return rate;
  488. }
  489. struct clk_ops tegra_clk_m_div_ops = {
  490. .recalc_rate = tegra30_clk_m_div_recalc_rate,
  491. };
  492. /* PLL reference divider functions */
  493. static unsigned long tegra30_pll_ref_recalc_rate(struct clk_hw *hw,
  494. unsigned long parent_rate)
  495. {
  496. struct clk_tegra *c = to_clk_tegra(hw);
  497. unsigned long rate = parent_rate;
  498. u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
  499. switch (pll_ref_div) {
  500. case OSC_CTRL_PLL_REF_DIV_1:
  501. c->div = 1;
  502. break;
  503. case OSC_CTRL_PLL_REF_DIV_2:
  504. c->div = 2;
  505. break;
  506. case OSC_CTRL_PLL_REF_DIV_4:
  507. c->div = 4;
  508. break;
  509. default:
  510. pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
  511. BUG();
  512. }
  513. c->mul = 1;
  514. if (c->mul != 0 && c->div != 0) {
  515. rate *= c->mul;
  516. rate += c->div - 1; /* round up */
  517. do_div(rate, c->div);
  518. }
  519. return rate;
  520. }
  521. struct clk_ops tegra_pll_ref_ops = {
  522. .recalc_rate = tegra30_pll_ref_recalc_rate,
  523. };
  524. /* super clock functions */
  525. /* "super clocks" on tegra30 have two-stage muxes, fractional 7.1 divider and
  526. * clock skipping super divider. We will ignore the clock skipping divider,
  527. * since we can't lower the voltage when using the clock skip, but we can if
  528. * we lower the PLL frequency. We will use 7.1 divider for CPU super-clock
  529. * only when its parent is a fixed rate PLL, since we can't change PLL rate
  530. * in this case.
  531. */
  532. static void tegra30_super_clk_init(struct clk_hw *hw)
  533. {
  534. struct clk_tegra *c = to_clk_tegra(hw);
  535. struct clk_tegra *p =
  536. to_clk_tegra(__clk_get_hw(__clk_get_parent(hw->clk)));
  537. c->state = ON;
  538. if (c->flags & DIV_U71) {
  539. /* Init safe 7.1 divider value (does not affect PLLX path) */
  540. clk_writel(SUPER_CLOCK_DIV_U71_MIN << SUPER_CLOCK_DIV_U71_SHIFT,
  541. c->reg + SUPER_CLK_DIVIDER);
  542. c->mul = 2;
  543. c->div = 2;
  544. if (!(p->flags & PLLX))
  545. c->div += SUPER_CLOCK_DIV_U71_MIN;
  546. } else
  547. clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
  548. }
  549. static u8 tegra30_super_clk_get_parent(struct clk_hw *hw)
  550. {
  551. struct clk_tegra *c = to_clk_tegra(hw);
  552. u32 val;
  553. int source;
  554. int shift;
  555. val = clk_readl(c->reg + SUPER_CLK_MUX);
  556. BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
  557. ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
  558. shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
  559. SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
  560. source = (val >> shift) & SUPER_SOURCE_MASK;
  561. if (c->flags & DIV_2)
  562. source |= val & SUPER_LP_DIV2_BYPASS;
  563. return source;
  564. }
  565. static int tegra30_super_clk_set_parent(struct clk_hw *hw, u8 index)
  566. {
  567. struct clk_tegra *c = to_clk_tegra(hw);
  568. struct clk_tegra *p =
  569. to_clk_tegra(__clk_get_hw(clk_get_parent(hw->clk)));
  570. u32 val;
  571. int shift;
  572. val = clk_readl(c->reg + SUPER_CLK_MUX);
  573. BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
  574. ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
  575. shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
  576. SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
  577. /* For LP mode super-clock switch between PLLX direct
  578. and divided-by-2 outputs is allowed only when other
  579. than PLLX clock source is current parent */
  580. if ((c->flags & DIV_2) && (p->flags & PLLX) &&
  581. ((index ^ val) & SUPER_LP_DIV2_BYPASS)) {
  582. if (p->flags & PLLX)
  583. return -EINVAL;
  584. val ^= SUPER_LP_DIV2_BYPASS;
  585. clk_writel_delay(val, c->reg);
  586. }
  587. val &= ~(SUPER_SOURCE_MASK << shift);
  588. val |= (index & SUPER_SOURCE_MASK) << shift;
  589. /* 7.1 divider for CPU super-clock does not affect
  590. PLLX path */
  591. if (c->flags & DIV_U71) {
  592. u32 div = 0;
  593. if (!(p->flags & PLLX)) {
  594. div = clk_readl(c->reg +
  595. SUPER_CLK_DIVIDER);
  596. div &= SUPER_CLOCK_DIV_U71_MASK;
  597. div >>= SUPER_CLOCK_DIV_U71_SHIFT;
  598. }
  599. c->div = div + 2;
  600. c->mul = 2;
  601. }
  602. clk_writel_delay(val, c->reg);
  603. return 0;
  604. }
  605. /*
  606. * Do not use super clocks "skippers", since dividing using a clock skipper
  607. * does not allow the voltage to be scaled down. Instead adjust the rate of
  608. * the parent clock. This requires that the parent of a super clock have no
  609. * other children, otherwise the rate will change underneath the other
  610. * children. Special case: if fixed rate PLL is CPU super clock parent the
  611. * rate of this PLL can't be changed, and it has many other children. In
  612. * this case use 7.1 fractional divider to adjust the super clock rate.
  613. */
  614. static int tegra30_super_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  615. unsigned long parent_rate)
  616. {
  617. struct clk_tegra *c = to_clk_tegra(hw);
  618. struct clk *parent = __clk_get_parent(hw->clk);
  619. struct clk_tegra *cparent = to_clk_tegra(__clk_get_hw(parent));
  620. if ((c->flags & DIV_U71) && (cparent->flags & PLL_FIXED)) {
  621. int div = clk_div71_get_divider(parent_rate,
  622. rate, c->flags, ROUND_DIVIDER_DOWN);
  623. div = max(div, SUPER_CLOCK_DIV_U71_MIN);
  624. clk_writel(div << SUPER_CLOCK_DIV_U71_SHIFT,
  625. c->reg + SUPER_CLK_DIVIDER);
  626. c->div = div + 2;
  627. c->mul = 2;
  628. return 0;
  629. }
  630. return 0;
  631. }
  632. static unsigned long tegra30_super_clk_recalc_rate(struct clk_hw *hw,
  633. unsigned long parent_rate)
  634. {
  635. struct clk_tegra *c = to_clk_tegra(hw);
  636. u64 rate = parent_rate;
  637. if (c->mul != 0 && c->div != 0) {
  638. rate *= c->mul;
  639. rate += c->div - 1; /* round up */
  640. do_div(rate, c->div);
  641. }
  642. return rate;
  643. }
  644. static long tegra30_super_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  645. unsigned long *prate)
  646. {
  647. struct clk_tegra *c = to_clk_tegra(hw);
  648. struct clk *parent = __clk_get_parent(hw->clk);
  649. struct clk_tegra *cparent = to_clk_tegra(__clk_get_hw(parent));
  650. int mul = 2;
  651. int div;
  652. if ((c->flags & DIV_U71) && (cparent->flags & PLL_FIXED)) {
  653. div = clk_div71_get_divider(*prate,
  654. rate, c->flags, ROUND_DIVIDER_DOWN);
  655. div = max(div, SUPER_CLOCK_DIV_U71_MIN) + 2;
  656. rate = *prate * mul;
  657. rate += div - 1; /* round up */
  658. do_div(rate, c->div);
  659. return rate;
  660. }
  661. return *prate;
  662. }
  663. struct clk_ops tegra30_super_ops = {
  664. .init = tegra30_super_clk_init,
  665. .set_parent = tegra30_super_clk_set_parent,
  666. .get_parent = tegra30_super_clk_get_parent,
  667. .recalc_rate = tegra30_super_clk_recalc_rate,
  668. .round_rate = tegra30_super_clk_round_rate,
  669. .set_rate = tegra30_super_clk_set_rate,
  670. };
  671. static unsigned long tegra30_twd_clk_recalc_rate(struct clk_hw *hw,
  672. unsigned long parent_rate)
  673. {
  674. struct clk_tegra *c = to_clk_tegra(hw);
  675. u64 rate = parent_rate;
  676. if (c->mul != 0 && c->div != 0) {
  677. rate *= c->mul;
  678. rate += c->div - 1; /* round up */
  679. do_div(rate, c->div);
  680. }
  681. return rate;
  682. }
  683. struct clk_ops tegra30_twd_ops = {
  684. .recalc_rate = tegra30_twd_clk_recalc_rate,
  685. };
  686. /* Blink output functions */
  687. static int tegra30_blink_clk_is_enabled(struct clk_hw *hw)
  688. {
  689. struct clk_tegra *c = to_clk_tegra(hw);
  690. u32 val;
  691. val = pmc_readl(PMC_CTRL);
  692. c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
  693. return c->state;
  694. }
  695. static int tegra30_blink_clk_enable(struct clk_hw *hw)
  696. {
  697. u32 val;
  698. val = pmc_readl(PMC_DPD_PADS_ORIDE);
  699. pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
  700. val = pmc_readl(PMC_CTRL);
  701. pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
  702. return 0;
  703. }
  704. static void tegra30_blink_clk_disable(struct clk_hw *hw)
  705. {
  706. u32 val;
  707. val = pmc_readl(PMC_CTRL);
  708. pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
  709. val = pmc_readl(PMC_DPD_PADS_ORIDE);
  710. pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
  711. }
  712. static int tegra30_blink_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  713. unsigned long parent_rate)
  714. {
  715. struct clk_tegra *c = to_clk_tegra(hw);
  716. if (rate >= parent_rate) {
  717. c->div = 1;
  718. pmc_writel(0, c->reg);
  719. } else {
  720. unsigned int on_off;
  721. u32 val;
  722. on_off = DIV_ROUND_UP(parent_rate / 8, rate);
  723. c->div = on_off * 8;
  724. val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
  725. PMC_BLINK_TIMER_DATA_ON_SHIFT;
  726. on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
  727. on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
  728. val |= on_off;
  729. val |= PMC_BLINK_TIMER_ENB;
  730. pmc_writel(val, c->reg);
  731. }
  732. return 0;
  733. }
  734. static unsigned long tegra30_blink_clk_recalc_rate(struct clk_hw *hw,
  735. unsigned long parent_rate)
  736. {
  737. struct clk_tegra *c = to_clk_tegra(hw);
  738. u64 rate = parent_rate;
  739. u32 val;
  740. u32 mul;
  741. u32 div;
  742. u32 on_off;
  743. mul = 1;
  744. val = pmc_readl(c->reg);
  745. if (val & PMC_BLINK_TIMER_ENB) {
  746. on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
  747. PMC_BLINK_TIMER_DATA_ON_MASK;
  748. val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
  749. val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
  750. on_off += val;
  751. /* each tick in the blink timer is 4 32KHz clocks */
  752. div = on_off * 4;
  753. } else {
  754. div = 1;
  755. }
  756. if (mul != 0 && div != 0) {
  757. rate *= mul;
  758. rate += div - 1; /* round up */
  759. do_div(rate, div);
  760. }
  761. return rate;
  762. }
  763. static long tegra30_blink_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  764. unsigned long *prate)
  765. {
  766. int div;
  767. int mul;
  768. long round_rate = *prate;
  769. mul = 1;
  770. if (rate >= *prate) {
  771. div = 1;
  772. } else {
  773. div = DIV_ROUND_UP(*prate / 8, rate);
  774. div *= 8;
  775. }
  776. round_rate *= mul;
  777. round_rate += div - 1;
  778. do_div(round_rate, div);
  779. return round_rate;
  780. }
  781. struct clk_ops tegra30_blink_clk_ops = {
  782. .is_enabled = tegra30_blink_clk_is_enabled,
  783. .enable = tegra30_blink_clk_enable,
  784. .disable = tegra30_blink_clk_disable,
  785. .recalc_rate = tegra30_blink_clk_recalc_rate,
  786. .round_rate = tegra30_blink_clk_round_rate,
  787. .set_rate = tegra30_blink_clk_set_rate,
  788. };
  789. static void tegra30_utmi_param_configure(struct clk_hw *hw)
  790. {
  791. unsigned long main_rate =
  792. __clk_get_rate(__clk_get_parent(__clk_get_parent(hw->clk)));
  793. u32 reg;
  794. int i;
  795. for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
  796. if (main_rate == utmi_parameters[i].osc_frequency)
  797. break;
  798. }
  799. if (i >= ARRAY_SIZE(utmi_parameters)) {
  800. pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
  801. return;
  802. }
  803. reg = clk_readl(UTMIP_PLL_CFG2);
  804. /* Program UTMIP PLL stable and active counts */
  805. /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
  806. reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
  807. reg |= UTMIP_PLL_CFG2_STABLE_COUNT(
  808. utmi_parameters[i].stable_count);
  809. reg &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
  810. reg |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(
  811. utmi_parameters[i].active_delay_count);
  812. /* Remove power downs from UTMIP PLL control bits */
  813. reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
  814. reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
  815. reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
  816. clk_writel(reg, UTMIP_PLL_CFG2);
  817. /* Program UTMIP PLL delay and oscillator frequency counts */
  818. reg = clk_readl(UTMIP_PLL_CFG1);
  819. reg &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
  820. reg |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(
  821. utmi_parameters[i].enable_delay_count);
  822. reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
  823. reg |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(
  824. utmi_parameters[i].xtal_freq_count);
  825. /* Remove power downs from UTMIP PLL control bits */
  826. reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
  827. reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
  828. reg &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
  829. clk_writel(reg, UTMIP_PLL_CFG1);
  830. }
  831. /* PLL Functions */
  832. static int tegra30_pll_clk_wait_for_lock(struct clk_tegra *c, u32 lock_reg,
  833. u32 lock_bit)
  834. {
  835. int ret = 0;
  836. #if USE_PLL_LOCK_BITS
  837. int i;
  838. for (i = 0; i < c->u.pll.lock_delay; i++) {
  839. if (clk_readl(lock_reg) & lock_bit) {
  840. udelay(PLL_POST_LOCK_DELAY);
  841. return 0;
  842. }
  843. udelay(2); /* timeout = 2 * lock time */
  844. }
  845. pr_err("Timed out waiting for lock bit on pll %s",
  846. __clk_get_name(hw->clk));
  847. ret = -1;
  848. #else
  849. udelay(c->u.pll.lock_delay);
  850. #endif
  851. return ret;
  852. }
  853. static int tegra30_pll_clk_is_enabled(struct clk_hw *hw)
  854. {
  855. struct clk_tegra *c = to_clk_tegra(hw);
  856. u32 val = clk_readl(c->reg + PLL_BASE);
  857. c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
  858. return c->state;
  859. }
  860. static void tegra30_pll_clk_init(struct clk_hw *hw)
  861. {
  862. struct clk_tegra *c = to_clk_tegra(hw);
  863. if (c->flags & PLLU)
  864. tegra30_utmi_param_configure(hw);
  865. }
  866. static int tegra30_pll_clk_enable(struct clk_hw *hw)
  867. {
  868. struct clk_tegra *c = to_clk_tegra(hw);
  869. u32 val;
  870. pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
  871. #if USE_PLL_LOCK_BITS
  872. val = clk_readl(c->reg + PLL_MISC(c));
  873. val |= PLL_MISC_LOCK_ENABLE(c);
  874. clk_writel(val, c->reg + PLL_MISC(c));
  875. #endif
  876. val = clk_readl(c->reg + PLL_BASE);
  877. val &= ~PLL_BASE_BYPASS;
  878. val |= PLL_BASE_ENABLE;
  879. clk_writel(val, c->reg + PLL_BASE);
  880. if (c->flags & PLLM) {
  881. val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
  882. val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
  883. pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
  884. }
  885. tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
  886. return 0;
  887. }
  888. static void tegra30_pll_clk_disable(struct clk_hw *hw)
  889. {
  890. struct clk_tegra *c = to_clk_tegra(hw);
  891. u32 val;
  892. pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
  893. val = clk_readl(c->reg);
  894. val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
  895. clk_writel(val, c->reg);
  896. if (c->flags & PLLM) {
  897. val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
  898. val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
  899. pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
  900. }
  901. }
  902. static int tegra30_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  903. unsigned long parent_rate)
  904. {
  905. struct clk_tegra *c = to_clk_tegra(hw);
  906. u32 val, p_div, old_base;
  907. unsigned long input_rate;
  908. const struct clk_pll_freq_table *sel;
  909. struct clk_pll_freq_table cfg;
  910. if (c->flags & PLL_FIXED) {
  911. int ret = 0;
  912. if (rate != c->u.pll.fixed_rate) {
  913. pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
  914. __func__, __clk_get_name(hw->clk),
  915. c->u.pll.fixed_rate, rate);
  916. ret = -EINVAL;
  917. }
  918. return ret;
  919. }
  920. if (c->flags & PLLM) {
  921. if (rate != __clk_get_rate(hw->clk)) {
  922. pr_err("%s: Can not change memory %s rate in flight\n",
  923. __func__, __clk_get_name(hw->clk));
  924. return -EINVAL;
  925. }
  926. }
  927. p_div = 0;
  928. input_rate = parent_rate;
  929. /* Check if the target rate is tabulated */
  930. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  931. if (sel->input_rate == input_rate && sel->output_rate == rate) {
  932. if (c->flags & PLLU) {
  933. BUG_ON(sel->p < 1 || sel->p > 2);
  934. if (sel->p == 1)
  935. p_div = PLLU_BASE_POST_DIV;
  936. } else {
  937. BUG_ON(sel->p < 1);
  938. for (val = sel->p; val > 1; val >>= 1)
  939. p_div++;
  940. p_div <<= PLL_BASE_DIVP_SHIFT;
  941. }
  942. break;
  943. }
  944. }
  945. /* Configure out-of-table rate */
  946. if (sel->input_rate == 0) {
  947. unsigned long cfreq;
  948. BUG_ON(c->flags & PLLU);
  949. sel = &cfg;
  950. switch (input_rate) {
  951. case 12000000:
  952. case 26000000:
  953. cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
  954. break;
  955. case 13000000:
  956. cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
  957. break;
  958. case 16800000:
  959. case 19200000:
  960. cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
  961. break;
  962. default:
  963. pr_err("%s: Unexpected reference rate %lu\n",
  964. __func__, input_rate);
  965. BUG();
  966. }
  967. /* Raise VCO to guarantee 0.5% accuracy */
  968. for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
  969. cfg.output_rate <<= 1)
  970. p_div++;
  971. cfg.p = 0x1 << p_div;
  972. cfg.m = input_rate / cfreq;
  973. cfg.n = cfg.output_rate / cfreq;
  974. cfg.cpcon = OUT_OF_TABLE_CPCON;
  975. if ((cfg.m > (PLL_BASE_DIVM_MASK >> PLL_BASE_DIVM_SHIFT)) ||
  976. (cfg.n > (PLL_BASE_DIVN_MASK >> PLL_BASE_DIVN_SHIFT)) ||
  977. (p_div > (PLL_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT)) ||
  978. (cfg.output_rate > c->u.pll.vco_max)) {
  979. pr_err("%s: Failed to set %s out-of-table rate %lu\n",
  980. __func__, __clk_get_name(hw->clk), rate);
  981. return -EINVAL;
  982. }
  983. p_div <<= PLL_BASE_DIVP_SHIFT;
  984. }
  985. c->mul = sel->n;
  986. c->div = sel->m * sel->p;
  987. old_base = val = clk_readl(c->reg + PLL_BASE);
  988. val &= ~(PLL_BASE_DIVM_MASK | PLL_BASE_DIVN_MASK |
  989. ((c->flags & PLLU) ? PLLU_BASE_POST_DIV : PLL_BASE_DIVP_MASK));
  990. val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
  991. (sel->n << PLL_BASE_DIVN_SHIFT) | p_div;
  992. if (val == old_base)
  993. return 0;
  994. if (c->state == ON) {
  995. tegra30_pll_clk_disable(hw);
  996. val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
  997. }
  998. clk_writel(val, c->reg + PLL_BASE);
  999. if (c->flags & PLL_HAS_CPCON) {
  1000. val = clk_readl(c->reg + PLL_MISC(c));
  1001. val &= ~PLL_MISC_CPCON_MASK;
  1002. val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
  1003. if (c->flags & (PLLU | PLLD)) {
  1004. val &= ~PLL_MISC_LFCON_MASK;
  1005. if (sel->n >= PLLDU_LFCON_SET_DIVN)
  1006. val |= 0x1 << PLL_MISC_LFCON_SHIFT;
  1007. } else if (c->flags & (PLLX | PLLM)) {
  1008. val &= ~(0x1 << PLL_MISC_DCCON_SHIFT);
  1009. if (rate >= (c->u.pll.vco_max >> 1))
  1010. val |= 0x1 << PLL_MISC_DCCON_SHIFT;
  1011. }
  1012. clk_writel(val, c->reg + PLL_MISC(c));
  1013. }
  1014. if (c->state == ON)
  1015. tegra30_pll_clk_enable(hw);
  1016. c->u.pll.fixed_rate = rate;
  1017. return 0;
  1018. }
  1019. static long tegra30_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  1020. unsigned long *prate)
  1021. {
  1022. struct clk_tegra *c = to_clk_tegra(hw);
  1023. unsigned long input_rate = *prate;
  1024. u64 output_rate = *prate;
  1025. const struct clk_pll_freq_table *sel;
  1026. struct clk_pll_freq_table cfg;
  1027. int mul;
  1028. int div;
  1029. u32 p_div;
  1030. u32 val;
  1031. if (c->flags & PLL_FIXED)
  1032. return c->u.pll.fixed_rate;
  1033. if (c->flags & PLLM)
  1034. return __clk_get_rate(hw->clk);
  1035. p_div = 0;
  1036. /* Check if the target rate is tabulated */
  1037. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  1038. if (sel->input_rate == input_rate && sel->output_rate == rate) {
  1039. if (c->flags & PLLU) {
  1040. BUG_ON(sel->p < 1 || sel->p > 2);
  1041. if (sel->p == 1)
  1042. p_div = PLLU_BASE_POST_DIV;
  1043. } else {
  1044. BUG_ON(sel->p < 1);
  1045. for (val = sel->p; val > 1; val >>= 1)
  1046. p_div++;
  1047. p_div <<= PLL_BASE_DIVP_SHIFT;
  1048. }
  1049. break;
  1050. }
  1051. }
  1052. if (sel->input_rate == 0) {
  1053. unsigned long cfreq;
  1054. BUG_ON(c->flags & PLLU);
  1055. sel = &cfg;
  1056. switch (input_rate) {
  1057. case 12000000:
  1058. case 26000000:
  1059. cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
  1060. break;
  1061. case 13000000:
  1062. cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
  1063. break;
  1064. case 16800000:
  1065. case 19200000:
  1066. cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
  1067. break;
  1068. default:
  1069. pr_err("%s: Unexpected reference rate %lu\n",
  1070. __func__, input_rate);
  1071. BUG();
  1072. }
  1073. /* Raise VCO to guarantee 0.5% accuracy */
  1074. for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
  1075. cfg.output_rate <<= 1)
  1076. p_div++;
  1077. cfg.p = 0x1 << p_div;
  1078. cfg.m = input_rate / cfreq;
  1079. cfg.n = cfg.output_rate / cfreq;
  1080. }
  1081. mul = sel->n;
  1082. div = sel->m * sel->p;
  1083. output_rate *= mul;
  1084. output_rate += div - 1; /* round up */
  1085. do_div(output_rate, div);
  1086. return output_rate;
  1087. }
  1088. static unsigned long tegra30_pll_recalc_rate(struct clk_hw *hw,
  1089. unsigned long parent_rate)
  1090. {
  1091. struct clk_tegra *c = to_clk_tegra(hw);
  1092. u64 rate = parent_rate;
  1093. u32 val = clk_readl(c->reg + PLL_BASE);
  1094. if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
  1095. const struct clk_pll_freq_table *sel;
  1096. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  1097. if (sel->input_rate == parent_rate &&
  1098. sel->output_rate == c->u.pll.fixed_rate) {
  1099. c->mul = sel->n;
  1100. c->div = sel->m * sel->p;
  1101. break;
  1102. }
  1103. }
  1104. pr_err("Clock %s has unknown fixed frequency\n",
  1105. __clk_get_name(hw->clk));
  1106. BUG();
  1107. } else if (val & PLL_BASE_BYPASS) {
  1108. c->mul = 1;
  1109. c->div = 1;
  1110. } else {
  1111. c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
  1112. c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
  1113. if (c->flags & PLLU)
  1114. c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
  1115. else
  1116. c->div *= (0x1 << ((val & PLL_BASE_DIVP_MASK) >>
  1117. PLL_BASE_DIVP_SHIFT));
  1118. }
  1119. if (c->mul != 0 && c->div != 0) {
  1120. rate *= c->mul;
  1121. rate += c->div - 1; /* round up */
  1122. do_div(rate, c->div);
  1123. }
  1124. return rate;
  1125. }
  1126. struct clk_ops tegra30_pll_ops = {
  1127. .is_enabled = tegra30_pll_clk_is_enabled,
  1128. .init = tegra30_pll_clk_init,
  1129. .enable = tegra30_pll_clk_enable,
  1130. .disable = tegra30_pll_clk_disable,
  1131. .recalc_rate = tegra30_pll_recalc_rate,
  1132. .round_rate = tegra30_pll_round_rate,
  1133. .set_rate = tegra30_pll_clk_set_rate,
  1134. };
  1135. int tegra30_plld_clk_cfg_ex(struct clk_hw *hw,
  1136. enum tegra_clk_ex_param p, u32 setting)
  1137. {
  1138. struct clk_tegra *c = to_clk_tegra(hw);
  1139. u32 val, mask, reg;
  1140. switch (p) {
  1141. case TEGRA_CLK_PLLD_CSI_OUT_ENB:
  1142. mask = PLLD_BASE_CSI_CLKENABLE;
  1143. reg = c->reg + PLL_BASE;
  1144. break;
  1145. case TEGRA_CLK_PLLD_DSI_OUT_ENB:
  1146. mask = PLLD_MISC_DSI_CLKENABLE;
  1147. reg = c->reg + PLL_MISC(c);
  1148. break;
  1149. case TEGRA_CLK_PLLD_MIPI_MUX_SEL:
  1150. if (!(c->flags & PLL_ALT_MISC_REG)) {
  1151. mask = PLLD_BASE_DSIB_MUX_MASK;
  1152. reg = c->reg + PLL_BASE;
  1153. break;
  1154. }
  1155. /* fall through - error since PLLD2 does not have MUX_SEL control */
  1156. default:
  1157. return -EINVAL;
  1158. }
  1159. val = clk_readl(reg);
  1160. if (setting)
  1161. val |= mask;
  1162. else
  1163. val &= ~mask;
  1164. clk_writel(val, reg);
  1165. return 0;
  1166. }
  1167. static int tegra30_plle_clk_is_enabled(struct clk_hw *hw)
  1168. {
  1169. struct clk_tegra *c = to_clk_tegra(hw);
  1170. u32 val;
  1171. val = clk_readl(c->reg + PLL_BASE);
  1172. c->state = (val & PLLE_BASE_ENABLE) ? ON : OFF;
  1173. return c->state;
  1174. }
  1175. static void tegra30_plle_clk_disable(struct clk_hw *hw)
  1176. {
  1177. struct clk_tegra *c = to_clk_tegra(hw);
  1178. u32 val;
  1179. val = clk_readl(c->reg + PLL_BASE);
  1180. val &= ~(PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
  1181. clk_writel(val, c->reg + PLL_BASE);
  1182. }
  1183. static void tegra30_plle_training(struct clk_tegra *c)
  1184. {
  1185. u32 val;
  1186. /* PLLE is already disabled, and setup cleared;
  1187. * create falling edge on PLLE IDDQ input */
  1188. val = pmc_readl(PMC_SATA_PWRGT);
  1189. val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
  1190. pmc_writel(val, PMC_SATA_PWRGT);
  1191. val = pmc_readl(PMC_SATA_PWRGT);
  1192. val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
  1193. pmc_writel(val, PMC_SATA_PWRGT);
  1194. val = pmc_readl(PMC_SATA_PWRGT);
  1195. val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
  1196. pmc_writel(val, PMC_SATA_PWRGT);
  1197. do {
  1198. val = clk_readl(c->reg + PLL_MISC(c));
  1199. } while (!(val & PLLE_MISC_READY));
  1200. }
  1201. static int tegra30_plle_configure(struct clk_hw *hw, bool force_training)
  1202. {
  1203. struct clk_tegra *c = to_clk_tegra(hw);
  1204. struct clk *parent = __clk_get_parent(hw->clk);
  1205. const struct clk_pll_freq_table *sel;
  1206. u32 val;
  1207. unsigned long rate = c->u.pll.fixed_rate;
  1208. unsigned long input_rate = __clk_get_rate(parent);
  1209. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  1210. if (sel->input_rate == input_rate && sel->output_rate == rate)
  1211. break;
  1212. }
  1213. if (sel->input_rate == 0)
  1214. return -ENOSYS;
  1215. /* disable PLLE, clear setup fiels */
  1216. tegra30_plle_clk_disable(hw);
  1217. val = clk_readl(c->reg + PLL_MISC(c));
  1218. val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
  1219. clk_writel(val, c->reg + PLL_MISC(c));
  1220. /* training */
  1221. val = clk_readl(c->reg + PLL_MISC(c));
  1222. if (force_training || (!(val & PLLE_MISC_READY)))
  1223. tegra30_plle_training(c);
  1224. /* configure dividers, setup, disable SS */
  1225. val = clk_readl(c->reg + PLL_BASE);
  1226. val &= ~PLLE_BASE_DIV_MASK;
  1227. val |= PLLE_BASE_DIV(sel->m, sel->n, sel->p, sel->cpcon);
  1228. clk_writel(val, c->reg + PLL_BASE);
  1229. c->mul = sel->n;
  1230. c->div = sel->m * sel->p;
  1231. val = clk_readl(c->reg + PLL_MISC(c));
  1232. val |= PLLE_MISC_SETUP_VALUE;
  1233. val |= PLLE_MISC_LOCK_ENABLE;
  1234. clk_writel(val, c->reg + PLL_MISC(c));
  1235. val = clk_readl(PLLE_SS_CTRL);
  1236. val |= PLLE_SS_DISABLE;
  1237. clk_writel(val, PLLE_SS_CTRL);
  1238. /* enable and lock PLLE*/
  1239. val = clk_readl(c->reg + PLL_BASE);
  1240. val |= (PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
  1241. clk_writel(val, c->reg + PLL_BASE);
  1242. tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_MISC(c), PLLE_MISC_LOCK);
  1243. return 0;
  1244. }
  1245. static int tegra30_plle_clk_enable(struct clk_hw *hw)
  1246. {
  1247. struct clk_tegra *c = to_clk_tegra(hw);
  1248. return tegra30_plle_configure(hw, !c->set);
  1249. }
  1250. static unsigned long tegra30_plle_clk_recalc_rate(struct clk_hw *hw,
  1251. unsigned long parent_rate)
  1252. {
  1253. struct clk_tegra *c = to_clk_tegra(hw);
  1254. unsigned long rate = parent_rate;
  1255. u32 val;
  1256. val = clk_readl(c->reg + PLL_BASE);
  1257. c->mul = (val & PLLE_BASE_DIVN_MASK) >> PLLE_BASE_DIVN_SHIFT;
  1258. c->div = (val & PLLE_BASE_DIVM_MASK) >> PLLE_BASE_DIVM_SHIFT;
  1259. c->div *= (val & PLLE_BASE_DIVP_MASK) >> PLLE_BASE_DIVP_SHIFT;
  1260. if (c->mul != 0 && c->div != 0) {
  1261. rate *= c->mul;
  1262. rate += c->div - 1; /* round up */
  1263. do_div(rate, c->div);
  1264. }
  1265. return rate;
  1266. }
  1267. struct clk_ops tegra30_plle_ops = {
  1268. .is_enabled = tegra30_plle_clk_is_enabled,
  1269. .enable = tegra30_plle_clk_enable,
  1270. .disable = tegra30_plle_clk_disable,
  1271. .recalc_rate = tegra30_plle_clk_recalc_rate,
  1272. };
  1273. /* Clock divider ops */
  1274. static int tegra30_pll_div_clk_is_enabled(struct clk_hw *hw)
  1275. {
  1276. struct clk_tegra *c = to_clk_tegra(hw);
  1277. if (c->flags & DIV_U71) {
  1278. u32 val = clk_readl(c->reg);
  1279. val >>= c->reg_shift;
  1280. c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
  1281. if (!(val & PLL_OUT_RESET_DISABLE))
  1282. c->state = OFF;
  1283. } else {
  1284. c->state = ON;
  1285. }
  1286. return c->state;
  1287. }
  1288. static int tegra30_pll_div_clk_enable(struct clk_hw *hw)
  1289. {
  1290. struct clk_tegra *c = to_clk_tegra(hw);
  1291. u32 val;
  1292. u32 new_val;
  1293. pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
  1294. if (c->flags & DIV_U71) {
  1295. val = clk_readl(c->reg);
  1296. new_val = val >> c->reg_shift;
  1297. new_val &= 0xFFFF;
  1298. new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
  1299. val &= ~(0xFFFF << c->reg_shift);
  1300. val |= new_val << c->reg_shift;
  1301. clk_writel_delay(val, c->reg);
  1302. return 0;
  1303. } else if (c->flags & DIV_2) {
  1304. return 0;
  1305. }
  1306. return -EINVAL;
  1307. }
  1308. static void tegra30_pll_div_clk_disable(struct clk_hw *hw)
  1309. {
  1310. struct clk_tegra *c = to_clk_tegra(hw);
  1311. u32 val;
  1312. u32 new_val;
  1313. pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
  1314. if (c->flags & DIV_U71) {
  1315. val = clk_readl(c->reg);
  1316. new_val = val >> c->reg_shift;
  1317. new_val &= 0xFFFF;
  1318. new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
  1319. val &= ~(0xFFFF << c->reg_shift);
  1320. val |= new_val << c->reg_shift;
  1321. clk_writel_delay(val, c->reg);
  1322. }
  1323. }
  1324. static int tegra30_pll_div_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  1325. unsigned long parent_rate)
  1326. {
  1327. struct clk_tegra *c = to_clk_tegra(hw);
  1328. u32 val;
  1329. u32 new_val;
  1330. int divider_u71;
  1331. if (c->flags & DIV_U71) {
  1332. divider_u71 = clk_div71_get_divider(
  1333. parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
  1334. if (divider_u71 >= 0) {
  1335. val = clk_readl(c->reg);
  1336. new_val = val >> c->reg_shift;
  1337. new_val &= 0xFFFF;
  1338. if (c->flags & DIV_U71_FIXED)
  1339. new_val |= PLL_OUT_OVERRIDE;
  1340. new_val &= ~PLL_OUT_RATIO_MASK;
  1341. new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
  1342. val &= ~(0xFFFF << c->reg_shift);
  1343. val |= new_val << c->reg_shift;
  1344. clk_writel_delay(val, c->reg);
  1345. c->div = divider_u71 + 2;
  1346. c->mul = 2;
  1347. c->fixed_rate = rate;
  1348. return 0;
  1349. }
  1350. } else if (c->flags & DIV_2) {
  1351. c->fixed_rate = rate;
  1352. return 0;
  1353. }
  1354. return -EINVAL;
  1355. }
  1356. static unsigned long tegra30_pll_div_clk_recalc_rate(struct clk_hw *hw,
  1357. unsigned long parent_rate)
  1358. {
  1359. struct clk_tegra *c = to_clk_tegra(hw);
  1360. u64 rate = parent_rate;
  1361. if (c->flags & DIV_U71) {
  1362. u32 divu71;
  1363. u32 val = clk_readl(c->reg);
  1364. val >>= c->reg_shift;
  1365. divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
  1366. c->div = (divu71 + 2);
  1367. c->mul = 2;
  1368. } else if (c->flags & DIV_2) {
  1369. if (c->flags & (PLLD | PLLX)) {
  1370. c->div = 2;
  1371. c->mul = 1;
  1372. } else
  1373. BUG();
  1374. } else {
  1375. c->div = 1;
  1376. c->mul = 1;
  1377. }
  1378. if (c->mul != 0 && c->div != 0) {
  1379. rate *= c->mul;
  1380. rate += c->div - 1; /* round up */
  1381. do_div(rate, c->div);
  1382. }
  1383. return rate;
  1384. }
  1385. static long tegra30_pll_div_clk_round_rate(struct clk_hw *hw,
  1386. unsigned long rate, unsigned long *prate)
  1387. {
  1388. struct clk_tegra *c = to_clk_tegra(hw);
  1389. unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
  1390. int divider;
  1391. if (prate)
  1392. parent_rate = *prate;
  1393. if (c->flags & DIV_U71) {
  1394. divider = clk_div71_get_divider(
  1395. parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
  1396. if (divider < 0)
  1397. return divider;
  1398. return DIV_ROUND_UP(parent_rate * 2, divider + 2);
  1399. } else if (c->flags & DIV_2) {
  1400. *prate = rate * 2;
  1401. return rate;
  1402. }
  1403. return -EINVAL;
  1404. }
  1405. struct clk_ops tegra30_pll_div_ops = {
  1406. .is_enabled = tegra30_pll_div_clk_is_enabled,
  1407. .enable = tegra30_pll_div_clk_enable,
  1408. .disable = tegra30_pll_div_clk_disable,
  1409. .set_rate = tegra30_pll_div_clk_set_rate,
  1410. .recalc_rate = tegra30_pll_div_clk_recalc_rate,
  1411. .round_rate = tegra30_pll_div_clk_round_rate,
  1412. };
  1413. /* Periph clk ops */
  1414. static inline u32 periph_clk_source_mask(struct clk_tegra *c)
  1415. {
  1416. if (c->flags & MUX8)
  1417. return 7 << 29;
  1418. else if (c->flags & MUX_PWM)
  1419. return 3 << 28;
  1420. else if (c->flags & MUX_CLK_OUT)
  1421. return 3 << (c->u.periph.clk_num + 4);
  1422. else if (c->flags & PLLD)
  1423. return PLLD_BASE_DSIB_MUX_MASK;
  1424. else
  1425. return 3 << 30;
  1426. }
  1427. static inline u32 periph_clk_source_shift(struct clk_tegra *c)
  1428. {
  1429. if (c->flags & MUX8)
  1430. return 29;
  1431. else if (c->flags & MUX_PWM)
  1432. return 28;
  1433. else if (c->flags & MUX_CLK_OUT)
  1434. return c->u.periph.clk_num + 4;
  1435. else if (c->flags & PLLD)
  1436. return PLLD_BASE_DSIB_MUX_SHIFT;
  1437. else
  1438. return 30;
  1439. }
  1440. static int tegra30_periph_clk_is_enabled(struct clk_hw *hw)
  1441. {
  1442. struct clk_tegra *c = to_clk_tegra(hw);
  1443. c->state = ON;
  1444. if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
  1445. c->state = OFF;
  1446. if (!(c->flags & PERIPH_NO_RESET))
  1447. if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
  1448. c->state = OFF;
  1449. return c->state;
  1450. }
  1451. static int tegra30_periph_clk_enable(struct clk_hw *hw)
  1452. {
  1453. struct clk_tegra *c = to_clk_tegra(hw);
  1454. tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
  1455. if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
  1456. return 0;
  1457. clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
  1458. if (!(c->flags & PERIPH_NO_RESET) &&
  1459. !(c->flags & PERIPH_MANUAL_RESET)) {
  1460. if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) &
  1461. PERIPH_CLK_TO_BIT(c)) {
  1462. udelay(5); /* reset propagation delay */
  1463. clk_writel(PERIPH_CLK_TO_BIT(c),
  1464. PERIPH_CLK_TO_RST_CLR_REG(c));
  1465. }
  1466. }
  1467. return 0;
  1468. }
  1469. static void tegra30_periph_clk_disable(struct clk_hw *hw)
  1470. {
  1471. struct clk_tegra *c = to_clk_tegra(hw);
  1472. unsigned long val;
  1473. tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
  1474. if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 0)
  1475. return;
  1476. /* If peripheral is in the APB bus then read the APB bus to
  1477. * flush the write operation in apb bus. This will avoid the
  1478. * peripheral access after disabling clock*/
  1479. if (c->flags & PERIPH_ON_APB)
  1480. val = chipid_readl();
  1481. clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_CLR_REG(c));
  1482. }
  1483. void tegra30_periph_clk_reset(struct clk_hw *hw, bool assert)
  1484. {
  1485. struct clk_tegra *c = to_clk_tegra(hw);
  1486. unsigned long val;
  1487. if (!(c->flags & PERIPH_NO_RESET)) {
  1488. if (assert) {
  1489. /* If peripheral is in the APB bus then read the APB
  1490. * bus to flush the write operation in apb bus. This
  1491. * will avoid the peripheral access after disabling
  1492. * clock */
  1493. if (c->flags & PERIPH_ON_APB)
  1494. val = chipid_readl();
  1495. clk_writel(PERIPH_CLK_TO_BIT(c),
  1496. PERIPH_CLK_TO_RST_SET_REG(c));
  1497. } else
  1498. clk_writel(PERIPH_CLK_TO_BIT(c),
  1499. PERIPH_CLK_TO_RST_CLR_REG(c));
  1500. }
  1501. }
  1502. static int tegra30_periph_clk_set_parent(struct clk_hw *hw, u8 index)
  1503. {
  1504. struct clk_tegra *c = to_clk_tegra(hw);
  1505. u32 val;
  1506. if (!(c->flags & MUX))
  1507. return (index == 0) ? 0 : (-EINVAL);
  1508. val = clk_readl(c->reg);
  1509. val &= ~periph_clk_source_mask(c);
  1510. val |= (index << periph_clk_source_shift(c));
  1511. clk_writel_delay(val, c->reg);
  1512. return 0;
  1513. }
  1514. static u8 tegra30_periph_clk_get_parent(struct clk_hw *hw)
  1515. {
  1516. struct clk_tegra *c = to_clk_tegra(hw);
  1517. u32 val = clk_readl(c->reg);
  1518. int source = (val & periph_clk_source_mask(c)) >>
  1519. periph_clk_source_shift(c);
  1520. if (!(c->flags & MUX))
  1521. return 0;
  1522. return source;
  1523. }
  1524. static int tegra30_periph_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  1525. unsigned long parent_rate)
  1526. {
  1527. struct clk_tegra *c = to_clk_tegra(hw);
  1528. u32 val;
  1529. int divider;
  1530. if (c->flags & DIV_U71) {
  1531. divider = clk_div71_get_divider(
  1532. parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
  1533. if (divider >= 0) {
  1534. val = clk_readl(c->reg);
  1535. val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
  1536. val |= divider;
  1537. if (c->flags & DIV_U71_UART) {
  1538. if (divider)
  1539. val |= PERIPH_CLK_UART_DIV_ENB;
  1540. else
  1541. val &= ~PERIPH_CLK_UART_DIV_ENB;
  1542. }
  1543. clk_writel_delay(val, c->reg);
  1544. c->div = divider + 2;
  1545. c->mul = 2;
  1546. return 0;
  1547. }
  1548. } else if (c->flags & DIV_U16) {
  1549. divider = clk_div16_get_divider(parent_rate, rate);
  1550. if (divider >= 0) {
  1551. val = clk_readl(c->reg);
  1552. val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
  1553. val |= divider;
  1554. clk_writel_delay(val, c->reg);
  1555. c->div = divider + 1;
  1556. c->mul = 1;
  1557. return 0;
  1558. }
  1559. } else if (parent_rate <= rate) {
  1560. c->div = 1;
  1561. c->mul = 1;
  1562. return 0;
  1563. }
  1564. return -EINVAL;
  1565. }
  1566. static long tegra30_periph_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  1567. unsigned long *prate)
  1568. {
  1569. struct clk_tegra *c = to_clk_tegra(hw);
  1570. unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
  1571. int divider;
  1572. if (prate)
  1573. parent_rate = *prate;
  1574. if (c->flags & DIV_U71) {
  1575. divider = clk_div71_get_divider(
  1576. parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
  1577. if (divider < 0)
  1578. return divider;
  1579. return DIV_ROUND_UP(parent_rate * 2, divider + 2);
  1580. } else if (c->flags & DIV_U16) {
  1581. divider = clk_div16_get_divider(parent_rate, rate);
  1582. if (divider < 0)
  1583. return divider;
  1584. return DIV_ROUND_UP(parent_rate, divider + 1);
  1585. }
  1586. return -EINVAL;
  1587. }
  1588. static unsigned long tegra30_periph_clk_recalc_rate(struct clk_hw *hw,
  1589. unsigned long parent_rate)
  1590. {
  1591. struct clk_tegra *c = to_clk_tegra(hw);
  1592. u64 rate = parent_rate;
  1593. u32 val = clk_readl(c->reg);
  1594. if (c->flags & DIV_U71) {
  1595. u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
  1596. if ((c->flags & DIV_U71_UART) &&
  1597. (!(val & PERIPH_CLK_UART_DIV_ENB))) {
  1598. divu71 = 0;
  1599. }
  1600. if (c->flags & DIV_U71_IDLE) {
  1601. val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
  1602. PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
  1603. val |= (PERIPH_CLK_SOURCE_DIVIDLE_VAL <<
  1604. PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
  1605. clk_writel(val, c->reg);
  1606. }
  1607. c->div = divu71 + 2;
  1608. c->mul = 2;
  1609. } else if (c->flags & DIV_U16) {
  1610. u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
  1611. c->div = divu16 + 1;
  1612. c->mul = 1;
  1613. } else {
  1614. c->div = 1;
  1615. c->mul = 1;
  1616. }
  1617. if (c->mul != 0 && c->div != 0) {
  1618. rate *= c->mul;
  1619. rate += c->div - 1; /* round up */
  1620. do_div(rate, c->div);
  1621. }
  1622. return rate;
  1623. }
  1624. struct clk_ops tegra30_periph_clk_ops = {
  1625. .is_enabled = tegra30_periph_clk_is_enabled,
  1626. .enable = tegra30_periph_clk_enable,
  1627. .disable = tegra30_periph_clk_disable,
  1628. .set_parent = tegra30_periph_clk_set_parent,
  1629. .get_parent = tegra30_periph_clk_get_parent,
  1630. .set_rate = tegra30_periph_clk_set_rate,
  1631. .round_rate = tegra30_periph_clk_round_rate,
  1632. .recalc_rate = tegra30_periph_clk_recalc_rate,
  1633. };
  1634. static int tegra30_dsib_clk_set_parent(struct clk_hw *hw, u8 index)
  1635. {
  1636. struct clk *d = clk_get_sys(NULL, "pll_d");
  1637. /* The DSIB parent selection bit is in PLLD base
  1638. register - can not do direct r-m-w, must be
  1639. protected by PLLD lock */
  1640. tegra_clk_cfg_ex(
  1641. d, TEGRA_CLK_PLLD_MIPI_MUX_SEL, index);
  1642. return 0;
  1643. }
  1644. struct clk_ops tegra30_dsib_clk_ops = {
  1645. .is_enabled = tegra30_periph_clk_is_enabled,
  1646. .enable = &tegra30_periph_clk_enable,
  1647. .disable = &tegra30_periph_clk_disable,
  1648. .set_parent = &tegra30_dsib_clk_set_parent,
  1649. .get_parent = &tegra30_periph_clk_get_parent,
  1650. .set_rate = &tegra30_periph_clk_set_rate,
  1651. .round_rate = &tegra30_periph_clk_round_rate,
  1652. .recalc_rate = &tegra30_periph_clk_recalc_rate,
  1653. };
  1654. /* Periph extended clock configuration ops */
  1655. int tegra30_vi_clk_cfg_ex(struct clk_hw *hw,
  1656. enum tegra_clk_ex_param p, u32 setting)
  1657. {
  1658. struct clk_tegra *c = to_clk_tegra(hw);
  1659. if (p == TEGRA_CLK_VI_INP_SEL) {
  1660. u32 val = clk_readl(c->reg);
  1661. val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
  1662. val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
  1663. PERIPH_CLK_VI_SEL_EX_MASK;
  1664. clk_writel(val, c->reg);
  1665. return 0;
  1666. }
  1667. return -EINVAL;
  1668. }
  1669. int tegra30_nand_clk_cfg_ex(struct clk_hw *hw,
  1670. enum tegra_clk_ex_param p, u32 setting)
  1671. {
  1672. struct clk_tegra *c = to_clk_tegra(hw);
  1673. if (p == TEGRA_CLK_NAND_PAD_DIV2_ENB) {
  1674. u32 val = clk_readl(c->reg);
  1675. if (setting)
  1676. val |= PERIPH_CLK_NAND_DIV_EX_ENB;
  1677. else
  1678. val &= ~PERIPH_CLK_NAND_DIV_EX_ENB;
  1679. clk_writel(val, c->reg);
  1680. return 0;
  1681. }
  1682. return -EINVAL;
  1683. }
  1684. int tegra30_dtv_clk_cfg_ex(struct clk_hw *hw,
  1685. enum tegra_clk_ex_param p, u32 setting)
  1686. {
  1687. struct clk_tegra *c = to_clk_tegra(hw);
  1688. if (p == TEGRA_CLK_DTV_INVERT) {
  1689. u32 val = clk_readl(c->reg);
  1690. if (setting)
  1691. val |= PERIPH_CLK_DTV_POLARITY_INV;
  1692. else
  1693. val &= ~PERIPH_CLK_DTV_POLARITY_INV;
  1694. clk_writel(val, c->reg);
  1695. return 0;
  1696. }
  1697. return -EINVAL;
  1698. }
  1699. /* Output clock ops */
  1700. static DEFINE_SPINLOCK(clk_out_lock);
  1701. static int tegra30_clk_out_is_enabled(struct clk_hw *hw)
  1702. {
  1703. struct clk_tegra *c = to_clk_tegra(hw);
  1704. u32 val = pmc_readl(c->reg);
  1705. c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
  1706. c->mul = 1;
  1707. c->div = 1;
  1708. return c->state;
  1709. }
  1710. static int tegra30_clk_out_enable(struct clk_hw *hw)
  1711. {
  1712. struct clk_tegra *c = to_clk_tegra(hw);
  1713. u32 val;
  1714. unsigned long flags;
  1715. spin_lock_irqsave(&clk_out_lock, flags);
  1716. val = pmc_readl(c->reg);
  1717. val |= (0x1 << c->u.periph.clk_num);
  1718. pmc_writel(val, c->reg);
  1719. spin_unlock_irqrestore(&clk_out_lock, flags);
  1720. return 0;
  1721. }
  1722. static void tegra30_clk_out_disable(struct clk_hw *hw)
  1723. {
  1724. struct clk_tegra *c = to_clk_tegra(hw);
  1725. u32 val;
  1726. unsigned long flags;
  1727. spin_lock_irqsave(&clk_out_lock, flags);
  1728. val = pmc_readl(c->reg);
  1729. val &= ~(0x1 << c->u.periph.clk_num);
  1730. pmc_writel(val, c->reg);
  1731. spin_unlock_irqrestore(&clk_out_lock, flags);
  1732. }
  1733. static int tegra30_clk_out_set_parent(struct clk_hw *hw, u8 index)
  1734. {
  1735. struct clk_tegra *c = to_clk_tegra(hw);
  1736. u32 val;
  1737. unsigned long flags;
  1738. spin_lock_irqsave(&clk_out_lock, flags);
  1739. val = pmc_readl(c->reg);
  1740. val &= ~periph_clk_source_mask(c);
  1741. val |= (index << periph_clk_source_shift(c));
  1742. pmc_writel(val, c->reg);
  1743. spin_unlock_irqrestore(&clk_out_lock, flags);
  1744. return 0;
  1745. }
  1746. static u8 tegra30_clk_out_get_parent(struct clk_hw *hw)
  1747. {
  1748. struct clk_tegra *c = to_clk_tegra(hw);
  1749. u32 val = pmc_readl(c->reg);
  1750. int source;
  1751. source = (val & periph_clk_source_mask(c)) >>
  1752. periph_clk_source_shift(c);
  1753. return source;
  1754. }
  1755. struct clk_ops tegra_clk_out_ops = {
  1756. .is_enabled = tegra30_clk_out_is_enabled,
  1757. .enable = tegra30_clk_out_enable,
  1758. .disable = tegra30_clk_out_disable,
  1759. .set_parent = tegra30_clk_out_set_parent,
  1760. .get_parent = tegra30_clk_out_get_parent,
  1761. .recalc_rate = tegra30_clk_fixed_recalc_rate,
  1762. };
  1763. /* Clock doubler ops */
  1764. static int tegra30_clk_double_is_enabled(struct clk_hw *hw)
  1765. {
  1766. struct clk_tegra *c = to_clk_tegra(hw);
  1767. c->state = ON;
  1768. if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
  1769. c->state = OFF;
  1770. return c->state;
  1771. };
  1772. static int tegra30_clk_double_set_rate(struct clk_hw *hw, unsigned long rate,
  1773. unsigned long parent_rate)
  1774. {
  1775. struct clk_tegra *c = to_clk_tegra(hw);
  1776. u32 val;
  1777. if (rate == parent_rate) {
  1778. val = clk_readl(c->reg) | (0x1 << c->reg_shift);
  1779. clk_writel(val, c->reg);
  1780. c->mul = 1;
  1781. c->div = 1;
  1782. return 0;
  1783. } else if (rate == 2 * parent_rate) {
  1784. val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
  1785. clk_writel(val, c->reg);
  1786. c->mul = 2;
  1787. c->div = 1;
  1788. return 0;
  1789. }
  1790. return -EINVAL;
  1791. }
  1792. static unsigned long tegra30_clk_double_recalc_rate(struct clk_hw *hw,
  1793. unsigned long parent_rate)
  1794. {
  1795. struct clk_tegra *c = to_clk_tegra(hw);
  1796. u64 rate = parent_rate;
  1797. u32 val = clk_readl(c->reg);
  1798. c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
  1799. c->div = 1;
  1800. if (c->mul != 0 && c->div != 0) {
  1801. rate *= c->mul;
  1802. rate += c->div - 1; /* round up */
  1803. do_div(rate, c->div);
  1804. }
  1805. return rate;
  1806. }
  1807. static long tegra30_clk_double_round_rate(struct clk_hw *hw, unsigned long rate,
  1808. unsigned long *prate)
  1809. {
  1810. unsigned long output_rate = *prate;
  1811. do_div(output_rate, 2);
  1812. return output_rate;
  1813. }
  1814. struct clk_ops tegra30_clk_double_ops = {
  1815. .is_enabled = tegra30_clk_double_is_enabled,
  1816. .enable = tegra30_periph_clk_enable,
  1817. .disable = tegra30_periph_clk_disable,
  1818. .recalc_rate = tegra30_clk_double_recalc_rate,
  1819. .round_rate = tegra30_clk_double_round_rate,
  1820. .set_rate = tegra30_clk_double_set_rate,
  1821. };
  1822. /* Audio sync clock ops */
  1823. struct clk_ops tegra_sync_source_ops = {
  1824. .recalc_rate = tegra30_clk_fixed_recalc_rate,
  1825. };
  1826. static int tegra30_audio_sync_clk_is_enabled(struct clk_hw *hw)
  1827. {
  1828. struct clk_tegra *c = to_clk_tegra(hw);
  1829. u32 val = clk_readl(c->reg);
  1830. c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
  1831. return c->state;
  1832. }
  1833. static int tegra30_audio_sync_clk_enable(struct clk_hw *hw)
  1834. {
  1835. struct clk_tegra *c = to_clk_tegra(hw);
  1836. u32 val = clk_readl(c->reg);
  1837. clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
  1838. return 0;
  1839. }
  1840. static void tegra30_audio_sync_clk_disable(struct clk_hw *hw)
  1841. {
  1842. struct clk_tegra *c = to_clk_tegra(hw);
  1843. u32 val = clk_readl(c->reg);
  1844. clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
  1845. }
  1846. static int tegra30_audio_sync_clk_set_parent(struct clk_hw *hw, u8 index)
  1847. {
  1848. struct clk_tegra *c = to_clk_tegra(hw);
  1849. u32 val;
  1850. val = clk_readl(c->reg);
  1851. val &= ~AUDIO_SYNC_SOURCE_MASK;
  1852. val |= index;
  1853. clk_writel(val, c->reg);
  1854. return 0;
  1855. }
  1856. static u8 tegra30_audio_sync_clk_get_parent(struct clk_hw *hw)
  1857. {
  1858. struct clk_tegra *c = to_clk_tegra(hw);
  1859. u32 val = clk_readl(c->reg);
  1860. int source;
  1861. source = val & AUDIO_SYNC_SOURCE_MASK;
  1862. return source;
  1863. }
  1864. struct clk_ops tegra30_audio_sync_clk_ops = {
  1865. .is_enabled = tegra30_audio_sync_clk_is_enabled,
  1866. .enable = tegra30_audio_sync_clk_enable,
  1867. .disable = tegra30_audio_sync_clk_disable,
  1868. .set_parent = tegra30_audio_sync_clk_set_parent,
  1869. .get_parent = tegra30_audio_sync_clk_get_parent,
  1870. .recalc_rate = tegra30_clk_fixed_recalc_rate,
  1871. };
  1872. /* cml0 (pcie), and cml1 (sata) clock ops */
  1873. static int tegra30_cml_clk_is_enabled(struct clk_hw *hw)
  1874. {
  1875. struct clk_tegra *c = to_clk_tegra(hw);
  1876. u32 val = clk_readl(c->reg);
  1877. c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
  1878. return c->state;
  1879. }
  1880. static int tegra30_cml_clk_enable(struct clk_hw *hw)
  1881. {
  1882. struct clk_tegra *c = to_clk_tegra(hw);
  1883. u32 val = clk_readl(c->reg);
  1884. val |= (0x1 << c->u.periph.clk_num);
  1885. clk_writel(val, c->reg);
  1886. return 0;
  1887. }
  1888. static void tegra30_cml_clk_disable(struct clk_hw *hw)
  1889. {
  1890. struct clk_tegra *c = to_clk_tegra(hw);
  1891. u32 val = clk_readl(c->reg);
  1892. val &= ~(0x1 << c->u.periph.clk_num);
  1893. clk_writel(val, c->reg);
  1894. }
  1895. struct clk_ops tegra_cml_clk_ops = {
  1896. .is_enabled = tegra30_cml_clk_is_enabled,
  1897. .enable = tegra30_cml_clk_enable,
  1898. .disable = tegra30_cml_clk_disable,
  1899. .recalc_rate = tegra30_clk_fixed_recalc_rate,
  1900. };
  1901. struct clk_ops tegra_pciex_clk_ops = {
  1902. .recalc_rate = tegra30_clk_fixed_recalc_rate,
  1903. };
  1904. /* Tegra30 CPU clock and reset control functions */
  1905. static void tegra30_wait_cpu_in_reset(u32 cpu)
  1906. {
  1907. unsigned int reg;
  1908. do {
  1909. reg = readl(reg_clk_base +
  1910. TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS);
  1911. cpu_relax();
  1912. } while (!(reg & (1 << cpu))); /* check CPU been reset or not */
  1913. return;
  1914. }
  1915. static void tegra30_put_cpu_in_reset(u32 cpu)
  1916. {
  1917. writel(CPU_RESET(cpu),
  1918. reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
  1919. dmb();
  1920. }
  1921. static void tegra30_cpu_out_of_reset(u32 cpu)
  1922. {
  1923. writel(CPU_RESET(cpu),
  1924. reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR);
  1925. wmb();
  1926. }
  1927. static void tegra30_enable_cpu_clock(u32 cpu)
  1928. {
  1929. unsigned int reg;
  1930. writel(CPU_CLOCK(cpu),
  1931. reg_clk_base + TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR);
  1932. reg = readl(reg_clk_base +
  1933. TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR);
  1934. }
  1935. static void tegra30_disable_cpu_clock(u32 cpu)
  1936. {
  1937. unsigned int reg;
  1938. reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
  1939. writel(reg | CPU_CLOCK(cpu),
  1940. reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
  1941. }
  1942. static struct tegra_cpu_car_ops tegra30_cpu_car_ops = {
  1943. .wait_for_reset = tegra30_wait_cpu_in_reset,
  1944. .put_in_reset = tegra30_put_cpu_in_reset,
  1945. .out_of_reset = tegra30_cpu_out_of_reset,
  1946. .enable_clock = tegra30_enable_cpu_clock,
  1947. .disable_clock = tegra30_disable_cpu_clock,
  1948. };
  1949. void __init tegra30_cpu_car_ops_init(void)
  1950. {
  1951. tegra_cpu_car_ops = &tegra30_cpu_car_ops;
  1952. }