rawnand.h 43 KB

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