@@ -293,6 +293,13 @@ struct sunxi_nfc_mdma_desc {
__le32 next;
} __packed __aligned(4);
+struct sunxi_nfc_user_data_layout {
+ /* Space to reserve before choosing the ECC strength. */
+ unsigned int (*min_user_data)(int nsteps);
+ /* Optional setup after the step size and parity length are known. */
+ int (*init_user_data)(struct nand_chip *nand, int nsteps);
+};
+
/*
* NAND Controller capabilities structure: stores NAND controller capabilities
* for distinction between compatible strings.
@@ -302,8 +309,7 @@ struct sunxi_nfc_mdma_desc {
* @has_ecc_block_512: If the ECC can handle 512B or only 1024B chunks
* @has_ecc_clk: If the controller needs an ECC clock.
* @has_mbus_clk: If the controller needs a mbus clock.
- * @legacy_max_strength:If the maximize strength function was off by 2 bytes
- * NB: this should not be used in new controllers
+ * @layout: protected user-data layout for the controller
* @reg_io_data: I/O data register
* @reg_ecc_err_cnt: ECC error counter register
* @reg_user_data: User data register
@@ -335,7 +341,7 @@ struct sunxi_nfc_caps {
bool has_ecc_block_512;
bool has_ecc_clk;
bool has_mbus_clk;
- bool legacy_max_strength;
+ const struct sunxi_nfc_user_data_layout *layout;
unsigned int reg_io_data;
unsigned int reg_ecc_err_cnt;
unsigned int reg_user_data;
@@ -2026,24 +2032,35 @@ static void sunxi_nand_detach_chip(struct nand_chip *nand)
sunxi_nand->user_data_bytes = NULL;
}
-static int sunxi_nfc_maximize_user_data(struct nand_chip *nand, uint32_t oobsize,
- int ecc_bytes, int nsectors)
+static unsigned int sunxi_nfc_fixed_min_user_data(int nsteps)
+{
+ /* Preserve the ECC strength selected with the legacy BBM allowance. */
+ return nsteps * USER_DATA_SZ + 2;
+}
+
+static unsigned int sunxi_nfc_variable_min_user_data(int nsteps)
+{
+ /* The first protected word contains the bad-block marker. */
+ return USER_DATA_SZ;
+}
+
+static int sunxi_nfc_variable_init_user_data(struct nand_chip *nand, int nsteps)
{
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
const struct sunxi_nfc_caps *c = nfc->caps;
- int remaining_bytes = oobsize - (ecc_bytes * nsectors);
+ int remaining_bytes = nand_to_mtd(nand)->oobsize - nand->ecc.bytes * nsteps;
int i, step;
- if (nsectors <= 0)
+ if (nsteps <= 0)
return -EINVAL;
- sunxi_nand->user_data_bytes = devm_kzalloc(nfc->dev, nsectors,
+ sunxi_nand->user_data_bytes = devm_kzalloc(nfc->dev, nsteps,
GFP_KERNEL);
if (!sunxi_nand->user_data_bytes)
return -ENOMEM;
- for (step = 0; (step < nsectors) && (remaining_bytes > 0); step++) {
+ for (step = 0; (step < nsteps) && (remaining_bytes > 0); step++) {
for (i = 0; i < c->nuser_data_tab; i++) {
if (c->user_data_len_tab[i] > remaining_bytes)
break;
@@ -2059,12 +2076,21 @@ static int sunxi_nfc_maximize_user_data(struct nand_chip *nand, uint32_t oobsize
return 0;
}
+static const struct sunxi_nfc_user_data_layout sunxi_nfc_fixed_layout = {
+ .min_user_data = sunxi_nfc_fixed_min_user_data,
+};
+
+static const struct sunxi_nfc_user_data_layout sunxi_nfc_variable_layout = {
+ .min_user_data = sunxi_nfc_variable_min_user_data,
+ .init_user_data = sunxi_nfc_variable_init_user_data,
+};
+
static int sunxi_nand_hw_ecc_ctrl_init(struct nand_chip *nand,
- struct nand_ecc_ctrl *ecc,
- struct device_node *np)
+ struct nand_ecc_ctrl *ecc)
{
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
+ const struct sunxi_nfc_user_data_layout *layout = nfc->caps->layout;
const u8 *strengths = nfc->caps->ecc_strengths;
struct mtd_info *mtd = nand_to_mtd(nand);
struct nand_device *nanddev = mtd_to_nanddev(mtd);
@@ -2083,33 +2109,7 @@ static int sunxi_nand_hw_ecc_ctrl_init(struct nand_chip *nand,
ecc->size = 1024;
nsectors = mtd->writesize / ecc->size;
- if (!nfc->caps->reg_user_data_len) {
- /*
- * If there's a fixed user data length, subtract it before
- * computing the max ECC strength
- */
-
- for (i = 0; i < nsectors; i++)
- total_user_data_sz += sunxi_nfc_user_data_sz(sunxi_nand, i);
-
- /*
- * The 2 BBM bytes should not be removed from the grand total,
- * because they are part of the USER_DATA_SZ.
- * But we can't modify that for older platform since it may
- * result in a stronger ECC at the end, and break the
- * compatibility.
- */
- if (nfc->caps->legacy_max_strength)
- bytes -= 2;
-
- bytes -= total_user_data_sz;
- } else {
- /*
- * User-data lengths are encoded in four-byte units. Reserve
- * the first word because it contains the two BBM bytes.
- */
- bytes -= USER_DATA_SZ;
- }
+ bytes -= layout->min_user_data(nsectors);
/*
* Once all user data has been subtracted, the rest can be used
@@ -2173,20 +2173,14 @@ static int sunxi_nand_hw_ecc_ctrl_init(struct nand_chip *nand,
return -EINVAL;
}
- /*
- * The rationale for variable data length is to prioritize maximum ECC
- * strength, and then use the remaining space for user data.
- */
- if (nfc->caps->reg_user_data_len) {
- ret = sunxi_nfc_maximize_user_data(nand, mtd->oobsize,
- ecc->bytes, nsectors);
+ if (layout->init_user_data) {
+ ret = layout->init_user_data(nand, nsectors);
if (ret)
return ret;
}
- if (total_user_data_sz == 0)
- for (i = 0; i < nsectors; i++)
- total_user_data_sz += sunxi_nfc_user_data_sz(sunxi_nand, i);
+ for (i = 0; i < nsectors; i++)
+ total_user_data_sz += sunxi_nfc_user_data_sz(sunxi_nand, i);
if (mtd->oobsize < (ecc->bytes * nsectors + total_user_data_sz))
return -EINVAL;
@@ -2233,7 +2227,6 @@ static int sunxi_nand_attach_chip(struct nand_chip *nand)
const struct nand_ecc_props *requirements =
nanddev_get_ecc_requirements(&nand->base);
struct nand_ecc_ctrl *ecc = &nand->ecc;
- struct device_node *np = nand_get_flash_node(nand);
int ret;
if (nand->bbt_options & NAND_BBT_USE_FLASH)
@@ -2254,7 +2247,7 @@ static int sunxi_nand_attach_chip(struct nand_chip *nand)
switch (ecc->engine_type) {
case NAND_ECC_ENGINE_TYPE_ON_HOST:
- ret = sunxi_nand_hw_ecc_ctrl_init(nand, ecc, np);
+ ret = sunxi_nand_hw_ecc_ctrl_init(nand, ecc);
if (ret)
return ret;
break;
@@ -2725,7 +2718,7 @@ static const u8 sunxi_user_data_len_h6[] = {
static const struct sunxi_nfc_caps sunxi_nfc_a10_caps = {
.has_ecc_block_512 = true,
- .legacy_max_strength = true,
+ .layout = &sunxi_nfc_fixed_layout,
.reg_io_data = NFC_REG_A10_IO_DATA,
.reg_ecc_err_cnt = NFC_REG_A10_ECC_ERR_CNT,
.reg_user_data = NFC_REG_A10_USER_DATA,
@@ -2748,7 +2741,7 @@ static const struct sunxi_nfc_caps sunxi_nfc_a10_caps = {
static const struct sunxi_nfc_caps sunxi_nfc_a23_caps = {
.has_mdma = true,
.has_ecc_block_512 = true,
- .legacy_max_strength = true,
+ .layout = &sunxi_nfc_fixed_layout,
.reg_io_data = NFC_REG_A23_IO_DATA,
.reg_ecc_err_cnt = NFC_REG_A10_ECC_ERR_CNT,
.reg_user_data = NFC_REG_A10_USER_DATA,
@@ -2769,6 +2762,7 @@ static const struct sunxi_nfc_caps sunxi_nfc_a23_caps = {
};
static const struct sunxi_nfc_caps sunxi_nfc_h616_caps = {
+ .layout = &sunxi_nfc_variable_layout,
.has_mdma_desc = true,
.has_ecc_clk = true,
.has_mbus_clk = true,