Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | eb51731 | 2018-10-01 12:22:45 -0600 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_SYSRESET |
| 8 | |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | 9a3b4ce | 2019-12-28 10:45:01 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <errno.h> |
Simon Glass | 4c66cb4 | 2020-12-23 08:11:14 -0700 | [diff] [blame] | 14 | #include <hang.h> |
| 15 | #include <log.h> |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 16 | #include <regmap.h> |
Simon Glass | 4c66cb4 | 2020-12-23 08:11:14 -0700 | [diff] [blame] | 17 | #include <spl.h> |
| 18 | #include <sysreset.h> |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
| 20 | #include <dm/lists.h> |
| 21 | #include <dm/root.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 23 | #include <linux/err.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 24 | #include <asm/global_data.h> |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 25 | |
| 26 | int sysreset_request(struct udevice *dev, enum sysreset_t type) |
| 27 | { |
| 28 | struct sysreset_ops *ops = sysreset_get_ops(dev); |
| 29 | |
| 30 | if (!ops->request) |
| 31 | return -ENOSYS; |
| 32 | |
| 33 | return ops->request(dev, type); |
| 34 | } |
| 35 | |
Mario Six | 245f5cd | 2018-08-06 10:23:32 +0200 | [diff] [blame] | 36 | int sysreset_get_status(struct udevice *dev, char *buf, int size) |
| 37 | { |
| 38 | struct sysreset_ops *ops = sysreset_get_ops(dev); |
| 39 | |
| 40 | if (!ops->get_status) |
| 41 | return -ENOSYS; |
| 42 | |
| 43 | return ops->get_status(dev, buf, size); |
| 44 | } |
| 45 | |
Simon Glass | 751fed4 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 46 | int sysreset_get_last(struct udevice *dev) |
| 47 | { |
| 48 | struct sysreset_ops *ops = sysreset_get_ops(dev); |
| 49 | |
| 50 | if (!ops->get_last) |
| 51 | return -ENOSYS; |
| 52 | |
| 53 | return ops->get_last(dev); |
| 54 | } |
| 55 | |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 56 | int sysreset_walk(enum sysreset_t type) |
| 57 | { |
| 58 | struct udevice *dev; |
| 59 | int ret = -ENOSYS; |
| 60 | |
| 61 | while (ret != -EINPROGRESS && type < SYSRESET_COUNT) { |
| 62 | for (uclass_first_device(UCLASS_SYSRESET, &dev); |
| 63 | dev; |
| 64 | uclass_next_device(&dev)) { |
| 65 | ret = sysreset_request(dev, type); |
| 66 | if (ret == -EINPROGRESS) |
| 67 | break; |
| 68 | } |
| 69 | type++; |
| 70 | } |
| 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
Simon Glass | 751fed4 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 75 | int sysreset_get_last_walk(void) |
| 76 | { |
| 77 | struct udevice *dev; |
| 78 | int value = -ENOENT; |
| 79 | |
| 80 | for (uclass_first_device(UCLASS_SYSRESET, &dev); |
| 81 | dev; |
| 82 | uclass_next_device(&dev)) { |
| 83 | int ret; |
| 84 | |
| 85 | ret = sysreset_get_last(dev); |
| 86 | if (ret >= 0) { |
| 87 | value = ret; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return value; |
| 93 | } |
| 94 | |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 95 | void sysreset_walk_halt(enum sysreset_t type) |
| 96 | { |
| 97 | int ret; |
| 98 | |
| 99 | ret = sysreset_walk(type); |
| 100 | |
| 101 | /* Wait for the reset to take effect */ |
| 102 | if (ret == -EINPROGRESS) |
| 103 | mdelay(100); |
| 104 | |
| 105 | /* Still no reset? Give up */ |
Simon Glass | 4c66cb4 | 2020-12-23 08:11:14 -0700 | [diff] [blame] | 106 | if (spl_phase() <= PHASE_SPL) |
| 107 | log_err("no sysreset\n"); |
| 108 | else |
| 109 | log_err("System reset not supported on this platform\n"); |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 110 | hang(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * reset_cpu() - calls sysreset_walk(SYSRESET_WARM) |
| 115 | */ |
Harald Seiler | 35b65dd | 2020-12-15 16:47:52 +0100 | [diff] [blame] | 116 | void reset_cpu(void) |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 117 | { |
| 118 | sysreset_walk_halt(SYSRESET_WARM); |
| 119 | } |
| 120 | |
| 121 | |
Bin Meng | 5f1a08b | 2021-02-25 17:22:52 +0800 | [diff] [blame] | 122 | #if IS_ENABLED(CONFIG_SYSRESET_CMD_RESET) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 123 | int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 124 | { |
Igor Opaniuk | a6713b3 | 2021-04-01 02:01:55 +0300 | [diff] [blame] | 125 | enum sysreset_t reset_type = SYSRESET_COLD; |
| 126 | |
| 127 | if (argc > 2) |
| 128 | return CMD_RET_USAGE; |
| 129 | |
| 130 | if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'w') { |
| 131 | reset_type = SYSRESET_WARM; |
| 132 | } |
| 133 | |
Bin Meng | 406be39 | 2018-07-19 03:07:31 -0700 | [diff] [blame] | 134 | printf("resetting ...\n"); |
Heinrich Schuchardt | e20a6e4 | 2020-07-29 12:13:41 +0200 | [diff] [blame] | 135 | mdelay(100); |
Bin Meng | 406be39 | 2018-07-19 03:07:31 -0700 | [diff] [blame] | 136 | |
Igor Opaniuk | a6713b3 | 2021-04-01 02:01:55 +0300 | [diff] [blame] | 137 | sysreset_walk_halt(reset_type); |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
Bin Meng | 5f1a08b | 2021-02-25 17:22:52 +0800 | [diff] [blame] | 141 | #endif |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 142 | |
Urja Rannikko | b805051 | 2019-05-16 21:48:42 +0000 | [diff] [blame] | 143 | #if IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 144 | int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Urja Rannikko | b805051 | 2019-05-16 21:48:42 +0000 | [diff] [blame] | 145 | { |
| 146 | int ret; |
| 147 | |
| 148 | puts("poweroff ...\n"); |
| 149 | mdelay(100); |
| 150 | |
| 151 | ret = sysreset_walk(SYSRESET_POWER_OFF); |
| 152 | |
| 153 | if (ret == -EINPROGRESS) |
| 154 | mdelay(1000); |
| 155 | |
| 156 | /*NOTREACHED when power off*/ |
| 157 | return CMD_RET_FAILURE; |
| 158 | } |
| 159 | #endif |
| 160 | |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 161 | UCLASS_DRIVER(sysreset) = { |
| 162 | .id = UCLASS_SYSRESET, |
| 163 | .name = "sysreset", |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 164 | }; |