Skip to content

Commit 8177be5

Browse files
gautierg-stkartben
authored andcommitted
drivers: memc: compute prescaler automatically for stm32 xspi
For STM32 XSPI PSRAM driver, compute and set the prescaler automatically according to the kernel clock and the max frequency of the PSRAM. Copied from what is done in the STM32 XSPI Flash driver. Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
1 parent a156ba3 commit 8177be5

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

drivers/memc/memc_stm32_xspi_psram.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ LOG_MODULE_REGISTER(memc_stm32_xspi_psram, CONFIG_MEMC_LOG_LEVEL);
4040
#define DUMMY_CLK_CYCLES_READ 6U
4141
#define DUMMY_CLK_CYCLES_WRITE 6U
4242

43+
#define STM32_XSPI_CLOCK_PRESCALER_MIN 0U
44+
#define STM32_XSPI_CLOCK_PRESCALER_MAX 255U
45+
#define STM32_XSPI_CLOCK_COMPUTE(bus_freq, prescaler) ((bus_freq) / ((prescaler) + 1U))
46+
4347
struct memc_stm32_xspi_psram_config {
4448
const struct pinctrl_dev_config *pcfg;
4549
const struct stm32_pclken pclken;
4650
const struct stm32_pclken pclken_ker;
4751
const struct stm32_pclken pclken_mgr;
4852
size_t memory_size;
53+
uint32_t max_frequency;
4954
};
5055

5156
struct memc_stm32_xspi_psram_data {
@@ -206,6 +211,7 @@ static int memc_stm32_xspi_psram_init(const struct device *dev)
206211
XSPIM_CfgTypeDef cfg = {0};
207212
XSPI_RegularCmdTypeDef cmd = {0};
208213
XSPI_MemoryMappedTypeDef mem_mapped_cfg = {0};
214+
uint32_t prescaler = STM32_XSPI_CLOCK_PRESCALER_MIN;
209215
int ret;
210216

211217
/* Signals configuration */
@@ -259,6 +265,20 @@ static int memc_stm32_xspi_psram_init(const struct device *dev)
259265
}
260266
#endif
261267

268+
for (; prescaler <= STM32_XSPI_CLOCK_PRESCALER_MAX; prescaler++) {
269+
uint32_t clk = STM32_XSPI_CLOCK_COMPUTE(ahb_clock_freq, prescaler);
270+
271+
if (clk <= dev_cfg->max_frequency) {
272+
break;
273+
}
274+
}
275+
276+
if (prescaler > STM32_XSPI_CLOCK_PRESCALER_MAX) {
277+
LOG_ERR("XSPI could not find valid prescaler value");
278+
return -EINVAL;
279+
}
280+
281+
hxspi.Init.ClockPrescaler = prescaler;
262282
hxspi.Init.MemorySize = find_msb_set(dev_cfg->memory_size) - 2;
263283

264284
if (HAL_XSPI_Init(&hxspi) != HAL_OK) {
@@ -339,6 +359,7 @@ static const struct memc_stm32_xspi_psram_config memc_stm32_xspi_cfg = {
339359
.enr = DT_CLOCKS_CELL_BY_NAME(STM32_XSPI_NODE, xspi_mgr, bits)},
340360
#endif
341361
.memory_size = DT_INST_PROP(0, size) / 8, /* In Bytes */
362+
.max_frequency = DT_INST_PROP(0, max_frequency),
342363
};
343364

344365
static struct memc_stm32_xspi_psram_data memc_stm32_xspi_data = {
@@ -354,7 +375,6 @@ static struct memc_stm32_xspi_psram_data memc_stm32_xspi_data = {
354375
.FreeRunningClock = HAL_XSPI_FREERUNCLK_DISABLE,
355376
.ClockMode = HAL_XSPI_CLOCK_MODE_0,
356377
.WrapSize = HAL_XSPI_WRAP_NOT_SUPPORTED,
357-
.ClockPrescaler = 3U,
358378
.SampleShifting = HAL_XSPI_SAMPLE_SHIFT_NONE,
359379
.DelayHoldQuarterCycle = HAL_XSPI_DHQC_ENABLE,
360380
.ChipSelectBoundary = HAL_XSPI_BONDARYOF_16KB,

0 commit comments

Comments
 (0)