Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2020, Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <dm.h> |
| 9 | #include <malloc.h> |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 10 | #include <reset.h> |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/scmi_test.h> |
| 13 | #include <dm/device_compat.h> |
| 14 | |
| 15 | /* |
| 16 | * Simulate to some extent a SCMI exchange. |
| 17 | * This drivers gets SCMI resources and offers API function to the |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 18 | * SCMI test sequence manipulate the resources, currently clock |
| 19 | * and reset controllers. |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #define SCMI_TEST_DEVICES_CLK_COUNT 3 |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 23 | #define SCMI_TEST_DEVICES_RD_COUNT 1 |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * struct sandbox_scmi_device_priv - Storage for device handles used by test |
| 27 | * @clk: Array of clock instances used by tests |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 28 | * @reset_clt: Array of the reset controller instances used by tests |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 29 | * @devices: Resources exposed by sandbox_scmi_devices_ctx() |
| 30 | */ |
| 31 | struct sandbox_scmi_device_priv { |
| 32 | struct clk clk[SCMI_TEST_DEVICES_CLK_COUNT]; |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 33 | struct reset_ctl reset_ctl[SCMI_TEST_DEVICES_RD_COUNT]; |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 34 | struct sandbox_scmi_devices devices; |
| 35 | }; |
| 36 | |
| 37 | struct sandbox_scmi_devices *sandbox_scmi_devices_ctx(struct udevice *dev) |
| 38 | { |
| 39 | struct sandbox_scmi_device_priv *priv = dev_get_priv(dev); |
| 40 | |
| 41 | if (priv) |
| 42 | return &priv->devices; |
| 43 | |
| 44 | return NULL; |
| 45 | } |
| 46 | |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 47 | static int sandbox_scmi_devices_remove(struct udevice *dev) |
| 48 | { |
| 49 | struct sandbox_scmi_devices *devices = sandbox_scmi_devices_ctx(dev); |
| 50 | int ret = 0; |
| 51 | size_t n; |
| 52 | |
| 53 | for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) { |
| 54 | int ret2 = reset_free(devices->reset + n); |
| 55 | |
| 56 | if (ret2 && !ret) |
| 57 | ret = ret2; |
| 58 | } |
| 59 | |
| 60 | return ret; |
| 61 | } |
| 62 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 63 | static int sandbox_scmi_devices_probe(struct udevice *dev) |
| 64 | { |
| 65 | struct sandbox_scmi_device_priv *priv = dev_get_priv(dev); |
| 66 | int ret; |
| 67 | size_t n; |
| 68 | |
| 69 | priv->devices = (struct sandbox_scmi_devices){ |
| 70 | .clk = priv->clk, |
| 71 | .clk_count = SCMI_TEST_DEVICES_CLK_COUNT, |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 72 | .reset = priv->reset_ctl, |
| 73 | .reset_count = SCMI_TEST_DEVICES_RD_COUNT, |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | for (n = 0; n < SCMI_TEST_DEVICES_CLK_COUNT; n++) { |
| 77 | ret = clk_get_by_index(dev, n, priv->devices.clk + n); |
| 78 | if (ret) { |
| 79 | dev_err(dev, "%s: Failed on clk %zu\n", __func__, n); |
| 80 | return ret; |
| 81 | } |
| 82 | } |
| 83 | |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 84 | for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) { |
| 85 | ret = reset_get_by_index(dev, n, priv->devices.reset + n); |
| 86 | if (ret) { |
| 87 | dev_err(dev, "%s: Failed on reset %zu\n", __func__, n); |
| 88 | goto err_reset; |
| 89 | } |
| 90 | } |
| 91 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 92 | return 0; |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 93 | |
| 94 | err_reset: |
| 95 | for (; n > 0; n--) |
| 96 | reset_free(priv->devices.reset + n - 1); |
| 97 | |
| 98 | return ret; |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static const struct udevice_id sandbox_scmi_devices_ids[] = { |
| 102 | { .compatible = "sandbox,scmi-devices" }, |
| 103 | { } |
| 104 | }; |
| 105 | |
| 106 | U_BOOT_DRIVER(sandbox_scmi_devices) = { |
| 107 | .name = "sandbox-scmi_devices", |
| 108 | .id = UCLASS_MISC, |
| 109 | .of_match = sandbox_scmi_devices_ids, |
| 110 | .priv_auto_alloc_size = sizeof(struct sandbox_scmi_device_priv), |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 111 | .remove = sandbox_scmi_devices_remove, |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 112 | .probe = sandbox_scmi_devices_probe, |
| 113 | }; |