6
6
7
7
#define DT_DRV_COMPAT nxp_imx_lpspi
8
8
9
- #include <errno.h>
10
9
#include <zephyr/drivers/spi.h>
10
+ #include <zephyr/drivers/pinctrl.h>
11
11
#include <zephyr/drivers/clock_control.h>
12
- #include <fsl_lpspi.h>
13
- #if CONFIG_NXP_LP_FLEXCOMM
14
- #include <zephyr/drivers/mfd/nxp_lp_flexcomm.h>
15
- #endif
16
- #include <zephyr/logging/log.h>
17
12
#include <zephyr/irq.h>
18
- #ifdef CONFIG_SPI_MCUX_LPSPI_DMA
19
- #include <zephyr/drivers/dma .h>
20
- #endif
21
- #include <zephyr/drivers/pinctrl.h>
13
+
14
+ #include <zephyr/logging/log .h>
15
+ LOG_MODULE_REGISTER ( spi_mcux_lpspi , CONFIG_SPI_LOG_LEVEL );
16
+
22
17
#ifdef CONFIG_SPI_RTIO
23
- #include <zephyr/rtio/rtio.h>
24
18
#include <zephyr/drivers/spi/rtio.h>
25
- #include <zephyr/spinlock.h>
26
19
#endif
27
20
28
- LOG_MODULE_REGISTER (spi_mcux_lpspi , CONFIG_SPI_LOG_LEVEL );
29
-
30
21
#include "spi_context.h"
31
22
23
+ #if CONFIG_NXP_LP_FLEXCOMM
24
+ #include <zephyr/drivers/mfd/nxp_lp_flexcomm.h>
25
+ #endif
26
+
27
+ #include <fsl_lpspi.h>
28
+
29
+ /* If any hardware revisions change this, make it into a DT property.
30
+ * DONT'T make #ifdefs here by platform.
31
+ */
32
32
#define CHIP_SELECT_COUNT 4
33
33
#define MAX_DATA_WIDTH 4096
34
34
35
35
/* Required by DEVICE_MMIO_NAMED_* macros */
36
36
#define DEV_CFG (_dev ) ((const struct spi_mcux_config *)(_dev)->config)
37
37
#define DEV_DATA (_dev ) ((struct spi_mcux_data *)(_dev)->data)
38
38
39
+ #ifdef CONFIG_SPI_MCUX_LPSPI_DMA
40
+ #include <zephyr/drivers/dma.h>
41
+
42
+ /* These flags are arbitrary */
43
+ #define LPSPI_DMA_ERROR_FLAG BIT(0)
44
+ #define LPSPI_DMA_RX_DONE_FLAG BIT(1)
45
+ #define LPSPI_DMA_TX_DONE_FLAG BIT(2)
46
+ #define LPSPI_DMA_DONE_FLAG (LPSPI_DMA_RX_DONE_FLAG | LPSPI_DMA_TX_DONE_FLAG)
47
+
48
+ struct spi_dma_stream {
49
+ const struct device * dma_dev ;
50
+ uint32_t channel ; /* stores the channel for dma */
51
+ struct dma_config dma_cfg ;
52
+ struct dma_block_config dma_blk_cfg ;
53
+ };
54
+ #endif /* CONFIG_SPI_MCUX_LPSPI_DMA */
55
+
39
56
struct spi_mcux_config {
40
57
DEVICE_MMIO_NAMED_ROM (reg_base );
41
58
#ifdef CONFIG_NXP_LP_FLEXCOMM
@@ -51,36 +68,19 @@ struct spi_mcux_config {
51
68
lpspi_pin_config_t data_pin_config ;
52
69
};
53
70
54
- #ifdef CONFIG_SPI_MCUX_LPSPI_DMA
55
- #define SPI_MCUX_LPSPI_DMA_ERROR_FLAG 0x01
56
- #define SPI_MCUX_LPSPI_DMA_RX_DONE_FLAG 0x02
57
- #define SPI_MCUX_LPSPI_DMA_TX_DONE_FLAG 0x04
58
- #define SPI_MCUX_LPSPI_DMA_DONE_FLAG \
59
- (SPI_MCUX_LPSPI_DMA_RX_DONE_FLAG | SPI_MCUX_LPSPI_DMA_TX_DONE_FLAG)
60
-
61
- struct stream {
62
- const struct device * dma_dev ;
63
- uint32_t channel ; /* stores the channel for dma */
64
- struct dma_config dma_cfg ;
65
- struct dma_block_config dma_blk_cfg ;
66
- };
67
- #endif
68
-
69
71
struct spi_mcux_data {
70
72
DEVICE_MMIO_NAMED_RAM (reg_base );
71
73
const struct device * dev ;
72
74
lpspi_master_handle_t handle ;
73
75
struct spi_context ctx ;
74
76
size_t transfer_len ;
75
-
76
77
#ifdef CONFIG_SPI_RTIO
77
78
struct spi_rtio * rtio_ctx ;
78
79
#endif
79
-
80
80
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
81
81
volatile uint32_t status_flags ;
82
- struct stream dma_rx ;
83
- struct stream dma_tx ;
82
+ struct spi_dma_stream dma_rx ;
83
+ struct spi_dma_stream dma_tx ;
84
84
/* dummy value used for transferring NOP when tx buf is null */
85
85
uint32_t dummy_tx_buffer ;
86
86
/* dummy value used to read RX data into when rx buf is null */
@@ -286,25 +286,25 @@ static void spi_mcux_dma_callback(const struct device *dev, void *arg, uint32_t
286
286
287
287
if (status < 0 ) {
288
288
LOG_ERR ("DMA callback error with channel %d." , channel );
289
- data -> status_flags |= SPI_MCUX_LPSPI_DMA_ERROR_FLAG ;
289
+ data -> status_flags |= LPSPI_DMA_ERROR_FLAG ;
290
290
} else {
291
291
/* identify the origin of this callback */
292
292
if (channel == data -> dma_tx .channel ) {
293
293
/* this part of the transfer ends */
294
- data -> status_flags |= SPI_MCUX_LPSPI_DMA_TX_DONE_FLAG ;
294
+ data -> status_flags |= LPSPI_DMA_TX_DONE_FLAG ;
295
295
LOG_DBG ("DMA TX Block Complete" );
296
296
} else if (channel == data -> dma_rx .channel ) {
297
297
/* this part of the transfer ends */
298
- data -> status_flags |= SPI_MCUX_LPSPI_DMA_RX_DONE_FLAG ;
298
+ data -> status_flags |= LPSPI_DMA_RX_DONE_FLAG ;
299
299
LOG_DBG ("DMA RX Block Complete" );
300
300
} else {
301
301
LOG_ERR ("DMA callback channel %d is not valid." , channel );
302
- data -> status_flags |= SPI_MCUX_LPSPI_DMA_ERROR_FLAG ;
302
+ data -> status_flags |= LPSPI_DMA_ERROR_FLAG ;
303
303
}
304
304
}
305
305
#if CONFIG_SPI_ASYNC
306
306
if (data -> ctx .asynchronous &&
307
- ((data -> status_flags & SPI_MCUX_LPSPI_DMA_DONE_FLAG ) == SPI_MCUX_LPSPI_DMA_DONE_FLAG )) {
307
+ ((data -> status_flags & LPSPI_DMA_DONE_FLAG ) == LPSPI_DMA_DONE_FLAG )) {
308
308
/* Load dma blocks of equal length */
309
309
size_t dma_size = MIN (data -> ctx .tx_len , data -> ctx .rx_len );
310
310
@@ -332,7 +332,7 @@ static int spi_mcux_dma_tx_load(const struct device *dev, const uint8_t *buf, si
332
332
LPSPI_Type * base = (LPSPI_Type * )DEVICE_MMIO_NAMED_GET (dev , reg_base );
333
333
334
334
/* remember active TX DMA channel (used in callback) */
335
- struct stream * stream = & data -> dma_tx ;
335
+ struct spi_dma_stream * stream = & data -> dma_tx ;
336
336
337
337
blk_cfg = & stream -> dma_blk_cfg ;
338
338
@@ -373,7 +373,7 @@ static int spi_mcux_dma_rx_load(const struct device *dev, uint8_t *buf, size_t l
373
373
LPSPI_Type * base = (LPSPI_Type * )DEVICE_MMIO_NAMED_GET (dev , reg_base );
374
374
375
375
/* retrieve active RX DMA channel (used in callback) */
376
- struct stream * stream = & data -> dma_rx ;
376
+ struct spi_dma_stream * stream = & data -> dma_rx ;
377
377
378
378
blk_cfg = & stream -> dma_blk_cfg ;
379
379
@@ -416,12 +416,11 @@ static int wait_dma_rx_tx_done(const struct device *dev)
416
416
LOG_DBG ("Timed out waiting for SPI context to complete" );
417
417
return ret ;
418
418
}
419
- if (data -> status_flags & SPI_MCUX_LPSPI_DMA_ERROR_FLAG ) {
419
+ if (data -> status_flags & LPSPI_DMA_ERROR_FLAG ) {
420
420
return - EIO ;
421
421
}
422
422
423
- if ((data -> status_flags & SPI_MCUX_LPSPI_DMA_DONE_FLAG ) ==
424
- SPI_MCUX_LPSPI_DMA_DONE_FLAG ) {
423
+ if ((data -> status_flags & LPSPI_DMA_DONE_FLAG ) == LPSPI_DMA_DONE_FLAG ) {
425
424
LOG_DBG ("DMA block completed" );
426
425
return 0 ;
427
426
}
0 commit comments