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 | |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/mtd/mtd.h> |
| 17 | #include <linux/mtd/nand.h> |
| 18 | #include <linux/mtd/partitions.h> |
| 19 | |
| 20 | struct plat_nand_data { |
| 21 | struct nand_chip chip; |
| 22 | struct mtd_info mtd; |
| 23 | void __iomem *io_base; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 24 | }; |
| 25 | |
H Hartley Sweeten | f2e5a24 | 2012-03-28 11:13:05 -0700 | [diff] [blame] | 26 | static const char *part_probe_types[] = { "cmdlinepart", NULL }; |
| 27 | |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 28 | /* |
| 29 | * Probe for the NAND device. |
| 30 | */ |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 31 | static int plat_nand_probe(struct platform_device *pdev) |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 32 | { |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 33 | struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev); |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 34 | struct mtd_part_parser_data ppdata; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 35 | struct plat_nand_data *data; |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 36 | struct resource *res; |
H Hartley Sweeten | f2e5a24 | 2012-03-28 11:13:05 -0700 | [diff] [blame] | 37 | const char **part_types; |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 38 | int err = 0; |
| 39 | |
John Crispin | da3888c | 2012-07-22 08:59:57 +0200 | [diff] [blame] | 40 | if (!pdata) { |
| 41 | dev_err(&pdev->dev, "platform_nand_data is missing\n"); |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | |
Marek Vasut | 01cd2aba | 2010-08-12 03:53:55 +0100 | [diff] [blame] | 45 | if (pdata->chip.nr_chips < 1) { |
| 46 | dev_err(&pdev->dev, "invalid number of chips specified\n"); |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 50 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 51 | if (!res) |
| 52 | return -ENXIO; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 53 | |
| 54 | /* Allocate memory for the device structure (and zero it) */ |
| 55 | data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL); |
| 56 | if (!data) { |
| 57 | dev_err(&pdev->dev, "failed to allocate device structure.\n"); |
| 58 | return -ENOMEM; |
| 59 | } |
| 60 | |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 61 | if (!request_mem_region(res->start, resource_size(res), |
| 62 | dev_name(&pdev->dev))) { |
| 63 | dev_err(&pdev->dev, "request_mem_region failed\n"); |
| 64 | err = -EBUSY; |
| 65 | goto out_free; |
| 66 | } |
| 67 | |
| 68 | data->io_base = ioremap(res->start, resource_size(res)); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 69 | if (data->io_base == NULL) { |
| 70 | dev_err(&pdev->dev, "ioremap failed\n"); |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 71 | err = -EIO; |
| 72 | goto out_release_io; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | data->chip.priv = &data; |
| 76 | data->mtd.priv = &data->chip; |
| 77 | data->mtd.owner = THIS_MODULE; |
Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 78 | data->mtd.name = dev_name(&pdev->dev); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 79 | |
| 80 | data->chip.IO_ADDR_R = data->io_base; |
| 81 | data->chip.IO_ADDR_W = data->io_base; |
| 82 | data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl; |
| 83 | data->chip.dev_ready = pdata->ctrl.dev_ready; |
| 84 | data->chip.select_chip = pdata->ctrl.select_chip; |
Alexander Clouter | d6fed9e | 2009-05-11 19:28:01 +0100 | [diff] [blame] | 85 | data->chip.write_buf = pdata->ctrl.write_buf; |
| 86 | data->chip.read_buf = pdata->ctrl.read_buf; |
John Crispin | b4f7aa8 | 2012-04-30 19:30:47 +0200 | [diff] [blame] | 87 | data->chip.read_byte = pdata->ctrl.read_byte; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 88 | data->chip.chip_delay = pdata->chip.chip_delay; |
| 89 | data->chip.options |= pdata->chip.options; |
Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 90 | data->chip.bbt_options |= pdata->chip.bbt_options; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 91 | |
| 92 | data->chip.ecc.hwctl = pdata->ctrl.hwcontrol; |
| 93 | data->chip.ecc.layout = pdata->chip.ecclayout; |
| 94 | data->chip.ecc.mode = NAND_ECC_SOFT; |
| 95 | |
| 96 | platform_set_drvdata(pdev, data); |
| 97 | |
H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 98 | /* Handle any platform specific setup */ |
| 99 | if (pdata->ctrl.probe) { |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 100 | err = pdata->ctrl.probe(pdev); |
| 101 | if (err) |
H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 102 | goto out; |
| 103 | } |
| 104 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 105 | /* Scan to find existence of the device */ |
Marek Vasut | 81cbb0b | 2010-07-28 07:36:54 +0200 | [diff] [blame] | 106 | if (nand_scan(&data->mtd, pdata->chip.nr_chips)) { |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 107 | err = -ENXIO; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 108 | goto out; |
| 109 | } |
| 110 | |
H Hartley Sweeten | f2e5a24 | 2012-03-28 11:13:05 -0700 | [diff] [blame] | 111 | part_types = pdata->chip.part_probe_types ? : part_probe_types; |
| 112 | |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 113 | ppdata.of_node = pdev->dev.of_node; |
| 114 | err = mtd_device_parse_register(&data->mtd, part_types, &ppdata, |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 115 | pdata->chip.partitions, |
| 116 | pdata->chip.nr_partitions); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 117 | |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 118 | if (!err) |
| 119 | return err; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 120 | |
| 121 | nand_release(&data->mtd); |
| 122 | out: |
H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 123 | if (pdata->ctrl.remove) |
| 124 | pdata->ctrl.remove(pdev); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 125 | iounmap(data->io_base); |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 126 | out_release_io: |
| 127 | release_mem_region(res->start, resource_size(res)); |
| 128 | out_free: |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 129 | kfree(data); |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 130 | return err; |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Remove a NAND device. |
| 135 | */ |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 136 | static int plat_nand_remove(struct platform_device *pdev) |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 137 | { |
| 138 | struct plat_nand_data *data = platform_get_drvdata(pdev); |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 139 | struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev); |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 140 | struct resource *res; |
| 141 | |
| 142 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 143 | |
| 144 | nand_release(&data->mtd); |
H Hartley Sweeten | bf95efd | 2009-05-12 13:46:58 -0700 | [diff] [blame] | 145 | if (pdata->ctrl.remove) |
| 146 | pdata->ctrl.remove(pdev); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 147 | iounmap(data->io_base); |
H Hartley Sweeten | 2d098a7 | 2009-10-19 19:45:29 -0400 | [diff] [blame] | 148 | release_mem_region(res->start, resource_size(res)); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 149 | kfree(data); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 154 | static const struct of_device_id plat_nand_match[] = { |
| 155 | { .compatible = "gen_nand" }, |
| 156 | {}, |
| 157 | }; |
| 158 | MODULE_DEVICE_TABLE(of, plat_nand_match); |
| 159 | |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 160 | static struct platform_driver plat_nand_driver = { |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 161 | .probe = plat_nand_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 162 | .remove = plat_nand_remove, |
John Crispin | a4f20351 | 2012-04-30 19:30:46 +0200 | [diff] [blame] | 163 | .driver = { |
| 164 | .name = "gen_nand", |
| 165 | .owner = THIS_MODULE, |
| 166 | .of_match_table = plat_nand_match, |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 167 | }, |
| 168 | }; |
| 169 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 170 | module_platform_driver(plat_nand_driver); |
Vitaly Wool | 711fdf6 | 2007-05-06 19:31:18 +0400 | [diff] [blame] | 171 | |
| 172 | MODULE_LICENSE("GPL"); |
| 173 | MODULE_AUTHOR("Vitaly Wool"); |
| 174 | MODULE_DESCRIPTION("Simple generic NAND driver"); |
Kay Sievers | 1ff1842 | 2008-04-18 13:44:27 -0700 | [diff] [blame] | 175 | MODULE_ALIAS("platform:gen_nand"); |