blob: cecd3db9584a7cd8e5280ea7c9ca4de8c5ec108a [file] [log] [blame]
Peter Pandf1859e2018-08-16 17:30:13 +02001// 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 Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Peter Pandf1859e2018-08-16 17:30:13 +020011#include <linux/device.h>
12#include <linux/kernel.h>
13#endif
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Peter Pandf1859e2018-08-16 17:30:13 +020015#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 Shastri92ecb1a2020-07-07 22:04:11 +020025#define MICRON_CFG_CR BIT(0)
26
Shivamurthy Shastrib7242882020-07-07 22:04:13 +020027/*
28 * As per datasheet, die selection is done by the 6th bit of Die
29 * Select Register (Address 0xD0).
30 */
31#define MICRON_DIE_SELECT_REG 0xD0
32
33#define MICRON_SELECT_DIE(x) ((x) << 6)
34
Peter Pandf1859e2018-08-16 17:30:13 +020035static SPINAND_OP_VARIANTS(read_cache_variants,
36 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
37 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
38 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
39 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
40 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
41 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
42
43static SPINAND_OP_VARIANTS(write_cache_variants,
44 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
45 SPINAND_PROG_LOAD(true, 0, NULL, 0));
46
47static SPINAND_OP_VARIANTS(update_cache_variants,
48 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
49 SPINAND_PROG_LOAD(false, 0, NULL, 0));
50
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020051static int micron_8_ooblayout_ecc(struct mtd_info *mtd, int section,
52 struct mtd_oob_region *region)
Peter Pandf1859e2018-08-16 17:30:13 +020053{
54 if (section)
55 return -ERANGE;
56
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020057 region->offset = mtd->oobsize / 2;
58 region->length = mtd->oobsize / 2;
Peter Pandf1859e2018-08-16 17:30:13 +020059
60 return 0;
61}
62
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020063static int micron_8_ooblayout_free(struct mtd_info *mtd, int section,
64 struct mtd_oob_region *region)
Peter Pandf1859e2018-08-16 17:30:13 +020065{
66 if (section)
67 return -ERANGE;
68
69 /* Reserve 2 bytes for the BBM. */
70 region->offset = 2;
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020071 region->length = (mtd->oobsize / 2) - 2;
Peter Pandf1859e2018-08-16 17:30:13 +020072
73 return 0;
74}
75
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020076static const struct mtd_ooblayout_ops micron_8_ooblayout = {
77 .ecc = micron_8_ooblayout_ecc,
78 .rfree = micron_8_ooblayout_free,
Peter Pandf1859e2018-08-16 17:30:13 +020079};
80
Shivamurthy Shastrib7242882020-07-07 22:04:13 +020081static int micron_select_target(struct spinand_device *spinand,
82 unsigned int target)
83{
84 struct spi_mem_op op = SPINAND_SET_FEATURE_OP(MICRON_DIE_SELECT_REG,
85 spinand->scratchbuf);
86
87 if (target > 1)
88 return -EINVAL;
89
90 *spinand->scratchbuf = MICRON_SELECT_DIE(target);
91
92 return spi_mem_exec_op(spinand->slave, &op);
93}
94
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020095static int micron_8_ecc_get_status(struct spinand_device *spinand,
96 u8 status)
Peter Pandf1859e2018-08-16 17:30:13 +020097{
98 switch (status & MICRON_STATUS_ECC_MASK) {
99 case STATUS_ECC_NO_BITFLIPS:
100 return 0;
101
102 case STATUS_ECC_UNCOR_ERROR:
103 return -EBADMSG;
104
105 case MICRON_STATUS_ECC_1TO3_BITFLIPS:
106 return 3;
107
108 case MICRON_STATUS_ECC_4TO6_BITFLIPS:
109 return 6;
110
111 case MICRON_STATUS_ECC_7TO8_BITFLIPS:
112 return 8;
113
114 default:
115 break;
116 }
117
118 return -EINVAL;
119}
120
121static const struct spinand_info micron_spinand_table[] = {
Shivamurthy Shastria44f6a92020-07-07 22:04:09 +0200122 /* M79A 2Gb 3.3V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100123 SPINAND_INFO("MT29F2G01ABAGD",
124 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x24),
Peter Pandf1859e2018-08-16 17:30:13 +0200125 NAND_MEMORG(1, 2048, 128, 64, 2048, 2, 1, 1),
126 NAND_ECCREQ(8, 512),
127 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
128 &write_cache_variants,
129 &update_cache_variants),
130 0,
Shivamurthy Shastricfe77252020-07-07 22:04:08 +0200131 SPINAND_ECCINFO(&micron_8_ooblayout,
132 micron_8_ecc_get_status)),
Shivamurthy Shastri8d06c5b2020-07-07 22:04:10 +0200133 /* M79A 2Gb 1.8V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100134 SPINAND_INFO("MT29F2G01ABBGD",
135 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x25),
Shivamurthy Shastri8d06c5b2020-07-07 22:04:10 +0200136 NAND_MEMORG(1, 2048, 128, 64, 2048, 2, 1, 1),
137 NAND_ECCREQ(8, 512),
138 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
139 &write_cache_variants,
140 &update_cache_variants),
141 0,
142 SPINAND_ECCINFO(&micron_8_ooblayout,
143 micron_8_ecc_get_status)),
144 /* M78A 1Gb 3.3V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100145 SPINAND_INFO("MT29F1G01ABAFD",
146 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x14),
Shivamurthy Shastri8d06c5b2020-07-07 22:04:10 +0200147 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
148 NAND_ECCREQ(8, 512),
149 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
150 &write_cache_variants,
151 &update_cache_variants),
152 0,
153 SPINAND_ECCINFO(&micron_8_ooblayout,
154 micron_8_ecc_get_status)),
155 /* M78A 1Gb 1.8V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100156 SPINAND_INFO("MT29F1G01ABAFD",
157 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x15),
Shivamurthy Shastri8d06c5b2020-07-07 22:04:10 +0200158 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
159 NAND_ECCREQ(8, 512),
160 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
161 &write_cache_variants,
162 &update_cache_variants),
163 0,
164 SPINAND_ECCINFO(&micron_8_ooblayout,
165 micron_8_ecc_get_status)),
Shivamurthy Shastrib7242882020-07-07 22:04:13 +0200166 /* M79A 4Gb 3.3V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100167 SPINAND_INFO("MT29F4G01ADAGD",
168 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x36),
Shivamurthy Shastrib7242882020-07-07 22:04:13 +0200169 NAND_MEMORG(1, 2048, 128, 64, 2048, 2, 1, 2),
170 NAND_ECCREQ(8, 512),
171 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
172 &write_cache_variants,
173 &update_cache_variants),
174 0,
175 SPINAND_ECCINFO(&micron_8_ooblayout,
176 micron_8_ecc_get_status),
177 SPINAND_SELECT_TARGET(micron_select_target)),
Shivamurthy Shastri5cbf17f2020-07-07 22:04:12 +0200178 /* M70A 4Gb 3.3V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100179 SPINAND_INFO("MT29F4G01ABAFD",
180 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x34),
Shivamurthy Shastri5cbf17f2020-07-07 22:04:12 +0200181 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
182 NAND_ECCREQ(8, 512),
183 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
184 &write_cache_variants,
185 &update_cache_variants),
186 SPINAND_HAS_CR_FEAT_BIT,
187 SPINAND_ECCINFO(&micron_8_ooblayout,
188 micron_8_ecc_get_status)),
189 /* M70A 4Gb 1.8V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100190 SPINAND_INFO("MT29F4G01ABBFD",
191 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x35),
Shivamurthy Shastri5cbf17f2020-07-07 22:04:12 +0200192 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
193 NAND_ECCREQ(8, 512),
194 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
195 &write_cache_variants,
196 &update_cache_variants),
197 SPINAND_HAS_CR_FEAT_BIT,
198 SPINAND_ECCINFO(&micron_8_ooblayout,
199 micron_8_ecc_get_status)),
Shivamurthy Shastrib7242882020-07-07 22:04:13 +0200200 /* M70A 8Gb 3.3V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100201 SPINAND_INFO("MT29F8G01ADAFD",
202 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x46),
Shivamurthy Shastrib7242882020-07-07 22:04:13 +0200203 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 2),
204 NAND_ECCREQ(8, 512),
205 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
206 &write_cache_variants,
207 &update_cache_variants),
208 SPINAND_HAS_CR_FEAT_BIT,
209 SPINAND_ECCINFO(&micron_8_ooblayout,
210 micron_8_ecc_get_status),
211 SPINAND_SELECT_TARGET(micron_select_target)),
212 /* M70A 8Gb 1.8V */
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100213 SPINAND_INFO("MT29F8G01ADBFD",
214 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x47),
Shivamurthy Shastrib7242882020-07-07 22:04:13 +0200215 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 2),
216 NAND_ECCREQ(8, 512),
217 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
218 &write_cache_variants,
219 &update_cache_variants),
220 SPINAND_HAS_CR_FEAT_BIT,
221 SPINAND_ECCINFO(&micron_8_ooblayout,
222 micron_8_ecc_get_status),
223 SPINAND_SELECT_TARGET(micron_select_target)),
Peter Pandf1859e2018-08-16 17:30:13 +0200224};
225
Shivamurthy Shastri92ecb1a2020-07-07 22:04:11 +0200226static int micron_spinand_init(struct spinand_device *spinand)
227{
228 /*
229 * M70A device series enable Continuous Read feature at Power-up,
230 * which is not supported. Disable this bit to avoid any possible
231 * failure.
232 */
233 if (spinand->flags & SPINAND_HAS_CR_FEAT_BIT)
234 return spinand_upd_cfg(spinand, MICRON_CFG_CR, 0);
235
236 return 0;
237}
238
Peter Pandf1859e2018-08-16 17:30:13 +0200239static const struct spinand_manufacturer_ops micron_spinand_manuf_ops = {
Shivamurthy Shastri92ecb1a2020-07-07 22:04:11 +0200240 .init = micron_spinand_init,
Peter Pandf1859e2018-08-16 17:30:13 +0200241};
242
243const struct spinand_manufacturer micron_spinand_manufacturer = {
244 .id = SPINAND_MFR_MICRON,
245 .name = "Micron",
Mikhail Kshevetskiy72010312023-01-10 12:58:38 +0100246 .chips = micron_spinand_table,
247 .nchips = ARRAY_SIZE(micron_spinand_table),
Peter Pandf1859e2018-08-16 17:30:13 +0200248 .ops = &micron_spinand_manuf_ops,
249};