1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2018 Macronix
4 *
5 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6 */
7
8 #ifndef __UBOOT__
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #endif
12 #include <linux/mtd/spinand.h>
13
14 #define SPINAND_MFR_MACRONIX 0xC2
15
16 static SPINAND_OP_VARIANTS(read_cache_variants,
17 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
18 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
19 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
20 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
21
22 static SPINAND_OP_VARIANTS(write_cache_variants,
23 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
24 SPINAND_PROG_LOAD(true, 0, NULL, 0));
25
26 static SPINAND_OP_VARIANTS(update_cache_variants,
27 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
28 SPINAND_PROG_LOAD(false, 0, NULL, 0));
29
mx35lfxge4ab_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)30 static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section,
31 struct mtd_oob_region *region)
32 {
33 return -ERANGE;
34 }
35
mx35lfxge4ab_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)36 static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section,
37 struct mtd_oob_region *region)
38 {
39 if (section)
40 return -ERANGE;
41
42 region->offset = 2;
43 region->length = mtd->oobsize - 2;
44
45 return 0;
46 }
47
48 static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = {
49 .ecc = mx35lfxge4ab_ooblayout_ecc,
50 .free = mx35lfxge4ab_ooblayout_free,
51 };
52
mx35lf1ge4ab_get_eccsr(struct spinand_device * spinand,u8 * eccsr)53 static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr)
54 {
55 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1),
56 SPI_MEM_OP_NO_ADDR,
57 SPI_MEM_OP_DUMMY(1, 1),
58 SPI_MEM_OP_DATA_IN(1, eccsr, 1));
59
60 return spi_mem_exec_op(spinand->slave, &op);
61 }
62
mx35lf1ge4ab_ecc_get_status(struct spinand_device * spinand,u8 status)63 static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand,
64 u8 status)
65 {
66 struct nand_device *nand = spinand_to_nand(spinand);
67 u8 eccsr;
68
69 switch (status & STATUS_ECC_MASK) {
70 case STATUS_ECC_NO_BITFLIPS:
71 return 0;
72
73 case STATUS_ECC_UNCOR_ERROR:
74 return -EBADMSG;
75
76 case STATUS_ECC_HAS_BITFLIPS:
77 /*
78 * Let's try to retrieve the real maximum number of bitflips
79 * in order to avoid forcing the wear-leveling layer to move
80 * data around if it's not necessary.
81 */
82 if (mx35lf1ge4ab_get_eccsr(spinand, &eccsr))
83 return nand->eccreq.strength;
84
85 if (WARN_ON(eccsr > nand->eccreq.strength || !eccsr))
86 return nand->eccreq.strength;
87
88 return eccsr;
89
90 default:
91 break;
92 }
93
94 return -EINVAL;
95 }
96
97 static const struct spinand_info macronix_spinand_table[] = {
98 SPINAND_INFO("MX35LF1GE4AB", 0x12,
99 NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
100 NAND_ECCREQ(4, 512),
101 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
102 &write_cache_variants,
103 &update_cache_variants),
104 SPINAND_HAS_QE_BIT,
105 SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
106 mx35lf1ge4ab_ecc_get_status)),
107 SPINAND_INFO("MX35LF2GE4AB", 0x22,
108 NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1),
109 NAND_ECCREQ(4, 512),
110 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
111 &write_cache_variants,
112 &update_cache_variants),
113 SPINAND_HAS_QE_BIT,
114 SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
115 };
116
macronix_spinand_detect(struct spinand_device * spinand)117 static int macronix_spinand_detect(struct spinand_device *spinand)
118 {
119 u8 *id = spinand->id.data;
120 int ret;
121
122 /*
123 * Macronix SPI NAND read ID needs a dummy byte, so the first byte in
124 * raw_id is garbage.
125 */
126 if (id[1] != SPINAND_MFR_MACRONIX)
127 return 0;
128
129 ret = spinand_match_and_init(spinand, macronix_spinand_table,
130 ARRAY_SIZE(macronix_spinand_table),
131 id[2]);
132 if (ret)
133 return ret;
134
135 return 1;
136 }
137
138 static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
139 .detect = macronix_spinand_detect,
140 };
141
142 const struct spinand_manufacturer macronix_spinand_manufacturer = {
143 .id = SPINAND_MFR_MACRONIX,
144 .name = "Macronix",
145 .ops = ¯onix_spinand_manuf_ops,
146 };
147