Skip to contentSkip to conversation

NAND: davinci_nand driver

patch/RESEND 2.6.29-rc3-git New David Brownell 6 participants 10 messages Feb 25, 2009 – Mar 2, 2009

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

From: David Brownell <[email protected]>

This is a device driver for the NAND flash controller found on the various DaVinci family chips. It handles up to four SoC chipselects, and some flavors of secondary chipselect (e.g. based on upper bits of the address bus) as used with some multichip packages. (Including the 2 GByte chips used on some TI devel boards.)

The 1-bit ECC hardware is supported (3 bytes ECC per 512 bytes data); but not yet the newer 4-bit ECC (10 bytes ECC per 512 bytes data), as available on chips like the DM355 or OMAP-L137 and needed with the more error-prone MLC NAND chips.

This is a cleaned-up version of code that's been in use for several years now; sanity checked with the new drivers/mtd/tests.

---
NOTE: matches current version in DaVinci GIT tree, which means it depends on <mach/cpu.h> which isn't (yet) in mainline.

ALSO: includes <mach/nand.h> ... arguably that should be part of the same set of patches that include <mach/cpu.h> and board setup code patches that use that platform data.

 arch/arm/mach-davinci/include/mach/nand.h |   80 +++
 drivers/mtd/nand/Kconfig                  |    7 
 drivers/mtd/nand/Makefile                 |    1 
 drivers/mtd/nand/davinci_nand.c           |  587 ++++++++++++++++++++++++++++
 4 files changed, 675 insertions(+)
arch/arm/mach-davinci/include/mach/nand.h new file +80 -0
@@ -0,0 +1,80 @@
+/*
+ * mach-davinci/nand.h
+ *
+ * Copyright (C) 2006 Texas Instruments.
+ *
+ * Ported to 2.6.23 Copyright (C) 2008 by
+ *   Sander Huijsen <[email protected]>
+ *   Troy Kisky <[email protected]>
+ *   Dirk Behme <[email protected]>
+ *
+ * --------------------------------------------------------------------------
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef __ARCH_ARM_DAVINCI_NAND_H
+#define __ARCH_ARM_DAVINCI_NAND_H
+
+#include <linux/mtd/nand.h>
+
+#define NRCSR_OFFSET		0x00
+#define AWCCR_OFFSET		0x04
+#define A1CR_OFFSET		0x10
+#define NANDFCR_OFFSET		0x60
+#define NANDFSR_OFFSET		0x64
+#define NANDF1ECC_OFFSET	0x70
+
+/* 4-bit ECC syndrome registers */
+#define NAND_4BIT_ECC_LOAD_OFFSET	0xbc
+#define NAND_4BIT_ECC1_OFFSET		0xc0
+#define NAND_4BIT_ECC2_OFFSET		0xc4
+#define NAND_4BIT_ECC3_OFFSET		0xc8
+#define NAND_4BIT_ECC4_OFFSET		0xcc
+#define NAND_ERR_ADD1_OFFSET		0xd0
+#define NAND_ERR_ADD2_OFFSET		0xd4
+#define NAND_ERR_ERRVAL1_OFFSET		0xd8
+#define NAND_ERR_ERRVAL2_OFFSET		0xdc
+
+/* NOTE:  boards don't need to use these address bits
+ * for ALE/CLE unless they support booting from NAND.
+ * They're used unless platform data overrides them.
+ */
+#define	MASK_ALE		0x08
+#define	MASK_CLE		0x10
+
+struct davinci_nand_pdata {		/* platform_data */
+	u32			mask_ale;
+	u32			mask_cle;
+
+	/* for packages using two chipselects */
+	u32			mask_chipsel;
+
+	/* board's default static partition info */
+	struct mtd_partition	*parts;
+	unsigned		nr_parts;
+
+	/* none  == NAND_ECC_NONE (strongly *not* advised!!)
+	 * soft  == NAND_ECC_SOFT
+	 * 1-bit == NAND_ECC_HW
+	 * 4-bit == NAND_ECC_HW_SYNDROME (not on all chips)
+	 */
+	nand_ecc_modes_t	ecc_mode;
+
+	/* e.g. NAND_BUSWIDTH_16 or NAND_USE_FLASH_BBT */
+	unsigned		options;
+};
+
+#endif	/* __ARCH_ARM_DAVINCI_NAND_H */
drivers/mtd/nand/Kconfig +7 -0
@@ -427,4 +427,11 @@ config MTD_NAND_SH_FLCTL
 	  Several Renesas SuperH CPU has FLCTL. This option enables support
 	  for NAND Flash using FLCTL. This driver support SH7723.
 
