blob: 314f956cdfb7a62dd103af4bc6b1f787127d2b68 [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
Tom Rini8c70baa2021-12-14 13:36:40 -05008#include <clock_legacy.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);
Marek Vasut6be61c62019-05-04 17:30:58 +020051#else
Tom Rini8c70baa2021-12-14 13:36:40 -050052 uc_priv->clock_rate = get_board_sys_clk() / 2;
Marek Vasut6be61c62019-05-04 17:30:58 +020053#endif
54
55 readb(priv->regs + OSTM_CTL);
56 writeb(OSTM_CTL_D, priv->regs + OSTM_CTL);
57
58 setbits_8(priv->regs + OSTM_TT, BIT(0));
59 writel(0xffffffff, priv->regs + OSTM_CMP);
60 setbits_8(priv->regs + OSTM_TS, BIT(0));
61
62 return 0;
63}
64
Simon Glassaad29ae2020-12-03 16:55:21 -070065static int ostm_of_to_plat(struct udevice *dev)
Marek Vasut6be61c62019-05-04 17:30:58 +020066{
67 struct ostm_priv *priv = dev_get_priv(dev);
68
69 priv->regs = dev_read_addr(dev);
70
71 return 0;
72}
73
74static const struct timer_ops ostm_ops = {
75 .get_count = ostm_get_count,
76};
77
78static const struct udevice_id ostm_ids[] = {
79 { .compatible = "renesas,ostm" },
80 {}
81};
82
83U_BOOT_DRIVER(ostm_timer) = {
84 .name = "ostm-timer",
85 .id = UCLASS_TIMER,
86 .ops = &ostm_ops,
87 .probe = ostm_probe,
88 .of_match = ostm_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -070089 .of_to_plat = ostm_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -070090 .priv_auto = sizeof(struct ostm_priv),
Marek Vasut6be61c62019-05-04 17:30:58 +020091};