|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#define DT_DRV_COMPAT nordic_nrf_vevif_event_rx |
| 7 | + |
| 8 | +#include <zephyr/devicetree.h> |
| 9 | +#include <zephyr/drivers/mbox.h> |
| 10 | + |
| 11 | +#include <haly/nrfy_vpr.h> |
| 12 | + |
| 13 | +#if defined(CONFIG_SOC_NRF54L15_ENGA_CPUAPP) |
| 14 | +#define EVENTS_IDX_MIN 11U |
| 15 | +#define EVENTS_IDX_MAX 17U |
| 16 | +#else |
| 17 | +#define EVENTS_IDX_MIN NRF_VPR_EVENTS_TRIGGERED_MIN |
| 18 | +#define EVENTS_IDX_MAX NRF_VPR_EVENTS_TRIGGERED_MAX |
| 19 | +#endif |
| 20 | + |
| 21 | +/* callbacks */ |
| 22 | +struct mbox_vevif_event_rx_cbs { |
| 23 | + mbox_callback_t cb[EVENTS_IDX_MAX - EVENTS_IDX_MIN + 1U]; |
| 24 | + void *user_data[EVENTS_IDX_MAX - EVENTS_IDX_MIN + 1U]; |
| 25 | + uint32_t enabled_mask; |
| 26 | +}; |
| 27 | + |
| 28 | +struct mbox_vevif_event_rx_conf { |
| 29 | + NRF_VPR_Type *vpr; |
| 30 | + uint32_t events_mask; |
| 31 | + uint8_t events; |
| 32 | + void (*irq_connect)(void); |
| 33 | +}; |
| 34 | + |
| 35 | +static void vevif_event_rx_isr(const void *device) |
| 36 | +{ |
| 37 | + const struct device *dev = (struct device *)device; |
| 38 | + const struct mbox_vevif_event_rx_conf *config = dev->config; |
| 39 | + struct mbox_vevif_event_rx_cbs *cbs = dev->data; |
| 40 | + |
| 41 | + for (uint8_t id = EVENTS_IDX_MIN; id < EVENTS_IDX_MAX + 1U; id++) { |
| 42 | + nrf_vpr_event_t event = nrfy_vpr_triggered_event_get(id); |
| 43 | + |
| 44 | + if (nrfy_vpr_event_check(config->vpr, event)) { |
| 45 | + nrfy_vpr_event_clear(config->vpr, event); |
| 46 | + uint8_t idx = id - EVENTS_IDX_MIN; |
| 47 | + |
| 48 | + if ((cbs->enabled_mask & BIT(id)) && (cbs->cb[idx] != NULL)) { |
| 49 | + cbs->cb[idx](dev, id, cbs->user_data[idx], NULL); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +static inline bool vevif_event_rx_event_is_valid(uint32_t events_mask, uint32_t id) |
| 56 | +{ |
| 57 | + return ((id <= EVENTS_IDX_MAX) && ((events_mask & BIT(id)) != 0U)); |
| 58 | +} |
| 59 | + |
| 60 | +static uint32_t vevif_event_rx_max_channels_get(const struct device *dev) |
| 61 | +{ |
| 62 | + const struct mbox_vevif_event_rx_conf *config = dev->config; |
| 63 | + |
| 64 | + return config->events; |
| 65 | +} |
| 66 | + |
| 67 | +static int vevif_event_rx_register_callback(const struct device *dev, uint32_t id, |
| 68 | + mbox_callback_t cb, void *user_data) |
| 69 | +{ |
| 70 | + const struct mbox_vevif_event_rx_conf *config = dev->config; |
| 71 | + struct mbox_vevif_event_rx_cbs *cbs = dev->data; |
| 72 | + uint8_t idx = id - EVENTS_IDX_MIN; |
| 73 | + |
| 74 | + if (!vevif_event_rx_event_is_valid(config->events_mask, id)) { |
| 75 | + return -EINVAL; |
| 76 | + } |
| 77 | + |
| 78 | + cbs->cb[idx] = cb; |
| 79 | + cbs->user_data[idx] = user_data; |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +static int vevif_event_rx_set_enabled(const struct device *dev, uint32_t id, bool enable) |
| 85 | +{ |
| 86 | + const struct mbox_vevif_event_rx_conf *config = dev->config; |
| 87 | + struct mbox_vevif_event_rx_cbs *cbs = dev->data; |
| 88 | + |
| 89 | + if (!vevif_event_rx_event_is_valid(config->events_mask, id)) { |
| 90 | + return -EINVAL; |
| 91 | + } |
| 92 | + |
| 93 | + if (enable) { |
| 94 | + if ((cbs->enabled_mask & BIT(id)) != 0U) { |
| 95 | + return -EALREADY; |
| 96 | + } |
| 97 | + |
| 98 | + cbs->enabled_mask |= BIT(id); |
| 99 | + nrfy_vpr_int_enable(config->vpr, BIT(id)); |
| 100 | + } else { |
| 101 | + if ((cbs->enabled_mask & BIT(id)) == 0U) { |
| 102 | + return -EALREADY; |
| 103 | + } |
| 104 | + |
| 105 | + cbs->enabled_mask &= ~BIT(id); |
| 106 | + nrfy_vpr_int_disable(config->vpr, BIT(id)); |
| 107 | + } |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +static const struct mbox_driver_api vevif_event_rx_driver_api = { |
| 113 | + .max_channels_get = vevif_event_rx_max_channels_get, |
| 114 | + .register_callback = vevif_event_rx_register_callback, |
| 115 | + .set_enabled = vevif_event_rx_set_enabled, |
| 116 | +}; |
| 117 | + |
| 118 | +static int vevif_event_rx_init(const struct device *dev) |
| 119 | +{ |
| 120 | + const struct mbox_vevif_event_rx_conf *config = dev->config; |
| 121 | + |
| 122 | + config->irq_connect(); |
| 123 | + |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +#define VEVIF_EVENT_RX_DEFINE(inst) \ |
| 128 | + BUILD_ASSERT(DT_INST_PROP(inst, nordic_events) <= NRF_VPR_EVENTS_TRIGGERED_COUNT, \ |
| 129 | + "Number of events exceeds maximum"); \ |
| 130 | + \ |
| 131 | + static void irq_connect##inst(void) \ |
| 132 | + { \ |
| 133 | + IRQ_CONNECT(DT_IRQN(DT_DRV_INST(inst)), DT_IRQ(DT_DRV_INST(inst), priority), \ |
| 134 | + vevif_event_rx_isr, (const void *)DEVICE_DT_GET(DT_DRV_INST(inst)), \ |
| 135 | + 0); \ |
| 136 | + irq_enable(DT_IRQN(DT_DRV_INST(inst))); \ |
| 137 | + }; \ |
| 138 | + \ |
| 139 | + static struct mbox_vevif_event_rx_cbs data##inst = { \ |
| 140 | + .enabled_mask = 0, \ |
| 141 | + }; \ |
| 142 | + static const struct mbox_vevif_event_rx_conf conf##inst = { \ |
| 143 | + .vpr = (NRF_VPR_Type *)DT_INST_REG_ADDR(inst), \ |
| 144 | + .events = DT_INST_PROP(inst, nordic_events), \ |
| 145 | + .events_mask = DT_INST_PROP(inst, nordic_events_mask), \ |
| 146 | + .irq_connect = irq_connect##inst, \ |
| 147 | + }; \ |
| 148 | + \ |
| 149 | + DEVICE_DT_INST_DEFINE(inst, vevif_event_rx_init, NULL, &data##inst, &conf##inst, \ |
| 150 | + POST_KERNEL, CONFIG_MBOX_INIT_PRIORITY, &vevif_event_rx_driver_api); |
| 151 | + |
| 152 | +DT_INST_FOREACH_STATUS_OKAY(VEVIF_EVENT_RX_DEFINE) |
0 commit comments