blob: 24813de265301b8efdb2fc04c05937160737ae63 [file] [log] [blame]
Marek Vasut6be61c62019-05-04 17:30:58 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Renesas RZ/A1 R7S72100 OSTM Timer driver
4 *
5 * Copyright (C) 2019 Marek Vasut <marek.vasut@gmail.com>
6 */
7
8#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Marek Vasut6be61c62019-05-04 17:30:58 +020011#include <asm/io.h>
12#include <dm.h>
13#include <clk.h>
14#include <timer.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Marek Vasut6be61c62019-05-04 17:30:58 +020016
17#define OSTM_CMP 0x00
18#define OSTM_CNT 0x04
19#define OSTM_TE 0x10
20#define OSTM_TS 0x14
21#define OSTM_TT 0x18
22#define OSTM_CTL 0x20
23#define OSTM_CTL_D BIT(1)
24
25DECLARE_GLOBAL_DATA_PTR;
26
27struct ostm_priv {
28 fdt_addr_t regs;
29};
30
Sean Anderson947fc2d2020-10-07 14:37:44 -040031static u64 ostm_get_count(struct udevice *dev)
Marek Vasut6be61c62019-05-04 17:30:58 +020032{
33 struct ostm_priv *priv = dev_get_priv(dev);
34
Sean Anderson947fc2d2020-10-07 14:37:44 -040035 return timer_conv_64(readl(priv->regs + OSTM_CNT));
Marek Vasut6be61c62019-05-04 17:30:58 +020036}
37
38static int ostm_probe(struct udevice *dev)
39{
40 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
41 struct ostm_priv *priv = dev_get_priv(dev);
42#if CONFIG_IS_ENABLED(CLK)
43 struct clk clk;
44 int ret;
45
46 ret = clk_get_by_index(dev, 0, &clk);
47 if (ret)
48 return ret;
49
50 uc_priv->clock_rate = clk_get_rate(&clk);
51
52 clk_free(&clk);
53#else
54 uc_priv->clock_rate = CONFIG_SYS_CLK_FREQ / 2;
55#endif
56
57 readb(priv->regs + OSTM_CTL);
58 writeb(OSTM_CTL_D, priv->regs + OSTM_CTL);
59
60 setbits_8(priv->regs + OSTM_TT, BIT(0));
61 writel(0xffffffff, priv->regs + OSTM_CMP);
62 setbits_8(priv->regs + OSTM_TS, BIT(0));
63
64 return 0;
65}
66
Simon Glassaad29ae2020-12-03 16:55:21 -070067static int ostm_of_to_plat(struct udevice *dev)
Marek Vasut6be61c62019-05-04 17:30:58 +020068{
69 struct ostm_priv *priv = dev_get_priv(dev);
70
71 priv->regs = dev_read_addr(dev);
72
73 return 0;
74}
75
76static const struct timer_ops ostm_ops = {
77 .get_count = ostm_get_count,
78};
79
80static const struct udevice_id ostm_ids[] = {
81 { .compatible = "renesas,ostm" },
82 {}
83};
84
85U_BOOT_DRIVER(ostm_timer) = {
86 .name = "ostm-timer",
87 .id = UCLASS_TIMER,
88 .ops = &ostm_ops,
89 .probe = ostm_probe,
90 .of_match = ostm_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -070091 .of_to_plat = ostm_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -070092 .priv_auto = sizeof(struct ostm_priv),
Marek Vasut6be61c62019-05-04 17:30:58 +020093};