Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / mtd / nand / raw / plat_nand.c
blob7711e1020c21c537c8a8eb5ccd8a6ea201da270d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic NAND driver
5 * Author: Vitaly Wool <vitalywool@gmail.com>
6 */
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/platnand.h>
16 struct plat_nand_data {
17 struct nand_controller controller;
18 struct nand_chip chip;
19 void __iomem *io_base;
22 static int plat_nand_attach_chip(struct nand_chip *chip)
24 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
26 if (chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
27 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
29 return 0;
32 static const struct nand_controller_ops plat_nand_ops = {
33 .attach_chip = plat_nand_attach_chip,
37 * Probe for the NAND device.
39 static int plat_nand_probe(struct platform_device *pdev)
41 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
42 struct plat_nand_data *data;
43 struct mtd_info *mtd;
44 struct resource *res;
45 const char **part_types;
46 int err = 0;
48 if (!pdata) {
49 dev_err(&pdev->dev, "platform_nand_data is missing\n");
50 return -EINVAL;
53 if (pdata->chip.nr_chips < 1) {
54 dev_err(&pdev->dev, "invalid number of chips specified\n");
55 return -EINVAL;
58 /* Allocate memory for the device structure (and zero it) */
59 data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
60 GFP_KERNEL);
61 if (!data)
62 return -ENOMEM;
64 data->controller.ops = &plat_nand_ops;
65 nand_controller_init(&data->controller);
66 data->chip.controller = &data->controller;
68 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
69 data->io_base = devm_ioremap_resource(&pdev->dev, res);
70 if (IS_ERR(data->io_base))
71 return PTR_ERR(data->io_base);
73 nand_set_flash_node(&data->chip, pdev->dev.of_node);
74 mtd = nand_to_mtd(&data->chip);
75 mtd->dev.parent = &pdev->dev;
77 data->chip.legacy.IO_ADDR_R = data->io_base;
78 data->chip.legacy.IO_ADDR_W = data->io_base;
79 data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
80 data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
81 data->chip.legacy.select_chip = pdata->ctrl.select_chip;
82 data->chip.legacy.write_buf = pdata->ctrl.write_buf;
83 data->chip.legacy.read_buf = pdata->ctrl.read_buf;
84 data->chip.legacy.chip_delay = pdata->chip.chip_delay;
85 data->chip.options |= pdata->chip.options;
86 data->chip.bbt_options |= pdata->chip.bbt_options;
88 platform_set_drvdata(pdev, data);
90 /* Handle any platform specific setup */
91 if (pdata->ctrl.probe) {
92 err = pdata->ctrl.probe(pdev);
93 if (err)
94 goto out;
97 /* Scan to find existence of the device */
98 err = nand_scan(&data->chip, pdata->chip.nr_chips);
99 if (err)
100 goto out;
102 part_types = pdata->chip.part_probe_types;
104 err = mtd_device_parse_register(mtd, part_types, NULL,
105 pdata->chip.partitions,
106 pdata->chip.nr_partitions);
108 if (!err)
109 return err;
111 nand_cleanup(&data->chip);
112 out:
113 if (pdata->ctrl.remove)
114 pdata->ctrl.remove(pdev);
115 return err;
119 * Remove a NAND device.
121 static int plat_nand_remove(struct platform_device *pdev)
123 struct plat_nand_data *data = platform_get_drvdata(pdev);
124 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
125 struct nand_chip *chip = &data->chip;
126 int ret;
128 ret = mtd_device_unregister(nand_to_mtd(chip));
129 WARN_ON(ret);
130 nand_cleanup(chip);
131 if (pdata->ctrl.remove)
132 pdata->ctrl.remove(pdev);
134 return 0;
137 static const struct of_device_id plat_nand_match[] = {
138 { .compatible = "gen_nand" },
141 MODULE_DEVICE_TABLE(of, plat_nand_match);
143 static struct platform_driver plat_nand_driver = {
144 .probe = plat_nand_probe,
145 .remove = plat_nand_remove,
146 .driver = {
147 .name = "gen_nand",
148 .of_match_table = plat_nand_match,
152 module_platform_driver(plat_nand_driver);
154 MODULE_LICENSE("GPL");
155 MODULE_AUTHOR("Vitaly Wool");
156 MODULE_DESCRIPTION("Simple generic NAND driver");
157 MODULE_ALIAS("platform:gen_nand");