1 // SPDX-License-Identifier: GPL-2.0-only
5 * Author: Vitaly Wool <vitalywool@gmail.com>
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 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
25 chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
26 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
31 static const struct nand_controller_ops plat_nand_ops = {
32 .attach_chip = plat_nand_attach_chip,
36 * Probe for the NAND device.
38 static int plat_nand_probe(struct platform_device *pdev)
40 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
41 struct plat_nand_data *data;
43 const char **part_types;
47 dev_err(&pdev->dev, "platform_nand_data is missing\n");
51 if (pdata->chip.nr_chips < 1) {
52 dev_err(&pdev->dev, "invalid number of chips specified\n");
56 /* Allocate memory for the device structure (and zero it) */
57 data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
62 data->controller.ops = &plat_nand_ops;
63 nand_controller_init(&data->controller);
64 data->chip.controller = &data->controller;
66 data->io_base = devm_platform_ioremap_resource(pdev, 0);
67 if (IS_ERR(data->io_base))
68 return PTR_ERR(data->io_base);
70 nand_set_flash_node(&data->chip, pdev->dev.of_node);
71 mtd = nand_to_mtd(&data->chip);
72 mtd->dev.parent = &pdev->dev;
74 data->chip.legacy.IO_ADDR_R = data->io_base;
75 data->chip.legacy.IO_ADDR_W = data->io_base;
76 data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
77 data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
78 data->chip.legacy.select_chip = pdata->ctrl.select_chip;
79 data->chip.legacy.write_buf = pdata->ctrl.write_buf;
80 data->chip.legacy.read_buf = pdata->ctrl.read_buf;
81 data->chip.legacy.chip_delay = pdata->chip.chip_delay;
82 data->chip.options |= pdata->chip.options;
83 data->chip.bbt_options |= pdata->chip.bbt_options;
85 platform_set_drvdata(pdev, data);
87 /* Handle any platform specific setup */
88 if (pdata->ctrl.probe) {
89 err = pdata->ctrl.probe(pdev);
95 * This driver assumes that the default ECC engine should be TYPE_SOFT.
96 * Set ->engine_type before registering the NAND devices in order to
97 * provide a driver specific default value.
99 data->chip.ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
101 /* Scan to find existence of the device */
102 err = nand_scan(&data->chip, pdata->chip.nr_chips);
106 part_types = pdata->chip.part_probe_types;
108 err = mtd_device_parse_register(mtd, part_types, NULL,
109 pdata->chip.partitions,
110 pdata->chip.nr_partitions);
115 nand_cleanup(&data->chip);
117 if (pdata->ctrl.remove)
118 pdata->ctrl.remove(pdev);
123 * Remove a NAND device.
125 static int plat_nand_remove(struct platform_device *pdev)
127 struct plat_nand_data *data = platform_get_drvdata(pdev);
128 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
129 struct nand_chip *chip = &data->chip;
132 ret = mtd_device_unregister(nand_to_mtd(chip));
135 if (pdata->ctrl.remove)
136 pdata->ctrl.remove(pdev);
141 static const struct of_device_id plat_nand_match[] = {
142 { .compatible = "gen_nand" },
145 MODULE_DEVICE_TABLE(of, plat_nand_match);
147 static struct platform_driver plat_nand_driver = {
148 .probe = plat_nand_probe,
149 .remove = plat_nand_remove,
152 .of_match_table = plat_nand_match,
156 module_platform_driver(plat_nand_driver);
158 MODULE_LICENSE("GPL");
159 MODULE_AUTHOR("Vitaly Wool");
160 MODULE_DESCRIPTION("Simple generic NAND driver");
161 MODULE_ALIAS("platform:gen_nand");