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