blob: d6301f08906dffd0b769643a55a3281b13f2dcb6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Adrian Bunk2b9175c2005-11-29 14:49:38 +00002 * $Id: physmap.c,v 1.39 2005/11/29 14:49:36 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Normal mappings of chips in physical memory
5 *
6 * Copyright (C) 2003 MontaVista Software Inc.
7 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
8 *
9 * 031022 - [jsun] add run-time configure and partition setup
10 */
11
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/slab.h>
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010017#include <linux/device.h>
18#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mtd/mtd.h>
20#include <linux/mtd/map.h>
21#include <linux/config.h>
22#include <linux/mtd/partitions.h>
Adrian Bunk2b9175c2005-11-29 14:49:38 +000023#include <linux/mtd/physmap.h>
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010024#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010026struct physmap_flash_info {
27 struct mtd_info *mtd;
28 struct map_info map;
29 struct resource *res;
30#ifdef CONFIG_MTD_PARTITIONS
31 int nr_parts;
32 struct mtd_partition *parts;
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010037static int physmap_flash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010039 struct physmap_flash_info *info;
40 struct physmap_flash_data *physmap_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010042 info = platform_get_drvdata(dev);
43 if (info == NULL)
44 return 0;
45 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010047 physmap_data = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010049 if (info->mtd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef CONFIG_MTD_PARTITIONS
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010051 if (info->nr_parts) {
52 del_mtd_partitions(info->mtd);
53 kfree(info->parts);
54 } else if (physmap_data->nr_parts) {
55 del_mtd_partitions(info->mtd);
56 } else {
57 del_mtd_device(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010059#else
60 del_mtd_device(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010062 map_destroy(info->mtd);
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010065 if (info->map.virt != NULL)
66 iounmap((void *)info->map.virt);
67
68 if (info->res != NULL) {
69 release_resource(info->res);
70 kfree(info->res);
71 }
72
73 return 0;
74}
75
76static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
77#ifdef CONFIG_MTD_PARTITIONS
78static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
79#endif
80
81static int physmap_flash_probe(struct platform_device *dev)
82{
83 struct physmap_flash_data *physmap_data;
84 struct physmap_flash_info *info;
85 const char **probe_type;
86 int err;
87
88 physmap_data = dev->dev.platform_data;
89 if (physmap_data == NULL)
90 return -ENODEV;
91
Andrew Mortonf24ff6b2006-06-09 15:12:34 +010092 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
93 (unsigned long long)dev->resource->end - dev->resource->start + 1,
94 (unsigned long long)dev->resource->start);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010095
96 info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
97 if (info == NULL) {
98 err = -ENOMEM;
99 goto err_out;
100 }
101 memset(info, 0, sizeof(*info));
102
103 platform_set_drvdata(dev, info);
104
105 info->res = request_mem_region(dev->resource->start,
106 dev->resource->end - dev->resource->start + 1,
107 dev->dev.bus_id);
108 if (info->res == NULL) {
109 dev_err(&dev->dev, "Could not reserve memory region\n");
110 err = -ENOMEM;
111 goto err_out;
112 }
113
114 info->map.name = dev->dev.bus_id;
115 info->map.phys = dev->resource->start;
116 info->map.size = dev->resource->end - dev->resource->start + 1;
117 info->map.bankwidth = physmap_data->width;
118 info->map.set_vpp = physmap_data->set_vpp;
119
120 info->map.virt = ioremap(info->map.phys, info->map.size);
121 if (info->map.virt == NULL) {
122 dev_err(&dev->dev, "Failed to ioremap flash region\n");
123 err = EIO;
124 goto err_out;
125 }
126
127 simple_map_init(&info->map);
128
129 probe_type = rom_probe_types;
130 for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
131 info->mtd = do_map_probe(*probe_type, &info->map);
132 if (info->mtd == NULL) {
133 dev_err(&dev->dev, "map_probe failed\n");
134 err = -ENXIO;
135 goto err_out;
136 }
137 info->mtd->owner = THIS_MODULE;
138
139#ifdef CONFIG_MTD_PARTITIONS
140 err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
141 if (err > 0) {
142 add_mtd_partitions(info->mtd, info->parts, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
144 }
145
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100146 if (physmap_data->nr_parts) {
147 printk(KERN_NOTICE "Using physmap partition information\n");
148 add_mtd_partitions(info->mtd, physmap_data->parts,
149 physmap_data->nr_parts);
150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100154 add_mtd_device(info->mtd);
155 return 0;
156
157err_out:
158 physmap_flash_remove(dev);
159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100162static struct platform_driver physmap_flash_driver = {
163 .probe = physmap_flash_probe,
164 .remove = physmap_flash_remove,
165 .driver = {
166 .name = "physmap-flash",
167 },
168};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100171#ifdef CONFIG_MTD_PHYSMAP_LEN
172#if CONFIG_MTD_PHYSMAP_LEN != 0
173#warning using PHYSMAP compat code
174#define PHYSMAP_COMPAT
175#endif
176#endif
177
178#ifdef PHYSMAP_COMPAT
179static struct physmap_flash_data physmap_flash_data = {
180 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
181};
182
183static struct resource physmap_flash_resource = {
184 .start = CONFIG_MTD_PHYSMAP_START,
Sascha Hauer6d4f8222006-06-27 14:38:15 +0100185 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100186 .flags = IORESOURCE_MEM,
187};
188
189static struct platform_device physmap_flash = {
190 .name = "physmap-flash",
191 .id = 0,
192 .dev = {
193 .platform_data = &physmap_flash_data,
194 },
195 .num_resources = 1,
196 .resource = &physmap_flash_resource,
197};
198
199void physmap_configure(unsigned long addr, unsigned long size,
200 int bankwidth, void (*set_vpp)(struct map_info *, int))
201{
202 physmap_flash_resource.start = addr;
203 physmap_flash_resource.end = addr + size - 1;
204 physmap_flash_data.width = bankwidth;
205 physmap_flash_data.set_vpp = set_vpp;
206}
207
208#ifdef CONFIG_MTD_PARTITIONS
209void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
210{
211 physmap_flash_data.nr_parts = num_parts;
212 physmap_flash_data.parts = parts;
213}
214#endif
215#endif
216
217static int __init physmap_init(void)
218{
219 int err;
220
221 err = platform_driver_register(&physmap_flash_driver);
222#ifdef PHYSMAP_COMPAT
223 if (err == 0)
224 platform_device_register(&physmap_flash);
225#endif
226
227 return err;
228}
229
230static void __exit physmap_exit(void)
231{
232#ifdef PHYSMAP_COMPAT
233 platform_device_unregister(&physmap_flash);
234#endif
235 platform_driver_unregister(&physmap_flash_driver);
236}
237
238module_init(physmap_init);
239module_exit(physmap_exit);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241MODULE_LICENSE("GPL");
242MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
243MODULE_DESCRIPTION("Generic configurable MTD map driver");