drv_sdram.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2017 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <rtthread.h>
  8. #ifdef BSP_USING_SDRAM
  9. #include "sdram_port.h"
  10. #include "board.h"
  11. #include "fsl_semc.h"
  12. #include "drv_sdram.h"
  13. #define DRV_DEBUG
  14. #define LOG_TAG "drv.sdram"
  15. #include <drv_log.h>
  16. #ifdef RT_USING_MEMHEAP
  17. static struct rt_memheap system_heap;
  18. #endif
  19. int rt_hw_sdram_init(void)
  20. {
  21. int result = RT_EOK;
  22. semc_config_t config;
  23. semc_sdram_config_t sdramconfig;
  24. #if defined(SOC_IMXRT1170_SERIES)
  25. rt_uint32_t clockFrq = CLOCK_GetRootClockFreq(kCLOCK_Root_Semc);
  26. #else
  27. rt_uint32_t clockFrq = CLOCK_GetFreq(kCLOCK_SemcClk);
  28. #endif
  29. /* Initializes the MAC configure structure to zero. */
  30. rt_memset(&config, 0, sizeof(semc_config_t));
  31. rt_memset(&sdramconfig, 0, sizeof(semc_sdram_config_t));
  32. /* Initialize SEMC. */
  33. SEMC_GetDefaultConfig(&config);
  34. config.dqsMode = kSEMC_Loopbackdqspad; /* For more accurate timing. */
  35. SEMC_Init(SEMC, &config);
  36. /* Configure SDRAM. */
  37. sdramconfig.csxPinMux = SDRAM_CS_PIN;
  38. sdramconfig.address = SDRAM_BANK_ADDR;
  39. sdramconfig.memsize_kbytes = SDRAM_SIZE;
  40. sdramconfig.portSize = SDRAM_DATA_WIDTH;
  41. sdramconfig.burstLen = kSEMC_Sdram_BurstLen8;
  42. sdramconfig.columnAddrBitNum = SDRAM_COLUMN_BITS;
  43. sdramconfig.casLatency = SDRAM_CAS_LATENCY;
  44. sdramconfig.tPrecharge2Act_Ns = SDRAM_TRP;
  45. sdramconfig.tAct2ReadWrite_Ns = SDRAM_TRCD;
  46. sdramconfig.tRefreshRecovery_Ns = SDRAM_REFRESH_RECOVERY;
  47. sdramconfig.tWriteRecovery_Ns = SDRAM_TWR;
  48. sdramconfig.tCkeOff_Ns = 42; /* The minimum cycle of SDRAM CLK off state. CKE is off in self refresh at a minimum period tRAS.*/
  49. sdramconfig.tAct2Prechage_Ns = SDRAM_TRAS;
  50. sdramconfig.tSelfRefRecovery_Ns = 67;
  51. sdramconfig.tRefresh2Refresh_Ns = SDRAM_TRC;
  52. sdramconfig.tAct2Act_Ns = SDRAM_ACT2ACT;
  53. sdramconfig.tPrescalePeriod_Ns = 160 * (1000000000 / clockFrq);
  54. sdramconfig.refreshPeriod_nsPerRow = SDRAM_REFRESH_ROW;
  55. sdramconfig.refreshUrgThreshold = sdramconfig.refreshPeriod_nsPerRow;
  56. sdramconfig.refreshBurstLen = 1;
  57. result = SEMC_ConfigureSDRAM(SEMC, SDRAM_REGION, &sdramconfig, clockFrq);
  58. if(result != kStatus_Success)
  59. {
  60. LOG_E("SDRAM init failed!");
  61. result = -RT_ERROR;
  62. }
  63. else
  64. {
  65. LOG_D("sdram init success, mapped at 0x%X, size is %d Kbytes.", SDRAM_BANK_ADDR, SDRAM_SIZE);
  66. #ifdef RT_USING_MEMHEAP
  67. /*
  68. * If RT_USING_MEMHEAP is enabled, SDRAM is initialized to the heap.
  69. * The heap start address is (base + half size), and the size is (half size - 2M).
  70. * The reasons are:
  71. * 1. Reserve the half space for SDRAM link case
  72. * 2. Reserve the 2M for non-cache space
  73. */
  74. rt_memheap_init(&system_heap, "sdram", (void *)(SDRAM_BANK_ADDR + (SDRAM_SIZE * 1024)/2),
  75. (SDRAM_SIZE * 1024)/2 - (2 * 1024 * 1024));
  76. #endif
  77. }
  78. return result;
  79. }
  80. INIT_PREV_EXPORT(rt_hw_sdram_init);
  81. #ifdef DRV_DEBUG
  82. #ifdef FINSH_USING_MSH
  83. #define SEMC_DATALEN (0x1000U)
  84. rt_uint32_t sdram_writeBuffer[SEMC_DATALEN];
  85. rt_uint32_t sdram_readBuffer[SEMC_DATALEN];
  86. /* read write 32bit test */
  87. static void sdram_test(void)
  88. {
  89. rt_uint32_t index;
  90. rt_uint32_t datalen = SEMC_DATALEN;
  91. rt_uint32_t *sdram = (rt_uint32_t *)SDRAM_BANK_ADDR; /* SDRAM start address. */
  92. bool result = true;
  93. LOG_D("SEMC SDRAM Memory 32 bit Write Start, Start Address 0x%x, Data Length %d !", sdram, datalen);
  94. /* Prepare data and write to SDRAM. */
  95. for (index = 0; index < datalen; index++)
  96. {
  97. sdram_writeBuffer[index] = index;
  98. sdram[index] = sdram_writeBuffer[index];
  99. }
  100. LOG_D("SEMC SDRAM Read 32 bit Data Start, Start Address 0x%x, Data Length %d !", sdram, datalen);
  101. /* Read data from the SDRAM. */
  102. for (index = 0; index < datalen; index++)
  103. {
  104. sdram_readBuffer[index] = sdram[index];
  105. }
  106. LOG_D("SEMC SDRAM 32 bit Data Write and Read Compare Start!");
  107. /* Compare the two buffers. */
  108. while (datalen--)
  109. {
  110. if (sdram_writeBuffer[datalen] != sdram_readBuffer[datalen])
  111. {
  112. result = false;
  113. break;
  114. }
  115. }
  116. if (!result)
  117. {
  118. LOG_E("SEMC SDRAM 32 bit Data Write and Read Compare Failed!");
  119. }
  120. else
  121. {
  122. LOG_D("SEMC SDRAM 32 bit Data Write and Read Compare Succeed!");
  123. }
  124. }
  125. MSH_CMD_EXPORT(sdram_test, sdram test)
  126. #endif /* DRV_DEBUG */
  127. #endif /* FINSH_USING_MSH */
  128. #endif /* BSP_USING_SDRAM */