blob: 0607f751ca708792bb28d36aa4d641919a53b69c [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>
Marek Vasut442c0f12018-08-18 15:58:32 +020016
17#include <asm/io.h>
18#include <asm/arch/timer.h>
19
20#define DW_APB_LOAD_VAL 0x0
21#define DW_APB_CURR_VAL 0x4
22#define DW_APB_CTRL 0x8
23
Marek Vasut442c0f12018-08-18 15:58:32 +020024struct dw_apb_timer_priv {
Johan Jonker16427632023-03-13 01:29:47 +010025 uintptr_t regs;
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +020026 struct reset_ctl_bulk resets;
Marek Vasut442c0f12018-08-18 15:58:32 +020027};
28
Johan Jonker03872982022-04-09 18:55:06 +020029struct dw_apb_timer_plat {
30#if CONFIG_IS_ENABLED(OF_PLATDATA)
31 struct dtd_snps_dw_apb_timer dtplat;
32#endif
33};
34
Sean Anderson947fc2d2020-10-07 14:37:44 -040035static u64 dw_apb_timer_get_count(struct udevice *dev)
Marek Vasut442c0f12018-08-18 15:58:32 +020036{
37 struct dw_apb_timer_priv *priv = dev_get_priv(dev);
38
39 /*
40 * The DW APB counter counts down, but this function
41 * requires the count to be incrementing. Invert the
42 * result.
43 */
Sean Anderson947fc2d2020-10-07 14:37:44 -040044 return timer_conv_64(~readl(priv->regs + DW_APB_CURR_VAL));
Marek Vasut442c0f12018-08-18 15:58:32 +020045}
46
47static int dw_apb_timer_probe(struct udevice *dev)
48{
49 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
50 struct dw_apb_timer_priv *priv = dev_get_priv(dev);
51 struct clk clk;
52 int ret;
Johan Jonker03872982022-04-09 18:55:06 +020053#if CONFIG_IS_ENABLED(OF_PLATDATA)
54 struct dw_apb_timer_plat *plat = dev_get_plat(dev);
55 struct dtd_snps_dw_apb_timer *dtplat = &plat->dtplat;
Marek Vasut442c0f12018-08-18 15:58:32 +020056
Johan Jonker03872982022-04-09 18:55:06 +020057 priv->regs = dtplat->reg[0];
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +020058
Johan Jonker03872982022-04-09 18:55:06 +020059 ret = clk_get_by_phandle(dev, &dtplat->clocks[0], &clk);
60 if (ret < 0)
Marek Vasut442c0f12018-08-18 15:58:32 +020061 return ret;
62
Johan Jonker03872982022-04-09 18:55:06 +020063 uc_priv->clock_rate = dtplat->clock_frequency;
64#endif
65 if (CONFIG_IS_ENABLED(OF_REAL)) {
66 ret = reset_get_bulk(dev, &priv->resets);
67 if (ret)
68 dev_warn(dev, "Can't get reset: %d\n", ret);
69 else
70 reset_deassert_bulk(&priv->resets);
Marek Vasut442c0f12018-08-18 15:58:32 +020071
Johan Jonker03872982022-04-09 18:55:06 +020072 ret = clk_get_by_index(dev, 0, &clk);
73 if (ret)
74 return ret;
75
76 uc_priv->clock_rate = clk_get_rate(&clk);
Johan Jonker03872982022-04-09 18:55:06 +020077 }
Marek Vasut442c0f12018-08-18 15:58:32 +020078
79 /* init timer */
80 writel(0xffffffff, priv->regs + DW_APB_LOAD_VAL);
81 writel(0xffffffff, priv->regs + DW_APB_CURR_VAL);
82 setbits_le32(priv->regs + DW_APB_CTRL, 0x3);
83
84 return 0;
85}
86
Simon Glassaad29ae2020-12-03 16:55:21 -070087static int dw_apb_timer_of_to_plat(struct udevice *dev)
Marek Vasut442c0f12018-08-18 15:58:32 +020088{
Johan Jonker03872982022-04-09 18:55:06 +020089 if (CONFIG_IS_ENABLED(OF_REAL)) {
90 struct dw_apb_timer_priv *priv = dev_get_priv(dev);
Marek Vasut442c0f12018-08-18 15:58:32 +020091
Johan Jonker03872982022-04-09 18:55:06 +020092 priv->regs = dev_read_addr(dev);
93 }
Marek Vasut442c0f12018-08-18 15:58:32 +020094
95 return 0;
96}
97
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +020098static int dw_apb_timer_remove(struct udevice *dev)
99{
100 struct dw_apb_timer_priv *priv = dev_get_priv(dev);
101
102 return reset_release_bulk(&priv->resets);
103}
104
Marek Vasut442c0f12018-08-18 15:58:32 +0200105static const struct timer_ops dw_apb_timer_ops = {
106 .get_count = dw_apb_timer_get_count,
107};
108
109static const struct udevice_id dw_apb_timer_ids[] = {
110 { .compatible = "snps,dw-apb-timer" },
111 {}
112};
113
Johan Jonker03872982022-04-09 18:55:06 +0200114U_BOOT_DRIVER(snps_dw_apb_timer) = {
115 .name = "snps_dw_apb_timer",
Marek Vasut442c0f12018-08-18 15:58:32 +0200116 .id = UCLASS_TIMER,
117 .ops = &dw_apb_timer_ops,
118 .probe = dw_apb_timer_probe,
Marek Vasut442c0f12018-08-18 15:58:32 +0200119 .of_match = dw_apb_timer_ids,
Johan Jonker32de1ee2022-04-09 18:55:07 +0200120 .of_to_plat = dw_apb_timer_of_to_plat,
Simon Goldschmidt0b6b82a2019-10-23 22:23:12 +0200121 .remove = dw_apb_timer_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700122 .priv_auto = sizeof(struct dw_apb_timer_priv),
Johan Jonker03872982022-04-09 18:55:06 +0200123 .plat_auto = sizeof(struct dw_apb_timer_plat),
Marek Vasut442c0f12018-08-18 15:58:32 +0200124};