xref: /rk3399_rockchip-uboot/drivers/mtd/nand/spi/biwin.c (revision c3e08fa050aa3e8eb31fbd76f4d1e28c00ffe0b4)
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
4  */
5 
6 #ifndef __UBOOT__
7 #include <linux/device.h>
8 #include <linux/kernel.h>
9 #endif
10 #include <linux/mtd/spinand.h>
11 
12 #define SPINAND_MFR_BIWIN		0xBC
13 
14 #define BIWIN_CFG_BUF_READ		BIT(3)
15 #define BIWIN_STATUS_ECC_HAS_BITFLIPS_T	(3 << 4)
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 bwjx08k_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) + 12;
40 	region->length = 4;
41 
42 	return 0;
43 }
44 
45 static int bwjx08k_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 = 10;
53 
54 	return 0;
55 }
56 
57 static const struct mtd_ooblayout_ops bwjx08k_ooblayout = {
58 	.ecc = bwjx08k_ooblayout_ecc,
59 	.rfree = bwjx08k_ooblayout_free,
60 };
61 
62 static int bwjx08k_ecc_get_status(struct spinand_device *spinand,
63 				  u8 status)
64 {
65 	struct nand_device *nand = spinand_to_nand(spinand);
66 
67 	switch (status & STATUS_ECC_MASK) {
68 	case STATUS_ECC_NO_BITFLIPS:
69 		return 0;
70 
71 	case STATUS_ECC_UNCOR_ERROR:
72 		return -EBADMSG;
73 
74 	case STATUS_ECC_HAS_BITFLIPS:
75 		return 1;
76 
77 	default:
78 		return nand->eccreq.strength;
79 	}
80 
81 	return -EINVAL;
82 }
83 
84 /* Another set for the same id[2] devices in one series */
85 static const struct spinand_info biwin_spinand_table[] = {
86 	SPINAND_INFO("BWJX08K", 0xB3,
87 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 1, 1, 1),
88 		     NAND_ECCREQ(8, 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(&bwjx08k_ooblayout,
94 				     bwjx08k_ecc_get_status)),
95 };
96 
97 /**
98  * biwin_spinand_detect - initialize device related part in spinand_device
99  * struct if it is a Winbond device.
100  * @spinand: SPI NAND device structure
101  */
102 static int biwin_spinand_detect(struct spinand_device *spinand)
103 {
104 	u8 *id = spinand->id.data;
105 	int ret;
106 
107 	/*
108 	 * BIWIN SPI NAND read ID need a dummy byte,
109 	 * so the first byte in raw_id is dummy.
110 	 */
111 	if (id[1] != SPINAND_MFR_BIWIN)
112 		return 0;
113 
114 	ret = spinand_match_and_init(spinand, biwin_spinand_table,
115 				     ARRAY_SIZE(biwin_spinand_table), id[2]);
116 	if (ret)
117 		return ret;
118 
119 	return 1;
120 }
121 
122 static const struct spinand_manufacturer_ops biwin_spinand_manuf_ops = {
123 	.detect = biwin_spinand_detect,
124 };
125 
126 const struct spinand_manufacturer biwin_spinand_manufacturer = {
127 	.id = SPINAND_MFR_BIWIN,
128 	.name = "BIWIN",
129 	.ops = &biwin_spinand_manuf_ops,
130 };
131