Skip to content

Commit 0ce4399

Browse files
CkovMkcarlescufi
authored andcommitted
driver: mucx_lpspi: enable runtime mmio configuration
Enable runtime mmio configuration in mcux_lpspi driver. The fsl_lpspi driver relies on physical address to determine instance number. So mmap flag 'K_MEM_DIRECT_MAP' is required. Signed-off-by: Chekhov Ma <[email protected]>
1 parent 13ba531 commit 0ce4399

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed

drivers/spi/spi_mcux_lpspi.c

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ LOG_MODULE_REGISTER(spi_mcux_lpspi, CONFIG_SPI_LOG_LEVEL);
2828
#define CHIP_SELECT_COUNT 4
2929
#define MAX_DATA_WIDTH 4096
3030

31+
/* Required by DEVICE_MMIO_NAMED_* macros */
32+
#define DEV_CFG(_dev) \
33+
((const struct spi_mcux_config *)(_dev)->config)
34+
#define DEV_DATA(_dev) ((struct spi_mcux_data *)(_dev)->data)
35+
3136
struct spi_mcux_config {
32-
LPSPI_Type *base;
37+
DEVICE_MMIO_NAMED_ROM(reg_base);
3338
const struct device *clock_dev;
3439
clock_control_subsys_t clock_subsys;
3540
void (*irq_config_func)(const struct device *dev);
@@ -56,6 +61,7 @@ struct stream {
5661
#endif
5762

5863
struct spi_mcux_data {
64+
DEVICE_MMIO_NAMED_RAM(reg_base);
5965
const struct device *dev;
6066
lpspi_master_handle_t handle;
6167
struct spi_context ctx;
@@ -83,9 +89,9 @@ struct spi_mcux_data {
8389

8490
static int spi_mcux_transfer_next_packet(const struct device *dev)
8591
{
86-
const struct spi_mcux_config *config = dev->config;
92+
/* const struct spi_mcux_config *config = dev->config; */
8793
struct spi_mcux_data *data = dev->data;
88-
LPSPI_Type *base = config->base;
94+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
8995
struct spi_context *ctx = &data->ctx;
9096
lpspi_transfer_t transfer;
9197
status_t status;
@@ -98,7 +104,7 @@ static int spi_mcux_transfer_next_packet(const struct device *dev)
98104
}
99105

100106
transfer.configFlags = kLPSPI_MasterPcsContinuous |
101-
(ctx->config->slave << LPSPI_MASTER_PCS_SHIFT);
107+
(ctx->config->slave << LPSPI_MASTER_PCS_SHIFT);
102108

103109
if (ctx->tx_len == 0) {
104110
/* rx only, nothing to tx */
@@ -153,9 +159,9 @@ static int spi_mcux_transfer_next_packet(const struct device *dev)
153159

154160
static void spi_mcux_isr(const struct device *dev)
155161
{
156-
const struct spi_mcux_config *config = dev->config;
162+
/* const struct spi_mcux_config *config = dev->config; */
157163
struct spi_mcux_data *data = dev->data;
158-
LPSPI_Type *base = config->base;
164+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
159165

160166
LPSPI_MasterTransferHandleIRQ(base, &data->handle);
161167
}
@@ -182,11 +188,11 @@ static void spi_mcux_master_transfer_callback(LPSPI_Type *base,
182188
}
183189

184190
static int spi_mcux_configure(const struct device *dev,
185-
const struct spi_config *spi_cfg)
191+
const struct spi_config *spi_cfg)
186192
{
187193
const struct spi_mcux_config *config = dev->config;
188194
struct spi_mcux_data *data = dev->data;
189-
LPSPI_Type *base = config->base;
195+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
190196
lpspi_master_config_t master_config;
191197
uint32_t clock_freq;
192198
uint32_t word_size;
@@ -205,15 +211,15 @@ static int spi_mcux_configure(const struct device *dev,
205211

206212
if (spi_cfg->slave > CHIP_SELECT_COUNT) {
207213
LOG_ERR("Slave %d is greater than %d",
208-
spi_cfg->slave,
209-
CHIP_SELECT_COUNT);
214+
spi_cfg->slave,
215+
CHIP_SELECT_COUNT);
210216
return -EINVAL;
211217
}
212218

213219
word_size = SPI_WORD_SIZE_GET(spi_cfg->operation);
214220
if (word_size > MAX_DATA_WIDTH) {
215221
LOG_ERR("Word size %d is greater than %d",
216-
word_size, MAX_DATA_WIDTH);
222+
word_size, MAX_DATA_WIDTH);
217223
return -EINVAL;
218224
}
219225

@@ -335,10 +341,10 @@ static void spi_mcux_dma_callback(const struct device *dev, void *arg,
335341

336342
static int spi_mcux_dma_tx_load(const struct device *dev, const uint8_t *buf, size_t len)
337343
{
338-
const struct spi_mcux_config *cfg = dev->config;
344+
/* const struct spi_mcux_config *cfg = dev->config; */
339345
struct spi_mcux_data *data = dev->data;
340346
struct dma_block_config *blk_cfg;
341-
LPSPI_Type *base = cfg->base;
347+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
342348

343349
/* remember active TX DMA channel (used in callback) */
344350
struct stream *stream = &data->dma_tx;
@@ -378,10 +384,10 @@ static int spi_mcux_dma_tx_load(const struct device *dev, const uint8_t *buf, si
378384
static int spi_mcux_dma_rx_load(const struct device *dev, uint8_t *buf,
379385
size_t len)
380386
{
381-
const struct spi_mcux_config *cfg = dev->config;
387+
/*const struct spi_mcux_config *cfg = dev->config; */
382388
struct spi_mcux_data *data = dev->data;
383389
struct dma_block_config *blk_cfg;
384-
LPSPI_Type *base = cfg->base;
390+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
385391

386392
/* retrieve active RX DMA channel (used in callback) */
387393
struct stream *stream = &data->dma_rx;
@@ -480,16 +486,16 @@ static inline int spi_mcux_dma_rxtx_load(const struct device *dev,
480486
}
481487

482488
static int transceive_dma(const struct device *dev,
483-
const struct spi_config *spi_cfg,
484-
const struct spi_buf_set *tx_bufs,
485-
const struct spi_buf_set *rx_bufs,
486-
bool asynchronous,
487-
spi_callback_t cb,
488-
void *userdata)
489+
const struct spi_config *spi_cfg,
490+
const struct spi_buf_set *tx_bufs,
491+
const struct spi_buf_set *rx_bufs,
492+
bool asynchronous,
493+
spi_callback_t cb,
494+
void *userdata)
489495
{
490-
const struct spi_mcux_config *config = dev->config;
496+
/* const struct spi_mcux_config *config = dev->config; */
491497
struct spi_mcux_data *data = dev->data;
492-
LPSPI_Type *base = config->base;
498+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
493499
int ret;
494500
size_t dma_size;
495501

@@ -564,12 +570,12 @@ static int transceive_dma(const struct device *dev,
564570
#endif
565571

566572
static int transceive(const struct device *dev,
567-
const struct spi_config *spi_cfg,
568-
const struct spi_buf_set *tx_bufs,
569-
const struct spi_buf_set *rx_bufs,
570-
bool asynchronous,
571-
spi_callback_t cb,
572-
void *userdata)
573+
const struct spi_config *spi_cfg,
574+
const struct spi_buf_set *tx_bufs,
575+
const struct spi_buf_set *rx_bufs,
576+
bool asynchronous,
577+
spi_callback_t cb,
578+
void *userdata)
573579
{
574580
struct spi_mcux_data *data = dev->data;
575581
int ret;
@@ -599,9 +605,9 @@ static int transceive(const struct device *dev,
599605

600606

601607
static int spi_mcux_transceive(const struct device *dev,
602-
const struct spi_config *spi_cfg,
603-
const struct spi_buf_set *tx_bufs,
604-
const struct spi_buf_set *rx_bufs)
608+
const struct spi_config *spi_cfg,
609+
const struct spi_buf_set *tx_bufs,
610+
const struct spi_buf_set *rx_bufs)
605611
{
606612
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
607613
const struct spi_mcux_data *data = dev->data;
@@ -616,11 +622,11 @@ static int spi_mcux_transceive(const struct device *dev,
616622

617623
#ifdef CONFIG_SPI_ASYNC
618624
static int spi_mcux_transceive_async(const struct device *dev,
619-
const struct spi_config *spi_cfg,
620-
const struct spi_buf_set *tx_bufs,
621-
const struct spi_buf_set *rx_bufs,
622-
spi_callback_t cb,
623-
void *userdata)
625+
const struct spi_config *spi_cfg,
626+
const struct spi_buf_set *tx_bufs,
627+
const struct spi_buf_set *rx_bufs,
628+
spi_callback_t cb,
629+
void *userdata)
624630
{
625631
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
626632
struct spi_mcux_data *data = dev->data;
@@ -637,7 +643,7 @@ static int spi_mcux_transceive_async(const struct device *dev,
637643
#endif /* CONFIG_SPI_ASYNC */
638644

639645
static int spi_mcux_release(const struct device *dev,
640-
const struct spi_config *spi_cfg)
646+
const struct spi_config *spi_cfg)
641647
{
642648
struct spi_mcux_data *data = dev->data;
643649

@@ -652,6 +658,8 @@ static int spi_mcux_init(const struct device *dev)
652658
const struct spi_mcux_config *config = dev->config;
653659
struct spi_mcux_data *data = dev->data;
654660

661+
DEVICE_MMIO_NAMED_MAP(dev, reg_base, K_MEM_CACHE_NONE | K_MEM_DIRECT_MAP);
662+
655663
config->irq_config_func(dev);
656664

657665
err = spi_context_cs_configure_all(&data->ctx);
@@ -714,19 +722,19 @@ static void spi_mcux_iodev_next(const struct device *dev, bool completion);
714722

715723
static void spi_mcux_iodev_start(const struct device *dev)
716724
{
717-
const struct spi_mcux_config *config = dev->config;
725+
/* const struct spi_mcux_config *config = dev->config; */
718726
struct spi_mcux_data *data = dev->data;
719727
struct rtio_sqe *sqe = &data->txn_curr->sqe;
720728
struct spi_dt_spec *spi_dt_spec = sqe->iodev->data;
721729
struct spi_config *spi_cfg = &spi_dt_spec->config;
722730
struct rtio_iodev_sqe *txn_head = data->txn_head;
723731

724-
LPSPI_Type *base = config->base;
732+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
725733
lpspi_transfer_t transfer;
726734
status_t status;
727735

728736
transfer.configFlags = kLPSPI_MasterPcsContinuous |
729-
(spi_cfg->slave << LPSPI_MASTER_PCS_SHIFT);
737+
(spi_cfg->slave << LPSPI_MASTER_PCS_SHIFT);
730738

731739
switch (sqe->op) {
732740
case RTIO_OP_RX:
@@ -895,7 +903,7 @@ static const struct spi_driver_api spi_mcux_driver_api = {
895903
static void spi_mcux_config_func_##n(const struct device *dev); \
896904
\
897905
static const struct spi_mcux_config spi_mcux_config_##n = { \
898-
.base = (LPSPI_Type *) DT_INST_REG_ADDR(n), \
906+
DEVICE_MMIO_NAMED_ROM_INIT(reg_base, DT_DRV_INST(n)), \
899907
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
900908
.clock_subsys = \
901909
(clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name), \
@@ -924,15 +932,15 @@ static const struct spi_driver_api spi_mcux_driver_api = {
924932
}; \
925933
\
926934
DEVICE_DT_INST_DEFINE(n, &spi_mcux_init, NULL, \
927-
&spi_mcux_data_##n, \
928-
&spi_mcux_config_##n, POST_KERNEL, \
929-
CONFIG_SPI_INIT_PRIORITY, \
930-
&spi_mcux_driver_api); \
935+
&spi_mcux_data_##n, \
936+
&spi_mcux_config_##n, POST_KERNEL, \
937+
CONFIG_SPI_INIT_PRIORITY, \
938+
&spi_mcux_driver_api); \
931939
\
932940
static void spi_mcux_config_func_##n(const struct device *dev) \
933941
{ \
934942
IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), \
935-
spi_mcux_isr, DEVICE_DT_INST_GET(n), 0); \
943+
spi_mcux_isr, DEVICE_DT_INST_GET(n), 0); \
936944
\
937945
irq_enable(DT_INST_IRQN(n)); \
938946
}

0 commit comments

Comments
 (0)