Simon Glass | b9d33fa | 2015-07-06 12:54:28 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <reset.h> |
| 12 | #include <asm/state.h> |
| 13 | #include <asm/test.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | static int sandbox_warm_reset_request(struct udevice *dev, enum reset_t type) |
| 18 | { |
| 19 | struct sandbox_state *state = state_get_current(); |
| 20 | |
| 21 | switch (type) { |
| 22 | case RESET_WARM: |
| 23 | state->last_reset = type; |
| 24 | break; |
| 25 | default: |
| 26 | return -ENOSYS; |
| 27 | } |
| 28 | if (!state->reset_allowed[type]) |
| 29 | return -EACCES; |
| 30 | |
| 31 | return -EINPROGRESS; |
| 32 | } |
| 33 | |
| 34 | static int sandbox_reset_request(struct udevice *dev, enum reset_t type) |
| 35 | { |
| 36 | struct sandbox_state *state = state_get_current(); |
| 37 | |
| 38 | /* |
| 39 | * If we have a device tree, the device we created from platform data |
| 40 | * (see the U_BOOT_DEVICE() declaration below) should not do anything. |
| 41 | * If we are that device, return an error. |
| 42 | */ |
Simon Glass | 7bb91dd | 2015-10-03 11:21:16 -0600 | [diff] [blame] | 43 | if (state->fdt_fname && dev->of_offset == -1) |
Simon Glass | b9d33fa | 2015-07-06 12:54:28 -0600 | [diff] [blame] | 44 | return -ENODEV; |
| 45 | |
| 46 | switch (type) { |
| 47 | case RESET_COLD: |
| 48 | state->last_reset = type; |
| 49 | break; |
| 50 | case RESET_POWER: |
| 51 | state->last_reset = type; |
| 52 | if (!state->reset_allowed[type]) |
| 53 | return -EACCES; |
Simon Glass | 5010d98 | 2015-07-06 12:54:29 -0600 | [diff] [blame] | 54 | sandbox_exit(); |
Simon Glass | b9d33fa | 2015-07-06 12:54:28 -0600 | [diff] [blame] | 55 | break; |
| 56 | default: |
| 57 | return -ENOSYS; |
| 58 | } |
| 59 | if (!state->reset_allowed[type]) |
| 60 | return -EACCES; |
| 61 | |
| 62 | return -EINPROGRESS; |
| 63 | } |
| 64 | |
| 65 | static struct reset_ops sandbox_reset_ops = { |
| 66 | .request = sandbox_reset_request, |
| 67 | }; |
| 68 | |
| 69 | static const struct udevice_id sandbox_reset_ids[] = { |
| 70 | { .compatible = "sandbox,reset" }, |
| 71 | { } |
| 72 | }; |
| 73 | |
| 74 | U_BOOT_DRIVER(reset_sandbox) = { |
| 75 | .name = "reset_sandbox", |
| 76 | .id = UCLASS_RESET, |
| 77 | .of_match = sandbox_reset_ids, |
| 78 | .ops = &sandbox_reset_ops, |
| 79 | }; |
| 80 | |
| 81 | static struct reset_ops sandbox_warm_reset_ops = { |
| 82 | .request = sandbox_warm_reset_request, |
| 83 | }; |
| 84 | |
| 85 | static const struct udevice_id sandbox_warm_reset_ids[] = { |
| 86 | { .compatible = "sandbox,warm-reset" }, |
| 87 | { } |
| 88 | }; |
| 89 | |
| 90 | U_BOOT_DRIVER(warm_reset_sandbox) = { |
| 91 | .name = "warm_reset_sandbox", |
| 92 | .id = UCLASS_RESET, |
| 93 | .of_match = sandbox_warm_reset_ids, |
| 94 | .ops = &sandbox_warm_reset_ops, |
| 95 | }; |
| 96 | |
| 97 | /* This is here in case we don't have a device tree */ |
| 98 | U_BOOT_DEVICE(reset_sandbox_non_fdt) = { |
| 99 | .name = "reset_sandbox", |
| 100 | }; |