blob: 10f0a9f646e55c6214327af1df2be9ab2be79ee6 [file] [log] [blame]
Marek Vasut442c0f12018-08-18 15:58:32 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Designware APB Timer driver
4 *
5 * Copyright (C) 2018 Marek Vasut <marex@denx.de>
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <clk.h>
Johan Jonker03872982022-04-09 18:55:06 +020011#include <dt-structs.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +020013#include <reset.h>
Marek Vasut442c0f12018-08-18 15:58:32 +020014#include <timer.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Johan Jonker03872982022-04-09 18:55:06 +020016#include <linux/kconfig.h>
Marek Vasut442c0f12018-08-18 15:58:32 +020017
18#include <asm/io.h>
19#include <asm/arch/timer.h>
20
21#define DW_APB_LOAD_VAL 0x0
22#define DW_APB_CURR_VAL 0x4
23#define DW_APB_CTRL 0x8
24
Marek Vasut442c0f12018-08-18 15:58:32 +020025struct dw_apb_timer_priv {
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +020026 fdt_addr_t regs;
27 struct reset_ctl_bulk resets;
Marek Vasut442c0f12018-08-18 15:58:32 +020028};
29
Johan Jonker03872982022-04-09 18:55:06 +020030struct dw_apb_timer_plat {
31#if CONFIG_IS_ENABLED(OF_PLATDATA)
32 struct dtd_snps_dw_apb_timer dtplat;
33#endif
34};
35
Sean Anderson947fc2d2020-10-07 14:37:44 -040036static u64 dw_apb_timer_get_count(struct udevice *dev)
Marek Vasut442c0f12018-08-18 15:58:32 +020037{
38 struct dw_apb_timer_priv *priv = dev_get_priv(dev);
39
40 /*
41 * The DW APB counter counts down, but this function
42 * requires the count to be incrementing. Invert the
43 * result.
44 */
Sean Anderson947fc2d2020-10-07 14:37:44 -040045 return timer_conv_64(~readl(priv->regs + DW_APB_CURR_VAL));
Marek Vasut442c0f12018-08-18 15:58:32 +020046}
47
48static int dw_apb_timer_probe(struct udevice *dev)
49{
50 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
51 struct dw_apb_timer_priv *priv = dev_get_priv(dev);
52 struct clk clk;
53 int ret;
Johan Jonker03872982022-04-09 18:55:06 +020054#if CONFIG_IS_ENABLED(OF_PLATDATA)
55 struct dw_apb_timer_plat *plat = dev_get_plat(dev);
56 struct dtd_snps_dw_apb_timer *dtplat = &plat->dtplat;
Marek Vasut442c0f12018-08-18 15:58:32 +020057
Johan Jonker03872982022-04-09 18:55:06 +020058 priv->regs = dtplat->reg[0];
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +020059
Johan Jonker03872982022-04-09 18:55:06 +020060 ret = clk_get_by_phandle(dev, &dtplat->clocks[0], &clk);
61 if (ret < 0)
Marek Vasut442c0f12018-08-18 15:58:32 +020062 return ret;
63
Johan Jonker03872982022-04-09 18:55:06 +020064 uc_priv->clock_rate = dtplat->clock_frequency;
65#endif
66 if (CONFIG_IS_ENABLED(OF_REAL)) {
67 ret = reset_get_bulk(dev, &priv->resets);
68 if (ret)
69 dev_warn(dev, "Can't get reset: %d\n", ret);
70 else
71 reset_deassert_bulk(&priv->resets);
Marek Vasut442c0f12018-08-18 15:58:32 +020072
Johan Jonker03872982022-04-09 18:55:06 +020073 ret = clk_get_by_index(dev, 0, &clk);
74 if (ret)
75 return ret;
76
77 uc_priv->clock_rate = clk_get_rate(&clk);
78
79 clk_free(&clk);
80 }
Marek Vasut442c0f12018-08-18 15:58:32 +020081
82 /* init timer */
83 writel(0xffffffff, priv->regs + DW_APB_LOAD_VAL);
84 writel(0xffffffff, priv->regs + DW_APB_CURR_VAL);
85 setbits_le32(priv->regs + DW_APB_CTRL, 0x3);
86
87 return 0;
88}
89
Simon Glassaad29ae2020-12-03 16:55:21 -070090static int dw_apb_timer_of_to_plat(struct udevice *dev)
Marek Vasut442c0f12018-08-18 15:58:32 +020091{
Johan Jonker03872982022-04-09 18:55:06 +020092 if (CONFIG_IS_ENABLED(OF_REAL)) {
93 struct dw_apb_timer_priv *priv = dev_get_priv(dev);
Marek Vasut442c0f12018-08-18 15:58:32 +020094
Johan Jonker03872982022-04-09 18:55:06 +020095 priv->regs = dev_read_addr(dev);
96 }
Marek Vasut442c0f12018-08-18 15:58:32 +020097
98 return 0;
99}
100
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +0200101static int dw_apb_timer_remove(struct udevice *dev)
102{
103 struct dw_apb_timer_priv *priv = dev_get_priv(dev);
104
105 return reset_release_bulk(&priv->resets);
106}
107
Marek Vasut442c0f12018-08-18 15:58:32 +0200108static const struct timer_ops dw_apb_timer_ops = {
109 .get_count = dw_apb_timer_get_count,
110};
111
112static const struct udevice_id dw_apb_timer_ids[] = {
113 { .compatible = "snps,dw-apb-timer" },
114 {}
115};
116
Johan Jonker03872982022-04-09 18:55:06 +0200117U_BOOT_DRIVER(snps_dw_apb_timer) = {
118 .name = "snps_dw_apb_timer",
Marek Vasut442c0f12018-08-18 15:58:32 +0200119 .id = UCLASS_TIMER,
120 .ops = &dw_apb_timer_ops,
121 .probe = dw_apb_timer_probe,
Marek Vasut442c0f12018-08-18 15:58:32 +0200122 .of_match = dw_apb_timer_ids,
Johan Jonker32de1ee2022-04-09 18:55:07 +0200123 .of_to_plat = dw_apb_timer_of_to_plat,
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +0200124 .remove = dw_apb_timer_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700125 .priv_auto = sizeof(struct dw_apb_timer_priv),
Johan Jonker03872982022-04-09 18:55:06 +0200126 .plat_auto = sizeof(struct dw_apb_timer_plat),
Marek Vasut442c0f12018-08-18 15:58:32 +0200127};