Jim Liu | e885d09 | 2022-04-01 17:59:39 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2022 Nuvoton Technology, Inc |
| 4 | */ |
| 5 | |
| 6 | #include <dm.h> |
| 7 | #include <errno.h> |
| 8 | #include <log.h> |
| 9 | #include <wdt.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/err.h> |
| 13 | |
| 14 | #define NPCM_WTCLK (BIT(10) | BIT(11)) /* Clock divider */ |
| 15 | #define NPCM_WTE BIT(7) /* Enable */ |
| 16 | #define NPCM_WTIE BIT(6) /* Enable irq */ |
| 17 | #define NPCM_WTIS (BIT(4) | BIT(5)) /* Interval selection */ |
| 18 | #define NPCM_WTIF BIT(3) /* Interrupt flag*/ |
| 19 | #define NPCM_WTRF BIT(2) /* Reset flag */ |
| 20 | #define NPCM_WTRE BIT(1) /* Reset enable */ |
| 21 | #define NPCM_WTR BIT(0) /* Reset counter */ |
| 22 | |
| 23 | struct npcm_wdt_priv { |
| 24 | void __iomem *regs; |
| 25 | }; |
| 26 | |
| 27 | static int npcm_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
| 28 | { |
| 29 | struct npcm_wdt_priv *priv = dev_get_priv(dev); |
| 30 | u32 time_out, val; |
| 31 | |
| 32 | time_out = (u32)(timeout_ms) / 1000; |
| 33 | if (time_out < 2) |
| 34 | val = 0x800; |
| 35 | else if (time_out < 3) |
| 36 | val = 0x420; |
| 37 | else if (time_out < 6) |
| 38 | val = 0x810; |
| 39 | else if (time_out < 11) |
| 40 | val = 0x430; |
| 41 | else if (time_out < 22) |
| 42 | val = 0x820; |
| 43 | else if (time_out < 44) |
| 44 | val = 0xc00; |
| 45 | else if (time_out < 87) |
| 46 | val = 0x830; |
| 47 | else if (time_out < 173) |
| 48 | val = 0xc10; |
| 49 | else if (time_out < 688) |
| 50 | val = 0xc20; |
| 51 | else |
| 52 | val = 0xc30; |
| 53 | |
| 54 | val |= NPCM_WTRE | NPCM_WTE | NPCM_WTR | NPCM_WTIE; |
| 55 | writel(val, priv->regs); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int npcm_wdt_stop(struct udevice *dev) |
| 61 | { |
| 62 | struct npcm_wdt_priv *priv = dev_get_priv(dev); |
| 63 | |
| 64 | writel(0, priv->regs); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int npcm_wdt_reset(struct udevice *dev) |
| 70 | { |
| 71 | struct npcm_wdt_priv *priv = dev_get_priv(dev); |
| 72 | |
| 73 | writel(NPCM_WTR | NPCM_WTRE | NPCM_WTE, priv->regs); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Jim Liu | 0ab55cb | 2022-09-13 14:19:24 +0800 | [diff] [blame] | 78 | static int npcm_wdt_expire_now(struct udevice *dev, ulong flags) |
| 79 | { |
| 80 | return npcm_wdt_reset(dev); |
| 81 | } |
| 82 | |
Jim Liu | e885d09 | 2022-04-01 17:59:39 +0800 | [diff] [blame] | 83 | static int npcm_wdt_of_to_plat(struct udevice *dev) |
| 84 | { |
| 85 | struct npcm_wdt_priv *priv = dev_get_priv(dev); |
| 86 | |
| 87 | priv->regs = dev_read_addr_ptr(dev); |
| 88 | if (!priv->regs) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static const struct wdt_ops npcm_wdt_ops = { |
Jim Liu | 0ab55cb | 2022-09-13 14:19:24 +0800 | [diff] [blame] | 95 | .expire_now = npcm_wdt_expire_now, |
Jim Liu | e885d09 | 2022-04-01 17:59:39 +0800 | [diff] [blame] | 96 | .start = npcm_wdt_start, |
| 97 | .reset = npcm_wdt_reset, |
| 98 | .stop = npcm_wdt_stop, |
| 99 | }; |
| 100 | |
| 101 | static const struct udevice_id npcm_wdt_ids[] = { |
| 102 | { .compatible = "nuvoton,npcm750-wdt" }, |
| 103 | { } |
| 104 | }; |
| 105 | |
| 106 | static int npcm_wdt_probe(struct udevice *dev) |
| 107 | { |
| 108 | debug("%s() wdt%u\n", __func__, dev_seq(dev)); |
| 109 | npcm_wdt_stop(dev); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | U_BOOT_DRIVER(npcm_wdt) = { |
| 115 | .name = "npcm_wdt", |
| 116 | .id = UCLASS_WDT, |
| 117 | .of_match = npcm_wdt_ids, |
| 118 | .probe = npcm_wdt_probe, |
| 119 | .priv_auto = sizeof(struct npcm_wdt_priv), |
| 120 | .of_to_plat = npcm_wdt_of_to_plat, |
| 121 | .ops = &npcm_wdt_ops, |
| 122 | }; |