blob: eef75e305a595f6f4e453c6d5f0e950cc419bd56 [file] [log] [blame]
Marc Gonzalezed12dfc2015-10-30 13:25:28 +01001#include <linux/kernel.h>
2#include <linux/clk-provider.h>
3#include <linux/of_address.h>
4#include <linux/init.h>
5#include <linux/io.h>
6
Marc Gonzalezfefe0532016-04-04 11:21:09 +02007#define CLK_COUNT 4 /* cpu_clk, sys_clk, usb_clk, sdio_clk */
8static struct clk *clks[CLK_COUNT];
9static struct clk_onecell_data clk_data = { clks, CLK_COUNT };
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010010
Marc Gonzalezfefe0532016-04-04 11:21:09 +020011#define SYSCLK_DIV 0x20
12#define CPUCLK_DIV 0x24
13#define DIV_BYPASS BIT(23)
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010014
Marc Gonzalezfefe0532016-04-04 11:21:09 +020015/*** CLKGEN_PLL ***/
16#define extract_pll_n(val) ((val >> 0) & ((1u << 7) - 1))
17#define extract_pll_k(val) ((val >> 13) & ((1u << 3) - 1))
18#define extract_pll_m(val) ((val >> 16) & ((1u << 3) - 1))
19#define extract_pll_isel(val) ((val >> 24) & ((1u << 3) - 1))
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010020
21static void __init make_pll(int idx, const char *parent, void __iomem *base)
22{
23 char name[8];
24 u32 val, mul, div;
25
26 sprintf(name, "pll%d", idx);
Marc Gonzalezfefe0532016-04-04 11:21:09 +020027 val = readl(base + idx * 8);
28 mul = extract_pll_n(val) + 1;
29 div = (extract_pll_m(val) + 1) << extract_pll_k(val);
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010030 clk_register_fixed_factor(NULL, name, parent, 0, mul, div);
Marc Gonzalezfefe0532016-04-04 11:21:09 +020031 if (extract_pll_isel(val) != 1)
32 panic("%s: input not set to XTAL_IN\n", name);
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010033}
34
Marc Gonzalezfefe0532016-04-04 11:21:09 +020035static void __init make_cd(int idx, void __iomem *base)
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010036{
Marc Gonzalezfefe0532016-04-04 11:21:09 +020037 char name[8];
38 u32 val, mul, div;
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010039
Marc Gonzalezfefe0532016-04-04 11:21:09 +020040 sprintf(name, "cd%d", idx);
41 val = readl(base + idx * 8);
42 mul = 1 << 27;
43 div = (2 << 27) + val;
44 clk_register_fixed_factor(NULL, name, "pll2", 0, mul, div);
45 if (val > 0xf0000000)
46 panic("%s: unsupported divider %x\n", name, val);
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010047}
48
49static void __init tango4_clkgen_setup(struct device_node *np)
50{
Marc Gonzalezfefe0532016-04-04 11:21:09 +020051 struct clk **pp = clk_data.clks;
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010052 void __iomem *base = of_iomap(np, 0);
53 const char *parent = of_clk_get_parent_name(np, 0);
54
55 if (!base)
Marc Gonzalezfefe0532016-04-04 11:21:09 +020056 panic("%s: invalid address\n", np->name);
57
58 if (readl(base + CPUCLK_DIV) & DIV_BYPASS)
59 panic("%s: unsupported cpuclk setup\n", np->name);
60
61 if (readl(base + SYSCLK_DIV) & DIV_BYPASS)
62 panic("%s: unsupported sysclk setup\n", np->name);
63
64 writel(0x100, base + CPUCLK_DIV); /* disable frequency ramping */
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010065
66 make_pll(0, parent, base);
67 make_pll(1, parent, base);
Marc Gonzalezfefe0532016-04-04 11:21:09 +020068 make_pll(2, parent, base);
69 make_cd(2, base + 0x80);
70 make_cd(6, base + 0x80);
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010071
Marc Gonzalezfefe0532016-04-04 11:21:09 +020072 pp[0] = clk_register_divider(NULL, "cpu_clk", "pll0", 0,
73 base + CPUCLK_DIV, 8, 8, CLK_DIVIDER_ONE_BASED, NULL);
74 pp[1] = clk_register_fixed_factor(NULL, "sys_clk", "pll1", 0, 1, 4);
75 pp[2] = clk_register_fixed_factor(NULL, "usb_clk", "cd2", 0, 1, 2);
76 pp[3] = clk_register_fixed_factor(NULL, "sdio_clk", "cd6", 0, 1, 2);
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010077
Marc Gonzalezfefe0532016-04-04 11:21:09 +020078 if (IS_ERR(pp[0]) || IS_ERR(pp[1]) || IS_ERR(pp[2]) || IS_ERR(pp[3]))
79 panic("%s: clk registration failed\n", np->name);
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010080
Marc Gonzalezfefe0532016-04-04 11:21:09 +020081 if (of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data))
82 panic("%s: clk provider registration failed\n", np->name);
Marc Gonzalezed12dfc2015-10-30 13:25:28 +010083}
84CLK_OF_DECLARE(tango4_clkgen, "sigma,tango4-clkgen", tango4_clkgen_setup);