Skip to content

Commit 35d5d20

Browse files
Uwe Kleine-Königcomputersforpeace
Uwe Kleine-König
authored andcommitted
mtd: mxc_nand: cleanup copy_spare function
To give people without the reference manual at hand a chance to understand how spare area is handled in the i.MX nand controller, improve commenting, naming of variables and coding style. No functional change intended. Reviewed-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> [baruch: declare oob_chunk_size; update comments; reword commit log] Signed-off-by: Baruch Siach <baruch@tkos.co.il> Signed-off-by: Brian Norris <computersforpeace@gmail.com>
1 parent bcb83a1 commit 35d5d20

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

drivers/mtd/nand/mxc_nand.c

+31-15
Original file line numberDiff line numberDiff line change
@@ -807,32 +807,48 @@ static void mxc_nand_select_chip_v2(struct mtd_info *mtd, int chip)
807807
}
808808

809809
/*
810-
* Function to transfer data to/from spare area.
810+
* The controller splits a page into data chunks of 512 bytes + partial oob.
811+
* There are writesize / 512 such chunks, the size of the partial oob parts is
812+
* oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then
813+
* contains additionally the byte lost by rounding (if any).
814+
* This function handles the needed shuffling between host->data_buf (which
815+
* holds a page in natural order, i.e. writesize bytes data + oobsize bytes
816+
* spare) and the NFC buffer.
811817
*/
812818
static void copy_spare(struct mtd_info *mtd, bool bfrom)
813819
{
814820
struct nand_chip *this = mtd->priv;
815821
struct mxc_nand_host *host = this->priv;
816-
u16 i, j;
817-
u16 n = mtd->writesize >> 9;
822+
u16 i, oob_chunk_size;
823+
u16 num_chunks = mtd->writesize / 512;
824+
818825
u8 *d = host->data_buf + mtd->writesize;
819826
u8 __iomem *s = host->spare0;
820-
u16 t = host->devtype_data->spare_len;
827+
u16 sparebuf_size = host->devtype_data->spare_len;
821828

822-
j = (mtd->oobsize / n >> 1) << 1;
829+
/* size of oob chunk for all but possibly the last one */
830+
oob_chunk_size = (mtd->oobsize / num_chunks) & ~1;
823831

824832
if (bfrom) {
825-
for (i = 0; i < n - 1; i++)
826-
memcpy32_fromio(d + i * j, s + i * t, j);
827-
828-
/* the last section */
829-
memcpy32_fromio(d + i * j, s + i * t, mtd->oobsize - i * j);
833+
for (i = 0; i < num_chunks - 1; i++)
834+
memcpy32_fromio(d + i * oob_chunk_size,
835+
s + i * sparebuf_size,
836+
oob_chunk_size);
837+
838+
/* the last chunk */
839+
memcpy32_fromio(d + i * oob_chunk_size,
840+
s + i * sparebuf_size,
841+
mtd->oobsize - i * oob_chunk_size);
830842
} else {
831-
for (i = 0; i < n - 1; i++)
832-
memcpy32_toio(&s[i * t], &d[i * j], j);
833-
834-
/* the last section */
835-
memcpy32_toio(&s[i * t], &d[i * j], mtd->oobsize - i * j);
843+
for (i = 0; i < num_chunks - 1; i++)
844+
memcpy32_toio(&s[i * sparebuf_size],
845+
&d[i * oob_chunk_size],
846+
oob_chunk_size);
847+
848+
/* the last chunk */
849+
memcpy32_toio(&s[oob_chunk_size * sparebuf_size],
850+
&d[i * oob_chunk_size],
851+
mtd->oobsize - i * oob_chunk_size);
836852
}
837853
}
838854

0 commit comments

Comments
 (0)