nand.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * linux/include/linux/mtd/nand.h
  3. *
  4. * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
  5. * Steven J. Hill <sjhill@realitydiluted.com>
  6. * Thomas Gleixner <tglx@linutronix.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Info:
  13. * Contains standard defines and IDs for NAND flash devices
  14. *
  15. * Changelog:
  16. * See git changelog.
  17. */
  18. #ifndef __LINUX_MTD_NAND_H
  19. #define __LINUX_MTD_NAND_H
  20. #include <linux/wait.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/mtd/mtd.h>
  23. #include <linux/mtd/flashchip.h>
  24. #include <linux/mtd/bbm.h>
  25. struct mtd_info;
  26. struct nand_flash_dev;
  27. /* Scan and identify a NAND device */
  28. extern int nand_scan(struct mtd_info *mtd, int max_chips);
  29. /*
  30. * Separate phases of nand_scan(), allowing board driver to intervene
  31. * and override command or ECC setup according to flash type.
  32. */
  33. extern int nand_scan_ident(struct mtd_info *mtd, int max_chips,
  34. struct nand_flash_dev *table);
  35. extern int nand_scan_tail(struct mtd_info *mtd);
  36. /* Free resources held by the NAND device */
  37. extern void nand_release(struct mtd_info *mtd);
  38. /* Internal helper for board drivers which need to override command function */
  39. extern void nand_wait_ready(struct mtd_info *mtd);
  40. /* locks all blocks present in the device */
  41. extern int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  42. /* unlocks specified locked blocks */
  43. extern int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  44. /* The maximum number of NAND chips in an array */
  45. #define NAND_MAX_CHIPS 8
  46. /*
  47. * This constant declares the max. oobsize / page, which
  48. * is supported now. If you add a chip with bigger oobsize/page
  49. * adjust this accordingly.
  50. */
  51. #define NAND_MAX_OOBSIZE 744
  52. #define NAND_MAX_PAGESIZE 8192
  53. /*
  54. * Constants for hardware specific CLE/ALE/NCE function
  55. *
  56. * These are bits which can be or'ed to set/clear multiple
  57. * bits in one go.
  58. */
  59. /* Select the chip by setting nCE to low */
  60. #define NAND_NCE 0x01
  61. /* Select the command latch by setting CLE to high */
  62. #define NAND_CLE 0x02
  63. /* Select the address latch by setting ALE to high */
  64. #define NAND_ALE 0x04
  65. #define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
  66. #define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
  67. #define NAND_CTRL_CHANGE 0x80
  68. /*
  69. * Standard NAND flash commands
  70. */
  71. #define NAND_CMD_READ0 0
  72. #define NAND_CMD_READ1 1
  73. #define NAND_CMD_RNDOUT 5
  74. #define NAND_CMD_PAGEPROG 0x10
  75. #define NAND_CMD_READOOB 0x50
  76. #define NAND_CMD_ERASE1 0x60
  77. #define NAND_CMD_STATUS 0x70
  78. #define NAND_CMD_SEQIN 0x80
  79. #define NAND_CMD_RNDIN 0x85
  80. #define NAND_CMD_READID 0x90
  81. #define NAND_CMD_ERASE2 0xd0
  82. #define NAND_CMD_PARAM 0xec
  83. #define NAND_CMD_GET_FEATURES 0xee
  84. #define NAND_CMD_SET_FEATURES 0xef
  85. #define NAND_CMD_RESET 0xff
  86. #define NAND_CMD_LOCK 0x2a
  87. #define NAND_CMD_UNLOCK1 0x23
  88. #define NAND_CMD_UNLOCK2 0x24
  89. /* Extended commands for large page devices */
  90. #define NAND_CMD_READSTART 0x30
  91. #define NAND_CMD_RNDOUTSTART 0xE0
  92. #define NAND_CMD_CACHEDPROG 0x15
  93. #define NAND_CMD_NONE -1
  94. /* Status bits */
  95. #define NAND_STATUS_FAIL 0x01
  96. #define NAND_STATUS_FAIL_N1 0x02
  97. #define NAND_STATUS_TRUE_READY 0x20
  98. #define NAND_STATUS_READY 0x40
  99. #define NAND_STATUS_WP 0x80
  100. /*
  101. * Constants for ECC_MODES
  102. */
  103. typedef enum {
  104. NAND_ECC_NONE,
  105. NAND_ECC_SOFT,
  106. NAND_ECC_HW,
  107. NAND_ECC_HW_SYNDROME,
  108. NAND_ECC_HW_OOB_FIRST,
  109. NAND_ECC_SOFT_BCH,
  110. } nand_ecc_modes_t;
  111. /*
  112. * Constants for Hardware ECC
  113. */
  114. /* Reset Hardware ECC for read */
  115. #define NAND_ECC_READ 0
  116. /* Reset Hardware ECC for write */
  117. #define NAND_ECC_WRITE 1
  118. /* Enable Hardware ECC before syndrome is read back from flash */
  119. #define NAND_ECC_READSYN 2
  120. /* Bit mask for flags passed to do_nand_read_ecc */
  121. #define NAND_GET_DEVICE 0x80
  122. /*
  123. * Option constants for bizarre disfunctionality and real
  124. * features.
  125. */
  126. /* Buswidth is 16 bit */
  127. #define NAND_BUSWIDTH_16 0x00000002
  128. /* Chip has cache program function */
  129. #define NAND_CACHEPRG 0x00000008
  130. /*
  131. * Chip requires ready check on read (for auto-incremented sequential read).
  132. * True only for small page devices; large page devices do not support
  133. * autoincrement.
  134. */
  135. #define NAND_NEED_READRDY 0x00000100
  136. /* Chip does not allow subpage writes */
  137. #define NAND_NO_SUBPAGE_WRITE 0x00000200
  138. /* Device is one of 'new' xD cards that expose fake nand command set */
  139. #define NAND_BROKEN_XD 0x00000400
  140. /* Device behaves just like nand, but is readonly */
  141. #define NAND_ROM 0x00000800
  142. /* Device supports subpage reads */
  143. #define NAND_SUBPAGE_READ 0x00001000
  144. /* Options valid for Samsung large page devices */
  145. #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
  146. /* Macros to identify the above */
  147. #define NAND_HAS_CACHEPROG(chip) ((chip->options & NAND_CACHEPRG))
  148. #define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
  149. /* Non chip related options */
  150. /* This option skips the bbt scan during initialization. */
  151. #define NAND_SKIP_BBTSCAN 0x00010000
  152. /*
  153. * This option is defined if the board driver allocates its own buffers
  154. * (e.g. because it needs them DMA-coherent).
  155. */
  156. #define NAND_OWN_BUFFERS 0x00020000
  157. /* Chip may not exist, so silence any errors in scan */
  158. #define NAND_SCAN_SILENT_NODEV 0x00040000
  159. /*
  160. * Autodetect nand buswidth with readid/onfi.
  161. * This suppose the driver will configure the hardware in 8 bits mode
  162. * when calling nand_scan_ident, and update its configuration
  163. * before calling nand_scan_tail.
  164. */
  165. #define NAND_BUSWIDTH_AUTO 0x00080000
  166. /* Options set by nand scan */
  167. /* Nand scan has allocated controller struct */
  168. #define NAND_CONTROLLER_ALLOC 0x80000000
  169. /* Cell info constants */
  170. #define NAND_CI_CHIPNR_MSK 0x03
  171. #define NAND_CI_CELLTYPE_MSK 0x0C
  172. #define NAND_CI_CELLTYPE_SHIFT 2
  173. /* Keep gcc happy */
  174. struct nand_chip;
  175. /* ONFI features */
  176. #define ONFI_FEATURE_16_BIT_BUS (1 << 0)
  177. #define ONFI_FEATURE_EXT_PARAM_PAGE (1 << 7)
  178. /* ONFI timing mode, used in both asynchronous and synchronous mode */
  179. #define ONFI_TIMING_MODE_0 (1 << 0)
  180. #define ONFI_TIMING_MODE_1 (1 << 1)
  181. #define ONFI_TIMING_MODE_2 (1 << 2)
  182. #define ONFI_TIMING_MODE_3 (1 << 3)
  183. #define ONFI_TIMING_MODE_4 (1 << 4)
  184. #define ONFI_TIMING_MODE_5 (1 << 5)
  185. #define ONFI_TIMING_MODE_UNKNOWN (1 << 6)
  186. /* ONFI feature address */
  187. #define ONFI_FEATURE_ADDR_TIMING_MODE 0x1
  188. /* ONFI subfeature parameters length */
  189. #define ONFI_SUBFEATURE_PARAM_LEN 4
  190. /* ONFI optional commands SET/GET FEATURES supported? */
  191. #define ONFI_OPT_CMD_SET_GET_FEATURES (1 << 2)
  192. struct nand_onfi_params {
  193. /* rev info and features block */
  194. /* 'O' 'N' 'F' 'I' */
  195. u8 sig[4];
  196. __le16 revision;
  197. __le16 features;
  198. __le16 opt_cmd;
  199. u8 reserved0[2];
  200. __le16 ext_param_page_length; /* since ONFI 2.1 */
  201. u8 num_of_param_pages; /* since ONFI 2.1 */
  202. u8 reserved1[17];
  203. /* manufacturer information block */
  204. char manufacturer[12];
  205. char model[20];
  206. u8 jedec_id;
  207. __le16 date_code;
  208. u8 reserved2[13];
  209. /* memory organization block */
  210. __le32 byte_per_page;
  211. __le16 spare_bytes_per_page;
  212. __le32 data_bytes_per_ppage;
  213. __le16 spare_bytes_per_ppage;
  214. __le32 pages_per_block;
  215. __le32 blocks_per_lun;
  216. u8 lun_count;
  217. u8 addr_cycles;
  218. u8 bits_per_cell;
  219. __le16 bb_per_lun;
  220. __le16 block_endurance;
  221. u8 guaranteed_good_blocks;
  222. __le16 guaranteed_block_endurance;
  223. u8 programs_per_page;
  224. u8 ppage_attr;
  225. u8 ecc_bits;
  226. u8 interleaved_bits;
  227. u8 interleaved_ops;
  228. u8 reserved3[13];
  229. /* electrical parameter block */
  230. u8 io_pin_capacitance_max;
  231. __le16 async_timing_mode;
  232. __le16 program_cache_timing_mode;
  233. __le16 t_prog;
  234. __le16 t_bers;
  235. __le16 t_r;
  236. __le16 t_ccs;
  237. __le16 src_sync_timing_mode;
  238. __le16 src_ssync_features;
  239. __le16 clk_pin_capacitance_typ;
  240. __le16 io_pin_capacitance_typ;
  241. __le16 input_pin_capacitance_typ;
  242. u8 input_pin_capacitance_max;
  243. u8 driver_strenght_support;
  244. __le16 t_int_r;
  245. __le16 t_ald;
  246. u8 reserved4[7];
  247. /* vendor */
  248. u8 reserved5[90];
  249. __le16 crc;
  250. } __attribute__((packed));
  251. #define ONFI_CRC_BASE 0x4F4E
  252. /* Extended ECC information Block Definition (since ONFI 2.1) */
  253. struct onfi_ext_ecc_info {
  254. u8 ecc_bits;
  255. u8 codeword_size;
  256. __le16 bb_per_lun;
  257. __le16 block_endurance;
  258. u8 reserved[2];
  259. } __packed;
  260. #define ONFI_SECTION_TYPE_0 0 /* Unused section. */
  261. #define ONFI_SECTION_TYPE_1 1 /* for additional sections. */
  262. #define ONFI_SECTION_TYPE_2 2 /* for ECC information. */
  263. struct onfi_ext_section {
  264. u8 type;
  265. u8 length;
  266. } __packed;
  267. #define ONFI_EXT_SECTION_MAX 8
  268. /* Extended Parameter Page Definition (since ONFI 2.1) */
  269. struct onfi_ext_param_page {
  270. __le16 crc;
  271. u8 sig[4]; /* 'E' 'P' 'P' 'S' */
  272. u8 reserved0[10];
  273. struct onfi_ext_section sections[ONFI_EXT_SECTION_MAX];
  274. /*
  275. * The actual size of the Extended Parameter Page is in
  276. * @ext_param_page_length of nand_onfi_params{}.
  277. * The following are the variable length sections.
  278. * So we do not add any fields below. Please see the ONFI spec.
  279. */
  280. } __packed;
  281. /**
  282. * struct nand_hw_control - Control structure for hardware controller (e.g ECC generator) shared among independent devices
  283. * @lock: protection lock
  284. * @active: the mtd device which holds the controller currently
  285. * @wq: wait queue to sleep on if a NAND operation is in
  286. * progress used instead of the per chip wait queue
  287. * when a hw controller is available.
  288. */
  289. struct nand_hw_control {
  290. spinlock_t lock;
  291. struct nand_chip *active;
  292. wait_queue_head_t wq;
  293. };
  294. /**
  295. * struct nand_ecc_ctrl - Control structure for ECC
  296. * @mode: ECC mode
  297. * @steps: number of ECC steps per page
  298. * @size: data bytes per ECC step
  299. * @bytes: ECC bytes per step
  300. * @strength: max number of correctible bits per ECC step
  301. * @total: total number of ECC bytes per page
  302. * @prepad: padding information for syndrome based ECC generators
  303. * @postpad: padding information for syndrome based ECC generators
  304. * @layout: ECC layout control struct pointer
  305. * @priv: pointer to private ECC control data
  306. * @hwctl: function to control hardware ECC generator. Must only
  307. * be provided if an hardware ECC is available
  308. * @calculate: function for ECC calculation or readback from ECC hardware
  309. * @correct: function for ECC correction, matching to ECC generator (sw/hw)
  310. * @read_page_raw: function to read a raw page without ECC
  311. * @write_page_raw: function to write a raw page without ECC
  312. * @read_page: function to read a page according to the ECC generator
  313. * requirements; returns maximum number of bitflips corrected in
  314. * any single ECC step, 0 if bitflips uncorrectable, -EIO hw error
  315. * @read_subpage: function to read parts of the page covered by ECC;
  316. * returns same as read_page()
  317. * @write_subpage: function to write parts of the page covered by ECC.
  318. * @write_page: function to write a page according to the ECC generator
  319. * requirements.
  320. * @write_oob_raw: function to write chip OOB data without ECC
  321. * @read_oob_raw: function to read chip OOB data without ECC
  322. * @read_oob: function to read chip OOB data
  323. * @write_oob: function to write chip OOB data
  324. */
  325. struct nand_ecc_ctrl {
  326. nand_ecc_modes_t mode;
  327. int steps;
  328. int size;
  329. int bytes;
  330. int total;
  331. int strength;
  332. int prepad;
  333. int postpad;
  334. struct nand_ecclayout *layout;
  335. void *priv;
  336. void (*hwctl)(struct mtd_info *mtd, int mode);
  337. int (*calculate)(struct mtd_info *mtd, const uint8_t *dat,
  338. uint8_t *ecc_code);
  339. int (*correct)(struct mtd_info *mtd, uint8_t *dat, uint8_t *read_ecc,
  340. uint8_t *calc_ecc);
  341. int (*read_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  342. uint8_t *buf, int oob_required, int page);
  343. int (*write_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  344. const uint8_t *buf, int oob_required);
  345. int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
  346. uint8_t *buf, int oob_required, int page);
  347. int (*read_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
  348. uint32_t offs, uint32_t len, uint8_t *buf);
  349. int (*write_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
  350. uint32_t offset, uint32_t data_len,
  351. const uint8_t *data_buf, int oob_required);
  352. int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
  353. const uint8_t *buf, int oob_required);
  354. int (*write_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  355. int page);
  356. int (*read_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  357. int page);
  358. int (*read_oob)(struct mtd_info *mtd, struct nand_chip *chip, int page);
  359. int (*write_oob)(struct mtd_info *mtd, struct nand_chip *chip,
  360. int page);
  361. };
  362. /**
  363. * struct nand_buffers - buffer structure for read/write
  364. * @ecccalc: buffer for calculated ECC
  365. * @ecccode: buffer for ECC read from flash
  366. * @databuf: buffer for data - dynamically sized
  367. *
  368. * Do not change the order of buffers. databuf and oobrbuf must be in
  369. * consecutive order.
  370. */
  371. struct nand_buffers {
  372. uint8_t ecccalc[NAND_MAX_OOBSIZE];
  373. uint8_t ecccode[NAND_MAX_OOBSIZE];
  374. uint8_t databuf[NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE];
  375. };
  376. /**
  377. * struct nand_chip - NAND Private Flash Chip Data
  378. * @IO_ADDR_R: [BOARDSPECIFIC] address to read the 8 I/O lines of the
  379. * flash device
  380. * @IO_ADDR_W: [BOARDSPECIFIC] address to write the 8 I/O lines of the
  381. * flash device.
  382. * @read_byte: [REPLACEABLE] read one byte from the chip
  383. * @read_word: [REPLACEABLE] read one word from the chip
  384. * @write_buf: [REPLACEABLE] write data from the buffer to the chip
  385. * @read_buf: [REPLACEABLE] read data from the chip into the buffer
  386. * @select_chip: [REPLACEABLE] select chip nr
  387. * @block_bad: [REPLACEABLE] check if a block is bad, using OOB markers
  388. * @block_markbad: [REPLACEABLE] mark a block bad
  389. * @cmd_ctrl: [BOARDSPECIFIC] hardwarespecific function for controlling
  390. * ALE/CLE/nCE. Also used to write command and address
  391. * @init_size: [BOARDSPECIFIC] hardwarespecific function for setting
  392. * mtd->oobsize, mtd->writesize and so on.
  393. * @id_data contains the 8 bytes values of NAND_CMD_READID.
  394. * Return with the bus width.
  395. * @dev_ready: [BOARDSPECIFIC] hardwarespecific function for accessing
  396. * device ready/busy line. If set to NULL no access to
  397. * ready/busy is available and the ready/busy information
  398. * is read from the chip status register.
  399. * @cmdfunc: [REPLACEABLE] hardwarespecific function for writing
  400. * commands to the chip.
  401. * @waitfunc: [REPLACEABLE] hardwarespecific function for wait on
  402. * ready.
  403. * @ecc: [BOARDSPECIFIC] ECC control structure
  404. * @buffers: buffer structure for read/write
  405. * @hwcontrol: platform-specific hardware control structure
  406. * @erase_cmd: [INTERN] erase command write function, selectable due
  407. * to AND support.
  408. * @scan_bbt: [REPLACEABLE] function to scan bad block table
  409. * @chip_delay: [BOARDSPECIFIC] chip dependent delay for transferring
  410. * data from array to read regs (tR).
  411. * @state: [INTERN] the current state of the NAND device
  412. * @oob_poi: "poison value buffer," used for laying out OOB data
  413. * before writing
  414. * @page_shift: [INTERN] number of address bits in a page (column
  415. * address bits).
  416. * @phys_erase_shift: [INTERN] number of address bits in a physical eraseblock
  417. * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
  418. * @chip_shift: [INTERN] number of address bits in one chip
  419. * @options: [BOARDSPECIFIC] various chip options. They can partly
  420. * be set to inform nand_scan about special functionality.
  421. * See the defines for further explanation.
  422. * @bbt_options: [INTERN] bad block specific options. All options used
  423. * here must come from bbm.h. By default, these options
  424. * will be copied to the appropriate nand_bbt_descr's.
  425. * @badblockpos: [INTERN] position of the bad block marker in the oob
  426. * area.
  427. * @badblockbits: [INTERN] minimum number of set bits in a good block's
  428. * bad block marker position; i.e., BBM == 11110111b is
  429. * not bad when badblockbits == 7
  430. * @bits_per_cell: [INTERN] number of bits per cell. i.e., 1 means SLC.
  431. * @ecc_strength_ds: [INTERN] ECC correctability from the datasheet.
  432. * Minimum amount of bit errors per @ecc_step_ds guaranteed
  433. * to be correctable. If unknown, set to zero.
  434. * @ecc_step_ds: [INTERN] ECC step required by the @ecc_strength_ds,
  435. * also from the datasheet. It is the recommended ECC step
  436. * size, if known; if unknown, set to zero.
  437. * @numchips: [INTERN] number of physical chips
  438. * @chipsize: [INTERN] the size of one chip for multichip arrays
  439. * @pagemask: [INTERN] page number mask = number of (pages / chip) - 1
  440. * @pagebuf: [INTERN] holds the pagenumber which is currently in
  441. * data_buf.
  442. * @pagebuf_bitflips: [INTERN] holds the bitflip count for the page which is
  443. * currently in data_buf.
  444. * @subpagesize: [INTERN] holds the subpagesize
  445. * @onfi_version: [INTERN] holds the chip ONFI version (BCD encoded),
  446. * non 0 if ONFI supported.
  447. * @onfi_params: [INTERN] holds the ONFI page parameter when ONFI is
  448. * supported, 0 otherwise.
  449. * @onfi_set_features: [REPLACEABLE] set the features for ONFI nand
  450. * @onfi_get_features: [REPLACEABLE] get the features for ONFI nand
  451. * @bbt: [INTERN] bad block table pointer
  452. * @bbt_td: [REPLACEABLE] bad block table descriptor for flash
  453. * lookup.
  454. * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
  455. * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
  456. * bad block scan.
  457. * @controller: [REPLACEABLE] a pointer to a hardware controller
  458. * structure which is shared among multiple independent
  459. * devices.
  460. * @priv: [OPTIONAL] pointer to private chip data
  461. * @errstat: [OPTIONAL] hardware specific function to perform
  462. * additional error status checks (determine if errors are
  463. * correctable).
  464. * @write_page: [REPLACEABLE] High-level page write function
  465. */
  466. struct nand_chip {
  467. void __iomem *IO_ADDR_R;
  468. void __iomem *IO_ADDR_W;
  469. uint8_t (*read_byte)(struct mtd_info *mtd);
  470. u16 (*read_word)(struct mtd_info *mtd);
  471. void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
  472. void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
  473. void (*select_chip)(struct mtd_info *mtd, int chip);
  474. int (*block_bad)(struct mtd_info *mtd, loff_t ofs, int getchip);
  475. int (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
  476. void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
  477. int (*init_size)(struct mtd_info *mtd, struct nand_chip *this,
  478. u8 *id_data);
  479. int (*dev_ready)(struct mtd_info *mtd);
  480. void (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column,
  481. int page_addr);
  482. int(*waitfunc)(struct mtd_info *mtd, struct nand_chip *this);
  483. void (*erase_cmd)(struct mtd_info *mtd, int page);
  484. int (*scan_bbt)(struct mtd_info *mtd);
  485. int (*errstat)(struct mtd_info *mtd, struct nand_chip *this, int state,
  486. int status, int page);
  487. int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
  488. uint32_t offset, int data_len, const uint8_t *buf,
  489. int oob_required, int page, int cached, int raw);
  490. int (*onfi_set_features)(struct mtd_info *mtd, struct nand_chip *chip,
  491. int feature_addr, uint8_t *subfeature_para);
  492. int (*onfi_get_features)(struct mtd_info *mtd, struct nand_chip *chip,
  493. int feature_addr, uint8_t *subfeature_para);
  494. int chip_delay;
  495. unsigned int options;
  496. unsigned int bbt_options;
  497. int page_shift;
  498. int phys_erase_shift;
  499. int bbt_erase_shift;
  500. int chip_shift;
  501. int numchips;
  502. uint64_t chipsize;
  503. int pagemask;
  504. int pagebuf;
  505. unsigned int pagebuf_bitflips;
  506. int subpagesize;
  507. uint8_t bits_per_cell;
  508. uint16_t ecc_strength_ds;
  509. uint16_t ecc_step_ds;
  510. int badblockpos;
  511. int badblockbits;
  512. int onfi_version;
  513. struct nand_onfi_params onfi_params;
  514. flstate_t state;
  515. uint8_t *oob_poi;
  516. struct nand_hw_control *controller;
  517. struct nand_ecc_ctrl ecc;
  518. struct nand_buffers *buffers;
  519. struct nand_hw_control hwcontrol;
  520. uint8_t *bbt;
  521. struct nand_bbt_descr *bbt_td;
  522. struct nand_bbt_descr *bbt_md;
  523. struct nand_bbt_descr *badblock_pattern;
  524. void *priv;
  525. };
  526. /*
  527. * NAND Flash Manufacturer ID Codes
  528. */
  529. #define NAND_MFR_TOSHIBA 0x98
  530. #define NAND_MFR_SAMSUNG 0xec
  531. #define NAND_MFR_FUJITSU 0x04
  532. #define NAND_MFR_NATIONAL 0x8f
  533. #define NAND_MFR_RENESAS 0x07
  534. #define NAND_MFR_STMICRO 0x20
  535. #define NAND_MFR_HYNIX 0xad
  536. #define NAND_MFR_MICRON 0x2c
  537. #define NAND_MFR_AMD 0x01
  538. #define NAND_MFR_MACRONIX 0xc2
  539. #define NAND_MFR_EON 0x92
  540. /* The maximum expected count of bytes in the NAND ID sequence */
  541. #define NAND_MAX_ID_LEN 8
  542. /*
  543. * A helper for defining older NAND chips where the second ID byte fully
  544. * defined the chip, including the geometry (chip size, eraseblock size, page
  545. * size). All these chips have 512 bytes NAND page size.
  546. */
  547. #define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
  548. { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
  549. .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
  550. /*
  551. * A helper for defining newer chips which report their page size and
  552. * eraseblock size via the extended ID bytes.
  553. *
  554. * The real difference between LEGACY_ID_NAND and EXTENDED_ID_NAND is that with
  555. * EXTENDED_ID_NAND, manufacturers overloaded the same device ID so that the
  556. * device ID now only represented a particular total chip size (and voltage,
  557. * buswidth), and the page size, eraseblock size, and OOB size could vary while
  558. * using the same device ID.
  559. */
  560. #define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
  561. { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
  562. .options = (opts) }
  563. #define NAND_ECC_INFO(_strength, _step) \
  564. { .strength_ds = (_strength), .step_ds = (_step) }
  565. #define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
  566. #define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
  567. /**
  568. * struct nand_flash_dev - NAND Flash Device ID Structure
  569. * @name: a human-readable name of the NAND chip
  570. * @dev_id: the device ID (the second byte of the full chip ID array)
  571. * @mfr_id: manufecturer ID part of the full chip ID array (refers the same
  572. * memory address as @id[0])
  573. * @dev_id: device ID part of the full chip ID array (refers the same memory
  574. * address as @id[1])
  575. * @id: full device ID array
  576. * @pagesize: size of the NAND page in bytes; if 0, then the real page size (as
  577. * well as the eraseblock size) is determined from the extended NAND
  578. * chip ID array)
  579. * @chipsize: total chip size in MiB
  580. * @erasesize: eraseblock size in bytes (determined from the extended ID if 0)
  581. * @options: stores various chip bit options
  582. * @id_len: The valid length of the @id.
  583. * @oobsize: OOB size
  584. * @ecc.strength_ds: The ECC correctability from the datasheet, same as the
  585. * @ecc_strength_ds in nand_chip{}.
  586. * @ecc.step_ds: The ECC step required by the @ecc.strength_ds, same as the
  587. * @ecc_step_ds in nand_chip{}, also from the datasheet.
  588. * For example, the "4bit ECC for each 512Byte" can be set with
  589. * NAND_ECC_INFO(4, 512).
  590. */
  591. struct nand_flash_dev {
  592. char *name;
  593. union {
  594. struct {
  595. uint8_t mfr_id;
  596. uint8_t dev_id;
  597. };
  598. uint8_t id[NAND_MAX_ID_LEN];
  599. };
  600. unsigned int pagesize;
  601. unsigned int chipsize;
  602. unsigned int erasesize;
  603. unsigned int options;
  604. uint16_t id_len;
  605. uint16_t oobsize;
  606. struct {
  607. uint16_t strength_ds;
  608. uint16_t step_ds;
  609. } ecc;
  610. };
  611. /**
  612. * struct nand_manufacturers - NAND Flash Manufacturer ID Structure
  613. * @name: Manufacturer name
  614. * @id: manufacturer ID code of device.
  615. */
  616. struct nand_manufacturers {
  617. int id;
  618. char *name;
  619. };
  620. extern struct nand_flash_dev nand_flash_ids[];
  621. extern struct nand_manufacturers nand_manuf_ids[];
  622. extern int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
  623. extern int nand_default_bbt(struct mtd_info *mtd);
  624. extern int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs);
  625. extern int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt);
  626. extern int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  627. int allowbbt);
  628. extern int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
  629. size_t *retlen, uint8_t *buf);
  630. /**
  631. * struct platform_nand_chip - chip level device structure
  632. * @nr_chips: max. number of chips to scan for
  633. * @chip_offset: chip number offset
  634. * @nr_partitions: number of partitions pointed to by partitions (or zero)
  635. * @partitions: mtd partition list
  636. * @chip_delay: R/B delay value in us
  637. * @options: Option flags, e.g. 16bit buswidth
  638. * @bbt_options: BBT option flags, e.g. NAND_BBT_USE_FLASH
  639. * @ecclayout: ECC layout info structure
  640. * @part_probe_types: NULL-terminated array of probe types
  641. */
  642. struct platform_nand_chip {
  643. int nr_chips;
  644. int chip_offset;
  645. int nr_partitions;
  646. struct mtd_partition *partitions;
  647. struct nand_ecclayout *ecclayout;
  648. int chip_delay;
  649. unsigned int options;
  650. unsigned int bbt_options;
  651. const char **part_probe_types;
  652. };
  653. /* Keep gcc happy */
  654. struct platform_device;
  655. /**
  656. * struct platform_nand_ctrl - controller level device structure
  657. * @probe: platform specific function to probe/setup hardware
  658. * @remove: platform specific function to remove/teardown hardware
  659. * @hwcontrol: platform specific hardware control structure
  660. * @dev_ready: platform specific function to read ready/busy pin
  661. * @select_chip: platform specific chip select function
  662. * @cmd_ctrl: platform specific function for controlling
  663. * ALE/CLE/nCE. Also used to write command and address
  664. * @write_buf: platform specific function for write buffer
  665. * @read_buf: platform specific function for read buffer
  666. * @read_byte: platform specific function to read one byte from chip
  667. * @priv: private data to transport driver specific settings
  668. *
  669. * All fields are optional and depend on the hardware driver requirements
  670. */
  671. struct platform_nand_ctrl {
  672. int (*probe)(struct platform_device *pdev);
  673. void (*remove)(struct platform_device *pdev);
  674. void (*hwcontrol)(struct mtd_info *mtd, int cmd);
  675. int (*dev_ready)(struct mtd_info *mtd);
  676. void (*select_chip)(struct mtd_info *mtd, int chip);
  677. void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
  678. void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
  679. void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
  680. unsigned char (*read_byte)(struct mtd_info *mtd);
  681. void *priv;
  682. };
  683. /**
  684. * struct platform_nand_data - container structure for platform-specific data
  685. * @chip: chip level chip structure
  686. * @ctrl: controller level device structure
  687. */
  688. struct platform_nand_data {
  689. struct platform_nand_chip chip;
  690. struct platform_nand_ctrl ctrl;
  691. };
  692. /* Some helpers to access the data structures */
  693. static inline
  694. struct platform_nand_chip *get_platform_nandchip(struct mtd_info *mtd)
  695. {
  696. struct nand_chip *chip = mtd->priv;
  697. return chip->priv;
  698. }
  699. /* return the supported features. */
  700. static inline int onfi_feature(struct nand_chip *chip)
  701. {
  702. return chip->onfi_version ? le16_to_cpu(chip->onfi_params.features) : 0;
  703. }
  704. /* return the supported asynchronous timing mode. */
  705. static inline int onfi_get_async_timing_mode(struct nand_chip *chip)
  706. {
  707. if (!chip->onfi_version)
  708. return ONFI_TIMING_MODE_UNKNOWN;
  709. return le16_to_cpu(chip->onfi_params.async_timing_mode);
  710. }
  711. /* return the supported synchronous timing mode. */
  712. static inline int onfi_get_sync_timing_mode(struct nand_chip *chip)
  713. {
  714. if (!chip->onfi_version)
  715. return ONFI_TIMING_MODE_UNKNOWN;
  716. return le16_to_cpu(chip->onfi_params.src_sync_timing_mode);
  717. }
  718. /*
  719. * Check if it is a SLC nand.
  720. * The !nand_is_slc() can be used to check the MLC/TLC nand chips.
  721. * We do not distinguish the MLC and TLC now.
  722. */
  723. static inline bool nand_is_slc(struct nand_chip *chip)
  724. {
  725. return chip->bits_per_cell == 1;
  726. }
  727. #endif /* __LINUX_MTD_NAND_H */