xref: /OK3568_Linux_fs/kernel/drivers/mtd/nand/spi/biwin.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
4  */
5 
6 #include <linux/device.h>
7 #include <linux/kernel.h>
8 #include <linux/mtd/spinand.h>
9 
10 #define SPINAND_MFR_BIWIN		0xBC
11 
12 #define BIWIN_CFG_BUF_READ		BIT(3)
13 #define BIWIN_STATUS_ECC_HAS_BITFLIPS_T	(3 << 4)
14 
15 static SPINAND_OP_VARIANTS(read_cache_variants,
16 		SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
17 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
18 		SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
19 		SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
20 		SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
21 		SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
22 
23 static SPINAND_OP_VARIANTS(write_cache_variants,
24 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
25 		SPINAND_PROG_LOAD(true, 0, NULL, 0));
26 
27 static SPINAND_OP_VARIANTS(update_cache_variants,
28 		SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
29 		SPINAND_PROG_LOAD(false, 0, NULL, 0));
30 
bwjx08k_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)31 static int bwjx08k_ooblayout_ecc(struct mtd_info *mtd, int section,
32 				 struct mtd_oob_region *region)
33 {
34 	if (section > 3)
35 		return -ERANGE;
36 
37 	region->offset = (16 * section) + 12;
38 	region->length = 4;
39 
40 	return 0;
41 }
42 
bwjx08k_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)43 static int bwjx08k_ooblayout_free(struct mtd_info *mtd, int section,
44 				  struct mtd_oob_region *region)
45 {
46 	if (section > 3)
47 		return -ERANGE;
48 
49 	region->offset = (16 * section) + 2;
50 	region->length = 10;
51 
52 	return 0;
53 }
54 
55 static const struct mtd_ooblayout_ops bwjx08k_ooblayout = {
56 	.ecc = bwjx08k_ooblayout_ecc,
57 	.free = bwjx08k_ooblayout_free,
58 };
59 
bwjx08k_ecc_get_status(struct spinand_device * spinand,u8 status)60 static int bwjx08k_ecc_get_status(struct spinand_device *spinand,
61 				  u8 status)
62 {
63 	struct nand_device *nand = spinand_to_nand(spinand);
64 
65 	switch (status & STATUS_ECC_MASK) {
66 	case STATUS_ECC_NO_BITFLIPS:
67 		return 0;
68 
69 	case STATUS_ECC_UNCOR_ERROR:
70 		return -EBADMSG;
71 
72 	case STATUS_ECC_HAS_BITFLIPS:
73 		return 1;
74 
75 	default:
76 		return nanddev_get_ecc_requirements(nand)->strength;
77 	}
78 
79 	return -EINVAL;
80 }
81 
82 /* Another set for the same id[2] devices in one series */
83 static const struct spinand_info biwin_spinand_table[] = {
84 	SPINAND_INFO("BWJX08K",
85 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xB3),
86 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
87 		     NAND_ECCREQ(8, 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(&bwjx08k_ooblayout,
93 				     bwjx08k_ecc_get_status)),
94 };
95 
96 static const struct spinand_manufacturer_ops biwin_spinand_manuf_ops = {
97 };
98 
99 const struct spinand_manufacturer biwin_spinand_manufacturer = {
100 	.id = SPINAND_MFR_BIWIN,
101 	.name = "BIWIN",
102 	.chips = biwin_spinand_table,
103 	.nchips = ARRAY_SIZE(biwin_spinand_table),
104 	.ops = &biwin_spinand_manuf_ops,
105 };
106