drv_sdram.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_AS_HEAP
  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. rt_uint32_t clockFrq = CLOCK_GetFreq(kCLOCK_SemcClk);
  25. /* Initializes the MAC configure structure to zero. */
  26. memset(&config, 0, sizeof(semc_config_t));
  27. memset(&sdramconfig, 0, sizeof(semc_sdram_config_t));
  28. /* Initialize SEMC. */
  29. SEMC_GetDefaultConfig(&config);
  30. config.dqsMode = kSEMC_Loopbackdqspad; /* For more accurate timing. */
  31. SEMC_Init(SEMC, &config);
  32. /* Configure SDRAM. */
  33. sdramconfig.csxPinMux = SDRAM_CS_PIN;
  34. sdramconfig.address = SDRAM_BANK_ADDR;
  35. sdramconfig.memsize_kbytes = SDRAM_SIZE;
  36. sdramconfig.portSize = SDRAM_DATA_WIDTH;
  37. sdramconfig.burstLen = kSEMC_Sdram_BurstLen8;
  38. sdramconfig.columnAddrBitNum = SDRAM_COLUMN_BITS;
  39. sdramconfig.casLatency = SDRAM_CAS_LATENCY;
  40. sdramconfig.tPrecharge2Act_Ns = SDRAM_TRP;
  41. sdramconfig.tAct2ReadWrite_Ns = SDRAM_TRCD;
  42. sdramconfig.tRefreshRecovery_Ns = SDRAM_REFRESH_RECOVERY;
  43. sdramconfig.tWriteRecovery_Ns = SDRAM_TWR;
  44. sdramconfig.tCkeOff_Ns = 42; /* The minimum cycle of SDRAM CLK off state. CKE is off in self refresh at a minimum period tRAS.*/
  45. sdramconfig.tAct2Prechage_Ns = SDRAM_TRAS;
  46. sdramconfig.tSelfRefRecovery_Ns = 67;
  47. sdramconfig.tRefresh2Refresh_Ns = SDRAM_TRC;
  48. sdramconfig.tAct2Act_Ns = SDRAM_ACT2ACT;
  49. sdramconfig.tPrescalePeriod_Ns = 160 * (1000000000 / clockFrq);
  50. sdramconfig.refreshPeriod_nsPerRow = SDRAM_REFRESH_ROW;
  51. sdramconfig.refreshUrgThreshold = sdramconfig.refreshPeriod_nsPerRow;
  52. sdramconfig.refreshBurstLen = 1;
  53. result = SEMC_ConfigureSDRAM(SEMC, SDRAM_REGION, &sdramconfig, clockFrq);
  54. if(result != kStatus_Success)
  55. {
  56. LOG_E("SDRAM init failed!");
  57. result = -RT_ERROR;
  58. }
  59. else
  60. {
  61. LOG_D("sdram init success, mapped at 0x%X, size is %d Kbytes.", SDRAM_BANK_ADDR, SDRAM_SIZE);
  62. #ifdef RT_USING_MEMHEAP_AS_HEAP
  63. /*
  64. * If RT_USING_MEMHEAP_AS_HEAP is enabled, SDRAM is initialized to the heap.
  65. * The heap start address is (base + half size), and the size is (half size - 2M).
  66. * The reasons are:
  67. * 1. Reserve the half space for SDRAM link case
  68. * 2. Reserve the 2M for non-cache space
  69. */
  70. rt_memheap_init(&system_heap, "sdram", (void *)(SDRAM_BANK_ADDR + (SDRAM_SIZE * 1024)/2),
  71. (SDRAM_SIZE * 1024)/2 - (2 * 1024 * 1024));
  72. #endif
  73. }
  74. return result;
  75. }
  76. INIT_BOARD_EXPORT(rt_hw_sdram_Init);
  77. #ifdef DRV_DEBUG
  78. #ifdef FINSH_USING_MSH
  79. #define SEMC_DATALEN (0x1000U)
  80. rt_uint32_t sdram_writeBuffer[SEMC_DATALEN];
  81. rt_uint32_t sdram_readBuffer[SEMC_DATALEN];
  82. /* read write 32bit test */
  83. void sdram_test(void)
  84. {
  85. rt_uint32_t index;
  86. rt_uint32_t datalen = SEMC_DATALEN;
  87. rt_uint32_t *sdram = (rt_uint32_t *)SDRAM_BANK_ADDR; /* SDRAM start address. */
  88. bool result = true;
  89. LOG_D("\r\n SEMC SDRAM Memory 32 bit Write Start, Start Address 0x%x, Data Length %d !\r\n", sdram, datalen);
  90. /* Prepare data and write to SDRAM. */
  91. for (index = 0; index < datalen; index++)
  92. {
  93. sdram_writeBuffer[index] = index;
  94. sdram[index] = sdram_writeBuffer[index];
  95. }
  96. LOG_D("\r\n SEMC SDRAM Read 32 bit Data Start, Start Address 0x%x, Data Length %d !\r\n", sdram, datalen);
  97. /* Read data from the SDRAM. */
  98. for (index = 0; index < datalen; index++)
  99. {
  100. sdram_readBuffer[index] = sdram[index];
  101. }
  102. LOG_D("\r\n SEMC SDRAM 32 bit Data Write and Read Compare Start!\r\n");
  103. /* Compare the two buffers. */
  104. while (datalen--)
  105. {
  106. if (sdram_writeBuffer[datalen] != sdram_readBuffer[datalen])
  107. {
  108. result = false;
  109. break;
  110. }
  111. }
  112. if (!result)
  113. {
  114. LOG_E("\r\n SEMC SDRAM 32 bit Data Write and Read Compare Failed!\r\n");
  115. }
  116. else
  117. {
  118. LOG_D("\r\n SEMC SDRAM 32 bit Data Write and Read Compare Succeed!\r\n");
  119. }
  120. }
  121. MSH_CMD_EXPORT(sdram_test, sdram test)
  122. #endif /* DRV_DEBUG */
  123. #endif /* FINSH_USING_MSH */
  124. #endif /* BSP_USING_SDRAM */