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.
 
 
 
 
 
 

302 lines
9.5 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;
  29. #define __NAND_DEVICE_COMMAND(name) \
  30. COMMAND_HELPER(name, struct nand_device *nand)
  31. /**
  32. * Interface for NAND flash controllers. Not all of these functions are
  33. * required for full functionality of the NAND driver, but better performance
  34. * can be achieved by implementing each function.
  35. */
  36. struct nand_flash_controller
  37. {
  38. /** Driver name that is used to select it from configuration files. */
  39. char *name;
  40. const struct command_registration *commands;
  41. /** NAND device command called when driver is instantiated during configuration. */
  42. __NAND_DEVICE_COMMAND((*nand_device_command));
  43. /** Register controller specific commands as a TCL interface to the driver. */
  44. int (*register_commands)(struct command_context *cmd_ctx);
  45. /** Initialize the NAND device. */
  46. int (*init)(struct nand_device *nand);
  47. /** Reset the NAND device. */
  48. int (*reset)(struct nand_device *nand);
  49. /** Issue a command to the NAND device. */
  50. int (*command)(struct nand_device *nand, uint8_t command);
  51. /** Write an address to the NAND device. */
  52. int (*address)(struct nand_device *nand, uint8_t address);
  53. /** Write word of data to the NAND device. */
  54. int (*write_data)(struct nand_device *nand, uint16_t data);
  55. /** Read word of data from the NAND device. */
  56. int (*read_data)(struct nand_device *nand, void *data);
  57. /** Write a block of data to the NAND device. */
  58. int (*write_block_data)(struct nand_device *nand, uint8_t *data, int size);
  59. /** Read a block of data from the NAND device. */
  60. int (*read_block_data)(struct nand_device *nand, uint8_t *data, int size);
  61. /** Write a page to the NAND device. */
  62. int (*write_page)(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
  63. /** Read a page from the NAND device. */
  64. int (*read_page)(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
  65. /** Check if the controller is ready for more instructions with timeout. */
  66. int (*controller_ready)(struct nand_device *nand, int timeout);
  67. /** Check if the NAND device is ready for more instructions with timeout. */
  68. int (*nand_ready)(struct nand_device *nand, int timeout);
  69. };
  70. #define NAND_DEVICE_COMMAND_HANDLER(name) static __NAND_DEVICE_COMMAND(name)
  71. /**
  72. * Representation of a single NAND block in a NAND device.
  73. */
  74. struct nand_block
  75. {
  76. /** Offset to the block. */
  77. uint32_t offset;
  78. /** Size of the block. */
  79. uint32_t size;
  80. /** True if the block has been erased. */
  81. int is_erased;
  82. /** True if the block is bad. */
  83. int is_bad;
  84. };
  85. struct nand_oobfree {
  86. int offset;
  87. int length;
  88. };
  89. struct nand_ecclayout {
  90. int eccbytes;
  91. int eccpos[64];
  92. int oobavail;
  93. struct nand_oobfree oobfree[2];
  94. };
  95. struct nand_device
  96. {
  97. char *name;
  98. struct nand_flash_controller *controller;
  99. void *controller_priv;
  100. struct nand_manufacturer *manufacturer;
  101. struct nand_info *device;
  102. int bus_width;
  103. int address_cycles;
  104. int page_size;
  105. int erase_size;
  106. int use_raw;
  107. int num_blocks;
  108. struct nand_block *blocks;
  109. struct nand_device *next;
  110. };
  111. /* NAND Flash Manufacturer ID Codes
  112. */
  113. enum
  114. {
  115. NAND_MFR_TOSHIBA = 0x98,
  116. NAND_MFR_SAMSUNG = 0xec,
  117. NAND_MFR_FUJITSU = 0x04,
  118. NAND_MFR_NATIONAL = 0x8f,
  119. NAND_MFR_RENESAS = 0x07,
  120. NAND_MFR_STMICRO = 0x20,
  121. NAND_MFR_HYNIX = 0xad,
  122. NAND_MFR_MICRON = 0x2c,
  123. };
  124. struct nand_manufacturer
  125. {
  126. int id;
  127. char *name;
  128. };
  129. struct nand_info
  130. {
  131. char *name;
  132. int id;
  133. int page_size;
  134. int chip_size;
  135. int erase_size;
  136. int options;
  137. };
  138. /* Option constants for bizarre disfunctionality and real features
  139. */
  140. enum {
  141. /* Chip can not auto increment pages */
  142. NAND_NO_AUTOINCR = 0x00000001,
  143. /* Buswitdh is 16 bit */
  144. NAND_BUSWIDTH_16 = 0x00000002,
  145. /* Device supports partial programming without padding */
  146. NAND_NO_PADDING = 0x00000004,
  147. /* Chip has cache program function */
  148. NAND_CACHEPRG = 0x00000008,
  149. /* Chip has copy back function */
  150. NAND_COPYBACK = 0x00000010,
  151. /* AND Chip which has 4 banks and a confusing page / block
  152. * assignment. See Renesas datasheet for further information */
  153. NAND_IS_AND = 0x00000020,
  154. /* Chip has a array of 4 pages which can be read without
  155. * additional ready /busy waits */
  156. NAND_4PAGE_ARRAY = 0x00000040,
  157. /* Chip requires that BBT is periodically rewritten to prevent
  158. * bits from adjacent blocks from 'leaking' in altering data.
  159. * This happens with the Renesas AG-AND chips, possibly others. */
  160. BBT_AUTO_REFRESH = 0x00000080,
  161. /* Chip does not require ready check on read. True
  162. * for all large page devices, as they do not support
  163. * autoincrement.*/
  164. NAND_NO_READRDY = 0x00000100,
  165. /* Options valid for Samsung large page devices */
  166. NAND_SAMSUNG_LP_OPTIONS = (NAND_NO_PADDING | NAND_CACHEPRG | NAND_COPYBACK),
  167. /* Options for new chips with large page size. The pagesize and the
  168. * erasesize is determined from the extended id bytes
  169. */
  170. LP_OPTIONS = (NAND_SAMSUNG_LP_OPTIONS | NAND_NO_READRDY | NAND_NO_AUTOINCR),
  171. LP_OPTIONS16 = (LP_OPTIONS | NAND_BUSWIDTH_16),
  172. };
  173. enum
  174. {
  175. /* Standard NAND flash commands */
  176. NAND_CMD_READ0 = 0x0,
  177. NAND_CMD_READ1 = 0x1,
  178. NAND_CMD_RNDOUT = 0x5,
  179. NAND_CMD_PAGEPROG = 0x10,
  180. NAND_CMD_READOOB = 0x50,
  181. NAND_CMD_ERASE1 = 0x60,
  182. NAND_CMD_STATUS = 0x70,
  183. NAND_CMD_STATUS_MULTI = 0x71,
  184. NAND_CMD_SEQIN = 0x80,
  185. NAND_CMD_RNDIN = 0x85,
  186. NAND_CMD_READID = 0x90,
  187. NAND_CMD_ERASE2 = 0xd0,
  188. NAND_CMD_RESET = 0xff,
  189. /* Extended commands for large page devices */
  190. NAND_CMD_READSTART = 0x30,
  191. NAND_CMD_RNDOUTSTART = 0xE0,
  192. NAND_CMD_CACHEDPROG = 0x15,
  193. };
  194. /* Status bits */
  195. enum
  196. {
  197. NAND_STATUS_FAIL = 0x01,
  198. NAND_STATUS_FAIL_N1 = 0x02,
  199. NAND_STATUS_TRUE_READY = 0x20,
  200. NAND_STATUS_READY = 0x40,
  201. NAND_STATUS_WP = 0x80,
  202. };
  203. /* OOB (spare) data formats */
  204. enum oob_formats
  205. {
  206. NAND_OOB_NONE = 0x0, /* no OOB data at all */
  207. NAND_OOB_RAW = 0x1, /* raw OOB data (16 bytes for 512b page sizes, 64 bytes for 2048b page sizes) */
  208. NAND_OOB_ONLY = 0x2, /* only OOB data */
  209. NAND_OOB_SW_ECC = 0x10, /* when writing, use SW ECC (as opposed to no ECC) */
  210. NAND_OOB_HW_ECC = 0x20, /* when writing, use HW ECC (as opposed to no ECC) */
  211. NAND_OOB_SW_ECC_KW = 0x40, /* when writing, use Marvell's Kirkwood bootrom format */
  212. NAND_OOB_JFFS2 = 0x100, /* when writing, use JFFS2 OOB layout */
  213. NAND_OOB_YAFFS2 = 0x100,/* when writing, use YAFFS2 OOB layout */
  214. };
  215. /**
  216. * Returns the flash bank specified by @a name, which matches the
  217. * driver name and a suffix (option) specify the driver-specific
  218. * bank number. The suffix consists of the '.' and the driver-specific
  219. * bank number: when two davinci banks are defined, then 'davinci.1' refers
  220. * to the second (e.g. DM355EVM).
  221. */
  222. struct nand_device *get_nand_device_by_name(const char *name);
  223. struct nand_device *get_nand_device_by_num(int num);
  224. int nand_read_page_raw(struct nand_device *nand, uint32_t page,
  225. uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
  226. int nand_write_page_raw(struct nand_device *nand, uint32_t page,
  227. uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
  228. int nand_read_status(struct nand_device *nand, uint8_t *status);
  229. int nand_calculate_ecc(struct nand_device *nand,
  230. const uint8_t *dat, uint8_t *ecc_code);
  231. int nand_calculate_ecc_kw(struct nand_device *nand,
  232. const uint8_t *dat, uint8_t *ecc_code);
  233. int nand_register_commands(struct command_context *cmd_ctx);
  234. int nand_init(struct command_context *cmd_ctx);
  235. /// helper for parsing a nand device command argument string
  236. COMMAND_HELPER(nand_command_get_device, unsigned name_index,
  237. struct nand_device **nand);
  238. #define ERROR_NAND_DEVICE_INVALID (-1100)
  239. #define ERROR_NAND_OPERATION_FAILED (-1101)
  240. #define ERROR_NAND_OPERATION_TIMEOUT (-1102)
  241. #define ERROR_NAND_OPERATION_NOT_SUPPORTED (-1103)
  242. #define ERROR_NAND_DEVICE_NOT_PROBED (-1104)
  243. #define ERROR_NAND_ERROR_CORRECTION_FAILED (-1105)
  244. #define ERROR_NAND_NO_BUFFER (-1106)
  245. #endif /* NAND_H */