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 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/mtd/spinand.h>
12
13 #define SPINAND_MFR_JSC 0xBF
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
js28u1gqscahg_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)31 static int js28u1gqscahg_ooblayout_ecc(struct mtd_info *mtd, int section,
32 struct mtd_oob_region *region)
33 {
34 if (section)
35 return -ERANGE;
36
37 region->offset = mtd->oobsize / 2;
38 region->length = mtd->oobsize / 2;
39
40 return 0;
41 }
42
js28u1gqscahg_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)43 static int js28u1gqscahg_ooblayout_free(struct mtd_info *mtd, int section,
44 struct mtd_oob_region *region)
45 {
46 if (section)
47 return -ERANGE;
48
49 /* Reserve 2 bytes for the BBM. */
50 region->offset = 2;
51 region->length = mtd->oobsize / 2 - 2;
52
53 return 0;
54 }
55
56 static const struct mtd_ooblayout_ops js28u1gqscahg_ooblayout = {
57 .ecc = js28u1gqscahg_ooblayout_ecc,
58 .free = js28u1gqscahg_ooblayout_free,
59 };
60
61 /*
62 * ecc bits: 0xC0[4,6]
63 * [0b000], No bit errors were detected;
64 * [0b001, 0b011], 1~3 Bit errors were detected and corrected. Not
65 * reach Flipping Bits;
66 * [0b100], Bit error count equals the bit flip
67 * detection threshold
68 * others, Reserved.
69 */
js28u1gqscahg_ecc_get_status(struct spinand_device * spinand,u8 status)70 static int js28u1gqscahg_ecc_get_status(struct spinand_device *spinand,
71 u8 status)
72 {
73 struct nand_device *nand = spinand_to_nand(spinand);
74 u8 eccsr = (status & GENMASK(6, 4)) >> 4;
75
76 if (eccsr < 4)
77 return eccsr;
78 else if (eccsr == 4)
79 return nanddev_get_ecc_requirements(nand)->strength;
80 else
81 return -EBADMSG;
82 }
83
84 static const struct spinand_info jsc_spinand_table[] = {
85 SPINAND_INFO("JS28U1GQSCAHG-83",
86 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x21),
87 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 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(&js28u1gqscahg_ooblayout, js28u1gqscahg_ecc_get_status)),
94 };
95
96 static const struct spinand_manufacturer_ops jsc_spinand_manuf_ops = {
97 };
98
99 const struct spinand_manufacturer jsc_spinand_manufacturer = {
100 .id = SPINAND_MFR_JSC,
101 .name = "JSC",
102 .chips = jsc_spinand_table,
103 .nchips = ARRAY_SIZE(jsc_spinand_table),
104 .ops = &jsc_spinand_manuf_ops,
105 };
106