Patrice Chotard | 473b244 | 2019-04-30 17:26:22 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2019, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 9 | #include <log.h> |
Patrice Chotard | 473b244 | 2019-04-30 17:26:22 +0200 | [diff] [blame] | 10 | #include <syscon.h> |
| 11 | #include <wdt.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <linux/iopoll.h> |
| 14 | |
| 15 | /* IWDG registers */ |
| 16 | #define IWDG_KR 0x00 /* Key register */ |
| 17 | #define IWDG_PR 0x04 /* Prescaler Register */ |
| 18 | #define IWDG_RLR 0x08 /* ReLoad Register */ |
| 19 | #define IWDG_SR 0x0C /* Status Register */ |
| 20 | |
| 21 | /* IWDG_KR register bit mask */ |
| 22 | #define KR_KEY_RELOAD 0xAAAA /* Reload counter enable */ |
| 23 | #define KR_KEY_ENABLE 0xCCCC /* Peripheral enable */ |
| 24 | #define KR_KEY_EWA 0x5555 /* Write access enable */ |
| 25 | |
| 26 | /* IWDG_PR register bit values */ |
| 27 | #define PR_256 0x06 /* Prescaler set to 256 */ |
| 28 | |
| 29 | /* IWDG_RLR register values */ |
| 30 | #define RLR_MAX 0xFFF /* Max value supported by reload register */ |
| 31 | |
| 32 | /* IWDG_SR register bit values */ |
| 33 | #define SR_PVU BIT(0) /* Watchdog prescaler value update */ |
| 34 | #define SR_RVU BIT(1) /* Watchdog counter reload value update */ |
| 35 | |
| 36 | struct stm32mp_wdt_priv { |
| 37 | fdt_addr_t base; /* registers addr in physical memory */ |
| 38 | unsigned long wdt_clk_rate; /* Watchdog dedicated clock rate */ |
| 39 | }; |
| 40 | |
| 41 | static int stm32mp_wdt_reset(struct udevice *dev) |
| 42 | { |
| 43 | struct stm32mp_wdt_priv *priv = dev_get_priv(dev); |
| 44 | |
| 45 | writel(KR_KEY_RELOAD, priv->base + IWDG_KR); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int stm32mp_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
| 51 | { |
| 52 | struct stm32mp_wdt_priv *priv = dev_get_priv(dev); |
| 53 | int reload; |
| 54 | u32 val; |
| 55 | int ret; |
| 56 | |
| 57 | /* Prescaler fixed to 256 */ |
| 58 | reload = timeout_ms * priv->wdt_clk_rate / 256; |
| 59 | if (reload > RLR_MAX + 1) |
| 60 | /* Force to max watchdog counter reload value */ |
| 61 | reload = RLR_MAX + 1; |
| 62 | else if (!reload) |
| 63 | /* Force to min watchdog counter reload value */ |
| 64 | reload = priv->wdt_clk_rate / 256; |
| 65 | |
| 66 | /* Set prescaler & reload registers */ |
| 67 | writel(KR_KEY_EWA, priv->base + IWDG_KR); |
| 68 | writel(PR_256, priv->base + IWDG_PR); |
| 69 | writel(reload - 1, priv->base + IWDG_RLR); |
| 70 | |
| 71 | /* Enable watchdog */ |
| 72 | writel(KR_KEY_ENABLE, priv->base + IWDG_KR); |
| 73 | |
| 74 | /* Wait for the registers to be updated */ |
| 75 | ret = readl_poll_timeout(priv->base + IWDG_SR, val, |
| 76 | val & (SR_PVU | SR_RVU), CONFIG_SYS_HZ); |
| 77 | |
| 78 | if (ret < 0) { |
| 79 | pr_err("Updating IWDG registers timeout"); |
| 80 | return -ETIMEDOUT; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int stm32mp_wdt_probe(struct udevice *dev) |
| 87 | { |
| 88 | struct stm32mp_wdt_priv *priv = dev_get_priv(dev); |
| 89 | struct clk clk; |
| 90 | int ret; |
| 91 | |
| 92 | debug("IWDG init\n"); |
| 93 | |
| 94 | priv->base = devfdt_get_addr(dev); |
| 95 | if (priv->base == FDT_ADDR_T_NONE) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | /* Enable clock */ |
| 99 | ret = clk_get_by_name(dev, "pclk", &clk); |
| 100 | if (ret) |
| 101 | return ret; |
| 102 | |
| 103 | ret = clk_enable(&clk); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | |
| 107 | /* Get LSI clock */ |
| 108 | ret = clk_get_by_name(dev, "lsi", &clk); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
| 112 | priv->wdt_clk_rate = clk_get_rate(&clk); |
| 113 | |
| 114 | debug("IWDG init done\n"); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static const struct wdt_ops stm32mp_wdt_ops = { |
| 120 | .start = stm32mp_wdt_start, |
| 121 | .reset = stm32mp_wdt_reset, |
| 122 | }; |
| 123 | |
| 124 | static const struct udevice_id stm32mp_wdt_match[] = { |
| 125 | { .compatible = "st,stm32mp1-iwdg" }, |
| 126 | { /* sentinel */ } |
| 127 | }; |
| 128 | |
| 129 | U_BOOT_DRIVER(stm32mp_wdt) = { |
| 130 | .name = "stm32mp-wdt", |
| 131 | .id = UCLASS_WDT, |
| 132 | .of_match = stm32mp_wdt_match, |
| 133 | .priv_auto_alloc_size = sizeof(struct stm32mp_wdt_priv), |
| 134 | .probe = stm32mp_wdt_probe, |
| 135 | .ops = &stm32mp_wdt_ops, |
| 136 | }; |