clk-tango4.c 2.62 KB
Newer Older
1 2 3 4 5 6
#include <linux/kernel.h>
#include <linux/clk-provider.h>
#include <linux/of_address.h>
#include <linux/init.h>
#include <linux/io.h>

7 8 9
#define CLK_COUNT 4 /* cpu_clk, sys_clk, usb_clk, sdio_clk */
static struct clk *clks[CLK_COUNT];
static struct clk_onecell_data clk_data = { clks, CLK_COUNT };
10

11 12 13
#define SYSCLK_DIV	0x20
#define CPUCLK_DIV	0x24
#define DIV_BYPASS	BIT(23)
14

15 16 17 18 19
/*** CLKGEN_PLL ***/
#define extract_pll_n(val)	((val >>  0) & ((1u << 7) - 1))
#define extract_pll_k(val)	((val >> 13) & ((1u << 3) - 1))
#define extract_pll_m(val)	((val >> 16) & ((1u << 3) - 1))
#define extract_pll_isel(val)	((val >> 24) & ((1u << 3) - 1))
20 21 22 23 24 25 26

static void __init make_pll(int idx, const char *parent, void __iomem *base)
{
	char name[8];
	u32 val, mul, div;

	sprintf(name, "pll%d", idx);
27 28 29
	val = readl(base + idx * 8);
	mul =  extract_pll_n(val) + 1;
	div = (extract_pll_m(val) + 1) << extract_pll_k(val);
30
	clk_register_fixed_factor(NULL, name, parent, 0, mul, div);
31 32
	if (extract_pll_isel(val) != 1)
		panic("%s: input not set to XTAL_IN\n", name);
33 34
}

35
static void __init make_cd(int idx, void __iomem *base)
36
{
37 38
	char name[8];
	u32 val, mul, div;
39

40 41 42 43 44 45 46
	sprintf(name, "cd%d", idx);
	val = readl(base + idx * 8);
	mul =  1 << 27;
	div = (2 << 27) + val;
	clk_register_fixed_factor(NULL, name, "pll2", 0, mul, div);
	if (val > 0xf0000000)
		panic("%s: unsupported divider %x\n", name, val);
47 48 49 50
}

static void __init tango4_clkgen_setup(struct device_node *np)
{
51
	struct clk **pp = clk_data.clks;
52 53 54 55
	void __iomem *base = of_iomap(np, 0);
	const char *parent = of_clk_get_parent_name(np, 0);

	if (!base)
56 57 58 59 60 61 62 63 64
		panic("%s: invalid address\n", np->name);

	if (readl(base + CPUCLK_DIV) & DIV_BYPASS)
		panic("%s: unsupported cpuclk setup\n", np->name);

	if (readl(base + SYSCLK_DIV) & DIV_BYPASS)
		panic("%s: unsupported sysclk setup\n", np->name);

	writel(0x100, base + CPUCLK_DIV); /* disable frequency ramping */
65 66 67

	make_pll(0, parent, base);
	make_pll(1, parent, base);
68 69 70
	make_pll(2, parent, base);
	make_cd(2, base + 0x80);
	make_cd(6, base + 0x80);
71

72 73 74 75 76
	pp[0] = clk_register_divider(NULL, "cpu_clk", "pll0", 0,
			base + CPUCLK_DIV, 8, 8, CLK_DIVIDER_ONE_BASED, NULL);
	pp[1] = clk_register_fixed_factor(NULL, "sys_clk", "pll1", 0, 1, 4);
	pp[2] = clk_register_fixed_factor(NULL,  "usb_clk", "cd2", 0, 1, 2);
	pp[3] = clk_register_fixed_factor(NULL, "sdio_clk", "cd6", 0, 1, 2);
77

78 79
	if (IS_ERR(pp[0]) || IS_ERR(pp[1]) || IS_ERR(pp[2]) || IS_ERR(pp[3]))
		panic("%s: clk registration failed\n", np->name);
80

81 82
	if (of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data))
		panic("%s: clk provider registration failed\n", np->name);
83 84
}
CLK_OF_DECLARE(tango4_clkgen, "sigma,tango4-clkgen", tango4_clkgen_setup);