Skip to content

Commit d01b142

Browse files
mif1-nordicmasz-nordic
authored andcommitted
drivers: mspi: Reduced IPC structures-drivers
Implemented smaller structures and reduced ammount of opcodes in IPC. Added support for multiple target devices. Signed-off-by: Michal Frankiewicz <michal.frankiewicz@nordicsemi.no>
1 parent b1a37c0 commit d01b142

File tree

2 files changed

+97
-121
lines changed

2 files changed

+97
-121
lines changed

drivers/mspi/mspi_nrfe.c

+49-88
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ static atomic_t ipc_atomic_sem = ATOMIC_INIT(0);
6565
.sw_multi_periph = false, \
6666
}
6767

68-
struct mspi_nrfe_data {
69-
struct mspi_xfer xfer;
70-
struct mspi_dev_id dev_id;
71-
struct mspi_dev_cfg dev_cfg;
72-
};
73-
74-
static struct mspi_nrfe_data dev_data;
75-
7668
struct mspi_nrfe_config {
7769
struct mspi_cfg mspicfg;
7870
const struct pinctrl_dev_config *pcfg;
@@ -83,7 +75,7 @@ static const struct mspi_nrfe_config dev_config = {
8375
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0),
8476
};
8577

86-
static void ipc_recv_clbk(const void *data, size_t len);
78+
static void ep_recv(const void *data, size_t len, void *priv);
8779

8880
static void ep_bound(void *priv)
8981
{
@@ -96,18 +88,8 @@ static void ep_bound(void *priv)
9688
LOG_DBG("Ep bounded");
9789
}
9890

99-
static void ep_recv(const void *data, size_t len, void *priv)
100-
{
101-
(void)priv;
102-
103-
ipc_recv_clbk(data, len);
104-
}
105-
10691
static struct ipc_ept_cfg ep_cfg = {
107-
.cb = {
108-
.bound = ep_bound,
109-
.received = ep_recv,
110-
},
92+
.cb = {.bound = ep_bound, .received = ep_recv},
11193
};
11294

