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