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> |
| 24 | |
| 25 | int sysreset_request(struct udevice *dev, enum sysreset_t type) |
| 26 | { |
| 27 | struct sysreset_ops *ops = sysreset_get_ops(dev); |
| 28 | |
| 29 | if (!ops->request) |
| 30 | return -ENOSYS; |
| 31 | |
| 32 | return ops->request(dev, type); |
| 33 | } |
| 34 | |
Mario Six | 245f5cd | 2018-08-06 10:23:32 +0200 | [diff] [blame] | 35 | int sysreset_get_status(struct udevice *dev, char *buf, int size) |
| 36 | { |
| 37 | struct sysreset_ops *ops = sysreset_get_ops(dev); |
| 38 | |
| 39 | if (!ops->get_status) |
| 40 | return -ENOSYS; |
| 41 | |
| 42 | return ops->get_status(dev, buf, size); |
| 43 | } |
| 44 | |
Simon Glass | 751fed4 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 45 | int sysreset_get_last(struct udevice *dev) |
| 46 | { |
| 47 | struct sysreset_ops *ops = sysreset_get_ops(dev); |
| 48 | |
| 49 | if (!ops->get_last) |
| 50 | return -ENOSYS; |
| 51 | |
| 52 | return ops->get_last(dev); |
| 53 | } |
| 54 | |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 55 | int sysreset_walk(enum sysreset_t type) |
| 56 | { |
| 57 | struct udevice *dev; |
| 58 | int ret = -ENOSYS; |
| 59 | |
| 60 | while (ret != -EINPROGRESS && type < SYSRESET_COUNT) { |
| 61 | for (uclass_first_device(UCLASS_SYSRESET, &dev); |
| 62 | dev; |
| 63 | uclass_next_device(&dev)) { |
| 64 | ret = sysreset_request(dev, type); |
| 65 | if (ret == -EINPROGRESS) |
| 66 | break; |
| 67 | } |
| 68 | type++; |
| 69 | } |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
Simon Glass | 751fed4 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 74 | int sysreset_get_last_walk(void) |
| 75 | { |
| 76 | struct udevice *dev; |
| 77 | int value = -ENOENT; |
| 78 | |
| 79 | for (uclass_first_device(UCLASS_SYSRESET, &dev); |
| 80 | dev; |
| 81 | uclass_next_device(&dev)) { |
| 82 | int ret; |
| 83 | |
| 84 | ret = sysreset_get_last(dev); |
| 85 | if (ret >= 0) { |
| 86 | value = ret; |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return value; |
| 92 | } |
| 93 | |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 94 | void sysreset_walk_halt(enum sysreset_t type) |
| 95 | { |
| 96 | int ret; |
| 97 | |
| 98 | ret = sysreset_walk(type); |
| 99 | |
| 100 | /* Wait for the reset to take effect */ |
| 101 | if (ret == -EINPROGRESS) |
| 102 | mdelay(100); |
| 103 | |
| 104 | /* Still no reset? Give up */ |
Simon Glass | 4c66cb4 | 2020-12-23 08:11:14 -0700 | [diff] [blame] | 105 | if (spl_phase() <= PHASE_SPL) |
| 106 | log_err("no sysreset\n"); |
| 107 | else |
| 108 | log_err("System reset not supported on this platform\n"); |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 109 | hang(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * reset_cpu() - calls sysreset_walk(SYSRESET_WARM) |
| 114 | */ |
| 115 | void reset_cpu(ulong addr) |
| 116 | { |
| 117 | sysreset_walk_halt(SYSRESET_WARM); |
| 118 | } |
| 119 | |
| 120 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 121 | 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] | 122 | { |
Bin Meng | 406be39 | 2018-07-19 03:07:31 -0700 | [diff] [blame] | 123 | printf("resetting ...\n"); |
Heinrich Schuchardt | e20a6e4 | 2020-07-29 12:13:41 +0200 | [diff] [blame] | 124 | mdelay(100); |
Bin Meng | 406be39 | 2018-07-19 03:07:31 -0700 | [diff] [blame] | 125 | |
Philipp Tomsich | b53f6992 | 2017-11-24 18:37:58 +0100 | [diff] [blame] | 126 | sysreset_walk_halt(SYSRESET_COLD); |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Urja Rannikko | b805051 | 2019-05-16 21:48:42 +0000 | [diff] [blame] | 131 | #if IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 132 | 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] | 133 | { |
| 134 | int ret; |
| 135 | |
| 136 | puts("poweroff ...\n"); |
| 137 | mdelay(100); |
| 138 | |
| 139 | ret = sysreset_walk(SYSRESET_POWER_OFF); |
| 140 | |
| 141 | if (ret == -EINPROGRESS) |
| 142 | mdelay(1000); |
| 143 | |
| 144 | /*NOTREACHED when power off*/ |
| 145 | return CMD_RET_FAILURE; |
| 146 | } |
| 147 | #endif |
| 148 | |
Michal Simek | 758de97 | 2018-07-12 10:36:07 +0200 | [diff] [blame] | 149 | static int sysreset_post_bind(struct udevice *dev) |
| 150 | { |
| 151 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 152 | struct sysreset_ops *ops = sysreset_get_ops(dev); |
| 153 | static int reloc_done; |
| 154 | |
| 155 | if (!reloc_done) { |
| 156 | if (ops->request) |
| 157 | ops->request += gd->reloc_off; |
| 158 | reloc_done++; |
| 159 | } |
| 160 | #endif |
| 161 | return 0; |
| 162 | } |
| 163 | |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 164 | UCLASS_DRIVER(sysreset) = { |
| 165 | .id = UCLASS_SYSRESET, |
| 166 | .name = "sysreset", |
Michal Simek | 758de97 | 2018-07-12 10:36:07 +0200 | [diff] [blame] | 167 | .post_bind = sysreset_post_bind, |
Stephen Warren | 1163625 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 168 | }; |