Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Christophe Leroy | 8906764 | 2018-03-16 17:21:01 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2017 CS Systemes d'Information |
Christophe Leroy | 8906764 | 2018-03-16 17:21:01 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Charles Frey | 3b44628 | 2020-02-19 16:50:15 +0000 | [diff] [blame] | 7 | #include <env.h> |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <wdt.h> |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 10 | #include <clock_legacy.h> |
Christophe Leroy | 8906764 | 2018-03-16 17:21:01 +0100 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 13 | struct mpc8xxx_wdt { |
| 14 | __be32 res0; |
| 15 | __be32 swcrr; /* System watchdog control register */ |
| 16 | #define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */ |
| 17 | #define SWCRR_BME 0x00000080 /* Bus monitor enable (mpc8xx) */ |
| 18 | #define SWCRR_SWF 0x00000008 /* Software Watchdog Freeze (mpc8xx). */ |
| 19 | #define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */ |
| 20 | #define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/ |
| 21 | #define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */ |
| 22 | __be32 swcnr; /* System watchdog count register */ |
| 23 | u8 res1[2]; |
| 24 | __be16 swsrr; /* System watchdog service register */ |
| 25 | u8 res2[0xf0]; |
| 26 | }; |
| 27 | |
| 28 | struct mpc8xxx_wdt_priv { |
| 29 | struct mpc8xxx_wdt __iomem *base; |
| 30 | }; |
| 31 | |
| 32 | static int mpc8xxx_wdt_reset(struct udevice *dev) |
Christophe Leroy | 8906764 | 2018-03-16 17:21:01 +0100 | [diff] [blame] | 33 | { |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 34 | struct mpc8xxx_wdt_priv *priv = dev_get_priv(dev); |
| 35 | |
| 36 | out_be16(&priv->base->swsrr, 0x556c); /* write magic1 */ |
| 37 | out_be16(&priv->base->swsrr, 0xaa39); /* write magic2 */ |
Christophe Leroy | 8906764 | 2018-03-16 17:21:01 +0100 | [diff] [blame] | 38 | |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 39 | return 0; |
Christophe Leroy | 8906764 | 2018-03-16 17:21:01 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Christophe Leroy | 1919fce | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 42 | static int mpc8xxx_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 43 | { |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 44 | struct mpc8xxx_wdt_priv *priv = dev_get_priv(dev); |
Charles Frey | 3b44628 | 2020-02-19 16:50:15 +0000 | [diff] [blame] | 45 | const char *mode = env_get("watchdog_mode"); |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 46 | ulong prescaler = dev_get_driver_data(dev); |
| 47 | u16 swtc = min_t(u16, timeout * get_board_sys_clk() / 1000 / prescaler, U16_MAX); |
| 48 | u32 val; |
| 49 | |
| 50 | mpc8xxx_wdt_reset(dev); |
Charles Frey | 3b44628 | 2020-02-19 16:50:15 +0000 | [diff] [blame] | 51 | |
| 52 | if (strcmp(mode, "off") == 0) |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 53 | val = (swtc << 16) | SWCRR_SWPR; |
Charles Frey | 3b44628 | 2020-02-19 16:50:15 +0000 | [diff] [blame] | 54 | else if (strcmp(mode, "nmi") == 0) |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 55 | val = (swtc << 16) | SWCRR_SWPR | SWCRR_SWEN; |
| 56 | else |
| 57 | val = (swtc << 16) | SWCRR_SWPR | SWCRR_SWEN | SWCRR_SWRI; |
| 58 | |
| 59 | if (IS_ENABLED(CONFIG_WDT_MPC8xxx_BME)) |
| 60 | val |= (CONFIG_WDT_MPC8xxx_BMT << 8) | SWCRR_BME; |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 61 | |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 62 | out_be32(&priv->base->swcrr, val); |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 63 | |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 64 | if (!(in_be32(&priv->base->swcrr) & SWCRR_SWEN)) |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 65 | return -EBUSY; |
| 66 | return 0; |
| 67 | |
| 68 | } |
| 69 | |
Christophe Leroy | 1919fce | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 70 | static int mpc8xxx_wdt_stop(struct udevice *dev) |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 71 | { |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 72 | struct mpc8xxx_wdt_priv *priv = dev_get_priv(dev); |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 73 | |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 74 | clrbits_be32(&priv->base->swcrr, SWCRR_SWEN); |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 75 | |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 76 | if (in_be32(&priv->base->swcrr) & SWCRR_SWEN) |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 77 | return -EBUSY; |
| 78 | return 0; |
| 79 | } |
| 80 | |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 81 | static int mpc8xxx_wdt_of_to_plat(struct udevice *dev) |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 82 | { |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 83 | struct mpc8xxx_wdt_priv *priv = dev_get_priv(dev); |
| 84 | |
| 85 | priv->base = (void __iomem *)devfdt_remap_addr(dev); |
| 86 | |
| 87 | if (!priv->base) |
| 88 | return -EINVAL; |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Christophe Leroy | 1919fce | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 93 | static const struct wdt_ops mpc8xxx_wdt_ops = { |
| 94 | .start = mpc8xxx_wdt_start, |
| 95 | .reset = mpc8xxx_wdt_reset, |
| 96 | .stop = mpc8xxx_wdt_stop, |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Christophe Leroy | 1919fce | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 99 | static const struct udevice_id mpc8xxx_wdt_ids[] = { |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 100 | { .compatible = "fsl,pq1-wdt", .data = 0x800 }, |
Christophe Leroy | 39d3795 | 2023-04-03 10:39:59 +0200 | [diff] [blame] | 101 | { .compatible = "fsl,pq2pro-wdt", .data = 0x10000 }, |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 102 | {} |
| 103 | }; |
| 104 | |
Christophe Leroy | 1919fce | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 105 | U_BOOT_DRIVER(wdt_mpc8xxx) = { |
| 106 | .name = "wdt_mpc8xxx", |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 107 | .id = UCLASS_WDT, |
Christophe Leroy | 1919fce | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 108 | .of_match = mpc8xxx_wdt_ids, |
| 109 | .ops = &mpc8xxx_wdt_ops, |
Christophe Leroy | 0fe1a20 | 2023-04-03 10:27:39 +0200 | [diff] [blame] | 110 | .of_to_plat = mpc8xxx_wdt_of_to_plat, |
| 111 | .priv_auto = sizeof(struct mpc8xxx_wdt_priv), |
Christophe Leroy | 4015930 | 2018-11-21 08:51:45 +0000 | [diff] [blame] | 112 | }; |