nand.h 41 KB

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