Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 2 | /* drivers/mtd/maps/plat-ram.c |
| 3 | * |
| 4 | * (c) 2004-2005 Simtec Electronics |
| 5 | * http://www.simtec.co.uk/products/SWLINUX/ |
| 6 | * Ben Dooks <ben@simtec.co.uk> |
| 7 | * |
Stefan Weil | 947af29 | 2010-01-07 00:03:52 +0100 | [diff] [blame] | 8 | * Generic platform device based RAM map |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/device.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 17 | #include <linux/slab.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 19 | |
| 20 | #include <linux/mtd/mtd.h> |
| 21 | #include <linux/mtd/map.h> |
| 22 | #include <linux/mtd/partitions.h> |
| 23 | #include <linux/mtd/plat-ram.h> |
| 24 | |
| 25 | #include <asm/io.h> |
| 26 | |
| 27 | /* private structure for each mtd platform ram device created */ |
| 28 | |
| 29 | struct platram_info { |
| 30 | struct device *dev; |
| 31 | struct mtd_info *mtd; |
| 32 | struct map_info map; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 33 | struct platdata_mtd_ram *pdata; |
| 34 | }; |
| 35 | |
| 36 | /* to_platram_info() |
| 37 | * |
| 38 | * device private data to struct platram_info conversion |
| 39 | */ |
| 40 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 41 | static inline struct platram_info *to_platram_info(struct platform_device *dev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 42 | { |
Jingoo Han | 14ac078 | 2013-09-09 14:42:52 +0900 | [diff] [blame] | 43 | return platform_get_drvdata(dev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* platram_setrw |
| 47 | * |
| 48 | * call the platform device's set rw/ro control |
| 49 | * |
| 50 | * to = 0 => read-only |
| 51 | * = 1 => read-write |
| 52 | */ |
| 53 | |
| 54 | static inline void platram_setrw(struct platram_info *info, int to) |
| 55 | { |
| 56 | if (info->pdata == NULL) |
| 57 | return; |
| 58 | |
| 59 | if (info->pdata->set_rw != NULL) |
| 60 | (info->pdata->set_rw)(info->dev, to); |
| 61 | } |
| 62 | |
| 63 | /* platram_remove |
| 64 | * |
| 65 | * called to remove the device from the driver's control |
| 66 | */ |
| 67 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 68 | static int platram_remove(struct platform_device *pdev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 69 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 70 | struct platram_info *info = to_platram_info(pdev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 71 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 72 | dev_dbg(&pdev->dev, "removing device\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 73 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 74 | if (info == NULL) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | |
| 77 | if (info->mtd) { |
Jamie Iles | 095dd50 | 2011-05-23 10:23:07 +0100 | [diff] [blame] | 78 | mtd_device_unregister(info->mtd); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 79 | map_destroy(info->mtd); |
| 80 | } |
| 81 | |
| 82 | /* ensure ram is left read-only */ |
| 83 | |
| 84 | platram_setrw(info, PLATRAM_RO); |
| 85 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 86 | kfree(info); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* platram_probe |
| 92 | * |
| 93 | * called from device drive system when a device matching our |
| 94 | * driver is found. |
| 95 | */ |
| 96 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 97 | static int platram_probe(struct platform_device *pdev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 98 | { |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 99 | struct platdata_mtd_ram *pdata; |
| 100 | struct platram_info *info; |
| 101 | struct resource *res; |
| 102 | int err = 0; |
| 103 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 104 | dev_dbg(&pdev->dev, "probe entered\n"); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 105 | |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 106 | if (dev_get_platdata(&pdev->dev) == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 107 | dev_err(&pdev->dev, "no platform data supplied\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 108 | err = -ENOENT; |
| 109 | goto exit_error; |
| 110 | } |
| 111 | |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 112 | pdata = dev_get_platdata(&pdev->dev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 113 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 114 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 115 | if (info == NULL) { |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 116 | err = -ENOMEM; |
| 117 | goto exit_error; |
| 118 | } |
| 119 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 120 | platform_set_drvdata(pdev, info); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 121 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 122 | info->dev = &pdev->dev; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 123 | info->pdata = pdata; |
| 124 | |
| 125 | /* get the resource for the memory mapping */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 126 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Anton Vasilyev | 2e442ae | 2017-08-22 16:24:11 +0300 | [diff] [blame] | 127 | info->map.virt = devm_ioremap_resource(&pdev->dev, res); |
| 128 | if (IS_ERR(info->map.virt)) { |
| 129 | err = PTR_ERR(info->map.virt); |
| 130 | dev_err(&pdev->dev, "failed to ioremap() region\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 131 | goto exit_free; |
| 132 | } |
| 133 | |
Randy Dunlap | 06d63cc | 2007-04-25 22:41:34 -0700 | [diff] [blame] | 134 | dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res, |
| 135 | (unsigned long long)res->start); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 136 | |
| 137 | /* setup map parameters */ |
| 138 | |
| 139 | info->map.phys = res->start; |
Wolfram Sang | 76d6a47 | 2009-07-17 17:47:37 +0200 | [diff] [blame] | 140 | info->map.size = resource_size(res); |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 141 | info->map.name = pdata->mapname != NULL ? |
| 142 | (char *)pdata->mapname : (char *)pdev->name; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 143 | info->map.bankwidth = pdata->bankwidth; |
| 144 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 145 | dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 146 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 147 | simple_map_init(&info->map); |
| 148 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 149 | dev_dbg(&pdev->dev, "initialised map, probing for mtd\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 150 | |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 151 | /* probe for the right mtd map driver |
| 152 | * supplied by the platform_data struct */ |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 153 | |
Harvey Harrison | a01e035 | 2008-04-28 16:50:04 -0700 | [diff] [blame] | 154 | if (pdata->map_probes) { |
Artem Bityutskiy | d50dcb1 | 2013-03-12 10:28:31 +0200 | [diff] [blame] | 155 | const char * const *map_probes = pdata->map_probes; |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 156 | |
| 157 | for ( ; !info->mtd && *map_probes; map_probes++) |
| 158 | info->mtd = do_map_probe(*map_probes , &info->map); |
| 159 | } |
| 160 | /* fallback to map_ram */ |
| 161 | else |
| 162 | info->mtd = do_map_probe("map_ram", &info->map); |
| 163 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 164 | if (info->mtd == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 165 | dev_err(&pdev->dev, "failed to probe for map_ram\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 166 | err = -ENOMEM; |
| 167 | goto exit_free; |
| 168 | } |
| 169 | |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 170 | info->mtd->dev.parent = &pdev->dev; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 171 | |
| 172 | platram_setrw(info, PLATRAM_RW); |
| 173 | |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 174 | /* check to see if there are any available partitions, or whether |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 175 | * to add this device whole */ |
| 176 | |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 177 | err = mtd_device_parse_register(info->mtd, pdata->probes, NULL, |
| 178 | pdata->partitions, |
| 179 | pdata->nr_partitions); |
Baskov Evgeiny | 8c293f5 | 2020-11-13 19:05:37 +0300 | [diff] [blame] | 180 | if (err) { |
| 181 | dev_err(&pdev->dev, "failed to register mtd device\n"); |
| 182 | goto exit_free; |
| 183 | } |
| 184 | |
| 185 | dev_info(&pdev->dev, "registered mtd device\n"); |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 186 | |
Ilya Yanok | c329879 | 2011-12-13 00:37:56 +0100 | [diff] [blame] | 187 | if (pdata->nr_partitions) { |
| 188 | /* add the whole device. */ |
| 189 | err = mtd_device_register(info->mtd, NULL, 0); |
| 190 | if (err) { |
| 191 | dev_err(&pdev->dev, |
| 192 | "failed to register the entire device\n"); |
Baskov Evgeiny | 8c293f5 | 2020-11-13 19:05:37 +0300 | [diff] [blame] | 193 | goto exit_free; |
Ilya Yanok | c329879 | 2011-12-13 00:37:56 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
Jamie Iles | 095dd50 | 2011-05-23 10:23:07 +0100 | [diff] [blame] | 196 | |
Baskov Evgeiny | 8c293f5 | 2020-11-13 19:05:37 +0300 | [diff] [blame] | 197 | return 0; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 198 | |
| 199 | exit_free: |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 200 | platram_remove(pdev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 201 | exit_error: |
| 202 | return err; |
| 203 | } |
| 204 | |
| 205 | /* device driver info */ |
| 206 | |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 207 | /* work with hotplug and coldplug */ |
| 208 | MODULE_ALIAS("platform:mtd-ram"); |
| 209 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 210 | static struct platform_driver platram_driver = { |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 211 | .probe = platram_probe, |
| 212 | .remove = platram_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 213 | .driver = { |
| 214 | .name = "mtd-ram", |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 215 | }, |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
Sachin Kamat | 98a7c74 | 2013-10-11 10:11:23 +0530 | [diff] [blame] | 218 | module_platform_driver(platram_driver); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 219 | |
| 220 | MODULE_LICENSE("GPL"); |
| 221 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 222 | MODULE_DESCRIPTION("MTD platform RAM map driver"); |