11075492bSFrieder Schrempf // SPDX-License-Identifier: GPL-2.0
21075492bSFrieder Schrempf /*
31075492bSFrieder Schrempf * Copyright (c) 2017 exceet electronics GmbH
41075492bSFrieder Schrempf *
51075492bSFrieder Schrempf * Authors:
61075492bSFrieder Schrempf * Frieder Schrempf <frieder.schrempf@exceet.de>
71075492bSFrieder Schrempf * Boris Brezillon <boris.brezillon@bootlin.com>
81075492bSFrieder Schrempf */
91075492bSFrieder Schrempf
101075492bSFrieder Schrempf #include <linux/device.h>
111075492bSFrieder Schrempf #include <linux/kernel.h>
121075492bSFrieder Schrempf #include <linux/mtd/spinand.h>
131075492bSFrieder Schrempf
141075492bSFrieder Schrempf #define SPINAND_MFR_WINBOND 0xEF
151075492bSFrieder Schrempf
161075492bSFrieder Schrempf #define WINBOND_CFG_BUF_READ BIT(3)
171075492bSFrieder Schrempf
18e0ccf861SZhi-Jun You #define W25N04KV_STATUS_ECC_5_8_BITFLIPS (3 << 4)
19e0ccf861SZhi-Jun You
201075492bSFrieder Schrempf static SPINAND_OP_VARIANTS(read_cache_variants,
211075492bSFrieder Schrempf SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
221075492bSFrieder Schrempf SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
231075492bSFrieder Schrempf SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
241075492bSFrieder Schrempf SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
251075492bSFrieder Schrempf SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
261075492bSFrieder Schrempf SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
271075492bSFrieder Schrempf
281075492bSFrieder Schrempf static SPINAND_OP_VARIANTS(write_cache_variants,
291075492bSFrieder Schrempf SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
301075492bSFrieder Schrempf SPINAND_PROG_LOAD(true, 0, NULL, 0));
311075492bSFrieder Schrempf
321075492bSFrieder Schrempf static SPINAND_OP_VARIANTS(update_cache_variants,
331075492bSFrieder Schrempf SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
341075492bSFrieder Schrempf SPINAND_PROG_LOAD(false, 0, NULL, 0));
351075492bSFrieder Schrempf
w25m02gv_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)361075492bSFrieder Schrempf static int w25m02gv_ooblayout_ecc(struct mtd_info *mtd, int section,
371075492bSFrieder Schrempf struct mtd_oob_region *region)
381075492bSFrieder Schrempf {
391075492bSFrieder Schrempf if (section > 3)
401075492bSFrieder Schrempf return -ERANGE;
411075492bSFrieder Schrempf
421075492bSFrieder Schrempf region->offset = (16 * section) + 8;
431075492bSFrieder Schrempf region->length = 8;
441075492bSFrieder Schrempf
451075492bSFrieder Schrempf return 0;
461075492bSFrieder Schrempf }
471075492bSFrieder Schrempf
w25m02gv_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)481075492bSFrieder Schrempf static int w25m02gv_ooblayout_free(struct mtd_info *mtd, int section,
491075492bSFrieder Schrempf struct mtd_oob_region *region)
501075492bSFrieder Schrempf {
511075492bSFrieder Schrempf if (section > 3)
521075492bSFrieder Schrempf return -ERANGE;
531075492bSFrieder Schrempf
541075492bSFrieder Schrempf region->offset = (16 * section) + 2;
551075492bSFrieder Schrempf region->length = 6;
561075492bSFrieder Schrempf
571075492bSFrieder Schrempf return 0;
581075492bSFrieder Schrempf }
591075492bSFrieder Schrempf
601075492bSFrieder Schrempf static const struct mtd_ooblayout_ops w25m02gv_ooblayout = {
611075492bSFrieder Schrempf .ecc = w25m02gv_ooblayout_ecc,
621075492bSFrieder Schrempf .free = w25m02gv_ooblayout_free,
631075492bSFrieder Schrempf };
641075492bSFrieder Schrempf
w25m02gv_select_target(struct spinand_device * spinand,unsigned int target)651075492bSFrieder Schrempf static int w25m02gv_select_target(struct spinand_device *spinand,
661075492bSFrieder Schrempf unsigned int target)
671075492bSFrieder Schrempf {
681075492bSFrieder Schrempf struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0xc2, 1),
691075492bSFrieder Schrempf SPI_MEM_OP_NO_ADDR,
701075492bSFrieder Schrempf SPI_MEM_OP_NO_DUMMY,
711075492bSFrieder Schrempf SPI_MEM_OP_DATA_OUT(1,
721075492bSFrieder Schrempf spinand->scratchbuf,
731075492bSFrieder Schrempf 1));
741075492bSFrieder Schrempf
751075492bSFrieder Schrempf *spinand->scratchbuf = target;
761075492bSFrieder Schrempf return spi_mem_exec_op(spinand->spimem, &op);
771075492bSFrieder Schrempf }
781075492bSFrieder Schrempf
w25n01kv_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)79*e2a9fcb3SRobert Marko static int w25n01kv_ooblayout_ecc(struct mtd_info *mtd, int section,
80*e2a9fcb3SRobert Marko struct mtd_oob_region *region)
81*e2a9fcb3SRobert Marko {
82*e2a9fcb3SRobert Marko if (section > 3)
83*e2a9fcb3SRobert Marko return -ERANGE;
84*e2a9fcb3SRobert Marko
85*e2a9fcb3SRobert Marko region->offset = 64 + (8 * section);
86*e2a9fcb3SRobert Marko region->length = 7;
87*e2a9fcb3SRobert Marko
88*e2a9fcb3SRobert Marko return 0;
89*e2a9fcb3SRobert Marko }
90*e2a9fcb3SRobert Marko
w25n02kv_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)916154c7a5SMikhail Kshevetskiy static int w25n02kv_ooblayout_ecc(struct mtd_info *mtd, int section,
926154c7a5SMikhail Kshevetskiy struct mtd_oob_region *region)
936154c7a5SMikhail Kshevetskiy {
946154c7a5SMikhail Kshevetskiy if (section > 3)
956154c7a5SMikhail Kshevetskiy return -ERANGE;
966154c7a5SMikhail Kshevetskiy
976154c7a5SMikhail Kshevetskiy region->offset = 64 + (16 * section);
986154c7a5SMikhail Kshevetskiy region->length = 13;
996154c7a5SMikhail Kshevetskiy
1006154c7a5SMikhail Kshevetskiy return 0;
1016154c7a5SMikhail Kshevetskiy }
1026154c7a5SMikhail Kshevetskiy
w25n02kv_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)1036154c7a5SMikhail Kshevetskiy static int w25n02kv_ooblayout_free(struct mtd_info *mtd, int section,
1046154c7a5SMikhail Kshevetskiy struct mtd_oob_region *region)
1056154c7a5SMikhail Kshevetskiy {
1066154c7a5SMikhail Kshevetskiy if (section > 3)
1076154c7a5SMikhail Kshevetskiy return -ERANGE;
1086154c7a5SMikhail Kshevetskiy
1096154c7a5SMikhail Kshevetskiy region->offset = (16 * section) + 2;
1106154c7a5SMikhail Kshevetskiy region->length = 14;
1116154c7a5SMikhail Kshevetskiy
1126154c7a5SMikhail Kshevetskiy return 0;
1136154c7a5SMikhail Kshevetskiy }
1146154c7a5SMikhail Kshevetskiy
115*e2a9fcb3SRobert Marko static const struct mtd_ooblayout_ops w25n01kv_ooblayout = {
116*e2a9fcb3SRobert Marko .ecc = w25n01kv_ooblayout_ecc,
117*e2a9fcb3SRobert Marko .free = w25n02kv_ooblayout_free,
118*e2a9fcb3SRobert Marko };
119*e2a9fcb3SRobert Marko
1206154c7a5SMikhail Kshevetskiy static const struct mtd_ooblayout_ops w25n02kv_ooblayout = {
1216154c7a5SMikhail Kshevetskiy .ecc = w25n02kv_ooblayout_ecc,
1226154c7a5SMikhail Kshevetskiy .free = w25n02kv_ooblayout_free,
1236154c7a5SMikhail Kshevetskiy };
1246154c7a5SMikhail Kshevetskiy
w25n02kv_ecc_get_status(struct spinand_device * spinand,u8 status)1256154c7a5SMikhail Kshevetskiy static int w25n02kv_ecc_get_status(struct spinand_device *spinand,
1266154c7a5SMikhail Kshevetskiy u8 status)
1276154c7a5SMikhail Kshevetskiy {
1286154c7a5SMikhail Kshevetskiy struct nand_device *nand = spinand_to_nand(spinand);
1296154c7a5SMikhail Kshevetskiy u8 mbf = 0;
130f5a05060SOlivier Maignial struct spi_mem_op op = SPINAND_GET_FEATURE_OP(0x30, spinand->scratchbuf);
1316154c7a5SMikhail Kshevetskiy
1326154c7a5SMikhail Kshevetskiy switch (status & STATUS_ECC_MASK) {
1336154c7a5SMikhail Kshevetskiy case STATUS_ECC_NO_BITFLIPS:
1346154c7a5SMikhail Kshevetskiy return 0;
1356154c7a5SMikhail Kshevetskiy
1366154c7a5SMikhail Kshevetskiy case STATUS_ECC_UNCOR_ERROR:
1376154c7a5SMikhail Kshevetskiy return -EBADMSG;
1386154c7a5SMikhail Kshevetskiy
1396154c7a5SMikhail Kshevetskiy case STATUS_ECC_HAS_BITFLIPS:
140e0ccf861SZhi-Jun You case W25N04KV_STATUS_ECC_5_8_BITFLIPS:
1416154c7a5SMikhail Kshevetskiy /*
1426154c7a5SMikhail Kshevetskiy * Let's try to retrieve the real maximum number of bitflips
1436154c7a5SMikhail Kshevetskiy * in order to avoid forcing the wear-leveling layer to move
1446154c7a5SMikhail Kshevetskiy * data around if it's not necessary.
1456154c7a5SMikhail Kshevetskiy */
1466154c7a5SMikhail Kshevetskiy if (spi_mem_exec_op(spinand->spimem, &op))
1476154c7a5SMikhail Kshevetskiy return nanddev_get_ecc_conf(nand)->strength;
1486154c7a5SMikhail Kshevetskiy
149f5a05060SOlivier Maignial mbf = *(spinand->scratchbuf) >> 4;
1506154c7a5SMikhail Kshevetskiy
1516154c7a5SMikhail Kshevetskiy if (WARN_ON(mbf > nanddev_get_ecc_conf(nand)->strength || !mbf))
1526154c7a5SMikhail Kshevetskiy return nanddev_get_ecc_conf(nand)->strength;
1536154c7a5SMikhail Kshevetskiy
1546154c7a5SMikhail Kshevetskiy return mbf;
1556154c7a5SMikhail Kshevetskiy
1566154c7a5SMikhail Kshevetskiy default:
1576154c7a5SMikhail Kshevetskiy break;
1586154c7a5SMikhail Kshevetskiy }
1596154c7a5SMikhail Kshevetskiy
1606154c7a5SMikhail Kshevetskiy return -EINVAL;
1616154c7a5SMikhail Kshevetskiy }
1626154c7a5SMikhail Kshevetskiy
1631075492bSFrieder Schrempf static const struct spinand_info winbond_spinand_table[] = {
164f1541773SChuanhong Guo SPINAND_INFO("W25M02GV",
165dbf70fc2SMikhail Kshevetskiy SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xab, 0x21),
166377e517bSBoris Brezillon NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 2),
1671075492bSFrieder Schrempf NAND_ECCREQ(1, 512),
1681075492bSFrieder Schrempf SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
1691075492bSFrieder Schrempf &write_cache_variants,
1701075492bSFrieder Schrempf &update_cache_variants),
1711075492bSFrieder Schrempf 0,
1721075492bSFrieder Schrempf SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
1731075492bSFrieder Schrempf SPINAND_SELECT_TARGET(w25m02gv_select_target)),
174f1541773SChuanhong Guo SPINAND_INFO("W25N01GV",
175dbf70fc2SMikhail Kshevetskiy SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xaa, 0x21),
176377e517bSBoris Brezillon NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
1779a4d8307SRobert Marko NAND_ECCREQ(1, 512),
1789a4d8307SRobert Marko SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
1799a4d8307SRobert Marko &write_cache_variants,
1809a4d8307SRobert Marko &update_cache_variants),
1819a4d8307SRobert Marko 0,
1829a4d8307SRobert Marko SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)),
183*e2a9fcb3SRobert Marko SPINAND_INFO("W25N01KV",
184*e2a9fcb3SRobert Marko SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xae, 0x21),
185*e2a9fcb3SRobert Marko NAND_MEMORG(1, 2048, 96, 64, 1024, 20, 1, 1, 1),
186*e2a9fcb3SRobert Marko NAND_ECCREQ(4, 512),
187*e2a9fcb3SRobert Marko SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
188*e2a9fcb3SRobert Marko &write_cache_variants,
189*e2a9fcb3SRobert Marko &update_cache_variants),
190*e2a9fcb3SRobert Marko 0,
191*e2a9fcb3SRobert Marko SPINAND_ECCINFO(&w25n01kv_ooblayout, w25n02kv_ecc_get_status)),
1926154c7a5SMikhail Kshevetskiy SPINAND_INFO("W25N02KV",
1936154c7a5SMikhail Kshevetskiy SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xaa, 0x22),
1946154c7a5SMikhail Kshevetskiy NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
1956154c7a5SMikhail Kshevetskiy NAND_ECCREQ(8, 512),
1966154c7a5SMikhail Kshevetskiy SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
1976154c7a5SMikhail Kshevetskiy &write_cache_variants,
1986154c7a5SMikhail Kshevetskiy &update_cache_variants),
1996154c7a5SMikhail Kshevetskiy 0,
2006154c7a5SMikhail Kshevetskiy SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
2016a804fb7SSridharan S N SPINAND_INFO("W25N01JW",
2026a804fb7SSridharan S N SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xbc, 0x21),
2036a804fb7SSridharan S N NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
2046a804fb7SSridharan S N NAND_ECCREQ(4, 512),
2056a804fb7SSridharan S N SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
2066a804fb7SSridharan S N &write_cache_variants,
2076a804fb7SSridharan S N &update_cache_variants),
2086a804fb7SSridharan S N 0,
2096a804fb7SSridharan S N SPINAND_ECCINFO(&w25m02gv_ooblayout, w25n02kv_ecc_get_status)),
2106a804fb7SSridharan S N SPINAND_INFO("W25N02JWZEIF",
2116a804fb7SSridharan S N SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xbf, 0x22),
2126a804fb7SSridharan S N NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 2, 1),
2136a804fb7SSridharan S N NAND_ECCREQ(4, 512),
2146a804fb7SSridharan S N SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
2156a804fb7SSridharan S N &write_cache_variants,
2166a804fb7SSridharan S N &update_cache_variants),
2176a804fb7SSridharan S N 0,
2186a804fb7SSridharan S N SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
2196a804fb7SSridharan S N SPINAND_INFO("W25N512GW",
2206a804fb7SSridharan S N SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xba, 0x20),
2216a804fb7SSridharan S N NAND_MEMORG(1, 2048, 64, 64, 512, 10, 1, 1, 1),
2226a804fb7SSridharan S N NAND_ECCREQ(4, 512),
2236a804fb7SSridharan S N SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
2246a804fb7SSridharan S N &write_cache_variants,
2256a804fb7SSridharan S N &update_cache_variants),
2266a804fb7SSridharan S N 0,
2276a804fb7SSridharan S N SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
2286a804fb7SSridharan S N SPINAND_INFO("W25N02KWZEIR",
2296a804fb7SSridharan S N SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xba, 0x22),
2306a804fb7SSridharan S N NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
2316a804fb7SSridharan S N NAND_ECCREQ(8, 512),
2326a804fb7SSridharan S N SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
2336a804fb7SSridharan S N &write_cache_variants,
2346a804fb7SSridharan S N &update_cache_variants),
2356a804fb7SSridharan S N 0,
2366a804fb7SSridharan S N SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
2376a804fb7SSridharan S N SPINAND_INFO("W25N01GWZEIG",
2386a804fb7SSridharan S N SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xba, 0x21),
2396a804fb7SSridharan S N NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
2406a804fb7SSridharan S N NAND_ECCREQ(4, 512),
2416a804fb7SSridharan S N SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
2426a804fb7SSridharan S N &write_cache_variants,
2436a804fb7SSridharan S N &update_cache_variants),
2446a804fb7SSridharan S N 0,
2456a804fb7SSridharan S N SPINAND_ECCINFO(&w25m02gv_ooblayout, w25n02kv_ecc_get_status)),
246e0ccf861SZhi-Jun You SPINAND_INFO("W25N04KV",
247e0ccf861SZhi-Jun You SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xaa, 0x23),
248e0ccf861SZhi-Jun You NAND_MEMORG(1, 2048, 128, 64, 4096, 40, 2, 1, 1),
249e0ccf861SZhi-Jun You NAND_ECCREQ(8, 512),
250e0ccf861SZhi-Jun You SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
251e0ccf861SZhi-Jun You &write_cache_variants,
252e0ccf861SZhi-Jun You &update_cache_variants),
253e0ccf861SZhi-Jun You 0,
254e0ccf861SZhi-Jun You SPINAND_ECCINFO(&w25n02kv_ooblayout, w25n02kv_ecc_get_status)),
2551075492bSFrieder Schrempf };
2561075492bSFrieder Schrempf
winbond_spinand_init(struct spinand_device * spinand)2571075492bSFrieder Schrempf static int winbond_spinand_init(struct spinand_device *spinand)
2581075492bSFrieder Schrempf {
2591075492bSFrieder Schrempf struct nand_device *nand = spinand_to_nand(spinand);
2601075492bSFrieder Schrempf unsigned int i;
2611075492bSFrieder Schrempf
2621075492bSFrieder Schrempf /*
2631075492bSFrieder Schrempf * Make sure all dies are in buffer read mode and not continuous read
2641075492bSFrieder Schrempf * mode.
2651075492bSFrieder Schrempf */
2661075492bSFrieder Schrempf for (i = 0; i < nand->memorg.ntargets; i++) {
2671075492bSFrieder Schrempf spinand_select_target(spinand, i);
2681075492bSFrieder Schrempf spinand_upd_cfg(spinand, WINBOND_CFG_BUF_READ,
2691075492bSFrieder Schrempf WINBOND_CFG_BUF_READ);
2701075492bSFrieder Schrempf }
2711075492bSFrieder Schrempf
2721075492bSFrieder Schrempf return 0;
2731075492bSFrieder Schrempf }
2741075492bSFrieder Schrempf
2751075492bSFrieder Schrempf static const struct spinand_manufacturer_ops winbond_spinand_manuf_ops = {
2761075492bSFrieder Schrempf .init = winbond_spinand_init,
2771075492bSFrieder Schrempf };
2781075492bSFrieder Schrempf
2791075492bSFrieder Schrempf const struct spinand_manufacturer winbond_spinand_manufacturer = {
2801075492bSFrieder Schrempf .id = SPINAND_MFR_WINBOND,
2811075492bSFrieder Schrempf .name = "Winbond",
282f1541773SChuanhong Guo .chips = winbond_spinand_table,
283f1541773SChuanhong Guo .nchips = ARRAY_SIZE(winbond_spinand_table),
2841075492bSFrieder Schrempf .ops = &winbond_spinand_manuf_ops,
2851075492bSFrieder Schrempf };
286