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