@@ -267,7 +267,8 @@ struct sunxi_nfc_timings {
* @timing_ctl: TIMING_CTL register value for this NAND chip
* @nsels: number of CS lines required by the NAND chip
* @sels: array of CS lines descriptions
- * @user_data_bytes: array of user data lengths for all ECC steps
+ * @user_data_total: protected user bytes in a programmable layout
+ * @user_data_step: maximum bytes assigned to each step, zero for a fixed layout
*/
struct sunxi_nand_chip {
struct list_head node;
@@ -276,7 +277,8 @@ struct sunxi_nand_chip {
unsigned long clk_rate;
u32 timing_cfg;
u32 timing_ctl;
- u8 *user_data_bytes;
+ u16 user_data_total;
+ u8 user_data_step;
int nsels;
struct sunxi_nand_chip_sel sels[] __counted_by(nsels);
};
@@ -327,11 +329,7 @@ struct sunxi_nfc_user_data_layout {
* @nstrengths: Size of @ecc_strengths
* @max_ecc_steps: Maximum supported steps for ECC, this is also the
* number of user data registers
- * @user_data_len_tab: Table of lengths supported by USER_DATA_LEN register
- * The table index is the value to set in NFC_USER_DATA_LEN
- * registers, and the corresponding value is the number of
- * bytes to write
- * @nuser_data_tab: Size of @user_data_len_tab
+ * @max_user_data_len: Maximum programmable user-data bytes per ECC step
* @sram_size: Size of the NAND controller SRAM
* @timings: Controller timing characteristics
*/
@@ -357,8 +355,7 @@ struct sunxi_nfc_caps {
unsigned int dma_maxburst;
const u8 *ecc_strengths;
unsigned int nstrengths;
- const u8 *user_data_len_tab;
- unsigned int nuser_data_tab;
+ unsigned int max_user_data_len;
unsigned int max_ecc_steps;
int sram_size;
const struct sunxi_nfc_timings *timings;
@@ -911,12 +908,21 @@ static inline u32 sunxi_nfc_buf_to_user_data(const u8 *buf)
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
}
-static u8 sunxi_nfc_user_data_sz(struct sunxi_nand_chip *sunxi_nand, int step)
+/* Earlier ECC steps take their full share before later steps use any bytes. */
+static unsigned int sunxi_nfc_user_data_before(struct sunxi_nand_chip *sunxi_nand,
+ unsigned int step)
{
- if (!sunxi_nand->user_data_bytes)
- return USER_DATA_SZ;
+ if (!sunxi_nand->user_data_step)
+ return step * USER_DATA_SZ;
+
+ return min_t(unsigned int, step * sunxi_nand->user_data_step,
+ sunxi_nand->user_data_total);
+}
- return sunxi_nand->user_data_bytes[step];
+static u8 sunxi_nfc_user_data_sz(struct sunxi_nand_chip *sunxi_nand, int step)
+{
+ return sunxi_nfc_user_data_before(sunxi_nand, step + 1) -
+ sunxi_nfc_user_data_before(sunxi_nand, step);
}
static void sunxi_nfc_hw_ecc_get_prot_oob_bytes(struct nand_chip *nand, u8 *oob,
@@ -977,34 +983,35 @@ static void sunxi_nfc_reset_user_data_len(struct sunxi_nfc *nfc)
writel(0, nfc->regs + NFC_REG_USER_DATA_LEN(nfc, i));
}
+static int sunxi_nfc_user_data_len_code(struct sunxi_nfc *nfc, int len)
+{
+ /* Each code counts four-byte words; larger codes are reserved. */
+ if (len < 0 || len > nfc->caps->max_user_data_len || !IS_ALIGNED(len, USER_DATA_SZ)) {
+ dev_warn(nfc->dev, "Unsupported length for user data reg: %d\n", len);
+ return -EINVAL;
+ }
+
+ return len / USER_DATA_SZ;
+}
+
static void sunxi_nfc_set_user_data_len(struct sunxi_nfc *nfc,
int len, int step)
{
- bool found = false;
u32 val;
- int i;
+ int code;
/* not all SoCs have this register */
if (!nfc->caps->reg_user_data_len)
return;
- for (i = 0; i < nfc->caps->nuser_data_tab; i++) {
- if (len == nfc->caps->user_data_len_tab[i]) {
- found = true;
- break;
- }
- }
-
- if (!found) {
- dev_warn(nfc->dev,
- "Unsupported length for user data reg: %d\n", len);
+ code = sunxi_nfc_user_data_len_code(nfc, len);
+ if (code < 0)
return;
- }
val = readl(nfc->regs + NFC_REG_USER_DATA_LEN(nfc, step));
val &= ~NFC_USER_DATA_LEN_MSK(step);
- val |= field_prep(NFC_USER_DATA_LEN_MSK(step), i);
+ val |= field_prep(NFC_USER_DATA_LEN_MSK(step), code);
writel(val, nfc->regs + NFC_REG_USER_DATA_LEN(nfc, step));
}
@@ -1204,13 +1211,7 @@ static int sunxi_nfc_hw_ecc_read_chunk(struct nand_chip *nand,
static int sunxi_get_oob_offset(struct sunxi_nand_chip *sunxi_nand,
struct nand_ecc_ctrl *ecc, int step)
{
- int ecc_off = step * ecc->bytes;
- int i;
-
- for (i = 0; i < step; i++)
- ecc_off += sunxi_nfc_user_data_sz(sunxi_nand, i);
-
- return ecc_off;
+ return step * ecc->bytes + sunxi_nfc_user_data_before(sunxi_nand, step);
}
/*
@@ -1221,8 +1222,7 @@ static int sunxi_get_oob_offset(struct sunxi_nand_chip *sunxi_nand,
static int sunxi_get_ecc_offset(struct sunxi_nand_chip *sunxi_nand,
struct nand_ecc_ctrl *ecc, int step)
{
- return sunxi_get_oob_offset(sunxi_nand, ecc, step) +
- sunxi_nfc_user_data_sz(sunxi_nand, step);
+ return step * ecc->bytes + sunxi_nfc_user_data_before(sunxi_nand, step + 1);
}
static void sunxi_nfc_hw_ecc_read_extra_oob(struct nand_chip *nand,
@@ -1991,10 +1991,7 @@ static int sunxi_nand_ooblayout_free(struct mtd_info *mtd, int section,
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
unsigned int user_data_sz;
- /*
- * The controller does not provide access to OOB bytes
- * past the end of the ECC data.
- */
+ /* Only ECC-protected user data is exposed as free OOB. */
if (section >= ecc->steps)
return -ERANGE;
@@ -2023,15 +2020,6 @@ static const struct mtd_ooblayout_ops sunxi_nand_ooblayout_ops = {
.free = sunxi_nand_ooblayout_free,
};
-static void sunxi_nand_detach_chip(struct nand_chip *nand)
-{
- struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
- struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
-
- devm_kfree(nfc->dev, sunxi_nand->user_data_bytes);
- sunxi_nand->user_data_bytes = NULL;
-}
-
static unsigned int sunxi_nfc_fixed_min_user_data(int nsteps)
{
/* Preserve the ECC strength selected with the legacy BBM allowance. */
@@ -2048,31 +2036,19 @@ 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 = nand_to_mtd(nand)->oobsize - nand->ecc.bytes * nsteps;
- int i, step;
+ const struct sunxi_nfc_caps *caps = nfc->caps;
+ unsigned int oobsize = nand_to_mtd(nand)->oobsize;
+ unsigned int parity = nsteps * nand->ecc.bytes;
+ unsigned int available;
- if (nsteps <= 0)
- return -EINVAL;
-
- sunxi_nand->user_data_bytes = devm_kzalloc(nfc->dev, nsteps,
- GFP_KERNEL);
- if (!sunxi_nand->user_data_bytes)
- return -ENOMEM;
-
- 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;
- sunxi_nand->user_data_bytes[step] = c->user_data_len_tab[i];
- }
- remaining_bytes -= sunxi_nand->user_data_bytes[step];
- if (sunxi_nand->user_data_bytes[step] == 0)
- break;
- }
- if (sunxi_nand->user_data_bytes[0] < USER_DATA_SZ)
+ /* The first protected word must contain the bad-block marker. */
+ if (!nsteps || oobsize < parity + USER_DATA_SZ)
return -EINVAL;
+ /* Use as many complete words as fit after parity. */
+ available = min(oobsize - parity, nsteps * caps->max_user_data_len);
+ sunxi_nand->user_data_step = caps->max_user_data_len;
+ sunxi_nand->user_data_total = round_down(available, USER_DATA_SZ);
return 0;
}
@@ -2094,7 +2070,6 @@ static int sunxi_nand_hw_ecc_ctrl_init(struct nand_chip *nand,
const u8 *strengths = nfc->caps->ecc_strengths;
struct mtd_info *mtd = nand_to_mtd(nand);
struct nand_device *nanddev = mtd_to_nanddev(mtd);
- int total_user_data_sz = 0;
int nsectors;
int ecc_mode;
int ret;
@@ -2179,10 +2154,7 @@ static int sunxi_nand_hw_ecc_ctrl_init(struct nand_chip *nand,
return ret;
}
- 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))
+ if (mtd->oobsize < sunxi_get_oob_offset(sunxi_nand, ecc, nsectors))
return -EINVAL;
ecc->read_oob = sunxi_nfc_hw_ecc_read_oob;
@@ -2413,7 +2385,6 @@ static int sunxi_nfc_exec_op(struct nand_chip *nand,
static const struct nand_controller_ops sunxi_nand_controller_ops = {
.attach_chip = sunxi_nand_attach_chip,
- .detach_chip = sunxi_nand_detach_chip,
.setup_interface = sunxi_nfc_setup_interface,
.exec_op = sunxi_nfc_exec_op,
};
@@ -2712,10 +2683,6 @@ static const u8 sunxi_ecc_strengths_h6[] = {
16, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80
};
-static const u8 sunxi_user_data_len_h6[] = {
- 0, 4, 8, 12, 16, 20, 24, 28, 32
-};
-
static const struct sunxi_nfc_caps sunxi_nfc_a10_caps = {
.has_ecc_block_512 = true,
.layout = &sunxi_nfc_fixed_layout,
@@ -2781,8 +2748,7 @@ static const struct sunxi_nfc_caps sunxi_nfc_h616_caps = {
.dma_maxburst = 8,
.ecc_strengths = sunxi_ecc_strengths_h6,
.nstrengths = ARRAY_SIZE(sunxi_ecc_strengths_h6),
- .user_data_len_tab = sunxi_user_data_len_h6,
- .nuser_data_tab = ARRAY_SIZE(sunxi_user_data_len_h6),
+ .max_user_data_len = 32,
.max_ecc_steps = 32,
.sram_size = 8192,
.timings = &sun50i_h616_nfc_timings,