public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [patch] generic nand driver for SoCs
@ 2007-05-01 12:24 Vitaly Wool
  2007-05-01 13:00 ` Thomas Gleixner
  2007-05-01 14:17 ` Stefan Roese
  0 siblings, 2 replies; 6+ messages in thread
From: Vitaly Wool @ 2007-05-01 12:24 UTC (permalink / raw)
  To: linux-mtd

This is a very first shot on the "generic NAND driver" subject as was 
discussed by Lennert and Thomas.

 drivers/mtd/nand/Makefile   |    2
 drivers/mtd/nand/Kconfig    |   12 +++
 drivers/mtd/nand/gen_nand.c |  160 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+)

Signed-off-by: Vitaly Wool <vitalywool@gmail.com>

Index: linux-2.6/drivers/mtd/nand/gen_nand.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/mtd/nand/gen_nand.c
@@ -0,0 +1,160 @@
+/*
+ * Generic NAND driver
+ *
+ * Author: Vitaly Wool <vitalywool@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/nand.h>
+#include <linux/mtd/partitions.h>
+#include <linux/io.h>
+
+struct gen_nand_data {
+	struct nand_chip	chip;
+	struct mtd_info		mtd;
+	void __iomem		*io_base;
+#ifdef CONFIG_MTD_PARTITIONS
+	int			nr_parts;
+	struct mtd_partition	*parts;
+#endif
+};
+
+#ifdef CONFIG_MTD_PARTITIONS
+static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
+#endif
+
+/*
+ * Probe for the NAND device.
+ */
+static int __init gen_nand_probe(struct platform_device *pdev)
+{
+	struct platform_nand_data *pdata = pdev->dev.platform_data;
+	struct gen_nand_data *data;
+	int res;
+
+	/* Allocate memory for the device structure (and zero it) */
+	data = kzalloc(sizeof(struct gen_nand_data), GFP_KERNEL);
+	if (!data) {
+		dev_err(&pdev->dev, "failed to allocate device structure.\n");
+		return -ENOMEM;
+	}
+
+	data->io_base = ioremap(pdev->resource[0].start,
+				pdev->resource[0].end - pdev->resource[0].start + 1);
+	if (data->io_base == NULL) {
+		dev_err(&pdev->dev, "ioremap failed\n");
+		kfree(data);
+		return -EIO;
+	}
+
+	data->chip.priv = &data;	/* link the private data structures */
+	data->mtd.priv = &data->chip;
+	data->mtd.owner = THIS_MODULE;
+
+	data->chip.IO_ADDR_R = data->io_base;
+	data->chip.IO_ADDR_W = data->io_base;
+	if (pdata->ctrl.cmd_ctrl)
+		data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
+	if (pdata->ctrl.dev_ready)
+		data->chip.dev_ready = pdata->ctrl.dev_ready;
+	if (pdata->ctrl.select_chip)
+		data->chip.select_chip = pdata->ctrl.select_chip;
+	if (pdata->ctrl.hwcontrol)
+		data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
+	if (pdata->chip.ecclayout)
+		data->chip.ecc.layout = pdata->chip.ecclayout;
+	data->chip.ecc.mode = NAND_ECC_SOFT;
+	data->chip.chip_delay = pdata->chip.chip_delay;
+	data->chip.options |= pdata->chip.options;
+
+	platform_set_drvdata(pdev, data);
+
+	/* Scan to find existance of the device */
+	if (nand_scan(&data->mtd, 1)) {
+		res = -ENXIO;
+		goto out;
+	}
+
+#ifdef CONFIG_MTD_PARTITIONS
+	res = parse_mtd_partitions(&data->mtd, part_probe_types, &data->parts, 0);
+	if (res > 0) {
+		add_mtd_partitions(&data->mtd, data->parts, res);
+		return 0;
+	} else if (pdata->chip.partitions) {
+		data->parts = pdata->chip.partitions;
+		res = add_mtd_partitions(&data->mtd, data->parts, pdata->chip.nr_partitions);
+	}
+#else
+	res = add_mtd_device(data->mtd);
+#endif
+
+	if (!res)
+		return res;
+
+	nand_release(&data->mtd);
+out:
+	platform_set_drvdata(pdev, NULL);
+	iounmap(data->io_base);
+	kfree(data);
+	return res;
+}
+
+/*
+ * Remove a NAND device.
+ */
+static int __devexit gen_nand_remove(struct platform_device *pdev)
+{
+	struct gen_nand_data *data = platform_get_drvdata(pdev);
+	struct platform_nand_data *pdata = pdev->dev.platform_data;
+
+#ifdef CONFIG_MTD_PARTITIONS
+	if (data->parts) {
+		del_mtd_partitions(&data->mtd);
+		if (data->parts != pdata->chip.partitions)
+			kfree(data->parts);
+	} else
+		del_mtd_device(&data->mtd);
+#else
+	del_mtd_device(&data->mtd);
+#endif
+
+	nand_release(&data->mtd);
+	iounmap(data->io_base);
+	kfree(data);
+
+	return 0;
+}
+
+static struct platform_driver gen_nand_driver = {
+	.probe		= gen_nand_probe,
+	.remove		= gen_nand_remove,
+	.driver		= {
+		.name	= "gen_nand",
+		.owner	= THIS_MODULE,
+	},
+};
+
+static int __init gen_nand_init(void)
+{
+	return platform_driver_register(&gen_nand_driver);
+}
+
+static void __exit gen_nand_exit(void)
+{
+	platform_driver_unregister(&gen_nand_driver);
+}
+
+module_init(gen_nand_init);
+module_exit(gen_nand_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Vitaly Wool");
+MODULE_DESCRIPTION("Simple generic NAND driver");
Index: linux-2.6/drivers/mtd/nand/Kconfig
===================================================================
--- linux-2.6.orig/drivers/mtd/nand/Kconfig
+++ linux-2.6/drivers/mtd/nand/Kconfig
@@ -277,4 +277,16 @@ config MTD_NAND_PLATFORM
 	help
 	  This driver enables support for common on-SoC NAND devices.
 
+config MTD_NAND_PLATFORM
+	tristate "Support for generic NAND driver"
+	depends on MTD_NAND
+	help
+	  This driver enables support for common on-SoC NAND devices.
+
+config MTD_NAND_PLATFORM
+	tristate "Support for generic NAND driver"
+	depends on MTD_NAND
+	help
+	  This driver enables support for common on-SoC NAND devices.
+
 endmenu
Index: linux-2.6/drivers/mtd/nand/gen_nand.c
===================================================================
--- linux-2.6.orig/drivers/mtd/nand/Makefile
+++ linux-2.6/drivers/mtd/nand/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_MTD_NAND_CS553X)		+= cs553x
 obj-$(CONFIG_MTD_NAND_NDFC)		+= ndfc.o
 obj-$(CONFIG_MTD_NAND_AT91)		+= at91_nand.o
 obj-$(CONFIG_MTD_NAND_BASLER_EXCITE)	+= excite_nandflash.o
