blob: 5d15b6b32265acc63f86c8aa4424fada2101a2eb [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 int nr_parts;
29 struct mtd_partition *parts;
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080030};
31
32static int rbtx4939_flash_remove(struct platform_device *dev)
33{
34 struct rbtx4939_flash_info *info;
35
36 info = platform_get_drvdata(dev);
37 if (!info)
38 return 0;
39 platform_set_drvdata(dev, NULL);
40
41 if (info->mtd) {
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080042 struct rbtx4939_flash_data *pdata = dev->dev.platform_data;
43
Jamie Iles16b0eb12011-05-23 10:23:08 +010044 if (info->nr_parts)
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080045 kfree(info->parts);
Jamie Iles16b0eb12011-05-23 10:23:08 +010046 mtd_device_unregister(info->mtd);
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080047 map_destroy(info->mtd);
48 }
49 return 0;
50}
51
52static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
Atsushi Nemoto610f75e2009-03-04 12:01:34 -080053
54static int rbtx4939_flash_probe(struct platform_device *dev)
55{
56 struct rbtx4939_flash_data *pdata;
57 struct rbtx4939_flash_info *info;
58 struct resource *res;
59 const char **probe_type;
60 int err = 0;
61 unsigned long size;
62
63 pdata = dev->dev.platform_data;
64 if (!pdata)
65 return -ENODEV;
66
67 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
68 if (!res)
69 return -ENODEV;
70 info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info),
71 GFP_KERNEL);
72 if (!info)
73 return -ENOMEM;
74
75 platform_set_drvdata(dev, info);
76
77 size = resource_size(res);
78 pr_notice("rbtx4939 platform flash device: %pR\n", res);
79
80 if (!devm_request_mem_region(&dev->dev, res->start, size,
81 dev_name(&dev->dev)))
82 return -EBUSY;
83
84 info->map.name = dev_name(&dev->dev);
85 info->map.phys = res->start;
86 info->map.size = size;
87 info->map.bankwidth = pdata->width;
88
89 info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size);
90 if (!info->map.virt)
91 return -EBUSY;
92
93 if (pdata->map_init)
94 (*pdata->map_init)(&info->map);
95 else
96 simple_map_init(&info->map);
97
98 probe_type = rom_probe_types;
99 for (; !info->mtd && *probe_type; probe_type++)
100 info->mtd = do_map_probe(*probe_type, &info->map);
101 if (!info->mtd) {
102 dev_err(&dev->dev, "map_probe failed\n");
103 err = -ENXIO;
104 goto err_out;
105 }
106 info->mtd->owner = THIS_MODULE;
107 if (err)
108 goto err_out;
Dmitry Eremin-Solenikov2ef38552011-05-29 20:16:47 +0400109 err = parse_mtd_partitions(info->mtd, NULL, &info->parts, 0);
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800110 if (err > 0) {
Jamie Iles16b0eb12011-05-23 10:23:08 +0100111 mtd_device_register(info->mtd, info->parts, err);
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800112 info->nr_parts = err;
113 return 0;
114 }
115
116 if (pdata->nr_parts) {
117 pr_notice("Using rbtx4939 partition information\n");
Jamie Iles16b0eb12011-05-23 10:23:08 +0100118 mtd_device_register(info->mtd, pdata->parts, pdata->nr_parts);
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800119 return 0;
120 }
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800121
Jamie Iles16b0eb12011-05-23 10:23:08 +0100122 mtd_device_register(info->mtd, NULL, 0);
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800123 return 0;
124
125err_out:
126 rbtx4939_flash_remove(dev);
127 return err;
128}
129
130#ifdef CONFIG_PM
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800131static void rbtx4939_flash_shutdown(struct platform_device *dev)
132{
133 struct rbtx4939_flash_info *info = platform_get_drvdata(dev);
134
135 if (info->mtd->suspend && info->mtd->resume)
136 if (info->mtd->suspend(info->mtd) == 0)
137 info->mtd->resume(info->mtd);
138}
139#else
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800140#define rbtx4939_flash_shutdown NULL
141#endif
142
143static struct platform_driver rbtx4939_flash_driver = {
144 .probe = rbtx4939_flash_probe,
145 .remove = rbtx4939_flash_remove,
Atsushi Nemoto610f75e2009-03-04 12:01:34 -0800146 .shutdown = rbtx4939_flash_shutdown,
147 .driver = {
148 .name = "rbtx4939-flash",
149 .owner = THIS_MODULE,
150 },
151};
152
153static int __init rbtx4939_flash_init(void)
154{
155 return platform_driver_register(&rbtx4939_flash_driver);
156}
157
158static void __exit rbtx4939_flash_exit(void)
159{
160 platform_driver_unregister(&rbtx4939_flash_driver);
161}
162
163module_init(rbtx4939_flash_init);
164module_exit(rbtx4939_flash_exit);
165
166MODULE_LICENSE("GPL");
167MODULE_DESCRIPTION("RBTX4939 MTD map driver");
168MODULE_ALIAS("platform:rbtx4939-flash");