1150c2493STom Warren /* 2150c2493STom Warren * Copyright (c) 2011 The Chromium OS Authors. 3150c2493STom Warren * 41a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 5150c2493STom Warren */ 6150c2493STom Warren 7150c2493STom Warren /* Tegra clock control functions */ 8150c2493STom Warren 9dc89ad14STom Warren #ifndef _TEGRA_CLOCK_H_ 10dc89ad14STom Warren #define _TEGRA_CLOCK_H_ 11150c2493STom Warren 12150c2493STom Warren /* Set of oscillator frequencies supported in the internal API. */ 13150c2493STom Warren enum clock_osc_freq { 14150c2493STom Warren /* All in MHz, so 13_0 is 13.0MHz */ 15150c2493STom Warren CLOCK_OSC_FREQ_13_0, 16150c2493STom Warren CLOCK_OSC_FREQ_19_2, 17150c2493STom Warren CLOCK_OSC_FREQ_12_0, 18150c2493STom Warren CLOCK_OSC_FREQ_26_0, 19*3e8650c0STom Warren CLOCK_OSC_FREQ_38_4, 20*3e8650c0STom Warren CLOCK_OSC_FREQ_48_0, 21150c2493STom Warren 22150c2493STom Warren CLOCK_OSC_FREQ_COUNT, 23150c2493STom Warren }; 24150c2493STom Warren 255916a36eSStephen Warren /* 265916a36eSStephen Warren * Note that no Tegra clock register actually uses all of bits 31:28 as 275916a36eSStephen Warren * the mux field. Rather, bits 30:28, 29:28, or 28 are used. However, in 285916a36eSStephen Warren * those cases, nothing is stored in the bits about the mux field, so it's 295916a36eSStephen Warren * safe to pretend that the mux field extends all the way to the end of the 305916a36eSStephen Warren * register. As such, the U-Boot clock driver is currently a bit lazy, and 315916a36eSStephen Warren * doesn't distinguish between 31:28, 30:28, 29:28 and 28; it just lumps 325916a36eSStephen Warren * them all together and pretends they're all 31:28. 335916a36eSStephen Warren */ 340b01b53aSTom Warren enum { 3504b8e8e7SStephen Warren MASK_BITS_31_30, 360b01b53aSTom Warren MASK_BITS_31_29, 375916a36eSStephen Warren MASK_BITS_31_28, 380b01b53aSTom Warren }; 390b01b53aSTom Warren 40150c2493STom Warren #include <asm/arch/clock-tables.h> 41150c2493STom Warren /* PLL stabilization delay in usec */ 42150c2493STom Warren #define CLOCK_PLL_STABLE_DELAY_US 300 43150c2493STom Warren 44150c2493STom Warren /* return the current oscillator clock frequency */ 45150c2493STom Warren enum clock_osc_freq clock_get_osc_freq(void); 46150c2493STom Warren 47150c2493STom Warren /** 48150c2493STom Warren * Start PLL using the provided configuration parameters. 49150c2493STom Warren * 50150c2493STom Warren * @param id clock id 51150c2493STom Warren * @param divm input divider 52150c2493STom Warren * @param divn feedback divider 53150c2493STom Warren * @param divp post divider 2^n 54150c2493STom Warren * @param cpcon charge pump setup control 55150c2493STom Warren * @param lfcon loop filter setup control 56150c2493STom Warren * 57150c2493STom Warren * @returns monotonic time in us that the PLL will be stable 58150c2493STom Warren */ 59150c2493STom Warren unsigned long clock_start_pll(enum clock_id id, u32 divm, u32 divn, 60150c2493STom Warren u32 divp, u32 cpcon, u32 lfcon); 61150c2493STom Warren 62150c2493STom Warren /** 6365530a84SLucas Stach * Set PLL output frequency 6465530a84SLucas Stach * 6565530a84SLucas Stach * @param clkid clock id 6665530a84SLucas Stach * @param pllout pll output id 6765530a84SLucas Stach * @param rate desired output rate 6865530a84SLucas Stach * 6965530a84SLucas Stach * @return 0 if ok, -1 on error (invalid clock id or no suitable divider) 7065530a84SLucas Stach */ 7165530a84SLucas Stach int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, 7265530a84SLucas Stach unsigned rate); 7365530a84SLucas Stach 7465530a84SLucas Stach /** 75150c2493STom Warren * Read low-level parameters of a PLL. 76150c2493STom Warren * 77150c2493STom Warren * @param id clock id to read (note: USB is not supported) 78150c2493STom Warren * @param divm returns input divider 79150c2493STom Warren * @param divn returns feedback divider 80150c2493STom Warren * @param divp returns post divider 2^n 81150c2493STom Warren * @param cpcon returns charge pump setup control 82150c2493STom Warren * @param lfcon returns loop filter setup control 83150c2493STom Warren * 84150c2493STom Warren * @returns 0 if ok, -1 on error (invalid clock id) 85150c2493STom Warren */ 86150c2493STom Warren int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn, 87150c2493STom Warren u32 *divp, u32 *cpcon, u32 *lfcon); 88150c2493STom Warren 89150c2493STom Warren /* 90150c2493STom Warren * Enable a clock 91150c2493STom Warren * 92150c2493STom Warren * @param id clock id 93150c2493STom Warren */ 94150c2493STom Warren void clock_enable(enum periph_id clkid); 95150c2493STom Warren 96150c2493STom Warren /* 97150c2493STom Warren * Disable a clock 98150c2493STom Warren * 99150c2493STom Warren * @param id clock id 100150c2493STom Warren */ 101150c2493STom Warren void clock_disable(enum periph_id clkid); 102150c2493STom Warren 103150c2493STom Warren /* 104150c2493STom Warren * Set whether a clock is enabled or disabled. 105150c2493STom Warren * 106150c2493STom Warren * @param id clock id 107150c2493STom Warren * @param enable 1 to enable, 0 to disable 108150c2493STom Warren */ 109150c2493STom Warren void clock_set_enable(enum periph_id clkid, int enable); 110150c2493STom Warren 111150c2493STom Warren /** 112150c2493STom Warren * Reset a peripheral. This puts it in reset, waits for a delay, then takes 113150c2493STom Warren * it out of reset and waits for th delay again. 114150c2493STom Warren * 115150c2493STom Warren * @param periph_id peripheral to reset 116150c2493STom Warren * @param us_delay time to delay in microseconds 117150c2493STom Warren */ 118150c2493STom Warren void reset_periph(enum periph_id periph_id, int us_delay); 119150c2493STom Warren 120150c2493STom Warren /** 121150c2493STom Warren * Put a peripheral into or out of reset. 122150c2493STom Warren * 123150c2493STom Warren * @param periph_id peripheral to reset 124150c2493STom Warren * @param enable 1 to put into reset, 0 to take out of reset 125150c2493STom Warren */ 126150c2493STom Warren void reset_set_enable(enum periph_id periph_id, int enable); 127150c2493STom Warren 128150c2493STom Warren 129150c2493STom Warren /* CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET/CLR_0 */ 130150c2493STom Warren enum crc_reset_id { 131150c2493STom Warren /* Things we can hold in reset for each CPU */ 132150c2493STom Warren crc_rst_cpu = 1, 133766afc3dSAlban Bedel crc_rst_de = 1 << 4, /* What is de? */ 134766afc3dSAlban Bedel crc_rst_watchdog = 1 << 8, 135766afc3dSAlban Bedel crc_rst_debug = 1 << 12, 136150c2493STom Warren }; 137150c2493STom Warren 138150c2493STom Warren /** 139150c2493STom Warren * Put parts of the CPU complex into or out of reset.\ 140150c2493STom Warren * 141dc89ad14STom Warren * @param cpu cpu number (0 or 1 on Tegra2, 0-3 on Tegra3) 142150c2493STom Warren * @param which which parts of the complex to affect (OR of crc_reset_id) 143150c2493STom Warren * @param reset 1 to assert reset, 0 to de-assert 144150c2493STom Warren */ 145150c2493STom Warren void reset_cmplx_set_enable(int cpu, int which, int reset); 146150c2493STom Warren 147150c2493STom Warren /** 148150c2493STom Warren * Set the source for a peripheral clock. This plus the divisor sets the 149150c2493STom Warren * clock rate. You need to look up the datasheet to see the meaning of the 150150c2493STom Warren * source parameter as it changes for each peripheral. 151150c2493STom Warren * 152150c2493STom Warren * Warning: This function is only for use pre-relocation. Please use 153150c2493STom Warren * clock_start_periph_pll() instead. 154150c2493STom Warren * 155150c2493STom Warren * @param periph_id peripheral to adjust 156150c2493STom Warren * @param source source clock (0, 1, 2 or 3) 157150c2493STom Warren */ 158150c2493STom Warren void clock_ll_set_source(enum periph_id periph_id, unsigned source); 159150c2493STom Warren 160150c2493STom Warren /** 1617bb6199bSSimon Glass * This function is similar to clock_ll_set_source() except that it can be 1627bb6199bSSimon Glass * used for clocks with more than 2 mux bits. 1637bb6199bSSimon Glass * 1647bb6199bSSimon Glass * @param periph_id peripheral to adjust 1657bb6199bSSimon Glass * @param mux_bits number of mux bits for the clock 1667bb6199bSSimon Glass * @param source source clock (0-15 depending on mux_bits) 1677bb6199bSSimon Glass */ 1687bb6199bSSimon Glass int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits, 1697bb6199bSSimon Glass unsigned source); 1707bb6199bSSimon Glass 1717bb6199bSSimon Glass /** 172150c2493STom Warren * Set the source and divisor for a peripheral clock. This sets the 173150c2493STom Warren * clock rate. You need to look up the datasheet to see the meaning of the 174150c2493STom Warren * source parameter as it changes for each peripheral. 175150c2493STom Warren * 176150c2493STom Warren * Warning: This function is only for use pre-relocation. Please use 177150c2493STom Warren * clock_start_periph_pll() instead. 178150c2493STom Warren * 179150c2493STom Warren * @param periph_id peripheral to adjust 180150c2493STom Warren * @param source source clock (0, 1, 2 or 3) 181150c2493STom Warren * @param divisor divisor value to use 182150c2493STom Warren */ 183150c2493STom Warren void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source, 184150c2493STom Warren unsigned divisor); 185150c2493STom Warren 186150c2493STom Warren /** 187150c2493STom Warren * Start a peripheral PLL clock at the given rate. This also resets the 188150c2493STom Warren * peripheral. 189150c2493STom Warren * 190150c2493STom Warren * @param periph_id peripheral to start 191150c2493STom Warren * @param parent PLL id of required parent clock 192150c2493STom Warren * @param rate Required clock rate in Hz 193150c2493STom Warren * @return rate selected in Hz, or -1U if something went wrong 194150c2493STom Warren */ 195150c2493STom Warren unsigned clock_start_periph_pll(enum periph_id periph_id, 196150c2493STom Warren enum clock_id parent, unsigned rate); 197150c2493STom Warren 198150c2493STom Warren /** 199150c2493STom Warren * Returns the rate of a peripheral clock in Hz. Since the caller almost 200150c2493STom Warren * certainly knows the parent clock (having just set it) we require that 201150c2493STom Warren * this be passed in so we don't need to work it out. 202150c2493STom Warren * 203150c2493STom Warren * @param periph_id peripheral to start 204150c2493STom Warren * @param parent PLL id of parent clock (used to calculate rate, you 205150c2493STom Warren * must know this!) 206150c2493STom Warren * @return clock rate of peripheral in Hz 207150c2493STom Warren */ 208150c2493STom Warren unsigned long clock_get_periph_rate(enum periph_id periph_id, 209150c2493STom Warren enum clock_id parent); 210150c2493STom Warren 211150c2493STom Warren /** 212150c2493STom Warren * Adjust peripheral PLL clock to the given rate. This does not reset the 213150c2493STom Warren * peripheral. If a second stage divisor is not available, pass NULL for 214150c2493STom Warren * extra_div. If it is available, then this parameter will return the 215150c2493STom Warren * divisor selected (which will be a power of 2 from 1 to 256). 216150c2493STom Warren * 217150c2493STom Warren * @param periph_id peripheral to start 218150c2493STom Warren * @param parent PLL id of required parent clock 219150c2493STom Warren * @param rate Required clock rate in Hz 220150c2493STom Warren * @param extra_div value for the second-stage divisor (NULL if one is 221150c2493STom Warren not available) 222150c2493STom Warren * @return rate selected in Hz, or -1U if something went wrong 223150c2493STom Warren */ 224150c2493STom Warren unsigned clock_adjust_periph_pll_div(enum periph_id periph_id, 225150c2493STom Warren enum clock_id parent, unsigned rate, int *extra_div); 226150c2493STom Warren 227150c2493STom Warren /** 228150c2493STom Warren * Returns the clock rate of a specified clock, in Hz. 229150c2493STom Warren * 230150c2493STom Warren * @param parent PLL id of clock to check 231150c2493STom Warren * @return rate of clock in Hz 232150c2493STom Warren */ 233150c2493STom Warren unsigned clock_get_rate(enum clock_id clkid); 234150c2493STom Warren 235150c2493STom Warren /** 236150c2493STom Warren * Start up a UART using low-level calls 237150c2493STom Warren * 238150c2493STom Warren * Prior to relocation clock_start_periph_pll() cannot be called. This 239150c2493STom Warren * function provides a way to set up a UART using low-level calls which 240150c2493STom Warren * do not require BSS. 241150c2493STom Warren * 242150c2493STom Warren * @param periph_id Peripheral ID of UART to enable (e,g, PERIPH_ID_UART1) 243150c2493STom Warren */ 244150c2493STom Warren void clock_ll_start_uart(enum periph_id periph_id); 245150c2493STom Warren 246150c2493STom Warren /** 247150c2493STom Warren * Decode a peripheral ID from a device tree node. 248150c2493STom Warren * 249150c2493STom Warren * This works by looking up the peripheral's 'clocks' node and reading out 250150c2493STom Warren * the second cell, which is the clock number / peripheral ID. 251150c2493STom Warren * 252150c2493STom Warren * @param blob FDT blob to use 253150c2493STom Warren * @param node Node to look at 254150c2493STom Warren * @return peripheral ID, or PERIPH_ID_NONE if none 255150c2493STom Warren */ 256150c2493STom Warren enum periph_id clock_decode_periph_id(const void *blob, int node); 257150c2493STom Warren 258150c2493STom Warren /** 259150c2493STom Warren * Checks if the oscillator bypass is enabled (XOBP bit) 260150c2493STom Warren * 261150c2493STom Warren * @return 1 if bypass is enabled, 0 if not 262150c2493STom Warren */ 263150c2493STom Warren int clock_get_osc_bypass(void); 264150c2493STom Warren 265150c2493STom Warren /* 266150c2493STom Warren * Checks that clocks are valid and prints a warning if not 267150c2493STom Warren * 268150c2493STom Warren * @return 0 if ok, -1 on error 269150c2493STom Warren */ 270150c2493STom Warren int clock_verify(void); 271150c2493STom Warren 272150c2493STom Warren /* Initialize the clocks */ 273150c2493STom Warren void clock_init(void); 274150c2493STom Warren 275150c2493STom Warren /* Initialize the PLLs */ 276150c2493STom Warren void clock_early_init(void); 277150c2493STom Warren 278f29f086aSTom Warren /* Returns a pointer to the clock source register for a peripheral */ 279f29f086aSTom Warren u32 *get_periph_source_reg(enum periph_id periph_id); 280f29f086aSTom Warren 281801b05cdSSimon Glass /* Returns a pointer to the given 'simple' PLL */ 282801b05cdSSimon Glass struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid); 283801b05cdSSimon Glass 284f29f086aSTom Warren /** 285f29f086aSTom Warren * Given a peripheral ID and the required source clock, this returns which 286f29f086aSTom Warren * value should be programmed into the source mux for that peripheral. 287f29f086aSTom Warren * 288f29f086aSTom Warren * There is special code here to handle the one source type with 5 sources. 289f29f086aSTom Warren * 290f29f086aSTom Warren * @param periph_id peripheral to start 291f29f086aSTom Warren * @param source PLL id of required parent clock 292f29f086aSTom Warren * @param mux_bits Set to number of bits in mux register: 2 or 4 293f29f086aSTom Warren * @param divider_bits Set to number of divider bits (8 or 16) 294f29f086aSTom Warren * @return mux value (0-4, or -1 if not found) 295f29f086aSTom Warren */ 296f29f086aSTom Warren int get_periph_clock_source(enum periph_id periph_id, 297f29f086aSTom Warren enum clock_id parent, int *mux_bits, int *divider_bits); 298f29f086aSTom Warren 299f29f086aSTom Warren /* 300f29f086aSTom Warren * Convert a device tree clock ID to our peripheral ID. They are mostly 301f29f086aSTom Warren * the same but we are very cautious so we check that a valid clock ID is 302f29f086aSTom Warren * provided. 303f29f086aSTom Warren * 304f29f086aSTom Warren * @param clk_id Clock ID according to tegra30 device tree binding 305f29f086aSTom Warren * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid 306f29f086aSTom Warren */ 307f29f086aSTom Warren enum periph_id clk_id_to_periph_id(int clk_id); 308f29f086aSTom Warren 309f29f086aSTom Warren /** 310f29f086aSTom Warren * Set the output frequency you want for each PLL clock. 311f29f086aSTom Warren * PLL output frequencies are programmed by setting their N, M and P values. 312f29f086aSTom Warren * The governing equations are: 313f29f086aSTom Warren * VCO = (Fi / m) * n, Fo = VCO / (2^p) 314f29f086aSTom Warren * where Fo is the output frequency from the PLL. 315f29f086aSTom Warren * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi) 316f29f086aSTom Warren * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1 317f29f086aSTom Warren * Please see Tegra TRM section 5.3 to get the detail for PLL Programming 318f29f086aSTom Warren * 319f29f086aSTom Warren * @param n PLL feedback divider(DIVN) 320f29f086aSTom Warren * @param m PLL input divider(DIVN) 321f29f086aSTom Warren * @param p post divider(DIVP) 322f29f086aSTom Warren * @param cpcon base PLL charge pump(CPCON) 323f29f086aSTom Warren * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot 324f29f086aSTom Warren * be overriden), 1 if PLL is already correct 325f29f086aSTom Warren */ 326f29f086aSTom Warren int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon); 327f29f086aSTom Warren 328f29f086aSTom Warren /* return 1 if a peripheral ID is in range */ 329f29f086aSTom Warren #define clock_type_id_isvalid(id) ((id) >= 0 && \ 330f29f086aSTom Warren (id) < CLOCK_TYPE_COUNT) 331f29f086aSTom Warren 332f29f086aSTom Warren /* return 1 if a periphc_internal_id is in range */ 333f29f086aSTom Warren #define periphc_internal_id_isvalid(id) ((id) >= 0 && \ 334f29f086aSTom Warren (id) < PERIPHC_COUNT) 335f29f086aSTom Warren 336b40f734aSTom Warren /* SoC-specific TSC init */ 337b40f734aSTom Warren void arch_timer_init(void); 338b40f734aSTom Warren 339b9dd6215SJimmy Zhang void tegra30_set_up_pllp(void); 340b9dd6215SJimmy Zhang 341746dc76bSSimon Glass /** 342746dc76bSSimon Glass * Enable output clock for external peripherals 343746dc76bSSimon Glass * 344746dc76bSSimon Glass * @param clk_id Clock ID to output (1, 2 or 3) 345746dc76bSSimon Glass * @return 0 if OK. -ve on error 346746dc76bSSimon Glass */ 347746dc76bSSimon Glass int clock_external_output(int clk_id); 348746dc76bSSimon Glass 349dc89ad14STom Warren #endif /* _TEGRA_CLOCK_H_ */ 350