tegra30_clocks.c 57 KB

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