David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Interface for NOR flash driver whose high address lines are latched |
| 3 | * |
| 4 | * Copyright © 2000 Nicolas Pitre <nico@cam.org> |
| 5 | * Copyright © 2005-2008 Analog Devices Inc. |
| 6 | * Copyright © 2008 MontaVista Software, Inc. <source@mvista.com> |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public License |
| 9 | * version 2. This program is licensed "as is" without any warranty of any |
| 10 | * kind, whether express or implied. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/mtd/mtd.h> |
| 17 | #include <linux/mtd/map.h> |
| 18 | #include <linux/mtd/partitions.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/mtd/latch-addr-flash.h> |
| 21 | #include <linux/slab.h> |
| 22 | |
| 23 | #define DRIVER_NAME "latch-addr-flash" |
| 24 | |
| 25 | struct latch_addr_flash_info { |
| 26 | struct mtd_info *mtd; |
| 27 | struct map_info map; |
| 28 | struct resource *res; |
| 29 | |
| 30 | void (*set_window)(unsigned long offset, void *data); |
| 31 | void *data; |
| 32 | |
| 33 | /* cache; could be found out of res */ |
| 34 | unsigned long win_mask; |
| 35 | |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 36 | spinlock_t lock; |
| 37 | }; |
| 38 | |
| 39 | static map_word lf_read(struct map_info *map, unsigned long ofs) |
| 40 | { |
| 41 | struct latch_addr_flash_info *info; |
| 42 | map_word datum; |
| 43 | |
| 44 | info = (struct latch_addr_flash_info *)map->map_priv_1; |
| 45 | |
| 46 | spin_lock(&info->lock); |
| 47 | |
| 48 | info->set_window(ofs, info->data); |
| 49 | datum = inline_map_read(map, info->win_mask & ofs); |
| 50 | |
| 51 | spin_unlock(&info->lock); |
| 52 | |
| 53 | return datum; |
| 54 | } |
| 55 | |
| 56 | static void lf_write(struct map_info *map, map_word datum, unsigned long ofs) |
| 57 | { |
| 58 | struct latch_addr_flash_info *info; |
| 59 | |
| 60 | info = (struct latch_addr_flash_info *)map->map_priv_1; |
| 61 | |
| 62 | spin_lock(&info->lock); |
| 63 | |
| 64 | info->set_window(ofs, info->data); |
| 65 | inline_map_write(map, datum, info->win_mask & ofs); |
| 66 | |
| 67 | spin_unlock(&info->lock); |
| 68 | } |
| 69 | |
| 70 | static void lf_copy_from(struct map_info *map, void *to, |
| 71 | unsigned long from, ssize_t len) |
| 72 | { |
| 73 | struct latch_addr_flash_info *info = |
| 74 | (struct latch_addr_flash_info *) map->map_priv_1; |
| 75 | unsigned n; |
| 76 | |
| 77 | while (len > 0) { |
| 78 | n = info->win_mask + 1 - (from & info->win_mask); |
| 79 | if (n > len) |
| 80 | n = len; |
| 81 | |
| 82 | spin_lock(&info->lock); |
| 83 | |
| 84 | info->set_window(from, info->data); |
| 85 | memcpy_fromio(to, map->virt + (from & info->win_mask), n); |
| 86 | |
| 87 | spin_unlock(&info->lock); |
| 88 | |
| 89 | to += n; |
| 90 | from += n; |
| 91 | len -= n; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static char *rom_probe_types[] = { "cfi_probe", NULL }; |
| 96 | |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 97 | static int latch_addr_flash_remove(struct platform_device *dev) |
| 98 | { |
| 99 | struct latch_addr_flash_info *info; |
| 100 | struct latch_addr_flash_data *latch_addr_data; |
| 101 | |
| 102 | info = platform_get_drvdata(dev); |
| 103 | if (info == NULL) |
| 104 | return 0; |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 105 | |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 106 | latch_addr_data = dev_get_platdata(&dev->dev); |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 107 | |
| 108 | if (info->mtd != NULL) { |
Jamie Iles | 902766b | 2011-05-23 10:23:06 +0100 | [diff] [blame] | 109 | mtd_device_unregister(info->mtd); |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 110 | map_destroy(info->mtd); |
| 111 | } |
| 112 | |
| 113 | if (info->map.virt != NULL) |
| 114 | iounmap(info->map.virt); |
| 115 | |
| 116 | if (info->res != NULL) |
| 117 | release_mem_region(info->res->start, resource_size(info->res)); |
| 118 | |
| 119 | kfree(info); |
| 120 | |
| 121 | if (latch_addr_data->done) |
| 122 | latch_addr_data->done(latch_addr_data->data); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 127 | static int latch_addr_flash_probe(struct platform_device *dev) |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 128 | { |
| 129 | struct latch_addr_flash_data *latch_addr_data; |
| 130 | struct latch_addr_flash_info *info; |
| 131 | resource_size_t win_base = dev->resource->start; |
| 132 | resource_size_t win_size = resource_size(dev->resource); |
| 133 | char **probe_type; |
| 134 | int chipsel; |
| 135 | int err; |
| 136 | |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 137 | latch_addr_data = dev_get_platdata(&dev->dev); |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 138 | if (latch_addr_data == NULL) |
| 139 | return -ENODEV; |
| 140 | |
| 141 | pr_notice("latch-addr platform flash device: %#llx byte " |
| 142 | "window at %#.8llx\n", |
| 143 | (unsigned long long)win_size, (unsigned long long)win_base); |
| 144 | |
| 145 | chipsel = dev->id; |
| 146 | |
| 147 | if (latch_addr_data->init) { |
| 148 | err = latch_addr_data->init(latch_addr_data->data, chipsel); |
| 149 | if (err != 0) |
| 150 | return err; |
| 151 | } |
| 152 | |
| 153 | info = kzalloc(sizeof(struct latch_addr_flash_info), GFP_KERNEL); |
| 154 | if (info == NULL) { |
| 155 | err = -ENOMEM; |
| 156 | goto done; |
| 157 | } |
| 158 | |
| 159 | platform_set_drvdata(dev, info); |
| 160 | |
| 161 | info->res = request_mem_region(win_base, win_size, DRIVER_NAME); |
| 162 | if (info->res == NULL) { |
| 163 | dev_err(&dev->dev, "Could not reserve memory region\n"); |
| 164 | err = -EBUSY; |
| 165 | goto free_info; |
| 166 | } |
| 167 | |
| 168 | info->map.name = DRIVER_NAME; |
| 169 | info->map.size = latch_addr_data->size; |
| 170 | info->map.bankwidth = latch_addr_data->width; |
| 171 | |
| 172 | info->map.phys = NO_XIP; |
| 173 | info->map.virt = ioremap(win_base, win_size); |
| 174 | if (!info->map.virt) { |
| 175 | err = -ENOMEM; |
| 176 | goto free_res; |
| 177 | } |
| 178 | |
| 179 | info->map.map_priv_1 = (unsigned long)info; |
| 180 | |
| 181 | info->map.read = lf_read; |
| 182 | info->map.copy_from = lf_copy_from; |
| 183 | info->map.write = lf_write; |
| 184 | info->set_window = latch_addr_data->set_window; |
| 185 | info->data = latch_addr_data->data; |
| 186 | info->win_mask = win_size - 1; |
| 187 | |
| 188 | spin_lock_init(&info->lock); |
| 189 | |
| 190 | for (probe_type = rom_probe_types; !info->mtd && *probe_type; |
| 191 | probe_type++) |
| 192 | info->mtd = do_map_probe(*probe_type, &info->map); |
| 193 | |
| 194 | if (info->mtd == NULL) { |
| 195 | dev_err(&dev->dev, "map_probe failed\n"); |
| 196 | err = -ENODEV; |
| 197 | goto iounmap; |
| 198 | } |
| 199 | info->mtd->owner = THIS_MODULE; |
| 200 | |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 201 | mtd_device_parse_register(info->mtd, NULL, NULL, |
| 202 | latch_addr_data->parts, |
| 203 | latch_addr_data->nr_parts); |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 204 | return 0; |
| 205 | |
| 206 | iounmap: |
| 207 | iounmap(info->map.virt); |
| 208 | free_res: |
| 209 | release_mem_region(info->res->start, resource_size(info->res)); |
| 210 | free_info: |
| 211 | kfree(info); |
| 212 | done: |
| 213 | if (latch_addr_data->done) |
| 214 | latch_addr_data->done(latch_addr_data->data); |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | static struct platform_driver latch_addr_flash_driver = { |
| 219 | .probe = latch_addr_flash_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 220 | .remove = latch_addr_flash_remove, |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 221 | .driver = { |
| 222 | .name = DRIVER_NAME, |
| 223 | }, |
| 224 | }; |
| 225 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 226 | module_platform_driver(latch_addr_flash_driver); |
David Griego | dcfb81d | 2010-11-30 15:32:05 +0530 | [diff] [blame] | 227 | |
| 228 | MODULE_AUTHOR("David Griego <dgriego@mvista.com>"); |
| 229 | MODULE_DESCRIPTION("MTD map driver for flashes addressed physically with upper " |
| 230 | "address lines being set board specifically"); |
| 231 | MODULE_LICENSE("GPL v2"); |