rawnand.h 51 KB

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