1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Map driver for Intel XScale PXA2xx platforms.
4 *
5 * Author:	Nicolas Pitre
6 * Copyright:	(C) 2001 MontaVista Software Inc.
7 */
8
9#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/kernel.h>
13#include <linux/platform_device.h>
14#include <linux/mtd/mtd.h>
15#include <linux/mtd/map.h>
16#include <linux/mtd/partitions.h>
17
18#include <asm/io.h>
19#include <asm/mach/flash.h>
20
21#define CACHELINESIZE	32
22
23static void pxa2xx_map_inval_cache(struct map_info *map, unsigned long from,
24				      ssize_t len)
25{
26	unsigned long start = (unsigned long)map->cached + from;
27	unsigned long end = start + len;
28
29	start &= ~(CACHELINESIZE - 1);
30	while (start < end) {
31		/* invalidate D cache line */
32		asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start));
33		start += CACHELINESIZE;
34	}
35}
36
37struct pxa2xx_flash_info {
38	struct mtd_info		*mtd;
39	struct map_info		map;
40};
41
42static const char * const probes[] = { "RedBoot", "cmdlinepart", NULL };
43
44static int pxa2xx_flash_probe(struct platform_device *pdev)
45{
46	struct flash_platform_data *flash = dev_get_platdata(&pdev->dev);
47	struct pxa2xx_flash_info *info;
48	struct resource *res;
49
50	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
51	if (!res)
52		return -ENODEV;
53
54	info = kzalloc(sizeof(struct pxa2xx_flash_info), GFP_KERNEL);
55	if (!info)
56		return -ENOMEM;
57
58	info->map.name = flash->name;
59	info->map.bankwidth = flash->width;
60	info->map.phys = res->start;
61	info->map.size = resource_size(res);
62
63	info->map.virt = ioremap(info->map.phys, info->map.size);
64	if (!info->map.virt) {
65		printk(KERN_WARNING "Failed to ioremap %s\n",
66		       info->map.name);
67		kfree(info);
68		return -ENOMEM;
69	}
70	info->map.cached = ioremap_cache(info->map.phys, info->map.size);
71	if (!info->map.cached)
72		printk(KERN_WARNING "Failed to ioremap cached %s\n",
73		       info->map.name);
74	info->map.inval_cache = pxa2xx_map_inval_cache;
75	simple_map_init(&info->map);
76
77	printk(KERN_NOTICE
78	       "Probing %s at physical address 0x%08lx"
79	       " (%d-bit bankwidth)\n",
80	       info->map.name, (unsigned long)info->map.phys,
81	       info->map.bankwidth * 8);
82
83	info->mtd = do_map_probe(flash->map_name, &info->map);
84
85	if (!info->mtd) {
86		iounmap((void *)info->map.virt);
87		if (info->map.cached)
88			iounmap(info->map.cached);
89		kfree(info);
90		return -EIO;
91	}
92	info->mtd->dev.parent = &pdev->dev;
93
94	mtd_device_parse_register(info->mtd, probes, NULL, flash->parts,
95				  flash->nr_parts);
96
97	platform_set_drvdata(pdev, info);
98	return 0;
99}
100
101static void pxa2xx_flash_remove(struct platform_device *dev)
102{
103	struct pxa2xx_flash_info *info = platform_get_drvdata(dev);
104
105	mtd_device_unregister(info->mtd);
106
107	map_destroy(info->mtd);
108	iounmap(info->map.virt);
109	if (info->map.cached)
110		iounmap(info->map.cached);
111	kfree(info);
112}
113
114#ifdef CONFIG_PM
115static void pxa2xx_flash_shutdown(struct platform_device *dev)
116{
117	struct pxa2xx_flash_info *info = platform_get_drvdata(dev);
118
119	if (info && mtd_suspend(info->mtd) == 0)
120		mtd_resume(info->mtd);
121}
122#else
123#define pxa2xx_flash_shutdown NULL
124#endif
125
126static struct platform_driver pxa2xx_flash_driver = {
127	.driver = {
128		.name		= "pxa2xx-flash",
129	},
130	.probe		= pxa2xx_flash_probe,
131	.remove_new	= pxa2xx_flash_remove,
132	.shutdown	= pxa2xx_flash_shutdown,
133};
134
135module_platform_driver(pxa2xx_flash_driver);
136
137MODULE_LICENSE("GPL");
138MODULE_AUTHOR("Nicolas Pitre <nico@fluxnic.net>");
139MODULE_DESCRIPTION("MTD map driver for Intel XScale PXA2xx");
140