Skip to content

Commit b02308a

Browse files
Boris Brezillonmiquelraynal
authored andcommitted
mtd: spinand: Add initial support for the MX35LF1GE4AB chip
Add minimal support for the MX35LF1GE4AB SPI NAND chip. Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
1 parent 1075492 commit b02308a

4 files changed

Lines changed: 139 additions & 1 deletion

File tree

‎drivers/mtd/nand/spi/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# SPDX-License-Identifier: GPL-2.0
2-
spinand-objs := core.o micron.o winbond.o
2+
spinand-objs := core.o macronix.o micron.o winbond.o
33
obj-$(CONFIG_MTD_SPI_NAND) += spinand.o

‎drivers/mtd/nand/spi/core.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ static const struct nand_ops spinand_ops = {
764764
};
765765

766766
static const struct spinand_manufacturer *spinand_manufacturers[] = {
767+
&macronix_spinand_manufacturer,
767768
&micron_spinand_manufacturer,
768769
&winbond_spinand_manufacturer,
769770
};

‎drivers/mtd/nand/spi/macronix.c‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (c) 2018 Macronix
4+
*
5+
* Author: Boris Brezillon <boris.brezillon@bootlin.com>
6+
*/
7+
8+
#include <linux/device.h>
9+
#include <linux/kernel.h>
10+
#include <linux/mtd/spinand.h>
11+
12+
#define SPINAND_MFR_MACRONIX 0xC2
13+
14+
static SPINAND_OP_VARIANTS(read_cache_variants,
15+
SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
16+
SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
17+
SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
18+
SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
19+
20+
static SPINAND_OP_VARIANTS(write_cache_variants,
21+
SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
22+
SPINAND_PROG_LOAD(true, 0, NULL, 0));
23+
24+
static SPINAND_OP_VARIANTS(update_cache_variants,
25+
SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
26+
SPINAND_PROG_LOAD(false, 0, NULL, 0));
27+
28+
static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section,
29+
struct mtd_oob_region *region)
30+
{
31+
return -ERANGE;
32+
}
33+
34+
static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section,
35+
struct mtd_oob_region *region)
36+
{
37+
if (section)
38+
return -ERANGE;
39+
40+
region->offset = 2;
41+
region->length = mtd->oobsize - 2;
42+
43+
return 0;
44+
}
45+
46+
static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = {
47+
.ecc = mx35lfxge4ab_ooblayout_ecc,
48+
.free = mx35lfxge4ab_ooblayout_free,
49+
};
50+
51+
static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr)
52+
{
53+
struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1),
54+
SPI_MEM_OP_NO_ADDR,
55+
SPI_MEM_OP_DUMMY(1, 1),
56+
SPI_MEM_OP_DATA_IN(1, eccsr, 1));
57+
58+
return spi_mem_exec_op(spinand->spimem, &op);
59+
}
60+
61+
static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand,
62+
u8 status)
63+
{
64+
struct nand_device *nand = spinand_to_nand(spinand);
65+
u8 eccsr;
66+
67+
switch (status & STATUS_ECC_MASK) {
68+
case STATUS_ECC_NO_BITFLIPS:
69+
return 0;
70+
71+
case STATUS_ECC_UNCOR_ERROR:
72+
return -EBADMSG;
73+
74+
case STATUS_ECC_HAS_BITFLIPS:
75+
/*
76+
* Let's try to retrieve the real maximum number of bitflips
77+
* in order to avoid forcing the wear-leveling layer to move
78+
* data around if it's not necessary.
79+
*/
80+
if (mx35lf1ge4ab_get_eccsr(spinand, &eccsr))
81+
return nand->eccreq.strength;
82+
83+
if (WARN_ON(eccsr > nand->eccreq.strength || !eccsr))
84+
return nand->eccreq.strength;
85+
86+
return eccsr;
87+
88+
default:
89+
break;
90+
}
91+
92+
return -EINVAL;
93+
}
94+
95+
static const struct spinand_info macronix_spinand_table[] = {
96+
SPINAND_INFO("MX35LF1GE4AB", 0x12,
97+
NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
98+
NAND_ECCREQ(4, 512),
99+
SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
100+
&write_cache_variants,
101+
&update_cache_variants),
102+
SPINAND_HAS_QE_BIT,
103+
SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
104+
mx35lf1ge4ab_ecc_get_status)),
105+
};
106+
107+
static int macronix_spinand_detect(struct spinand_device *spinand)
108+
{
109+
u8 *id = spinand->id.data;
110+
int ret;
111+
112+
/*
113+
* Macronix SPI NAND read ID needs a dummy byte, so the first byte in
114+
* raw_id is garbage.
115+
*/
116+
if (id[1] != SPINAND_MFR_MACRONIX)
117+
return 0;
118+
119+
ret = spinand_match_and_init(spinand, macronix_spinand_table,
120+
ARRAY_SIZE(macronix_spinand_table),
121+
id[2]);
122+
if (ret)
123+
return ret;
124+
125+
return 1;
126+
}
127+
128+
static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
129+
.detect = macronix_spinand_detect,
130+
};
131+
132+
const struct spinand_manufacturer macronix_spinand_manufacturer = {
133+
.id = SPINAND_MFR_MACRONIX,
134+
.name = "Macronix",
135+
.ops = &macronix_spinand_manuf_ops,
136+
};

‎include/linux/mtd/spinand.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ struct spinand_manufacturer {
194194
};
195195

196196
/* SPI NAND manufacturers */
197+
extern const struct spinand_manufacturer macronix_spinand_manufacturer;
197198
extern const struct spinand_manufacturer micron_spinand_manufacturer;
198199
extern const struct spinand_manufacturer winbond_spinand_manufacturer;
199200

0 commit comments

Comments
 (0)