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