blob: ccdb16ec81430028eff5d6ba363d5a317d9fba5a [file] [log] [blame]
Vitaly Wool711fdf62007-05-06 19:31:18 +04001/*
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
20struct plat_nand_data {
21 struct nand_chip chip;
22 struct mtd_info mtd;
23 void __iomem *io_base;
Vitaly Wool711fdf62007-05-06 19:31:18 +040024 int nr_parts;
25 struct mtd_partition *parts;
Vitaly Wool711fdf62007-05-06 19:31:18 +040026};
27
28/*
29 * Probe for the NAND device.
30 */
Uwe Kleine-König9d632872009-03-28 00:26:58 +010031static int __devinit plat_nand_probe(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +040032{
33 struct platform_nand_data *pdata = pdev->dev.platform_data;
34 struct plat_nand_data *data;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040035 struct resource *res;
36 int err = 0;
37
Marek Vasut01cd2aba2010-08-12 03:53:55 +010038 if (pdata->chip.nr_chips < 1) {
39 dev_err(&pdev->dev, "invalid number of chips specified\n");
40 return -EINVAL;
41 }
42
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040043 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
44 if (!res)
45 return -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +040046
47 /* Allocate memory for the device structure (and zero it) */
48 data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL);
49 if (!data) {
50 dev_err(&pdev->dev, "failed to allocate device structure.\n");
51 return -ENOMEM;
52 }
53
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040054 if (!request_mem_region(res->start, resource_size(res),
55 dev_name(&pdev->dev))) {
56 dev_err(&pdev->dev, "request_mem_region failed\n");
57 err = -EBUSY;
58 goto out_free;
59 }
60
61 data->io_base = ioremap(res->start, resource_size(res));
Vitaly Wool711fdf62007-05-06 19:31:18 +040062 if (data->io_base == NULL) {
63 dev_err(&pdev->dev, "ioremap failed\n");
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040064 err = -EIO;
65 goto out_release_io;
Vitaly Wool711fdf62007-05-06 19:31:18 +040066 }
67
68 data->chip.priv = &data;
69 data->mtd.priv = &data->chip;
70 data->mtd.owner = THIS_MODULE;
Kay Sievers475b44c2009-01-06 10:44:38 -080071 data->mtd.name = dev_name(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +040072
73 data->chip.IO_ADDR_R = data->io_base;
74 data->chip.IO_ADDR_W = data->io_base;
75 data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
76 data->chip.dev_ready = pdata->ctrl.dev_ready;
77 data->chip.select_chip = pdata->ctrl.select_chip;
Alexander Clouterd6fed9e2009-05-11 19:28:01 +010078 data->chip.write_buf = pdata->ctrl.write_buf;
79 data->chip.read_buf = pdata->ctrl.read_buf;
Vitaly Wool711fdf62007-05-06 19:31:18 +040080 data->chip.chip_delay = pdata->chip.chip_delay;
81 data->chip.options |= pdata->chip.options;
Brian Norrisa40f7342011-05-31 16:31:22 -070082 data->chip.bbt_options |= pdata->chip.bbt_options;
Vitaly Wool711fdf62007-05-06 19:31:18 +040083
84 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
85 data->chip.ecc.layout = pdata->chip.ecclayout;
86 data->chip.ecc.mode = NAND_ECC_SOFT;
87
88 platform_set_drvdata(pdev, data);
89
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070090 /* Handle any platform specific setup */
91 if (pdata->ctrl.probe) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040092 err = pdata->ctrl.probe(pdev);
93 if (err)
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070094 goto out;
95 }
96
Lucas De Marchi25985ed2011-03-30 22:57:33 -030097 /* Scan to find existence of the device */
Marek Vasut81cbb0b2010-07-28 07:36:54 +020098 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040099 err = -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400100 goto out;
101 }
102
Vitaly Wool711fdf62007-05-06 19:31:18 +0400103 if (pdata->chip.part_probe_types) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400104 err = parse_mtd_partitions(&data->mtd,
Vitaly Wool711fdf62007-05-06 19:31:18 +0400105 pdata->chip.part_probe_types,
106 &data->parts, 0);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400107 if (err > 0) {
Jamie Iles3ee904f2011-05-23 10:22:47 +0100108 mtd_device_register(&data->mtd, data->parts, err);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400109 return 0;
110 }
111 }
H Hartley Sweetenf36e20c2009-05-12 13:46:59 -0700112 if (pdata->chip.set_parts)
113 pdata->chip.set_parts(data->mtd.size, &pdata->chip);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400114 if (pdata->chip.partitions) {
115 data->parts = pdata->chip.partitions;
Jamie Iles3ee904f2011-05-23 10:22:47 +0100116 err = mtd_device_register(&data->mtd, data->parts,
Vitaly Wool711fdf62007-05-06 19:31:18 +0400117 pdata->chip.nr_partitions);
118 } else
Jamie Iles3ee904f2011-05-23 10:22:47 +0100119 err = mtd_device_register(&data->mtd, NULL, 0);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400120
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400121 if (!err)
122 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400123
124 nand_release(&data->mtd);
125out:
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700126 if (pdata->ctrl.remove)
127 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400128 platform_set_drvdata(pdev, NULL);
129 iounmap(data->io_base);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400130out_release_io:
131 release_mem_region(res->start, resource_size(res));
132out_free:
Vitaly Wool711fdf62007-05-06 19:31:18 +0400133 kfree(data);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400134 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400135}
136
137/*
138 * Remove a NAND device.
139 */
140static int __devexit plat_nand_remove(struct platform_device *pdev)
141{
142 struct plat_nand_data *data = platform_get_drvdata(pdev);
143 struct platform_nand_data *pdata = pdev->dev.platform_data;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400144 struct resource *res;
145
146 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400147
148 nand_release(&data->mtd);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400149 if (data->parts && data->parts != pdata->chip.partitions)
150 kfree(data->parts);
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700151 if (pdata->ctrl.remove)
152 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400153 iounmap(data->io_base);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400154 release_mem_region(res->start, resource_size(res));
Vitaly Wool711fdf62007-05-06 19:31:18 +0400155 kfree(data);
156
157 return 0;
158}
159
160static struct platform_driver plat_nand_driver = {
161 .probe = plat_nand_probe,
Thomas Chou24b5ce22009-04-21 12:27:34 +0800162 .remove = __devexit_p(plat_nand_remove),
Vitaly Wool711fdf62007-05-06 19:31:18 +0400163 .driver = {
164 .name = "gen_nand",
165 .owner = THIS_MODULE,
166 },
167};
168
169static int __init plat_nand_init(void)
170{
171 return platform_driver_register(&plat_nand_driver);
172}
173
174static void __exit plat_nand_exit(void)
175{
176 platform_driver_unregister(&plat_nand_driver);
177}
178
179module_init(plat_nand_init);
180module_exit(plat_nand_exit);
181
182MODULE_LICENSE("GPL");
183MODULE_AUTHOR("Vitaly Wool");
184MODULE_DESCRIPTION("Simple generic NAND driver");
Kay Sievers1ff18422008-04-18 13:44:27 -0700185MODULE_ALIAS("platform:gen_nand");