Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2016-2017 Micron Technology, Inc. |
| 4 | * |
| 5 | * Authors: |
| 6 | * Peter Pan <peterpandong@micron.com> |
| 7 | */ |
| 8 | |
| 9 | #ifndef __UBOOT__ |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #endif |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 15 | #include <linux/mtd/spinand.h> |
| 16 | |
| 17 | #define SPINAND_MFR_MICRON 0x2c |
| 18 | |
| 19 | #define MICRON_STATUS_ECC_MASK GENMASK(7, 4) |
| 20 | #define MICRON_STATUS_ECC_NO_BITFLIPS (0 << 4) |
| 21 | #define MICRON_STATUS_ECC_1TO3_BITFLIPS (1 << 4) |
| 22 | #define MICRON_STATUS_ECC_4TO6_BITFLIPS (3 << 4) |
| 23 | #define MICRON_STATUS_ECC_7TO8_BITFLIPS (5 << 4) |
| 24 | |
Shivamurthy Shastri | 92ecb1a | 2020-07-07 22:04:11 +0200 | [diff] [blame] | 25 | #define MICRON_CFG_CR BIT(0) |
| 26 | |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 27 | static SPINAND_OP_VARIANTS(read_cache_variants, |
| 28 | SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0), |
| 29 | SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0), |
| 30 | SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0), |
| 31 | SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0), |
| 32 | SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0), |
| 33 | SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0)); |
| 34 | |
| 35 | static SPINAND_OP_VARIANTS(write_cache_variants, |
| 36 | SPINAND_PROG_LOAD_X4(true, 0, NULL, 0), |
| 37 | SPINAND_PROG_LOAD(true, 0, NULL, 0)); |
| 38 | |
| 39 | static SPINAND_OP_VARIANTS(update_cache_variants, |
| 40 | SPINAND_PROG_LOAD_X4(false, 0, NULL, 0), |
| 41 | SPINAND_PROG_LOAD(false, 0, NULL, 0)); |
| 42 | |
Shivamurthy Shastri | cfe7725 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 43 | static int micron_8_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 44 | struct mtd_oob_region *region) |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 45 | { |
| 46 | if (section) |
| 47 | return -ERANGE; |
| 48 | |
Shivamurthy Shastri | cfe7725 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 49 | region->offset = mtd->oobsize / 2; |
| 50 | region->length = mtd->oobsize / 2; |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Shivamurthy Shastri | cfe7725 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 55 | static int micron_8_ooblayout_free(struct mtd_info *mtd, int section, |
| 56 | struct mtd_oob_region *region) |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 57 | { |
| 58 | if (section) |
| 59 | return -ERANGE; |
| 60 | |
| 61 | /* Reserve 2 bytes for the BBM. */ |
| 62 | region->offset = 2; |
Shivamurthy Shastri | cfe7725 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 63 | region->length = (mtd->oobsize / 2) - 2; |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Shivamurthy Shastri | cfe7725 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 68 | static const struct mtd_ooblayout_ops micron_8_ooblayout = { |
| 69 | .ecc = micron_8_ooblayout_ecc, |
| 70 | .rfree = micron_8_ooblayout_free, |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Shivamurthy Shastri | cfe7725 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 73 | static int micron_8_ecc_get_status(struct spinand_device *spinand, |
| 74 | u8 status) |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 75 | { |
| 76 | switch (status & MICRON_STATUS_ECC_MASK) { |
| 77 | case STATUS_ECC_NO_BITFLIPS: |
| 78 | return 0; |
| 79 | |
| 80 | case STATUS_ECC_UNCOR_ERROR: |
| 81 | return -EBADMSG; |
| 82 | |
| 83 | case MICRON_STATUS_ECC_1TO3_BITFLIPS: |
| 84 | return 3; |
| 85 | |
| 86 | case MICRON_STATUS_ECC_4TO6_BITFLIPS: |
| 87 | return 6; |
| 88 | |
| 89 | case MICRON_STATUS_ECC_7TO8_BITFLIPS: |
| 90 | return 8; |
| 91 | |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | |
| 99 | static const struct spinand_info micron_spinand_table[] = { |
Shivamurthy Shastri | a44f6a9 | 2020-07-07 22:04:09 +0200 | [diff] [blame] | 100 | /* M79A 2Gb 3.3V */ |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 101 | SPINAND_INFO("MT29F2G01ABAGD", 0x24, |
| 102 | NAND_MEMORG(1, 2048, 128, 64, 2048, 2, 1, 1), |
| 103 | NAND_ECCREQ(8, 512), |
| 104 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 105 | &write_cache_variants, |
| 106 | &update_cache_variants), |
| 107 | 0, |
Shivamurthy Shastri | cfe7725 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 108 | SPINAND_ECCINFO(µn_8_ooblayout, |
| 109 | micron_8_ecc_get_status)), |
Shivamurthy Shastri | 8d06c5b | 2020-07-07 22:04:10 +0200 | [diff] [blame] | 110 | /* M79A 2Gb 1.8V */ |
| 111 | SPINAND_INFO("MT29F2G01ABBGD", 0x25, |
| 112 | NAND_MEMORG(1, 2048, 128, 64, 2048, 2, 1, 1), |
| 113 | NAND_ECCREQ(8, 512), |
| 114 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 115 | &write_cache_variants, |
| 116 | &update_cache_variants), |
| 117 | 0, |
| 118 | SPINAND_ECCINFO(µn_8_ooblayout, |
| 119 | micron_8_ecc_get_status)), |
| 120 | /* M78A 1Gb 3.3V */ |
| 121 | SPINAND_INFO("MT29F1G01ABAFD", 0x14, |
| 122 | NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1), |
| 123 | NAND_ECCREQ(8, 512), |
| 124 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 125 | &write_cache_variants, |
| 126 | &update_cache_variants), |
| 127 | 0, |
| 128 | SPINAND_ECCINFO(µn_8_ooblayout, |
| 129 | micron_8_ecc_get_status)), |
| 130 | /* M78A 1Gb 1.8V */ |
| 131 | SPINAND_INFO("MT29F1G01ABAFD", 0x15, |
| 132 | NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1), |
| 133 | NAND_ECCREQ(8, 512), |
| 134 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 135 | &write_cache_variants, |
| 136 | &update_cache_variants), |
| 137 | 0, |
| 138 | SPINAND_ECCINFO(µn_8_ooblayout, |
| 139 | micron_8_ecc_get_status)), |
Shivamurthy Shastri | 5cbf17f | 2020-07-07 22:04:12 +0200 | [diff] [blame^] | 140 | /* M70A 4Gb 3.3V */ |
| 141 | SPINAND_INFO("MT29F4G01ABAFD", 0x34, |
| 142 | NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1), |
| 143 | NAND_ECCREQ(8, 512), |
| 144 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 145 | &write_cache_variants, |
| 146 | &update_cache_variants), |
| 147 | SPINAND_HAS_CR_FEAT_BIT, |
| 148 | SPINAND_ECCINFO(µn_8_ooblayout, |
| 149 | micron_8_ecc_get_status)), |
| 150 | /* M70A 4Gb 1.8V */ |
| 151 | SPINAND_INFO("MT29F4G01ABBFD", 0x35, |
| 152 | NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1), |
| 153 | NAND_ECCREQ(8, 512), |
| 154 | SPINAND_INFO_OP_VARIANTS(&read_cache_variants, |
| 155 | &write_cache_variants, |
| 156 | &update_cache_variants), |
| 157 | SPINAND_HAS_CR_FEAT_BIT, |
| 158 | SPINAND_ECCINFO(µn_8_ooblayout, |
| 159 | micron_8_ecc_get_status)), |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | static int micron_spinand_detect(struct spinand_device *spinand) |
| 163 | { |
| 164 | u8 *id = spinand->id.data; |
| 165 | int ret; |
| 166 | |
| 167 | /* |
| 168 | * Micron SPI NAND read ID need a dummy byte, |
| 169 | * so the first byte in raw_id is dummy. |
| 170 | */ |
| 171 | if (id[1] != SPINAND_MFR_MICRON) |
| 172 | return 0; |
| 173 | |
| 174 | ret = spinand_match_and_init(spinand, micron_spinand_table, |
| 175 | ARRAY_SIZE(micron_spinand_table), id[2]); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | |
| 179 | return 1; |
| 180 | } |
| 181 | |
Shivamurthy Shastri | 92ecb1a | 2020-07-07 22:04:11 +0200 | [diff] [blame] | 182 | static int micron_spinand_init(struct spinand_device *spinand) |
| 183 | { |
| 184 | /* |
| 185 | * M70A device series enable Continuous Read feature at Power-up, |
| 186 | * which is not supported. Disable this bit to avoid any possible |
| 187 | * failure. |
| 188 | */ |
| 189 | if (spinand->flags & SPINAND_HAS_CR_FEAT_BIT) |
| 190 | return spinand_upd_cfg(spinand, MICRON_CFG_CR, 0); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 195 | static const struct spinand_manufacturer_ops micron_spinand_manuf_ops = { |
| 196 | .detect = micron_spinand_detect, |
Shivamurthy Shastri | 92ecb1a | 2020-07-07 22:04:11 +0200 | [diff] [blame] | 197 | .init = micron_spinand_init, |
Peter Pan | df1859e | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | const struct spinand_manufacturer micron_spinand_manufacturer = { |
| 201 | .id = SPINAND_MFR_MICRON, |
| 202 | .name = "Micron", |
| 203 | .ops = µn_spinand_manuf_ops, |
| 204 | }; |