xref: /rk3399_rockchip-uboot/drivers/mtd/nand/spi/macronix.c (revision 258d2dcb26879538abc00fc64a8a34c45c046465)
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 #ifndef __UBOOT__
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #endif
12 #include <linux/mtd/spinand.h>
13 
14 #define SPINAND_MFR_MACRONIX		0xC2
15 
16 static SPINAND_OP_VARIANTS(read_cache_variants,
17 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
18 		SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
19 		SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
20 		SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
21 
22 static SPINAND_OP_VARIANTS(write_cache_variants,
23 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
24 		SPINAND_PROG_LOAD(true, 0, NULL, 0));
25 
26 static SPINAND_OP_VARIANTS(update_cache_variants,
27 		SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
28 		SPINAND_PROG_LOAD(false, 0, NULL, 0));
29 
30 static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section,
31 				      struct mtd_oob_region *region)
32 {
33 	return -ERANGE;
34 }
35 
36 static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section,
37 				       struct mtd_oob_region *region)
38 {
39 	if (section)
40 		return -ERANGE;
41 
42 	region->offset = 2;
43 	region->length = mtd->oobsize - 2;
44 
45 	return 0;
46 }
47 
48 static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = {
49 	.ecc = mx35lfxge4ab_ooblayout_ecc,
50 	.rfree = mx35lfxge4ab_ooblayout_free,
51 };
52 
53 static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr)
54 {
55 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1),
56 					  SPI_MEM_OP_NO_ADDR,
57 					  SPI_MEM_OP_DUMMY(1, 1),
58 					  SPI_MEM_OP_DATA_IN(1, eccsr, 1));
59 
60 	return spi_mem_exec_op(spinand->slave, &op);
61 }
62 
63 static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand,
64 				       u8 status)
65 {
66 	struct nand_device *nand = spinand_to_nand(spinand);
67 	u8 eccsr;
68 
69 	switch (status & STATUS_ECC_MASK) {
70 	case STATUS_ECC_NO_BITFLIPS:
71 		return 0;
72 
73 	case STATUS_ECC_UNCOR_ERROR:
74 		return -EBADMSG;
75 
76 	case STATUS_ECC_HAS_BITFLIPS:
77 		/*
78 		 * Let's try to retrieve the real maximum number of bitflips
79 		 * in order to avoid forcing the wear-leveling layer to move
80 		 * data around if it's not necessary.
81 		 */
82 		if (mx35lf1ge4ab_get_eccsr(spinand, &eccsr))
83 			return nand->eccreq.strength;
84 
85 		if (WARN_ON(eccsr > nand->eccreq.strength || !eccsr))
86 			return nand->eccreq.strength;
87 
88 		return eccsr;
89 
90 	default:
91 		break;
92 	}
93 
94 	return -EINVAL;
95 }
96 
97 static const struct spinand_info macronix_spinand_table[] = {
98 	SPINAND_INFO("MX35LF1GE4AB", 0x12,
99 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
100 		     NAND_ECCREQ(4, 512),
101 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
102 					      &write_cache_variants,
103 					      &update_cache_variants),
104 		     SPINAND_HAS_QE_BIT,
105 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
106 				     mx35lf1ge4ab_ecc_get_status)),
107 	SPINAND_INFO("MX35LF2GE4AB", 0x22,
108 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1),
109 		     NAND_ECCREQ(4, 512),
110 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
111 					      &write_cache_variants,
112 					      &update_cache_variants),
113 		     SPINAND_HAS_QE_BIT,
114 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
115 	SPINAND_INFO("MX35LF2GE4AD", 0x26,
116 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1),
117 		     NAND_ECCREQ(8, 512),
118 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
119 					      &write_cache_variants,
120 					      &update_cache_variants),
121 		     SPINAND_HAS_QE_BIT,
122 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
123 				     mx35lf1ge4ab_ecc_get_status)),
124 	SPINAND_INFO("MX35LF4GE4AD", 0x37,
125 		     NAND_MEMORG(1, 4096, 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 		     SPINAND_HAS_QE_BIT,
131 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
132 				     mx35lf1ge4ab_ecc_get_status)),
133 };
134 
135 static int macronix_spinand_detect(struct spinand_device *spinand)
136 {
137 	u8 *id = spinand->id.data;
138 	int ret;
139 
140 	/*
141 	 * Macronix SPI NAND read ID needs a dummy byte, so the first byte in
142 	 * raw_id is garbage.
143 	 */
144 	if (id[1] != SPINAND_MFR_MACRONIX)
145 		return 0;
146 
147 	ret = spinand_match_and_init(spinand, macronix_spinand_table,
148 				     ARRAY_SIZE(macronix_spinand_table),
149 				     id[2]);
150 	if (ret)
151 		return ret;
152 
153 	return 1;
154 }
155 
156 static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
157 	.detect = macronix_spinand_detect,
158 };
159 
160 const struct spinand_manufacturer macronix_spinand_manufacturer = {
161 	.id = SPINAND_MFR_MACRONIX,
162 	.name = "Macronix",
163 	.ops = &macronix_spinand_manuf_ops,
164 };
165