blob: 7799a25a7f2ae9c55242b6e83682e19b3981723c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mtd/partitions.h>
Adrian Bunk2b9175c2005-11-29 14:49:38 +000022#include <linux/mtd/physmap.h>
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010023#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010025struct physmap_flash_info {
26 struct mtd_info *mtd;
27 struct map_info map;
28 struct resource *res;
29#ifdef CONFIG_MTD_PARTITIONS
30 int nr_parts;
31 struct mtd_partition *parts;
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010036static int physmap_flash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010038 struct physmap_flash_info *info;
39 struct physmap_flash_data *physmap_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010041 info = platform_get_drvdata(dev);
42 if (info == NULL)
43 return 0;
44 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010046 physmap_data = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010048 if (info->mtd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef CONFIG_MTD_PARTITIONS
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010050 if (info->nr_parts) {
51 del_mtd_partitions(info->mtd);
52 kfree(info->parts);
53 } else if (physmap_data->nr_parts) {
54 del_mtd_partitions(info->mtd);
55 } else {
56 del_mtd_device(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010058#else
59 del_mtd_device(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010061 map_destroy(info->mtd);
62 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010064 if (info->map.virt != NULL)
65 iounmap((void *)info->map.virt);
66
67 if (info->res != NULL) {
68 release_resource(info->res);
69 kfree(info->res);
70 }
71
72 return 0;
73}
74
75static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
76#ifdef CONFIG_MTD_PARTITIONS
77static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
78#endif
79
80static int physmap_flash_probe(struct platform_device *dev)
81{
82 struct physmap_flash_data *physmap_data;
83 struct physmap_flash_info *info;
84 const char **probe_type;
85 int err;
86
87 physmap_data = dev->dev.platform_data;
88 if (physmap_data == NULL)
89 return -ENODEV;
90
Andrew Mortonf24ff6b2006-06-09 15:12:34 +010091 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
92 (unsigned long long)dev->resource->end - dev->resource->start + 1,
93 (unsigned long long)dev->resource->start);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010094
95 info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
96 if (info == NULL) {
97 err = -ENOMEM;
98 goto err_out;
99 }
100 memset(info, 0, sizeof(*info));
101
102 platform_set_drvdata(dev, info);
103
104 info->res = request_mem_region(dev->resource->start,
105 dev->resource->end - dev->resource->start + 1,
106 dev->dev.bus_id);
107 if (info->res == NULL) {
108 dev_err(&dev->dev, "Could not reserve memory region\n");
109 err = -ENOMEM;
110 goto err_out;
111 }
112
113 info->map.name = dev->dev.bus_id;
114 info->map.phys = dev->resource->start;
115 info->map.size = dev->resource->end - dev->resource->start + 1;
116 info->map.bankwidth = physmap_data->width;
117 info->map.set_vpp = physmap_data->set_vpp;
118
119 info->map.virt = ioremap(info->map.phys, info->map.size);
120 if (info->map.virt == NULL) {
121 dev_err(&dev->dev, "Failed to ioremap flash region\n");
122 err = EIO;
123 goto err_out;
124 }
125
126 simple_map_init(&info->map);
127
128 probe_type = rom_probe_types;
129 for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
130 info->mtd = do_map_probe(*probe_type, &info->map);
131 if (info->mtd == NULL) {
132 dev_err(&dev->dev, "map_probe failed\n");
133 err = -ENXIO;
134 goto err_out;
135 }
136 info->mtd->owner = THIS_MODULE;
137
138#ifdef CONFIG_MTD_PARTITIONS
139 err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
140 if (err > 0) {
141 add_mtd_partitions(info->mtd, info->parts, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return 0;
143 }
144
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100145 if (physmap_data->nr_parts) {
146 printk(KERN_NOTICE "Using physmap partition information\n");
147 add_mtd_partitions(info->mtd, physmap_data->parts,
148 physmap_data->nr_parts);
149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100153 add_mtd_device(info->mtd);
154 return 0;
155
156err_out:
157 physmap_flash_remove(dev);
158 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100161static struct platform_driver physmap_flash_driver = {
162 .probe = physmap_flash_probe,
163 .remove = physmap_flash_remove,
164 .driver = {
165 .name = "physmap-flash",
166 },
167};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100170#ifdef CONFIG_MTD_PHYSMAP_LEN
171#if CONFIG_MTD_PHYSMAP_LEN != 0
172#warning using PHYSMAP compat code
173#define PHYSMAP_COMPAT
174#endif
175#endif
176
177#ifdef PHYSMAP_COMPAT
178static struct physmap_flash_data physmap_flash_data = {
179 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
180};
181
182static struct resource physmap_flash_resource = {
183 .start = CONFIG_MTD_PHYSMAP_START,
Sascha Hauer6d4f8222006-06-27 14:38:15 +0100184 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100185 .flags = IORESOURCE_MEM,
186};
187
188static struct platform_device physmap_flash = {
189 .name = "physmap-flash",
190 .id = 0,
191 .dev = {
192 .platform_data = &physmap_flash_data,
193 },
194 .num_resources = 1,
195 .resource = &physmap_flash_resource,
196};
197
198void physmap_configure(unsigned long addr, unsigned long size,
199 int bankwidth, void (*set_vpp)(struct map_info *, int))
200{
201 physmap_flash_resource.start = addr;
202 physmap_flash_resource.end = addr + size - 1;
203 physmap_flash_data.width = bankwidth;
204 physmap_flash_data.set_vpp = set_vpp;
205}
206
207#ifdef CONFIG_MTD_PARTITIONS
208void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
209{
210 physmap_flash_data.nr_parts = num_parts;
211 physmap_flash_data.parts = parts;
212}
213#endif
214#endif
215
216static int __init physmap_init(void)
217{
218 int err;
219
220 err = platform_driver_register(&physmap_flash_driver);
221#ifdef PHYSMAP_COMPAT
222 if (err == 0)
223 platform_device_register(&physmap_flash);
224#endif
225
226 return err;
227}
228
229static void __exit physmap_exit(void)
230{
231#ifdef PHYSMAP_COMPAT
232 platform_device_unregister(&physmap_flash);
233#endif
234 platform_driver_unregister(&physmap_flash_driver);
235}
236
237module_init(physmap_init);
238module_exit(physmap_exit);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240MODULE_LICENSE("GPL");
241MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
242MODULE_DESCRIPTION("Generic configurable MTD map driver");