+obj-$(CONFIG_MTD_NAND_PLATFORM)		+= gen_nand.o
 
 nand-objs := nand_base.o nand_bbt.o
 cafe_nand-objs := cafe.o cafe_ecc.o

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] generic nand driver for SoCs
  2007-05-01 12:24 [patch] generic nand driver for SoCs Vitaly Wool
@ 2007-05-01 13:00 ` Thomas Gleixner
  2007-05-01 13:03   ` Vitaly Wool
  2007-05-01 14:17 ` Stefan Roese
  1 sibling, 1 reply; 6+ messages in thread
From: Thomas Gleixner @ 2007-05-01 13:00 UTC (permalink / raw)
  To: Vitaly Wool; +Cc: linux-mtd

On Tue, 2007-05-01 at 16:24 +0400, Vitaly Wool wrote:
> This is a very first shot on the "generic NAND driver" subject as was 
> discussed by Lennert and Thomas.

> +#include <linux/slab.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/mtd/mtd.h>
> +#include <linux/mtd/nand.h>
> +#include <linux/mtd/partitions.h>
> +#include <linux/io.h>

#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>

#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>

Please

> +struct gen_nand_data {
> +	struct nand_chip	chip;
> +	struct mtd_info		mtd;
> +	void __iomem		*io_base;
> +#ifdef CONFIG_MTD_PARTITIONS
> +	int			nr_parts;
> +	struct mtd_partition	*parts;
> +#endif
> +};
> +
> +#ifdef CONFIG_MTD_PARTITIONS
> +static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
> +#endif
> +

Hmm, this should be provided by the platform as well.

> +/*
> + * Probe for the NAND device.
> + */
> +static int __init gen_nand_probe(struct platform_device *pdev)
> +{
> +	struct platform_nand_data *pdata = pdev->dev.platform_data;
> +	struct gen_nand_data *data;
> +	int res;
> +
> +	/* Allocate memory for the device structure (and zero it) */
> +	data = kzalloc(sizeof(struct gen_nand_data), GFP_KERNEL);
> +	if (!data) {
> +		dev_err(&pdev->dev, "failed to allocate device structure.\n");
> +		return -ENOMEM;
> +	}
> +
> +	data->io_base = ioremap(pdev->resource[0].start,
> +				pdev->resource[0].end - pdev->resource[0].start + 1);
> +	if (data->io_base == NULL) {
> +		dev_err(&pdev->dev, "ioremap failed\n");
> +		kfree(data);
> +		return -EIO;
> +	}
> +
> +	data->chip.priv = &data;	/* link the private data structures */

	No comments after code please

> +	data->mtd.priv = &data->chip;
> +	data->mtd.owner = THIS_MODULE;
> +
> +	data->chip.IO_ADDR_R = data->io_base;
> +	data->chip.IO_ADDR_W = data->io_base;
> +	if (pdata->ctrl.cmd_ctrl)
> +		data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
> +	if (pdata->ctrl.dev_ready)
> +		data->chip.dev_ready = pdata->ctrl.dev_ready;
> +	if (pdata->ctrl.select_chip)
> +		data->chip.select_chip = pdata->ctrl.select_chip;
> +	if (pdata->ctrl.hwcontrol)
> +		data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
> +	if (pdata->chip.ecclayout)
> +		data->chip.ecc.layout = pdata->chip.ecclayout;

	The if(..)s are superfluid. 

> +static int __devexit gen_nand_remove(struct platform_device *pdev)
> +{
> +	struct gen_nand_data *data = platform_get_drvdata(pdev);
> +	struct platform_nand_data *pdata = pdev->dev.platform_data;
> +
> +#ifdef CONFIG_MTD_PARTITIONS
> +	if (data->parts) {
> +		del_mtd_partitions(&data->mtd);
> +		if (data->parts != pdata->chip.partitions)
> +			kfree(data->parts);
> +	} else
> +		del_mtd_device(&data->mtd);
> +#else
> +	del_mtd_device(&data->mtd);
> +#endif

This all except kfree(data->parts) is done in nand_release !

> +	nand_release(&data->mtd);
> +	iounmap(data->io_base);
> +	kfree(data);
> +
> +	return 0;
> +}
> +

Looks good otherwise.

	tglx

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] generic nand driver for SoCs
  2007-05-01 13:00 ` Thomas Gleixner
