blob: 662c561e501b150ab84a12f958b684540dfbda41 [file] [log] [blame]
Boris Brezillonfa465252018-08-16 17:30:15 +02001// 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
16static 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
22static SPINAND_OP_VARIANTS(write_cache_variants,
23 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
24 SPINAND_PROG_LOAD(true, 0, NULL, 0));
25
26static SPINAND_OP_VARIANTS(update_cache_variants,
27 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
28 SPINAND_PROG_LOAD(false, 0, NULL, 0));
29
Miquel Raynal02b49792018-08-16 17:30:16 +020030static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section,
Boris Brezillonfa465252018-08-16 17:30:15 +020031 struct mtd_oob_region *region)
32{
33 return -ERANGE;
34}
35
Miquel Raynal02b49792018-08-16 17:30:16 +020036static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section,
Boris Brezillonfa465252018-08-16 17:30:15 +020037 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
Miquel Raynal02b49792018-08-16 17:30:16 +020048static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = {
49 .ecc = mx35lfxge4ab_ooblayout_ecc,
50 .free = mx35lfxge4ab_ooblayout_free,
Boris Brezillonfa465252018-08-16 17:30:15 +020051};
52
53static 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
63static 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
97static 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,
Miquel Raynal02b49792018-08-16 17:30:16 +0200105 SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
Boris Brezillonfa465252018-08-16 17:30:15 +0200106 mx35lf1ge4ab_ecc_get_status)),
Miquel Raynal02b49792018-08-16 17:30:16 +0200107 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)),
Boris Brezillonfa465252018-08-16 17:30:15 +0200115};
116
117static 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
138static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
139 .detect = macronix_spinand_detect,
140};
141
142const struct spinand_manufacturer macronix_spinand_manufacturer = {
143 .id = SPINAND_MFR_MACRONIX,
144 .name = "Macronix",
145 .ops = &macronix_spinand_manuf_ops,
146};