Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 859f256 | 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 | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <sysreset.h> |
| 10 | #include <asm/state.h> |
| 11 | #include <asm/test.h> |
| 12 | |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 13 | static int sandbox_warm_sysreset_request(struct udevice *dev, |
| 14 | enum sysreset_t type) |
| 15 | { |
| 16 | struct sandbox_state *state = state_get_current(); |
| 17 | |
| 18 | switch (type) { |
| 19 | case SYSRESET_WARM: |
| 20 | state->last_sysreset = type; |
| 21 | break; |
| 22 | default: |
Paul Barker | bdb3a3f | 2023-11-08 08:51:10 +0000 | [diff] [blame] | 23 | return -EPROTONOSUPPORT; |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 24 | } |
| 25 | if (!state->sysreset_allowed[type]) |
| 26 | return -EACCES; |
| 27 | |
| 28 | return -EINPROGRESS; |
| 29 | } |
| 30 | |
Mario Six | b6da1fb | 2018-08-06 10:23:33 +0200 | [diff] [blame] | 31 | int sandbox_warm_sysreset_get_status(struct udevice *dev, char *buf, int size) |
| 32 | { |
| 33 | strlcpy(buf, "Reset Status: WARM", size); |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
Simon Glass | 07fbba5 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 38 | int sandbox_warm_sysreset_get_last(struct udevice *dev) |
| 39 | { |
| 40 | return SYSRESET_WARM; |
| 41 | } |
| 42 | |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 43 | static int sandbox_sysreset_request(struct udevice *dev, enum sysreset_t type) |
| 44 | { |
| 45 | struct sandbox_state *state = state_get_current(); |
| 46 | |
| 47 | /* |
| 48 | * If we have a device tree, the device we created from platform data |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 49 | * (see the U_BOOT_DRVINFO() declaration below) should not do anything. |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 50 | * If we are that device, return an error. |
| 51 | */ |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 52 | if (state->fdt_fname && !dev_has_ofnode(dev)) |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 53 | return -ENODEV; |
| 54 | |
| 55 | switch (type) { |
| 56 | case SYSRESET_COLD: |
| 57 | state->last_sysreset = type; |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 58 | if (!state->sysreset_allowed[type]) |
| 59 | return -EACCES; |
| 60 | sandbox_reset(); |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 61 | break; |
Urja Rannikko | 25394e7 | 2019-05-16 21:48:41 +0000 | [diff] [blame] | 62 | case SYSRESET_POWER_OFF: |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 63 | state->last_sysreset = type; |
| 64 | if (!state->sysreset_allowed[type]) |
| 65 | return -EACCES; |
| 66 | sandbox_exit(); |
Urja Rannikko | 25394e7 | 2019-05-16 21:48:41 +0000 | [diff] [blame] | 67 | case SYSRESET_POWER: |
Simon Glass | 07fbba5 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 68 | if (!state->sysreset_allowed[type]) |
| 69 | return -EACCES; |
Simon Glass | 2fe5b91 | 2019-05-18 11:59:43 -0600 | [diff] [blame] | 70 | sandbox_exit(); |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 71 | default: |
Paul Barker | bdb3a3f | 2023-11-08 08:51:10 +0000 | [diff] [blame] | 72 | return -EPROTONOSUPPORT; |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 73 | } |
| 74 | if (!state->sysreset_allowed[type]) |
| 75 | return -EACCES; |
| 76 | |
| 77 | return -EINPROGRESS; |
| 78 | } |
| 79 | |
Mario Six | b6da1fb | 2018-08-06 10:23:33 +0200 | [diff] [blame] | 80 | int sandbox_sysreset_get_status(struct udevice *dev, char *buf, int size) |
| 81 | { |
| 82 | strlcpy(buf, "Reset Status: COLD", size); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Simon Glass | 07fbba5 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 87 | int sandbox_sysreset_get_last(struct udevice *dev) |
| 88 | { |
Simon Glass | 7e4d49c | 2018-11-23 21:29:28 -0700 | [diff] [blame] | 89 | struct sandbox_state *state = state_get_current(); |
| 90 | |
| 91 | /* |
| 92 | * The first phase is a power reset, after that we assume we don't |
| 93 | * know. |
| 94 | */ |
| 95 | return state->jumped_fname ? SYSRESET_WARM : SYSRESET_POWER; |
Simon Glass | 07fbba5 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 98 | static struct sysreset_ops sandbox_sysreset_ops = { |
| 99 | .request = sandbox_sysreset_request, |
Mario Six | b6da1fb | 2018-08-06 10:23:33 +0200 | [diff] [blame] | 100 | .get_status = sandbox_sysreset_get_status, |
Simon Glass | 07fbba5 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 101 | .get_last = sandbox_sysreset_get_last, |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | static const struct udevice_id sandbox_sysreset_ids[] = { |
| 105 | { .compatible = "sandbox,reset" }, |
| 106 | { } |
| 107 | }; |
| 108 | |
| 109 | U_BOOT_DRIVER(sysreset_sandbox) = { |
| 110 | .name = "sysreset_sandbox", |
| 111 | .id = UCLASS_SYSRESET, |
| 112 | .of_match = sandbox_sysreset_ids, |
| 113 | .ops = &sandbox_sysreset_ops, |
| 114 | }; |
| 115 | |
| 116 | static struct sysreset_ops sandbox_warm_sysreset_ops = { |
| 117 | .request = sandbox_warm_sysreset_request, |
Mario Six | b6da1fb | 2018-08-06 10:23:33 +0200 | [diff] [blame] | 118 | .get_status = sandbox_warm_sysreset_get_status, |
Simon Glass | 07fbba5 | 2018-10-01 12:22:46 -0600 | [diff] [blame] | 119 | .get_last = sandbox_warm_sysreset_get_last, |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static const struct udevice_id sandbox_warm_sysreset_ids[] = { |
| 123 | { .compatible = "sandbox,warm-reset" }, |
| 124 | { } |
| 125 | }; |
| 126 | |
| 127 | U_BOOT_DRIVER(warm_sysreset_sandbox) = { |
| 128 | .name = "warm_sysreset_sandbox", |
| 129 | .id = UCLASS_SYSRESET, |
| 130 | .of_match = sandbox_warm_sysreset_ids, |
| 131 | .ops = &sandbox_warm_sysreset_ops, |
| 132 | }; |
| 133 | |
Sean Anderson | 7bf2f8c | 2023-10-14 16:47:46 -0400 | [diff] [blame] | 134 | #if CONFIG_IS_ENABLED(OF_REAL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 135 | /* This is here in case we don't have a device tree */ |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 136 | U_BOOT_DRVINFO(sysreset_sandbox_non_fdt) = { |
Stephen Warren | 859f256 | 2016-05-12 12:03:35 -0600 | [diff] [blame] | 137 | .name = "sysreset_sandbox", |
| 138 | }; |
Simon Glass | cb90bd3 | 2020-10-03 11:31:23 -0600 | [diff] [blame] | 139 | #endif |