aboutsummaryrefslogtreecommitdiff
path: root/platform/ext/target/musca_b1/services/src/tfm_platform_system.c
blob: 47eedb284d703d9b4c32e926db6f68aaf0f55c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include <stdbool.h>
#include "platform/include/tfm_platform_system.h"
#include "platform_description.h"
#include "target_cfg.h"
#include "device_definition.h"
#include "psa/client.h"
#include "tfm_secure_api.h"
#include "services/include/tfm_ioctl_api.h"

#ifndef TFM_PSA_API
/*!
 * \brief Verify access rights for memory addresses sent in io vectors
 *
 * \param[in] in_vec     Pointer to in_vec array, which contains pointer
 *                       to input arguments for the service
 * \param[in] out_vec    Pointer out_vec array, which contains pointer to
 *                       output data of the pin service
 *
 * \return Returns true if memory is accessible by the service
 */
static bool memory_addr_check(const psa_invec *in_vec,
                              const psa_outvec *out_vec)
{
    if ((in_vec->base != NULL) &&
        (tfm_core_memory_permission_check((void *)in_vec->base, in_vec->len,
                                        TFM_MEMORY_ACCESS_RO) == TFM_SUCCESS) &&
        (out_vec->base != NULL) &&
        (tfm_core_memory_permission_check((void *)out_vec->base, out_vec->len,
                                        TFM_MEMORY_ACCESS_RW) == TFM_SUCCESS)) {
        return true;
    } else {
        return false;
    }
}
#endif /* TFM_PSA_API */

void tfm_platform_hal_system_reset(void)
{
    __disable_irq();
    mpc_revert_non_secure_to_secure_cfg();

    NVIC->ICPR[0] = UINT32_MAX;         /* Clear all pending interrupts */
    NVIC->ICPR[1] = UINT32_MAX;         /* Clear all pending interrupts */
    NVIC->ICPR[2] = UINT32_MAX;         /* Clear all pending interrupts */
    NVIC->ICPR[3] = UINT32_MAX;         /* Clear all pending interrupts */
    NVIC->ICPR[4] = UINT32_MAX;         /* Clear all pending interrupts */
    NVIC->ICPR[5] = UINT32_MAX;         /* Clear all pending interrupts */
    NVIC->ICPR[6] = UINT32_MAX;         /* Clear all pending interrupts */
    NVIC->ICPR[7] = UINT32_MAX;         /* Clear all pending interrupts */

    NVIC_SystemReset();
}

static enum tfm_platform_err_t
musca_b1_pin_service(const psa_invec  *in_vec,
                     const psa_outvec *out_vec)
{
    struct tfm_pin_service_args_t *args;
    uint32_t *result;
    enum gpio_altfunc_t altfunc;
    enum pinmode_select_t pin_mode;

#ifndef TFM_PSA_API
    if (memory_addr_check(in_vec, out_vec) == false) {
        return TFM_PLATFORM_ERR_SYSTEM_ERROR;
    }
#endif /* TFM_PSA_API */

    if (in_vec->len != sizeof(struct tfm_pin_service_args_t) ||
                                         out_vec->len != sizeof(uint32_t)) {
        return TFM_PLATFORM_ERR_INVALID_PARAM;
    }

    args = (struct tfm_pin_service_args_t *)in_vec->base;
    result = (uint32_t *)out_vec->base;
    switch (args->type) {
    case TFM_PIN_SERVICE_TYPE_SET_ALTFUNC:
        altfunc = (enum gpio_altfunc_t)args->u.set_altfunc.alt_func;
        *result = musca_b1_scc_set_alt_func(&MUSCA_B1_SCC_DEV_S, altfunc,
                                            args->u.set_altfunc.pin_mask);
        break;
    case TFM_PIN_SERVICE_TYPE_SET_DEFAULT_IN:
        altfunc = (enum gpio_altfunc_t)args->u.set_altfunc.alt_func;
        *result = musca_b1_scc_set_default_in(&MUSCA_B1_SCC_DEV_S, altfunc,
                                   args->u.set_default_in.pin_value,
                                   args->u.set_default_in.default_in_value);
        break;
    case TFM_PIN_SERVICE_TYPE_SET_PIN_MODE:
        pin_mode = (enum pinmode_select_t)args->u.set_pin_mode.pin_mode;
        *result = musca_b1_scc_set_pinmode(&MUSCA_B1_SCC_DEV_S,
                                           args->u.set_pin_mode.pin_mask,
                                           pin_mode);
        break;
     default:
        *result = SCC_INVALID_ARG;
        break;
    }

    return TFM_PLATFORM_ERR_SUCCESS;
}

