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 | |
Heinrich Schuchardt | 748cbaa | 2021-02-01 03:01:54 +0100 | [diff] [blame] | 53 | if (!devices) |
| 54 | return 0; |
| 55 | |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 56 | for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) { |
| 57 | int ret2 = reset_free(devices->reset + n); |
| 58 | |
| 59 | if (ret2 && !ret) |
| 60 | ret = ret2; |
| 61 | } |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 66 | static int sandbox_scmi_devices_probe(struct udevice *dev) |
| 67 | { |
| 68 | struct sandbox_scmi_device_priv *priv = dev_get_priv(dev); |
| 69 | int ret; |
| 70 | size_t n; |
| 71 | |
| 72 | priv->devices = (struct sandbox_scmi_devices){ |
| 73 | .clk = priv->clk, |
| 74 | .clk_count = SCMI_TEST_DEVICES_CLK_COUNT, |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 75 | .reset = priv->reset_ctl, |
| 76 | .reset_count = SCMI_TEST_DEVICES_RD_COUNT, |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | for (n = 0; n < SCMI_TEST_DEVICES_CLK_COUNT; n++) { |
| 80 | ret = clk_get_by_index(dev, n, priv->devices.clk + n); |
| 81 | if (ret) { |
| 82 | dev_err(dev, "%s: Failed on clk %zu\n", __func__, n); |
| 83 | return ret; |
| 84 | } |
| 85 | } |
| 86 | |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 87 | for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) { |
| 88 | ret = reset_get_by_index(dev, n, priv->devices.reset + n); |
| 89 | if (ret) { |
| 90 | dev_err(dev, "%s: Failed on reset %zu\n", __func__, n); |
| 91 | goto err_reset; |
| 92 | } |
| 93 | } |
| 94 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 95 | return 0; |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 96 | |
| 97 | err_reset: |
| 98 | for (; n > 0; n--) |
| 99 | reset_free(priv->devices.reset + n - 1); |
| 100 | |
| 101 | return ret; |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static const struct udevice_id sandbox_scmi_devices_ids[] = { |
| 105 | { .compatible = "sandbox,scmi-devices" }, |
| 106 | { } |
| 107 | }; |
| 108 | |
| 109 | U_BOOT_DRIVER(sandbox_scmi_devices) = { |
| 110 | .name = "sandbox-scmi_devices", |
| 111 | .id = UCLASS_MISC, |
| 112 | .of_match = sandbox_scmi_devices_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 113 | .priv_auto = sizeof(struct sandbox_scmi_device_priv), |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 114 | .remove = sandbox_scmi_devices_remove, |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 115 | .probe = sandbox_scmi_devices_probe, |
| 116 | }; |