xref: /rk3399_rockchip-uboot/drivers/mtd/nand/spi/jsc.c (revision 156fbe7cda4b7fa76135bfe4f47f55d5f0cc51a7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 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_JSC		0xBF
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 
33 static int js28u1gqscahg_ooblayout_ecc(struct mtd_info *mtd, int section,
34 				       struct mtd_oob_region *region)
35 {
36 	if (section)
37 		return -ERANGE;
38 
39 	region->offset = mtd->oobsize / 2;
40 	region->length = mtd->oobsize / 2;
41 
42 	return 0;
43 }
44 
45 static int js28u1gqscahg_ooblayout_free(struct mtd_info *mtd, int section,
46 					struct mtd_oob_region *region)
47 {
48 	if (section)
49 		return -ERANGE;
50 
51 	/* Reserve 2 bytes for the BBM. */
52 	region->offset = 2;
53 	region->length = mtd->oobsize / 2 - 2;
54 
55 	return 0;
56 }
57 
58 static const struct mtd_ooblayout_ops js28u1gqscahg_ooblayout = {
59 	.ecc = js28u1gqscahg_ooblayout_ecc,
60 	.rfree = js28u1gqscahg_ooblayout_free,
61 };
62 
63 /*
64  * ecc bits: 0xC0[4,6]
65  * [0b000], No bit errors were detected;
66  * [0b001, 0b011], 1~3 Bit errors were detected and corrected. Not
67  *	reach Flipping Bits;
68  * [0b100], Bit error count equals the bit flip
69  *	detection threshold
70  * others, Reserved.
71  */
72 static int js28u1gqscahg_ecc_get_status(struct spinand_device *spinand,
73 					u8 status)
74 {
75 	u8 eccsr = (status & GENMASK(6, 4)) >> 2;
76 
77 	if (eccsr <= 7)
78 		return eccsr;
79 	else if (eccsr == 12)
80 		return 8;
81 	else
82 		return -EBADMSG;
83 }
84 
85 static const struct spinand_info jsc_spinand_table[] = {
86 	SPINAND_INFO("JS28U1GQSCAHG-83", 0x21,
87 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
88 		     NAND_ECCREQ(4, 512),
89 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
90 					      &write_cache_variants,
91 					      &update_cache_variants),
92 		     SPINAND_HAS_QE_BIT,
93 		     SPINAND_ECCINFO(&js28u1gqscahg_ooblayout, js28u1gqscahg_ecc_get_status)),
94 };
95 
96 /**
97  * jsc_spinand_detect - initialize device related part in spinand_device
98  * struct if it is a JSC device.
99  * @spinand: SPI NAND device structure
100  */
101 static int jsc_spinand_detect(struct spinand_device *spinand)
102 {
103 	u8 *id = spinand->id.data;
104 	int ret;
105 
106 	/*
107 	 * JSC SPI NAND read ID need a dummy byte,
108 	 * so the first byte in raw_id is dummy.
109 	 */
110 	if (id[1] != SPINAND_MFR_JSC)
111 		return 0;
112 
113 	ret = spinand_match_and_init(spinand, jsc_spinand_table,
114 				     ARRAY_SIZE(jsc_spinand_table),
115 				     id[2]);
116 
117 	if (ret)
118 		return ret;
119 
120 	return 1;
121 }
122 
123 static const struct spinand_manufacturer_ops jsc_spinand_manuf_ops = {
124 	.detect = jsc_spinand_detect,
125 };
126 
127 const struct spinand_manufacturer jsc_spinand_manufacturer = {
128 	.id = SPINAND_MFR_JSC,
129 	.name = "JSC",
130 	.ops = &jsc_spinand_manuf_ops,
131 };
132