blob: 67d092be2cf191b649b60c4daa16c1d99bdd769c [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__
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Boris Brezillonfa465252018-08-16 17:30:15 +020010#include <linux/device.h>
11#include <linux/kernel.h>
12#endif
13#include <linux/mtd/spinand.h>
14
15#define SPINAND_MFR_MACRONIX 0xC2
16
17static SPINAND_OP_VARIANTS(read_cache_variants,
18 SPINAND_PAGE_READ_FROM_CACHE_X4_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
23static SPINAND_OP_VARIANTS(write_cache_variants,
24 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
25 SPINAND_PROG_LOAD(true, 0, NULL, 0));
26
27static SPINAND_OP_VARIANTS(update_cache_variants,
28 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
29 SPINAND_PROG_LOAD(false, 0, NULL, 0));
30
Miquel Raynal02b49792018-08-16 17:30:16 +020031static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section,
Boris Brezillonfa465252018-08-16 17:30:15 +020032 struct mtd_oob_region *region)
33{
34 return -ERANGE;
35}
36
Miquel Raynal02b49792018-08-16 17:30:16 +020037static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section,
Boris Brezillonfa465252018-08-16 17:30:15 +020038 struct mtd_oob_region *region)
39{
40 if (section)
41 return -ERANGE;
42
43 region->offset = 2;
44 region->length = mtd->oobsize - 2;
45
46 return 0;
47}
48
Miquel Raynal02b49792018-08-16 17:30:16 +020049static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = {
50 .ecc = mx35lfxge4ab_ooblayout_ecc,
Simon Glass62fd1a42020-02-03 07:35:56 -070051 .rfree = mx35lfxge4ab_ooblayout_free,
Boris Brezillonfa465252018-08-16 17:30:15 +020052};
53
54static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr)
55{
56 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1),
57 SPI_MEM_OP_NO_ADDR,
58 SPI_MEM_OP_DUMMY(1, 1),
59 SPI_MEM_OP_DATA_IN(1, eccsr, 1));
60
61 return spi_mem_exec_op(spinand->slave, &op);
62}
63
64static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand,
65 u8 status)
66{
67 struct nand_device *nand = spinand_to_nand(spinand);
68 u8 eccsr;
69
70 switch (status & STATUS_ECC_MASK) {
71 case STATUS_ECC_NO_BITFLIPS:
72 return 0;
73
74 case STATUS_ECC_UNCOR_ERROR:
75 return -EBADMSG;
76
77 case STATUS_ECC_HAS_BITFLIPS:
78 /*
79 * Let's try to retrieve the real maximum number of bitflips
80 * in order to avoid forcing the wear-leveling layer to move
81 * data around if it's not necessary.
82 */
83 if (mx35lf1ge4ab_get_eccsr(spinand, &eccsr))
84 return nand->eccreq.strength;
85
86 if (WARN_ON(eccsr > nand->eccreq.strength || !eccsr))
87 return nand->eccreq.strength;
88
89 return eccsr;
90
91 default:
92 break;
93 }
94
95 return -EINVAL;
96}
97
98static const struct spinand_info macronix_spinand_table[] = {
99 SPINAND_INFO("MX35LF1GE4AB", 0x12,
100 NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
101 NAND_ECCREQ(4, 512),
102 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
103 &write_cache_variants,
104 &update_cache_variants),
105 SPINAND_HAS_QE_BIT,
Miquel Raynal02b49792018-08-16 17:30:16 +0200106 SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
Boris Brezillonfa465252018-08-16 17:30:15 +0200107 mx35lf1ge4ab_ecc_get_status)),
Miquel Raynal02b49792018-08-16 17:30:16 +0200108 SPINAND_INFO("MX35LF2GE4AB", 0x22,
109 NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1),
110 NAND_ECCREQ(4, 512),
111 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
112 &write_cache_variants,
113 &update_cache_variants),
114 SPINAND_HAS_QE_BIT,
115 SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
Boris Brezillonfa465252018-08-16 17:30:15 +0200116};
117
118static int macronix_spinand_detect(struct spinand_device *spinand)
119{
120 u8 *id = spinand->id.data;
121 int ret;
122
123 /*
124 * Macronix SPI NAND read ID needs a dummy byte, so the first byte in
125 * raw_id is garbage.
126 */
127 if (id[1] != SPINAND_MFR_MACRONIX)
128 return 0;
129
130 ret = spinand_match_and_init(spinand, macronix_spinand_table,
131 ARRAY_SIZE(macronix_spinand_table),
132 id[2]);
133 if (ret)
134 return ret;
135
136 return 1;
137}
138
139static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
140 .detect = macronix_spinand_detect,
141};
142
143const struct spinand_manufacturer macronix_spinand_manufacturer = {
144 .id = SPINAND_MFR_MACRONIX,
145 .name = "Macronix",
146 .ops = &macronix_spinand_manuf_ops,
147};