tegra20_clocks.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /*
  2. * arch/arm/mach-tegra/tegra20_clocks.c
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. * Copyright (c) 2010-2012 NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * Author:
  8. * Colin Cross <ccross@google.com>
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  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/io.h>
  26. #include <linux/clkdev.h>
  27. #include <linux/clk.h>
  28. #include "clock.h"
  29. #include "fuse.h"
  30. #include "iomap.h"
  31. #include "tegra2_emc.h"
  32. #include "tegra_cpu_car.h"
  33. #define RST_DEVICES 0x004
  34. #define RST_DEVICES_SET 0x300
  35. #define RST_DEVICES_CLR 0x304
  36. #define RST_DEVICES_NUM 3
  37. #define CLK_OUT_ENB 0x010
  38. #define CLK_OUT_ENB_SET 0x320
  39. #define CLK_OUT_ENB_CLR 0x324
  40. #define CLK_OUT_ENB_NUM 3
  41. #define CLK_MASK_ARM 0x44
  42. #define MISC_CLK_ENB 0x48
  43. #define OSC_CTRL 0x50
  44. #define OSC_CTRL_OSC_FREQ_MASK (3<<30)
  45. #define OSC_CTRL_OSC_FREQ_13MHZ (0<<30)
  46. #define OSC_CTRL_OSC_FREQ_19_2MHZ (1<<30)
  47. #define OSC_CTRL_OSC_FREQ_12MHZ (2<<30)
  48. #define OSC_CTRL_OSC_FREQ_26MHZ (3<<30)
  49. #define OSC_CTRL_MASK (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
  50. #define OSC_FREQ_DET 0x58
  51. #define OSC_FREQ_DET_TRIG (1<<31)
  52. #define OSC_FREQ_DET_STATUS 0x5C
  53. #define OSC_FREQ_DET_BUSY (1<<31)
  54. #define OSC_FREQ_DET_CNT_MASK 0xFFFF
  55. #define PERIPH_CLK_SOURCE_I2S1 0x100
  56. #define PERIPH_CLK_SOURCE_EMC 0x19c
  57. #define PERIPH_CLK_SOURCE_OSC 0x1fc
  58. #define PERIPH_CLK_SOURCE_NUM \
  59. ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
  60. #define PERIPH_CLK_SOURCE_MASK (3<<30)
  61. #define PERIPH_CLK_SOURCE_SHIFT 30
  62. #define PERIPH_CLK_SOURCE_PWM_MASK (7<<28)
  63. #define PERIPH_CLK_SOURCE_PWM_SHIFT 28
  64. #define PERIPH_CLK_SOURCE_ENABLE (1<<28)
  65. #define PERIPH_CLK_SOURCE_DIVU71_MASK 0xFF
  66. #define PERIPH_CLK_SOURCE_DIVU16_MASK 0xFFFF
  67. #define PERIPH_CLK_SOURCE_DIV_SHIFT 0
  68. #define SDMMC_CLK_INT_FB_SEL (1 << 23)
  69. #define SDMMC_CLK_INT_FB_DLY_SHIFT 16
  70. #define SDMMC_CLK_INT_FB_DLY_MASK (0xF << SDMMC_CLK_INT_FB_DLY_SHIFT)
  71. #define PLL_BASE 0x0
  72. #define PLL_BASE_BYPASS (1<<31)
  73. #define PLL_BASE_ENABLE (1<<30)
  74. #define PLL_BASE_REF_ENABLE (1<<29)
  75. #define PLL_BASE_OVERRIDE (1<<28)
  76. #define PLL_BASE_DIVP_MASK (0x7<<20)
  77. #define PLL_BASE_DIVP_SHIFT 20
  78. #define PLL_BASE_DIVN_MASK (0x3FF<<8)
  79. #define PLL_BASE_DIVN_SHIFT 8
  80. #define PLL_BASE_DIVM_MASK (0x1F)
  81. #define PLL_BASE_DIVM_SHIFT 0
  82. #define PLL_OUT_RATIO_MASK (0xFF<<8)
  83. #define PLL_OUT_RATIO_SHIFT 8
  84. #define PLL_OUT_OVERRIDE (1<<2)
  85. #define PLL_OUT_CLKEN (1<<1)
  86. #define PLL_OUT_RESET_DISABLE (1<<0)
  87. #define PLL_MISC(c) (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
  88. #define PLL_MISC_DCCON_SHIFT 20
  89. #define PLL_MISC_CPCON_SHIFT 8
  90. #define PLL_MISC_CPCON_MASK (0xF<<PLL_MISC_CPCON_SHIFT)
  91. #define PLL_MISC_LFCON_SHIFT 4
  92. #define PLL_MISC_LFCON_MASK (0xF<<PLL_MISC_LFCON_SHIFT)
  93. #define PLL_MISC_VCOCON_SHIFT 0
  94. #define PLL_MISC_VCOCON_MASK (0xF<<PLL_MISC_VCOCON_SHIFT)
  95. #define PLLU_BASE_POST_DIV (1<<20)
  96. #define PLLD_MISC_CLKENABLE (1<<30)
  97. #define PLLD_MISC_DIV_RST (1<<23)
  98. #define PLLD_MISC_DCCON_SHIFT 12
  99. #define PLLE_MISC_READY (1 << 15)
  100. #define PERIPH_CLK_TO_ENB_REG(c) ((c->u.periph.clk_num / 32) * 4)
  101. #define PERIPH_CLK_TO_ENB_SET_REG(c) ((c->u.periph.clk_num / 32) * 8)
  102. #define PERIPH_CLK_TO_ENB_BIT(c) (1 << (c->u.periph.clk_num % 32))
  103. #define SUPER_CLK_MUX 0x00
  104. #define SUPER_STATE_SHIFT 28
  105. #define SUPER_STATE_MASK (0xF << SUPER_STATE_SHIFT)
  106. #define SUPER_STATE_STANDBY (0x0 << SUPER_STATE_SHIFT)
  107. #define SUPER_STATE_IDLE (0x1 << SUPER_STATE_SHIFT)
  108. #define SUPER_STATE_RUN (0x2 << SUPER_STATE_SHIFT)
  109. #define SUPER_STATE_IRQ (0x3 << SUPER_STATE_SHIFT)
  110. #define SUPER_STATE_FIQ (0x4 << SUPER_STATE_SHIFT)
  111. #define SUPER_SOURCE_MASK 0xF
  112. #define SUPER_FIQ_SOURCE_SHIFT 12
  113. #define SUPER_IRQ_SOURCE_SHIFT 8
  114. #define SUPER_RUN_SOURCE_SHIFT 4
  115. #define SUPER_IDLE_SOURCE_SHIFT 0
  116. #define SUPER_CLK_DIVIDER 0x04
  117. #define BUS_CLK_DISABLE (1<<3)
  118. #define BUS_CLK_DIV_MASK 0x3
  119. #define PMC_CTRL 0x0
  120. #define PMC_CTRL_BLINK_ENB (1 << 7)
  121. #define PMC_DPD_PADS_ORIDE 0x1c
  122. #define PMC_DPD_PADS_ORIDE_BLINK_ENB (1 << 20)
  123. #define PMC_BLINK_TIMER_DATA_ON_SHIFT 0
  124. #define PMC_BLINK_TIMER_DATA_ON_MASK 0x7fff
  125. #define PMC_BLINK_TIMER_ENB (1 << 15)
  126. #define PMC_BLINK_TIMER_DATA_OFF_SHIFT 16
  127. #define PMC_BLINK_TIMER_DATA_OFF_MASK 0xffff
  128. /* Tegra CPU clock and reset control regs */
  129. #define TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX 0x4c
  130. #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET 0x340
  131. #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR 0x344
  132. #define CPU_CLOCK(cpu) (0x1 << (8 + cpu))
  133. #define CPU_RESET(cpu) (0x1111ul << (cpu))
  134. static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
  135. static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
  136. /*
  137. * Some clocks share a register with other clocks. Any clock op that
  138. * non-atomically modifies a register used by another clock must lock
  139. * clock_register_lock first.
  140. */
  141. static DEFINE_SPINLOCK(clock_register_lock);
  142. /*
  143. * Some peripheral clocks share an enable bit, so refcount the enable bits
  144. * in registers CLK_ENABLE_L, CLK_ENABLE_H, and CLK_ENABLE_U
  145. */
  146. static int tegra_periph_clk_enable_refcount[3 * 32];
  147. #define clk_writel(value, reg) \
  148. __raw_writel(value, reg_clk_base + (reg))
  149. #define clk_readl(reg) \
  150. __raw_readl(reg_clk_base + (reg))
  151. #define pmc_writel(value, reg) \
  152. __raw_writel(value, reg_pmc_base + (reg))
  153. #define pmc_readl(reg) \
  154. __raw_readl(reg_pmc_base + (reg))
  155. static unsigned long clk_measure_input_freq(void)
  156. {
  157. u32 clock_autodetect;
  158. clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
  159. do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
  160. clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
  161. if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
  162. return 12000000;
  163. } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
  164. return 13000000;
  165. } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
  166. return 19200000;
  167. } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
  168. return 26000000;
  169. } else {
  170. pr_err("%s: Unexpected clock autodetect value %d",
  171. __func__, clock_autodetect);
  172. BUG();
  173. return 0;
  174. }
  175. }
  176. static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate)
  177. {
  178. s64 divider_u71 = parent_rate * 2;
  179. divider_u71 += rate - 1;
  180. do_div(divider_u71, rate);
  181. if (divider_u71 - 2 < 0)
  182. return 0;
  183. if (divider_u71 - 2 > 255)
  184. return -EINVAL;
  185. return divider_u71 - 2;
  186. }
  187. static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
  188. {
  189. s64 divider_u16;
  190. divider_u16 = parent_rate;
  191. divider_u16 += rate - 1;
  192. do_div(divider_u16, rate);
  193. if (divider_u16 - 1 < 0)
  194. return 0;
  195. if (divider_u16 - 1 > 0xFFFF)
  196. return -EINVAL;
  197. return divider_u16 - 1;
  198. }
  199. static unsigned long tegra_clk_fixed_recalc_rate(struct clk_hw *hw,
  200. unsigned long parent_rate)
  201. {
  202. return to_clk_tegra(hw)->fixed_rate;
  203. }
  204. struct clk_ops tegra_clk_32k_ops = {
  205. .recalc_rate = tegra_clk_fixed_recalc_rate,
  206. };
  207. /* clk_m functions */
  208. static unsigned long tegra20_clk_m_recalc_rate(struct clk_hw *hw,
  209. unsigned long prate)
  210. {
  211. if (!to_clk_tegra(hw)->fixed_rate)
  212. to_clk_tegra(hw)->fixed_rate = clk_measure_input_freq();
  213. return to_clk_tegra(hw)->fixed_rate;
  214. }
  215. static void tegra20_clk_m_init(struct clk_hw *hw)
  216. {
  217. struct clk_tegra *c = to_clk_tegra(hw);
  218. u32 osc_ctrl = clk_readl(OSC_CTRL);
  219. u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
  220. switch (c->fixed_rate) {
  221. case 12000000:
  222. auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
  223. break;
  224. case 13000000:
  225. auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
  226. break;
  227. case 19200000:
  228. auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
  229. break;
  230. case 26000000:
  231. auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
  232. break;
  233. default:
  234. BUG();
  235. }
  236. clk_writel(auto_clock_control, OSC_CTRL);
  237. }
  238. struct clk_ops tegra_clk_m_ops = {
  239. .init = tegra20_clk_m_init,
  240. .recalc_rate = tegra20_clk_m_recalc_rate,
  241. };
  242. /* super clock functions */
  243. /* "super clocks" on tegra have two-stage muxes and a clock skipping
  244. * super divider. We will ignore the clock skipping divider, since we
  245. * can't lower the voltage when using the clock skip, but we can if we
  246. * lower the PLL frequency.
  247. */
  248. static int tegra20_super_clk_is_enabled(struct clk_hw *hw)
  249. {
  250. struct clk_tegra *c = to_clk_tegra(hw);
  251. u32 val;
  252. val = clk_readl(c->reg + SUPER_CLK_MUX);
  253. BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
  254. ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
  255. c->state = ON;
  256. return c->state;
  257. }
  258. static int tegra20_super_clk_enable(struct clk_hw *hw)
  259. {
  260. struct clk_tegra *c = to_clk_tegra(hw);
  261. clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
  262. return 0;
  263. }
  264. static void tegra20_super_clk_disable(struct clk_hw *hw)
  265. {
  266. pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
  267. /* oops - don't disable the CPU clock! */
  268. BUG();
  269. }
  270. static u8 tegra20_super_clk_get_parent(struct clk_hw *hw)
  271. {
  272. struct clk_tegra *c = to_clk_tegra(hw);
  273. int val = clk_readl(c->reg + SUPER_CLK_MUX);
  274. int source;
  275. int shift;
  276. BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
  277. ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
  278. shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
  279. SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
  280. source = (val >> shift) & SUPER_SOURCE_MASK;
  281. return source;
  282. }
  283. static int tegra20_super_clk_set_parent(struct clk_hw *hw, u8 index)
  284. {
  285. struct clk_tegra *c = to_clk_tegra(hw);
  286. u32 val = clk_readl(c->reg + SUPER_CLK_MUX);
  287. int shift;
  288. BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
  289. ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
  290. shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
  291. SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
  292. val &= ~(SUPER_SOURCE_MASK << shift);
  293. val |= index << shift;
  294. clk_writel(val, c->reg);
  295. return 0;
  296. }
  297. /* FIX ME: Need to switch parents to change the source PLL rate */
  298. static unsigned long tegra20_super_clk_recalc_rate(struct clk_hw *hw,
  299. unsigned long prate)
  300. {
  301. return prate;
  302. }
  303. static long tegra20_super_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  304. unsigned long *prate)
  305. {
  306. return *prate;
  307. }
  308. static int tegra20_super_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  309. unsigned long parent_rate)
  310. {
  311. return 0;
  312. }
  313. struct clk_ops tegra_super_ops = {
  314. .is_enabled = tegra20_super_clk_is_enabled,
  315. .enable = tegra20_super_clk_enable,
  316. .disable = tegra20_super_clk_disable,
  317. .set_parent = tegra20_super_clk_set_parent,
  318. .get_parent = tegra20_super_clk_get_parent,
  319. .set_rate = tegra20_super_clk_set_rate,
  320. .round_rate = tegra20_super_clk_round_rate,
  321. .recalc_rate = tegra20_super_clk_recalc_rate,
  322. };
  323. static unsigned long tegra20_twd_clk_recalc_rate(struct clk_hw *hw,
  324. unsigned long parent_rate)
  325. {
  326. struct clk_tegra *c = to_clk_tegra(hw);
  327. u64 rate = parent_rate;
  328. if (c->mul != 0 && c->div != 0) {
  329. rate *= c->mul;
  330. rate += c->div - 1; /* round up */
  331. do_div(rate, c->div);
  332. }
  333. return rate;
  334. }
  335. struct clk_ops tegra_twd_ops = {
  336. .recalc_rate = tegra20_twd_clk_recalc_rate,
  337. };
  338. static u8 tegra20_cop_clk_get_parent(struct clk_hw *hw)
  339. {
  340. return 0;
  341. }
  342. struct clk_ops tegra_cop_ops = {
  343. .get_parent = tegra20_cop_clk_get_parent,
  344. };
  345. /* virtual cop clock functions. Used to acquire the fake 'cop' clock to
  346. * reset the COP block (i.e. AVP) */
  347. void tegra2_cop_clk_reset(struct clk_hw *hw, bool assert)
  348. {
  349. unsigned long reg = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
  350. pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
  351. clk_writel(1 << 1, reg);
  352. }
  353. /* bus clock functions */
  354. static int tegra20_bus_clk_is_enabled(struct clk_hw *hw)
  355. {
  356. struct clk_tegra *c = to_clk_tegra(hw);
  357. u32 val = clk_readl(c->reg);
  358. c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
  359. return c->state;
  360. }
  361. static int tegra20_bus_clk_enable(struct clk_hw *hw)
  362. {
  363. struct clk_tegra *c = to_clk_tegra(hw);
  364. unsigned long flags;
  365. u32 val;
  366. spin_lock_irqsave(&clock_register_lock, flags);
  367. val = clk_readl(c->reg);
  368. val &= ~(BUS_CLK_DISABLE << c->reg_shift);
  369. clk_writel(val, c->reg);
  370. spin_unlock_irqrestore(&clock_register_lock, flags);
  371. return 0;
  372. }
  373. static void tegra20_bus_clk_disable(struct clk_hw *hw)
  374. {
  375. struct clk_tegra *c = to_clk_tegra(hw);
  376. unsigned long flags;
  377. u32 val;
  378. spin_lock_irqsave(&clock_register_lock, flags);
  379. val = clk_readl(c->reg);
  380. val |= BUS_CLK_DISABLE << c->reg_shift;
  381. clk_writel(val, c->reg);
  382. spin_unlock_irqrestore(&clock_register_lock, flags);
  383. }
  384. static unsigned long tegra20_bus_clk_recalc_rate(struct clk_hw *hw,
  385. unsigned long prate)
  386. {
  387. struct clk_tegra *c = to_clk_tegra(hw);
  388. u32 val = clk_readl(c->reg);
  389. u64 rate = prate;
  390. c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
  391. c->mul = 1;
  392. if (c->mul != 0 && c->div != 0) {
  393. rate *= c->mul;
  394. rate += c->div - 1; /* round up */
  395. do_div(rate, c->div);
  396. }
  397. return rate;
  398. }
  399. static int tegra20_bus_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  400. unsigned long parent_rate)
  401. {
  402. struct clk_tegra *c = to_clk_tegra(hw);
  403. int ret = -EINVAL;
  404. unsigned long flags;
  405. u32 val;
  406. int i;
  407. spin_lock_irqsave(&clock_register_lock, flags);
  408. val = clk_readl(c->reg);
  409. for (i = 1; i <= 4; i++) {
  410. if (rate == parent_rate / i) {
  411. val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
  412. val |= (i - 1) << c->reg_shift;
  413. clk_writel(val, c->reg);
  414. c->div = i;
  415. c->mul = 1;
  416. ret = 0;
  417. break;
  418. }
  419. }
  420. spin_unlock_irqrestore(&clock_register_lock, flags);
  421. return ret;
  422. }
  423. static long tegra20_bus_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  424. unsigned long *prate)
  425. {
  426. unsigned long parent_rate = *prate;
  427. s64 divider;
  428. if (rate >= parent_rate)
  429. return rate;
  430. divider = parent_rate;
  431. divider += rate - 1;
  432. do_div(divider, rate);
  433. if (divider < 0)
  434. return divider;
  435. if (divider > 4)
  436. divider = 4;
  437. do_div(parent_rate, divider);
  438. return parent_rate;
  439. }
  440. struct clk_ops tegra_bus_ops = {
  441. .is_enabled = tegra20_bus_clk_is_enabled,
  442. .enable = tegra20_bus_clk_enable,
  443. .disable = tegra20_bus_clk_disable,
  444. .set_rate = tegra20_bus_clk_set_rate,
  445. .round_rate = tegra20_bus_clk_round_rate,
  446. .recalc_rate = tegra20_bus_clk_recalc_rate,
  447. };
  448. /* Blink output functions */
  449. static int tegra20_blink_clk_is_enabled(struct clk_hw *hw)
  450. {
  451. struct clk_tegra *c = to_clk_tegra(hw);
  452. u32 val;
  453. val = pmc_readl(PMC_CTRL);
  454. c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
  455. return c->state;
  456. }
  457. static unsigned long tegra20_blink_clk_recalc_rate(struct clk_hw *hw,
  458. unsigned long prate)
  459. {
  460. struct clk_tegra *c = to_clk_tegra(hw);
  461. u64 rate = prate;
  462. u32 val;
  463. c->mul = 1;
  464. val = pmc_readl(c->reg);
  465. if (val & PMC_BLINK_TIMER_ENB) {
  466. unsigned int on_off;
  467. on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
  468. PMC_BLINK_TIMER_DATA_ON_MASK;
  469. val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
  470. val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
  471. on_off += val;
  472. /* each tick in the blink timer is 4 32KHz clocks */
  473. c->div = on_off * 4;
  474. } else {
  475. c->div = 1;
  476. }
  477. if (c->mul != 0 && c->div != 0) {
  478. rate *= c->mul;
  479. rate += c->div - 1; /* round up */
  480. do_div(rate, c->div);
  481. }
  482. return rate;
  483. }
  484. static int tegra20_blink_clk_enable(struct clk_hw *hw)
  485. {
  486. u32 val;
  487. val = pmc_readl(PMC_DPD_PADS_ORIDE);
  488. pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
  489. val = pmc_readl(PMC_CTRL);
  490. pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
  491. return 0;
  492. }
  493. static void tegra20_blink_clk_disable(struct clk_hw *hw)
  494. {
  495. u32 val;
  496. val = pmc_readl(PMC_CTRL);
  497. pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
  498. val = pmc_readl(PMC_DPD_PADS_ORIDE);
  499. pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
  500. }
  501. static int tegra20_blink_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  502. unsigned long parent_rate)
  503. {
  504. struct clk_tegra *c = to_clk_tegra(hw);
  505. if (rate >= parent_rate) {
  506. c->div = 1;
  507. pmc_writel(0, c->reg);
  508. } else {
  509. unsigned int on_off;
  510. u32 val;
  511. on_off = DIV_ROUND_UP(parent_rate / 8, rate);
  512. c->div = on_off * 8;
  513. val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
  514. PMC_BLINK_TIMER_DATA_ON_SHIFT;
  515. on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
  516. on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
  517. val |= on_off;
  518. val |= PMC_BLINK_TIMER_ENB;
  519. pmc_writel(val, c->reg);
  520. }
  521. return 0;
  522. }
  523. static long tegra20_blink_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  524. unsigned long *prate)
  525. {
  526. int div;
  527. int mul;
  528. long round_rate = *prate;
  529. mul = 1;
  530. if (rate >= *prate) {
  531. div = 1;
  532. } else {
  533. div = DIV_ROUND_UP(*prate / 8, rate);
  534. div *= 8;
  535. }
  536. round_rate *= mul;
  537. round_rate += div - 1;
  538. do_div(round_rate, div);
  539. return round_rate;
  540. }
  541. struct clk_ops tegra_blink_clk_ops = {
  542. .is_enabled = tegra20_blink_clk_is_enabled,
  543. .enable = tegra20_blink_clk_enable,
  544. .disable = tegra20_blink_clk_disable,
  545. .set_rate = tegra20_blink_clk_set_rate,
  546. .round_rate = tegra20_blink_clk_round_rate,
  547. .recalc_rate = tegra20_blink_clk_recalc_rate,
  548. };
  549. /* PLL Functions */
  550. static int tegra20_pll_clk_wait_for_lock(struct clk_tegra *c)
  551. {
  552. udelay(c->u.pll.lock_delay);
  553. return 0;
  554. }
  555. static int tegra20_pll_clk_is_enabled(struct clk_hw *hw)
  556. {
  557. struct clk_tegra *c = to_clk_tegra(hw);
  558. u32 val = clk_readl(c->reg + PLL_BASE);
  559. c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
  560. return c->state;
  561. }
  562. static unsigned long tegra20_pll_clk_recalc_rate(struct clk_hw *hw,
  563. unsigned long prate)
  564. {
  565. struct clk_tegra *c = to_clk_tegra(hw);
  566. u32 val = clk_readl(c->reg + PLL_BASE);
  567. u64 rate = prate;
  568. if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
  569. const struct clk_pll_freq_table *sel;
  570. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  571. if (sel->input_rate == prate &&
  572. sel->output_rate == c->u.pll.fixed_rate) {
  573. c->mul = sel->n;
  574. c->div = sel->m * sel->p;
  575. break;
  576. }
  577. }
  578. pr_err("Clock %s has unknown fixed frequency\n",
  579. __clk_get_name(hw->clk));
  580. BUG();
  581. } else if (val & PLL_BASE_BYPASS) {
  582. c->mul = 1;
  583. c->div = 1;
  584. } else {
  585. c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
  586. c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
  587. if (c->flags & PLLU)
  588. c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
  589. else
  590. c->div *= (val & PLL_BASE_DIVP_MASK) ? 2 : 1;
  591. }
  592. if (c->mul != 0 && c->div != 0) {
  593. rate *= c->mul;
  594. rate += c->div - 1; /* round up */
  595. do_div(rate, c->div);
  596. }
  597. return rate;
  598. }
  599. static int tegra20_pll_clk_enable(struct clk_hw *hw)
  600. {
  601. struct clk_tegra *c = to_clk_tegra(hw);
  602. u32 val;
  603. pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
  604. val = clk_readl(c->reg + PLL_BASE);
  605. val &= ~PLL_BASE_BYPASS;
  606. val |= PLL_BASE_ENABLE;
  607. clk_writel(val, c->reg + PLL_BASE);
  608. tegra20_pll_clk_wait_for_lock(c);
  609. return 0;
  610. }
  611. static void tegra20_pll_clk_disable(struct clk_hw *hw)
  612. {
  613. struct clk_tegra *c = to_clk_tegra(hw);
  614. u32 val;
  615. pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
  616. val = clk_readl(c->reg);
  617. val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
  618. clk_writel(val, c->reg);
  619. }
  620. static int tegra20_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  621. unsigned long parent_rate)
  622. {
  623. struct clk_tegra *c = to_clk_tegra(hw);
  624. unsigned long input_rate = parent_rate;
  625. const struct clk_pll_freq_table *sel;
  626. u32 val;
  627. pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
  628. if (c->flags & PLL_FIXED) {
  629. int ret = 0;
  630. if (rate != c->u.pll.fixed_rate) {
  631. pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
  632. __func__, __clk_get_name(hw->clk),
  633. c->u.pll.fixed_rate, rate);
  634. ret = -EINVAL;
  635. }
  636. return ret;
  637. }
  638. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
  639. if (sel->input_rate == input_rate && sel->output_rate == rate) {
  640. c->mul = sel->n;
  641. c->div = sel->m * sel->p;
  642. val = clk_readl(c->reg + PLL_BASE);
  643. if (c->flags & PLL_FIXED)
  644. val |= PLL_BASE_OVERRIDE;
  645. val &= ~(PLL_BASE_DIVP_MASK | PLL_BASE_DIVN_MASK |
  646. PLL_BASE_DIVM_MASK);
  647. val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
  648. (sel->n << PLL_BASE_DIVN_SHIFT);
  649. BUG_ON(sel->p < 1 || sel->p > 2);
  650. if (c->flags & PLLU) {
  651. if (sel->p == 1)
  652. val |= PLLU_BASE_POST_DIV;
  653. } else {
  654. if (sel->p == 2)
  655. val |= 1 << PLL_BASE_DIVP_SHIFT;
  656. }
  657. clk_writel(val, c->reg + PLL_BASE);
  658. if (c->flags & PLL_HAS_CPCON) {
  659. val = clk_readl(c->reg + PLL_MISC(c));
  660. val &= ~PLL_MISC_CPCON_MASK;
  661. val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
  662. clk_writel(val, c->reg + PLL_MISC(c));
  663. }
  664. if (c->state == ON)
  665. tegra20_pll_clk_enable(hw);
  666. return 0;
  667. }
  668. }
  669. return -EINVAL;
  670. }
  671. static long tegra20_pll_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  672. unsigned long *prate)
  673. {
  674. struct clk_tegra *c = to_clk_tegra(hw);
  675. const struct clk_pll_freq_table *sel;
  676. unsigned long input_rate = *prate;
  677. u64 output_rate = *prate;
  678. int mul;
  679. int div;
  680. if (c->flags & PLL_FIXED)
  681. return c->u.pll.fixed_rate;
  682. for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++)
  683. if (sel->input_rate == input_rate && sel->output_rate == rate) {
  684. mul = sel->n;
  685. div = sel->m * sel->p;
  686. break;
  687. }
  688. if (sel->input_rate == 0)
  689. return -EINVAL;
  690. output_rate *= mul;
  691. output_rate += div - 1; /* round up */
  692. do_div(output_rate, div);
  693. return output_rate;
  694. }
  695. struct clk_ops tegra_pll_ops = {
  696. .is_enabled = tegra20_pll_clk_is_enabled,
  697. .enable = tegra20_pll_clk_enable,
  698. .disable = tegra20_pll_clk_disable,
  699. .set_rate = tegra20_pll_clk_set_rate,
  700. .recalc_rate = tegra20_pll_clk_recalc_rate,
  701. .round_rate = tegra20_pll_clk_round_rate,
  702. };
  703. static void tegra20_pllx_clk_init(struct clk_hw *hw)
  704. {
  705. struct clk_tegra *c = to_clk_tegra(hw);
  706. if (tegra_sku_id == 7)
  707. c->max_rate = 750000000;
  708. }
  709. struct clk_ops tegra_pllx_ops = {
  710. .init = tegra20_pllx_clk_init,
  711. .is_enabled = tegra20_pll_clk_is_enabled,
  712. .enable = tegra20_pll_clk_enable,
  713. .disable = tegra20_pll_clk_disable,
  714. .set_rate = tegra20_pll_clk_set_rate,
  715. .recalc_rate = tegra20_pll_clk_recalc_rate,
  716. .round_rate = tegra20_pll_clk_round_rate,
  717. };
  718. static int tegra20_plle_clk_enable(struct clk_hw *hw)
  719. {
  720. struct clk_tegra *c = to_clk_tegra(hw);
  721. u32 val;
  722. pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
  723. mdelay(1);
  724. val = clk_readl(c->reg + PLL_BASE);
  725. if (!(val & PLLE_MISC_READY))
  726. return -EBUSY;
  727. val = clk_readl(c->reg + PLL_BASE);
  728. val |= PLL_BASE_ENABLE | PLL_BASE_BYPASS;
  729. clk_writel(val, c->reg + PLL_BASE);
  730. return 0;
  731. }
  732. struct clk_ops tegra_plle_ops = {
  733. .is_enabled = tegra20_pll_clk_is_enabled,
  734. .enable = tegra20_plle_clk_enable,
  735. .set_rate = tegra20_pll_clk_set_rate,
  736. .recalc_rate = tegra20_pll_clk_recalc_rate,
  737. .round_rate = tegra20_pll_clk_round_rate,
  738. };
  739. /* Clock divider ops */
  740. static int tegra20_pll_div_clk_is_enabled(struct clk_hw *hw)
  741. {
  742. struct clk_tegra *c = to_clk_tegra(hw);
  743. u32 val = clk_readl(c->reg);
  744. val >>= c->reg_shift;
  745. c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
  746. if (!(val & PLL_OUT_RESET_DISABLE))
  747. c->state = OFF;
  748. return c->state;
  749. }
  750. static unsigned long tegra20_pll_div_clk_recalc_rate(struct clk_hw *hw,
  751. unsigned long prate)
  752. {
  753. struct clk_tegra *c = to_clk_tegra(hw);
  754. u64 rate = prate;
  755. u32 val = clk_readl(c->reg);
  756. u32 divu71;
  757. val >>= c->reg_shift;
  758. if (c->flags & DIV_U71) {
  759. divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
  760. c->div = (divu71 + 2);
  761. c->mul = 2;
  762. } else if (c->flags & DIV_2) {
  763. c->div = 2;
  764. c->mul = 1;
  765. } else {
  766. c->div = 1;
  767. c->mul = 1;
  768. }
  769. rate *= c->mul;
  770. rate += c->div - 1; /* round up */
  771. do_div(rate, c->div);
  772. return rate;
  773. }
  774. static int tegra20_pll_div_clk_enable(struct clk_hw *hw)
  775. {
  776. struct clk_tegra *c = to_clk_tegra(hw);
  777. unsigned long flags;
  778. u32 new_val;
  779. u32 val;
  780. pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
  781. if (c->flags & DIV_U71) {
  782. spin_lock_irqsave(&clock_register_lock, flags);
  783. val = clk_readl(c->reg);
  784. new_val = val >> c->reg_shift;
  785. new_val &= 0xFFFF;
  786. new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
  787. val &= ~(0xFFFF << c->reg_shift);
  788. val |= new_val << c->reg_shift;
  789. clk_writel(val, c->reg);
  790. spin_unlock_irqrestore(&clock_register_lock, flags);
  791. return 0;
  792. } else if (c->flags & DIV_2) {
  793. BUG_ON(!(c->flags & PLLD));
  794. spin_lock_irqsave(&clock_register_lock, flags);
  795. val = clk_readl(c->reg);
  796. val &= ~PLLD_MISC_DIV_RST;
  797. clk_writel(val, c->reg);
  798. spin_unlock_irqrestore(&clock_register_lock, flags);
  799. return 0;
  800. }
  801. return -EINVAL;
  802. }
  803. static void tegra20_pll_div_clk_disable(struct clk_hw *hw)
  804. {
  805. struct clk_tegra *c = to_clk_tegra(hw);
  806. unsigned long flags;
  807. u32 new_val;
  808. u32 val;
  809. pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
  810. if (c->flags & DIV_U71) {
  811. spin_lock_irqsave(&clock_register_lock, flags);
  812. val = clk_readl(c->reg);
  813. new_val = val >> c->reg_shift;
  814. new_val &= 0xFFFF;
  815. new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
  816. val &= ~(0xFFFF << c->reg_shift);
  817. val |= new_val << c->reg_shift;
  818. clk_writel(val, c->reg);
  819. spin_unlock_irqrestore(&clock_register_lock, flags);
  820. } else if (c->flags & DIV_2) {
  821. BUG_ON(!(c->flags & PLLD));
  822. spin_lock_irqsave(&clock_register_lock, flags);
  823. val = clk_readl(c->reg);
  824. val |= PLLD_MISC_DIV_RST;
  825. clk_writel(val, c->reg);
  826. spin_unlock_irqrestore(&clock_register_lock, flags);
  827. }
  828. }
  829. static int tegra20_pll_div_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  830. unsigned long parent_rate)
  831. {
  832. struct clk_tegra *c = to_clk_tegra(hw);
  833. unsigned long flags;
  834. int divider_u71;
  835. u32 new_val;
  836. u32 val;
  837. pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
  838. if (c->flags & DIV_U71) {
  839. divider_u71 = clk_div71_get_divider(parent_rate, rate);
  840. if (divider_u71 >= 0) {
  841. spin_lock_irqsave(&clock_register_lock, flags);
  842. val = clk_readl(c->reg);
  843. new_val = val >> c->reg_shift;
  844. new_val &= 0xFFFF;
  845. if (c->flags & DIV_U71_FIXED)
  846. new_val |= PLL_OUT_OVERRIDE;
  847. new_val &= ~PLL_OUT_RATIO_MASK;
  848. new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
  849. val &= ~(0xFFFF << c->reg_shift);
  850. val |= new_val << c->reg_shift;
  851. clk_writel(val, c->reg);
  852. c->div = divider_u71 + 2;
  853. c->mul = 2;
  854. spin_unlock_irqrestore(&clock_register_lock, flags);
  855. return 0;
  856. }
  857. } else if (c->flags & DIV_2) {
  858. if (parent_rate == rate * 2)
  859. return 0;
  860. }
  861. return -EINVAL;
  862. }
  863. static long tegra20_pll_div_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  864. unsigned long *prate)
  865. {
  866. struct clk_tegra *c = to_clk_tegra(hw);
  867. unsigned long parent_rate = *prate;
  868. int divider;
  869. pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
  870. if (c->flags & DIV_U71) {
  871. divider = clk_div71_get_divider(parent_rate, rate);
  872. if (divider < 0)
  873. return divider;
  874. return DIV_ROUND_UP(parent_rate * 2, divider + 2);
  875. } else if (c->flags & DIV_2) {
  876. return DIV_ROUND_UP(parent_rate, 2);
  877. }
  878. return -EINVAL;
  879. }
  880. struct clk_ops tegra_pll_div_ops = {
  881. .is_enabled = tegra20_pll_div_clk_is_enabled,
  882. .enable = tegra20_pll_div_clk_enable,
  883. .disable = tegra20_pll_div_clk_disable,
  884. .set_rate = tegra20_pll_div_clk_set_rate,
  885. .round_rate = tegra20_pll_div_clk_round_rate,
  886. .recalc_rate = tegra20_pll_div_clk_recalc_rate,
  887. };
  888. /* Periph clk ops */
  889. static int tegra20_periph_clk_is_enabled(struct clk_hw *hw)
  890. {
  891. struct clk_tegra *c = to_clk_tegra(hw);
  892. c->state = ON;
  893. if (!c->u.periph.clk_num)
  894. goto out;
  895. if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
  896. PERIPH_CLK_TO_ENB_BIT(c)))
  897. c->state = OFF;
  898. if (!(c->flags & PERIPH_NO_RESET))
  899. if (clk_readl(RST_DEVICES + PERIPH_CLK_TO_ENB_REG(c)) &
  900. PERIPH_CLK_TO_ENB_BIT(c))
  901. c->state = OFF;
  902. out:
  903. return c->state;
  904. }
  905. static int tegra20_periph_clk_enable(struct clk_hw *hw)
  906. {
  907. struct clk_tegra *c = to_clk_tegra(hw);
  908. unsigned long flags;
  909. u32 val;
  910. pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
  911. if (!c->u.periph.clk_num)
  912. return 0;
  913. tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
  914. if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
  915. return 0;
  916. spin_lock_irqsave(&clock_register_lock, flags);
  917. clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
  918. CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
  919. if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET))
  920. clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
  921. RST_DEVICES_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
  922. if (c->flags & PERIPH_EMC_ENB) {
  923. /* The EMC peripheral clock has 2 extra enable bits */
  924. /* FIXME: Do they need to be disabled? */
  925. val = clk_readl(c->reg);
  926. val |= 0x3 << 24;
  927. clk_writel(val, c->reg);
  928. }
  929. spin_unlock_irqrestore(&clock_register_lock, flags);
  930. return 0;
  931. }
  932. static void tegra20_periph_clk_disable(struct clk_hw *hw)
  933. {
  934. struct clk_tegra *c = to_clk_tegra(hw);
  935. unsigned long flags;
  936. pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
  937. if (!c->u.periph.clk_num)
  938. return;
  939. tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
  940. if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 0)
  941. return;
  942. spin_lock_irqsave(&clock_register_lock, flags);
  943. clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
  944. CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
  945. spin_unlock_irqrestore(&clock_register_lock, flags);
  946. }
  947. void tegra2_periph_clk_reset(struct clk_hw *hw, bool assert)
  948. {
  949. struct clk_tegra *c = to_clk_tegra(hw);
  950. unsigned long base = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
  951. pr_debug("%s %s on clock %s\n", __func__,
  952. assert ? "assert" : "deassert", __clk_get_name(hw->clk));
  953. BUG_ON(!c->u.periph.clk_num);
  954. if (!(c->flags & PERIPH_NO_RESET))
  955. clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
  956. base + PERIPH_CLK_TO_ENB_SET_REG(c));
  957. }
  958. static int tegra20_periph_clk_set_parent(struct clk_hw *hw, u8 index)
  959. {
  960. struct clk_tegra *c = to_clk_tegra(hw);
  961. u32 val;
  962. u32 mask;
  963. u32 shift;
  964. pr_debug("%s: %s %d\n", __func__, __clk_get_name(hw->clk), index);
  965. if (c->flags & MUX_PWM) {
  966. shift = PERIPH_CLK_SOURCE_PWM_SHIFT;
  967. mask = PERIPH_CLK_SOURCE_PWM_MASK;
  968. } else {
  969. shift = PERIPH_CLK_SOURCE_SHIFT;
  970. mask = PERIPH_CLK_SOURCE_MASK;
  971. }
  972. val = clk_readl(c->reg);
  973. val &= ~mask;
  974. val |= (index) << shift;
  975. clk_writel(val, c->reg);
  976. return 0;
  977. }
  978. static u8 tegra20_periph_clk_get_parent(struct clk_hw *hw)
  979. {
  980. struct clk_tegra *c = to_clk_tegra(hw);
  981. u32 val = clk_readl(c->reg);
  982. u32 mask;
  983. u32 shift;
  984. if (c->flags & MUX_PWM) {
  985. shift = PERIPH_CLK_SOURCE_PWM_SHIFT;
  986. mask = PERIPH_CLK_SOURCE_PWM_MASK;
  987. } else {
  988. shift = PERIPH_CLK_SOURCE_SHIFT;
  989. mask = PERIPH_CLK_SOURCE_MASK;
  990. }
  991. if (c->flags & MUX)
  992. return (val & mask) >> shift;
  993. else
  994. return 0;
  995. }
  996. static unsigned long tegra20_periph_clk_recalc_rate(struct clk_hw *hw,
  997. unsigned long prate)
  998. {
  999. struct clk_tegra *c = to_clk_tegra(hw);
  1000. unsigned long rate = prate;
  1001. u32 val = clk_readl(c->reg);
  1002. if (c->flags & DIV_U71) {
  1003. u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
  1004. c->div = divu71 + 2;
  1005. c->mul = 2;
  1006. } else if (c->flags & DIV_U16) {
  1007. u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
  1008. c->div = divu16 + 1;
  1009. c->mul = 1;
  1010. } else {
  1011. c->div = 1;
  1012. c->mul = 1;
  1013. return rate;
  1014. }
  1015. if (c->mul != 0 && c->div != 0) {
  1016. rate *= c->mul;
  1017. rate += c->div - 1; /* round up */
  1018. do_div(rate, c->div);
  1019. }
  1020. return rate;
  1021. }
  1022. static int tegra20_periph_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  1023. unsigned long parent_rate)
  1024. {
  1025. struct clk_tegra *c = to_clk_tegra(hw);
  1026. u32 val;
  1027. int divider;
  1028. val = clk_readl(c->reg);
  1029. if (c->flags & DIV_U71) {
  1030. divider = clk_div71_get_divider(parent_rate, rate);
  1031. if (divider >= 0) {
  1032. val = clk_readl(c->reg);
  1033. val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
  1034. val |= divider;
  1035. clk_writel(val, c->reg);
  1036. c->div = divider + 2;
  1037. c->mul = 2;
  1038. return 0;
  1039. }
  1040. } else if (c->flags & DIV_U16) {
  1041. divider = clk_div16_get_divider(parent_rate, rate);
  1042. if (divider >= 0) {
  1043. val = clk_readl(c->reg);
  1044. val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
  1045. val |= divider;
  1046. clk_writel(val, c->reg);
  1047. c->div = divider + 1;
  1048. c->mul = 1;
  1049. return 0;
  1050. }
  1051. } else if (parent_rate <= rate) {
  1052. c->div = 1;
  1053. c->mul = 1;
  1054. return 0;
  1055. }
  1056. return -EINVAL;
  1057. }
  1058. static long tegra20_periph_clk_round_rate(struct clk_hw *hw,
  1059. unsigned long rate, unsigned long *prate)
  1060. {
  1061. struct clk_tegra *c = to_clk_tegra(hw);
  1062. unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
  1063. int divider;
  1064. pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
  1065. if (prate)
  1066. parent_rate = *prate;
  1067. if (c->flags & DIV_U71) {
  1068. divider = clk_div71_get_divider(parent_rate, rate);
  1069. if (divider < 0)
  1070. return divider;
  1071. return DIV_ROUND_UP(parent_rate * 2, divider + 2);
  1072. } else if (c->flags & DIV_U16) {
  1073. divider = clk_div16_get_divider(parent_rate, rate);
  1074. if (divider < 0)
  1075. return divider;
  1076. return DIV_ROUND_UP(parent_rate, divider + 1);
  1077. }
  1078. return -EINVAL;
  1079. }
  1080. struct clk_ops tegra_periph_clk_ops = {
  1081. .is_enabled = tegra20_periph_clk_is_enabled,
  1082. .enable = tegra20_periph_clk_enable,
  1083. .disable = tegra20_periph_clk_disable,
  1084. .set_parent = tegra20_periph_clk_set_parent,
  1085. .get_parent = tegra20_periph_clk_get_parent,
  1086. .set_rate = tegra20_periph_clk_set_rate,
  1087. .round_rate = tegra20_periph_clk_round_rate,
  1088. .recalc_rate = tegra20_periph_clk_recalc_rate,
  1089. };
  1090. /* External memory controller clock ops */
  1091. static void tegra20_emc_clk_init(struct clk_hw *hw)
  1092. {
  1093. struct clk_tegra *c = to_clk_tegra(hw);
  1094. c->max_rate = __clk_get_rate(hw->clk);
  1095. }
  1096. static long tegra20_emc_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  1097. unsigned long *prate)
  1098. {
  1099. struct clk_tegra *c = to_clk_tegra(hw);
  1100. long emc_rate;
  1101. long clk_rate;
  1102. /*
  1103. * The slowest entry in the EMC clock table that is at least as
  1104. * fast as rate.
  1105. */
  1106. emc_rate = tegra_emc_round_rate(rate);
  1107. if (emc_rate < 0)
  1108. return c->max_rate;
  1109. /*
  1110. * The fastest rate the PLL will generate that is at most the
  1111. * requested rate.
  1112. */
  1113. clk_rate = tegra20_periph_clk_round_rate(hw, emc_rate, NULL);
  1114. /*
  1115. * If this fails, and emc_rate > clk_rate, it's because the maximum
  1116. * rate in the EMC tables is larger than the maximum rate of the EMC
  1117. * clock. The EMC clock's max rate is the rate it was running when the
  1118. * kernel booted. Such a mismatch is probably due to using the wrong
  1119. * BCT, i.e. using a Tegra20 BCT with an EMC table written for Tegra25.
  1120. */
  1121. WARN_ONCE(emc_rate != clk_rate,
  1122. "emc_rate %ld != clk_rate %ld",
  1123. emc_rate, clk_rate);
  1124. return emc_rate;
  1125. }
  1126. static int tegra20_emc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  1127. unsigned long parent_rate)
  1128. {
  1129. int ret;
  1130. /*
  1131. * The Tegra2 memory controller has an interlock with the clock
  1132. * block that allows memory shadowed registers to be updated,
  1133. * and then transfer them to the main registers at the same
  1134. * time as the clock update without glitches.
  1135. */
  1136. ret = tegra_emc_set_rate(rate);
  1137. if (ret < 0)
  1138. return ret;
  1139. ret = tegra20_periph_clk_set_rate(hw, rate, parent_rate);
  1140. udelay(1);
  1141. return ret;
  1142. }
  1143. struct clk_ops tegra_emc_clk_ops = {
  1144. .init = tegra20_emc_clk_init,
  1145. .is_enabled = tegra20_periph_clk_is_enabled,
  1146. .enable = tegra20_periph_clk_enable,
  1147. .disable = tegra20_periph_clk_disable,
  1148. .set_parent = tegra20_periph_clk_set_parent,
  1149. .get_parent = tegra20_periph_clk_get_parent,
  1150. .set_rate = tegra20_emc_clk_set_rate,
  1151. .round_rate = tegra20_emc_clk_round_rate,
  1152. .recalc_rate = tegra20_periph_clk_recalc_rate,
  1153. };
  1154. /* Clock doubler ops */
  1155. static int tegra20_clk_double_is_enabled(struct clk_hw *hw)
  1156. {
  1157. struct clk_tegra *c = to_clk_tegra(hw);
  1158. c->state = ON;
  1159. if (!c->u.periph.clk_num)
  1160. goto out;
  1161. if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
  1162. PERIPH_CLK_TO_ENB_BIT(c)))
  1163. c->state = OFF;
  1164. out:
  1165. return c->state;
  1166. };
  1167. static unsigned long tegra20_clk_double_recalc_rate(struct clk_hw *hw,
  1168. unsigned long prate)
  1169. {
  1170. struct clk_tegra *c = to_clk_tegra(hw);
  1171. u64 rate = prate;
  1172. c->mul = 2;
  1173. c->div = 1;
  1174. rate *= c->mul;
  1175. rate += c->div - 1; /* round up */
  1176. do_div(rate, c->div);
  1177. return rate;
  1178. }
  1179. static long tegra20_clk_double_round_rate(struct clk_hw *hw, unsigned long rate,
  1180. unsigned long *prate)
  1181. {
  1182. unsigned long output_rate = *prate;
  1183. do_div(output_rate, 2);
  1184. return output_rate;
  1185. }
  1186. static int tegra20_clk_double_set_rate(struct clk_hw *hw, unsigned long rate,
  1187. unsigned long parent_rate)
  1188. {
  1189. if (rate != 2 * parent_rate)
  1190. return -EINVAL;
  1191. return 0;
  1192. }
  1193. struct clk_ops tegra_clk_double_ops = {
  1194. .is_enabled = tegra20_clk_double_is_enabled,
  1195. .enable = tegra20_periph_clk_enable,
  1196. .disable = tegra20_periph_clk_disable,
  1197. .set_rate = tegra20_clk_double_set_rate,
  1198. .recalc_rate = tegra20_clk_double_recalc_rate,
  1199. .round_rate = tegra20_clk_double_round_rate,
  1200. };
  1201. /* Audio sync clock ops */
  1202. static int tegra20_audio_sync_clk_is_enabled(struct clk_hw *hw)
  1203. {
  1204. struct clk_tegra *c = to_clk_tegra(hw);
  1205. u32 val = clk_readl(c->reg);
  1206. c->state = (val & (1<<4)) ? OFF : ON;
  1207. return c->state;
  1208. }
  1209. static int tegra20_audio_sync_clk_enable(struct clk_hw *hw)
  1210. {
  1211. struct clk_tegra *c = to_clk_tegra(hw);
  1212. clk_writel(0, c->reg);
  1213. return 0;
  1214. }
  1215. static void tegra20_audio_sync_clk_disable(struct clk_hw *hw)
  1216. {
  1217. struct clk_tegra *c = to_clk_tegra(hw);
  1218. clk_writel(1, c->reg);
  1219. }
  1220. static u8 tegra20_audio_sync_clk_get_parent(struct clk_hw *hw)
  1221. {
  1222. struct clk_tegra *c = to_clk_tegra(hw);
  1223. u32 val = clk_readl(c->reg);
  1224. int source;
  1225. source = val & 0xf;
  1226. return source;
  1227. }
  1228. static int tegra20_audio_sync_clk_set_parent(struct clk_hw *hw, u8 index)
  1229. {
  1230. struct clk_tegra *c = to_clk_tegra(hw);
  1231. u32 val;
  1232. val = clk_readl(c->reg);
  1233. val &= ~0xf;
  1234. val |= index;
  1235. clk_writel(val, c->reg);
  1236. return 0;
  1237. }
  1238. struct clk_ops tegra_audio_sync_clk_ops = {
  1239. .is_enabled = tegra20_audio_sync_clk_is_enabled,
  1240. .enable = tegra20_audio_sync_clk_enable,
  1241. .disable = tegra20_audio_sync_clk_disable,
  1242. .set_parent = tegra20_audio_sync_clk_set_parent,
  1243. .get_parent = tegra20_audio_sync_clk_get_parent,
  1244. };
  1245. /* cdev1 and cdev2 (dap_mclk1 and dap_mclk2) ops */
  1246. static int tegra20_cdev_clk_is_enabled(struct clk_hw *hw)
  1247. {
  1248. struct clk_tegra *c = to_clk_tegra(hw);
  1249. /* We could un-tristate the cdev1 or cdev2 pingroup here; this is
  1250. * currently done in the pinmux code. */
  1251. c->state = ON;
  1252. BUG_ON(!c->u.periph.clk_num);
  1253. if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
  1254. PERIPH_CLK_TO_ENB_BIT(c)))
  1255. c->state = OFF;
  1256. return c->state;
  1257. }
  1258. static int tegra20_cdev_clk_enable(struct clk_hw *hw)
  1259. {
  1260. struct clk_tegra *c = to_clk_tegra(hw);
  1261. BUG_ON(!c->u.periph.clk_num);
  1262. clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
  1263. CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
  1264. return 0;
  1265. }
  1266. static void tegra20_cdev_clk_disable(struct clk_hw *hw)
  1267. {
  1268. struct clk_tegra *c = to_clk_tegra(hw);
  1269. BUG_ON(!c->u.periph.clk_num);
  1270. clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
  1271. CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
  1272. }
  1273. static unsigned long tegra20_cdev_recalc_rate(struct clk_hw *hw,
  1274. unsigned long prate)
  1275. {
  1276. return to_clk_tegra(hw)->fixed_rate;
  1277. }
  1278. struct clk_ops tegra_cdev_clk_ops = {
  1279. .is_enabled = tegra20_cdev_clk_is_enabled,
  1280. .enable = tegra20_cdev_clk_enable,
  1281. .disable = tegra20_cdev_clk_disable,
  1282. .recalc_rate = tegra20_cdev_recalc_rate,
  1283. };
  1284. /* Tegra20 CPU clock and reset control functions */
  1285. static void tegra20_wait_cpu_in_reset(u32 cpu)
  1286. {
  1287. unsigned int reg;
  1288. do {
  1289. reg = readl(reg_clk_base +
  1290. TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
  1291. cpu_relax();
  1292. } while (!(reg & (1 << cpu))); /* check CPU been reset or not */
  1293. return;
  1294. }
  1295. static void tegra20_put_cpu_in_reset(u32 cpu)
  1296. {
  1297. writel(CPU_RESET(cpu),
  1298. reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
  1299. dmb();
  1300. }
  1301. static void tegra20_cpu_out_of_reset(u32 cpu)
  1302. {
  1303. writel(CPU_RESET(cpu),
  1304. reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR);
  1305. wmb();
  1306. }
  1307. static void tegra20_enable_cpu_clock(u32 cpu)
  1308. {
  1309. unsigned int reg;
  1310. reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
  1311. writel(reg & ~CPU_CLOCK(cpu),
  1312. reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
  1313. barrier();
  1314. reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
  1315. }
  1316. static void tegra20_disable_cpu_clock(u32 cpu)
  1317. {
  1318. unsigned int reg;
  1319. reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
  1320. writel(reg | CPU_CLOCK(cpu),
  1321. reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
  1322. }
  1323. static struct tegra_cpu_car_ops tegra20_cpu_car_ops = {
  1324. .wait_for_reset = tegra20_wait_cpu_in_reset,
  1325. .put_in_reset = tegra20_put_cpu_in_reset,
  1326. .out_of_reset = tegra20_cpu_out_of_reset,
  1327. .enable_clock = tegra20_enable_cpu_clock,
  1328. .disable_clock = tegra20_disable_cpu_clock,
  1329. };
  1330. void __init tegra20_cpu_car_ops_init(void)
  1331. {
  1332. tegra_cpu_car_ops = &tegra20_cpu_car_ops;
  1333. }