blob: ead8c9b84ad55a078c64bea6a4c5d29d30cc5078 [file] [log] [blame]
Claudiu Beznea5669c3d2020-09-07 18:36:33 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * 64-bit Periodic Interval Timer driver
4 *
5 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
6 *
7 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
8 */
9
10#include <common.h>
11#include <clk.h>
12#include <dm.h>
13#include <timer.h>
14#include <asm/io.h>
15
16#define MCHP_PIT64B_CR 0x00 /* Control Register */
17#define MCHP_PIT64B_CR_START BIT(0)
18#define MCHP_PIT64B_CR_SWRST BIT(8)
19#define MCHP_PIT64B_MR 0x04 /* Mode Register */
20#define MCHP_PIT64B_MR_CONT BIT(0)
21#define MCHP_PIT64B_LSB_PR 0x08 /* LSB Period Register */
22#define MCHP_PIT64B_MSB_PR 0x0C /* MSB Period Register */
23#define MCHP_PIT64B_TLSBR 0x20 /* Timer LSB Register */
24#define MCHP_PIT64B_TMSBR 0x24 /* Timer MSB Register */
25
26struct mchp_pit64b_priv {
27 void __iomem *base;
28};
29
30static int mchp_pit64b_get_count(struct udevice *dev, u64 *count)
31{
32 struct mchp_pit64b_priv *priv = dev_get_priv(dev);
33
34 u32 lsb = readl(priv->base + MCHP_PIT64B_TLSBR);
35 u32 msb = readl(priv->base + MCHP_PIT64B_TMSBR);
36
37 *count = ((u64)msb << 32) | lsb;
38
39 return 0;
40}
41
42static int mchp_pit64b_probe(struct udevice *dev)
43{
44 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
45 struct mchp_pit64b_priv *priv = dev_get_priv(dev);
46 struct clk clk;
47 ulong rate;
48 int ret;
49
50 priv->base = dev_read_addr_ptr(dev);
51 if (IS_ERR(priv->base))
52 return PTR_ERR(priv->base);
53
54 ret = clk_get_by_index(dev, 0, &clk);
55 if (ret)
56 return ret;
57
58 ret = clk_enable(&clk);
59 if (ret)
60 return ret;
61
62 rate = clk_get_rate(&clk);
63 if (!rate) {
64 clk_disable(&clk);
65 return -ENOTSUPP;
66 }
67
68 /* Reset the timer in case it was used by previous bootloaders. */
69 writel(MCHP_PIT64B_CR_SWRST, priv->base + MCHP_PIT64B_CR);
70
71 /*
72 * Use highest prescaller (for a peripheral clock running at 200MHz
73 * this will lead to the timer running at 12.5MHz) and continuous mode.
74 */
75 writel((15 << 8) | MCHP_PIT64B_MR_CONT, priv->base + MCHP_PIT64B_MR);
76 uc_priv->clock_rate = rate / 16;
77
78 /*
79 * Simulate free running counter by setting max values to period
80 * registers.
81 */
82 writel(~0UL, priv->base + MCHP_PIT64B_MSB_PR);
83 writel(~0UL, priv->base + MCHP_PIT64B_LSB_PR);
84
85 /* Start the timer. */
86 writel(MCHP_PIT64B_CR_START, priv->base + MCHP_PIT64B_CR);
87
88 return 0;
89}
90
91static const struct timer_ops mchp_pit64b_ops = {
92 .get_count = mchp_pit64b_get_count,
93};
94
95static const struct udevice_id mchp_pit64b_ids[] = {
96 { .compatible = "microchip,sam9x60-pit64b", },
97 { .compatible = "microchip,sama7g5-pit64b", },
98 { }
99};
100
101U_BOOT_DRIVER(mchp_pit64b) = {
102 .name = "mchp-pit64b",
103 .id = UCLASS_TIMER,
104 .of_match = mchp_pit64b_ids,
105 .priv_auto_alloc_size = sizeof(struct mchp_pit64b_priv),
106 .probe = mchp_pit64b_probe,
107 .ops = &mchp_pit64b_ops,
108 .flags = DM_FLAG_PRE_RELOC,
109};