aboutsummaryrefslogtreecommitdiff
path: root/drivers/timer/mchp-pit64b-timer.c
blob: ead8c9b84ad55a078c64bea6a4c5d29d30cc5078 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// SPDX-License-Identifier: GPL-2.0+
/*
 * 64-bit Periodic Interval Timer driver
 *
 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
 *
 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
 */

#include <common.h>
#include <clk.h>
#include <dm.h>
#include <timer.h>
#include <asm/io.h>

#define MCHP_PIT64B_CR			0x00	/* Control Register */
#define		MCHP_PIT64B_CR_START	BIT(0)
#define		MCHP_PIT64B_CR_SWRST	BIT(8)
#define MCHP_PIT64B_MR			0x04	/* Mode Register */
#define		MCHP_PIT64B_MR_CONT	BIT(0)
#define MCHP_PIT64B_LSB_PR		0x08	/* LSB Period Register */
#define MCHP_PIT64B_MSB_PR		0x0C	/* MSB Period Register */
#define MCHP_PIT64B_TLSBR		0x20	/* Timer LSB Register */
#define MCHP_PIT64B_TMSBR		0x24	/* Timer MSB Register */

struct mchp_pit64b_priv {
	void __iomem *base;
};

static int mchp_pit64b_get_count(struct udevice *dev, u64 *count)
{
	struct mchp_pit64b_priv *priv = dev_get_priv(dev);

	u32 lsb = readl(priv->base + MCHP_PIT64B_TLSBR);
	u32 msb = readl(priv->base + MCHP_PIT64B_TMSBR);

	*count = ((u64)msb << 32) | lsb;

	return 0;
}

static int mchp_pit64b_probe(struct udevice *dev)
{
	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
	struct mchp_pit64b_priv *priv = dev_get_priv(dev);
	struct clk clk;
	ulong rate;
	int ret;

	priv->base = dev_read_addr_ptr(dev);
	if (IS_ERR(priv->base))
		return PTR_ERR(priv->base);

	ret = clk_get_by_index(dev, 0, &clk);
	if (ret)
		return ret;

	ret = clk_enable(&clk);
	if (ret)
		return ret;

	rate = clk_get_rate(&clk);
	if (!rate) {
		clk_disable(&clk);
		return -ENOTSUPP;
	}

	/* Reset the timer in case it was used by previous bootloaders. */
	writel(MCHP_PIT64B_CR_SWRST, priv->base + MCHP_PIT64B_CR);

	/*
	 * Use highest prescaller (for a peripheral clock running at 200MHz
	 * this will lead to the timer running at 12.5MHz) and continuous mode.
	 */
	writel((15 << 8) | MCHP_PIT64B_MR_CONT, priv->base + MCHP_PIT64B_MR);
	uc_priv->clock_rate = rate / 16;

	/*
	 * Simulate free running counter by setting max values to period
	 * registers.
	 */
	writel(~0UL, priv->base + MCHP_PIT64B_MSB_PR);
	writel(~0UL, priv->base + MCHP_PIT64B_LSB_PR);

	/* Start the timer. */
	writel(MCHP_PIT64B_CR_START, priv->base + MCHP_PIT64B_CR);

	return 0;
}

static const struct timer_ops mchp_pit64b_ops = {
	.get_count = mchp_pit64b_get_count,
};

static const struct udevice_id mchp_pit64b_ids[] = {
	{ .compatible = "microchip,sam9x60-pit64b", },
	{ .compatible = "microchip,sama7g5-pit64b", },
	{ }
};

U_BOOT_DRIVER(mchp_pit64b) = {
	.name	= "mchp-pit64b",
	.id	= UCLASS_TIMER,
	.of_match = mchp_pit64b_ids,
	.priv_auto_alloc_size = sizeof(struct mchp_pit64b_priv),
	.probe	= mchp_pit64b_probe,
	.ops	= &mchp_pit64b_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};