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_ETRON 0xD5
11
12 static SPINAND_OP_VARIANTS(read_cache_variants,
13 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
14 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
15 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_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
em73c044vcf_oh_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)28 static int em73c044vcf_oh_ooblayout_ecc(struct mtd_info *mtd, int section,
29 struct mtd_oob_region *region)
30 {
31 if (section > 3)
32 return -ERANGE;
33
34 region->offset = (16 * section) + 8;
35 region->length = 8;
36
37 return 0;
38 }
39
em73c044vcf_oh_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)40 static int em73c044vcf_oh_ooblayout_free(struct mtd_info *mtd, int section,
41 struct mtd_oob_region *region)
42 {
43 if (section > 3)
44 return -ERANGE;
45
46 region->offset = (16 * section) + 2;
47 region->length = 6;
48
49 return 0;
50 }
51
52 static const struct mtd_ooblayout_ops em73c044vcf_oh_ooblayout = {
53 .ecc = em73c044vcf_oh_ooblayout_ecc,
54 .free = em73c044vcf_oh_ooblayout_free,
55 };
56
em73c044vcf_oh_ecc_get_status(struct spinand_device * spinand,u8 status)57 static int em73c044vcf_oh_ecc_get_status(struct spinand_device *spinand,
58 u8 status)
59 {
60 struct nand_device *nand = spinand_to_nand(spinand);
61
62 switch (status & STATUS_ECC_MASK) {
63 case STATUS_ECC_NO_BITFLIPS:
64 return 0;
65
66 case STATUS_ECC_UNCOR_ERROR:
67 return -EBADMSG;
68
69 case STATUS_ECC_HAS_BITFLIPS:
70 return 1;
71
72 default:
73 return nanddev_get_ecc_requirements(nand)->strength;
74 }
75
76 return -EINVAL;
77 }
78
79 static const struct spinand_info etron_spinand_table[] = {
80 SPINAND_INFO("EM73C044VCF-0H",
81 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0x36),
82 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
83 NAND_ECCREQ(4, 512),
84 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
85 &write_cache_variants,
86 &update_cache_variants),
87 SPINAND_HAS_QE_BIT,
88 SPINAND_ECCINFO(&em73c044vcf_oh_ooblayout,
89 em73c044vcf_oh_ecc_get_status)),
90 };
91
92 static const struct spinand_manufacturer_ops etron_spinand_manuf_ops = {
93 };
94
95 const struct spinand_manufacturer etron_spinand_manufacturer = {
96 .id = SPINAND_MFR_ETRON,
97 .name = "Etron",
98 .chips = etron_spinand_table,
99 .nchips = ARRAY_SIZE(etron_spinand_table),
100 .ops = &etron_spinand_manuf_ops,
101 };
102