blob: 415a53a0deeb306bb5baeaf3f5ad6638fd41d103 [file] [log] [blame]
Vitaly Wool711fdf62007-05-06 19:31:18 +04001/*
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
Jingoo Hanffdac7c2014-01-03 11:16:28 +090012#include <linux/err.h>
Vitaly Wool711fdf62007-05-06 19:31:18 +040013#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;
Vitaly Wool711fdf62007-05-06 19:31:18 +040023 void __iomem *io_base;
Vitaly Wool711fdf62007-05-06 19:31:18 +040024};
25
26/*
27 * Probe for the NAND device.
28 */
Bill Pemberton06f25512012-11-19 13:23:07 -050029static int plat_nand_probe(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +040030{
Jingoo Han453810b2013-07-30 17:18:33 +090031 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +040032 struct plat_nand_data *data;
Boris BREZILLONa0260d22015-12-10 09:00:19 +010033 struct mtd_info *mtd;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040034 struct resource *res;
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -070035 const char **part_types;
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040036 int err = 0;
37
John Crispinda3888c2012-07-22 08:59:57 +020038 if (!pdata) {
39 dev_err(&pdev->dev, "platform_nand_data is missing\n");
40 return -EINVAL;
41 }
42
Marek Vasut01cd2aba2010-08-12 03:53:55 +010043 if (pdata->chip.nr_chips < 1) {
44 dev_err(&pdev->dev, "invalid number of chips specified\n");
45 return -EINVAL;
46 }
47
Vitaly Wool711fdf62007-05-06 19:31:18 +040048 /* Allocate memory for the device structure (and zero it) */
Jingoo Hanffdac7c2014-01-03 11:16:28 +090049 data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
50 GFP_KERNEL);
Jingoo Hana01eb202014-01-03 11:17:29 +090051 if (!data)
Vitaly Wool711fdf62007-05-06 19:31:18 +040052 return -ENOMEM;
Vitaly Wool711fdf62007-05-06 19:31:18 +040053
Wei Yongjun840f53c2014-01-07 21:38:12 +080054 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Hanffdac7c2014-01-03 11:16:28 +090055 data->io_base = devm_ioremap_resource(&pdev->dev, res);
56 if (IS_ERR(data->io_base))
57 return PTR_ERR(data->io_base);
Vitaly Wool711fdf62007-05-06 19:31:18 +040058
Brian Norrisa61ae812015-10-30 20:33:25 -070059 nand_set_flash_node(&data->chip, pdev->dev.of_node);
Boris BREZILLONa0260d22015-12-10 09:00:19 +010060 mtd = nand_to_mtd(&data->chip);
Boris BREZILLONa0260d22015-12-10 09:00:19 +010061 mtd->dev.parent = &pdev->dev;
Vitaly Wool711fdf62007-05-06 19:31:18 +040062
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;
Alexander Clouterd6fed9e2009-05-11 19:28:01 +010068 data->chip.write_buf = pdata->ctrl.write_buf;
69 data->chip.read_buf = pdata->ctrl.read_buf;
John Crispinb4f7aa82012-04-30 19:30:47 +020070 data->chip.read_byte = pdata->ctrl.read_byte;
Vitaly Wool711fdf62007-05-06 19:31:18 +040071 data->chip.chip_delay = pdata->chip.chip_delay;
72 data->chip.options |= pdata->chip.options;
Brian Norrisa40f7342011-05-31 16:31:22 -070073 data->chip.bbt_options |= pdata->chip.bbt_options;
Vitaly Wool711fdf62007-05-06 19:31:18 +040074
75 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
Vitaly Wool711fdf62007-05-06 19:31:18 +040076 data->chip.ecc.mode = NAND_ECC_SOFT;
Rafał Miłecki41ccb492016-04-08 12:23:50 +020077 data->chip.ecc.algo = NAND_ECC_HAMMING;
Vitaly Wool711fdf62007-05-06 19:31:18 +040078
79 platform_set_drvdata(pdev, data);
80
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070081 /* Handle any platform specific setup */
82 if (pdata->ctrl.probe) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040083 err = pdata->ctrl.probe(pdev);
84 if (err)
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070085 goto out;
86 }
87
Lucas De Marchi25985ed2011-03-30 22:57:33 -030088 /* Scan to find existence of the device */
Boris BREZILLONa0260d22015-12-10 09:00:19 +010089 if (nand_scan(mtd, pdata->chip.nr_chips)) {
H Hartley Sweeten2d098a72009-10-19 19:45:29 -040090 err = -ENXIO;
Vitaly Wool711fdf62007-05-06 19:31:18 +040091 goto out;
92 }
93
Brian Norris8bf57b02015-05-18 16:15:24 -070094 part_types = pdata->chip.part_probe_types;
H Hartley Sweetenf2e5a242012-03-28 11:13:05 -070095
Boris BREZILLONa0260d22015-12-10 09:00:19 +010096 err = mtd_device_parse_register(mtd, part_types, NULL,
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +020097 pdata->chip.partitions,
98 pdata->chip.nr_partitions);
Vitaly Wool711fdf62007-05-06 19:31:18 +040099
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400100 if (!err)
101 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400102
Boris BREZILLONa0260d22015-12-10 09:00:19 +0100103 nand_release(mtd);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400104out:
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700105 if (pdata->ctrl.remove)
106 pdata->ctrl.remove(pdev);
H Hartley Sweeten2d098a72009-10-19 19:45:29 -0400107 return err;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400108}
109
110/*
111 * Remove a NAND device.
112 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500113static int plat_nand_remove(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +0400114{
115 struct plat_nand_data *data = platform_get_drvdata(pdev);
Jingoo Han453810b2013-07-30 17:18:33 +0900116 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400117
Boris BREZILLONa0260d22015-12-10 09:00:19 +0100118 nand_release(nand_to_mtd(&data->chip));
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700119 if (pdata->ctrl.remove)
120 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400121
122 return 0;
123}
124
John Crispina4f203512012-04-30 19:30:46 +0200125static const struct of_device_id plat_nand_match[] = {
126 { .compatible = "gen_nand" },
127 {},
128};
129MODULE_DEVICE_TABLE(of, plat_nand_match);
130
Vitaly Wool711fdf62007-05-06 19:31:18 +0400131static struct platform_driver plat_nand_driver = {
John Crispina4f203512012-04-30 19:30:46 +0200132 .probe = plat_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500133 .remove = plat_nand_remove,
John Crispina4f203512012-04-30 19:30:46 +0200134 .driver = {
135 .name = "gen_nand",
John Crispina4f203512012-04-30 19:30:46 +0200136 .of_match_table = plat_nand_match,
Vitaly Wool711fdf62007-05-06 19:31:18 +0400137 },
138};
139
Axel Linf99640d2011-11-27 20:45:03 +0800140module_platform_driver(plat_nand_driver);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400141
142MODULE_LICENSE("GPL");
143MODULE_AUTHOR("Vitaly Wool");
144MODULE_DESCRIPTION("Simple generic NAND driver");
Kay Sievers1ff18422008-04-18 13:44:27 -0700145MODULE_ALIAS("platform:gen_nand");