blob: dc0f3074ddbff71bc23376fdfd482826394bc7d5 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Vitaly Wool711fdf62007-05-06 19:31:18 +04002/*
3 * Generic NAND driver
4 *
5 * Author: Vitaly Wool <vitalywool@gmail.com>
Vitaly Wool711fdf62007-05-06 19:31:18 +04006 */
7
Jingoo Hanffdac7c2014-01-03 11:16:28 +09008#include <linux/err.h>
Vitaly Wool711fdf62007-05-06 19:31:18 +04009#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>
Boris Brezillonc7921bb2018-09-07 00:38:46 +020014#include <linux/mtd/platnand.h>
Vitaly Wool711fdf62007-05-06 19:31:18 +040015
16struct plat_nand_data {
17 struct nand_chip chip;
Vitaly Wool711fdf62007-05-06 19:31:18 +040018 void __iomem *io_base;
Vitaly Wool711fdf62007-05-06 19:31:18 +040019};
20
21/*
22 * Probe for the NAND device.
23 */
Bill Pemberton06f25512012-11-19 13:23:07 -050024static int plat_nand_probe(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +040025{
Jingoo Han453810b2013-07-30 17:18:33 +090026 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +040027 struct plat_nand_data *data;
Boris BREZILLONa0260d22015-12-10 09:00:19 +010028 struct mtd_info *mtd;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040029 struct resource *res;
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -070030 const char **part_types;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040031 int err = 0;
32
John Crispinda3888c2012-07-22 08:59:57 +020033 if (!pdata) {
34 dev_err(&pdev->dev, "platform_nand_data is missing\n");
35 return -EINVAL;
36 }
37
Marek Vasut01cd2aba2010-08-12 03:53:55 +010038 if (pdata->chip.nr_chips < 1) {
39 dev_err(&pdev->dev, "invalid number of chips specified\n");
40 return -EINVAL;
41 }
42
Vitaly Wool711fdf62007-05-06 19:31:18 +040043 /* Allocate memory for the device structure (and zero it) */
Jingoo Hanffdac7c2014-01-03 11:16:28 +090044 data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
45 GFP_KERNEL);
Jingoo Hana01eb202014-01-03 11:17:29 +090046 if (!data)
Vitaly Wool711fdf62007-05-06 19:31:18 +040047 return -ENOMEM;
Vitaly Wool711fdf62007-05-06 19:31:18 +040048
Wei Yongjun840f53c2014-01-07 21:38:12 +080049 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Hanffdac7c2014-01-03 11:16:28 +090050 data->io_base = devm_ioremap_resource(&pdev->dev, res);
51 if (IS_ERR(data->io_base))
52 return PTR_ERR(data->io_base);
Vitaly Wool711fdf62007-05-06 19:31:18 +040053
Brian Norrisa61ae812015-10-30 20:33:25 -070054 nand_set_flash_node(&data->chip, pdev->dev.of_node);
Boris BREZILLONa0260d22015-12-10 09:00:19 +010055 mtd = nand_to_mtd(&data->chip);
Boris BREZILLONa0260d22015-12-10 09:00:19 +010056 mtd->dev.parent = &pdev->dev;
Vitaly Wool711fdf62007-05-06 19:31:18 +040057
Boris Brezillon82fc5092018-09-07 00:38:34 +020058 data->chip.legacy.IO_ADDR_R = data->io_base;
59 data->chip.legacy.IO_ADDR_W = data->io_base;
Boris Brezillonbf6065c2018-09-07 00:38:36 +020060 data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
Boris Brezillon8395b752018-09-07 00:38:37 +020061 data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
Boris Brezillon7d6c37e2018-11-11 08:55:22 +010062 data->chip.legacy.select_chip = pdata->ctrl.select_chip;
Boris Brezillon716bbba2018-09-07 00:38:35 +020063 data->chip.legacy.write_buf = pdata->ctrl.write_buf;
64 data->chip.legacy.read_buf = pdata->ctrl.read_buf;
Boris Brezillon3cece3a2018-09-07 00:38:41 +020065 data->chip.legacy.chip_delay = pdata->chip.chip_delay;
Vitaly Wool711fdf62007-05-06 19:31:18 +040066 data->chip.options |= pdata->chip.options;
Brian Norrisa40f7342011-05-31 16:31:22 -070067 data->chip.bbt_options |= pdata->chip.bbt_options;
Vitaly Wool711fdf62007-05-06 19:31:18 +040068
Vitaly Wool711fdf62007-05-06 19:31:18 +040069 data->chip.ecc.mode = NAND_ECC_SOFT;
Rafał Miłecki41ccb492016-04-08 12:23:50 +020070 data->chip.ecc.algo = NAND_ECC_HAMMING;
Vitaly Wool711fdf62007-05-06 19:31:18 +040071
72 platform_set_drvdata(pdev, data);
73
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070074 /* Handle any platform specific setup */
75 if (pdata->ctrl.probe) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040076 err = pdata->ctrl.probe(pdev);
77 if (err)
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070078 goto out;
79 }
80
Lucas De Marchi25985ed2011-03-30 22:57:33 -030081 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +020082 err = nand_scan(&data->chip, pdata->chip.nr_chips);
Masahiro Yamadace2eaca2016-11-04 19:42:57 +090083 if (err)
Vitaly Wool711fdf62007-05-06 19:31:18 +040084 goto out;
Vitaly Wool711fdf62007-05-06 19:31:18 +040085
Brian Norris8bf57b02015-05-18 16:15:24 -070086 part_types = pdata->chip.part_probe_types;
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -070087
Boris BREZILLONa0260d22015-12-10 09:00:19 +010088 err = mtd_device_parse_register(mtd, part_types, NULL,
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +020089 pdata->chip.partitions,
90 pdata->chip.nr_partitions);
Vitaly Wool711fdf62007-05-06 19:31:18 +040091
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040092 if (!err)
93 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +040094
Boris Brezillon59ac2762018-09-06 14:05:15 +020095 nand_release(&data->chip);
Vitaly Wool711fdf62007-05-06 19:31:18 +040096out:
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070097 if (pdata->ctrl.remove)
98 pdata->ctrl.remove(pdev);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040099 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400100}
101
102/*
103 * Remove a NAND device.
104 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500105static int plat_nand_remove(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +0400106{
107 struct plat_nand_data *data = platform_get_drvdata(pdev);
Jingoo Han453810b2013-07-30 17:18:33 +0900108 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400109
Boris Brezillon59ac2762018-09-06 14:05:15 +0200110 nand_release(&data->chip);
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700111 if (pdata->ctrl.remove)
112 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400113
114 return 0;
115}
116
John Crispina4f203512012-04-30 19:30:46 +0200117static const struct of_device_id plat_nand_match[] = {
118 { .compatible = "gen_nand" },
119 {},
120};
121MODULE_DEVICE_TABLE(of, plat_nand_match);
122
Vitaly Wool711fdf62007-05-06 19:31:18 +0400123static struct platform_driver plat_nand_driver = {
John Crispina4f203512012-04-30 19:30:46 +0200124 .probe = plat_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500125 .remove = plat_nand_remove,
John Crispina4f203512012-04-30 19:30:46 +0200126 .driver = {
127 .name = "gen_nand",
John Crispina4f203512012-04-30 19:30:46 +0200128 .of_match_table = plat_nand_match,
Vitaly Wool711fdf62007-05-06 19:31:18 +0400129 },
130};
131
Axel Linf99640d2011-11-27 20:45:03 +0800132module_platform_driver(plat_nand_driver);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400133
134MODULE_LICENSE("GPL");
135MODULE_AUTHOR("Vitaly Wool");
136MODULE_DESCRIPTION("Simple generic NAND driver");
Kay Sievers1ff18422008-04-18 13:44:27 -0700137MODULE_ALIAS("platform:gen_nand");