1 // SPDX-License-Identifier: GPL-2.0+
3 * Normal mappings of chips in physical memory
5 * Copyright (C) 2003 MontaVista Software Inc.
8 * 031022 - [jsun] add run-time configure and partition setup
10 * Device tree support:
11 * Copyright (C) 2006 MontaVista Software Inc.
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.
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);
74 WARN_ON(mtd_device_unregister(info->cmtd));
76 if (info->cmtd != info->mtds[0])
77 mtd_concat_destroy(info->cmtd);
80 for (i = 0; i < info->nmaps; i++) {
82 map_destroy(info->mtds[i]);
85 physmap_data = dev_get_platdata(&dev->dev);
86 if (physmap_data && physmap_data->exit)
87 physmap_data->exit(dev);
89 pm_runtime_put(&dev->dev);
90 pm_runtime_disable(&dev->dev);
94 static void physmap_set_vpp(struct map_info *map, int state)
96 struct platform_device *pdev;
97 struct physmap_flash_data *physmap_data;
98 struct physmap_flash_info *info;
101 pdev = (struct platform_device *)map->map_priv_1;
102 physmap_data = dev_get_platdata(&pdev->dev);
104 if (!physmap_data->set_vpp)
107 info = platform_get_drvdata(pdev);
109 spin_lock_irqsave(&info->vpp_lock, flags);
111 if (++info->vpp_refcnt == 1) /* first nested 'on' */
112 physmap_data->set_vpp(pdev, 1);
114 if (--info->vpp_refcnt == 0) /* last nested 'off' */
115 physmap_data->set_vpp(pdev, 0);
117 spin_unlock_irqrestore(&info->vpp_lock, flags);
120 #if IS_ENABLED(CONFIG_MTD_PHYSMAP_GPIO_ADDR)
121 static void physmap_set_addr_gpios(struct physmap_flash_info *info,
126 ofs >>= info->win_order;
127 if (info->gpio_values == ofs)
130 for (i = 0; i < info->gpios->ndescs; i++) {
131 if ((BIT(i) & ofs) == (BIT(i) & info->gpio_values))
134 gpiod_set_value(info->gpios->desc[i], !!(BIT(i) & ofs));
137 info->gpio_values = ofs;
140 #define win_mask(order) (BIT(order) - 1)
142 static map_word physmap_addr_gpios_read(struct map_info *map,
145 struct platform_device *pdev;
146 struct physmap_flash_info *info;
150 pdev = (struct platform_device *)map->map_priv_1;
151 info = platform_get_drvdata(pdev);
152 physmap_set_addr_gpios(info, ofs);
154 word = readw(map->virt + (ofs & win_mask(info->win_order)));
159 static void physmap_addr_gpios_copy_from(struct map_info *map, void *buf,
160 unsigned long ofs, ssize_t len)
162 struct platform_device *pdev;
163 struct physmap_flash_info *info;
165 pdev = (struct platform_device *)map->map_priv_1;
166 info = platform_get_drvdata(pdev);
169 unsigned int winofs = ofs & win_mask(info->win_order);
170 unsigned int chunklen = min_t(unsigned int, len,
171 BIT(info->win_order) - winofs);
173 physmap_set_addr_gpios(info, ofs);
174 memcpy_fromio(buf, map->virt + winofs, chunklen);
181 static void physmap_addr_gpios_write(struct map_info *map, map_word mw,
184 struct platform_device *pdev;
185 struct physmap_flash_info *info;
188 pdev = (struct platform_device *)map->map_priv_1;
189 info = platform_get_drvdata(pdev);
190 physmap_set_addr_gpios(info, ofs);
193 writew(word, map->virt + (ofs & win_mask(info->win_order)));
196 static void physmap_addr_gpios_copy_to(struct map_info *map, unsigned long ofs,
197 const void *buf, ssize_t len)
199 struct platform_device *pdev;
200 struct physmap_flash_info *info;
202 pdev = (struct platform_device *)map->map_priv_1;
203 info = platform_get_drvdata(pdev);
206 unsigned int winofs = ofs & win_mask(info->win_order);
207 unsigned int chunklen = min_t(unsigned int, len,
208 BIT(info->win_order) - winofs);
210 physmap_set_addr_gpios(info, ofs);
211 memcpy_toio(map->virt + winofs, buf, chunklen);
218 static int physmap_addr_gpios_map_init(struct map_info *map)
221 map->read = physmap_addr_gpios_read;
222 map->copy_from = physmap_addr_gpios_copy_from;
223 map->write = physmap_addr_gpios_write;
224 map->copy_to = physmap_addr_gpios_copy_to;
229 static int physmap_addr_gpios_map_init(struct map_info *map)
235 #if IS_ENABLED(CONFIG_MTD_PHYSMAP_OF)
236 static const struct of_device_id of_flash_match[] = {
238 .compatible = "cfi-flash",
243 * FIXME: JEDEC chips can't be safely and reliably
244 * probed, although the mtd code gets it right in
245 * practice most of the time. We should use the
246 * vendor and device ids specified by the binding to
247 * bypass the heuristic probe code, but the mtd layer
248 * provides, at present, no interface for doing so
251 .compatible = "jedec-flash",
252 .data = "jedec_probe",
255 .compatible = "mtd-ram",
259 .compatible = "mtd-rom",
264 .compatible = "direct-mapped"
268 MODULE_DEVICE_TABLE(of, of_flash_match);
270 static const char * const of_default_part_probes[] = {
271 "cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL
274 static const char * const *of_get_part_probes(struct platform_device *dev)
276 struct device_node *dp = dev->dev.of_node;
280 count = of_property_count_strings(dp, "linux,part-probe");
282 return of_default_part_probes;
284 res = devm_kcalloc(&dev->dev, count + 1, sizeof(*res), GFP_KERNEL);
288 count = of_property_read_string_array(dp, "linux,part-probe", res,
296 static const char *of_select_probe_type(struct platform_device *dev)
298 struct device_node *dp = dev->dev.of_node;
299 const struct of_device_id *match;
300 const char *probe_type;
302 match = of_match_device(of_flash_match, &dev->dev);
303 probe_type = match->data;
308 "Device tree uses obsolete \"direct-mapped\" flash binding\n");
310 of_property_read_string(dp, "probe-type", &probe_type);
314 if (!strcmp(probe_type, "CFI")) {
315 probe_type = "cfi_probe";
316 } else if (!strcmp(probe_type, "JEDEC")) {
317 probe_type = "jedec_probe";
318 } else if (!strcmp(probe_type, "ROM")) {
319 probe_type = "map_rom";
322 "obsolete_probe: don't know probe type '%s', mapping as rom\n",
324 probe_type = "map_rom";
330 static int physmap_flash_of_init(struct platform_device *dev)
332 struct physmap_flash_info *info = platform_get_drvdata(dev);
333 struct device_node *dp = dev->dev.of_node;
334 const char *mtd_name = NULL;
343 info->probe_type = of_select_probe_type(dev);
345 info->part_types = of_get_part_probes(dev);
346 if (!info->part_types)
349 of_property_read_string(dp, "linux,mtd-name", &mtd_name);
351 map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access");
353 err = of_property_read_u32(dp, "bank-width", &bankwidth);
355 dev_err(&dev->dev, "Can't get bank width from device tree\n");
359 if (of_property_read_bool(dp, "big-endian"))
360 swap = CFI_BIG_ENDIAN;
361 else if (of_property_read_bool(dp, "little-endian"))
362 swap = CFI_LITTLE_ENDIAN;
364 for (i = 0; i < info->nmaps; i++) {
365 info->maps[i].name = mtd_name;
366 info->maps[i].swap = swap;
367 info->maps[i].bankwidth = bankwidth;
368 info->maps[i].device_node = dp;
370 err = of_flash_probe_bt1_rom(dev, dp, &info->maps[i]);
374 err = of_flash_probe_gemini(dev, dp, &info->maps[i]);
378 err = of_flash_probe_ixp4xx(dev, dp, &info->maps[i]);
382 err = of_flash_probe_versatile(dev, dp, &info->maps[i]);
387 * On some platforms (e.g. MPC5200) a direct 1:1 mapping
388 * may cause problems with JFFS2 usage, as the local bus (LPB)
389 * doesn't support unaligned accesses as implemented in the
390 * JFFS2 code via memcpy(). By setting NO_XIP, the
391 * flash will not be exposed directly to the MTD users
392 * (e.g. JFFS2) any more.
395 info->maps[i].phys = NO_XIP;
400 #else /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */
401 #define of_flash_match NULL
403 static int physmap_flash_of_init(struct platform_device *dev)
407 #endif /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */
409 static const char * const rom_probe_types[] = {
410 "cfi_probe", "jedec_probe", "qinfo_probe", "map_rom",
413 static const char * const part_probe_types[] = {
414 "cmdlinepart", "RedBoot", "afs", NULL
417 static int physmap_flash_pdata_init(struct platform_device *dev)
419 struct physmap_flash_info *info = platform_get_drvdata(dev);
420 struct physmap_flash_data *physmap_data;
424 physmap_data = dev_get_platdata(&dev->dev);
428 info->probe_type = physmap_data->probe_type;
429 info->part_types = physmap_data->part_probe_types ? : part_probe_types;
430 info->parts = physmap_data->parts;
431 info->nparts = physmap_data->nr_parts;
433 if (physmap_data->init) {
434 err = physmap_data->init(dev);
439 for (i = 0; i < info->nmaps; i++) {
440 info->maps[i].bankwidth = physmap_data->width;
441 info->maps[i].pfow_base = physmap_data->pfow_base;
442 info->maps[i].set_vpp = physmap_set_vpp;
448 static int physmap_flash_probe(struct platform_device *dev)
450 struct physmap_flash_info *info;
454 if (!dev->dev.of_node && !dev_get_platdata(&dev->dev))
457 info = devm_kzalloc(&dev->dev, sizeof(*info), GFP_KERNEL);
461 while (platform_get_resource(dev, IORESOURCE_MEM, info->nmaps))
467 info->maps = devm_kzalloc(&dev->dev,
468 sizeof(*info->maps) * info->nmaps,
473 info->mtds = devm_kzalloc(&dev->dev,
474 sizeof(*info->mtds) * info->nmaps,
479 platform_set_drvdata(dev, info);
481 info->gpios = devm_gpiod_get_array_optional(&dev->dev, "addr",
483 if (IS_ERR(info->gpios))
484 return PTR_ERR(info->gpios);
486 if (info->gpios && info->nmaps > 1) {
487 dev_err(&dev->dev, "addr-gpios only supported for nmaps == 1\n");
491 pm_runtime_enable(&dev->dev);
492 pm_runtime_get_sync(&dev->dev);
494 if (dev->dev.of_node)
495 err = physmap_flash_of_init(dev);
497 err = physmap_flash_pdata_init(dev);
500 pm_runtime_put(&dev->dev);
501 pm_runtime_disable(&dev->dev);
505 for (i = 0; i < info->nmaps; i++) {
506 struct resource *res;
508 res = platform_get_resource(dev, IORESOURCE_MEM, i);
509 info->maps[i].virt = devm_ioremap_resource(&dev->dev, res);
510 if (IS_ERR(info->maps[i].virt)) {
511 err = PTR_ERR(info->maps[i].virt);
515 dev_notice(&dev->dev, "physmap platform flash device: %pR\n",
518 if (!info->maps[i].name)
519 info->maps[i].name = dev_name(&dev->dev);
521 if (!info->maps[i].phys)
522 info->maps[i].phys = res->start;
524 info->win_order = get_bitmask_order(resource_size(res)) - 1;
525 info->maps[i].size = BIT(info->win_order +
527 info->gpios->ndescs : 0));
529 info->maps[i].map_priv_1 = (unsigned long)dev;
532 err = physmap_addr_gpios_map_init(&info->maps[i]);
537 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
539 * Only use the simple_map implementation if map hooks are not
540 * implemented. Since map->read() is mandatory checking for its
541 * presence is enough.
543 if (!info->maps[i].read)
544 simple_map_init(&info->maps[i]);
546 simple_map_init(&info->maps[i]);
549 if (info->probe_type) {
550 info->mtds[i] = do_map_probe(info->probe_type,
555 for (j = 0; j < ARRAY_SIZE(rom_probe_types); j++) {
556 info->mtds[i] = do_map_probe(rom_probe_types[j],
563 if (!info->mtds[i]) {
564 dev_err(&dev->dev, "map_probe failed\n");
568 info->mtds[i]->dev.parent = &dev->dev;
571 if (info->nmaps == 1) {
572 info->cmtd = info->mtds[0];
575 * We detected multiple devices. Concatenate them together.
577 info->cmtd = mtd_concat_create(info->mtds, info->nmaps,
578 dev_name(&dev->dev));
585 spin_lock_init(&info->vpp_lock);
587 mtd_set_of_node(info->cmtd, dev->dev.of_node);
588 err = mtd_device_parse_register(info->cmtd, info->part_types, NULL,
589 info->parts, info->nparts);
596 physmap_flash_remove(dev);
601 static void physmap_flash_shutdown(struct platform_device *dev)
603 struct physmap_flash_info *info = platform_get_drvdata(dev);
606 for (i = 0; i < info->nmaps && info->mtds[i]; i++)
607 if (mtd_suspend(info->mtds[i]) == 0)
608 mtd_resume(info->mtds[i]);
611 #define physmap_flash_shutdown NULL
614 static struct platform_driver physmap_flash_driver = {
615 .probe = physmap_flash_probe,
616 .remove = physmap_flash_remove,
617 .shutdown = physmap_flash_shutdown,
619 .name = "physmap-flash",
620 .of_match_table = of_flash_match,
624 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
625 static struct physmap_flash_data physmap_flash_data = {
626 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
629 static struct resource physmap_flash_resource = {
630 .start = CONFIG_MTD_PHYSMAP_START,
631 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
632 .flags = IORESOURCE_MEM,
635 static struct platform_device physmap_flash = {
636 .name = "physmap-flash",
639 .platform_data = &physmap_flash_data,
642 .resource = &physmap_flash_resource,
646 static int __init physmap_init(void)
650 err = platform_driver_register(&physmap_flash_driver);
651 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
653 err = platform_device_register(&physmap_flash);
655 platform_driver_unregister(&physmap_flash_driver);
662 static void __exit physmap_exit(void)
664 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
665 platform_device_unregister(&physmap_flash);
667 platform_driver_unregister(&physmap_flash_driver);
670 module_init(physmap_init);
671 module_exit(physmap_exit);
673 MODULE_LICENSE("GPL");
677 MODULE_DESCRIPTION("Generic configurable MTD map driver");
679 /* legacy platform drivers can't hotplug or coldplg */
680 #ifndef CONFIG_MTD_PHYSMAP_COMPAT
681 /* work with hotplug and coldplug */
682 MODULE_ALIAS("platform:physmap-flash");