@ 2007-05-01 13:03   ` Vitaly Wool
  2007-05-01 13:09     ` Thomas Gleixner
  0 siblings, 1 reply; 6+ messages in thread
From: Vitaly Wool @ 2007-05-01 13:03 UTC (permalink / raw)
  To: tglx; +Cc: linux-mtd

On 5/1/07, Thomas Gleixner <tglx@linutronix.de> wrote:
> > +#ifdef CONFIG_MTD_PARTITIONS
> > +static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
> > +#endif
> > +
> Hmm, this should be provided by the platform as well.

Really? I don't think platform code can call parse_mtd_partitions --
nothing is set up at that moment yet. Please correct me if I'm missing
something.

Other than that, I do agree with your comments, thanks,

Vitaly

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] generic nand driver for SoCs
  2007-05-01 13:03   ` Vitaly Wool
@ 2007-05-01 13:09     ` Thomas Gleixner
  2007-05-01 13:11       ` Vitaly Wool
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Gleixner @ 2007-05-01 13:09 UTC (permalink / raw)
  To: Vitaly Wool; +Cc: linux-mtd

On Tue, 2007-05-01 at 17:03 +0400, Vitaly Wool wrote:
> On 5/1/07, Thomas Gleixner <tglx@linutronix.de> wrote:
> > > +#ifdef CONFIG_MTD_PARTITIONS
> > > +static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
> > > +#endif
> > > +
> > Hmm, this should be provided by the platform as well.
> 
> Really? I don't think platform code can call parse_mtd_partitions --
> nothing is set up at that moment yet. Please correct me if I'm missing
> something.

I meant the list of probe types which should be done.

	tglx

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] generic nand driver for SoCs
  2007-05-01 13:09     ` Thomas Gleixner
