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