enum tfm_platform_err_t
tfm_platform_hal_gpio_service(const psa_invec  *in_vec,
                              const psa_outvec *out_vec)
{
    struct tfm_gpio_service_args_t *args;
    struct tfm_gpio_service_out_t *out;
    enum gpio_cmsdk_direction_t dir;
    /* Alternate function is configured through the SCC, this is not used
     * on Musca-B1, the default value is passed to the driver
     */
    enum gpio_cmsdk_altfunc_t altfunc = GPIO_CMSDK_MAIN_FUNC;

#ifndef TFM_PSA_API
    if (memory_addr_check(in_vec, out_vec) == false) {
            return TFM_PLATFORM_ERR_SYSTEM_ERROR;
    }
#endif /* TFM_PSA_API */

    if (in_vec->len != sizeof(struct tfm_gpio_service_args_t) ||
        out_vec->len != sizeof(struct tfm_gpio_service_out_t)) {
        return TFM_PLATFORM_ERR_INVALID_PARAM;
    }

    args = (struct tfm_gpio_service_args_t *)in_vec->base;
    out = (struct tfm_gpio_service_out_t *)out_vec->base;
    switch (args->type) {
    case TFM_GPIO_SERVICE_TYPE_INIT:
        gpio_cmsdk_init(&GPIO0_CMSDK_DEV_S);
        out->u.result = GPIO_CMSDK_ERR_NONE;
        break;
    case TFM_GPIO_SERVICE_TYPE_PIN_CONFIG:
        dir = (enum gpio_cmsdk_direction_t)args->u.gpio_config.direction;
        out->u.result = gpio_cmsdk_pin_config(
                                            &GPIO0_CMSDK_DEV_S,
                                            args->u.gpio_config.pin_num_or_mask,
                                            dir, altfunc);
        break;
    case TFM_GPIO_SERVICE_TYPE_PIN_WRITE:
        out->u.result = gpio_cmsdk_pin_write(
                                             &GPIO0_CMSDK_DEV_S,
                                             args->u.gpio_write.pin_num_or_mask,
                                             args->u.gpio_write.value);
        break;
    case TFM_GPIO_SERVICE_TYPE_PIN_READ:
        out->u.gpio_read_result.result =
                          gpio_cmsdk_pin_read(&GPIO0_CMSDK_DEV_S,
                                              args->u.gpio_read.pin_num_or_mask,
                                              &out->u.gpio_read_result.data);
        break;
    case TFM_GPIO_SERVICE_TYPE_PORT_CONFIG:
        dir = (enum gpio_cmsdk_direction_t)args->u.gpio_config.direction;
        out->u.result = gpio_cmsdk_port_config(
                                            &GPIO0_CMSDK_DEV_S,
                                            args->u.gpio_config.pin_num_or_mask,
                                            dir, altfunc);
        break;
    case TFM_GPIO_SERVICE_TYPE_PORT_WRITE:
        out->u.result = gpio_cmsdk_port_write(
                                             &GPIO0_CMSDK_DEV_S,
                                             args->u.gpio_write.pin_num_or_mask,
                                             args->u.gpio_write.value);
        break;
    case TFM_GPIO_SERVICE_TYPE_PORT_READ:
        out->u.gpio_read_result.result =
                         gpio_cmsdk_port_read(&GPIO0_CMSDK_DEV_S,
                                              args->u.gpio_read.pin_num_or_mask,
                                              &out->u.gpio_read_result.data);
        break;
    default:
        out->u.result = GPIO_CMSDK_ERR_INVALID_ARG;
        break;
    }

    in_vec++;
    out_vec++;

    return TFM_PLATFORM_ERR_SUCCESS;
}

enum tfm_platform_err_t tfm_platform_hal_ioctl(tfm_platform_ioctl_req_t request,
                                               psa_invec  *in_vec,
                                               psa_outvec *out_vec)
{
    switch (request) {
    case TFM_PLATFORM_IOCTL_PIN_SERVICE:
        return musca_b1_pin_service(in_vec, out_vec);
    case TFM_PLATFORM_IOCTL_GPIO_SERVICE:
        return tfm_platform_hal_gpio_service(in_vec, out_vec);
    default:
        return TFM_PLATFORM_ERR_NOT_SUPPORTED;
    }
}