nand.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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. struct device_node;
  28. /* Scan and identify a NAND device */
  29. int nand_scan(struct mtd_info *mtd, int max_chips);
  30. /*
  31. * Separate phases of nand_scan(), allowing board driver to intervene
  32. * and override command or ECC setup according to flash type.
  33. */
  34. int nand_scan_ident(struct mtd_info *mtd, int max_chips,
  35. struct nand_flash_dev *table);
  36. int nand_scan_tail(struct mtd_info *mtd);
  37. /* Unregister the MTD device and free resources held by the NAND device */
  38. void nand_release(struct mtd_info *mtd);
  39. /* Internal helper for board drivers which need to override command function */
  40. void nand_wait_ready(struct mtd_info *mtd);
  41. /* locks all blocks present in the device */
  42. int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  43. /* unlocks specified locked blocks */
  44. int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  45. /* The maximum number of NAND chips in an array */
  46. #define NAND_MAX_CHIPS 8
  47. /*
  48. * Constants for hardware specific CLE/ALE/NCE function
  49. *
  50. * These are bits which can be or'ed to set/clear multiple
  51. * bits in one go.
  52. */
  53. /* Select the chip by setting nCE to low */
  54. #define NAND_NCE 0x01
  55. /* Select the command latch by setting CLE to high */
  56. #define NAND_CLE 0x02
  57. /* Select the address latch by setting ALE to high */
  58. #define NAND_ALE 0x04
  59. #define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
  60. #define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
  61. #define NAND_CTRL_CHANGE 0x80
  62. /*
  63. * Standard NAND flash commands
  64. */
  65. #define NAND_CMD_READ0 0
  66. #define NAND_CMD_READ1 1
  67. #define NAND_CMD_RNDOUT 5
  68. #define NAND_CMD_PAGEPROG 0x10
  69. #define NAND_CMD_READOOB 0x50
  70. #define NAND_CMD_ERASE1 0x60
  71. #define NAND_CMD_STATUS 0x70
  72. #define NAND_CMD_SEQIN 0x80
  73. #define NAND_CMD_RNDIN 0x85
  74. #define NAND_CMD_READID 0x90
  75. #define NAND_CMD_ERASE2 0xd0
  76. #define NAND_CMD_PARAM 0xec
  77. #define NAND_CMD_GET_FEATURES 0xee
  78. #define NAND_CMD_SET_FEATURES 0xef
  79. #define NAND_CMD_RESET 0xff
  80. #define NAND_CMD_LOCK 0x2a
  81. #define NAND_CMD_UNLOCK1 0x23
  82. #define NAND_CMD_UNLOCK2 0x24
  83. /* Extended commands for large page devices */
  84. #define NAND_CMD_READSTART 0x30
  85. #define NAND_CMD_RNDOUTSTART 0xE0
  86. #define NAND_CMD_CACHEDPROG 0x15
  87. #define NAND_CMD_NONE -1
  88. /* Status bits */
  89. #define NAND_STATUS_FAIL 0x01
  90. #define NAND_STATUS_FAIL_N1 0x02
  91. #define NAND_STATUS_TRUE_READY 0x20
  92. #define NAND_STATUS_READY 0x40
  93. #define NAND_STATUS_WP 0x80
  94. /*
  95. * Constants for ECC_MODES
  96. */
  97. typedef enum {
  98. NAND_ECC_NONE,
  99. NAND_ECC_SOFT,
  100. NAND_ECC_HW,
  101. NAND_ECC_HW_SYNDROME,
  102. NAND_ECC_HW_OOB_FIRST,
  103. } nand_ecc_modes_t;
  104. enum nand_ecc_algo {
  105. NAND_ECC_UNKNOWN,
  106. NAND_ECC_HAMMING,
  107. NAND_ECC_BCH,
  108. };
  109. /*
  110. * Constants for Hardware ECC
  111. */
  112. /* Reset Hardware ECC for read */
  113. #define NAND_ECC_READ 0
  114. /* Reset Hardware ECC for write */
  115. #define NAND_ECC_WRITE 1
  116. /* Enable Hardware ECC before syndrome is read back from flash */
  117. #define NAND_ECC_READSYN 2
  118. /*
  119. * Enable generic NAND 'page erased' check. This check is only done when
  120. * ecc.correct() returns -EBADMSG.
  121. * Set this flag if your implementation does not fix bitflips in erased
  122. * pages and you want to rely on the default implementation.
  123. */
  124. #define NAND_ECC_GENERIC_ERASED_CHECK BIT(0)
  125. #define NAND_ECC_MAXIMIZE BIT(1)
  126. /*
  127. * If your controller already sends the required NAND commands when
  128. * reading or writing a page, then the framework is not supposed to
  129. * send READ0 and SEQIN/PAGEPROG respectively.
  130. */
  131. #define NAND_ECC_CUSTOM_PAGE_ACCESS BIT(2)
  132. /* Bit mask for flags passed to do_nand_read_ecc */
  133. #define NAND_GET_DEVICE 0x80
  134. /*
  135. * Option constants for bizarre disfunctionality and real
  136. * features.
  137. */
  138. /* Buswidth is 16 bit */
  139. #define NAND_BUSWIDTH_16 0x00000002
  140. /* Chip has cache program function */
  141. #define NAND_CACHEPRG 0x00000008
  142. /*
  143. * Chip requires ready check on read (for auto-incremented sequential read).
  144. * True only for small page devices; large page devices do not support
  145. * autoincrement.
  146. */
  147. #define NAND_NEED_READRDY 0x00000100
  148. /* Chip does not allow subpage writes */
  149. #define NAND_NO_SUBPAGE_WRITE 0x00000200
  150. /* Device is one of 'new' xD cards that expose fake nand command set */
  151. #define NAND_BROKEN_XD 0x00000400
  152. /* Device behaves just like nand, but is readonly */
  153. #define NAND_ROM 0x00000800
  154. /* Device supports subpage reads */
  155. #define NAND_SUBPAGE_READ 0x00001000
  156. /*
  157. * Some MLC NANDs need data scrambling to limit bitflips caused by repeated
  158. * patterns.
  159. */
  160. #define NAND_NEED_SCRAMBLING 0x00002000
  161. /* Options valid for Samsung large page devices */
  162. #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
  163. /* Macros to identify the above */
  164. #define NAND_HAS_CACHEPROG(chip) ((chip->options & NAND_CACHEPRG))
  165. #define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
  166. #define NAND_HAS_SUBPAGE_WRITE(chip) !((chip)->options & NAND_NO_SUBPAGE_WRITE)
  167. /* Non chip related options */
  168. /* This option skips the bbt scan during initialization. */
  169. #define NAND_SKIP_BBTSCAN 0x00010000
  170. /*
  171. * This option is defined if the board driver allocates its own buffers
  172. * (e.g. because it needs them DMA-coherent).
  173. */
  174. #define NAND_OWN_BUFFERS 0x00020000
  175. /* Chip may not exist, so silence any errors in scan */
  176. #define NAND_SCAN_SILENT_NODEV 0x00040000
  177. /*
  178. * Autodetect nand buswidth with readid/onfi.
  179. * This suppose the driver will configure the hardware in 8 bits mode
  180. * when calling nand_scan_ident, and update its configuration
  181. * before calling nand_scan_tail.
  182. */
  183. #define NAND_BUSWIDTH_AUTO 0x00080000
  184. /*
  185. * This option could be defined by controller drivers to protect against
  186. * kmap'ed, vmalloc'ed highmem buffers being passed from upper layers
  187. */
  188. #define NAND_USE_BOUNCE_BUFFER 0x00100000
  189. /*
  190. * In case your controller is implementing ->cmd_ctrl() and is relying on the
  191. * default ->cmdfunc() implementation, you may want to let the core handle the
  192. * tCCS delay which is required when a column change (RNDIN or RNDOUT) is
  193. * requested.
  194. * If your controller already takes care of this delay, you don't need to set
  195. * this flag.
  196. */
  197. #define NAND_WAIT_TCCS 0x00200000
  198. /* Options set by nand scan */
  199. /* Nand scan has allocated controller struct */
  200. #define NAND_CONTROLLER_ALLOC 0x80000000
  201. /* Cell info constants */
  202. #define NAND_CI_CHIPNR_MSK 0x03
  203. #define NAND_CI_CELLTYPE_MSK 0x0C
  204. #define NAND_CI_CELLTYPE_SHIFT 2
  205. /* Keep gcc happy */
  206. struct nand_chip;
  207. /* ONFI features */
  208. #define ONFI_FEATURE_16_BIT_BUS (1 << 0)
  209. #define ONFI_FEATURE_EXT_PARAM_PAGE (1 << 7)
  210. /* ONFI timing mode, used in both asynchronous and synchronous mode */
  211. #define ONFI_TIMING_MODE_0 (1 << 0)
  212. #define ONFI_TIMING_MODE_1 (1 << 1)
  213. #define ONFI_TIMING_MODE_2 (1 << 2)
  214. #define ONFI_TIMING_MODE_3 (1 << 3)
  215. #define ONFI_TIMING_MODE_4 (1 << 4)
  216. #define ONFI_TIMING_MODE_5 (1 << 5)
  217. #define ONFI_TIMING_MODE_UNKNOWN (1 << 6)
  218. /* ONFI feature address */
  219. #define ONFI_FEATURE_ADDR_TIMING_MODE 0x1
  220. /* Vendor-specific feature address (Micron) */
  221. #define ONFI_FEATURE_ADDR_READ_RETRY 0x89
  222. /* ONFI subfeature parameters length */
  223. #define ONFI_SUBFEATURE_PARAM_LEN 4
  224. /* ONFI optional commands SET/GET FEATURES supported? */
  225. #define ONFI_OPT_CMD_SET_GET_FEATURES (1 << 2)
  226. struct nand_onfi_params {
  227. /* rev info and features block */
  228. /* 'O' 'N' 'F' 'I' */
  229. u8 sig[4];
  230. __le16 revision;
  231. __le16 features;
  232. __le16 opt_cmd;
  233. u8 reserved0[2];
  234. __le16 ext_param_page_length; /* since ONFI 2.1 */
  235. u8 num_of_param_pages; /* since ONFI 2.1 */
  236. u8 reserved1[17];
  237. /* manufacturer information block */
  238. char manufacturer[12];
  239. char model[20];
  240. u8 jedec_id;
  241. __le16 date_code;
  242. u8 reserved2[13];
  243. /* memory organization block */
  244. __le32 byte_per_page;
  245. __le16 spare_bytes_per_page;
  246. __le32 data_bytes_per_ppage;
  247. __le16 spare_bytes_per_ppage;
  248. __le32 pages_per_block;
  249. __le32 blocks_per_lun;
  250. u8 lun_count;
  251. u8 addr_cycles;
  252. u8 bits_per_cell;
  253. __le16 bb_per_lun;
  254. __le16 block_endurance;
  255. u8 guaranteed_good_blocks;
  256. __le16 guaranteed_block_endurance;
  257. u8 programs_per_page;
  258. u8 ppage_attr;
  259. u8 ecc_bits;
  260. u8 interleaved_bits;
  261. u8 interleaved_ops;
  262. u8 reserved3[13];
  263. /* electrical parameter block */
  264. u8 io_pin_capacitance_max;
  265. __le16 async_timing_mode;
  266. __le16 program_cache_timing_mode;
  267. __le16 t_prog;
  268. __le16 t_bers;
  269. __le16 t_r;
  270. __le16 t_ccs;
  271. __le16 src_sync_timing_mode;
  272. u8 src_ssync_features;
  273. __le16 clk_pin_capacitance_typ;
  274. __le16 io_pin_capacitance_typ;
  275. __le16 input_pin_capacitance_typ;
  276. u8 input_pin_capacitance_max;
  277. u8 driver_strength_support;
  278. __le16 t_int_r;
  279. __le16 t_adl;
  280. u8 reserved4[8];
  281. /* vendor */
  282. __le16 vendor_revision;
  283. u8 vendor[88];
  284. __le16 crc;
  285. } __packed;
  286. #define ONFI_CRC_BASE 0x4F4E
  287. /* Extended ECC information Block Definition (since ONFI 2.1) */
  288. struct onfi_ext_ecc_info {
  289. u8 ecc_bits;
  290. u8 codeword_size;
  291. __le16 bb_per_lun;
  292. __le16 block_endurance;
  293. u8 reserved[2];
  294. } __packed;
  295. #define ONFI_SECTION_TYPE_0 0 /* Unused section. */
  296. #define ONFI_SECTION_TYPE_1 1 /* for additional sections. */
  297. #define ONFI_SECTION_TYPE_2 2 /* for ECC information. */
  298. struct onfi_ext_section {
  299. u8 type;
  300. u8 length;
  301. } __packed;
  302. #define ONFI_EXT_SECTION_MAX 8
  303. /* Extended Parameter Page Definition (since ONFI 2.1) */
  304. struct onfi_ext_param_page {
  305. __le16 crc;
  306. u8 sig[4]; /* 'E' 'P' 'P' 'S' */
  307. u8 reserved0[10];
  308. struct onfi_ext_section sections[ONFI_EXT_SECTION_MAX];
  309. /*
  310. * The actual size of the Extended Parameter Page is in
  311. * @ext_param_page_length of nand_onfi_params{}.
  312. * The following are the variable length sections.
  313. * So we do not add any fields below. Please see the ONFI spec.
  314. */
  315. } __packed;
  316. struct nand_onfi_vendor_micron {
  317. u8 two_plane_read;
  318. u8 read_cache;
  319. u8 read_unique_id;
  320. u8 dq_imped;
  321. u8 dq_imped_num_settings;
  322. u8 dq_imped_feat_addr;
  323. u8 rb_pulldown_strength;
  324. u8 rb_pulldown_strength_feat_addr;
  325. u8 rb_pulldown_strength_num_settings;
  326. u8 otp_mode;
  327. u8 otp_page_start;
  328. u8 otp_data_prot_addr;
  329. u8 otp_num_pages;
  330. u8 otp_feat_addr;
  331. u8 read_retry_options;
  332. u8 reserved[72];
  333. u8 param_revision;
  334. } __packed;
  335. struct jedec_ecc_info {
  336. u8 ecc_bits;
  337. u8 codeword_size;
  338. __le16 bb_per_lun;
  339. __le16 block_endurance;
  340. u8 reserved[2];
  341. } __packed;
  342. /* JEDEC features */
  343. #define JEDEC_FEATURE_16_BIT_BUS (1 << 0)
  344. struct nand_jedec_params {
  345. /* rev info and features block */
  346. /* 'J' 'E' 'S' 'D' */
  347. u8 sig[4];
  348. __le16 revision;
  349. __le16 features;
  350. u8 opt_cmd[3];
  351. __le16 sec_cmd;
  352. u8 num_of_param_pages;
  353. u8 reserved0[18];
  354. /* manufacturer information block */
  355. char manufacturer[12];
  356. char model[20];
  357. u8 jedec_id[6];
  358. u8 reserved1[10];
  359. /* memory organization block */
  360. __le32 byte_per_page;
  361. __le16 spare_bytes_per_page;
  362. u8 reserved2[6];
  363. __le32 pages_per_block;
  364. __le32 blocks_per_lun;
  365. u8 lun_count;
  366. u8 addr_cycles;
  367. u8 bits_per_cell;
  368. u8 programs_per_page;
  369. u8 multi_plane_addr;
  370. u8 multi_plane_op_attr;
  371. u8 reserved3[38];
  372. /* electrical parameter block */
  373. __le16 async_sdr_speed_grade;
  374. __le16 toggle_ddr_speed_grade;
  375. __le16 sync_ddr_speed_grade;
  376. u8 async_sdr_features;
  377. u8 toggle_ddr_features;
  378. u8 sync_ddr_features;
  379. __le16 t_prog;
  380. __le16 t_bers;
  381. __le16 t_r;
  382. __le16 t_r_multi_plane;
  383. __le16 t_ccs;
  384. __le16 io_pin_capacitance_typ;
  385. __le16 input_pin_capacitance_typ;
  386. __le16 clk_pin_capacitance_typ;
  387. u8 driver_strength_support;
  388. __le16 t_adl;
  389. u8 reserved4[36];
  390. /* ECC and endurance block */
  391. u8 guaranteed_good_blocks;
  392. __le16 guaranteed_block_endurance;
  393. struct jedec_ecc_info ecc_info[4];
  394. u8 reserved5[29];
  395. /* reserved */
  396. u8 reserved6[148];
  397. /* vendor */
  398. __le16 vendor_rev_num;
  399. u8 reserved7[88];
  400. /* CRC for Parameter Page */
  401. __le16 crc;
  402. } __packed;
  403. /**
  404. * struct nand_id - NAND id structure
  405. * @data: buffer containing the id bytes. Currently 8 bytes large, but can
  406. * be extended if required.
  407. * @len: ID length.
  408. */
  409. struct nand_id {
  410. u8 data[8];
  411. int len;
  412. };
  413. /**
  414. * struct nand_hw_control - Control structure for hardware controller (e.g ECC generator) shared among independent devices
  415. * @lock: protection lock
  416. * @active: the mtd device which holds the controller currently
  417. * @wq: wait queue to sleep on if a NAND operation is in
  418. * progress used instead of the per chip wait queue
  419. * when a hw controller is available.
  420. */
  421. struct nand_hw_control {
  422. spinlock_t lock;
  423. struct nand_chip *active;
  424. wait_queue_head_t wq;
  425. };
  426. static inline void nand_hw_control_init(struct nand_hw_control *nfc)
  427. {
  428. nfc->active = NULL;
  429. spin_lock_init(&nfc->lock);
  430. init_waitqueue_head(&nfc->wq);
  431. }
  432. /**
  433. * struct nand_ecc_ctrl - Control structure for ECC
  434. * @mode: ECC mode
  435. * @algo: ECC algorithm
  436. * @steps: number of ECC steps per page
  437. * @size: data bytes per ECC step
  438. * @bytes: ECC bytes per step
  439. * @strength: max number of correctible bits per ECC step
  440. * @total: total number of ECC bytes per page
  441. * @prepad: padding information for syndrome based ECC generators
  442. * @postpad: padding information for syndrome based ECC generators
  443. * @options: ECC specific options (see NAND_ECC_XXX flags defined above)
  444. * @priv: pointer to private ECC control data
  445. * @hwctl: function to control hardware ECC generator. Must only
  446. * be provided if an hardware ECC is available
  447. * @calculate: function for ECC calculation or readback from ECC hardware
  448. * @correct: function for ECC correction, matching to ECC generator (sw/hw).
  449. * Should return a positive number representing the number of
  450. * corrected bitflips, -EBADMSG if the number of bitflips exceed
  451. * ECC strength, or any other error code if the error is not
  452. * directly related to correction.
  453. * If -EBADMSG is returned the input buffers should be left
  454. * untouched.
  455. * @read_page_raw: function to read a raw page without ECC. This function
  456. * should hide the specific layout used by the ECC
  457. * controller and always return contiguous in-band and
  458. * out-of-band data even if they're not stored
  459. * contiguously on the NAND chip (e.g.
  460. * NAND_ECC_HW_SYNDROME interleaves in-band and
  461. * out-of-band data).
  462. * @write_page_raw: function to write a raw page without ECC. This function
  463. * should hide the specific layout used by the ECC
  464. * controller and consider the passed data as contiguous
  465. * in-band and out-of-band data. ECC controller is
  466. * responsible for doing the appropriate transformations
  467. * to adapt to its specific layout (e.g.
  468. * NAND_ECC_HW_SYNDROME interleaves in-band and
  469. * out-of-band data).
  470. * @read_page: function to read a page according to the ECC generator
  471. * requirements; returns maximum number of bitflips corrected in
  472. * any single ECC step, 0 if bitflips uncorrectable, -EIO hw error
  473. * @read_subpage: function to read parts of the page covered by ECC;
  474. * returns same as read_page()
  475. * @write_subpage: function to write parts of the page covered by ECC.
  476. * @write_page: function to write a page according to the ECC generator
  477. * requirements.
  478. * @write_oob_raw: function to write chip OOB data without ECC
  479. * @read_oob_raw: function to read chip OOB data without ECC
  480. * @read_oob: function to read chip OOB data
  481. * @write_oob: function to write chip OOB data
  482. */
  483. struct nand_ecc_ctrl {
  484. nand_ecc_modes_t mode;
  485. enum nand_ecc_algo algo;
  486. int steps;
  487. int size;
  488. int bytes;
  489. int total;
  490. int strength;
  491. int prepad;
  492. int postpad;
  493. unsigned int options;
  494. void *priv;
  495. void (*hwctl)(struct mtd_info *mtd, int mode);
  496. int (*calculate)(struct mtd_info *mtd, const uint8_t *dat,
  497. uint8_t *ecc_code);
  498. int (*correct)(struct mtd_info *mtd, uint8_t *dat, uint8_t *read_ecc,
  499. uint8_t *calc_ecc);
  500. int (*read_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  501. uint8_t *buf, int oob_required, int page);
  502. int (*write_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  503. const uint8_t *buf, int oob_required, int page);
  504. int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
  505. uint8_t *buf, int oob_required, int page);
  506. int (*read_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
  507. uint32_t offs, uint32_t len, uint8_t *buf, int page);
  508. int (*write_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
  509. uint32_t offset, uint32_t data_len,
  510. const uint8_t *data_buf, int oob_required, int page);
  511. int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
  512. const uint8_t *buf, int oob_required, int page);
  513. int (*write_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  514. int page);
  515. int (*read_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
  516. int page);
  517. int (*read_oob)(struct mtd_info *mtd, struct nand_chip *chip, int page);
  518. int (*write_oob)(struct mtd_info *mtd, struct nand_chip *chip,
  519. int page);
  520. };
  521. static inline int nand_standard_page_accessors(struct nand_ecc_ctrl *ecc)
  522. {
  523. return !(ecc->options & NAND_ECC_CUSTOM_PAGE_ACCESS);
  524. }
  525. /**
  526. * struct nand_buffers - buffer structure for read/write
  527. * @ecccalc: buffer pointer for calculated ECC, size is oobsize.
  528. * @ecccode: buffer pointer for ECC read from flash, size is oobsize.
  529. * @databuf: buffer pointer for data, size is (page size + oobsize).
  530. *
  531. * Do not change the order of buffers. databuf and oobrbuf must be in
  532. * consecutive order.
  533. */
  534. struct nand_buffers {
  535. uint8_t *ecccalc;
  536. uint8_t *ecccode;
  537. uint8_t *databuf;
  538. };
  539. /**
  540. * struct nand_sdr_timings - SDR NAND chip timings
  541. *
  542. * This struct defines the timing requirements of a SDR NAND chip.
  543. * These information can be found in every NAND datasheets and the timings
  544. * meaning are described in the ONFI specifications:
  545. * www.onfi.org/~/media/ONFI/specs/onfi_3_1_spec.pdf (chapter 4.15 Timing
  546. * Parameters)
  547. *
  548. * All these timings are expressed in picoseconds.
  549. *
  550. * @tBERS_max: Block erase time
  551. * @tCCS_min: Change column setup time
  552. * @tPROG_max: Page program time
  553. * @tR_max: Page read time
  554. * @tALH_min: ALE hold time
  555. * @tADL_min: ALE to data loading time
  556. * @tALS_min: ALE setup time
  557. * @tAR_min: ALE to RE# delay
  558. * @tCEA_max: CE# access time
  559. * @tCEH_min: CE# high hold time
  560. * @tCH_min: CE# hold time
  561. * @tCHZ_max: CE# high to output hi-Z
  562. * @tCLH_min: CLE hold time
  563. * @tCLR_min: CLE to RE# delay
  564. * @tCLS_min: CLE setup time
  565. * @tCOH_min: CE# high to output hold
  566. * @tCS_min: CE# setup time
  567. * @tDH_min: Data hold time
  568. * @tDS_min: Data setup time
  569. * @tFEAT_max: Busy time for Set Features and Get Features
  570. * @tIR_min: Output hi-Z to RE# low
  571. * @tITC_max: Interface and Timing Mode Change time
  572. * @tRC_min: RE# cycle time
  573. * @tREA_max: RE# access time
  574. * @tREH_min: RE# high hold time
  575. * @tRHOH_min: RE# high to output hold
  576. * @tRHW_min: RE# high to WE# low
  577. * @tRHZ_max: RE# high to output hi-Z
  578. * @tRLOH_min: RE# low to output hold
  579. * @tRP_min: RE# pulse width
  580. * @tRR_min: Ready to RE# low (data only)
  581. * @tRST_max: Device reset time, measured from the falling edge of R/B# to the
  582. * rising edge of R/B#.
  583. * @tWB_max: WE# high to SR[6] low
  584. * @tWC_min: WE# cycle time
  585. * @tWH_min: WE# high hold time
  586. * @tWHR_min: WE# high to RE# low
  587. * @tWP_min: WE# pulse width
  588. * @tWW_min: WP# transition to WE# low
  589. */
  590. struct nand_sdr_timings {
  591. u32 tBERS_max;
  592. u32 tCCS_min;
  593. u32 tPROG_max;
  594. u32 tR_max;
  595. u32 tALH_min;
  596. u32 tADL_min;
  597. u32 tALS_min;
  598. u32 tAR_min;
  599. u32 tCEA_max;
  600. u32 tCEH_min;
  601. u32 tCH_min;
  602. u32 tCHZ_max;
  603. u32 tCLH_min;
  604. u32 tCLR_min;
  605. u32 tCLS_min;
  606. u32 tCOH_min;
  607. u32 tCS_min;
  608. u32 tDH_min;
  609. u32 tDS_min;
  610. u32 tFEAT_max;
  611. u32 tIR_min;
  612. u32 tITC_max;
  613. u32 tRC_min;
  614. u32 tREA_max;
  615. u32 tREH_min;
  616. u32 tRHOH_min;
  617. u32 tRHW_min;
  618. u32 tRHZ_max;
  619. u32 tRLOH_min;
  620. u32 tRP_min;
  621. u32 tRR_min;
  622. u64 tRST_max;
  623. u32 tWB_max;
  624. u32 tWC_min;
  625. u32 tWH_min;
  626. u32 tWHR_min;
  627. u32 tWP_min;
  628. u32 tWW_min;
  629. };
  630. /**
  631. * enum nand_data_interface_type - NAND interface timing type
  632. * @NAND_SDR_IFACE: Single Data Rate interface
  633. */
  634. enum nand_data_interface_type {
  635. NAND_SDR_IFACE,
  636. };
  637. /**
  638. * struct nand_data_interface - NAND interface timing
  639. * @type: type of the timing
  640. * @timings: The timing, type according to @type
  641. */
  642. struct nand_data_interface {
  643. enum nand_data_interface_type type;
  644. union {
  645. struct nand_sdr_timings sdr;
  646. } timings;
  647. };
  648. /**
  649. * nand_get_sdr_timings - get SDR timing from data interface
  650. * @conf: The data interface
  651. */
  652. static inline const struct nand_sdr_timings *
  653. nand_get_sdr_timings(const struct nand_data_interface *conf)
  654. {
  655. if (conf->type != NAND_SDR_IFACE)
  656. return ERR_PTR(-EINVAL);
  657. return &conf->timings.sdr;
  658. }
  659. /**
  660. * struct nand_manufacturer_ops - NAND Manufacturer operations
  661. * @detect: detect the NAND memory organization and capabilities
  662. * @init: initialize all vendor specific fields (like the ->read_retry()
  663. * implementation) if any.
  664. * @cleanup: the ->init() function may have allocated resources, ->cleanup()
  665. * is here to let vendor specific code release those resources.
  666. */
  667. struct nand_manufacturer_ops {
  668. void (*detect)(struct nand_chip *chip);
  669. int (*init)(struct nand_chip *chip);
  670. void (*cleanup)(struct nand_chip *chip);
  671. };
  672. /**
  673. * struct nand_chip - NAND Private Flash Chip Data
  674. * @mtd: MTD device registered to the MTD framework
  675. * @IO_ADDR_R: [BOARDSPECIFIC] address to read the 8 I/O lines of the
  676. * flash device
  677. * @IO_ADDR_W: [BOARDSPECIFIC] address to write the 8 I/O lines of the
  678. * flash device.
  679. * @read_byte: [REPLACEABLE] read one byte from the chip
  680. * @read_word: [REPLACEABLE] read one word from the chip
  681. * @write_byte: [REPLACEABLE] write a single byte to the chip on the
  682. * low 8 I/O lines
  683. * @write_buf: [REPLACEABLE] write data from the buffer to the chip
  684. * @read_buf: [REPLACEABLE] read data from the chip into the buffer
  685. * @select_chip: [REPLACEABLE] select chip nr
  686. * @block_bad: [REPLACEABLE] check if a block is bad, using OOB markers
  687. * @block_markbad: [REPLACEABLE] mark a block bad
  688. * @cmd_ctrl: [BOARDSPECIFIC] hardwarespecific function for controlling
  689. * ALE/CLE/nCE. Also used to write command and address
  690. * @dev_ready: [BOARDSPECIFIC] hardwarespecific function for accessing
  691. * device ready/busy line. If set to NULL no access to
  692. * ready/busy is available and the ready/busy information
  693. * is read from the chip status register.
  694. * @cmdfunc: [REPLACEABLE] hardwarespecific function for writing
  695. * commands to the chip.
  696. * @waitfunc: [REPLACEABLE] hardwarespecific function for wait on
  697. * ready.
  698. * @setup_read_retry: [FLASHSPECIFIC] flash (vendor) specific function for
  699. * setting the read-retry mode. Mostly needed for MLC NAND.
  700. * @ecc: [BOARDSPECIFIC] ECC control structure
  701. * @buffers: buffer structure for read/write
  702. * @hwcontrol: platform-specific hardware control structure
  703. * @erase: [REPLACEABLE] erase function
  704. * @scan_bbt: [REPLACEABLE] function to scan bad block table
  705. * @chip_delay: [BOARDSPECIFIC] chip dependent delay for transferring
  706. * data from array to read regs (tR).
  707. * @state: [INTERN] the current state of the NAND device
  708. * @oob_poi: "poison value buffer," used for laying out OOB data
  709. * before writing
  710. * @page_shift: [INTERN] number of address bits in a page (column
  711. * address bits).
  712. * @phys_erase_shift: [INTERN] number of address bits in a physical eraseblock
  713. * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
  714. * @chip_shift: [INTERN] number of address bits in one chip
  715. * @options: [BOARDSPECIFIC] various chip options. They can partly
  716. * be set to inform nand_scan about special functionality.
  717. * See the defines for further explanation.
  718. * @bbt_options: [INTERN] bad block specific options. All options used
  719. * here must come from bbm.h. By default, these options
  720. * will be copied to the appropriate nand_bbt_descr's.
  721. * @badblockpos: [INTERN] position of the bad block marker in the oob
  722. * area.
  723. * @badblockbits: [INTERN] minimum number of set bits in a good block's
  724. * bad block marker position; i.e., BBM == 11110111b is
  725. * not bad when badblockbits == 7
  726. * @bits_per_cell: [INTERN] number of bits per cell. i.e., 1 means SLC.
  727. * @ecc_strength_ds: [INTERN] ECC correctability from the datasheet.
  728. * Minimum amount of bit errors per @ecc_step_ds guaranteed
  729. * to be correctable. If unknown, set to zero.
  730. * @ecc_step_ds: [INTERN] ECC step required by the @ecc_strength_ds,
  731. * also from the datasheet. It is the recommended ECC step
  732. * size, if known; if unknown, set to zero.
  733. * @onfi_timing_mode_default: [INTERN] default ONFI timing mode. This field is
  734. * set to the actually used ONFI mode if the chip is
  735. * ONFI compliant or deduced from the datasheet if
  736. * the NAND chip is not ONFI compliant.
  737. * @numchips: [INTERN] number of physical chips
  738. * @chipsize: [INTERN] the size of one chip for multichip arrays
  739. * @pagemask: [INTERN] page number mask = number of (pages / chip) - 1
  740. * @pagebuf: [INTERN] holds the pagenumber which is currently in
  741. * data_buf.
  742. * @pagebuf_bitflips: [INTERN] holds the bitflip count for the page which is
  743. * currently in data_buf.
  744. * @subpagesize: [INTERN] holds the subpagesize
  745. * @id: [INTERN] holds NAND ID
  746. * @onfi_version: [INTERN] holds the chip ONFI version (BCD encoded),
  747. * non 0 if ONFI supported.
  748. * @jedec_version: [INTERN] holds the chip JEDEC version (BCD encoded),
  749. * non 0 if JEDEC supported.
  750. * @onfi_params: [INTERN] holds the ONFI page parameter when ONFI is
  751. * supported, 0 otherwise.
  752. * @jedec_params: [INTERN] holds the JEDEC parameter page when JEDEC is
  753. * supported, 0 otherwise.
  754. * @max_bb_per_die: [INTERN] the max number of bad blocks each die of a
  755. * this nand device will encounter their life times.
  756. * @blocks_per_die: [INTERN] The number of PEBs in a die
  757. * @data_interface: [INTERN] NAND interface timing information
  758. * @read_retries: [INTERN] the number of read retry modes supported
  759. * @onfi_set_features: [REPLACEABLE] set the features for ONFI nand
  760. * @onfi_get_features: [REPLACEABLE] get the features for ONFI nand
  761. * @setup_data_interface: [OPTIONAL] setup the data interface and timing
  762. * @bbt: [INTERN] bad block table pointer
  763. * @bbt_td: [REPLACEABLE] bad block table descriptor for flash
  764. * lookup.
  765. * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
  766. * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
  767. * bad block scan.
  768. * @controller: [REPLACEABLE] a pointer to a hardware controller
  769. * structure which is shared among multiple independent
  770. * devices.
  771. * @priv: [OPTIONAL] pointer to private chip data
  772. * @errstat: [OPTIONAL] hardware specific function to perform
  773. * additional error status checks (determine if errors are
  774. * correctable).
  775. * @write_page: [REPLACEABLE] High-level page write function
  776. * @manufacturer: [INTERN] Contains manufacturer information
  777. */
  778. struct nand_chip {
  779. struct mtd_info mtd;
  780. void __iomem *IO_ADDR_R;
  781. void __iomem *IO_ADDR_W;
  782. uint8_t (*read_byte)(struct mtd_info *mtd);
  783. u16 (*read_word)(struct mtd_info *mtd);
  784. void (*write_byte)(struct mtd_info *mtd, uint8_t byte);
  785. void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
  786. void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
  787. void (*select_chip)(struct mtd_info *mtd, int chip);
  788. int (*block_bad)(struct mtd_info *mtd, loff_t ofs);
  789. int (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
  790. void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
  791. int (*dev_ready)(struct mtd_info *mtd);
  792. void (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column,
  793. int page_addr);
  794. int(*waitfunc)(struct mtd_info *mtd, struct nand_chip *this);
  795. int (*erase)(struct mtd_info *mtd, int page);
  796. int (*scan_bbt)(struct mtd_info *mtd);
  797. int (*errstat)(struct mtd_info *mtd, struct nand_chip *this, int state,
  798. int status, int page);
  799. int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
  800. uint32_t offset, int data_len, const uint8_t *buf,
  801. int oob_required, int page, int cached, int raw);
  802. int (*onfi_set_features)(struct mtd_info *mtd, struct nand_chip *chip,
  803. int feature_addr, uint8_t *subfeature_para);
  804. int (*onfi_get_features)(struct mtd_info *mtd, struct nand_chip *chip,
  805. int feature_addr, uint8_t *subfeature_para);
  806. int (*setup_read_retry)(struct mtd_info *mtd, int retry_mode);
  807. int (*setup_data_interface)(struct mtd_info *mtd,
  808. const struct nand_data_interface *conf,
  809. bool check_only);
  810. int chip_delay;
  811. unsigned int options;
  812. unsigned int bbt_options;
  813. int page_shift;
  814. int phys_erase_shift;
  815. int bbt_erase_shift;
  816. int chip_shift;
  817. int numchips;
  818. uint64_t chipsize;
  819. int pagemask;
  820. int pagebuf;
  821. unsigned int pagebuf_bitflips;
  822. int subpagesize;
  823. uint8_t bits_per_cell;
  824. uint16_t ecc_strength_ds;
  825. uint16_t ecc_step_ds;
  826. int onfi_timing_mode_default;
  827. int badblockpos;
  828. int badblockbits;
  829. struct nand_id id;
  830. int onfi_version;
  831. int jedec_version;
  832. union {
  833. struct nand_onfi_params onfi_params;
  834. struct nand_jedec_params jedec_params;
  835. };
  836. u16 max_bb_per_die;
  837. u32 blocks_per_die;
  838. struct nand_data_interface *data_interface;
  839. int read_retries;
  840. flstate_t state;
  841. uint8_t *oob_poi;
  842. struct nand_hw_control *controller;
  843. struct nand_ecc_ctrl ecc;
  844. struct nand_buffers *buffers;
  845. struct nand_hw_control hwcontrol;
  846. uint8_t *bbt;
  847. struct nand_bbt_descr *bbt_td;
  848. struct nand_bbt_descr *bbt_md;
  849. struct nand_bbt_descr *badblock_pattern;
  850. void *priv;
  851. struct {
  852. const struct nand_manufacturer *desc;
  853. void *priv;
  854. } manufacturer;
  855. };
  856. extern const struct mtd_ooblayout_ops nand_ooblayout_sp_ops;
  857. extern const struct mtd_ooblayout_ops nand_ooblayout_lp_ops;
  858. static inline void nand_set_flash_node(struct nand_chip *chip,
  859. struct device_node *np)
  860. {
  861. mtd_set_of_node(&chip->mtd, np);
  862. }
  863. static inline struct device_node *nand_get_flash_node(struct nand_chip *chip)
  864. {
  865. return mtd_get_of_node(&chip->mtd);
  866. }
  867. static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
  868. {
  869. return container_of(mtd, struct nand_chip, mtd);
  870. }
  871. static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
  872. {
  873. return &chip->mtd;
  874. }
  875. static inline void *nand_get_controller_data(struct nand_chip *chip)
  876. {
  877. return chip->priv;
  878. }
  879. static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
  880. {
  881. chip->priv = priv;
  882. }
  883. static inline void nand_set_manufacturer_data(struct nand_chip *chip,
  884. void *priv)
  885. {
  886. chip->manufacturer.priv = priv;
  887. }
  888. static inline void *nand_get_manufacturer_data(struct nand_chip *chip)
  889. {
  890. return chip->manufacturer.priv;
  891. }
  892. /*
  893. * NAND Flash Manufacturer ID Codes
  894. */
  895. #define NAND_MFR_TOSHIBA 0x98
  896. #define NAND_MFR_ESMT 0xc8
  897. #define NAND_MFR_SAMSUNG 0xec
  898. #define NAND_MFR_FUJITSU 0x04
  899. #define NAND_MFR_NATIONAL 0x8f
  900. #define NAND_MFR_RENESAS 0x07
  901. #define NAND_MFR_STMICRO 0x20
  902. #define NAND_MFR_HYNIX 0xad
  903. #define NAND_MFR_MICRON 0x2c
  904. #define NAND_MFR_AMD 0x01
  905. #define NAND_MFR_MACRONIX 0xc2
  906. #define NAND_MFR_EON 0x92
  907. #define NAND_MFR_SANDISK 0x45
  908. #define NAND_MFR_INTEL 0x89
  909. #define NAND_MFR_ATO 0x9b
  910. #define NAND_MFR_WINBOND 0xef
  911. /* The maximum expected count of bytes in the NAND ID sequence */
  912. #define NAND_MAX_ID_LEN 8
  913. /*
  914. * A helper for defining older NAND chips where the second ID byte fully
  915. * defined the chip, including the geometry (chip size, eraseblock size, page
  916. * size). All these chips have 512 bytes NAND page size.
  917. */
  918. #define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
  919. { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
  920. .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
  921. /*
  922. * A helper for defining newer chips which report their page size and
  923. * eraseblock size via the extended ID bytes.
  924. *
  925. * The real difference between LEGACY_ID_NAND and EXTENDED_ID_NAND is that with
  926. * EXTENDED_ID_NAND, manufacturers overloaded the same device ID so that the
  927. * device ID now only represented a particular total chip size (and voltage,
  928. * buswidth), and the page size, eraseblock size, and OOB size could vary while
  929. * using the same device ID.
  930. */
  931. #define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
  932. { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
  933. .options = (opts) }
  934. #define NAND_ECC_INFO(_strength, _step) \
  935. { .strength_ds = (_strength), .step_ds = (_step) }
  936. #define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
  937. #define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
  938. /**
  939. * struct nand_flash_dev - NAND Flash Device ID Structure
  940. * @name: a human-readable name of the NAND chip
  941. * @dev_id: the device ID (the second byte of the full chip ID array)
  942. * @mfr_id: manufecturer ID part of the full chip ID array (refers the same
  943. * memory address as @id[0])
  944. * @dev_id: device ID part of the full chip ID array (refers the same memory
  945. * address as @id[1])
  946. * @id: full device ID array
  947. * @pagesize: size of the NAND page in bytes; if 0, then the real page size (as
  948. * well as the eraseblock size) is determined from the extended NAND
  949. * chip ID array)
  950. * @chipsize: total chip size in MiB
  951. * @erasesize: eraseblock size in bytes (determined from the extended ID if 0)
  952. * @options: stores various chip bit options
  953. * @id_len: The valid length of the @id.
  954. * @oobsize: OOB size
  955. * @ecc: ECC correctability and step information from the datasheet.
  956. * @ecc.strength_ds: The ECC correctability from the datasheet, same as the
  957. * @ecc_strength_ds in nand_chip{}.
  958. * @ecc.step_ds: The ECC step required by the @ecc.strength_ds, same as the
  959. * @ecc_step_ds in nand_chip{}, also from the datasheet.
  960. * For example, the "4bit ECC for each 512Byte" can be set with
  961. * NAND_ECC_INFO(4, 512).
  962. * @onfi_timing_mode_default: the default ONFI timing mode entered after a NAND
  963. * reset. Should be deduced from timings described
  964. * in the datasheet.
  965. *
  966. */
  967. struct nand_flash_dev {
  968. char *name;
  969. union {
  970. struct {
  971. uint8_t mfr_id;
  972. uint8_t dev_id;
  973. };
  974. uint8_t id[NAND_MAX_ID_LEN];
  975. };
  976. unsigned int pagesize;
  977. unsigned int chipsize;
  978. unsigned int erasesize;
  979. unsigned int options;
  980. uint16_t id_len;
  981. uint16_t oobsize;
  982. struct {
  983. uint16_t strength_ds;
  984. uint16_t step_ds;
  985. } ecc;
  986. int onfi_timing_mode_default;
  987. };
  988. /**
  989. * struct nand_manufacturer - NAND Flash Manufacturer structure
  990. * @name: Manufacturer name
  991. * @id: manufacturer ID code of device.
  992. * @ops: manufacturer operations
  993. */
  994. struct nand_manufacturer {
  995. int id;
  996. char *name;
  997. const struct nand_manufacturer_ops *ops;
  998. };
  999. const struct nand_manufacturer *nand_get_manufacturer(u8 id);
  1000. static inline const char *
  1001. nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
  1002. {
  1003. return manufacturer ? manufacturer->name : "Unknown";
  1004. }
  1005. extern struct nand_flash_dev nand_flash_ids[];
  1006. extern const struct nand_manufacturer_ops toshiba_nand_manuf_ops;
  1007. extern const struct nand_manufacturer_ops samsung_nand_manuf_ops;
  1008. extern const struct nand_manufacturer_ops hynix_nand_manuf_ops;
  1009. int nand_default_bbt(struct mtd_info *mtd);
  1010. int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs);
  1011. int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs);
  1012. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt);
  1013. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  1014. int allowbbt);
  1015. int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
  1016. size_t *retlen, uint8_t *buf);
  1017. /**
  1018. * struct platform_nand_chip - chip level device structure
  1019. * @nr_chips: max. number of chips to scan for
  1020. * @chip_offset: chip number offset
  1021. * @nr_partitions: number of partitions pointed to by partitions (or zero)
  1022. * @partitions: mtd partition list
  1023. * @chip_delay: R/B delay value in us
  1024. * @options: Option flags, e.g. 16bit buswidth
  1025. * @bbt_options: BBT option flags, e.g. NAND_BBT_USE_FLASH
  1026. * @part_probe_types: NULL-terminated array of probe types
  1027. */
  1028. struct platform_nand_chip {
  1029. int nr_chips;
  1030. int chip_offset;
  1031. int nr_partitions;
  1032. struct mtd_partition *partitions;
  1033. int chip_delay;
  1034. unsigned int options;
  1035. unsigned int bbt_options;
  1036. const char **part_probe_types;
  1037. };
  1038. /* Keep gcc happy */
  1039. struct platform_device;
  1040. /**
  1041. * struct platform_nand_ctrl - controller level device structure
  1042. * @probe: platform specific function to probe/setup hardware
  1043. * @remove: platform specific function to remove/teardown hardware
  1044. * @hwcontrol: platform specific hardware control structure
  1045. * @dev_ready: platform specific function to read ready/busy pin
  1046. * @select_chip: platform specific chip select function
  1047. * @cmd_ctrl: platform specific function for controlling
  1048. * ALE/CLE/nCE. Also used to write command and address
  1049. * @write_buf: platform specific function for write buffer
  1050. * @read_buf: platform specific function for read buffer
  1051. * @read_byte: platform specific function to read one byte from chip
  1052. * @priv: private data to transport driver specific settings
  1053. *
  1054. * All fields are optional and depend on the hardware driver requirements
  1055. */
  1056. struct platform_nand_ctrl {
  1057. int (*probe)(struct platform_device *pdev);
  1058. void (*remove)(struct platform_device *pdev);
  1059. void (*hwcontrol)(struct mtd_info *mtd, int cmd);
  1060. int (*dev_ready)(struct mtd_info *mtd);
  1061. void (*select_chip)(struct mtd_info *mtd, int chip);
  1062. void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
  1063. void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
  1064. void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
  1065. unsigned char (*read_byte)(struct mtd_info *mtd);
  1066. void *priv;
  1067. };
  1068. /**
  1069. * struct platform_nand_data - container structure for platform-specific data
  1070. * @chip: chip level chip structure
  1071. * @ctrl: controller level device structure
  1072. */
  1073. struct platform_nand_data {
  1074. struct platform_nand_chip chip;
  1075. struct platform_nand_ctrl ctrl;
  1076. };
  1077. /* return the supported features. */
  1078. static inline int onfi_feature(struct nand_chip *chip)
  1079. {
  1080. return chip->onfi_version ? le16_to_cpu(chip->onfi_params.features) : 0;
  1081. }
  1082. /* return the supported asynchronous timing mode. */
  1083. static inline int onfi_get_async_timing_mode(struct nand_chip *chip)
  1084. {
  1085. if (!chip->onfi_version)
  1086. return ONFI_TIMING_MODE_UNKNOWN;
  1087. return le16_to_cpu(chip->onfi_params.async_timing_mode);
  1088. }
  1089. /* return the supported synchronous timing mode. */
  1090. static inline int onfi_get_sync_timing_mode(struct nand_chip *chip)
  1091. {
  1092. if (!chip->onfi_version)
  1093. return ONFI_TIMING_MODE_UNKNOWN;
  1094. return le16_to_cpu(chip->onfi_params.src_sync_timing_mode);
  1095. }
  1096. int onfi_init_data_interface(struct nand_chip *chip,
  1097. struct nand_data_interface *iface,
  1098. enum nand_data_interface_type type,
  1099. int timing_mode);
  1100. /*
  1101. * Check if it is a SLC nand.
  1102. * The !nand_is_slc() can be used to check the MLC/TLC nand chips.
  1103. * We do not distinguish the MLC and TLC now.
  1104. */
  1105. static inline bool nand_is_slc(struct nand_chip *chip)
  1106. {
  1107. return chip->bits_per_cell == 1;
  1108. }
  1109. /**
  1110. * Check if the opcode's address should be sent only on the lower 8 bits
  1111. * @command: opcode to check
  1112. */
  1113. static inline int nand_opcode_8bits(unsigned int command)
  1114. {
  1115. switch (command) {
  1116. case NAND_CMD_READID:
  1117. case NAND_CMD_PARAM:
  1118. case NAND_CMD_GET_FEATURES:
  1119. case NAND_CMD_SET_FEATURES:
  1120. return 1;
  1121. default:
  1122. break;
  1123. }
  1124. return 0;
  1125. }
  1126. /* return the supported JEDEC features. */
  1127. static inline int jedec_feature(struct nand_chip *chip)
  1128. {
  1129. return chip->jedec_version ? le16_to_cpu(chip->jedec_params.features)
  1130. : 0;
  1131. }
  1132. /* get timing characteristics from ONFI timing mode. */
  1133. const struct nand_sdr_timings *onfi_async_timing_mode_to_sdr_timings(int mode);
  1134. /* get data interface from ONFI timing mode 0, used after reset. */
  1135. const struct nand_data_interface *nand_get_default_data_interface(void);
  1136. int nand_check_erased_ecc_chunk(void *data, int datalen,
  1137. void *ecc, int ecclen,
  1138. void *extraoob, int extraooblen,
  1139. int threshold);
  1140. /* Default write_oob implementation */
  1141. int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page);
  1142. /* Default write_oob syndrome implementation */
  1143. int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1144. int page);
  1145. /* Default read_oob implementation */
  1146. int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page);
  1147. /* Default read_oob syndrome implementation */
  1148. int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1149. int page);
  1150. /* Reset and initialize a NAND device */
  1151. int nand_reset(struct nand_chip *chip, int chipnr);
  1152. /* Free resources held by the NAND device */
  1153. void nand_cleanup(struct nand_chip *chip);
  1154. /* Default extended ID decoding function */
  1155. void nand_decode_ext_id(struct nand_chip *chip);
  1156. #endif /* __LINUX_MTD_NAND_H */