Skip to content

Commit 8bdf1e9

Browse files
committed
litespi: use new clk divisor
use new clk divisor, that is used for SDR and DDR from litex-hub/litespi#86 Signed-off-by: Fin Maaß <[email protected]>
1 parent b9a1bd2 commit 8bdf1e9

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

litex/soc/integration/soc.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,8 +2128,10 @@ def add_spi_flash(self, name="spiflash", mode="4x", clk_freq=20e6, module=None,
21282128

21292129
# Checks/Parameters.
21302130
assert mode in ["1x", "4x"]
2131-
default_divisor = math.ceil(self.sys_clk_freq/(2*clk_freq)) - 1
2132-
clk_freq = int(self.sys_clk_freq/(2*(default_divisor + 1)))
2131+
default_divisor = math.ceil(self.sys_clk_freq/clk_freq)
2132+
if rate == "1:1":
2133+
default_divisor += default_divisor % 2 # Round up to nearest even number.
2134+
clk_freq = int(self.sys_clk_freq/default_divisor)
21332135

21342136
if "master_with_irq" not in kwargs and self.irq.enabled and name in self.irq.locs.keys():
21352137
# If IRQ is enabled, use master_with_irq.
@@ -2154,6 +2156,7 @@ def add_spi_flash(self, name="spiflash", mode="4x", clk_freq=20e6, module=None,
21542156

21552157
# Constants.
21562158
self.add_constant(f"{name}_PHY_FREQUENCY", clk_freq)
2159+
self.add_constant(f"{name}_PHY_MIN_DIVISOR", 2 if rate == "1:1" else 1)
21572160
self.add_constant(f"{name}_MODULE_NAME", module.name)
21582161
self.add_constant(f"{name}_MODULE_TOTAL_SIZE", module.total_size)
21592162
self.add_constant(f"{name}_MODULE_PAGE_SIZE", module.page_size)
@@ -2179,8 +2182,10 @@ def add_spi_ram(self, name="spiram", mode="4x", clk_freq=20e6, module=None, phy=
21792182

21802183
# Checks/Parameters.
21812184
assert mode in ["1x", "4x"]
2182-
default_divisor = math.ceil(self.sys_clk_freq/(2*clk_freq)) - 1
2183-
clk_freq = int(self.sys_clk_freq/(2*(default_divisor + 1)))
2185+
default_divisor = math.ceil(self.sys_clk_freq/clk_freq)
2186+
if rate == "1:1":
2187+
default_divisor += default_divisor % 2 # Round up to nearest even number.
2188+
clk_freq = int(self.sys_clk_freq/default_divisor)
21842189

21852190
if "master_with_irq" not in kwargs and self.irq.enabled and name in self.irq.locs.keys():
21862191
# If IRQ is enabled, use master_with_irq.
@@ -2226,6 +2231,7 @@ def add_spi_ram(self, name="spiram", mode="4x", clk_freq=20e6, module=None, phy=
22262231

22272232
# Constants.
22282233
self.add_constant(f"{name}_PHY_FREQUENCY", clk_freq)
2234+
self.add_constant(f"{name}_PHY_MIN_DIVISOR", 2 if rate == "1:1" else 1)
22292235
self.add_constant(f"{name}_MODULE_NAME", module.name)
22302236
self.add_constant(f"{name}_MODULE_TOTAL_SIZE", module.total_size)
22312237
self.add_constant(f"{name}_MODULE_PAGE_SIZE", module.page_size)

litex/soc/software/liblitespi/spiflash.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ int spiflash_freq_init(void)
4040
printf("First SPI Flash block erased, unable to perform freq test.\n\r");
4141
return -1;
4242
}
43-
44-
while((crc == crc_test) && (lowest_div-- > 0)) {
43+
#if defined(SPIFLASH_PHY_MIN_DIVISOR) && SPIFLASH_PHY_MIN_DIVISOR == 1
44+
while((crc == crc_test) && (lowest_div-- > 1)) {
45+
#else
46+
while((crc == crc_test) && ((lowest_div -= 2) >= 2)) {
47+
#endif
4548
spiflash_phy_clk_divisor_write((uint32_t)lowest_div);
4649
flush_cpu_dcache();
4750
flush_l2_cache();
@@ -50,10 +53,15 @@ int spiflash_freq_init(void)
5053
printf("[DIV: %d] %08x\n\r", lowest_div, crc_test);
5154
#endif
5255
}
56+
#if defined(SPIFLASH_PHY_MIN_DIVISOR) && SPIFLASH_PHY_MIN_DIVISOR == 1
5357
lowest_div++;
54-
printf("SPI Flash clk configured to %d MHz\n", CONFIG_CLOCK_FREQUENCY/(2*(1+lowest_div)*1000000));
58+
#else
59+
lowest_div += 2;
60+
#endif
61+
printf("SPI Flash clk configured to %d MHz (div: %d)\n", CONFIG_CLOCK_FREQUENCY/(lowest_div*1000000), lowest_div);
5562

5663
spiflash_phy_clk_divisor_write(lowest_div);
64+
spiflash_mmap_clk_divisor_write(lowest_div);
5765

5866
#else
5967

0 commit comments

Comments
 (0)