Peter Pan | 883d877 | 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 | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Peter Pan | 883d877 | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #endif |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Peter Pan | 883d877 | 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 | 720fcb2 | 2020-07-07 22:04:11 +0200 | [diff] [blame^] | 25 | #define MICRON_CFG_CR BIT(0) |
| 26 | |
Peter Pan | 883d877 | 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 | 1527ec4 | 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 | 883d877 | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 45 | { |
| 46 | if (section) |
| 47 | return -ERANGE; |
| 48 | |
Shivamurthy Shastri | 1527ec4 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 49 | region->offset = mtd->oobsize / 2; |
| 50 | region->length = mtd->oobsize / 2; |
Peter Pan | 883d877 | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Shivamurthy Shastri | 1527ec4 | 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 | 883d877 | 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 | 1527ec4 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 63 | region->length = (mtd->oobsize / 2) - 2; |
Peter Pan | 883d877 | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Shivamurthy Shastri | 1527ec4 | 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 | 883d877 | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Shivamurthy Shastri | 1527ec4 | 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 | 883d877 | 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 | 92fc25d | 2020-07-07 22:04:09 +0200 | [diff] [blame] | 100 | /* M79A 2Gb 3.3V */ |
Peter Pan | 883d877 | 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 | 1527ec4 | 2020-07-07 22:04:08 +0200 | [diff] [blame] | 108 | SPINAND_ECCINFO(µn_8_ooblayout, |
| 109 | micron_8_ecc_get_status)), |
Shivamurthy Shastri | 5cf049c | 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)), |
Peter Pan | 883d877 | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static int micron_spinand_detect(struct spinand_device *spinand) |
| 143 | { |
| 144 | u8 *id = spinand->id.data; |
| 145 | int ret; |
| 146 | |
| 147 | /* |
| 148 | * Micron SPI NAND read ID need a dummy byte, |
| 149 | * so the first byte in raw_id is dummy. |
| 150 | */ |
| 151 | if (id[1] != SPINAND_MFR_MICRON) |
| 152 | return 0; |
| 153 | |
| 154 | ret = spinand_match_and_init(spinand, micron_spinand_table, |
| 155 | ARRAY_SIZE(micron_spinand_table), id[2]); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | return 1; |
| 160 | } |
| 161 | |
Shivamurthy Shastri | 720fcb2 | 2020-07-07 22:04:11 +0200 | [diff] [blame^] | 162 | static int micron_spinand_init(struct spinand_device *spinand) |
| 163 | { |
| 164 | /* |
| 165 | * M70A device series enable Continuous Read feature at Power-up, |
| 166 | * which is not supported. Disable this bit to avoid any possible |
| 167 | * failure. |
| 168 | */ |
| 169 | if (spinand->flags & SPINAND_HAS_CR_FEAT_BIT) |
| 170 | return spinand_upd_cfg(spinand, MICRON_CFG_CR, 0); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Peter Pan | 883d877 | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 175 | static const struct spinand_manufacturer_ops micron_spinand_manuf_ops = { |
| 176 | .detect = micron_spinand_detect, |
Shivamurthy Shastri | 720fcb2 | 2020-07-07 22:04:11 +0200 | [diff] [blame^] | 177 | .init = micron_spinand_init, |
Peter Pan | 883d877 | 2018-08-16 17:30:13 +0200 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | const struct spinand_manufacturer micron_spinand_manufacturer = { |
| 181 | .id = SPINAND_MFR_MICRON, |
| 182 | .name = "Micron", |
| 183 | .ops = µn_spinand_manuf_ops, |
| 184 | }; |