xref: /rk3399_rockchip-uboot/drivers/mtd/nand/spi/unim.c (revision 4e0fa9f6a6afd1c4db979a3d2e9f1e67d6e1f06f)
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_UNIM		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 
33 static int tx25g01_ooblayout_ecc(struct mtd_info *mtd, int section,
34 				 struct mtd_oob_region *region)
35 {
36 	if (section > 3)
37 		return -ERANGE;
38 
39 	region->offset = (16 * section) + 8;
40 	region->length = 8;
41 
42 	return 0;
43 }
44 
45 static int tx25g01_ooblayout_free(struct mtd_info *mtd, int section,
46 				  struct mtd_oob_region *region)
47 {
48 	if (section > 3)
49 		return -ERANGE;
50 
51 	region->offset = (16 * section) + 2;
52 	region->length = 6;
53 
54 	return 0;
55 }
56 
57 static const struct mtd_ooblayout_ops tx25g01_ooblayout = {
58 	.ecc = tx25g01_ooblayout_ecc,
59 	.rfree = tx25g01_ooblayout_free,
60 };
61 
62 /*
63  * ecc bits: 0xC0[4,6]
64  * [0b000], No bit errors were detected;
65  * [0b001, 0b011], 1~3 Bit errors were detected and corrected. Not
66  *	reach Flipping Bits;
67  * [0b100], Bit error count equals the bit flip
68  *	detection threshold
69  * others, Reserved.
70  */
71 static int tx25g01_ecc_get_status(struct spinand_device *spinand,
72 				  u8 status)
73 {
74 	u8 eccsr = (status & GENMASK(6, 4)) >> 2;
75 
76 	if (eccsr <= 7)
77 		return eccsr;
78 	else if (eccsr == 12)
79 		return 8;
80 	else
81 		return -EBADMSG;
82 }
83 
84 static const struct spinand_info unim_spinand_table[] = {
85 	SPINAND_INFO("TX25G01",
86 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xF1),
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(&tx25g01_ooblayout, tx25g01_ecc_get_status)),
94 };
95 
96 static const struct spinand_manufacturer_ops unim_spinand_manuf_ops = {
97 };
98 
99 const struct spinand_manufacturer unim_spinand_manufacturer = {
100 	.id = SPINAND_MFR_UNIM,
101 	.name = "UNIM",
102 	.chips = unim_spinand_table,
103 	.nchips = ARRAY_SIZE(unim_spinand_table),
104 	.ops = &unim_spinand_manuf_ops,
105 };
106