Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 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 | |
Jingoo Han | ffdac7c | 2014-01-03 11:16:28 +0900 | [diff] [blame] | 12 | #include <linux/err.h> |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 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; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | /* |
| 28 | * Probe for the NAND device. |
| 29 | */ |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 30 | static int plat_nand_probe(struct platform_device *pdev) |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 31 | { |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 32 | struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev); |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 33 | struct mtd_part_parser_data ppdata; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 34 | struct plat_nand_data *data; |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 35 | struct resource *res; |
H Hartley Sweeten | f2e5a24 | 2012-03-28 11:13:05 -0700 | [diff] [blame] | 36 | const char **part_types; |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 37 | int err = 0; |
| 38 | |
John Crispin | da3888c | 2012-07-22 08:59:57 +0200 | [diff] [blame] | 39 | if (!pdata) { |
| 40 | dev_err(&pdev->dev, "platform_nand_data is missing\n"); |
| 41 | return -EINVAL; |
| 42 | } |
| 43 | |
Marek Vasut | 01cd2aba | 2010-08-12 03:53:55 +0100 | [diff] [blame] | 44 | if (pdata->chip.nr_chips < 1) { |
| 45 | dev_err(&pdev->dev, "invalid number of chips specified\n"); |
| 46 | return -EINVAL; |
| 47 | } |
| 48 | |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 49 | /* Allocate memory for the device structure (and zero it) */ |
Jingoo Han | ffdac7c | 2014-01-03 11:16:28 +0900 | [diff] [blame] | 50 | data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data), |
| 51 | GFP_KERNEL); |
Jingoo Han | a01eb20 | 2014-01-03 11:17:29 +0900 | [diff] [blame] | 52 | if (!data) |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 53 | return -ENOMEM; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 54 | |
Wei Yongjun | 840f53c | 2014-01-07 21:38:12 +0800 | [diff] [blame] | 55 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Jingoo Han | ffdac7c | 2014-01-03 11:16:28 +0900 | [diff] [blame] | 56 | data->io_base = devm_ioremap_resource(&pdev->dev, res); |
| 57 | if (IS_ERR(data->io_base)) |
| 58 | return PTR_ERR(data->io_base); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 59 | |
| 60 | data->chip.priv = &data; |
| 61 | data->mtd.priv = &data->chip; |
Frans Klaver | f0aa200 | 2015-06-10 22:39:00 +0200 | [diff] [blame^] | 62 | data->mtd.dev.parent = &pdev->dev; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 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; |
Alexander Clouter | d6fed9e | 2009-05-11 19:28:01 +0100 | [diff] [blame] | 69 | data->chip.write_buf = pdata->ctrl.write_buf; |
| 70 | data->chip.read_buf = pdata->ctrl.read_buf; |
John Crispin | b4f7aa8 | 2012-04-30 19:30:47 +0200 | [diff] [blame] | 71 | data->chip.read_byte = pdata->ctrl.read_byte; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 72 | data->chip.chip_delay = pdata->chip.chip_delay; |
| 73 | data->chip.options |= pdata->chip.options; |
Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 74 | data->chip.bbt_options |= pdata->chip.bbt_options; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 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 | |
H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 82 | /* Handle any platform specific setup */ |
| 83 | if (pdata->ctrl.probe) { |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 84 | err = pdata->ctrl.probe(pdev); |
| 85 | if (err) |
H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 86 | goto out; |
| 87 | } |
| 88 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 89 | /* Scan to find existence of the device */ |
Marek Vasut | 81cbb0b | 2010-07-28 07:36:54 +0200 | [diff] [blame] | 90 | if (nand_scan(&data->mtd, pdata->chip.nr_chips)) { |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 91 | err = -ENXIO; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 92 | goto out; |
| 93 | } |
| 94 | |
Brian Norris | 8bf57b0 | 2015-05-18 16:15:24 -0700 | [diff] [blame] | 95 | part_types = pdata->chip.part_probe_types; |
H Hartley Sweeten | f2e5a24 | 2012-03-28 11:13:05 -0700 | [diff] [blame] | 96 | |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 97 | ppdata.of_node = pdev->dev.of_node; |
| 98 | err = mtd_device_parse_register(&data->mtd, part_types, &ppdata, |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 99 | pdata->chip.partitions, |
| 100 | pdata->chip.nr_partitions); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 101 | |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 102 | if (!err) |
| 103 | return err; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 104 | |
| 105 | nand_release(&data->mtd); |
| 106 | out: |
H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 107 | if (pdata->ctrl.remove) |
| 108 | pdata->ctrl.remove(pdev); |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 109 | return err; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Remove a NAND device. |
| 114 | */ |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 115 | static int plat_nand_remove(struct platform_device *pdev) |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 116 | { |
| 117 | struct plat_nand_data *data = platform_get_drvdata(pdev); |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 118 | struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 119 | |
| 120 | nand_release(&data->mtd); |
H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 121 | if (pdata->ctrl.remove) |
| 122 | pdata->ctrl.remove(pdev); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 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 | |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 133 | static struct platform_driver plat_nand_driver = { |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 134 | .probe = plat_nand_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 135 | .remove = plat_nand_remove, |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 136 | .driver = { |
| 137 | .name = "gen_nand", |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 138 | .of_match_table = plat_nand_match, |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 139 | }, |
| 140 | }; |
| 141 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 142 | module_platform_driver(plat_nand_driver); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 143 | |
| 144 | MODULE_LICENSE("GPL"); |
| 145 | MODULE_AUTHOR("Vitaly Wool"); |
| 146 | MODULE_DESCRIPTION("Simple generic NAND driver"); |
Kay Sievers | 1ff1842 | 2008-04-18 13:44:27 -0700 | [diff] [blame] | 147 | MODULE_ALIAS("platform:gen_nand"); |