xref: /linux/drivers/mtd/nand/spi/foresee.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (c) 2023, SberDevices. All Rights Reserved.
4  *
5  * Author: Martin Kurbanov <mmkurbanov@salutedevices.com>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/mtd/spinand.h>
11 
12 #define SPINAND_MFR_FORESEE		0xCD
13 
14 static SPINAND_OP_VARIANTS(read_cache_variants,
15 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
16 		SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
17 		SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
18 		SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
19 
20 static SPINAND_OP_VARIANTS(write_cache_variants,
21 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
22 		SPINAND_PROG_LOAD(true, 0, NULL, 0));
23 
24 static SPINAND_OP_VARIANTS(update_cache_variants,
25 		SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
26 		SPINAND_PROG_LOAD(false, 0, NULL, 0));
27 
f35sqa002g_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)28 static int f35sqa002g_ooblayout_ecc(struct mtd_info *mtd, int section,
29 				    struct mtd_oob_region *region)
30 {
31 	return -ERANGE;
32 }
33 
f35sqa002g_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)34 static int f35sqa002g_ooblayout_free(struct mtd_info *mtd, int section,
35 				     struct mtd_oob_region *region)
36 {
37 	if (section)
38 		return -ERANGE;
39 
40 	/* Reserve 2 bytes for the BBM. */
41 	region->offset = 2;
42 	region->length = 62;
43 
44 	return 0;
45 }
46 
47 static const struct mtd_ooblayout_ops f35sqa002g_ooblayout = {
48 	.ecc = f35sqa002g_ooblayout_ecc,
49 	.free = f35sqa002g_ooblayout_free,
50 };
51 
f35sqa002g_ecc_get_status(struct spinand_device * spinand,u8 status)52 static int f35sqa002g_ecc_get_status(struct spinand_device *spinand, u8 status)
53 {
54 	struct nand_device *nand = spinand_to_nand(spinand);
55 
56 	switch (status & STATUS_ECC_MASK) {
57 	case STATUS_ECC_NO_BITFLIPS:
58 		return 0;
59 
60 	case STATUS_ECC_HAS_BITFLIPS:
61 		return nanddev_get_ecc_conf(nand)->strength;
62 
63 	default:
64 		break;
65 	}
66 
67 	/* More than 1-bit error was detected in one or more sectors and
68 	 * cannot be corrected.
69 	 */
70 	return -EBADMSG;
71 }
72 
73 static const struct spinand_info foresee_spinand_table[] = {
74 	SPINAND_INFO("F35SQA002G",
75 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x72, 0x72),
76 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
77 		     NAND_ECCREQ(1, 512),
78 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
79 					      &write_cache_variants,
80 					      &update_cache_variants),
81 		     SPINAND_HAS_QE_BIT,
82 		     SPINAND_ECCINFO(&f35sqa002g_ooblayout,
83 				     f35sqa002g_ecc_get_status)),
84 };
85 
86 static const struct spinand_manufacturer_ops foresee_spinand_manuf_ops = {
87 };
88 
89 const struct spinand_manufacturer foresee_spinand_manufacturer = {
90 	.id = SPINAND_MFR_FORESEE,
91 	.name = "FORESEE",
92 	.chips = foresee_spinand_table,
93 	.nchips = ARRAY_SIZE(foresee_spinand_table),
94 	.ops = &foresee_spinand_manuf_ops,
95 };
96