2 * Copyright (C) 2013 Freescale Semiconductor, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/device.h>
25 #include <linux/platform_device.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/map.h>
28 #include <linux/mtd/partitions.h>
29 #include <linux/mtd/physmap.h>
30 #include <linux/mtd/concat.h>
33 #include <asm/outercache.h>
35 #define MAX_RESOURCES 4
37 struct imx6x_weimnor_info {
38 struct mtd_info *mtd[MAX_RESOURCES];
39 struct mtd_info *cmtd;
40 struct map_info map[MAX_RESOURCES];
42 struct mtd_partition *parts;
45 static int imx6x_weimnor_remove(struct platform_device *dev)
47 struct imx6x_weimnor_info *info;
50 info = platform_get_drvdata(dev);
55 platform_set_drvdata(dev, NULL);
58 mtd_device_unregister(info->cmtd);
61 if (info->cmtd != info->mtd[0])
62 mtd_concat_destroy(info->cmtd);
65 for (i = 0; i < MAX_RESOURCES; i++) {
66 if (info->mtd[i] != NULL)
67 map_destroy(info->mtd[i]);
68 if (info->map[i].cached)
69 iounmap(info->map[i].cached);
77 static void imx6x_set_vpp(struct map_info *map, int state)
79 struct platform_device *pdev;
80 struct physmap_flash_data *flash;
82 pdev = (struct platform_device *)map->map_priv_1;
83 flash = pdev->dev.platform_data;
86 flash->set_vpp(pdev, state);
89 #define CACHELINESIZE 32
90 static void imx6x_map_inval_cache(struct map_info *map, unsigned long from,
95 unsigned long phys_start;
96 unsigned long phys_end;
98 if (from > map->size) {
99 start = (unsigned long)map->cached + map->size;
100 phys_start = (unsigned long)map->phys + map->size;
102 start = (unsigned long)map->cached + from;
103 phys_start = (unsigned long)map->phys + from;
106 if ((from + len) > map->size) {
107 end = start + map->size;
108 phys_end = phys_start + map->size;
111 phys_end = phys_start + len;
114 start &= ~(CACHELINESIZE - 1);
115 while (start < end) {
116 /* invalidate D cache line */
117 asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start));
118 start += CACHELINESIZE;
120 outer_inv_range(phys_start, phys_end);
123 static const char *rom_probe_types[] = {
130 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", "afs",
133 static int imx6x_weimnor_probe(struct platform_device *dev)
135 struct physmap_flash_data *flash;
136 struct imx6x_weimnor_info *info;
137 const char **probe_type;
140 int devices_found = 0;
142 flash = dev->dev.platform_data;
146 info = devm_kzalloc(&dev->dev, sizeof(struct imx6x_weimnor_info),
154 err = flash->init(dev);
159 platform_set_drvdata(dev, info);
161 for (i = 0; i < dev->num_resources; i++) {
163 "imx6x-flash (physmap) platform "
164 "flash device: %.8llx at %.8llx\n",
165 (unsigned long long)resource_size(&dev->resource[i]),
166 (unsigned long long)dev->resource[i].start);
168 if (!devm_request_mem_region(&dev->dev,
169 dev->resource[i].start,
170 resource_size(&dev->resource[i]),
171 dev_name(&dev->dev))) {
172 dev_err(&dev->dev, "Could not reserve memory region\n");
177 info->map[i].name = dev_name(&dev->dev);
178 info->map[i].phys = dev->resource[i].start;
179 info->map[i].size = resource_size(&dev->resource[i]);
180 info->map[i].bankwidth = flash->width;
181 info->map[i].set_vpp = imx6x_set_vpp;
182 info->map[i].pfow_base = 0;
183 info->map[i].map_priv_1 = (unsigned long)dev;
184 info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
186 if (info->map[i].virt == NULL) {
187 dev_err(&dev->dev, "Failed to ioremap flash region\n");
192 info->map[i].cached =
193 ioremap_cached(info->map[i].phys, info->map[i].size);
194 if (!info->map[i].cached)
195 printk(KERN_WARNING "Failed to ioremap cached %s\n",
198 info->map[i].inval_cache = imx6x_map_inval_cache;
200 simple_map_init(&info->map[i]);
201 probe_type = rom_probe_types;
203 for (; info->mtd[i] == NULL && *probe_type != NULL;
205 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
207 if (info->mtd[i] == NULL) {
208 dev_err(&dev->dev, "map_probe failed\n");
214 info->mtd[i]->owner = THIS_MODULE;
215 info->mtd[i]->dev.parent = &dev->dev;
218 if (devices_found == 1) {
219 info->cmtd = info->mtd[0];
220 } else if (devices_found > 1) {
222 * We detected multiple devices. Concatenate them together.
224 info->cmtd = mtd_concat_create(info->mtd, devices_found,
225 dev_name(&dev->dev));
226 if (info->cmtd == NULL)
232 err = parse_mtd_partitions(info->cmtd, part_probe_types,
235 mtd_device_register(info->cmtd, info->parts, err);
236 info->nr_parts = err;
240 if (flash->nr_parts) {
241 printk(KERN_NOTICE "Using physmap partition information\n");
242 mtd_device_register(info->cmtd, flash->parts,
247 mtd_device_register(info->cmtd, NULL, 0);
252 imx6x_weimnor_remove(dev);
257 static void imx6x_weimnor_shutdown(struct platform_device *dev)
259 struct imx6x_weimnor_info *info = platform_get_drvdata(dev);
262 for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
263 if (info->mtd[i]->suspend && info->mtd[i]->resume)
264 if (info->mtd[i]->suspend(info->mtd[i]) == 0)
265 info->mtd[i]->resume(info->mtd[i]);
268 #define imx6x_weimnor_shutdown NULL
271 static struct platform_driver imx6x_weimnor_driver = {
272 .probe = imx6x_weimnor_probe,
273 .remove = __devexit_p(imx6x_weimnor_remove),
274 .shutdown = imx6x_weimnor_shutdown,
276 .name = "imx6x-weimnor",
277 .owner = THIS_MODULE,
281 static int __init imx6x_init(void)
284 err = platform_driver_register(&imx6x_weimnor_driver);
288 static void __exit imx6x_exit(void)
290 platform_driver_unregister(&imx6x_weimnor_driver);
293 module_init(imx6x_init);
294 module_exit(imx6x_exit);
296 MODULE_LICENSE("GPL");
297 MODULE_AUTHOR("Oliver Brown <oliver.brown@freescale.com>");
298 MODULE_DESCRIPTION("MTD map driver for Freescale iMX");