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