clock.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. /* Tegra20 Clock control functions */
  22. #include <common.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/clock.h>
  25. #include <asm/arch/tegra.h>
  26. #include <asm/arch-tegra/clk_rst.h>
  27. #include <asm/arch-tegra/timer.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 Tegra20 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. /* number of clock outputs of a PLL */
  360. static const u8 pll_num_clkouts[] = {
  361. 1, /* PLLC */
  362. 1, /* PLLM */
  363. 4, /* PLLP */
  364. 1, /* PLLA */
  365. 0, /* PLLU */
  366. 0, /* PLLD */
  367. };
  368. /*
  369. * Get the oscillator frequency, from the corresponding hardware configuration
  370. * field.
  371. */
  372. enum clock_osc_freq clock_get_osc_freq(void)
  373. {
  374. struct clk_rst_ctlr *clkrst =
  375. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  376. u32 reg;
  377. reg = readl(&clkrst->crc_osc_ctrl);
  378. return (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
  379. }
  380. int clock_get_osc_bypass(void)
  381. {
  382. struct clk_rst_ctlr *clkrst =
  383. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  384. u32 reg;
  385. reg = readl(&clkrst->crc_osc_ctrl);
  386. return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
  387. }
  388. /* Returns a pointer to the registers of the given pll */
  389. static struct clk_pll *get_pll(enum clock_id clkid)
  390. {
  391. struct clk_rst_ctlr *clkrst =
  392. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  393. assert(clock_id_is_pll(clkid));
  394. return &clkrst->crc_pll[clkid];
  395. }
  396. int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
  397. u32 *divp, u32 *cpcon, u32 *lfcon)
  398. {
  399. struct clk_pll *pll = get_pll(clkid);
  400. u32 data;
  401. assert(clkid != CLOCK_ID_USB);
  402. /* Safety check, adds to code size but is small */
  403. if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
  404. return -1;
  405. data = readl(&pll->pll_base);
  406. *divm = (data & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
  407. *divn = (data & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT;
  408. *divp = (data & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
  409. data = readl(&pll->pll_misc);
  410. *cpcon = (data & PLL_CPCON_MASK) >> PLL_CPCON_SHIFT;
  411. *lfcon = (data & PLL_LFCON_MASK) >> PLL_LFCON_SHIFT;
  412. return 0;
  413. }
  414. unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
  415. u32 divp, u32 cpcon, u32 lfcon)
  416. {
  417. struct clk_pll *pll = get_pll(clkid);
  418. u32 data;
  419. /*
  420. * We cheat by treating all PLL (except PLLU) in the same fashion.
  421. * This works only because:
  422. * - same fields are always mapped at same offsets, except DCCON
  423. * - DCCON is always 0, doesn't conflict
  424. * - M,N, P of PLLP values are ignored for PLLP
  425. */
  426. data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT);
  427. writel(data, &pll->pll_misc);
  428. data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) |
  429. (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT);
  430. if (clkid == CLOCK_ID_USB)
  431. data |= divp << PLLU_VCO_FREQ_SHIFT;
  432. else
  433. data |= divp << PLL_DIVP_SHIFT;
  434. writel(data, &pll->pll_base);
  435. /* calculate the stable time */
  436. return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
  437. }
  438. /* return 1 if a peripheral ID is in range and valid */
  439. static int clock_periph_id_isvalid(enum periph_id id)
  440. {
  441. if (id < PERIPH_ID_FIRST || id >= PERIPH_ID_COUNT)
  442. printf("Peripheral id %d out of range\n", id);
  443. else {
  444. switch (id) {
  445. case PERIPH_ID_RESERVED1:
  446. case PERIPH_ID_RESERVED2:
  447. case PERIPH_ID_RESERVED30:
  448. case PERIPH_ID_RESERVED35:
  449. case PERIPH_ID_RESERVED56:
  450. case PERIPH_ID_RESERVED74:
  451. case PERIPH_ID_RESERVED76:
  452. case PERIPH_ID_RESERVED77:
  453. case PERIPH_ID_RESERVED78:
  454. case PERIPH_ID_RESERVED79:
  455. case PERIPH_ID_RESERVED80:
  456. case PERIPH_ID_RESERVED81:
  457. case PERIPH_ID_RESERVED82:
  458. case PERIPH_ID_RESERVED83:
  459. case PERIPH_ID_RESERVED91:
  460. printf("Peripheral id %d is reserved\n", id);
  461. break;
  462. default:
  463. return 1;
  464. }
  465. }
  466. return 0;
  467. }
  468. /* Returns a pointer to the clock source register for a peripheral */
  469. static u32 *get_periph_source_reg(enum periph_id periph_id)
  470. {
  471. struct clk_rst_ctlr *clkrst =
  472. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  473. enum periphc_internal_id internal_id;
  474. assert(clock_periph_id_isvalid(periph_id));
  475. internal_id = periph_id_to_internal_id[periph_id];
  476. assert(internal_id != -1);
  477. return &clkrst->crc_clk_src[internal_id];
  478. }
  479. void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
  480. unsigned divisor)
  481. {
  482. u32 *reg = get_periph_source_reg(periph_id);
  483. u32 value;
  484. value = readl(reg);
  485. value &= ~OUT_CLK_SOURCE_MASK;
  486. value |= source << OUT_CLK_SOURCE_SHIFT;
  487. value &= ~OUT_CLK_DIVISOR_MASK;
  488. value |= divisor << OUT_CLK_DIVISOR_SHIFT;
  489. writel(value, reg);
  490. }
  491. void clock_ll_set_source(enum periph_id periph_id, unsigned source)
  492. {
  493. u32 *reg = get_periph_source_reg(periph_id);
  494. clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
  495. source << OUT_CLK_SOURCE_SHIFT);
  496. }
  497. /**
  498. * Given the parent's rate and the required rate for the children, this works
  499. * out the peripheral clock divider to use, in 7.1 binary format.
  500. *
  501. * @param divider_bits number of divider bits (8 or 16)
  502. * @param parent_rate clock rate of parent clock in Hz
  503. * @param rate required clock rate for this clock
  504. * @return divider which should be used
  505. */
  506. static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
  507. unsigned long rate)
  508. {
  509. u64 divider = parent_rate * 2;
  510. unsigned max_divider = 1 << divider_bits;
  511. divider += rate - 1;
  512. do_div(divider, rate);
  513. if ((s64)divider - 2 < 0)
  514. return 0;
  515. if ((s64)divider - 2 >= max_divider)
  516. return -1;
  517. return divider - 2;
  518. }
  519. /**
  520. * Given the parent's rate and the divider in 7.1 format, this works out the
  521. * resulting peripheral clock rate.
  522. *
  523. * @param parent_rate clock rate of parent clock in Hz
  524. * @param divider which should be used in 7.1 format
  525. * @return effective clock rate of peripheral
  526. */
  527. static unsigned long get_rate_from_divider(unsigned long parent_rate,
  528. int divider)
  529. {
  530. u64 rate;
  531. rate = (u64)parent_rate * 2;
  532. do_div(rate, divider + 2);
  533. return rate;
  534. }
  535. unsigned long clock_get_periph_rate(enum periph_id periph_id,
  536. enum clock_id parent)
  537. {
  538. u32 *reg = get_periph_source_reg(periph_id);
  539. return get_rate_from_divider(pll_rate[parent],
  540. (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
  541. }
  542. int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate)
  543. {
  544. struct clk_pll *pll = get_pll(clkid);
  545. int data = 0, div = 0, offset = 0;
  546. if (!clock_id_is_pll(clkid))
  547. return -1;
  548. if (pllout + 1 > pll_num_clkouts[clkid])
  549. return -1;
  550. div = clk_get_divider(8, pll_rate[clkid], rate);
  551. if (div < 0)
  552. return -1;
  553. /* out2 and out4 are in the high part of the register */
  554. if (pllout == PLL_OUT2 || pllout == PLL_OUT4)
  555. offset = 16;
  556. data = (div << PLL_OUT_RATIO_SHIFT) |
  557. PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN;
  558. clrsetbits_le32(&pll->pll_out[pllout >> 1],
  559. PLL_OUT_RATIO_MASK << offset, data << offset);
  560. return 0;
  561. }
  562. /**
  563. * Find the best available 7.1 format divisor given a parent clock rate and
  564. * required child clock rate. This function assumes that a second-stage
  565. * divisor is available which can divide by powers of 2 from 1 to 256.
  566. *
  567. * @param divider_bits number of divider bits (8 or 16)
  568. * @param parent_rate clock rate of parent clock in Hz
  569. * @param rate required clock rate for this clock
  570. * @param extra_div value for the second-stage divisor (not set if this
  571. * function returns -1.
  572. * @return divider which should be used, or -1 if nothing is valid
  573. *
  574. */
  575. static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
  576. unsigned long rate, int *extra_div)
  577. {
  578. int shift;
  579. int best_divider = -1;
  580. int best_error = rate;
  581. /* try dividers from 1 to 256 and find closest match */
  582. for (shift = 0; shift <= 8 && best_error > 0; shift++) {
  583. unsigned divided_parent = parent_rate >> shift;
  584. int divider = clk_get_divider(divider_bits, divided_parent,
  585. rate);
  586. unsigned effective_rate = get_rate_from_divider(divided_parent,
  587. divider);
  588. int error = rate - effective_rate;
  589. /* Given a valid divider, look for the lowest error */
  590. if (divider != -1 && error < best_error) {
  591. best_error = error;
  592. *extra_div = 1 << shift;
  593. best_divider = divider;
  594. }
  595. }
  596. /* return what we found - *extra_div will already be set */
  597. return best_divider;
  598. }
  599. /**
  600. * Given a peripheral ID and the required source clock, this returns which
  601. * value should be programmed into the source mux for that peripheral.
  602. *
  603. * There is special code here to handle the one source type with 5 sources.
  604. *
  605. * @param periph_id peripheral to start
  606. * @param source PLL id of required parent clock
  607. * @param mux_bits Set to number of bits in mux register: 2 or 4
  608. * @param divider_bits Set to number of divider bits (8 or 16)
  609. * @return mux value (0-4, or -1 if not found)
  610. */
  611. static int get_periph_clock_source(enum periph_id periph_id,
  612. enum clock_id parent, int *mux_bits, int *divider_bits)
  613. {
  614. enum clock_type_id type;
  615. enum periphc_internal_id internal_id;
  616. int mux;
  617. assert(clock_periph_id_isvalid(periph_id));
  618. internal_id = periph_id_to_internal_id[periph_id];
  619. assert(periphc_internal_id_isvalid(internal_id));
  620. type = clock_periph_type[internal_id];
  621. assert(clock_type_id_isvalid(type));
  622. /*
  623. * Special cases here for the clock with a 4-bit source mux and I2C
  624. * with its 16-bit divisor
  625. */
  626. if (type == CLOCK_TYPE_PCXTS)
  627. *mux_bits = 4;
  628. else
  629. *mux_bits = 2;
  630. if (type == CLOCK_TYPE_PCMT16)
  631. *divider_bits = 16;
  632. else
  633. *divider_bits = 8;
  634. for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
  635. if (clock_source[type][mux] == parent)
  636. return mux;
  637. /*
  638. * Not found: it might be looking for the 'S' in CLOCK_TYPE_PCXTS
  639. * which is not in our table. If not, then they are asking for a
  640. * source which this peripheral can't access through its mux.
  641. */
  642. assert(type == CLOCK_TYPE_PCXTS);
  643. assert(parent == CLOCK_ID_SFROM32KHZ);
  644. if (type == CLOCK_TYPE_PCXTS && parent == CLOCK_ID_SFROM32KHZ)
  645. return 4; /* mux value for this clock */
  646. /* if we get here, either us or the caller has made a mistake */
  647. printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
  648. parent);
  649. return -1;
  650. }
  651. /**
  652. * Adjust peripheral PLL to use the given divider and source.
  653. *
  654. * @param periph_id peripheral to adjust
  655. * @param source Source number (0-3 or 0-7)
  656. * @param mux_bits Number of mux bits (2 or 4)
  657. * @param divider Required divider in 7.1 or 15.1 format
  658. * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
  659. * for this peripheral)
  660. */
  661. static int adjust_periph_pll(enum periph_id periph_id, int source,
  662. int mux_bits, unsigned divider)
  663. {
  664. u32 *reg = get_periph_source_reg(periph_id);
  665. clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
  666. divider << OUT_CLK_DIVISOR_SHIFT);
  667. udelay(1);
  668. /* work out the source clock and set it */
  669. if (source < 0)
  670. return -1;
  671. if (mux_bits == 4) {
  672. clrsetbits_le32(reg, OUT_CLK_SOURCE4_MASK,
  673. source << OUT_CLK_SOURCE4_SHIFT);
  674. } else {
  675. clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
  676. source << OUT_CLK_SOURCE_SHIFT);
  677. }
  678. udelay(2);
  679. return 0;
  680. }
  681. unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
  682. enum clock_id parent, unsigned rate, int *extra_div)
  683. {
  684. unsigned effective_rate;
  685. int mux_bits, divider_bits, source;
  686. int divider;
  687. /* work out the source clock and set it */
  688. source = get_periph_clock_source(periph_id, parent, &mux_bits,
  689. &divider_bits);
  690. if (extra_div)
  691. divider = find_best_divider(divider_bits, pll_rate[parent],
  692. rate, extra_div);
  693. else
  694. divider = clk_get_divider(divider_bits, pll_rate[parent],
  695. rate);
  696. assert(divider >= 0);
  697. if (adjust_periph_pll(periph_id, source, mux_bits, divider))
  698. return -1U;
  699. debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
  700. get_periph_source_reg(periph_id),
  701. readl(get_periph_source_reg(periph_id)));
  702. /* Check what we ended up with. This shouldn't matter though */
  703. effective_rate = clock_get_periph_rate(periph_id, parent);
  704. if (extra_div)
  705. effective_rate /= *extra_div;
  706. if (rate != effective_rate)
  707. debug("Requested clock rate %u not honored (got %u)\n",
  708. rate, effective_rate);
  709. return effective_rate;
  710. }
  711. unsigned clock_start_periph_pll(enum periph_id periph_id,
  712. enum clock_id parent, unsigned rate)
  713. {
  714. unsigned effective_rate;
  715. reset_set_enable(periph_id, 1);
  716. clock_enable(periph_id);
  717. effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
  718. NULL);
  719. reset_set_enable(periph_id, 0);
  720. return effective_rate;
  721. }
  722. void clock_set_enable(enum periph_id periph_id, int enable)
  723. {
  724. struct clk_rst_ctlr *clkrst =
  725. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  726. u32 *clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
  727. u32 reg;
  728. /* Enable/disable the clock to this peripheral */
  729. assert(clock_periph_id_isvalid(periph_id));
  730. reg = readl(clk);
  731. if (enable)
  732. reg |= PERIPH_MASK(periph_id);
  733. else
  734. reg &= ~PERIPH_MASK(periph_id);
  735. writel(reg, clk);
  736. }
  737. void clock_enable(enum periph_id clkid)
  738. {
  739. clock_set_enable(clkid, 1);
  740. }
  741. void clock_disable(enum periph_id clkid)
  742. {
  743. clock_set_enable(clkid, 0);
  744. }
  745. void reset_set_enable(enum periph_id periph_id, int enable)
  746. {
  747. struct clk_rst_ctlr *clkrst =
  748. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  749. u32 *reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
  750. u32 reg;
  751. /* Enable/disable reset to the peripheral */
  752. assert(clock_periph_id_isvalid(periph_id));
  753. reg = readl(reset);
  754. if (enable)
  755. reg |= PERIPH_MASK(periph_id);
  756. else
  757. reg &= ~PERIPH_MASK(periph_id);
  758. writel(reg, reset);
  759. }
  760. void reset_periph(enum periph_id periph_id, int us_delay)
  761. {
  762. /* Put peripheral into reset */
  763. reset_set_enable(periph_id, 1);
  764. udelay(us_delay);
  765. /* Remove reset */
  766. reset_set_enable(periph_id, 0);
  767. udelay(us_delay);
  768. }
  769. void reset_cmplx_set_enable(int cpu, int which, int reset)
  770. {
  771. struct clk_rst_ctlr *clkrst =
  772. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  773. u32 mask;
  774. /* Form the mask, which depends on the cpu chosen. Tegra20 has 2 */
  775. assert(cpu >= 0 && cpu < 2);
  776. mask = which << cpu;
  777. /* either enable or disable those reset for that CPU */
  778. if (reset)
  779. writel(mask, &clkrst->crc_cpu_cmplx_set);
  780. else
  781. writel(mask, &clkrst->crc_cpu_cmplx_clr);
  782. }
  783. unsigned clock_get_rate(enum clock_id clkid)
  784. {
  785. struct clk_pll *pll;
  786. u32 base;
  787. u32 divm;
  788. u64 parent_rate;
  789. u64 rate;
  790. parent_rate = osc_freq[clock_get_osc_freq()];
  791. if (clkid == CLOCK_ID_OSC)
  792. return parent_rate;
  793. pll = get_pll(clkid);
  794. base = readl(&pll->pll_base);
  795. /* Oh for bf_unpack()... */
  796. rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT);
  797. divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
  798. if (clkid == CLOCK_ID_USB)
  799. divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT;
  800. else
  801. divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
  802. do_div(rate, divm);
  803. return rate;
  804. }
  805. /**
  806. * Set the output frequency you want for each PLL clock.
  807. * PLL output frequencies are programmed by setting their N, M and P values.
  808. * The governing equations are:
  809. * VCO = (Fi / m) * n, Fo = VCO / (2^p)
  810. * where Fo is the output frequency from the PLL.
  811. * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
  812. * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
  813. * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
  814. *
  815. * @param n PLL feedback divider(DIVN)
  816. * @param m PLL input divider(DIVN)
  817. * @param p post divider(DIVP)
  818. * @param cpcon base PLL charge pump(CPCON)
  819. * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
  820. * be overriden), 1 if PLL is already correct
  821. */
  822. static int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
  823. {
  824. u32 base_reg;
  825. u32 misc_reg;
  826. struct clk_pll *pll;
  827. pll = get_pll(clkid);
  828. base_reg = readl(&pll->pll_base);
  829. /* Set BYPASS, m, n and p to PLL_BASE */
  830. base_reg &= ~PLL_DIVM_MASK;
  831. base_reg |= m << PLL_DIVM_SHIFT;
  832. base_reg &= ~PLL_DIVN_MASK;
  833. base_reg |= n << PLL_DIVN_SHIFT;
  834. base_reg &= ~PLL_DIVP_MASK;
  835. base_reg |= p << PLL_DIVP_SHIFT;
  836. if (clkid == CLOCK_ID_PERIPH) {
  837. /*
  838. * If the PLL is already set up, check that it is correct
  839. * and record this info for clock_verify() to check.
  840. */
  841. if (base_reg & PLL_BASE_OVRRIDE_MASK) {
  842. base_reg |= PLL_ENABLE_MASK;
  843. if (base_reg != readl(&pll->pll_base))
  844. pllp_valid = 0;
  845. return pllp_valid ? 1 : -1;
  846. }
  847. base_reg |= PLL_BASE_OVRRIDE_MASK;
  848. }
  849. base_reg |= PLL_BYPASS_MASK;
  850. writel(base_reg, &pll->pll_base);
  851. /* Set cpcon to PLL_MISC */
  852. misc_reg = readl(&pll->pll_misc);
  853. misc_reg &= ~PLL_CPCON_MASK;
  854. misc_reg |= cpcon << PLL_CPCON_SHIFT;
  855. writel(misc_reg, &pll->pll_misc);
  856. /* Enable PLL */
  857. base_reg |= PLL_ENABLE_MASK;
  858. writel(base_reg, &pll->pll_base);
  859. /* Disable BYPASS */
  860. base_reg &= ~PLL_BYPASS_MASK;
  861. writel(base_reg, &pll->pll_base);
  862. return 0;
  863. }
  864. void clock_ll_start_uart(enum periph_id periph_id)
  865. {
  866. /* Assert UART reset and enable clock */
  867. reset_set_enable(periph_id, 1);
  868. clock_enable(periph_id);
  869. clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
  870. /* wait for 2us */
  871. udelay(2);
  872. /* De-assert reset to UART */
  873. reset_set_enable(periph_id, 0);
  874. }
  875. #ifdef CONFIG_OF_CONTROL
  876. /*
  877. * Convert a device tree clock ID to our peripheral ID. They are mostly
  878. * the same but we are very cautious so we check that a valid clock ID is
  879. * provided.
  880. *
  881. * @param clk_id Clock ID according to tegra20 device tree binding
  882. * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
  883. */
  884. static enum periph_id clk_id_to_periph_id(int clk_id)
  885. {
  886. if (clk_id > 95)
  887. return PERIPH_ID_NONE;
  888. switch (clk_id) {
  889. case 1:
  890. case 2:
  891. case 7:
  892. case 10:
  893. case 20:
  894. case 30:
  895. case 35:
  896. case 49:
  897. case 56:
  898. case 74:
  899. case 76:
  900. case 77:
  901. case 78:
  902. case 79:
  903. case 80:
  904. case 81:
  905. case 82:
  906. case 83:
  907. case 91:
  908. case 95:
  909. return PERIPH_ID_NONE;
  910. default:
  911. return clk_id;
  912. }
  913. }
  914. int clock_decode_periph_id(const void *blob, int node)
  915. {
  916. enum periph_id id;
  917. u32 cell[2];
  918. int err;
  919. err = fdtdec_get_int_array(blob, node, "clocks", cell,
  920. ARRAY_SIZE(cell));
  921. if (err)
  922. return -1;
  923. id = clk_id_to_periph_id(cell[1]);
  924. assert(clock_periph_id_isvalid(id));
  925. return id;
  926. }
  927. #endif /* CONFIG_OF_CONTROL */
  928. int clock_verify(void)
  929. {
  930. struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
  931. u32 reg = readl(&pll->pll_base);
  932. if (!pllp_valid) {
  933. printf("Warning: PLLP %x is not correct\n", reg);
  934. return -1;
  935. }
  936. debug("PLLX %x is correct\n", reg);
  937. return 0;
  938. }
  939. void clock_early_init(void)
  940. {
  941. /*
  942. * PLLP output frequency set to 216MHz
  943. * PLLC output frequency set to 600Mhz
  944. *
  945. * TODO: Can we calculate these values instead of hard-coding?
  946. */
  947. switch (clock_get_osc_freq()) {
  948. case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
  949. clock_set_rate(CLOCK_ID_PERIPH, 432, 12, 1, 8);
  950. clock_set_rate(CLOCK_ID_CGENERAL, 600, 12, 0, 8);
  951. break;
  952. case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
  953. clock_set_rate(CLOCK_ID_PERIPH, 432, 26, 1, 8);
  954. clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
  955. break;
  956. case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
  957. clock_set_rate(CLOCK_ID_PERIPH, 432, 13, 1, 8);
  958. clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
  959. break;
  960. case CLOCK_OSC_FREQ_19_2:
  961. default:
  962. /*
  963. * These are not supported. It is too early to print a
  964. * message and the UART likely won't work anyway due to the
  965. * oscillator being wrong.
  966. */
  967. break;
  968. }
  969. }
  970. void clock_init(void)
  971. {
  972. pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
  973. pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
  974. pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
  975. pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
  976. pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
  977. debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
  978. debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
  979. debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
  980. }