1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic NAND driver
4 *
5 * Author: Vitaly Wool <vitalywool@gmail.com>
6 */
7
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>
15
16 struct plat_nand_data {
17 struct nand_controller controller;
18 struct nand_chip chip;
19 void __iomem *io_base;
20 };
21
plat_nand_attach_chip(struct nand_chip * chip)22 static int plat_nand_attach_chip(struct nand_chip *chip)
23 {
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;
27
28 return 0;
29 }
30
31 static const struct nand_controller_ops plat_nand_ops = {
32 .attach_chip = plat_nand_attach_chip,
33 };
34
35 /*
36 * Probe for the NAND device.
37 */
plat_nand_probe(struct platform_device * pdev)38 static int plat_nand_probe(struct platform_device *pdev)
39 {
40 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
41 struct plat_nand_data *data;
42 struct mtd_info *mtd;
43 struct resource *res;
44 const char **part_types;
45 int err = 0;
46
47 if (!pdata) {
48 dev_err(&pdev->dev, "platform_nand_data is missing\n");
49 return -EINVAL;
50 }
51
52 if (pdata->chip.nr_chips < 1) {
53 dev_err(&pdev->dev, "invalid number of chips specified\n");
54 return -EINVAL;
55 }
56
57 /* Allocate memory for the device structure (and zero it) */
58 data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
59 GFP_KERNEL);
60 if (!data)
61 return -ENOMEM;
62
63 data->controller.ops = &plat_nand_ops;
64 nand_controller_init(&data->controller);
65 data->chip.controller = &data->controller;
66
67 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
68 data->io_base = devm_ioremap_resource(&pdev->dev, res);
69 if (IS_ERR(data->io_base))
70 return PTR_ERR(data->io_base);
71
72 nand_set_flash_node(&data->chip, pdev->dev.of_node);
73 mtd = nand_to_mtd(&data->chip);
74 mtd->dev.parent = &pdev->dev;
75
76 data->chip.legacy.IO_ADDR_R = data->io_base;
77 data->chip.legacy.IO_ADDR_W = data->io_base;
78 data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
79 data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
80 data->chip.legacy.select_chip = pdata->ctrl.select_chip;
81 data->chip.legacy.write_buf = pdata->ctrl.write_buf;
82 data->chip.legacy.read_buf = pdata->ctrl.read_buf;
83 data->chip.legacy.chip_delay = pdata->chip.chip_delay;
84 data->chip.options |= pdata->chip.options;
85 data->chip.bbt_options |= pdata->chip.bbt_options;
86
87 platform_set_drvdata(pdev, data);
88
89 /* Handle any platform specific setup */
90 if (pdata->ctrl.probe) {
91 err = pdata->ctrl.probe(pdev);
92 if (err)
93 goto out;
94 }
95
96 /*
97 * This driver assumes that the default ECC engine should be TYPE_SOFT.
98 * Set ->engine_type before registering the NAND devices in order to
99 * provide a driver specific default value.
100 */
101 data->chip.ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
102
103 /* Scan to find existence of the device */
104 err = nand_scan(&data->chip, pdata->chip.nr_chips);
105 if (err)
106 goto out;
107
108 part_types = pdata->chip.part_probe_types;
109
110 err = mtd_device_parse_register(mtd, part_types, NULL,
111 pdata->chip.partitions,
112 pdata->chip.nr_partitions);
113
114 if (!err)
115 return err;
116
117 nand_cleanup(&data->chip);
118 out:
119 if (pdata->ctrl.remove)
120 pdata->ctrl.remove(pdev);
121 return err;
122 }
123
124 /*
125 * Remove a NAND device.
126 */
plat_nand_remove(struct platform_device * pdev)127 static int plat_nand_remove(struct platform_device *pdev)
128 {
129 struct plat_nand_data *data = platform_get_drvdata(pdev);
130 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
131 struct nand_chip *chip = &data->chip;
132 int ret;
133
134 ret = mtd_device_unregister(nand_to_mtd(chip));
135 WARN_ON(ret);
136 nand_cleanup(chip);
137 if (pdata->ctrl.remove)
138 pdata->ctrl.remove(pdev);
139
140 return 0;
141 }
142
143 static const struct of_device_id plat_nand_match[] = {
144 { .compatible = "gen_nand" },
145 {},
146 };
147 MODULE_DEVICE_TABLE(of, plat_nand_match);
148
149 static struct platform_driver plat_nand_driver = {
150 .probe = plat_nand_probe,
151 .remove = plat_nand_remove,
152 .driver = {
153 .name = "gen_nand",
154 .of_match_table = plat_nand_match,
155 },
156 };
157
158 module_platform_driver(plat_nand_driver);
159
160 MODULE_LICENSE("GPL");
161 MODULE_AUTHOR("Vitaly Wool");
162 MODULE_DESCRIPTION("Simple generic NAND driver");
163 MODULE_ALIAS("platform:gen_nand");
164