2 * Normal mappings of chips in physical memory
4 * Copyright (C) 2003 MontaVista Software Inc.
5 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
7 * 031022 - [jsun] add run-time configure and partition setup
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/map.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/mtd/physmap.h>
21 #include <linux/mtd/concat.h>
24 #define MAX_RESOURCES 4
26 struct physmap_flash_info
{
27 struct mtd_info
*mtd
[MAX_RESOURCES
];
28 struct mtd_info
*cmtd
;
29 struct map_info map
[MAX_RESOURCES
];
32 static int physmap_flash_remove(struct platform_device
*dev
)
34 struct physmap_flash_info
*info
;
35 struct physmap_flash_data
*physmap_data
;
38 info
= platform_get_drvdata(dev
);
41 platform_set_drvdata(dev
, NULL
);
43 physmap_data
= dev
->dev
.platform_data
;
46 mtd_device_unregister(info
->cmtd
);
47 if (info
->cmtd
!= info
->mtd
[0])
48 mtd_concat_destroy(info
->cmtd
);
51 for (i
= 0; i
< MAX_RESOURCES
; i
++) {
52 if (info
->mtd
[i
] != NULL
)
53 map_destroy(info
->mtd
[i
]);
56 if (physmap_data
->exit
)
57 physmap_data
->exit(dev
);
62 static void physmap_set_vpp(struct map_info
*map
, int state
)
64 struct platform_device
*pdev
;
65 struct physmap_flash_data
*physmap_data
;
67 pdev
= (struct platform_device
*)map
->map_priv_1
;
68 physmap_data
= pdev
->dev
.platform_data
;
70 if (physmap_data
->set_vpp
)
71 physmap_data
->set_vpp(pdev
, state
);
74 static const char *rom_probe_types
[] = {
80 static const char *part_probe_types
[] = { "cmdlinepart", "RedBoot", "afs",
83 static int physmap_flash_probe(struct platform_device
*dev
)
85 struct physmap_flash_data
*physmap_data
;
86 struct physmap_flash_info
*info
;
87 const char **probe_type
;
90 int devices_found
= 0;
92 physmap_data
= dev
->dev
.platform_data
;
93 if (physmap_data
== NULL
)
96 info
= devm_kzalloc(&dev
->dev
, sizeof(struct physmap_flash_info
),
103 if (physmap_data
->init
) {
104 err
= physmap_data
->init(dev
);
109 platform_set_drvdata(dev
, info
);
111 for (i
= 0; i
< dev
->num_resources
; i
++) {
112 printk(KERN_NOTICE
"physmap platform flash device: %.8llx at %.8llx\n",
113 (unsigned long long)resource_size(&dev
->resource
[i
]),
114 (unsigned long long)dev
->resource
[i
].start
);
116 if (!devm_request_mem_region(&dev
->dev
,
117 dev
->resource
[i
].start
,
118 resource_size(&dev
->resource
[i
]),
119 dev_name(&dev
->dev
))) {
120 dev_err(&dev
->dev
, "Could not reserve memory region\n");
125 info
->map
[i
].name
= dev_name(&dev
->dev
);
126 info
->map
[i
].phys
= dev
->resource
[i
].start
;
127 info
->map
[i
].size
= resource_size(&dev
->resource
[i
]);
128 info
->map
[i
].bankwidth
= physmap_data
->width
;
129 info
->map
[i
].set_vpp
= physmap_set_vpp
;
130 info
->map
[i
].pfow_base
= physmap_data
->pfow_base
;
131 info
->map
[i
].map_priv_1
= (unsigned long)dev
;
133 info
->map
[i
].virt
= devm_ioremap(&dev
->dev
, info
->map
[i
].phys
,
135 if (info
->map
[i
].virt
== NULL
) {
136 dev_err(&dev
->dev
, "Failed to ioremap flash region\n");
141 simple_map_init(&info
->map
[i
]);
143 probe_type
= rom_probe_types
;
144 if (physmap_data
->probe_type
== NULL
) {
145 for (; info
->mtd
[i
] == NULL
&& *probe_type
!= NULL
; probe_type
++)
146 info
->mtd
[i
] = do_map_probe(*probe_type
, &info
->map
[i
]);
148 info
->mtd
[i
] = do_map_probe(physmap_data
->probe_type
, &info
->map
[i
]);
150 if (info
->mtd
[i
] == NULL
) {
151 dev_err(&dev
->dev
, "map_probe failed\n");
157 info
->mtd
[i
]->owner
= THIS_MODULE
;
158 info
->mtd
[i
]->dev
.parent
= &dev
->dev
;
161 if (devices_found
== 1) {
162 info
->cmtd
= info
->mtd
[0];
163 } else if (devices_found
> 1) {
165 * We detected multiple devices. Concatenate them together.
167 info
->cmtd
= mtd_concat_create(info
->mtd
, devices_found
, dev_name(&dev
->dev
));
168 if (info
->cmtd
== NULL
)
174 mtd_device_parse_register(info
->cmtd
, part_probe_types
, 0,
175 physmap_data
->parts
, physmap_data
->nr_parts
);
179 physmap_flash_remove(dev
);
184 static void physmap_flash_shutdown(struct platform_device
*dev
)
186 struct physmap_flash_info
*info
= platform_get_drvdata(dev
);
189 for (i
= 0; i
< MAX_RESOURCES
&& info
->mtd
[i
]; i
++)
190 if (info
->mtd
[i
]->suspend
&& info
->mtd
[i
]->resume
)
191 if (info
->mtd
[i
]->suspend(info
->mtd
[i
]) == 0)
192 info
->mtd
[i
]->resume(info
->mtd
[i
]);
195 #define physmap_flash_shutdown NULL
198 static struct platform_driver physmap_flash_driver
= {
199 .probe
= physmap_flash_probe
,
200 .remove
= physmap_flash_remove
,
201 .shutdown
= physmap_flash_shutdown
,
203 .name
= "physmap-flash",
204 .owner
= THIS_MODULE
,
209 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
210 static struct physmap_flash_data physmap_flash_data
= {
211 .width
= CONFIG_MTD_PHYSMAP_BANKWIDTH
,
214 static struct resource physmap_flash_resource
= {
215 .start
= CONFIG_MTD_PHYSMAP_START
,
216 .end
= CONFIG_MTD_PHYSMAP_START
+ CONFIG_MTD_PHYSMAP_LEN
- 1,
217 .flags
= IORESOURCE_MEM
,
220 static struct platform_device physmap_flash
= {
221 .name
= "physmap-flash",
224 .platform_data
= &physmap_flash_data
,
227 .resource
= &physmap_flash_resource
,
231 static int __init
physmap_init(void)
235 err
= platform_driver_register(&physmap_flash_driver
);
236 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
238 err
= platform_device_register(&physmap_flash
);
240 platform_driver_unregister(&physmap_flash_driver
);
247 static void __exit
physmap_exit(void)
249 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
250 platform_device_unregister(&physmap_flash
);
252 platform_driver_unregister(&physmap_flash_driver
);
255 module_init(physmap_init
);
256 module_exit(physmap_exit
);
258 MODULE_LICENSE("GPL");
259 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
260 MODULE_DESCRIPTION("Generic configurable MTD map driver");
262 /* legacy platform drivers can't hotplug or coldplg */
263 #ifndef CONFIG_MTD_PHYSMAP_COMPAT
264 /* work with hotplug and coldplug */
265 MODULE_ALIAS("platform:physmap-flash");