Boris Brezillon | 10d4e75 | 2016-06-08 10:38:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Free Electrons |
| 3 | * Copyright (C) 2017 NextThing Co |
| 4 | * |
| 5 | * Author: Boris Brezillon <boris.brezillon@free-electrons.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 18 | #include <linux/mtd/rawnand.h> |
Boris Brezillon | 10d4e75 | 2016-06-08 10:38:57 +0200 | [diff] [blame] | 19 | |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 20 | /* |
| 21 | * Special Micron status bit that indicates when the block has been |
| 22 | * corrected by on-die ECC and should be rewritten |
| 23 | */ |
| 24 | #define NAND_STATUS_WRITE_RECOMMENDED BIT(3) |
| 25 | |
Boris Brezillon | 10d4e75 | 2016-06-08 10:38:57 +0200 | [diff] [blame] | 26 | struct nand_onfi_vendor_micron { |
| 27 | u8 two_plane_read; |
| 28 | u8 read_cache; |
| 29 | u8 read_unique_id; |
| 30 | u8 dq_imped; |
| 31 | u8 dq_imped_num_settings; |
| 32 | u8 dq_imped_feat_addr; |
| 33 | u8 rb_pulldown_strength; |
| 34 | u8 rb_pulldown_strength_feat_addr; |
| 35 | u8 rb_pulldown_strength_num_settings; |
| 36 | u8 otp_mode; |
| 37 | u8 otp_page_start; |
| 38 | u8 otp_data_prot_addr; |
| 39 | u8 otp_num_pages; |
| 40 | u8 otp_feat_addr; |
| 41 | u8 read_retry_options; |
| 42 | u8 reserved[72]; |
| 43 | u8 param_revision; |
| 44 | } __packed; |
| 45 | |
| 46 | static int micron_nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 47 | { |
| 48 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 49 | u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode}; |
| 50 | |
| 51 | return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY, |
| 52 | feature); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Configure chip properties from Micron vendor-specific ONFI table |
| 57 | */ |
| 58 | static int micron_nand_onfi_init(struct nand_chip *chip) |
| 59 | { |
| 60 | struct nand_onfi_params *p = &chip->onfi_params; |
| 61 | struct nand_onfi_vendor_micron *micron = (void *)p->vendor; |
| 62 | |
| 63 | if (!chip->onfi_version) |
| 64 | return 0; |
| 65 | |
| 66 | if (le16_to_cpu(p->vendor_revision) < 1) |
| 67 | return 0; |
| 68 | |
| 69 | chip->read_retries = micron->read_retry_options; |
| 70 | chip->setup_read_retry = micron_nand_setup_read_retry; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 75 | static int micron_nand_on_die_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 76 | struct mtd_oob_region *oobregion) |
| 77 | { |
| 78 | if (section >= 4) |
| 79 | return -ERANGE; |
| 80 | |
| 81 | oobregion->offset = (section * 16) + 8; |
| 82 | oobregion->length = 8; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int micron_nand_on_die_ooblayout_free(struct mtd_info *mtd, int section, |
| 88 | struct mtd_oob_region *oobregion) |
| 89 | { |
| 90 | if (section >= 4) |
| 91 | return -ERANGE; |
| 92 | |
| 93 | oobregion->offset = (section * 16) + 2; |
| 94 | oobregion->length = 6; |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static const struct mtd_ooblayout_ops micron_nand_on_die_ooblayout_ops = { |
| 100 | .ecc = micron_nand_on_die_ooblayout_ecc, |
| 101 | .free = micron_nand_on_die_ooblayout_free, |
| 102 | }; |
| 103 | |
| 104 | static int micron_nand_on_die_ecc_setup(struct nand_chip *chip, bool enable) |
| 105 | { |
| 106 | u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, }; |
| 107 | |
| 108 | if (enable) |
| 109 | feature[0] |= ONFI_FEATURE_ON_DIE_ECC_EN; |
| 110 | |
| 111 | return chip->onfi_set_features(nand_to_mtd(chip), chip, |
| 112 | ONFI_FEATURE_ON_DIE_ECC, feature); |
| 113 | } |
| 114 | |
| 115 | static int |
| 116 | micron_nand_read_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 117 | uint8_t *buf, int oob_required, |
| 118 | int page) |
| 119 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 120 | u8 status; |
| 121 | int ret, max_bitflips = 0; |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 122 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 123 | ret = micron_nand_on_die_ecc_setup(chip, true); |
| 124 | if (ret) |
| 125 | return ret; |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 126 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 127 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 128 | if (ret) |
| 129 | goto out; |
| 130 | |
| 131 | ret = nand_status_op(chip, &status); |
| 132 | if (ret) |
| 133 | goto out; |
| 134 | |
| 135 | ret = nand_exit_status_op(chip); |
| 136 | if (ret) |
| 137 | goto out; |
| 138 | |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 139 | if (status & NAND_STATUS_FAIL) |
| 140 | mtd->ecc_stats.failed++; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 141 | |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 142 | /* |
| 143 | * The internal ECC doesn't tell us the number of bitflips |
| 144 | * that have been corrected, but tells us if it recommends to |
| 145 | * rewrite the block. If it's the case, then we pretend we had |
| 146 | * a number of bitflips equal to the ECC strength, which will |
| 147 | * hint the NAND core to rewrite the block. |
| 148 | */ |
| 149 | else if (status & NAND_STATUS_WRITE_RECOMMENDED) |
| 150 | max_bitflips = chip->ecc.strength; |
| 151 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 152 | ret = nand_read_page_raw(mtd, chip, buf, oob_required, page); |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 153 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 154 | out: |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 155 | micron_nand_on_die_ecc_setup(chip, false); |
| 156 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 157 | return ret ? ret : max_bitflips; |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int |
| 161 | micron_nand_write_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 162 | const uint8_t *buf, int oob_required, |
| 163 | int page) |
| 164 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 165 | int ret; |
Boris Brezillon | 4114564 | 2017-05-16 18:27:49 +0200 | [diff] [blame] | 166 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 167 | ret = micron_nand_on_die_ecc_setup(chip, true); |
| 168 | if (ret) |
| 169 | return ret; |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 170 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 171 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 172 | if (ret) |
| 173 | goto out; |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 174 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 175 | ret = nand_write_page_raw(mtd, chip, buf, oob_required, page); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | |
| 179 | ret = nand_prog_page_end_op(chip); |
| 180 | |
| 181 | out: |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 182 | micron_nand_on_die_ecc_setup(chip, false); |
| 183 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 184 | return ret; |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static int |
| 188 | micron_nand_read_page_raw_on_die_ecc(struct mtd_info *mtd, |
| 189 | struct nand_chip *chip, |
| 190 | uint8_t *buf, int oob_required, |
| 191 | int page) |
| 192 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 193 | int ret; |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 194 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 195 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | return nand_read_page_raw(mtd, chip, buf, oob_required, page); |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static int |
| 203 | micron_nand_write_page_raw_on_die_ecc(struct mtd_info *mtd, |
| 204 | struct nand_chip *chip, |
| 205 | const uint8_t *buf, int oob_required, |
| 206 | int page) |
| 207 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 208 | int ret; |
Boris Brezillon | 4114564 | 2017-05-16 18:27:49 +0200 | [diff] [blame] | 209 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 210 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 211 | if (ret) |
| 212 | return ret; |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 213 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 214 | ret = nand_write_page_raw(mtd, chip, buf, oob_required, page); |
| 215 | if (ret) |
| 216 | return ret; |
| 217 | |
| 218 | return nand_prog_page_end_op(chip); |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | enum { |
| 222 | /* The NAND flash doesn't support on-die ECC */ |
| 223 | MICRON_ON_DIE_UNSUPPORTED, |
| 224 | |
| 225 | /* |
| 226 | * The NAND flash supports on-die ECC and it can be |
| 227 | * enabled/disabled by a set features command. |
| 228 | */ |
| 229 | MICRON_ON_DIE_SUPPORTED, |
| 230 | |
| 231 | /* |
| 232 | * The NAND flash supports on-die ECC, and it cannot be |
| 233 | * disabled. |
| 234 | */ |
| 235 | MICRON_ON_DIE_MANDATORY, |
| 236 | }; |
| 237 | |
| 238 | /* |
| 239 | * Try to detect if the NAND support on-die ECC. To do this, we enable |
| 240 | * the feature, and read back if it has been enabled as expected. We |
| 241 | * also check if it can be disabled, because some Micron NANDs do not |
| 242 | * allow disabling the on-die ECC and we don't support such NANDs for |
| 243 | * now. |
| 244 | * |
| 245 | * This function also has the side effect of disabling on-die ECC if |
| 246 | * it had been left enabled by the firmware/bootloader. |
| 247 | */ |
| 248 | static int micron_supports_on_die_ecc(struct nand_chip *chip) |
| 249 | { |
| 250 | u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, }; |
| 251 | int ret; |
| 252 | |
| 253 | if (chip->onfi_version == 0) |
| 254 | return MICRON_ON_DIE_UNSUPPORTED; |
| 255 | |
| 256 | if (chip->bits_per_cell != 1) |
| 257 | return MICRON_ON_DIE_UNSUPPORTED; |
| 258 | |
| 259 | ret = micron_nand_on_die_ecc_setup(chip, true); |
| 260 | if (ret) |
| 261 | return MICRON_ON_DIE_UNSUPPORTED; |
| 262 | |
| 263 | chip->onfi_get_features(nand_to_mtd(chip), chip, |
| 264 | ONFI_FEATURE_ON_DIE_ECC, feature); |
| 265 | if ((feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN) == 0) |
| 266 | return MICRON_ON_DIE_UNSUPPORTED; |
| 267 | |
| 268 | ret = micron_nand_on_die_ecc_setup(chip, false); |
| 269 | if (ret) |
| 270 | return MICRON_ON_DIE_UNSUPPORTED; |
| 271 | |
| 272 | chip->onfi_get_features(nand_to_mtd(chip), chip, |
| 273 | ONFI_FEATURE_ON_DIE_ECC, feature); |
| 274 | if (feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN) |
| 275 | return MICRON_ON_DIE_MANDATORY; |
| 276 | |
| 277 | /* |
| 278 | * Some Micron NANDs have an on-die ECC of 4/512, some other |
| 279 | * 8/512. We only support the former. |
| 280 | */ |
| 281 | if (chip->onfi_params.ecc_bits != 4) |
| 282 | return MICRON_ON_DIE_UNSUPPORTED; |
| 283 | |
| 284 | return MICRON_ON_DIE_SUPPORTED; |
| 285 | } |
| 286 | |
Boris Brezillon | 10d4e75 | 2016-06-08 10:38:57 +0200 | [diff] [blame] | 287 | static int micron_nand_init(struct nand_chip *chip) |
| 288 | { |
| 289 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 290 | int ondie; |
Boris Brezillon | 10d4e75 | 2016-06-08 10:38:57 +0200 | [diff] [blame] | 291 | int ret; |
| 292 | |
| 293 | ret = micron_nand_onfi_init(chip); |
| 294 | if (ret) |
| 295 | return ret; |
| 296 | |
| 297 | if (mtd->writesize == 2048) |
| 298 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; |
| 299 | |
Thomas Petazzoni | 9748e1d | 2017-04-29 11:06:45 +0200 | [diff] [blame] | 300 | ondie = micron_supports_on_die_ecc(chip); |
| 301 | |
| 302 | if (ondie == MICRON_ON_DIE_MANDATORY) { |
| 303 | pr_err("On-die ECC forcefully enabled, not supported\n"); |
| 304 | return -EINVAL; |
| 305 | } |
| 306 | |
| 307 | if (chip->ecc.mode == NAND_ECC_ON_DIE) { |
| 308 | if (ondie == MICRON_ON_DIE_UNSUPPORTED) { |
| 309 | pr_err("On-die ECC selected but not supported\n"); |
| 310 | return -EINVAL; |
| 311 | } |
| 312 | |
| 313 | chip->ecc.options = NAND_ECC_CUSTOM_PAGE_ACCESS; |
| 314 | chip->ecc.bytes = 8; |
| 315 | chip->ecc.size = 512; |
| 316 | chip->ecc.strength = 4; |
| 317 | chip->ecc.algo = NAND_ECC_BCH; |
| 318 | chip->ecc.read_page = micron_nand_read_page_on_die_ecc; |
| 319 | chip->ecc.write_page = micron_nand_write_page_on_die_ecc; |
| 320 | chip->ecc.read_page_raw = |
| 321 | micron_nand_read_page_raw_on_die_ecc; |
| 322 | chip->ecc.write_page_raw = |
| 323 | micron_nand_write_page_raw_on_die_ecc; |
| 324 | |
| 325 | mtd_set_ooblayout(mtd, µn_nand_on_die_ooblayout_ops); |
| 326 | } |
| 327 | |
Boris Brezillon | 10d4e75 | 2016-06-08 10:38:57 +0200 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | const struct nand_manufacturer_ops micron_nand_manuf_ops = { |
| 332 | .init = micron_nand_init, |
| 333 | }; |