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