xref: /OK3568_Linux_fs/u-boot/drivers/mtd/nand/spi/fmsh.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd
4  *
5  * Authors:
6  *	Dingqiang Lin <jon.lin@rock-chips.com>
7  */
8 
9 #ifndef __UBOOT__
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #endif
13 #include <linux/mtd/spinand.h>
14 
15 #define SPINAND_MFR_FMSH		0xA1
16 
17 static SPINAND_OP_VARIANTS(read_cache_variants,
18 		SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
19 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
20 		SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
21 		SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
22 		SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
23 		SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
24 
25 static SPINAND_OP_VARIANTS(write_cache_variants,
26 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
27 		SPINAND_PROG_LOAD(true, 0, NULL, 0));
28 
29 static SPINAND_OP_VARIANTS(update_cache_variants,
30 		SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
31 		SPINAND_PROG_LOAD(false, 0, NULL, 0));
32 
fm25s01a_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)33 static int fm25s01a_ooblayout_ecc(struct mtd_info *mtd, int section,
34 				  struct mtd_oob_region *region)
35 {
36 	return -ERANGE;
37 }
38 
fm25s01a_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)39 static int fm25s01a_ooblayout_free(struct mtd_info *mtd, int section,
40 				   struct mtd_oob_region *region)
41 {
42 	if (section)
43 		return -ERANGE;
44 
45 	region->offset = 2;
46 	region->length = 62;
47 
48 	return 0;
49 }
50 
51 static const struct mtd_ooblayout_ops fm25s01a_ooblayout = {
52 	.ecc = fm25s01a_ooblayout_ecc,
53 	.rfree = fm25s01a_ooblayout_free,
54 };
55 
fm25s01_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)56 static int fm25s01_ooblayout_ecc(struct mtd_info *mtd, int section,
57 				 struct mtd_oob_region *region)
58 {
59 	if (section)
60 		return -ERANGE;
61 
62 	region->offset = 64;
63 	region->length = 64;
64 
65 	return 0;
66 }
67 
fm25s01_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)68 static int fm25s01_ooblayout_free(struct mtd_info *mtd, int section,
69 				  struct mtd_oob_region *region)
70 {
71 	if (section)
72 		return -ERANGE;
73 
74 	region->offset = 2;
75 	region->length = 62;
76 
77 	return 0;
78 }
79 
80 static const struct mtd_ooblayout_ops fm25s01_ooblayout = {
81 	.ecc = fm25s01_ooblayout_ecc,
82 	.rfree = fm25s01_ooblayout_free,
83 };
84 
85 /*
86  * ecc bits: 0xC0[4,6]
87  * [0b000], No bit errors were detected;
88  * [0b001] and [0b011], 1~6 Bit errors were detected and corrected. Not
89  *	reach Flipping Bits;
90  * [0b101], Bit error count equals the bit flip
91  *	detection threshold
92  * [0b010], Multiple bit errors were detected and
93  *	not corrected.
94  * others, Reserved.
95  */
fm25s01bi3_ecc_ecc_get_status(struct spinand_device * spinand,u8 status)96 static int fm25s01bi3_ecc_ecc_get_status(struct spinand_device *spinand,
97 					u8 status)
98 {
99 	struct nand_device *nand = spinand_to_nand(spinand);
100 	u8 eccsr = (status & GENMASK(6, 4)) >> 4;
101 
102 	if (eccsr <= 1 || eccsr == 3)
103 		return eccsr;
104 	else if (eccsr == 5)
105 		return nand->eccreq.strength;
106 	else
107 		return -EBADMSG;
108 }
109 
110 static const struct spinand_info fmsh_spinand_table[] = {
111 	SPINAND_INFO("FM25S01A",
112 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xE4),
113 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
114 		     NAND_ECCREQ(1, 512),
115 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
116 					      &write_cache_variants,
117 					      &update_cache_variants),
118 		     0,
119 		     SPINAND_ECCINFO(&fm25s01a_ooblayout, NULL)),
120 	SPINAND_INFO("FM25S02A",
121 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xE5),
122 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1),
123 		     NAND_ECCREQ(1, 512),
124 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
125 					      &write_cache_variants,
126 					      &update_cache_variants),
127 		     SPINAND_HAS_QE_BIT,
128 		     SPINAND_ECCINFO(&fm25s01a_ooblayout, NULL)),
129 	SPINAND_INFO("FM25S01",
130 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xA1),
131 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
132 		     NAND_ECCREQ(1, 512),
133 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
134 					      &write_cache_variants,
135 					      &update_cache_variants),
136 		     0,
137 		     SPINAND_ECCINFO(&fm25s01_ooblayout, NULL)),
138 	SPINAND_INFO("FM25LS01",
139 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xA5),
140 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
141 		     NAND_ECCREQ(1, 512),
142 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
143 					      &write_cache_variants,
144 					      &update_cache_variants),
145 		     0,
146 		     SPINAND_ECCINFO(&fm25s01_ooblayout, NULL)),
147 	SPINAND_INFO("FM25S01BI3",
148 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xD4),
149 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
150 		     NAND_ECCREQ(8, 512),
151 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
152 					      &write_cache_variants,
153 					      &update_cache_variants),
154 		     SPINAND_HAS_QE_BIT,
155 		     SPINAND_ECCINFO(&fm25s01_ooblayout, fm25s01bi3_ecc_ecc_get_status)),
156 };
157 
158 static const struct spinand_manufacturer_ops fmsh_spinand_manuf_ops = {
159 };
160 
161 const struct spinand_manufacturer fmsh_spinand_manufacturer = {
162 	.id = SPINAND_MFR_FMSH,
163 	.name = "FMSH",
164 	.chips = fmsh_spinand_table,
165 	.nchips = ARRAY_SIZE(fmsh_spinand_table),
166 	.ops = &fmsh_spinand_manuf_ops,
167 };
168