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/gpio/consumer.h>
43 #include "physmap-gemini.h"
44 #include "physmap-versatile.h"
46 struct physmap_flash_info {
48 struct mtd_info **mtds;
49 struct mtd_info *cmtd;
50 struct map_info *maps;
53 const char *probe_type;
54 const char * const *part_types;
56 const struct mtd_partition *parts;
57 struct gpio_descs *gpios;
58 unsigned int gpio_values;
59 unsigned int win_order;
62 static int physmap_flash_remove(struct platform_device *dev)
64 struct physmap_flash_info *info;
65 struct physmap_flash_data *physmap_data;
68 info = platform_get_drvdata(dev);
73 err = mtd_device_unregister(info->cmtd);
77 if (info->cmtd != info->mtds[0])
78 mtd_concat_destroy(info->cmtd);
81 for (i = 0; i < info->nmaps; i++) {
83 map_destroy(info->mtds[i]);
86 physmap_data = dev_get_platdata(&dev->dev);
87 if (physmap_data && physmap_data->exit)
88 physmap_data->exit(dev);
93 static void physmap_set_vpp(struct map_info *map, int state)
95 struct platform_device *pdev;
96 struct physmap_flash_data *physmap_data;
97 struct physmap_flash_info *info;
100 pdev = (struct platform_device *)map->map_priv_1;
101 physmap_data = dev_get_platdata(&pdev->dev);
103 if (!physmap_data->set_vpp)
106 info = platform_get_drvdata(pdev);
108 spin_lock_irqsave(&info->vpp_lock, flags);
110 if (++info->vpp_refcnt == 1) /* first nested 'on' */
111 physmap_data->set_vpp(pdev, 1);
113 if (--info->vpp_refcnt == 0) /* last nested 'off' */
114 physmap_data->set_vpp(pdev, 0);
116 spin_unlock_irqrestore(&info->vpp_lock, flags);
119 #if IS_ENABLED(CONFIG_MTD_PHYSMAP_GPIO_ADDR)
120 static void physmap_set_addr_gpios(struct physmap_flash_info *info,
125 ofs >>= info->win_order;
126 if (info->gpio_values == ofs)
129 for (i = 0; i < info->gpios->ndescs; i++) {
130 if ((BIT(i) & ofs) == (BIT(i) & info->gpio_values))
133 gpiod_set_value(info->gpios->desc[i], !!(BIT(i) & ofs));
136 info->gpio_values = ofs;
139 #define win_mask(order) (BIT(order) - 1)
141 static map_word physmap_addr_gpios_read(struct map_info *map,
144 struct platform_device *pdev;
145 struct physmap_flash_info *info;
149 pdev = (struct platform_device *)map->map_priv_1;
150 info = platform_get_drvdata(pdev);
151 physmap_set_addr_gpios(info, ofs);
153 word = readw(map->virt + (ofs & win_mask(info->win_order)));
158 static void physmap_addr_gpios_copy_from(struct map_info *map, void *buf,
159 unsigned long ofs, ssize_t len)
161 struct platform_device *pdev;
162 struct physmap_flash_info *info;
164 pdev = (struct platform_device *)map->map_priv_1;
165 info = platform_get_drvdata(pdev);
168 unsigned int winofs = ofs & win_mask(info->win_order);
169 unsigned int chunklen = min_t(unsigned int, len,
170 BIT(info->win_order) - winofs);
172 physmap_set_addr_gpios(info, ofs);
173 memcpy_fromio(buf, map->virt + winofs, chunklen);
180 static void physmap_addr_gpios_write(struct map_info *map, map_word mw,
183 struct platform_device *pdev;
184 struct physmap_flash_info *info;
187 pdev = (struct platform_device *)map->map_priv_1;
188 info = platform_get_drvdata(pdev);
189 physmap_set_addr_gpios(info, ofs);
192 writew(word, map->virt + (ofs & win_mask(info->win_order)));
195 static void physmap_addr_gpios_copy_to(struct map_info *map, unsigned long ofs,
196 const void *buf, ssize_t len)
198 struct platform_device *pdev;
199 struct physmap_flash_info *info;
201 pdev = (struct platform_device *)map->map_priv_1;
202 info = platform_get_drvdata(pdev);
205 unsigned int winofs = ofs & win_mask(info->win_order);
206 unsigned int chunklen = min_t(unsigned int, len,
207 BIT(info->win_order) - winofs);
209 physmap_set_addr_gpios(info, ofs);
210 memcpy_toio(map->virt + winofs, buf, chunklen);
217 static int physmap_addr_gpios_map_init(struct map_info *map)
220 map->read = physmap_addr_gpios_read;
221 map->copy_from = physmap_addr_gpios_copy_from;
222 map->write = physmap_addr_gpios_write;
223 map->copy_to = physmap_addr_gpios_copy_to;
228 static int physmap_addr_gpios_map_init(struct map_info *map)
234 #if IS_ENABLED(CONFIG_MTD_PHYSMAP_OF)
235 static const struct of_device_id of_flash_match[] = {
237 .compatible = "cfi-flash",
242 * FIXME: JEDEC chips can't be safely and reliably
243 * probed, although the mtd code gets it right in
244 * practice most of the time. We should use the
245 * vendor and device ids specified by the binding to
246 * bypass the heuristic probe code, but the mtd layer
247 * provides, at present, no interface for doing so
250 .compatible = "jedec-flash",
251 .data = "jedec_probe",
254 .compatible = "mtd-ram",
258 .compatible = "mtd-rom",
263 .compatible = "direct-mapped"
267 MODULE_DEVICE_TABLE(of, of_flash_match);
269 static const char * const of_default_part_probes[] = {
270 "cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL
273 static const char * const *of_get_part_probes(struct platform_device *dev)
275 struct device_node *dp = dev->dev.of_node;
279 count = of_property_count_strings(dp, "linux,part-probe");
281 return of_default_part_probes;
283 res = devm_kcalloc(&dev->dev, count + 1, sizeof(*res), GFP_KERNEL);
287 count = of_property_read_string_array(dp, "linux,part-probe", res,
295 static const char *of_select_probe_type(struct platform_device *dev)
297 struct device_node *dp = dev->dev.of_node;
298 const struct of_device_id *match;
299 const char *probe_type;
301 match = of_match_device(of_flash_match, &dev->dev);
302 probe_type = match->data;
307 "Device tree uses obsolete \"direct-mapped\" flash binding\n");
309 of_property_read_string(dp, "probe-type", &probe_type);
313 if (!strcmp(probe_type, "CFI")) {
314 probe_type = "cfi_probe";
315 } else if (!strcmp(probe_type, "JEDEC")) {
316 probe_type = "jedec_probe";
317 } else if (!strcmp(probe_type, "ROM")) {
318 probe_type = "map_rom";
321 "obsolete_probe: don't know probe type '%s', mapping as rom\n",
323 probe_type = "map_rom";
329 static int physmap_flash_of_init(struct platform_device *dev)
331 struct physmap_flash_info *info = platform_get_drvdata(dev);
332 struct device_node *dp = dev->dev.of_node;
333 const char *mtd_name = NULL;
342 info->probe_type = of_select_probe_type(dev);
344 info->part_types = of_get_part_probes(dev);
345 if (!info->part_types)
348 of_property_read_string(dp, "linux,mtd-name", &mtd_name);
350 map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access");
352 err = of_property_read_u32(dp, "bank-width", &bankwidth);
354 dev_err(&dev->dev, "Can't get bank width from device tree\n");
358 if (of_property_read_bool(dp, "big-endian"))
359 swap = CFI_BIG_ENDIAN;
360 else if (of_property_read_bool(dp, "little-endian"))
361 swap = CFI_LITTLE_ENDIAN;
363 for (i = 0; i < info->nmaps; i++) {
364 info->maps[i].name = mtd_name;
365 info->maps[i].swap = swap;
366 info->maps[i].bankwidth = bankwidth;
367 info->maps[i].device_node = dp;
369 err = of_flash_probe_gemini(dev, dp, &info->maps[i]);
373 err = of_flash_probe_versatile(dev, dp, &info->maps[i]);
378 * On some platforms (e.g. MPC5200) a direct 1:1 mapping
379 * may cause problems with JFFS2 usage, as the local bus (LPB)
380 * doesn't support unaligned accesses as implemented in the
381 * JFFS2 code via memcpy(). By setting NO_XIP, the
382 * flash will not be exposed directly to the MTD users
383 * (e.g. JFFS2) any more.
386 info->maps[i].phys = NO_XIP;
391 #else /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */
392 #define of_flash_match NULL
394 static int physmap_flash_of_init(struct platform_device *dev)
398 #endif /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */
400 static const char * const rom_probe_types[] = {
401 "cfi_probe", "jedec_probe", "qinfo_probe", "map_rom",
404 static const char * const part_probe_types[] = {
405 "cmdlinepart", "RedBoot", "afs", NULL
408 static int physmap_flash_pdata_init(struct platform_device *dev)
410 struct physmap_flash_info *info = platform_get_drvdata(dev);
411 struct physmap_flash_data *physmap_data;
415 physmap_data = dev_get_platdata(&dev->dev);
419 info->probe_type = physmap_data->probe_type;
420 info->part_types = physmap_data->part_probe_types ? : part_probe_types;
421 info->parts = physmap_data->parts;
422 info->nparts = physmap_data->nr_parts;
424 if (physmap_data->init) {
425 err = physmap_data->init(dev);
430 for (i = 0; i < info->nmaps; i++) {
431 info->maps[i].bankwidth = physmap_data->width;
432 info->maps[i].pfow_base = physmap_data->pfow_base;
433 info->maps[i].set_vpp = physmap_set_vpp;
439 static int physmap_flash_probe(struct platform_device *dev)
441 struct physmap_flash_info *info;
445 if (!dev->dev.of_node && !dev_get_platdata(&dev->dev))
448 info = devm_kzalloc(&dev->dev, sizeof(*info), GFP_KERNEL);
452 while (platform_get_resource(dev, IORESOURCE_MEM, info->nmaps))
458 info->maps = devm_kzalloc(&dev->dev,
459 sizeof(*info->maps) * info->nmaps,
464 info->mtds = devm_kzalloc(&dev->dev,
465 sizeof(*info->mtds) * info->nmaps,
470 platform_set_drvdata(dev, info);
472 info->gpios = devm_gpiod_get_array_optional(&dev->dev, "addr",
474 if (IS_ERR(info->gpios))
475 return PTR_ERR(info->gpios);
477 if (info->gpios && info->nmaps > 1) {
478 dev_err(&dev->dev, "addr-gpios only supported for nmaps == 1\n");
482 if (dev->dev.of_node)
483 err = physmap_flash_of_init(dev);
485 err = physmap_flash_pdata_init(dev);
490 for (i = 0; i < info->nmaps; i++) {
491 struct resource *res;
493 res = platform_get_resource(dev, IORESOURCE_MEM, i);
494 info->maps[i].virt = devm_ioremap_resource(&dev->dev, res);
495 if (IS_ERR(info->maps[i].virt)) {
496 err = PTR_ERR(info->maps[i].virt);
500 dev_notice(&dev->dev, "physmap platform flash device: %pR\n",
503 info->maps[i].name = dev_name(&dev->dev);
505 if (!info->maps[i].phys)
506 info->maps[i].phys = res->start;
508 info->win_order = get_bitmask_order(resource_size(res)) - 1;
509 info->maps[i].size = BIT(info->win_order +
511 info->gpios->ndescs : 0));
513 info->maps[i].map_priv_1 = (unsigned long)dev;
516 err = physmap_addr_gpios_map_init(&info->maps[i]);
521 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
523 * Only use the simple_map implementation if map hooks are not
524 * implemented. Since map->read() is mandatory checking for its
525 * presence is enough.
527 if (!info->maps[i].read)
528 simple_map_init(&info->maps[i]);
530 simple_map_init(&info->maps[i]);
533 if (info->probe_type) {
534 info->mtds[i] = do_map_probe(info->probe_type,
539 for (j = 0; j < ARRAY_SIZE(rom_probe_types); j++) {
540 info->mtds[i] = do_map_probe(rom_probe_types[j],
547 if (!info->mtds[i]) {
548 dev_err(&dev->dev, "map_probe failed\n");
552 info->mtds[i]->dev.parent = &dev->dev;
555 if (info->nmaps == 1) {
556 info->cmtd = info->mtds[0];
559 * We detected multiple devices. Concatenate them together.
561 info->cmtd = mtd_concat_create(info->mtds, info->nmaps,
562 dev_name(&dev->dev));
569 spin_lock_init(&info->vpp_lock);
571 mtd_set_of_node(info->cmtd, dev->dev.of_node);
572 err = mtd_device_parse_register(info->cmtd, info->part_types, NULL,
573 info->parts, info->nparts);
580 physmap_flash_remove(dev);
585 static void physmap_flash_shutdown(struct platform_device *dev)
587 struct physmap_flash_info *info = platform_get_drvdata(dev);
590 for (i = 0; i < info->nmaps && info->mtds[i]; i++)
591 if (mtd_suspend(info->mtds[i]) == 0)
592 mtd_resume(info->mtds[i]);
595 #define physmap_flash_shutdown NULL
598 static struct platform_driver physmap_flash_driver = {
599 .probe = physmap_flash_probe,
600 .remove = physmap_flash_remove,
601 .shutdown = physmap_flash_shutdown,
603 .name = "physmap-flash",
604 .of_match_table = of_flash_match,
608 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
609 static struct physmap_flash_data physmap_flash_data = {
610 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
613 static struct resource physmap_flash_resource = {
614 .start = CONFIG_MTD_PHYSMAP_START,
615 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
616 .flags = IORESOURCE_MEM,
619 static struct platform_device physmap_flash = {
620 .name = "physmap-flash",
623 .platform_data = &physmap_flash_data,
626 .resource = &physmap_flash_resource,
630 static int __init physmap_init(void)
634 err = platform_driver_register(&physmap_flash_driver);
635 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
637 err = platform_device_register(&physmap_flash);
639 platform_driver_unregister(&physmap_flash_driver);
646 static void __exit physmap_exit(void)
648 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
649 platform_device_unregister(&physmap_flash);
651 platform_driver_unregister(&physmap_flash_driver);
654 module_init(physmap_init);
655 module_exit(physmap_exit);
657 MODULE_LICENSE("GPL");
658 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
659 MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
660 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
661 MODULE_DESCRIPTION("Generic configurable MTD map driver");
663 /* legacy platform drivers can't hotplug or coldplg */
664 #ifndef CONFIG_MTD_PHYSMAP_COMPAT
665 /* work with hotplug and coldplug */
666 MODULE_ALIAS("platform:physmap-flash");