blob: 4ebed7273bc00bf65acd936cacbfb3ceaf62cb56 [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
Jingoo Hanffdac7c2014-01-03 11:16:28 +090012#include <linux/err.h>
Vitaly Wool711fdf62007-05-06 19:31:18 +040013#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/nand.h>
19#include <linux/mtd/partitions.h>
20
21struct plat_nand_data {
22 struct nand_chip chip;
23 struct mtd_info mtd;
24 void __iomem *io_base;
Vitaly Wool711fdf62007-05-06 19:31:18 +040025};
26
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -070027static const char *part_probe_types[] = { "cmdlinepart", NULL };
28
Vitaly Wool711fdf62007-05-06 19:31:18 +040029/*
30 * Probe for the NAND device.
31 */
Bill Pemberton06f25512012-11-19 13:23:07 -050032static int plat_nand_probe(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +040033{
Jingoo Han453810b2013-07-30 17:18:33 +090034 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
John Crispina4f203512012-04-30 19:30:46 +020035 struct mtd_part_parser_data ppdata;
Vitaly Wool711fdf62007-05-06 19:31:18 +040036 struct plat_nand_data *data;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040037 struct resource *res;
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -070038 const char **part_types;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040039 int err = 0;
40
John Crispinda3888c2012-07-22 08:59:57 +020041 if (!pdata) {
42 dev_err(&pdev->dev, "platform_nand_data is missing\n");
43 return -EINVAL;
44 }
45
Marek Vasut01cd2aba2010-08-12 03:53:55 +010046 if (pdata->chip.nr_chips < 1) {
47 dev_err(&pdev->dev, "invalid number of chips specified\n");
48 return -EINVAL;
49 }
50
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040051 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
52 if (!res)
53 return -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +040054
55 /* Allocate memory for the device structure (and zero it) */
Jingoo Hanffdac7c2014-01-03 11:16:28 +090056 data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
57 GFP_KERNEL);
Jingoo Hana01eb202014-01-03 11:17:29 +090058 if (!data)
Vitaly Wool711fdf62007-05-06 19:31:18 +040059 return -ENOMEM;
Vitaly Wool711fdf62007-05-06 19:31:18 +040060
Jingoo Hanffdac7c2014-01-03 11:16:28 +090061 data->io_base = devm_ioremap_resource(&pdev->dev, res);
62 if (IS_ERR(data->io_base))
63 return PTR_ERR(data->io_base);
Vitaly Wool711fdf62007-05-06 19:31:18 +040064
65 data->chip.priv = &data;
66 data->mtd.priv = &data->chip;
67 data->mtd.owner = THIS_MODULE;
Kay Sievers475b44c2009-01-06 10:44:38 -080068 data->mtd.name = dev_name(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +040069
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 Clouterd6fed9e2009-05-11 19:28:01 +010075 data->chip.write_buf = pdata->ctrl.write_buf;
76 data->chip.read_buf = pdata->ctrl.read_buf;
John Crispinb4f7aa82012-04-30 19:30:47 +020077 data->chip.read_byte = pdata->ctrl.read_byte;
Vitaly Wool711fdf62007-05-06 19:31:18 +040078 data->chip.chip_delay = pdata->chip.chip_delay;
79 data->chip.options |= pdata->chip.options;
Brian Norrisa40f7342011-05-31 16:31:22 -070080 data->chip.bbt_options |= pdata->chip.bbt_options;
Vitaly Wool711fdf62007-05-06 19:31:18 +040081
82 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
83 data->chip.ecc.layout = pdata->chip.ecclayout;
84 data->chip.ecc.mode = NAND_ECC_SOFT;
85
86 platform_set_drvdata(pdev, data);
87
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070088 /* Handle any platform specific setup */
89 if (pdata->ctrl.probe) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040090 err = pdata->ctrl.probe(pdev);
91 if (err)
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070092 goto out;
93 }
94
Lucas De Marchi25985ed2011-03-30 22:57:33 -030095 /* Scan to find existence of the device */
Marek Vasut81cbb0b2010-07-28 07:36:54 +020096 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040097 err = -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +040098 goto out;
99 }
100
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -0700101 part_types = pdata->chip.part_probe_types ? : part_probe_types;
102
John Crispina4f203512012-04-30 19:30:46 +0200103 ppdata.of_node = pdev->dev.of_node;
104 err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200105 pdata->chip.partitions,
106 pdata->chip.nr_partitions);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400107
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400108 if (!err)
109 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400110
111 nand_release(&data->mtd);
112out:
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700113 if (pdata->ctrl.remove)
114 pdata->ctrl.remove(pdev);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400115 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400116}
117
118/*
119 * Remove a NAND device.
120 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500121static int plat_nand_remove(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +0400122{
123 struct plat_nand_data *data = platform_get_drvdata(pdev);
Jingoo Han453810b2013-07-30 17:18:33 +0900124 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400125
126 nand_release(&data->mtd);
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700127 if (pdata->ctrl.remove)
128 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400129
130 return 0;
131}
132
John Crispina4f203512012-04-30 19:30:46 +0200133static const struct of_device_id plat_nand_match[] = {
134 { .compatible = "gen_nand" },
135 {},
136};
137MODULE_DEVICE_TABLE(of, plat_nand_match);
138
Vitaly Wool711fdf62007-05-06 19:31:18 +0400139static struct platform_driver plat_nand_driver = {
John Crispina4f203512012-04-30 19:30:46 +0200140 .probe = plat_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500141 .remove = plat_nand_remove,
John Crispina4f203512012-04-30 19:30:46 +0200142 .driver = {
143 .name = "gen_nand",
144 .owner = THIS_MODULE,
145 .of_match_table = plat_nand_match,
Vitaly Wool711fdf62007-05-06 19:31:18 +0400146 },
147};
148
Axel Linf99640d2011-11-27 20:45:03 +0800149module_platform_driver(plat_nand_driver);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400150
151MODULE_LICENSE("GPL");
152MODULE_AUTHOR("Vitaly Wool");
153MODULE_DESCRIPTION("Simple generic NAND driver");
Kay Sievers1ff18422008-04-18 13:44:27 -0700154MODULE_ALIAS("platform:gen_nand");