blob: f4fc0cce7c559414ac467211e25c4d6c1ce0bd45 [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/wait.h>
20#include <linux/spinlock.h>
21#include <linux/mtd/mtd.h>
Alessandro Rubini30631cb2009-09-20 23:28:14 +020022#include <linux/mtd/flashchip.h>
Alessandro Rubinic62d81b2009-09-20 23:28:04 +020023#include <linux/mtd/bbm.h>
Boris Brezillon1c325cc2018-09-07 00:38:50 +020024#include <linux/mtd/onfi.h>
Boris Brezillon1c3ab612018-07-05 12:27:29 +020025#include <linux/of.h>
Miquel Raynal789157e2018-03-19 14:47:28 +010026#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Boris Brezillon00ad3782018-09-06 14:05:14 +020028struct nand_chip;
Brian Norris5844fee2015-01-23 00:22:27 -080029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/* The maximum number of NAND chips in an array */
31#define NAND_MAX_CHIPS 8
32
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +020033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * Constants for hardware specific CLE/ALE/NCE function
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020035 *
36 * These are bits which can be or'ed to set/clear multiple
37 * bits in one go.
38 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* Select the chip by setting nCE to low */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020040#define NAND_NCE 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* Select the command latch by setting CLE to high */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020042#define NAND_CLE 0x02
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Select the address latch by setting ALE to high */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020044#define NAND_ALE 0x04
45
46#define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
47#define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
48#define NAND_CTRL_CHANGE 0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * Standard NAND flash commands
52 */
53#define NAND_CMD_READ0 0
54#define NAND_CMD_READ1 1
Thomas Gleixner7bc33122006-06-20 20:05:05 +020055#define NAND_CMD_RNDOUT 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define NAND_CMD_PAGEPROG 0x10
57#define NAND_CMD_READOOB 0x50
58#define NAND_CMD_ERASE1 0x60
59#define NAND_CMD_STATUS 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define NAND_CMD_SEQIN 0x80
Thomas Gleixner7bc33122006-06-20 20:05:05 +020061#define NAND_CMD_RNDIN 0x85
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NAND_CMD_READID 0x90
63#define NAND_CMD_ERASE2 0xd0
Florian Fainellicaa4b6f2010-08-30 18:32:14 +020064#define NAND_CMD_PARAM 0xec
Huang Shijie7db03ec2012-09-13 14:57:52 +080065#define NAND_CMD_GET_FEATURES 0xee
66#define NAND_CMD_SET_FEATURES 0xef
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define NAND_CMD_RESET 0xff
68
69/* Extended commands for large page devices */
70#define NAND_CMD_READSTART 0x30
Thomas Gleixner7bc33122006-06-20 20:05:05 +020071#define NAND_CMD_RNDOUTSTART 0xE0
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NAND_CMD_CACHEDPROG 0x15
73
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020074#define NAND_CMD_NONE -1
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* Status bits */
77#define NAND_STATUS_FAIL 0x01
78#define NAND_STATUS_FAIL_N1 0x02
79#define NAND_STATUS_TRUE_READY 0x20
80#define NAND_STATUS_READY 0x40
81#define NAND_STATUS_WP 0x80
82
Boris Brezillon104e4422017-03-16 09:35:58 +010083#define NAND_DATA_IFACE_CHECK_ONLY -1
84
Thomas Gleixner61ecfa82005-11-07 11:15:31 +000085/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * Constants for ECC_MODES
87 */
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +020088typedef enum {
89 NAND_ECC_NONE,
90 NAND_ECC_SOFT,
91 NAND_ECC_HW,
92 NAND_ECC_HW_SYNDROME,
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -070093 NAND_ECC_HW_OOB_FIRST,
Thomas Petazzoni785818f2017-04-29 11:06:43 +020094 NAND_ECC_ON_DIE,
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +020095} nand_ecc_modes_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Rafał Miłeckib0fcd8a2016-03-23 11:19:00 +010097enum nand_ecc_algo {
98 NAND_ECC_UNKNOWN,
99 NAND_ECC_HAMMING,
100 NAND_ECC_BCH,
Stefan Agnerf308d732018-06-24 23:27:22 +0200101 NAND_ECC_RS,
Rafał Miłeckib0fcd8a2016-03-23 11:19:00 +0100102};
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Constants for Hardware ECC
David A. Marlin068e3c02005-01-24 03:07:46 +0000106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* Reset Hardware ECC for read */
108#define NAND_ECC_READ 0
109/* Reset Hardware ECC for write */
110#define NAND_ECC_WRITE 1
Brian Norris7854d3f2011-06-23 14:12:08 -0700111/* Enable Hardware ECC before syndrome is read back from flash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define NAND_ECC_READSYN 2
113
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +0100114/*
115 * Enable generic NAND 'page erased' check. This check is only done when
116 * ecc.correct() returns -EBADMSG.
117 * Set this flag if your implementation does not fix bitflips in erased
118 * pages and you want to rely on the default implementation.
119 */
120#define NAND_ECC_GENERIC_ERASED_CHECK BIT(0)
Boris Brezillonba78ee02016-06-08 17:04:22 +0200121#define NAND_ECC_MAXIMIZE BIT(1)
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +0100122
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200123/*
124 * Option constants for bizarre disfunctionality and real
125 * features.
126 */
Brian Norris7854d3f2011-06-23 14:12:08 -0700127/* Buswidth is 16 bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define NAND_BUSWIDTH_16 0x00000002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* Chip has cache program function */
130#define NAND_CACHEPRG 0x00000008
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200131/*
Brian Norris5bc7c332013-03-13 09:51:31 -0700132 * Chip requires ready check on read (for auto-incremented sequential read).
133 * True only for small page devices; large page devices do not support
134 * autoincrement.
135 */
136#define NAND_NEED_READRDY 0x00000100
137
Thomas Gleixner29072b92006-09-28 15:38:36 +0200138/* Chip does not allow subpage writes */
139#define NAND_NO_SUBPAGE_WRITE 0x00000200
140
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200141/* Device is one of 'new' xD cards that expose fake nand command set */
142#define NAND_BROKEN_XD 0x00000400
143
144/* Device behaves just like nand, but is readonly */
145#define NAND_ROM 0x00000800
146
Jeff Westfahla5ff4f12012-08-13 16:35:30 -0500147/* Device supports subpage reads */
148#define NAND_SUBPAGE_READ 0x00001000
149
Boris BREZILLONc03d9962015-12-02 12:01:05 +0100150/*
151 * Some MLC NANDs need data scrambling to limit bitflips caused by repeated
152 * patterns.
153 */
154#define NAND_NEED_SCRAMBLING 0x00002000
155
Masahiro Yamada14157f82017-09-13 11:05:50 +0900156/* Device needs 3rd row address cycle */
157#define NAND_ROW_ADDR_3 0x00004000
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* Options valid for Samsung large page devices */
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +0200160#define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/* Macros to identify the above */
Jeff Westfahla5ff4f12012-08-13 16:35:30 -0500163#define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* Non chip related options */
Thomas Gleixner0040bf32005-02-09 12:20:00 +0000166/* This option skips the bbt scan during initialization. */
Brian Norrisb4dc53e2011-05-31 16:31:26 -0700167#define NAND_SKIP_BBTSCAN 0x00010000
Ben Dooksb1c6e6d2009-11-02 18:12:33 +0000168/* Chip may not exist, so silence any errors in scan */
Brian Norrisb4dc53e2011-05-31 16:31:26 -0700169#define NAND_SCAN_SILENT_NODEV 0x00040000
Matthieu CASTET64b37b22012-11-06 11:51:44 +0100170/*
171 * Autodetect nand buswidth with readid/onfi.
172 * This suppose the driver will configure the hardware in 8 bits mode
173 * when calling nand_scan_ident, and update its configuration
174 * before calling nand_scan_tail.
175 */
176#define NAND_BUSWIDTH_AUTO 0x00080000
Scott Wood5f867db2015-06-26 19:43:58 -0500177/*
178 * This option could be defined by controller drivers to protect against
179 * kmap'ed, vmalloc'ed highmem buffers being passed from upper layers
180 */
181#define NAND_USE_BOUNCE_BUFFER 0x00100000
Ben Dooksb1c6e6d2009-11-02 18:12:33 +0000182
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200183/*
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200184 * In case your controller is implementing ->legacy.cmd_ctrl() and is relying
185 * on the default ->cmdfunc() implementation, you may want to let the core
186 * handle the tCCS delay which is required when a column change (RNDIN or
187 * RNDOUT) is requested.
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200188 * If your controller already takes care of this delay, you don't need to set
189 * this flag.
190 */
191#define NAND_WAIT_TCCS 0x00200000
192
Stefan Agnerf922bd72018-06-24 23:27:23 +0200193/*
194 * Whether the NAND chip is a boot medium. Drivers might use this information
195 * to select ECC algorithms supported by the boot ROM or similar restrictions.
196 */
197#define NAND_IS_BOOT_MEDIUM 0x00400000
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* Options set by nand scan */
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200200/* Nand scan has allocated controller struct */
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200201#define NAND_CONTROLLER_ALLOC 0x80000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Thomas Gleixner29072b92006-09-28 15:38:36 +0200203/* Cell info constants */
204#define NAND_CI_CHIPNR_MSK 0x03
205#define NAND_CI_CELLTYPE_MSK 0x0C
Huang Shijie7db906b2013-09-25 14:58:11 +0800206#define NAND_CI_CELLTYPE_SHIFT 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Huang Shijieafbfff02014-02-21 13:39:37 +0800208struct jedec_ecc_info {
209 u8 ecc_bits;
210 u8 codeword_size;
211 __le16 bb_per_lun;
212 __le16 block_endurance;
213 u8 reserved[2];
214} __packed;
215
Huang Shijie7852f892014-02-21 13:39:39 +0800216/* JEDEC features */
217#define JEDEC_FEATURE_16_BIT_BUS (1 << 0)
218
Huang Shijieafbfff02014-02-21 13:39:37 +0800219struct nand_jedec_params {
220 /* rev info and features block */
221 /* 'J' 'E' 'S' 'D' */
222 u8 sig[4];
223 __le16 revision;
224 __le16 features;
225 u8 opt_cmd[3];
226 __le16 sec_cmd;
227 u8 num_of_param_pages;
228 u8 reserved0[18];
229
230 /* manufacturer information block */
231 char manufacturer[12];
232 char model[20];
233 u8 jedec_id[6];
234 u8 reserved1[10];
235
236 /* memory organization block */
237 __le32 byte_per_page;
238 __le16 spare_bytes_per_page;
239 u8 reserved2[6];
240 __le32 pages_per_block;
241 __le32 blocks_per_lun;
242 u8 lun_count;
243 u8 addr_cycles;
244 u8 bits_per_cell;
245 u8 programs_per_page;
246 u8 multi_plane_addr;
247 u8 multi_plane_op_attr;
248 u8 reserved3[38];
249
250 /* electrical parameter block */
251 __le16 async_sdr_speed_grade;
252 __le16 toggle_ddr_speed_grade;
253 __le16 sync_ddr_speed_grade;
254 u8 async_sdr_features;
255 u8 toggle_ddr_features;
256 u8 sync_ddr_features;
257 __le16 t_prog;
258 __le16 t_bers;
259 __le16 t_r;
260 __le16 t_r_multi_plane;
261 __le16 t_ccs;
262 __le16 io_pin_capacitance_typ;
263 __le16 input_pin_capacitance_typ;
264 __le16 clk_pin_capacitance_typ;
265 u8 driver_strength_support;
Brian Norris74e98be2015-12-01 11:08:32 -0800266 __le16 t_adl;
Huang Shijieafbfff02014-02-21 13:39:37 +0800267 u8 reserved4[36];
268
269 /* ECC and endurance block */
270 u8 guaranteed_good_blocks;
271 __le16 guaranteed_block_endurance;
272 struct jedec_ecc_info ecc_info[4];
273 u8 reserved5[29];
274
275 /* reserved */
276 u8 reserved6[148];
277
278 /* vendor */
279 __le16 vendor_rev_num;
280 u8 reserved7[88];
281
282 /* CRC for Parameter Page */
283 __le16 crc;
284} __packed;
285
Miquel Raynalf4531b22018-03-19 14:47:26 +0100286/**
287 * struct nand_parameters - NAND generic parameters from the parameter page
288 * @model: Model name
289 * @supports_set_get_features: The NAND chip supports setting/getting features
Miquel Raynal789157e2018-03-19 14:47:28 +0100290 * @set_feature_list: Bitmap of features that can be set
291 * @get_feature_list: Bitmap of features that can be get
Miquel Raynala97421c2018-03-19 14:47:27 +0100292 * @onfi: ONFI specific parameters
Miquel Raynalf4531b22018-03-19 14:47:26 +0100293 */
294struct nand_parameters {
Miquel Raynala97421c2018-03-19 14:47:27 +0100295 /* Generic parameters */
Miquel Raynal2023f1fa2018-07-25 15:31:51 +0200296 const char *model;
Miquel Raynalf4531b22018-03-19 14:47:26 +0100297 bool supports_set_get_features;
Miquel Raynal789157e2018-03-19 14:47:28 +0100298 DECLARE_BITMAP(set_feature_list, ONFI_FEATURE_NUMBER);
299 DECLARE_BITMAP(get_feature_list, ONFI_FEATURE_NUMBER);
Miquel Raynala97421c2018-03-19 14:47:27 +0100300
301 /* ONFI parameters */
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +0200302 struct onfi_params *onfi;
Miquel Raynalf4531b22018-03-19 14:47:26 +0100303};
304
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +0200305/* The maximum expected count of bytes in the NAND ID sequence */
306#define NAND_MAX_ID_LEN 8
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/**
Boris Brezillon7f501f02016-05-24 19:20:05 +0200309 * struct nand_id - NAND id structure
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +0200310 * @data: buffer containing the id bytes.
Boris Brezillon7f501f02016-05-24 19:20:05 +0200311 * @len: ID length.
312 */
313struct nand_id {
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +0200314 u8 data[NAND_MAX_ID_LEN];
Boris Brezillon7f501f02016-05-24 19:20:05 +0200315 int len;
316};
317
318/**
Miquel Raynal05b54c72018-07-19 01:05:46 +0200319 * struct nand_controller_ops - Controller operations
320 *
321 * @attach_chip: this method is called after the NAND detection phase after
322 * flash ID and MTD fields such as erase size, page size and OOB
323 * size have been set up. ECC requirements are available if
324 * provided by the NAND chip or device tree. Typically used to
325 * choose the appropriate ECC configuration and allocate
326 * associated resources.
327 * This hook is optional.
328 * @detach_chip: free all resources allocated/claimed in
329 * nand_controller_ops->attach_chip().
330 * This hook is optional.
331 */
332struct nand_controller_ops {
333 int (*attach_chip)(struct nand_chip *chip);
334 void (*detach_chip)(struct nand_chip *chip);
335};
336
337/**
Miquel Raynal7da45132018-07-17 09:08:02 +0200338 * struct nand_controller - Structure used to describe a NAND controller
339 *
Thomas Gleixner61ecfa82005-11-07 11:15:31 +0000340 * @lock: protection lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * @active: the mtd device which holds the controller currently
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200342 * @wq: wait queue to sleep on if a NAND operation is in
343 * progress used instead of the per chip wait queue
344 * when a hw controller is available.
Miquel Raynal05b54c72018-07-19 01:05:46 +0200345 * @ops: NAND controller operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Miquel Raynal7da45132018-07-17 09:08:02 +0200347struct nand_controller {
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +0200348 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct nand_chip *active;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100350 wait_queue_head_t wq;
Miquel Raynal05b54c72018-07-19 01:05:46 +0200351 const struct nand_controller_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352};
353
Miquel Raynal7da45132018-07-17 09:08:02 +0200354static inline void nand_controller_init(struct nand_controller *nfc)
Marc Gonzalezd45bc582016-07-27 11:23:52 +0200355{
356 nfc->active = NULL;
357 spin_lock_init(&nfc->lock);
358 init_waitqueue_head(&nfc->wq);
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +0900362 * struct nand_ecc_step_info - ECC step information of ECC engine
363 * @stepsize: data bytes per ECC step
364 * @strengths: array of supported strengths
365 * @nstrengths: number of supported strengths
366 */
367struct nand_ecc_step_info {
368 int stepsize;
369 const int *strengths;
370 int nstrengths;
371};
372
373/**
374 * struct nand_ecc_caps - capability of ECC engine
375 * @stepinfos: array of ECC step information
376 * @nstepinfos: number of ECC step information
377 * @calc_ecc_bytes: driver's hook to calculate ECC bytes per step
378 */
379struct nand_ecc_caps {
380 const struct nand_ecc_step_info *stepinfos;
381 int nstepinfos;
382 int (*calc_ecc_bytes)(int step_size, int strength);
383};
384
Masahiro Yamadaa03c6012017-06-07 20:52:11 +0900385/* a shorthand to generate struct nand_ecc_caps with only one ECC stepsize */
386#define NAND_ECC_CAPS_SINGLE(__name, __calc, __step, ...) \
387static const int __name##_strengths[] = { __VA_ARGS__ }; \
388static const struct nand_ecc_step_info __name##_stepinfo = { \
389 .stepsize = __step, \
390 .strengths = __name##_strengths, \
391 .nstrengths = ARRAY_SIZE(__name##_strengths), \
392}; \
393static const struct nand_ecc_caps __name = { \
394 .stepinfos = &__name##_stepinfo, \
395 .nstepinfos = 1, \
396 .calc_ecc_bytes = __calc, \
397}
398
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +0900399/**
Brian Norris7854d3f2011-06-23 14:12:08 -0700400 * struct nand_ecc_ctrl - Control structure for ECC
401 * @mode: ECC mode
Rafał Miłeckib0fcd8a2016-03-23 11:19:00 +0100402 * @algo: ECC algorithm
Brian Norris7854d3f2011-06-23 14:12:08 -0700403 * @steps: number of ECC steps per page
404 * @size: data bytes per ECC step
405 * @bytes: ECC bytes per step
Mike Dunn1d0b95b02012-03-11 14:21:10 -0700406 * @strength: max number of correctible bits per ECC step
Brian Norris7854d3f2011-06-23 14:12:08 -0700407 * @total: total number of ECC bytes per page
408 * @prepad: padding information for syndrome based ECC generators
409 * @postpad: padding information for syndrome based ECC generators
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +0100410 * @options: ECC specific options (see NAND_ECC_XXX flags defined above)
Brian Norris7854d3f2011-06-23 14:12:08 -0700411 * @priv: pointer to private ECC control data
Masahiro Yamadac0313b92017-12-05 17:47:16 +0900412 * @calc_buf: buffer for calculated ECC, size is oobsize.
413 * @code_buf: buffer for ECC read from flash, size is oobsize.
Brian Norris7854d3f2011-06-23 14:12:08 -0700414 * @hwctl: function to control hardware ECC generator. Must only
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200415 * be provided if an hardware ECC is available
Brian Norris7854d3f2011-06-23 14:12:08 -0700416 * @calculate: function for ECC calculation or readback from ECC hardware
Boris BREZILLON6e941192015-12-30 20:32:03 +0100417 * @correct: function for ECC correction, matching to ECC generator (sw/hw).
418 * Should return a positive number representing the number of
419 * corrected bitflips, -EBADMSG if the number of bitflips exceed
420 * ECC strength, or any other error code if the error is not
421 * directly related to correction.
422 * If -EBADMSG is returned the input buffers should be left
423 * untouched.
Boris BREZILLON62d956d2014-10-20 10:46:14 +0200424 * @read_page_raw: function to read a raw page without ECC. This function
425 * should hide the specific layout used by the ECC
426 * controller and always return contiguous in-band and
427 * out-of-band data even if they're not stored
428 * contiguously on the NAND chip (e.g.
429 * NAND_ECC_HW_SYNDROME interleaves in-band and
430 * out-of-band data).
431 * @write_page_raw: function to write a raw page without ECC. This function
432 * should hide the specific layout used by the ECC
433 * controller and consider the passed data as contiguous
434 * in-band and out-of-band data. ECC controller is
435 * responsible for doing the appropriate transformations
436 * to adapt to its specific layout (e.g.
437 * NAND_ECC_HW_SYNDROME interleaves in-band and
438 * out-of-band data).
Brian Norris7854d3f2011-06-23 14:12:08 -0700439 * @read_page: function to read a page according to the ECC generator
Mike Dunn5ca7f412012-09-11 08:59:03 -0700440 * requirements; returns maximum number of bitflips corrected in
Masahiro Yamada07604682017-03-30 15:45:47 +0900441 * any single ECC step, -EIO hw error
Mike Dunn5ca7f412012-09-11 08:59:03 -0700442 * @read_subpage: function to read parts of the page covered by ECC;
443 * returns same as read_page()
Gupta, Pekon837a6ba2013-03-15 17:55:53 +0530444 * @write_subpage: function to write parts of the page covered by ECC.
Brian Norris7854d3f2011-06-23 14:12:08 -0700445 * @write_page: function to write a page according to the ECC generator
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +0200446 * requirements.
Brian Norris9ce244b2011-08-30 18:45:37 -0700447 * @write_oob_raw: function to write chip OOB data without ECC
Brian Norrisc46f6482011-08-30 18:45:38 -0700448 * @read_oob_raw: function to read chip OOB data without ECC
Randy Dunlap844d3b42006-06-28 21:48:27 -0700449 * @read_oob: function to read chip OOB data
450 * @write_oob: function to write chip OOB data
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200451 */
452struct nand_ecc_ctrl {
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +0200453 nand_ecc_modes_t mode;
Rafał Miłeckib0fcd8a2016-03-23 11:19:00 +0100454 enum nand_ecc_algo algo;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +0200455 int steps;
456 int size;
457 int bytes;
458 int total;
Mike Dunn1d0b95b02012-03-11 14:21:10 -0700459 int strength;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +0200460 int prepad;
461 int postpad;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +0100462 unsigned int options;
Ivan Djelic193bd402011-03-11 11:05:33 +0100463 void *priv;
Masahiro Yamadac0313b92017-12-05 17:47:16 +0900464 u8 *calc_buf;
465 u8 *code_buf;
Boris Brezillonec476362018-09-06 14:05:17 +0200466 void (*hwctl)(struct nand_chip *chip, int mode);
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200467 int (*calculate)(struct nand_chip *chip, const uint8_t *dat,
468 uint8_t *ecc_code);
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200469 int (*correct)(struct nand_chip *chip, uint8_t *dat, uint8_t *read_ecc,
470 uint8_t *calc_ecc);
Boris Brezillonb9761682018-09-06 14:05:20 +0200471 int (*read_page_raw)(struct nand_chip *chip, uint8_t *buf,
472 int oob_required, int page);
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200473 int (*write_page_raw)(struct nand_chip *chip, const uint8_t *buf,
474 int oob_required, int page);
Boris Brezillonb9761682018-09-06 14:05:20 +0200475 int (*read_page)(struct nand_chip *chip, uint8_t *buf,
476 int oob_required, int page);
477 int (*read_subpage)(struct nand_chip *chip, uint32_t offs,
478 uint32_t len, uint8_t *buf, int page);
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200479 int (*write_subpage)(struct nand_chip *chip, uint32_t offset,
480 uint32_t data_len, const uint8_t *data_buf,
481 int oob_required, int page);
482 int (*write_page)(struct nand_chip *chip, const uint8_t *buf,
483 int oob_required, int page);
484 int (*write_oob_raw)(struct nand_chip *chip, int page);
Boris Brezillonb9761682018-09-06 14:05:20 +0200485 int (*read_oob_raw)(struct nand_chip *chip, int page);
486 int (*read_oob)(struct nand_chip *chip, int page);
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200487 int (*write_oob)(struct nand_chip *chip, int page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +0200488};
489
490/**
Sascha Hauereee64b72016-09-15 10:32:46 +0200491 * struct nand_sdr_timings - SDR NAND chip timings
492 *
493 * This struct defines the timing requirements of a SDR NAND chip.
494 * These information can be found in every NAND datasheets and the timings
495 * meaning are described in the ONFI specifications:
496 * www.onfi.org/~/media/ONFI/specs/onfi_3_1_spec.pdf (chapter 4.15 Timing
497 * Parameters)
498 *
499 * All these timings are expressed in picoseconds.
500 *
Boris Brezillon204e7ec2016-10-01 10:24:02 +0200501 * @tBERS_max: Block erase time
502 * @tCCS_min: Change column setup time
503 * @tPROG_max: Page program time
504 * @tR_max: Page read time
Sascha Hauereee64b72016-09-15 10:32:46 +0200505 * @tALH_min: ALE hold time
506 * @tADL_min: ALE to data loading time
507 * @tALS_min: ALE setup time
508 * @tAR_min: ALE to RE# delay
509 * @tCEA_max: CE# access time
Randy Dunlap61babe92016-11-21 18:32:08 -0800510 * @tCEH_min: CE# high hold time
Sascha Hauereee64b72016-09-15 10:32:46 +0200511 * @tCH_min: CE# hold time
512 * @tCHZ_max: CE# high to output hi-Z
513 * @tCLH_min: CLE hold time
514 * @tCLR_min: CLE to RE# delay
515 * @tCLS_min: CLE setup time
516 * @tCOH_min: CE# high to output hold
517 * @tCS_min: CE# setup time
518 * @tDH_min: Data hold time
519 * @tDS_min: Data setup time
520 * @tFEAT_max: Busy time for Set Features and Get Features
521 * @tIR_min: Output hi-Z to RE# low
522 * @tITC_max: Interface and Timing Mode Change time
523 * @tRC_min: RE# cycle time
524 * @tREA_max: RE# access time
525 * @tREH_min: RE# high hold time
526 * @tRHOH_min: RE# high to output hold
527 * @tRHW_min: RE# high to WE# low
528 * @tRHZ_max: RE# high to output hi-Z
529 * @tRLOH_min: RE# low to output hold
530 * @tRP_min: RE# pulse width
531 * @tRR_min: Ready to RE# low (data only)
532 * @tRST_max: Device reset time, measured from the falling edge of R/B# to the
533 * rising edge of R/B#.
534 * @tWB_max: WE# high to SR[6] low
535 * @tWC_min: WE# cycle time
536 * @tWH_min: WE# high hold time
537 * @tWHR_min: WE# high to RE# low
538 * @tWP_min: WE# pulse width
539 * @tWW_min: WP# transition to WE# low
540 */
541struct nand_sdr_timings {
Boris Brezillon6d292312017-07-31 10:31:27 +0200542 u64 tBERS_max;
Boris Brezillon204e7ec2016-10-01 10:24:02 +0200543 u32 tCCS_min;
Boris Brezillon6d292312017-07-31 10:31:27 +0200544 u64 tPROG_max;
545 u64 tR_max;
Sascha Hauereee64b72016-09-15 10:32:46 +0200546 u32 tALH_min;
547 u32 tADL_min;
548 u32 tALS_min;
549 u32 tAR_min;
550 u32 tCEA_max;
551 u32 tCEH_min;
552 u32 tCH_min;
553 u32 tCHZ_max;
554 u32 tCLH_min;
555 u32 tCLR_min;
556 u32 tCLS_min;
557 u32 tCOH_min;
558 u32 tCS_min;
559 u32 tDH_min;
560 u32 tDS_min;
561 u32 tFEAT_max;
562 u32 tIR_min;
563 u32 tITC_max;
564 u32 tRC_min;
565 u32 tREA_max;
566 u32 tREH_min;
567 u32 tRHOH_min;
568 u32 tRHW_min;
569 u32 tRHZ_max;
570 u32 tRLOH_min;
571 u32 tRP_min;
572 u32 tRR_min;
573 u64 tRST_max;
574 u32 tWB_max;
575 u32 tWC_min;
576 u32 tWH_min;
577 u32 tWHR_min;
578 u32 tWP_min;
579 u32 tWW_min;
580};
581
582/**
583 * enum nand_data_interface_type - NAND interface timing type
584 * @NAND_SDR_IFACE: Single Data Rate interface
585 */
586enum nand_data_interface_type {
587 NAND_SDR_IFACE,
588};
589
590/**
591 * struct nand_data_interface - NAND interface timing
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -0300592 * @type: type of the timing
593 * @timings: The timing, type according to @type
594 * @timings.sdr: Use it when @type is %NAND_SDR_IFACE.
Sascha Hauereee64b72016-09-15 10:32:46 +0200595 */
596struct nand_data_interface {
597 enum nand_data_interface_type type;
598 union {
599 struct nand_sdr_timings sdr;
600 } timings;
601};
602
603/**
604 * nand_get_sdr_timings - get SDR timing from data interface
605 * @conf: The data interface
606 */
607static inline const struct nand_sdr_timings *
608nand_get_sdr_timings(const struct nand_data_interface *conf)
609{
610 if (conf->type != NAND_SDR_IFACE)
611 return ERR_PTR(-EINVAL);
612
613 return &conf->timings.sdr;
614}
615
616/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100617 * struct nand_op_cmd_instr - Definition of a command instruction
618 * @opcode: the command to issue in one cycle
619 */
620struct nand_op_cmd_instr {
621 u8 opcode;
622};
623
624/**
625 * struct nand_op_addr_instr - Definition of an address instruction
626 * @naddrs: length of the @addrs array
627 * @addrs: array containing the address cycles to issue
628 */
629struct nand_op_addr_instr {
630 unsigned int naddrs;
631 const u8 *addrs;
632};
633
634/**
635 * struct nand_op_data_instr - Definition of a data instruction
636 * @len: number of data bytes to move
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -0300637 * @buf: buffer to fill
638 * @buf.in: buffer to fill when reading from the NAND chip
639 * @buf.out: buffer to read from when writing to the NAND chip
Miquel Raynal8878b122017-11-09 14:16:45 +0100640 * @force_8bit: force 8-bit access
641 *
642 * Please note that "in" and "out" are inverted from the ONFI specification
643 * and are from the controller perspective, so a "in" is a read from the NAND
644 * chip while a "out" is a write to the NAND chip.
645 */
646struct nand_op_data_instr {
647 unsigned int len;
648 union {
649 void *in;
650 const void *out;
651 } buf;
652 bool force_8bit;
653};
654
655/**
656 * struct nand_op_waitrdy_instr - Definition of a wait ready instruction
657 * @timeout_ms: maximum delay while waiting for the ready/busy pin in ms
658 */
659struct nand_op_waitrdy_instr {
660 unsigned int timeout_ms;
661};
662
663/**
664 * enum nand_op_instr_type - Definition of all instruction types
665 * @NAND_OP_CMD_INSTR: command instruction
666 * @NAND_OP_ADDR_INSTR: address instruction
667 * @NAND_OP_DATA_IN_INSTR: data in instruction
668 * @NAND_OP_DATA_OUT_INSTR: data out instruction
669 * @NAND_OP_WAITRDY_INSTR: wait ready instruction
670 */
671enum nand_op_instr_type {
672 NAND_OP_CMD_INSTR,
673 NAND_OP_ADDR_INSTR,
674 NAND_OP_DATA_IN_INSTR,
675 NAND_OP_DATA_OUT_INSTR,
676 NAND_OP_WAITRDY_INSTR,
677};
678
679/**
680 * struct nand_op_instr - Instruction object
681 * @type: the instruction type
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -0300682 * @ctx: extra data associated to the instruction. You'll have to use the
683 * appropriate element depending on @type
684 * @ctx.cmd: use it if @type is %NAND_OP_CMD_INSTR
685 * @ctx.addr: use it if @type is %NAND_OP_ADDR_INSTR
686 * @ctx.data: use it if @type is %NAND_OP_DATA_IN_INSTR
687 * or %NAND_OP_DATA_OUT_INSTR
688 * @ctx.waitrdy: use it if @type is %NAND_OP_WAITRDY_INSTR
Miquel Raynal8878b122017-11-09 14:16:45 +0100689 * @delay_ns: delay the controller should apply after the instruction has been
690 * issued on the bus. Most modern controllers have internal timings
691 * control logic, and in this case, the controller driver can ignore
692 * this field.
693 */
694struct nand_op_instr {
695 enum nand_op_instr_type type;
696 union {
697 struct nand_op_cmd_instr cmd;
698 struct nand_op_addr_instr addr;
699 struct nand_op_data_instr data;
700 struct nand_op_waitrdy_instr waitrdy;
701 } ctx;
702 unsigned int delay_ns;
703};
704
705/*
706 * Special handling must be done for the WAITRDY timeout parameter as it usually
707 * is either tPROG (after a prog), tR (before a read), tRST (during a reset) or
708 * tBERS (during an erase) which all of them are u64 values that cannot be
709 * divided by usual kernel macros and must be handled with the special
710 * DIV_ROUND_UP_ULL() macro.
Geert Uytterhoeven9f825e72018-05-14 12:49:37 +0200711 *
712 * Cast to type of dividend is needed here to guarantee that the result won't
713 * be an unsigned long long when the dividend is an unsigned long (or smaller),
714 * which is what the compiler does when it sees ternary operator with 2
715 * different return types (picks the largest type to make sure there's no
716 * loss).
Miquel Raynal8878b122017-11-09 14:16:45 +0100717 */
Geert Uytterhoeven9f825e72018-05-14 12:49:37 +0200718#define __DIVIDE(dividend, divisor) ({ \
719 (__typeof__(dividend))(sizeof(dividend) <= sizeof(unsigned long) ? \
720 DIV_ROUND_UP(dividend, divisor) : \
721 DIV_ROUND_UP_ULL(dividend, divisor)); \
722 })
Miquel Raynal8878b122017-11-09 14:16:45 +0100723#define PSEC_TO_NSEC(x) __DIVIDE(x, 1000)
724#define PSEC_TO_MSEC(x) __DIVIDE(x, 1000000000)
725
726#define NAND_OP_CMD(id, ns) \
727 { \
728 .type = NAND_OP_CMD_INSTR, \
729 .ctx.cmd.opcode = id, \
730 .delay_ns = ns, \
731 }
732
733#define NAND_OP_ADDR(ncycles, cycles, ns) \
734 { \
735 .type = NAND_OP_ADDR_INSTR, \
736 .ctx.addr = { \
737 .naddrs = ncycles, \
738 .addrs = cycles, \
739 }, \
740 .delay_ns = ns, \
741 }
742
743#define NAND_OP_DATA_IN(l, b, ns) \
744 { \
745 .type = NAND_OP_DATA_IN_INSTR, \
746 .ctx.data = { \
747 .len = l, \
748 .buf.in = b, \
749 .force_8bit = false, \
750 }, \
751 .delay_ns = ns, \
752 }
753
754#define NAND_OP_DATA_OUT(l, b, ns) \
755 { \
756 .type = NAND_OP_DATA_OUT_INSTR, \
757 .ctx.data = { \
758 .len = l, \
759 .buf.out = b, \
760 .force_8bit = false, \
761 }, \
762 .delay_ns = ns, \
763 }
764
765#define NAND_OP_8BIT_DATA_IN(l, b, ns) \
766 { \
767 .type = NAND_OP_DATA_IN_INSTR, \
768 .ctx.data = { \
769 .len = l, \
770 .buf.in = b, \
771 .force_8bit = true, \
772 }, \
773 .delay_ns = ns, \
774 }
775
776#define NAND_OP_8BIT_DATA_OUT(l, b, ns) \
777 { \
778 .type = NAND_OP_DATA_OUT_INSTR, \
779 .ctx.data = { \
780 .len = l, \
781 .buf.out = b, \
782 .force_8bit = true, \
783 }, \
784 .delay_ns = ns, \
785 }
786
787#define NAND_OP_WAIT_RDY(tout_ms, ns) \
788 { \
789 .type = NAND_OP_WAITRDY_INSTR, \
790 .ctx.waitrdy.timeout_ms = tout_ms, \
791 .delay_ns = ns, \
792 }
793
794/**
795 * struct nand_subop - a sub operation
796 * @instrs: array of instructions
797 * @ninstrs: length of the @instrs array
798 * @first_instr_start_off: offset to start from for the first instruction
799 * of the sub-operation
800 * @last_instr_end_off: offset to end at (excluded) for the last instruction
801 * of the sub-operation
802 *
803 * Both @first_instr_start_off and @last_instr_end_off only apply to data or
804 * address instructions.
805 *
806 * When an operation cannot be handled as is by the NAND controller, it will
807 * be split by the parser into sub-operations which will be passed to the
808 * controller driver.
809 */
810struct nand_subop {
811 const struct nand_op_instr *instrs;
812 unsigned int ninstrs;
813 unsigned int first_instr_start_off;
814 unsigned int last_instr_end_off;
815};
816
Miquel Raynal760c4352018-07-19 00:09:12 +0200817unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
818 unsigned int op_id);
819unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
820 unsigned int op_id);
821unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
822 unsigned int op_id);
823unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
824 unsigned int op_id);
Miquel Raynal8878b122017-11-09 14:16:45 +0100825
826/**
827 * struct nand_op_parser_addr_constraints - Constraints for address instructions
828 * @maxcycles: maximum number of address cycles the controller can issue in a
829 * single step
830 */
831struct nand_op_parser_addr_constraints {
832 unsigned int maxcycles;
833};
834
835/**
836 * struct nand_op_parser_data_constraints - Constraints for data instructions
837 * @maxlen: maximum data length that the controller can handle in a single step
838 */
839struct nand_op_parser_data_constraints {
840 unsigned int maxlen;
841};
842
843/**
844 * struct nand_op_parser_pattern_elem - One element of a pattern
845 * @type: the instructuction type
846 * @optional: whether this element of the pattern is optional or mandatory
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -0300847 * @ctx: address or data constraint
848 * @ctx.addr: address constraint (number of cycles)
849 * @ctx.data: data constraint (data length)
Miquel Raynal8878b122017-11-09 14:16:45 +0100850 */
851struct nand_op_parser_pattern_elem {
852 enum nand_op_instr_type type;
853 bool optional;
854 union {
855 struct nand_op_parser_addr_constraints addr;
856 struct nand_op_parser_data_constraints data;
Miquel Raynalc1a72e22018-01-19 19:11:27 +0100857 } ctx;
Miquel Raynal8878b122017-11-09 14:16:45 +0100858};
859
860#define NAND_OP_PARSER_PAT_CMD_ELEM(_opt) \
861 { \
862 .type = NAND_OP_CMD_INSTR, \
863 .optional = _opt, \
864 }
865
866#define NAND_OP_PARSER_PAT_ADDR_ELEM(_opt, _maxcycles) \
867 { \
868 .type = NAND_OP_ADDR_INSTR, \
869 .optional = _opt, \
Miquel Raynalc1a72e22018-01-19 19:11:27 +0100870 .ctx.addr.maxcycles = _maxcycles, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100871 }
872
873#define NAND_OP_PARSER_PAT_DATA_IN_ELEM(_opt, _maxlen) \
874 { \
875 .type = NAND_OP_DATA_IN_INSTR, \
876 .optional = _opt, \
Miquel Raynalc1a72e22018-01-19 19:11:27 +0100877 .ctx.data.maxlen = _maxlen, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100878 }
879
880#define NAND_OP_PARSER_PAT_DATA_OUT_ELEM(_opt, _maxlen) \
881 { \
882 .type = NAND_OP_DATA_OUT_INSTR, \
883 .optional = _opt, \
Miquel Raynalc1a72e22018-01-19 19:11:27 +0100884 .ctx.data.maxlen = _maxlen, \
Miquel Raynal8878b122017-11-09 14:16:45 +0100885 }
886
887#define NAND_OP_PARSER_PAT_WAITRDY_ELEM(_opt) \
888 { \
889 .type = NAND_OP_WAITRDY_INSTR, \
890 .optional = _opt, \
891 }
892
893/**
894 * struct nand_op_parser_pattern - NAND sub-operation pattern descriptor
895 * @elems: array of pattern elements
896 * @nelems: number of pattern elements in @elems array
897 * @exec: the function that will issue a sub-operation
898 *
899 * A pattern is a list of elements, each element reprensenting one instruction
900 * with its constraints. The pattern itself is used by the core to match NAND
901 * chip operation with NAND controller operations.
902 * Once a match between a NAND controller operation pattern and a NAND chip
903 * operation (or a sub-set of a NAND operation) is found, the pattern ->exec()
904 * hook is called so that the controller driver can issue the operation on the
905 * bus.
906 *
907 * Controller drivers should declare as many patterns as they support and pass
908 * this list of patterns (created with the help of the following macro) to
909 * the nand_op_parser_exec_op() helper.
910 */
911struct nand_op_parser_pattern {
912 const struct nand_op_parser_pattern_elem *elems;
913 unsigned int nelems;
914 int (*exec)(struct nand_chip *chip, const struct nand_subop *subop);
915};
916
917#define NAND_OP_PARSER_PATTERN(_exec, ...) \
918 { \
919 .exec = _exec, \
920 .elems = (struct nand_op_parser_pattern_elem[]) { __VA_ARGS__ }, \
921 .nelems = sizeof((struct nand_op_parser_pattern_elem[]) { __VA_ARGS__ }) / \
922 sizeof(struct nand_op_parser_pattern_elem), \
923 }
924
925/**
926 * struct nand_op_parser - NAND controller operation parser descriptor
927 * @patterns: array of supported patterns
928 * @npatterns: length of the @patterns array
929 *
930 * The parser descriptor is just an array of supported patterns which will be
931 * iterated by nand_op_parser_exec_op() everytime it tries to execute an
932 * NAND operation (or tries to determine if a specific operation is supported).
933 *
934 * It is worth mentioning that patterns will be tested in their declaration
935 * order, and the first match will be taken, so it's important to order patterns
936 * appropriately so that simple/inefficient patterns are placed at the end of
937 * the list. Usually, this is where you put single instruction patterns.
938 */
939struct nand_op_parser {
940 const struct nand_op_parser_pattern *patterns;
941 unsigned int npatterns;
942};
943
944#define NAND_OP_PARSER(...) \
945 { \
946 .patterns = (struct nand_op_parser_pattern[]) { __VA_ARGS__ }, \
947 .npatterns = sizeof((struct nand_op_parser_pattern[]) { __VA_ARGS__ }) / \
948 sizeof(struct nand_op_parser_pattern), \
949 }
950
951/**
952 * struct nand_operation - NAND operation descriptor
953 * @instrs: array of instructions to execute
954 * @ninstrs: length of the @instrs array
955 *
956 * The actual operation structure that will be passed to chip->exec_op().
957 */
958struct nand_operation {
959 const struct nand_op_instr *instrs;
960 unsigned int ninstrs;
961};
962
963#define NAND_OPERATION(_instrs) \
964 { \
965 .instrs = _instrs, \
966 .ninstrs = ARRAY_SIZE(_instrs), \
967 }
968
969int nand_op_parser_exec_op(struct nand_chip *chip,
970 const struct nand_op_parser *parser,
971 const struct nand_operation *op, bool check_only);
972
973/**
Boris Brezillon82fc5092018-09-07 00:38:34 +0200974 * struct nand_legacy - NAND chip legacy fields/hooks
975 * @IO_ADDR_R: address to read the 8 I/O lines of the flash device
976 * @IO_ADDR_W: address to write the 8 I/O lines of the flash device
Boris Brezillon716bbba2018-09-07 00:38:35 +0200977 * @read_byte: read one byte from the chip
978 * @write_byte: write a single byte to the chip on the low 8 I/O lines
979 * @write_buf: write data from the buffer to the chip
980 * @read_buf: read data from the chip into the buffer
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200981 * @cmd_ctrl: hardware specific function for controlling ALE/CLE/nCE. Also used
982 * to write command and address
983 * @cmdfunc: hardware specific function for writing commands to the chip.
Boris Brezillon8395b752018-09-07 00:38:37 +0200984 * @dev_ready: hardware specific function for accessing device ready/busy line.
985 * If set to NULL no access to ready/busy is available and the
986 * ready/busy information is read from the chip status register.
987 * @waitfunc: hardware specific function for wait on ready.
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200988 * @block_bad: check if a block is bad, using OOB markers
989 * @block_markbad: mark a block bad
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +0200990 * @erase: erase function
Boris Brezillon45240362018-09-07 00:38:40 +0200991 * @set_features: set the NAND chip features
992 * @get_features: get the NAND chip features
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200993 * @chip_delay: chip dependent delay for transferring data from array to read
994 * regs (tR).
Boris Brezillon82fc5092018-09-07 00:38:34 +0200995 *
996 * If you look at this structure you're already wrong. These fields/hooks are
997 * all deprecated.
998 */
999struct nand_legacy {
1000 void __iomem *IO_ADDR_R;
1001 void __iomem *IO_ADDR_W;
Boris Brezillon716bbba2018-09-07 00:38:35 +02001002 u8 (*read_byte)(struct nand_chip *chip);
1003 void (*write_byte)(struct nand_chip *chip, u8 byte);
1004 void (*write_buf)(struct nand_chip *chip, const u8 *buf, int len);
1005 void (*read_buf)(struct nand_chip *chip, u8 *buf, int len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001006 void (*cmd_ctrl)(struct nand_chip *chip, int dat, unsigned int ctrl);
1007 void (*cmdfunc)(struct nand_chip *chip, unsigned command, int column,
1008 int page_addr);
Boris Brezillon8395b752018-09-07 00:38:37 +02001009 int (*dev_ready)(struct nand_chip *chip);
1010 int (*waitfunc)(struct nand_chip *chip);
Boris Brezilloncdc784c2018-09-07 00:38:38 +02001011 int (*block_bad)(struct nand_chip *chip, loff_t ofs);
1012 int (*block_markbad)(struct nand_chip *chip, loff_t ofs);
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02001013 int (*erase)(struct nand_chip *chip, int page);
Boris Brezillon45240362018-09-07 00:38:40 +02001014 int (*set_features)(struct nand_chip *chip, int feature_addr,
1015 u8 *subfeature_para);
1016 int (*get_features)(struct nand_chip *chip, int feature_addr,
1017 u8 *subfeature_para);
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001018 int chip_delay;
Boris Brezillon82fc5092018-09-07 00:38:34 +02001019};
1020
1021/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * struct nand_chip - NAND Private Flash Chip Data
Boris BREZILLONed4f85c2015-12-01 12:03:06 +01001023 * @mtd: MTD device registered to the MTD framework
Boris Brezillon82fc5092018-09-07 00:38:34 +02001024 * @legacy: All legacy fields/hooks. If you develop a new driver,
1025 * don't even try to use any of these fields/hooks, and if
1026 * you're modifying an existing driver that is using those
1027 * fields/hooks, you should consider reworking the driver
1028 * avoid using them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * @select_chip: [REPLACEABLE] select chip nr
Miquel Raynal8878b122017-11-09 14:16:45 +01001030 * @exec_op: controller specific method to execute NAND operations.
1031 * This method replaces ->cmdfunc(),
Boris Brezillon8395b752018-09-07 00:38:37 +02001032 * ->legacy.{read,write}_{buf,byte,word}(),
1033 * ->legacy.dev_ready() and ->waifunc().
Brian Norrisba84fb52014-01-03 15:13:33 -08001034 * @setup_read_retry: [FLASHSPECIFIC] flash (vendor) specific function for
1035 * setting the read-retry mode. Mostly needed for MLC NAND.
Brian Norris7854d3f2011-06-23 14:12:08 -07001036 * @ecc: [BOARDSPECIFIC] ECC control structure
Masahiro Yamada477544c2017-03-30 17:15:05 +09001037 * @buf_align: minimum buffer alignment required by a platform
Miquel Raynal7da45132018-07-17 09:08:02 +02001038 * @dummy_controller: dummy controller implementation for drivers that can
1039 * only control a single chip
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001040 * @state: [INTERN] the current state of the NAND device
Brian Norrise9195ed2011-08-30 18:45:43 -07001041 * @oob_poi: "poison value buffer," used for laying out OOB data
1042 * before writing
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001043 * @page_shift: [INTERN] number of address bits in a page (column
1044 * address bits).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * @phys_erase_shift: [INTERN] number of address bits in a physical eraseblock
1046 * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
1047 * @chip_shift: [INTERN] number of address bits in one chip
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001048 * @options: [BOARDSPECIFIC] various chip options. They can partly
1049 * be set to inform nand_scan about special functionality.
1050 * See the defines for further explanation.
Brian Norris5fb15492011-05-31 16:31:21 -07001051 * @bbt_options: [INTERN] bad block specific options. All options used
1052 * here must come from bbm.h. By default, these options
1053 * will be copied to the appropriate nand_bbt_descr's.
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001054 * @badblockpos: [INTERN] position of the bad block marker in the oob
1055 * area.
Brian Norris661a0832012-01-13 18:11:50 -08001056 * @badblockbits: [INTERN] minimum number of set bits in a good block's
1057 * bad block marker position; i.e., BBM == 11110111b is
1058 * not bad when badblockbits == 7
Huang Shijie7db906b2013-09-25 14:58:11 +08001059 * @bits_per_cell: [INTERN] number of bits per cell. i.e., 1 means SLC.
Huang Shijie4cfeca22013-05-17 11:17:25 +08001060 * @ecc_strength_ds: [INTERN] ECC correctability from the datasheet.
1061 * Minimum amount of bit errors per @ecc_step_ds guaranteed
1062 * to be correctable. If unknown, set to zero.
1063 * @ecc_step_ds: [INTERN] ECC step required by the @ecc_strength_ds,
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -03001064 * also from the datasheet. It is the recommended ECC step
Huang Shijie4cfeca22013-05-17 11:17:25 +08001065 * size, if known; if unknown, set to zero.
Boris BREZILLON57a94e22014-09-22 20:11:50 +02001066 * @onfi_timing_mode_default: [INTERN] default ONFI timing mode. This field is
Boris Brezillond8e725d2016-09-15 10:32:50 +02001067 * set to the actually used ONFI mode if the chip is
1068 * ONFI compliant or deduced from the datasheet if
1069 * the NAND chip is not ONFI compliant.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 * @numchips: [INTERN] number of physical chips
1071 * @chipsize: [INTERN] the size of one chip for multichip arrays
1072 * @pagemask: [INTERN] page number mask = number of (pages / chip) - 1
Masahiro Yamadac0313b92017-12-05 17:47:16 +09001073 * @data_buf: [INTERN] buffer for data, size is (page size + oobsize).
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001074 * @pagebuf: [INTERN] holds the pagenumber which is currently in
1075 * data_buf.
Mike Dunnedbc45402012-04-25 12:06:11 -07001076 * @pagebuf_bitflips: [INTERN] holds the bitflip count for the page which is
1077 * currently in data_buf.
Thomas Gleixner29072b92006-09-28 15:38:36 +02001078 * @subpagesize: [INTERN] holds the subpagesize
Boris Brezillon7f501f02016-05-24 19:20:05 +02001079 * @id: [INTERN] holds NAND ID
Miquel Raynalf4531b22018-03-19 14:47:26 +01001080 * @parameters: [INTERN] holds generic parameters under an easily
1081 * readable form.
Zach Brownceb374e2017-01-10 13:30:19 -06001082 * @max_bb_per_die: [INTERN] the max number of bad blocks each die of a
1083 * this nand device will encounter their life times.
1084 * @blocks_per_die: [INTERN] The number of PEBs in a die
Randy Dunlap61babe92016-11-21 18:32:08 -08001085 * @data_interface: [INTERN] NAND interface timing information
Brian Norrisba84fb52014-01-03 15:13:33 -08001086 * @read_retries: [INTERN] the number of read retry modes supported
Boris Brezillon104e4422017-03-16 09:35:58 +01001087 * @setup_data_interface: [OPTIONAL] setup the data interface and timing. If
1088 * chipnr is set to %NAND_DATA_IFACE_CHECK_ONLY this
1089 * means the configuration should not be applied but
1090 * only checked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * @bbt: [INTERN] bad block table pointer
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001092 * @bbt_td: [REPLACEABLE] bad block table descriptor for flash
1093 * lookup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001095 * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
1096 * bad block scan.
1097 * @controller: [REPLACEABLE] a pointer to a hardware controller
Brian Norris7854d3f2011-06-23 14:12:08 -07001098 * structure which is shared among multiple independent
Sebastian Andrzej Siewiora0491fc2010-10-05 12:41:01 +02001099 * devices.
Brian Norris32c8db82011-08-23 17:17:35 -07001100 * @priv: [OPTIONAL] pointer to private chip data
Boris Brezillonabbe26d2016-06-08 09:32:55 +02001101 * @manufacturer: [INTERN] Contains manufacturer information
Mauro Carvalho Chehaba6766882018-05-07 06:35:52 -03001102 * @manufacturer.desc: [INTERN] Contains manufacturer's description
1103 * @manufacturer.priv: [INTERN] Contains manufacturer private information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Thomas Gleixner61ecfa82005-11-07 11:15:31 +00001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106struct nand_chip {
Boris BREZILLONed4f85c2015-12-01 12:03:06 +01001107 struct mtd_info mtd;
Boris Brezillon82fc5092018-09-07 00:38:34 +02001108
1109 struct nand_legacy legacy;
Thomas Gleixner61ecfa82005-11-07 11:15:31 +00001110
Boris Brezillon758b56f2018-09-06 14:05:24 +02001111 void (*select_chip)(struct nand_chip *chip, int cs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001112 int (*exec_op)(struct nand_chip *chip,
1113 const struct nand_operation *op,
1114 bool check_only);
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02001115 int (*setup_read_retry)(struct nand_chip *chip, int retry_mode);
Boris Brezillon858838b2018-09-06 14:05:33 +02001116 int (*setup_data_interface)(struct nand_chip *chip, int chipnr,
Boris Brezillon104e4422017-03-16 09:35:58 +01001117 const struct nand_data_interface *conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001118
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001119 unsigned int options;
Brian Norris5fb15492011-05-31 16:31:21 -07001120 unsigned int bbt_options;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001121
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001122 int page_shift;
1123 int phys_erase_shift;
1124 int bbt_erase_shift;
1125 int chip_shift;
1126 int numchips;
1127 uint64_t chipsize;
1128 int pagemask;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09001129 u8 *data_buf;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001130 int pagebuf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001131 unsigned int pagebuf_bitflips;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001132 int subpagesize;
Huang Shijie7db906b2013-09-25 14:58:11 +08001133 uint8_t bits_per_cell;
Huang Shijie4cfeca22013-05-17 11:17:25 +08001134 uint16_t ecc_strength_ds;
1135 uint16_t ecc_step_ds;
Boris BREZILLON57a94e22014-09-22 20:11:50 +02001136 int onfi_timing_mode_default;
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001137 int badblockpos;
1138 int badblockbits;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001139
Boris Brezillon7f501f02016-05-24 19:20:05 +02001140 struct nand_id id;
Miquel Raynalf4531b22018-03-19 14:47:26 +01001141 struct nand_parameters parameters;
Zach Brownceb374e2017-01-10 13:30:19 -06001142 u16 max_bb_per_die;
1143 u32 blocks_per_die;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02001144
Miquel Raynal17fa8042017-11-30 18:01:31 +01001145 struct nand_data_interface data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001146
Brian Norrisba84fb52014-01-03 15:13:33 -08001147 int read_retries;
1148
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001149 flstate_t state;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001150
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001151 uint8_t *oob_poi;
Miquel Raynal7da45132018-07-17 09:08:02 +02001152 struct nand_controller *controller;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001153
1154 struct nand_ecc_ctrl ecc;
Masahiro Yamada477544c2017-03-30 17:15:05 +09001155 unsigned long buf_align;
Miquel Raynal7da45132018-07-17 09:08:02 +02001156 struct nand_controller dummy_controller;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001157
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001158 uint8_t *bbt;
1159 struct nand_bbt_descr *bbt_td;
1160 struct nand_bbt_descr *bbt_md;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001161
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001162 struct nand_bbt_descr *badblock_pattern;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001163
Sebastian Andrzej Siewiorb46daf72010-10-07 21:48:27 +02001164 void *priv;
Boris Brezillonabbe26d2016-06-08 09:32:55 +02001165
1166 struct {
1167 const struct nand_manufacturer *desc;
1168 void *priv;
1169 } manufacturer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170};
1171
Miquel Raynal8878b122017-11-09 14:16:45 +01001172static inline int nand_exec_op(struct nand_chip *chip,
1173 const struct nand_operation *op)
1174{
1175 if (!chip->exec_op)
1176 return -ENOTSUPP;
1177
1178 return chip->exec_op(chip, op, false);
1179}
1180
Boris Brezillon41b207a2016-02-03 19:06:15 +01001181extern const struct mtd_ooblayout_ops nand_ooblayout_sp_ops;
1182extern const struct mtd_ooblayout_ops nand_ooblayout_lp_ops;
1183
Brian Norris28b8b26b2015-10-30 20:33:20 -07001184static inline void nand_set_flash_node(struct nand_chip *chip,
1185 struct device_node *np)
1186{
Boris BREZILLON29574ed2015-12-10 09:00:38 +01001187 mtd_set_of_node(&chip->mtd, np);
Brian Norris28b8b26b2015-10-30 20:33:20 -07001188}
1189
1190static inline struct device_node *nand_get_flash_node(struct nand_chip *chip)
1191{
Boris BREZILLON29574ed2015-12-10 09:00:38 +01001192 return mtd_get_of_node(&chip->mtd);
Brian Norris28b8b26b2015-10-30 20:33:20 -07001193}
1194
Boris BREZILLON9eba47d2015-11-16 14:37:35 +01001195static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
1196{
Boris BREZILLON2d3b77b2015-12-10 09:00:33 +01001197 return container_of(mtd, struct nand_chip, mtd);
Boris BREZILLON9eba47d2015-11-16 14:37:35 +01001198}
1199
Boris BREZILLONffd014f2015-12-01 12:03:07 +01001200static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
1201{
1202 return &chip->mtd;
1203}
1204
Boris BREZILLONd39ddbd2015-12-10 09:00:39 +01001205static inline void *nand_get_controller_data(struct nand_chip *chip)
1206{
1207 return chip->priv;
1208}
1209
1210static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
1211{
1212 chip->priv = priv;
1213}
1214
Boris Brezillonabbe26d2016-06-08 09:32:55 +02001215static inline void nand_set_manufacturer_data(struct nand_chip *chip,
1216 void *priv)
1217{
1218 chip->manufacturer.priv = priv;
1219}
1220
1221static inline void *nand_get_manufacturer_data(struct nand_chip *chip)
1222{
1223 return chip->manufacturer.priv;
1224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/*
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +02001227 * A helper for defining older NAND chips where the second ID byte fully
1228 * defined the chip, including the geometry (chip size, eraseblock size, page
Artem Bityutskiy5bfa9b72013-03-19 10:29:26 +02001229 * size). All these chips have 512 bytes NAND page size.
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +02001230 */
Artem Bityutskiy5bfa9b72013-03-19 10:29:26 +02001231#define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
1232 { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
1233 .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +02001234
1235/*
1236 * A helper for defining newer chips which report their page size and
1237 * eraseblock size via the extended ID bytes.
1238 *
1239 * The real difference between LEGACY_ID_NAND and EXTENDED_ID_NAND is that with
1240 * EXTENDED_ID_NAND, manufacturers overloaded the same device ID so that the
1241 * device ID now only represented a particular total chip size (and voltage,
1242 * buswidth), and the page size, eraseblock size, and OOB size could vary while
1243 * using the same device ID.
1244 */
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001245#define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
1246 { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
Artem Bityutskiy8dbfae12013-03-04 15:39:18 +02001247 .options = (opts) }
1248
Huang Shijie2dc0bdd2013-05-17 11:17:31 +08001249#define NAND_ECC_INFO(_strength, _step) \
1250 { .strength_ds = (_strength), .step_ds = (_step) }
1251#define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
1252#define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/**
1255 * struct nand_flash_dev - NAND Flash Device ID Structure
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001256 * @name: a human-readable name of the NAND chip
1257 * @dev_id: the device ID (the second byte of the full chip ID array)
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001258 * @mfr_id: manufecturer ID part of the full chip ID array (refers the same
1259 * memory address as @id[0])
1260 * @dev_id: device ID part of the full chip ID array (refers the same memory
1261 * address as @id[1])
1262 * @id: full device ID array
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001263 * @pagesize: size of the NAND page in bytes; if 0, then the real page size (as
1264 * well as the eraseblock size) is determined from the extended NAND
1265 * chip ID array)
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001266 * @chipsize: total chip size in MiB
Artem Bityutskiyecb42fe2013-03-13 13:45:00 +02001267 * @erasesize: eraseblock size in bytes (determined from the extended ID if 0)
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001268 * @options: stores various chip bit options
Huang Shijief22d5f62013-03-15 11:00:59 +08001269 * @id_len: The valid length of the @id.
1270 * @oobsize: OOB size
Randy Dunlap7b7d8982014-07-27 14:31:53 -07001271 * @ecc: ECC correctability and step information from the datasheet.
Huang Shijie2dc0bdd2013-05-17 11:17:31 +08001272 * @ecc.strength_ds: The ECC correctability from the datasheet, same as the
1273 * @ecc_strength_ds in nand_chip{}.
1274 * @ecc.step_ds: The ECC step required by the @ecc.strength_ds, same as the
1275 * @ecc_step_ds in nand_chip{}, also from the datasheet.
1276 * For example, the "4bit ECC for each 512Byte" can be set with
1277 * NAND_ECC_INFO(4, 512).
Boris BREZILLON57a94e22014-09-22 20:11:50 +02001278 * @onfi_timing_mode_default: the default ONFI timing mode entered after a NAND
1279 * reset. Should be deduced from timings described
1280 * in the datasheet.
1281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
1283struct nand_flash_dev {
1284 char *name;
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001285 union {
1286 struct {
1287 uint8_t mfr_id;
1288 uint8_t dev_id;
1289 };
Artem Bityutskiy53552d22013-03-14 09:57:23 +02001290 uint8_t id[NAND_MAX_ID_LEN];
Artem Bityutskiy8e12b472013-03-04 16:26:56 +02001291 };
Artem Bityutskiyecb42fe2013-03-13 13:45:00 +02001292 unsigned int pagesize;
1293 unsigned int chipsize;
1294 unsigned int erasesize;
1295 unsigned int options;
Huang Shijief22d5f62013-03-15 11:00:59 +08001296 uint16_t id_len;
1297 uint16_t oobsize;
Huang Shijie2dc0bdd2013-05-17 11:17:31 +08001298 struct {
1299 uint16_t strength_ds;
1300 uint16_t step_ds;
1301 } ecc;
Boris BREZILLON57a94e22014-09-22 20:11:50 +02001302 int onfi_timing_mode_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303};
1304
Boris Brezillon44b07b92018-07-05 12:27:30 +02001305int nand_create_bbt(struct nand_chip *chip);
Sascha Hauerb88730a2016-09-15 10:32:48 +02001306
Huang Shijie1d0ed692013-09-25 14:58:10 +08001307/*
1308 * Check if it is a SLC nand.
1309 * The !nand_is_slc() can be used to check the MLC/TLC nand chips.
1310 * We do not distinguish the MLC and TLC now.
1311 */
1312static inline bool nand_is_slc(struct nand_chip *chip)
1313{
Lothar Waßmann2d2a2b82017-08-29 12:17:13 +02001314 WARN(chip->bits_per_cell == 0,
1315 "chip->bits_per_cell is used uninitialized\n");
Huang Shijie7db906b2013-09-25 14:58:11 +08001316 return chip->bits_per_cell == 1;
Huang Shijie1d0ed692013-09-25 14:58:10 +08001317}
Brian Norris3dad2342014-01-29 14:08:12 -08001318
1319/**
1320 * Check if the opcode's address should be sent only on the lower 8 bits
1321 * @command: opcode to check
1322 */
1323static inline int nand_opcode_8bits(unsigned int command)
1324{
David Mosbergere34fcb02014-03-21 16:05:10 -06001325 switch (command) {
1326 case NAND_CMD_READID:
1327 case NAND_CMD_PARAM:
1328 case NAND_CMD_GET_FEATURES:
1329 case NAND_CMD_SET_FEATURES:
1330 return 1;
1331 default:
1332 break;
1333 }
1334 return 0;
Brian Norris3dad2342014-01-29 14:08:12 -08001335}
1336
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001337int nand_check_erased_ecc_chunk(void *data, int datalen,
1338 void *ecc, int ecclen,
1339 void *extraoob, int extraooblen,
1340 int threshold);
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001341
Abhishek Sahu181ace92018-06-20 12:57:28 +05301342int nand_ecc_choose_conf(struct nand_chip *chip,
1343 const struct nand_ecc_caps *caps, int oobavail);
1344
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001345/* Default write_oob implementation */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001346int nand_write_oob_std(struct nand_chip *chip, int page);
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001347
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001348/* Default read_oob implementation */
Boris Brezillonb9761682018-09-06 14:05:20 +02001349int nand_read_oob_std(struct nand_chip *chip, int page);
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001350
Boris Brezillon4a78cc62017-05-26 17:10:15 +02001351/* Stub used by drivers that do not support GET/SET FEATURES operations */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02001352int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
1353 u8 *subfeature_param);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02001354
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001355/* Default read_page_raw implementation */
Boris Brezillonb9761682018-09-06 14:05:20 +02001356int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
1357 int page);
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001358
1359/* Default write_page_raw implementation */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001360int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
1361 int oob_required, int page);
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001362
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001363/* Reset and initialize a NAND device */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001364int nand_reset(struct nand_chip *chip, int chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001365
Boris Brezillon97d90da2017-11-30 18:01:29 +01001366/* NAND operation helpers */
1367int nand_reset_op(struct nand_chip *chip);
1368int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1369 unsigned int len);
1370int nand_status_op(struct nand_chip *chip, u8 *status);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001371int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock);
1372int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1373 unsigned int offset_in_page, void *buf, unsigned int len);
1374int nand_change_read_column_op(struct nand_chip *chip,
1375 unsigned int offset_in_page, void *buf,
1376 unsigned int len, bool force_8bit);
1377int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1378 unsigned int offset_in_page, void *buf, unsigned int len);
1379int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1380 unsigned int offset_in_page, const void *buf,
1381 unsigned int len);
1382int nand_prog_page_end_op(struct nand_chip *chip);
1383int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1384 unsigned int offset_in_page, const void *buf,
1385 unsigned int len);
1386int nand_change_write_column_op(struct nand_chip *chip,
1387 unsigned int offset_in_page, const void *buf,
1388 unsigned int len, bool force_8bit);
1389int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1390 bool force_8bit);
1391int nand_write_data_op(struct nand_chip *chip, const void *buf,
1392 unsigned int len, bool force_8bit);
1393
Boris Brezillon0b4e61c2018-09-07 00:38:42 +02001394/* Scan and identify a NAND device */
1395int nand_scan_with_ids(struct nand_chip *chip, unsigned int max_chips,
1396 struct nand_flash_dev *ids);
1397
1398static inline int nand_scan(struct nand_chip *chip, unsigned int max_chips)
1399{
1400 return nand_scan_with_ids(chip, max_chips, NULL);
1401}
1402
1403/* Internal helper for board drivers which need to override command function */
1404void nand_wait_ready(struct nand_chip *chip);
1405
Miquel Raynal98732da2018-07-25 15:31:50 +02001406/*
1407 * Free resources held by the NAND device, must be called on error after a
1408 * sucessful nand_scan().
1409 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02001410void nand_cleanup(struct nand_chip *chip);
Miquel Raynal98732da2018-07-25 15:31:50 +02001411/* Unregister the MTD device and calls nand_cleanup() */
Boris Brezillon59ac2762018-09-06 14:05:15 +02001412void nand_release(struct nand_chip *chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02001413
Miquel Raynal8878b122017-11-09 14:16:45 +01001414/*
1415 * External helper for controller drivers that have to implement the WAITRDY
1416 * instruction and have no physical pin to check it.
1417 */
1418int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms);
1419
Boris Brezillond4092d72017-08-04 17:29:10 +02001420#endif /* __LINUX_MTD_RAWNAND_H */