xref: /rk3399_rockchip-uboot/drivers/mtd/nand/spi/kingston.c (revision 534e5d47c0a0fbcbd4683ec669edb0108801e0fc)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2025 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_KINGSTON		0x98
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 
spi004_sdeg_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)33 static int spi004_sdeg_ooblayout_ecc(struct mtd_info *mtd, int section,
34 				     struct mtd_oob_region *region)
35 {
36 	if (section > 0)
37 		return -ERANGE;
38 
39 	region->offset = mtd->oobsize / 2;
40 	region->length = mtd->oobsize / 2;
41 
42 	return 0;
43 }
44 
spi004_sdeg_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)45 static int spi004_sdeg_ooblayout_free(struct mtd_info *mtd, int section,
46 				      struct mtd_oob_region *region)
47 {
48 	if (section)
49 		return -ERANGE;
50 
51 	/* 2 bytes reserved for 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 spi004_sdeg_ooblayout = {
59 	.ecc = spi004_sdeg_ooblayout_ecc,
60 	.rfree = spi004_sdeg_ooblayout_free,
61 };
62 
63 /*
64  * ecc bits: 0xC0[4,6]
65  * [0b000], No bit errors were detected;
66  * [0b001, 0b101], 3~7 Bit errors were detected and corrected. Not
67  *	reach Flipping Bits;
68  * [0b110], Bit error count equals the bit flip detection threshold
69  * [0b111], Bit errors greater than ECC capability(8 bits) and not corrected;
70  */
spi004_sdeg_ecc_get_status(struct spinand_device * spinand,u8 status)71 static int spi004_sdeg_ecc_get_status(struct spinand_device *spinand, u8 status)
72 {
73 	struct nand_device *nand = spinand_to_nand(spinand);
74 	u8 eccsr = (status & GENMASK(6, 4)) >> 4;
75 
76 	if (eccsr == 0)
77 		return 0;
78 	else if (eccsr < 6)
79 		return eccsr + 2;
80 	else if (eccsr == 6)
81 		return nand->eccreq.strength;
82 	else
83 		return -EBADMSG;
84 }
85 
86 static const struct spinand_info kingston_spinand_table[] = {
87 	SPINAND_INFO("SPI004-SDEG",
88 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x53),
89 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
90 		     NAND_ECCREQ(8, 512),
91 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
92 					      &write_cache_variants,
93 					      &update_cache_variants),
94 		     SPINAND_HAS_QE_BIT,
95 		     SPINAND_ECCINFO(&spi004_sdeg_ooblayout, spi004_sdeg_ecc_get_status)),
96 };
97 
98 static const struct spinand_manufacturer_ops kingston_spinand_manuf_ops = {
99 };
100 
101 const struct spinand_manufacturer kingston_spinand_manufacturer = {
102 	.id = SPINAND_MFR_KINGSTON,
103 	.name = "kingston",
104 	.chips = kingston_spinand_table,
105 	.nchips = ARRAY_SIZE(kingston_spinand_table),
106 	.ops = &kingston_spinand_manuf_ops,
107 };
108