| 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; | 
|  | 24 | #ifdef CONFIG_MTD_PARTITIONS | 
|  | 25 | int			nr_parts; | 
|  | 26 | struct mtd_partition	*parts; | 
|  | 27 | #endif | 
|  | 28 | }; | 
|  | 29 |  | 
|  | 30 | /* | 
|  | 31 | * Probe for the NAND device. | 
|  | 32 | */ | 
| Uwe Kleine-König | 9d63287 | 2009-03-28 00:26:58 +0100 | [diff] [blame] | 33 | static int __devinit plat_nand_probe(struct platform_device *pdev) | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 34 | { | 
|  | 35 | struct platform_nand_data *pdata = pdev->dev.platform_data; | 
|  | 36 | struct plat_nand_data *data; | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 37 | struct resource *res; | 
|  | 38 | int err = 0; | 
|  | 39 |  | 
|  | 40 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
|  | 41 | if (!res) | 
|  | 42 | return -ENXIO; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 43 |  | 
|  | 44 | /* Allocate memory for the device structure (and zero it) */ | 
|  | 45 | data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL); | 
|  | 46 | if (!data) { | 
|  | 47 | dev_err(&pdev->dev, "failed to allocate device structure.\n"); | 
|  | 48 | return -ENOMEM; | 
|  | 49 | } | 
|  | 50 |  | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 51 | if (!request_mem_region(res->start, resource_size(res), | 
|  | 52 | dev_name(&pdev->dev))) { | 
|  | 53 | dev_err(&pdev->dev, "request_mem_region failed\n"); | 
|  | 54 | err = -EBUSY; | 
|  | 55 | goto out_free; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | data->io_base = ioremap(res->start, resource_size(res)); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 59 | if (data->io_base == NULL) { | 
|  | 60 | dev_err(&pdev->dev, "ioremap failed\n"); | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 61 | err = -EIO; | 
|  | 62 | goto out_release_io; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
|  | 65 | data->chip.priv = &data; | 
|  | 66 | data->mtd.priv = &data->chip; | 
|  | 67 | data->mtd.owner = THIS_MODULE; | 
| Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 68 | data->mtd.name = dev_name(&pdev->dev); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 69 |  | 
|  | 70 | data->chip.IO_ADDR_R = data->io_base; | 
|  | 71 | data->chip.IO_ADDR_W = data->io_base; | 
|  | 72 | data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl; | 
|  | 73 | data->chip.dev_ready = pdata->ctrl.dev_ready; | 
|  | 74 | data->chip.select_chip = pdata->ctrl.select_chip; | 
| Alexander Clouter | d6fed9e | 2009-05-11 19:28:01 +0100 | [diff] [blame] | 75 | data->chip.write_buf = pdata->ctrl.write_buf; | 
|  | 76 | data->chip.read_buf = pdata->ctrl.read_buf; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 77 | data->chip.chip_delay = pdata->chip.chip_delay; | 
|  | 78 | data->chip.options |= pdata->chip.options; | 
|  | 79 |  | 
|  | 80 | data->chip.ecc.hwctl = pdata->ctrl.hwcontrol; | 
|  | 81 | data->chip.ecc.layout = pdata->chip.ecclayout; | 
|  | 82 | data->chip.ecc.mode = NAND_ECC_SOFT; | 
|  | 83 |  | 
|  | 84 | platform_set_drvdata(pdev, data); | 
|  | 85 |  | 
| H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 86 | /* Handle any platform specific setup */ | 
|  | 87 | if (pdata->ctrl.probe) { | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 88 | err = pdata->ctrl.probe(pdev); | 
|  | 89 | if (err) | 
| H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 90 | goto out; | 
|  | 91 | } | 
|  | 92 |  | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 93 | /* Scan to find existance of the device */ | 
|  | 94 | if (nand_scan(&data->mtd, 1)) { | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 95 | err = -ENXIO; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 96 | goto out; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | #ifdef CONFIG_MTD_PARTITIONS | 
|  | 100 | if (pdata->chip.part_probe_types) { | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 101 | err = parse_mtd_partitions(&data->mtd, | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 102 | pdata->chip.part_probe_types, | 
|  | 103 | &data->parts, 0); | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 104 | if (err > 0) { | 
|  | 105 | add_mtd_partitions(&data->mtd, data->parts, err); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 106 | return 0; | 
|  | 107 | } | 
|  | 108 | } | 
| H Hartley Sweeten | f36e20c | 2009-05-12 13:46:59 -0700 | [diff] [blame] | 109 | if (pdata->chip.set_parts) | 
|  | 110 | pdata->chip.set_parts(data->mtd.size, &pdata->chip); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 111 | if (pdata->chip.partitions) { | 
|  | 112 | data->parts = pdata->chip.partitions; | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 113 | err = add_mtd_partitions(&data->mtd, data->parts, | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 114 | pdata->chip.nr_partitions); | 
|  | 115 | } else | 
|  | 116 | #endif | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 117 | err = add_mtd_device(&data->mtd); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 118 |  | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 119 | if (!err) | 
|  | 120 | return err; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 121 |  | 
|  | 122 | nand_release(&data->mtd); | 
|  | 123 | out: | 
| H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 124 | if (pdata->ctrl.remove) | 
|  | 125 | pdata->ctrl.remove(pdev); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 126 | platform_set_drvdata(pdev, NULL); | 
|  | 127 | iounmap(data->io_base); | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 128 | out_release_io: | 
|  | 129 | release_mem_region(res->start, resource_size(res)); | 
|  | 130 | out_free: | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 131 | kfree(data); | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 132 | return err; | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
|  | 135 | /* | 
|  | 136 | * Remove a NAND device. | 
|  | 137 | */ | 
|  | 138 | static int __devexit plat_nand_remove(struct platform_device *pdev) | 
|  | 139 | { | 
|  | 140 | struct plat_nand_data *data = platform_get_drvdata(pdev); | 
|  | 141 | struct platform_nand_data *pdata = pdev->dev.platform_data; | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 142 | struct resource *res; | 
|  | 143 |  | 
|  | 144 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 145 |  | 
|  | 146 | nand_release(&data->mtd); | 
|  | 147 | #ifdef CONFIG_MTD_PARTITIONS | 
|  | 148 | if (data->parts && data->parts != pdata->chip.partitions) | 
|  | 149 | kfree(data->parts); | 
|  | 150 | #endif | 
| H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 151 | if (pdata->ctrl.remove) | 
|  | 152 | pdata->ctrl.remove(pdev); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 153 | iounmap(data->io_base); | 
| H Hartley Sweeten | 2d098a72 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 154 | release_mem_region(res->start, resource_size(res)); | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 155 | kfree(data); | 
|  | 156 |  | 
|  | 157 | return 0; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | static struct platform_driver plat_nand_driver = { | 
|  | 161 | .probe		= plat_nand_probe, | 
| Thomas Chou | 24b5ce2 | 2009-04-21 12:27:34 +0800 | [diff] [blame] | 162 | .remove		= __devexit_p(plat_nand_remove), | 
| Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 163 | .driver		= { | 
|  | 164 | .name	= "gen_nand", | 
|  | 165 | .owner	= THIS_MODULE, | 
|  | 166 | }, | 
|  | 167 | }; | 
|  | 168 |  | 
|  | 169 | static int __init plat_nand_init(void) | 
|  | 170 | { | 
|  | 171 | return platform_driver_register(&plat_nand_driver); | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | static void __exit plat_nand_exit(void) | 
|  | 175 | { | 
|  | 176 | platform_driver_unregister(&plat_nand_driver); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | module_init(plat_nand_init); | 
|  | 180 | module_exit(plat_nand_exit); | 
|  | 181 |  | 
|  | 182 | MODULE_LICENSE("GPL"); | 
|  | 183 | MODULE_AUTHOR("Vitaly Wool"); | 
|  | 184 | MODULE_DESCRIPTION("Simple generic NAND driver"); | 
| Kay Sievers | 1ff1842 | 2008-04-18 13:44:27 -0700 | [diff] [blame] | 185 | MODULE_ALIAS("platform:gen_nand"); |