+config MTD_NAND_DAVINCI
+        tristate "Support NAND on DaVinci SoC"
+        depends on ARCH_DAVINCI
+        help
+	  Enable the driver for NAND flash chips on Texas Instruments
+	  DaVinci processors.
+
 endif # MTD_NAND
drivers/mtd/nand/Makefile +1 -0
@@ -36,5 +36,6 @@ obj-$(CONFIG_MTD_NAND_FSL_ELBC)		+= fsl_
 obj-$(CONFIG_MTD_NAND_FSL_UPM)		+= fsl_upm.o
 obj-$(CONFIG_MTD_NAND_SH_FLCTL)		+= sh_flctl.o
 obj-$(CONFIG_MTD_NAND_MXC)		+= mxc_nand.o
+obj-$(CONFIG_MTD_NAND_DAVINCI)		+= davinci_nand.o
 
 nand-objs := nand_base.o nand_bbt.o
drivers/mtd/nand/davinci_nand.c new file +587 -0

587 lines not on this page · raw message

#1 lore raw

OK a couple of things
+

drivers/mtd/nand/davinci_nand.c line 52

Quoted 12 lines
drivers/mtd/nand/davinci_nand.c new file +12 -0
+
+#ifdef CONFIG_MTD_PARTITIONS
+static inline int mtd_has_partitions(void) { return 1; }
+#else
+static inline int mtd_has_partitions(void) { return 0; }
+#endif
+
+#ifdef CONFIG_MTD_CMDLINE_PARTS
+static inline int mtd_has_cmdlinepart(void) { return 1; }
+#else
+static inline int mtd_has_cmdlinepart(void) { return 0; }
+#endif

I'm not sure stylewise this is the best way. Even though #ifdefs stays outside of functions, this is a case where maybe it's better to put it inside or rething the need / usage for these functions.

Quoted 2 lines

+static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
+                                     const u_char *dat, u_char *ecc_code)

(and others)

My beef here is the use of u_char. This is really not a standart C type or a standart Linux type (u8/u32 etc), so we should aim for those (yes, I hate typing unsigned blah blah blah) but you can use u8 for that.

drivers/mtd/nand/davinci_nand.c line 249

Quoted 7 lines
drivers/mtd/nand/davinci_nand.c new file +7 -0
+static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
+{
+       struct nand_chip *chip = mtd->priv;
+
+       if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
+               ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
+       else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)

What are those 0x03 and 0x01 (and other places as well), you'll have to spell out those, preferably using defines.

Signature · 2 lines
-
Thiago Galesi
#2 lore raw
Quoted 18 lines · Thiago Galesi

OK a couple of things
+

drivers/mtd/nand/davinci_nand.c line 52

Quoted 12 lines
drivers/mtd/nand/davinci_nand.c new file +12 -0
+
+#ifdef CONFIG_MTD_PARTITIONS
+static inline int mtd_has_partitions(void) { return 1; }
+#else
+static inline int mtd_has_partitions(void) { return 0; }
+#endif
+
+#ifdef CONFIG_MTD_CMDLINE_PARTS
+static inline int mtd_has_cmdlinepart(void) { return 1; }
+#else
+static inline int mtd_has_cmdlinepart(void) { return 0; }
+#endif

I'm not sure stylewise this is the best way. Even though #ifdefs stays outside of functions, this is a case where maybe it's better to put it inside or rething the need / usage for these functions.

My preference would be to have those functions live in the MTD headers.

Needing #ifdefs in the body of probe() is *extremely* error prone. Having function versions makes it harder to commit a lot of the common errors I've seen, like having some mix of options gratuitously break compiles because of missing code fragments or variable declarations. It also makes it easier to see when code is obviously doing the wrong thing.

Note that #ifdefs-in-functions is contrary to standard kernel coding practices. It persists in the MTD code mostly due to legacy drivers, from what I can tell, which do so because ... there's been no better option, such as having those functions.

Quoted 9 lines
Quoted 2 lines

+static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
+                                     const u_char *dat, u_char *ecc_code)

(and others)

My beef here is the use of u_char. This is really not a standart C type or a standart Linux type (u8/u32 etc), so we should aim for those (yes, I hate typing unsigned blah blah blah) but you can use u8 for that.

