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