xref: /linux/drivers/mtd/maps/pxa2xx-flash.c (revision 7a192ec334cab9fafe3a8665a65af398b0e24730)
1e644f7d6STodd Poynor /*
2e644f7d6STodd Poynor  * Map driver for Intel XScale PXA2xx platforms.
3e644f7d6STodd Poynor  *
4e644f7d6STodd Poynor  * Author:	Nicolas Pitre
5e644f7d6STodd Poynor  * Copyright:	(C) 2001 MontaVista Software Inc.
6e644f7d6STodd Poynor  *
7e644f7d6STodd Poynor  * This program is free software; you can redistribute it and/or modify
8e644f7d6STodd Poynor  * it under the terms of the GNU General Public License version 2 as
9e644f7d6STodd Poynor  * published by the Free Software Foundation.
10e644f7d6STodd Poynor  */
11e644f7d6STodd Poynor 
12e644f7d6STodd Poynor #include <linux/module.h>
13e644f7d6STodd Poynor #include <linux/types.h>
14e644f7d6STodd Poynor #include <linux/kernel.h>
15e644f7d6STodd Poynor #include <linux/init.h>
16e644f7d6STodd Poynor #include <linux/platform_device.h>
17e644f7d6STodd Poynor #include <linux/mtd/mtd.h>
18e644f7d6STodd Poynor #include <linux/mtd/map.h>
19e644f7d6STodd Poynor #include <linux/mtd/partitions.h>
20e644f7d6STodd Poynor 
21e644f7d6STodd Poynor #include <asm/io.h>
22a09e64fbSRussell King #include <mach/hardware.h>
23355aaffdSLinus Torvalds #include <asm/cacheflush.h>
24e644f7d6STodd Poynor 
25e644f7d6STodd Poynor #include <asm/mach/flash.h>
26e644f7d6STodd Poynor 
27e644f7d6STodd Poynor static void pxa2xx_map_inval_cache(struct map_info *map, unsigned long from,
28e644f7d6STodd Poynor 				      ssize_t len)
29e644f7d6STodd Poynor {
30355aaffdSLinus Torvalds 	flush_ioremap_region(map->phys, map->cached, from, len);
31e644f7d6STodd Poynor }
32e644f7d6STodd Poynor 
33e644f7d6STodd Poynor struct pxa2xx_flash_info {
34e644f7d6STodd Poynor 	struct mtd_partition	*parts;
35e644f7d6STodd Poynor 	int			nr_parts;
36e644f7d6STodd Poynor 	struct mtd_info		*mtd;
37e644f7d6STodd Poynor 	struct map_info		map;
38e644f7d6STodd Poynor };
39e644f7d6STodd Poynor 
40e644f7d6STodd Poynor 
41e644f7d6STodd Poynor static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
42e644f7d6STodd Poynor 
43e644f7d6STodd Poynor 
44*7a192ec3SMing Lei static int __init pxa2xx_flash_probe(struct platform_device *pdev)
45e644f7d6STodd Poynor {
46e644f7d6STodd Poynor 	struct flash_platform_data *flash = pdev->dev.platform_data;
47e644f7d6STodd Poynor 	struct pxa2xx_flash_info *info;
48e644f7d6STodd Poynor 	struct mtd_partition *parts;
49e644f7d6STodd Poynor 	struct resource *res;
50e644f7d6STodd Poynor 	int ret = 0;
51e644f7d6STodd Poynor 
52e644f7d6STodd Poynor 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
53e644f7d6STodd Poynor 	if (!res)
54e644f7d6STodd Poynor 		return -ENODEV;
55e644f7d6STodd Poynor 
56e644f7d6STodd Poynor 	info = kmalloc(sizeof(struct pxa2xx_flash_info), GFP_KERNEL);
57e644f7d6STodd Poynor 	if (!info)
58e644f7d6STodd Poynor 		return -ENOMEM;
59e644f7d6STodd Poynor 
60e644f7d6STodd Poynor 	memset(info, 0, sizeof(struct pxa2xx_flash_info));
61e644f7d6STodd Poynor 	info->map.name = (char *) flash->name;
62e644f7d6STodd Poynor 	info->map.bankwidth = flash->width;
63e644f7d6STodd Poynor 	info->map.phys = res->start;
64e644f7d6STodd Poynor 	info->map.size = res->end - res->start + 1;
65e644f7d6STodd Poynor 	info->parts = flash->parts;
66e644f7d6STodd Poynor 	info->nr_parts = flash->nr_parts;
67e644f7d6STodd Poynor 
68e644f7d6STodd Poynor 	info->map.virt = ioremap(info->map.phys, info->map.size);
69e644f7d6STodd Poynor 	if (!info->map.virt) {
70e644f7d6STodd Poynor 		printk(KERN_WARNING "Failed to ioremap %s\n",
71e644f7d6STodd Poynor 		       info->map.name);
72e644f7d6STodd Poynor 		return -ENOMEM;
73e644f7d6STodd Poynor 	}
74e644f7d6STodd Poynor 	info->map.cached =
75e644f7d6STodd Poynor 		ioremap_cached(info->map.phys, info->map.size);
76e644f7d6STodd Poynor 	if (!info->map.cached)
77e644f7d6STodd Poynor 		printk(KERN_WARNING "Failed to ioremap cached %s\n",
78e644f7d6STodd Poynor 		       info->map.name);
79e644f7d6STodd Poynor 	info->map.inval_cache = pxa2xx_map_inval_cache;
80e644f7d6STodd Poynor 	simple_map_init(&info->map);
81e644f7d6STodd Poynor 
82e644f7d6STodd Poynor 	printk(KERN_NOTICE
83e644f7d6STodd Poynor 	       "Probing %s at physical address 0x%08lx"
84e644f7d6STodd Poynor 	       " (%d-bit bankwidth)\n",
85e644f7d6STodd Poynor 	       info->map.name, (unsigned long)info->map.phys,
86e644f7d6STodd Poynor 	       info->map.bankwidth * 8);
87e644f7d6STodd Poynor 
88e644f7d6STodd Poynor 	info->mtd = do_map_probe(flash->map_name, &info->map);
89e644f7d6STodd Poynor 
90e644f7d6STodd Poynor 	if (!info->mtd) {
91e644f7d6STodd Poynor 		iounmap((void *)info->map.virt);
92e644f7d6STodd Poynor 		if (info->map.cached)
93e644f7d6STodd Poynor 			iounmap(info->map.cached);
94e644f7d6STodd Poynor 		return -EIO;
95e644f7d6STodd Poynor 	}
96e644f7d6STodd Poynor 	info->mtd->owner = THIS_MODULE;
97e644f7d6STodd Poynor 
98e644f7d6STodd Poynor #ifdef CONFIG_MTD_PARTITIONS
99e644f7d6STodd Poynor 	ret = parse_mtd_partitions(info->mtd, probes, &parts, 0);
100e644f7d6STodd Poynor 
101e644f7d6STodd Poynor 	if (ret > 0) {
102e644f7d6STodd Poynor 		info->nr_parts = ret;
103e644f7d6STodd Poynor 		info->parts = parts;
104e644f7d6STodd Poynor 	}
105e644f7d6STodd Poynor #endif
106e644f7d6STodd Poynor 
107e644f7d6STodd Poynor 	if (info->nr_parts) {
108e644f7d6STodd Poynor 		add_mtd_partitions(info->mtd, info->parts,
109e644f7d6STodd Poynor 				   info->nr_parts);
110e644f7d6STodd Poynor 	} else {
111e644f7d6STodd Poynor 		printk("Registering %s as whole device\n",
112e644f7d6STodd Poynor 		       info->map.name);
113e644f7d6STodd Poynor 		add_mtd_device(info->mtd);
114e644f7d6STodd Poynor 	}
115e644f7d6STodd Poynor 
116*7a192ec3SMing Lei 	platform_set_drvdata(pdev, info);
117e644f7d6STodd Poynor 	return 0;
118e644f7d6STodd Poynor }
119e644f7d6STodd Poynor 
120*7a192ec3SMing Lei static int __exit pxa2xx_flash_remove(struct platform_device *dev)
121e644f7d6STodd Poynor {
122*7a192ec3SMing Lei 	struct pxa2xx_flash_info *info = platform_get_drvdata(dev);
123e644f7d6STodd Poynor 
124*7a192ec3SMing Lei 	platform_set_drvdata(dev, NULL);
125e644f7d6STodd Poynor 
126e644f7d6STodd Poynor #ifdef CONFIG_MTD_PARTITIONS
127e644f7d6STodd Poynor 	if (info->nr_parts)
128e644f7d6STodd Poynor 		del_mtd_partitions(info->mtd);
129e644f7d6STodd Poynor 	else
130e644f7d6STodd Poynor #endif
131e644f7d6STodd Poynor 		del_mtd_device(info->mtd);
132e644f7d6STodd Poynor 
133e644f7d6STodd Poynor 	map_destroy(info->mtd);
134e644f7d6STodd Poynor 	iounmap(info->map.virt);
135e644f7d6STodd Poynor 	if (info->map.cached)
136e644f7d6STodd Poynor 		iounmap(info->map.cached);
137e644f7d6STodd Poynor 	kfree(info->parts);
138e644f7d6STodd Poynor 	kfree(info);
139e644f7d6STodd Poynor 	return 0;
140e644f7d6STodd Poynor }
141e644f7d6STodd Poynor 
142e644f7d6STodd Poynor #ifdef CONFIG_PM
143*7a192ec3SMing Lei static int pxa2xx_flash_suspend(struct platform_device *dev, pm_message_t state)
144e644f7d6STodd Poynor {
145*7a192ec3SMing Lei 	struct pxa2xx_flash_info *info = platform_get_drvdata(dev);
146e644f7d6STodd Poynor 	int ret = 0;
147e644f7d6STodd Poynor 
148e644f7d6STodd Poynor 	if (info->mtd && info->mtd->suspend)
149e644f7d6STodd Poynor 		ret = info->mtd->suspend(info->mtd);
150e644f7d6STodd Poynor 	return ret;
151e644f7d6STodd Poynor }
152e644f7d6STodd Poynor 
153*7a192ec3SMing Lei static int pxa2xx_flash_resume(struct platform_device *dev)
154e644f7d6STodd Poynor {
155*7a192ec3SMing Lei 	struct pxa2xx_flash_info *info = platform_get_drvdata(dev);
156e644f7d6STodd Poynor 
157e644f7d6STodd Poynor 	if (info->mtd && info->mtd->resume)
158e644f7d6STodd Poynor 		info->mtd->resume(info->mtd);
159e644f7d6STodd Poynor 	return 0;
160e644f7d6STodd Poynor }
161*7a192ec3SMing Lei static void pxa2xx_flash_shutdown(struct platform_device *dev)
162e644f7d6STodd Poynor {
163*7a192ec3SMing Lei 	struct pxa2xx_flash_info *info = platform_get_drvdata(dev);
164e644f7d6STodd Poynor 
165e644f7d6STodd Poynor 	if (info && info->mtd->suspend(info->mtd) == 0)
166e644f7d6STodd Poynor 		info->mtd->resume(info->mtd);
167e644f7d6STodd Poynor }
168e644f7d6STodd Poynor #else
169e644f7d6STodd Poynor #define pxa2xx_flash_suspend NULL
170e644f7d6STodd Poynor #define pxa2xx_flash_resume NULL
171e644f7d6STodd Poynor #define pxa2xx_flash_shutdown NULL
172e644f7d6STodd Poynor #endif
173e644f7d6STodd Poynor 
174*7a192ec3SMing Lei static struct platform_driver pxa2xx_flash_driver = {
175*7a192ec3SMing Lei 	.driver = {
176e644f7d6STodd Poynor 		.name		= "pxa2xx-flash",
177*7a192ec3SMing Lei 		.owner		= THIS_MODULE,
178*7a192ec3SMing Lei 	},
179e644f7d6STodd Poynor 	.probe		= pxa2xx_flash_probe,
180*7a192ec3SMing Lei 	.remove		= __devexit_p(pxa2xx_flash_remove),
181e644f7d6STodd Poynor 	.suspend	= pxa2xx_flash_suspend,
182e644f7d6STodd Poynor 	.resume		= pxa2xx_flash_resume,
183e644f7d6STodd Poynor 	.shutdown	= pxa2xx_flash_shutdown,
184e644f7d6STodd Poynor };
185e644f7d6STodd Poynor 
186e644f7d6STodd Poynor static int __init init_pxa2xx_flash(void)
187e644f7d6STodd Poynor {
188*7a192ec3SMing Lei 	return platform_driver_register(&pxa2xx_flash_driver);
189e644f7d6STodd Poynor }
190e644f7d6STodd Poynor 
191e644f7d6STodd Poynor static void __exit cleanup_pxa2xx_flash(void)
192e644f7d6STodd Poynor {
193*7a192ec3SMing Lei 	platform_driver_unregister(&pxa2xx_flash_driver);
194e644f7d6STodd Poynor }
195e644f7d6STodd Poynor 
196e644f7d6STodd Poynor module_init(init_pxa2xx_flash);
197e644f7d6STodd Poynor module_exit(cleanup_pxa2xx_flash);
198e644f7d6STodd Poynor 
199e644f7d6STodd Poynor MODULE_LICENSE("GPL");
200e644f7d6STodd Poynor MODULE_AUTHOR("Nicolas Pitre <nico@cam.org>");
201e644f7d6STodd Poynor MODULE_DESCRIPTION("MTD map driver for Intel XScale PXA2xx");
202