I too would like to see <linux/mtd/*.h> use u8/u32/etc. But in this case, it's just doing what the MTD framework does. If I used u8/u32/etc the usual feedback would be "do what all the other drivers do", "match the interface decls", etc.

Quoted 10 lines

drivers/mtd/nand/davinci_nand.c line 249

Quoted 7 lines
drivers/mtd/nand/davinci_nand.c new file +7 -0
+static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
+{
+       struct nand_chip *chip = mtd->priv;
+
+       if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
+               ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
+       else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)

What are those 0x03 and 0x01 (and other places as well), you'll have to spell out those, preferably using defines.

The "0x03" is "low two bits", "0x01" is "low one bit", etc. Those functions can't be used except when memory address is "naturally" aligned, and the data length likewise.

You might have an argument if you suggested a comment were appropriate ... but those are very common idioms, used all over the kernel without #defines that I've ever seen. Did you have suggestions for pre-existing symbols to use?

- Dave

#3 lore raw
Quoted 13 lines · David Brownell
Quoted 8 lines · Thiago Galesi

OK a couple of things
 +

drivers/mtd/nand/davinci_nand.c line 46

Quoted 6 lines
drivers/mtd/nand/davinci_nand.c new file +6 -0
+
+#ifdef CONFIG_MTD_PARTITIONS
+static inline int mtd_has_partitions(void) { return 1; }
+#else
+static inline int mtd_has_partitions(void) { return 0; }
+#endif

My preference would be to have those functions live in the MTD headers.

Quoted 6 lines

Needing #ifdefs in the body of probe() is *extremely* error prone.  Having function versions makes it harder to commit a lot of the common errors I've seen, like having some mix of options gratuitously break compiles because of missing code fragments or variable declarations.  It also makes it easier to see when code is obviously doing the wrong thing.

Agreed. Especially about having #ifdefs in probe, no question about that, this way is _much_better_

Quoted 3 lines

Note that #ifdefs-in-functions is contrary to standard kernel coding practices.

Yes, I know (and agree with) that 100% :)

Quoted 5 lines

I too would like to see <linux/mtd/*.h> use u8/u32/etc.  But in this case, it's just doing what the MTD framework does. If I used u8/u32/etc the usual feedback would be "do what all the other drivers do", "match the interface decls", etc.

Yes, that happens a lot. But a movement towards 'the right way' is always welcome.

Quoted 12 lines
Quoted 11 lines

drivers/mtd/nand/davinci_nand.c line 249

Quoted 8 lines
drivers/mtd/nand/davinci_nand.c new file +7 -0
+static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
+{
+       struct nand_chip *chip = mtd->priv;
+
+       if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
+               ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
+       else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)

What are those 0x03 and 0x01 (and other places as well), you'll have to spell out those, preferably using defines.

Quoted 6 lines

The "0x03" is "low two bits", "0x01" is "low one bit", etc. Those functions can't be used except when memory address is "naturally" aligned, and the data length likewise.

You might have an argument if you suggested a comment were appropriate ... but those are very common idioms,

Oh, ok, now I get it. But this is still confusing. (Yes, put a comment there) No need for the ifdefs though.

Also, in the case of non-aligned acesses what is commonly done goes like this:
you write the small unaligned part with 8/16 bit ops, then the rest with 32 bit ops.

Maybe it's really not worth speedwise to do all of this, but this is ARM after all :)

Signature · 2 lines
-
Thiago Galesi
#4 lore raw
In reply to David Brownell
Quoted 18 lines · On Tue, 24 Feb 2009 15:09:54 -0800 David Brownell <[email protected]> wrote:

From: David Brownell <[email protected]>

This is a device driver for the NAND flash controller found on the various DaVinci family chips. It handles up to four SoC chipselects, and some flavors of secondary chipselect (e.g. based on upper bits of the address bus) as used with some multichip packages. (Including the 2 GByte chips used on some TI devel boards.)

The 1-bit ECC hardware is supported (3 bytes ECC per 512 bytes data); but not yet the newer 4-bit ECC (10 bytes ECC per 512 bytes data), as available on chips like the DM355 or OMAP-L137 and needed with the more error-prone MLC NAND chips.

This is a cleaned-up version of code that's been in use for several years now; sanity checked with the new drivers/mtd/tests.

Quoted 8 lines

...

+ * Copyright (C) 2006 Texas Instruments.
+ *
+ * Ported to 2.6.23 Copyright (C) 2008 by

+ *   Sander Huijsen <[email protected]>
+ *   Troy Kisky <[email protected]>
+ *   Dirk Behme <[email protected]>

hm. What's the story with authorship, attributions and signoffs here?

Quoted 4 lines

...

drivers/mtd/nand/davinci_nand.c new file +1 -0
+#ifdef CONFIG_MTD_PARTITIONS

drivers/mtd/nand/davinci_nand.c line 52

Quoted 10 lines
drivers/mtd/nand/davinci_nand.c new file +10 -0
+static inline int mtd_has_partitions(void) { return 1; }
+#else
+static inline int mtd_has_partitions(void) { return 0; }
+#endif
+
+#ifdef CONFIG_MTD_CMDLINE_PARTS
+static inline int mtd_has_cmdlinepart(void) { return 1; }
+#else
+static inline int mtd_has_cmdlinepart(void) { return 0; }
+#endif

These definitions shouldn't be buried in a .c file.

drivers/mtd/nand/davinci_nand.c line 164

Quoted 7 lines

...

drivers/mtd/nand/davinci_nand.c new file +4 -0
+static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode)
+{
+	struct davinci_nand_info *info;
+	u32 retval;

The identifier `retval' is usually used to identify the value which this function will return.

Quoted 15 lines
drivers/mtd/nand/davinci_nand.c new file +15 -0
+	unsigned long flags;
+
+	info = to_davinci_nand(mtd);
+
+	/* Reset ECC hardware */
+	nand_davinci_readecc_1bit(mtd);
+
+	spin_lock_irqsave(&davinci_nand_lock, flags);
+
+	/* Restart ECC hardware */
+	retval = davinci_nand_readl(info, NANDFCR_OFFSET);
+	retval |= BIT(8 + info->core_chipsel);
+	davinci_nand_writel(info, NANDFCR_OFFSET, retval);
+
+	spin_unlock_irqrestore(&davinci_nand_lock, flags);

