rawnand.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
  3. * Steven J. Hill <sjhill@realitydiluted.com>
  4. * Thomas Gleixner <tglx@linutronix.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Info:
  11. * Contains standard defines and IDs for NAND flash devices
  12. *
  13. * Changelog:
  14. * See git changelog.
  15. */
  16. #ifndef __LINUX_MTD_RAWNAND_H
  17. #define __LINUX_MTD_RAWNAND_H
  18. #include <linux/wait.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/flashchip.h>
  22. #include <linux/mtd/bbm.h>
  23. #include <linux/mtd/onfi.h>
  24. #include <linux/of.h>
  25. #include <linux/types.h>
  26. struct nand_chip;
  27. /* The maximum number of NAND chips in an array */
  28. #define NAND_MAX_CHIPS 8
  29. /*
  30. * Constants for hardware specific CLE/ALE/NCE function
  31. *
  32. * These are bits which can be or'ed to set/clear multiple
  33. * bits in one go.
  34. */
  35. /* Select the chip by setting nCE to low */
  36. #define NAND_NCE 0x01
  37. /* Select the command latch by setting CLE to high */
  38. #define NAND_CLE 0x02
  39. /* Select the address latch by setting ALE to high */
  40. #define NAND_ALE 0x04
  41. #define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
  42. #define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
  43. #define NAND_CTRL_CHANGE 0x80
  44. /*
  45. * Standard NAND flash commands
  46. */
  47. #define NAND_CMD_READ0 0
  48. #define NAND_CMD_READ1 1
  49. #define NAND_CMD_RNDOUT 5
  50. #define NAND_CMD_PAGEPROG 0x10
  51. #define NAND_CMD_READOOB 0x50
  52. #define NAND_CMD_ERASE1 0x60
  53. #define NAND_CMD_STATUS 0x70
  54. #define NAND_CMD_SEQIN 0x80
  55. #define NAND_CMD_RNDIN 0x85
  56. #define NAND_CMD_READID 0x90
  57. #define NAND_CMD_ERASE2 0xd0
  58. #define NAND_CMD_PARAM 0xec
  59. #define NAND_CMD_GET_FEATURES 0xee
  60. #define NAND_CMD_SET_FEATURES 0xef
  61. #define NAND_CMD_RESET 0xff
  62. /* Extended commands for large page devices */
  63. #define NAND_CMD_READSTART 0x30
  64. #define NAND_CMD_RNDOUTSTART 0xE0
  65. #define NAND_CMD_CACHEDPROG 0x15
  66. #define NAND_CMD_NONE -1
  67. /* Status bits */
  68. #define NAND_STATUS_FAIL 0x01
  69. #define NAND_STATUS_FAIL_N1 0x02
  70. #define NAND_STATUS_TRUE_READY 0x20
  71. #define NAND_STATUS_READY 0x40
  72. #define NAND_STATUS_WP 0x80
  73. #define NAND_DATA_IFACE_CHECK_ONLY -1
  74. /*
  75. * Constants for ECC_MODES
  76. */
  77. typedef enum {
  78. NAND_ECC_NONE,
  79. NAND_ECC_SOFT,
  80. NAND_ECC_HW,
  81. NAND_ECC_HW_SYNDROME,
  82. NAND_ECC_HW_OOB_FIRST,
  83. NAND_ECC_ON_DIE,
  84. } nand_ecc_modes_t;
  85. enum nand_ecc_algo {
  86. NAND_ECC_UNKNOWN,
  87. NAND_ECC_HAMMING,
  88. NAND_ECC_BCH,
  89. NAND_ECC_RS,
  90. };
  91. /*
  92. * Constants for Hardware ECC
  93. */
  94. /* Reset Hardware ECC for read */
  95. #define NAND_ECC_READ 0
  96. /* Reset Hardware ECC for write */
  97. #define NAND_ECC_WRITE 1
  98. /* Enable Hardware ECC before syndrome is read back from flash */
  99. #define NAND_ECC_READSYN 2
  100. /*
  101. * Enable generic NAND 'page erased' check. This check is only done when
  102. * ecc.correct() returns -EBADMSG.
  103. * Set this flag if your implementation does not fix bitflips in erased
  104. * pages and you want to rely on the default implementation.
  105. */
  106. #define NAND_ECC_GENERIC_ERASED_CHECK BIT(0)
  107. #define NAND_ECC_MAXIMIZE BIT(1)
  108. /*
  109. * Option constants for bizarre disfunctionality and real
  110. * features.
  111. */
  112. /* Buswidth is 16 bit */
  113. #define NAND_BUSWIDTH_16 0x00000002
  114. /* Chip has cache program function */
  115. #define NAND_CACHEPRG 0x00000008
  116. /*
  117. * Chip requires ready check on read (for auto-incremented sequential read).
  118. * True only for small page devices; large page devices do not support
  119. * autoincrement.
  120. */
  121. #define NAND_NEED_READRDY 0x00000100
  122. /* Chip does not allow subpage writes */
  123. #define NAND_NO_SUBPAGE_WRITE 0x00000200
  124. /* Device is one of 'new' xD cards that expose fake nand command set */
  125. #define NAND_BROKEN_XD 0x00000400
  126. /* Device behaves just like nand, but is readonly */
  127. #define NAND_ROM 0x00000800
  128. /* Device supports subpage reads */
  129. #define NAND_SUBPAGE_READ 0x00001000
  130. /*
  131. * Some MLC NANDs need data scrambling to limit bitflips caused by repeated
  132. * patterns.
  133. */
  134. #define NAND_NEED_SCRAMBLING 0x00002000
  135. /* Device needs 3rd row address cycle */
  136. #define NAND_ROW_ADDR_3 0x00004000
  137. /* Options valid for Samsung large page devices */
  138. #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
  139. /* Macros to identify the above */
  140. #define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
  141. /* Non chip related options */
  142. /* This option skips the bbt scan during initialization. */
  143. #define NAND_SKIP_BBTSCAN 0x00010000
  144. /* Chip may not exist, so silence any errors in scan */
  145. #define NAND_SCAN_SILENT_NODEV 0x00040000
  146. /*
  147. * Autodetect nand buswidth with readid/onfi.
  148. * This suppose the driver will configure the hardware in 8 bits mode
  149. * when calling nand_scan_ident, and update its configuration
  150. * before calling nand_scan_tail.
  151. */
  152. #define NAND_BUSWIDTH_AUTO 0x00080000
  153. /*
  154. * This option could be defined by controller drivers to protect against
  155. * kmap'ed, vmalloc'ed highmem buffers being passed from upper layers
  156. */
  157. #define NAND_USE_BOUNCE_BUFFER 0x00100000
  158. /*
  159. * In case your controller is implementing ->legacy.cmd_ctrl() and is relying
  160. * on the default ->cmdfunc() implementation, you may want to let the core
  161. * handle the tCCS delay which is required when a column change (RNDIN or
  162. * RNDOUT) is requested.
  163. * If your controller already takes care of this delay, you don't need to set
  164. * this flag.
  165. */
  166. #define NAND_WAIT_TCCS 0x00200000
  167. /*
  168. * Whether the NAND chip is a boot medium. Drivers might use this information
  169. * to select ECC algorithms supported by the boot ROM or similar restrictions.
  170. */
  171. #define NAND_IS_BOOT_MEDIUM 0x00400000
  172. /* Options set by nand scan */
  173. /* Nand scan has allocated controller struct */
  174. #define NAND_CONTROLLER_ALLOC 0x80000000
  175. /* Cell info constants */
  176. #define NAND_CI_CHIPNR_MSK 0x03
  177. #define NAND_CI_CELLTYPE_MSK 0x0C
  178. #define NAND_CI_CELLTYPE_SHIFT 2
  179. struct jedec_ecc_info {
  180. u8 ecc_bits;
  181. u8 codeword_size;
  182. __le16 bb_per_lun;
  183. __le16 block_endurance;
  184. u8 reserved[2];
  185. } __packed;
  186. /* JEDEC features */
  187. #define JEDEC_FEATURE_16_BIT_BUS (1 << 0)
  188. struct nand_jedec_params {
  189. /* rev info and features block */
  190. /* 'J' 'E' 'S' 'D' */
  191. u8 sig[4];
  192. __le16 revision;
  193. __le16 features;
  194. u8 opt_cmd[3];
  195. __le16 sec_cmd;
  196. u8 num_of_param_pages;
  197. u8 reserved0[18];
  198. /* manufacturer information block */
  199. char manufacturer[12];
  200. char model[20];
  201. u8 jedec_id[6];
  202. u8 reserved1[10];
  203. /* memory organization block */
  204. __le32 byte_per_page;
  205. __le16 spare_bytes_per_page;
  206. u8 reserved2[6];
  207. __le32 pages_per_block;
  208. __le32 blocks_per_lun;
  209. u8 lun_count;
  210. u8 addr_cycles;
  211. u8 bits_per_cell;
  212. u8 programs_per_page;
  213. u8 multi_plane_addr;
  214. u8 multi_plane_op_attr;
  215. u8 reserved3[38];
  216. /* electrical parameter block */
  217. __le16 async_sdr_speed_grade;
  218. __le16 toggle_ddr_speed_grade;
  219. __le16 sync_ddr_speed_grade;
  220. u8 async_sdr_features;
  221. u8 toggle_ddr_features;
  222. u8 sync_ddr_features;
  223. __le16 t_prog;
  224. __le16 t_bers;
  225. __le16 t_r;
  226. __le16 t_r_multi_plane;
  227. __le16 t_ccs;
  228. __le16 io_pin_capacitance_typ;
  229. __le16 input_pin_capacitance_typ;
  230. __le16 clk_pin_capacitance_typ;
  231. u8 driver_strength_support;
  232. __le16 t_adl;
  233. u8 reserved4[36];
  234. /* ECC and endurance block */
  235. u8 guaranteed_good_blocks;
  236. __le16 guaranteed_block_endurance;
  237. struct jedec_ecc_info ecc_info[4];
  238. u8 reserved5[29];
  239. /* reserved */
  240. u8 reserved6[148];
  241. /* vendor */
  242. __le16 vendor_rev_num;
  243. u8 reserved7[88];
  244. /* CRC for Parameter Page */
  245. __le16 crc;
  246. } __packed;
  247. /**
  248. * struct nand_parameters - NAND generic parameters from the parameter page
  249. * @model: Model name
  250. * @supports_set_get_features: The NAND chip supports setting/getting features
  251. * @set_feature_list: Bitmap of features that can be set
  252. * @get_feature_list: Bitmap of features that can be get
  253. * @onfi: ONFI specific parameters
  254. */
  255. struct nand_parameters {
  256. /* Generic parameters */
  257. const char *model;
  258. bool supports_set_get_features;
  259. DECLARE_BITMAP(set_feature_list, ONFI_FEATURE_NUMBER);
  260. DECLARE_BITMAP(get_feature_list, ONFI_FEATURE_NUMBER);
  261. /* ONFI parameters */
  262. struct onfi_params *onfi;
  263. };
  264. /* The maximum expected count of bytes in the NAND ID sequence */
  265. #define NAND_MAX_ID_LEN 8
  266. /**
  267. * struct nand_id - NAND id structure
  268. * @data: buffer containing the id bytes.
  269. * @len: ID length.
  270. */
  271. struct nand_id {
  272. u8 data[NAND_MAX_ID_LEN];
  273. int len;
  274. };
  275. /**
  276. * struct nand_controller_ops - Controller operations
  277. *
  278. * @attach_chip: this method is called after the NAND detection phase after
  279. * flash ID and MTD fields such as erase size, page size and OOB
  280. * size have been set up. ECC requirements are available if
  281. * provided by the NAND chip or device tree. Typically used to
  282. * choose the appropriate ECC configuration and allocate
  283. * associated resources.
  284. * This hook is optional.
  285. * @detach_chip: free all resources allocated/claimed in
  286. * nand_controller_ops->attach_chip().
  287. * This hook is optional.
  288. */
  289. struct nand_controller_ops {
  290. int (*attach_chip)(struct nand_chip *chip);
  291. void (*detach_chip)(struct nand_chip *chip);
  292. };
  293. /**
  294. * struct nand_controller - Structure used to describe a NAND controller
  295. *
  296. * @lock: protection lock
  297. * @active: the mtd device which holds the controller currently
  298. * @wq: wait queue to sleep on if a NAND operation is in
  299. * progress used instead of the per chip wait queue
  300. * when a hw controller is available.
  301. * @ops: NAND controller operations.
  302. */
  303. struct nand_controller {
  304. spinlock_t lock;
  305. struct nand_chip *active;
  306. wait_queue_head_t wq;
  307. const struct nand_controller_ops *ops;
  308. };
  309. static inline void nand_controller_init(struct nand_controller *nfc)
  310. {
  311. nfc->active = NULL;
  312. spin_lock_init(&nfc->lock);
  313. init_waitqueue_head(&nfc->wq);
  314. }
  315. /**
  316. * struct nand_ecc_step_info - ECC step information of ECC engine
  317. * @stepsize: data bytes per ECC step
  318. * @strengths: array of supported strengths
  319. * @nstrengths: number of supported strengths
  320. */
  321. struct nand_ecc_step_info {
  322. int stepsize;
  323. const int *strengths;
  324. int nstrengths;
  325. };
  326. /**
  327. * struct nand_ecc_caps - capability of ECC engine
  328. * @stepinfos: array of ECC step information
  329. * @nstepinfos: number of ECC step information
  330. * @calc_ecc_bytes: driver's hook to calculate ECC bytes per step
  331. */
  332. struct nand_ecc_caps {
  333. const struct nand_ecc_step_info *stepinfos;
  334. int nstepinfos;
  335. int (*calc_ecc_bytes)(int step_size, int strength);
  336. };
  337. /* a shorthand to generate struct nand_ecc_caps with only one ECC stepsize */
  338. #define NAND_ECC_CAPS_SINGLE(__name, __calc, __step, ...) \
  339. static const int __name##_strengths[] = { __VA_ARGS__ }; \
  340. static const struct nand_ecc_step_info __name##_stepinfo = { \
  341. .stepsize = __step, \
  342. .strengths = __name##_strengths, \
  343. .nstrengths = ARRAY_SIZE(__name##_strengths), \
  344. }; \
  345. static const struct nand_ecc_caps __name = { \
  346. .stepinfos = &__name##_stepinfo, \
  347. .nstepinfos = 1, \
  348. .calc_ecc_bytes = __calc, \
  349. }
  350. /**
  351. * struct nand_ecc_ctrl - Control structure for ECC
  352. * @mode: ECC mode
  353. * @algo: ECC algorithm
  354. * @steps: number of ECC steps per page
  355. * @size: data bytes per ECC step
  356. * @bytes: ECC bytes per step
  357. * @strength: max number of correctible bits per ECC step
  358. * @total: total number of ECC bytes per page
  359. * @prepad: padding information for syndrome based ECC generators
  360. * @postpad: padding information for syndrome based ECC generators
  361. * @options: ECC specific options (see NAND_ECC_XXX flags defined above)
  362. * @priv: pointer to private ECC control data
  363. * @calc_buf: buffer for calculated ECC, size is oobsize.
  364. * @code_buf: buffer for ECC read from flash, size is oobsize.
  365. * @hwctl: function to control hardware ECC generator. Must only
  366. * be provided if an hardware ECC is available
  367. * @calculate: function for ECC calculation or readback from ECC hardware
  368. * @correct: function for ECC correction, matching to ECC generator (sw/hw).
  369. * Should return a positive number representing the number of
  370. * corrected bitflips, -EBADMSG if the number of bitflips exceed
  371. * ECC strength, or any other error code if the error is not
  372. * directly related to correction.
  373. * If -EBADMSG is returned the input buffers should be left
  374. * untouched.
  375. * @read_page_raw: function to read a raw page without ECC. This function
  376. * should hide the specific layout used by the ECC
  377. * controller and always return contiguous in-band and
  378. * out-of-band data even if they're not stored
  379. * contiguously on the NAND chip (e.g.
  380. * NAND_ECC_HW_SYNDROME interleaves in-band and
  381. * out-of-band data).
  382. * @write_page_raw: function to write a raw page without ECC. This function
  383. * should hide the specific layout used by the ECC
  384. * controller and consider the passed data as contiguous
  385. * in-band and out-of-band data. ECC controller is
  386. * responsible for doing the appropriate transformations
  387. * to adapt to its specific layout (e.g.
  388. * NAND_ECC_HW_SYNDROME interleaves in-band and
  389. * out-of-band data).
  390. * @read_page: function to read a page according to the ECC generator
  391. * requirements; returns maximum number of bitflips corrected in
  392. * any single ECC step, -EIO hw error
  393. * @read_subpage: function to read parts of the page covered by ECC;
  394. * returns same as read_page()
  395. * @write_subpage: function to write parts of the page covered by ECC.
  396. * @write_page: function to write a page according to the ECC generator
  397. * requirements.
  398. * @write_oob_raw: function to write chip OOB data without ECC
  399. * @read_oob_raw: function to read chip OOB data without ECC
  400. * @read_oob: function to read chip OOB data
  401. * @write_oob: function to write chip OOB data
  402. */
  403. struct nand_ecc_ctrl {
  404. nand_ecc_modes_t mode;
  405. enum nand_ecc_algo algo;
  406. int steps;
  407. int size;
  408. int bytes;
  409. int total;
  410. int strength;
  411. int prepad;
  412. int postpad;
  413. unsigned int options;
  414. void *priv;
  415. u8 *calc_buf;
  416. u8 *code_buf;
  417. void (*hwctl)(struct nand_chip *chip, int mode);
  418. int (*calculate)(struct nand_chip *chip, const uint8_t *dat,
  419. uint8_t *ecc_code);
  420. int (*correct)(struct nand_chip *chip, uint8_t *dat, uint8_t *read_ecc,
  421. uint8_t *calc_ecc);
  422. int (*read_page_raw)(struct nand_chip *chip, uint8_t *buf,
  423. int oob_required, int page);
  424. int (*write_page_raw)(struct nand_chip *chip, const uint8_t *buf,
  425. int oob_required, int page);
  426. int (*read_page)(struct nand_chip *chip, uint8_t *buf,
  427. int oob_required, int page);
  428. int (*read_subpage)(struct nand_chip *chip, uint32_t offs,
  429. uint32_t len, uint8_t *buf, int page);
  430. int (*write_subpage)(struct nand_chip *chip, uint32_t offset,
  431. uint32_t data_len, const uint8_t *data_buf,
  432. int oob_required, int page);
  433. int (*write_page)(struct nand_chip *chip, const uint8_t *buf,
  434. int oob_required, int page);
  435. int (*write_oob_raw)(struct nand_chip *chip, int page);
  436. int (*read_oob_raw)(struct nand_chip *chip, int page);
  437. int (*read_oob)(struct nand_chip *chip, int page);
  438. int (*write_oob)(struct nand_chip *chip, int page);
  439. };
  440. /**
  441. * struct nand_sdr_timings - SDR NAND chip timings
  442. *
  443. * This struct defines the timing requirements of a SDR NAND chip.
  444. * These information can be found in every NAND datasheets and the timings
  445. * meaning are described in the ONFI specifications:
  446. * www.onfi.org/~/media/ONFI/specs/onfi_3_1_spec.pdf (chapter 4.15 Timing
  447. * Parameters)
  448. *
  449. * All these timings are expressed in picoseconds.
  450. *
  451. * @tBERS_max: Block erase time
  452. * @tCCS_min: Change column setup time
  453. * @tPROG_max: Page program time
  454. * @tR_max: Page read time
  455. * @tALH_min: ALE hold time
  456. * @tADL_min: ALE to data loading time
  457. * @tALS_min: ALE setup time
  458. * @tAR_min: ALE to RE# delay
  459. * @tCEA_max: CE# access time
  460. * @tCEH_min: CE# high hold time
  461. * @tCH_min: CE# hold time
  462. * @tCHZ_max: CE# high to output hi-Z
  463. * @tCLH_min: CLE hold time
  464. * @tCLR_min: CLE to RE# delay
  465. * @tCLS_min: CLE setup time
  466. * @tCOH_min: CE# high to output hold
  467. * @tCS_min: CE# setup time
  468. * @tDH_min: Data hold time
  469. * @tDS_min: Data setup time
  470. * @tFEAT_max: Busy time for Set Features and Get Features
  471. * @tIR_min: Output hi-Z to RE# low
  472. * @tITC_max: Interface and Timing Mode Change time
  473. * @tRC_min: RE# cycle time
  474. * @tREA_max: RE# access time
  475. * @tREH_min: RE# high hold time
  476. * @tRHOH_min: RE# high to output hold
  477. * @tRHW_min: RE# high to WE# low
  478. * @tRHZ_max: RE# high to output hi-Z
  479. * @tRLOH_min: RE# low to output hold
  480. * @tRP_min: RE# pulse width
  481. * @tRR_min: Ready to RE# low (data only)
  482. * @tRST_max: Device reset time, measured from the falling edge of R/B# to the
  483. * rising edge of R/B#.
  484. * @tWB_max: WE# high to SR[6] low
  485. * @tWC_min: WE# cycle time
  486. * @tWH_min: WE# high hold time
  487. * @tWHR_min: WE# high to RE# low
  488. * @tWP_min: WE# pulse width
  489. * @tWW_min: WP# transition to WE# low
  490. */
  491. struct nand_sdr_timings {
  492. u64 tBERS_max;
  493. u32 tCCS_min;
  494. u64 tPROG_max;
  495. u64 tR_max;
  496. u32 tALH_min;
  497. u32 tADL_min;
  498. u32 tALS_min;
  499. u32 tAR_min;
  500. u32 tCEA_max;
  501. u32 tCEH_min;
  502. u32 tCH_min;
  503. u32 tCHZ_max;
  504. u32 tCLH_min;
  505. u32 tCLR_min;
  506. u32 tCLS_min;
  507. u32 tCOH_min;
  508. u32 tCS_min;
  509. u32 tDH_min;
  510. u32 tDS_min;
  511. u32 tFEAT_max;
  512. u32 tIR_min;
  513. u32 tITC_max;
  514. u32 tRC_min;
  515. u32 tREA_max;
  516. u32 tREH_min;
  517. u32 tRHOH_min;
  518. u32 tRHW_min;
  519. u32 tRHZ_max;
  520. u32 tRLOH_min;
  521. u32 tRP_min;
  522. u32 tRR_min;
  523. u64 tRST_max;
  524. u32 tWB_max;
  525. u32 tWC_min;
  526. u32 tWH_min;
  527. u32 tWHR_min;
  528. u32 tWP_min;
  529. u32 tWW_min;
  530. };
  531. /**
  532. * enum nand_data_interface_type - NAND interface timing type
  533. * @NAND_SDR_IFACE: Single Data Rate interface
  534. */
  535. enum nand_data_interface_type {
  536. NAND_SDR_IFACE,
  537. };
  538. /**
  539. * struct nand_data_interface - NAND interface timing
  540. * @type: type of the timing
  541. * @timings: The timing, type according to @type
  542. * @timings.sdr: Use it when @type is %NAND_SDR_IFACE.
  543. */
  544. struct nand_data_interface {
  545. enum nand_data_interface_type type;
  546. union {
  547. struct nand_sdr_timings sdr;
  548. } timings;
  549. };
  550. /**
  551. * nand_get_sdr_timings - get SDR timing from data interface
  552. * @conf: The data interface
  553. */
  554. static inline const struct nand_sdr_timings *
  555. nand_get_sdr_timings(const struct nand_data_interface *conf)
  556. {
  557. if (conf->type != NAND_SDR_IFACE)
  558. return ERR_PTR(-EINVAL);
  559. return &conf->timings.sdr;
  560. }
  561. /**
  562. * struct nand_op_cmd_instr - Definition of a command instruction
  563. * @opcode: the command to issue in one cycle
  564. */
  565. struct nand_op_cmd_instr {
  566. u8 opcode;
  567. };
  568. /**
  569. * struct nand_op_addr_instr - Definition of an address instruction
  570. * @naddrs: length of the @addrs array
  571. * @addrs: array containing the address cycles to issue
  572. */
  573. struct nand_op_addr_instr {
  574. unsigned int naddrs;
  575. const u8 *addrs;
  576. };
  577. /**
  578. * struct nand_op_data_instr - Definition of a data instruction
  579. * @len: number of data bytes to move
  580. * @buf: buffer to fill
  581. * @buf.in: buffer to fill when reading from the NAND chip
  582. * @buf.out: buffer to read from when writing to the NAND chip
  583. * @force_8bit: force 8-bit access
  584. *
  585. * Please note that "in" and "out" are inverted from the ONFI specification
  586. * and are from the controller perspective, so a "in" is a read from the NAND
  587. * chip while a "out" is a write to the NAND chip.
  588. */
  589. struct nand_op_data_instr {
  590. unsigned int len;
  591. union {
  592. void *in;
  593. const void *out;
  594. } buf;
  595. bool force_8bit;
  596. };
  597. /**
  598. * struct nand_op_waitrdy_instr - Definition of a wait ready instruction
  599. * @timeout_ms: maximum delay while waiting for the ready/busy pin in ms
  600. */
  601. struct nand_op_waitrdy_instr {
  602. unsigned int timeout_ms;
  603. };
  604. /**
  605. * enum nand_op_instr_type - Definition of all instruction types
  606. * @NAND_OP_CMD_INSTR: command instruction
  607. * @NAND_OP_ADDR_INSTR: address instruction
  608. * @NAND_OP_DATA_IN_INSTR: data in instruction
  609. * @NAND_OP_DATA_OUT_INSTR: data out instruction
  610. * @NAND_OP_WAITRDY_INSTR: wait ready instruction
  611. */
  612. enum nand_op_instr_type {
  613. NAND_OP_CMD_INSTR,
  614. NAND_OP_ADDR_INSTR,
  615. NAND_OP_DATA_IN_INSTR,
  616. NAND_OP_DATA_OUT_INSTR,
  617. NAND_OP_WAITRDY_INSTR,
  618. };
  619. /**
  620. * struct nand_op_instr - Instruction object
  621. * @type: the instruction type
  622. * @ctx: extra data associated to the instruction. You'll have to use the
  623. * appropriate element depending on @type
  624. * @ctx.cmd: use it if @type is %NAND_OP_CMD_INSTR
  625. * @ctx.addr: use it if @type is %NAND_OP_ADDR_INSTR
  626. * @ctx.data: use it if @type is %NAND_OP_DATA_IN_INSTR
  627. * or %NAND_OP_DATA_OUT_INSTR
  628. * @ctx.waitrdy: use it if @type is %NAND_OP_WAITRDY_INSTR
  629. * @delay_ns: delay the controller should apply after the instruction has been
  630. * issued on the bus. Most modern controllers have internal timings
  631. * control logic, and in this case, the controller driver can ignore
  632. * this field.
  633. */
  634. struct nand_op_instr {
  635. enum nand_op_instr_type type;
  636. union {
  637. struct nand_op_cmd_instr cmd;
  638. struct nand_op_addr_instr addr;
  639. struct nand_op_data_instr data;
  640. struct nand_op_waitrdy_instr waitrdy;
  641. } ctx;
  642. unsigned int delay_ns;
  643. };
  644. /*
  645. * Special handling must be done for the WAITRDY timeout parameter as it usually
  646. * is either tPROG (after a prog), tR (before a read), tRST (during a reset) or
  647. * tBERS (during an erase) which all of them are u64 values that cannot be
  648. * divided by usual kernel macros and must be handled with the special
  649. * DIV_ROUND_UP_ULL() macro.
  650. *
  651. * Cast to type of dividend is needed here to guarantee that the result won't
  652. * be an unsigned long long when the dividend is an unsigned long (or smaller),
  653. * which is what the compiler does when it sees ternary operator with 2
  654. * different return types (picks the largest type to make sure there's no
  655. * loss).
  656. */
  657. #define __DIVIDE(dividend, divisor) ({ \
  658. (__typeof__(dividend))(sizeof(dividend) <= sizeof(unsigned long) ? \
  659. DIV_ROUND_UP(dividend, divisor) : \
  660. DIV_ROUND_UP_ULL(dividend, divisor)); \
  661. })
  662. #define PSEC_TO_NSEC(x) __DIVIDE(x, 1000)
  663. #define PSEC_TO_MSEC(x) __DIVIDE(x, 1000000000)
  664. #define NAND_OP_CMD(id, ns) \
  665. { \
  666. .type = NAND_OP_CMD_INSTR, \
  667. .ctx.cmd.opcode = id, \
  668. .delay_ns = ns, \
  669. }
  670. #define NAND_OP_ADDR(ncycles, cycles, ns) \
  671. { \
  672. .type = NAND_OP_ADDR_INSTR, \
  673. .ctx.addr = { \
  674. .naddrs = ncycles, \
  675. .addrs = cycles, \
  676. }, \
  677. .delay_ns = ns, \
  678. }
  679. #define NAND_OP_DATA_IN(l, b, ns) \
  680. { \
  681. .type = NAND_OP_DATA_IN_INSTR, \
  682. .ctx.data = { \
  683. .len = l, \
  684. .buf.in = b, \
  685. .force_8bit = false, \
  686. }, \
  687. .delay_ns = ns, \
  688. }
  689. #define NAND_OP_DATA_OUT(l, b, ns) \
  690. { \
  691. .type = NAND_OP_DATA_OUT_INSTR, \
  692. .ctx.data = { \
  693. .len = l, \
  694. .buf.out = b, \
  695. .force_8bit = false, \
  696. }, \
  697. .delay_ns = ns, \
  698. }
  699. #define NAND_OP_8BIT_DATA_IN(l, b, ns) \
  700. { \
  701. .type = NAND_OP_DATA_IN_INSTR, \
  702. .ctx.data = { \
  703. .len = l, \
  704. .buf.in = b, \
  705. .force_8bit = true, \
  706. }, \
  707. .delay_ns = ns, \
  708. }
  709. #define NAND_OP_8BIT_DATA_OUT(l, b, ns) \
  710. { \
  711. .type = NAND_OP_DATA_OUT_INSTR, \
  712. .ctx.data = { \
  713. .len = l, \
  714. .buf.out = b, \
  715. .force_8bit = true, \
  716. }, \
  717. .delay_ns = ns, \
  718. }
  719. #define NAND_OP_WAIT_RDY(tout_ms, ns) \
  720. { \
  721. .type = NAND_OP_WAITRDY_INSTR, \
  722. .ctx.waitrdy.timeout_ms = tout_ms, \
  723. .delay_ns = ns, \
  724. }
  725. /**
  726. * struct nand_subop - a sub operation
  727. * @instrs: array of instructions
  728. * @ninstrs: length of the @instrs array
  729. * @first_instr_start_off: offset to start from for the first instruction
  730. * of the sub-operation
  731. * @last_instr_end_off: offset to end at (excluded) for the last instruction
  732. * of the sub-operation
  733. *
  734. * Both @first_instr_start_off and @last_instr_end_off only apply to data or
  735. * address instructions.
  736. *
  737. * When an operation cannot be handled as is by the NAND controller, it will
  738. * be split by the parser into sub-operations which will be passed to the
  739. * controller driver.
  740. */
  741. struct nand_subop {
  742. const struct nand_op_instr *instrs;
  743. unsigned int ninstrs;
  744. unsigned int first_instr_start_off;
  745. unsigned int last_instr_end_off;
  746. };
  747. unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
  748. unsigned int op_id);
  749. unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
  750. unsigned int op_id);
  751. unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
  752. unsigned int op_id);
  753. unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
  754. unsigned int op_id);
  755. /**
  756. * struct nand_op_parser_addr_constraints - Constraints for address instructions
  757. * @maxcycles: maximum number of address cycles the controller can issue in a
  758. * single step
  759. */
  760. struct nand_op_parser_addr_constraints {
  761. unsigned int maxcycles;
  762. };
  763. /**
  764. * struct nand_op_parser_data_constraints - Constraints for data instructions
  765. * @maxlen: maximum data length that the controller can handle in a single step
  766. */
  767. struct nand_op_parser_data_constraints {
  768. unsigned int maxlen;
  769. };
  770. /**
  771. * struct nand_op_parser_pattern_elem - One element of a pattern
  772. * @type: the instructuction type
  773. * @optional: whether this element of the pattern is optional or mandatory
  774. * @ctx: address or data constraint
  775. * @ctx.addr: address constraint (number of cycles)
  776. * @ctx.data: data constraint (data length)
  777. */
  778. struct nand_op_parser_pattern_elem {
  779. enum nand_op_instr_type type;
  780. bool optional;
  781. union {
  782. struct nand_op_parser_addr_constraints addr;
  783. struct nand_op_parser_data_constraints data;
  784. } ctx;
  785. };
  786. #define NAND_OP_PARSER_PAT_CMD_ELEM(_opt) \
  787. { \
  788. .type = NAND_OP_CMD_INSTR, \
  789. .optional = _opt, \
  790. }
  791. #define NAND_OP_PARSER_PAT_ADDR_ELEM(_opt, _maxcycles) \
  792. { \
  793. .type = NAND_OP_ADDR_INSTR, \
  794. .optional = _opt, \
  795. .ctx.addr.maxcycles = _maxcycles, \
  796. }
  797. #define NAND_OP_PARSER_PAT_DATA_IN_ELEM(_opt, _maxlen) \
  798. { \
  799. .type = NAND_OP_DATA_IN_INSTR, \
  800. .optional = _opt, \
  801. .ctx.data.maxlen = _maxlen, \
  802. }
  803. #define NAND_OP_PARSER_PAT_DATA_OUT_ELEM(_opt, _maxlen) \
  804. { \
  805. .type = NAND_OP_DATA_OUT_INSTR, \
  806. .optional = _opt, \
  807. .ctx.data.maxlen = _maxlen, \
  808. }
  809. #define NAND_OP_PARSER_PAT_WAITRDY_ELEM(_opt) \
  810. { \
  811. .type = NAND_OP_WAITRDY_INSTR, \
  812. .optional = _opt, \
  813. }
  814. /**
  815. * struct nand_op_parser_pattern - NAND sub-operation pattern descriptor
  816. * @elems: array of pattern elements
  817. * @nelems: number of pattern elements in @elems array
  818. * @exec: the function that will issue a sub-operation
  819. *
  820. * A pattern is a list of elements, each element reprensenting one instruction
  821. * with its constraints. The pattern itself is used by the core to match NAND
  822. * chip operation with NAND controller operations.
  823. * Once a match between a NAND controller operation pattern and a NAND chip
  824. * operation (or a sub-set of a NAND operation) is found, the pattern ->exec()
  825. * hook is called so that the controller driver can issue the operation on the
  826. * bus.
  827. *
  828. * Controller drivers should declare as many patterns as they support and pass
  829. * this list of patterns (created with the help of the following macro) to
  830. * the nand_op_parser_exec_op() helper.
  831. */
  832. struct nand_op_parser_pattern {
  833. const struct nand_op_parser_pattern_elem *elems;
  834. unsigned int nelems;
  835. int (*exec)(struct nand_chip *chip, const struct nand_subop *subop);
  836. };
  837. #define NAND_OP_PARSER_PATTERN(_exec, ...) \
  838. { \
  839. .exec = _exec, \
  840. .elems = (struct nand_op_parser_pattern_elem[]) { __VA_ARGS__ }, \
  841. .nelems = sizeof((struct nand_op_parser_pattern_elem[]) { __VA_ARGS__ }) / \
  842. sizeof(struct nand_op_parser_pattern_elem), \
  843. }
  844. /**
  845. * struct nand_op_parser - NAND controller operation parser descriptor
  846. * @patterns: array of supported patterns
  847. * @npatterns: length of the @patterns array
  848. *
  849. * The parser descriptor is just an array of supported patterns which will be
  850. * iterated by nand_op_parser_exec_op() everytime it tries to execute an
  851. * NAND operation (or tries to determine if a specific operation is supported).
  852. *
  853. * It is worth mentioning that patterns will be tested in their declaration
  854. * order, and the first match will be taken, so it's important to order patterns
  855. * appropriately so that simple/inefficient patterns are placed at the end of
  856. * the list. Usually, this is where you put single instruction patterns.
  857. */
  858. struct nand_op_parser {
  859. const struct nand_op_parser_pattern *patterns;
  860. unsigned int npatterns;
  861. };
  862. #define NAND_OP_PARSER(...) \
  863. { \
  864. .patterns = (struct nand_op_parser_pattern[]) { __VA_ARGS__ }, \
  865. .npatterns = sizeof((struct nand_op_parser_pattern[]) { __VA_ARGS__ }) / \
  866. sizeof(struct nand_op_parser_pattern), \
  867. }
  868. /**
  869. * struct nand_operation - NAND operation descriptor
  870. * @instrs: array of instructions to execute
  871. * @ninstrs: length of the @instrs array
  872. *
  873. * The actual operation structure that will be passed to chip->exec_op().
  874. */
  875. struct nand_operation {
  876. const struct nand_op_instr *instrs;
  877. unsigned int ninstrs;
  878. };
  879. #define NAND_OPERATION(_instrs) \
  880. { \
  881. .instrs = _instrs, \
  882. .ninstrs = ARRAY_SIZE(_instrs), \
  883. }
  884. int nand_op_parser_exec_op(struct nand_chip *chip,
  885. const struct nand_op_parser *parser,
  886. const struct nand_operation *op, bool check_only);
  887. /**
  888. * struct nand_legacy - NAND chip legacy fields/hooks
  889. * @IO_ADDR_R: address to read the 8 I/O lines of the flash device
  890. * @IO_ADDR_W: address to write the 8 I/O lines of the flash device
  891. * @read_byte: read one byte from the chip
  892. * @write_byte: write a single byte to the chip on the low 8 I/O lines
  893. * @write_buf: write data from the buffer to the chip
  894. * @read_buf: read data from the chip into the buffer
  895. * @cmd_ctrl: hardware specific function for controlling ALE/CLE/nCE. Also used
  896. * to write command and address
  897. * @cmdfunc: hardware specific function for writing commands to the chip.
  898. * @dev_ready: hardware specific function for accessing device ready/busy line.
  899. * If set to NULL no access to ready/busy is available and the
  900. * ready/busy information is read from the chip status register.
  901. * @waitfunc: hardware specific function for wait on ready.
  902. * @block_bad: check if a block is bad, using OOB markers
  903. * @block_markbad: mark a block bad
  904. * @erase: erase function
  905. * @set_features: set the NAND chip features
  906. * @get_features: get the NAND chip features
  907. * @chip_delay: chip dependent delay for transferring data from array to read
  908. * regs (tR).
  909. *
  910. * If you look at this structure you're already wrong. These fields/hooks are
  911. * all deprecated.
  912. */
  913. struct nand_legacy {
  914. void __iomem *IO_ADDR_R;
  915. void __iomem *IO_ADDR_W;
  916. u8 (*read_byte)(struct nand_chip *chip);
  917. void (*write_byte)(struct nand_chip *chip, u8 byte);
  918. void (*write_buf)(struct nand_chip *chip, const u8 *buf, int len);
  919. void (*read_buf)(struct nand_chip *chip, u8 *buf, int len);
  920. void (*cmd_ctrl)(struct nand_chip *chip, int dat, unsigned int ctrl);
  921. void (*cmdfunc)(struct nand_chip *chip, unsigned command, int column,
  922. int page_addr);
  923. int (*dev_ready)(struct nand_chip *chip);
  924. int (*waitfunc)(struct nand_chip *chip);
  925. int (*block_bad)(struct nand_chip *chip, loff_t ofs);
  926. int (*block_markbad)(struct nand_chip *chip, loff_t ofs);
  927. int (*erase)(struct nand_chip *chip, int page);
  928. int (*set_features)(struct nand_chip *chip, int feature_addr,
  929. u8 *subfeature_para);
  930. int (*get_features)(struct nand_chip *chip, int feature_addr,
  931. u8 *subfeature_para);
  932. int chip_delay;
  933. };
  934. /**
  935. * struct nand_chip - NAND Private Flash Chip Data
  936. * @mtd: MTD device registered to the MTD framework
  937. * @legacy: All legacy fields/hooks. If you develop a new driver,
  938. * don't even try to use any of these fields/hooks, and if
  939. * you're modifying an existing driver that is using those
  940. * fields/hooks, you should consider reworking the driver
  941. * avoid using them.
  942. * @select_chip: [REPLACEABLE] select chip nr
  943. * @exec_op: controller specific method to execute NAND operations.
  944. * This method replaces ->cmdfunc(),
  945. * ->legacy.{read,write}_{buf,byte,word}(),
  946. * ->legacy.dev_ready() and ->waifunc().
  947. * @setup_read_retry: [FLASHSPECIFIC] flash (vendor) specific function for
  948. * setting the read-retry mode. Mostly needed for MLC NAND.
  949. * @ecc: [BOARDSPECIFIC] ECC control structure
  950. * @buf_align: minimum buffer alignment required by a platform
  951. * @dummy_controller: dummy controller implementation for drivers that can
  952. * only control a single chip
  953. * @state: [INTERN] the current state of the NAND device
  954. * @oob_poi: "poison value buffer," used for laying out OOB data
  955. * before writing
  956. * @page_shift: [INTERN] number of address bits in a page (column
  957. * address bits).
  958. * @phys_erase_shift: [INTERN] number of address bits in a physical eraseblock
  959. * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
  960. * @chip_shift: [INTERN] number of address bits in one chip
  961. * @options: [BOARDSPECIFIC] various chip options. They can partly
  962. * be set to inform nand_scan about special functionality.
  963. * See the defines for further explanation.
  964. * @bbt_options: [INTERN] bad block specific options. All options used
  965. * here must come from bbm.h. By default, these options
  966. * will be copied to the appropriate nand_bbt_descr's.
  967. * @badblockpos: [INTERN] position of the bad block marker in the oob
  968. * area.
  969. * @badblockbits: [INTERN] minimum number of set bits in a good block's
  970. * bad block marker position; i.e., BBM == 11110111b is
  971. * not bad when badblockbits == 7
  972. * @bits_per_cell: [INTERN] number of bits per cell. i.e., 1 means SLC.
  973. * @ecc_strength_ds: [INTERN] ECC correctability from the datasheet.
  974. * Minimum amount of bit errors per @ecc_step_ds guaranteed
  975. * to be correctable. If unknown, set to zero.
  976. * @ecc_step_ds: [INTERN] ECC step required by the @ecc_strength_ds,
  977. * also from the datasheet. It is the recommended ECC step
  978. * size, if known; if unknown, set to zero.
  979. * @onfi_timing_mode_default: [INTERN] default ONFI timing mode. This field is
  980. * set to the actually used ONFI mode if the chip is
  981. * ONFI compliant or deduced from the datasheet if
  982. * the NAND chip is not ONFI compliant.
  983. * @numchips: [INTERN] number of physical chips
  984. * @chipsize: [INTERN] the size of one chip for multichip arrays
  985. * @pagemask: [INTERN] page number mask = number of (pages / chip) - 1
  986. * @data_buf: [INTERN] buffer for data, size is (page size + oobsize).
  987. * @pagebuf: [INTERN] holds the pagenumber which is currently in
  988. * data_buf.
  989. * @pagebuf_bitflips: [INTERN] holds the bitflip count for the page which is
  990. * currently in data_buf.
  991. * @subpagesize: [INTERN] holds the subpagesize
  992. * @id: [INTERN] holds NAND ID
  993. * @parameters: [INTERN] holds generic parameters under an easily
  994. * readable form.
  995. * @max_bb_per_die: [INTERN] the max number of bad blocks each die of a
  996. * this nand device will encounter their life times.
  997. * @blocks_per_die: [INTERN] The number of PEBs in a die
  998. * @data_interface: [INTERN] NAND interface timing information
  999. * @read_retries: [INTERN] the number of read retry modes supported
  1000. * @setup_data_interface: [OPTIONAL] setup the data interface and timing. If
  1001. * chipnr is set to %NAND_DATA_IFACE_CHECK_ONLY this
  1002. * means the configuration should not be applied but
  1003. * only checked.
  1004. * @bbt: [INTERN] bad block table pointer
  1005. * @bbt_td: [REPLACEABLE] bad block table descriptor for flash
  1006. * lookup.
  1007. * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
  1008. * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
  1009. * bad block scan.
  1010. * @controller: [REPLACEABLE] a pointer to a hardware controller
  1011. * structure which is shared among multiple independent
  1012. * devices.
  1013. * @priv: [OPTIONAL] pointer to private chip data
  1014. * @manufacturer: [INTERN] Contains manufacturer information
  1015. * @manufacturer.desc: [INTERN] Contains manufacturer's description
  1016. * @manufacturer.priv: [INTERN] Contains manufacturer private information
  1017. */
  1018. struct nand_chip {
  1019. struct mtd_info mtd;
  1020. struct nand_legacy legacy;
  1021. void (*select_chip)(struct nand_chip *chip, int cs);
  1022. int (*exec_op)(struct nand_chip *chip,
  1023. const struct nand_operation *op,
  1024. bool check_only);
  1025. int (*setup_read_retry)(struct nand_chip *chip, int retry_mode);
  1026. int (*setup_data_interface)(struct nand_chip *chip, int chipnr,
  1027. const struct nand_data_interface *conf);
  1028. unsigned int options;
  1029. unsigned int bbt_options;
  1030. int page_shift;
  1031. int phys_erase_shift;
  1032. int bbt_erase_shift;
  1033. int chip_shift;
  1034. int numchips;
  1035. uint64_t chipsize;
  1036. int pagemask;
  1037. u8 *data_buf;
  1038. int pagebuf;
  1039. unsigned int pagebuf_bitflips;
  1040. int subpagesize;
  1041. uint8_t bits_per_cell;
  1042. uint16_t ecc_strength_ds;
  1043. uint16_t ecc_step_ds;
  1044. int onfi_timing_mode_default;
  1045. int badblockpos;
  1046. int badblockbits;
  1047. struct nand_id id;
  1048. struct nand_parameters parameters;
  1049. u16 max_bb_per_die;
  1050. u32 blocks_per_die;
  1051. struct nand_data_interface data_interface;
  1052. int read_retries;
  1053. flstate_t state;
  1054. uint8_t *oob_poi;
  1055. struct nand_controller *controller;
  1056. struct nand_ecc_ctrl ecc;
  1057. unsigned long buf_align;
  1058. struct nand_controller dummy_controller;
  1059. uint8_t *bbt;
  1060. struct nand_bbt_descr *bbt_td;
  1061. struct nand_bbt_descr *bbt_md;
  1062. struct nand_bbt_descr *badblock_pattern;
  1063. void *priv;
  1064. struct {
  1065. const struct nand_manufacturer *desc;
  1066. void *priv;
  1067. } manufacturer;
  1068. };
  1069. static inline int nand_exec_op(struct nand_chip *chip,
  1070. const struct nand_operation *op)
  1071. {
  1072. if (!chip->exec_op)
  1073. return -ENOTSUPP;
  1074. return chip->exec_op(chip, op, false);
  1075. }
  1076. extern const struct mtd_ooblayout_ops nand_ooblayout_sp_ops;
  1077. extern const struct mtd_ooblayout_ops nand_ooblayout_lp_ops;
  1078. static inline void nand_set_flash_node(struct nand_chip *chip,
  1079. struct device_node *np)
  1080. {
  1081. mtd_set_of_node(&chip->mtd, np);
  1082. }
  1083. static inline struct device_node *nand_get_flash_node(struct nand_chip *chip)
  1084. {
  1085. return mtd_get_of_node(&chip->mtd);
  1086. }
  1087. static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
  1088. {
  1089. return container_of(mtd, struct nand_chip, mtd);
  1090. }
  1091. static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
  1092. {
  1093. return &chip->mtd;
  1094. }
  1095. static inline void *nand_get_controller_data(struct nand_chip *chip)
  1096. {
  1097. return chip->priv;
  1098. }
  1099. static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
  1100. {
  1101. chip->priv = priv;
  1102. }
  1103. static inline void nand_set_manufacturer_data(struct nand_chip *chip,
  1104. void *priv)
  1105. {
  1106. chip->manufacturer.priv = priv;
  1107. }
  1108. static inline void *nand_get_manufacturer_data(struct nand_chip *chip)
  1109. {
  1110. return chip->manufacturer.priv;
  1111. }
  1112. /*
  1113. * A helper for defining older NAND chips where the second ID byte fully
  1114. * defined the chip, including the geometry (chip size, eraseblock size, page
  1115. * size). All these chips have 512 bytes NAND page size.
  1116. */
  1117. #define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
  1118. { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
  1119. .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
  1120. /*
  1121. * A helper for defining newer chips which report their page size and
  1122. * eraseblock size via the extended ID bytes.
  1123. *
  1124. * The real difference between LEGACY_ID_NAND and EXTENDED_ID_NAND is that with
  1125. * EXTENDED_ID_NAND, manufacturers overloaded the same device ID so that the
  1126. * device ID now only represented a particular total chip size (and voltage,
  1127. * buswidth), and the page size, eraseblock size, and OOB size could vary while
  1128. * using the same device ID.
  1129. */
  1130. #define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
  1131. { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
  1132. .options = (opts) }
  1133. #define NAND_ECC_INFO(_strength, _step) \
  1134. { .strength_ds = (_strength), .step_ds = (_step) }
  1135. #define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
  1136. #define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
  1137. /**
  1138. * struct nand_flash_dev - NAND Flash Device ID Structure
  1139. * @name: a human-readable name of the NAND chip
  1140. * @dev_id: the device ID (the second byte of the full chip ID array)
  1141. * @mfr_id: manufecturer ID part of the full chip ID array (refers the same
  1142. * memory address as @id[0])
  1143. * @dev_id: device ID part of the full chip ID array (refers the same memory
  1144. * address as @id[1])
  1145. * @id: full device ID array
  1146. * @pagesize: size of the NAND page in bytes; if 0, then the real page size (as
  1147. * well as the eraseblock size) is determined from the extended NAND
  1148. * chip ID array)
  1149. * @chipsize: total chip size in MiB
  1150. * @erasesize: eraseblock size in bytes (determined from the extended ID if 0)
  1151. * @options: stores various chip bit options
  1152. * @id_len: The valid length of the @id.
  1153. * @oobsize: OOB size
  1154. * @ecc: ECC correctability and step information from the datasheet.
  1155. * @ecc.strength_ds: The ECC correctability from the datasheet, same as the
  1156. * @ecc_strength_ds in nand_chip{}.
  1157. * @ecc.step_ds: The ECC step required by the @ecc.strength_ds, same as the
  1158. * @ecc_step_ds in nand_chip{}, also from the datasheet.
  1159. * For example, the "4bit ECC for each 512Byte" can be set with
  1160. * NAND_ECC_INFO(4, 512).
  1161. * @onfi_timing_mode_default: the default ONFI timing mode entered after a NAND
  1162. * reset. Should be deduced from timings described
  1163. * in the datasheet.
  1164. *
  1165. */
  1166. struct nand_flash_dev {
  1167. char *name;
  1168. union {
  1169. struct {
  1170. uint8_t mfr_id;
  1171. uint8_t dev_id;
  1172. };
  1173. uint8_t id[NAND_MAX_ID_LEN];
  1174. };
  1175. unsigned int pagesize;
  1176. unsigned int chipsize;
  1177. unsigned int erasesize;
  1178. unsigned int options;
  1179. uint16_t id_len;
  1180. uint16_t oobsize;
  1181. struct {
  1182. uint16_t strength_ds;
  1183. uint16_t step_ds;
  1184. } ecc;
  1185. int onfi_timing_mode_default;
  1186. };
  1187. int nand_create_bbt(struct nand_chip *chip);
  1188. /*
  1189. * Check if it is a SLC nand.
  1190. * The !nand_is_slc() can be used to check the MLC/TLC nand chips.
  1191. * We do not distinguish the MLC and TLC now.
  1192. */
  1193. static inline bool nand_is_slc(struct nand_chip *chip)
  1194. {
  1195. WARN(chip->bits_per_cell == 0,
  1196. "chip->bits_per_cell is used uninitialized\n");
  1197. return chip->bits_per_cell == 1;
  1198. }
  1199. /**
  1200. * Check if the opcode's address should be sent only on the lower 8 bits
  1201. * @command: opcode to check
  1202. */
  1203. static inline int nand_opcode_8bits(unsigned int command)
  1204. {
  1205. switch (command) {
  1206. case NAND_CMD_READID:
  1207. case NAND_CMD_PARAM:
  1208. case NAND_CMD_GET_FEATURES:
  1209. case NAND_CMD_SET_FEATURES:
  1210. return 1;
  1211. default:
  1212. break;
  1213. }
  1214. return 0;
  1215. }
  1216. int nand_check_erased_ecc_chunk(void *data, int datalen,
  1217. void *ecc, int ecclen,
  1218. void *extraoob, int extraooblen,
  1219. int threshold);
  1220. int nand_ecc_choose_conf(struct nand_chip *chip,
  1221. const struct nand_ecc_caps *caps, int oobavail);
  1222. /* Default write_oob implementation */
  1223. int nand_write_oob_std(struct nand_chip *chip, int page);
  1224. /* Default read_oob implementation */
  1225. int nand_read_oob_std(struct nand_chip *chip, int page);
  1226. /* Stub used by drivers that do not support GET/SET FEATURES operations */
  1227. int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
  1228. u8 *subfeature_param);
  1229. /* Default read_page_raw implementation */
  1230. int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
  1231. int page);
  1232. /* Default write_page_raw implementation */
  1233. int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
  1234. int oob_required, int page);
  1235. /* Reset and initialize a NAND device */
  1236. int nand_reset(struct nand_chip *chip, int chipnr);
  1237. /* NAND operation helpers */
  1238. int nand_reset_op(struct nand_chip *chip);
  1239. int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
  1240. unsigned int len);
  1241. int nand_status_op(struct nand_chip *chip, u8 *status);
  1242. int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock);
  1243. int nand_read_page_op(struct nand_chip *chip, unsigned int page,
  1244. unsigned int offset_in_page, void *buf, unsigned int len);
  1245. int nand_change_read_column_op(struct nand_chip *chip,
  1246. unsigned int offset_in_page, void *buf,
  1247. unsigned int len, bool force_8bit);
  1248. int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
  1249. unsigned int offset_in_page, void *buf, unsigned int len);
  1250. int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
  1251. unsigned int offset_in_page, const void *buf,
  1252. unsigned int len);
  1253. int nand_prog_page_end_op(struct nand_chip *chip);
  1254. int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
  1255. unsigned int offset_in_page, const void *buf,
  1256. unsigned int len);
  1257. int nand_change_write_column_op(struct nand_chip *chip,
  1258. unsigned int offset_in_page, const void *buf,
  1259. unsigned int len, bool force_8bit);
  1260. int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
  1261. bool force_8bit);
  1262. int nand_write_data_op(struct nand_chip *chip, const void *buf,
  1263. unsigned int len, bool force_8bit);
  1264. /* Scan and identify a NAND device */
  1265. int nand_scan_with_ids(struct nand_chip *chip, unsigned int max_chips,
  1266. struct nand_flash_dev *ids);
  1267. static inline int nand_scan(struct nand_chip *chip, unsigned int max_chips)
  1268. {
  1269. return nand_scan_with_ids(chip, max_chips, NULL);
  1270. }
  1271. /* Internal helper for board drivers which need to override command function */
  1272. void nand_wait_ready(struct nand_chip *chip);
  1273. /*
  1274. * Free resources held by the NAND device, must be called on error after a
  1275. * sucessful nand_scan().
  1276. */
  1277. void nand_cleanup(struct nand_chip *chip);
  1278. /* Unregister the MTD device and calls nand_cleanup() */
  1279. void nand_release(struct nand_chip *chip);
  1280. /*
  1281. * External helper for controller drivers that have to implement the WAITRDY
  1282. * instruction and have no physical pin to check it.
  1283. */
  1284. int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms);
  1285. #endif /* __LINUX_MTD_RAWNAND_H */