nand.h 30 KB

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