drivers/mtd/nand/davinci_nand.c line 189

Quoted 10 lines
drivers/mtd/nand/davinci_nand.c new file +10 -0
+}
+
+/*
+ * Read hardware ECC value and pack into three bytes
+ */
+static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
+				      const u_char *dat, u_char *ecc_code)
+{
+	unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
+	unsigned int tmp = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);

argh.

drivers/mtd/nand/davinci_nand.c line 195

Quoted 5 lines
drivers/mtd/nand/davinci_nand.c new file +5 -0
+	/* invert so that erased block ecc is correct */
+	tmp = ~tmp;
+	ecc_code[0] = (u_char)(tmp);
+	ecc_code[1] = (u_char)(tmp >> 8);
+	ecc_code[2] = (u_char)(tmp >> 16);

Is there some library facility which is being open-coded here?

Quoted 27 lines
drivers/mtd/nand/davinci_nand.c new file +24 -0
+	return 0;
+}
+
 
...
 
+static int __init nand_davinci_probe(struct platform_device *pdev)
+{
+	struct davinci_nand_pdata	*pdata = pdev->dev.platform_data;
+	struct davinci_nand_info	*info;
+	struct resource			*res1;
+	struct resource			*res2;
+	void __iomem			*vaddr;
+	void __iomem			*base;
+	int				ret;
+	u32				val;
+	nand_ecc_modes_t		ecc_mode;
+
+	/* which external chipselect will we be managing? */
+	if (pdev->id < 0 || pdev->id > 3)
+		return -ENODEV;
+
+	info = kzalloc(sizeof(*info), GFP_KERNEL);
+	if (!info) {
+		dev_err(&pdev->dev, "unable to allocate memory\n");
+		ret = -ENOMEM;
+		goto err_nomem;

drivers/mtd/nand/davinci_nand.c line 341

Quoted 10 lines
drivers/mtd/nand/davinci_nand.c new file +10 -0
+	}
+
+	platform_set_drvdata(pdev, info);
+
+	res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	if (!res1 || !res2) {
+		dev_err(&pdev->dev, "resource missing\n");
+		ret = -EINVAL;
+		goto err_res;

This leaks `info'.

Please check all the error path unwinding here.

Quoted 5 lines
drivers/mtd/nand/davinci_nand.c new file +2 -0
+	}
+
 
...
#5 lore raw
Quoted 8 lines · Andrew Morton
Quoted 6 lines

+ * Copyright (C) 2006 Texas Instruments.
+ *
+ * Ported to 2.6.23 Copyright (C) 2008 by

+ *   Sander Huijsen <[email protected]>
+ *   Troy Kisky <[email protected]>
+ *   Dirk Behme <[email protected]>

hm. What's the story with authorship, attributions and signoffs here?

Written by TI (PSP team in India, ISTR) with no individual authorship credited, and shipped with a MontaVista 2.6.10 kernel. Ported as noted; I could presumably add my own copyright given recent updates I've made. Likewise Felipe Balbi. Kevin Hilman has signed off on various patches as part of merging them to the DaVinci tree.

(To the TI team reading this via the DaVinci list: I think Andrew is hinting that a Signed-off-By from a TI person would be a Nice Thing. Same for Dirk, and maybe others.)

Quoted 15 lines

Review

Quoted 13 lines

...

(quoted diff) +11 -0
+#ifdef CONFIG_MTD_PARTITIONS
+static inline int mtd_has_partitions(void) { return 1; }
+#else
+static inline int mtd_has_partitions(void) { return 0; }
+#endif
+
+#ifdef CONFIG_MTD_CMDLINE_PARTS
+static inline int mtd_has_cmdlinepart(void) { return 1; }
+#else
+static inline int mtd_has_cmdlinepart(void) { return 0; }
+#endif

These definitions shouldn't be buried in a .c file.

I will send along a patch to move them to <linux/mtd/...> headers, now that there seems to be a bit of recognition that the current ifdef-centric approach in the MTD mapping drivers is trouble. ;)

Quoted 10 lines

Review

Quoted 7 lines

...

(quoted diff) +4 -0
+static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode)
+{
+	struct davinci_nand_info *info;
+	u32 retval;

The identifier `retval' is usually used to identify the value which this function will return.

True; resolved in the appended fixup patch.

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
+				      const u_char *dat, u_char *ecc_code)
+{
+	unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
+	unsigned int tmp = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);

argh.

It seems the best-dressed pirates have parrots named "argh"! ;)

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	/* invert so that erased block ecc is correct */
+	tmp = ~tmp;
+	ecc_code[0] = (u_char)(tmp);
+	ecc_code[1] = (u_char)(tmp >> 8);
+	ecc_code[2] = (u_char)(tmp >> 16);