@ 2007-05-01 13:11       ` Vitaly Wool
  0 siblings, 0 replies; 6+ messages in thread
From: Vitaly Wool @ 2007-05-01 13:11 UTC (permalink / raw)
  To: tglx; +Cc: linux-mtd

On 5/1/07, Thomas Gleixner <tglx@linutronix.de> wrote:
> On Tue, 2007-05-01 at 17:03 +0400, Vitaly Wool wrote:
> > On 5/1/07, Thomas Gleixner <tglx@linutronix.de> wrote:
> > > > +#ifdef CONFIG_MTD_PARTITIONS
> > > > +static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
> > > > +#endif
> > > > +
> > > Hmm, this should be provided by the platform as well.
> >
> > Really? I don't think platform code can call parse_mtd_partitions --
> > nothing is set up at that moment yet. Please correct me if I'm missing
> > something.
>
> I meant the list of probe types which should be done.

Ah, okay, will do.

Vitaly

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] generic nand driver for SoCs
  2007-05-01 12:24 [patch] generic nand driver for SoCs Vitaly Wool
  2007-05-01 13:00 ` Thomas Gleixner
@ 2007-05-01 14:17 ` Stefan Roese
  1 sibling, 0 replies; 6+ messages in thread
From: Stefan Roese @ 2007-05-01 14:17 UTC (permalink / raw)
  To: linux-mtd; +Cc: Vitaly Wool

On Tuesday 01 May 2007 14:24, Vitaly Wool wrote:
> Index: linux-2.6/drivers/mtd/nand/Kconfig
> ===================================================================
> --- linux-2.6.orig/drivers/mtd/nand/Kconfig
> +++ linux-2.6/drivers/mtd/nand/Kconfig
> @@ -277,4 +277,16 @@ config MTD_NAND_PLATFORM
>  	help
>  	  This driver enables support for common on-SoC NAND devices.
>
> +config MTD_NAND_PLATFORM
> +	tristate "Support for generic NAND driver"
> +	depends on MTD_NAND
> +	help
> +	  This driver enables support for common on-SoC NAND devices.
> +
> +config MTD_NAND_PLATFORM
> +	tristate "Support for generic NAND driver"
> +	depends on MTD_NAND
> +	help
> +	  This driver enables support for common on-SoC NAND devices.
> +

One "config MTD_NAND_PLATFORM" should be enough, don't you think? ;-)

Best regards,
Stefan

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-05-01 14:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-01 12:24 [patch] generic nand driver for SoCs Vitaly Wool
2007-05-01 13:00 ` Thomas Gleixner
2007-05-01 13:03   ` Vitaly Wool
2007-05-01 13:09     ` Thomas Gleixner
2007-05-01 13:11       ` Vitaly Wool
2007-05-01 14:17 ` Stefan Roese

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox