nand_raw.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /****************************************************************************
  2. * include/nuttx/mtd/nand_raw.h
  3. *
  4. * Copyright (C) 2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * This logic was based largely on Atmel sample code with modifications for
  8. * better integration with NuttX. The Atmel sample code has a BSD
  9. * compatible license that requires this copyright notice:
  10. *
  11. * Copyright (c) 2012, Atmel Corporation
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * 3. Neither the names NuttX nor Atmel nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  34. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  35. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ****************************************************************************/
  41. #ifndef __INCLUDE_NUTTX_MTD_NAND_RAW_H
  42. #define __INCLUDE_NUTTX_MTD_NAND_RAW_H
  43. /****************************************************************************
  44. * Included Files
  45. ****************************************************************************/
  46. #include <nuttx/config.h>
  47. #include <nuttx/mtd/nand_config.h>
  48. #include <stdint.h>
  49. #include <stdbool.h>
  50. #include <nuttx/mtd/mtd.h>
  51. #include <nuttx/mtd/nand_model.h>
  52. /****************************************************************************
  53. * Pre-processor Definitions
  54. ****************************************************************************/
  55. /* Nand flash commands */
  56. #define COMMAND_READ_1 0x00
  57. #define COMMAND_READ_2 0x30
  58. #define COMMAND_COPYBACK_READ_1 0x00
  59. #define COMMAND_COPYBACK_READ_2 0x35
  60. #define COMMAND_COPYBACK_PROGRAM_1 0x85
  61. #define COMMAND_COPYBACK_PROGRAM_2 0x10
  62. #define COMMAND_RANDOM_OUT 0x05
  63. #define COMMAND_RANDOM_OUT_2 0xe0
  64. #define COMMAND_RANDOM_IN 0x85
  65. #define COMMAND_READID 0x90
  66. #define COMMAND_WRITE_1 0x80
  67. #define COMMAND_WRITE_2 0x10
  68. #define COMMAND_ERASE_1 0x60
  69. #define COMMAND_ERASE_2 0xd0
  70. #define COMMAND_STATUS 0x70
  71. #define COMMAND_RESET 0xff
  72. /* Nand flash commands (small blocks) */
  73. #define COMMAND_READ_A 0x00
  74. #define COMMAND_READ_C 0x50
  75. /* Type of ECC to be performed (may also need to be enabled in the
  76. * configuration)
  77. *
  78. * NANDECC_NONE No ECC, only raw NAND FLASH accesses
  79. * NANDECC_SWECC Software ECC. Handled by the common MTD logic.
  80. * NANDECC_HWECC Values >= 2 are various hardware ECC implementations
  81. * all handled by the lower-half, raw NAND FLASH driver.
  82. * These hardware ECC types may be extended beginning
  83. * with the value NANDECC_HWECC.
  84. *
  85. * Software ECC is performed by common, upper-half MTD logic; All
  86. * hardware assisted ECC operations are handled by the platform-specific,
  87. * lower-half driver.
  88. */
  89. #define NANDECC_NONE 0
  90. #define NANDECC_SWECC 1
  91. #define NANDECC_HWECC 2
  92. /* NAND access macros */
  93. #define WRITE_COMMAND8(raw, command) \
  94. {*((volatile uint8_t *)(raw)->cmdaddr) = (uint8_t)command;}
  95. #define WRITE_COMMAND16(raw, command) \
  96. {*((volatile uint16_t *)(raw)->cmdaddr) = (uint16_t)command;}
  97. #define WRITE_ADDRESS8(raw, address) \
  98. {*((volatile uint8_t *)(raw)->addraddr) = (uint8_t)address;}
  99. #define WRITE_ADDRESS16(raw, address) \
  100. {*((volatile uint16_t *)(raw)->addraddr) = (uint16_t)address;}
  101. #define WRITE_DATA8(raw, data) \
  102. {*((volatile uint8_t *)(raw)->dataaddr) = (uint8_t)data;}
  103. #define READ_DATA8(raw) \
  104. (*((volatile uint8_t *)(raw)->dataaddr))
  105. #define WRITE_DATA16(raw, data) \
  106. {*((volatile uint16_t *)(raw)->dataaddr) = (uint16_t)data;}
  107. #define READ_DATA16(raw) \
  108. (*((volatile uint16_t *)(raw)->dataaddr))
  109. /* struct nand_raw_s operations */
  110. /****************************************************************************
  111. * Name: NAND_ERASEBLOCK
  112. *
  113. * Description:
  114. * Erases the specified block of the device.
  115. *
  116. * Input Parameters:
  117. * raw - Lower-half, raw NAND FLASH interface
  118. * block - Number of the physical block to erase.
  119. *
  120. * Returned Value:
  121. * OK is returned in success; a negated errno value is returned on failure.
  122. *
  123. ****************************************************************************/
  124. #define NAND_ERASEBLOCK(r,b) ((r)->eraseblock(r,b))
  125. /****************************************************************************
  126. * Name: NAND_RAWREAD
  127. *
  128. * Description:
  129. * Reads the data and/or the spare areas of a page of a NAND FLASH into the
  130. * provided buffers. This is a raw read of the flash contents.
  131. *
  132. * Input Parameters:
  133. * raw - Lower-half, raw NAND FLASH interface
  134. * block - Number of the block where the page to read resides.
  135. * page - Number of the page to read inside the given block.
  136. * data - Buffer where the data area will be stored.
  137. * spare - Buffer where the spare area will be stored.
  138. *
  139. * Returned Value:
  140. * OK is returned in success; a negated errno value is returned on failure.
  141. *
  142. ****************************************************************************/
  143. #define NAND_RAWREAD(r,b,p,d,s) ((r)->rawread(r,b,p,d,s))
  144. /****************************************************************************
  145. * Name: NAND_RAWWRITE
  146. *
  147. * Description:
  148. * Writes the data and/or the spare area of a page on a NAND FLASH chip.
  149. * This is a raw write of the flash contents.
  150. *
  151. * Input Parameters:
  152. * raw - Lower-half, raw NAND FLASH interface
  153. * block - Number of the block where the page to write resides.
  154. * page - Number of the page to write inside the given block.
  155. * data - Buffer containing the data to be writing
  156. * spare - Buffer containing the spare data to be written.
  157. *
  158. * Returned Value:
  159. * OK is returned in success; a negated errno value is returned on failure.
  160. *
  161. ****************************************************************************/
  162. #define NAND_RAWWRITE(r,b,p,d,s) ((r)->rawwrite(r,b,p,d,s))
  163. /****************************************************************************
  164. * Name: NAND_READPAGE
  165. *
  166. * Description:
  167. * Reads the data and/or the spare areas of a page of a NAND FLASH into the
  168. * provided buffers. Hardware ECC checking will be performed if so
  169. * configured.
  170. *
  171. * Input Parameters:
  172. * raw - Lower-half, raw NAND FLASH interface
  173. * block - Number of the block where the page to read resides.
  174. * page - Number of the page to read inside the given block.
  175. * data - Buffer where the data area will be stored.
  176. * spare - Buffer where the spare area will be stored.
  177. *
  178. * Returned Value:
  179. * OK is returned in success; a negated errno value is returned on failure.
  180. *
  181. ****************************************************************************/
  182. #ifdef CONFIG_MTD_NAND_HWECC
  183. # define NAND_READPAGE(r,b,p,d,s) ((r)->readpage(r,b,p,d,s))
  184. #else
  185. # define NAND_READPAGE(r,b,p,d,s) ((r)->rawread(r,b,p,d,s))
  186. #endif
  187. /****************************************************************************
  188. * Name: NAND_WRITEPAGE
  189. *
  190. * Description:
  191. * Writes the data and/or the spare area of a page on a NAND FLASH chip.
  192. * Hardware ECC checking will be performed if so configured.
  193. *
  194. * Input Parameters:
  195. * raw - Lower-half, raw NAND FLASH interface
  196. * block - Number of the block where the page to write resides.
  197. * page - Number of the page to write inside the given block.
  198. * data - Buffer containing the data to be writing
  199. * spare - Buffer containing the spare data to be written.
  200. *
  201. * Returned Value:
  202. * OK is returned in success; a negated errno value is returned on failure.
  203. *
  204. ****************************************************************************/
  205. #ifdef CONFIG_MTD_NAND_HWECC
  206. # define NAND_WRITEPAGE(r,b,p,d,s) ((r)->writepage(r,b,p,d,s))
  207. #else
  208. # define NAND_WRITEPAGE(r,b,p,d,s) ((r)->rawwrite(r,b,p,d,s))
  209. #endif
  210. /****************************************************************************
  211. * Public Types
  212. ****************************************************************************/
  213. /* This type represents the visible portion of the lower-half, raw NAND MTD
  214. * device. The lower-half driver may freely append additional information
  215. * after this required header information.
  216. */
  217. struct nand_raw_s
  218. {
  219. /* NAND data description */
  220. struct nand_model_s model; /* The NAND model storage */
  221. uintptr_t cmdaddr; /* NAND command address base */
  222. uintptr_t addraddr; /* NAND address address base */
  223. uintptr_t dataaddr; /* NAND data address */
  224. uint8_t ecctype; /* See NANDECC_* definitions */
  225. /* NAND operations */
  226. CODE int (*eraseblock)(FAR struct nand_raw_s *raw, off_t block);
  227. CODE int (*rawread)(FAR struct nand_raw_s *raw, off_t block,
  228. unsigned int page, FAR void *data, FAR void *spare);
  229. CODE int (*rawwrite)(FAR struct nand_raw_s *raw, off_t block,
  230. unsigned int page, FAR const void *data,
  231. FAR const void *spare);
  232. #ifdef CONFIG_MTD_NAND_HWECC
  233. CODE int (*readpage)(FAR struct nand_raw_s *raw, off_t block,
  234. unsigned int page, FAR void *data, FAR void *spare);
  235. CODE int (*writepage)(FAR struct nand_raw_s *raw, off_t block,
  236. unsigned int page, FAR const void *data,
  237. FAR const void *spare);
  238. #endif
  239. #if defined(CONFIG_MTD_NAND_SWECC) || defined(CONFIG_MTD_NAND_HWECC)
  240. /* ECC working buffers*/
  241. uint8_t spare[CONFIG_MTD_NAND_MAXPAGESPARESIZE];
  242. uint8_t ecc[CONFIG_MTD_NAND_MAXSPAREECCBYTES];
  243. #endif
  244. };
  245. /****************************************************************************
  246. * Public Data
  247. ****************************************************************************/
  248. #ifndef __ASSEMBLY__
  249. #ifdef __cplusplus
  250. #define EXTERN extern "C"
  251. extern "C"
  252. {
  253. #else
  254. #define EXTERN extern
  255. #endif
  256. /****************************************************************************
  257. * Public Function Prototypes
  258. ****************************************************************************/
  259. #undef EXTERN
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif /* __ASSEMBLY__ */
  264. #endif /* __INCLUDE_NUTTX_MTD_NAND_RAW_H */