Is there some library facility which is being open-coded here?

I don't know of such a facility: 24-bit integer into 3-byte buffer.

Quoted 28 lines
Quoted 15 lines
(quoted diff) +12 -0
+	return 0;
+}
+
 
...
 
+static int __init nand_davinci_probe(struct platform_device *pdev)
+{
+		... deletia ...
+
+	info = kzalloc(sizeof(*info), GFP_KERNEL);
+	if (!info) {
+		dev_err(&pdev->dev, "unable to allocate memory\n");
+		ret = -ENOMEM;
+		goto err_nomem;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	}
+
+	platform_set_drvdata(pdev, info);
+
+	res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	if (!res1 || !res2) {
+		dev_err(&pdev->dev, "resource missing\n");
+		ret = -EINVAL;
+		goto err_res;

This leaks `info'.

Quoted 1 line

Please check all the error path unwinding here.

OK -- that does look buggish.

(Kevin -- I suggest you merge this to the DaVinci tree to make the eventual resync-with-mainline easier.)

=====================
From: David Brownell <[email protected]>

Minor fixes to the davinci-nand driver patch.

---
 drivers/mtd/nand/davinci_nand.c |   35 ++++++++++++++++-------------------
 1 file changed, 16 insertions(+), 19 deletions(-)
drivers/mtd/nand/davinci_nand.c +16 -19
@@ -161,7 +161,7 @@ static inline u32 nand_davinci_readecc_1
 static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode)
 {
 	struct davinci_nand_info *info;
-	u32 retval;
+	u32 nandcfr;
 	unsigned long flags;
 
 	info = to_davinci_nand(mtd);
@@ -172,9 +172,9 @@ static void nand_davinci_hwctl_1bit(stru
 	spin_lock_irqsave(&davinci_nand_lock, flags);
 
 	/* Restart ECC hardware */
-	retval = davinci_nand_readl(info, NANDFCR_OFFSET);
-	retval |= BIT(8 + info->core_chipsel);
-	davinci_nand_writel(info, NANDFCR_OFFSET, retval);
+	nandcfr = davinci_nand_readl(info, NANDFCR_OFFSET);
+	nandcfr |= BIT(8 + info->core_chipsel);
+	davinci_nand_writel(info, NANDFCR_OFFSET, nandcfr);
 
 	spin_unlock_irqrestore(&davinci_nand_lock, flags);
 }
@@ -186,13 +186,13 @@ static int nand_davinci_calculate_1bit(s
 				      const u_char *dat, u_char *ecc_code)
 {
 	unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
-	unsigned int tmp = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
+	unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
 
 	/* invert so that erased block ecc is correct */
-	tmp = ~tmp;
-	ecc_code[0] = (u_char)(tmp);
-	ecc_code[1] = (u_char)(tmp >> 8);
-	ecc_code[2] = (u_char)(tmp >> 16);
+	ecc24 = ~ecc24;
+	ecc_code[0] = (u_char)(ecc24);
+	ecc_code[1] = (u_char)(ecc24 >> 8);
+	ecc_code[2] = (u_char)(ecc24 >> 16);
 
 	return 0;
 }
@@ -278,7 +278,7 @@ static int nand_davinci_dev_ready(struct
 
 static void __init nand_dm6446evm_flash_init(struct davinci_nand_info *info)
 {
-	u32 regval, tmp;
+	u32 regval, a1cr;
 
 	/*
 	 * NAND FLASH timings @ PLL1 == 459 MHz
@@ -297,11 +297,11 @@ static void __init nand_dm6446evm_flash_
 		| (3 << 2)            /* turnAround      ?? ns */
 		| (0 << 0)            /* asyncSize       8-bit bus */
 		;
-	tmp = davinci_nand_readl(info, A1CR_OFFSET);
-	if (tmp != regval) {
+	a1cr = davinci_nand_readl(info, A1CR_OFFSET);
+	if (a1cr != regval) {
 		dev_dbg(info->dev, "Warning: NAND config: Set A1CR " \
 		       "reg to 0x%08x, was 0x%08x, should be done by " \
-		       "bootloader.\n", regval, tmp);
+		       "bootloader.\n", regval, a1cr);
 		davinci_nand_writel(info, A1CR_OFFSET, regval);
 	}
 }
@@ -338,7 +338,7 @@ static int __init nand_davinci_probe(str
 	if (!res1 || !res2) {
 		dev_err(&pdev->dev, "resource missing\n");
 		ret = -EINVAL;
-		goto err_res;
+		goto err_nomem;
 	}
 
 	vaddr = ioremap(res1->start, res1->end - res1->start);
@@ -347,7 +347,6 @@ static int __init nand_davinci_probe(str
 		dev_err(&pdev->dev, "ioremap failed\n");
 		ret = -EINVAL;
 		goto err_ioremap;
-
 	}
 
 	info->dev		= &pdev->dev;
@@ -525,16 +524,14 @@ err_clk_enable:
 
 err_ecc:
 err_clk:
+err_ioremap:
 	if (base)
 		iounmap(base);
 	if (vaddr)
 		iounmap(vaddr);
 
-err_ioremap:
-	kfree(info);
-
 err_nomem:
-err_res:
+	kfree(info);
 	return ret;
 }
 
#6 lore raw
Quoted 3 lines · David Brownell

(To the TI team reading this via the DaVinci list: I think Andrew is hinting that a Signed-off-By from a TI person would be a Nice Thing. Same for Dirk, and maybe others.)

If it's really necessary, feel free to add mine:

Signature · 1 line
balbi
#7 lore raw

I and one of my colleague Vinod, who is no more with TI, worked on this driver. I can sign-off this driver.

Regards, Sudhakar

Quoted 17 lines · David Brownell
Quoted 3 lines

(To the TI team reading this via the DaVinci list: I think Andrew is hinting that a Signed-off-By from a TI person would be a Nice Thing. Same for Dirk, and maybe others.)

If it's really necessary, feel free to add mine:

--
balbi

_______________________________________________
Davinci-linux-open-source mailing list
[email protected]
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source

#8 lore raw
In reply to David Brownell
Quoted 108 lines · David Brownell
Quoted 8 lines · Andrew Morton
Quoted 6 lines

+ * Copyright (C) 2006 Texas Instruments.
+ *
+ * Ported to 2.6.23 Copyright (C) 2008 by

+ *   Sander Huijsen <[email protected]>
+ *   Troy Kisky <[email protected]>
+ *   Dirk Behme <[email protected]>

hm. What's the story with authorship, attributions and signoffs here?

Written by TI (PSP team in India, ISTR) with no individual authorship credited, and shipped with a MontaVista 2.6.10 kernel. Ported as noted; I could presumably add my own copyright given recent updates I've made. Likewise Felipe Balbi. Kevin Hilman has signed off on various patches as part of merging them to the DaVinci tree.

(To the TI team reading this via the DaVinci list: I think Andrew is hinting that a Signed-off-By from a TI person would be a Nice Thing. Same for Dirk, and maybe others.)

Quoted 15 lines

Review

Quoted 13 lines

...

(quoted diff) +11 -0
+#ifdef CONFIG_MTD_PARTITIONS
+static inline int mtd_has_partitions(void) { return 1; }
+#else
+static inline int mtd_has_partitions(void) { return 0; }
+#endif
+
+#ifdef CONFIG_MTD_CMDLINE_PARTS
+static inline int mtd_has_cmdlinepart(void) { return 1; }
+#else
+static inline int mtd_has_cmdlinepart(void) { return 0; }
+#endif

These definitions shouldn't be buried in a .c file.

I will send along a patch to move them to <linux/mtd/...> headers, now that there seems to be a bit of recognition that the current ifdef-centric approach in the MTD mapping drivers is trouble. ;)

Quoted 10 lines

Review

Quoted 7 lines

...

(quoted diff) +4 -0
+static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode)
+{
+	struct davinci_nand_info *info;
+	u32 retval;

The identifier `retval' is usually used to identify the value which this function will return.

True; resolved in the appended fixup patch.

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
+				      const u_char *dat, u_char *ecc_code)
+{
+	unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
+	unsigned int tmp = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);

argh.

It seems the best-dressed pirates have parrots named "argh"! ;)

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	/* invert so that erased block ecc is correct */
+	tmp = ~tmp;
+	ecc_code[0] = (u_char)(tmp);
+	ecc_code[1] = (u_char)(tmp >> 8);
+	ecc_code[2] = (u_char)(tmp >> 16);

