1 // SPDX-License-Identifier: GPL-2.0+
3 * Normal mappings of chips in physical memory
5 * Copyright (C) 2003 MontaVista Software Inc.
6 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
8 * 031022 - [jsun] add run-time configure and partition setup
10 * Device tree support:
11 * Copyright (C) 2006 MontaVista Software Inc.
12 * Author: Vitaly Wool <vwool@ru.mvista.com>
14 * Revised to handle newer style flash binding by:
15 * Copyright (C) 2007 David Gibson, IBM Corporation.
17 * GPIO address extension:
18 * Handle the case where a flash device is mostly addressed using physical
19 * line and supplemented by GPIOs. This way you can hook up say a 8MiB flash
20 * to a 2MiB memory range and use the GPIOs to select a particular range.
22 * Copyright © 2000 Nicolas Pitre <nico@cam.org>
23 * Copyright © 2005-2009 Analog Devices Inc.
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/device.h>
32 #include <linux/platform_device.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/mtd/map.h>
35 #include <linux/mtd/partitions.h>
36 #include <linux/mtd/physmap.h>
37 #include <linux/mtd/concat.h>
38 #include <linux/mtd/cfi_endian.h>
40 #include <linux/of_device.h>
41 #include <linux/pm_runtime.h>
42 #include <linux/gpio/consumer.h>
44 #include "physmap-bt1-rom.h"
45 #include "physmap-gemini.h"
46 #include "physmap-ixp4xx.h"
47 #include "physmap-versatile.h"
49 struct physmap_flash_info {
51 struct mtd_info **mtds;
52 struct mtd_info *cmtd;
53 struct map_info *maps;
56 const char *probe_type;
57 const char * const *part_types;
59 const struct mtd_partition *parts;
60 struct gpio_descs *gpios;
61 unsigned int gpio_values;
62 unsigned int win_order;
65 static int physmap_flash_remove(struct platform_device *dev)
67 struct physmap_flash_info *info;
68 struct physmap_flash_data *physmap_data;
71 info = platform_get_drvdata(dev);
78 err = mtd_device_unregister(info->cmtd);
82 if (info->cmtd != info->mtds[0])
83 mtd_concat_destroy(info->cmtd);
86 for (i = 0; i < info->nmaps; i++) {
88 map_destroy(info->mtds[i]);
91 physmap_data = dev_get_platdata(&dev->dev);
92 if (physmap_data && physmap_data->exit)
93 physmap_data->exit(dev);
96 pm_runtime_put(&dev->dev);
97 pm_runtime_disable(&dev->dev);
101 static void physmap_set_vpp(struct map_info *map, int state)
103 struct platform_device *pdev;
104 struct physmap_flash_data *physmap_data;
105 struct physmap_flash_info *info;
108 pdev = (struct platform_device *)map->map_priv_1;
109 physmap_data = dev_get_platdata(&pdev->dev);
111 if (!physmap_data->set_vpp)
114 info = platform_get_drvdata(pdev);
116 spin_lock_irqsave(&info->vpp_lock, flags);
118 if (++info->vpp_refcnt == 1) /* first nested 'on' */
119 physmap_data->set_vpp(pdev, 1);
121 if (--info->vpp_refcnt == 0) /* last nested 'off' */
122 physmap_data->set_vpp(pdev, 0);
124 spin_unlock_irqrestore(&info->vpp_lock, flags);
127 #if IS_ENABLED(CONFIG_MTD_PHYSMAP_GPIO_ADDR)
128 static void physmap_set_addr_gpios(struct physmap_flash_info *info,
133 ofs >>= info->win_order;
134 if (info->gpio_values == ofs)
137 for (i = 0; i < info->gpios->ndescs; i++) {
138 if ((BIT(i) & ofs) == (BIT(i) & info->gpio_values))
141 gpiod_set_value(info->gpios->desc[i], !!(BIT(i) & ofs));
144 info->gpio_values = ofs;
147 #define win_mask(order) (BIT(order) - 1)
149 static map_word physmap_addr_gpios_read(struct map_info *map,
152 struct platform_device *pdev;
153 struct physmap_flash_info *info;
157 pdev = (struct platform_device *)map->map_priv_1;
158 info = platform_get_drvdata(pdev);
159 physmap_set_addr_gpios(info, ofs);
161 word = readw(map->virt + (ofs & win_mask(info->win_order)));
166 static void physmap_addr_gpios_copy_from(struct map_info *map, void *buf,
167 unsigned long ofs, ssize_t len)
169 struct platform_device *pdev;
170 struct physmap_flash_info *info;
172 pdev = (struct platform_device *)map->map_priv_1;
173 info = platform_get_drvdata(pdev);
176 unsigned int winofs = ofs & win_mask(info->win_order);
177 unsigned int chunklen = min_t(unsigned int, len,
178 BIT(info->win_order) - winofs);
180 physmap_set_addr_gpios(info, ofs);
181 memcpy_fromio(buf, map->virt + winofs, chunklen);
188 static void physmap_addr_gpios_write(struct map_info *map, map_word mw,
191 struct platform_device *pdev;
192 struct physmap_flash_info *info;
195 pdev = (struct platform_device *)map->map_priv_1;
196 info = platform_get_drvdata(pdev);
197 physmap_set_addr_gpios(info, ofs);
200 writew(word, map->virt + (ofs & win_mask(info->win_order)));
203 static void physmap_addr_gpios_copy_to(struct map_info *map, unsigned long ofs,
204 const void *buf, ssize_t len)
206 struct platform_device *pdev;
207 struct physmap_flash_info *info;
209 pdev = (struct platform_device *)map->map_priv_1;
210 info = platform_get_drvdata(pdev);
213 unsigned int winofs = ofs & win_mask(info->win_order);
214 unsigned int chunklen = min_t(unsigned int, len,
215 BIT(info->win_order) - winofs);
217 physmap_set_addr_gpios(info, ofs);
218 memcpy_toio(map->virt + winofs, buf, chunklen);
225 static int physmap_addr_gpios_map_init(struct map_info *map)
228 map->read = physmap_addr_gpios_read;
229 map->copy_from = physmap_addr_gpios_copy_from;
230 map->write = physmap_addr_gpios_write;
231 map->copy_to = physmap_addr_gpios_copy_to;
236 static int physmap_addr_gpios_map_init(struct map_info *map)
242 #if IS_ENABLED(CONFIG_MTD_PHYSMAP_OF)
243 static const struct of_device_id of_flash_match[] = {
245 .compatible = "cfi-flash",
250 * FIXME: JEDEC chips can't be safely and reliably
251 * probed, although the mtd code gets it right in
252 * practice most of the time. We should use the
253 * vendor and device ids specified by the binding to
254 * bypass the heuristic probe code, but the mtd layer
255 * provides, at present, no interface for doing so
258 .compatible = "jedec-flash",
259 .data = "jedec_probe",
262 .compatible = "mtd-ram",
266 .compatible = "mtd-rom",
271 .compatible = "direct-mapped"
275 MODULE_DEVICE_TABLE(of, of_flash_match);
277 static const char * const of_default_part_probes[] = {
278 "cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL
281 static const char * const *of_get_part_probes(struct platform_device *dev)
283 struct device_node *dp = dev->dev.of_node;
287 count = of_property_count_strings(dp, "linux,part-probe");
289 return of_default_part_probes;
291 res = devm_kcalloc(&dev->dev, count + 1, sizeof(*res), GFP_KERNEL);
295 count = of_property_read_string_array(dp, "linux,part-probe", res,
303 static const char *of_select_probe_type(struct platform_device *dev)
305 struct device_node *dp = dev->dev.of_node;
306 const struct of_device_id *match;
307 const char *probe_type;
309 match = of_match_device(of_flash_match, &dev->dev);
310 probe_type = match->data;
315 "Device tree uses obsolete \"direct-mapped\" flash binding\n");
317 of_property_read_string(dp, "probe-type", &probe_type);
321 if (!strcmp(probe_type, "CFI")) {
322 probe_type = "cfi_probe";
323 } else if (!strcmp(probe_type, "JEDEC")) {
324 probe_type = "jedec_probe";
325 } else if (!strcmp(probe_type, "ROM")) {
326 probe_type = "map_rom";
329 "obsolete_probe: don't know probe type '%s', mapping as rom\n",
331 probe_type = "map_rom";
337 static int physmap_flash_of_init(struct platform_device *dev)
339 struct physmap_flash_info *info = platform_get_drvdata(dev);
340 struct device_node *dp = dev->dev.of_node;
341 const char *mtd_name = NULL;
350 info->probe_type = of_select_probe_type(dev);
352 info->part_types = of_get_part_probes(dev);
353 if (!info->part_types)
356 of_property_read_string(dp, "linux,mtd-name", &mtd_name);
358 map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access");
360 err = of_property_read_u32(dp, "bank-width", &bankwidth);
362 dev_err(&dev->dev, "Can't get bank width from device tree\n");
366 if (of_property_read_bool(dp, "big-endian"))
367 swap = CFI_BIG_ENDIAN;
368 else if (of_property_read_bool(dp, "little-endian"))
369 swap = CFI_LITTLE_ENDIAN;
371 for (i = 0; i < info->nmaps; i++) {
372 info->maps[i].name = mtd_name;
373 info->maps[i].swap = swap;
374 info->maps[i].bankwidth = bankwidth;
375 info->maps[i].device_node = dp;
377 err = of_flash_probe_bt1_rom(dev, dp, &info->maps[i]);
381 err = of_flash_probe_gemini(dev, dp, &info->maps[i]);
385 err = of_flash_probe_ixp4xx(dev, dp, &info->maps[i]);
389 err = of_flash_probe_versatile(dev, dp, &info->maps[i]);
394 * On some platforms (e.g. MPC5200) a direct 1:1 mapping
395 * may cause problems with JFFS2 usage, as the local bus (LPB)
396 * doesn't support unaligned accesses as implemented in the
397 * JFFS2 code via memcpy(). By setting NO_XIP, the
398 * flash will not be exposed directly to the MTD users
399 * (e.g. JFFS2) any more.
402 info->maps[i].phys = NO_XIP;
407 #else /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */
408 #define of_flash_match NULL
410 static int physmap_flash_of_init(struct platform_device *dev)
414 #endif /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */
416 static const char * const rom_probe_types[] = {
417 "cfi_probe", "jedec_probe", "qinfo_probe", "map_rom",
420 static const char * const part_probe_types[] = {
421 "cmdlinepart", "RedBoot", "afs", NULL
424 static int physmap_flash_pdata_init(struct platform_device *dev)
426 struct physmap_flash_info *info = platform_get_drvdata(dev);
427 struct physmap_flash_data *physmap_data;
431 physmap_data = dev_get_platdata(&dev->dev);
435 info->probe_type = physmap_data->probe_type;
436 info->part_types = physmap_data->part_probe_types ? : part_probe_types;
437 info->parts = physmap_data->parts;
438 info->nparts = physmap_data->nr_parts;
440 if (physmap_data->init) {
441 err = physmap_data->init(dev);
446 for (i = 0; i < info->nmaps; i++) {
447 info->maps[i].bankwidth = physmap_data->width;
448 info->maps[i].pfow_base = physmap_data->pfow_base;
449 info->maps[i].set_vpp = physmap_set_vpp;
455 static int physmap_flash_probe(struct platform_device *dev)
457 struct physmap_flash_info *info;
461 if (!dev->dev.of_node && !dev_get_platdata(&dev->dev))
464 info = devm_kzalloc(&dev->dev, sizeof(*info), GFP_KERNEL);
468 while (platform_get_resource(dev, IORESOURCE_MEM, info->nmaps))
474 info->maps = devm_kzalloc(&dev->dev,
475 sizeof(*info->maps) * info->nmaps,
480 info->mtds = devm_kzalloc(&dev->dev,
481 sizeof(*info->mtds) * info->nmaps,
486 platform_set_drvdata(dev, info);
488 info->gpios = devm_gpiod_get_array_optional(&dev->dev, "addr",
490 if (IS_ERR(info->gpios))
491 return PTR_ERR(info->gpios);
493 if (info->gpios && info->nmaps > 1) {
494 dev_err(&dev->dev, "addr-gpios only supported for nmaps == 1\n");
498 pm_runtime_enable(&dev->dev);
499 pm_runtime_get_sync(&dev->dev);
501 if (dev->dev.of_node)
502 err = physmap_flash_of_init(dev);
504 err = physmap_flash_pdata_init(dev);
507 pm_runtime_put(&dev->dev);
508 pm_runtime_disable(&dev->dev);
512 for (i = 0; i < info->nmaps; i++) {
513 struct resource *res;
515 res = platform_get_resource(dev, IORESOURCE_MEM, i);
516 info->maps[i].virt = devm_ioremap_resource(&dev->dev, res);
517 if (IS_ERR(info->maps[i].virt)) {
518 err = PTR_ERR(info->maps[i].virt);
522 dev_notice(&dev->dev, "physmap platform flash device: %pR\n",
525 if (!info->maps[i].name)
526 info->maps[i].name = dev_name(&dev->dev);
528 if (!info->maps[i].phys)
529 info->maps[i].phys = res->start;
531 info->win_order = fls64(resource_size(res)) - 1;
532 info->maps[i].size = BIT(info->win_order +
534 info->gpios->ndescs : 0));
536 info->maps[i].map_priv_1 = (unsigned long)dev;
539 err = physmap_addr_gpios_map_init(&info->maps[i]);
544 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
546 * Only use the simple_map implementation if map hooks are not
547 * implemented. Since map->read() is mandatory checking for its
548 * presence is enough.
550 if (!info->maps[i].read)
551 simple_map_init(&info->maps[i]);
553 simple_map_init(&info->maps[i]);
556 if (info->probe_type) {
557 info->mtds[i] = do_map_probe(info->probe_type,
560 /* Fall back to mapping region as ROM */
561 if (!info->mtds[i] && IS_ENABLED(CONFIG_MTD_ROM) &&
562 strcmp(info->probe_type, "map_rom")) {
564 "map_probe() failed for type %s\n",
567 info->mtds[i] = do_map_probe("map_rom",
573 for (j = 0; j < ARRAY_SIZE(rom_probe_types); j++) {
574 info->mtds[i] = do_map_probe(rom_probe_types[j],
581 if (!info->mtds[i]) {
582 dev_err(&dev->dev, "map_probe failed\n");
586 info->mtds[i]->dev.parent = &dev->dev;
589 if (info->nmaps == 1) {
590 info->cmtd = info->mtds[0];
593 * We detected multiple devices. Concatenate them together.
595 info->cmtd = mtd_concat_create(info->mtds, info->nmaps,
596 dev_name(&dev->dev));
603 spin_lock_init(&info->vpp_lock);
605 mtd_set_of_node(info->cmtd, dev->dev.of_node);
606 err = mtd_device_parse_register(info->cmtd, info->part_types, NULL,
607 info->parts, info->nparts);
614 physmap_flash_remove(dev);
619 static void physmap_flash_shutdown(struct platform_device *dev)
621 struct physmap_flash_info *info = platform_get_drvdata(dev);
624 for (i = 0; i < info->nmaps && info->mtds[i]; i++)
625 if (mtd_suspend(info->mtds[i]) == 0)
626 mtd_resume(info->mtds[i]);
629 #define physmap_flash_shutdown NULL
632 static struct platform_driver physmap_flash_driver = {
633 .probe = physmap_flash_probe,
634 .remove = physmap_flash_remove,
635 .shutdown = physmap_flash_shutdown,
637 .name = "physmap-flash",
638 .of_match_table = of_flash_match,
642 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
643 static struct physmap_flash_data physmap_flash_data = {
644 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
647 static struct resource physmap_flash_resource = {
648 .start = CONFIG_MTD_PHYSMAP_START,
649 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
650 .flags = IORESOURCE_MEM,
653 static struct platform_device physmap_flash = {
654 .name = "physmap-flash",
657 .platform_data = &physmap_flash_data,
660 .resource = &physmap_flash_resource,
664 static int __init physmap_init(void)
668 err = platform_driver_register(&physmap_flash_driver);
669 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
671 err = platform_device_register(&physmap_flash);
673 platform_driver_unregister(&physmap_flash_driver);
680 static void __exit physmap_exit(void)
682 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
683 platform_device_unregister(&physmap_flash);
685 platform_driver_unregister(&physmap_flash_driver);
688 module_init(physmap_init);
689 module_exit(physmap_exit);
691 MODULE_LICENSE("GPL");
692 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
693 MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
694 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
695 MODULE_DESCRIPTION("Generic configurable MTD map driver");
697 /* legacy platform drivers can't hotplug or coldplg */
698 #ifndef CONFIG_MTD_PHYSMAP_COMPAT
699 /* work with hotplug and coldplug */
700 MODULE_ALIAS("platform:physmap-flash");