summaryrefslogtreecommitdiff
path: root/bsp/imxrt/libraries/drivers/drv_sdram.c
blob: 25b7169dbca5e2443374f0a49e557c2ef59bef48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright 2017 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <rtthread.h>
#ifdef BSP_USING_SDRAM

#include "sdram_port.h"
#include "board.h"
#include "fsl_semc.h"
#include "drv_sdram.h"

//#define DBG_COLOR
//#define DRV_DEBUG
//#define LOG_TAG             "drv.sdram"
//#include <drv_log.h>

#define DBG_ENABLE
#define DBG_SECTION_NAME "[drv.sdram]"
#define DBG_COLOR
#define DBG_LEVEL DBG_INFO
#include <rtdbg.h>

#ifdef RT_USING_MEMHEAP
static struct rt_memheap system_heap;
#endif

int rt_hw_sdram_init(void)
{
    int result = RT_EOK;
    semc_config_t config;
    semc_sdram_config_t sdramconfig;

#if defined(SOC_IMXRT1170_SERIES)
    rt_uint32_t clockFrq = CLOCK_GetRootClockFreq(kCLOCK_Root_Semc);
#else
    rt_uint32_t clockFrq = CLOCK_GetFreq(kCLOCK_SemcClk);
#endif

    /* Initializes the MAC configure structure to zero. */
    rt_memset(&config, 0, sizeof(semc_config_t));
    rt_memset(&sdramconfig, 0, sizeof(semc_sdram_config_t));

    /* Initialize SEMC. */
    SEMC_GetDefaultConfig(&config);
    config.dqsMode = kSEMC_Loopbackdqspad;  /* For more accurate timing. */
    SEMC_Init(SEMC, &config);

    /* Configure SDRAM. */
    sdramconfig.csxPinMux               = SDRAM_CS_PIN;
    sdramconfig.address                 = SDRAM_BANK_ADDR;
    sdramconfig.memsize_kbytes          = SDRAM_SIZE;
    sdramconfig.portSize                = SDRAM_DATA_WIDTH;
    sdramconfig.burstLen                = kSEMC_Sdram_BurstLen8;
    sdramconfig.columnAddrBitNum        = SDRAM_COLUMN_BITS;
    sdramconfig.casLatency              = SDRAM_CAS_LATENCY;
    sdramconfig.tPrecharge2Act_Ns       = SDRAM_TRP;
    sdramconfig.tAct2ReadWrite_Ns       = SDRAM_TRCD;
    sdramconfig.tRefreshRecovery_Ns     = SDRAM_REFRESH_RECOVERY;
    sdramconfig.tWriteRecovery_Ns       = SDRAM_TWR;
    sdramconfig.tCkeOff_Ns              = 42;  /* The minimum cycle of SDRAM CLK off state. CKE is off in self refresh at a minimum period tRAS.*/
    sdramconfig.tAct2Prechage_Ns        = SDRAM_TRAS;
    sdramconfig.tSelfRefRecovery_Ns     = 67;
    sdramconfig.tRefresh2Refresh_Ns     = SDRAM_TRC;
    sdramconfig.tAct2Act_Ns             = SDRAM_ACT2ACT;
    sdramconfig.tPrescalePeriod_Ns      = 160 * (1000000000 / clockFrq);
    sdramconfig.refreshPeriod_nsPerRow  = SDRAM_REFRESH_ROW;
    sdramconfig.refreshUrgThreshold     = sdramconfig.refreshPeriod_nsPerRow;
    sdramconfig.refreshBurstLen         = 1;
    result = SEMC_ConfigureSDRAM(SEMC, SDRAM_REGION, &sdramconfig, clockFrq);
    if(result != kStatus_Success)
    {
        LOG_E("SDRAM init failed!");
        result = -RT_ERROR;
    }
    else
    {
        LOG_I("sdram init success, mapped at 0x%X, size is %d Kbytes.", SDRAM_BANK_ADDR, SDRAM_SIZE);
#ifdef RT_USING_MEMHEAP
    /*
     * If RT_USING_MEMHEAP is enabled, SDRAM is initialized to the heap.
     * The heap start address is (base + half size), and the size is (half size - 2M).
     * The reasons are:
     *      1. Reserve the half space for SDRAM link case
     *      2. Reserve the 2M for non-cache space
     */
        rt_memheap_init(&system_heap, "sdram", (void *)(SDRAM_BANK_ADDR + (SDRAM_SIZE * 1024)/2),
            (SDRAM_SIZE * 1024)/2 - (2 * 1024 * 1024));
#endif
    }

    return result;
}
INIT_PREV_EXPORT(rt_hw_sdram_init);

#ifdef DBG_ENABLE
#ifdef FINSH_USING_MSH

#define SEMC_DATALEN                (0x1000U)
rt_uint32_t sdram_writeBuffer[SEMC_DATALEN];
rt_uint32_t sdram_readBuffer[SEMC_DATALEN];

/* read write 32bit test */
static void sdram_test(void)
{
    rt_uint32_t index;
    rt_uint32_t datalen = SEMC_DATALEN;
    rt_uint32_t *sdram = (rt_uint32_t *)SDRAM_BANK_ADDR; /* SDRAM start address. */
    bool result = true;

    LOG_D("SEMC SDRAM Memory 32 bit Write Start, Start Address 0x%x, Data Length %d !", sdram, datalen);
    /* Prepare data and write to SDRAM. */
    for (index = 0; index < datalen; index++)
    {
        sdram_writeBuffer[index] = index;
        sdram[index] = sdram_writeBuffer[index];
    }

    LOG_D("SEMC SDRAM Read 32 bit Data Start, Start Address 0x%x, Data Length %d !", sdram, datalen);
    /* Read data from the SDRAM. */
    for (index = 0; index < datalen; index++)
    {
        sdram_readBuffer[index] = sdram[index];
    }

    LOG_I("SEMC SDRAM 32 bit Data Write and Read Compare Start!");
    /* Compare the two buffers. */
    while (datalen--)
    {
        if (sdram_writeBuffer[datalen] != sdram_readBuffer[datalen])
        {
            result = false;
            break;
        }
    }

    if (!result)
    {
        LOG_E("SEMC SDRAM 32 bit Data Write and Read Compare Failed!");
    }
    else
    {
        LOG_I("SEMC SDRAM 32 bit Data Write and Read Compare Succeed!");
    }
}
MSH_CMD_EXPORT(sdram_test, sdram test)

#endif /* DRV_DEBUG */
#endif /* FINSH_USING_MSH */
#endif /* BSP_USING_SDRAM */