1 // SPDX-License-Identifier: GPL-2.0
3 * Marvell Kirkwood SoC clocks
5 * Copyright (C) 2012 Marvell
7 * Gregory CLEMENT <gregory.clement@free-electrons.com>
8 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
9 * Andrew Lunn <andrew@lunn.ch>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/clk-provider.h>
18 #include <linux/of_address.h>
24 * Kirkwood PLL sample-at-reset configuration
25 * (6180 has different SAR layout than other Kirkwood SoCs)
27 * SAR0[4:3,22,1] : CPU frequency (6281,6292,6282)
38 * SAR0[19,10:9] : CPU to L2 Clock divider ratio (6281,6292,6282)
44 * SAR0[8:5] : CPU to DDR DRAM Clock divider ratio (6281,6292,6282)
53 * SAR0[4:2] : Kirkwood 6180 cpu/l2/ddr clock configuration (6180 only)
54 * 5 = [CPU = 600 MHz, L2 = (1/2) * CPU, DDR = 200 MHz = (1/3) * CPU]
55 * 6 = [CPU = 800 MHz, L2 = (1/2) * CPU, DDR = 200 MHz = (1/4) * CPU]
56 * 7 = [CPU = 1000 MHz, L2 = (1/2) * CPU, DDR = 200 MHz = (1/5) * CPU]
59 * SAR0[21] : TCLK frequency
65 #define SAR_KIRKWOOD_CPU_FREQ(x) \
66 (((x & (1 << 1)) >> 1) | \
67 ((x & (1 << 22)) >> 21) | \
68 ((x & (3 << 3)) >> 1))
69 #define SAR_KIRKWOOD_L2_RATIO(x) \
70 (((x & (3 << 9)) >> 9) | \
71 (((x & (1 << 19)) >> 17)))
72 #define SAR_KIRKWOOD_DDR_RATIO 5
73 #define SAR_KIRKWOOD_DDR_RATIO_MASK 0xf
74 #define SAR_MV88F6180_CLK 2
75 #define SAR_MV88F6180_CLK_MASK 0x7
76 #define SAR_KIRKWOOD_TCLK_FREQ 21
77 #define SAR_KIRKWOOD_TCLK_FREQ_MASK 0x1
79 enum { KIRKWOOD_CPU_TO_L2
, KIRKWOOD_CPU_TO_DDR
};
81 static const struct coreclk_ratio kirkwood_coreclk_ratios
[] __initconst
= {
82 { .id
= KIRKWOOD_CPU_TO_L2
, .name
= "l2clk", },
83 { .id
= KIRKWOOD_CPU_TO_DDR
, .name
= "ddrclk", }
86 static u32 __init
kirkwood_get_tclk_freq(void __iomem
*sar
)
88 u32 opt
= (readl(sar
) >> SAR_KIRKWOOD_TCLK_FREQ
) &
89 SAR_KIRKWOOD_TCLK_FREQ_MASK
;
90 return (opt
) ? 166666667 : 200000000;
93 static const u32 kirkwood_cpu_freqs
[] __initconst
= {
108 static u32 __init
kirkwood_get_cpu_freq(void __iomem
*sar
)
110 u32 opt
= SAR_KIRKWOOD_CPU_FREQ(readl(sar
));
111 return kirkwood_cpu_freqs
[opt
];
114 static const int kirkwood_cpu_l2_ratios
[8][2] __initconst
= {
115 { 0, 1 }, { 1, 2 }, { 0, 1 }, { 1, 3 },
116 { 0, 1 }, { 1, 4 }, { 0, 1 }, { 0, 1 }
119 static const int kirkwood_cpu_ddr_ratios
[16][2] __initconst
= {
120 { 0, 1 }, { 0, 1 }, { 1, 2 }, { 0, 1 },
121 { 1, 3 }, { 0, 1 }, { 1, 4 }, { 2, 9 },
122 { 1, 5 }, { 1, 6 }, { 0, 1 }, { 0, 1 },
123 { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }
126 static void __init
kirkwood_get_clk_ratio(
127 void __iomem
*sar
, int id
, int *mult
, int *div
)
130 case KIRKWOOD_CPU_TO_L2
:
132 u32 opt
= SAR_KIRKWOOD_L2_RATIO(readl(sar
));
133 *mult
= kirkwood_cpu_l2_ratios
[opt
][0];
134 *div
= kirkwood_cpu_l2_ratios
[opt
][1];
137 case KIRKWOOD_CPU_TO_DDR
:
139 u32 opt
= (readl(sar
) >> SAR_KIRKWOOD_DDR_RATIO
) &
140 SAR_KIRKWOOD_DDR_RATIO_MASK
;
141 *mult
= kirkwood_cpu_ddr_ratios
[opt
][0];
142 *div
= kirkwood_cpu_ddr_ratios
[opt
][1];
148 static const u32 mv88f6180_cpu_freqs
[] __initconst
= {
155 static u32 __init
mv88f6180_get_cpu_freq(void __iomem
*sar
)
157 u32 opt
= (readl(sar
) >> SAR_MV88F6180_CLK
) & SAR_MV88F6180_CLK_MASK
;
158 return mv88f6180_cpu_freqs
[opt
];
161 static const int mv88f6180_cpu_ddr_ratios
[8][2] __initconst
= {
162 { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
163 { 0, 1 }, { 1, 3 }, { 1, 4 }, { 1, 5 }
166 static void __init
mv88f6180_get_clk_ratio(
167 void __iomem
*sar
, int id
, int *mult
, int *div
)
170 case KIRKWOOD_CPU_TO_L2
:
172 /* mv88f6180 has a fixed 1:2 CPU-to-L2 ratio */
177 case KIRKWOOD_CPU_TO_DDR
:
179 u32 opt
= (readl(sar
) >> SAR_MV88F6180_CLK
) &
180 SAR_MV88F6180_CLK_MASK
;
181 *mult
= mv88f6180_cpu_ddr_ratios
[opt
][0];
182 *div
= mv88f6180_cpu_ddr_ratios
[opt
][1];
188 static u32 __init
mv98dx1135_get_tclk_freq(void __iomem
*sar
)
193 static const struct coreclk_soc_desc kirkwood_coreclks
= {
194 .get_tclk_freq
= kirkwood_get_tclk_freq
,
195 .get_cpu_freq
= kirkwood_get_cpu_freq
,
196 .get_clk_ratio
= kirkwood_get_clk_ratio
,
197 .ratios
= kirkwood_coreclk_ratios
,
198 .num_ratios
= ARRAY_SIZE(kirkwood_coreclk_ratios
),
201 static const struct coreclk_soc_desc mv88f6180_coreclks
= {
202 .get_tclk_freq
= kirkwood_get_tclk_freq
,
203 .get_cpu_freq
= mv88f6180_get_cpu_freq
,
204 .get_clk_ratio
= mv88f6180_get_clk_ratio
,
205 .ratios
= kirkwood_coreclk_ratios
,
206 .num_ratios
= ARRAY_SIZE(kirkwood_coreclk_ratios
),
209 static const struct coreclk_soc_desc mv98dx1135_coreclks
= {
210 .get_tclk_freq
= mv98dx1135_get_tclk_freq
,
211 .get_cpu_freq
= kirkwood_get_cpu_freq
,
212 .get_clk_ratio
= kirkwood_get_clk_ratio
,
213 .ratios
= kirkwood_coreclk_ratios
,
214 .num_ratios
= ARRAY_SIZE(kirkwood_coreclk_ratios
),
218 * Clock Gating Control
221 static const struct clk_gating_soc_desc kirkwood_gating_desc
[] __initconst
= {
222 { "ge0", NULL
, 0, 0 },
223 { "pex0", NULL
, 2, 0 },
224 { "usb0", NULL
, 3, 0 },
225 { "sdio", NULL
, 4, 0 },
226 { "tsu", NULL
, 5, 0 },
227 { "runit", NULL
, 7, 0 },
228 { "xor0", NULL
, 8, 0 },
229 { "audio", NULL
, 9, 0 },
230 { "sata0", NULL
, 14, 0 },
231 { "sata1", NULL
, 15, 0 },
232 { "xor1", NULL
, 16, 0 },
233 { "crypto", NULL
, 17, 0 },
234 { "pex1", NULL
, 18, 0 },
235 { "ge1", NULL
, 19, 0 },
236 { "tdm", NULL
, 20, 0 },
242 * Clock Muxing Control
245 struct clk_muxing_soc_desc
{
247 const char **parents
;
254 struct clk_muxing_ctrl
{
260 static const char *powersave_parents
[] = {
265 static const struct clk_muxing_soc_desc kirkwood_mux_desc
[] __initconst
= {
266 { "powersave", powersave_parents
, ARRAY_SIZE(powersave_parents
),
270 static struct clk
*clk_muxing_get_src(
271 struct of_phandle_args
*clkspec
, void *data
)
273 struct clk_muxing_ctrl
*ctrl
= (struct clk_muxing_ctrl
*)data
;
276 if (clkspec
->args_count
< 1)
277 return ERR_PTR(-EINVAL
);
279 for (n
= 0; n
< ctrl
->num_muxes
; n
++) {
280 struct clk_mux
*mux
=
281 to_clk_mux(__clk_get_hw(ctrl
->muxes
[n
]));
282 if (clkspec
->args
[0] == mux
->shift
)
283 return ctrl
->muxes
[n
];
285 return ERR_PTR(-ENODEV
);
288 static void __init
kirkwood_clk_muxing_setup(struct device_node
*np
,
289 const struct clk_muxing_soc_desc
*desc
)
291 struct clk_muxing_ctrl
*ctrl
;
295 base
= of_iomap(np
, 0);
299 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
303 /* lock must already be initialized */
304 ctrl
->lock
= &ctrl_gating_lock
;
306 /* Count, allocate, and register clock muxes */
307 for (n
= 0; desc
[n
].name
;)
311 ctrl
->muxes
= kcalloc(ctrl
->num_muxes
, sizeof(struct clk
*),
313 if (WARN_ON(!ctrl
->muxes
))
316 for (n
= 0; n
< ctrl
->num_muxes
; n
++) {
317 ctrl
->muxes
[n
] = clk_register_mux(NULL
, desc
[n
].name
,
318 desc
[n
].parents
, desc
[n
].num_parents
,
319 desc
[n
].flags
, base
, desc
[n
].shift
,
320 desc
[n
].width
, desc
[n
].flags
, ctrl
->lock
);
321 WARN_ON(IS_ERR(ctrl
->muxes
[n
]));
324 of_clk_add_provider(np
, clk_muxing_get_src
, ctrl
);
333 static void __init
kirkwood_clk_init(struct device_node
*np
)
335 struct device_node
*cgnp
=
336 of_find_compatible_node(NULL
, NULL
, "marvell,kirkwood-gating-clock");
339 if (of_device_is_compatible(np
, "marvell,mv88f6180-core-clock"))
340 mvebu_coreclk_setup(np
, &mv88f6180_coreclks
);
341 else if (of_device_is_compatible(np
, "marvell,mv98dx1135-core-clock"))
342 mvebu_coreclk_setup(np
, &mv98dx1135_coreclks
);
344 mvebu_coreclk_setup(np
, &kirkwood_coreclks
);
347 mvebu_clk_gating_setup(cgnp
, kirkwood_gating_desc
);
348 kirkwood_clk_muxing_setup(cgnp
, kirkwood_mux_desc
);
353 CLK_OF_DECLARE(kirkwood_clk
, "marvell,kirkwood-core-clock",
355 CLK_OF_DECLARE(mv88f6180_clk
, "marvell,mv88f6180-core-clock",
357 CLK_OF_DECLARE(98dx1135_clk
, "marvell,mv98dx1135-core-clock",