88#include <zephyr/drivers/spi/rtio.h>
99#include <zephyr/drivers/pinctrl.h>
1010#include <zephyr/drivers/gpio.h>
11+ #include <dmm.h>
1112#include <soc.h>
1213#include <nrfx_spis.h>
1314#include <zephyr/pm/device.h>
@@ -40,6 +41,7 @@ struct spi_nrfx_config {
4041#endif
4142 const struct pinctrl_dev_config * pcfg ;
4243 struct gpio_dt_spec wake_gpio ;
44+ void * mem_reg ;
4345};
4446
4547static inline nrf_spis_mode_t get_nrf_spis_mode (uint16_t operation )
@@ -125,7 +127,11 @@ static int prepare_for_transfer(const struct device *dev,
125127 uint8_t * rx_buf , size_t rx_buf_len )
126128{
127129 const struct spi_nrfx_config * dev_config = dev -> config ;
130+ struct spi_nrfx_data * dev_data = dev -> data ;
128131 nrfx_err_t result ;
132+ uint8_t * dmm_tx_buf ;
133+ uint8_t * dmm_rx_buf ;
134+ int err ;
129135
130136 if (tx_buf_len > dev_config -> max_buf_len ||
131137 rx_buf_len > dev_config -> max_buf_len ) {
@@ -134,14 +140,36 @@ static int prepare_for_transfer(const struct device *dev,
134140 return - EINVAL ;
135141 }
136142
143+ err = dmm_buffer_out_prepare (dev_config -> mem_reg , tx_buf , tx_buf_len , (void * * )& dmm_tx_buf );
144+ if (err != 0 ) {
145+ LOG_ERR ("DMM TX allocation failed err=%d" , err );
146+ goto out_alloc_failed ;
147+ }
148+
149+ /* Keep user RX buffer address to copy data from DMM RX buffer on transfer completion. */
150+ dev_data -> ctx .rx_buf = rx_buf ;
151+ err = dmm_buffer_in_prepare (dev_config -> mem_reg , rx_buf , rx_buf_len , (void * * )& dmm_rx_buf );
152+ if (err != 0 ) {
153+ LOG_ERR ("DMM RX allocation failed err=%d" , err );
154+ goto in_alloc_failed ;
155+ }
156+
137157 result = nrfx_spis_buffers_set (& dev_config -> spis ,
138- tx_buf , tx_buf_len ,
139- rx_buf , rx_buf_len );
158+ dmm_tx_buf , tx_buf_len ,
159+ dmm_rx_buf , rx_buf_len );
140160 if (result != NRFX_SUCCESS ) {
141- return - EIO ;
161+ err = - EIO ;
162+ goto buffers_set_failed ;
142163 }
143164
144165 return 0 ;
166+
167+ buffers_set_failed :
168+ dmm_buffer_in_release (dev_config -> mem_reg , rx_buf , rx_buf_len , rx_buf );
169+ in_alloc_failed :
170+ dmm_buffer_out_release (dev_config -> mem_reg , (void * )tx_buf );
171+ out_alloc_failed :
172+ return err ;
145173}
146174
147175static void wake_callback (const struct device * dev , struct gpio_callback * cb ,
@@ -288,9 +316,22 @@ static DEVICE_API(spi, spi_nrfx_driver_api) = {
288316
289317static void event_handler (const nrfx_spis_evt_t * p_event , void * p_context )
290318{
291- struct spi_nrfx_data * dev_data = p_context ;
319+ const struct device * dev = p_context ;
320+ struct spi_nrfx_data * dev_data = dev -> data ;
321+ const struct spi_nrfx_config * dev_config = dev -> config ;
292322
293323 if (p_event -> evt_type == NRFX_SPIS_XFER_DONE ) {
324+ int err ;
325+
326+
327+ err = dmm_buffer_out_release (dev_config -> mem_reg , p_event -> p_tx_buf );
328+ (void )err ;
329+ __ASSERT_NO_MSG (err == 0 );
330+
331+ err = dmm_buffer_in_release (dev_config -> mem_reg , dev_data -> ctx .rx_buf ,
332+ p_event -> rx_amount , p_event -> p_rx_buf );
333+ __ASSERT_NO_MSG (err == 0 );
334+
294335 spi_context_complete (& dev_data -> ctx , dev_data -> dev ,
295336 p_event -> rx_amount );
296337
@@ -361,7 +402,7 @@ static int spi_nrfx_init(const struct device *dev)
361402 * actually used are set in configure() when a transfer is prepared.
362403 */
363404 result = nrfx_spis_init (& dev_config -> spis , & dev_config -> config ,
364- event_handler , dev_data );
405+ event_handler , ( void * ) dev );
365406
366407 if (result != NRFX_SUCCESS ) {
367408 LOG_ERR ("Failed to initialize device: %s" , dev -> name );
@@ -416,7 +457,10 @@ static int spi_nrfx_init(const struct device *dev)
416457 * - Name-based HAL IRQ handlers, e.g. nrfx_spis_0_irq_handler
417458 */
418459
419- #define SPIS (idx ) DT_NODELABEL(spi##idx)
460+ #define SPIS_NODE (idx ) COND_CODE_1(IS_EQ(idx, 120), (spis##idx), (spi##idx))
461+
462+ #define SPIS (idx ) DT_NODELABEL(SPIS_NODE(idx))
463+
420464#define SPIS_PROP (idx , prop ) DT_PROP(SPIS(idx), prop)
421465
422466#define SPI_NRFX_SPIS_DEFINE (idx ) \
@@ -453,6 +497,7 @@ static int spi_nrfx_init(const struct device *dev)
453497 (.gpd_ctrl = NRF_PERIPH_GET_FREQUENCY(SPIS(idx)) > \
454498 NRFX_MHZ_TO_HZ(16UL),)) \
455499 .wake_gpio = GPIO_DT_SPEC_GET_OR(SPIS(idx), wake_gpios, {0}), \
500+ .mem_reg = DMM_DEV_TO_REG(SPIS(idx)), \
456501 }; \
457502 BUILD_ASSERT(!DT_NODE_HAS_PROP(SPIS(idx), wake_gpios) || \
458503 !(DT_GPIO_FLAGS(SPIS(idx), wake_gpios) & GPIO_ACTIVE_LOW),\
0 commit comments