blob: 508088b8b483c02a24d4d5d5ccfe072021f295bc [file] [log] [blame]
Peter Pan883d8772018-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 Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Peter Pan883d8772018-08-16 17:30:13 +020011#include <linux/device.h>
12#include <linux/kernel.h>
13#endif
Simon Glasscd93d622020-05-10 11:40:13 -060014#include <linux/bitops.h>
Peter Pan883d8772018-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 Shastri720fcb22020-07-07 22:04:11 +020025#define MICRON_CFG_CR BIT(0)
26
Peter Pan883d8772018-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 Shastri1527ec42020-07-07 22:04:08 +020043static int micron_8_ooblayout_ecc(struct mtd_info *mtd, int section,
44 struct mtd_oob_region *region)
Peter Pan883d8772018-08-16 17:30:13 +020045{
46 if (section)
47 return -ERANGE;
48
Shivamurthy Shastri1527ec42020-07-07 22:04:08 +020049 region->offset = mtd->oobsize / 2;
50 region->length = mtd->oobsize / 2;
Peter Pan883d8772018-08-16 17:30:13 +020051
52 return 0;
53}
54
Shivamurthy Shastri1527ec42020-07-07 22:04:08 +020055static int micron_8_ooblayout_free(struct mtd_info *mtd, int section,
56 struct mtd_oob_region *region)
Peter Pan883d8772018-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 Shastri1527ec42020-07-07 22:04:08 +020063 region->length = (mtd->oobsize / 2) - 2;
Peter Pan883d8772018-08-16 17:30:13 +020064
65 return 0;
66}
67
Shivamurthy Shastri1527ec42020-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 Pan883d8772018-08-16 17:30:13 +020071};
72
Shivamurthy Shastri1527ec42020-07-07 22:04:08 +020073static int micron_8_ecc_get_status(struct spinand_device *spinand,
74 u8 status)
Peter Pan883d8772018-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 Shastri92fc25d2020-07-07 22:04:09 +0200100 /* M79A 2Gb 3.3V */
Peter Pan883d8772018-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 Shastri1527ec42020-07-07 22:04:08 +0200108 SPINAND_ECCINFO(&micron_8_ooblayout,
109 micron_8_ecc_get_status)),
Shivamurthy Shastri5cf049c2020-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)),
Peter Pan883d8772018-08-16 17:30:13 +0200140};
141
142static 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 Shastri720fcb22020-07-07 22:04:11 +0200162static 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 Pan883d8772018-08-16 17:30:13 +0200175static const struct spinand_manufacturer_ops micron_spinand_manuf_ops = {
176 .detect = micron_spinand_detect,
Shivamurthy Shastri720fcb22020-07-07 22:04:11 +0200177 .init = micron_spinand_init,
Peter Pan883d8772018-08-16 17:30:13 +0200178};
179
180const struct spinand_manufacturer micron_spinand_manufacturer = {
181 .id = SPINAND_MFR_MICRON,
182 .name = "Micron",
183 .ops = &micron_spinand_manuf_ops,
184};