clock.c 26 KB

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