blob: 76ce9bd943aad13b4fecd67b05808210e721659c [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>
25#include <asm/mach/flash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010027struct physmap_flash_info {
28 struct mtd_info *mtd;
29 struct map_info map;
30 struct resource *res;
31#ifdef CONFIG_MTD_PARTITIONS
32 int nr_parts;
33 struct mtd_partition *parts;
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010038static int physmap_flash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010040 struct physmap_flash_info *info;
41 struct physmap_flash_data *physmap_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010043 info = platform_get_drvdata(dev);
44 if (info == NULL)
45 return 0;
46 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010048 physmap_data = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010050 if (info->mtd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_MTD_PARTITIONS
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010052 if (info->nr_parts) {
53 del_mtd_partitions(info->mtd);
54 kfree(info->parts);
55 } else if (physmap_data->nr_parts) {
56 del_mtd_partitions(info->mtd);
57 } else {
58 del_mtd_device(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010060#else
61 del_mtd_device(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010063 map_destroy(info->mtd);
64 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010066 if (info->map.virt != NULL)
67 iounmap((void *)info->map.virt);
68
69 if (info->res != NULL) {
70 release_resource(info->res);
71 kfree(info->res);
72 }
73
74 return 0;
75}
76
77static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
78#ifdef CONFIG_MTD_PARTITIONS
79static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
80#endif
81
82static int physmap_flash_probe(struct platform_device *dev)
83{
84 struct physmap_flash_data *physmap_data;
85 struct physmap_flash_info *info;
86 const char **probe_type;
87 int err;
88
89 physmap_data = dev->dev.platform_data;
90 if (physmap_data == NULL)
91 return -ENODEV;
92
93 printk(KERN_NOTICE "physmap platform flash device: %.8lx at %.8lx\n",
94 dev->resource->end - dev->resource->start + 1,
95 dev->resource->start);
96
97 info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
98 if (info == NULL) {
99 err = -ENOMEM;
100 goto err_out;
101 }
102 memset(info, 0, sizeof(*info));
103
104 platform_set_drvdata(dev, info);
105
106 info->res = request_mem_region(dev->resource->start,
107 dev->resource->end - dev->resource->start + 1,
108 dev->dev.bus_id);
109 if (info->res == NULL) {
110 dev_err(&dev->dev, "Could not reserve memory region\n");
111 err = -ENOMEM;
112 goto err_out;
113 }
114
115 info->map.name = dev->dev.bus_id;
116 info->map.phys = dev->resource->start;
117 info->map.size = dev->resource->end - dev->resource->start + 1;
118 info->map.bankwidth = physmap_data->width;
119 info->map.set_vpp = physmap_data->set_vpp;
120
121 info->map.virt = ioremap(info->map.phys, info->map.size);
122 if (info->map.virt == NULL) {
123 dev_err(&dev->dev, "Failed to ioremap flash region\n");
124 err = EIO;
125 goto err_out;
126 }
127
128 simple_map_init(&info->map);
129
130 probe_type = rom_probe_types;
131 for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
132 info->mtd = do_map_probe(*probe_type, &info->map);
133 if (info->mtd == NULL) {
134 dev_err(&dev->dev, "map_probe failed\n");
135 err = -ENXIO;
136 goto err_out;
137 }
138 info->mtd->owner = THIS_MODULE;
139
140#ifdef CONFIG_MTD_PARTITIONS
141 err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
142 if (err > 0) {
143 add_mtd_partitions(info->mtd, info->parts, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return 0;
145 }
146
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100147 if (physmap_data->nr_parts) {
148 printk(KERN_NOTICE "Using physmap partition information\n");
149 add_mtd_partitions(info->mtd, physmap_data->parts,
150 physmap_data->nr_parts);
151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100155 add_mtd_device(info->mtd);
156 return 0;
157
158err_out:
159 physmap_flash_remove(dev);
160 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100163static struct platform_driver physmap_flash_driver = {
164 .probe = physmap_flash_probe,
165 .remove = physmap_flash_remove,
166 .driver = {
167 .name = "physmap-flash",
168 },
169};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100172#ifdef CONFIG_MTD_PHYSMAP_LEN
173#if CONFIG_MTD_PHYSMAP_LEN != 0
174#warning using PHYSMAP compat code
175#define PHYSMAP_COMPAT
176#endif
177#endif
178
179#ifdef PHYSMAP_COMPAT
180static struct physmap_flash_data physmap_flash_data = {
181 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
182};
183
184static struct resource physmap_flash_resource = {
185 .start = CONFIG_MTD_PHYSMAP_START,
186 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN,
187 .flags = IORESOURCE_MEM,
188};
189
190static struct platform_device physmap_flash = {
191 .name = "physmap-flash",
192 .id = 0,
193 .dev = {
194 .platform_data = &physmap_flash_data,
195 },
196 .num_resources = 1,
197 .resource = &physmap_flash_resource,
198};
199
200void physmap_configure(unsigned long addr, unsigned long size,
201 int bankwidth, void (*set_vpp)(struct map_info *, int))
202{
203 physmap_flash_resource.start = addr;
204 physmap_flash_resource.end = addr + size - 1;
205 physmap_flash_data.width = bankwidth;
206 physmap_flash_data.set_vpp = set_vpp;
207}
208
209#ifdef CONFIG_MTD_PARTITIONS
210void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
211{
212 physmap_flash_data.nr_parts = num_parts;
213 physmap_flash_data.parts = parts;
214}
215#endif
216#endif
217
218static int __init physmap_init(void)
219{
220 int err;
221
222 err = platform_driver_register(&physmap_flash_driver);
223#ifdef PHYSMAP_COMPAT
224 if (err == 0)
225 platform_device_register(&physmap_flash);
226#endif
227
228 return err;
229}
230
231static void __exit physmap_exit(void)
232{
233#ifdef PHYSMAP_COMPAT
234 platform_device_unregister(&physmap_flash);
235#endif
236 platform_driver_unregister(&physmap_flash_driver);
237}
238
239module_init(physmap_init);
240module_exit(physmap_exit);
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242MODULE_LICENSE("GPL");
243MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
244MODULE_DESCRIPTION("Generic configurable MTD map driver");