blob: 5856aa2d99f72670247e7b11bd758c583c9970a5 [file] [log] [blame]
Atsushi Nemoto610f75e2009-03-04 12:01:34 -08001/*
2 * rbtx4939-flash (based on physmap.c)
3 *
4 * This is a simplified physmap driver with map_init callback function.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
11 */
12
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/device.h>
19#include <linux/platform_device.h>
20#include <linux/mtd/mtd.h>
21#include <linux/mtd/map.h>
22#include <linux/mtd/partitions.h>
23#include <asm/txx9/rbtx4939.h>
24
25struct rbtx4939_flash_info {
26 struct mtd_info *mtd;
27 struct map_info map;
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080028};
29
30static int rbtx4939_flash_remove(struct platform_device *dev)
31{
32 struct rbtx4939_flash_info *info;
33
34 info = platform_get_drvdata(dev);
35 if (!info)
36 return 0;
37 platform_set_drvdata(dev, NULL);
38
39 if (info->mtd) {
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080040 struct rbtx4939_flash_data *pdata = dev->dev.platform_data;
41
Jamie Iles16b0eb12011-05-23 10:23:08 +010042 mtd_device_unregister(info->mtd);
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080043 map_destroy(info->mtd);
44 }
45 return 0;
46}
47
48static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080049
50static int rbtx4939_flash_probe(struct platform_device *dev)
51{
52 struct rbtx4939_flash_data *pdata;
53 struct rbtx4939_flash_info *info;
54 struct resource *res;
55 const char **probe_type;
56 int err = 0;
57 unsigned long size;
58
59 pdata = dev->dev.platform_data;
60 if (!pdata)
61 return -ENODEV;
62
63 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
64 if (!res)
65 return -ENODEV;
66 info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info),
67 GFP_KERNEL);
68 if (!info)
69 return -ENOMEM;
70
71 platform_set_drvdata(dev, info);
72
73 size = resource_size(res);
74 pr_notice("rbtx4939 platform flash device: %pR\n", res);
75
76 if (!devm_request_mem_region(&dev->dev, res->start, size,
77 dev_name(&dev->dev)))
78 return -EBUSY;
79
80 info->map.name = dev_name(&dev->dev);
81 info->map.phys = res->start;
82 info->map.size = size;
83 info->map.bankwidth = pdata->width;
84
85 info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size);
86 if (!info->map.virt)
87 return -EBUSY;
88
89 if (pdata->map_init)
90 (*pdata->map_init)(&info->map);
91 else
92 simple_map_init(&info->map);
93
94 probe_type = rom_probe_types;
95 for (; !info->mtd && *probe_type; probe_type++)
96 info->mtd = do_map_probe(*probe_type, &info->map);
97 if (!info->mtd) {
98 dev_err(&dev->dev, "map_probe failed\n");
99 err = -ENXIO;
100 goto err_out;
101 }
102 info->mtd->owner = THIS_MODULE;
103 if (err)
104 goto err_out;
Dmitry Eremin-Solenikovc77d8092011-06-02 18:00:04 +0400105 err = mtd_device_parse_register(info->mtd, NULL, 0,
106 pdata->parts, pdata->nr_parts);
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800107
Dmitry Eremin-Solenikovc77d8092011-06-02 18:00:04 +0400108 if (err)
109 goto err_out;
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800110 return 0;
111
112err_out:
113 rbtx4939_flash_remove(dev);
114 return err;
115}
116
117#ifdef CONFIG_PM
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800118static void rbtx4939_flash_shutdown(struct platform_device *dev)
119{
120 struct rbtx4939_flash_info *info = platform_get_drvdata(dev);
121
122 if (info->mtd->suspend && info->mtd->resume)
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +0200123 if (mtd_suspend(info->mtd) == 0)
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800124 info->mtd->resume(info->mtd);
125}
126#else
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800127#define rbtx4939_flash_shutdown NULL
128#endif
129
130static struct platform_driver rbtx4939_flash_driver = {
131 .probe = rbtx4939_flash_probe,
132 .remove = rbtx4939_flash_remove,
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800133 .shutdown = rbtx4939_flash_shutdown,
134 .driver = {
135 .name = "rbtx4939-flash",
136 .owner = THIS_MODULE,
137 },
138};
139
Axel Linf99640d2011-11-27 20:45:03 +0800140module_platform_driver(rbtx4939_flash_driver);
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800141
142MODULE_LICENSE("GPL");
143MODULE_DESCRIPTION("RBTX4939 MTD map driver");
144MODULE_ALIAS("platform:rbtx4939-flash");