blob: 1bcb520404228ba2d8fe9cec1d29d9a6c535eac7 [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};
25
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -070026static const char *part_probe_types[] = { "cmdlinepart", NULL };
27
Vitaly Wool711fdf62007-05-06 19:31:18 +040028/*
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;
John Crispina4f203512012-04-30 19:30:46 +020034 struct mtd_part_parser_data ppdata;
Vitaly Wool711fdf62007-05-06 19:31:18 +040035 struct plat_nand_data *data;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040036 struct resource *res;
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -070037 const char **part_types;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040038 int err = 0;
39
Marek Vasut01cd2aba2010-08-12 03:53:55 +010040 if (pdata->chip.nr_chips < 1) {
41 dev_err(&pdev->dev, "invalid number of chips specified\n");
42 return -EINVAL;
43 }
44
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040045 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
46 if (!res)
47 return -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +040048
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 Sweeten2d098a72009-10-19 19:45:29 -040056 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 Wool711fdf62007-05-06 19:31:18 +040064 if (data->io_base == NULL) {
65 dev_err(&pdev->dev, "ioremap failed\n");
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040066 err = -EIO;
67 goto out_release_io;
Vitaly Wool711fdf62007-05-06 19:31:18 +040068 }
69
70 data->chip.priv = &data;
71 data->mtd.priv = &data->chip;
72 data->mtd.owner = THIS_MODULE;
Kay Sievers475b44c2009-01-06 10:44:38 -080073 data->mtd.name = dev_name(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +040074
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 Clouterd6fed9e2009-05-11 19:28:01 +010080 data->chip.write_buf = pdata->ctrl.write_buf;
81 data->chip.read_buf = pdata->ctrl.read_buf;
John Crispinb4f7aa82012-04-30 19:30:47 +020082 data->chip.read_byte = pdata->ctrl.read_byte;
Vitaly Wool711fdf62007-05-06 19:31:18 +040083 data->chip.chip_delay = pdata->chip.chip_delay;
84 data->chip.options |= pdata->chip.options;
Brian Norrisa40f7342011-05-31 16:31:22 -070085 data->chip.bbt_options |= pdata->chip.bbt_options;
Vitaly Wool711fdf62007-05-06 19:31:18 +040086
87 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
88 data->chip.ecc.layout = pdata->chip.ecclayout;
89 data->chip.ecc.mode = NAND_ECC_SOFT;
90
91 platform_set_drvdata(pdev, data);
92
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070093 /* Handle any platform specific setup */
94 if (pdata->ctrl.probe) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040095 err = pdata->ctrl.probe(pdev);
96 if (err)
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070097 goto out;
98 }
99
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300100 /* Scan to find existence of the device */
Marek Vasut81cbb0b2010-07-28 07:36:54 +0200101 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400102 err = -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400103 goto out;
104 }
105
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -0700106 part_types = pdata->chip.part_probe_types ? : part_probe_types;
107
John Crispina4f203512012-04-30 19:30:46 +0200108 ppdata.of_node = pdev->dev.of_node;
109 err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200110 pdata->chip.partitions,
111 pdata->chip.nr_partitions);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400112
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400113 if (!err)
114 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400115
116 nand_release(&data->mtd);
117out:
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700118 if (pdata->ctrl.remove)
119 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400120 platform_set_drvdata(pdev, NULL);
121 iounmap(data->io_base);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400122out_release_io:
123 release_mem_region(res->start, resource_size(res));
124out_free:
Vitaly Wool711fdf62007-05-06 19:31:18 +0400125 kfree(data);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400126 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400127}
128
129/*
130 * Remove a NAND device.
131 */
132static int __devexit plat_nand_remove(struct platform_device *pdev)
133{
134 struct plat_nand_data *data = platform_get_drvdata(pdev);
135 struct platform_nand_data *pdata = pdev->dev.platform_data;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400136 struct resource *res;
137
138 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400139
140 nand_release(&data->mtd);
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700141 if (pdata->ctrl.remove)
142 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400143 iounmap(data->io_base);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400144 release_mem_region(res->start, resource_size(res));
Vitaly Wool711fdf62007-05-06 19:31:18 +0400145 kfree(data);
146
147 return 0;
148}
149
John Crispina4f203512012-04-30 19:30:46 +0200150static const struct of_device_id plat_nand_match[] = {
151 { .compatible = "gen_nand" },
152 {},
153};
154MODULE_DEVICE_TABLE(of, plat_nand_match);
155
Vitaly Wool711fdf62007-05-06 19:31:18 +0400156static struct platform_driver plat_nand_driver = {
John Crispina4f203512012-04-30 19:30:46 +0200157 .probe = plat_nand_probe,
158 .remove = __devexit_p(plat_nand_remove),
159 .driver = {
160 .name = "gen_nand",
161 .owner = THIS_MODULE,
162 .of_match_table = plat_nand_match,
Vitaly Wool711fdf62007-05-06 19:31:18 +0400163 },
164};
165
Axel Linf99640d2011-11-27 20:45:03 +0800166module_platform_driver(plat_nand_driver);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400167
168MODULE_LICENSE("GPL");
169MODULE_AUTHOR("Vitaly Wool");
170MODULE_DESCRIPTION("Simple generic NAND driver");
Kay Sievers1ff18422008-04-18 13:44:27 -0700171MODULE_ALIAS("platform:gen_nand");