exmc_nandflash.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*!
  2. \file exmc_nandflash.h
  3. \brief the header file of nandflash driver
  4. \version 2023-06-30, V2.1.6, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2023, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #ifndef EXMC_NANDFLASH_H
  30. #define EXMC_NANDFLASH_H
  31. #include "gd32f30x.h"
  32. #include "string.h"
  33. /* NAND area definition */
  34. /* A16 = CLE high command area */
  35. #define EXMC_CMD_AREA (uint32_t)(1<<16)
  36. /* A17 = ALE high address area */
  37. #define EXMC_ADDR_AREA (uint32_t)(1<<17)
  38. /* data area */
  39. #define EXMC_DATA_AREA ((uint32_t)0x00000000)
  40. /* NAND memory command (hynix HY27UF081G2A) */
  41. #define NAND_CMD_READ1_1ST ((uint8_t)0x00)
  42. #define NAND_CMD_READ1_2ND ((uint8_t)0x30)
  43. #define NAND_CMD_WRITE_1ST ((uint8_t)0x80)
  44. #define NAND_CMD_WRITE_2ND ((uint8_t)0x10)
  45. #define NAND_CMD_ERASE_1ST ((uint8_t)0x60)
  46. #define NAND_CMD_ERASE_2ND ((uint8_t)0xD0)
  47. #define NAND_CMD_READID ((uint8_t)0x90)
  48. #define NAND_CMD_STATUS ((uint8_t)0x70)
  49. #define NAND_CMD_LOCK_STATUS ((uint8_t)0x7A)
  50. #define NAND_CMD_RESET ((uint8_t)0xFF)
  51. /* NAND memory status */
  52. #define NAND_BUSY ((uint8_t)0x00)
  53. #define NAND_ERROR ((uint8_t)0x01)
  54. #define NAND_READY ((uint8_t)0x40)
  55. #define NAND_TIMEOUT_ERROR ((uint8_t)0x80)
  56. /* NAND memory parameters */
  57. #define NAND_ZONE_COUNT ((uint16_t)0x0001) /* NAND zone count */
  58. #define NAND_PAGE_SIZE ((uint16_t)0x0800) /* 2 * 1024 bytes per page */
  59. #define NAND_BLOCK_SIZE ((uint16_t)0x0040) /* 64 pages per block */
  60. #define NAND_ZONE_SIZE ((uint16_t)0x0400) /* 1024 block per zone */
  61. #define NAND_SPARE_AREA_SIZE ((uint16_t)0x0040) /* last 128 bytes as spare area */
  62. #define NAND_BLOCK_COUNT 1024 /* block count */
  63. #define NAND_PAGE_TOTAL_SIZE (NAND_PAGE_SIZE + NAND_SPARE_AREA_SIZE) /* total page size = page size + spare are size */
  64. /* command code type define */
  65. #define NAND_CMD_COPYBACK_A ((uint8_t)0x00) /* PAGE COPY-BACK command sequence */
  66. #define NAND_CMD_COPYBACK_B ((uint8_t)0x35)
  67. #define NAND_CMD_COPYBACK_C ((uint8_t)0x85)
  68. #define NAND_CMD_COPYBACK_D ((uint8_t)0x10)
  69. #define NAND_CMD_STATUS ((uint8_t)0x70) /* Read NAND Flash status */
  70. #define MAX_PHY_BLOCKS_PER_ZONE 1024
  71. #define MAX_LOG_BLOCKS_PER_ZONE 1000
  72. #define NAND_BAD_BLOCK_FLAG 0x00
  73. #define NAND_USED_BLOCK_FLAG 0xFE
  74. #define BI_OFFSET 0
  75. #define USED_OFFSET 1
  76. #define LBN0_OFFSET 2
  77. #define LBN1_OFFSET 3
  78. #define VALID_SPARE_SIZE 4
  79. #define NAND_OK 0
  80. #define NAND_FAIL 1
  81. #define BLOCK_ODD 0
  82. #define BLOCK_EVEN 1
  83. #define FREE_BLOCK (1 << 12)
  84. #define BAD_BLOCK (1 << 13)
  85. #define VALID_BLOCK (1 << 14)
  86. #define USED_BLOCK (1 << 15)
  87. #define EXCHANGE_BLOCKS_NUM 2
  88. #define BAD_BALOK_TEST_CYCLE 5
  89. #define DATA_BLOCK_PERCENT 98
  90. #define ADDR_1ST_CYCLE(ADDR) (uint8_t)((ADDR)& 0xFF) /* 1st addressing cycle */
  91. #define ADDR_2ND_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF00) >> 8) /* 2nd addressing cycle */
  92. #define ADDR_3RD_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF0000) >> 16) /* 3rd addressing cycle */
  93. #define ADDR_4TH_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF000000) >> 24) /* 4th addressing cycle */
  94. /* NAND id structure */
  95. typedef struct {
  96. uint8_t maker_id;
  97. uint8_t device_id;
  98. uint8_t third_id;
  99. uint8_t fourth_id;
  100. } nand_id_struct;
  101. /* NAND address structure */
  102. typedef struct {
  103. uint16_t zone;
  104. uint16_t block;
  105. uint16_t page;
  106. uint16_t page_in_offset;
  107. } nand_address_struct;
  108. /* function declarations */
  109. /* nand flash peripheral initialize */
  110. uint8_t exmc_nandflash_init(uint32_t nand_bank);
  111. /* read NAND flash ID */
  112. void nand_read_id(nand_id_struct *nand_id);
  113. /* write data to nand flash */
  114. uint8_t nand_write(uint32_t memaddr, uint8_t *pwritebuf, uint16_t bytecount);
  115. /* read data from nand flash */
  116. uint8_t nand_read(uint32_t mem_addr, uint8_t *preadbuf, uint16_t bytecount);
  117. /* scan and test specific block */
  118. uint8_t nand_scan_block(uint32_t pagenum);
  119. /* check block is free block */
  120. uint8_t nand_judge_free_block(uint32_t blocknum);
  121. #endif /* EXMC_NANDFLASH_H */