Boris Brezillon | fa46525 | 2018-08-16 17:30:15 +0200 | [diff] [blame] | 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 | #ifndef __UBOOT__ |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Boris Brezillon | fa46525 | 2018-08-16 17:30:15 +0200 | [diff] [blame] | 10 | #include <linux/device.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #endif |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 13 | #include <linux/bug.h> |
Boris Brezillon | fa46525 | 2018-08-16 17:30:15 +0200 | [diff] [blame] | 14 | #include <linux/mtd/spinand.h> |
| 15 | |
| 16 | #define SPINAND_MFR_MACRONIX 0xC2 |
| 17 | |
| 18 | static SPINAND_OP_VARIANTS(read_cache_variants, |
| 19 | SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0), |
| 20 | SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0), |
| 21 | SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0), |
| 22 | SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0)); |
| 23 | |
| 24 | static SPINAND_OP_VARIANTS(write_cache_variants, |
| 25 | SPINAND_PROG_LOAD_X4(true, 0, NULL, 0), |
| 26 | SPINAND_PROG_LOAD(true, 0, NULL, 0)); |
| 27 | |
| 28 | static SPINAND_OP_VARIANTS(update_cache_variants, |
| 29 | SPINAND_PROG_LOAD_X4(false, 0, NULL, 0), |
| 30 | SPINAND_PROG_LOAD(false, 0, NULL, 0)); |
| 31 | |
Miquel Raynal | 02b4979 | 2018-08-16 17:30:16 +0200 | [diff] [blame] | 32 | static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section, |
Boris Brezillon | fa46525 | 2018-08-16 17:30:15 +0200 | [diff] [blame] | 33 | struct mtd_oob_region *region) |
| 34 | { |
| 35 | return -ERANGE; |
| 36 | } |
| 37 | |
Miquel Raynal | 02b4979 | 2018-08-16 17:30:16 +0200 | [diff] [blame] | 38 | static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section, |
Boris Brezillon | fa46525 | 2018-08-16 17:30:15 +0200 | [diff] [blame] | 39 | struct mtd_oob_region *region) |
| 40 | { |
| 41 | if (section) |
| 42 | return -ERANGE; |
| 43 | |
| 44 | region->offset = 2; |
| 45 | region->length = mtd->oobsize - 2; |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Miquel Raynal | 02b4979 | 2018-08-16 17:30:16 +0200 | [diff] [blame] | 50 | static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = { |
| 51 | .ecc = mx35lfxge4ab_ooblayout_ecc, |
Simon Glass | 62fd1a4 | 2020-02-03 07:35:56 -0700 | [diff] [blame] | 52 | .rfree = mx35lfxge4ab_ooblayout_free, |
Boris Brezillon | fa46525 | 2018-08-16 17:30:15 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr) |
| 56 | { |
| 57 | struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1), |
| 58 | SPI_MEM_OP_NO_ADDR, |
| 59 | SPI_MEM_OP_DUMMY(1, 1), |
| 60 | SPI_MEM_OP_DATA_IN(1, eccsr, 1)); |
| 61 | |
| 62 | return spi_mem_exec_op(spinand->slave, &op); |
| 63 | } |
| 64 | |
| 65 | static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand, |
| 66 | u8 status) |
| 67 | { |
| 68 | struct nand_device *nand = spinand_to_nand(spinand); |
| 69 | u8 eccsr; |
| 70 | |
| 71 | switch (status & STATUS_ECC_MASK) { |
| 72 | case STATUS_ECC_NO_BITFLIPS: |
| 73 | return 0; |
| 74 | |
| 75 | case STATUS_ECC_UNCOR_ERROR: |
| 76 | return -EBADMSG; |
| 77 | |
| 78 | case STATUS_ECC_HAS_BITFLIPS: |
| 79 | /* |
| 80 | * Let's try to retrieve the real maximum number of bitflips |
| 81 | * in order to avoid forcing the wear-leveling layer to move |
| 82 | * data around if it's not necessary. |
| 83 | */ |
| 84 | if (mx35lf1ge4ab_get_eccsr(spinand, &eccsr)) |
| 85 | return nand->eccreq.strength; |
| 86 | |
| 87 | if (WARN_ON(eccsr > nand->eccreq.strength || !eccsr)) |
| 88 | return nand->eccreq.strength; |
| 89 | |
| 90 | return eccsr; |
| 91 | |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | |
| 99 | static const struct spinand_info macronix_spinand_table[] = { |
| 100 | SPINAND_INFO("MX35LF1GE4AB", 0x12, |
| 101 | NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1), |
| 102 | NAND_ECCREQ(4, 512), |
| 103 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 104 | &write_cache_variants, |
| 105 | &update_cache_variants), |
| 106 | SPINAND_HAS_QE_BIT, |
Miquel Raynal | 02b4979 | 2018-08-16 17:30:16 +0200 | [diff] [blame] | 107 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, |
Boris Brezillon | fa46525 | 2018-08-16 17:30:15 +0200 | [diff] [blame] | 108 | mx35lf1ge4ab_ecc_get_status)), |
Miquel Raynal | 02b4979 | 2018-08-16 17:30:16 +0200 | [diff] [blame] | 109 | SPINAND_INFO("MX35LF2GE4AB", 0x22, |
| 110 | NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1), |
| 111 | NAND_ECCREQ(4, 512), |
| 112 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 113 | &write_cache_variants, |
| 114 | &update_cache_variants), |
| 115 | SPINAND_HAS_QE_BIT, |
| 116 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)), |
Jaime Liao | 4b017b9 | 2021-06-07 16:19:15 +0800 | [diff] [blame] | 117 | SPINAND_INFO("MX35UF4GE4AD", 0xb7, |
| 118 | NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1), |
| 119 | NAND_ECCREQ(8, 512), |
| 120 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 121 | &write_cache_variants, |
| 122 | &update_cache_variants), |
| 123 | SPINAND_HAS_QE_BIT, |
| 124 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, |
| 125 | mx35lf1ge4ab_ecc_get_status)), |
| 126 | SPINAND_INFO("MX35UF2GE4AD", 0xa6, |
| 127 | NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1), |
| 128 | NAND_ECCREQ(8, 512), |
| 129 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 130 | &write_cache_variants, |
| 131 | &update_cache_variants), |
| 132 | SPINAND_HAS_QE_BIT, |
| 133 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, |
| 134 | mx35lf1ge4ab_ecc_get_status)), |
| 135 | SPINAND_INFO("MX35UF2GE4AC", 0xa2, |
| 136 | NAND_MEMORG(1, 2048, 64, 64, 2048, 1, 1, 1), |
| 137 | NAND_ECCREQ(4, 512), |
| 138 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 139 | &write_cache_variants, |
| 140 | &update_cache_variants), |
| 141 | SPINAND_HAS_QE_BIT, |
| 142 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, |
| 143 | mx35lf1ge4ab_ecc_get_status)), |
| 144 | SPINAND_INFO("MX35UF1GE4AD", 0x96, |
| 145 | NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1), |
| 146 | NAND_ECCREQ(8, 512), |
| 147 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 148 | &write_cache_variants, |
| 149 | &update_cache_variants), |
| 150 | SPINAND_HAS_QE_BIT, |
| 151 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, |
| 152 | mx35lf1ge4ab_ecc_get_status)), |
| 153 | SPINAND_INFO("MX35UF1GE4AC", 0x92, |
| 154 | NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1), |
| 155 | NAND_ECCREQ(4, 512), |
| 156 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 157 | &write_cache_variants, |
| 158 | &update_cache_variants), |
| 159 | SPINAND_HAS_QE_BIT, |
| 160 | SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, |
| 161 | mx35lf1ge4ab_ecc_get_status)), |
| 162 | |
Boris Brezillon | fa46525 | 2018-08-16 17:30:15 +0200 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static int macronix_spinand_detect(struct spinand_device *spinand) |
| 166 | { |
| 167 | u8 *id = spinand->id.data; |
| 168 | int ret; |
| 169 | |
| 170 | /* |
| 171 | * Macronix SPI NAND read ID needs a dummy byte, so the first byte in |
| 172 | * raw_id is garbage. |
| 173 | */ |
| 174 | if (id[1] != SPINAND_MFR_MACRONIX) |
| 175 | return 0; |
| 176 | |
| 177 | ret = spinand_match_and_init(spinand, macronix_spinand_table, |
| 178 | ARRAY_SIZE(macronix_spinand_table), |
| 179 | id[2]); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = { |
| 187 | .detect = macronix_spinand_detect, |
| 188 | }; |
| 189 | |
| 190 | const struct spinand_manufacturer macronix_spinand_manufacturer = { |
| 191 | .id = SPINAND_MFR_MACRONIX, |
| 192 | .name = "Macronix", |
| 193 | .ops = ¯onix_spinand_manuf_ops, |
| 194 | }; |