blob: c004566a9ad2ae383a311ea31587411e0a98aaa7 [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 */
Bill Pemberton06f25512012-11-19 13:23:07 -050031static int 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
John Crispinda3888c2012-07-22 08:59:57 +020040 if (!pdata) {
41 dev_err(&pdev->dev, "platform_nand_data is missing\n");
42 return -EINVAL;
43 }
44
Marek Vasut01cd2aba2010-08-12 03:53:55 +010045 if (pdata->chip.nr_chips < 1) {
46 dev_err(&pdev->dev, "invalid number of chips specified\n");
47 return -EINVAL;
48 }
49
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040050 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
51 if (!res)
52 return -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +040053
54 /* Allocate memory for the device structure (and zero it) */
55 data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL);
56 if (!data) {
57 dev_err(&pdev->dev, "failed to allocate device structure.\n");
58 return -ENOMEM;
59 }
60
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040061 if (!request_mem_region(res->start, resource_size(res),
62 dev_name(&pdev->dev))) {
63 dev_err(&pdev->dev, "request_mem_region failed\n");
64 err = -EBUSY;
65 goto out_free;
66 }
67
68 data->io_base = ioremap(res->start, resource_size(res));
Vitaly Wool711fdf62007-05-06 19:31:18 +040069 if (data->io_base == NULL) {
70 dev_err(&pdev->dev, "ioremap failed\n");
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040071 err = -EIO;
72 goto out_release_io;
Vitaly Wool711fdf62007-05-06 19:31:18 +040073 }
74
75 data->chip.priv = &data;
76 data->mtd.priv = &data->chip;
77 data->mtd.owner = THIS_MODULE;
Kay Sievers475b44c2009-01-06 10:44:38 -080078 data->mtd.name = dev_name(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +040079
80 data->chip.IO_ADDR_R = data->io_base;
81 data->chip.IO_ADDR_W = data->io_base;
82 data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
83 data->chip.dev_ready = pdata->ctrl.dev_ready;
84 data->chip.select_chip = pdata->ctrl.select_chip;
Alexander Clouterd6fed9e2009-05-11 19:28:01 +010085 data->chip.write_buf = pdata->ctrl.write_buf;
86 data->chip.read_buf = pdata->ctrl.read_buf;
John Crispinb4f7aa82012-04-30 19:30:47 +020087 data->chip.read_byte = pdata->ctrl.read_byte;
Vitaly Wool711fdf62007-05-06 19:31:18 +040088 data->chip.chip_delay = pdata->chip.chip_delay;
89 data->chip.options |= pdata->chip.options;
Brian Norrisa40f7342011-05-31 16:31:22 -070090 data->chip.bbt_options |= pdata->chip.bbt_options;
Vitaly Wool711fdf62007-05-06 19:31:18 +040091
92 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
93 data->chip.ecc.layout = pdata->chip.ecclayout;
94 data->chip.ecc.mode = NAND_ECC_SOFT;
95
96 platform_set_drvdata(pdev, data);
97
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070098 /* Handle any platform specific setup */
99 if (pdata->ctrl.probe) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400100 err = pdata->ctrl.probe(pdev);
101 if (err)
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700102 goto out;
103 }
104
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300105 /* Scan to find existence of the device */
Marek Vasut81cbb0b2010-07-28 07:36:54 +0200106 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400107 err = -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400108 goto out;
109 }
110
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -0700111 part_types = pdata->chip.part_probe_types ? : part_probe_types;
112
John Crispina4f203512012-04-30 19:30:46 +0200113 ppdata.of_node = pdev->dev.of_node;
114 err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200115 pdata->chip.partitions,
116 pdata->chip.nr_partitions);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400117
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400118 if (!err)
119 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400120
121 nand_release(&data->mtd);
122out:
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700123 if (pdata->ctrl.remove)
124 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400125 platform_set_drvdata(pdev, NULL);
126 iounmap(data->io_base);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400127out_release_io:
128 release_mem_region(res->start, resource_size(res));
129out_free:
Vitaly Wool711fdf62007-05-06 19:31:18 +0400130 kfree(data);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400131 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400132}
133
134/*
135 * Remove a NAND device.
136 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500137static int plat_nand_remove(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +0400138{
139 struct plat_nand_data *data = platform_get_drvdata(pdev);
140 struct platform_nand_data *pdata = pdev->dev.platform_data;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400141 struct resource *res;
142
143 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400144
145 nand_release(&data->mtd);
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700146 if (pdata->ctrl.remove)
147 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400148 iounmap(data->io_base);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400149 release_mem_region(res->start, resource_size(res));
Vitaly Wool711fdf62007-05-06 19:31:18 +0400150 kfree(data);
151
152 return 0;
153}
154
John Crispina4f203512012-04-30 19:30:46 +0200155static const struct of_device_id plat_nand_match[] = {
156 { .compatible = "gen_nand" },
157 {},
158};
159MODULE_DEVICE_TABLE(of, plat_nand_match);
160
Vitaly Wool711fdf62007-05-06 19:31:18 +0400161static struct platform_driver plat_nand_driver = {
John Crispina4f203512012-04-30 19:30:46 +0200162 .probe = plat_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500163 .remove = plat_nand_remove,
John Crispina4f203512012-04-30 19:30:46 +0200164 .driver = {
165 .name = "gen_nand",
166 .owner = THIS_MODULE,
167 .of_match_table = plat_nand_match,
Vitaly Wool711fdf62007-05-06 19:31:18 +0400168 },
169};
170
Axel Linf99640d2011-11-27 20:45:03 +0800171module_platform_driver(plat_nand_driver);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400172
173MODULE_LICENSE("GPL");
174MODULE_AUTHOR("Vitaly Wool");
175MODULE_DESCRIPTION("Simple generic NAND driver");
Kay Sievers1ff18422008-04-18 13:44:27 -0700176MODULE_ALIAS("platform:gen_nand");