summaryrefslogtreecommitdiff
path: root/drivers/clk/sophgo/clk-cv18xx-common.c
blob: cbcdd88f0e23fa73663bdeb3680238ba4be1ba87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2023 Inochi Amaoto <inochiama@outlook.com>
 */

#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/spinlock.h>
#include <linux/bug.h>

#include "clk-cv18xx-common.h"

int cv1800_clk_setbit(struct cv1800_clk_common *common,
		      struct cv1800_clk_regbit *field)
{
	u32 mask = BIT(field->shift);
	u32 value;
	unsigned long flags;

	spin_lock_irqsave(common->lock, flags);

	value = readl(common->base + field->reg);
	writel(value | mask, common->base + field->reg);

	spin_unlock_irqrestore(common->lock, flags);

	return 0;
}

int cv1800_clk_clearbit(struct cv1800_clk_common *common,
			struct cv1800_clk_regbit *field)
{
	u32 mask = BIT(field->shift);
	u32 value;
	unsigned long flags;

	spin_lock_irqsave(common->lock, flags);

	value = readl(common->base + field->reg);
	writel(value & ~mask, common->base + field->reg);

	spin_unlock_irqrestore(common->lock, flags);

	return 0;
}

int cv1800_clk_checkbit(struct cv1800_clk_common *common,
			struct cv1800_clk_regbit *field)
{
	return readl(common->base + field->reg) & BIT(field->shift);
}

#define PLL_LOCK_TIMEOUT_US	(200 * 1000)

void cv1800_clk_wait_for_lock(struct cv1800_clk_common *common,
			      u32 reg, u32 lock)
{
	void __iomem *addr = common->base + reg;
	u32 regval;

	if (!lock)
		return;

	WARN_ON(readl_relaxed_poll_timeout(addr, regval, regval & lock,
					   100, PLL_LOCK_TIMEOUT_US));
}