nand.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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_strength_support;
  244. __le16 t_int_r;
  245. __le16 t_ald;
  246. u8 reserved4[7];
  247. /* vendor */
  248. __le16 vendor_revision;
  249. u8 vendor[88];
  250. __le16 crc;
  251. } __attribute__((packed));
  252. #define ONFI_CRC_BASE 0x4F4E
  253. /* Extended ECC information Block Definition (since ONFI 2.1) */
  254. struct onfi_ext_ecc_info {
  255. u8 ecc_bits;
  256. u8 codeword_size;
  257. __le16 bb_per_lun;
  258. __le16 block_endurance;
  259. u8 reserved[2];
  260. } __packed;
  261. #define ONFI_SECTION_TYPE_0 0 /* Unused section. */
  262. #define ONFI_SECTION_TYPE_1 1 /* for additional sections. */
  263. #define ONFI_SECTION_TYPE_2 2 /* for ECC information. */
  264. struct onfi_ext_section {
  265. u8 type;
  266. u8 length;
  267. } __packed;
  268. #define ONFI_EXT_SECTION_MAX 8
  269. /* Extended Parameter Page Definition (since ONFI 2.1) */
  270. struct onfi_ext_param_page {
  271. __le16 crc;
  272. u8 sig[4]; /* 'E' 'P' 'P' 'S' */
  273. u8 reserved0[10];
  274. struct onfi_ext_section sections[ONFI_EXT_SECTION_MAX];
  275. /*
  276. * The actual size of the Extended Parameter Page is in
  277. * @ext_param_page_length of nand_onfi_params{}.
  278. * The following are the variable length sections.
  279. * So we do not add any fields below. Please see the ONFI spec.
  280. */
  281. } __packed;
  282. struct nand_onfi_vendor_micron {
  283. u8 two_plane_read;
  284. u8 read_cache;
  285. u8 read_unique_id;
  286. u8 dq_imped;
  287. u8 dq_imped_num_settings;
  288. u8 dq_imped_feat_addr;
  289. u8 rb_pulldown_strength;
  290. u8 rb_pulldown_strength_feat_addr;
  291. u8 rb_pulldown_strength_num_settings;
  292. u8 otp_mode;
  293. u8 otp_page_start;
  294. u8 otp_data_prot_addr;
  295. u8 otp_num_pages;
  296. u8 otp_feat_addr;
  297. u8 read_retry_options;
  298. u8 reserved[72];
  299. u8 param_revision;
  300. } __packed;
  301. /**
  302. * struct nand_hw_control - Control structure for hardware controller (e.g ECC generator) shared among independent devices
  303. * @lock: protection lock
  304. * @active: the mtd device which holds the controller currently
  305. * @wq: wait queue to sleep on if a NAND operation is in
  306. * progress used instead of the per chip wait queue
  307. * when a hw controller is available.
  308. */
  309. struct nand_hw_control {
  310. spinlock_t lock;
  311. struct nand_chip *active;
  312. wait_queue_head_t wq;
  313. };
  314. /**
  315. * struct nand_ecc_ctrl - Control structure for ECC
  316. * @mode: ECC mode
  317. * @steps: number of ECC steps per page
  318. * @size: data bytes per ECC step
  319. * @bytes: ECC bytes per step
  320. * @strength: max number of correctible bits per ECC step
  321. * @total: total number of ECC bytes per page
  322. * @prepad: padding information for syndrome based ECC generators
  323. * @postpad: padding information for syndrome based ECC generators
  324. * @layout: ECC layout control struct pointer
  325. * @priv: pointer to private ECC control data
  326. * @hwctl: function to control hardware ECC generator. Must only
  327. * be provided if an hardware ECC is available
  328. * @calculate: function for ECC calculation or readback from ECC hardware
  329. * @correct: function for ECC correction, matching to ECC generator (sw/hw)
  330. * @read_page_raw: function to read a raw page without ECC
  331. * @write_page_raw: function to write a raw page without ECC
  332. * @read_page: function to read a page according to the ECC generator
  333. * requirements; returns maximum number of bitflips corrected in
  334. * any single ECC step, 0 if bitflips uncorrectable, -EIO hw error
  335. * @read_subpage: function to read parts of the page covered by ECC;
  336. * returns same as read_page()
  337. * @write_subpage: function to write parts of the page covered by ECC.
  338. * @write_page: function to write a page according to the ECC generator
  339. * requirements.
  340. * @write_oob_raw: function to write chip OOB data without ECC
  341. * @read_oob_raw: function to read chip OOB data without ECC
  342. * @read_oob: function to read chip OOB data
  343. * @write_oob: function to write chip OOB data
  344. */
  345. struct nand_ecc_ctrl {
  346. nand_ecc_modes_t mode;
  347. int steps;
  348. int size;
  349. int bytes;
  350. int total;
  351. int strength;
  352. int prepad;
  353. int postpad;
  354. struct nand_ecclayout *layout;
  355. void *priv;
  356. void (*hwctl)(struct mtd_info *mtd, int mode);
  357. int (*calculate)(struct mtd_info *mtd, const uint8_t *dat,
  358. uint8_t *ecc_code);
  359. int (*correct)(struct mtd_info *mtd, uint8_t *dat, uint8_t *read_ecc,
  360. uint8_t *calc_ecc);
  361. int (*read_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  362. uint8_t *buf, int oob_required, int page);
  363. int (*write_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  364. const uint8_t *buf, int oob_required);
  365. int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
  366. uint8_t *buf, int oob_required, int page);
  367. int (*read_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
  368. uint32_t offs, uint32_t len, uint8_t *buf);
  369. int (*write_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
  370. uint32_t offset, uint32_t data_len,
  371. const uint8_t *data_buf, int oob_required);
  372. int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
  373. const uint8_t *buf, int oob_required);
  374. int (*write_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  375. int page);
  376. int (*read_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  377. int page);
  378. int (*read_oob)(struct mtd_info *mtd, struct nand_chip *chip, int page);
  379. int (*write_oob)(struct mtd_info *mtd, struct nand_chip *chip,
  380. int page);
  381. };
  382. /**
  383. * struct nand_buffers - buffer structure for read/write
  384. * @ecccalc: buffer for calculated ECC
  385. * @ecccode: buffer for ECC read from flash
  386. * @databuf: buffer for data - dynamically sized
  387. *
  388. * Do not change the order of buffers. databuf and oobrbuf must be in
  389. * consecutive order.
  390. */
  391. struct nand_buffers {
  392. uint8_t ecccalc[NAND_MAX_OOBSIZE];
  393. uint8_t ecccode[NAND_MAX_OOBSIZE];
  394. uint8_t databuf[NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE];
  395. };
  396. /**
  397. * struct nand_chip - NAND Private Flash Chip Data
  398. * @IO_ADDR_R: [BOARDSPECIFIC] address to read the 8 I/O lines of the
  399. * flash device
  400. * @IO_ADDR_W: [BOARDSPECIFIC] address to write the 8 I/O lines of the
  401. * flash device.
  402. * @read_byte: [REPLACEABLE] read one byte from the chip
  403. * @read_word: [REPLACEABLE] read one word from the chip
  404. * @write_buf: [REPLACEABLE] write data from the buffer to the chip
  405. * @read_buf: [REPLACEABLE] read data from the chip into the buffer
  406. * @select_chip: [REPLACEABLE] select chip nr
  407. * @block_bad: [REPLACEABLE] check if a block is bad, using OOB markers
  408. * @block_markbad: [REPLACEABLE] mark a block bad
  409. * @cmd_ctrl: [BOARDSPECIFIC] hardwarespecific function for controlling
  410. * ALE/CLE/nCE. Also used to write command and address
  411. * @init_size: [BOARDSPECIFIC] hardwarespecific function for setting
  412. * mtd->oobsize, mtd->writesize and so on.
  413. * @id_data contains the 8 bytes values of NAND_CMD_READID.
  414. * Return with the bus width.
  415. * @dev_ready: [BOARDSPECIFIC] hardwarespecific function for accessing
  416. * device ready/busy line. If set to NULL no access to
  417. * ready/busy is available and the ready/busy information
  418. * is read from the chip status register.
  419. * @cmdfunc: [REPLACEABLE] hardwarespecific function for writing
  420. * commands to the chip.
  421. * @waitfunc: [REPLACEABLE] hardwarespecific function for wait on
  422. * ready.
  423. * @setup_read_retry: [FLASHSPECIFIC] flash (vendor) specific function for
  424. * setting the read-retry mode. Mostly needed for MLC NAND.
  425. * @ecc: [BOARDSPECIFIC] ECC control structure
  426. * @buffers: buffer structure for read/write
  427. * @hwcontrol: platform-specific hardware control structure
  428. * @erase_cmd: [INTERN] erase command write function, selectable due
  429. * to AND support.
  430. * @scan_bbt: [REPLACEABLE] function to scan bad block table
  431. * @chip_delay: [BOARDSPECIFIC] chip dependent delay for transferring
  432. * data from array to read regs (tR).
  433. * @state: [INTERN] the current state of the NAND device
  434. * @oob_poi: "poison value buffer," used for laying out OOB data
  435. * before writing
  436. * @page_shift: [INTERN] number of address bits in a page (column
  437. * address bits).
  438. * @phys_erase_shift: [INTERN] number of address bits in a physical eraseblock
  439. * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
  440. * @chip_shift: [INTERN] number of address bits in one chip
  441. * @options: [BOARDSPECIFIC] various chip options. They can partly
  442. * be set to inform nand_scan about special functionality.
  443. * See the defines for further explanation.
  444. * @bbt_options: [INTERN] bad block specific options. All options used
  445. * here must come from bbm.h. By default, these options
  446. * will be copied to the appropriate nand_bbt_descr's.
  447. * @badblockpos: [INTERN] position of the bad block marker in the oob
  448. * area.
  449. * @badblockbits: [INTERN] minimum number of set bits in a good block's
  450. * bad block marker position; i.e., BBM == 11110111b is
  451. * not bad when badblockbits == 7
  452. * @bits_per_cell: [INTERN] number of bits per cell. i.e., 1 means SLC.
  453. * @ecc_strength_ds: [INTERN] ECC correctability from the datasheet.
  454. * Minimum amount of bit errors per @ecc_step_ds guaranteed
  455. * to be correctable. If unknown, set to zero.
  456. * @ecc_step_ds: [INTERN] ECC step required by the @ecc_strength_ds,
  457. * also from the datasheet. It is the recommended ECC step
  458. * size, if known; if unknown, set to zero.
  459. * @numchips: [INTERN] number of physical chips
  460. * @chipsize: [INTERN] the size of one chip for multichip arrays
  461. * @pagemask: [INTERN] page number mask = number of (pages / chip) - 1
  462. * @pagebuf: [INTERN] holds the pagenumber which is currently in
  463. * data_buf.
  464. * @pagebuf_bitflips: [INTERN] holds the bitflip count for the page which is
  465. * currently in data_buf.
  466. * @subpagesize: [INTERN] holds the subpagesize
  467. * @onfi_version: [INTERN] holds the chip ONFI version (BCD encoded),
  468. * non 0 if ONFI supported.
  469. * @onfi_params: [INTERN] holds the ONFI page parameter when ONFI is
  470. * supported, 0 otherwise.
  471. * @read_retries: [INTERN] the number of read retry modes supported
  472. * @onfi_set_features: [REPLACEABLE] set the features for ONFI nand
  473. * @onfi_get_features: [REPLACEABLE] get the features for ONFI nand
  474. * @bbt: [INTERN] bad block table pointer
  475. * @bbt_td: [REPLACEABLE] bad block table descriptor for flash
  476. * lookup.
  477. * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
  478. * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
  479. * bad block scan.
  480. * @controller: [REPLACEABLE] a pointer to a hardware controller
  481. * structure which is shared among multiple independent
  482. * devices.
  483. * @priv: [OPTIONAL] pointer to private chip data
  484. * @errstat: [OPTIONAL] hardware specific function to perform
  485. * additional error status checks (determine if errors are
  486. * correctable).
  487. * @write_page: [REPLACEABLE] High-level page write function
  488. */
  489. struct nand_chip {
  490. void __iomem *IO_ADDR_R;
  491. void __iomem *IO_ADDR_W;
  492. uint8_t (*read_byte)(struct mtd_info *mtd);
  493. u16 (*read_word)(struct mtd_info *mtd);
  494. void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
  495. void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
  496. void (*select_chip)(struct mtd_info *mtd, int chip);
  497. int (*block_bad)(struct mtd_info *mtd, loff_t ofs, int getchip);
  498. int (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
  499. void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
  500. int (*init_size)(struct mtd_info *mtd, struct nand_chip *this,
  501. u8 *id_data);
  502. int (*dev_ready)(struct mtd_info *mtd);
  503. void (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column,
  504. int page_addr);
  505. int(*waitfunc)(struct mtd_info *mtd, struct nand_chip *this);
  506. void (*erase_cmd)(struct mtd_info *mtd, int page);
  507. int (*scan_bbt)(struct mtd_info *mtd);
  508. int (*errstat)(struct mtd_info *mtd, struct nand_chip *this, int state,
  509. int status, int page);
  510. int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
  511. uint32_t offset, int data_len, const uint8_t *buf,
  512. int oob_required, int page, int cached, int raw);
  513. int (*onfi_set_features)(struct mtd_info *mtd, struct nand_chip *chip,
  514. int feature_addr, uint8_t *subfeature_para);
  515. int (*onfi_get_features)(struct mtd_info *mtd, struct nand_chip *chip,
  516. int feature_addr, uint8_t *subfeature_para);
  517. int (*setup_read_retry)(struct mtd_info *mtd, int retry_mode);
  518. int chip_delay;
  519. unsigned int options;
  520. unsigned int bbt_options;
  521. int page_shift;
  522. int phys_erase_shift;
  523. int bbt_erase_shift;
  524. int chip_shift;
  525. int numchips;
  526. uint64_t chipsize;
  527. int pagemask;
  528. int pagebuf;
  529. unsigned int pagebuf_bitflips;
  530. int subpagesize;
  531. uint8_t bits_per_cell;
  532. uint16_t ecc_strength_ds;
  533. uint16_t ecc_step_ds;
  534. int badblockpos;
  535. int badblockbits;
  536. int onfi_version;
  537. struct nand_onfi_params onfi_params;
  538. int read_retries;
  539. flstate_t state;
  540. uint8_t *oob_poi;
  541. struct nand_hw_control *controller;
  542. struct nand_ecc_ctrl ecc;
  543. struct nand_buffers *buffers;
  544. struct nand_hw_control hwcontrol;
  545. uint8_t *bbt;
  546. struct nand_bbt_descr *bbt_td;
  547. struct nand_bbt_descr *bbt_md;
  548. struct nand_bbt_descr *badblock_pattern;
  549. void *priv;
  550. };
  551. /*
  552. * NAND Flash Manufacturer ID Codes
  553. */
  554. #define NAND_MFR_TOSHIBA 0x98
  555. #define NAND_MFR_SAMSUNG 0xec
  556. #define NAND_MFR_FUJITSU 0x04
  557. #define NAND_MFR_NATIONAL 0x8f
  558. #define NAND_MFR_RENESAS 0x07
  559. #define NAND_MFR_STMICRO 0x20
  560. #define NAND_MFR_HYNIX 0xad
  561. #define NAND_MFR_MICRON 0x2c
  562. #define NAND_MFR_AMD 0x01
  563. #define NAND_MFR_MACRONIX 0xc2
  564. #define NAND_MFR_EON 0x92
  565. /* The maximum expected count of bytes in the NAND ID sequence */
  566. #define NAND_MAX_ID_LEN 8
  567. /*
  568. * A helper for defining older NAND chips where the second ID byte fully
  569. * defined the chip, including the geometry (chip size, eraseblock size, page
  570. * size). All these chips have 512 bytes NAND page size.
  571. */
  572. #define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
  573. { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
  574. .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
  575. /*
  576. * A helper for defining newer chips which report their page size and
  577. * eraseblock size via the extended ID bytes.
  578. *
  579. * The real difference between LEGACY_ID_NAND and EXTENDED_ID_NAND is that with
  580. * EXTENDED_ID_NAND, manufacturers overloaded the same device ID so that the
  581. * device ID now only represented a particular total chip size (and voltage,
  582. * buswidth), and the page size, eraseblock size, and OOB size could vary while
  583. * using the same device ID.
  584. */
  585. #define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
  586. { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
  587. .options = (opts) }
  588. #define NAND_ECC_INFO(_strength, _step) \
  589. { .strength_ds = (_strength), .step_ds = (_step) }
  590. #define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
  591. #define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
  592. /**
  593. * struct nand_flash_dev - NAND Flash Device ID Structure
  594. * @name: a human-readable name of the NAND chip
  595. * @dev_id: the device ID (the second byte of the full chip ID array)
  596. * @mfr_id: manufecturer ID part of the full chip ID array (refers the same
  597. * memory address as @id[0])
  598. * @dev_id: device ID part of the full chip ID array (refers the same memory
  599. * address as @id[1])
  600. * @id: full device ID array
  601. * @pagesize: size of the NAND page in bytes; if 0, then the real page size (as
  602. * well as the eraseblock size) is determined from the extended NAND
  603. * chip ID array)
  604. * @chipsize: total chip size in MiB
  605. * @erasesize: eraseblock size in bytes (determined from the extended ID if 0)
  606. * @options: stores various chip bit options
  607. * @id_len: The valid length of the @id.
  608. * @oobsize: OOB size
  609. * @ecc.strength_ds: The ECC correctability from the datasheet, same as the
  610. * @ecc_strength_ds in nand_chip{}.
  611. * @ecc.step_ds: The ECC step required by the @ecc.strength_ds, same as the
  612. * @ecc_step_ds in nand_chip{}, also from the datasheet.
  613. * For example, the "4bit ECC for each 512Byte" can be set with
  614. * NAND_ECC_INFO(4, 512).
  615. */
  616. struct nand_flash_dev {
  617. char *name;
  618. union {
  619. struct {
  620. uint8_t mfr_id;
  621. uint8_t dev_id;
  622. };
  623. uint8_t id[NAND_MAX_ID_LEN];
  624. };
  625. unsigned int pagesize;
  626. unsigned int chipsize;
  627. unsigned int erasesize;
  628. unsigned int options;
  629. uint16_t id_len;
  630. uint16_t oobsize;
  631. struct {
  632. uint16_t strength_ds;
  633. uint16_t step_ds;
  634. } ecc;
  635. };
  636. /**
  637. * struct nand_manufacturers - NAND Flash Manufacturer ID Structure
  638. * @name: Manufacturer name
  639. * @id: manufacturer ID code of device.
  640. */
  641. struct nand_manufacturers {
  642. int id;
  643. char *name;
  644. };
  645. extern struct nand_flash_dev nand_flash_ids[];
  646. extern struct nand_manufacturers nand_manuf_ids[];
  647. extern int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
  648. extern int nand_default_bbt(struct mtd_info *mtd);
  649. extern int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs);
  650. extern int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt);
  651. extern int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  652. int allowbbt);
  653. extern int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
  654. size_t *retlen, uint8_t *buf);
  655. /**
  656. * struct platform_nand_chip - chip level device structure
  657. * @nr_chips: max. number of chips to scan for
  658. * @chip_offset: chip number offset
  659. * @nr_partitions: number of partitions pointed to by partitions (or zero)
  660. * @partitions: mtd partition list
  661. * @chip_delay: R/B delay value in us
  662. * @options: Option flags, e.g. 16bit buswidth
  663. * @bbt_options: BBT option flags, e.g. NAND_BBT_USE_FLASH
  664. * @ecclayout: ECC layout info structure
  665. * @part_probe_types: NULL-terminated array of probe types
  666. */
  667. struct platform_nand_chip {
  668. int nr_chips;
  669. int chip_offset;
  670. int nr_partitions;
  671. struct mtd_partition *partitions;
  672. struct nand_ecclayout *ecclayout;
  673. int chip_delay;
  674. unsigned int options;
  675. unsigned int bbt_options;
  676. const char **part_probe_types;
  677. };
  678. /* Keep gcc happy */
  679. struct platform_device;
  680. /**
  681. * struct platform_nand_ctrl - controller level device structure
  682. * @probe: platform specific function to probe/setup hardware
  683. * @remove: platform specific function to remove/teardown hardware
  684. * @hwcontrol: platform specific hardware control structure
  685. * @dev_ready: platform specific function to read ready/busy pin
  686. * @select_chip: platform specific chip select function
  687. * @cmd_ctrl: platform specific function for controlling
  688. * ALE/CLE/nCE. Also used to write command and address
  689. * @write_buf: platform specific function for write buffer
  690. * @read_buf: platform specific function for read buffer
  691. * @read_byte: platform specific function to read one byte from chip
  692. * @priv: private data to transport driver specific settings
  693. *
  694. * All fields are optional and depend on the hardware driver requirements
  695. */
  696. struct platform_nand_ctrl {
  697. int (*probe)(struct platform_device *pdev);
  698. void (*remove)(struct platform_device *pdev);
  699. void (*hwcontrol)(struct mtd_info *mtd, int cmd);
  700. int (*dev_ready)(struct mtd_info *mtd);
  701. void (*select_chip)(struct mtd_info *mtd, int chip);
  702. void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
  703. void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
  704. void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
  705. unsigned char (*read_byte)(struct mtd_info *mtd);
  706. void *priv;
  707. };
  708. /**
  709. * struct platform_nand_data - container structure for platform-specific data
  710. * @chip: chip level chip structure
  711. * @ctrl: controller level device structure
  712. */
  713. struct platform_nand_data {
  714. struct platform_nand_chip chip;
  715. struct platform_nand_ctrl ctrl;
  716. };
  717. /* Some helpers to access the data structures */
  718. static inline
  719. struct platform_nand_chip *get_platform_nandchip(struct mtd_info *mtd)
  720. {
  721. struct nand_chip *chip = mtd->priv;
  722. return chip->priv;
  723. }
  724. /* return the supported features. */
  725. static inline int onfi_feature(struct nand_chip *chip)
  726. {
  727. return chip->onfi_version ? le16_to_cpu(chip->onfi_params.features) : 0;
  728. }
  729. /* return the supported asynchronous timing mode. */
  730. static inline int onfi_get_async_timing_mode(struct nand_chip *chip)
  731. {
  732. if (!chip->onfi_version)
  733. return ONFI_TIMING_MODE_UNKNOWN;
  734. return le16_to_cpu(chip->onfi_params.async_timing_mode);
  735. }
  736. /* return the supported synchronous timing mode. */
  737. static inline int onfi_get_sync_timing_mode(struct nand_chip *chip)
  738. {
  739. if (!chip->onfi_version)
  740. return ONFI_TIMING_MODE_UNKNOWN;
  741. return le16_to_cpu(chip->onfi_params.src_sync_timing_mode);
  742. }
  743. /*
  744. * Check if it is a SLC nand.
  745. * The !nand_is_slc() can be used to check the MLC/TLC nand chips.
  746. * We do not distinguish the MLC and TLC now.
  747. */
  748. static inline bool nand_is_slc(struct nand_chip *chip)
  749. {
  750. return chip->bits_per_cell == 1;
  751. }
  752. #endif /* __LINUX_MTD_NAND_H */