blob: abc562653b31777d4ea6d4217335d14b64ec8b15 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Normal mappings of chips in physical memory
3 *
4 * Copyright (C) 2003 MontaVista Software Inc.
5 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
6 *
7 * 031022 - [jsun] add run-time configure and partition setup
8 */
9
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>
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010015#include <linux/device.h>
16#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mtd/mtd.h>
18#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mtd/partitions.h>
Adrian Bunk2b9175c2005-11-29 14:49:38 +000020#include <linux/mtd/physmap.h>
Stefan Roesedf66e712008-02-01 15:26:54 +010021#include <linux/mtd/concat.h>
Atsushi Nemoto3136e902008-11-26 10:26:29 +000022#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Stefan Roesedf66e712008-02-01 15:26:54 +010024#define MAX_RESOURCES 4
25
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010026struct physmap_flash_info {
Stefan Roesedf66e712008-02-01 15:26:54 +010027 struct mtd_info *mtd[MAX_RESOURCES];
28 struct mtd_info *cmtd;
29 struct map_info map[MAX_RESOURCES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010032static int physmap_flash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010034 struct physmap_flash_info *info;
35 struct physmap_flash_data *physmap_data;
Stefan Roesedf66e712008-02-01 15:26:54 +010036 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010038 info = platform_get_drvdata(dev);
39 if (info == NULL)
40 return 0;
41 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010043 physmap_data = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
H Hartley Sweeten8ce110a2009-10-20 12:23:33 -040045 if (info->cmtd) {
Jamie Iles984e6d82011-05-23 10:22:45 +010046 mtd_device_unregister(info->cmtd);
H Hartley Sweeten8ce110a2009-10-20 12:23:33 -040047 if (info->cmtd != info->mtd[0])
48 mtd_concat_destroy(info->cmtd);
H Hartley Sweeten8ce110a2009-10-20 12:23:33 -040049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stefan Roesedf66e712008-02-01 15:26:54 +010051 for (i = 0; i < MAX_RESOURCES; i++) {
Atsushi Nemotoe4808142009-02-11 13:12:17 -080052 if (info->mtd[i] != NULL)
Stefan Roesedf66e712008-02-01 15:26:54 +010053 map_destroy(info->mtd[i]);
Stefan Roesedf66e712008-02-01 15:26:54 +010054 }
Marc Zyngierb7281ca2011-05-18 10:51:48 +010055
56 if (physmap_data->exit)
57 physmap_data->exit(dev);
58
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010059 return 0;
60}
61
Marc Zyngier667f3902011-05-18 10:51:55 +010062static void physmap_set_vpp(struct map_info *map, int state)
63{
64 struct platform_device *pdev;
65 struct physmap_flash_data *physmap_data;
66
67 pdev = (struct platform_device *)map->map_priv_1;
68 physmap_data = pdev->dev.platform_data;
69
70 if (physmap_data->set_vpp)
71 physmap_data->set_vpp(pdev, state);
72}
73
Alexey Korolevd8140832008-12-16 18:22:39 +000074static const char *rom_probe_types[] = {
75 "cfi_probe",
76 "jedec_probe",
77 "qinfo_probe",
78 "map_rom",
79 NULL };
Marc Zyngier667f3902011-05-18 10:51:55 +010080static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", "afs",
Marc Zyngierb7281ca2011-05-18 10:51:48 +010081 NULL };
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010082
83static int physmap_flash_probe(struct platform_device *dev)
84{
85 struct physmap_flash_data *physmap_data;
86 struct physmap_flash_info *info;
87 const char **probe_type;
Jonas Gorski529688f2011-12-05 16:08:09 +010088 const char **part_types;
Stefan Roesedf66e712008-02-01 15:26:54 +010089 int err = 0;
90 int i;
91 int devices_found = 0;
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010092
93 physmap_data = dev->dev.platform_data;
94 if (physmap_data == NULL)
95 return -ENODEV;
96
Atsushi Nemoto3136e902008-11-26 10:26:29 +000097 info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
98 GFP_KERNEL);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010099 if (info == NULL) {
100 err = -ENOMEM;
101 goto err_out;
102 }
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100103
Marc Zyngierb7281ca2011-05-18 10:51:48 +0100104 if (physmap_data->init) {
105 err = physmap_data->init(dev);
106 if (err)
107 goto err_out;
108 }
109
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100110 platform_set_drvdata(dev, info);
111
Stefan Roesedf66e712008-02-01 15:26:54 +0100112 for (i = 0; i < dev->num_resources; i++) {
113 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
H Hartley Sweeten1d90d2c2010-06-14 11:57:54 -0500114 (unsigned long long)resource_size(&dev->resource[i]),
Stefan Roesedf66e712008-02-01 15:26:54 +0100115 (unsigned long long)dev->resource[i].start);
116
Atsushi Nemoto3136e902008-11-26 10:26:29 +0000117 if (!devm_request_mem_region(&dev->dev,
118 dev->resource[i].start,
H Hartley Sweeten1d90d2c2010-06-14 11:57:54 -0500119 resource_size(&dev->resource[i]),
Kay Sievers160bbab2008-12-23 10:00:14 +0000120 dev_name(&dev->dev))) {
Stefan Roesedf66e712008-02-01 15:26:54 +0100121 dev_err(&dev->dev, "Could not reserve memory region\n");
122 err = -ENOMEM;
123 goto err_out;
124 }
125
Kay Sievers160bbab2008-12-23 10:00:14 +0000126 info->map[i].name = dev_name(&dev->dev);
Stefan Roesedf66e712008-02-01 15:26:54 +0100127 info->map[i].phys = dev->resource[i].start;
H Hartley Sweeten1d90d2c2010-06-14 11:57:54 -0500128 info->map[i].size = resource_size(&dev->resource[i]);
Stefan Roesedf66e712008-02-01 15:26:54 +0100129 info->map[i].bankwidth = physmap_data->width;
Marc Zyngier667f3902011-05-18 10:51:55 +0100130 info->map[i].set_vpp = physmap_set_vpp;
Alexey Korolevd8140832008-12-16 18:22:39 +0000131 info->map[i].pfow_base = physmap_data->pfow_base;
Marc Zyngier667f3902011-05-18 10:51:55 +0100132 info->map[i].map_priv_1 = (unsigned long)dev;
Stefan Roesedf66e712008-02-01 15:26:54 +0100133
Atsushi Nemoto3136e902008-11-26 10:26:29 +0000134 info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
135 info->map[i].size);
Stefan Roesedf66e712008-02-01 15:26:54 +0100136 if (info->map[i].virt == NULL) {
137 dev_err(&dev->dev, "Failed to ioremap flash region\n");
Roel Kluin895fb492009-11-11 21:47:06 +0100138 err = -EIO;
Stefan Roesedf66e712008-02-01 15:26:54 +0100139 goto err_out;
140 }
141
142 simple_map_init(&info->map[i]);
143
144 probe_type = rom_probe_types;
Barry Song78ef7fa2010-01-15 15:50:14 +0800145 if (physmap_data->probe_type == NULL) {
146 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
147 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
148 } else
149 info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
150
Stefan Roesedf66e712008-02-01 15:26:54 +0100151 if (info->mtd[i] == NULL) {
152 dev_err(&dev->dev, "map_probe failed\n");
153 err = -ENXIO;
154 goto err_out;
155 } else {
156 devices_found++;
157 }
158 info->mtd[i]->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -0700159 info->mtd[i]->dev.parent = &dev->dev;
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100160 }
161
Stefan Roesedf66e712008-02-01 15:26:54 +0100162 if (devices_found == 1) {
163 info->cmtd = info->mtd[0];
164 } else if (devices_found > 1) {
165 /*
166 * We detected multiple devices. Concatenate them together.
167 */
Kay Sievers160bbab2008-12-23 10:00:14 +0000168 info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
Stefan Roesedf66e712008-02-01 15:26:54 +0100169 if (info->cmtd == NULL)
170 err = -ENXIO;
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100171 }
Stefan Roesedf66e712008-02-01 15:26:54 +0100172 if (err)
173 goto err_out;
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100174
Jonas Gorski529688f2011-12-05 16:08:09 +0100175 part_types = physmap_data->part_probe_types ? : part_probe_types;
176
177 mtd_device_parse_register(info->cmtd, part_types, 0,
Dmitry Eremin-Solenikovd45fd122011-06-02 17:59:58 +0400178 physmap_data->parts, physmap_data->nr_parts);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100179 return 0;
180
181err_out:
182 physmap_flash_remove(dev);
183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200186#ifdef CONFIG_PM
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200187static void physmap_flash_shutdown(struct platform_device *dev)
188{
189 struct physmap_flash_info *info = platform_get_drvdata(dev);
Stefan Roesedf66e712008-02-01 15:26:54 +0100190 int i;
191
Anton Vorontsov4a5691c2008-03-28 14:16:09 -0700192 for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
Artem Bityutskiy079c9852011-12-30 17:15:59 +0200193 if (mtd_suspend(info->mtd[i]) == 0)
194 mtd_resume(info->mtd[i]);
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200195}
akpm@linux-foundation.orgd5476682008-02-03 12:56:03 -0800196#else
akpm@linux-foundation.orgd5476682008-02-03 12:56:03 -0800197#define physmap_flash_shutdown NULL
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200198#endif
199
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100200static struct platform_driver physmap_flash_driver = {
201 .probe = physmap_flash_probe,
202 .remove = physmap_flash_remove,
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200203 .shutdown = physmap_flash_shutdown,
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100204 .driver = {
205 .name = "physmap-flash",
Kay Sievers41d867c2008-04-18 13:44:26 -0700206 .owner = THIS_MODULE,
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100207 },
208};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210
Mike Frysingerdcb3e132008-12-01 14:23:40 -0800211#ifdef CONFIG_MTD_PHYSMAP_COMPAT
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100212static struct physmap_flash_data physmap_flash_data = {
213 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
214};
215
216static struct resource physmap_flash_resource = {
217 .start = CONFIG_MTD_PHYSMAP_START,
Sascha Hauer6d4f8222006-06-27 14:38:15 +0100218 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100219 .flags = IORESOURCE_MEM,
220};
221
222static struct platform_device physmap_flash = {
223 .name = "physmap-flash",
224 .id = 0,
225 .dev = {
226 .platform_data = &physmap_flash_data,
227 },
228 .num_resources = 1,
229 .resource = &physmap_flash_resource,
230};
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100231#endif
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100232
233static int __init physmap_init(void)
234{
235 int err;
236
237 err = platform_driver_register(&physmap_flash_driver);
Mike Frysingerdcb3e132008-12-01 14:23:40 -0800238#ifdef CONFIG_MTD_PHYSMAP_COMPAT
H Hartley Sweeten1ca5d2f2010-04-02 17:46:30 -0500239 if (err == 0) {
240 err = platform_device_register(&physmap_flash);
241 if (err)
242 platform_driver_unregister(&physmap_flash_driver);
243 }
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100244#endif
245
246 return err;
247}
248
249static void __exit physmap_exit(void)
250{
Mike Frysingerdcb3e132008-12-01 14:23:40 -0800251#ifdef CONFIG_MTD_PHYSMAP_COMPAT
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100252 platform_device_unregister(&physmap_flash);
253#endif
254 platform_driver_unregister(&physmap_flash_driver);
255}
256
257module_init(physmap_init);
258module_exit(physmap_exit);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260MODULE_LICENSE("GPL");
261MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
262MODULE_DESCRIPTION("Generic configurable MTD map driver");
Kay Sievers41d867c2008-04-18 13:44:26 -0700263
264/* legacy platform drivers can't hotplug or coldplg */
Mike Frysingerdcb3e132008-12-01 14:23:40 -0800265#ifndef CONFIG_MTD_PHYSMAP_COMPAT
Kay Sievers41d867c2008-04-18 13:44:26 -0700266/* work with hotplug and coldplug */
267MODULE_ALIAS("platform:physmap-flash");
268#endif