nand_scheme.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /****************************************************************************
  2. * include/nuttx/mtd/nand_scheme.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_SCHEME_H
  42. #define __INCLUDE_NUTTX_MTD_SCHEME_H
  43. /****************************************************************************
  44. * Included Files
  45. ****************************************************************************/
  46. #include <nuttx/config.h>
  47. #include <nuttx/mtd/nand_config.h>
  48. #include <stdint.h>
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Public Types
  54. ****************************************************************************/
  55. struct nand_scheme_s
  56. {
  57. uint8_t bbpos; /* Bad block position marker */
  58. uint8_t eccsize; /* Number of bytes of ECC correction */
  59. uint8_t nxbytes; /* Number of extra bytes */
  60. /* ECC byte position offsets */
  61. uint8_t eccbytepos[CONFIG_MTD_NAND_MAXSPAREECCBYTES];
  62. /* Extra byte position offsets */
  63. uint8_t xbytepos[CONFIG_MTD_NAND_MAXSPAREEXTRABYTES];
  64. };
  65. /****************************************************************************
  66. * Public Data
  67. ****************************************************************************/
  68. #ifndef __ASSEMBLY__
  69. #ifdef __cplusplus
  70. #define EXTERN extern "C"
  71. extern "C"
  72. {
  73. #else
  74. #define EXTERN extern
  75. #endif
  76. EXTERN const struct nand_scheme_s g_nand_sparescheme256;
  77. EXTERN const struct nand_scheme_s g_nand_sparescheme512;
  78. EXTERN const struct nand_scheme_s g_nand_sparescheme2048;
  79. EXTERN const struct nand_scheme_s g_nand_sparescheme4096;
  80. /****************************************************************************
  81. * Public Function Prototypes
  82. ****************************************************************************/
  83. /****************************************************************************
  84. * Name: nandscheme_readbadblockmarker
  85. *
  86. * Description:
  87. * Reads the bad block marker inside a spare area buffer using the provided
  88. * scheme.
  89. *
  90. * Input Parameters:
  91. * scheme Pointer to a nand_scheme_s instance.
  92. * spare Spare area buffer.
  93. * marker Pointer to the variable to store the bad block marker.
  94. *
  95. * Returned Value:
  96. * None
  97. *
  98. ****************************************************************************/
  99. void nandscheme_readbadblockmarker(FAR const struct nand_scheme_s *scheme,
  100. FAR const uint8_t *spare,
  101. FAR uint8_t *marker);
  102. /****************************************************************************
  103. * Name: nandscheme_readbadblockmarker
  104. *
  105. * Description:
  106. * Modifies the bad block marker inside a spare area, using the given
  107. * scheme.
  108. *
  109. * Input Parameters:
  110. * scheme Pointer to a nand_scheme_s instance.
  111. * spare Spare area buffer.
  112. * marker Bad block marker to write.
  113. *
  114. * Returned Value:
  115. * None
  116. *
  117. ****************************************************************************/
  118. void nandscheme_writebadblockmarker(FAR const struct nand_scheme_s *scheme,
  119. FAR uint8_t *spare, uint8_t marker);
  120. /****************************************************************************
  121. * Name: nandscheme_eccoffset
  122. *
  123. * Description:
  124. * Return the offset to the first byte of ECC information. This define
  125. * makes the assumption that the first byte of the eccbytepos[] array
  126. * is an offset to beginning of the ECC area. This might not necessarily
  127. * be true!
  128. *
  129. * Input Parameters:
  130. * scheme Pointer to a nand_scheme_s instance.
  131. *
  132. * Returned Value:
  133. * Offset in the spare area to the first ECC byte
  134. *
  135. ****************************************************************************/
  136. #define nandscheme_eccoffset(s) ((s)->eccbytepos[0])
  137. /****************************************************************************
  138. * Name: nandscheme_eccsize
  139. *
  140. * Description:
  141. * Return the size of the ECC information in the spare area
  142. *
  143. * Input Parameters:
  144. * scheme Pointer to a nand_scheme_s instance.
  145. *
  146. * Returned Value:
  147. * Size of the ECC information in the spare area.
  148. *
  149. ****************************************************************************/
  150. #define nandscheme_eccsize(s) ((s)->eccsize)
  151. /****************************************************************************
  152. * Name: nandscheme_readecc
  153. *
  154. * Description:
  155. * Reads ECC information from a spare area using the provided scheme.
  156. *
  157. * Input Parameters:
  158. * scheme Pointer to a nand_scheme_s instance.
  159. * spare Spare area buffer.
  160. * ecc ECC buffer.
  161. *
  162. * Returned Value:
  163. * None
  164. *
  165. ****************************************************************************/
  166. void nandscheme_readecc(FAR const struct nand_scheme_s *scheme,
  167. FAR const uint8_t *spare, FAR uint8_t *ecc);
  168. /****************************************************************************
  169. * Name: nandschem_writeecc
  170. *
  171. * Description:
  172. * Writes ECC information in a spare area, using a particular scheme.
  173. *
  174. * Input Parameters:
  175. * scheme Pointer to a nand_scheme_s instance.
  176. * spare Spare area buffer.
  177. * ecc ECC buffer.
  178. *
  179. * Returned Value:
  180. * None
  181. *
  182. ****************************************************************************/
  183. void nandscheme_writeecc(FAR const struct nand_scheme_s *scheme,
  184. FAR uint8_t *spare, FAR const uint8_t *ecc);
  185. /****************************************************************************
  186. * Name: nandscheme_xoffset
  187. *
  188. * Description:
  189. * Return the offset to the first byte of extra information. This define
  190. * makes the assumption that the first byte of the xbytepos[] array
  191. * is an offset to the beginning of the extra information area. This
  192. * might not necessarily be true!
  193. *
  194. * Input Parameters:
  195. * scheme Pointer to a nand_scheme_s instance.
  196. *
  197. * Returned Value:
  198. * Offset in the spare area to the first extra byte
  199. *
  200. ****************************************************************************/
  201. #define nandscheme_xoffset(s) ((s)->xbytepos[0])
  202. /****************************************************************************
  203. * Name: nandscheme_xsize
  204. *
  205. * Description:
  206. * Return the size of the extra information in the spare area
  207. *
  208. * Input Parameters:
  209. * scheme Pointer to a nand_scheme_s instance.
  210. *
  211. * Returned Value:
  212. * Size of the extra information in the spare area.
  213. *
  214. ****************************************************************************/
  215. #define nandscheme_xsize(s) ((s)->nxbytes)
  216. /****************************************************************************
  217. * Name: nandscheme_readextra
  218. *
  219. * Description:
  220. * Reads extra bytes of information from a spare area, using the provided
  221. * scheme.
  222. *
  223. * Input Parameters:
  224. * scheme Pointer to a nand_scheme_s instance.
  225. * spare Spare area buffer.
  226. * extra Extra bytes buffer.
  227. * size Number of extra bytes to read.
  228. * offset Index where to read the first extra byte.
  229. *
  230. * Returned Value:
  231. * None
  232. *
  233. ****************************************************************************/
  234. void nandscheme_readextra(FAR const struct nand_scheme_s *scheme,
  235. FAR const uint8_t *spare, FAR void *extra,
  236. unsigned int size, unsigned int offset);
  237. /****************************************************************************
  238. * Name: nandscheme_readextra
  239. *
  240. * Description:
  241. * Write extra bytes of information inside a spare area, using the provided
  242. * scheme.
  243. *
  244. * Input Parameters:
  245. * scheme Pointer to a nand_scheme_s instance.
  246. * spare Spare area buffer.
  247. * extra Extra bytes buffer.
  248. * size Number of extra bytes to write.
  249. * offset Index where to write the first extra byte.
  250. *
  251. * Returned Value:
  252. * None
  253. *
  254. ****************************************************************************/
  255. void nandscheme_writeextra(FAR const struct nand_scheme_s *scheme,
  256. FAR uint8_t *spare, FAR const void *extra,
  257. unsigned int size, unsigned int offset);
  258. /****************************************************************************
  259. * Name: nandscheme_readextra
  260. *
  261. * Description:
  262. * Build a scheme instance for 4096 page size nand flash
  263. *
  264. * Input Parameters:
  265. * scheme Pointer to a nand_scheme_s instance.
  266. * spareSize Size of spare area.
  267. * offset Index where to write the first extra byte.
  268. * size Number of extra bytes to write.
  269. * offset Index where to write the first extra byte.
  270. *
  271. * Returned Value:
  272. * OK on success; a negated errno value on failure.
  273. *
  274. ****************************************************************************/
  275. int nandscheme_build4086(FAR struct nand_scheme_s *scheme,
  276. unsigned int spareSize, unsigned int eccOffset);
  277. #undef EXTERN
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. #endif /* __ASSEMBLY__ */
  282. #endif /* __INCLUDE_NUTTX_MTD_SCHEME_H */