| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * Generic NAND driver | 
 | 3 |  * | 
 | 4 |  * Author: Vitaly Wool <vitalywool@gmail.com> | 
 | 5 |  * | 
 | 6 |  * This program is free software; you can redistribute it and/or modify | 
 | 7 |  * it under the terms of the GNU General Public License version 2 as | 
 | 8 |  * published by the Free Software Foundation. | 
 | 9 |  * | 
 | 10 |  */ | 
 | 11 |  | 
 | 12 | #include <linux/io.h> | 
 | 13 | #include <linux/module.h> | 
 | 14 | #include <linux/platform_device.h> | 
 | 15 | #include <linux/slab.h> | 
 | 16 | #include <linux/mtd/mtd.h> | 
 | 17 | #include <linux/mtd/nand.h> | 
 | 18 | #include <linux/mtd/partitions.h> | 
 | 19 |  | 
 | 20 | struct plat_nand_data { | 
 | 21 | 	struct nand_chip	chip; | 
 | 22 | 	struct mtd_info		mtd; | 
 | 23 | 	void __iomem		*io_base; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 24 | }; | 
 | 25 |  | 
 | 26 | /* | 
 | 27 |  * Probe for the NAND device. | 
 | 28 |  */ | 
| Uwe Kleine-König | 9d63287 | 2009-03-28 00:26:58 +0100 | [diff] [blame] | 29 | static int __devinit plat_nand_probe(struct platform_device *pdev) | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 30 | { | 
 | 31 | 	struct platform_nand_data *pdata = pdev->dev.platform_data; | 
 | 32 | 	struct plat_nand_data *data; | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 33 | 	struct resource *res; | 
 | 34 | 	int err = 0; | 
 | 35 |  | 
| Marek Vasut | 01cd2ab | 2010-08-12 03:53:55 +0100 | [diff] [blame] | 36 | 	if (pdata->chip.nr_chips < 1) { | 
 | 37 | 		dev_err(&pdev->dev, "invalid number of chips specified\n"); | 
 | 38 | 		return -EINVAL; | 
 | 39 | 	} | 
 | 40 |  | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 41 | 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
 | 42 | 	if (!res) | 
 | 43 | 		return -ENXIO; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 44 |  | 
 | 45 | 	/* Allocate memory for the device structure (and zero it) */ | 
 | 46 | 	data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL); | 
 | 47 | 	if (!data) { | 
 | 48 | 		dev_err(&pdev->dev, "failed to allocate device structure.\n"); | 
 | 49 | 		return -ENOMEM; | 
 | 50 | 	} | 
 | 51 |  | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 52 | 	if (!request_mem_region(res->start, resource_size(res), | 
 | 53 | 				dev_name(&pdev->dev))) { | 
 | 54 | 		dev_err(&pdev->dev, "request_mem_region failed\n"); | 
 | 55 | 		err = -EBUSY; | 
 | 56 | 		goto out_free; | 
 | 57 | 	} | 
 | 58 |  | 
 | 59 | 	data->io_base = ioremap(res->start, resource_size(res)); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 60 | 	if (data->io_base == NULL) { | 
 | 61 | 		dev_err(&pdev->dev, "ioremap failed\n"); | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 62 | 		err = -EIO; | 
 | 63 | 		goto out_release_io; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 64 | 	} | 
 | 65 |  | 
 | 66 | 	data->chip.priv = &data; | 
 | 67 | 	data->mtd.priv = &data->chip; | 
 | 68 | 	data->mtd.owner = THIS_MODULE; | 
| Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 69 | 	data->mtd.name = dev_name(&pdev->dev); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 70 |  | 
 | 71 | 	data->chip.IO_ADDR_R = data->io_base; | 
 | 72 | 	data->chip.IO_ADDR_W = data->io_base; | 
 | 73 | 	data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl; | 
 | 74 | 	data->chip.dev_ready = pdata->ctrl.dev_ready; | 
 | 75 | 	data->chip.select_chip = pdata->ctrl.select_chip; | 
| Alexander Clouter | d6fed9e | 2009-05-11 19:28:01 +0100 | [diff] [blame] | 76 | 	data->chip.write_buf = pdata->ctrl.write_buf; | 
 | 77 | 	data->chip.read_buf = pdata->ctrl.read_buf; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 78 | 	data->chip.chip_delay = pdata->chip.chip_delay; | 
 | 79 | 	data->chip.options |= pdata->chip.options; | 
| Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 80 | 	data->chip.bbt_options |= pdata->chip.bbt_options; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 81 |  | 
 | 82 | 	data->chip.ecc.hwctl = pdata->ctrl.hwcontrol; | 
 | 83 | 	data->chip.ecc.layout = pdata->chip.ecclayout; | 
 | 84 | 	data->chip.ecc.mode = NAND_ECC_SOFT; | 
 | 85 |  | 
 | 86 | 	platform_set_drvdata(pdev, data); | 
 | 87 |  | 
| H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 88 | 	/* Handle any platform specific setup */ | 
 | 89 | 	if (pdata->ctrl.probe) { | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 90 | 		err = pdata->ctrl.probe(pdev); | 
 | 91 | 		if (err) | 
| H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 92 | 			goto out; | 
 | 93 | 	} | 
 | 94 |  | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 95 | 	/* Scan to find existence of the device */ | 
| Marek Vasut | 81cbb0b | 2010-07-28 07:36:54 +0200 | [diff] [blame] | 96 | 	if (nand_scan(&data->mtd, pdata->chip.nr_chips)) { | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 97 | 		err = -ENXIO; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 98 | 		goto out; | 
 | 99 | 	} | 
 | 100 |  | 
| Dmitry Eremin-Solenikov | 009c840 | 2011-06-02 18:00:57 +0400 | [diff] [blame] | 101 | 	err = mtd_device_parse_register(&data->mtd, | 
 | 102 | 			pdata->chip.part_probe_types, 0, | 
 | 103 | 			pdata->chip.partitions, pdata->chip.nr_partitions); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 104 |  | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 105 | 	if (!err) | 
 | 106 | 		return err; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 107 |  | 
 | 108 | 	nand_release(&data->mtd); | 
 | 109 | out: | 
| H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 110 | 	if (pdata->ctrl.remove) | 
 | 111 | 		pdata->ctrl.remove(pdev); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 112 | 	platform_set_drvdata(pdev, NULL); | 
 | 113 | 	iounmap(data->io_base); | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 114 | out_release_io: | 
 | 115 | 	release_mem_region(res->start, resource_size(res)); | 
 | 116 | out_free: | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 117 | 	kfree(data); | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 118 | 	return err; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 119 | } | 
 | 120 |  | 
 | 121 | /* | 
 | 122 |  * Remove a NAND device. | 
 | 123 |  */ | 
 | 124 | static int __devexit plat_nand_remove(struct platform_device *pdev) | 
 | 125 | { | 
 | 126 | 	struct plat_nand_data *data = platform_get_drvdata(pdev); | 
 | 127 | 	struct platform_nand_data *pdata = pdev->dev.platform_data; | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 128 | 	struct resource *res; | 
 | 129 |  | 
 | 130 | 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 131 |  | 
 | 132 | 	nand_release(&data->mtd); | 
| H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 133 | 	if (pdata->ctrl.remove) | 
 | 134 | 		pdata->ctrl.remove(pdev); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 135 | 	iounmap(data->io_base); | 
| H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 136 | 	release_mem_region(res->start, resource_size(res)); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 137 | 	kfree(data); | 
 | 138 |  | 
 | 139 | 	return 0; | 
 | 140 | } | 
 | 141 |  | 
 | 142 | static struct platform_driver plat_nand_driver = { | 
 | 143 | 	.probe		= plat_nand_probe, | 
| Thomas Chou | 24b5ce2 | 2009-04-21 12:27:34 +0800 | [diff] [blame] | 144 | 	.remove		= __devexit_p(plat_nand_remove), | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 145 | 	.driver		= { | 
 | 146 | 		.name	= "gen_nand", | 
 | 147 | 		.owner	= THIS_MODULE, | 
 | 148 | 	}, | 
 | 149 | }; | 
 | 150 |  | 
 | 151 | static int __init plat_nand_init(void) | 
 | 152 | { | 
 | 153 | 	return platform_driver_register(&plat_nand_driver); | 
 | 154 | } | 
 | 155 |  | 
 | 156 | static void __exit plat_nand_exit(void) | 
 | 157 | { | 
 | 158 | 	platform_driver_unregister(&plat_nand_driver); | 
 | 159 | } | 
 | 160 |  | 
 | 161 | module_init(plat_nand_init); | 
 | 162 | module_exit(plat_nand_exit); | 
 | 163 |  | 
 | 164 | MODULE_LICENSE("GPL"); | 
 | 165 | MODULE_AUTHOR("Vitaly Wool"); | 
 | 166 | MODULE_DESCRIPTION("Simple generic NAND driver"); | 
| Kay Sievers | 1ff1842 | 2008-04-18 13:44:27 -0700 | [diff] [blame] | 167 | MODULE_ALIAS("platform:gen_nand"); |