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