developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 1 | From 0aeb63ff705a42b9140cad179ebe7f5fc54d285c Mon Sep 17 00:00:00 2001 |
| 2 | From: Sam Shih <sam.shih@mediatek.com> |
| 3 | Date: Fri, 2 Jun 2023 13:06:09 +0800 |
| 4 | Subject: [PATCH] |
| 5 | [spi-and-storage][999-2310-v5.7-mtd-nand-spi-rework-detect-procedure-for-different-read-id-op.patch] |
| 6 | |
| 7 | --- |
| 8 | drivers/mtd/nand/spi/core.c | 86 ++++++++++++++++++++++--------- |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 9 | drivers/mtd/nand/spi/macronix.c | 30 +++-------- |
| 10 | drivers/mtd/nand/spi/micron.c | 26 ++-------- |
| 11 | drivers/mtd/nand/spi/paragon.c | 28 +++------- |
| 12 | drivers/mtd/nand/spi/toshiba.c | 42 +++++---------- |
| 13 | drivers/mtd/nand/spi/winbond.c | 34 +++--------- |
| 14 | include/linux/mtd/spinand.h | 66 ++++++++++++++++-------- |
| 15 | 8 files changed, 155 insertions(+), 202 deletions(-) |
| 16 | |
| 17 | diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c |
| 18 | index 55e636efc..9f5f95ff7 100644 |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 19 | --- a/drivers/mtd/nand/spi/core.c |
| 20 | +++ b/drivers/mtd/nand/spi/core.c |
| 21 | @@ -16,6 +16,7 @@ |
| 22 | #include <linux/mtd/spinand.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/slab.h> |
| 25 | +#include <linux/string.h> |
| 26 | #include <linux/spi/spi.h> |
| 27 | #include <linux/spi/spi-mem.h> |
| 28 | |
| 29 | @@ -370,10 +371,11 @@ out: |
| 30 | return status & STATUS_BUSY ? -ETIMEDOUT : 0; |
| 31 | } |
| 32 | |
| 33 | -static int spinand_read_id_op(struct spinand_device *spinand, u8 *buf) |
| 34 | +static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr, |
| 35 | + u8 ndummy, u8 *buf) |
| 36 | { |
| 37 | - struct spi_mem_op op = SPINAND_READID_OP(0, spinand->scratchbuf, |
| 38 | - SPINAND_MAX_ID_LEN); |
| 39 | + struct spi_mem_op op = SPINAND_READID_OP( |
| 40 | + naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN); |
| 41 | int ret; |
| 42 | |
| 43 | ret = spi_mem_exec_op(spinand->spimem, &op); |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 44 | @@ -760,24 +762,62 @@ static const struct spinand_manufacturer *spinand_manufacturers[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 45 | &winbond_spinand_manufacturer, |
| 46 | }; |
| 47 | |
| 48 | -static int spinand_manufacturer_detect(struct spinand_device *spinand) |
| 49 | +static int spinand_manufacturer_match(struct spinand_device *spinand, |
| 50 | + enum spinand_readid_method rdid_method) |
| 51 | { |
| 52 | + u8 *id = spinand->id.data; |
| 53 | unsigned int i; |
| 54 | int ret; |
| 55 | |
| 56 | for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) { |
| 57 | - ret = spinand_manufacturers[i]->ops->detect(spinand); |
| 58 | - if (ret > 0) { |
| 59 | - spinand->manufacturer = spinand_manufacturers[i]; |
| 60 | - return 0; |
| 61 | - } else if (ret < 0) { |
| 62 | - return ret; |
| 63 | - } |
| 64 | - } |
| 65 | + const struct spinand_manufacturer *manufacturer = |
| 66 | + spinand_manufacturers[i]; |
| 67 | + |
| 68 | + if (id[0] != manufacturer->id) |
| 69 | + continue; |
| 70 | |
| 71 | + ret = spinand_match_and_init(spinand, |
| 72 | + manufacturer->chips, |
| 73 | + manufacturer->nchips, |
| 74 | + rdid_method); |
| 75 | + if (ret < 0) |
| 76 | + continue; |
| 77 | + |
| 78 | + spinand->manufacturer = manufacturer; |
| 79 | + return 0; |
| 80 | + } |
| 81 | return -ENOTSUPP; |
| 82 | } |
| 83 | |
| 84 | +static int spinand_id_detect(struct spinand_device *spinand) |
| 85 | +{ |
| 86 | + u8 *id = spinand->id.data; |
| 87 | + int ret; |
| 88 | + |
| 89 | + ret = spinand_read_id_op(spinand, 0, 0, id); |
| 90 | + if (ret) |
| 91 | + return ret; |
| 92 | + ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE); |
| 93 | + if (!ret) |
| 94 | + return 0; |
| 95 | + |
| 96 | + ret = spinand_read_id_op(spinand, 1, 0, id); |
| 97 | + if (ret) |
| 98 | + return ret; |
| 99 | + ret = spinand_manufacturer_match(spinand, |
| 100 | + SPINAND_READID_METHOD_OPCODE_ADDR); |
| 101 | + if (!ret) |
| 102 | + return 0; |
| 103 | + |
| 104 | + ret = spinand_read_id_op(spinand, 0, 1, id); |
| 105 | + if (ret) |
| 106 | + return ret; |
| 107 | + ret = spinand_manufacturer_match(spinand, |
| 108 | + SPINAND_READID_METHOD_OPCODE_DUMMY); |
| 109 | + |
| 110 | + return ret; |
| 111 | +} |
| 112 | + |
| 113 | static int spinand_manufacturer_init(struct spinand_device *spinand) |
| 114 | { |
| 115 | if (spinand->manufacturer->ops->init) |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 116 | @@ -833,9 +873,9 @@ spinand_select_op_variant(struct spinand_device *spinand, |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 117 | * @spinand: SPI NAND object |
| 118 | * @table: SPI NAND device description table |
| 119 | * @table_size: size of the device description table |
| 120 | + * @rdid_method: read id method to match |
| 121 | * |
| 122 | - * Should be used by SPI NAND manufacturer drivers when they want to find a |
| 123 | - * match between a device ID retrieved through the READ_ID command and an |
| 124 | + * Match between a device ID retrieved through the READ_ID command and an |
| 125 | * entry in the SPI NAND description table. If a match is found, the spinand |
| 126 | * object will be initialized with information provided by the matching |
| 127 | * spinand_info entry. |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 128 | @@ -844,8 +884,10 @@ spinand_select_op_variant(struct spinand_device *spinand, |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 129 | */ |
| 130 | int spinand_match_and_init(struct spinand_device *spinand, |
| 131 | const struct spinand_info *table, |
| 132 | - unsigned int table_size, u16 devid) |
| 133 | + unsigned int table_size, |
| 134 | + enum spinand_readid_method rdid_method) |
| 135 | { |
| 136 | + u8 *id = spinand->id.data; |
| 137 | struct nand_device *nand = spinand_to_nand(spinand); |
| 138 | unsigned int i; |
| 139 | |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 140 | @@ -853,13 +895,17 @@ int spinand_match_and_init(struct spinand_device *spinand, |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 141 | const struct spinand_info *info = &table[i]; |
| 142 | const struct spi_mem_op *op; |
| 143 | |
| 144 | - if (devid != info->devid) |
| 145 | + if (rdid_method != info->devid.method) |
| 146 | + continue; |
| 147 | + |
| 148 | + if (memcmp(id + 1, info->devid.id, info->devid.len)) |
| 149 | continue; |
| 150 | |
| 151 | nand->memorg = table[i].memorg; |
| 152 | nand->eccreq = table[i].eccreq; |
| 153 | spinand->eccinfo = table[i].eccinfo; |
| 154 | spinand->flags = table[i].flags; |
| 155 | + spinand->id.len = 1 + table[i].devid.len; |
| 156 | spinand->select_target = table[i].select_target; |
| 157 | |
| 158 | op = spinand_select_op_variant(spinand, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 159 | @@ -896,13 +942,7 @@ static int spinand_detect(struct spinand_device *spinand) |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 160 | if (ret) |
| 161 | return ret; |
| 162 | |
| 163 | - ret = spinand_read_id_op(spinand, spinand->id.data); |
| 164 | - if (ret) |
| 165 | - return ret; |
| 166 | - |
| 167 | - spinand->id.len = SPINAND_MAX_ID_LEN; |
| 168 | - |
| 169 | - ret = spinand_manufacturer_detect(spinand); |
| 170 | + ret = spinand_id_detect(spinand); |
| 171 | if (ret) { |
| 172 | dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN, |
| 173 | spinand->id.data); |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 174 | diff --git a/drivers/mtd/nand/spi/macronix.c b/drivers/mtd/nand/spi/macronix.c |
| 175 | index 21def3f8f..0f900f3aa 100644 |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 176 | --- a/drivers/mtd/nand/spi/macronix.c |
| 177 | +++ b/drivers/mtd/nand/spi/macronix.c |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 178 | @@ -99,7 +99,8 @@ static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand, |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static const struct spinand_info macronix_spinand_table[] = { |
| 182 | - SPINAND_INFO("MX35LF1GE4AB", 0x12, |
| 183 | + SPINAND_INFO("MX35LF1GE4AB", |
| 184 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x12), |
| 185 | NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1), |
| 186 | NAND_ECCREQ(4, 512), |
| 187 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 188 | @@ -108,7 +109,8 @@ static const struct spinand_info macronix_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 189 | SPINAND_HAS_QE_BIT, |
| 190 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, |
| 191 | mx35lf1ge4ab_ecc_get_status)), |
| 192 | - SPINAND_INFO("MX35LF2GE4AB", 0x22, |
| 193 | + SPINAND_INFO("MX35LF2GE4AB", |
| 194 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x22), |
| 195 | NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 2, 1, 1), |
| 196 | NAND_ECCREQ(4, 512), |
| 197 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 198 | @@ -118,33 +120,13 @@ static const struct spinand_info macronix_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 199 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)), |
| 200 | }; |
| 201 | |
| 202 | -static int macronix_spinand_detect(struct spinand_device *spinand) |
| 203 | -{ |
| 204 | - u8 *id = spinand->id.data; |
| 205 | - int ret; |
| 206 | - |
| 207 | - /* |
| 208 | - * Macronix SPI NAND read ID needs a dummy byte, so the first byte in |
| 209 | - * raw_id is garbage. |
| 210 | - */ |
| 211 | - if (id[1] != SPINAND_MFR_MACRONIX) |
| 212 | - return 0; |
| 213 | - |
| 214 | - ret = spinand_match_and_init(spinand, macronix_spinand_table, |
| 215 | - ARRAY_SIZE(macronix_spinand_table), |
| 216 | - id[2]); |
| 217 | - if (ret) |
| 218 | - return ret; |
| 219 | - |
| 220 | - return 1; |
| 221 | -} |
| 222 | - |
| 223 | static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = { |
| 224 | - .detect = macronix_spinand_detect, |
| 225 | }; |
| 226 | |
| 227 | const struct spinand_manufacturer macronix_spinand_manufacturer = { |
| 228 | .id = SPINAND_MFR_MACRONIX, |
| 229 | .name = "Macronix", |
| 230 | + .chips = macronix_spinand_table, |
| 231 | + .nchips = ARRAY_SIZE(macronix_spinand_table), |
| 232 | .ops = ¯onix_spinand_manuf_ops, |
| 233 | }; |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 234 | diff --git a/drivers/mtd/nand/spi/micron.c b/drivers/mtd/nand/spi/micron.c |
| 235 | index 7d7b1f7fc..f56f81325 100644 |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 236 | --- a/drivers/mtd/nand/spi/micron.c |
| 237 | +++ b/drivers/mtd/nand/spi/micron.c |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 238 | @@ -91,7 +91,8 @@ static int mt29f2g01abagd_ecc_get_status(struct spinand_device *spinand, |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static const struct spinand_info micron_spinand_table[] = { |
| 242 | - SPINAND_INFO("MT29F2G01ABAGD", 0x24, |
| 243 | + SPINAND_INFO("MT29F2G01ABAGD", |
| 244 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x24), |
| 245 | NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 2, 1, 1), |
| 246 | NAND_ECCREQ(8, 512), |
| 247 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 248 | @@ -102,32 +103,13 @@ static const struct spinand_info micron_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 249 | mt29f2g01abagd_ecc_get_status)), |
| 250 | }; |
| 251 | |
| 252 | -static int micron_spinand_detect(struct spinand_device *spinand) |
| 253 | -{ |
| 254 | - u8 *id = spinand->id.data; |
| 255 | - int ret; |
| 256 | - |
| 257 | - /* |
| 258 | - * Micron SPI NAND read ID need a dummy byte, |
| 259 | - * so the first byte in raw_id is dummy. |
| 260 | - */ |
| 261 | - if (id[1] != SPINAND_MFR_MICRON) |
| 262 | - return 0; |
| 263 | - |
| 264 | - ret = spinand_match_and_init(spinand, micron_spinand_table, |
| 265 | - ARRAY_SIZE(micron_spinand_table), id[2]); |
| 266 | - if (ret) |
| 267 | - return ret; |
| 268 | - |
| 269 | - return 1; |
| 270 | -} |
| 271 | - |
| 272 | static const struct spinand_manufacturer_ops micron_spinand_manuf_ops = { |
| 273 | - .detect = micron_spinand_detect, |
| 274 | }; |
| 275 | |
| 276 | const struct spinand_manufacturer micron_spinand_manufacturer = { |
| 277 | .id = SPINAND_MFR_MICRON, |
| 278 | .name = "Micron", |
| 279 | + .chips = micron_spinand_table, |
| 280 | + .nchips = ARRAY_SIZE(micron_spinand_table), |
| 281 | .ops = µn_spinand_manuf_ops, |
| 282 | }; |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 283 | diff --git a/drivers/mtd/nand/spi/paragon.c b/drivers/mtd/nand/spi/paragon.c |
| 284 | index 52307681c..519ade513 100644 |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 285 | --- a/drivers/mtd/nand/spi/paragon.c |
| 286 | +++ b/drivers/mtd/nand/spi/paragon.c |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 287 | @@ -97,7 +97,8 @@ static const struct mtd_ooblayout_ops pn26g0xa_ooblayout = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 288 | |
| 289 | |
| 290 | static const struct spinand_info paragon_spinand_table[] = { |
| 291 | - SPINAND_INFO("PN26G01A", 0xe1, |
| 292 | + SPINAND_INFO("PN26G01A", |
| 293 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xe1), |
| 294 | NAND_MEMORG(1, 2048, 128, 64, 1024, 21, 1, 1, 1), |
| 295 | NAND_ECCREQ(8, 512), |
| 296 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 297 | @@ -106,7 +107,8 @@ static const struct spinand_info paragon_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 298 | 0, |
| 299 | SPINAND_ECCINFO(&pn26g0xa_ooblayout, |
| 300 | pn26g0xa_ecc_get_status)), |
| 301 | - SPINAND_INFO("PN26G02A", 0xe2, |
| 302 | + SPINAND_INFO("PN26G02A", |
| 303 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xe2), |
| 304 | NAND_MEMORG(1, 2048, 128, 64, 2048, 41, 1, 1, 1), |
| 305 | NAND_ECCREQ(8, 512), |
| 306 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 307 | @@ -117,31 +119,13 @@ static const struct spinand_info paragon_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 308 | pn26g0xa_ecc_get_status)), |
| 309 | }; |
| 310 | |
| 311 | -static int paragon_spinand_detect(struct spinand_device *spinand) |
| 312 | -{ |
| 313 | - u8 *id = spinand->id.data; |
| 314 | - int ret; |
| 315 | - |
| 316 | - /* Read ID returns [0][MID][DID] */ |
| 317 | - |
| 318 | - if (id[1] != SPINAND_MFR_PARAGON) |
| 319 | - return 0; |
| 320 | - |
| 321 | - ret = spinand_match_and_init(spinand, paragon_spinand_table, |
| 322 | - ARRAY_SIZE(paragon_spinand_table), |
| 323 | - id[2]); |
| 324 | - if (ret) |
| 325 | - return ret; |
| 326 | - |
| 327 | - return 1; |
| 328 | -} |
| 329 | - |
| 330 | static const struct spinand_manufacturer_ops paragon_spinand_manuf_ops = { |
| 331 | - .detect = paragon_spinand_detect, |
| 332 | }; |
| 333 | |
| 334 | const struct spinand_manufacturer paragon_spinand_manufacturer = { |
| 335 | .id = SPINAND_MFR_PARAGON, |
| 336 | .name = "Paragon", |
| 337 | + .chips = paragon_spinand_table, |
| 338 | + .nchips = ARRAY_SIZE(paragon_spinand_table), |
| 339 | .ops = ¶gon_spinand_manuf_ops, |
| 340 | }; |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 341 | diff --git a/drivers/mtd/nand/spi/toshiba.c b/drivers/mtd/nand/spi/toshiba.c |
| 342 | index 1cb3760ff..35da3c6e9 100644 |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 343 | --- a/drivers/mtd/nand/spi/toshiba.c |
| 344 | +++ b/drivers/mtd/nand/spi/toshiba.c |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 345 | @@ -95,7 +95,8 @@ static int tc58cxgxsx_ecc_get_status(struct spinand_device *spinand, |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 346 | |
| 347 | static const struct spinand_info toshiba_spinand_table[] = { |
| 348 | /* 3.3V 1Gb */ |
| 349 | - SPINAND_INFO("TC58CVG0S3", 0xC2, |
| 350 | + SPINAND_INFO("TC58CVG0S3", |
| 351 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xC2), |
| 352 | NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1), |
| 353 | NAND_ECCREQ(8, 512), |
| 354 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 355 | @@ -105,7 +106,8 @@ static const struct spinand_info toshiba_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 356 | SPINAND_ECCINFO(&tc58cxgxsx_ooblayout, |
| 357 | tc58cxgxsx_ecc_get_status)), |
| 358 | /* 3.3V 2Gb */ |
| 359 | - SPINAND_INFO("TC58CVG1S3", 0xCB, |
| 360 | + SPINAND_INFO("TC58CVG1S3", |
| 361 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xCB), |
| 362 | NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1), |
| 363 | NAND_ECCREQ(8, 512), |
| 364 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 365 | @@ -115,7 +117,8 @@ static const struct spinand_info toshiba_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 366 | SPINAND_ECCINFO(&tc58cxgxsx_ooblayout, |
| 367 | tc58cxgxsx_ecc_get_status)), |
| 368 | /* 3.3V 4Gb */ |
| 369 | - SPINAND_INFO("TC58CVG2S0", 0xCD, |
| 370 | + SPINAND_INFO("TC58CVG2S0", |
| 371 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xCD), |
| 372 | NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1), |
| 373 | NAND_ECCREQ(8, 512), |
| 374 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 375 | @@ -125,7 +128,8 @@ static const struct spinand_info toshiba_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 376 | SPINAND_ECCINFO(&tc58cxgxsx_ooblayout, |
| 377 | tc58cxgxsx_ecc_get_status)), |
| 378 | /* 1.8V 1Gb */ |
| 379 | - SPINAND_INFO("TC58CYG0S3", 0xB2, |
| 380 | + SPINAND_INFO("TC58CYG0S3", |
| 381 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xB2), |
| 382 | NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1), |
| 383 | NAND_ECCREQ(8, 512), |
| 384 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 385 | @@ -135,7 +139,8 @@ static const struct spinand_info toshiba_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 386 | SPINAND_ECCINFO(&tc58cxgxsx_ooblayout, |
| 387 | tc58cxgxsx_ecc_get_status)), |
| 388 | /* 1.8V 2Gb */ |
| 389 | - SPINAND_INFO("TC58CYG1S3", 0xBB, |
| 390 | + SPINAND_INFO("TC58CYG1S3", |
| 391 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xBB), |
| 392 | NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1), |
| 393 | NAND_ECCREQ(8, 512), |
| 394 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 395 | @@ -145,7 +150,8 @@ static const struct spinand_info toshiba_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 396 | SPINAND_ECCINFO(&tc58cxgxsx_ooblayout, |
| 397 | tc58cxgxsx_ecc_get_status)), |
| 398 | /* 1.8V 4Gb */ |
| 399 | - SPINAND_INFO("TC58CYG2S0", 0xBD, |
| 400 | + SPINAND_INFO("TC58CYG2S0", |
| 401 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xBD), |
| 402 | NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1), |
| 403 | NAND_ECCREQ(8, 512), |
| 404 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 405 | @@ -156,33 +162,13 @@ static const struct spinand_info toshiba_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 406 | tc58cxgxsx_ecc_get_status)), |
| 407 | }; |
| 408 | |
| 409 | -static int toshiba_spinand_detect(struct spinand_device *spinand) |
| 410 | -{ |
| 411 | - u8 *id = spinand->id.data; |
| 412 | - int ret; |
| 413 | - |
| 414 | - /* |
| 415 | - * Toshiba SPI NAND read ID needs a dummy byte, |
| 416 | - * so the first byte in id is garbage. |
| 417 | - */ |
| 418 | - if (id[1] != SPINAND_MFR_TOSHIBA) |
| 419 | - return 0; |
| 420 | - |
| 421 | - ret = spinand_match_and_init(spinand, toshiba_spinand_table, |
| 422 | - ARRAY_SIZE(toshiba_spinand_table), |
| 423 | - id[2]); |
| 424 | - if (ret) |
| 425 | - return ret; |
| 426 | - |
| 427 | - return 1; |
| 428 | -} |
| 429 | - |
| 430 | static const struct spinand_manufacturer_ops toshiba_spinand_manuf_ops = { |
| 431 | - .detect = toshiba_spinand_detect, |
| 432 | }; |
| 433 | |
| 434 | const struct spinand_manufacturer toshiba_spinand_manufacturer = { |
| 435 | .id = SPINAND_MFR_TOSHIBA, |
| 436 | .name = "Toshiba", |
| 437 | + .chips = toshiba_spinand_table, |
| 438 | + .nchips = ARRAY_SIZE(toshiba_spinand_table), |
| 439 | .ops = &toshiba_spinand_manuf_ops, |
| 440 | }; |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 441 | diff --git a/drivers/mtd/nand/spi/winbond.c b/drivers/mtd/nand/spi/winbond.c |
| 442 | index a6c17e0ca..766844283 100644 |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 443 | --- a/drivers/mtd/nand/spi/winbond.c |
| 444 | +++ b/drivers/mtd/nand/spi/winbond.c |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 445 | @@ -75,7 +75,8 @@ static int w25m02gv_select_target(struct spinand_device *spinand, |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static const struct spinand_info winbond_spinand_table[] = { |
| 449 | - SPINAND_INFO("W25M02GV", 0xAB, |
| 450 | + SPINAND_INFO("W25M02GV", |
| 451 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xab), |
| 452 | NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 2), |
| 453 | NAND_ECCREQ(1, 512), |
| 454 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 455 | @@ -84,7 +85,8 @@ static const struct spinand_info winbond_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 456 | 0, |
| 457 | SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL), |
| 458 | SPINAND_SELECT_TARGET(w25m02gv_select_target)), |
| 459 | - SPINAND_INFO("W25N01GV", 0xAA, |
| 460 | + SPINAND_INFO("W25N01GV", |
| 461 | + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xaa), |
| 462 | NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1), |
| 463 | NAND_ECCREQ(1, 512), |
| 464 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 465 | @@ -94,31 +96,6 @@ static const struct spinand_info winbond_spinand_table[] = { |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 466 | SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)), |
| 467 | }; |
| 468 | |
| 469 | -/** |
| 470 | - * winbond_spinand_detect - initialize device related part in spinand_device |
| 471 | - * struct if it is a Winbond device. |
| 472 | - * @spinand: SPI NAND device structure |
| 473 | - */ |
| 474 | -static int winbond_spinand_detect(struct spinand_device *spinand) |
| 475 | -{ |
| 476 | - u8 *id = spinand->id.data; |
| 477 | - int ret; |
| 478 | - |
| 479 | - /* |
| 480 | - * Winbond SPI NAND read ID need a dummy byte, |
| 481 | - * so the first byte in raw_id is dummy. |
| 482 | - */ |
| 483 | - if (id[1] != SPINAND_MFR_WINBOND) |
| 484 | - return 0; |
| 485 | - |
| 486 | - ret = spinand_match_and_init(spinand, winbond_spinand_table, |
| 487 | - ARRAY_SIZE(winbond_spinand_table), id[2]); |
| 488 | - if (ret) |
| 489 | - return ret; |
| 490 | - |
| 491 | - return 1; |
| 492 | -} |
| 493 | - |
| 494 | static int winbond_spinand_init(struct spinand_device *spinand) |
| 495 | { |
| 496 | struct nand_device *nand = spinand_to_nand(spinand); |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 497 | @@ -138,12 +115,13 @@ static int winbond_spinand_init(struct spinand_device *spinand) |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static const struct spinand_manufacturer_ops winbond_spinand_manuf_ops = { |
| 501 | - .detect = winbond_spinand_detect, |
| 502 | .init = winbond_spinand_init, |
| 503 | }; |
| 504 | |
| 505 | const struct spinand_manufacturer winbond_spinand_manufacturer = { |
| 506 | .id = SPINAND_MFR_WINBOND, |
| 507 | .name = "Winbond", |
| 508 | + .chips = winbond_spinand_table, |
| 509 | + .nchips = ARRAY_SIZE(winbond_spinand_table), |
| 510 | .ops = &winbond_spinand_manuf_ops, |
| 511 | }; |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 512 | diff --git a/include/linux/mtd/spinand.h b/include/linux/mtd/spinand.h |
| 513 | index 4ea558bd3..f4c4ae871 100644 |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 514 | --- a/include/linux/mtd/spinand.h |
| 515 | +++ b/include/linux/mtd/spinand.h |
| 516 | @@ -32,9 +32,9 @@ |
| 517 | SPI_MEM_OP_NO_DUMMY, \ |
| 518 | SPI_MEM_OP_NO_DATA) |
| 519 | |
| 520 | -#define SPINAND_READID_OP(ndummy, buf, len) \ |
| 521 | +#define SPINAND_READID_OP(naddr, ndummy, buf, len) \ |
| 522 | SPI_MEM_OP(SPI_MEM_OP_CMD(0x9f, 1), \ |
| 523 | - SPI_MEM_OP_NO_ADDR, \ |
| 524 | + SPI_MEM_OP_ADDR(naddr, 0, 1), \ |
| 525 | SPI_MEM_OP_DUMMY(ndummy, 1), \ |
| 526 | SPI_MEM_OP_DATA_IN(len, buf, 1)) |
| 527 | |
| 528 | @@ -176,37 +176,46 @@ struct spinand_device; |
| 529 | * @data: buffer containing the id bytes. Currently 4 bytes large, but can |
| 530 | * be extended if required |
| 531 | * @len: ID length |
| 532 | - * |
| 533 | - * struct_spinand_id->data contains all bytes returned after a READ_ID command, |
| 534 | - * including dummy bytes if the chip does not emit ID bytes right after the |
| 535 | - * READ_ID command. The responsibility to extract real ID bytes is left to |
| 536 | - * struct_manufacurer_ops->detect(). |
| 537 | */ |
| 538 | struct spinand_id { |
| 539 | u8 data[SPINAND_MAX_ID_LEN]; |
| 540 | int len; |
| 541 | }; |
| 542 | |
| 543 | +enum spinand_readid_method { |
| 544 | + SPINAND_READID_METHOD_OPCODE, |
| 545 | + SPINAND_READID_METHOD_OPCODE_ADDR, |
| 546 | + SPINAND_READID_METHOD_OPCODE_DUMMY, |
| 547 | +}; |
| 548 | + |
| 549 | +/** |
| 550 | + * struct spinand_devid - SPI NAND device id structure |
| 551 | + * @id: device id of current chip |
| 552 | + * @len: number of bytes in device id |
| 553 | + * @method: method to read chip id |
| 554 | + * There are 3 possible variants: |
| 555 | + * SPINAND_READID_METHOD_OPCODE: chip id is returned immediately |
| 556 | + * after read_id opcode. |
| 557 | + * SPINAND_READID_METHOD_OPCODE_ADDR: chip id is returned after |
| 558 | + * read_id opcode + 1-byte address. |
| 559 | + * SPINAND_READID_METHOD_OPCODE_DUMMY: chip id is returned after |
| 560 | + * read_id opcode + 1 dummy byte. |
| 561 | + */ |
| 562 | +struct spinand_devid { |
| 563 | + const u8 *id; |
| 564 | + const u8 len; |
| 565 | + const enum spinand_readid_method method; |
| 566 | +}; |
| 567 | + |
| 568 | /** |
| 569 | * struct manufacurer_ops - SPI NAND manufacturer specific operations |
| 570 | - * @detect: detect a SPI NAND device. Every time a SPI NAND device is probed |
| 571 | - * the core calls the struct_manufacurer_ops->detect() hook of each |
| 572 | - * registered manufacturer until one of them return 1. Note that |
| 573 | - * the first thing to check in this hook is that the manufacturer ID |
| 574 | - * in struct_spinand_device->id matches the manufacturer whose |
| 575 | - * ->detect() hook has been called. Should return 1 if there's a |
| 576 | - * match, 0 if the manufacturer ID does not match and a negative |
| 577 | - * error code otherwise. When true is returned, the core assumes |
| 578 | - * that properties of the NAND chip (spinand->base.memorg and |
| 579 | - * spinand->base.eccreq) have been filled |
| 580 | * @init: initialize a SPI NAND device |
| 581 | * @cleanup: cleanup a SPI NAND device |
| 582 | * |
| 583 | * Each SPI NAND manufacturer driver should implement this interface so that |
| 584 | - * NAND chips coming from this vendor can be detected and initialized properly. |
| 585 | + * NAND chips coming from this vendor can be initialized properly. |
| 586 | */ |
| 587 | struct spinand_manufacturer_ops { |
| 588 | - int (*detect)(struct spinand_device *spinand); |
| 589 | int (*init)(struct spinand_device *spinand); |
| 590 | void (*cleanup)(struct spinand_device *spinand); |
| 591 | }; |
| 592 | @@ -215,11 +224,16 @@ struct spinand_manufacturer_ops { |
| 593 | * struct spinand_manufacturer - SPI NAND manufacturer instance |
| 594 | * @id: manufacturer ID |
| 595 | * @name: manufacturer name |
| 596 | + * @devid_len: number of bytes in device ID |
| 597 | + * @chips: supported SPI NANDs under current manufacturer |
| 598 | + * @nchips: number of SPI NANDs available in chips array |
| 599 | * @ops: manufacturer operations |
| 600 | */ |
| 601 | struct spinand_manufacturer { |
| 602 | u8 id; |
| 603 | char *name; |
| 604 | + const struct spinand_info *chips; |
| 605 | + const size_t nchips; |
| 606 | const struct spinand_manufacturer_ops *ops; |
| 607 | }; |
| 608 | |
| 609 | @@ -291,7 +305,7 @@ struct spinand_ecc_info { |
| 610 | */ |
| 611 | struct spinand_info { |
| 612 | const char *model; |
| 613 | - u16 devid; |
| 614 | + struct spinand_devid devid; |
| 615 | u32 flags; |
| 616 | struct nand_memory_organization memorg; |
| 617 | struct nand_ecc_req eccreq; |
| 618 | @@ -305,6 +319,13 @@ struct spinand_info { |
| 619 | unsigned int target); |
| 620 | }; |
| 621 | |
| 622 | +#define SPINAND_ID(__method, ...) \ |
| 623 | + { \ |
| 624 | + .id = (const u8[]){ __VA_ARGS__ }, \ |
| 625 | + .len = sizeof((u8[]){ __VA_ARGS__ }), \ |
| 626 | + .method = __method, \ |
| 627 | + } |
| 628 | + |
| 629 | #define SPINAND_INFO_OP_VARIANTS(__read, __write, __update) \ |
| 630 | { \ |
| 631 | .read_cache = __read, \ |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 632 | @@ -451,9 +472,10 @@ static inline void spinand_set_of_node(struct spinand_device *spinand, |
developer | 41370d5 | 2022-03-16 16:01:59 +0800 | [diff] [blame] | 633 | nanddev_set_of_node(&spinand->base, np); |
| 634 | } |
| 635 | |
| 636 | -int spinand_match_and_init(struct spinand_device *dev, |
| 637 | +int spinand_match_and_init(struct spinand_device *spinand, |
| 638 | const struct spinand_info *table, |
| 639 | - unsigned int table_size, u16 devid); |
| 640 | + unsigned int table_size, |
| 641 | + enum spinand_readid_method rdid_method); |
| 642 | |
| 643 | int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val); |
| 644 | int spinand_select_target(struct spinand_device *spinand, unsigned int target); |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 645 | -- |
| 646 | 2.34.1 |
| 647 | |