| [18171] | 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; |
|---|
| 24 | }; |
|---|
| 25 | |
|---|
| 26 | /* |
|---|
| 27 | * Probe for the NAND device. |
|---|
| 28 | */ |
|---|
| 29 | static int __devinit plat_nand_probe(struct platform_device *pdev) |
|---|
| 30 | { |
|---|
| 31 | struct platform_nand_data *pdata = pdev->dev.platform_data; |
|---|
| 32 | struct plat_nand_data *data; |
|---|
| 33 | struct resource *res; |
|---|
| 34 | int err = 0; |
|---|
| 35 | |
|---|
| 36 | if (pdata->chip.nr_chips < 1) { |
|---|
| 37 | dev_err(&pdev->dev, "invalid number of chips specified\n"); |
|---|
| 38 | return -EINVAL; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 42 | if (!res) |
|---|
| 43 | return -ENXIO; |
|---|
| 44 | |
|---|
| 45 | /* Allocate memory for the device structure (and zero it) */ |
|---|
| 46 | data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL); |
|---|
| 47 | if (!data) { |
|---|
| 48 | dev_err(&pdev->dev, "failed to allocate device structure.\n"); |
|---|
| 49 | return -ENOMEM; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | if (!request_mem_region(res->start, resource_size(res), |
|---|
| 53 | dev_name(&pdev->dev))) { |
|---|
| 54 | dev_err(&pdev->dev, "request_mem_region failed\n"); |
|---|
| 55 | err = -EBUSY; |
|---|
| 56 | goto out_free; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | data->io_base = ioremap(res->start, resource_size(res)); |
|---|
| 60 | if (data->io_base == NULL) { |
|---|
| 61 | dev_err(&pdev->dev, "ioremap failed\n"); |
|---|
| 62 | err = -EIO; |
|---|
| 63 | goto out_release_io; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | data->chip.priv = &data; |
|---|
| 67 | data->mtd.priv = &data->chip; |
|---|
| 68 | data->mtd.owner = THIS_MODULE; |
|---|
| 69 | data->mtd.name = dev_name(&pdev->dev); |
|---|
| 70 | |
|---|
| 71 | data->chip.IO_ADDR_R = data->io_base; |
|---|
| 72 | data->chip.IO_ADDR_W = data->io_base; |
|---|
| 73 | data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl; |
|---|
| 74 | data->chip.dev_ready = pdata->ctrl.dev_ready; |
|---|
| 75 | data->chip.select_chip = pdata->ctrl.select_chip; |
|---|
| 76 | data->chip.write_buf = pdata->ctrl.write_buf; |
|---|
| 77 | data->chip.read_buf = pdata->ctrl.read_buf; |
|---|
| [18224] | 78 | data->chip.read_byte = pdata->ctrl.read_byte; |
|---|
| [18171] | 79 | data->chip.chip_delay = pdata->chip.chip_delay; |
|---|
| 80 | data->chip.options |= pdata->chip.options; |
|---|
| 81 | data->chip.bbt_options |= pdata->chip.bbt_options; |
|---|
| 82 | |
|---|
| 83 | data->chip.ecc.hwctl = pdata->ctrl.hwcontrol; |
|---|
| 84 | data->chip.ecc.layout = pdata->chip.ecclayout; |
|---|
| 85 | data->chip.ecc.mode = NAND_ECC_SOFT; |
|---|
| 86 | |
|---|
| 87 | platform_set_drvdata(pdev, data); |
|---|
| 88 | |
|---|
| 89 | /* Handle any platform specific setup */ |
|---|
| 90 | if (pdata->ctrl.probe) { |
|---|
| 91 | err = pdata->ctrl.probe(pdev); |
|---|
| 92 | if (err) |
|---|
| 93 | goto out; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /* Scan to find existence of the device */ |
|---|
| 97 | if (nand_scan_ident(&data->mtd, pdata->chip.nr_chips, NULL)) { |
|---|
| 98 | res = -ENXIO; |
|---|
| 99 | goto out; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if (pdata->chip.chip_fixup) { |
|---|
| 103 | res = pdata->chip.chip_fixup(&data->mtd); |
|---|
| 104 | if (res) |
|---|
| 105 | goto out; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | if (nand_scan_tail(&data->mtd)) { |
|---|
| 109 | err = -ENXIO; |
|---|
| 110 | goto out; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | err = mtd_device_parse_register(&data->mtd, |
|---|
| 114 | pdata->chip.part_probe_types, 0, |
|---|
| 115 | pdata->chip.partitions, pdata->chip.nr_partitions); |
|---|
| 116 | |
|---|
| 117 | if (!err) |
|---|
| 118 | return err; |
|---|
| 119 | |
|---|
| 120 | nand_release(&data->mtd); |
|---|
| 121 | out: |
|---|
| 122 | if (pdata->ctrl.remove) |
|---|
| 123 | pdata->ctrl.remove(pdev); |
|---|
| 124 | platform_set_drvdata(pdev, NULL); |
|---|
| 125 | iounmap(data->io_base); |
|---|
| 126 | out_release_io: |
|---|
| 127 | release_mem_region(res->start, resource_size(res)); |
|---|
| 128 | out_free: |
|---|
| 129 | kfree(data); |
|---|
| 130 | return err; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /* |
|---|
| 134 | * Remove a NAND device. |
|---|
| 135 | */ |
|---|
| 136 | static int __devexit plat_nand_remove(struct platform_device *pdev) |
|---|
| 137 | { |
|---|
| 138 | struct plat_nand_data *data = platform_get_drvdata(pdev); |
|---|
| 139 | struct platform_nand_data *pdata = pdev->dev.platform_data; |
|---|
| 140 | struct resource *res; |
|---|
| 141 | |
|---|
| 142 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 143 | |
|---|
| 144 | nand_release(&data->mtd); |
|---|
| 145 | if (pdata->ctrl.remove) |
|---|
| 146 | pdata->ctrl.remove(pdev); |
|---|
| 147 | iounmap(data->io_base); |
|---|
| 148 | release_mem_region(res->start, resource_size(res)); |
|---|
| 149 | kfree(data); |
|---|
| 150 | |
|---|
| 151 | return 0; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | static struct platform_driver plat_nand_driver = { |
|---|
| 155 | .probe = plat_nand_probe, |
|---|
| 156 | .remove = __devexit_p(plat_nand_remove), |
|---|
| 157 | .driver = { |
|---|
| 158 | .name = "gen_nand", |
|---|
| 159 | .owner = THIS_MODULE, |
|---|
| 160 | }, |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | static int __init plat_nand_init(void) |
|---|
| 164 | { |
|---|
| 165 | return platform_driver_register(&plat_nand_driver); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | static void __exit plat_nand_exit(void) |
|---|
| 169 | { |
|---|
| 170 | platform_driver_unregister(&plat_nand_driver); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | module_init(plat_nand_init); |
|---|
| 174 | module_exit(plat_nand_exit); |
|---|
| 175 | |
|---|
| 176 | MODULE_LICENSE("GPL"); |
|---|
| 177 | MODULE_AUTHOR("Vitaly Wool"); |
|---|
| 178 | MODULE_DESCRIPTION("Simple generic NAND driver"); |
|---|
| 179 | MODULE_ALIAS("platform:gen_nand"); |
|---|