@@ -218,33 +218,6 @@ static inline void spi_cdns_cs_control(const struct device *dev, bool on)
218
218
}
219
219
}
220
220
221
- static void spi_cdns_config_clock_freq (const struct device * dev , uint32_t spi_freq )
222
- {
223
- const struct spi_cdns_cfg * cfg = dev -> config ;
224
- uint32_t ctrl_reg , baud_rate_div ;
225
- uint32_t clock_freq ;
226
-
227
- clock_freq = cfg -> clock_frequency ;
228
-
229
- ctrl_reg = sys_read32 (SPI_REG (dev , SPI_CONF ));
230
-
231
- /*
232
- * Set the clock frequency
233
- * first valid value is 0 (/2)
234
- */
235
- baud_rate_div = SPI_MBRD_MIN ;
236
- while ((baud_rate_div < SPI_MBRD_MAX ) && (clock_freq / (2 << baud_rate_div )) > spi_freq ) {
237
- baud_rate_div ++ ;
238
- }
239
-
240
- ctrl_reg &= ~SPI_CONF_MBRD_MASK ;
241
- ctrl_reg |= baud_rate_div << SPI_CONF_MBRD_OFFSET ;
242
-
243
- LOG_DBG ("%s: spi baud rate %uHz" , dev -> name , clock_freq / (2 << baud_rate_div ));
244
-
245
- sys_write32 (ctrl_reg , SPI_REG (dev , SPI_CONF ));
246
- }
247
-
248
221
/**
249
222
* @brief Send 1-entry to Tx-FIFO
250
223
*
@@ -439,7 +412,8 @@ static int spi_cdns_configure(const struct device *dev, const struct spi_config
439
412
{
440
413
const struct spi_cdns_cfg * dev_config = dev -> config ;
441
414
struct spi_cdns_data * data = dev -> data ;
442
- uint32_t word_size , conf_val ;
415
+ uint32_t word_size , conf_val , clock_freq , ext_clock_freq ;
416
+ uint8_t baud_rate_div , ext_baud_rate_div ;
443
417
444
418
if (spi_cdns_context_configured (dev , config )) {
445
419
/* Nothing to do */
@@ -473,7 +447,7 @@ static int spi_cdns_configure(const struct device *dev, const struct spi_config
473
447
data -> ctx .config = config ;
474
448
data -> config = * config ;
475
449
476
- conf_val = SPI_CONF_PCSL_MASK | SPI_CONF_MCSE | SPI_CONF_MRCS ;
450
+ conf_val = SPI_CONF_PCSL_MASK | SPI_CONF_MCSE ;
477
451
478
452
/* Configure for Master or Slave */
479
453
if (config -> operation & SPI_OP_MODE_SLAVE ) {
@@ -501,7 +475,38 @@ static int spi_cdns_configure(const struct device *dev, const struct spi_config
501
475
* SPI clock is generated based on pclk or ext_clk, and the frequency closest
502
476
* to the value obtained by dividing the two base clocks is selected.
503
477
*/
504
- spi_cdns_config_clock_freq (dev , config -> frequency );
478
+ clock_freq = dev_config -> clock_frequency ;
479
+ baud_rate_div = SPI_MBRD_MIN ;
480
+ while ((baud_rate_div < SPI_MBRD_MAX ) &&
481
+ ((clock_freq / (2 << baud_rate_div )) > config -> frequency )) {
482
+ baud_rate_div ++ ;
483
+ }
484
+
485
+ if (dev_config -> ext_clock ) {
486
+ /* check if there is a closer frequency with ext_clock */
487
+ ext_clock_freq = dev_config -> ext_clock ;
488
+ ext_baud_rate_div = SPI_MBRD_MIN ;
489
+ while ((ext_baud_rate_div < SPI_MBRD_MAX ) &&
490
+ ((ext_clock_freq / (2 << ext_baud_rate_div )) > config -> frequency )) {
491
+ ext_baud_rate_div ++ ;
492
+ }
493
+ if (config -> frequency - (clock_freq / (2 << baud_rate_div )) >
494
+ config -> frequency - (ext_clock_freq / (2 << ext_baud_rate_div ))) {
495
+ /* ext_clock is closer, so use it instead */
496
+ baud_rate_div = ext_baud_rate_div ;
497
+ clock_freq = ext_clock_freq ;
498
+ conf_val |= SPI_CONF_MRCS ;
499
+ } else {
500
+ conf_val &= ~SPI_CONF_MRCS ;
501
+ }
502
+ } else {
503
+ conf_val &= ~SPI_CONF_MRCS ;
504
+ }
505
+
506
+ conf_val &= ~SPI_CONF_MBRD_MASK ;
507
+ conf_val |= baud_rate_div << SPI_CONF_MBRD_OFFSET ;
508
+
509
+ LOG_DBG ("%s: spi baud rate %uHz" , dev -> name , clock_freq / (2 << baud_rate_div ));
505
510
506
511
/* Set transfer word size */
507
512
conf_val &= ~(SPI_CONF_TWS_MASK );
@@ -803,12 +808,6 @@ static DEVICE_API(spi, spi_cdns_api) = {
803
808
#endif /* CONFIG_SPI_RTIO */
804
809
};
805
810
806
- /* Set clock-frequency-ext to pclk / 5 if there is no clock-frequency-ext */
807
- #define SPI_CLOCK_FREQUENCY_EXT (n ) \
808
- COND_CODE_1(DT_INST_NODE_HAS_PROP(n, clock_frequency_ext), \
809
- (DT_INST_PROP(n, clock_frequency_ext)), \
810
- (DT_INST_PROP(n, clock_frequency) / 5))
811
-
812
811
#define SPI_CDNS_INIT (n ) \
813
812
static void spi_cdns_irq_config_##n(void); \
814
813
static struct spi_cdns_data spi_cdns_data_##n = { \
@@ -819,7 +818,7 @@ static DEVICE_API(spi, spi_cdns_api) = {
819
818
.base = DT_INST_REG_ADDR(n), \
820
819
.irq_config = spi_cdns_irq_config_##n, \
821
820
.clock_frequency = DT_INST_PROP(n, clock_frequency), \
822
- .ext_clock = SPI_CLOCK_FREQUENCY_EXT(n), \
821
+ .ext_clock = DT_INST_PROP_OR(n, clock_frequency_ext, 0), \
823
822
.fifo_width = DT_INST_PROP(n, fifo_width), \
824
823
.tx_fifo_depth = DT_INST_PROP(n, tx_fifo_depth), \
825
824
.rx_fifo_depth = DT_INST_PROP(n, rx_fifo_depth), \
0 commit comments