Commit 626c8a6d authored by Peng Fan's avatar Peng Fan Committed by Ye Li
Browse files

imx: mx7ulp: Add clock framework and functions



Add a clock framework to support SCG1/PCC2/PCC3 for A7 to support get/set
clock source, divider, clock rate and parent source.
Users need to include pcc.h to use the APIs to for peripherals clock. Each
peripheral clock is defined in enum pcc_clk type.

SCG relevants APIs are defined in scg.h which supports clock rate get, PLL/PFD
enablement and settings, and all SCG clock initialization. User need use enum
scg_clk to access each clock source.

In clock.c, we initialize necessary clocks at u-boot s_init and implement the
clock functions used by driver modules to operate clocks dynamically.
Signed-off-by: default avatarPeng Fan <peng.fan@nxp.com>
Signed-off-by: default avatarYe Li <ye.li@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
parent e605114f
......@@ -5,4 +5,4 @@
#
#
obj-y := iomux.o
obj-y := clock.o iomux.o pcc.o scg.o
/*
* Copyright (C) 2016 Freescale Semiconductor, Inc.
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <div64.h>
#include <asm/io.h>
#include <errno.h>
#include <asm/arch/clock.h>
#include <asm/arch/sys_proto.h>
DECLARE_GLOBAL_DATA_PTR;
int get_clocks(void)
{
#ifdef CONFIG_FSL_ESDHC
#if CONFIG_SYS_FSL_ESDHC_ADDR == USDHC0_RBASE
gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK);
#elif CONFIG_SYS_FSL_ESDHC_ADDR == USDHC1_RBASE
gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
#endif
#endif
return 0;
}
static u32 get_fast_plat_clk(void)
{
return scg_clk_get_rate(SCG_NIC0_CLK);
}
static u32 get_slow_plat_clk(void)
{
return scg_clk_get_rate(SCG_NIC1_CLK);
}
static u32 get_ipg_clk(void)
{
return scg_clk_get_rate(SCG_NIC1_BUS_CLK);
}
u32 get_lpuart_clk(void)
{
int index = 0;
const u32 lpuart_array[] = {
LPUART0_RBASE,
LPUART1_RBASE,
LPUART2_RBASE,
LPUART3_RBASE,
LPUART4_RBASE,
LPUART5_RBASE,
LPUART6_RBASE,
LPUART7_RBASE,
};
const enum pcc_clk lpuart_pcc_clks[] = {
PER_CLK_LPUART4,
PER_CLK_LPUART5,
PER_CLK_LPUART6,
PER_CLK_LPUART7,
};
for (index = 0; index < 8; index++) {
if (lpuart_array[index] == LPUART_BASE)
break;
}
if (index < 4 || index > 7)
return 0;
return pcc_clock_get_rate(lpuart_pcc_clks[index - 4]);
}
unsigned int mxc_get_clock(enum mxc_clock clk)
{
switch (clk) {
case MXC_ARM_CLK:
return scg_clk_get_rate(SCG_CORE_CLK);
case MXC_AXI_CLK:
return get_fast_plat_clk();
case MXC_AHB_CLK:
return get_slow_plat_clk();
case MXC_IPG_CLK:
return get_ipg_clk();
case MXC_I2C_CLK:
return pcc_clock_get_rate(PER_CLK_LPI2C4);
case MXC_UART_CLK:
return get_lpuart_clk();
case MXC_ESDHC_CLK:
return pcc_clock_get_rate(PER_CLK_USDHC0);
case MXC_ESDHC2_CLK:
return pcc_clock_get_rate(PER_CLK_USDHC1);
case MXC_DDR_CLK:
return scg_clk_get_rate(SCG_DDR_CLK);
default:
printf("Unsupported mxc_clock %d\n", clk);
break;
}
return 0;
}
void init_clk_usdhc(u32 index)
{
switch (index) {
case 0:
/*Disable the clock before configure it */
pcc_clock_enable(PER_CLK_USDHC0, false);
/* 158MHz / 1 = 158MHz */
pcc_clock_sel(PER_CLK_USDHC0, SCG_NIC1_CLK);
pcc_clock_div_config(PER_CLK_USDHC0, false, 1);
pcc_clock_enable(PER_CLK_USDHC0, true);
break;
case 1:
/*Disable the clock before configure it */
pcc_clock_enable(PER_CLK_USDHC1, false);
/* 158MHz / 1 = 158MHz */
pcc_clock_sel(PER_CLK_USDHC1, SCG_NIC1_CLK);
pcc_clock_div_config(PER_CLK_USDHC1, false, 1);
pcc_clock_enable(PER_CLK_USDHC1, true);
break;
default:
printf("Invalid index for USDHC %d\n", index);
break;
}
}
#ifdef CONFIG_MXC_OCOTP
#define OCOTP_CTRL_PCC1_SLOT (38)
#define OCOTP_CTRL_HIGH4K_PCC1_SLOT (39)
void enable_ocotp_clk(unsigned char enable)
{
u32 val;
/*
* Seems the OCOTP CLOCKs have been enabled at default,
* check its inuse flag
*/
val = readl(PCC1_RBASE + 4 * OCOTP_CTRL_PCC1_SLOT);
if (!(val & PCC_INUSE_MASK))
writel(PCC_CGC_MASK, (PCC1_RBASE + 4 * OCOTP_CTRL_PCC1_SLOT));
val = readl(PCC1_RBASE + 4 * OCOTP_CTRL_HIGH4K_PCC1_SLOT);
if (!(val & PCC_INUSE_MASK))
writel(PCC_CGC_MASK,
(PCC1_RBASE + 4 * OCOTP_CTRL_HIGH4K_PCC1_SLOT));
}
#endif
void enable_usboh3_clk(unsigned char enable)
{
if (enable) {
pcc_clock_enable(PER_CLK_USB0, false);
pcc_clock_sel(PER_CLK_USB0, SCG_NIC1_BUS_CLK);
pcc_clock_enable(PER_CLK_USB0, true);
#ifdef CONFIG_USB_MAX_CONTROLLER_COUNT
if (CONFIG_USB_MAX_CONTROLLER_COUNT > 1) {
pcc_clock_enable(PER_CLK_USB1, false);
pcc_clock_sel(PER_CLK_USB1, SCG_NIC1_BUS_CLK);
pcc_clock_enable(PER_CLK_USB1, true);
}
#endif
pcc_clock_enable(PER_CLK_USB_PHY, true);
pcc_clock_enable(PER_CLK_USB_PL301, true);
} else {
pcc_clock_enable(PER_CLK_USB0, false);
pcc_clock_enable(PER_CLK_USB1, false);
pcc_clock_enable(PER_CLK_USB_PHY, false);
pcc_clock_enable(PER_CLK_USB_PL301, false);
}
}
static void lpuart_set_clk(uint32_t index, enum scg_clk clk)
{
const enum pcc_clk lpuart_pcc_clks[] = {
PER_CLK_LPUART4,
PER_CLK_LPUART5,
PER_CLK_LPUART6,
PER_CLK_LPUART7,
};
if (index < 4 || index > 7)
return;
#ifndef CONFIG_CLK_DEBUG
pcc_clock_enable(lpuart_pcc_clks[index - 4], false);
#endif
pcc_clock_sel(lpuart_pcc_clks[index - 4], clk);
pcc_clock_enable(lpuart_pcc_clks[index - 4], true);
}
static void init_clk_lpuart(void)
{
u32 index = 0, i;
const u32 lpuart_array[] = {
LPUART0_RBASE,
LPUART1_RBASE,
LPUART2_RBASE,
LPUART3_RBASE,
LPUART4_RBASE,
LPUART5_RBASE,
LPUART6_RBASE,
LPUART7_RBASE,
};
for (i = 0; i < 8; i++) {
if (lpuart_array[i] == LPUART_BASE) {
index = i;
break;
}
}
lpuart_set_clk(index, SCG_SOSC_DIV2_CLK);
}
static void init_clk_rgpio2p(void)
{
/*Enable RGPIO2P1 clock */
pcc_clock_enable(PER_CLK_RGPIO2P1, true);
/*
* Hard code to enable RGPIO2P0 clock since it is not
* in clock frame for A7 domain
*/
writel(PCC_CGC_MASK, (PCC0_RBASE + 0x3C));
}
/* Configure PLL/PFD freq */
void clock_init(void)
{
/*
* ROM has enabled clocks:
* A4 side: SIRC 16Mhz (DIV1-3 off), FIRC 48Mhz (DIV1-2 on),
* Non-LP-boot: SOSC, SPLL PFD0 (scs selected)
* A7 side: SPLL PFD0 (scs selected, 413Mhz),
* APLL PFD0 (352Mhz), DDRCLK, all NIC clocks
* A7 Plat0 (NIC0) = 176Mhz, Plat1 (NIC1) = 176Mhz,
* IP BUS (NIC1_BUS) = 58.6Mhz
*
* In u-boot:
* 1. Enable PFD1-3 of APLL for A7 side. Enable FIRC and DIVs.
* 2. Enable USB PLL
* 3. Init the clocks of peripherals used in u-boot bu
* without set rate interface.The clocks for these
* peripherals are enabled in this intialization.
* 4.Other peripherals with set clock rate interface
* does not be set in this function.
*/
scg_a7_firc_init();
scg_a7_soscdiv_init();
/* APLL PFD1 = 270Mhz, PFD2=480Mhz, PFD3=800Mhz */
scg_enable_pll_pfd(SCG_APLL_PFD1_CLK, 35);
scg_enable_pll_pfd(SCG_APLL_PFD2_CLK, 20);
scg_enable_pll_pfd(SCG_APLL_PFD3_CLK, 12);
init_clk_lpuart();
init_clk_rgpio2p();
enable_usboh3_clk(1);
}
/*
* Dump some core clockes.
*/
int do_mx7_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
u32 addr = 0;
u32 freq;
freq = decode_pll(PLL_A7_SPLL);
printf("PLL_A7_SPLL %8d MHz\n", freq / 1000000);
freq = decode_pll(PLL_A7_APLL);
printf("PLL_A7_APLL %8d MHz\n", freq / 1000000);
freq = decode_pll(PLL_USB);
printf("PLL_USB %8d MHz\n", freq / 1000000);
printf("\n");
printf("CORE %8d kHz\n", scg_clk_get_rate(SCG_CORE_CLK) / 1000);
printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
printf("I2C4 %8d kHz\n", mxc_get_clock(MXC_I2C_CLK) / 1000);
addr = (u32) clock_init;
printf("[%s] addr = 0x%08X\r\n", __func__, addr);
scg_a7_info();
return 0;
}
U_BOOT_CMD(
clocks, CONFIG_SYS_MAXARGS, 1, do_mx7_showclocks,
"display clocks",
""
);
/*
* Copyright (C) 2016 Freescale Semiconductor, Inc.
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <div64.h>
#include <asm/io.h>
#include <errno.h>
#include <asm/arch/imx-regs.h>
#include <asm/arch/pcc.h>
#include <asm/arch/sys_proto.h>
DECLARE_GLOBAL_DATA_PTR;
#define PCC_CLKSRC_TYPES 2
#define PCC_CLKSRC_NUM 7
static enum scg_clk pcc_clksrc[PCC_CLKSRC_TYPES][PCC_CLKSRC_NUM] = {
{ SCG_NIC1_BUS_CLK,
SCG_NIC1_CLK,
SCG_DDR_CLK,
SCG_APLL_PFD2_CLK,
SCG_APLL_PFD1_CLK,
SCG_APLL_PFD0_CLK,
USB_PLL_OUT,
},
{ SCG_SOSC_DIV2_CLK, /* SOSC BUS clock */
MIPI_PLL_OUT,
SCG_FIRC_DIV2_CLK, /* FIRC BUS clock */
SCG_ROSC_CLK,
SCG_NIC1_BUS_CLK,
SCG_NIC1_CLK,
SCG_APLL_PFD3_CLK,
},
};
static struct pcc_entry pcc_arrays[] = {
{PCC2_RBASE, DMA1_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, RGPIO1_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, FLEXBUS0_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, SEMA42_1_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, DMA1_CH_MUX0_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, SNVS_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, CAAM_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, LPTPM4_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, LPTPM5_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, LPIT1_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, LPSPI2_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, LPSPI3_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, LPI2C4_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, LPI2C5_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, LPUART4_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, LPUART5_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, FLEXIO1_PCC2_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC2_RBASE, USBOTG0_PCC2_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV},
{PCC2_RBASE, USBOTG1_PCC2_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV},
{PCC2_RBASE, USBPHY_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, USB_PL301_PCC2_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC2_RBASE, USDHC0_PCC2_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV},
{PCC2_RBASE, USDHC1_PCC2_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV},
{PCC2_RBASE, WDG1_PCC2_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV},
{PCC2_RBASE, WDG2_PCC2_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV},
{PCC3_RBASE, LPTPM6_PCC3_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC3_RBASE, LPTPM7_PCC3_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC3_RBASE, LPI2C6_PCC3_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC3_RBASE, LPI2C7_PCC3_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC3_RBASE, LPUART6_PCC3_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC3_RBASE, LPUART7_PCC3_SLOT, CLKSRC_PER_BUS, PCC_NO_DIV},
{PCC3_RBASE, VIU0_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC3_RBASE, DSI0_PCC3_SLOT, CLKSRC_PER_BUS, PCC_HAS_DIV},
{PCC3_RBASE, LCDIF0_PCC3_SLOT, CLKSRC_PER_PLAT, PCC_HAS_DIV},
{PCC3_RBASE, MMDC0_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC3_RBASE, PORTC_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC3_RBASE, PORTD_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC3_RBASE, PORTE_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC3_RBASE, PORTF_PCC3_SLOT, CLKSRC_NO_PCS, PCC_NO_DIV},
{PCC3_RBASE, GPU3D_PCC3_SLOT, CLKSRC_PER_PLAT, PCC_NO_DIV},
{PCC3_RBASE, GPU2D_PCC3_SLOT, CLKSRC_PER_PLAT, PCC_NO_DIV},
};
int pcc_clock_enable(enum pcc_clk clk, bool enable)
{
u32 reg, val;
if (clk >= ARRAY_SIZE(pcc_arrays))
return -EINVAL;
reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
val = readl(reg);
clk_debug("pcc_clock_enable: clk %d, reg 0x%x, val 0x%x, enable %d\n",
clk, reg, val, enable);
if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK))
return -EPERM;
if (enable)
val |= PCC_CGC_MASK;
else
val &= ~PCC_CGC_MASK;
writel(val, reg);
clk_debug("pcc_clock_enable: val 0x%x\n", val);
return 0;
}
/* The clock source select needs clock is disabled */
int pcc_clock_sel(enum pcc_clk clk, enum scg_clk src)
{
u32 reg, val, i, clksrc_type;
if (clk >= ARRAY_SIZE(pcc_arrays))
return -EINVAL;
clksrc_type = pcc_arrays[clk].clksrc;
if (clksrc_type >= CLKSRC_NO_PCS) {
printf("No PCS field for the PCC %d, clksrc type %d\n",
clk, clksrc_type);
return -EPERM;
}
for (i = 0; i < PCC_CLKSRC_NUM; i++) {
if (pcc_clksrc[clksrc_type][i] == src) {
/* Find the clock src, then set it to PCS */
break;
}
}
if (i == PCC_CLKSRC_NUM) {
printf("Not find the parent scg_clk in PCS of PCC %d, invalid scg_clk %d\n", clk, src);
return -EINVAL;
}
reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
val = readl(reg);
clk_debug("pcc_clock_sel: clk %d, reg 0x%x, val 0x%x, clksrc_type %d\n",
clk, reg, val, clksrc_type);
if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK) ||
(val & PCC_CGC_MASK)) {
printf("Not permit to select clock source val = 0x%x\n", val);
return -EPERM;
}
val &= ~PCC_PCS_MASK;
val |= ((i + 1) << PCC_PCS_OFFSET);
writel(val, reg);
clk_debug("pcc_clock_sel: val 0x%x\n", val);
return 0;
}
int pcc_clock_div_config(enum pcc_clk clk, bool frac, u8 div)
{
u32 reg, val;
if (clk >= ARRAY_SIZE(pcc_arrays) || div > 8 ||
(div == 1 && frac != 0))
return -EINVAL;
if (pcc_arrays[clk].div >= PCC_NO_DIV) {
printf("No DIV/FRAC field for the PCC %d\n", clk);
return -EPERM;
}
reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
val = readl(reg);
if (!(val & PCC_PR_MASK) || (val & PCC_INUSE_MASK) ||
(val & PCC_CGC_MASK)) {
printf("Not permit to set div/frac val = 0x%x\n", val);
return -EPERM;
}
if (frac)
val |= PCC_FRAC_MASK;
else
val &= ~PCC_FRAC_MASK;
val &= ~PCC_PCD_MASK;
val |= (div - 1) & PCC_PCD_MASK;
writel(val, reg);
return 0;
}
bool pcc_clock_is_enable(enum pcc_clk clk)
{
u32 reg, val;
if (clk >= ARRAY_SIZE(pcc_arrays))
return -EINVAL;
reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
val = readl(reg);
if ((val & PCC_INUSE_MASK) || (val & PCC_CGC_MASK))
return true;
return false;
}
int pcc_clock_get_clksrc(enum pcc_clk clk, enum scg_clk *src)
{
u32 reg, val, clksrc_type;
if (clk >= ARRAY_SIZE(pcc_arrays))
return -EINVAL;
clksrc_type = pcc_arrays[clk].clksrc;
if (clksrc_type >= CLKSRC_NO_PCS) {
printf("No PCS field for the PCC %d, clksrc type %d\n",
clk, clksrc_type);
return -EPERM;
}
reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
val = readl(reg);
clk_debug("pcc_clock_get_clksrc: clk %d, reg 0x%x, val 0x%x, type %d\n",
clk, reg, val, clksrc_type);
if (!(val & PCC_PR_MASK)) {
printf("This pcc slot is not present = 0x%x\n", val);
return -EPERM;
}
val &= PCC_PCS_MASK;
val = (val >> PCC_PCS_OFFSET);
if (!val) {
printf("Clock source is off\n");
return -EIO;
}
*src = pcc_clksrc[clksrc_type][val - 1];
clk_debug("pcc_clock_get_clksrc: parent scg clk %d\n", *src);
return 0;
}
u32 pcc_clock_get_rate(enum pcc_clk clk)
{
u32 reg, val, rate, frac, div;
enum scg_clk parent;
int ret;
ret = pcc_clock_get_clksrc(clk, &parent);
if (ret)
return 0;
rate = scg_clk_get_rate(parent);
clk_debug("pcc_clock_get_rate: parent rate %u\n", rate);
if (pcc_arrays[clk].div == PCC_HAS_DIV) {
reg = pcc_arrays[clk].pcc_base + pcc_arrays[clk].pcc_slot * 4;
val = readl(reg);
frac = (val & PCC_FRAC_MASK) >> PCC_FRAC_OFFSET;
div = (val & PCC_PCD_MASK) >> PCC_PCD_OFFSET;
/*
* Theoretically don't have overflow in the calc,
* the rate won't exceed 2G
*/
rate = rate * (frac + 1) / (div + 1);
}
clk_debug("pcc_clock_get_rate: rate %u\n", rate);
return rate;
}
This diff is collapsed.
/*
* Copyright (C) 2016 Freescale Semiconductor, Inc.
*