2019-06-24 13:50:45 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 DENX Software Engineering
|
|
|
|
* Lukasz Majewski, DENX Software Engineering, lukma@denx.de
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/io.h>
|
2020-01-10 14:46:59 +00:00
|
|
|
#include <div64.h>
|
2019-06-24 13:50:45 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <clk-uclass.h>
|
|
|
|
#include <dm/device.h>
|
|
|
|
#include <dm/uclass.h>
|
|
|
|
#include <clk.h>
|
|
|
|
#include "clk.h"
|
|
|
|
|
2020-01-10 14:46:53 +00:00
|
|
|
#define UBOOT_DM_CLK_IMX_PLLV3_GENERIC "imx_clk_pllv3_generic"
|
2020-01-10 14:46:58 +00:00
|
|
|
#define UBOOT_DM_CLK_IMX_PLLV3_SYS "imx_clk_pllv3_sys"
|
2020-01-10 14:46:53 +00:00
|
|
|
#define UBOOT_DM_CLK_IMX_PLLV3_USB "imx_clk_pllv3_usb"
|
2020-01-10 14:46:59 +00:00
|
|
|
#define UBOOT_DM_CLK_IMX_PLLV3_AV "imx_clk_pllv3_av"
|
|
|
|
|
|
|
|
#define PLL_NUM_OFFSET 0x10
|
|
|
|
#define PLL_DENOM_OFFSET 0x20
|
2019-06-24 13:50:45 +00:00
|
|
|
|
2020-01-10 14:46:55 +00:00
|
|
|
#define BM_PLL_POWER (0x1 << 12)
|
2020-01-10 14:46:57 +00:00
|
|
|
#define BM_PLL_LOCK (0x1 << 31)
|
2020-01-10 14:46:55 +00:00
|
|
|
|
2019-06-24 13:50:45 +00:00
|
|
|
struct clk_pllv3 {
|
|
|
|
struct clk clk;
|
|
|
|
void __iomem *base;
|
2020-01-10 14:46:55 +00:00
|
|
|
u32 power_bit;
|
|
|
|
bool powerup_set;
|
2019-06-24 13:50:45 +00:00
|
|
|
u32 div_mask;
|
|
|
|
u32 div_shift;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define to_clk_pllv3(_clk) container_of(_clk, struct clk_pllv3, clk)
|
|
|
|
|
2020-01-10 14:46:53 +00:00
|
|
|
static ulong clk_pllv3_generic_get_rate(struct clk *clk)
|
2019-06-24 13:50:45 +00:00
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(dev_get_clk_ptr(clk->dev));
|
|
|
|
unsigned long parent_rate = clk_get_parent_rate(clk);
|
|
|
|
|
|
|
|
u32 div = (readl(pll->base) >> pll->div_shift) & pll->div_mask;
|
|
|
|
|
|
|
|
return (div == 1) ? parent_rate * 22 : parent_rate * 20;
|
|
|
|
}
|
|
|
|
|
2020-01-10 14:46:57 +00:00
|
|
|
static ulong clk_pllv3_generic_set_rate(struct clk *clk, ulong rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(clk);
|
|
|
|
unsigned long parent_rate = clk_get_parent_rate(clk);
|
|
|
|
u32 val, div;
|
|
|
|
|
|
|
|
if (rate == parent_rate * 22)
|
|
|
|
div = 1;
|
|
|
|
else if (rate == parent_rate * 20)
|
|
|
|
div = 0;
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
val = readl(pll->base);
|
|
|
|
val &= ~(pll->div_mask << pll->div_shift);
|
|
|
|
val |= (div << pll->div_shift);
|
|
|
|
writel(val, pll->base);
|
|
|
|
|
|
|
|
/* Wait for PLL to lock */
|
|
|
|
while (!(readl(pll->base) & BM_PLL_LOCK))
|
|
|
|
;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-10 14:46:55 +00:00
|
|
|
static int clk_pllv3_generic_enable(struct clk *clk)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(clk);
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl(pll->base);
|
|
|
|
if (pll->powerup_set)
|
|
|
|
val |= pll->power_bit;
|
|
|
|
else
|
|
|
|
val &= ~pll->power_bit;
|
|
|
|
writel(val, pll->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-10 14:46:56 +00:00
|
|
|
static int clk_pllv3_generic_disable(struct clk *clk)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(clk);
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl(pll->base);
|
|
|
|
if (pll->powerup_set)
|
|
|
|
val &= ~pll->power_bit;
|
|
|
|
else
|
|
|
|
val |= pll->power_bit;
|
|
|
|
writel(val, pll->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-24 13:50:45 +00:00
|
|
|
static const struct clk_ops clk_pllv3_generic_ops = {
|
2020-01-10 14:46:53 +00:00
|
|
|
.get_rate = clk_pllv3_generic_get_rate,
|
2020-01-10 14:46:55 +00:00
|
|
|
.enable = clk_pllv3_generic_enable,
|
2020-01-10 14:46:56 +00:00
|
|
|
.disable = clk_pllv3_generic_disable,
|
2020-01-10 14:46:57 +00:00
|
|
|
.set_rate = clk_pllv3_generic_set_rate,
|
2019-06-24 13:50:45 +00:00
|
|
|
};
|
|
|
|
|
2020-01-10 14:46:58 +00:00
|
|
|
static ulong clk_pllv3_sys_get_rate(struct clk *clk)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(clk);
|
|
|
|
unsigned long parent_rate = clk_get_parent_rate(clk);
|
|
|
|
u32 div = readl(pll->base) & pll->div_mask;
|
|
|
|
|
|
|
|
return parent_rate * div / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ulong clk_pllv3_sys_set_rate(struct clk *clk, ulong rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(clk);
|
|
|
|
unsigned long parent_rate = clk_get_parent_rate(clk);
|
2020-01-17 12:06:40 +00:00
|
|
|
unsigned long min_rate;
|
|
|
|
unsigned long max_rate;
|
2020-01-10 14:46:58 +00:00
|
|
|
u32 val, div;
|
|
|
|
|
2020-01-17 12:06:40 +00:00
|
|
|
if (parent_rate == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
min_rate = parent_rate * 54 / 2;
|
|
|
|
max_rate = parent_rate * 108 / 2;
|
|
|
|
|
2020-01-10 14:46:58 +00:00
|
|
|
if (rate < min_rate || rate > max_rate)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
div = rate * 2 / parent_rate;
|
|
|
|
val = readl(pll->base);
|
|
|
|
val &= ~pll->div_mask;
|
|
|
|
val |= div;
|
|
|
|
writel(val, pll->base);
|
|
|
|
|
|
|
|
/* Wait for PLL to lock */
|
|
|
|
while (!(readl(pll->base) & BM_PLL_LOCK))
|
|
|
|
;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops clk_pllv3_sys_ops = {
|
|
|
|
.enable = clk_pllv3_generic_enable,
|
|
|
|
.disable = clk_pllv3_generic_disable,
|
|
|
|
.get_rate = clk_pllv3_sys_get_rate,
|
|
|
|
.set_rate = clk_pllv3_sys_set_rate,
|
|
|
|
};
|
|
|
|
|
2020-01-10 14:46:59 +00:00
|
|
|
static ulong clk_pllv3_av_get_rate(struct clk *clk)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(clk);
|
|
|
|
unsigned long parent_rate = clk_get_parent_rate(clk);
|
|
|
|
u32 mfn = readl(pll->base + PLL_NUM_OFFSET);
|
|
|
|
u32 mfd = readl(pll->base + PLL_DENOM_OFFSET);
|
|
|
|
u32 div = readl(pll->base) & pll->div_mask;
|
|
|
|
u64 temp64 = (u64)parent_rate;
|
|
|
|
|
2020-01-17 12:06:41 +00:00
|
|
|
if (mfd == 0)
|
|
|
|
return -EIO;
|
|
|
|
|
2020-01-10 14:46:59 +00:00
|
|
|
temp64 *= mfn;
|
|
|
|
do_div(temp64, mfd);
|
|
|
|
|
|
|
|
return parent_rate * div + (unsigned long)temp64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ulong clk_pllv3_av_set_rate(struct clk *clk, ulong rate)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll = to_clk_pllv3(clk);
|
|
|
|
unsigned long parent_rate = clk_get_parent_rate(clk);
|
2020-01-17 12:06:42 +00:00
|
|
|
unsigned long min_rate;
|
|
|
|
unsigned long max_rate;
|
2020-01-10 14:46:59 +00:00
|
|
|
u32 val, div;
|
|
|
|
u32 mfn, mfd = 1000000;
|
|
|
|
u32 max_mfd = 0x3FFFFFFF;
|
|
|
|
u64 temp64;
|
|
|
|
|
2020-01-17 12:06:42 +00:00
|
|
|
if (parent_rate == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
min_rate = parent_rate * 27;
|
|
|
|
max_rate = parent_rate * 54;
|
|
|
|
|
2020-01-10 14:46:59 +00:00
|
|
|
if (rate < min_rate || rate > max_rate)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (parent_rate <= max_mfd)
|
|
|
|
mfd = parent_rate;
|
|
|
|
|
|
|
|
div = rate / parent_rate;
|
|
|
|
temp64 = (u64)(rate - div * parent_rate);
|
|
|
|
temp64 *= mfd;
|
|
|
|
do_div(temp64, parent_rate);
|
|
|
|
mfn = temp64;
|
|
|
|
|
|
|
|
val = readl(pll->base);
|
|
|
|
val &= ~pll->div_mask;
|
|
|
|
val |= div;
|
|
|
|
writel(val, pll->base);
|
|
|
|
writel(mfn, pll->base + PLL_NUM_OFFSET);
|
|
|
|
writel(mfd, pll->base + PLL_DENOM_OFFSET);
|
|
|
|
|
|
|
|
/* Wait for PLL to lock */
|
|
|
|
while (!(readl(pll->base) & BM_PLL_LOCK))
|
|
|
|
;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops clk_pllv3_av_ops = {
|
|
|
|
.enable = clk_pllv3_generic_enable,
|
|
|
|
.disable = clk_pllv3_generic_disable,
|
|
|
|
.get_rate = clk_pllv3_av_get_rate,
|
|
|
|
.set_rate = clk_pllv3_av_set_rate,
|
|
|
|
};
|
|
|
|
|
2019-06-24 13:50:45 +00:00
|
|
|
struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
|
|
|
|
const char *parent_name, void __iomem *base,
|
|
|
|
u32 div_mask)
|
|
|
|
{
|
|
|
|
struct clk_pllv3 *pll;
|
|
|
|
struct clk *clk;
|
|
|
|
char *drv_name;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pll = kzalloc(sizeof(*pll), GFP_KERNEL);
|
|
|
|
if (!pll)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2020-01-10 14:46:55 +00:00
|
|
|
pll->power_bit = BM_PLL_POWER;
|
|
|
|
|
2019-06-24 13:50:45 +00:00
|
|
|
switch (type) {
|
|
|
|
case IMX_PLLV3_GENERIC:
|
2020-01-10 14:46:53 +00:00
|
|
|
drv_name = UBOOT_DM_CLK_IMX_PLLV3_GENERIC;
|
2020-01-10 14:46:54 +00:00
|
|
|
pll->div_shift = 0;
|
2020-01-10 14:46:55 +00:00
|
|
|
pll->powerup_set = false;
|
2020-01-10 14:46:53 +00:00
|
|
|
break;
|
2020-01-10 14:46:58 +00:00
|
|
|
case IMX_PLLV3_SYS:
|
|
|
|
drv_name = UBOOT_DM_CLK_IMX_PLLV3_SYS;
|
|
|
|
pll->div_shift = 0;
|
|
|
|
pll->powerup_set = false;
|
|
|
|
break;
|
2019-06-24 13:50:45 +00:00
|
|
|
case IMX_PLLV3_USB:
|
2020-01-10 14:46:53 +00:00
|
|
|
drv_name = UBOOT_DM_CLK_IMX_PLLV3_USB;
|
2020-01-10 14:46:54 +00:00
|
|
|
pll->div_shift = 1;
|
2020-01-10 14:46:55 +00:00
|
|
|
pll->powerup_set = true;
|
2019-06-24 13:50:45 +00:00
|
|
|
break;
|
2020-01-10 14:46:59 +00:00
|
|
|
case IMX_PLLV3_AV:
|
|
|
|
drv_name = UBOOT_DM_CLK_IMX_PLLV3_AV;
|
|
|
|
pll->div_shift = 0;
|
|
|
|
pll->powerup_set = false;
|
|
|
|
break;
|
2019-06-24 13:50:45 +00:00
|
|
|
default:
|
|
|
|
kfree(pll);
|
|
|
|
return ERR_PTR(-ENOTSUPP);
|
|
|
|
}
|
|
|
|
|
|
|
|
pll->base = base;
|
|
|
|
pll->div_mask = div_mask;
|
|
|
|
clk = &pll->clk;
|
|
|
|
|
|
|
|
ret = clk_register(clk, drv_name, name, parent_name);
|
|
|
|
if (ret) {
|
|
|
|
kfree(pll);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clk;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(clk_pllv3_generic) = {
|
2020-01-10 14:46:53 +00:00
|
|
|
.name = UBOOT_DM_CLK_IMX_PLLV3_GENERIC,
|
|
|
|
.id = UCLASS_CLK,
|
|
|
|
.ops = &clk_pllv3_generic_ops,
|
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
};
|
|
|
|
|
2020-01-10 14:46:58 +00:00
|
|
|
U_BOOT_DRIVER(clk_pllv3_sys) = {
|
|
|
|
.name = UBOOT_DM_CLK_IMX_PLLV3_SYS,
|
|
|
|
.id = UCLASS_CLK,
|
|
|
|
.ops = &clk_pllv3_sys_ops,
|
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
};
|
|
|
|
|
2020-01-10 14:46:53 +00:00
|
|
|
U_BOOT_DRIVER(clk_pllv3_usb) = {
|
|
|
|
.name = UBOOT_DM_CLK_IMX_PLLV3_USB,
|
2019-06-24 13:50:45 +00:00
|
|
|
.id = UCLASS_CLK,
|
|
|
|
.ops = &clk_pllv3_generic_ops,
|
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
};
|
2020-01-10 14:46:59 +00:00
|
|
|
|
|
|
|
U_BOOT_DRIVER(clk_pllv3_av) = {
|
|
|
|
.name = UBOOT_DM_CLK_IMX_PLLV3_AV,
|
|
|
|
.id = UCLASS_CLK,
|
|
|
|
.ops = &clk_pllv3_av_ops,
|
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
};
|