Is there some library facility which is being open-coded here?

I don't know of such a facility: 24-bit integer into 3-byte buffer.

Quoted 28 lines
Quoted 15 lines
(quoted diff) +12 -0
+	return 0;
+}
+
 
...
 
+static int __init nand_davinci_probe(struct platform_device *pdev)
+{
+		... deletia ...
+
+	info = kzalloc(sizeof(*info), GFP_KERNEL);
+	if (!info) {
+		dev_err(&pdev->dev, "unable to allocate memory\n");
+		ret = -ENOMEM;
+		goto err_nomem;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	}
+
+	platform_set_drvdata(pdev, info);
+
+	res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	if (!res1 || !res2) {
+		dev_err(&pdev->dev, "resource missing\n");
+		ret = -EINVAL;
+		goto err_res;

This leaks `info'.

Quoted 7 lines
Quoted 1 line

Please check all the error path unwinding here.

OK -- that does look buggish.

(Kevin -- I suggest you merge this to the DaVinci tree to make the eventual resync-with-mainline easier.)

Done.

It also needs this cleanup bit below which is in DaVinci git now that we've deprecated the use of the davinci cpu_is_* macros in drivers.

This could be just folded into current patch if desired.

Thanks,

Kevin

commit 1bacc33ccc9bd0f3c109bf8a8550e9b6f99397bd

Author: Kevin Hilman <[email protected]>
Date:   Thu Feb 26 17:15:18 2009 -0800

MTD: NAND: drop usage of cpu_is_* macro

Usage of davinci-specific cpu_is macros is not allowed in drivers. These options should be passed in through platform_data.

drivers/mtd/nand/davinci_nand.c +0 -3
index aa70b4e..a2f78ad 100644
@@ -33,7 +33,6 @@
 #include <linux/mtd/nand.h>
 #include <linux/mtd/partitions.h>
 
-#include <mach/cpu.h>
 #include <mach/nand.h>
 
 #include <asm/mach-types.h>
@@ -392,8 +391,6 @@ static int __init nand_davinci_probe(struct platform_device *pdev)
 	/* use board-specific ECC config; else, the best available */
 	if (pdata)
 		ecc_mode = pdata->ecc_mode;
-	else if (cpu_is_davinci_dm355())
-		ecc_mode = NAND_ECC_HW_SYNDROME;
 	else
 		ecc_mode = NAND_ECC_HW;
 
#9 lore raw

PATCH 1/1 MTD: partitioning utility predicates

In reply to Andrew Morton

From: David Brownell <[email protected]>

Move mtd_has_partitions() and mtd_has_cmdlinepart() inlines from a DaVinci-specific driver to the <linux/mtd/partitions.h> header.

Use those to eliminate #ifdefs in two drivers which had their own definitions of mtd_has_partitions().

---
Quite a lot of other MTD drivers could benefit from using use one or both of these to remove #ifdeffery. Maybe some Janitors would like to help?

 drivers/mtd/devices/m25p80.c        |   17 ++++++-----------
 drivers/mtd/devices/mtd_dataflash.c |   16 ++++++----------
 drivers/mtd/nand/davinci_nand.c     |   13 -------------
 include/linux/mtd/partitions.h      |   12 ++++++++++++
 4 files changed, 24 insertions(+), 34 deletions(-)
drivers/mtd/devices/m25p80.c +6 -11
@@ -65,12 +65,6 @@
 #define FAST_READ_DUMMY_BYTE 0
 #endif
 
-#ifdef CONFIG_MTD_PARTITIONS
-#define	mtd_has_partitions()	(1)
-#else
-#define	mtd_has_partitions()	(0)
-#endif
-
 /****************************************************************************/
 
 struct m25p {
@@ -708,12 +702,13 @@ static int __devinit m25p_probe(struct s
 		struct mtd_partition	*parts = NULL;
 		int			nr_parts = 0;
 
-#ifdef CONFIG_MTD_CMDLINE_PARTS
-		static const char *part_probes[] = { "cmdlinepart", NULL, };
+		if (mtd_has_cmdlinepart()) {
+			static const char *part_probes[]
+					= { "cmdlinepart", NULL, };
 
-		nr_parts = parse_mtd_partitions(&flash->mtd,
-				part_probes, &parts, 0);
-#endif
+			nr_parts = parse_mtd_partitions(&flash->mtd,
+					part_probes, &parts, 0);
+		}
 
 		if (nr_parts <= 0 && data && data->parts) {
 			parts = data->parts;
drivers/mtd/devices/mtd_dataflash.c +6 -10
@@ -98,12 +98,6 @@ struct dataflash {
 	struct mtd_info		mtd;
 };
 
-#ifdef CONFIG_MTD_PARTITIONS
-#define	mtd_has_partitions()	(1)
-#else
-#define	mtd_has_partitions()	(0)
-#endif
-
 /* ......................................................................... */
 
 /*
@@ -682,11 +676,13 @@ add_dataflash_otp(struct spi_device *spi
 		struct mtd_partition	*parts;
 		int			nr_parts = 0;
 
-#ifdef CONFIG_MTD_CMDLINE_PARTS
-		static const char *part_probes[] = { "cmdlinepart", NULL, };
+		if (mtd_has_cmdlinepart()) {
+			static const char *part_probes[]
+					= { "cmdlinepart", NULL, };
 
-		nr_parts = parse_mtd_partitions(device, part_probes, &parts, 0);
-#endif
+			nr_parts = parse_mtd_partitions(device,
+					part_probes, &parts, 0);
+		}
 
 		if (nr_parts <= 0 && pdata && pdata->parts) {
 			parts = pdata->parts;
drivers/mtd/nand/davinci_nand.c +0 -13
@@ -39,19 +39,6 @@
 #include <asm/mach-types.h>
 
 
-#ifdef CONFIG_MTD_PARTITIONS
-static inline int mtd_has_partitions(void) { return 1; }
-#else
-static inline int mtd_has_partitions(void) { return 0; }
-#endif
-
-#ifdef CONFIG_MTD_CMDLINE_PARTS
-static inline int mtd_has_cmdlinepart(void) { return 1; }
-#else
-static inline int mtd_has_cmdlinepart(void) { return 0; }
-#endif
-
-
 /*
  * This is a device driver for the NAND flash controller found on the
  * various DaVinci family chips.  It handles up to four SoC chipselects,
include/linux/mtd/partitions.h +12 -0
@@ -76,4 +76,16 @@ int __devinit of_mtd_parse_partitions(st
                                       struct device_node *node,
                                       struct mtd_partition **pparts);
 
+#ifdef CONFIG_MTD_PARTITIONS
+static inline int mtd_has_partitions(void) { return 1; }
+#else
+static inline int mtd_has_partitions(void) { return 0; }
+#endif
+
+#ifdef CONFIG_MTD_CMDLINE_PARTS
+static inline int mtd_has_cmdlinepart(void) { return 1; }
+#else
+static inline int mtd_has_cmdlinepart(void) { return 0; }
+#endif
+
 #endif