xref: /openbmc/linux/drivers/clk/pxa/clk-pxa.c (revision fd13f8117f7a2d4054bf420ec1428e918a24a480)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2bda00303SRobert Jarzmik /*
3bda00303SRobert Jarzmik  * Marvell PXA family clocks
4bda00303SRobert Jarzmik  *
5bda00303SRobert Jarzmik  * Copyright (C) 2014 Robert Jarzmik
6bda00303SRobert Jarzmik  *
7bda00303SRobert Jarzmik  * Common clock code for PXA clocks ("CKEN" type clocks + DT)
8bda00303SRobert Jarzmik  */
9bda00303SRobert Jarzmik #include <linux/clk.h>
10bda00303SRobert Jarzmik #include <linux/clk-provider.h>
11bda00303SRobert Jarzmik #include <linux/clkdev.h>
1262e59c4eSStephen Boyd #include <linux/io.h>
13bda00303SRobert Jarzmik #include <linux/of.h>
14*fd13f811SArnd Bergmann #include <linux/soc/pxa/smemc.h>
15bda00303SRobert Jarzmik 
16bda00303SRobert Jarzmik #include <dt-bindings/clock/pxa-clock.h>
17bda00303SRobert Jarzmik #include "clk-pxa.h"
18bda00303SRobert Jarzmik 
199fe69429SRobert Jarzmik #define KHz 1000
209fe69429SRobert Jarzmik #define MHz (1000 * 1000)
219fe69429SRobert Jarzmik 
229fe69429SRobert Jarzmik #define MDREFR_K0DB4	(1 << 29)	/* SDCLK0 Divide by 4 Control/Status */
239fe69429SRobert Jarzmik #define MDREFR_K2FREE	(1 << 25)	/* SDRAM Free-Running Control */
249fe69429SRobert Jarzmik #define MDREFR_K1FREE	(1 << 24)	/* SDRAM Free-Running Control */
259fe69429SRobert Jarzmik #define MDREFR_K0FREE	(1 << 23)	/* SDRAM Free-Running Control */
269fe69429SRobert Jarzmik #define MDREFR_SLFRSH	(1 << 22)	/* SDRAM Self-Refresh Control/Status */
279fe69429SRobert Jarzmik #define MDREFR_APD	(1 << 20)	/* SDRAM/SSRAM Auto-Power-Down Enable */
289fe69429SRobert Jarzmik #define MDREFR_K2DB2	(1 << 19)	/* SDCLK2 Divide by 2 Control/Status */
299fe69429SRobert Jarzmik #define MDREFR_K2RUN	(1 << 18)	/* SDCLK2 Run Control/Status */
309fe69429SRobert Jarzmik #define MDREFR_K1DB2	(1 << 17)	/* SDCLK1 Divide by 2 Control/Status */
319fe69429SRobert Jarzmik #define MDREFR_K1RUN	(1 << 16)	/* SDCLK1 Run Control/Status */
329fe69429SRobert Jarzmik #define MDREFR_E1PIN	(1 << 15)	/* SDCKE1 Level Control/Status */
339fe69429SRobert Jarzmik #define MDREFR_K0DB2	(1 << 14)	/* SDCLK0 Divide by 2 Control/Status */
349fe69429SRobert Jarzmik #define MDREFR_K0RUN	(1 << 13)	/* SDCLK0 Run Control/Status */
359fe69429SRobert Jarzmik #define MDREFR_E0PIN	(1 << 12)	/* SDCKE0 Level Control/Status */
369fe69429SRobert Jarzmik #define MDREFR_DB2_MASK	(MDREFR_K2DB2 | MDREFR_K1DB2)
379fe69429SRobert Jarzmik #define MDREFR_DRI_MASK	0xFFF
389fe69429SRobert Jarzmik 
3984558ff7SStephen Boyd static DEFINE_SPINLOCK(pxa_clk_lock);
40bda00303SRobert Jarzmik 
41bda00303SRobert Jarzmik static struct clk *pxa_clocks[CLK_MAX];
42bda00303SRobert Jarzmik static struct clk_onecell_data onecell_data = {
43bda00303SRobert Jarzmik 	.clks = pxa_clocks,
44bda00303SRobert Jarzmik 	.clk_num = CLK_MAX,
45bda00303SRobert Jarzmik };
46bda00303SRobert Jarzmik 
4714dd5b01SRobert Jarzmik struct pxa_clk {
4814dd5b01SRobert Jarzmik 	struct clk_hw hw;
4914dd5b01SRobert Jarzmik 	struct clk_fixed_factor lp;
5014dd5b01SRobert Jarzmik 	struct clk_fixed_factor hp;
5114dd5b01SRobert Jarzmik 	struct clk_gate gate;
5214dd5b01SRobert Jarzmik 	bool (*is_in_low_power)(void);
5314dd5b01SRobert Jarzmik };
5414dd5b01SRobert Jarzmik 
5514dd5b01SRobert Jarzmik #define to_pxa_clk(_hw) container_of(_hw, struct pxa_clk, hw)
56bda00303SRobert Jarzmik 
57bda00303SRobert Jarzmik static unsigned long cken_recalc_rate(struct clk_hw *hw,
58bda00303SRobert Jarzmik 				      unsigned long parent_rate)
59bda00303SRobert Jarzmik {
6014dd5b01SRobert Jarzmik 	struct pxa_clk *pclk = to_pxa_clk(hw);
61bda00303SRobert Jarzmik 	struct clk_fixed_factor *fix;
62bda00303SRobert Jarzmik 
63bda00303SRobert Jarzmik 	if (!pclk->is_in_low_power || pclk->is_in_low_power())
64bda00303SRobert Jarzmik 		fix = &pclk->lp;
65bda00303SRobert Jarzmik 	else
66bda00303SRobert Jarzmik 		fix = &pclk->hp;
674e907ef6SJavier Martinez Canillas 	__clk_hw_set_clk(&fix->hw, hw);
68bda00303SRobert Jarzmik 	return clk_fixed_factor_ops.recalc_rate(&fix->hw, parent_rate);
69bda00303SRobert Jarzmik }
70bda00303SRobert Jarzmik 
715fc6eb7dSJulia Lawall static const struct clk_ops cken_rate_ops = {
72bda00303SRobert Jarzmik 	.recalc_rate = cken_recalc_rate,
73bda00303SRobert Jarzmik };
74bda00303SRobert Jarzmik 
75bda00303SRobert Jarzmik static u8 cken_get_parent(struct clk_hw *hw)
76bda00303SRobert Jarzmik {
7714dd5b01SRobert Jarzmik 	struct pxa_clk *pclk = to_pxa_clk(hw);
78bda00303SRobert Jarzmik 
79bda00303SRobert Jarzmik 	if (!pclk->is_in_low_power)
80bda00303SRobert Jarzmik 		return 0;
81bda00303SRobert Jarzmik 	return pclk->is_in_low_power() ? 0 : 1;
82bda00303SRobert Jarzmik }
83bda00303SRobert Jarzmik 
845fc6eb7dSJulia Lawall static const struct clk_ops cken_mux_ops = {
85bda00303SRobert Jarzmik 	.get_parent = cken_get_parent,
86bda00303SRobert Jarzmik 	.set_parent = dummy_clk_set_parent,
87bda00303SRobert Jarzmik };
88bda00303SRobert Jarzmik 
89bda00303SRobert Jarzmik void __init clkdev_pxa_register(int ckid, const char *con_id,
90bda00303SRobert Jarzmik 				const char *dev_id, struct clk *clk)
91bda00303SRobert Jarzmik {
92bda00303SRobert Jarzmik 	if (!IS_ERR(clk) && (ckid != CLK_NONE))
93bda00303SRobert Jarzmik 		pxa_clocks[ckid] = clk;
94bda00303SRobert Jarzmik 	if (!IS_ERR(clk))
95bda00303SRobert Jarzmik 		clk_register_clkdev(clk, con_id, dev_id);
96bda00303SRobert Jarzmik }
97bda00303SRobert Jarzmik 
9814dd5b01SRobert Jarzmik int __init clk_pxa_cken_init(const struct desc_clk_cken *clks, int nb_clks)
99bda00303SRobert Jarzmik {
100bda00303SRobert Jarzmik 	int i;
10114dd5b01SRobert Jarzmik 	struct pxa_clk *pxa_clk;
102bda00303SRobert Jarzmik 	struct clk *clk;
103bda00303SRobert Jarzmik 
104bda00303SRobert Jarzmik 	for (i = 0; i < nb_clks; i++) {
10514dd5b01SRobert Jarzmik 		pxa_clk = kzalloc(sizeof(*pxa_clk), GFP_KERNEL);
10614dd5b01SRobert Jarzmik 		pxa_clk->is_in_low_power = clks[i].is_in_low_power;
10714dd5b01SRobert Jarzmik 		pxa_clk->lp = clks[i].lp;
10814dd5b01SRobert Jarzmik 		pxa_clk->hp = clks[i].hp;
10914dd5b01SRobert Jarzmik 		pxa_clk->gate = clks[i].gate;
11084558ff7SStephen Boyd 		pxa_clk->gate.lock = &pxa_clk_lock;
11114dd5b01SRobert Jarzmik 		clk = clk_register_composite(NULL, clks[i].name,
11214dd5b01SRobert Jarzmik 					     clks[i].parent_names, 2,
11314dd5b01SRobert Jarzmik 					     &pxa_clk->hw, &cken_mux_ops,
11414dd5b01SRobert Jarzmik 					     &pxa_clk->hw, &cken_rate_ops,
11514dd5b01SRobert Jarzmik 					     &pxa_clk->gate.hw, &clk_gate_ops,
11614dd5b01SRobert Jarzmik 					     clks[i].flags);
11714dd5b01SRobert Jarzmik 		clkdev_pxa_register(clks[i].ckid, clks[i].con_id,
11814dd5b01SRobert Jarzmik 				    clks[i].dev_id, clk);
119bda00303SRobert Jarzmik 	}
120bda00303SRobert Jarzmik 	return 0;
121bda00303SRobert Jarzmik }
122bda00303SRobert Jarzmik 
1236f8a444aSRobert Jarzmik void __init clk_pxa_dt_common_init(struct device_node *np)
124bda00303SRobert Jarzmik {
125bda00303SRobert Jarzmik 	of_clk_add_provider(np, of_clk_src_onecell_get, &onecell_data);
126bda00303SRobert Jarzmik }
1279fe69429SRobert Jarzmik 
1289fe69429SRobert Jarzmik void pxa2xx_core_turbo_switch(bool on)
1299fe69429SRobert Jarzmik {
1309fe69429SRobert Jarzmik 	unsigned long flags;
1319fe69429SRobert Jarzmik 	unsigned int unused, clkcfg;
1329fe69429SRobert Jarzmik 
1339fe69429SRobert Jarzmik 	local_irq_save(flags);
1349fe69429SRobert Jarzmik 
1359fe69429SRobert Jarzmik 	asm("mrc p14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
1369fe69429SRobert Jarzmik 	clkcfg &= ~CLKCFG_TURBO & ~CLKCFG_HALFTURBO;
1379fe69429SRobert Jarzmik 	if (on)
1389fe69429SRobert Jarzmik 		clkcfg |= CLKCFG_TURBO;
1399fe69429SRobert Jarzmik 	clkcfg |= CLKCFG_FCS;
1409fe69429SRobert Jarzmik 
1419fe69429SRobert Jarzmik 	asm volatile(
1429fe69429SRobert Jarzmik 	"	b	2f\n"
1439fe69429SRobert Jarzmik 	"	.align	5\n"
1449fe69429SRobert Jarzmik 	"1:	mcr	p14, 0, %1, c6, c0, 0\n"
1459fe69429SRobert Jarzmik 	"	b	3f\n"
1469fe69429SRobert Jarzmik 	"2:	b	1b\n"
1479fe69429SRobert Jarzmik 	"3:	nop\n"
148c82a2cb8SArnd Bergmann 		: "=&r" (unused) : "r" (clkcfg));
1499fe69429SRobert Jarzmik 
1509fe69429SRobert Jarzmik 	local_irq_restore(flags);
1519fe69429SRobert Jarzmik }
1529fe69429SRobert Jarzmik 
1539fe69429SRobert Jarzmik void pxa2xx_cpll_change(struct pxa2xx_freq *freq,
154*fd13f811SArnd Bergmann 			u32 (*mdrefr_dri)(unsigned int),
15584558ff7SStephen Boyd 			void __iomem *cccr)
1569fe69429SRobert Jarzmik {
1579fe69429SRobert Jarzmik 	unsigned int clkcfg = freq->clkcfg;
1589fe69429SRobert Jarzmik 	unsigned int unused, preset_mdrefr, postset_mdrefr;
1599fe69429SRobert Jarzmik 	unsigned long flags;
160*fd13f811SArnd Bergmann 	void __iomem *mdrefr = pxa_smemc_get_mdrefr();
1619fe69429SRobert Jarzmik 
1629fe69429SRobert Jarzmik 	local_irq_save(flags);
1639fe69429SRobert Jarzmik 
1649fe69429SRobert Jarzmik 	/* Calculate the next MDREFR.  If we're slowing down the SDRAM clock
1659fe69429SRobert Jarzmik 	 * we need to preset the smaller DRI before the change.	 If we're
1669fe69429SRobert Jarzmik 	 * speeding up we need to set the larger DRI value after the change.
1679fe69429SRobert Jarzmik 	 */
1689fe69429SRobert Jarzmik 	preset_mdrefr = postset_mdrefr = readl(mdrefr);
1699fe69429SRobert Jarzmik 	if ((preset_mdrefr & MDREFR_DRI_MASK) > mdrefr_dri(freq->membus_khz)) {
1709fe69429SRobert Jarzmik 		preset_mdrefr = (preset_mdrefr & ~MDREFR_DRI_MASK);
1719fe69429SRobert Jarzmik 		preset_mdrefr |= mdrefr_dri(freq->membus_khz);
1729fe69429SRobert Jarzmik 	}
1739fe69429SRobert Jarzmik 	postset_mdrefr =
1749fe69429SRobert Jarzmik 		(postset_mdrefr & ~MDREFR_DRI_MASK) |
1759fe69429SRobert Jarzmik 		mdrefr_dri(freq->membus_khz);
1769fe69429SRobert Jarzmik 
1779fe69429SRobert Jarzmik 	/* If we're dividing the memory clock by two for the SDRAM clock, this
1789fe69429SRobert Jarzmik 	 * must be set prior to the change.  Clearing the divide must be done
1799fe69429SRobert Jarzmik 	 * after the change.
1809fe69429SRobert Jarzmik 	 */
1819fe69429SRobert Jarzmik 	if (freq->div2) {
1829fe69429SRobert Jarzmik 		preset_mdrefr  |= MDREFR_DB2_MASK;
1839fe69429SRobert Jarzmik 		postset_mdrefr |= MDREFR_DB2_MASK;
1849fe69429SRobert Jarzmik 	} else {
1859fe69429SRobert Jarzmik 		postset_mdrefr &= ~MDREFR_DB2_MASK;
1869fe69429SRobert Jarzmik 	}
1879fe69429SRobert Jarzmik 
1889fe69429SRobert Jarzmik 	/* Set new the CCCR and prepare CLKCFG */
1899fe69429SRobert Jarzmik 	writel(freq->cccr, cccr);
1909fe69429SRobert Jarzmik 
1919fe69429SRobert Jarzmik 	asm volatile(
1929fe69429SRobert Jarzmik 	"	ldr	r4, [%1]\n"
1939fe69429SRobert Jarzmik 	"	b	2f\n"
1949fe69429SRobert Jarzmik 	"	.align	5\n"
1959fe69429SRobert Jarzmik 	"1:	str	%3, [%1]		/* preset the MDREFR */\n"
1969fe69429SRobert Jarzmik 	"	mcr	p14, 0, %2, c6, c0, 0	/* set CLKCFG[FCS] */\n"
1979fe69429SRobert Jarzmik 	"	str	%4, [%1]		/* postset the MDREFR */\n"
1989fe69429SRobert Jarzmik 	"	b	3f\n"
1999fe69429SRobert Jarzmik 	"2:	b	1b\n"
2009fe69429SRobert Jarzmik 	"3:	nop\n"
2019fe69429SRobert Jarzmik 	     : "=&r" (unused)
2029fe69429SRobert Jarzmik 	     : "r" (mdrefr), "r" (clkcfg), "r" (preset_mdrefr),
2039fe69429SRobert Jarzmik 	       "r" (postset_mdrefr)
2049fe69429SRobert Jarzmik 	     : "r4", "r5");
2059fe69429SRobert Jarzmik 
2069fe69429SRobert Jarzmik 	local_irq_restore(flags);
2079fe69429SRobert Jarzmik }
2089fe69429SRobert Jarzmik 
2099fe69429SRobert Jarzmik int pxa2xx_determine_rate(struct clk_rate_request *req,
2109fe69429SRobert Jarzmik 			  struct pxa2xx_freq *freqs, int nb_freqs)
2119fe69429SRobert Jarzmik {
2122517b32bSArnd Bergmann 	int i, closest_below = -1, closest_above = -1;
2139fe69429SRobert Jarzmik 	unsigned long rate;
2149fe69429SRobert Jarzmik 
2159fe69429SRobert Jarzmik 	for (i = 0; i < nb_freqs; i++) {
2169fe69429SRobert Jarzmik 		rate = freqs[i].cpll;
2179fe69429SRobert Jarzmik 		if (rate == req->rate)
2189fe69429SRobert Jarzmik 			break;
2199fe69429SRobert Jarzmik 		if (rate < req->min_rate)
2209fe69429SRobert Jarzmik 			continue;
2219fe69429SRobert Jarzmik 		if (rate > req->max_rate)
2229fe69429SRobert Jarzmik 			continue;
2239fe69429SRobert Jarzmik 		if (rate <= req->rate)
2249fe69429SRobert Jarzmik 			closest_below = i;
2259fe69429SRobert Jarzmik 		if ((rate >= req->rate) && (closest_above == -1))
2269fe69429SRobert Jarzmik 			closest_above = i;
2279fe69429SRobert Jarzmik 	}
2289fe69429SRobert Jarzmik 
2299fe69429SRobert Jarzmik 	req->best_parent_hw = NULL;
2309fe69429SRobert Jarzmik 
2312517b32bSArnd Bergmann 	if (i < nb_freqs) {
2322517b32bSArnd Bergmann 		rate = req->rate;
2332517b32bSArnd Bergmann 	} else if (closest_below >= 0) {
2349fe69429SRobert Jarzmik 		rate = freqs[closest_below].cpll;
2352517b32bSArnd Bergmann 	} else if (closest_above >= 0) {
2369fe69429SRobert Jarzmik 		rate = freqs[closest_above].cpll;
2372517b32bSArnd Bergmann 	} else {
2382517b32bSArnd Bergmann 		pr_debug("%s(rate=%lu) no match\n", __func__, req->rate);
2392517b32bSArnd Bergmann 		return -EINVAL;
2402517b32bSArnd Bergmann 	}
2419fe69429SRobert Jarzmik 
2422517b32bSArnd Bergmann 	pr_debug("%s(rate=%lu) rate=%lu\n", __func__, req->rate, rate);
2439fe69429SRobert Jarzmik 	req->rate = rate;
2449fe69429SRobert Jarzmik 
2452517b32bSArnd Bergmann 	return 0;
2469fe69429SRobert Jarzmik }
247