11395
/**
@@ -120,24 +102,16 @@ static struct ipc_ept_cfg ep_cfg = {
120102
* @param data Pointer to the received message.
121103
* @param len Length of the received message.
122104
*/
123-
static void ipc_recv_clbk(const void *data, size_t len)
105+
static void ep_recv(const void *data, size_t len, void *priv)
124106
{
125-
nrfe_mspi_flpr_response_t *response = (nrfe_mspi_flpr_response_t *)data;
107+
nrfe_mspi_flpr_response_msg_t *response = (nrfe_mspi_flpr_response_msg_t *)data;
126108

127109
switch (response->opcode) {
128110
case NRFE_MSPI_CONFIG_PINS: {
129111
#if defined(CONFIG_MULTITHREADING)
130112
k_sem_give(&ipc_sem_cfg);
131113
#else
132114
atomic_set_bit(&ipc_atomic_sem, NRFE_MSPI_CONFIG_PINS);
133-
#endif
134-
break;
135-
}
136-
case NRFE_MSPI_CONFIG_CTRL: {
137-
#if defined(CONFIG_MULTITHREADING)
138-
k_sem_give(&ipc_sem_cfg);
139-
#else
140-
atomic_set_bit(&ipc_atomic_sem, NRFE_MSPI_CONFIG_CTRL);
141115
#endif
142116
break;
143117
}
@@ -167,7 +141,7 @@ static void ipc_recv_clbk(const void *data, size_t len)
167141
}
168142
case NRFE_MSPI_TXRX: {
169143
if (len > 0) {
170-
ipc_received = len - 1;
144+
ipc_received = len - sizeof(nrfe_mspi_opcode_t);
171145
ipc_receive_buffer = (uint8_t *)&response->data;
172146
}
173147
#if defined(CONFIG_MULTITHREADING)
@@ -196,7 +170,7 @@ static void ipc_recv_clbk(const void *data, size_t len)
196170
* @return 0 on success, -ENOMEM if there is no space in the buffer,
197171
* -ETIMEDOUT if the transfer timed out.
198172
*/
199-
static int mspi_ipc_data_send(enum nrfe_mspi_opcode opcode, const void *data, size_t len)
173+
static int mspi_ipc_data_send(nrfe_mspi_opcode_t opcode, const void *data, size_t len)
200174
{
201175
int rc;
202176

@@ -233,14 +207,13 @@ static int mspi_ipc_data_send(enum nrfe_mspi_opcode opcode, const void *data, si
233207
*
234208
* @return 0 on success, -ETIMEDOUT if the operation timed out.
235209
*/
236-
static int nrfe_mspi_wait_for_response(enum nrfe_mspi_opcode opcode, uint32_t timeout)
210+
static int nrfe_mspi_wait_for_response(nrfe_mspi_opcode_t opcode, uint32_t timeout)
237211
{
238212
#if defined(CONFIG_MULTITHREADING)
239213
int ret = 0;
240214

241215
switch (opcode) {
242216
case NRFE_MSPI_CONFIG_PINS:
243-
case NRFE_MSPI_CONFIG_CTRL:
244217
case NRFE_MSPI_CONFIG_DEV:
245218
case NRFE_MSPI_CONFIG_XFER: {
246219
ret = k_sem_take(&ipc_sem_cfg, K_MSEC(timeout));
@@ -280,28 +253,6 @@ static int nrfe_mspi_wait_for_response(enum nrfe_mspi_opcode opcode, uint32_t ti
280253
return 0;
281254
}
282255

283-
/**
284-
* @brief Send a data struct to the FLPR core using the IPC service.
285-
*
286-
* The function sends a data structure to the FLPR core,
287-
* inserting a byte at the beginning responsible for the opcode.
288-
*
289-
* @param opcode The NRFE MSPI opcode.
290-
* @param data The data to send.
291-
* @param len The length of the data to send.
292-
*
293-
* @return 0 on success, negative errno code on failure.
294-
*/
295-
static int send_with_opcode(enum nrfe_mspi_opcode opcode, const void *data, size_t len)
296-
{
297-
uint8_t buffer[len + 1];
298-
299-
buffer[0] = (uint8_t)opcode;
300-
memcpy(&buffer[1], data, len);
301-
302-
return mspi_ipc_data_send(opcode, buffer, sizeof(buffer));
303-
}
304-
305256
/**
306257
* @brief Send a configuration struct to the FLPR core using the IPC service.
307258
*
@@ -311,11 +262,11 @@ static int send_with_opcode(enum nrfe_mspi_opcode opcode, const void *data, size
311262
*
312263
* @return 0 on success, negative errno code on failure.
313264
*/
314-
static int send_config(enum nrfe_mspi_opcode opcode, const void *config, size_t len)
265+
static int send_config(nrfe_mspi_opcode_t opcode, const void *config, size_t len)
315266
{
316267
int rc;
268+
rc = mspi_ipc_data_send(opcode, config, len);
317269

318-
rc = send_with_opcode(opcode, config, len);
319270
if (rc < 0) {
320271
LOG_ERR("Configuration send failed: %d", rc);
321272
return rc;
@@ -342,9 +293,9 @@ static int send_config(enum nrfe_mspi_opcode opcode, const void *config, size_t
342293
*/
343294
static int api_config(const struct mspi_dt_spec *spec)
344295
{
345-
int ret;
346296
const struct mspi_cfg *config = &spec->config;
347297
const struct mspi_nrfe_config *drv_cfg = spec->bus->config;
298+
nrfe_mspi_pinctrl_soc_pin_msg_t mspi_pin_config;
348299

349300
if (config->op_mode != MSPI_OP_MODE_CONTROLLER) {
350301
LOG_ERR("Only MSPI controller mode is supported.");
@@ -363,7 +314,6 @@ static int api_config(const struct mspi_dt_spec *spec)
363314

364315
/* Create pinout configuration */
365316
uint8_t state_id;
366-
nrfe_mspi_pinctrl_soc_pin_t pins_cfg;
367317

368318
for (state_id = 0; state_id < drv_cfg->pcfg->state_cnt; state_id++) {
369319
if (drv_cfg->pcfg->states[state_id].id == PINCTRL_STATE_DEFAULT) {
@@ -382,17 +332,13 @@ static int api_config(const struct mspi_dt_spec *spec)
382332
}
383333

384334
for (uint8_t i = 0; i < drv_cfg->pcfg->states[state_id].pin_cnt; i++) {
385-
pins_cfg.pin[i] = drv_cfg->pcfg->states[state_id].pins[i];
335+
mspi_pin_config.pin[i] = drv_cfg->pcfg->states[state_id].pins[i];
386336
}
337+
mspi_pin_config.opcode = NRFE_MSPI_CONFIG_PINS;
387338

388339
/* Send pinout configuration to FLPR */
389-
ret = send_config(NRFE_MSPI_CONFIG_PINS, (const void *)pins_cfg.pin, sizeof(pins_cfg));
390-
if (ret < 0) {
391-
return ret;
392-
}
393-
394-
/* Send controller configuration to FLPR */
395-
return send_config(NRFE_MSPI_CONFIG_CTRL, (const void *)config, sizeof(struct mspi_cfg));
340+
return send_config(NRFE_MSPI_CONFIG_PINS, (const void *)&mspi_pin_config,
341+
sizeof(nrfe_mspi_pinctrl_soc_pin_msg_t));
396342
}
397343

398344
static int check_io_mode(enum mspi_io_mode io_mode)
@@ -425,8 +371,8 @@ static int api_dev_config(const struct device *dev, const struct mspi_dev_id *de
425371
const enum mspi_dev_cfg_mask param_mask, const struct mspi_dev_cfg *cfg)
426372
{
427373
const struct mspi_nrfe_config *drv_cfg = dev->config;
428-
struct mspi_nrfe_data *drv_data = dev->data;
429374
int rc;
375+
nrfe_mspi_dev_config_msg_t mspi_dev_config_msg;
430376

431377
if (param_mask & MSPI_DEVICE_CONFIG_MEM_BOUND) {
432378
if (cfg->mem_boundary) {
@@ -471,10 +417,16 @@ static int api_dev_config(const struct device *dev, const struct mspi_dev_id *de
471417
}
472418
}
473419

474-
memcpy((void *)&drv_data->dev_cfg, (void *)cfg, sizeof(drv_data->dev_cfg));
475-
drv_data->dev_id = *dev_id;
420+
mspi_dev_config_msg.opcode = NRFE_MSPI_CONFIG_DEV;
421+
mspi_dev_config_msg.device_index = dev_id->dev_idx;
422+
mspi_dev_config_msg.dev_config.io_mode = cfg->io_mode;
423+
mspi_dev_config_msg.dev_config.cpp = cfg->cpp;
424+
mspi_dev_config_msg.dev_config.ce_polarity = cfg->ce_polarity;
425+
mspi_dev_config_msg.dev_config.freq = cfg->freq;
426+
mspi_dev_config_msg.dev_config.ce_index = cfg->ce_num;
476427

477-
return send_config(NRFE_MSPI_CONFIG_DEV, (void *)cfg, sizeof(struct mspi_dev_cfg));
428+
return send_config(NRFE_MSPI_CONFIG_DEV, (void *)&mspi_dev_config_msg,
429+
sizeof(nrfe_mspi_dev_config_msg_t));
478430
}
479431

480432
static int api_get_channel_status(const struct device *dev, uint8_t ch)
@@ -497,21 +449,23 @@ static int api_get_channel_status(const struct device *dev, uint8_t ch)
497449
static int xfer_packet(struct mspi_xfer_packet *packet, uint32_t timeout)
498450
{
499451
int rc;
500-
uint32_t struct_size = sizeof(struct mspi_xfer_packet);
501-
uint32_t len = struct_size + packet->num_bytes + 1;
452+
uint32_t len = sizeof(nrfe_mspi_xfer_packet_msg_t) + packet->num_bytes;
502453
uint8_t buffer[len];
503-
enum nrfe_mspi_opcode opcode = (packet->dir == MSPI_RX) ? NRFE_MSPI_TXRX : NRFE_MSPI_TX;
454+
nrfe_mspi_xfer_packet_msg_t *xfer_packet = (nrfe_mspi_xfer_packet_msg_t *)buffer;
455+
456+
xfer_packet->opcode = (packet->dir == MSPI_RX) ? NRFE_MSPI_TXRX : NRFE_MSPI_TX;
457+
xfer_packet->command = packet->cmd;
458+
xfer_packet->address = packet->address;
459+
xfer_packet->num_bytes = packet->num_bytes;
504460

505-
buffer[0] = (uint8_t)opcode;
506-
memcpy((void *)&buffer[1], (void *)packet, struct_size);
507-
memcpy((void *)(&buffer[1] + struct_size), (void *)packet->data_buf, packet->num_bytes);
461+
memcpy((void *)xfer_packet->data, (void *)packet->data_buf, packet->num_bytes);
508462

509-
rc = mspi_ipc_data_send(opcode, buffer, len);
463+
rc = mspi_ipc_data_send(xfer_packet->opcode, buffer, len);
510464
if (rc < 0) {
511465
LOG_ERR("Packet transfer error: %d", rc);
512466
}
513467

514-
rc = nrfe_mspi_wait_for_response(opcode, timeout);
468+
rc = nrfe_mspi_wait_for_response(xfer_packet->opcode, timeout);
515469
if (rc < 0) {
516470
LOG_ERR("FLPR Xfer response timeout: %d", rc);
517471
return rc;
@@ -573,10 +527,10 @@ static int start_next_packet(struct mspi_xfer *xfer, uint32_t packets_done)
573527
static int api_transceive(const struct device *dev, const struct mspi_dev_id *dev_id,
574528
const struct mspi_xfer *req)
575529
{
576-
(void)dev_id;
577-
struct mspi_nrfe_data *drv_data = dev->data;
530+
(void)dev;
578531
uint32_t packets_done = 0;
579532
int rc;
533+
nrfe_mspi_xfer_config_msg_t mspi_xfer_config_msg;
580534

581535
/* TODO: add support for asynchronous transfers */
582536
if (req->async) {
@@ -588,16 +542,23 @@ static int api_transceive(const struct device *dev, const struct mspi_dev_id *de
588542
return -EFAULT;
589543
}
590544

591-
drv_data->xfer = *req;
545+
mspi_xfer_config_msg.opcode = NRFE_MSPI_CONFIG_XFER;
546+
mspi_xfer_config_msg.xfer_config.device_index = dev_id->dev_idx;
547+
mspi_xfer_config_msg.xfer_config.command_length = req->cmd_length;
548+
mspi_xfer_config_msg.xfer_config.address_length = req->addr_length;
549+
mspi_xfer_config_msg.xfer_config.hold_ce = req->hold_ce;
550+
mspi_xfer_config_msg.xfer_config.tx_dummy = req->tx_dummy;
551+
mspi_xfer_config_msg.xfer_config.rx_dummy = req->rx_dummy;
592552

593-
rc = send_config(NRFE_MSPI_CONFIG_XFER, (void *)&drv_data->xfer, sizeof(struct mspi_xfer));
553+
rc = send_config(NRFE_MSPI_CONFIG_XFER, (void *)&mspi_xfer_config_msg,
554+
sizeof(nrfe_mspi_xfer_config_msg_t));
594555
if (rc < 0) {
595556
LOG_ERR("Send xfer config error: %d", rc);
596557
return rc;
597558
}
598559

599-
while (packets_done < drv_data->xfer.num_packet) {
600-
rc = start_next_packet(&drv_data->xfer, packets_done);
560+
while (packets_done < req->num_packet) {
561+
rc = start_next_packet((struct mspi_xfer *)req, packets_done);
601562
if (rc < 0) {
602563
LOG_ERR("Start next packet error: %d", rc);
603564
return rc;
@@ -710,5 +671,5 @@ static const struct mspi_driver_api drv_api = {
710671

711672
PM_DEVICE_DT_INST_DEFINE(0, dev_pm_action_cb);
712673

713-
DEVICE_DT_INST_DEFINE(0, nrfe_mspi_init, PM_DEVICE_DT_INST_GET(0), &dev_data, &dev_config,
714-
POST_KERNEL, CONFIG_MSPI_NRFE_INIT_PRIORITY, &drv_api);
674+
DEVICE_DT_INST_DEFINE(0, nrfe_mspi_init, PM_DEVICE_DT_INST_GET(0), NULL, &dev_config, POST_KERNEL,
675+
CONFIG_MSPI_NRFE_INIT_PRIORITY, &drv_api);

include/drivers/mspi/nrfe_mspi.h

+48-33
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,62 @@ extern "C" {
3030
#endif
3131

3232
/** @brief eMSPI opcodes. */
33-
enum nrfe_mspi_opcode {
33+
typedef enum {
3434
NRFE_MSPI_EP_BOUNDED = 0,
35-
NRFE_MSPI_CONFIG_PINS,
36-
NRFE_MSPI_CONFIG_CTRL, /* struct mspi_cfg */
37-
NRFE_MSPI_CONFIG_DEV, /* struct mspi_dev_cfg */
38-
NRFE_MSPI_CONFIG_XFER, /* struct mspi_xfer */
39-
NRFE_MSPI_TX,
35+
NRFE_MSPI_CONFIG_PINS, /* nrfe_mspi_pinctrl_soc_pin_msg_t */
36+
NRFE_MSPI_CONFIG_DEV, /* nrfe_mspi_dev_config_msg_t */
37+
NRFE_MSPI_CONFIG_XFER, /* nrfe_mspi_xfer_config_msg_t */
38+
NRFE_MSPI_TX, /* nrfe_mspi_xfer_packet_msg_t + data buffer at the end */
4039
NRFE_MSPI_TXRX,
4140
NRFE_MSPI_WRONG_OPCODE,
4241
NRFE_MSPI_ALL_OPCODES = NRFE_MSPI_WRONG_OPCODE,
43-
};
42+
} nrfe_mspi_opcode_t;
4443

45-
typedef struct __packed {
46-
uint8_t opcode; /* nrfe_mspi_opcode */
44+
typedef struct {
45+
enum mspi_io_mode io_mode;
46+
enum mspi_cpp_mode cpp;
47+
uint8_t ce_index;
48+
enum mspi_ce_polarity ce_polarity;
49+
uint32_t freq;
50+
} nrfe_mspi_dev_config_t;
51+
52+
typedef struct {
53+
uint8_t device_index;
54+
uint8_t command_length;
55+
uint8_t address_length;
56+
bool hold_ce;
57+
uint16_t tx_dummy;
58+
uint16_t rx_dummy;
59+
} nrfe_mspi_xfer_config_t;
60+
61+
typedef struct {
62+
nrfe_mspi_opcode_t opcode; /* NRFE_MSPI_CONFIG_PINS */
4763
pinctrl_soc_pin_t pin[NRFE_MSPI_PINS_MAX];
48-
} nrfe_mspi_pinctrl_soc_pin_t;
64+
} nrfe_mspi_pinctrl_soc_pin_msg_t;
65+
66+
typedef struct {
67+
nrfe_mspi_opcode_t opcode; /* NRFE_MSPI_CONFIG_DEV */
68+
uint8_t device_index;
69+
nrfe_mspi_dev_config_t dev_config;
70+
} nrfe_mspi_dev_config_msg_t;
71+
72+
typedef struct {
73+
nrfe_mspi_opcode_t opcode; /* NRFE_MSPI_CONFIG_XFER */
74+
nrfe_mspi_xfer_config_t xfer_config;
75+
} nrfe_mspi_xfer_config_msg_t;
76+
77+
typedef struct {
78+
nrfe_mspi_opcode_t opcode; /* NRFE_MSPI_TX or NRFE_MSPI_TXRX */
79+
uint32_t command;
80+
uint32_t address;
81+
uint32_t num_bytes; /* Size of data */
82+
uint8_t data[]; /* Variable length data field at the end of packet. */
83+
} nrfe_mspi_xfer_packet_msg_t;
4984

50-
typedef struct __packed {
51-
uint8_t opcode; /* nrfe_mspi_opcode */
85+
typedef struct {
86+
nrfe_mspi_opcode_t opcode; /* Same as application's request. */
5287
uint8_t data;
53-
} nrfe_mspi_flpr_response_t;
54-
55-
typedef struct __packed {
56-
uint8_t opcode;
57-
struct mspi_cfg cfg;
58-
} nrfe_mspi_cfg_t;
59-
60-
typedef struct __packed {
61-
uint8_t opcode;
62-
struct mspi_dev_cfg cfg;
63-
} nrfe_mspi_dev_cfg_t;
64-
65-
typedef struct __packed {
66-
uint8_t opcode;
67-
struct mspi_xfer xfer;
68-
} nrfe_mspi_xfer_t;
69-
70-
typedef struct __packed {
71-
uint8_t opcode;
72-
struct mspi_xfer_packet packet;
73-
} nrfe_mspi_xfer_packet_t;
88+
} nrfe_mspi_flpr_response_msg_t;
7489

7590
#ifdef __cplusplus
7691
}

0 commit comments

Comments
 (0)