tegra30_clocks.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  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 *c, u32 n)
  331. {
  332. return clk_set_rate(c, (clk_get_rate(c->parent) + n-1) / n);
  333. }
  334. static inline u32 periph_clk_to_reg(
  335. struct clk *c, u32 reg_L, u32 reg_V, int offs)
  336. {
  337. u32 reg = c->u.periph.clk_num / 32;
  338. BUG_ON(reg >= RST_DEVICES_NUM);
  339. if (reg < 3)
  340. reg = reg_L + (reg * offs);
  341. else
  342. reg = reg_V + ((reg - 3) * offs);
  343. return reg;
  344. }
  345. static unsigned long clk_measure_input_freq(void)
  346. {
  347. u32 clock_autodetect;
  348. clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
  349. do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
  350. clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
  351. if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
  352. return 12000000;
  353. } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
  354. return 13000000;
  355. } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
  356. return 19200000;
  357. } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
  358. return 26000000;
  359. } else if (clock_autodetect >= 1025 - 3 && clock_autodetect <= 1025 + 3) {
  360. return 16800000;
  361. } else if (clock_autodetect >= 2344 - 3 && clock_autodetect <= 2344 + 3) {
  362. return 38400000;
  363. } else if (clock_autodetect >= 2928 - 3 && clock_autodetect <= 2928 + 3) {
  364. return 48000000;
  365. } else {
  366. pr_err("%s: Unexpected clock autodetect value %d", __func__,
  367. clock_autodetect);
  368. BUG();
  369. return 0;
  370. }
  371. }
  372. static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
  373. u32 flags, u32 round_mode)
  374. {
  375. s64 divider_u71 = parent_rate;
  376. if (!rate)
  377. return -EINVAL;
  378. if (!(flags & DIV_U71_INT))
  379. divider_u71 *= 2;
  380. if (round_mode == ROUND_DIVIDER_UP)
  381. divider_u71 += rate - 1;
  382. do_div(divider_u71, rate);
  383. if (flags & DIV_U71_INT)
  384. divider_u71 *= 2;
  385. if (divider_u71 - 2 < 0)
  386. return 0;
  387. if (divider_u71 - 2 > 255)
  388. return -EINVAL;
  389. return divider_u71 - 2;
  390. }
  391. static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
  392. {
  393. s64 divider_u16;
  394. divider_u16 = parent_rate;
  395. if (!rate)
  396. return -EINVAL;
  397. divider_u16 += rate - 1;
  398. do_div(divider_u16, rate);
  399. if (divider_u16 - 1 < 0)
  400. return 0;
  401. if (divider_u16 - 1 > 0xFFFF)
  402. return -EINVAL;
  403. return divider_u16 - 1;
  404. }
  405. /* clk_m functions */
  406. static unsigned long tegra30_clk_m_autodetect_rate(struct clk *c)
  407. {
  408. u32 osc_ctrl = clk_readl(OSC_CTRL);
  409. u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
  410. u32 pll_ref_div = osc_ctrl & OSC_CTRL_PLL_REF_DIV_MASK;
  411. c->rate = clk_measure_input_freq();
  412. switch (c->rate) {
  413. case 12000000:
  414. auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
  415. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  416. break;
  417. case 13000000:
  418. auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
  419. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  420. break;
  421. case 19200000:
  422. auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
  423. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  424. break;
  425. case 26000000:
  426. auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
  427. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  428. break;
  429. case 16800000:
  430. auto_clock_control |= OSC_CTRL_OSC_FREQ_16_8MHZ;
  431. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
  432. break;
  433. case 38400000:
  434. auto_clock_control |= OSC_CTRL_OSC_FREQ_38_4MHZ;
  435. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_2);
  436. break;
  437. case 48000000:
  438. auto_clock_control |= OSC_CTRL_OSC_FREQ_48MHZ;
  439. BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_4);
  440. break;
  441. default:
  442. pr_err("%s: Unexpected clock rate %ld", __func__, c->rate);
  443. BUG();
  444. }
  445. clk_writel(auto_clock_control, OSC_CTRL);
  446. return c->rate;
  447. }
  448. static void tegra30_clk_m_init(struct clk *c)
  449. {
  450. pr_debug("%s on clock %s\n", __func__, c->name);
  451. tegra30_clk_m_autodetect_rate(c);
  452. }
  453. static int tegra30_clk_m_enable(struct clk *c)
  454. {
  455. pr_debug("%s on clock %s\n", __func__, c->name);
  456. return 0;
  457. }
  458. static void tegra30_clk_m_disable(struct clk *c)
  459. {
  460. pr_debug("%s on clock %s\n", __func__, c->name);
  461. WARN(1, "Attempting to disable main SoC clock\n");
  462. }
  463. struct clk_ops tegra30_clk_m_ops = {
  464. .init = tegra30_clk_m_init,
  465. .enable = tegra30_clk_m_enable,
  466. .disable = tegra30_clk_m_disable,
  467. };
  468. struct clk_ops tegra_clk_m_div_ops = {
  469. .enable = tegra30_clk_m_enable,
  470. };
  471. /* PLL reference divider functions */
  472. static void tegra30_pll_ref_init(struct clk *c)
  473. {
  474. u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
  475. pr_debug("%s on clock %s\n", __func__, c->name);
  476. switch (pll_ref_div) {
  477. case OSC_CTRL_PLL_REF_DIV_1:
  478. c->div = 1;
  479. break;
  480. case OSC_CTRL_PLL_REF_DIV_2:
  481. c->div = 2;
  482. break;
  483. case OSC_CTRL_PLL_REF_DIV_4:
  484. c->div = 4;
  485. break;
  486. default:
  487. pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
  488. BUG();
  489. }
  490. c->mul = 1;
  491. c->state = ON;
  492. }
  493. struct clk_ops tegra_pll_ref_ops = {
  494. .init = tegra30_pll_ref_init,
  495. .enable = tegra30_clk_m_enable,
  496. .disable = tegra30_clk_m_disable,
  497. };
  498. /* super clock functions */
  499. /* "super clocks" on tegra30 have two-stage muxes, fractional 7.1 divider and
  500. * clock skipping super divider. We will ignore the clock skipping divider,
  501. * since we can't lower the voltage when using the clock skip, but we can if
  502. * we lower the PLL frequency. We will use 7.1 divider for CPU super-clock
  503. * only when its parent is a fixed rate PLL, since we can't change PLL rate
  504. * in this case.
  505. */
  506. static void tegra30_super_clk_init(struct clk *c)
  507. {
  508. u32 val;
  509. int source;
  510. int shift;
  511. const struct clk_mux_sel *sel;
  512. val = clk_readl(c->reg + SUPER_CLK_MUX);
  513. c->state = ON;
  514. BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
  515. ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
  516. shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
  517. SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
  518. source = (val >> shift) & SUPER_SOURCE_MASK;
  519. if (c->flags & DIV_2)
  520. source |= val & SUPER_LP_DIV2_BYPASS;
  521. for (sel = c->inputs; sel->input != NULL; sel++) {
  522. if (sel->value == source)
  523. break;
  524. }
  525. BUG_ON(sel->input == NULL);
  526. c->parent = sel->input;
  527. if (c->flags & DIV_U71) {
  528. /* Init safe 7.1 divider value (does not affect PLLX path) */
  529. clk_writel(SUPER_CLOCK_DIV_U71_MIN << SUPER_CLOCK_DIV_U71_SHIFT,
  530. c->reg + SUPER_CLK_DIVIDER);
  531. c->mul = 2;
  532. c->div = 2;
  533. if (!(c->parent->flags & PLLX))
  534. c->div += SUPER_CLOCK_DIV_U71_MIN;
  535. } else
  536. clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
  537. }
  538. static int tegra30_super_clk_enable(struct clk *c)
  539. {
  540. return 0;
  541. }
  542. static void tegra30_super_clk_disable(struct clk *c)
  543. {
  544. /* since tegra 3 has 2 CPU super clocks - low power lp-mode clock and
  545. geared up g-mode super clock - mode switch may request to disable
  546. either of them; accept request with no affect on h/w */
  547. }
  548. static int tegra30_super_clk_set_parent(struct clk *c, struct clk *p)
  549. {
  550. u32 val;
  551. const struct clk_mux_sel *sel;
  552. int shift;
  553. val = clk_readl(c->reg + SUPER_CLK_MUX);
  554. BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
  555. ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
  556. shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
  557. SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
  558. for (sel = c->inputs; sel->input != NULL; sel++) {
  559. if (sel->input == p) {
  560. /* For LP mode super-clock switch between PLLX direct
  561. and divided-by-2 outputs is allowed only when other
  562. than PLLX clock source is current parent */
  563. if ((c->flags & DIV_2) && (p->flags & PLLX) &&
  564. ((sel->value ^ val) & SUPER_LP_DIV2_BYPASS)) {
  565. if (c->parent->flags & PLLX)
  566. return -EINVAL;
  567. val ^= SUPER_LP_DIV2_BYPASS;
  568. clk_writel_delay(val, c->reg);
  569. }
  570. val &= ~(SUPER_SOURCE_MASK << shift);
  571. val |= (sel->value & SUPER_SOURCE_MASK) << shift;
  572. /* 7.1 divider for CPU super-clock does not affect
  573. PLLX path */
  574. if (c->flags & DIV_U71) {
  575. u32 div = 0;
  576. if (!(p->flags & PLLX)) {
  577. div = clk_readl(c->reg +
  578. SUPER_CLK_DIVIDER);
  579. div &= SUPER_CLOCK_DIV_U71_MASK;
  580. div >>= SUPER_CLOCK_DIV_U71_SHIFT;
  581. }
  582. c->div = div + 2;
  583. c->mul = 2;
  584. }
  585. if (c->refcnt)
  586. clk_enable(p);
  587. clk_writel_delay(val, c->reg);
  588. if (c->refcnt && c->parent)
  589. clk_disable(c->parent);
  590. clk_reparent(c, p);
  591. return 0;
  592. }
  593. }
  594. return -EINVAL;
  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 *c, unsigned long rate)
  606. {
  607. if ((c->flags & DIV_U71) && (c->parent->flags & PLL_FIXED)) {
  608. int div = clk_div71_get_divider(c->parent->u.pll.fixed_rate,
  609. rate, c->flags, ROUND_DIVIDER_DOWN);
  610. div = max(div, SUPER_CLOCK_DIV_U71_MIN);
  611. clk_writel(div << SUPER_CLOCK_DIV_U71_SHIFT,
  612. c->reg + SUPER_CLK_DIVIDER);
  613. c->div = div + 2;
  614. c->mul = 2;
  615. return 0;
  616. }
  617. return clk_set_rate(c->parent, rate);
  618. }
  619. struct clk_ops tegra30_super_ops = {
  620. .init = tegra30_super_clk_init,
  621. .enable = tegra30_super_clk_enable,
  622. .disable = tegra30_super_clk_disable,
  623. .set_parent = tegra30_super_clk_set_parent,
  624. .set_rate = tegra30_super_clk_set_rate,
  625. };
  626. static int tegra30_twd_clk_set_rate(struct clk *c, unsigned long rate)
  627. {
  628. /* The input value 'rate' is the clock rate of the CPU complex. */
  629. c->rate = (rate * c->mul) / c->div;
  630. return 0;
  631. }
  632. struct clk_ops tegra30_twd_ops = {
  633. .set_rate = tegra30_twd_clk_set_rate,
  634. };
  635. /* Blink output functions */
  636. static void tegra30_blink_clk_init(struct clk *c)
  637. {
  638. u32 val;
  639. val = pmc_readl(PMC_CTRL);
  640. c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
  641. c->mul = 1;
  642. val = pmc_readl(c->reg);
  643. if (val & PMC_BLINK_TIMER_ENB) {
  644. unsigned int on_off;
  645. on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
  646. PMC_BLINK_TIMER_DATA_ON_MASK;
  647. val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
  648. val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
  649. on_off += val;
  650. /* each tick in the blink timer is 4 32KHz clocks */
  651. c->div = on_off * 4;
  652. } else {
  653. c->div = 1;
  654. }
  655. }
  656. static int tegra30_blink_clk_enable(struct clk *c)
  657. {
  658. u32 val;
  659. val = pmc_readl(PMC_DPD_PADS_ORIDE);
  660. pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
  661. val = pmc_readl(PMC_CTRL);
  662. pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
  663. return 0;
  664. }
  665. static void tegra30_blink_clk_disable(struct clk *c)
  666. {
  667. u32 val;
  668. val = pmc_readl(PMC_CTRL);
  669. pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
  670. val = pmc_readl(PMC_DPD_PADS_ORIDE);
  671. pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
  672. }
  673. static int tegra30_blink_clk_set_rate(struct clk *c, unsigned long rate)
  674. {
  675. unsigned long parent_rate = clk_get_rate(c->parent);
  676. if (rate >= parent_rate) {
  677. c->div = 1;
  678. pmc_writel(0, c->reg);
  679. } else {
  680. unsigned int on_off;
  681. u32 val;
  682. on_off = DIV_ROUND_UP(parent_rate / 8, rate);
  683. c->div = on_off * 8;
  684. val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
  685. PMC_BLINK_TIMER_DATA_ON_SHIFT;
  686. on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
  687. on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
  688. val |= on_off;
  689. val |= PMC_BLINK_TIMER_ENB;
  690. pmc_writel(val, c->reg);
  691. }
  692. return 0;
  693. }
  694. struct clk_ops tegra30_blink_clk_ops = {
  695. .init = &tegra30_blink_clk_init,
  696. .enable = &tegra30_blink_clk_enable,
  697. .disable = &tegra30_blink_clk_disable,
  698. .set_rate = &tegra30_blink_clk_set_rate,
  699. };
  700. /* PLL Functions */
  701. static int tegra30_pll_clk_wait_for_lock(struct clk *c, u32 lock_reg,
  702. u32 lock_bit)
  703. {
  704. #if USE_PLL_LOCK_BITS
  705. int i;
  706. for (i = 0; i < c->u.pll.lock_delay; i++) {
  707. if (clk_readl(lock_reg) & lock_bit) {
  708. udelay(PLL_POST_LOCK_DELAY);
  709. return 0;
  710. }
  711. udelay(2); /* timeout = 2 * lock time */
  712. }
  713. pr_err("Timed out waiting for lock bit on pll %s", c->name);
  714. return -1;
  715. #endif
  716. udelay(c->u.pll.lock_delay);
  717. return 0;
  718. }
  719. static void tegra30_utmi_param_configure(struct clk *c)
  720. {
  721. u32 reg;
  722. int i;
  723. unsigned long main_rate =
  724. clk_get_rate(c->parent->parent);
  725. for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
  726. if (main_rate == utmi_parameters[i].osc_frequency)
  727. break;
  728. }
  729. if (i >= ARRAY_SIZE(utmi_parameters)) {
  730. pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
  731. return;
  732. }
  733. reg = clk_readl(UTMIP_PLL_CFG2);
  734. /* Program UTMIP PLL stable and active counts */
  735. /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
  736. reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
  737. reg |= UTMIP_PLL_CFG2_STABLE_COUNT(
  738. utmi_parameters[i].stable_count);
  739. reg &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
  740. reg |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(
  741. utmi_parameters[i].active_delay_count);
  742. /* Remove power downs from UTMIP PLL control bits */
  743. reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
  744. reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
  745. reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
  746. clk_writel(reg, UTMIP_PLL_CFG2);
  747. /* Program UTMIP PLL delay and oscillator frequency counts */
  748. reg = clk_readl(UTMIP_PLL_CFG1);
  749. reg &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
  750. reg |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(
  751. utmi_parameters[i].enable_delay_count);
  752. reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
  753. reg |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(
  754. utmi_parameters[i].xtal_freq_count);
  755. /* Remove power downs from UTMIP PLL control bits */
  756. reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
  757. reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
  758. reg &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
  759. clk_writel(reg, UTMIP_PLL_CFG1);
  760. }
  761. static void tegra30_pll_clk_init(struct clk *c)
  762. {
  763. u32 val = clk_readl(c->reg + PLL_BASE);
  764. c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
  765. if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
  766. const struct clk_pll_freq_table *sel;
  767. unsigned long input_rate = clk_get_rate(c->parent);
  768. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  769. if (sel->input_rate == input_rate &&
  770. sel->output_rate == c->u.pll.fixed_rate) {
  771. c->mul = sel->n;
  772. c->div = sel->m * sel->p;
  773. return;
  774. }
  775. }
  776. pr_err("Clock %s has unknown fixed frequency\n", c->name);
  777. BUG();
  778. } else if (val & PLL_BASE_BYPASS) {
  779. c->mul = 1;
  780. c->div = 1;
  781. } else {
  782. c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
  783. c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
  784. if (c->flags & PLLU)
  785. c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
  786. else
  787. c->div *= (0x1 << ((val & PLL_BASE_DIVP_MASK) >>
  788. PLL_BASE_DIVP_SHIFT));
  789. if (c->flags & PLL_FIXED) {
  790. unsigned long rate = clk_get_rate_locked(c);
  791. BUG_ON(rate != c->u.pll.fixed_rate);
  792. }
  793. }
  794. if (c->flags & PLLU)
  795. tegra30_utmi_param_configure(c);
  796. }
  797. static int tegra30_pll_clk_enable(struct clk *c)
  798. {
  799. u32 val;
  800. pr_debug("%s on clock %s\n", __func__, c->name);
  801. #if USE_PLL_LOCK_BITS
  802. val = clk_readl(c->reg + PLL_MISC(c));
  803. val |= PLL_MISC_LOCK_ENABLE(c);
  804. clk_writel(val, c->reg + PLL_MISC(c));
  805. #endif
  806. val = clk_readl(c->reg + PLL_BASE);
  807. val &= ~PLL_BASE_BYPASS;
  808. val |= PLL_BASE_ENABLE;
  809. clk_writel(val, c->reg + PLL_BASE);
  810. if (c->flags & PLLM) {
  811. val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
  812. val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
  813. pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
  814. }
  815. tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
  816. return 0;
  817. }
  818. static void tegra30_pll_clk_disable(struct clk *c)
  819. {
  820. u32 val;
  821. pr_debug("%s on clock %s\n", __func__, c->name);
  822. val = clk_readl(c->reg);
  823. val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
  824. clk_writel(val, c->reg);
  825. if (c->flags & PLLM) {
  826. val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
  827. val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
  828. pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
  829. }
  830. }
  831. static int tegra30_pll_clk_set_rate(struct clk *c, unsigned long rate)
  832. {
  833. u32 val, p_div, old_base;
  834. unsigned long input_rate;
  835. const struct clk_pll_freq_table *sel;
  836. struct clk_pll_freq_table cfg;
  837. pr_debug("%s: %s %lu\n", __func__, c->name, rate);
  838. if (c->flags & PLL_FIXED) {
  839. int ret = 0;
  840. if (rate != c->u.pll.fixed_rate) {
  841. pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
  842. __func__, c->name, c->u.pll.fixed_rate, rate);
  843. ret = -EINVAL;
  844. }
  845. return ret;
  846. }
  847. if (c->flags & PLLM) {
  848. if (rate != clk_get_rate_locked(c)) {
  849. pr_err("%s: Can not change memory %s rate in flight\n",
  850. __func__, c->name);
  851. return -EINVAL;
  852. }
  853. return 0;
  854. }
  855. p_div = 0;
  856. input_rate = clk_get_rate(c->parent);
  857. /* Check if the target rate is tabulated */
  858. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  859. if (sel->input_rate == input_rate && sel->output_rate == rate) {
  860. if (c->flags & PLLU) {
  861. BUG_ON(sel->p < 1 || sel->p > 2);
  862. if (sel->p == 1)
  863. p_div = PLLU_BASE_POST_DIV;
  864. } else {
  865. BUG_ON(sel->p < 1);
  866. for (val = sel->p; val > 1; val >>= 1)
  867. p_div++;
  868. p_div <<= PLL_BASE_DIVP_SHIFT;
  869. }
  870. break;
  871. }
  872. }
  873. /* Configure out-of-table rate */
  874. if (sel->input_rate == 0) {
  875. unsigned long cfreq;
  876. BUG_ON(c->flags & PLLU);
  877. sel = &cfg;
  878. switch (input_rate) {
  879. case 12000000:
  880. case 26000000:
  881. cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
  882. break;
  883. case 13000000:
  884. cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
  885. break;
  886. case 16800000:
  887. case 19200000:
  888. cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
  889. break;
  890. default:
  891. pr_err("%s: Unexpected reference rate %lu\n",
  892. __func__, input_rate);
  893. BUG();
  894. }
  895. /* Raise VCO to guarantee 0.5% accuracy */
  896. for (cfg.output_rate = rate; cfg.output_rate < 200 * cfreq;
  897. cfg.output_rate <<= 1)
  898. p_div++;
  899. cfg.p = 0x1 << p_div;
  900. cfg.m = input_rate / cfreq;
  901. cfg.n = cfg.output_rate / cfreq;
  902. cfg.cpcon = OUT_OF_TABLE_CPCON;
  903. if ((cfg.m > (PLL_BASE_DIVM_MASK >> PLL_BASE_DIVM_SHIFT)) ||
  904. (cfg.n > (PLL_BASE_DIVN_MASK >> PLL_BASE_DIVN_SHIFT)) ||
  905. (p_div > (PLL_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT)) ||
  906. (cfg.output_rate > c->u.pll.vco_max)) {
  907. pr_err("%s: Failed to set %s out-of-table rate %lu\n",
  908. __func__, c->name, rate);
  909. return -EINVAL;
  910. }
  911. p_div <<= PLL_BASE_DIVP_SHIFT;
  912. }
  913. c->mul = sel->n;
  914. c->div = sel->m * sel->p;
  915. old_base = val = clk_readl(c->reg + PLL_BASE);
  916. val &= ~(PLL_BASE_DIVM_MASK | PLL_BASE_DIVN_MASK |
  917. ((c->flags & PLLU) ? PLLU_BASE_POST_DIV : PLL_BASE_DIVP_MASK));
  918. val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
  919. (sel->n << PLL_BASE_DIVN_SHIFT) | p_div;
  920. if (val == old_base)
  921. return 0;
  922. if (c->state == ON) {
  923. tegra30_pll_clk_disable(c);
  924. val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
  925. }
  926. clk_writel(val, c->reg + PLL_BASE);
  927. if (c->flags & PLL_HAS_CPCON) {
  928. val = clk_readl(c->reg + PLL_MISC(c));
  929. val &= ~PLL_MISC_CPCON_MASK;
  930. val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
  931. if (c->flags & (PLLU | PLLD)) {
  932. val &= ~PLL_MISC_LFCON_MASK;
  933. if (sel->n >= PLLDU_LFCON_SET_DIVN)
  934. val |= 0x1 << PLL_MISC_LFCON_SHIFT;
  935. } else if (c->flags & (PLLX | PLLM)) {
  936. val &= ~(0x1 << PLL_MISC_DCCON_SHIFT);
  937. if (rate >= (c->u.pll.vco_max >> 1))
  938. val |= 0x1 << PLL_MISC_DCCON_SHIFT;
  939. }
  940. clk_writel(val, c->reg + PLL_MISC(c));
  941. }
  942. if (c->state == ON)
  943. tegra30_pll_clk_enable(c);
  944. return 0;
  945. }
  946. struct clk_ops tegra30_pll_ops = {
  947. .init = tegra30_pll_clk_init,
  948. .enable = tegra30_pll_clk_enable,
  949. .disable = tegra30_pll_clk_disable,
  950. .set_rate = tegra30_pll_clk_set_rate,
  951. };
  952. static int
  953. tegra30_plld_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
  954. {
  955. u32 val, mask, reg;
  956. switch (p) {
  957. case TEGRA_CLK_PLLD_CSI_OUT_ENB:
  958. mask = PLLD_BASE_CSI_CLKENABLE;
  959. reg = c->reg + PLL_BASE;
  960. break;
  961. case TEGRA_CLK_PLLD_DSI_OUT_ENB:
  962. mask = PLLD_MISC_DSI_CLKENABLE;
  963. reg = c->reg + PLL_MISC(c);
  964. break;
  965. case TEGRA_CLK_PLLD_MIPI_MUX_SEL:
  966. if (!(c->flags & PLL_ALT_MISC_REG)) {
  967. mask = PLLD_BASE_DSIB_MUX_MASK;
  968. reg = c->reg + PLL_BASE;
  969. break;
  970. }
  971. /* fall through - error since PLLD2 does not have MUX_SEL control */
  972. default:
  973. return -EINVAL;
  974. }
  975. val = clk_readl(reg);
  976. if (setting)
  977. val |= mask;
  978. else
  979. val &= ~mask;
  980. clk_writel(val, reg);
  981. return 0;
  982. }
  983. struct clk_ops tegra_plld_ops = {
  984. .init = tegra30_pll_clk_init,
  985. .enable = tegra30_pll_clk_enable,
  986. .disable = tegra30_pll_clk_disable,
  987. .set_rate = tegra30_pll_clk_set_rate,
  988. .clk_cfg_ex = tegra30_plld_clk_cfg_ex,
  989. };
  990. static void tegra30_plle_clk_init(struct clk *c)
  991. {
  992. u32 val;
  993. val = clk_readl(PLLE_AUX);
  994. c->parent = (val & PLLE_AUX_PLLP_SEL) ?
  995. tegra_get_clock_by_name("pll_p") :
  996. tegra_get_clock_by_name("pll_ref");
  997. val = clk_readl(c->reg + PLL_BASE);
  998. c->state = (val & PLLE_BASE_ENABLE) ? ON : OFF;
  999. c->mul = (val & PLLE_BASE_DIVN_MASK) >> PLLE_BASE_DIVN_SHIFT;
  1000. c->div = (val & PLLE_BASE_DIVM_MASK) >> PLLE_BASE_DIVM_SHIFT;
  1001. c->div *= (val & PLLE_BASE_DIVP_MASK) >> PLLE_BASE_DIVP_SHIFT;
  1002. }
  1003. static void tegra30_plle_clk_disable(struct clk *c)
  1004. {
  1005. u32 val;
  1006. pr_debug("%s on clock %s\n", __func__, c->name);
  1007. val = clk_readl(c->reg + PLL_BASE);
  1008. val &= ~(PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
  1009. clk_writel(val, c->reg + PLL_BASE);
  1010. }
  1011. static void tegra30_plle_training(struct clk *c)
  1012. {
  1013. u32 val;
  1014. /* PLLE is already disabled, and setup cleared;
  1015. * create falling edge on PLLE IDDQ input */
  1016. val = pmc_readl(PMC_SATA_PWRGT);
  1017. val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
  1018. pmc_writel(val, PMC_SATA_PWRGT);
  1019. val = pmc_readl(PMC_SATA_PWRGT);
  1020. val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
  1021. pmc_writel(val, PMC_SATA_PWRGT);
  1022. val = pmc_readl(PMC_SATA_PWRGT);
  1023. val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
  1024. pmc_writel(val, PMC_SATA_PWRGT);
  1025. do {
  1026. val = clk_readl(c->reg + PLL_MISC(c));
  1027. } while (!(val & PLLE_MISC_READY));
  1028. }
  1029. static int tegra30_plle_configure(struct clk *c, bool force_training)
  1030. {
  1031. u32 val;
  1032. const struct clk_pll_freq_table *sel;
  1033. unsigned long rate = c->u.pll.fixed_rate;
  1034. unsigned long input_rate = clk_get_rate(c->parent);
  1035. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  1036. if (sel->input_rate == input_rate && sel->output_rate == rate)
  1037. break;
  1038. }
  1039. if (sel->input_rate == 0)
  1040. return -ENOSYS;
  1041. /* disable PLLE, clear setup fiels */
  1042. tegra30_plle_clk_disable(c);
  1043. val = clk_readl(c->reg + PLL_MISC(c));
  1044. val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
  1045. clk_writel(val, c->reg + PLL_MISC(c));
  1046. /* training */
  1047. val = clk_readl(c->reg + PLL_MISC(c));
  1048. if (force_training || (!(val & PLLE_MISC_READY)))
  1049. tegra30_plle_training(c);
  1050. /* configure dividers, setup, disable SS */
  1051. val = clk_readl(c->reg + PLL_BASE);
  1052. val &= ~PLLE_BASE_DIV_MASK;
  1053. val |= PLLE_BASE_DIV(sel->m, sel->n, sel->p, sel->cpcon);
  1054. clk_writel(val, c->reg + PLL_BASE);
  1055. c->mul = sel->n;
  1056. c->div = sel->m * sel->p;
  1057. val = clk_readl(c->reg + PLL_MISC(c));
  1058. val |= PLLE_MISC_SETUP_VALUE;
  1059. val |= PLLE_MISC_LOCK_ENABLE;
  1060. clk_writel(val, c->reg + PLL_MISC(c));
  1061. val = clk_readl(PLLE_SS_CTRL);
  1062. val |= PLLE_SS_DISABLE;
  1063. clk_writel(val, PLLE_SS_CTRL);
  1064. /* enable and lock PLLE*/
  1065. val = clk_readl(c->reg + PLL_BASE);
  1066. val |= (PLLE_BASE_CML_ENABLE | PLLE_BASE_ENABLE);
  1067. clk_writel(val, c->reg + PLL_BASE);
  1068. tegra30_pll_clk_wait_for_lock(c, c->reg + PLL_MISC(c), PLLE_MISC_LOCK);
  1069. return 0;
  1070. }
  1071. static int tegra30_plle_clk_enable(struct clk *c)
  1072. {
  1073. pr_debug("%s on clock %s\n", __func__, c->name);
  1074. return tegra30_plle_configure(c, !c->set);
  1075. }
  1076. struct clk_ops tegra30_plle_ops = {
  1077. .init = tegra30_plle_clk_init,
  1078. .enable = tegra30_plle_clk_enable,
  1079. .disable = tegra30_plle_clk_disable,
  1080. };
  1081. /* Clock divider ops */
  1082. static void tegra30_pll_div_clk_init(struct clk *c)
  1083. {
  1084. if (c->flags & DIV_U71) {
  1085. u32 divu71;
  1086. u32 val = clk_readl(c->reg);
  1087. val >>= c->reg_shift;
  1088. c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
  1089. if (!(val & PLL_OUT_RESET_DISABLE))
  1090. c->state = OFF;
  1091. divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
  1092. c->div = (divu71 + 2);
  1093. c->mul = 2;
  1094. } else if (c->flags & DIV_2) {
  1095. c->state = ON;
  1096. if (c->flags & (PLLD | PLLX)) {
  1097. c->div = 2;
  1098. c->mul = 1;
  1099. } else
  1100. BUG();
  1101. } else {
  1102. c->state = ON;
  1103. c->div = 1;
  1104. c->mul = 1;
  1105. }
  1106. }
  1107. static int tegra30_pll_div_clk_enable(struct clk *c)
  1108. {
  1109. u32 val;
  1110. u32 new_val;
  1111. pr_debug("%s: %s\n", __func__, c->name);
  1112. if (c->flags & DIV_U71) {
  1113. val = clk_readl(c->reg);
  1114. new_val = val >> c->reg_shift;
  1115. new_val &= 0xFFFF;
  1116. new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
  1117. val &= ~(0xFFFF << c->reg_shift);
  1118. val |= new_val << c->reg_shift;
  1119. clk_writel_delay(val, c->reg);
  1120. return 0;
  1121. } else if (c->flags & DIV_2) {
  1122. return 0;
  1123. }
  1124. return -EINVAL;
  1125. }
  1126. static void tegra30_pll_div_clk_disable(struct clk *c)
  1127. {
  1128. u32 val;
  1129. u32 new_val;
  1130. pr_debug("%s: %s\n", __func__, c->name);
  1131. if (c->flags & DIV_U71) {
  1132. val = clk_readl(c->reg);
  1133. new_val = val >> c->reg_shift;
  1134. new_val &= 0xFFFF;
  1135. new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
  1136. val &= ~(0xFFFF << c->reg_shift);
  1137. val |= new_val << c->reg_shift;
  1138. clk_writel_delay(val, c->reg);
  1139. }
  1140. }
  1141. static int tegra30_pll_div_clk_set_rate(struct clk *c, unsigned long rate)
  1142. {
  1143. u32 val;
  1144. u32 new_val;
  1145. int divider_u71;
  1146. unsigned long parent_rate = clk_get_rate(c->parent);
  1147. pr_debug("%s: %s %lu\n", __func__, c->name, rate);
  1148. if (c->flags & DIV_U71) {
  1149. divider_u71 = clk_div71_get_divider(
  1150. parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
  1151. if (divider_u71 >= 0) {
  1152. val = clk_readl(c->reg);
  1153. new_val = val >> c->reg_shift;
  1154. new_val &= 0xFFFF;
  1155. if (c->flags & DIV_U71_FIXED)
  1156. new_val |= PLL_OUT_OVERRIDE;
  1157. new_val &= ~PLL_OUT_RATIO_MASK;
  1158. new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
  1159. val &= ~(0xFFFF << c->reg_shift);
  1160. val |= new_val << c->reg_shift;
  1161. clk_writel_delay(val, c->reg);
  1162. c->div = divider_u71 + 2;
  1163. c->mul = 2;
  1164. return 0;
  1165. }
  1166. } else if (c->flags & DIV_2)
  1167. return clk_set_rate(c->parent, rate * 2);
  1168. return -EINVAL;
  1169. }
  1170. static long tegra30_pll_div_clk_round_rate(struct clk *c, unsigned long rate)
  1171. {
  1172. int divider;
  1173. unsigned long parent_rate = clk_get_rate(c->parent);
  1174. pr_debug("%s: %s %lu\n", __func__, c->name, rate);
  1175. if (c->flags & DIV_U71) {
  1176. divider = clk_div71_get_divider(
  1177. parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
  1178. if (divider < 0)
  1179. return divider;
  1180. return DIV_ROUND_UP(parent_rate * 2, divider + 2);
  1181. } else if (c->flags & DIV_2)
  1182. /* no rounding - fixed DIV_2 dividers pass rate to parent PLL */
  1183. return rate;
  1184. return -EINVAL;
  1185. }
  1186. struct clk_ops tegra30_pll_div_ops = {
  1187. .init = tegra30_pll_div_clk_init,
  1188. .enable = tegra30_pll_div_clk_enable,
  1189. .disable = tegra30_pll_div_clk_disable,
  1190. .set_rate = tegra30_pll_div_clk_set_rate,
  1191. .round_rate = tegra30_pll_div_clk_round_rate,
  1192. };
  1193. /* Periph clk ops */
  1194. static inline u32 periph_clk_source_mask(struct clk *c)
  1195. {
  1196. if (c->flags & MUX8)
  1197. return 7 << 29;
  1198. else if (c->flags & MUX_PWM)
  1199. return 3 << 28;
  1200. else if (c->flags & MUX_CLK_OUT)
  1201. return 3 << (c->u.periph.clk_num + 4);
  1202. else if (c->flags & PLLD)
  1203. return PLLD_BASE_DSIB_MUX_MASK;
  1204. else
  1205. return 3 << 30;
  1206. }
  1207. static inline u32 periph_clk_source_shift(struct clk *c)
  1208. {
  1209. if (c->flags & MUX8)
  1210. return 29;
  1211. else if (c->flags & MUX_PWM)
  1212. return 28;
  1213. else if (c->flags & MUX_CLK_OUT)
  1214. return c->u.periph.clk_num + 4;
  1215. else if (c->flags & PLLD)
  1216. return PLLD_BASE_DSIB_MUX_SHIFT;
  1217. else
  1218. return 30;
  1219. }
  1220. static void tegra30_periph_clk_init(struct clk *c)
  1221. {
  1222. u32 val = clk_readl(c->reg);
  1223. const struct clk_mux_sel *mux = 0;
  1224. const struct clk_mux_sel *sel;
  1225. if (c->flags & MUX) {
  1226. for (sel = c->inputs; sel->input != NULL; sel++) {
  1227. if (((val & periph_clk_source_mask(c)) >>
  1228. periph_clk_source_shift(c)) == sel->value)
  1229. mux = sel;
  1230. }
  1231. BUG_ON(!mux);
  1232. c->parent = mux->input;
  1233. } else {
  1234. c->parent = c->inputs[0].input;
  1235. }
  1236. if (c->flags & DIV_U71) {
  1237. u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
  1238. if ((c->flags & DIV_U71_UART) &&
  1239. (!(val & PERIPH_CLK_UART_DIV_ENB))) {
  1240. divu71 = 0;
  1241. }
  1242. if (c->flags & DIV_U71_IDLE) {
  1243. val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
  1244. PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
  1245. val |= (PERIPH_CLK_SOURCE_DIVIDLE_VAL <<
  1246. PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
  1247. clk_writel(val, c->reg);
  1248. }
  1249. c->div = divu71 + 2;
  1250. c->mul = 2;
  1251. } else if (c->flags & DIV_U16) {
  1252. u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
  1253. c->div = divu16 + 1;
  1254. c->mul = 1;
  1255. } else {
  1256. c->div = 1;
  1257. c->mul = 1;
  1258. }
  1259. c->state = ON;
  1260. if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
  1261. c->state = OFF;
  1262. if (!(c->flags & PERIPH_NO_RESET))
  1263. if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
  1264. c->state = OFF;
  1265. }
  1266. static int tegra30_periph_clk_enable(struct clk *c)
  1267. {
  1268. pr_debug("%s on clock %s\n", __func__, c->name);
  1269. tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
  1270. if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
  1271. return 0;
  1272. clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
  1273. if (!(c->flags & PERIPH_NO_RESET) &&
  1274. !(c->flags & PERIPH_MANUAL_RESET)) {
  1275. if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) &
  1276. PERIPH_CLK_TO_BIT(c)) {
  1277. udelay(5); /* reset propagation delay */
  1278. clk_writel(PERIPH_CLK_TO_BIT(c),
  1279. PERIPH_CLK_TO_RST_CLR_REG(c));
  1280. }
  1281. }
  1282. return 0;
  1283. }
  1284. static void tegra30_periph_clk_disable(struct clk *c)
  1285. {
  1286. unsigned long val;
  1287. pr_debug("%s on clock %s\n", __func__, c->name);
  1288. if (c->refcnt)
  1289. tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
  1290. if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] == 0) {
  1291. /* If peripheral is in the APB bus then read the APB bus to
  1292. * flush the write operation in apb bus. This will avoid the
  1293. * peripheral access after disabling clock*/
  1294. if (c->flags & PERIPH_ON_APB)
  1295. val = chipid_readl();
  1296. clk_writel_delay(
  1297. PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_CLR_REG(c));
  1298. }
  1299. }
  1300. static void tegra30_periph_clk_reset(struct clk *c, bool assert)
  1301. {
  1302. unsigned long val;
  1303. pr_debug("%s %s on clock %s\n", __func__,
  1304. assert ? "assert" : "deassert", c->name);
  1305. if (!(c->flags & PERIPH_NO_RESET)) {
  1306. if (assert) {
  1307. /* If peripheral is in the APB bus then read the APB
  1308. * bus to flush the write operation in apb bus. This
  1309. * will avoid the peripheral access after disabling
  1310. * clock */
  1311. if (c->flags & PERIPH_ON_APB)
  1312. val = chipid_readl();
  1313. clk_writel(PERIPH_CLK_TO_BIT(c),
  1314. PERIPH_CLK_TO_RST_SET_REG(c));
  1315. } else
  1316. clk_writel(PERIPH_CLK_TO_BIT(c),
  1317. PERIPH_CLK_TO_RST_CLR_REG(c));
  1318. }
  1319. }
  1320. static int tegra30_periph_clk_set_parent(struct clk *c, struct clk *p)
  1321. {
  1322. u32 val;
  1323. const struct clk_mux_sel *sel;
  1324. pr_debug("%s: %s %s\n", __func__, c->name, p->name);
  1325. if (!(c->flags & MUX))
  1326. return (p == c->parent) ? 0 : (-EINVAL);
  1327. for (sel = c->inputs; sel->input != NULL; sel++) {
  1328. if (sel->input == p) {
  1329. val = clk_readl(c->reg);
  1330. val &= ~periph_clk_source_mask(c);
  1331. val |= (sel->value << periph_clk_source_shift(c));
  1332. if (c->refcnt)
  1333. clk_enable(p);
  1334. clk_writel_delay(val, c->reg);
  1335. if (c->refcnt && c->parent)
  1336. clk_disable(c->parent);
  1337. clk_reparent(c, p);
  1338. return 0;
  1339. }
  1340. }
  1341. return -EINVAL;
  1342. }
  1343. static int tegra30_periph_clk_set_rate(struct clk *c, unsigned long rate)
  1344. {
  1345. u32 val;
  1346. int divider;
  1347. unsigned long parent_rate = clk_get_rate(c->parent);
  1348. if (c->flags & DIV_U71) {
  1349. divider = clk_div71_get_divider(
  1350. parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
  1351. if (divider >= 0) {
  1352. val = clk_readl(c->reg);
  1353. val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
  1354. val |= divider;
  1355. if (c->flags & DIV_U71_UART) {
  1356. if (divider)
  1357. val |= PERIPH_CLK_UART_DIV_ENB;
  1358. else
  1359. val &= ~PERIPH_CLK_UART_DIV_ENB;
  1360. }
  1361. clk_writel_delay(val, c->reg);
  1362. c->div = divider + 2;
  1363. c->mul = 2;
  1364. return 0;
  1365. }
  1366. } else if (c->flags & DIV_U16) {
  1367. divider = clk_div16_get_divider(parent_rate, rate);
  1368. if (divider >= 0) {
  1369. val = clk_readl(c->reg);
  1370. val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
  1371. val |= divider;
  1372. clk_writel_delay(val, c->reg);
  1373. c->div = divider + 1;
  1374. c->mul = 1;
  1375. return 0;
  1376. }
  1377. } else if (parent_rate <= rate) {
  1378. c->div = 1;
  1379. c->mul = 1;
  1380. return 0;
  1381. }
  1382. return -EINVAL;
  1383. }
  1384. static long tegra30_periph_clk_round_rate(struct clk *c,
  1385. unsigned long rate)
  1386. {
  1387. int divider;
  1388. unsigned long parent_rate = clk_get_rate(c->parent);
  1389. pr_debug("%s: %s %lu\n", __func__, c->name, rate);
  1390. if (c->flags & DIV_U71) {
  1391. divider = clk_div71_get_divider(
  1392. parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
  1393. if (divider < 0)
  1394. return divider;
  1395. return DIV_ROUND_UP(parent_rate * 2, divider + 2);
  1396. } else if (c->flags & DIV_U16) {
  1397. divider = clk_div16_get_divider(parent_rate, rate);
  1398. if (divider < 0)
  1399. return divider;
  1400. return DIV_ROUND_UP(parent_rate, divider + 1);
  1401. }
  1402. return -EINVAL;
  1403. }
  1404. struct clk_ops tegra30_periph_clk_ops = {
  1405. .init = &tegra30_periph_clk_init,
  1406. .enable = &tegra30_periph_clk_enable,
  1407. .disable = &tegra30_periph_clk_disable,
  1408. .set_parent = &tegra30_periph_clk_set_parent,
  1409. .set_rate = &tegra30_periph_clk_set_rate,
  1410. .round_rate = &tegra30_periph_clk_round_rate,
  1411. .reset = &tegra30_periph_clk_reset,
  1412. };
  1413. /* Periph extended clock configuration ops */
  1414. static int
  1415. tegra30_vi_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
  1416. {
  1417. if (p == TEGRA_CLK_VI_INP_SEL) {
  1418. u32 val = clk_readl(c->reg);
  1419. val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
  1420. val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
  1421. PERIPH_CLK_VI_SEL_EX_MASK;
  1422. clk_writel(val, c->reg);
  1423. return 0;
  1424. }
  1425. return -EINVAL;
  1426. }
  1427. struct clk_ops tegra_vi_clk_ops = {
  1428. .init = &tegra30_periph_clk_init,
  1429. .enable = &tegra30_periph_clk_enable,
  1430. .disable = &tegra30_periph_clk_disable,
  1431. .set_parent = &tegra30_periph_clk_set_parent,
  1432. .set_rate = &tegra30_periph_clk_set_rate,
  1433. .round_rate = &tegra30_periph_clk_round_rate,
  1434. .clk_cfg_ex = &tegra30_vi_clk_cfg_ex,
  1435. .reset = &tegra30_periph_clk_reset,
  1436. };
  1437. static int
  1438. tegra30_nand_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
  1439. {
  1440. if (p == TEGRA_CLK_NAND_PAD_DIV2_ENB) {
  1441. u32 val = clk_readl(c->reg);
  1442. if (setting)
  1443. val |= PERIPH_CLK_NAND_DIV_EX_ENB;
  1444. else
  1445. val &= ~PERIPH_CLK_NAND_DIV_EX_ENB;
  1446. clk_writel(val, c->reg);
  1447. return 0;
  1448. }
  1449. return -EINVAL;
  1450. }
  1451. struct clk_ops tegra_nand_clk_ops = {
  1452. .init = &tegra30_periph_clk_init,
  1453. .enable = &tegra30_periph_clk_enable,
  1454. .disable = &tegra30_periph_clk_disable,
  1455. .set_parent = &tegra30_periph_clk_set_parent,
  1456. .set_rate = &tegra30_periph_clk_set_rate,
  1457. .round_rate = &tegra30_periph_clk_round_rate,
  1458. .clk_cfg_ex = &tegra30_nand_clk_cfg_ex,
  1459. .reset = &tegra30_periph_clk_reset,
  1460. };
  1461. static int
  1462. tegra30_dtv_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
  1463. {
  1464. if (p == TEGRA_CLK_DTV_INVERT) {
  1465. u32 val = clk_readl(c->reg);
  1466. if (setting)
  1467. val |= PERIPH_CLK_DTV_POLARITY_INV;
  1468. else
  1469. val &= ~PERIPH_CLK_DTV_POLARITY_INV;
  1470. clk_writel(val, c->reg);
  1471. return 0;
  1472. }
  1473. return -EINVAL;
  1474. }
  1475. struct clk_ops tegra_dtv_clk_ops = {
  1476. .init = &tegra30_periph_clk_init,
  1477. .enable = &tegra30_periph_clk_enable,
  1478. .disable = &tegra30_periph_clk_disable,
  1479. .set_parent = &tegra30_periph_clk_set_parent,
  1480. .set_rate = &tegra30_periph_clk_set_rate,
  1481. .round_rate = &tegra30_periph_clk_round_rate,
  1482. .clk_cfg_ex = &tegra30_dtv_clk_cfg_ex,
  1483. .reset = &tegra30_periph_clk_reset,
  1484. };
  1485. static int tegra30_dsib_clk_set_parent(struct clk *c, struct clk *p)
  1486. {
  1487. const struct clk_mux_sel *sel;
  1488. struct clk *d = tegra_get_clock_by_name("pll_d");
  1489. pr_debug("%s: %s %s\n", __func__, c->name, p->name);
  1490. for (sel = c->inputs; sel->input != NULL; sel++) {
  1491. if (sel->input == p) {
  1492. if (c->refcnt)
  1493. clk_enable(p);
  1494. /* The DSIB parent selection bit is in PLLD base
  1495. register - can not do direct r-m-w, must be
  1496. protected by PLLD lock */
  1497. tegra_clk_cfg_ex(
  1498. d, TEGRA_CLK_PLLD_MIPI_MUX_SEL, sel->value);
  1499. if (c->refcnt && c->parent)
  1500. clk_disable(c->parent);
  1501. clk_reparent(c, p);
  1502. return 0;
  1503. }
  1504. }
  1505. return -EINVAL;
  1506. }
  1507. struct clk_ops tegra_dsib_clk_ops = {
  1508. .init = &tegra30_periph_clk_init,
  1509. .enable = &tegra30_periph_clk_enable,
  1510. .disable = &tegra30_periph_clk_disable,
  1511. .set_parent = &tegra30_dsib_clk_set_parent,
  1512. .set_rate = &tegra30_periph_clk_set_rate,
  1513. .round_rate = &tegra30_periph_clk_round_rate,
  1514. .reset = &tegra30_periph_clk_reset,
  1515. };
  1516. /* pciex clock support only reset function */
  1517. struct clk_ops tegra_pciex_clk_ops = {
  1518. .reset = tegra30_periph_clk_reset,
  1519. };
  1520. /* Output clock ops */
  1521. static DEFINE_SPINLOCK(clk_out_lock);
  1522. static void tegra30_clk_out_init(struct clk *c)
  1523. {
  1524. const struct clk_mux_sel *mux = 0;
  1525. const struct clk_mux_sel *sel;
  1526. u32 val = pmc_readl(c->reg);
  1527. c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
  1528. c->mul = 1;
  1529. c->div = 1;
  1530. for (sel = c->inputs; sel->input != NULL; sel++) {
  1531. if (((val & periph_clk_source_mask(c)) >>
  1532. periph_clk_source_shift(c)) == sel->value)
  1533. mux = sel;
  1534. }
  1535. BUG_ON(!mux);
  1536. c->parent = mux->input;
  1537. }
  1538. static int tegra30_clk_out_enable(struct clk *c)
  1539. {
  1540. u32 val;
  1541. unsigned long flags;
  1542. pr_debug("%s on clock %s\n", __func__, c->name);
  1543. spin_lock_irqsave(&clk_out_lock, flags);
  1544. val = pmc_readl(c->reg);
  1545. val |= (0x1 << c->u.periph.clk_num);
  1546. pmc_writel(val, c->reg);
  1547. spin_unlock_irqrestore(&clk_out_lock, flags);
  1548. return 0;
  1549. }
  1550. static void tegra30_clk_out_disable(struct clk *c)
  1551. {
  1552. u32 val;
  1553. unsigned long flags;
  1554. pr_debug("%s on clock %s\n", __func__, c->name);
  1555. spin_lock_irqsave(&clk_out_lock, flags);
  1556. val = pmc_readl(c->reg);
  1557. val &= ~(0x1 << c->u.periph.clk_num);
  1558. pmc_writel(val, c->reg);
  1559. spin_unlock_irqrestore(&clk_out_lock, flags);
  1560. }
  1561. static int tegra30_clk_out_set_parent(struct clk *c, struct clk *p)
  1562. {
  1563. u32 val;
  1564. unsigned long flags;
  1565. const struct clk_mux_sel *sel;
  1566. pr_debug("%s: %s %s\n", __func__, c->name, p->name);
  1567. for (sel = c->inputs; sel->input != NULL; sel++) {
  1568. if (sel->input == p) {
  1569. if (c->refcnt)
  1570. clk_enable(p);
  1571. spin_lock_irqsave(&clk_out_lock, flags);
  1572. val = pmc_readl(c->reg);
  1573. val &= ~periph_clk_source_mask(c);
  1574. val |= (sel->value << periph_clk_source_shift(c));
  1575. pmc_writel(val, c->reg);
  1576. spin_unlock_irqrestore(&clk_out_lock, flags);
  1577. if (c->refcnt && c->parent)
  1578. clk_disable(c->parent);
  1579. clk_reparent(c, p);
  1580. return 0;
  1581. }
  1582. }
  1583. return -EINVAL;
  1584. }
  1585. struct clk_ops tegra_clk_out_ops = {
  1586. .init = &tegra30_clk_out_init,
  1587. .enable = &tegra30_clk_out_enable,
  1588. .disable = &tegra30_clk_out_disable,
  1589. .set_parent = &tegra30_clk_out_set_parent,
  1590. };
  1591. /* Clock doubler ops */
  1592. static void tegra30_clk_double_init(struct clk *c)
  1593. {
  1594. u32 val = clk_readl(c->reg);
  1595. c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
  1596. c->div = 1;
  1597. c->state = ON;
  1598. if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
  1599. c->state = OFF;
  1600. };
  1601. static int tegra30_clk_double_set_rate(struct clk *c, unsigned long rate)
  1602. {
  1603. u32 val;
  1604. unsigned long parent_rate = clk_get_rate(c->parent);
  1605. if (rate == parent_rate) {
  1606. val = clk_readl(c->reg) | (0x1 << c->reg_shift);
  1607. clk_writel(val, c->reg);
  1608. c->mul = 1;
  1609. c->div = 1;
  1610. return 0;
  1611. } else if (rate == 2 * parent_rate) {
  1612. val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
  1613. clk_writel(val, c->reg);
  1614. c->mul = 2;
  1615. c->div = 1;
  1616. return 0;
  1617. }
  1618. return -EINVAL;
  1619. }
  1620. struct clk_ops tegra30_clk_double_ops = {
  1621. .init = &tegra30_clk_double_init,
  1622. .enable = &tegra30_periph_clk_enable,
  1623. .disable = &tegra30_periph_clk_disable,
  1624. .set_rate = &tegra30_clk_double_set_rate,
  1625. };
  1626. /* Audio sync clock ops */
  1627. static int tegra30_sync_source_set_rate(struct clk *c, unsigned long rate)
  1628. {
  1629. c->rate = rate;
  1630. return 0;
  1631. }
  1632. struct clk_ops tegra_sync_source_ops = {
  1633. .set_rate = &tegra30_sync_source_set_rate,
  1634. };
  1635. static void tegra30_audio_sync_clk_init(struct clk *c)
  1636. {
  1637. int source;
  1638. const struct clk_mux_sel *sel;
  1639. u32 val = clk_readl(c->reg);
  1640. c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
  1641. source = val & AUDIO_SYNC_SOURCE_MASK;
  1642. for (sel = c->inputs; sel->input != NULL; sel++)
  1643. if (sel->value == source)
  1644. break;
  1645. BUG_ON(sel->input == NULL);
  1646. c->parent = sel->input;
  1647. }
  1648. static int tegra30_audio_sync_clk_enable(struct clk *c)
  1649. {
  1650. u32 val = clk_readl(c->reg);
  1651. clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
  1652. return 0;
  1653. }
  1654. static void tegra30_audio_sync_clk_disable(struct clk *c)
  1655. {
  1656. u32 val = clk_readl(c->reg);
  1657. clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
  1658. }
  1659. static int tegra30_audio_sync_clk_set_parent(struct clk *c, struct clk *p)
  1660. {
  1661. u32 val;
  1662. const struct clk_mux_sel *sel;
  1663. for (sel = c->inputs; sel->input != NULL; sel++) {
  1664. if (sel->input == p) {
  1665. val = clk_readl(c->reg);
  1666. val &= ~AUDIO_SYNC_SOURCE_MASK;
  1667. val |= sel->value;
  1668. if (c->refcnt)
  1669. clk_enable(p);
  1670. clk_writel(val, c->reg);
  1671. if (c->refcnt && c->parent)
  1672. clk_disable(c->parent);
  1673. clk_reparent(c, p);
  1674. return 0;
  1675. }
  1676. }
  1677. return -EINVAL;
  1678. }
  1679. struct clk_ops tegra30_audio_sync_clk_ops = {
  1680. .init = tegra30_audio_sync_clk_init,
  1681. .enable = tegra30_audio_sync_clk_enable,
  1682. .disable = tegra30_audio_sync_clk_disable,
  1683. .set_parent = tegra30_audio_sync_clk_set_parent,
  1684. };
  1685. /* cml0 (pcie), and cml1 (sata) clock ops */
  1686. static void tegra30_cml_clk_init(struct clk *c)
  1687. {
  1688. u32 val = clk_readl(c->reg);
  1689. c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
  1690. }
  1691. static int tegra30_cml_clk_enable(struct clk *c)
  1692. {
  1693. u32 val = clk_readl(c->reg);
  1694. val |= (0x1 << c->u.periph.clk_num);
  1695. clk_writel(val, c->reg);
  1696. return 0;
  1697. }
  1698. static void tegra30_cml_clk_disable(struct clk *c)
  1699. {
  1700. u32 val = clk_readl(c->reg);
  1701. val &= ~(0x1 << c->u.periph.clk_num);
  1702. clk_writel(val, c->reg);
  1703. }
  1704. struct clk_ops tegra_cml_clk_ops = {
  1705. .init = &tegra30_cml_clk_init,
  1706. .enable = &tegra30_cml_clk_enable,
  1707. .disable = &tegra30_cml_clk_disable,
  1708. };