blob: fe91349999c689b9bba2896e17b086b65029639e [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
Peter Pandf1859e2018-08-16 17:30:13 +020027static 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
35static SPINAND_OP_VARIANTS(write_cache_variants,
36 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
37 SPINAND_PROG_LOAD(true, 0, NULL, 0));
38
39static 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 Shastricfe77252020-07-07 22:04:08 +020043static int micron_8_ooblayout_ecc(struct mtd_info *mtd, int section,
44 struct mtd_oob_region *region)
Peter Pandf1859e2018-08-16 17:30:13 +020045{
46 if (section)
47 return -ERANGE;
48
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020049 region->offset = mtd->oobsize / 2;
50 region->length = mtd->oobsize / 2;
Peter Pandf1859e2018-08-16 17:30:13 +020051
52 return 0;
53}
54
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020055static int micron_8_ooblayout_free(struct mtd_info *mtd, int section,
56 struct mtd_oob_region *region)
Peter Pandf1859e2018-08-16 17:30:13 +020057{
58 if (section)
59 return -ERANGE;
60
61 /* Reserve 2 bytes for the BBM. */
62 region->offset = 2;
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020063 region->length = (mtd->oobsize / 2) - 2;
Peter Pandf1859e2018-08-16 17:30:13 +020064
65 return 0;
66}
67
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020068static const struct mtd_ooblayout_ops micron_8_ooblayout = {
69 .ecc = micron_8_ooblayout_ecc,
70 .rfree = micron_8_ooblayout_free,
Peter Pandf1859e2018-08-16 17:30:13 +020071};
72
Shivamurthy Shastricfe77252020-07-07 22:04:08 +020073static int micron_8_ecc_get_status(struct spinand_device *spinand,
74 u8 status)
Peter Pandf1859e2018-08-16 17:30:13 +020075{
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
99static const struct spinand_info micron_spinand_table[] = {
Shivamurthy Shastria44f6a92020-07-07 22:04:09 +0200100 /* M79A 2Gb 3.3V */
Peter Pandf1859e2018-08-16 17:30:13 +0200101 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 Shastricfe77252020-07-07 22:04:08 +0200108 SPINAND_ECCINFO(&micron_8_ooblayout,
109 micron_8_ecc_get_status)),
Shivamurthy Shastri8d06c5b2020-07-07 22:04:10 +0200110 /* 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(&micron_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(&micron_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(&micron_8_ooblayout,
139 micron_8_ecc_get_status)),
Shivamurthy Shastri5cbf17f2020-07-07 22:04:12 +0200140 /* 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(&micron_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(&micron_8_ooblayout,
159 micron_8_ecc_get_status)),
Peter Pandf1859e2018-08-16 17:30:13 +0200160};
161
162static 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 Shastri92ecb1a2020-07-07 22:04:11 +0200182static 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 Pandf1859e2018-08-16 17:30:13 +0200195static const struct spinand_manufacturer_ops micron_spinand_manuf_ops = {
196 .detect = micron_spinand_detect,
Shivamurthy Shastri92ecb1a2020-07-07 22:04:11 +0200197 .init = micron_spinand_init,
Peter Pandf1859e2018-08-16 17:30:13 +0200198};
199
200const struct spinand_manufacturer micron_spinand_manufacturer = {
201 .id = SPINAND_MFR_MICRON,
202 .name = "Micron",
203 .ops = &micron_spinand_manuf_ops,
204};