2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-08-30 22:55:31 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2017-09-20 06:28:19 +00:00
|
|
|
#include <bitfield.h>
|
2016-06-17 15:44:00 +00:00
|
|
|
#include <clk-uclass.h>
|
2018-12-28 03:15:20 +00:00
|
|
|
#include <div64.h>
|
2015-08-30 22:55:31 +00:00
|
|
|
#include <dm.h>
|
2016-07-04 17:58:29 +00:00
|
|
|
#include <dt-structs.h>
|
2015-08-30 22:55:31 +00:00
|
|
|
#include <errno.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2016-07-04 17:58:29 +00:00
|
|
|
#include <mapmem.h>
|
2015-08-30 22:55:31 +00:00
|
|
|
#include <syscon.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2015-08-30 22:55:31 +00:00
|
|
|
#include <asm/io.h>
|
2019-03-28 03:01:23 +00:00
|
|
|
#include <asm/arch-rockchip/clock.h>
|
2020-01-09 08:52:17 +00:00
|
|
|
#include <asm/arch-rockchip/cru.h>
|
2019-03-28 03:01:23 +00:00
|
|
|
#include <asm/arch-rockchip/grf_rk3288.h>
|
|
|
|
#include <asm/arch-rockchip/hardware.h>
|
2016-01-22 02:43:38 +00:00
|
|
|
#include <dt-bindings/clock/rk3288-cru.h>
|
2016-01-22 02:43:41 +00:00
|
|
|
#include <dm/device-internal.h>
|
2015-08-30 22:55:31 +00:00
|
|
|
#include <dm/lists.h>
|
2016-01-22 02:43:41 +00:00
|
|
|
#include <dm/uclass-internal.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2020-02-03 14:36:15 +00:00
|
|
|
#include <linux/err.h>
|
2016-07-22 21:51:06 +00:00
|
|
|
#include <linux/log2.h>
|
2020-05-10 17:40:09 +00:00
|
|
|
#include <linux/stringify.h>
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2016-07-04 17:58:29 +00:00
|
|
|
struct rk3288_clk_plat {
|
|
|
|
#if CONFIG_IS_ENABLED(OF_PLATDATA)
|
|
|
|
struct dtd_rockchip_rk3288_cru dtd;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2015-08-30 22:55:31 +00:00
|
|
|
struct pll_div {
|
|
|
|
u32 nr;
|
|
|
|
u32 nf;
|
|
|
|
u32 no;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
VCO_MAX_HZ = 2200U * 1000000,
|
|
|
|
VCO_MIN_HZ = 440 * 1000000,
|
|
|
|
OUTPUT_MAX_HZ = 2200U * 1000000,
|
|
|
|
OUTPUT_MIN_HZ = 27500000,
|
|
|
|
FREF_MAX_HZ = 2200U * 1000000,
|
2016-07-15 22:17:17 +00:00
|
|
|
FREF_MIN_HZ = 269 * 1000,
|
2015-08-30 22:55:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
/* PLL CON0 */
|
|
|
|
PLL_OD_MASK = 0x0f,
|
|
|
|
|
|
|
|
/* PLL CON1 */
|
|
|
|
PLL_NF_MASK = 0x1fff,
|
|
|
|
|
|
|
|
/* PLL CON2 */
|
|
|
|
PLL_BWADJ_MASK = 0x0fff,
|
|
|
|
|
|
|
|
/* PLL CON3 */
|
|
|
|
PLL_RESET_SHIFT = 5,
|
|
|
|
|
2016-01-22 02:45:17 +00:00
|
|
|
/* CLKSEL0 */
|
|
|
|
CORE_SEL_PLL_SHIFT = 15,
|
2017-05-31 23:57:31 +00:00
|
|
|
CORE_SEL_PLL_MASK = 1 << CORE_SEL_PLL_SHIFT,
|
2016-01-22 02:45:17 +00:00
|
|
|
A17_DIV_SHIFT = 8,
|
2017-05-31 23:57:31 +00:00
|
|
|
A17_DIV_MASK = 0x1f << A17_DIV_SHIFT,
|
2016-01-22 02:45:17 +00:00
|
|
|
MP_DIV_SHIFT = 4,
|
2017-05-31 23:57:31 +00:00
|
|
|
MP_DIV_MASK = 0xf << MP_DIV_SHIFT,
|
2016-01-22 02:45:17 +00:00
|
|
|
M0_DIV_SHIFT = 0,
|
2017-05-31 23:57:31 +00:00
|
|
|
M0_DIV_MASK = 0xf << M0_DIV_SHIFT,
|
2016-01-22 02:45:17 +00:00
|
|
|
|
2015-08-30 22:55:31 +00:00
|
|
|
/* CLKSEL1: pd bus clk pll sel: codec or general */
|
|
|
|
PD_BUS_SEL_PLL_MASK = 15,
|
|
|
|
PD_BUS_SEL_CPLL = 0,
|
|
|
|
PD_BUS_SEL_GPLL,
|
|
|
|
|
|
|
|
/* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */
|
|
|
|
PD_BUS_PCLK_DIV_SHIFT = 12,
|
2017-05-31 23:57:31 +00:00
|
|
|
PD_BUS_PCLK_DIV_MASK = 7 << PD_BUS_PCLK_DIV_SHIFT,
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
/* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
|
|
|
|
PD_BUS_HCLK_DIV_SHIFT = 8,
|
2017-05-31 23:57:31 +00:00
|
|
|
PD_BUS_HCLK_DIV_MASK = 3 << PD_BUS_HCLK_DIV_SHIFT,
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
/* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */
|
|
|
|
PD_BUS_ACLK_DIV0_SHIFT = 3,
|
2017-05-31 23:57:31 +00:00
|
|
|
PD_BUS_ACLK_DIV0_MASK = 0x1f << PD_BUS_ACLK_DIV0_SHIFT,
|
2015-08-30 22:55:31 +00:00
|
|
|
PD_BUS_ACLK_DIV1_SHIFT = 0,
|
2017-05-31 23:57:31 +00:00
|
|
|
PD_BUS_ACLK_DIV1_MASK = 0x7 << PD_BUS_ACLK_DIV1_SHIFT,
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CLKSEL10
|
|
|
|
* peripheral bus pclk div:
|
|
|
|
* aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
|
|
|
|
*/
|
2016-01-22 02:45:15 +00:00
|
|
|
PERI_SEL_PLL_SHIFT = 15,
|
2017-05-31 23:57:31 +00:00
|
|
|
PERI_SEL_PLL_MASK = 1 << PERI_SEL_PLL_SHIFT,
|
2016-01-22 02:45:15 +00:00
|
|
|
PERI_SEL_CPLL = 0,
|
|
|
|
PERI_SEL_GPLL,
|
|
|
|
|
2015-08-30 22:55:31 +00:00
|
|
|
PERI_PCLK_DIV_SHIFT = 12,
|
2017-05-31 23:57:31 +00:00
|
|
|
PERI_PCLK_DIV_MASK = 3 << PERI_PCLK_DIV_SHIFT,
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
/* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
|
|
|
|
PERI_HCLK_DIV_SHIFT = 8,
|
2017-05-31 23:57:31 +00:00
|
|
|
PERI_HCLK_DIV_MASK = 3 << PERI_HCLK_DIV_SHIFT,
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* peripheral bus aclk div:
|
|
|
|
* aclk_periph = periph_clk_src / (peri_aclk_div_con + 1)
|
|
|
|
*/
|
|
|
|
PERI_ACLK_DIV_SHIFT = 0,
|
2017-05-31 23:57:31 +00:00
|
|
|
PERI_ACLK_DIV_MASK = 0x1f << PERI_ACLK_DIV_SHIFT,
|
2015-08-30 22:55:31 +00:00
|
|
|
|
2017-09-20 06:28:19 +00:00
|
|
|
/*
|
|
|
|
* CLKSEL24
|
|
|
|
* saradc_div_con:
|
|
|
|
* clk_saradc=24MHz/(saradc_div_con+1)
|
|
|
|
*/
|
|
|
|
CLK_SARADC_DIV_CON_SHIFT = 8,
|
|
|
|
CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
|
|
|
|
CLK_SARADC_DIV_CON_WIDTH = 8,
|
|
|
|
|
2015-08-30 22:55:31 +00:00
|
|
|
SOCSTS_DPLL_LOCK = 1 << 5,
|
|
|
|
SOCSTS_APLL_LOCK = 1 << 6,
|
|
|
|
SOCSTS_CPLL_LOCK = 1 << 7,
|
|
|
|
SOCSTS_GPLL_LOCK = 1 << 8,
|
|
|
|
SOCSTS_NPLL_LOCK = 1 << 9,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
|
|
|
|
|
|
|
|
#define PLL_DIVISORS(hz, _nr, _no) {\
|
|
|
|
.nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\
|
|
|
|
_Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
|
|
|
|
(_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\
|
|
|
|
"divisors on line " __stringify(__LINE__));
|
|
|
|
|
|
|
|
/* Keep divisors as low as possible to reduce jitter and power usage */
|
|
|
|
static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1);
|
|
|
|
static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2);
|
|
|
|
static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2);
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static int rkclk_set_pll(struct rockchip_cru *cru, enum rk_clk_id clk_id,
|
2015-08-30 22:55:31 +00:00
|
|
|
const struct pll_div *div)
|
|
|
|
{
|
|
|
|
int pll_id = rk_pll_id(clk_id);
|
|
|
|
struct rk3288_pll *pll = &cru->pll[pll_id];
|
|
|
|
/* All PLLs have same VCO and output frequency range restrictions. */
|
|
|
|
uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
|
|
|
|
uint output_hz = vco_hz / div->no;
|
|
|
|
|
2016-01-22 02:45:15 +00:00
|
|
|
debug("PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
|
|
|
|
(uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz);
|
2015-08-30 22:55:31 +00:00
|
|
|
assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
|
|
|
|
output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ &&
|
|
|
|
(div->no == 1 || !(div->no % 2)));
|
|
|
|
|
2016-01-22 02:45:15 +00:00
|
|
|
/* enter reset */
|
2015-08-30 22:55:31 +00:00
|
|
|
rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT);
|
|
|
|
|
2017-05-31 23:57:31 +00:00
|
|
|
rk_clrsetreg(&pll->con0, CLKR_MASK | PLL_OD_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
((div->nr - 1) << CLKR_SHIFT) | (div->no - 1));
|
|
|
|
rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1);
|
|
|
|
rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
|
|
|
|
|
|
|
|
udelay(10);
|
|
|
|
|
2016-01-22 02:45:15 +00:00
|
|
|
/* return from reset */
|
2015-08-30 22:55:31 +00:00
|
|
|
rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static int rkclk_configure_ddr(struct rockchip_cru *cru, struct rk3288_grf *grf,
|
2015-08-30 22:55:31 +00:00
|
|
|
unsigned int hz)
|
|
|
|
{
|
|
|
|
static const struct pll_div dpll_cfg[] = {
|
|
|
|
{.nf = 25, .nr = 2, .no = 1},
|
|
|
|
{.nf = 400, .nr = 9, .no = 2},
|
|
|
|
{.nf = 500, .nr = 9, .no = 2},
|
|
|
|
{.nf = 100, .nr = 3, .no = 1},
|
|
|
|
};
|
|
|
|
int cfg;
|
|
|
|
|
|
|
|
switch (hz) {
|
|
|
|
case 300000000:
|
|
|
|
cfg = 0;
|
|
|
|
break;
|
|
|
|
case 533000000: /* actually 533.3P MHz */
|
|
|
|
cfg = 1;
|
|
|
|
break;
|
|
|
|
case 666000000: /* actually 666.6P MHz */
|
|
|
|
cfg = 2;
|
|
|
|
break;
|
|
|
|
case 800000000:
|
|
|
|
cfg = 3;
|
|
|
|
break;
|
|
|
|
default:
|
2016-01-22 02:45:15 +00:00
|
|
|
debug("Unsupported SDRAM frequency");
|
2015-08-30 22:55:31 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pll enter slow-mode */
|
2017-05-31 23:57:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
DPLL_MODE_SLOW << DPLL_MODE_SHIFT);
|
|
|
|
|
|
|
|
rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]);
|
|
|
|
|
|
|
|
/* wait for pll lock */
|
|
|
|
while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK))
|
|
|
|
udelay(1);
|
|
|
|
|
|
|
|
/* PLL enter normal-mode */
|
2017-05-31 23:57:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
|
2016-01-22 02:45:01 +00:00
|
|
|
DPLL_MODE_NORMAL << DPLL_MODE_SHIFT);
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:45:02 +00:00
|
|
|
#ifndef CONFIG_SPL_BUILD
|
|
|
|
#define VCO_MAX_KHZ 2200000
|
|
|
|
#define VCO_MIN_KHZ 440000
|
|
|
|
#define FREF_MAX_KHZ 2200000
|
|
|
|
#define FREF_MIN_KHZ 269
|
|
|
|
|
|
|
|
static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div)
|
|
|
|
{
|
|
|
|
uint ref_khz = OSC_HZ / 1000, nr, nf = 0;
|
|
|
|
uint fref_khz;
|
|
|
|
uint diff_khz, best_diff_khz;
|
|
|
|
const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4;
|
|
|
|
uint vco_khz;
|
|
|
|
uint no = 1;
|
|
|
|
uint freq_khz = freq_hz / 1000;
|
|
|
|
|
|
|
|
if (!freq_hz) {
|
|
|
|
printf("%s: the frequency can not be 0 Hz\n", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
|
|
|
|
if (ext_div) {
|
|
|
|
*ext_div = DIV_ROUND_UP(no, max_no);
|
|
|
|
no = DIV_ROUND_UP(no, *ext_div);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only even divisors (and 1) are supported */
|
|
|
|
if (no > 1)
|
|
|
|
no = DIV_ROUND_UP(no, 2) * 2;
|
|
|
|
|
|
|
|
vco_khz = freq_khz * no;
|
|
|
|
if (ext_div)
|
|
|
|
vco_khz *= *ext_div;
|
|
|
|
|
|
|
|
if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) {
|
|
|
|
printf("%s: Cannot find out a supported VCO for Frequency (%luHz).\n",
|
|
|
|
__func__, freq_hz);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
div->no = no;
|
|
|
|
|
|
|
|
best_diff_khz = vco_khz;
|
|
|
|
for (nr = 1; nr < max_nr && best_diff_khz; nr++) {
|
|
|
|
fref_khz = ref_khz / nr;
|
|
|
|
if (fref_khz < FREF_MIN_KHZ)
|
|
|
|
break;
|
|
|
|
if (fref_khz > FREF_MAX_KHZ)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nf = vco_khz / fref_khz;
|
|
|
|
if (nf >= max_nf)
|
|
|
|
continue;
|
|
|
|
diff_khz = vco_khz - nf * fref_khz;
|
|
|
|
if (nf + 1 < max_nf && diff_khz > fref_khz / 2) {
|
|
|
|
nf++;
|
|
|
|
diff_khz = fref_khz - diff_khz;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diff_khz >= best_diff_khz)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
best_diff_khz = diff_khz;
|
|
|
|
div->nr = nr;
|
|
|
|
div->nf = nf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best_diff_khz > 4 * 1000) {
|
|
|
|
printf("%s: Failed to match output frequency %lu, difference is %u Hz, exceed 4MHZ\n",
|
|
|
|
__func__, freq_hz, best_diff_khz * 1000);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static int rockchip_mac_set_clk(struct rockchip_cru *cru, uint freq)
|
2016-02-28 21:24:59 +00:00
|
|
|
{
|
2018-01-13 06:06:33 +00:00
|
|
|
ulong ret;
|
2016-02-28 21:24:59 +00:00
|
|
|
|
2018-01-13 06:06:33 +00:00
|
|
|
/*
|
|
|
|
* The gmac clock can be derived either from an external clock
|
|
|
|
* or can be generated from internally by a divider from SCLK_MAC.
|
|
|
|
*/
|
|
|
|
if (readl(&cru->cru_clksel_con[21]) & RMII_EXTCLK_MASK) {
|
|
|
|
/* An external clock will always generate the right rate... */
|
|
|
|
ret = freq;
|
|
|
|
} else {
|
|
|
|
u32 con = readl(&cru->cru_clksel_con[21]);
|
|
|
|
ulong pll_rate;
|
|
|
|
u8 div;
|
|
|
|
|
|
|
|
if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
|
|
|
|
EMAC_PLL_SELECT_GENERAL)
|
|
|
|
pll_rate = GPLL_HZ;
|
|
|
|
else if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
|
|
|
|
EMAC_PLL_SELECT_CODEC)
|
|
|
|
pll_rate = CPLL_HZ;
|
|
|
|
else
|
|
|
|
pll_rate = NPLL_HZ;
|
|
|
|
|
|
|
|
div = DIV_ROUND_UP(pll_rate, freq) - 1;
|
|
|
|
if (div <= 0x1f)
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[21], MAC_DIV_CON_MASK,
|
|
|
|
div << MAC_DIV_CON_SHIFT);
|
|
|
|
else
|
|
|
|
debug("Unsupported div for gmac:%d\n", div);
|
|
|
|
|
|
|
|
return DIV_TO_RATE(pll_rate, div);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-02-28 21:24:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static int rockchip_vop_set_clk(struct rockchip_cru *cru, struct rk3288_grf *grf,
|
2016-01-22 02:45:02 +00:00
|
|
|
int periph, unsigned int rate_hz)
|
|
|
|
{
|
|
|
|
struct pll_div npll_config = {0};
|
|
|
|
u32 lcdc_div;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pll_para_config(rate_hz, &npll_config, &lcdc_div);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-05-31 23:57:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
|
2016-01-22 02:45:02 +00:00
|
|
|
NPLL_MODE_SLOW << NPLL_MODE_SHIFT);
|
|
|
|
rkclk_set_pll(cru, CLK_NEW, &npll_config);
|
|
|
|
|
|
|
|
/* waiting for pll lock */
|
|
|
|
while (1) {
|
|
|
|
if (readl(&grf->soc_status[1]) & SOCSTS_NPLL_LOCK)
|
|
|
|
break;
|
|
|
|
udelay(1);
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:57:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
|
2016-01-22 02:45:02 +00:00
|
|
|
NPLL_MODE_NORMAL << NPLL_MODE_SHIFT);
|
|
|
|
|
|
|
|
/* vop dclk source clk: npll,dclk_div: 1 */
|
|
|
|
switch (periph) {
|
|
|
|
case DCLK_VOP0:
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[27], 0xff << 8 | 3 << 0,
|
|
|
|
(lcdc_div - 1) << 8 | 2 << 0);
|
|
|
|
break;
|
|
|
|
case DCLK_VOP1:
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[29], 0xff << 8 | 3 << 6,
|
|
|
|
(lcdc_div - 1) << 8 | 2 << 6);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-28 03:15:20 +00:00
|
|
|
|
|
|
|
static u32 rockchip_clk_gcd(u32 a, u32 b)
|
|
|
|
{
|
|
|
|
while (b != 0) {
|
|
|
|
int r = b;
|
|
|
|
|
|
|
|
b = a % b;
|
|
|
|
a = r;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static ulong rockchip_i2s_get_clk(struct rockchip_cru *cru, uint gclk_rate)
|
2018-12-28 03:15:20 +00:00
|
|
|
{
|
|
|
|
unsigned long long rate;
|
|
|
|
uint val;
|
|
|
|
int n, d;
|
|
|
|
|
|
|
|
val = readl(&cru->cru_clksel_con[8]);
|
|
|
|
n = (val & I2S0_FRAC_NUMER_MASK) >> I2S0_FRAC_NUMER_SHIFT;
|
|
|
|
d = (val & I2S0_FRAC_DENOM_MASK) >> I2S0_FRAC_DENOM_SHIFT;
|
|
|
|
|
|
|
|
rate = (unsigned long long)gclk_rate * n;
|
|
|
|
do_div(rate, d);
|
|
|
|
|
|
|
|
return (ulong)rate;
|
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static ulong rockchip_i2s_set_clk(struct rockchip_cru *cru, uint gclk_rate,
|
2018-12-28 03:15:20 +00:00
|
|
|
uint freq)
|
|
|
|
{
|
|
|
|
int n, d;
|
|
|
|
int v;
|
|
|
|
|
|
|
|
/* set frac divider */
|
|
|
|
v = rockchip_clk_gcd(gclk_rate, freq);
|
|
|
|
n = gclk_rate / v;
|
|
|
|
d = freq / v;
|
|
|
|
assert(freq == gclk_rate / n * d);
|
|
|
|
writel(d << I2S0_FRAC_NUMER_SHIFT | n << I2S0_FRAC_DENOM_SHIFT,
|
|
|
|
&cru->cru_clksel_con[8]);
|
|
|
|
|
|
|
|
return rockchip_i2s_get_clk(cru, gclk_rate);
|
|
|
|
}
|
2017-05-31 23:57:32 +00:00
|
|
|
#endif /* CONFIG_SPL_BUILD */
|
2016-01-22 02:45:02 +00:00
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static void rkclk_init(struct rockchip_cru *cru, struct rk3288_grf *grf)
|
2015-08-30 22:55:31 +00:00
|
|
|
{
|
|
|
|
u32 aclk_div;
|
|
|
|
u32 hclk_div;
|
|
|
|
u32 pclk_div;
|
|
|
|
|
|
|
|
/* pll enter slow-mode */
|
|
|
|
rk_clrsetreg(&cru->cru_mode_con,
|
2017-05-31 23:57:31 +00:00
|
|
|
GPLL_MODE_MASK | CPLL_MODE_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
GPLL_MODE_SLOW << GPLL_MODE_SHIFT |
|
|
|
|
CPLL_MODE_SLOW << CPLL_MODE_SHIFT);
|
|
|
|
|
|
|
|
/* init pll */
|
|
|
|
rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
|
|
|
|
rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
|
|
|
|
|
|
|
|
/* waiting for pll lock */
|
|
|
|
while ((readl(&grf->soc_status[1]) &
|
|
|
|
(SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) !=
|
|
|
|
(SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK))
|
|
|
|
udelay(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pd_bus clock pll source selection and
|
|
|
|
* set up dependent divisors for PCLK/HCLK and ACLK clocks.
|
|
|
|
*/
|
|
|
|
aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1;
|
|
|
|
assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
|
|
|
|
hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1;
|
|
|
|
assert((hclk_div + 1) * PD_BUS_HCLK_HZ ==
|
|
|
|
PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2));
|
|
|
|
|
|
|
|
pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1;
|
|
|
|
assert((pclk_div + 1) * PD_BUS_PCLK_HZ ==
|
|
|
|
PD_BUS_ACLK_HZ && pclk_div < 0x7);
|
|
|
|
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[1],
|
2017-05-31 23:57:31 +00:00
|
|
|
PD_BUS_PCLK_DIV_MASK | PD_BUS_HCLK_DIV_MASK |
|
|
|
|
PD_BUS_ACLK_DIV0_MASK | PD_BUS_ACLK_DIV1_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
pclk_div << PD_BUS_PCLK_DIV_SHIFT |
|
|
|
|
hclk_div << PD_BUS_HCLK_DIV_SHIFT |
|
|
|
|
aclk_div << PD_BUS_ACLK_DIV0_SHIFT |
|
|
|
|
0 << 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* peri clock pll source selection and
|
|
|
|
* set up dependent divisors for PCLK/HCLK and ACLK clocks.
|
|
|
|
*/
|
|
|
|
aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1;
|
|
|
|
assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
|
|
|
|
|
2016-07-22 21:51:06 +00:00
|
|
|
hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ);
|
2015-08-30 22:55:31 +00:00
|
|
|
assert((1 << hclk_div) * PERI_HCLK_HZ ==
|
|
|
|
PERI_ACLK_HZ && (hclk_div < 0x4));
|
|
|
|
|
2016-07-22 21:51:06 +00:00
|
|
|
pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ);
|
2015-08-30 22:55:31 +00:00
|
|
|
assert((1 << pclk_div) * PERI_PCLK_HZ ==
|
|
|
|
PERI_ACLK_HZ && (pclk_div < 0x4));
|
|
|
|
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[10],
|
2017-05-31 23:57:31 +00:00
|
|
|
PERI_PCLK_DIV_MASK | PERI_HCLK_DIV_MASK |
|
|
|
|
PERI_ACLK_DIV_MASK,
|
2016-01-22 02:45:15 +00:00
|
|
|
PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT |
|
2015-08-30 22:55:31 +00:00
|
|
|
pclk_div << PERI_PCLK_DIV_SHIFT |
|
|
|
|
hclk_div << PERI_HCLK_DIV_SHIFT |
|
|
|
|
aclk_div << PERI_ACLK_DIV_SHIFT);
|
|
|
|
|
|
|
|
/* PLL enter normal-mode */
|
|
|
|
rk_clrsetreg(&cru->cru_mode_con,
|
2017-05-31 23:57:31 +00:00
|
|
|
GPLL_MODE_MASK | CPLL_MODE_MASK,
|
2016-01-22 02:45:01 +00:00
|
|
|
GPLL_MODE_NORMAL << GPLL_MODE_SHIFT |
|
|
|
|
CPLL_MODE_NORMAL << CPLL_MODE_SHIFT);
|
2015-08-30 22:55:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
void rk3288_clk_configure_cpu(struct rockchip_cru *cru, struct rk3288_grf *grf)
|
2016-01-22 02:45:17 +00:00
|
|
|
{
|
|
|
|
/* pll enter slow-mode */
|
2017-05-31 23:57:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
|
2016-01-22 02:45:17 +00:00
|
|
|
APLL_MODE_SLOW << APLL_MODE_SHIFT);
|
|
|
|
|
|
|
|
rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg);
|
|
|
|
|
|
|
|
/* waiting for pll lock */
|
|
|
|
while (!(readl(&grf->soc_status[1]) & SOCSTS_APLL_LOCK))
|
|
|
|
udelay(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* core clock pll source selection and
|
|
|
|
* set up dependent divisors for MPAXI/M0AXI and ARM clocks.
|
|
|
|
* core clock select apll, apll clk = 1800MHz
|
|
|
|
* arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz
|
|
|
|
*/
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[0],
|
2017-05-31 23:57:31 +00:00
|
|
|
CORE_SEL_PLL_MASK | A17_DIV_MASK | MP_DIV_MASK |
|
|
|
|
M0_DIV_MASK,
|
2016-01-22 02:45:17 +00:00
|
|
|
0 << A17_DIV_SHIFT |
|
|
|
|
3 << MP_DIV_SHIFT |
|
|
|
|
1 << M0_DIV_SHIFT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up dependent divisors for L2RAM/ATCLK and PCLK clocks.
|
|
|
|
* l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz
|
|
|
|
*/
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[37],
|
2017-05-31 23:57:31 +00:00
|
|
|
CLK_L2RAM_DIV_MASK | ATCLK_CORE_DIV_CON_MASK |
|
|
|
|
PCLK_CORE_DBG_DIV_MASK,
|
2016-01-22 02:45:17 +00:00
|
|
|
1 << CLK_L2RAM_DIV_SHIFT |
|
|
|
|
3 << ATCLK_CORE_DIV_CON_SHIFT |
|
|
|
|
3 << PCLK_CORE_DBG_DIV_SHIFT);
|
|
|
|
|
|
|
|
/* PLL enter normal-mode */
|
2017-05-31 23:57:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
|
2016-01-22 02:45:17 +00:00
|
|
|
APLL_MODE_NORMAL << APLL_MODE_SHIFT);
|
|
|
|
}
|
|
|
|
|
2015-08-30 22:55:31 +00:00
|
|
|
/* Get pll rate by id */
|
2020-01-09 08:52:17 +00:00
|
|
|
static uint32_t rkclk_pll_get_rate(struct rockchip_cru *cru,
|
2015-08-30 22:55:31 +00:00
|
|
|
enum rk_clk_id clk_id)
|
|
|
|
{
|
|
|
|
uint32_t nr, no, nf;
|
|
|
|
uint32_t con;
|
|
|
|
int pll_id = rk_pll_id(clk_id);
|
|
|
|
struct rk3288_pll *pll = &cru->pll[pll_id];
|
|
|
|
static u8 clk_shift[CLK_COUNT] = {
|
2016-01-22 02:45:01 +00:00
|
|
|
0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
|
|
|
|
GPLL_MODE_SHIFT, NPLL_MODE_SHIFT
|
2015-08-30 22:55:31 +00:00
|
|
|
};
|
|
|
|
uint shift;
|
|
|
|
|
|
|
|
con = readl(&cru->cru_mode_con);
|
|
|
|
shift = clk_shift[clk_id];
|
2017-05-31 23:57:31 +00:00
|
|
|
switch ((con >> shift) & CRU_MODE_MASK) {
|
2016-01-22 02:45:01 +00:00
|
|
|
case APLL_MODE_SLOW:
|
2015-08-30 22:55:31 +00:00
|
|
|
return OSC_HZ;
|
2016-01-22 02:45:01 +00:00
|
|
|
case APLL_MODE_NORMAL:
|
2015-08-30 22:55:31 +00:00
|
|
|
/* normal mode */
|
|
|
|
con = readl(&pll->con0);
|
2017-05-31 23:57:31 +00:00
|
|
|
no = ((con & CLKOD_MASK) >> CLKOD_SHIFT) + 1;
|
|
|
|
nr = ((con & CLKR_MASK) >> CLKR_SHIFT) + 1;
|
2015-08-30 22:55:31 +00:00
|
|
|
con = readl(&pll->con1);
|
2017-05-31 23:57:31 +00:00
|
|
|
nf = ((con & CLKF_MASK) >> CLKF_SHIFT) + 1;
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
return (24 * nf / (nr * no)) * 1000000;
|
2016-01-22 02:45:01 +00:00
|
|
|
case APLL_MODE_DEEP:
|
2015-08-30 22:55:31 +00:00
|
|
|
default:
|
|
|
|
return 32768;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static ulong rockchip_mmc_get_clk(struct rockchip_cru *cru, uint gclk_rate,
|
2016-01-22 02:43:38 +00:00
|
|
|
int periph)
|
2015-08-30 22:55:31 +00:00
|
|
|
{
|
|
|
|
uint src_rate;
|
|
|
|
uint div, mux;
|
|
|
|
u32 con;
|
|
|
|
|
|
|
|
switch (periph) {
|
2016-01-22 02:43:38 +00:00
|
|
|
case HCLK_EMMC:
|
2017-04-16 09:44:45 +00:00
|
|
|
case SCLK_EMMC:
|
2015-08-30 22:55:31 +00:00
|
|
|
con = readl(&cru->cru_clksel_con[12]);
|
2017-05-31 23:57:31 +00:00
|
|
|
mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT;
|
|
|
|
div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
|
2015-08-30 22:55:31 +00:00
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case HCLK_SDMMC:
|
2017-04-16 09:44:45 +00:00
|
|
|
case SCLK_SDMMC:
|
2016-01-22 02:43:38 +00:00
|
|
|
con = readl(&cru->cru_clksel_con[11]);
|
2017-05-31 23:57:31 +00:00
|
|
|
mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT;
|
|
|
|
div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT;
|
2015-08-30 22:55:31 +00:00
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case HCLK_SDIO0:
|
2017-04-16 09:44:45 +00:00
|
|
|
case SCLK_SDIO0:
|
2015-08-30 22:55:31 +00:00
|
|
|
con = readl(&cru->cru_clksel_con[12]);
|
2017-05-31 23:57:31 +00:00
|
|
|
mux = (con & SDIO0_PLL_MASK) >> SDIO0_PLL_SHIFT;
|
|
|
|
div = (con & SDIO0_DIV_MASK) >> SDIO0_DIV_SHIFT;
|
2015-08-30 22:55:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:43:39 +00:00
|
|
|
src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate;
|
2015-08-30 22:55:31 +00:00
|
|
|
return DIV_TO_RATE(src_rate, div);
|
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static ulong rockchip_mmc_set_clk(struct rockchip_cru *cru, uint gclk_rate,
|
2016-01-22 02:43:38 +00:00
|
|
|
int periph, uint freq)
|
2015-08-30 22:55:31 +00:00
|
|
|
{
|
|
|
|
int src_clk_div;
|
|
|
|
int mux;
|
|
|
|
|
2016-01-22 02:43:39 +00:00
|
|
|
debug("%s: gclk_rate=%u\n", __func__, gclk_rate);
|
2017-07-27 04:54:01 +00:00
|
|
|
/* mmc clock default div 2 internal, need provide double in cru */
|
|
|
|
src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq);
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
if (src_clk_div > 0x3f) {
|
2017-07-27 04:54:01 +00:00
|
|
|
src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq);
|
2017-07-27 04:54:02 +00:00
|
|
|
assert(src_clk_div < 0x40);
|
2015-08-30 22:55:31 +00:00
|
|
|
mux = EMMC_PLL_SELECT_24MHZ;
|
|
|
|
assert((int)EMMC_PLL_SELECT_24MHZ ==
|
|
|
|
(int)MMC0_PLL_SELECT_24MHZ);
|
|
|
|
} else {
|
|
|
|
mux = EMMC_PLL_SELECT_GENERAL;
|
|
|
|
assert((int)EMMC_PLL_SELECT_GENERAL ==
|
|
|
|
(int)MMC0_PLL_SELECT_GENERAL);
|
|
|
|
}
|
|
|
|
switch (periph) {
|
2016-01-22 02:43:38 +00:00
|
|
|
case HCLK_EMMC:
|
2017-04-16 09:44:45 +00:00
|
|
|
case SCLK_EMMC:
|
2015-08-30 22:55:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[12],
|
2017-05-31 23:57:31 +00:00
|
|
|
EMMC_PLL_MASK | EMMC_DIV_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
mux << EMMC_PLL_SHIFT |
|
|
|
|
(src_clk_div - 1) << EMMC_DIV_SHIFT);
|
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case HCLK_SDMMC:
|
2017-04-16 09:44:45 +00:00
|
|
|
case SCLK_SDMMC:
|
2015-08-30 22:55:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[11],
|
2017-05-31 23:57:31 +00:00
|
|
|
MMC0_PLL_MASK | MMC0_DIV_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
mux << MMC0_PLL_SHIFT |
|
|
|
|
(src_clk_div - 1) << MMC0_DIV_SHIFT);
|
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case HCLK_SDIO0:
|
2017-04-16 09:44:45 +00:00
|
|
|
case SCLK_SDIO0:
|
2015-08-30 22:55:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[12],
|
2017-05-31 23:57:31 +00:00
|
|
|
SDIO0_PLL_MASK | SDIO0_DIV_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
mux << SDIO0_PLL_SHIFT |
|
|
|
|
(src_clk_div - 1) << SDIO0_DIV_SHIFT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:43:39 +00:00
|
|
|
return rockchip_mmc_get_clk(cru, gclk_rate, periph);
|
2015-08-30 22:55:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static ulong rockchip_spi_get_clk(struct rockchip_cru *cru, uint gclk_rate,
|
2016-01-22 02:43:38 +00:00
|
|
|
int periph)
|
2015-08-30 22:55:31 +00:00
|
|
|
{
|
|
|
|
uint div, mux;
|
|
|
|
u32 con;
|
|
|
|
|
|
|
|
switch (periph) {
|
2016-01-22 02:43:38 +00:00
|
|
|
case SCLK_SPI0:
|
2015-08-30 22:55:31 +00:00
|
|
|
con = readl(&cru->cru_clksel_con[25]);
|
2017-05-31 23:57:31 +00:00
|
|
|
mux = (con & SPI0_PLL_MASK) >> SPI0_PLL_SHIFT;
|
|
|
|
div = (con & SPI0_DIV_MASK) >> SPI0_DIV_SHIFT;
|
2015-08-30 22:55:31 +00:00
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case SCLK_SPI1:
|
2015-08-30 22:55:31 +00:00
|
|
|
con = readl(&cru->cru_clksel_con[25]);
|
2017-05-31 23:57:31 +00:00
|
|
|
mux = (con & SPI1_PLL_MASK) >> SPI1_PLL_SHIFT;
|
|
|
|
div = (con & SPI1_DIV_MASK) >> SPI1_DIV_SHIFT;
|
2015-08-30 22:55:31 +00:00
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case SCLK_SPI2:
|
2015-08-30 22:55:31 +00:00
|
|
|
con = readl(&cru->cru_clksel_con[39]);
|
2017-05-31 23:57:31 +00:00
|
|
|
mux = (con & SPI2_PLL_MASK) >> SPI2_PLL_SHIFT;
|
|
|
|
div = (con & SPI2_DIV_MASK) >> SPI2_DIV_SHIFT;
|
2015-08-30 22:55:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
assert(mux == SPI0_PLL_SELECT_GENERAL);
|
|
|
|
|
2016-01-22 02:43:39 +00:00
|
|
|
return DIV_TO_RATE(gclk_rate, div);
|
2015-08-30 22:55:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static ulong rockchip_spi_set_clk(struct rockchip_cru *cru, uint gclk_rate,
|
2016-01-22 02:43:38 +00:00
|
|
|
int periph, uint freq)
|
2015-08-30 22:55:31 +00:00
|
|
|
{
|
|
|
|
int src_clk_div;
|
|
|
|
|
2016-01-22 02:43:39 +00:00
|
|
|
debug("%s: clk_general_rate=%u\n", __func__, gclk_rate);
|
2017-07-27 04:54:02 +00:00
|
|
|
src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1;
|
|
|
|
assert(src_clk_div < 128);
|
2015-08-30 22:55:31 +00:00
|
|
|
switch (periph) {
|
2016-01-22 02:43:38 +00:00
|
|
|
case SCLK_SPI0:
|
2015-08-30 22:55:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[25],
|
2017-05-31 23:57:31 +00:00
|
|
|
SPI0_PLL_MASK | SPI0_DIV_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT |
|
|
|
|
src_clk_div << SPI0_DIV_SHIFT);
|
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case SCLK_SPI1:
|
2015-08-30 22:55:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[25],
|
2017-05-31 23:57:31 +00:00
|
|
|
SPI1_PLL_MASK | SPI1_DIV_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT |
|
|
|
|
src_clk_div << SPI1_DIV_SHIFT);
|
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case SCLK_SPI2:
|
2015-08-30 22:55:31 +00:00
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[39],
|
2017-05-31 23:57:31 +00:00
|
|
|
SPI2_PLL_MASK | SPI2_DIV_MASK,
|
2015-08-30 22:55:31 +00:00
|
|
|
SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT |
|
|
|
|
src_clk_div << SPI2_DIV_SHIFT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:43:39 +00:00
|
|
|
return rockchip_spi_get_clk(cru, gclk_rate, periph);
|
2015-08-30 22:55:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static ulong rockchip_saradc_get_clk(struct rockchip_cru *cru)
|
2017-09-20 06:28:19 +00:00
|
|
|
{
|
|
|
|
u32 div, val;
|
|
|
|
|
|
|
|
val = readl(&cru->cru_clksel_con[24]);
|
|
|
|
div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
|
|
|
|
CLK_SARADC_DIV_CON_WIDTH);
|
|
|
|
|
|
|
|
return DIV_TO_RATE(OSC_HZ, div);
|
|
|
|
}
|
|
|
|
|
2020-01-09 08:52:17 +00:00
|
|
|
static ulong rockchip_saradc_set_clk(struct rockchip_cru *cru, uint hz)
|
2017-09-20 06:28:19 +00:00
|
|
|
{
|
|
|
|
int src_clk_div;
|
|
|
|
|
|
|
|
src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
|
|
|
|
assert(src_clk_div < 128);
|
|
|
|
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[24],
|
|
|
|
CLK_SARADC_DIV_CON_MASK,
|
|
|
|
src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
|
|
|
|
|
|
|
|
return rockchip_saradc_get_clk(cru);
|
|
|
|
}
|
|
|
|
|
2016-06-17 15:44:00 +00:00
|
|
|
static ulong rk3288_clk_get_rate(struct clk *clk)
|
2016-01-22 02:43:40 +00:00
|
|
|
{
|
2016-06-17 15:44:00 +00:00
|
|
|
struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
|
2016-01-22 02:43:40 +00:00
|
|
|
ulong new_rate, gclk_rate;
|
|
|
|
|
2016-06-17 15:44:00 +00:00
|
|
|
gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
|
|
|
|
switch (clk->id) {
|
|
|
|
case 0 ... 63:
|
|
|
|
new_rate = rkclk_pll_get_rate(priv->cru, clk->id);
|
|
|
|
break;
|
2016-01-22 02:43:40 +00:00
|
|
|
case HCLK_EMMC:
|
2016-01-22 02:43:45 +00:00
|
|
|
case HCLK_SDMMC:
|
2016-01-22 02:43:40 +00:00
|
|
|
case HCLK_SDIO0:
|
2017-04-16 09:44:45 +00:00
|
|
|
case SCLK_EMMC:
|
|
|
|
case SCLK_SDMMC:
|
|
|
|
case SCLK_SDIO0:
|
2016-06-17 15:44:00 +00:00
|
|
|
new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, clk->id);
|
2016-01-22 02:43:40 +00:00
|
|
|
break;
|
|
|
|
case SCLK_SPI0:
|
|
|
|
case SCLK_SPI1:
|
|
|
|
case SCLK_SPI2:
|
2016-06-17 15:44:00 +00:00
|
|
|
new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, clk->id);
|
2016-01-22 02:43:40 +00:00
|
|
|
break;
|
|
|
|
case PCLK_I2C0:
|
|
|
|
case PCLK_I2C1:
|
|
|
|
case PCLK_I2C2:
|
|
|
|
case PCLK_I2C3:
|
|
|
|
case PCLK_I2C4:
|
|
|
|
case PCLK_I2C5:
|
|
|
|
return gclk_rate;
|
2016-08-12 09:57:05 +00:00
|
|
|
case PCLK_PWM:
|
|
|
|
return PD_BUS_PCLK_HZ;
|
2017-09-20 06:28:19 +00:00
|
|
|
case SCLK_SARADC:
|
|
|
|
new_rate = rockchip_saradc_get_clk(priv->cru);
|
|
|
|
break;
|
2016-01-22 02:43:40 +00:00
|
|
|
default:
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_rate;
|
|
|
|
}
|
|
|
|
|
2016-06-17 15:44:00 +00:00
|
|
|
static ulong rk3288_clk_set_rate(struct clk *clk, ulong rate)
|
2015-08-30 22:55:31 +00:00
|
|
|
{
|
2016-06-17 15:44:00 +00:00
|
|
|
struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
|
2020-01-09 08:52:17 +00:00
|
|
|
struct rockchip_cru *cru = priv->cru;
|
2016-01-22 02:43:38 +00:00
|
|
|
ulong new_rate, gclk_rate;
|
|
|
|
|
2016-06-17 15:44:00 +00:00
|
|
|
gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
|
|
|
|
switch (clk->id) {
|
2016-11-13 21:22:13 +00:00
|
|
|
case PLL_APLL:
|
|
|
|
/* We only support a fixed rate here */
|
|
|
|
if (rate != 1800000000)
|
|
|
|
return -EINVAL;
|
|
|
|
rk3288_clk_configure_cpu(priv->cru, priv->grf);
|
|
|
|
new_rate = rate;
|
|
|
|
break;
|
2016-06-17 15:44:00 +00:00
|
|
|
case CLK_DDR:
|
|
|
|
new_rate = rkclk_configure_ddr(priv->cru, priv->grf, rate);
|
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case HCLK_EMMC:
|
|
|
|
case HCLK_SDMMC:
|
|
|
|
case HCLK_SDIO0:
|
2017-04-16 09:44:45 +00:00
|
|
|
case SCLK_EMMC:
|
|
|
|
case SCLK_SDMMC:
|
|
|
|
case SCLK_SDIO0:
|
2016-06-17 15:44:00 +00:00
|
|
|
new_rate = rockchip_mmc_set_clk(cru, gclk_rate, clk->id, rate);
|
2015-08-30 22:55:31 +00:00
|
|
|
break;
|
2016-01-22 02:43:38 +00:00
|
|
|
case SCLK_SPI0:
|
|
|
|
case SCLK_SPI1:
|
|
|
|
case SCLK_SPI2:
|
2016-06-17 15:44:00 +00:00
|
|
|
new_rate = rockchip_spi_set_clk(cru, gclk_rate, clk->id, rate);
|
2015-08-30 22:55:31 +00:00
|
|
|
break;
|
2016-01-22 02:45:02 +00:00
|
|
|
#ifndef CONFIG_SPL_BUILD
|
2018-12-28 03:15:20 +00:00
|
|
|
case SCLK_I2S0:
|
|
|
|
new_rate = rockchip_i2s_set_clk(cru, gclk_rate, rate);
|
|
|
|
break;
|
2016-02-28 21:24:59 +00:00
|
|
|
case SCLK_MAC:
|
2018-01-13 06:06:33 +00:00
|
|
|
new_rate = rockchip_mac_set_clk(priv->cru, rate);
|
2016-02-28 21:24:59 +00:00
|
|
|
break;
|
2016-01-22 02:45:02 +00:00
|
|
|
case DCLK_VOP0:
|
|
|
|
case DCLK_VOP1:
|
2016-06-17 15:44:00 +00:00
|
|
|
new_rate = rockchip_vop_set_clk(cru, priv->grf, clk->id, rate);
|
2016-01-22 02:45:02 +00:00
|
|
|
break;
|
|
|
|
case SCLK_EDP_24M:
|
|
|
|
/* clk_edp_24M source: 24M */
|
|
|
|
rk_setreg(&cru->cru_clksel_con[28], 1 << 15);
|
|
|
|
|
|
|
|
/* rst edp */
|
|
|
|
rk_setreg(&cru->cru_clksel_con[6], 1 << 15);
|
|
|
|
udelay(1);
|
|
|
|
rk_clrreg(&cru->cru_clksel_con[6], 1 << 15);
|
|
|
|
new_rate = rate;
|
|
|
|
break;
|
|
|
|
case ACLK_VOP0:
|
|
|
|
case ACLK_VOP1: {
|
|
|
|
u32 div;
|
|
|
|
|
|
|
|
/* vop aclk source clk: cpll */
|
|
|
|
div = CPLL_HZ / rate;
|
|
|
|
assert((div - 1 < 64) && (div * rate == CPLL_HZ));
|
|
|
|
|
2016-06-17 15:44:00 +00:00
|
|
|
switch (clk->id) {
|
2016-01-22 02:45:02 +00:00
|
|
|
case ACLK_VOP0:
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[31],
|
|
|
|
3 << 6 | 0x1f << 0,
|
|
|
|
0 << 6 | (div - 1) << 0);
|
|
|
|
break;
|
|
|
|
case ACLK_VOP1:
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[31],
|
|
|
|
3 << 14 | 0x1f << 8,
|
|
|
|
0 << 14 | (div - 1) << 8);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_rate = rate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PCLK_HDMI_CTRL:
|
|
|
|
/* enable pclk hdmi ctrl */
|
|
|
|
rk_clrreg(&cru->cru_clkgate_con[16], 1 << 9);
|
|
|
|
|
|
|
|
/* software reset hdmi */
|
|
|
|
rk_setreg(&cru->cru_clkgate_con[7], 1 << 9);
|
|
|
|
udelay(1);
|
|
|
|
rk_clrreg(&cru->cru_clkgate_con[7], 1 << 9);
|
|
|
|
new_rate = rate;
|
|
|
|
break;
|
|
|
|
#endif
|
2017-09-20 06:28:19 +00:00
|
|
|
case SCLK_SARADC:
|
|
|
|
new_rate = rockchip_saradc_set_clk(priv->cru, rate);
|
|
|
|
break;
|
2018-01-13 06:06:33 +00:00
|
|
|
case PLL_GPLL:
|
|
|
|
case PLL_CPLL:
|
|
|
|
case PLL_NPLL:
|
|
|
|
case ACLK_CPU:
|
|
|
|
case HCLK_CPU:
|
|
|
|
case PCLK_CPU:
|
|
|
|
case ACLK_PERI:
|
|
|
|
case HCLK_PERI:
|
|
|
|
case PCLK_PERI:
|
|
|
|
case SCLK_UART0:
|
|
|
|
return 0;
|
2015-08-30 22:55:31 +00:00
|
|
|
default:
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_rate;
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:27:10 +00:00
|
|
|
static int __maybe_unused rk3288_gmac_set_parent(struct clk *clk, struct clk *parent)
|
2018-01-13 06:06:33 +00:00
|
|
|
{
|
|
|
|
struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
|
2020-01-09 08:52:17 +00:00
|
|
|
struct rockchip_cru *cru = priv->cru;
|
2018-01-13 06:06:33 +00:00
|
|
|
const char *clock_output_name;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the requested parent is in the same clock-controller and
|
|
|
|
* the id is SCLK_MAC_PLL ("mac_pll_src"), switch to the internal
|
|
|
|
* clock.
|
|
|
|
*/
|
|
|
|
if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC_PLL)) {
|
|
|
|
debug("%s: switching GAMC to SCLK_MAC_PLL\n", __func__);
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise, we need to check the clock-output-names of the
|
|
|
|
* requested parent to see if the requested id is "ext_gmac".
|
|
|
|
*/
|
|
|
|
ret = dev_read_string_index(parent->dev, "clock-output-names",
|
|
|
|
parent->id, &clock_output_name);
|
|
|
|
if (ret < 0)
|
|
|
|
return -ENODATA;
|
|
|
|
|
|
|
|
/* If this is "ext_gmac", switch to the external clock input */
|
|
|
|
if (!strcmp(clock_output_name, "ext_gmac")) {
|
|
|
|
debug("%s: switching GMAC to external clock\n", __func__);
|
|
|
|
rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK,
|
|
|
|
RMII_EXTCLK_SELECT_EXT_CLK << RMII_EXTCLK_SHIFT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:27:10 +00:00
|
|
|
static int __maybe_unused rk3288_clk_set_parent(struct clk *clk, struct clk *parent)
|
2018-01-13 06:06:33 +00:00
|
|
|
{
|
|
|
|
switch (clk->id) {
|
|
|
|
case SCLK_MAC:
|
|
|
|
return rk3288_gmac_set_parent(clk, parent);
|
|
|
|
case SCLK_USBPHY480M_SRC:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s: unsupported clk %ld\n", __func__, clk->id);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2015-08-30 22:55:31 +00:00
|
|
|
static struct clk_ops rk3288_clk_ops = {
|
|
|
|
.get_rate = rk3288_clk_get_rate,
|
|
|
|
.set_rate = rk3288_clk_set_rate,
|
2018-01-25 14:27:10 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
|
2018-01-13 06:06:33 +00:00
|
|
|
.set_parent = rk3288_clk_set_parent,
|
2018-01-25 14:27:10 +00:00
|
|
|
#endif
|
2015-08-30 22:55:31 +00:00
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int rk3288_clk_of_to_plat(struct udevice *dev)
|
2015-08-30 22:55:31 +00:00
|
|
|
{
|
2016-07-04 17:58:29 +00:00
|
|
|
#if !CONFIG_IS_ENABLED(OF_PLATDATA)
|
2015-08-30 22:55:31 +00:00
|
|
|
struct rk3288_clk_priv *priv = dev_get_priv(dev);
|
|
|
|
|
2018-02-11 03:53:08 +00:00
|
|
|
priv->cru = dev_read_addr_ptr(dev);
|
2016-07-04 17:58:29 +00:00
|
|
|
#endif
|
2016-07-04 17:58:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rk3288_clk_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct rk3288_clk_priv *priv = dev_get_priv(dev);
|
2017-05-31 23:57:32 +00:00
|
|
|
bool init_clocks = false;
|
2016-07-04 17:58:28 +00:00
|
|
|
|
2015-08-30 22:55:31 +00:00
|
|
|
priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
|
2016-07-04 17:58:28 +00:00
|
|
|
if (IS_ERR(priv->grf))
|
|
|
|
return PTR_ERR(priv->grf);
|
2015-08-30 22:55:31 +00:00
|
|
|
#ifdef CONFIG_SPL_BUILD
|
2016-07-04 17:58:29 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_PLATDATA)
|
2020-12-03 23:55:20 +00:00
|
|
|
struct rk3288_clk_plat *plat = dev_get_plat(dev);
|
2016-07-04 17:58:29 +00:00
|
|
|
|
|
|
|
priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
|
|
|
|
#endif
|
2017-05-31 23:57:32 +00:00
|
|
|
init_clocks = true;
|
2015-08-30 22:55:31 +00:00
|
|
|
#endif
|
2017-05-31 23:57:32 +00:00
|
|
|
if (!(gd->flags & GD_FLG_RELOC)) {
|
|
|
|
u32 reg;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init clocks in U-Boot proper if the NPLL is runnning. This
|
|
|
|
* indicates that a previous boot loader set up the clocks, so
|
|
|
|
* we need to redo it. U-Boot's SPL does not set this clock.
|
|
|
|
*/
|
|
|
|
reg = readl(&priv->cru->cru_mode_con);
|
|
|
|
if (((reg & NPLL_MODE_MASK) >> NPLL_MODE_SHIFT) ==
|
|
|
|
NPLL_MODE_NORMAL)
|
|
|
|
init_clocks = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init_clocks)
|
|
|
|
rkclk_init(priv->cru, priv->grf);
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rk3288_clk_bind(struct udevice *dev)
|
|
|
|
{
|
2016-06-17 15:44:00 +00:00
|
|
|
int ret;
|
2017-11-03 07:16:13 +00:00
|
|
|
struct udevice *sys_child;
|
|
|
|
struct sysreset_reg *priv;
|
2015-08-30 22:55:31 +00:00
|
|
|
|
|
|
|
/* The reset driver does not have a device node, so bind it here */
|
2017-11-03 07:16:13 +00:00
|
|
|
ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
|
|
|
|
&sys_child);
|
|
|
|
if (ret) {
|
|
|
|
debug("Warning: No sysreset driver: ret=%d\n", ret);
|
|
|
|
} else {
|
|
|
|
priv = malloc(sizeof(struct sysreset_reg));
|
2020-01-09 08:52:17 +00:00
|
|
|
priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
|
2017-11-03 07:16:13 +00:00
|
|
|
cru_glb_srst_fst_value);
|
2020-01-09 08:52:17 +00:00
|
|
|
priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
|
2017-11-03 07:16:13 +00:00
|
|
|
cru_glb_srst_snd_value);
|
2020-12-23 02:30:28 +00:00
|
|
|
dev_set_priv(sys_child, priv);
|
2017-11-03 07:16:13 +00:00
|
|
|
}
|
2015-08-30 22:55:31 +00:00
|
|
|
|
2019-11-08 23:06:30 +00:00
|
|
|
#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
|
2020-01-09 08:52:17 +00:00
|
|
|
ret = offsetof(struct rockchip_cru, cru_softrst_con[0]);
|
2017-12-19 10:22:38 +00:00
|
|
|
ret = rockchip_reset_bind(dev, ret, 12);
|
|
|
|
if (ret)
|
|
|
|
debug("Warning: software reset driver bind faile\n");
|
|
|
|
#endif
|
|
|
|
|
2015-08-30 22:55:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct udevice_id rk3288_clk_ids[] = {
|
|
|
|
{ .compatible = "rockchip,rk3288-cru" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2016-07-04 17:58:29 +00:00
|
|
|
U_BOOT_DRIVER(rockchip_rk3288_cru) = {
|
|
|
|
.name = "rockchip_rk3288_cru",
|
2015-08-30 22:55:31 +00:00
|
|
|
.id = UCLASS_CLK,
|
|
|
|
.of_match = rk3288_clk_ids,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct rk3288_clk_priv),
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct rk3288_clk_plat),
|
2015-08-30 22:55:31 +00:00
|
|
|
.ops = &rk3288_clk_ops,
|
|
|
|
.bind = rk3288_clk_bind,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = rk3288_clk_of_to_plat,
|
2015-08-30 22:55:31 +00:00
|
|
|
.probe = rk3288_clk_probe,
|
|
|
|
};
|