clock.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. /* Tegra2 Clock control functions */
  22. #include <asm/io.h>
  23. #include <asm/arch/clk_rst.h>
  24. #include <asm/arch/clock.h>
  25. #include <asm/arch/timer.h>
  26. #include <asm/arch/tegra2.h>
  27. #include <common.h>
  28. #include <div64.h>
  29. /*
  30. * This is our record of the current clock rate of each clock. We don't
  31. * fill all of these in since we are only really interested in clocks which
  32. * we use as parents.
  33. */
  34. static unsigned pll_rate[CLOCK_ID_COUNT];
  35. /*
  36. * The oscillator frequency is fixed to one of four set values. Based on this
  37. * the other clocks are set up appropriately.
  38. */
  39. static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
  40. 13000000,
  41. 19200000,
  42. 12000000,
  43. 26000000,
  44. };
  45. /*
  46. * Clock types that we can use as a source. The Tegra2 has muxes for the
  47. * peripheral clocks, and in most cases there are four options for the clock
  48. * source. This gives us a clock 'type' and exploits what commonality exists
  49. * in the device.
  50. *
  51. * Letters are obvious, except for T which means CLK_M, and S which means the
  52. * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
  53. * datasheet) and PLL_M are different things. The former is the basic
  54. * clock supplied to the SOC from an external oscillator. The latter is the
  55. * memory clock PLL.
  56. *
  57. * See definitions in clock_id in the header file.
  58. */
  59. enum clock_type_id {
  60. CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
  61. CLOCK_TYPE_MCPA, /* and so on */
  62. CLOCK_TYPE_MCPT,
  63. CLOCK_TYPE_PCM,
  64. CLOCK_TYPE_PCMT,
  65. CLOCK_TYPE_PCXTS,
  66. CLOCK_TYPE_PDCT,
  67. CLOCK_TYPE_COUNT,
  68. CLOCK_TYPE_NONE = -1, /* invalid clock type */
  69. };
  70. /* return 1 if a peripheral ID is in range */
  71. #define clock_type_id_isvalid(id) ((id) >= 0 && \
  72. (id) < CLOCK_TYPE_COUNT)
  73. char pllp_valid = 1; /* PLLP is set up correctly */
  74. enum {
  75. CLOCK_MAX_MUX = 4 /* number of source options for each clock */
  76. };
  77. /*
  78. * Clock source mux for each clock type. This just converts our enum into
  79. * a list of mux sources for use by the code. Note that CLOCK_TYPE_PCXTS
  80. * is special as it has 5 sources. Since it also has a different number of
  81. * bits in its register for the source, we just handle it with a special
  82. * case in the code.
  83. */
  84. #define CLK(x) CLOCK_ID_ ## x
  85. static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX] = {
  86. { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC) },
  87. { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO) },
  88. { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC) },
  89. { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE) },
  90. { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC) },
  91. { CLK(PERIPH), CLK(CGENERAL), CLK(XCPU), CLK(OSC) },
  92. { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC) },
  93. };
  94. /*
  95. * Clock peripheral IDs which sadly don't match up with PERIPH_ID. This is
  96. * not in the header file since it is for purely internal use - we want
  97. * callers to use the PERIPH_ID for all access to peripheral clocks to avoid
  98. * confusion bewteen PERIPH_ID_... and PERIPHC_...
  99. *
  100. * We don't call this CLOCK_PERIPH_ID or PERIPH_CLOCK_ID as it would just be
  101. * confusing.
  102. *
  103. * Note to SOC vendors: perhaps define a unified numbering for peripherals and
  104. * use it for reset, clock enable, clock source/divider and even pinmuxing
  105. * if you can.
  106. */
  107. enum periphc_internal_id {
  108. /* 0x00 */
  109. PERIPHC_I2S1,
  110. PERIPHC_I2S2,
  111. PERIPHC_SPDIF_OUT,
  112. PERIPHC_SPDIF_IN,
  113. PERIPHC_PWM,
  114. PERIPHC_SPI1,
  115. PERIPHC_SPI2,
  116. PERIPHC_SPI3,
  117. /* 0x08 */
  118. PERIPHC_XIO,
  119. PERIPHC_I2C1,
  120. PERIPHC_DVC_I2C,
  121. PERIPHC_TWC,
  122. PERIPHC_0c,
  123. PERIPHC_10, /* PERIPHC_SPI1, what is this really? */
  124. PERIPHC_DISP1,
  125. PERIPHC_DISP2,
  126. /* 0x10 */
  127. PERIPHC_CVE,
  128. PERIPHC_IDE0,
  129. PERIPHC_VI,
  130. PERIPHC_1c,
  131. PERIPHC_SDMMC1,
  132. PERIPHC_SDMMC2,
  133. PERIPHC_G3D,
  134. PERIPHC_G2D,
  135. /* 0x18 */
  136. PERIPHC_NDFLASH,
  137. PERIPHC_SDMMC4,
  138. PERIPHC_VFIR,
  139. PERIPHC_EPP,
  140. PERIPHC_MPE,
  141. PERIPHC_MIPI,
  142. PERIPHC_UART1,
  143. PERIPHC_UART2,
  144. /* 0x20 */
  145. PERIPHC_HOST1X,
  146. PERIPHC_21,
  147. PERIPHC_TVO,
  148. PERIPHC_HDMI,
  149. PERIPHC_24,
  150. PERIPHC_TVDAC,
  151. PERIPHC_I2C2,
  152. PERIPHC_EMC,
  153. /* 0x28 */
  154. PERIPHC_UART3,
  155. PERIPHC_29,
  156. PERIPHC_VI_SENSOR,
  157. PERIPHC_2b,
  158. PERIPHC_2c,
  159. PERIPHC_SPI4,
  160. PERIPHC_I2C3,
  161. PERIPHC_SDMMC3,
  162. /* 0x30 */
  163. PERIPHC_UART4,
  164. PERIPHC_UART5,
  165. PERIPHC_VDE,
  166. PERIPHC_OWR,
  167. PERIPHC_NOR,
  168. PERIPHC_CSITE,
  169. PERIPHC_COUNT,
  170. PERIPHC_NONE = -1,
  171. };
  172. /* return 1 if a periphc_internal_id is in range */
  173. #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
  174. (id) < PERIPHC_COUNT)
  175. /*
  176. * Clock type for each peripheral clock source. We put the name in each
  177. * record just so it is easy to match things up
  178. */
  179. #define TYPE(name, type) type
  180. static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
  181. /* 0x00 */
  182. TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
  183. TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
  184. TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
  185. TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PCM),
  186. TYPE(PERIPHC_PWM, CLOCK_TYPE_PCXTS),
  187. TYPE(PERIPHC_SPI1, CLOCK_TYPE_PCMT),
  188. TYPE(PERIPHC_SPI22, CLOCK_TYPE_PCMT),
  189. TYPE(PERIPHC_SPI3, CLOCK_TYPE_PCMT),
  190. /* 0x08 */
  191. TYPE(PERIPHC_XIO, CLOCK_TYPE_PCMT),
  192. TYPE(PERIPHC_I2C1, CLOCK_TYPE_PCMT),
  193. TYPE(PERIPHC_DVC_I2C, CLOCK_TYPE_PCMT),
  194. TYPE(PERIPHC_TWC, CLOCK_TYPE_PCMT),
  195. TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
  196. TYPE(PERIPHC_SPI1, CLOCK_TYPE_PCMT),
  197. TYPE(PERIPHC_DISP1, CLOCK_TYPE_PDCT),
  198. TYPE(PERIPHC_DISP2, CLOCK_TYPE_PDCT),
  199. /* 0x10 */
  200. TYPE(PERIPHC_CVE, CLOCK_TYPE_PDCT),
  201. TYPE(PERIPHC_IDE0, CLOCK_TYPE_PCMT),
  202. TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
  203. TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
  204. TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PCMT),
  205. TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PCMT),
  206. TYPE(PERIPHC_G3D, CLOCK_TYPE_MCPA),
  207. TYPE(PERIPHC_G2D, CLOCK_TYPE_MCPA),
  208. /* 0x18 */
  209. TYPE(PERIPHC_NDFLASH, CLOCK_TYPE_PCMT),
  210. TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PCMT),
  211. TYPE(PERIPHC_VFIR, CLOCK_TYPE_PCMT),
  212. TYPE(PERIPHC_EPP, CLOCK_TYPE_MCPA),
  213. TYPE(PERIPHC_MPE, CLOCK_TYPE_MCPA),
  214. TYPE(PERIPHC_MIPI, CLOCK_TYPE_PCMT),
  215. TYPE(PERIPHC_UART1, CLOCK_TYPE_PCMT),
  216. TYPE(PERIPHC_UART2, CLOCK_TYPE_PCMT),
  217. /* 0x20 */
  218. TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MCPA),
  219. TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
  220. TYPE(PERIPHC_TVO, CLOCK_TYPE_PDCT),
  221. TYPE(PERIPHC_HDMI, CLOCK_TYPE_PDCT),
  222. TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
  223. TYPE(PERIPHC_TVDAC, CLOCK_TYPE_PDCT),
  224. TYPE(PERIPHC_I2C2, CLOCK_TYPE_PCMT),
  225. TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPT),
  226. /* 0x28 */
  227. TYPE(PERIPHC_UART3, CLOCK_TYPE_PCMT),
  228. TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
  229. TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
  230. TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
  231. TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
  232. TYPE(PERIPHC_SPI4, CLOCK_TYPE_PCMT),
  233. TYPE(PERIPHC_I2C3, CLOCK_TYPE_PCMT),
  234. TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PCMT),
  235. /* 0x30 */
  236. TYPE(PERIPHC_UART4, CLOCK_TYPE_PCMT),
  237. TYPE(PERIPHC_UART5, CLOCK_TYPE_PCMT),
  238. TYPE(PERIPHC_VDE, CLOCK_TYPE_PCMT),
  239. TYPE(PERIPHC_OWR, CLOCK_TYPE_PCMT),
  240. TYPE(PERIPHC_NOR, CLOCK_TYPE_PCMT),
  241. TYPE(PERIPHC_CSITE, CLOCK_TYPE_PCMT),
  242. };
  243. /*
  244. * This array translates a periph_id to a periphc_internal_id
  245. *
  246. * Not present/matched up:
  247. * uint vi_sensor; _VI_SENSOR_0, 0x1A8
  248. * SPDIF - which is both 0x08 and 0x0c
  249. *
  250. */
  251. #define NONE(name) (-1)
  252. #define OFFSET(name, value) PERIPHC_ ## name
  253. static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
  254. /* Low word: 31:0 */
  255. NONE(CPU),
  256. NONE(RESERVED1),
  257. NONE(RESERVED2),
  258. NONE(AC97),
  259. NONE(RTC),
  260. NONE(TMR),
  261. PERIPHC_UART1,
  262. PERIPHC_UART2, /* and vfir 0x68 */
  263. /* 0x08 */
  264. NONE(GPIO),
  265. PERIPHC_SDMMC2,
  266. NONE(SPDIF), /* 0x08 and 0x0c, unclear which to use */
  267. PERIPHC_I2S1,
  268. PERIPHC_I2C1,
  269. PERIPHC_NDFLASH,
  270. PERIPHC_SDMMC1,
  271. PERIPHC_SDMMC4,
  272. /* 0x10 */
  273. PERIPHC_TWC,
  274. PERIPHC_PWM,
  275. PERIPHC_I2S2,
  276. PERIPHC_EPP,
  277. PERIPHC_VI,
  278. PERIPHC_G2D,
  279. NONE(USBD),
  280. NONE(ISP),
  281. /* 0x18 */
  282. PERIPHC_G3D,
  283. PERIPHC_IDE0,
  284. PERIPHC_DISP2,
  285. PERIPHC_DISP1,
  286. PERIPHC_HOST1X,
  287. NONE(VCP),
  288. NONE(RESERVED30),
  289. NONE(CACHE2),
  290. /* Middle word: 63:32 */
  291. NONE(MEM),
  292. NONE(AHBDMA),
  293. NONE(APBDMA),
  294. NONE(RESERVED35),
  295. NONE(KBC),
  296. NONE(STAT_MON),
  297. NONE(PMC),
  298. NONE(FUSE),
  299. /* 0x28 */
  300. NONE(KFUSE),
  301. NONE(SBC1), /* SBC1, 0x34, is this SPI1? */
  302. PERIPHC_NOR,
  303. PERIPHC_SPI1,
  304. PERIPHC_SPI2,
  305. PERIPHC_XIO,
  306. PERIPHC_SPI3,
  307. PERIPHC_DVC_I2C,
  308. /* 0x30 */
  309. NONE(DSI),
  310. PERIPHC_TVO, /* also CVE 0x40 */
  311. PERIPHC_MIPI,
  312. PERIPHC_HDMI,
  313. PERIPHC_CSITE,
  314. PERIPHC_TVDAC,
  315. PERIPHC_I2C2,
  316. PERIPHC_UART3,
  317. /* 0x38 */
  318. NONE(RESERVED56),
  319. PERIPHC_EMC,
  320. NONE(USB2),
  321. NONE(USB3),
  322. PERIPHC_MPE,
  323. PERIPHC_VDE,
  324. NONE(BSEA),
  325. NONE(BSEV),
  326. /* Upper word 95:64 */
  327. NONE(SPEEDO),
  328. PERIPHC_UART4,
  329. PERIPHC_UART5,
  330. PERIPHC_I2C3,
  331. PERIPHC_SPI4,
  332. PERIPHC_SDMMC3,
  333. NONE(PCIE),
  334. PERIPHC_OWR,
  335. /* 0x48 */
  336. NONE(AFI),
  337. NONE(CORESIGHT),
  338. NONE(RESERVED74),
  339. NONE(AVPUCQ),
  340. NONE(RESERVED76),
  341. NONE(RESERVED77),
  342. NONE(RESERVED78),
  343. NONE(RESERVED79),
  344. /* 0x50 */
  345. NONE(RESERVED80),
  346. NONE(RESERVED81),
  347. NONE(RESERVED82),
  348. NONE(RESERVED83),
  349. NONE(IRAMA),
  350. NONE(IRAMB),
  351. NONE(IRAMC),
  352. NONE(IRAMD),
  353. /* 0x58 */
  354. NONE(CRAM2),
  355. };
  356. /*
  357. * Get the oscillator frequency, from the corresponding hardware configuration
  358. * field.
  359. */
  360. enum clock_osc_freq clock_get_osc_freq(void)
  361. {
  362. struct clk_rst_ctlr *clkrst =
  363. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  364. u32 reg;
  365. reg = readl(&clkrst->crc_osc_ctrl);
  366. return (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
  367. }
  368. /* Returns a pointer to the registers of the given pll */
  369. static struct clk_pll *get_pll(enum clock_id clkid)
  370. {
  371. struct clk_rst_ctlr *clkrst =
  372. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  373. assert(clock_id_isvalid(clkid));
  374. return &clkrst->crc_pll[clkid];
  375. }
  376. unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
  377. u32 divp, u32 cpcon, u32 lfcon)
  378. {
  379. struct clk_pll *pll = get_pll(clkid);
  380. u32 data;
  381. /*
  382. * We cheat by treating all PLL (except PLLU) in the same fashion.
  383. * This works only because:
  384. * - same fields are always mapped at same offsets, except DCCON
  385. * - DCCON is always 0, doesn't conflict
  386. * - M,N, P of PLLP values are ignored for PLLP
  387. */
  388. data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT);
  389. writel(data, &pll->pll_misc);
  390. data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) |
  391. (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT);
  392. if (clkid == CLOCK_ID_USB)
  393. data |= divp << PLLU_VCO_FREQ_SHIFT;
  394. else
  395. data |= divp << PLL_DIVP_SHIFT;
  396. writel(data, &pll->pll_base);
  397. /* calculate the stable time */
  398. return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
  399. }
  400. /* return 1 if a peripheral ID is in range and valid */
  401. static int clock_periph_id_isvalid(enum periph_id id)
  402. {
  403. if (id < PERIPH_ID_FIRST || id >= PERIPH_ID_COUNT)
  404. printf("Peripheral id %d out of range\n", id);
  405. else {
  406. switch (id) {
  407. case PERIPH_ID_RESERVED1:
  408. case PERIPH_ID_RESERVED2:
  409. case PERIPH_ID_RESERVED30:
  410. case PERIPH_ID_RESERVED35:
  411. case PERIPH_ID_RESERVED56:
  412. case PERIPH_ID_RESERVED74:
  413. case PERIPH_ID_RESERVED76:
  414. case PERIPH_ID_RESERVED77:
  415. case PERIPH_ID_RESERVED78:
  416. case PERIPH_ID_RESERVED79:
  417. case PERIPH_ID_RESERVED80:
  418. case PERIPH_ID_RESERVED81:
  419. case PERIPH_ID_RESERVED82:
  420. case PERIPH_ID_RESERVED83:
  421. printf("Peripheral id %d is reserved\n", id);
  422. break;
  423. default:
  424. return 1;
  425. }
  426. }
  427. return 0;
  428. }
  429. /* Returns a pointer to the clock source register for a peripheral */
  430. static u32 *get_periph_source_reg(enum periph_id periph_id)
  431. {
  432. struct clk_rst_ctlr *clkrst =
  433. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  434. enum periphc_internal_id internal_id;
  435. assert(clock_periph_id_isvalid(periph_id));
  436. internal_id = periph_id_to_internal_id[periph_id];
  437. assert(internal_id != -1);
  438. return &clkrst->crc_clk_src[internal_id];
  439. }
  440. void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
  441. unsigned divisor)
  442. {
  443. u32 *reg = get_periph_source_reg(periph_id);
  444. u32 value;
  445. value = readl(reg);
  446. value &= ~OUT_CLK_SOURCE_MASK;
  447. value |= source << OUT_CLK_SOURCE_SHIFT;
  448. value &= ~OUT_CLK_DIVISOR_MASK;
  449. value |= divisor << OUT_CLK_DIVISOR_SHIFT;
  450. writel(value, reg);
  451. }
  452. void clock_ll_set_source(enum periph_id periph_id, unsigned source)
  453. {
  454. u32 *reg = get_periph_source_reg(periph_id);
  455. clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
  456. source << OUT_CLK_SOURCE_SHIFT);
  457. }
  458. /**
  459. * Given the parent's rate and the required rate for the children, this works
  460. * out the peripheral clock divider to use, in 7.1 binary format.
  461. *
  462. * @param parent_rate clock rate of parent clock in Hz
  463. * @param rate required clock rate for this clock
  464. * @return divider which should be used
  465. */
  466. static int clk_div7_1_get_divider(unsigned long parent_rate,
  467. unsigned long rate)
  468. {
  469. u64 divider = parent_rate * 2;
  470. divider += rate - 1;
  471. do_div(divider, rate);
  472. if ((s64)divider - 2 < 0)
  473. return 0;
  474. if ((s64)divider - 2 > 255)
  475. return -1;
  476. return divider - 2;
  477. }
  478. /**
  479. * Given the parent's rate and the divider in 7.1 format, this works out the
  480. * resulting peripheral clock rate.
  481. *
  482. * @param parent_rate clock rate of parent clock in Hz
  483. * @param divider which should be used in 7.1 format
  484. * @return effective clock rate of peripheral
  485. */
  486. static unsigned long get_rate_from_divider(unsigned long parent_rate,
  487. int divider)
  488. {
  489. u64 rate;
  490. rate = (u64)parent_rate * 2;
  491. do_div(rate, divider + 2);
  492. return rate;
  493. }
  494. unsigned long clock_get_periph_rate(enum periph_id periph_id,
  495. enum clock_id parent)
  496. {
  497. u32 *reg = get_periph_source_reg(periph_id);
  498. return get_rate_from_divider(pll_rate[parent],
  499. (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
  500. }
  501. /**
  502. * Find the best available 7.1 format divisor given a parent clock rate and
  503. * required child clock rate. This function assumes that a second-stage
  504. * divisor is available which can divide by powers of 2 from 1 to 256.
  505. *
  506. * @param parent_rate clock rate of parent clock in Hz
  507. * @param rate required clock rate for this clock
  508. * @param extra_div value for the second-stage divisor (not set if this
  509. * function returns -1.
  510. * @return divider which should be used, or -1 if nothing is valid
  511. *
  512. */
  513. static int find_best_divider(unsigned long parent_rate, unsigned long rate,
  514. int *extra_div)
  515. {
  516. int shift;
  517. int best_divider = -1;
  518. int best_error = rate;
  519. /* try dividers from 1 to 256 and find closest match */
  520. for (shift = 0; shift <= 8 && best_error > 0; shift++) {
  521. unsigned divided_parent = parent_rate >> shift;
  522. int divider = clk_div7_1_get_divider(divided_parent, rate);
  523. unsigned effective_rate = get_rate_from_divider(divided_parent,
  524. divider);
  525. int error = rate - effective_rate;
  526. /* Given a valid divider, look for the lowest error */
  527. if (divider != -1 && error < best_error) {
  528. best_error = error;
  529. *extra_div = 1 << shift;
  530. best_divider = divider;
  531. }
  532. }
  533. /* return what we found - *extra_div will already be set */
  534. return best_divider;
  535. }
  536. /**
  537. * Given a peripheral ID and the required source clock, this returns which
  538. * value should be programmed into the source mux for that peripheral.
  539. *
  540. * There is special code here to handle the one source type with 5 sources.
  541. *
  542. * @param periph_id peripheral to start
  543. * @param source PLL id of required parent clock
  544. * @param mux_bits Set to number of bits in mux register: 2 or 4
  545. * @return mux value (0-4, or -1 if not found)
  546. */
  547. static int get_periph_clock_source(enum periph_id periph_id,
  548. enum clock_id parent, int *mux_bits)
  549. {
  550. enum clock_type_id type;
  551. enum periphc_internal_id internal_id;
  552. int mux;
  553. assert(clock_periph_id_isvalid(periph_id));
  554. internal_id = periph_id_to_internal_id[periph_id];
  555. assert(periphc_internal_id_isvalid(internal_id));
  556. type = clock_periph_type[internal_id];
  557. assert(clock_type_id_isvalid(type));
  558. /* Special case here for the clock with a 4-bit source mux */
  559. if (type == CLOCK_TYPE_PCXTS)
  560. *mux_bits = 4;
  561. else
  562. *mux_bits = 2;
  563. for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
  564. if (clock_source[type][mux] == parent)
  565. return mux;
  566. /*
  567. * Not found: it might be looking for the 'S' in CLOCK_TYPE_PCXTS
  568. * which is not in our table. If not, then they are asking for a
  569. * source which this peripheral can't access through its mux.
  570. */
  571. assert(type == CLOCK_TYPE_PCXTS);
  572. assert(parent == CLOCK_ID_SFROM32KHZ);
  573. if (type == CLOCK_TYPE_PCXTS && parent == CLOCK_ID_SFROM32KHZ)
  574. return 4; /* mux value for this clock */
  575. /* if we get here, either us or the caller has made a mistake */
  576. printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
  577. parent);
  578. return -1;
  579. }
  580. /**
  581. * Adjust peripheral PLL to use the given divider and source.
  582. *
  583. * @param periph_id peripheral to adjust
  584. * @param parent Required parent clock (for source mux)
  585. * @param divider Required divider in 7.1 format
  586. * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
  587. * for this peripheral)
  588. */
  589. static int adjust_periph_pll(enum periph_id periph_id,
  590. enum clock_id parent, unsigned divider)
  591. {
  592. u32 *reg = get_periph_source_reg(periph_id);
  593. unsigned source;
  594. int mux_bits;
  595. clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
  596. divider << OUT_CLK_DIVISOR_SHIFT);
  597. udelay(1);
  598. /* work out the source clock and set it */
  599. source = get_periph_clock_source(periph_id, parent, &mux_bits);
  600. if (source < 0)
  601. return -1;
  602. if (mux_bits == 4) {
  603. clrsetbits_le32(reg, OUT_CLK_SOURCE4_MASK,
  604. source << OUT_CLK_SOURCE4_SHIFT);
  605. } else {
  606. clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
  607. source << OUT_CLK_SOURCE_SHIFT);
  608. }
  609. udelay(2);
  610. return 0;
  611. }
  612. unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
  613. enum clock_id parent, unsigned rate, int *extra_div)
  614. {
  615. unsigned effective_rate;
  616. int divider;
  617. if (extra_div)
  618. divider = find_best_divider(pll_rate[parent], rate, extra_div);
  619. else
  620. divider = clk_div7_1_get_divider(pll_rate[parent], rate);
  621. assert(divider >= 0);
  622. if (adjust_periph_pll(periph_id, parent, divider))
  623. return -1U;
  624. debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
  625. get_periph_source_reg(periph_id),
  626. readl(get_periph_source_reg(periph_id)));
  627. /* Check what we ended up with. This shouldn't matter though */
  628. effective_rate = clock_get_periph_rate(periph_id, parent);
  629. if (extra_div)
  630. effective_rate /= *extra_div;
  631. if (rate != effective_rate)
  632. debug("Requested clock rate %u not honored (got %u)\n",
  633. rate, effective_rate);
  634. return effective_rate;
  635. }
  636. unsigned clock_start_periph_pll(enum periph_id periph_id,
  637. enum clock_id parent, unsigned rate)
  638. {
  639. unsigned effective_rate;
  640. reset_set_enable(periph_id, 1);
  641. clock_enable(periph_id);
  642. effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
  643. NULL);
  644. reset_set_enable(periph_id, 0);
  645. return effective_rate;
  646. }
  647. void clock_set_enable(enum periph_id periph_id, int enable)
  648. {
  649. struct clk_rst_ctlr *clkrst =
  650. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  651. u32 *clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
  652. u32 reg;
  653. /* Enable/disable the clock to this peripheral */
  654. assert(clock_periph_id_isvalid(periph_id));
  655. reg = readl(clk);
  656. if (enable)
  657. reg |= PERIPH_MASK(periph_id);
  658. else
  659. reg &= ~PERIPH_MASK(periph_id);
  660. writel(reg, clk);
  661. }
  662. void clock_enable(enum periph_id clkid)
  663. {
  664. clock_set_enable(clkid, 1);
  665. }
  666. void clock_disable(enum periph_id clkid)
  667. {
  668. clock_set_enable(clkid, 0);
  669. }
  670. void reset_set_enable(enum periph_id periph_id, int enable)
  671. {
  672. struct clk_rst_ctlr *clkrst =
  673. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  674. u32 *reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
  675. u32 reg;
  676. /* Enable/disable reset to the peripheral */
  677. assert(clock_periph_id_isvalid(periph_id));
  678. reg = readl(reset);
  679. if (enable)
  680. reg |= PERIPH_MASK(periph_id);
  681. else
  682. reg &= ~PERIPH_MASK(periph_id);
  683. writel(reg, reset);
  684. }
  685. void reset_periph(enum periph_id periph_id, int us_delay)
  686. {
  687. /* Put peripheral into reset */
  688. reset_set_enable(periph_id, 1);
  689. udelay(us_delay);
  690. /* Remove reset */
  691. reset_set_enable(periph_id, 0);
  692. udelay(us_delay);
  693. }
  694. void reset_cmplx_set_enable(int cpu, int which, int reset)
  695. {
  696. struct clk_rst_ctlr *clkrst =
  697. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  698. u32 mask;
  699. /* Form the mask, which depends on the cpu chosen. Tegra2 has 2 */
  700. assert(cpu >= 0 && cpu < 2);
  701. mask = which << cpu;
  702. /* either enable or disable those reset for that CPU */
  703. if (reset)
  704. writel(mask, &clkrst->crc_cpu_cmplx_set);
  705. else
  706. writel(mask, &clkrst->crc_cpu_cmplx_clr);
  707. }
  708. unsigned clock_get_rate(enum clock_id clkid)
  709. {
  710. struct clk_pll *pll;
  711. u32 base;
  712. u32 divm;
  713. u64 parent_rate;
  714. u64 rate;
  715. parent_rate = osc_freq[clock_get_osc_freq()];
  716. if (clkid == CLOCK_ID_OSC)
  717. return parent_rate;
  718. pll = get_pll(clkid);
  719. base = readl(&pll->pll_base);
  720. /* Oh for bf_unpack()... */
  721. rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT);
  722. divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
  723. if (clkid == CLOCK_ID_USB)
  724. divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT;
  725. else
  726. divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
  727. do_div(rate, divm);
  728. return rate;
  729. }
  730. /**
  731. * Set the output frequency you want for each PLL clock.
  732. * PLL output frequencies are programmed by setting their N, M and P values.
  733. * The governing equations are:
  734. * VCO = (Fi / m) * n, Fo = VCO / (2^p)
  735. * where Fo is the output frequency from the PLL.
  736. * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
  737. * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
  738. * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
  739. *
  740. * @param n PLL feedback divider(DIVN)
  741. * @param m PLL input divider(DIVN)
  742. * @param p post divider(DIVP)
  743. * @param cpcon base PLL charge pump(CPCON)
  744. * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
  745. * be overriden), 1 if PLL is already correct
  746. */
  747. static int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
  748. {
  749. u32 base_reg;
  750. u32 misc_reg;
  751. struct clk_pll *pll;
  752. pll = get_pll(clkid);
  753. base_reg = readl(&pll->pll_base);
  754. /* Set BYPASS, m, n and p to PLL_BASE */
  755. base_reg &= ~PLL_DIVM_MASK;
  756. base_reg |= m << PLL_DIVM_SHIFT;
  757. base_reg &= ~PLL_DIVN_MASK;
  758. base_reg |= n << PLL_DIVN_SHIFT;
  759. base_reg &= ~PLL_DIVP_MASK;
  760. base_reg |= p << PLL_DIVP_SHIFT;
  761. if (clkid == CLOCK_ID_PERIPH) {
  762. /*
  763. * If the PLL is already set up, check that it is correct
  764. * and record this info for clock_verify() to check.
  765. */
  766. if (base_reg & PLL_BASE_OVRRIDE_MASK) {
  767. base_reg |= PLL_ENABLE_MASK;
  768. if (base_reg != readl(&pll->pll_base))
  769. pllp_valid = 0;
  770. return pllp_valid ? 1 : -1;
  771. }
  772. base_reg |= PLL_BASE_OVRRIDE_MASK;
  773. }
  774. base_reg |= PLL_BYPASS_MASK;
  775. writel(base_reg, &pll->pll_base);
  776. /* Set cpcon to PLL_MISC */
  777. misc_reg = readl(&pll->pll_misc);
  778. misc_reg &= ~PLL_CPCON_MASK;
  779. misc_reg |= cpcon << PLL_CPCON_SHIFT;
  780. writel(misc_reg, &pll->pll_misc);
  781. /* Enable PLL */
  782. base_reg |= PLL_ENABLE_MASK;
  783. writel(base_reg, &pll->pll_base);
  784. /* Disable BYPASS */
  785. base_reg &= ~PLL_BYPASS_MASK;
  786. writel(base_reg, &pll->pll_base);
  787. return 0;
  788. }
  789. int clock_verify(void)
  790. {
  791. struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
  792. u32 reg = readl(&pll->pll_base);
  793. if (!pllp_valid) {
  794. printf("Warning: PLLP %x is not correct\n", reg);
  795. return -1;
  796. }
  797. debug("PLLX %x is correct\n", reg);
  798. return 0;
  799. }
  800. void clock_early_init(void)
  801. {
  802. /*
  803. * PLLP output frequency set to 216MHz
  804. * PLLC output frequency set to 600Mhz
  805. *
  806. * TODO: Can we calculate these values instead of hard-coding?
  807. */
  808. switch (clock_get_osc_freq()) {
  809. case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
  810. clock_set_rate(CLOCK_ID_PERIPH, 432, 12, 1, 8);
  811. clock_set_rate(CLOCK_ID_CGENERAL, 600, 12, 0, 8);
  812. break;
  813. case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
  814. clock_set_rate(CLOCK_ID_PERIPH, 432, 26, 1, 8);
  815. clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
  816. break;
  817. case CLOCK_OSC_FREQ_13_0:
  818. case CLOCK_OSC_FREQ_19_2:
  819. default:
  820. /*
  821. * These are not supported. It is too early to print a
  822. * message and the UART likely won't work anyway due to the
  823. * oscillator being wrong.
  824. */
  825. break;
  826. }
  827. }
  828. void clock_init(void)
  829. {
  830. pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
  831. pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
  832. pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
  833. pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
  834. pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
  835. debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
  836. debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
  837. debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
  838. }