nand.h 32 KB

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