1 // 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>
9 #include <malloc.h>
10 #include <asm/global_data.h>
11 #include <asm/io.h>
12 #include <dm.h>
13 #include <clk.h>
14 #include <timer.h>
15 #include <linux/bitops.h>
16 
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 
25 DECLARE_GLOBAL_DATA_PTR;
26 
27 struct ostm_priv {
28 	fdt_addr_t	regs;
29 };
30 
ostm_get_count(struct udevice * dev)31 static u64 ostm_get_count(struct udevice *dev)
32 {
33 	struct ostm_priv *priv = dev_get_priv(dev);
34 
35 	return timer_conv_64(readl(priv->regs + OSTM_CNT));
36 }
37 
ostm_probe(struct udevice * dev)38 static 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 
ostm_of_to_plat(struct udevice * dev)67 static int ostm_of_to_plat(struct udevice *dev)
68 {
69 	struct ostm_priv *priv = dev_get_priv(dev);
70 
71 	priv->regs = dev_read_addr(dev);
72 
73 	return 0;
74 }
75 
76 static const struct timer_ops ostm_ops = {
77 	.get_count	= ostm_get_count,
78 };
79 
80 static const struct udevice_id ostm_ids[] = {
81 	{ .compatible = "renesas,ostm" },
82 	{}
83 };
84 
85 U_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,
91 	.of_to_plat = ostm_of_to_plat,
92 	.priv_auto	= sizeof(struct ostm_priv),
93 };
94