You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

228 lines
7.8 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Partially based on linux/include/linux/mtd/nand.h *
  6. * Copyright (C) 2000 David Woodhouse <dwmw2@mvhi.com> *
  7. * Copyright (C) 2000 Steven J. Hill <sjhill@realitydiluted.com> *
  8. * Copyright (C) 2000 Thomas Gleixner <tglx@linutronix.de> *
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. * This program is distributed in the hope that it will be useful, *
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  18. * GNU General Public License for more details. *
  19. * *
  20. * You should have received a copy of the GNU General Public License *
  21. * along with this program; if not, write to the *
  22. * Free Software Foundation, Inc., *
  23. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  24. ***************************************************************************/
  25. #ifndef NAND_H
  26. #define NAND_H
  27. #include "flash.h"
  28. struct nand_device_s;
  29. typedef struct nand_flash_controller_s
  30. {
  31. char *name;
  32. int (*nand_device_command)(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct nand_device_s *device);
  33. int (*register_commands)(struct command_context_s *cmd_ctx);
  34. int (*init)(struct nand_device_s *device);
  35. int (*reset)(struct nand_device_s *device);
  36. int (*command)(struct nand_device_s *device, uint8_t command);
  37. int (*address)(struct nand_device_s *device, uint8_t address);
  38. int (*write_data)(struct nand_device_s *device, uint16_t data);
  39. int (*read_data)(struct nand_device_s *device, void *data);
  40. int (*write_block_data)(struct nand_device_s *device, uint8_t *data, int size);
  41. int (*read_block_data)(struct nand_device_s *device, uint8_t *data, int size);
  42. int (*write_page)(struct nand_device_s *device, u32 page, uint8_t *data, u32 data_size, uint8_t *oob, u32 oob_size);
  43. int (*read_page)(struct nand_device_s *device, u32 page, uint8_t *data, u32 data_size, uint8_t *oob, u32 oob_size);
  44. int (*controller_ready)(struct nand_device_s *device, int timeout);
  45. int (*nand_ready)(struct nand_device_s *device, int timeout);
  46. } nand_flash_controller_t;
  47. typedef struct nand_block_s
  48. {
  49. u32 offset;
  50. u32 size;
  51. int is_erased;
  52. int is_bad;
  53. } nand_block_t;
  54. struct nand_oobfree {
  55. int offset;
  56. int length;
  57. };
  58. typedef struct nand_ecclayout_s {
  59. int eccbytes;
  60. int eccpos[64];
  61. int oobavail;
  62. struct nand_oobfree oobfree[2];
  63. } nand_ecclayout_t;
  64. typedef struct nand_device_s
  65. {
  66. nand_flash_controller_t *controller;
  67. void *controller_priv;
  68. struct nand_manufacturer_s *manufacturer;
  69. struct nand_info_s *device;
  70. int bus_width;
  71. int address_cycles;
  72. int page_size;
  73. int erase_size;
  74. int use_raw;
  75. int num_blocks;
  76. nand_block_t *blocks;
  77. struct nand_device_s *next;
  78. } nand_device_t;
  79. /* NAND Flash Manufacturer ID Codes
  80. */
  81. enum
  82. {
  83. NAND_MFR_TOSHIBA = 0x98,
  84. NAND_MFR_SAMSUNG = 0xec,
  85. NAND_MFR_FUJITSU = 0x04,
  86. NAND_MFR_NATIONAL = 0x8f,
  87. NAND_MFR_RENESAS = 0x07,
  88. NAND_MFR_STMICRO = 0x20,
  89. NAND_MFR_HYNIX = 0xad,
  90. NAND_MFR_MICRON = 0x2c,
  91. };
  92. typedef struct nand_manufacturer_s
  93. {
  94. int id;
  95. char *name;
  96. } nand_manufacturer_t;
  97. typedef struct nand_info_s
  98. {
  99. char *name;
  100. int id;
  101. int page_size;
  102. int chip_size;
  103. int erase_size;
  104. int options;
  105. } nand_info_t;
  106. /* Option constants for bizarre disfunctionality and real features
  107. */
  108. enum {
  109. /* Chip can not auto increment pages */
  110. NAND_NO_AUTOINCR = 0x00000001,
  111. /* Buswitdh is 16 bit */
  112. NAND_BUSWIDTH_16 = 0x00000002,
  113. /* Device supports partial programming without padding */
  114. NAND_NO_PADDING = 0x00000004,
  115. /* Chip has cache program function */
  116. NAND_CACHEPRG = 0x00000008,
  117. /* Chip has copy back function */
  118. NAND_COPYBACK = 0x00000010,
  119. /* AND Chip which has 4 banks and a confusing page / block
  120. * assignment. See Renesas datasheet for further information */
  121. NAND_IS_AND = 0x00000020,
  122. /* Chip has a array of 4 pages which can be read without
  123. * additional ready /busy waits */
  124. NAND_4PAGE_ARRAY = 0x00000040,
  125. /* Chip requires that BBT is periodically rewritten to prevent
  126. * bits from adjacent blocks from 'leaking' in altering data.
  127. * This happens with the Renesas AG-AND chips, possibly others. */
  128. BBT_AUTO_REFRESH = 0x00000080,
  129. /* Chip does not require ready check on read. True
  130. * for all large page devices, as they do not support
  131. * autoincrement.*/
  132. NAND_NO_READRDY = 0x00000100,
  133. /* Options valid for Samsung large page devices */
  134. NAND_SAMSUNG_LP_OPTIONS = (NAND_NO_PADDING | NAND_CACHEPRG | NAND_COPYBACK),
  135. /* Options for new chips with large page size. The pagesize and the
  136. * erasesize is determined from the extended id bytes
  137. */
  138. LP_OPTIONS = (NAND_SAMSUNG_LP_OPTIONS | NAND_NO_READRDY | NAND_NO_AUTOINCR),
  139. LP_OPTIONS16 = (LP_OPTIONS | NAND_BUSWIDTH_16),
  140. };
  141. enum
  142. {
  143. /* Standard NAND flash commands */
  144. NAND_CMD_READ0 = 0x0,
  145. NAND_CMD_READ1 = 0x1,
  146. NAND_CMD_RNDOUT = 0x5,
  147. NAND_CMD_PAGEPROG = 0x10,
  148. NAND_CMD_READOOB = 0x50,
  149. NAND_CMD_ERASE1 = 0x60,
  150. NAND_CMD_STATUS = 0x70,
  151. NAND_CMD_STATUS_MULTI = 0x71,
  152. NAND_CMD_SEQIN = 0x80,
  153. NAND_CMD_RNDIN = 0x85,
  154. NAND_CMD_READID = 0x90,
  155. NAND_CMD_ERASE2 = 0xd0,
  156. NAND_CMD_RESET = 0xff,
  157. /* Extended commands for large page devices */
  158. NAND_CMD_READSTART = 0x30,
  159. NAND_CMD_RNDOUTSTART = 0xE0,
  160. NAND_CMD_CACHEDPROG = 0x15,
  161. };
  162. /* Status bits */
  163. enum
  164. {
  165. NAND_STATUS_FAIL = 0x01,
  166. NAND_STATUS_FAIL_N1 = 0x02,
  167. NAND_STATUS_TRUE_READY = 0x20,
  168. NAND_STATUS_READY = 0x40,
  169. NAND_STATUS_WP = 0x80,
  170. };
  171. /* OOB (spare) data formats */
  172. enum oob_formats
  173. {
  174. NAND_OOB_NONE = 0x0, /* no OOB data at all */
  175. NAND_OOB_RAW = 0x1, /* raw OOB data (16 bytes for 512b page sizes, 64 bytes for 2048b page sizes) */
  176. NAND_OOB_ONLY = 0x2, /* only OOB data */
  177. NAND_OOB_SW_ECC = 0x10, /* when writing, use SW ECC (as opposed to no ECC) */
  178. NAND_OOB_HW_ECC = 0x20, /* when writing, use HW ECC (as opposed to no ECC) */
  179. NAND_OOB_SW_ECC_KW = 0x40, /* when writing, use Marvell's Kirkwood bootrom format */
  180. NAND_OOB_JFFS2 = 0x100, /* when writing, use JFFS2 OOB layout */
  181. NAND_OOB_YAFFS2 = 0x100,/* when writing, use YAFFS2 OOB layout */
  182. };
  183. /* Function prototypes */
  184. extern nand_device_t *get_nand_device_by_num(int num);
  185. extern int nand_read_page_raw(struct nand_device_s *device, u32 page, uint8_t *data, u32 data_size, uint8_t *oob, u32 oob_size);
  186. extern int nand_write_page_raw(struct nand_device_s *device, u32 page, uint8_t *data, u32 data_size, uint8_t *oob, u32 oob_size);
  187. extern int nand_read_status(struct nand_device_s *device, uint8_t *status);
  188. extern int nand_calculate_ecc(struct nand_device_s *device, const uint8_t *dat, uint8_t *ecc_code);
  189. extern int nand_calculate_ecc_kw(struct nand_device_s *device, const uint8_t *dat, uint8_t *ecc_code);
  190. extern int nand_register_commands(struct command_context_s *cmd_ctx);
  191. extern int nand_init(struct command_context_s *cmd_ctx);
  192. #define ERROR_NAND_DEVICE_INVALID (-1100)
  193. #define ERROR_NAND_OPERATION_FAILED (-1101)
  194. #define ERROR_NAND_OPERATION_TIMEOUT (-1102)
  195. #define ERROR_NAND_OPERATION_NOT_SUPPORTED (-1103)
  196. #define ERROR_NAND_DEVICE_NOT_PROBED (-1104)
  197. #define ERROR_NAND_ERROR_CORRECTION_FAILED (-1105)
  198. #endif /* NAND_H */