tegra30_clocks.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  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. #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. unsigned long 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. }