Commit | Line | Data |
---|---|---|
610f75e7 AN |
1 | /* |
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 | ||
25 | struct rbtx4939_flash_info { | |
26 | struct mtd_info *mtd; | |
27 | struct map_info map; | |
610f75e7 AN |
28 | int nr_parts; |
29 | struct mtd_partition *parts; | |
610f75e7 AN |
30 | }; |
31 | ||
32 | static 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) { | |
610f75e7 AN |
42 | struct rbtx4939_flash_data *pdata = dev->dev.platform_data; |
43 | ||
16b0eb15 | 44 | if (info->nr_parts) |
610f75e7 | 45 | kfree(info->parts); |
16b0eb15 | 46 | mtd_device_unregister(info->mtd); |
610f75e7 AN |
47 | map_destroy(info->mtd); |
48 | } | |
49 | return 0; | |
50 | } | |
51 | ||
52 | static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL }; | |
610f75e7 | 53 | static const char *part_probe_types[] = { "cmdlinepart", NULL }; |
610f75e7 AN |
54 | |
55 | static int rbtx4939_flash_probe(struct platform_device *dev) | |
56 | { | |
57 | struct rbtx4939_flash_data *pdata; | |
58 | struct rbtx4939_flash_info *info; | |
59 | struct resource *res; | |
60 | const char **probe_type; | |
61 | int err = 0; | |
62 | unsigned long size; | |
63 | ||
64 | pdata = dev->dev.platform_data; | |
65 | if (!pdata) | |
66 | return -ENODEV; | |
67 | ||
68 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); | |
69 | if (!res) | |
70 | return -ENODEV; | |
71 | info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info), | |
72 | GFP_KERNEL); | |
73 | if (!info) | |
74 | return -ENOMEM; | |
75 | ||
76 | platform_set_drvdata(dev, info); | |
77 | ||
78 | size = resource_size(res); | |
79 | pr_notice("rbtx4939 platform flash device: %pR\n", res); | |
80 | ||
81 | if (!devm_request_mem_region(&dev->dev, res->start, size, | |
82 | dev_name(&dev->dev))) | |
83 | return -EBUSY; | |
84 | ||
85 | info->map.name = dev_name(&dev->dev); | |
86 | info->map.phys = res->start; | |
87 | info->map.size = size; | |
88 | info->map.bankwidth = pdata->width; | |
89 | ||
90 | info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size); | |
91 | if (!info->map.virt) | |
92 | return -EBUSY; | |
93 | ||
94 | if (pdata->map_init) | |
95 | (*pdata->map_init)(&info->map); | |
96 | else | |
97 | simple_map_init(&info->map); | |
98 | ||
99 | probe_type = rom_probe_types; | |
100 | for (; !info->mtd && *probe_type; probe_type++) | |
101 | info->mtd = do_map_probe(*probe_type, &info->map); | |
102 | if (!info->mtd) { | |
103 | dev_err(&dev->dev, "map_probe failed\n"); | |
104 | err = -ENXIO; | |
105 | goto err_out; | |
106 | } | |
107 | info->mtd->owner = THIS_MODULE; | |
108 | if (err) | |
109 | goto err_out; | |
110 | ||
610f75e7 AN |
111 | err = parse_mtd_partitions(info->mtd, part_probe_types, |
112 | &info->parts, 0); | |
113 | if (err > 0) { | |
16b0eb15 | 114 | mtd_device_register(info->mtd, info->parts, err); |
610f75e7 AN |
115 | info->nr_parts = err; |
116 | return 0; | |
117 | } | |
118 | ||
119 | if (pdata->nr_parts) { | |
120 | pr_notice("Using rbtx4939 partition information\n"); | |
16b0eb15 | 121 | mtd_device_register(info->mtd, pdata->parts, pdata->nr_parts); |
610f75e7 AN |
122 | return 0; |
123 | } | |
610f75e7 | 124 | |
16b0eb15 | 125 | mtd_device_register(info->mtd, NULL, 0); |
610f75e7 AN |
126 | return 0; |
127 | ||
128 | err_out: | |
129 | rbtx4939_flash_remove(dev); | |
130 | return err; | |
131 | } | |
132 | ||
133 | #ifdef CONFIG_PM | |
610f75e7 AN |
134 | static void rbtx4939_flash_shutdown(struct platform_device *dev) |
135 | { | |
136 | struct rbtx4939_flash_info *info = platform_get_drvdata(dev); | |
137 | ||
138 | if (info->mtd->suspend && info->mtd->resume) | |
139 | if (info->mtd->suspend(info->mtd) == 0) | |
140 | info->mtd->resume(info->mtd); | |
141 | } | |
142 | #else | |
610f75e7 AN |
143 | #define rbtx4939_flash_shutdown NULL |
144 | #endif | |
145 | ||
146 | static struct platform_driver rbtx4939_flash_driver = { | |
147 | .probe = rbtx4939_flash_probe, | |
148 | .remove = rbtx4939_flash_remove, | |
610f75e7 AN |
149 | .shutdown = rbtx4939_flash_shutdown, |
150 | .driver = { | |
151 | .name = "rbtx4939-flash", | |
152 | .owner = THIS_MODULE, | |
153 | }, | |
154 | }; | |
155 | ||
156 | static int __init rbtx4939_flash_init(void) | |
157 | { | |
158 | return platform_driver_register(&rbtx4939_flash_driver); | |
159 | } | |
160 | ||
161 | static void __exit rbtx4939_flash_exit(void) | |
162 | { | |
163 | platform_driver_unregister(&rbtx4939_flash_driver); | |
164 | } | |
165 | ||
166 | module_init(rbtx4939_flash_init); | |
167 | module_exit(rbtx4939_flash_exit); | |
168 | ||
169 | MODULE_LICENSE("GPL"); | |
170 | MODULE_DESCRIPTION("RBTX4939 MTD map driver"); | |
171 | MODULE_ALIAS("platform:rbtx4939-flash"); |