]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/mtd/nand/plat_nand.c
Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac802...
[mirror_ubuntu-bionic-kernel.git] / drivers / mtd / nand / plat_nand.c
CommitLineData
711fdf62
VW
1/*
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
ffdac7cd 12#include <linux/err.h>
711fdf62
VW
13#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;
711fdf62
VW
25};
26
f2e5a244
HS
27static const char *part_probe_types[] = { "cmdlinepart", NULL };
28
711fdf62
VW
29/*
30 * Probe for the NAND device.
31 */
06f25510 32static int plat_nand_probe(struct platform_device *pdev)
711fdf62 33{
453810b7 34 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
a4f20351 35 struct mtd_part_parser_data ppdata;
711fdf62 36 struct plat_nand_data *data;
2d098a72 37 struct resource *res;
f2e5a244 38 const char **part_types;
2d098a72
HS
39 int err = 0;
40
da3888cb
JC
41 if (!pdata) {
42 dev_err(&pdev->dev, "platform_nand_data is missing\n");
43 return -EINVAL;
44 }
45
01cd2aba
MV
46 if (pdata->chip.nr_chips < 1) {
47 dev_err(&pdev->dev, "invalid number of chips specified\n");
48 return -EINVAL;
49 }
50
711fdf62 51 /* Allocate memory for the device structure (and zero it) */
ffdac7cd
JH
52 data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
53 GFP_KERNEL);
a01eb204 54 if (!data)
711fdf62 55 return -ENOMEM;
711fdf62 56
840f53c3 57 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
ffdac7cd
JH
58 data->io_base = devm_ioremap_resource(&pdev->dev, res);
59 if (IS_ERR(data->io_base))
60 return PTR_ERR(data->io_base);
711fdf62
VW
61
62 data->chip.priv = &data;
63 data->mtd.priv = &data->chip;
64 data->mtd.owner = THIS_MODULE;
475b44c1 65 data->mtd.name = dev_name(&pdev->dev);
711fdf62
VW
66
67 data->chip.IO_ADDR_R = data->io_base;
68 data->chip.IO_ADDR_W = data->io_base;
69 data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
70 data->chip.dev_ready = pdata->ctrl.dev_ready;
71 data->chip.select_chip = pdata->ctrl.select_chip;
d6fed9e9
AC
72 data->chip.write_buf = pdata->ctrl.write_buf;
73 data->chip.read_buf = pdata->ctrl.read_buf;
b4f7aa84 74 data->chip.read_byte = pdata->ctrl.read_byte;
711fdf62
VW
75 data->chip.chip_delay = pdata->chip.chip_delay;
76 data->chip.options |= pdata->chip.options;
a40f7341 77 data->chip.bbt_options |= pdata->chip.bbt_options;
711fdf62
VW
78
79 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
80 data->chip.ecc.layout = pdata->chip.ecclayout;
81 data->chip.ecc.mode = NAND_ECC_SOFT;
82
83 platform_set_drvdata(pdev, data);
84
bf95efd4
HS
85 /* Handle any platform specific setup */
86 if (pdata->ctrl.probe) {
2d098a72
HS
87 err = pdata->ctrl.probe(pdev);
88 if (err)
bf95efd4
HS
89 goto out;
90 }
91
25985edc 92 /* Scan to find existence of the device */
81cbb0b1 93 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
2d098a72 94 err = -ENXIO;
711fdf62
VW
95 goto out;
96 }
97
f2e5a244
HS
98 part_types = pdata->chip.part_probe_types ? : part_probe_types;
99
a4f20351
JC
100 ppdata.of_node = pdev->dev.of_node;
101 err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
42d7fbe2
AB
102 pdata->chip.partitions,
103 pdata->chip.nr_partitions);
711fdf62 104
2d098a72
HS
105 if (!err)
106 return err;
711fdf62
VW
107
108 nand_release(&data->mtd);
109out:
bf95efd4
HS
110 if (pdata->ctrl.remove)
111 pdata->ctrl.remove(pdev);
2d098a72 112 return err;
711fdf62
VW
113}
114
115/*
116 * Remove a NAND device.
117 */
810b7e06 118static int plat_nand_remove(struct platform_device *pdev)
711fdf62
VW
119{
120 struct plat_nand_data *data = platform_get_drvdata(pdev);
453810b7 121 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
711fdf62
VW
122
123 nand_release(&data->mtd);
bf95efd4
HS
124 if (pdata->ctrl.remove)
125 pdata->ctrl.remove(pdev);
711fdf62
VW
126
127 return 0;
128}
129
a4f20351
JC
130static const struct of_device_id plat_nand_match[] = {
131 { .compatible = "gen_nand" },
132 {},
133};
134MODULE_DEVICE_TABLE(of, plat_nand_match);
135
711fdf62 136static struct platform_driver plat_nand_driver = {
a4f20351 137 .probe = plat_nand_probe,
5153b88c 138 .remove = plat_nand_remove,
a4f20351
JC
139 .driver = {
140 .name = "gen_nand",
141 .owner = THIS_MODULE,
142 .of_match_table = plat_nand_match,
711fdf62
VW
143 },
144};
145
f99640de 146module_platform_driver(plat_nand_driver);
711fdf62
VW
147
148MODULE_LICENSE("GPL");
149MODULE_AUTHOR("Vitaly Wool");
150MODULE_DESCRIPTION("Simple generic NAND driver");
1ff18422 151MODULE_ALIAS("platform:gen_nand");