blob: 8b3e28ef75ceaa327e652558efdf7d28acf5cbae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Woodhousea1452a32010-08-08 20:58:20 +01002 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
3 * Steven J. Hill <sjhill@realitydiluted.com>
4 * Thomas Gleixner <tglx@linutronix.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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 *
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +020010 * Info:
11 * Contains standard defines and IDs for NAND flash devices
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +020013 * Changelog:
14 * See git changelog.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
Boris Brezillond4092d72017-08-04 17:29:10 +020016#ifndef __LINUX_MTD_RAWNAND_H
17#define __LINUX_MTD_RAWNAND_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mtd/mtd.h>
Alessandro Rubini30631cb2009-09-20 23:28:14 +020020#include <linux/mtd/flashchip.h>
Alessandro Rubinic62d81b2009-09-20 23:28:04 +020021#include <linux/mtd/bbm.h>
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +020022#include <linux/mtd/jedec.h>
Boris Brezillon3020e302018-10-25 15:21:08 +020023#include <linux/mtd/nand.h>
Boris Brezillon1c325cc2018-09-07 00:38:50 +020024#include <linux/mtd/onfi.h>
Boris Brezillon013e6292018-11-20 11:57:20 +010025#include <linux/mutex.h>
Boris Brezillon1c3ab612018-07-05 12:27:29 +020026#include <linux/of.h>
Miquel Raynal789157e2018-03-19 14:47:28 +010027#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Boris Brezillon00ad3782018-09-06 14:05:14 +020029struct nand_chip;
Brian Norris5844fee2015-01-23 00:22:27 -080030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* The maximum number of NAND chips in an array */
32#define NAND_MAX_CHIPS 8
33
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +020034/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * Constants for hardware specific CLE/ALE/NCE function
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020036 *
37 * These are bits which can be or'ed to set/clear multiple
38 * bits in one go.
39 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* Select the chip by setting nCE to low */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020041#define NAND_NCE 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* Select the command latch by setting CLE to high */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020043#define NAND_CLE 0x02
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* Select the address latch by setting ALE to high */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020045#define NAND_ALE 0x04
46
47#define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
48#define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
49#define NAND_CTRL_CHANGE 0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * Standard NAND flash commands
53 */
54#define NAND_CMD_READ0 0
55#define NAND_CMD_READ1 1
Thomas Gleixner7bc33122006-06-20 20:05:05 +020056#define NAND_CMD_RNDOUT 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define NAND_CMD_PAGEPROG 0x10
58#define NAND_CMD_READOOB 0x50
59#define NAND_CMD_ERASE1 0x60
60#define NAND_CMD_STATUS 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define NAND_CMD_SEQIN 0x80
Thomas Gleixner7bc33122006-06-20 20:05:05 +020062#define NAND_CMD_RNDIN 0x85
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define NAND_CMD_READID 0x90
64#define NAND_CMD_ERASE2 0xd0
Florian Fainellicaa4b6f2010-08-30 18:32:14 +020065#define NAND_CMD_PARAM 0xec
Huang Shijie7db03ec2012-09-13 14:57:52 +080066#define NAND_CMD_GET_FEATURES 0xee
67#define NAND_CMD_SET_FEATURES 0xef
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define NAND_CMD_RESET 0xff
69
70/* Extended commands for large page devices */
71#define NAND_CMD_READSTART 0x30
Thomas Gleixner7bc33122006-06-20 20:05:05 +020072#define NAND_CMD_RNDOUTSTART 0xE0
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define NAND_CMD_CACHEDPROG 0x15
74
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020075#define NAND_CMD_NONE -1
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Status bits */
78#define NAND_STATUS_FAIL 0x01
79#define NAND_STATUS_FAIL_N1 0x02
80#define NAND_STATUS_TRUE_READY 0x20
81#define NAND_STATUS_READY 0x40
82#define NAND_STATUS_WP 0x80
83
Boris Brezillon104e4422017-03-16 09:35:58 +010084#define NAND_DATA_IFACE_CHECK_ONLY -1
85
Thomas Gleixner61ecfa82005-11-07 11:15:31 +000086/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Constants for ECC_MODES
88 */
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +020089typedef enum {
90 NAND_ECC_NONE,
91 NAND_ECC_SOFT,
92 NAND_ECC_HW,
93 NAND_ECC_HW_SYNDROME,
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -070094 NAND_ECC_HW_OOB_FIRST,
Thomas Petazzoni785818f2017-04-29 11:06:43 +020095 NAND_ECC_ON_DIE,
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +020096} nand_ecc_modes_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Rafał Miłeckib0fcd8a2016-03-23 11:19:00 +010098enum nand_ecc_algo {
99 NAND_ECC_UNKNOWN,
100 NAND_ECC_HAMMING,
101 NAND_ECC_BCH,
Stefan Agnerf308d732018-06-24 23:27:22 +0200102 NAND_ECC_RS,
Rafał Miłeckib0fcd8a2016-03-23 11:19:00 +0100103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * Constants for Hardware ECC
David A. Marlin068e3c02005-01-24 03:07:46 +0000107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* Reset Hardware ECC for read */
109#define NAND_ECC_READ 0
110/* Reset Hardware ECC for write */
111#define NAND_ECC_WRITE 1
Brian Norris7854d3f2011-06-23 14:12:08 -0700112/* Enable Hardware ECC before syndrome is read back from flash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define NAND_ECC_READSYN 2
114
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +0100115/*
116 * Enable generic NAND 'page erased' check. This check is only done when
117 * ecc.correct() returns -EBADMSG.
118 * Set this flag if your implementation does not fix bitflips in erased
119 * pages and you want to rely on the default implementation.
120 */
121#define NAND_ECC_GENERIC_ERASED_CHECK BIT(0)
Boris Brezillonba78ee02016-06-08 17:04:22 +0200122#define NAND_ECC_MAXIMIZE BIT(1)
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +0100123
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200124/*
Boris Brezillon309600c2018-09-04 16:23:28 +0200125 * When using software implementation of Hamming, we can specify which byte
126 * ordering should be used.
127 */
128#define NAND_ECC_SOFT_HAMMING_SM_ORDER BIT(2)
129
130/*
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200131 * Option constants for bizarre disfunctionality and real
132 * features.
133 */
Brian Norris7854d3f2011-06-23 14:12:08 -0700134/* Buswidth is 16 bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define NAND_BUSWIDTH_16 0x00000002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/* Chip has cache program function */
137#define NAND_CACHEPRG 0x00000008
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200138/*
Brian Norris5bc7c332013-03-13 09:51:31 -0700139 * Chip requires ready check on read (for auto-incremented sequential read).
140 * True only for small page devices; large page devices do not support
141 * autoincrement.
142 */
143#define NAND_NEED_READRDY 0x00000100
144
Thomas Gleixner29072b92006-09-28 15:38:36 +0200145/* Chip does not allow subpage writes */
146#define NAND_NO_SUBPAGE_WRITE 0x00000200
147
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200148/* Device is one of 'new' xD cards that expose fake nand command set */
149#define NAND_BROKEN_XD 0x00000400
150
151/* Device behaves just like nand, but is readonly */
152#define NAND_ROM 0x00000800
153
Jeff Westfahla5ff4f12012-08-13 16:35:30 -0500154/* Device supports subpage reads */
155#define NAND_SUBPAGE_READ 0x00001000
156
Boris BREZILLONc03d9962015-12-02 12:01:05 +0100157/*
158 * Some MLC NANDs need data scrambling to limit bitflips caused by repeated
159 * patterns.
160 */
161#define NAND_NEED_SCRAMBLING 0x00002000
162
Masahiro Yamada14157f82017-09-13 11:05:50 +0900163/* Device needs 3rd row address cycle */
164#define NAND_ROW_ADDR_3 0x00004000
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* Options valid for Samsung large page devices */
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +0200167#define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/* Macros to identify the above */
Jeff Westfahla5ff4f12012-08-13 16:35:30 -0500170#define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Frieder Schrempf04649ec2019-04-17 12:36:34 +0000172/*
173 * There are different places where the manufacturer stores the factory bad
174 * block markers.
175 *
176 * Position within the block: Each of these pages needs to be checked for a
177 * bad block marking pattern.
178 */
179#define NAND_BBM_SECONDPAGE 0x02000000
180#define NAND_BBM_LASTPAGE 0x04000000
181
182/* Position within the OOB data of the page */
183#define NAND_BBM_POS_SMALL 5
184#define NAND_BBM_POS_LARGE 0
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* Non chip related options */
Thomas Gleixner0040bf32005-02-09 12:20:00 +0000187/* This option skips the bbt scan during initialization. */
Brian Norrisb4dc53e2011-05-31 16:31:26 -0700188#define NAND_SKIP_BBTSCAN 0x00010000
Ben Dooksb1c6e6d2009-11-02 18:12:33 +0000189/* Chip may not exist, so silence any errors in scan */
Brian Norrisb4dc53e2011-05-31 16:31:26 -0700190#define NAND_SCAN_SILENT_NODEV 0x00040000
Matthieu CASTET64b37b22012-11-06 11:51:44 +0100191/*
192 * Autodetect nand buswidth with readid/onfi.
193 * This suppose the driver will configure the hardware in 8 bits mode
194 * when calling nand_scan_ident, and update its configuration
195 * before calling nand_scan_tail.
196 */
197#define NAND_BUSWIDTH_AUTO 0x00080000
Scott Wood5f867db2015-06-26 19:43:58 -0500198/*
199 * This option could be defined by controller drivers to protect against
200 * kmap'ed, vmalloc'ed highmem buffers being passed from upper layers
201 */
202#define NAND_USE_BOUNCE_BUFFER 0x00100000
Ben Dooksb1c6e6d2009-11-02 18:12:33 +0000203
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200204/*
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200205 * In case your controller is implementing ->legacy.cmd_ctrl() and is relying
206 * on the default ->cmdfunc() implementation, you may want to let the core
207 * handle the tCCS delay which is required when a column change (RNDIN or
208 * RNDOUT) is requested.
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200209 * If your controller already takes care of this delay, you don't need to set
210 * this flag.
211 */
212#define NAND_WAIT_TCCS 0x00200000
213
Stefan Agnerf922bd72018-06-24 23:27:23 +0200214/*
215 * Whether the NAND chip is a boot medium. Drivers might use this information
216 * to select ECC algorithms supported by the boot ROM or similar restrictions.
217 */
218#define NAND_IS_BOOT_MEDIUM 0x00400000
219
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100220/*
221 * Do not try to tweak the timings at runtime. This is needed when the
222 * controller initializes the timings on itself or when it relies on
223 * configuration done by the bootloader.
224 */
225#define NAND_KEEP_TIMINGS 0x00800000
226
Thomas Gleixner29072b92006-09-28 15:38:36 +0200227/* Cell info constants */
228#define NAND_CI_CHIPNR_MSK 0x03
229#define NAND_CI_CELLTYPE_MSK 0x0C
Huang Shijie7db906b2013-09-25 14:58:11 +0800230#define NAND_CI_CELLTYPE_SHIFT 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Miquel Raynalf4531b22018-03-19 14:47:26 +0100232/**
233 * struct nand_parameters - NAND generic parameters from the parameter page
234 * @model: Model name
235 * @supports_set_get_features: The NAND chip supports setting/getting features
Miquel Raynal789157e2018-03-19 14:47:28 +0100236 * @set_feature_list: Bitmap of features that can be set
237 * @get_feature_list: Bitmap of features that can be get
Miquel Raynala97421c2018-03-19 14:47:27 +0100238 * @onfi: ONFI specific parameters
Miquel Raynalf4531b22018-03-19 14:47:26 +0100239 */
240struct nand_parameters {
Miquel Raynala97421c2018-03-19 14:47:27 +0100241 /* Generic parameters */
Miquel Raynal2023f1fa2018-07-25 15:31:51 +0200242 const char *model;
Miquel Raynalf4531b22018-03-19 14:47:26 +0100243 bool supports_set_get_features;
Miquel Raynal789157e2018-03-19 14:47:28 +0100244 DECLARE_BITMAP(set_feature_list, ONFI_FEATURE_NUMBER);
245 DECLARE_BITMAP(get_feature_list, ONFI_FEATURE_NUMBER);
Miquel Raynala97421c2018-03-19 14:47:27 +0100246
247 /* ONFI parameters */
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +0200248 struct onfi_params *onfi;
Miquel Raynalf4531b22018-03-19 14:47:26 +0100249};
250
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +0200251/* The maximum expected count of bytes in the NAND ID sequence */
252#define NAND_MAX_ID_LEN 8
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/**
Boris Brezillon7f501f02016-05-24 19:20:05 +0200255 * struct nand_id - NAND id structure
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +0200256 * @data: buffer containing the id bytes.
Boris Brezillon7f501f02016-05-24 19:20:05 +0200257 * @len: ID length.
258 */
259struct nand_id {
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +0200260 u8 data[NAND_MAX_ID_LEN];
Boris Brezillon7f501f02016-05-24 19:20:05 +0200261 int len;
262};
263
264/**
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +0900265 * struct nand_ecc_step_info - ECC step information of ECC engine
266 * @stepsize: data bytes per ECC step
267 * @strengths: array of supported strengths
268 * @nstrengths: number of supported strengths
269 */
270struct nand_ecc_step_info {
271 int stepsize;
272 const int *strengths;
273 int nstrengths;
274};
275
276/**
277 * struct nand_ecc_caps - capability of ECC engine
278 * @stepinfos: array of ECC step information
279 * @nstepinfos: number of ECC step information
280 * @calc_ecc_bytes: driver's hook to calculate ECC bytes per step
281 */
282struct nand_ecc_caps {
283 const struct nand_ecc_step_info *stepinfos;
284 int nstepinfos;
285 int (*calc_ecc_bytes)(int step_size, int strength);
286};
287
Masahiro Yamadaa03c6012017-06-07 20:52:11 +0900288/* a shorthand to generate struct nand_ecc_caps with only one ECC stepsize */
289#define NAND_ECC_CAPS_SINGLE(__name, __calc, __step, ...) \
290static const int __name##_strengths[] = { __VA_ARGS__ }; \
291static const struct nand_ecc_step_info __name##_stepinfo = { \
292 .stepsize = __step, \
293 .strengths = __name##_strengths, \
294 .nstrengths = ARRAY_SIZE(__name##_strengths), \
295}; \
296static const struct nand_ecc_caps __name = { \
297 .stepinfos = &__name##_stepinfo, \
298 .nstepinfos = 1, \
299 .calc_ecc_bytes = __calc, \
300}
301
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +0900302/**
Brian Norris7854d3f2011-06-23 14:12:08 -0700303 * struct nand_ecc_ctrl - Control structure for ECC
304 * @mode: ECC mode
Rafał Miłeckib0fcd8a2016-03-23 11:19:00 +0100305 * @algo: ECC algorithm
Brian Norris7854d3f2011-06-23 14:12:08 -0700306 * @steps: number of ECC steps per page
307 * @size: data bytes per ECC step
308 * @bytes: ECC bytes per step
Mike Dunn1d0b95b2012-03-11 14:21:10 -0700309 * @strength: max number of correctible bits per ECC step
Brian Norris7854d3f2011-06-23 14:12:08 -0700310 * @total: total number of ECC bytes per page
311 * @prepad: padding information for syndrome based ECC generators
312 * @postpad: padding information for syndrome based ECC generators
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +0100313 * @options: ECC specific options (see NAND_ECC_XXX flags defined above)
Brian Norris7854d3f2011-06-23 14:12:08 -0700314 * @priv: pointer to private ECC control data
Masahiro Yamadac0313b92017-12-05 17:47:16 +0900315 * @calc_buf: buffer for calculated ECC, size is oobsize.
316 * @code_buf: buffer for ECC read from flash, size is oobsize.
Brian Norris7854d3f2011-06-23 14:12:08 -0700317 * @hwctl: function to control hardware ECC generator. Must only
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200318 * be provided if an hardware ECC is available
Brian Norris7854d3f2011-06-23 14:12:08 -0700319 * @calculate: function for ECC calculation or readback from ECC hardware
Boris BREZILLON6e941192015-12-30 20:32:03 +0100320 * @correct: function for ECC correction, matching to ECC generator (sw/hw).
321 * Should return a positive number representing the number of
322 * corrected bitflips, -EBADMSG if the number of bitflips exceed
323 * ECC strength, or any other error code if the error is not
324 * directly related to correction.
325 * If -EBADMSG is returned the input buffers should be left
326 * untouched.
Boris BREZILLON62d956d2014-10-20 10:46:14 +0200327 * @read_page_raw: function to read a raw page without ECC. This function
328 * should hide the specific layout used by the ECC
329 * controller and always return contiguous in-band and
330 * out-of-band data even if they're not stored
331 * contiguously on the NAND chip (e.g.
332 * NAND_ECC_HW_SYNDROME interleaves in-band and
333 * out-of-band data).
334 * @write_page_raw: function to write a raw page without ECC. This function
335 * should hide the specific layout used by the ECC
336 * controller and consider the passed data as contiguous
337 * in-band and out-of-band data. ECC controller is
338 * responsible for doing the appropriate transformations
339 * to adapt to its specific layout (e.g.
340 * NAND_ECC_HW_SYNDROME interleaves in-band and
341 * out-of-band data).
Brian Norris7854d3f2011-06-23 14:12:08 -0700342 * @read_page: function to read a page according to the ECC generator
Mike Dunn5ca7f412012-09-11 08:59:03 -0700343 * requirements; returns maximum number of bitflips corrected in
Masahiro Yamada07604682017-03-30 15:45:47 +0900344 * any single ECC step, -EIO hw error
Mike Dunn5ca7f412012-09-11 08:59:03 -0700345 * @read_subpage: function to read parts of the page covered by ECC;
346 * returns same as read_page()
Gupta, Pekon837a6ba2013-03-15 17:55:53 +0530347 * @write_subpage: function to write parts of the page covered by ECC.
Brian Norris7854d3f2011-06-23 14:12:08 -0700348 * @write_page: function to write a page according to the ECC generator
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200349 * requirements.
Brian Norris9ce244b2011-08-30 18:45:37 -0700350 * @write_oob_raw: function to write chip OOB data without ECC
Brian Norrisc46f6482011-08-30 18:45:38 -0700351 * @read_oob_raw: function to read chip OOB data without ECC
Randy Dunlap844d3b42006-06-28 21:48:27 -0700352 * @read_oob: function to read chip OOB data
353 * @write_oob: function to write chip OOB data
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200354 */
355struct nand_ecc_ctrl {
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +0200356 nand_ecc_modes_t mode;
Rafał Miłeckib0fcd8a2016-03-23 11:19:00 +0100357 enum nand_ecc_algo algo;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +0200358 int steps;
359 int size;
360 int bytes;
361 int total;
Mike Dunn1d0b95b2012-03-11 14:21:10 -0700362 int strength;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +0200363 int prepad;
364 int postpad;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +0100365 unsigned int options;
Ivan Djelic193bd402011-03-11 11:05:33 +0100366 void *priv;
Masahiro Yamadac0313b92017-12-05 17:47:16 +0900367 u8 *calc_buf;
368 u8 *code_buf;
Boris Brezillonec476362018-09-06 14:05:17 +0200369 void (*hwctl)(struct nand_chip *chip, int mode);
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200370 int (*calculate)(struct nand_chip *chip, const uint8_t *dat,
371 uint8_t *ecc_code);
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200372 int (*correct)(struct nand_chip *chip, uint8_t *dat, uint8_t *read_ecc,
373 uint8_t *calc_ecc);
Boris Brezillonb9761682018-09-06 14:05:20 +0200374 int (*read_page_raw)(struct nand_chip *chip, uint8_t *buf,
375 int oob_required, int page);
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200376 int (*write_page_raw)(struct nand_chip *chip, const uint8_t *buf,
377 int oob_required, int page);
Boris Brezillonb9761682018-09-06 14:05:20 +0200378 int (*read_page)(struct nand_chip *chip, uint8_t *buf,
379 int oob_required, int page);
380 int (*read_subpage)(struct nand_chip *chip, uint32_t offs,
381 uint32_t len, uint8_t *buf, int page);
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200382 int (*write_subpage)(struct nand_chip *chip, uint32_t offset,
383 uint32_t data_len, const uint8_t *data_buf,
384 int oob_required, int page);
385 int (*write_page)(struct nand_chip *chip, const uint8_t *buf,
386 int oob_required, int page);
387 int (*write_oob_raw)(struct nand_chip *chip, int page);
Boris Brezillonb9761682018-09-06 14:05:20 +0200388 int (*read_oob_raw)(struct nand_chip *chip, int page);
389 int (*read_oob)(struct nand_chip *chip, int page);
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200390 int (*write_oob)(struct nand_chip *chip, int page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200391};
392
393/**
Sascha Hauereee64b72016-09-15 10:32:46 +0200394 * struct nand_sdr_timings - SDR NAND chip timings
395 *
396 * This struct defines the timing requirements of a SDR NAND chip.
397 * These information can be found in every NAND datasheets and the timings
398 * meaning are described in the ONFI specifications:
399 * www.onfi.org/~/media/ONFI/specs/onfi_3_1_spec.pdf (chapter 4.15 Timing
400 * Parameters)
401 *
402 * All these timings are expressed in picoseconds.
403 *
Boris Brezillon204e7ec2016-10-01 10:24:02 +0200404 * @tBERS_max: Block erase time
405 * @tCCS_min: Change column setup time
406 * @tPROG_max: Page program time
407 * @tR_max: Page read time
Sascha Hauereee64b72016-09-15 10:32:46 +0200408 * @tALH_min: ALE hold time
409 * @tADL_min: ALE to data loading time
410 * @tALS_min: ALE setup time
411 * @tAR_min: ALE to RE# delay
412 * @tCEA_max: CE# access time
Randy Dunlap61babe92016-11-21 18:32:08 -0800413 * @tCEH_min: CE# high hold time
Sascha Hauereee64b72016-09-15 10:32:46 +0200414 * @tCH_min: CE# hold time
415 * @tCHZ_max: CE# high to output hi-Z
416 * @tCLH_min: CLE hold time
417 * @tCLR_min: CLE to RE# delay
418 * @tCLS_min: CLE setup time
419 * @tCOH_min: CE# high to output hold
420 * @tCS_min: CE# setup time
421 * @tDH_min: Data hold time
422 * @tDS_min: Data setup time
423 * @tFEAT_max: Busy time for Set Features and Get Features
424 * @tIR_min: Output hi-Z to RE# low
425 * @tITC_max: Interface and Timing Mode Change time
426 * @tRC_min: RE# cycle time
427 * @tREA_max: RE# access time
428 * @tREH_min: RE# high hold time
429 * @tRHOH_min: RE# high to output hold
430 * @tRHW_min: RE# high to WE# low
431 * @tRHZ_max: RE# high to output hi-Z
432 * @tRLOH_min: RE# low to output hold
433 * @tRP_min: RE# pulse width
434 * @tRR_min: Ready to RE# low (data only)
435 * @tRST_max: Device reset time, measured from the falling edge of R/B# to the
436 * rising edge of R/B#.
437 * @tWB_max: WE# high to SR[6] low
438 * @tWC_min: WE# cycle time
439 * @tWH_min: WE# high hold time
440 * @tWHR_min: WE# high to RE# low
441 * @tWP_min: WE# pulse width
442 * @tWW_min: WP# transition to WE# low
443 */
444struct nand_sdr_timings {
Boris Brezillon6d292312017-07-31 10:31:27 +0200445 u64 tBERS_max;
Boris Brezillon204e7ec2016-10-01 10:24:02 +0200446 u32 tCCS_min;
Boris Brezillon6d292312017-07-31 10:31:27 +0200447 u64 tPROG_max;
448 u64 tR_max;
Sascha Hauereee64b72016-09-15 10:32:46 +0200449 u32 tALH_min;
450 u32 tADL_min;
451 u32 tALS_min;
452 u32 tAR_min;
453 u32 tCEA_max;
454 u32 tCEH_min;
455 u32 tCH_min;
456 u32 tCHZ_max;
457 u32 tCLH_min;
458 u32 tCLR_min;
459 u32 tCLS_min;
460 u32 tCOH_min;
461 u32 tCS_min;
462 u32 tDH_min;
463 u32 tDS_min;
464 u32 tFEAT_max;
465 u32 tIR_min;
466 u32 tITC_max;
467 u32 tRC_min;
468 u32 tREA_max;
469 u32 tREH_min;
470 u32 tRHOH_min;
471 u32 tRHW_min;
472 u32 tRHZ_max;
473 u32 tRLOH_min;
474 u32 tRP_min;
475 u32 tRR_min;
476 u64 tRST_max;
477 u32 tWB_max;
478 u32 tWC_min;
479 u32 tWH_min;
480 u32 tWHR_min;
481 u32 tWP_min;
482 u32 tWW_min;
483};
484
485/**
486 * enum nand_data_interface_type - NAND interface timing type
487 * @NAND_SDR_IFACE: Single Data Rate interface
488 */
489enum nand_data_interface_type {
490 NAND_SDR_IFACE,
491};
492
493/**
494 * struct nand_data_interface - NAND interface timing
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -0300495 * @type: type of the timing
496 * @timings: The timing, type according to @type
497 * @timings.sdr: Use it when @type is %NAND_SDR_IFACE.
Sascha Hauereee64b72016-09-15 10:32:46 +0200498 */
499struct nand_data_interface {
500 enum nand_data_interface_type type;
501 union {
502 struct nand_sdr_timings sdr;
503 } timings;
504};
505
506/**
507 * nand_get_sdr_timings - get SDR timing from data interface
508 * @conf: The data interface
509 */
510static inline const struct nand_sdr_timings *
511nand_get_sdr_timings(const struct nand_data_interface *conf)
512{
513 if (conf->type != NAND_SDR_IFACE)
514 return ERR_PTR(-EINVAL);
515
516 return &conf->timings.sdr;
517}
518
519/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100520 * struct nand_op_cmd_instr - Definition of a command instruction
521 * @opcode: the command to issue in one cycle
522 */
523struct nand_op_cmd_instr {
524 u8 opcode;
525};
526
527/**
528 * struct nand_op_addr_instr - Definition of an address instruction
529 * @naddrs: length of the @addrs array
530 * @addrs: array containing the address cycles to issue
531 */
532struct nand_op_addr_instr {
533 unsigned int naddrs;
534 const u8 *addrs;
535};
536
537/**
538 * struct nand_op_data_instr - Definition of a data instruction
539 * @len: number of data bytes to move
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -0300540 * @buf: buffer to fill
541 * @buf.in: buffer to fill when reading from the NAND chip
542 * @buf.out: buffer to read from when writing to the NAND chip
Miquel Raynal8878b122017-11-09 14:16:45 +0100543 * @force_8bit: force 8-bit access
544 *
545 * Please note that "in" and "out" are inverted from the ONFI specification
546 * and are from the controller perspective, so a "in" is a read from the NAND
547 * chip while a "out" is a write to the NAND chip.
548 */
549struct nand_op_data_instr {
550 unsigned int len;
551 union {
552 void *in;
553 const void *out;
554 } buf;
555 bool force_8bit;
556};
557
558/**
559 * struct nand_op_waitrdy_instr - Definition of a wait ready instruction
560 * @timeout_ms: maximum delay while waiting for the ready/busy pin in ms
561 */
562struct nand_op_waitrdy_instr {
563 unsigned int timeout_ms;
564};
565
566/**
567 * enum nand_op_instr_type - Definition of all instruction types
568 * @NAND_OP_CMD_INSTR: command instruction
569 * @NAND_OP_ADDR_INSTR: address instruction
570 * @NAND_OP_DATA_IN_INSTR: data in instruction
571 * @NAND_OP_DATA_OUT_INSTR: data out instruction
572 * @NAND_OP_WAITRDY_INSTR: wait ready instruction
573 */
574enum nand_op_instr_type {
575 NAND_OP_CMD_INSTR,
576 NAND_OP_ADDR_INSTR,
577 NAND_OP_DATA_IN_INSTR,
578 NAND_OP_DATA_OUT_INSTR,
579 NAND_OP_WAITRDY_INSTR,
580};
581
582/**
583 * struct nand_op_instr - Instruction object
584 * @type: the instruction type
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -0300585 * @ctx: extra data associated to the instruction. You'll have to use the
586 * appropriate element depending on @type
587 * @ctx.cmd: use it if @type is %NAND_OP_CMD_INSTR
588 * @ctx.addr: use it if @type is %NAND_OP_ADDR_INSTR
589 * @ctx.data: use it if @type is %NAND_OP_DATA_IN_INSTR
590 * or %NAND_OP_DATA_OUT_INSTR
591 * @ctx.waitrdy: use it if @type is %NAND_OP_WAITRDY_INSTR
Miquel Raynal8878b122017-11-09 14:16:45 +0100592 * @delay_ns: delay the controller should apply after the instruction has been
593 * issued on the bus. Most modern controllers have internal timings
594 * control logic, and in this case, the controller driver can ignore
595 * this field.
596 */
597struct nand_op_instr {
598 enum nand_op_instr_type type;
599 union {
600 struct nand_op_cmd_instr cmd;
601 struct nand_op_addr_instr addr;
602 struct nand_op_data_instr data;
603 struct nand_op_waitrdy_instr waitrdy;
604 } ctx;
605 unsigned int delay_ns;
606};
607
608/*
609 * Special handling must be done for the WAITRDY timeout parameter as it usually
610 * is either tPROG (after a prog), tR (before a read), tRST (during a reset) or
611 * tBERS (during an erase) which all of them are u64 values that cannot be
612 * divided by usual kernel macros and must be handled with the special
613 * DIV_ROUND_UP_ULL() macro.
Geert Uytterhoeven9f825e72018-05-14 12:49:37 +0200614 *
615 * Cast to type of dividend is needed here to guarantee that the result won't
616 * be an unsigned long long when the dividend is an unsigned long (or smaller),
617 * which is what the compiler does when it sees ternary operator with 2
618 * different return types (picks the largest type to make sure there's no
619 * loss).
Miquel Raynal8878b122017-11-09 14:16:45 +0100620 */
Geert Uytterhoeven9f825e72018-05-14 12:49:37 +0200621#define __DIVIDE(dividend, divisor) ({ \
622 (__typeof__(dividend))(sizeof(dividend) <= sizeof(unsigned long) ? \
623 DIV_ROUND_UP(dividend, divisor) : \
624 DIV_ROUND_UP_ULL(dividend, divisor)); \
625 })
Miquel Raynal8878b122017-11-09 14:16:45 +0100626#define PSEC_TO_NSEC(x) __DIVIDE(x, 1000)
627#define PSEC_TO_MSEC(x) __DIVIDE(x, 1000000000)
628
629#define NAND_OP_CMD(id, ns) \
630 { \
631 .type = NAND_OP_CMD_INSTR, \
632 .ctx.cmd.opcode = id, \
633 .delay_ns = ns, \
634 }
635
636#define NAND_OP_ADDR(ncycles, cycles, ns) \
637 { \
638 .type = NAND_OP_ADDR_INSTR, \
639 .ctx.addr = { \
640 .naddrs = ncycles, \
641 .addrs = cycles, \
642 }, \
643 .delay_ns = ns, \
644 }
645
646#define NAND_OP_DATA_IN(l, b, ns) \
647 { \
648 .type = NAND_OP_DATA_IN_INSTR, \
649 .ctx.data = { \
650 .len = l, \
651 .buf.in = b, \
652 .force_8bit = false, \
653 }, \
654 .delay_ns = ns, \
655 }
656
657#define NAND_OP_DATA_OUT(l, b, ns) \
658 { \
659 .type = NAND_OP_DATA_OUT_INSTR, \
660 .ctx.data = { \
661 .len = l, \
662 .buf.out = b, \
663 .force_8bit = false, \
664 }, \
665 .delay_ns = ns, \
666 }
667
668#define NAND_OP_8BIT_DATA_IN(l, b, ns) \
669 { \
670 .type = NAND_OP_DATA_IN_INSTR, \
671 .ctx.data = { \
672 .len = l, \
673 .buf.in = b, \
674 .force_8bit = true, \
675 }, \
676 .delay_ns = ns, \
677 }
678
679#define NAND_OP_8BIT_DATA_OUT(l, b, ns) \
680 { \
681 .type = NAND_OP_DATA_OUT_INSTR, \
682 .ctx.data = { \
683 .len = l, \
684 .buf.out = b, \
685 .force_8bit = true, \
686 }, \
687 .delay_ns = ns, \
688 }
689
690#define NAND_OP_WAIT_RDY(tout_ms, ns) \
691 { \
692 .type = NAND_OP_WAITRDY_INSTR, \
693 .ctx.waitrdy.timeout_ms = tout_ms, \
694 .delay_ns = ns, \
695 }
696
697/**
698 * struct nand_subop - a sub operation
699 * @instrs: array of instructions
700 * @ninstrs: length of the @instrs array
701 * @first_instr_start_off: offset to start from for the first instruction
702 * of the sub-operation
703 * @last_instr_end_off: offset to end at (excluded) for the last instruction
704 * of the sub-operation
705 *
706 * Both @first_instr_start_off and @last_instr_end_off only apply to data or
707 * address instructions.
708 *
709 * When an operation cannot be handled as is by the NAND controller, it will
710 * be split by the parser into sub-operations which will be passed to the
711 * controller driver.
712 */
713struct nand_subop {
714 const struct nand_op_instr *instrs;
715 unsigned int ninstrs;
716 unsigned int first_instr_start_off;
717 unsigned int last_instr_end_off;
718};
719
Miquel Raynal760c4352018-07-19 00:09:12 +0200720unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
721 unsigned int op_id);
722unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
723 unsigned int op_id);
724unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
725 unsigned int op_id);
726unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
727 unsigned int op_id);
Miquel Raynal8878b122017-11-09 14:16:45 +0100728
729/**
730 * struct nand_op_parser_addr_constraints - Constraints for address instructions
731 * @maxcycles: maximum number of address cycles the controller can issue in a
732 * single step
733 */
734struct nand_op_parser_addr_constraints {
735 unsigned int maxcycles;
736};
737
738/**
739 * struct nand_op_parser_data_constraints - Constraints for data instructions
740 * @maxlen: maximum data length that the controller can handle in a single step
741 */
742struct nand_op_parser_data_constraints {
743 unsigned int maxlen;
744};
745
746/**
747 * struct nand_op_parser_pattern_elem - One element of a pattern
748 * @type: the instructuction type
749 * @optional: whether this element of the pattern is optional or mandatory
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -0300750 * @ctx: address or data constraint
751 * @ctx.addr: address constraint (number of cycles)
752 * @ctx.data: data constraint (data length)
Miquel Raynal8878b122017-11-09 14:16:45 +0100753 */
754struct nand_op_parser_pattern_elem {
755 enum nand_op_instr_type type;
756 bool optional;
757 union {
758 struct nand_op_parser_addr_constraints addr;
759 struct nand_op_parser_data_constraints data;
Miquel Raynalc1a72e22018-01-19 19:11:27 +0100760 } ctx;
Miquel Raynal8878b122017-11-09 14:16:45 +0100761};
762
763#define NAND_OP_PARSER_PAT_CMD_ELEM(_opt) \
764 { \
765 .type = NAND_OP_CMD_INSTR, \
766 .optional = _opt, \
767 }
768
769#define NAND_OP_PARSER_PAT_ADDR_ELEM(_opt, _maxcycles) \
770 { \
771 .type = NAND_OP_ADDR_INSTR, \
772 .optional = _opt, \
Miquel Raynalc1a72e22018-01-19 19:11:27 +0100773 .ctx.addr.maxcycles = _maxcycles, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100774 }
775
776#define NAND_OP_PARSER_PAT_DATA_IN_ELEM(_opt, _maxlen) \
777 { \
778 .type = NAND_OP_DATA_IN_INSTR, \
779 .optional = _opt, \
Miquel Raynalc1a72e22018-01-19 19:11:27 +0100780 .ctx.data.maxlen = _maxlen, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100781 }
782
783#define NAND_OP_PARSER_PAT_DATA_OUT_ELEM(_opt, _maxlen) \
784 { \
785 .type = NAND_OP_DATA_OUT_INSTR, \
786 .optional = _opt, \
Miquel Raynalc1a72e22018-01-19 19:11:27 +0100787 .ctx.data.maxlen = _maxlen, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100788 }
789
790#define NAND_OP_PARSER_PAT_WAITRDY_ELEM(_opt) \
791 { \
792 .type = NAND_OP_WAITRDY_INSTR, \
793 .optional = _opt, \
794 }
795
796/**
797 * struct nand_op_parser_pattern - NAND sub-operation pattern descriptor
798 * @elems: array of pattern elements
799 * @nelems: number of pattern elements in @elems array
800 * @exec: the function that will issue a sub-operation
801 *
802 * A pattern is a list of elements, each element reprensenting one instruction
803 * with its constraints. The pattern itself is used by the core to match NAND
804 * chip operation with NAND controller operations.
805 * Once a match between a NAND controller operation pattern and a NAND chip
806 * operation (or a sub-set of a NAND operation) is found, the pattern ->exec()
807 * hook is called so that the controller driver can issue the operation on the
808 * bus.
809 *
810 * Controller drivers should declare as many patterns as they support and pass
811 * this list of patterns (created with the help of the following macro) to
812 * the nand_op_parser_exec_op() helper.
813 */
814struct nand_op_parser_pattern {
815 const struct nand_op_parser_pattern_elem *elems;
816 unsigned int nelems;
817 int (*exec)(struct nand_chip *chip, const struct nand_subop *subop);
818};
819
820#define NAND_OP_PARSER_PATTERN(_exec, ...) \
821 { \
822 .exec = _exec, \
Masahiro Yamadaf56cad52019-04-09 13:53:32 +0900823 .elems = (const struct nand_op_parser_pattern_elem[]) { __VA_ARGS__ }, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100824 .nelems = sizeof((struct nand_op_parser_pattern_elem[]) { __VA_ARGS__ }) / \
825 sizeof(struct nand_op_parser_pattern_elem), \
826 }
827
828/**
829 * struct nand_op_parser - NAND controller operation parser descriptor
830 * @patterns: array of supported patterns
831 * @npatterns: length of the @patterns array
832 *
833 * The parser descriptor is just an array of supported patterns which will be
834 * iterated by nand_op_parser_exec_op() everytime it tries to execute an
835 * NAND operation (or tries to determine if a specific operation is supported).
836 *
837 * It is worth mentioning that patterns will be tested in their declaration
838 * order, and the first match will be taken, so it's important to order patterns
839 * appropriately so that simple/inefficient patterns are placed at the end of
840 * the list. Usually, this is where you put single instruction patterns.
841 */
842struct nand_op_parser {
843 const struct nand_op_parser_pattern *patterns;
844 unsigned int npatterns;
845};
846
847#define NAND_OP_PARSER(...) \
848 { \
Masahiro Yamadaf56cad52019-04-09 13:53:32 +0900849 .patterns = (const struct nand_op_parser_pattern[]) { __VA_ARGS__ }, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100850 .npatterns = sizeof((struct nand_op_parser_pattern[]) { __VA_ARGS__ }) / \
851 sizeof(struct nand_op_parser_pattern), \
852 }
853
854/**
855 * struct nand_operation - NAND operation descriptor
Boris Brezillonae2294b2018-11-11 08:55:15 +0100856 * @cs: the CS line to select for this NAND operation
Miquel Raynal8878b122017-11-09 14:16:45 +0100857 * @instrs: array of instructions to execute
858 * @ninstrs: length of the @instrs array
859 *
860 * The actual operation structure that will be passed to chip->exec_op().
861 */
862struct nand_operation {
Boris Brezillonae2294b2018-11-11 08:55:15 +0100863 unsigned int cs;
Miquel Raynal8878b122017-11-09 14:16:45 +0100864 const struct nand_op_instr *instrs;
865 unsigned int ninstrs;
866};
867
Boris Brezillonae2294b2018-11-11 08:55:15 +0100868#define NAND_OPERATION(_cs, _instrs) \
Miquel Raynal8878b122017-11-09 14:16:45 +0100869 { \
Boris Brezillonae2294b2018-11-11 08:55:15 +0100870 .cs = _cs, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100871 .instrs = _instrs, \
872 .ninstrs = ARRAY_SIZE(_instrs), \
873 }
874
875int nand_op_parser_exec_op(struct nand_chip *chip,
876 const struct nand_op_parser *parser,
877 const struct nand_operation *op, bool check_only);
Boris Brezillon3020e302018-10-25 15:21:08 +0200878
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100879/**
880 * struct nand_controller_ops - Controller operations
881 *
882 * @attach_chip: this method is called after the NAND detection phase after
883 * flash ID and MTD fields such as erase size, page size and OOB
884 * size have been set up. ECC requirements are available if
885 * provided by the NAND chip or device tree. Typically used to
886 * choose the appropriate ECC configuration and allocate
887 * associated resources.
888 * This hook is optional.
889 * @detach_chip: free all resources allocated/claimed in
890 * nand_controller_ops->attach_chip().
891 * This hook is optional.
892 * @exec_op: controller specific method to execute NAND operations.
893 * This method replaces chip->legacy.cmdfunc(),
894 * chip->legacy.{read,write}_{buf,byte,word}(),
895 * chip->legacy.dev_ready() and chip->legacy.waifunc().
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100896 * @setup_data_interface: setup the data interface and timing. If
897 * chipnr is set to %NAND_DATA_IFACE_CHECK_ONLY this
898 * means the configuration should not be applied but
899 * only checked.
900 * This hook is optional.
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100901 */
902struct nand_controller_ops {
903 int (*attach_chip)(struct nand_chip *chip);
904 void (*detach_chip)(struct nand_chip *chip);
905 int (*exec_op)(struct nand_chip *chip,
906 const struct nand_operation *op,
907 bool check_only);
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100908 int (*setup_data_interface)(struct nand_chip *chip, int chipnr,
909 const struct nand_data_interface *conf);
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100910};
911
912/**
913 * struct nand_controller - Structure used to describe a NAND controller
914 *
Boris Brezillon013e6292018-11-20 11:57:20 +0100915 * @lock: lock used to serialize accesses to the NAND controller
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100916 * @ops: NAND controller operations.
917 */
918struct nand_controller {
Boris Brezillon013e6292018-11-20 11:57:20 +0100919 struct mutex lock;
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100920 const struct nand_controller_ops *ops;
921};
922
923static inline void nand_controller_init(struct nand_controller *nfc)
924{
Boris Brezillon013e6292018-11-20 11:57:20 +0100925 mutex_init(&nfc->lock);
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100926}
Miquel Raynal8878b122017-11-09 14:16:45 +0100927
928/**
Boris Brezillon82fc5092018-09-07 00:38:34 +0200929 * struct nand_legacy - NAND chip legacy fields/hooks
930 * @IO_ADDR_R: address to read the 8 I/O lines of the flash device
931 * @IO_ADDR_W: address to write the 8 I/O lines of the flash device
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100932 * @select_chip: select/deselect a specific target/die
Boris Brezillon716bbba2018-09-07 00:38:35 +0200933 * @read_byte: read one byte from the chip
934 * @write_byte: write a single byte to the chip on the low 8 I/O lines
935 * @write_buf: write data from the buffer to the chip
936 * @read_buf: read data from the chip into the buffer
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200937 * @cmd_ctrl: hardware specific function for controlling ALE/CLE/nCE. Also used
938 * to write command and address
939 * @cmdfunc: hardware specific function for writing commands to the chip.
Boris Brezillon8395b752018-09-07 00:38:37 +0200940 * @dev_ready: hardware specific function for accessing device ready/busy line.
941 * If set to NULL no access to ready/busy is available and the
942 * ready/busy information is read from the chip status register.
943 * @waitfunc: hardware specific function for wait on ready.
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200944 * @block_bad: check if a block is bad, using OOB markers
945 * @block_markbad: mark a block bad
Boris Brezillon45240362018-09-07 00:38:40 +0200946 * @set_features: set the NAND chip features
947 * @get_features: get the NAND chip features
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200948 * @chip_delay: chip dependent delay for transferring data from array to read
949 * regs (tR).
Boris Brezillon7b6a9b22018-11-20 10:02:39 +0100950 * @dummy_controller: dummy controller implementation for drivers that can
951 * only control a single chip
Boris Brezillon82fc5092018-09-07 00:38:34 +0200952 *
953 * If you look at this structure you're already wrong. These fields/hooks are
954 * all deprecated.
955 */
956struct nand_legacy {
957 void __iomem *IO_ADDR_R;
958 void __iomem *IO_ADDR_W;
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100959 void (*select_chip)(struct nand_chip *chip, int cs);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200960 u8 (*read_byte)(struct nand_chip *chip);
961 void (*write_byte)(struct nand_chip *chip, u8 byte);
962 void (*write_buf)(struct nand_chip *chip, const u8 *buf, int len);
963 void (*read_buf)(struct nand_chip *chip, u8 *buf, int len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200964 void (*cmd_ctrl)(struct nand_chip *chip, int dat, unsigned int ctrl);
965 void (*cmdfunc)(struct nand_chip *chip, unsigned command, int column,
966 int page_addr);
Boris Brezillon8395b752018-09-07 00:38:37 +0200967 int (*dev_ready)(struct nand_chip *chip);
968 int (*waitfunc)(struct nand_chip *chip);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200969 int (*block_bad)(struct nand_chip *chip, loff_t ofs);
970 int (*block_markbad)(struct nand_chip *chip, loff_t ofs);
Boris Brezillon45240362018-09-07 00:38:40 +0200971 int (*set_features)(struct nand_chip *chip, int feature_addr,
972 u8 *subfeature_para);
973 int (*get_features)(struct nand_chip *chip, int feature_addr,
974 u8 *subfeature_para);
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200975 int chip_delay;
Boris Brezillon7b6a9b22018-11-20 10:02:39 +0100976 struct nand_controller dummy_controller;
Boris Brezillon82fc5092018-09-07 00:38:34 +0200977};
978
979/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * struct nand_chip - NAND Private Flash Chip Data
Boris Brezillon3020e302018-10-25 15:21:08 +0200981 * @base: Inherit from the generic NAND device
Boris Brezillon82fc5092018-09-07 00:38:34 +0200982 * @legacy: All legacy fields/hooks. If you develop a new driver,
983 * don't even try to use any of these fields/hooks, and if
984 * you're modifying an existing driver that is using those
985 * fields/hooks, you should consider reworking the driver
986 * avoid using them.
Brian Norrisba84fb52014-01-03 15:13:33 -0800987 * @setup_read_retry: [FLASHSPECIFIC] flash (vendor) specific function for
988 * setting the read-retry mode. Mostly needed for MLC NAND.
Brian Norris7854d3f2011-06-23 14:12:08 -0700989 * @ecc: [BOARDSPECIFIC] ECC control structure
Masahiro Yamada477544c2017-03-30 17:15:05 +0900990 * @buf_align: minimum buffer alignment required by a platform
Brian Norrise9195ed2011-08-30 18:45:43 -0700991 * @oob_poi: "poison value buffer," used for laying out OOB data
992 * before writing
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200993 * @page_shift: [INTERN] number of address bits in a page (column
994 * address bits).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 * @phys_erase_shift: [INTERN] number of address bits in a physical eraseblock
996 * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
997 * @chip_shift: [INTERN] number of address bits in one chip
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200998 * @options: [BOARDSPECIFIC] various chip options. They can partly
999 * be set to inform nand_scan about special functionality.
1000 * See the defines for further explanation.
Brian Norris5fb15492011-05-31 16:31:21 -07001001 * @bbt_options: [INTERN] bad block specific options. All options used
1002 * here must come from bbm.h. By default, these options
1003 * will be copied to the appropriate nand_bbt_descr's.
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001004 * @badblockpos: [INTERN] position of the bad block marker in the oob
1005 * area.
Brian Norris661a0832012-01-13 18:11:50 -08001006 * @badblockbits: [INTERN] minimum number of set bits in a good block's
1007 * bad block marker position; i.e., BBM == 11110111b is
1008 * not bad when badblockbits == 7
Boris BREZILLON57a94e22014-09-22 20:11:50 +02001009 * @onfi_timing_mode_default: [INTERN] default ONFI timing mode. This field is
Boris Brezillond8e725d2016-09-15 10:32:50 +02001010 * set to the actually used ONFI mode if the chip is
1011 * ONFI compliant or deduced from the datasheet if
1012 * the NAND chip is not ONFI compliant.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * @pagemask: [INTERN] page number mask = number of (pages / chip) - 1
Masahiro Yamadac0313b92017-12-05 17:47:16 +09001014 * @data_buf: [INTERN] buffer for data, size is (page size + oobsize).
Boris Brezillond9745412018-10-28 16:12:45 +01001015 * @pagecache: Structure containing page cache related fields
1016 * @pagecache.bitflips: Number of bitflips of the cached page
1017 * @pagecache.page: Page number currently in the cache. -1 means no page is
1018 * currently cached
Thomas Gleixner29072b92006-09-28 15:38:36 +02001019 * @subpagesize: [INTERN] holds the subpagesize
Boris Brezillon7f501f02016-05-24 19:20:05 +02001020 * @id: [INTERN] holds NAND ID
Miquel Raynalf4531b22018-03-19 14:47:26 +01001021 * @parameters: [INTERN] holds generic parameters under an easily
1022 * readable form.
Randy Dunlap61babe92016-11-21 18:32:08 -08001023 * @data_interface: [INTERN] NAND interface timing information
Boris Brezillonae2294b2018-11-11 08:55:15 +01001024 * @cur_cs: currently selected target. -1 means no target selected,
1025 * otherwise we should always have cur_cs >= 0 &&
Boris Brezillon32813e22018-10-29 11:58:29 +01001026 * cur_cs < nanddev_ntargets(). NAND Controller drivers
1027 * should not modify this value, but they're allowed to
1028 * read it.
Brian Norrisba84fb52014-01-03 15:13:33 -08001029 * @read_retries: [INTERN] the number of read retry modes supported
Boris Brezillon013e6292018-11-20 11:57:20 +01001030 * @lock: lock protecting the suspended field. Also used to
1031 * serialize accesses to the NAND device.
1032 * @suspended: set to 1 when the device is suspended, 0 when it's not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * @bbt: [INTERN] bad block table pointer
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001034 * @bbt_td: [REPLACEABLE] bad block table descriptor for flash
1035 * lookup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001037 * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
1038 * bad block scan.
1039 * @controller: [REPLACEABLE] a pointer to a hardware controller
Brian Norris7854d3f2011-06-23 14:12:08 -07001040 * structure which is shared among multiple independent
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001041 * devices.
Brian Norris32c8db82011-08-23 17:17:35 -07001042 * @priv: [OPTIONAL] pointer to private chip data
Boris Brezillonabbe26d2016-06-08 09:32:55 +02001043 * @manufacturer: [INTERN] Contains manufacturer information
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -03001044 * @manufacturer.desc: [INTERN] Contains manufacturer's description
1045 * @manufacturer.priv: [INTERN] Contains manufacturer private information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Thomas Gleixner61ecfa82005-11-07 11:15:31 +00001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048struct nand_chip {
Boris Brezillon3020e302018-10-25 15:21:08 +02001049 struct nand_device base;
Boris Brezillon82fc5092018-09-07 00:38:34 +02001050
1051 struct nand_legacy legacy;
Thomas Gleixner61ecfa82005-11-07 11:15:31 +00001052
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02001053 int (*setup_read_retry)(struct nand_chip *chip, int retry_mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001054
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001055 unsigned int options;
Brian Norris5fb15492011-05-31 16:31:21 -07001056 unsigned int bbt_options;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001057
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001058 int page_shift;
1059 int phys_erase_shift;
1060 int bbt_erase_shift;
1061 int chip_shift;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001062 int pagemask;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09001063 u8 *data_buf;
Boris Brezillond9745412018-10-28 16:12:45 +01001064
1065 struct {
1066 unsigned int bitflips;
1067 int page;
1068 } pagecache;
1069
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001070 int subpagesize;
Boris BREZILLON57a94e22014-09-22 20:11:50 +02001071 int onfi_timing_mode_default;
Frieder Schrempf04649ec2019-04-17 12:36:34 +00001072 unsigned int badblockpos;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001073 int badblockbits;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001074
Boris Brezillon7f501f02016-05-24 19:20:05 +02001075 struct nand_id id;
Miquel Raynalf4531b22018-03-19 14:47:26 +01001076 struct nand_parameters parameters;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02001077
Miquel Raynal17fa8042017-11-30 18:01:31 +01001078 struct nand_data_interface data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001079
Boris Brezillonae2294b2018-11-11 08:55:15 +01001080 int cur_cs;
1081
Brian Norrisba84fb52014-01-03 15:13:33 -08001082 int read_retries;
1083
Boris Brezillon013e6292018-11-20 11:57:20 +01001084 struct mutex lock;
1085 unsigned int suspended : 1;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001086
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001087 uint8_t *oob_poi;
Miquel Raynal7da45132018-07-17 09:08:02 +02001088 struct nand_controller *controller;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001089
1090 struct nand_ecc_ctrl ecc;
Masahiro Yamada477544c2017-03-30 17:15:05 +09001091 unsigned long buf_align;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001092
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001093 uint8_t *bbt;
1094 struct nand_bbt_descr *bbt_td;
1095 struct nand_bbt_descr *bbt_md;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001096
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001097 struct nand_bbt_descr *badblock_pattern;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001098
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001099 void *priv;
Boris Brezillonabbe26d2016-06-08 09:32:55 +02001100
1101 struct {
1102 const struct nand_manufacturer *desc;
1103 void *priv;
1104 } manufacturer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105};
1106
Boris Brezillon41b207a2016-02-03 19:06:15 +01001107extern const struct mtd_ooblayout_ops nand_ooblayout_sp_ops;
1108extern const struct mtd_ooblayout_ops nand_ooblayout_lp_ops;
1109
Boris BREZILLON9eba47d2015-11-16 14:37:35 +01001110static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
1111{
Boris Brezillon3020e302018-10-25 15:21:08 +02001112 return container_of(mtd, struct nand_chip, base.mtd);
Boris BREZILLON9eba47d2015-11-16 14:37:35 +01001113}
1114
Boris BREZILLONffd014f2015-12-01 12:03:07 +01001115static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
1116{
Boris Brezillon3020e302018-10-25 15:21:08 +02001117 return &chip->base.mtd;
Boris BREZILLONffd014f2015-12-01 12:03:07 +01001118}
1119
Boris BREZILLONd39ddbd2015-12-10 09:00:39 +01001120static inline void *nand_get_controller_data(struct nand_chip *chip)
1121{
1122 return chip->priv;
1123}
1124
1125static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
1126{
1127 chip->priv = priv;
1128}
1129
Boris Brezillonabbe26d2016-06-08 09:32:55 +02001130static inline void nand_set_manufacturer_data(struct nand_chip *chip,
1131 void *priv)
1132{
1133 chip->manufacturer.priv = priv;
1134}
1135
1136static inline void *nand_get_manufacturer_data(struct nand_chip *chip)
1137{
1138 return chip->manufacturer.priv;
1139}
1140
Boris Brezillon080d66e2018-10-25 15:05:39 +02001141static inline void nand_set_flash_node(struct nand_chip *chip,
1142 struct device_node *np)
1143{
1144 mtd_set_of_node(nand_to_mtd(chip), np);
1145}
1146
1147static inline struct device_node *nand_get_flash_node(struct nand_chip *chip)
1148{
1149 return mtd_get_of_node(nand_to_mtd(chip));
1150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/*
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +02001153 * A helper for defining older NAND chips where the second ID byte fully
1154 * defined the chip, including the geometry (chip size, eraseblock size, page
Artem Bityutskiy5bfa9b72013-03-19 10:29:26 +02001155 * size). All these chips have 512 bytes NAND page size.
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +02001156 */
Artem Bityutskiy5bfa9b72013-03-19 10:29:26 +02001157#define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
1158 { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
1159 .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +02001160
1161/*
1162 * A helper for defining newer chips which report their page size and
1163 * eraseblock size via the extended ID bytes.
1164 *
1165 * The real difference between LEGACY_ID_NAND and EXTENDED_ID_NAND is that with
1166 * EXTENDED_ID_NAND, manufacturers overloaded the same device ID so that the
1167 * device ID now only represented a particular total chip size (and voltage,
1168 * buswidth), and the page size, eraseblock size, and OOB size could vary while
1169 * using the same device ID.
1170 */
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001171#define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
1172 { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +02001173 .options = (opts) }
1174
Huang Shijie2dc0bdd2013-05-17 11:17:31 +08001175#define NAND_ECC_INFO(_strength, _step) \
1176 { .strength_ds = (_strength), .step_ds = (_step) }
1177#define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
1178#define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180/**
1181 * struct nand_flash_dev - NAND Flash Device ID Structure
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001182 * @name: a human-readable name of the NAND chip
1183 * @dev_id: the device ID (the second byte of the full chip ID array)
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001184 * @mfr_id: manufecturer ID part of the full chip ID array (refers the same
Jonathan Neuschäfer7e8afca2019-03-22 00:52:41 +01001185 * memory address as ``id[0]``)
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001186 * @dev_id: device ID part of the full chip ID array (refers the same memory
Jonathan Neuschäfer7e8afca2019-03-22 00:52:41 +01001187 * address as ``id[1]``)
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001188 * @id: full device ID array
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001189 * @pagesize: size of the NAND page in bytes; if 0, then the real page size (as
1190 * well as the eraseblock size) is determined from the extended NAND
1191 * chip ID array)
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001192 * @chipsize: total chip size in MiB
Artem Bityutskiyecb42fe2013-03-13 13:45:00 +02001193 * @erasesize: eraseblock size in bytes (determined from the extended ID if 0)
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001194 * @options: stores various chip bit options
Huang Shijief22d5f62013-03-15 11:00:59 +08001195 * @id_len: The valid length of the @id.
1196 * @oobsize: OOB size
Randy Dunlap7b7d8982014-07-27 14:31:53 -07001197 * @ecc: ECC correctability and step information from the datasheet.
Huang Shijie2dc0bdd2013-05-17 11:17:31 +08001198 * @ecc.strength_ds: The ECC correctability from the datasheet, same as the
1199 * @ecc_strength_ds in nand_chip{}.
1200 * @ecc.step_ds: The ECC step required by the @ecc.strength_ds, same as the
1201 * @ecc_step_ds in nand_chip{}, also from the datasheet.
1202 * For example, the "4bit ECC for each 512Byte" can be set with
1203 * NAND_ECC_INFO(4, 512).
Boris BREZILLON57a94e22014-09-22 20:11:50 +02001204 * @onfi_timing_mode_default: the default ONFI timing mode entered after a NAND
1205 * reset. Should be deduced from timings described
1206 * in the datasheet.
1207 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
1209struct nand_flash_dev {
1210 char *name;
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001211 union {
1212 struct {
1213 uint8_t mfr_id;
1214 uint8_t dev_id;
1215 };
Artem Bityutskiy53552d22013-03-14 09:57:23 +02001216 uint8_t id[NAND_MAX_ID_LEN];
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001217 };
Artem Bityutskiyecb42fe2013-03-13 13:45:00 +02001218 unsigned int pagesize;
1219 unsigned int chipsize;
1220 unsigned int erasesize;
1221 unsigned int options;
Huang Shijief22d5f62013-03-15 11:00:59 +08001222 uint16_t id_len;
1223 uint16_t oobsize;
Huang Shijie2dc0bdd2013-05-17 11:17:31 +08001224 struct {
1225 uint16_t strength_ds;
1226 uint16_t step_ds;
1227 } ecc;
Boris BREZILLON57a94e22014-09-22 20:11:50 +02001228 int onfi_timing_mode_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229};
1230
Boris Brezillon44b07b92018-07-05 12:27:30 +02001231int nand_create_bbt(struct nand_chip *chip);
Sascha Hauerb88730a2016-09-15 10:32:48 +02001232
Huang Shijie1d0ed692013-09-25 14:58:10 +08001233/*
1234 * Check if it is a SLC nand.
1235 * The !nand_is_slc() can be used to check the MLC/TLC nand chips.
1236 * We do not distinguish the MLC and TLC now.
1237 */
1238static inline bool nand_is_slc(struct nand_chip *chip)
1239{
Boris Brezillon29815162018-10-25 17:16:47 +02001240 WARN(nanddev_bits_per_cell(&chip->base) == 0,
Lothar Waßmann2d2a2b82017-08-29 12:17:13 +02001241 "chip->bits_per_cell is used uninitialized\n");
Boris Brezillon29815162018-10-25 17:16:47 +02001242 return nanddev_bits_per_cell(&chip->base) == 1;
Huang Shijie1d0ed692013-09-25 14:58:10 +08001243}
Brian Norris3dad2342014-01-29 14:08:12 -08001244
1245/**
1246 * Check if the opcode's address should be sent only on the lower 8 bits
1247 * @command: opcode to check
1248 */
1249static inline int nand_opcode_8bits(unsigned int command)
1250{
David Mosbergere34fcb02014-03-21 16:05:10 -06001251 switch (command) {
1252 case NAND_CMD_READID:
1253 case NAND_CMD_PARAM:
1254 case NAND_CMD_GET_FEATURES:
1255 case NAND_CMD_SET_FEATURES:
1256 return 1;
1257 default:
1258 break;
1259 }
1260 return 0;
Brian Norris3dad2342014-01-29 14:08:12 -08001261}
1262
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001263int nand_check_erased_ecc_chunk(void *data, int datalen,
1264 void *ecc, int ecclen,
1265 void *extraoob, int extraooblen,
1266 int threshold);
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001267
Abhishek Sahu181ace92018-06-20 12:57:28 +05301268int nand_ecc_choose_conf(struct nand_chip *chip,
1269 const struct nand_ecc_caps *caps, int oobavail);
1270
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001271/* Default write_oob implementation */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001272int nand_write_oob_std(struct nand_chip *chip, int page);
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001273
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001274/* Default read_oob implementation */
Boris Brezillonb9761682018-09-06 14:05:20 +02001275int nand_read_oob_std(struct nand_chip *chip, int page);
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001276
Boris Brezillon4a78cc62017-05-26 17:10:15 +02001277/* Stub used by drivers that do not support GET/SET FEATURES operations */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02001278int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
1279 u8 *subfeature_param);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02001280
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001281/* Default read_page_raw implementation */
Boris Brezillonb9761682018-09-06 14:05:20 +02001282int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
1283 int page);
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001284
1285/* Default write_page_raw implementation */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001286int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
1287 int oob_required, int page);
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001288
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001289/* Reset and initialize a NAND device */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001290int nand_reset(struct nand_chip *chip, int chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001291
Boris Brezillon97d90da2017-11-30 18:01:29 +01001292/* NAND operation helpers */
1293int nand_reset_op(struct nand_chip *chip);
1294int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1295 unsigned int len);
1296int nand_status_op(struct nand_chip *chip, u8 *status);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001297int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock);
1298int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1299 unsigned int offset_in_page, void *buf, unsigned int len);
1300int nand_change_read_column_op(struct nand_chip *chip,
1301 unsigned int offset_in_page, void *buf,
1302 unsigned int len, bool force_8bit);
1303int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1304 unsigned int offset_in_page, void *buf, unsigned int len);
1305int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1306 unsigned int offset_in_page, const void *buf,
1307 unsigned int len);
1308int nand_prog_page_end_op(struct nand_chip *chip);
1309int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1310 unsigned int offset_in_page, const void *buf,
1311 unsigned int len);
1312int nand_change_write_column_op(struct nand_chip *chip,
1313 unsigned int offset_in_page, const void *buf,
1314 unsigned int len, bool force_8bit);
1315int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1316 bool force_8bit);
1317int nand_write_data_op(struct nand_chip *chip, const void *buf,
1318 unsigned int len, bool force_8bit);
1319
Boris Brezillon0b4e61c2018-09-07 00:38:42 +02001320/* Scan and identify a NAND device */
1321int nand_scan_with_ids(struct nand_chip *chip, unsigned int max_chips,
1322 struct nand_flash_dev *ids);
1323
1324static inline int nand_scan(struct nand_chip *chip, unsigned int max_chips)
1325{
1326 return nand_scan_with_ids(chip, max_chips, NULL);
1327}
1328
1329/* Internal helper for board drivers which need to override command function */
1330void nand_wait_ready(struct nand_chip *chip);
1331
Miquel Raynal98732da2018-07-25 15:31:50 +02001332/*
1333 * Free resources held by the NAND device, must be called on error after a
1334 * sucessful nand_scan().
1335 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02001336void nand_cleanup(struct nand_chip *chip);
Miquel Raynal98732da2018-07-25 15:31:50 +02001337/* Unregister the MTD device and calls nand_cleanup() */
Boris Brezillon59ac2762018-09-06 14:05:15 +02001338void nand_release(struct nand_chip *chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02001339
Miquel Raynal8878b122017-11-09 14:16:45 +01001340/*
1341 * External helper for controller drivers that have to implement the WAITRDY
1342 * instruction and have no physical pin to check it.
1343 */
1344int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms);
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +02001345struct gpio_desc;
1346int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
1347 unsigned long timeout_ms);
1348
Boris Brezillon1d017852018-11-11 08:55:14 +01001349/* Select/deselect a NAND target. */
1350void nand_select_target(struct nand_chip *chip, unsigned int cs);
1351void nand_deselect_target(struct nand_chip *chip);
1352
Boris Brezilloneeab7172018-10-28 15:27:55 +01001353/**
1354 * nand_get_data_buf() - Get the internal page buffer
1355 * @chip: NAND chip object
1356 *
1357 * Returns the pre-allocated page buffer after invalidating the cache. This
1358 * function should be used by drivers that do not want to allocate their own
1359 * bounce buffer and still need such a buffer for specific operations (most
1360 * commonly when reading OOB data only).
1361 *
1362 * Be careful to never call this function in the write/write_oob path, because
1363 * the core may have placed the data to be written out in this buffer.
1364 *
1365 * Return: pointer to the page cache buffer
1366 */
1367static inline void *nand_get_data_buf(struct nand_chip *chip)
1368{
Boris Brezillond9745412018-10-28 16:12:45 +01001369 chip->pagecache.page = -1;
Boris Brezilloneeab7172018-10-28 15:27:55 +01001370
1371 return chip->data_buf;
1372}
1373
Boris Brezillond4092d72017-08-04 17:29:10 +02001374#endif /* __LINUX_MTD_RAWNAND_H */