xref: /openbmc/linux/drivers/clk/clk-apple-nco.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
16641057dSMartin Povišer // SPDX-License-Identifier: GPL-2.0-only OR MIT
26641057dSMartin Povišer /*
36641057dSMartin Povišer  * Driver for an SoC block (Numerically Controlled Oscillator)
46641057dSMartin Povišer  * found on t8103 (M1) and other Apple chips
56641057dSMartin Povišer  *
66641057dSMartin Povišer  * Copyright (C) The Asahi Linux Contributors
76641057dSMartin Povišer  */
86641057dSMartin Povišer 
96641057dSMartin Povišer #include <linux/bits.h>
106641057dSMartin Povišer #include <linux/bitfield.h>
116641057dSMartin Povišer #include <linux/clk-provider.h>
126641057dSMartin Povišer #include <linux/io.h>
136641057dSMartin Povišer #include <linux/kernel.h>
146641057dSMartin Povišer #include <linux/math64.h>
156641057dSMartin Povišer #include <linux/module.h>
166641057dSMartin Povišer #include <linux/of.h>
176641057dSMartin Povišer #include <linux/platform_device.h>
186641057dSMartin Povišer #include <linux/spinlock.h>
196641057dSMartin Povišer 
206641057dSMartin Povišer #define NCO_CHANNEL_STRIDE	0x4000
216641057dSMartin Povišer #define NCO_CHANNEL_REGSIZE	20
226641057dSMartin Povišer 
236641057dSMartin Povišer #define REG_CTRL	0
246641057dSMartin Povišer #define CTRL_ENABLE	BIT(31)
256641057dSMartin Povišer #define REG_DIV		4
266641057dSMartin Povišer #define DIV_FINE	GENMASK(1, 0)
276641057dSMartin Povišer #define DIV_COARSE	GENMASK(12, 2)
286641057dSMartin Povišer #define REG_INC1	8
296641057dSMartin Povišer #define REG_INC2	12
306641057dSMartin Povišer #define REG_ACCINIT	16
316641057dSMartin Povišer 
326641057dSMartin Povišer /*
336641057dSMartin Povišer  * Theory of operation (postulated)
346641057dSMartin Povišer  *
356641057dSMartin Povišer  * The REG_DIV register indirectly expresses a base integer divisor, roughly
366641057dSMartin Povišer  * corresponding to twice the desired ratio of input to output clock. This
376641057dSMartin Povišer  * base divisor is adjusted on a cycle-by-cycle basis based on the state of a
386641057dSMartin Povišer  * 32-bit phase accumulator to achieve a desired precise clock ratio over the
396641057dSMartin Povišer  * long term.
406641057dSMartin Povišer  *
416641057dSMartin Povišer  * Specifically an output clock cycle is produced after (REG_DIV divisor)/2
426641057dSMartin Povišer  * or (REG_DIV divisor + 1)/2 input cycles, the latter taking effect when top
436641057dSMartin Povišer  * bit of the 32-bit accumulator is set. The accumulator is incremented each
446641057dSMartin Povišer  * produced output cycle, by the value from either REG_INC1 or REG_INC2, which
456641057dSMartin Povišer  * of the two is selected depending again on the accumulator's current top bit.
466641057dSMartin Povišer  *
476641057dSMartin Povišer  * Because the NCO hardware implements counting of input clock cycles in part
486641057dSMartin Povišer  * in a Galois linear-feedback shift register, the higher bits of divisor
496641057dSMartin Povišer  * are programmed into REG_DIV by picking an appropriate LFSR state. See
506641057dSMartin Povišer  * applnco_compute_tables/applnco_div_translate for details on this.
516641057dSMartin Povišer  */
526641057dSMartin Povišer 
536641057dSMartin Povišer #define LFSR_POLY	0xa01
546641057dSMartin Povišer #define LFSR_INIT	0x7ff
556641057dSMartin Povišer #define LFSR_LEN	11
566641057dSMartin Povišer #define LFSR_PERIOD	((1 << LFSR_LEN) - 1)
576641057dSMartin Povišer #define LFSR_TBLSIZE	(1 << LFSR_LEN)
586641057dSMartin Povišer 
596641057dSMartin Povišer /* The minimal attainable coarse divisor (first value in table) */
606641057dSMartin Povišer #define COARSE_DIV_OFFSET 2
616641057dSMartin Povišer 
626641057dSMartin Povišer struct applnco_tables {
636641057dSMartin Povišer 	u16 fwd[LFSR_TBLSIZE];
646641057dSMartin Povišer 	u16 inv[LFSR_TBLSIZE];
656641057dSMartin Povišer };
666641057dSMartin Povišer 
676641057dSMartin Povišer struct applnco_channel {
686641057dSMartin Povišer 	void __iomem *base;
696641057dSMartin Povišer 	struct applnco_tables *tbl;
706641057dSMartin Povišer 	struct clk_hw hw;
716641057dSMartin Povišer 
726641057dSMartin Povišer 	spinlock_t lock;
736641057dSMartin Povišer };
746641057dSMartin Povišer 
756641057dSMartin Povišer #define to_applnco_channel(_hw) container_of(_hw, struct applnco_channel, hw)
766641057dSMartin Povišer 
applnco_enable_nolock(struct clk_hw * hw)776641057dSMartin Povišer static void applnco_enable_nolock(struct clk_hw *hw)
786641057dSMartin Povišer {
796641057dSMartin Povišer 	struct applnco_channel *chan = to_applnco_channel(hw);
806641057dSMartin Povišer 	u32 val;
816641057dSMartin Povišer 
826641057dSMartin Povišer 	val = readl_relaxed(chan->base + REG_CTRL);
836641057dSMartin Povišer 	writel_relaxed(val | CTRL_ENABLE, chan->base + REG_CTRL);
846641057dSMartin Povišer }
856641057dSMartin Povišer 
applnco_disable_nolock(struct clk_hw * hw)866641057dSMartin Povišer static void applnco_disable_nolock(struct clk_hw *hw)
876641057dSMartin Povišer {
886641057dSMartin Povišer 	struct applnco_channel *chan = to_applnco_channel(hw);
896641057dSMartin Povišer 	u32 val;
906641057dSMartin Povišer 
916641057dSMartin Povišer 	val = readl_relaxed(chan->base + REG_CTRL);
926641057dSMartin Povišer 	writel_relaxed(val & ~CTRL_ENABLE, chan->base + REG_CTRL);
936641057dSMartin Povišer }
946641057dSMartin Povišer 
applnco_is_enabled(struct clk_hw * hw)956641057dSMartin Povišer static int applnco_is_enabled(struct clk_hw *hw)
966641057dSMartin Povišer {
976641057dSMartin Povišer 	struct applnco_channel *chan = to_applnco_channel(hw);
986641057dSMartin Povišer 
996641057dSMartin Povišer 	return (readl_relaxed(chan->base + REG_CTRL) & CTRL_ENABLE) != 0;
1006641057dSMartin Povišer }
1016641057dSMartin Povišer 
applnco_compute_tables(struct applnco_tables * tbl)1026641057dSMartin Povišer static void applnco_compute_tables(struct applnco_tables *tbl)
1036641057dSMartin Povišer {
1046641057dSMartin Povišer 	int i;
1056641057dSMartin Povišer 	u32 state = LFSR_INIT;
1066641057dSMartin Povišer 
1076641057dSMartin Povišer 	/*
1086641057dSMartin Povišer 	 * Go through the states of a Galois LFSR and build
1096641057dSMartin Povišer 	 * a coarse divisor translation table.
1106641057dSMartin Povišer 	 */
1116641057dSMartin Povišer 	for (i = LFSR_PERIOD; i > 0; i--) {
1126641057dSMartin Povišer 		if (state & 1)
1136641057dSMartin Povišer 			state = (state >> 1) ^ (LFSR_POLY >> 1);
1146641057dSMartin Povišer 		else
1156641057dSMartin Povišer 			state = (state >> 1);
1166641057dSMartin Povišer 		tbl->fwd[i] = state;
1176641057dSMartin Povišer 		tbl->inv[state] = i;
1186641057dSMartin Povišer 	}
1196641057dSMartin Povišer 
1206641057dSMartin Povišer 	/* Zero value is special-cased */
1216641057dSMartin Povišer 	tbl->fwd[0] = 0;
1226641057dSMartin Povišer 	tbl->inv[0] = 0;
1236641057dSMartin Povišer }
1246641057dSMartin Povišer 
applnco_div_out_of_range(unsigned int div)1256641057dSMartin Povišer static bool applnco_div_out_of_range(unsigned int div)
1266641057dSMartin Povišer {
1276641057dSMartin Povišer 	unsigned int coarse = div / 4;
1286641057dSMartin Povišer 
1296641057dSMartin Povišer 	return coarse < COARSE_DIV_OFFSET ||
1306641057dSMartin Povišer 		coarse >= COARSE_DIV_OFFSET + LFSR_TBLSIZE;
1316641057dSMartin Povišer }
1326641057dSMartin Povišer 
applnco_div_translate(struct applnco_tables * tbl,unsigned int div)1336641057dSMartin Povišer static u32 applnco_div_translate(struct applnco_tables *tbl, unsigned int div)
1346641057dSMartin Povišer {
1356641057dSMartin Povišer 	unsigned int coarse = div / 4;
1366641057dSMartin Povišer 
1376641057dSMartin Povišer 	if (WARN_ON(applnco_div_out_of_range(div)))
1386641057dSMartin Povišer 		return 0;
1396641057dSMartin Povišer 
1406641057dSMartin Povišer 	return FIELD_PREP(DIV_COARSE, tbl->fwd[coarse - COARSE_DIV_OFFSET]) |
1416641057dSMartin Povišer 			FIELD_PREP(DIV_FINE, div % 4);
1426641057dSMartin Povišer }
1436641057dSMartin Povišer 
applnco_div_translate_inv(struct applnco_tables * tbl,u32 regval)1446641057dSMartin Povišer static unsigned int applnco_div_translate_inv(struct applnco_tables *tbl, u32 regval)
1456641057dSMartin Povišer {
1466641057dSMartin Povišer 	unsigned int coarse, fine;
1476641057dSMartin Povišer 
1486641057dSMartin Povišer 	coarse = tbl->inv[FIELD_GET(DIV_COARSE, regval)] + COARSE_DIV_OFFSET;
1496641057dSMartin Povišer 	fine = FIELD_GET(DIV_FINE, regval);
1506641057dSMartin Povišer 
1516641057dSMartin Povišer 	return coarse * 4 + fine;
1526641057dSMartin Povišer }
1536641057dSMartin Povišer 
applnco_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)1546641057dSMartin Povišer static int applnco_set_rate(struct clk_hw *hw, unsigned long rate,
1556641057dSMartin Povišer 				unsigned long parent_rate)
1566641057dSMartin Povišer {
1576641057dSMartin Povišer 	struct applnco_channel *chan = to_applnco_channel(hw);
1586641057dSMartin Povišer 	unsigned long flags;
1596641057dSMartin Povišer 	u32 div, inc1, inc2;
1606641057dSMartin Povišer 	bool was_enabled;
1616641057dSMartin Povišer 
1626641057dSMartin Povišer 	div = 2 * parent_rate / rate;
1636641057dSMartin Povišer 	inc1 = 2 * parent_rate - div * rate;
1646641057dSMartin Povišer 	inc2 = inc1 - rate;
1656641057dSMartin Povišer 
1666641057dSMartin Povišer 	if (applnco_div_out_of_range(div))
1676641057dSMartin Povišer 		return -EINVAL;
1686641057dSMartin Povišer 
1696641057dSMartin Povišer 	div = applnco_div_translate(chan->tbl, div);
1706641057dSMartin Povišer 
1716641057dSMartin Povišer 	spin_lock_irqsave(&chan->lock, flags);
1726641057dSMartin Povišer 	was_enabled = applnco_is_enabled(hw);
1736641057dSMartin Povišer 	applnco_disable_nolock(hw);
1746641057dSMartin Povišer 
1756641057dSMartin Povišer 	writel_relaxed(div,  chan->base + REG_DIV);
1766641057dSMartin Povišer 	writel_relaxed(inc1, chan->base + REG_INC1);
1776641057dSMartin Povišer 	writel_relaxed(inc2, chan->base + REG_INC2);
1786641057dSMartin Povišer 
1796641057dSMartin Povišer 	/* Presumably a neutral initial value for accumulator */
1806641057dSMartin Povišer 	writel_relaxed(1 << 31, chan->base + REG_ACCINIT);
1816641057dSMartin Povišer 
1826641057dSMartin Povišer 	if (was_enabled)
1836641057dSMartin Povišer 		applnco_enable_nolock(hw);
1846641057dSMartin Povišer 	spin_unlock_irqrestore(&chan->lock, flags);
1856641057dSMartin Povišer 
1866641057dSMartin Povišer 	return 0;
1876641057dSMartin Povišer }
1886641057dSMartin Povišer 
applnco_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)1896641057dSMartin Povišer static unsigned long applnco_recalc_rate(struct clk_hw *hw,
1906641057dSMartin Povišer 				unsigned long parent_rate)
1916641057dSMartin Povišer {
1926641057dSMartin Povišer 	struct applnco_channel *chan = to_applnco_channel(hw);
1936641057dSMartin Povišer 	u32 div, inc1, inc2, incbase;
1946641057dSMartin Povišer 
1956641057dSMartin Povišer 	div = applnco_div_translate_inv(chan->tbl,
1966641057dSMartin Povišer 			readl_relaxed(chan->base + REG_DIV));
1976641057dSMartin Povišer 
1986641057dSMartin Povišer 	inc1 = readl_relaxed(chan->base + REG_INC1);
1996641057dSMartin Povišer 	inc2 = readl_relaxed(chan->base + REG_INC2);
2006641057dSMartin Povišer 
2016641057dSMartin Povišer 	/*
2026641057dSMartin Povišer 	 * We don't support wraparound of accumulator
2036641057dSMartin Povišer 	 * nor the edge case of both increments being zero
2046641057dSMartin Povišer 	 */
2056641057dSMartin Povišer 	if (inc1 >= (1 << 31) || inc2 < (1 << 31) || (inc1 == 0 && inc2 == 0))
2066641057dSMartin Povišer 		return 0;
2076641057dSMartin Povišer 
2086641057dSMartin Povišer 	/* Scale both sides of division by incbase to maintain precision */
2096641057dSMartin Povišer 	incbase = inc1 - inc2;
2106641057dSMartin Povišer 
2116641057dSMartin Povišer 	return div64_u64(((u64) parent_rate) * 2 * incbase,
2126641057dSMartin Povišer 			((u64) div) * incbase + inc1);
2136641057dSMartin Povišer }
2146641057dSMartin Povišer 
applnco_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)2156641057dSMartin Povišer static long applnco_round_rate(struct clk_hw *hw, unsigned long rate,
2166641057dSMartin Povišer 				unsigned long *parent_rate)
2176641057dSMartin Povišer {
2186641057dSMartin Povišer 	unsigned long lo = *parent_rate / (COARSE_DIV_OFFSET + LFSR_TBLSIZE) + 1;
2196641057dSMartin Povišer 	unsigned long hi = *parent_rate / COARSE_DIV_OFFSET;
2206641057dSMartin Povišer 
2216641057dSMartin Povišer 	return clamp(rate, lo, hi);
2226641057dSMartin Povišer }
2236641057dSMartin Povišer 
applnco_enable(struct clk_hw * hw)2246641057dSMartin Povišer static int applnco_enable(struct clk_hw *hw)
2256641057dSMartin Povišer {
2266641057dSMartin Povišer 	struct applnco_channel *chan = to_applnco_channel(hw);
2276641057dSMartin Povišer 	unsigned long flags;
2286641057dSMartin Povišer 
2296641057dSMartin Povišer 	spin_lock_irqsave(&chan->lock, flags);
2306641057dSMartin Povišer 	applnco_enable_nolock(hw);
2316641057dSMartin Povišer 	spin_unlock_irqrestore(&chan->lock, flags);
2326641057dSMartin Povišer 
2336641057dSMartin Povišer 	return 0;
2346641057dSMartin Povišer }
2356641057dSMartin Povišer 
applnco_disable(struct clk_hw * hw)2366641057dSMartin Povišer static void applnco_disable(struct clk_hw *hw)
2376641057dSMartin Povišer {
2386641057dSMartin Povišer 	struct applnco_channel *chan = to_applnco_channel(hw);
2396641057dSMartin Povišer 	unsigned long flags;
2406641057dSMartin Povišer 
2416641057dSMartin Povišer 	spin_lock_irqsave(&chan->lock, flags);
2426641057dSMartin Povišer 	applnco_disable_nolock(hw);
2436641057dSMartin Povišer 	spin_unlock_irqrestore(&chan->lock, flags);
2446641057dSMartin Povišer }
2456641057dSMartin Povišer 
2466641057dSMartin Povišer static const struct clk_ops applnco_ops = {
2476641057dSMartin Povišer 	.set_rate = applnco_set_rate,
2486641057dSMartin Povišer 	.recalc_rate = applnco_recalc_rate,
2496641057dSMartin Povišer 	.round_rate = applnco_round_rate,
2506641057dSMartin Povišer 	.enable = applnco_enable,
2516641057dSMartin Povišer 	.disable = applnco_disable,
2526641057dSMartin Povišer 	.is_enabled = applnco_is_enabled,
2536641057dSMartin Povišer };
2546641057dSMartin Povišer 
applnco_probe(struct platform_device * pdev)2556641057dSMartin Povišer static int applnco_probe(struct platform_device *pdev)
2566641057dSMartin Povišer {
2576641057dSMartin Povišer 	struct device_node *np = pdev->dev.of_node;
2586641057dSMartin Povišer 	struct clk_parent_data pdata = { .index = 0 };
2596641057dSMartin Povišer 	struct clk_init_data init;
2606641057dSMartin Povišer 	struct clk_hw_onecell_data *onecell_data;
2616641057dSMartin Povišer 	void __iomem *base;
2626641057dSMartin Povišer 	struct resource *res;
2636641057dSMartin Povišer 	struct applnco_tables *tbl;
2646641057dSMartin Povišer 	unsigned int nchannels;
2656641057dSMartin Povišer 	int ret, i;
2666641057dSMartin Povišer 
2676641057dSMartin Povišer 	base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2686641057dSMartin Povišer 	if (IS_ERR(base))
2696641057dSMartin Povišer 		return PTR_ERR(base);
2706641057dSMartin Povišer 
2716641057dSMartin Povišer 	if (resource_size(res) < NCO_CHANNEL_REGSIZE)
2726641057dSMartin Povišer 		return -EINVAL;
2736641057dSMartin Povišer 	nchannels = (resource_size(res) - NCO_CHANNEL_REGSIZE)
2746641057dSMartin Povišer 			/ NCO_CHANNEL_STRIDE + 1;
2756641057dSMartin Povišer 
2766641057dSMartin Povišer 	onecell_data = devm_kzalloc(&pdev->dev, struct_size(onecell_data, hws,
2776641057dSMartin Povišer 							nchannels), GFP_KERNEL);
2786641057dSMartin Povišer 	if (!onecell_data)
2796641057dSMartin Povišer 		return -ENOMEM;
2806641057dSMartin Povišer 	onecell_data->num = nchannels;
2816641057dSMartin Povišer 
2826641057dSMartin Povišer 	tbl = devm_kzalloc(&pdev->dev, sizeof(*tbl), GFP_KERNEL);
2836641057dSMartin Povišer 	if (!tbl)
2846641057dSMartin Povišer 		return -ENOMEM;
2856641057dSMartin Povišer 	applnco_compute_tables(tbl);
2866641057dSMartin Povišer 
2876641057dSMartin Povišer 	for (i = 0; i < nchannels; i++) {
2886641057dSMartin Povišer 		struct applnco_channel *chan;
2896641057dSMartin Povišer 
2906641057dSMartin Povišer 		chan = devm_kzalloc(&pdev->dev, sizeof(*chan), GFP_KERNEL);
2916641057dSMartin Povišer 		if (!chan)
2926641057dSMartin Povišer 			return -ENOMEM;
2936641057dSMartin Povišer 		chan->base = base + NCO_CHANNEL_STRIDE * i;
2946641057dSMartin Povišer 		chan->tbl = tbl;
2956641057dSMartin Povišer 		spin_lock_init(&chan->lock);
2966641057dSMartin Povišer 
2976641057dSMartin Povišer 		memset(&init, 0, sizeof(init));
2986641057dSMartin Povišer 		init.name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
2996641057dSMartin Povišer 						"%s-%d", np->name, i);
300*72ea9a7eSCharles Han 		if (!init.name)
301*72ea9a7eSCharles Han 			return -ENOMEM;
302*72ea9a7eSCharles Han 
3036641057dSMartin Povišer 		init.ops = &applnco_ops;
3046641057dSMartin Povišer 		init.parent_data = &pdata;
3056641057dSMartin Povišer 		init.num_parents = 1;
3066641057dSMartin Povišer 		init.flags = 0;
3076641057dSMartin Povišer 
3086641057dSMartin Povišer 		chan->hw.init = &init;
3096641057dSMartin Povišer 		ret = devm_clk_hw_register(&pdev->dev, &chan->hw);
3106641057dSMartin Povišer 		if (ret)
3116641057dSMartin Povišer 			return ret;
3126641057dSMartin Povišer 
3136641057dSMartin Povišer 		onecell_data->hws[i] = &chan->hw;
3146641057dSMartin Povišer 	}
3156641057dSMartin Povišer 
3166641057dSMartin Povišer 	return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_onecell_get,
3176641057dSMartin Povišer 							onecell_data);
3186641057dSMartin Povišer }
3196641057dSMartin Povišer 
3206641057dSMartin Povišer static const struct of_device_id applnco_ids[] = {
3216641057dSMartin Povišer 	{ .compatible = "apple,nco" },
3226641057dSMartin Povišer 	{ }
3236641057dSMartin Povišer };
324236541acSMartin Povišer MODULE_DEVICE_TABLE(of, applnco_ids);
3256641057dSMartin Povišer 
3266641057dSMartin Povišer static struct platform_driver applnco_driver = {
3276641057dSMartin Povišer 	.driver = {
3286641057dSMartin Povišer 		.name = "apple-nco",
3296641057dSMartin Povišer 		.of_match_table = applnco_ids,
3306641057dSMartin Povišer 	},
3316641057dSMartin Povišer 	.probe = applnco_probe,
3326641057dSMartin Povišer };
3336641057dSMartin Povišer module_platform_driver(applnco_driver);
3346641057dSMartin Povišer 
3356641057dSMartin Povišer MODULE_AUTHOR("Martin Povišer <povik+lin@cutebit.org>");
3366641057dSMartin Povišer MODULE_DESCRIPTION("Clock driver for NCO blocks on Apple SoCs");
3376641057dSMartin Povišer MODULE_LICENSE("GPL");
338