blob: f7d201b921a11a5c1c0fe6b402d01b10b1ac63f9 [file] [log] [blame]
Stefan Roese053fce82018-10-04 13:39:07 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Ralink / Mediatek RT288x/RT3xxx/MT76xx built-in hardware watchdog timer
4 *
5 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
6 *
7 * Based on the Linux driver version which is:
8 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
9 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
10 */
11
12#include <common.h>
13#include <dm.h>
14#include <wdt.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glasscd93d622020-05-10 11:40:13 -060016#include <linux/bitops.h>
Stefan Roese053fce82018-10-04 13:39:07 +020017#include <linux/io.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21struct mt762x_wdt {
22 void __iomem *regs;
23};
24
25#define TIMER_REG_TMRSTAT 0x00
26#define TIMER_REG_TMR1CTL 0x20
27#define TIMER_REG_TMR1LOAD 0x24
28
29#define TMR1CTL_ENABLE BIT(7)
30#define TMR1CTL_RESTART BIT(9)
31#define TMR1CTL_PRESCALE_SHIFT 16
32
33static int mt762x_wdt_ping(struct mt762x_wdt *priv)
34{
35 writel(TMR1CTL_RESTART, priv->regs + TIMER_REG_TMRSTAT);
36
37 return 0;
38}
39
40static int mt762x_wdt_start(struct udevice *dev, u64 ms, ulong flags)
41{
42 struct mt762x_wdt *priv = dev_get_priv(dev);
43
44 /* set the prescaler to 1ms == 1000us */
45 writel(1000 << TMR1CTL_PRESCALE_SHIFT, priv->regs + TIMER_REG_TMR1CTL);
46 writel(ms, priv->regs + TIMER_REG_TMR1LOAD);
47
48 setbits_le32(priv->regs + TIMER_REG_TMR1CTL, TMR1CTL_ENABLE);
49
50 return 0;
51}
52
53static int mt762x_wdt_stop(struct udevice *dev)
54{
55 struct mt762x_wdt *priv = dev_get_priv(dev);
56
57 mt762x_wdt_ping(priv);
58
59 clrbits_le32(priv->regs + TIMER_REG_TMR1CTL, TMR1CTL_ENABLE);
60
61 return 0;
62}
63
64static int mt762x_wdt_reset(struct udevice *dev)
65{
66 struct mt762x_wdt *priv = dev_get_priv(dev);
67
68 mt762x_wdt_ping(priv);
69
70 return 0;
71}
72
73static int mt762x_wdt_probe(struct udevice *dev)
74{
75 struct mt762x_wdt *priv = dev_get_priv(dev);
76
77 priv->regs = dev_remap_addr(dev);
78 if (!priv->regs)
79 return -EINVAL;
80
81 mt762x_wdt_stop(dev);
82
83 return 0;
84}
85
86static const struct wdt_ops mt762x_wdt_ops = {
87 .start = mt762x_wdt_start,
88 .reset = mt762x_wdt_reset,
89 .stop = mt762x_wdt_stop,
90};
91
92static const struct udevice_id mt762x_wdt_ids[] = {
93 { .compatible = "mediatek,mt7621-wdt" },
94 {}
95};
96
97U_BOOT_DRIVER(mt762x_wdt) = {
98 .name = "mt762x_wdt",
99 .id = UCLASS_WDT,
100 .of_match = mt762x_wdt_ids,
101 .probe = mt762x_wdt_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700102 .priv_auto = sizeof(struct mt762x_wdt),
Stefan Roese053fce82018-10-04 13:39:07 +0200103 .ops = &mt762x_wdt_ops,
104};