Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2017 Google, Inc |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 9 | #include <wdt.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/wdt.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 13 | |
| 14 | #define WDT_AST2500 2500 |
| 15 | #define WDT_AST2400 2400 |
| 16 | |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 17 | struct ast_wdt_priv { |
| 18 | struct ast_wdt *regs; |
| 19 | }; |
| 20 | |
| 21 | static int ast_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
| 22 | { |
| 23 | struct ast_wdt_priv *priv = dev_get_priv(dev); |
| 24 | ulong driver_data = dev_get_driver_data(dev); |
| 25 | u32 reset_mode = ast_reset_mode_from_flags(flags); |
| 26 | |
Joel Stanley | 53e1d3c | 2019-06-06 17:08:45 +0930 | [diff] [blame] | 27 | /* 32 bits at 1MHz is 4294967ms */ |
| 28 | timeout = min_t(u64, timeout, 4294967); |
| 29 | |
| 30 | /* WDT counts in ticks of 1MHz clock. 1ms / 1e3 * 1e6 */ |
| 31 | timeout *= 1000; |
| 32 | |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 33 | clrsetbits_le32(&priv->regs->ctrl, |
| 34 | WDT_CTRL_RESET_MASK << WDT_CTRL_RESET_MODE_SHIFT, |
| 35 | reset_mode << WDT_CTRL_RESET_MODE_SHIFT); |
| 36 | |
| 37 | if (driver_data >= WDT_AST2500 && reset_mode == WDT_CTRL_RESET_SOC) |
| 38 | writel(ast_reset_mask_from_flags(flags), |
| 39 | &priv->regs->reset_mask); |
| 40 | |
| 41 | writel((u32) timeout, &priv->regs->counter_reload_val); |
| 42 | writel(WDT_COUNTER_RESTART_VAL, &priv->regs->counter_restart); |
| 43 | /* |
| 44 | * Setting CLK1MHZ bit is just for compatibility with ast2400 part. |
| 45 | * On ast2500 watchdog timer clock is fixed at 1MHz and the bit is |
| 46 | * read-only |
| 47 | */ |
| 48 | setbits_le32(&priv->regs->ctrl, |
| 49 | WDT_CTRL_EN | WDT_CTRL_RESET | WDT_CTRL_CLK1MHZ); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int ast_wdt_stop(struct udevice *dev) |
| 55 | { |
| 56 | struct ast_wdt_priv *priv = dev_get_priv(dev); |
| 57 | |
| 58 | clrbits_le32(&priv->regs->ctrl, WDT_CTRL_EN); |
| 59 | |
Cédric Le Goater | cb466db | 2018-10-16 13:57:11 +0200 | [diff] [blame] | 60 | writel(WDT_RESET_DEFAULT, &priv->regs->reset_mask); |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int ast_wdt_reset(struct udevice *dev) |
| 65 | { |
| 66 | struct ast_wdt_priv *priv = dev_get_priv(dev); |
| 67 | |
| 68 | writel(WDT_COUNTER_RESTART_VAL, &priv->regs->counter_restart); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int ast_wdt_expire_now(struct udevice *dev, ulong flags) |
| 74 | { |
| 75 | struct ast_wdt_priv *priv = dev_get_priv(dev); |
| 76 | int ret; |
| 77 | |
| 78 | ret = ast_wdt_start(dev, 1, flags); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | |
| 82 | while (readl(&priv->regs->ctrl) & WDT_CTRL_EN) |
| 83 | ; |
| 84 | |
| 85 | return ast_wdt_stop(dev); |
| 86 | } |
| 87 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 88 | static int ast_wdt_of_to_plat(struct udevice *dev) |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 89 | { |
| 90 | struct ast_wdt_priv *priv = dev_get_priv(dev); |
| 91 | |
Masahiro Yamada | 32822d0 | 2020-08-04 14:14:43 +0900 | [diff] [blame] | 92 | priv->regs = dev_read_addr_ptr(dev); |
Ovidiu Panait | a633f00 | 2020-08-03 22:17:35 +0300 | [diff] [blame] | 93 | if (!priv->regs) |
| 94 | return -EINVAL; |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static const struct wdt_ops ast_wdt_ops = { |
| 100 | .start = ast_wdt_start, |
| 101 | .reset = ast_wdt_reset, |
| 102 | .stop = ast_wdt_stop, |
| 103 | .expire_now = ast_wdt_expire_now, |
| 104 | }; |
| 105 | |
| 106 | static const struct udevice_id ast_wdt_ids[] = { |
| 107 | { .compatible = "aspeed,wdt", .data = WDT_AST2500 }, |
| 108 | { .compatible = "aspeed,ast2500-wdt", .data = WDT_AST2500 }, |
| 109 | { .compatible = "aspeed,ast2400-wdt", .data = WDT_AST2400 }, |
| 110 | {} |
| 111 | }; |
| 112 | |
| 113 | static int ast_wdt_probe(struct udevice *dev) |
| 114 | { |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 115 | debug("%s() wdt%u\n", __func__, dev_seq(dev)); |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 116 | ast_wdt_stop(dev); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | U_BOOT_DRIVER(ast_wdt) = { |
| 122 | .name = "ast_wdt", |
| 123 | .id = UCLASS_WDT, |
| 124 | .of_match = ast_wdt_ids, |
| 125 | .probe = ast_wdt_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 126 | .priv_auto = sizeof(struct ast_wdt_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 127 | .of_to_plat = ast_wdt_of_to_plat, |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 128 | .ops = &ast_wdt_ops, |
maxims@google.com | df35df2 | 2017-04-17 12:00:22 -0700 | [diff] [blame] | 129 | }; |