stm32h7: support SDRAM via FMC peripherial (#459)

* stm32h7: Add support for SDRAM connected to FMC

* stm32h7: nxstyle fixes in stm32_fmc.h and stm32h7x3xx_rcc.{c,h}
This commit is contained in:
Andrey Zabolotnyi
2020-03-25 18:34:15 +03:00
committed by GitHub
parent 2991836ee8
commit 3ff956dc00
9 changed files with 1507 additions and 1087 deletions
+6
View File
@@ -58,6 +58,12 @@
# define getreg32(a) (*(volatile uint32_t *)(a))
# define putreg32(v,a) (*(volatile uint32_t *)(a) = (v))
/* Non-atomic, but more effective modification of registers */
# define modreg8(v,m,a) putreg8((getreg8(a) & ~(m)) | ((v) & (m)), a)
# define modreg16(v,m,a) putreg16((getreg16(a) & ~(m)) | ((v) & (m)), a)
# define modreg32(v,m,a) putreg32((getreg32(a) & ~(m)) | ((v) & (m)), a)
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
+7 -11
View File
@@ -289,9 +289,13 @@ config STM32H7_ETHMAC
select ARCH_HAVE_PHY
config STM32H7_FMC
bool "FMC"
default n
depends on STM32H7_HAVE_FMC
bool "FMC"
default n
depends on STM32H7_HAVE_FMC
---help---
Enable Flexible Memory Controller.
To correctly configure FMC for your hardware, you will have to define
a number of macros in your board.h file. See stm32_fmc.c for directions.
config STM32H7_OTGFS
bool "OTG FS"
@@ -1187,14 +1191,6 @@ endif # STM32H7_RTC_LSECLOCK
endmenu # RTC Configuration
config STM32H7_EXTERNAL_RAM
bool "External RAM on FMC"
default n
depends on STM32H7_FMC
select ARCH_HAVE_HEAP2
---help---
In addition to internal SDRAM, external RAM may be available through the FMC.
menu "QuadSPI Configuration"
depends on STM32H7_QUADSPI
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+125 -115
View File
@@ -60,33 +60,57 @@
#include "hardware/stm32_memorymap.h"
#include "stm32_mpuinit.h"
#include "stm32_dtcm.h"
#include "stm32_fmc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Internal SRAM is available in all members of the STM32 family. The
* following definitions must be provided to specify the size and
* location of internal(system) SRAM:
/* At startup the kernel will invoke up_addregion() so that platform code
* may register available memories for use as part of system heap.
* The global configuration option CONFIG_MM_REGIONS defines the maximal
* number of non-contiguous memory ranges that may be registered with the
* system heap. You must make sure it is large enough to hold all memory
* regions you intend to use.
*
* In addition to internal SRAM, external RAM may also be available through
* the FMC. In order to use FMC RAM, the following additional things need
* to be present in the NuttX configuration file:
* The following memory types can be used for heap on STM32H7 platform:
*
* CONFIG_STM32H7_FMC=y : Enables the FMC
* CONFIG_STM32H7_FMC_S[D]RAM=y : SRAM and/or SDRAM is available via the FMC.
* Either of these autoselects
* CONFIG_ARCH_HAVE_HEAP2 which is what we
* are interested in here.
* CONFIG_HEAP2_BASE : The base address of the external RAM in
* the FMC address space
* CONFIG_HEAP2_SIZE : The size of the external RAM in the FMC
* address space
* CONFIG_MM_REGIONS : Must be set to a large enough value to
* include the FMC external RAM (as determined
* by the rules provided below)
* - AXI SRAM is a 512kb memory area. This will be automatically registered
* with the system heap in up_allocate_heap, all the other memory
* regions will be registered in up_addregion().
* So, CONFIG_MM_REGIONS must be at least 1 to use AXI SRAM.
*
* CONFIG_STM32H7_DTCMEXCLUDE : Set to exclude the DTCM from heap
* - Internal SRAM is available in all members of the STM32 family.
* This is always registered with system heap.
* There are two contiguous regions of internal SRAM:
* SRAM1+SRAM2+SRAM3 and SRAM4 at a separate address.
* So, add 2 more to CONFIG_MM_REGIONS.
*
* - Tightly Coupled Memory (TCM RAM), we can use Data TCM (DTCM) for system
* heap. Note that DTCM has a number of limitations, for example DMA
* transfers to/from DTCM are limited.
* Define CONFIG_STM32H7_DTCMEXCLUDE to exclude the DTCM from heap.
* +1 to CONFIG_MM_REGIONS if you want to use DTCM.
*
* - External SDRAM can be connected to the FMC peripherial. Initialization
* of FMC is done as up_addregion() will invoke stm32_fmc_init().
* Please read the comment in stm32_fmc.c how to initialize FMC
* correctly.
*
* Then, up to two regions of SDRAM may be registered with the heap:
*
* - BOARD_SDRAM1_SIZE, if defined, declares the size of SDRAM
* at address STM32_FMC_BANK5. +1 to CONFIG_MM_REGIONS.
* - BOARD_SDRAM2_SIZE, if defined, declares the size of SDRAM
* at address STM32_FMC_BANK6. +1 to CONFIG_MM_REGIONS.
*
* - Additionaly, you may use the following options to add one more region
* of memory to system heap:
*
* - CONFIG_ARCH_HAVE_HEAP2=y
* - CONFIG_HEAP2_BASE=base address of memory area.
* - CONFIG_HEAP2_SIZE=size of memory area.
* - +1 to CONFIG_MM_REGIONS
*/
/* Set the start and end of the SRAMs */
@@ -114,28 +138,6 @@
# undef HAVE_DTCM
#endif
/* There are <x> possible heap configurations:
*
* Configuration 1. System SRAM (only)
* CONFIG_MM_REGIONS == 1
* Configuration 2. System SRAM and SRAM123
* CONFIG_MM_REGIONS == 2
* Configuration 3. System SRAM and SRAM123 and DTCM
* CONFIG_MM_REGIONS == 3
* HAVE_DTCM defined
* Configuration 4. System SRAM and SRAM123 and DTCM and SRAM4
* CONFIG_MM_REGIONS == 4
* HAVE_DTCM defined
* Configuration 5. System SRAM and SRAM123 and DTCM and SRAM4 and FMC RAM
* CONFIG_MM_REGIONS == 5
* HAVE_DTCM defined
*
* TODO ....
*
* Let's make sure that all definitions are consistent before doing
* anything else
*/
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -204,7 +206,8 @@ void up_allocate_heap(FAR void **heap_start, size_t *heap_size)
* of CONFIG_MM_KERNEL_HEAPSIZE (subject to alignment).
*/
uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend + CONFIG_MM_KERNEL_HEAPSIZE;
uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend +
CONFIG_MM_KERNEL_HEAPSIZE;
size_t usize = SRAM123_END - ubase;
int log2;
@@ -246,6 +249,10 @@ void up_allocate_heap(FAR void **heap_start, size_t *heap_size)
up_heap_color(*heap_start, *heap_size);
#endif
/* Display memory ranges to help debugging */
minfo("%uKb of SRAM at %p\n", *heap_size / 1024, *heap_start);
}
/****************************************************************************
@@ -266,7 +273,8 @@ void up_allocate_kheap(FAR void **heap_start, size_t *heap_size)
* of CONFIG_MM_KERNEL_HEAPSIZE (subject to alignment).
*/
uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend + CONFIG_MM_KERNEL_HEAPSIZE;
uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend +
CONFIG_MM_KERNEL_HEAPSIZE;
size_t usize = SRAM123_END - ubase;
int log2;
@@ -292,6 +300,38 @@ void up_allocate_kheap(FAR void **heap_start, size_t *heap_size)
}
#endif
/****************************************************************************
* Name: addregion
*
* Description:
* Make a range of memory available for allocation from system heap.
* If debug is disabled, compiler should optimize out the "desc" strings.
*
****************************************************************************/
static void addregion (uintptr_t start, uint32_t size, const char *desc)
{
/* Display memory ranges to help debugging */
minfo("%uKb of %s at %p\n", size / 1024, desc, (FAR void *)start);
#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP)
/* Allow user-mode access to the SRAM123 heap */
stm32_mpu_uheap(start, size);
#endif
/* Colorize the heap for debug */
up_heap_color((FAR void *)start, size);
/* Add the SRAM123 user heap region. */
kumm_addregion((FAR void *)start, size);
}
/****************************************************************************
* Name: up_addregion
*
@@ -301,81 +341,51 @@ void up_allocate_kheap(FAR void **heap_start, size_t *heap_size)
*
****************************************************************************/
#if CONFIG_MM_REGIONS > 1
void up_addregion(void)
{
# if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP)
addregion (SRAM123_START, SRAM123_END - SRAM123_START, "SRAM1,2,3");
/* Allow user-mode access to the SRAM123 heap */
unsigned mm_regions = 1;
stm32_mpu_uheap((uintptr_t)SRAM123_START, SRAM123_END - SRAM123_START);
if (mm_regions < CONFIG_MM_REGIONS)
{
addregion (SRAM4_START, SRAM4_END - SRAM4_START, "SRAM4");
mm_regions++;
}
# endif
/* Colorize the heap for debug */
up_heap_color((FAR void *)SRAM123_START, SRAM123_END - SRAM123_START);
/* Add the SRAM123 user heap region. */
kumm_addregion((FAR void *)SRAM123_START, SRAM123_END - SRAM123_START);
# if CONFIG_MM_REGIONS > 2
# if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP)
/* Allow user-mode access to the SRAM4 user heap memory */
stm32_mpu_uheap((uintptr_t)SRAM4_START, SRAM4_END - SRAM4_START);
# endif
/* Colorize the heap for debug */
up_heap_color((FAR void *)SRAM4_START, SRAM4_END - SRAM4_START);
/* Add the external SRAM4 user heap region. */
kumm_addregion((FAR void *)SRAM4_START, SRAM4_END - SRAM4_START);
# endif
# if CONFIG_MM_REGIONS > 3
# ifdef HAVE_DTCM
# if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP)
/* Allow user-mode access to the DTCM heap */
stm32_mpu_uheap((uintptr_t)DTCM_START, DTCM_END - DTCM_START);
# endif
/* Colorize the heap for debug */
up_heap_color((FAR void *)DTCM_START, DTCM_END - DTCM_START);
/* Add the DTCM user heap region. */
kumm_addregion((FAR void *)DTCM_START, DTCM_END - DTCM_START);
# endif
# endif
# if CONFIG_MM_REGIONS > 4
# ifdef CONFIG_ARCH_HAVE_HEAP2
# if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP)
/* Allow user-mode access to the FMC RAM user heap memory */
stm32_mpu_uheap((uintptr_t)CONFIG_HEAP2_BASE, CONFIG_HEAP2_SIZE);
# endif
/* Colorize the heap for debug */
up_heap_color((FAR void *)CONFIG_HEAP2_BASE, CONFIG_HEAP2_SIZE);
/* Add the external FMC RAM user heap region. */
kumm_addregion((FAR void *)CONFIG_HEAP2_BASE, CONFIG_HEAP2_SIZE);
# endif
# endif
}
#ifdef HAVE_DTCM
if (mm_regions < CONFIG_MM_REGIONS)
{
addregion (DTCM_START, DTCM_END - DTCM_START, "DTCM");
mm_regions++;
}
#endif
#ifdef CONFIG_STM32H7_FMC
stm32_fmc_init();
#endif
#ifdef BOARD_SDRAM1_SIZE
if (mm_regions < CONFIG_MM_REGIONS)
{
addregion (STM32_FMC_BANK5, BOARD_SDRAM1_SIZE, "SDRAM1");
mm_regions++;
}
#endif
#ifdef BOARD_SDRAM2_SIZE
if (mm_regions < CONFIG_MM_REGIONS)
{
addregion (STM32_FMC_BANK6, BOARD_SDRAM2_SIZE, "SDRAM2");
mm_regions++;
}
#endif
#ifdef CONFIG_ARCH_HAVE_HEAP2
if (mm_regions < CONFIG_MM_REGIONS)
{
addregion (CONFIG_HEAP2_BASE, CONFIG_HEAP2_SIZE, "HEAP2");
mm_regions++;
}
#endif
}
+387 -19
View File
@@ -39,40 +39,410 @@
#include <nuttx/config.h>
#if defined(CONFIG_STM32H7_FMC)
#include "stm32.h"
#if defined(CONFIG_STM32H7_FMC)
#include <debug.h>
#include <nuttx/arch.h>
#include <arch/board/board.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
/****************************************************************************
* To use FMC, you must first enable it in configuration:
*
* CONFIG_STM32H7_FMC=y
*
* FMC is statically configured at startup. Its configuration is adjusted
* using BOARD_XXX macros described below, which should be declared
* in your board.h file.
*
* The BOARD_FMC_CLK macro can be defined to select the clock source for FMC.
* This can be one of RCC_D1CCIPR_FMCSEL_xxx macros, and defaults to
* RCC_D1CCIPR_FMCSEL_PLL2 (which means PLL2R).
*
*
* To correctly initialize GPIO pins the macro BOARD_FMC_GPIO_CONFIGS should
* define a list of 32-bit GPIO configuration bitmasks. Example:
*
* #define BOARD_FMC_GPIO_CONFIGS GPIO_FMC_A0,GPIO_FMC_A1,\
* GPIO_FMC_A2,...,GPIO_FMC_D0,...,GPIO_FMC_NBL0,...
*
*
* For SRAM/NOR-Flash memories FMC defines 4 x 64Mb sub-banks at addresses
* 0x60000000, 0x64000000, 0x68000000, 0x6C000000. The following macros
* can be defined in board.h to initialize FMC for use with SRAM/NOR-Flash:
*
* *** CURRENTLY SRAM/NOR-FLASH SUPPORT IS NOT IMPLEMENTED ***
*
* BOARD_FMC_BCR[1..4] - Initial value for control registers
* for subbanks 1-4.
* BOARD_FMC_BTR[1..4] - Initial value for SRAM/NOR-Flash timing registers
* for subbanks 1-4.
* BOARD_FMC_BWTR[1..4] - Initial value for SRAM/NOR-Flash write timing
* registers for subbanks 1-4.
*
*
* For NAND flash memories FMC reserves 64Mb at 0x80000000. Define the
* following macros in your board.h file to initialize FMC for this type
* of memory:
*
* *** CURRENTLY NAND FLASH SUPPORT IS NOT IMPLEMENTED ***
*
* BOARD_FMC_PCR - Initial value for NAND flash control register.
* BOARD_FMC_PMEM - Initial value for NAND flash common memory space timing
* register.
* BOARD_FMC_PATT - Initial value for NAND flash attribute memory space
* timing register.
*
*
* For SDRAM memory FMC reserves 2 x 256Mb address ranges at 0xC0000000
* and 0xD0000000. Define the following macros to initialize FMC to work
* with SDRAM:
*
* BOARD_FMC_SDCR[1..2] - Initial value for SDRAM control registers for SDRAM
* bank 1-2. Note that some bits in SDCR1 influence both SDRAM banks and
* are unused in SDCR2!
* BOARD_FMC_SDTR[1..2] - Initial value for SDRAM timing registeres for SDRAM
* bank 1-2. Note that some bits in SDTR1 influence both SDRAM banks and
* are unused in SDTR2!
* BOARD_FMC_SDRAM_REFR_PERIOD - The SDRAM refresh rate period in FMC clocks,
* OR
* BOARD_FMC_SDRAM_REFR_CYCLES and BOARD_FMC_SDRAM_REFR_PERIOD can be used to
* automatically compute refresh counter using data from SDRAM datasheet
* (cycles usually is 4096, 8192 and such, and typical period is 64 ms)
* and knowing FMC clock frequency.
* BOARD_FMC_SDRAM_AUTOREFRESH may be defined to a number between 1 and 16 to
* issue given number of SDRAM auto-refresh cycles before using it. This
* defaults to 3.
*
*
* Special notes:
* - FMC bank remapping (FMC_BCR_BMAP*) is not currently supported.
*
*
* Here's a working example of a configured IS42S16320D SDRAM on a particular
* board:
*
*#define BOARD_SDRAM1_SIZE (64*1024*1024)
*
*#define BOARD_FMC_SDCR1 \
* (FMC_SDCR_COLBITS_10 | FMC_SDCR_ROWBITS_13 | FMC_SDCR_WIDTH_16 |\
* FMC_SDCR_BANKS_4 | FMC_SDCR_CASLAT_2 | FMC_SDCR_SDCLK_2X |\
* FMC_SDCR_BURST_READ | FMC_SDCR_RPIPE_1)
*#define BOARD_FMC_SDTR1 \
* (FMC_SDTR_TMRD(2) | FMC_SDTR_TXSR(7) | FMC_SDTR_TRAS(4) | \
* FMC_SDTR_TRC(7) | FMC_SDTR_TWR(4) | FMC_SDTR_TRP(2) | \
* FMC_SDTR_TRCD(2))
*#define BOARD_FMC_SDRAM_REFR_CYCLES 8192
*#define BOARD_FMC_SDRAM_REFR_PERIOD 64
*#define BOARD_FMC_SDRAM_MODE \
* FMC_SDCMR_MRD_BURST_LENGTH_8 | \
* FMC_SDCMR_MRD_BURST_TYPE_SEQUENTIAL | \
* FMC_SDCMR_MRD_CAS_LATENCY_2
*
*#define BOARD_FMC_GPIO_CONFIGS \
* GPIO_FMC_A0,GPIO_FMC_A1,GPIO_FMC_A2,GPIO_FMC_A3,GPIO_FMC_A4,\
* GPIO_FMC_A5,GPIO_FMC_A6,GPIO_FMC_A7,GPIO_FMC_A8,GPIO_FMC_A9,\
* GPIO_FMC_A10,GPIO_FMC_A11,GPIO_FMC_A12,\
* GPIO_FMC_D0,GPIO_FMC_D1,GPIO_FMC_D2,GPIO_FMC_D3,GPIO_FMC_D4,\
* GPIO_FMC_D5,GPIO_FMC_D6,GPIO_FMC_D7,GPIO_FMC_D8,GPIO_FMC_D9,\
* GPIO_FMC_D10,GPIO_FMC_D11,GPIO_FMC_D12,GPIO_FMC_D13,\
* GPIO_FMC_D14,GPIO_FMC_D15,\
* GPIO_FMC_NBL0,GPIO_FMC_NBL1,GPIO_FMC_BA0,GPIO_FMC_BA1,\
* GPIO_FMC_SDNWE_2,GPIO_FMC_SDNCAS,GPIO_FMC_SDNRAS,\
* GPIO_FMC_SDNE0_1,GPIO_FMC_SDCKE0_1,GPIO_FMC_SDCLK
*
****************************************************************************/
#ifndef BOARD_FMC_CLK
/* Clock FMC from PLL2R by default */
# define BOARD_FMC_CLK RCC_D1CCIPR_FMCSEL_PLL2
#endif
/* A couple of macros to know if user uses SDRAM1 and/or SDRAM2 */
#if defined BOARD_FMC_SDCR1 && ((BOARD_FMC_SDCR1 & FMC_SDCR_CASLAT_MASK) != 0)
# define HAVE_FMC_SDRAM1 1
#endif
#if defined BOARD_FMC_SDCR2 && ((BOARD_FMC_SDCR2 & FMC_SDCR_CASLAT_MASK) != 0)
# define HAVE_FMC_SDRAM2 1
#endif
#if defined HAVE_FMC_SDRAM1 || defined HAVE_FMC_SDRAM2
# define HAVE_FMC_SDRAM 1
#endif
/* Number of auto-refresh cycles to issue at SDRAM initialization */
#ifndef BOARD_FMC_SDRAM_AUTOREFRESH
# define BOARD_FMC_SDRAM_AUTOREFRESH 3
#endif
/* Find out the clock frequency for FMC */
#if BOARD_FMC_CLK == RCC_D1CCIPR_FMCSEL_HCLK
# define FMC_CLK_FREQUENCY STM32_HCLK_FREQUENCY
#elif BOARD_FMC_CLK == RCC_D1CCIPR_FMCSEL_PLL1
# define FMC_CLK_FREQUENCY STM32_PLL1Q_FREQUENCY
#elif BOARD_FMC_CLK == RCC_D1CCIPR_FMCSEL_PLL2
# define FMC_CLK_FREQUENCY STM32_PLL2R_FREQUENCY
#elif BOARD_FMC_CLK == RCC_D1CCIPR_FMCSEL_PER
# define FMC_CLK_FREQUENCY STM32_PER_FREQUENCY
#else
# error "BOARD_FMC_CLK has unknown value!"
#endif
/* Compute the refresh rate in clocks */
#ifndef BOARD_FMC_SDRAM_REFR_PERIOD
# if !defined(BOARD_FMC_SDRAM_REFR_CYCLES) || !defined(BOARD_FMC_SDRAM_REFR_PERIOD)
# error "Both BOARD_FMC_SDRAM_REFR_CYCLES and BOARD_FMC_SDRAM_REFR_PERIOD have to be defined to compute BOARD_FMC_SDRAM_REFR_PERIOD!"
# else
/* Take care not to overflow on 32-bit arithmetic */
# define BOARD_FMC_SDRAM_REFR_PERIOD ((uint32_t)((uint64_t)BOARD_FMC_SDRAM_REFR_PERIOD * FMC_CLK_FREQUENCY / BOARD_FMC_SDRAM_REFR_CYCLES - 20))
# endif
#endif /* BOARD_FMC_SDRAM_REFR_PERIOD */
/* The bits in FMC_SDCR we will alter at initialization */
#define FMC_SDCR_MASK (FMC_SDCR_COLBITS_MASK | FMC_SDCR_ROWBITS_MASK | \
FMC_SDCR_WIDTH_MASK | FMC_SDCR_BANKS_MASK | FMC_SDCR_CASLAT_MASK | FMC_SDCR_WP | \
FMC_SDCR_SDCLK_MASK | FMC_SDCR_BURST_READ | FMC_SDCR_RPIPE_MASK)
/* The bits in FMC_SDTR we will alter at initialization */
#define FMC_SDTR_MASK (FMC_SDTR_TMRD_MASK | FMC_SDTR_TXSR_MASK | \
FMC_SDTR_TRAS_MASK | FMC_SDTR_TRC_MASK | FMC_SDTR_TWR_MASK | FMC_SDTR_TRP_MASK | \
FMC_SDTR_TRCD_MASK)
/* The timeout while waiting for SDRAM controller to initialize, in us */
#define SDRAM_INIT_TIMEOUT (1000)
/****************************************************************************
* Private data
****************************************************************************/
static uint32_t fmc_gpios[] =
{
BOARD_FMC_GPIO_CONFIGS
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void stm32_fmc_sdram_init(void);
static int stm32_fmc_sdram_wait(unsigned timeout);
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_fmc_enable
* Name: stm32_fmc_init
*
* Description:
* Enable clocking to the FMC.
* Initialize the FMC peripherial. Because FMC initialization is highly
* dependent on the used parts, definition of the initial values for FMC
* registers is mostly left to board designer.
*
* Typically called from up_addregion().
*
****************************************************************************/
void stm32_fmc_enable_clk(void)
void stm32_fmc_init(void)
{
modifyreg32(STM32_RCC_AHB3ENR, 0, RCC_AHB3ENR_FMCEN);
uint32_t regval;
/* Reset the FMC on the AHB3 bus */
regval = getreg32(STM32_RCC_AHB3RSTR);
regval |= RCC_AHB3RSTR_FMCRST;
putreg32(regval, STM32_RCC_AHB3RSTR);
/* Leave reset state */
regval &= ~RCC_AHB3RSTR_FMCRST;
putreg32(regval, STM32_RCC_AHB3RSTR);
/* Set FMC clocking */
modreg32 (BOARD_FMC_CLK, RCC_D1CCIPR_FMCSEL_MASK, STM32_RCC_D1CCIPR);
/* Set up FMC GPIOs */
for (regval = 0; regval < ARRAY_SIZE(fmc_gpios); regval++)
stm32_configgpio(fmc_gpios[regval]);
/* Set up FMC registers */
#ifdef BOARD_FMC_SDCR1
modreg32(BOARD_FMC_SDCR1, FMC_SDCR_MASK, STM32_FMC_SDCR1);
#endif
#ifdef BOARD_FMC_SDCR2
modreg32(BOARD_FMC_SDCR2, FMC_SDCR_MASK, STM32_FMC_SDCR2);
#endif
#ifdef BOARD_FMC_SDTR1
modreg32(BOARD_FMC_SDTR1, FMC_SDTR_MASK, STM32_FMC_SDTR1);
#endif
#ifdef BOARD_FMC_SDTR2
modreg32(BOARD_FMC_SDTR2, FMC_SDTR_MASK, STM32_FMC_SDTR2);
#endif
/* Enable the FMC peripherial */
modreg32(FMC_BCR_FMCEN, FMC_BCR_FMCEN, STM32_FMC_BCR1);
/* Initialize the SDRAM chips themselves */
#ifdef HAVE_FMC_SDRAM
stm32_fmc_sdram_init();
#endif
/* Set up SDRAM refresh timings */
#ifdef BOARD_FMC_SDRAM_REFR_PERIOD
/* ... The programmed COUNT value must not be equal to the sum of the
* following timings: TWR+TRP+TRC+TRCD+4 memory clock cycles
*/
#ifdef BOARD_FMC_SDTR1
DEBUGASSERT (BOARD_FMC_SDRAM_REFR_PERIOD !=
(4 +
(1 + ((BOARD_FMC_SDTR1 & FMC_SDTR_TWR_MASK) >> FMC_SDTR_TWR_SHIFT)) +
(1 + ((BOARD_FMC_SDTR1 & FMC_SDTR_TRP_MASK) >> FMC_SDTR_TRP_SHIFT)) +
(1 + ((BOARD_FMC_SDTR1 & FMC_SDTR_TRC_MASK) >> FMC_SDTR_TRC_SHIFT)) +
(1 + ((BOARD_FMC_SDTR1 & FMC_SDTR_TRCD_MASK) >> FMC_SDTR_TRCD_SHIFT))));
#endif
#ifdef BOARD_FMC_SDTR2
DEBUGASSERT (BOARD_FMC_SDRAM_REFR_PERIOD !=
(4 +
(1 + ((BOARD_FMC_SDTR2 & FMC_SDTR_TWR_MASK) >> FMC_SDTR_TWR_SHIFT)) +
(1 + ((BOARD_FMC_SDTR2 & FMC_SDTR_TRP_MASK) >> FMC_SDTR_TRP_SHIFT)) +
(1 + ((BOARD_FMC_SDTR2 & FMC_SDTR_TRC_MASK) >> FMC_SDTR_TRC_SHIFT)) +
(1 + ((BOARD_FMC_SDTR2 & FMC_SDTR_TRCD_MASK) >> FMC_SDTR_TRCD_SHIFT))));
#endif
stm32_fmc_sdram_set_refresh_rate(BOARD_FMC_SDRAM_REFR_PERIOD);
#endif /* BOARD_FMC_SDRTR */
}
#ifdef HAVE_FMC_SDRAM
/****************************************************************************
* Name: stm32_fmc_sdram_init
*
* Description:
* Initialize the SDRAM chips.
*
****************************************************************************/
#if defined HAVE_FMC_SDRAM1 && defined HAVE_FMC_SDRAM2
# define FMC_SDCMR_CTB FMC_SDCMR_CTB1 | FMC_SDCMR_CTB2
#elif defined HAVE_FMC_SDRAM1
# define FMC_SDCMR_CTB FMC_SDCMR_CTB1
#else
# define FMC_SDCMR_CTB FMC_SDCMR_CTB2
#endif
void stm32_fmc_sdram_init(void)
{
/* What is happening here:
* ... A 100μs delay is required prior to issuing any command [...]
* a PRECHARGE command should be applied once the 100μs delay has been
* satisfied.
*
* All banks must be precharged. This will leave all banks in an idle
* state after which at least two AUTO REFRESH cycles must be performed.
* After the AUTO REFRESH cycles are complete, the SDRAM is then ready
* for mode register programming.
*
* The mode register should be loaded prior to applying any operational
* command because it will power up in an unknown state.
*/
up_udelay(100);
/* Clock Configuration Enable */
stm32_fmc_sdram_command(FMC_SDCMR_CTB | FMC_SDCMR_MODE_CLK_ENABLE);
/* Wait for clock to stabilize */
up_mdelay(1);
/* Precharge all banks */
stm32_fmc_sdram_wait(SDRAM_INIT_TIMEOUT);
stm32_fmc_sdram_command(FMC_SDCMR_CTB | FMC_SDCMR_MODE_PALL);
/* Auto refresh */
stm32_fmc_sdram_wait(SDRAM_INIT_TIMEOUT);
stm32_fmc_sdram_command(FMC_SDCMR_CTB | FMC_SDCMR_MODE_AUTO_REFRESH |
FMC_SDCMR_NRFS(BOARD_FMC_SDRAM_AUTOREFRESH));
/* Program the SDRAM mode register */
/* If using two SDRAM chips, this will write same mode for both.
* If different mode is required for every chip (why?), it would be
* needed to split code into two separate LOAD_MODE commands,
* one for every SDRAM chip.
*/
stm32_fmc_sdram_wait(SDRAM_INIT_TIMEOUT);
stm32_fmc_sdram_command(FMC_SDCMR_CTB | FMC_SDCMR_MODE_LOAD_MODE |
BOARD_FMC_SDRAM_MODE);
}
/****************************************************************************
* Name: stm32_fmc_disable
* Name: stm32_fmc_sdram_wait
*
* Description:
* Disable clocking to the FMC.
* Wait until SDRAM controller is ready for all configured SDRAM banks.
*
* Parameters:
* timeout - number of microseconds before giving up waiting.
*
* Returns:
* 0 if all configured SDRAM banks are ready, -1 on timeout
*
****************************************************************************/
void stm32_fmc_disable(void)
int stm32_fmc_sdram_wait(unsigned timeout)
{
modifyreg32(STM32_RCC_AHB3ENR, RCC_AHB3ENR_FMCEN, 0);
/* Wait until the SDRAM controller is ready */
for (; timeout; timeout--)
{
uint32_t sdsr = getreg32(STM32_FMC_SDSR);
if (1
#if defined HAVE_FMC_SDRAM1
&& ((sdsr & FMC_SDSR_MODES1_MASK) == FMC_SDSR_MODES1_NORMAL)
#endif
#if defined HAVE_FMC_SDRAM2
&& ((sdsr & FMC_SDSR_MODES2_MASK) == FMC_SDSR_MODES2_NORMAL)
#endif
)
return 0;
up_udelay(1);
}
return -1;
}
#endif
/****************************************************************************
* Name: stm32_fmc_sdram_write_protect
*
@@ -92,11 +462,11 @@ void stm32_fmc_sdram_write_protect(int bank, bool state)
val = getreg32(sdcr);
if (state)
{
val |= FMC_SDRAM_CR_WRITE_PROTECT; /* wp == 1 */
val |= FMC_SDCR_WP; /* wp == 1 */
}
else
{
val &= ~FMC_SDRAM_CR_WRITE_PROTECT; /* wp == 0 */
val &= ~FMC_SDCR_WP; /* wp == 0 */
}
putreg32(val, sdcr);
@@ -112,10 +482,8 @@ void stm32_fmc_sdram_write_protect(int bank, bool state)
void stm32_fmc_sdram_set_refresh_rate(int count)
{
uint32_t val;
DEBUGASSERT(count <= 0x1fff && count >= 0x29);
putreg32(count << 1, STM32_FMC_SDRTR);
putreg32(FMC_SDRTR_COUNT(count), STM32_FMC_SDRTR);
}
/****************************************************************************
@@ -132,11 +500,11 @@ void stm32_fmc_sdram_set_timing(int bank, uint32_t timing)
uint32_t sdtr;
DEBUGASSERT((bank == 1) || (bank == 2));
DEBUGASSERT((timing & FMC_SDRAM_TR_RESERVED) == 0);
DEBUGASSERT((timing & FMC_SDTR_RESERVED) == 0);
sdtr = (bank == 1) ? STM32_FMC_SDTR1 : STM32_FMC_SDTR2;
val = getreg32(sdtr);
val &= FMC_SDRAM_TR_RESERVED; /* preserve reserved bits */
val &= FMC_SDTR_RESERVED; /* preserve reserved bits */
val |= timing;
putreg32(val, sdtr);
}
@@ -170,11 +538,11 @@ void stm32_fmc_sdram_set_control(int bank, uint32_t ctrl)
uint32_t sdcr;
DEBUGASSERT((bank == 1) || (bank == 2));
DEBUGASSERT((ctrl & FMC_SDRAM_CR_RESERVED) == 0);
DEBUGASSERT((ctrl & FMC_SDCR_RESERVED) == 0);
sdcr = (bank == 1) ? STM32_FMC_SDCR1 : STM32_FMC_SDCR2;
val = getreg32(sdcr);
val &= FMC_SDRAM_CR_RESERVED; /* preserve reserved bits */
val &= FMC_SDCR_RESERVED; /* preserve reserved bits */
val |= ctrl;
putreg32(val, sdcr);
}
@@ -189,7 +557,7 @@ void stm32_fmc_sdram_set_control(int bank, uint32_t ctrl)
void stm32_fmc_sdram_command(uint32_t cmd)
{
DEBUGASSERT((cmd & FMC_SDRAM_CMD_RESERVED) == 0);
DEBUGASSERT((cmd & FMC_SDCMR_RESERVED) == 0);
putreg32(cmd, STM32_FMC_SDCMR);
}
+11 -7
View File
@@ -45,10 +45,6 @@
#include "chip.h"
#include "hardware/stm32_fmc.h"
/****************************************************************************
* Public Functions
****************************************************************************/
#ifndef __ASSEMBLY__
#undef EXTERN
@@ -61,14 +57,22 @@ extern "C"
#endif
/****************************************************************************
* Name: stm32_fmc_sdram_wait
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: stm32_fmc_init
*
* Description:
* Wait for the SDRAM controller to be ready.
* Initialize the FMC peripherial. Because FMC initialization is highly
* dependent on the used parts, definition of the initial values for FMC
* registers is mostly left to board designer.
*
* Typically called from up_addregion().
*
****************************************************************************/
void stm32_fmc_sdram_wait(void);
void stm32_fmc_init(void);
/****************************************************************************
* Name: stm32_fmc_enable
+7 -1
View File
@@ -298,6 +298,12 @@ static inline void rcc_enableahb3(void)
regval |= RCC_AHB3ENR_SDMMC1EN;
#endif
#ifdef CONFIG_STM32H7_FMC
/* Flexible static memory controller module clock enable */
regval |= RCC_AHB3ENR_FMCEN;
#endif
/* TODO: ... */
putreg32(regval, STM32_RCC_AHB3ENR); /* Enable peripherals */
@@ -775,7 +781,7 @@ static void stm32_stdclockconfig(void)
}
#endif
/* Ww must write the lower byte of the PWR_CR3 register is written once
/* We must write the lower byte of the PWR_CR3 register is written once
* after POR and it shall be written before changing VOS level or
* ck_sys clock frequency. No limitation applies to the upper bytes.
*
+13 -7
View File
@@ -142,8 +142,8 @@ static inline void rcc_reset(void)
putreg32(regval, STM32_RCC_CR);
#if defined(CONFIG_STM32H7_AXI_SRAM_CORRUPTION_WAR)
/* Errata 2.2.9 Enable workaround for Reading from AXI SRAM may lead to data
* read corruption. See ES0392 Rev 6.
/* Errata 2.2.9 Enable workaround for Reading from AXI SRAM may lead to
* data read corruption. See ES0392 Rev 6.
*/
putreg32(AXI_TARG_READ_ISS_OVERRIDE, STM32_AXI_TARG7_FN_MOD);
@@ -298,6 +298,12 @@ static inline void rcc_enableahb3(void)
regval |= RCC_AHB3ENR_SDMMC1EN;
#endif
#ifdef CONFIG_STM32H7_FMC
/* Flexible static memory controller module clock enable */
regval |= RCC_AHB3ENR_FMCEN;
#endif
/* TODO: ... */
putreg32(regval, STM32_RCC_AHB3ENR); /* Enable peripherals */
@@ -775,16 +781,16 @@ static void stm32_stdclockconfig(void)
}
#endif
/* Ww must write the lower byte of the PWR_CR3 register is written once
* after POR and it shall be written before changing VOS level or ck_sys
* clock frequency. No limitation applies to the upper bytes.
/* We must write the lower byte of the PWR_CR3 register is written once
* after POR and it shall be written before changing VOS level or
* ck_sys clock frequency. No limitation applies to the upper bytes.
*
* Programming data corresponding to an invalid combination of
* LDOEN and BYPASS bits will be ignored: data will not be written,
* the written-once mechanism will lock the register and any further
* write access will be ignored. The default supply configuration will
* be kept and the ACTVOSRDY bit in PWR control status register 1 (PWR_CSR1)
* will go on indicating invalid voltage levels.
* be kept and the ACTVOSRDY bit in PWR control status register 1
* (PWR_CSR1) will go on indicating invalid voltage levels.
*
* N.B. The system shall be power cycled before writing a new value.
*/