Skip to content

Commit 2bda9ad

Browse files
XenuIsWatchingdkalowsk
authored andcommitted
drivers: spi: cdns: fixup spi clk divisor
Remove the auto setting of the external spi clock if its not there, also fix the calculation of calucation the spi divisor value. Signed-off-by: Ryan McClelland <[email protected]>
1 parent 241bb05 commit 2bda9ad

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

drivers/spi/spi_cdns.c

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -218,33 +218,6 @@ static inline void spi_cdns_cs_control(const struct device *dev, bool on)
218218
}
219219
}
220220

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-
248221
/**
249222
* @brief Send 1-entry to Tx-FIFO
250223
*
@@ -439,7 +412,8 @@ static int spi_cdns_configure(const struct device *dev, const struct spi_config
439412
{
440413
const struct spi_cdns_cfg *dev_config = dev->config;
441414
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;
443417

444418
if (spi_cdns_context_configured(dev, config)) {
445419
/* Nothing to do */
@@ -473,7 +447,7 @@ static int spi_cdns_configure(const struct device *dev, const struct spi_config
473447
data->ctx.config = config;
474448
data->config = *config;
475449

476-
conf_val = SPI_CONF_PCSL_MASK | SPI_CONF_MCSE | SPI_CONF_MRCS;
450+
conf_val = SPI_CONF_PCSL_MASK | SPI_CONF_MCSE;
477451

478452
/* Configure for Master or Slave */
479453
if (config->operation & SPI_OP_MODE_SLAVE) {
@@ -501,7 +475,38 @@ static int spi_cdns_configure(const struct device *dev, const struct spi_config
501475
* SPI clock is generated based on pclk or ext_clk, and the frequency closest
502476
* to the value obtained by dividing the two base clocks is selected.
503477
*/
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));
505510

506511
/* Set transfer word size */
507512
conf_val &= ~(SPI_CONF_TWS_MASK);
@@ -803,12 +808,6 @@ static DEVICE_API(spi, spi_cdns_api) = {
803808
#endif /* CONFIG_SPI_RTIO */
804809
};
805810

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-
812811
#define SPI_CDNS_INIT(n) \
813812
static void spi_cdns_irq_config_##n(void); \
814813
static struct spi_cdns_data spi_cdns_data_##n = { \
@@ -819,7 +818,7 @@ static DEVICE_API(spi, spi_cdns_api) = {
819818
.base = DT_INST_REG_ADDR(n), \
820819
.irq_config = spi_cdns_irq_config_##n, \
821820
.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), \
823822
.fifo_width = DT_INST_PROP(n, fifo_width), \
824823
.tx_fifo_depth = DT_INST_PROP(n, tx_fifo_depth), \
825824
.rx_fifo_depth = DT_INST_PROP(n, rx_fifo_depth), \

0 commit comments

Comments
 (0)