1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017-2022 Weidmüller Interface GmbH & Co. KG
4 * Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
6 * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
7 * Copyright (C) 2011-2017 Xilinx, Inc. All rights reserved.
10 * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
13 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
15 * (C) Copyright 2002-2004
16 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
19 * Texas Instruments <www.ti.com>
22 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
23 * Marius Groeger <mgroeger@sysgo.de>
26 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
27 * Alex Zuepke <azu@sysgo.de>
34 #include <linux/bitops.h>
38 #define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
39 #define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
40 #define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
42 #define TIMER_LOAD_VAL 0xFFFFFFFF
44 struct arm_twd_timer_regs {
45 u32 load; /* Timer Load Register */
46 u32 counter; /* Timer Counter Register */
47 u32 control; /* Timer Control Register */
50 struct arm_twd_timer_priv {
51 struct arm_twd_timer_regs *base;
54 static u64 arm_twd_timer_get_count(struct udevice *dev)
56 struct arm_twd_timer_priv *priv = dev_get_priv(dev);
57 struct arm_twd_timer_regs *regs = priv->base;
58 u32 count = TIMER_LOAD_VAL - readl(®s->counter);
60 return timer_conv_64(count);
63 static int arm_twd_timer_probe(struct udevice *dev)
65 struct arm_twd_timer_priv *priv = dev_get_priv(dev);
66 struct arm_twd_timer_regs *regs;
69 addr = dev_read_addr(dev);
70 if (addr == FDT_ADDR_T_NONE)
73 priv->base = (struct arm_twd_timer_regs *)addr;
77 /* Load the timer counter register */
78 writel(0xFFFFFFFF, ®s->load);
81 * Start the A9Timer device
82 * Enable Auto reload mode, Clear prescaler control bits
83 * Set prescaler value, Enable the decrementer
85 clrsetbits_le32(®s->control, SCUTIMER_CONTROL_PRESCALER_MASK,
86 SCUTIMER_CONTROL_AUTO_RELOAD_MASK |
87 SCUTIMER_CONTROL_ENABLE_MASK);
92 static const struct timer_ops arm_twd_timer_ops = {
93 .get_count = arm_twd_timer_get_count,
96 static const struct udevice_id arm_twd_timer_ids[] = {
97 { .compatible = "arm,cortex-a9-twd-timer" },
101 U_BOOT_DRIVER(arm_twd_timer) = {
102 .name = "arm_twd_timer",
104 .of_match = arm_twd_timer_ids,
105 .priv_auto = sizeof(struct arm_twd_timer_priv),
106 .probe = arm_twd_timer_probe,
107 .ops = &arm_twd_timer_ops,