Jason Li | dd2b30b | 2020-01-30 12:34:57 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Cortina-Access |
| 4 | * |
| 5 | */ |
| 6 | |
Jason Li | dd2b30b | 2020-01-30 12:34:57 -0800 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <hang.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <wdt.h> |
| 11 | #include <linux/bitops.h> |
| 12 | |
| 13 | #define CA_WDT_CTRL 0x00 |
| 14 | #define CA_WDT_PS 0x04 |
| 15 | #define CA_WDT_DIV 0x08 |
| 16 | #define CA_WDT_LD 0x0C |
| 17 | #define CA_WDT_LOADE 0x10 |
| 18 | #define CA_WDT_CNT 0x14 |
| 19 | #define CA_WDT_IE 0x18 |
| 20 | #define CA_WDT_INT 0x1C |
| 21 | #define CA_WDT_STAT 0x20 |
| 22 | |
| 23 | /* CA_WDT_CTRL */ |
| 24 | #define CTL_WDT_EN BIT(0) |
| 25 | #define CTL_WDT_RSTEN BIT(1) |
| 26 | #define CTL_WDT_CLK_SEL BIT(2) |
| 27 | /* CA_WDT_LOADE */ |
| 28 | #define WDT_UPD BIT(0) |
| 29 | #define WDT_UPD_PS BIT(1) |
| 30 | |
| 31 | /* Global config */ |
| 32 | #define WDT_RESET_SUB BIT(4) |
| 33 | #define WDT_RESET_ALL_BLOCK BIT(6) |
| 34 | #define WDT_RESET_REMAP BIT(7) |
| 35 | #define WDT_EXT_RESET BIT(8) |
| 36 | #define WDT_RESET_DEFAULT (WDT_EXT_RESET | WDT_RESET_REMAP | \ |
| 37 | WDT_RESET_ALL_BLOCK | WDT_RESET_SUB) |
| 38 | |
| 39 | struct ca_wdt_priv { |
| 40 | void __iomem *base; |
| 41 | void __iomem *global_config; |
| 42 | }; |
| 43 | |
| 44 | static void cortina_wdt_set_timeout(struct udevice *dev, u64 timeout_ms) |
| 45 | { |
| 46 | struct ca_wdt_priv *priv = dev_get_priv(dev); |
| 47 | |
| 48 | /* Prescale using millisecond unit */ |
| 49 | writel(CORTINA_PER_IO_FREQ / 1000, priv->base + CA_WDT_PS); |
| 50 | |
| 51 | /* Millisecond */ |
| 52 | writel(1, priv->base + CA_WDT_DIV); |
| 53 | |
| 54 | writel(timeout_ms, priv->base + CA_WDT_LD); |
| 55 | writel(WDT_UPD | WDT_UPD_PS, priv->base + CA_WDT_LOADE); |
| 56 | } |
| 57 | |
| 58 | static int cortina_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
| 59 | { |
| 60 | struct ca_wdt_priv *priv = dev_get_priv(dev); |
| 61 | |
| 62 | cortina_wdt_set_timeout(dev, timeout); |
| 63 | |
| 64 | /* WDT Reset option */ |
| 65 | setbits_32(priv->global_config, WDT_RESET_DEFAULT); |
| 66 | |
| 67 | /* Enable WDT */ |
| 68 | setbits_32(priv->base, CTL_WDT_EN | CTL_WDT_RSTEN | CTL_WDT_CLK_SEL); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int cortina_wdt_stop(struct udevice *dev) |
| 74 | { |
| 75 | struct ca_wdt_priv *priv = dev_get_priv(dev); |
| 76 | |
| 77 | /* Disable WDT */ |
| 78 | writel(0, priv->base); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int cortina_wdt_reset(struct udevice *dev) |
| 84 | { |
| 85 | struct ca_wdt_priv *priv = dev_get_priv(dev); |
| 86 | |
| 87 | /* Reload WDT counter */ |
| 88 | writel(WDT_UPD, priv->base + CA_WDT_LOADE); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int cortina_wdt_expire_now(struct udevice *dev, ulong flags) |
| 94 | { |
| 95 | /* Set 1ms timeout to reset system */ |
| 96 | cortina_wdt_set_timeout(dev, 1); |
| 97 | hang(); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int cortina_wdt_probe(struct udevice *dev) |
| 103 | { |
| 104 | struct ca_wdt_priv *priv = dev_get_priv(dev); |
| 105 | |
| 106 | priv->base = dev_remap_addr_index(dev, 0); |
| 107 | if (!priv->base) |
| 108 | return -ENOENT; |
| 109 | |
| 110 | priv->global_config = dev_remap_addr_index(dev, 1); |
| 111 | if (!priv->global_config) |
| 112 | return -ENOENT; |
| 113 | |
| 114 | /* Stop WDT */ |
| 115 | cortina_wdt_stop(dev); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static const struct wdt_ops cortina_wdt_ops = { |
| 121 | .start = cortina_wdt_start, |
| 122 | .reset = cortina_wdt_reset, |
| 123 | .stop = cortina_wdt_stop, |
| 124 | .expire_now = cortina_wdt_expire_now, |
| 125 | }; |
| 126 | |
| 127 | static const struct udevice_id cortina_wdt_ids[] = { |
| 128 | {.compatible = "cortina,ca-wdt"}, |
| 129 | {} |
| 130 | }; |
| 131 | |
| 132 | U_BOOT_DRIVER(cortina_wdt) = { |
| 133 | .name = "cortina_wdt", |
| 134 | .id = UCLASS_WDT, |
| 135 | .probe = cortina_wdt_probe, |
| 136 | .of_match = cortina_wdt_ids, |
| 137 | .ops = &cortina_wdt_ops, |
| 138 | }; |