Skip to content

Commit 41b207a

Browse files
author
Boris Brezillon
committed
mtd: nand: implement the default mtd_ooblayout_ops
Replace the default nand_ecclayout definitions for large and small page devices with the equivalent mtd_ooblayout_ops. Signed-off-by: Boris Brezillon <[email protected]>
1 parent 1bd0b24 commit 41b207a

File tree

2 files changed

+96
-49
lines changed

2 files changed

+96
-49
lines changed

drivers/mtd/nand/nand_base.c

Lines changed: 93 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,54 +47,96 @@
4747
#include <linux/mtd/partitions.h>
4848
#include <linux/of_mtd.h>
4949

50+
static int nand_get_device(struct mtd_info *mtd, int new_state);
51+
52+
static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
53+
struct mtd_oob_ops *ops);
54+
5055
/* Define default oob placement schemes for large and small page devices */
51-
static struct nand_ecclayout nand_oob_8 = {
52-
.eccbytes = 3,
53-
.eccpos = {0, 1, 2},
54-
.oobfree = {
55-
{.offset = 3,
56-
.length = 2},
57-
{.offset = 6,
58-
.length = 2} }
59-
};
56+
static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
57+
struct mtd_oob_region *oobregion)
58+
{
59+
struct nand_chip *chip = mtd_to_nand(mtd);
60+
struct nand_ecc_ctrl *ecc = &chip->ecc;
6061

61-
static struct nand_ecclayout nand_oob_16 = {
62-
.eccbytes = 6,
63-
.eccpos = {0, 1, 2, 3, 6, 7},
64-
.oobfree = {
65-
{.offset = 8,
66-
. length = 8} }
67-
};
62+
if (section > 1)
63+
return -ERANGE;
6864

69-
static struct nand_ecclayout nand_oob_64 = {
70-
.eccbytes = 24,
71-
.eccpos = {
72-
40, 41, 42, 43, 44, 45, 46, 47,
73-
48, 49, 50, 51, 52, 53, 54, 55,
74-
56, 57, 58, 59, 60, 61, 62, 63},
75-
.oobfree = {
76-
{.offset = 2,
77-
.length = 38} }
78-
};
65+
if (!section) {
66+
oobregion->offset = 0;
67+
oobregion->length = 4;
68+
} else {
69+
oobregion->offset = 6;
70+
oobregion->length = ecc->total - 4;
71+
}
7972

80-
static struct nand_ecclayout nand_oob_128 = {
81-
.eccbytes = 48,
82-
.eccpos = {
83-
80, 81, 82, 83, 84, 85, 86, 87,
84-
88, 89, 90, 91, 92, 93, 94, 95,
85-
96, 97, 98, 99, 100, 101, 102, 103,
86-
104, 105, 106, 107, 108, 109, 110, 111,
87-
112, 113, 114, 115, 116, 117, 118, 119,
88-
120, 121, 122, 123, 124, 125, 126, 127},
89-
.oobfree = {
90-
{.offset = 2,
91-
.length = 78} }
73+
return 0;
74+
}
75+
76+
static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
77+
struct mtd_oob_region *oobregion)
78+
{
79+
if (section > 1)
80+
return -ERANGE;
81+
82+
if (mtd->oobsize == 16) {
83+
if (section)
84+
return -ERANGE;
85+
86+
oobregion->length = 8;
87+
oobregion->offset = 8;
88+
} else {
89+
oobregion->length = 2;
90+
if (!section)
91+
oobregion->offset = 3;
92+
else
93+
oobregion->offset = 6;
94+
}
95+
96+
return 0;
97+
}
98+
99+
const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
100+
.ecc = nand_ooblayout_ecc_sp,
101+
.free = nand_ooblayout_free_sp,
92102
};
103+
EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
93104

94-
static int nand_get_device(struct mtd_info *mtd, int new_state);
105+
static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
106+
struct mtd_oob_region *oobregion)
107+
{
108+
struct nand_chip *chip = mtd_to_nand(mtd);
109+
struct nand_ecc_ctrl *ecc = &chip->ecc;
95110

96-
static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
97-
struct mtd_oob_ops *ops);
111+
if (section)
112+
return -ERANGE;
113+
114+
oobregion->length = ecc->total;
115+
oobregion->offset = mtd->oobsize - oobregion->length;
116+
117+
return 0;
118+
}
119+
120+
static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
121+
struct mtd_oob_region *oobregion)
122+
{
123+
struct nand_chip *chip = mtd_to_nand(mtd);
124+
struct nand_ecc_ctrl *ecc = &chip->ecc;
125+
126+
if (section)
127+
return -ERANGE;
128+
129+
oobregion->length = mtd->oobsize - ecc->total - 2;
130+
oobregion->offset = 2;
131+
132+
return 0;
133+
}
134+
135+
const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
136+
.ecc = nand_ooblayout_ecc_lp,
137+
.free = nand_ooblayout_free_lp,
138+
};
139+
EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
98140

99141
static int check_offs_len(struct mtd_info *mtd,
100142
loff_t ofs, uint64_t len)
@@ -4109,22 +4151,25 @@ int nand_scan_tail(struct mtd_info *mtd)
41094151
/* Set the internal oob buffer location, just after the page data */
41104152
chip->oob_poi = chip->buffers->databuf + mtd->writesize;
41114153

4154+
/*
4155+
* Set the provided ECC layout. If ecc->layout is NULL, the MTD core
4156+
* will just leave mtd->ooblayout to NULL, if it's not NULL, it will
4157+
* set ->ooblayout to the default ecclayout wrapper.
4158+
*/
4159+
mtd_set_ecclayout(mtd, ecc->layout);
4160+
41124161
/*
41134162
* If no default placement scheme is given, select an appropriate one.
41144163
*/
4115-
if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
4164+
if (!mtd->ooblayout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
41164165
switch (mtd->oobsize) {
41174166
case 8:
4118-
ecc->layout = &nand_oob_8;
4119-
break;
41204167
case 16:
4121-
ecc->layout = &nand_oob_16;
4168+
mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
41224169
break;
41234170
case 64:
4124-
ecc->layout = &nand_oob_64;
4125-
break;
41264171
case 128:
4127-
ecc->layout = &nand_oob_128;
4172+
mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
41284173
break;
41294174
default:
41304175
WARN(1, "No oob scheme defined for oobsize %d\n",
@@ -4285,7 +4330,6 @@ int nand_scan_tail(struct mtd_info *mtd)
42854330
ecc->write_oob_raw = ecc->write_oob;
42864331

42874332
/* propagate ecc info to mtd_info */
4288-
mtd_set_ecclayout(mtd, ecc->layout);
42894333
mtd->ecc_strength = ecc->strength;
42904334
mtd->ecc_step_size = ecc->size;
42914335

include/linux/mtd/nand.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,9 @@ struct nand_chip {
748748
void *priv;
749749
};
750750

751+
extern const struct mtd_ooblayout_ops nand_ooblayout_sp_ops;
752+
extern const struct mtd_ooblayout_ops nand_ooblayout_lp_ops;
753+
751754
static inline void nand_set_flash_node(struct nand_chip *chip,
752755
struct device_node *np)
753756
{

0 commit comments

Comments
 (0)