clock.c 27 KB

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