4 * Author: Vitaly Wool <vitalywool@gmail.com>
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.
12 #include <linux/err.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>
21 struct plat_nand_data {
22 struct nand_chip chip;
24 void __iomem *io_base;
28 * Probe for the NAND device.
30 static int plat_nand_probe(struct platform_device *pdev)
32 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
33 struct mtd_part_parser_data ppdata;
34 struct plat_nand_data *data;
36 const char **part_types;
40 dev_err(&pdev->dev, "platform_nand_data is missing\n");
44 if (pdata->chip.nr_chips < 1) {
45 dev_err(&pdev->dev, "invalid number of chips specified\n");
49 /* Allocate memory for the device structure (and zero it) */
50 data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
55 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
56 data->io_base = devm_ioremap_resource(&pdev->dev, res);
57 if (IS_ERR(data->io_base))
58 return PTR_ERR(data->io_base);
60 data->chip.priv = &data;
61 data->mtd.priv = &data->chip;
62 data->mtd.owner = THIS_MODULE;
63 data->mtd.name = dev_name(&pdev->dev);
65 data->chip.IO_ADDR_R = data->io_base;
66 data->chip.IO_ADDR_W = data->io_base;
67 data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
68 data->chip.dev_ready = pdata->ctrl.dev_ready;
69 data->chip.select_chip = pdata->ctrl.select_chip;
70 data->chip.write_buf = pdata->ctrl.write_buf;
71 data->chip.read_buf = pdata->ctrl.read_buf;
72 data->chip.read_byte = pdata->ctrl.read_byte;
73 data->chip.chip_delay = pdata->chip.chip_delay;
74 data->chip.options |= pdata->chip.options;
75 data->chip.bbt_options |= pdata->chip.bbt_options;
77 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
78 data->chip.ecc.layout = pdata->chip.ecclayout;
79 data->chip.ecc.mode = NAND_ECC_SOFT;
81 platform_set_drvdata(pdev, data);
83 /* Handle any platform specific setup */
84 if (pdata->ctrl.probe) {
85 err = pdata->ctrl.probe(pdev);
90 /* Scan to find existence of the device */
91 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
96 part_types = pdata->chip.part_probe_types;
98 ppdata.of_node = pdev->dev.of_node;
99 err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
100 pdata->chip.partitions,
101 pdata->chip.nr_partitions);
106 nand_release(&data->mtd);
108 if (pdata->ctrl.remove)
109 pdata->ctrl.remove(pdev);
114 * Remove a NAND device.
116 static int plat_nand_remove(struct platform_device *pdev)
118 struct plat_nand_data *data = platform_get_drvdata(pdev);
119 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
121 nand_release(&data->mtd);
122 if (pdata->ctrl.remove)
123 pdata->ctrl.remove(pdev);
128 static const struct of_device_id plat_nand_match[] = {
129 { .compatible = "gen_nand" },
132 MODULE_DEVICE_TABLE(of, plat_nand_match);
134 static struct platform_driver plat_nand_driver = {
135 .probe = plat_nand_probe,
136 .remove = plat_nand_remove,
139 .of_match_table = plat_nand_match,
143 module_platform_driver(plat_nand_driver);
145 MODULE_LICENSE("GPL");
146 MODULE_AUTHOR("Vitaly Wool");
147 MODULE_DESCRIPTION("Simple generic NAND driver");
148 MODULE_ALIAS("platform:gen_nand");