xref: /OK3568_Linux_fs/u-boot/drivers/mtd/nand/spi/skyhigh.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022 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_SKYHIGH		0x01
16 
17 #define SKYHIGH_STATUS_ECC_1_2_BITFLIPS	(1 << 4)
18 #define SKYHIGH_STATUS_ECC_3_4_BITFLIPS	(2 << 4)
19 #define SKYHIGH_STATUS_ECC_UNCOR_ERROR	(3 << 4)
20 
21 static SPINAND_OP_VARIANTS(read_cache_variants,
22 		SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
23 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
24 		SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
25 		SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
26 		SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
27 		SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
28 
29 static SPINAND_OP_VARIANTS(write_cache_variants,
30 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
31 		SPINAND_PROG_LOAD(true, 0, NULL, 0));
32 
33 static SPINAND_OP_VARIANTS(update_cache_variants,
34 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
35 		SPINAND_PROG_LOAD(true, 0, NULL, 0));
36 
s35ml04g3_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)37 static int s35ml04g3_ooblayout_ecc(struct mtd_info *mtd, int section,
38 				   struct mtd_oob_region *region)
39 {
40 	return -ERANGE;
41 }
42 
s35ml04g3_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)43 static int s35ml04g3_ooblayout_free(struct mtd_info *mtd, int section,
44 				    struct mtd_oob_region *region)
45 {
46 	if (section)
47 		return -ERANGE;
48 
49 	region->offset = 2;
50 	region->length = mtd->oobsize - 2;
51 
52 	return 0;
53 }
54 
55 static const struct mtd_ooblayout_ops s35ml04g3_ooblayout = {
56 	.ecc = s35ml04g3_ooblayout_ecc,
57 	.rfree = s35ml04g3_ooblayout_free,
58 };
59 
60 
s35ml0xg3_ecc_get_status(struct spinand_device * spinand,u8 status)61 static int s35ml0xg3_ecc_get_status(struct spinand_device *spinand,
62 				   u8 status)
63 {
64 	struct nand_device *nand = spinand_to_nand(spinand);
65 
66 	switch (status & STATUS_ECC_MASK) {
67 	case STATUS_ECC_NO_BITFLIPS:
68 		return 0;
69 
70 	case SKYHIGH_STATUS_ECC_UNCOR_ERROR:
71 		return -EBADMSG;
72 
73 	case SKYHIGH_STATUS_ECC_1_2_BITFLIPS:
74 		return 2;
75 
76 	default:
77 		return nand->eccreq.strength;
78 	}
79 
80 	return -EINVAL;
81 }
82 
83 static const struct spinand_info skyhigh_spinand_table[] = {
84 	SPINAND_INFO("S35ML01G3",
85 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x15),
86 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 2, 1, 1),
87 		     NAND_ECCREQ(4, 512),
88 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
89 					      &write_cache_variants,
90 					      &update_cache_variants),
91 		     SPINAND_HAS_QE_BIT,
92 		     SPINAND_ECCINFO(&s35ml04g3_ooblayout, s35ml0xg3_ecc_get_status)),
93 	SPINAND_INFO("S35ML02G3",
94 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x25),
95 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 2, 1, 1),
96 		     NAND_ECCREQ(4, 512),
97 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
98 					      &write_cache_variants,
99 					      &update_cache_variants),
100 		     SPINAND_HAS_QE_BIT,
101 		     SPINAND_ECCINFO(&s35ml04g3_ooblayout, s35ml0xg3_ecc_get_status)),
102 	SPINAND_INFO("S35ML04G3",
103 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x35),
104 		     NAND_MEMORG(1, 2048, 128, 64, 4096, 2, 1, 1),
105 		     NAND_ECCREQ(4, 512),
106 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
107 					      &write_cache_variants,
108 					      &update_cache_variants),
109 		     SPINAND_HAS_QE_BIT,
110 		     SPINAND_ECCINFO(&s35ml04g3_ooblayout, s35ml0xg3_ecc_get_status)),
111 };
112 
113 static const struct spinand_manufacturer_ops skyhigh_spinand_manuf_ops = {
114 };
115 
116 const struct spinand_manufacturer skyhigh_spinand_manufacturer = {
117 	.id = SPINAND_MFR_SKYHIGH,
118 	.name = "skyhigh",
119 	.chips = skyhigh_spinand_table,
120 	.nchips = ARRAY_SIZE(skyhigh_spinand_table),
121 	.ops = &skyhigh_spinand_manuf_ops,
122 };
123