|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright (c) 2018 Macronix |
| 4 | + * |
| 5 | + * Author: Boris Brezillon <boris.brezillon@bootlin.com> |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/device.h> |
| 9 | +#include <linux/kernel.h> |
| 10 | +#include <linux/mtd/spinand.h> |
| 11 | + |
| 12 | +#define SPINAND_MFR_MACRONIX 0xC2 |
| 13 | + |
| 14 | +static SPINAND_OP_VARIANTS(read_cache_variants, |
| 15 | + SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0), |
| 16 | + SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0), |
| 17 | + SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0), |
| 18 | + SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0)); |
| 19 | + |
| 20 | +static SPINAND_OP_VARIANTS(write_cache_variants, |
| 21 | + SPINAND_PROG_LOAD_X4(true, 0, NULL, 0), |
| 22 | + SPINAND_PROG_LOAD(true, 0, NULL, 0)); |
| 23 | + |
| 24 | +static SPINAND_OP_VARIANTS(update_cache_variants, |
| 25 | + SPINAND_PROG_LOAD_X4(false, 0, NULL, 0), |
| 26 | + SPINAND_PROG_LOAD(false, 0, NULL, 0)); |
| 27 | + |
| 28 | +static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 29 | + struct mtd_oob_region *region) |
| 30 | +{ |
| 31 | + return -ERANGE; |
| 32 | +} |
| 33 | + |
| 34 | +static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section, |
| 35 | + struct mtd_oob_region *region) |
| 36 | +{ |
| 37 | + if (section) |
| 38 | + return -ERANGE; |
| 39 | + |
| 40 | + region->offset = 2; |
| 41 | + region->length = mtd->oobsize - 2; |
| 42 | + |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = { |
| 47 | + .ecc = mx35lfxge4ab_ooblayout_ecc, |
| 48 | + .free = mx35lfxge4ab_ooblayout_free, |
| 49 | +}; |
| 50 | + |
| 51 | +static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr) |
| 52 | +{ |
| 53 | + struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1), |
| 54 | + SPI_MEM_OP_NO_ADDR, |
| 55 | + SPI_MEM_OP_DUMMY(1, 1), |
| 56 | + SPI_MEM_OP_DATA_IN(1, eccsr, 1)); |
| 57 | + |
| 58 | + return spi_mem_exec_op(spinand->spimem, &op); |
| 59 | +} |
| 60 | + |
| 61 | +static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand, |
| 62 | + u8 status) |
| 63 | +{ |
| 64 | + struct nand_device *nand = spinand_to_nand(spinand); |
| 65 | + u8 eccsr; |
| 66 | + |
| 67 | + switch (status & STATUS_ECC_MASK) { |
| 68 | + case STATUS_ECC_NO_BITFLIPS: |
| 69 | + return 0; |
| 70 | + |
| 71 | + case STATUS_ECC_UNCOR_ERROR: |
| 72 | + return -EBADMSG; |
| 73 | + |
| 74 | + case STATUS_ECC_HAS_BITFLIPS: |
| 75 | + /* |
| 76 | + * Let's try to retrieve the real maximum number of bitflips |
| 77 | + * in order to avoid forcing the wear-leveling layer to move |
| 78 | + * data around if it's not necessary. |
| 79 | + */ |
| 80 | + if (mx35lf1ge4ab_get_eccsr(spinand, &eccsr)) |
| 81 | + return nand->eccreq.strength; |
| 82 | + |
| 83 | + if (WARN_ON(eccsr > nand->eccreq.strength || !eccsr)) |
| 84 | + return nand->eccreq.strength; |
| 85 | + |
| 86 | + return eccsr; |
| 87 | + |
| 88 | + default: |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + return -EINVAL; |
| 93 | +} |
| 94 | + |
| 95 | +static const struct spinand_info macronix_spinand_table[] = { |
| 96 | + SPINAND_INFO("MX35LF1GE4AB", 0x12, |
| 97 | + NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1), |
| 98 | + NAND_ECCREQ(4, 512), |
| 99 | + SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 100 | + &write_cache_variants, |
| 101 | + &update_cache_variants), |
| 102 | + SPINAND_HAS_QE_BIT, |
| 103 | + SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, |
| 104 | + mx35lf1ge4ab_ecc_get_status)), |
| 105 | +}; |
| 106 | + |
| 107 | +static int macronix_spinand_detect(struct spinand_device *spinand) |
| 108 | +{ |
| 109 | + u8 *id = spinand->id.data; |
| 110 | + int ret; |
| 111 | + |
| 112 | + /* |
| 113 | + * Macronix SPI NAND read ID needs a dummy byte, so the first byte in |
| 114 | + * raw_id is garbage. |
| 115 | + */ |
| 116 | + if (id[1] != SPINAND_MFR_MACRONIX) |
| 117 | + return 0; |
| 118 | + |
| 119 | + ret = spinand_match_and_init(spinand, macronix_spinand_table, |
| 120 | + ARRAY_SIZE(macronix_spinand_table), |
| 121 | + id[2]); |
| 122 | + if (ret) |
| 123 | + return ret; |
| 124 | + |
| 125 | + return 1; |
| 126 | +} |
| 127 | + |
| 128 | +static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = { |
| 129 | + .detect = macronix_spinand_detect, |
| 130 | +}; |
| 131 | + |
| 132 | +const struct spinand_manufacturer macronix_spinand_manufacturer = { |
| 133 | + .id = SPINAND_MFR_MACRONIX, |
| 134 | + .name = "Macronix", |
| 135 | + .ops = ¯onix_spinand_manuf_ops, |
| 136 | +}; |
0 commit comments