summaryrefslogtreecommitdiffstats
path: root/target/linux/ar71xx/files/drivers/mtd/maps/ar91xx_flash.c
blob: 51ba7f578001daf3ddcd203d2ce9aba05fcad98e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Parallel flash driver for the Atheros AR91xx SoC
 *
 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
#include <linux/io.h>

#include <asm/mach-ar71xx/ar71xx.h>
#include <asm/mach-ar71xx/ar91xx_flash.h>

#define DRV_NAME	"ar91xx-flash"

struct ar91xx_flash_info {
	struct mtd_info		*mtd;
	struct map_info		map;
#ifdef CONFIG_MTD_PARTITIONS
	int			nr_parts;
	struct mtd_partition	*parts;
#endif
};

static map_word ar91xx_flash_read(struct map_info *map, unsigned long ofs)
{
	map_word val;

	if (map_bankwidth_is_1(map))
		val.x[0] = __raw_readb(map->virt + (ofs ^ 3));
	else if (map_bankwidth_is_2(map))
		val.x[0] = __raw_readw(map->virt + (ofs ^ 2));
	else
		val = map_word_ff(map);

	return val;
}

static void ar91xx_flash_write(struct map_info *map, map_word d,
			       unsigned long ofs)
{
	if (map_bankwidth_is_1(map))
		__raw_writeb(d.x[0], map->virt + (ofs ^ 3));
	else if (map_bankwidth_is_2(map))
		__raw_writew(d.x[0], map->virt + (ofs ^ 2));

	mb();
}

static map_word ar91xx_flash_read_lock(struct map_info *map, unsigned long ofs)
{
	map_word ret;

	ar71xx_flash_acquire();
	ret = ar91xx_flash_read(map, ofs);
	ar71xx_flash_release();

	return ret;
}

static void ar91xx_flash_write_lock(struct map_info *map, map_word d,
			       unsigned long ofs)
{
	ar71xx_flash_acquire();
	ar91xx_flash_write(map, d, ofs);
	ar71xx_flash_release();
}

static void ar91xx_flash_copy_from_lock(struct map_info *map, void *to,
					unsigned long from, ssize_t len)
{
	ar71xx_flash_acquire();
	inline_map_copy_from(map, to, from, len);
	ar71xx_flash_release();
}

static void ar91xx_flash_copy_to_lock(struct map_info *map, unsigned long to,
				      const void *from, ssize_t len)
{
	ar71xx_flash_acquire();
	inline_map_copy_to(map, to, from, len);
	ar71xx_flash_release();
}

static int ar91xx_flash_remove(struct platform_device *pdev)
{
	struct ar91xx_flash_platform_data *pdata;
	struct ar91xx_flash_info *info;

	info = platform_get_drvdata(pdev);
	if (info == NULL)
		return 0;

	platform_set_drvdata(pdev, NULL);

	if (info->mtd == NULL)
		return 0;

	pdata = pdev->dev.platform_data;
#ifdef CONFIG_MTD_PARTITIONS
	if (info->nr_parts) {
		del_mtd_partitions(info->mtd);
		kfree(info->parts);
	} else if (pdata->nr_parts) {
		del_mtd_partitions(info->mtd);
	} else {
		del_mtd_device(info->mtd);
	}
#else
	del_mtd_device(info->mtd);
#endif
	map_destroy(info->mtd);

	return 0;
}

static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
#ifdef CONFIG_MTD_PARTITIONS
static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
#endif

static int ar91xx_flash_probe(struct platform_device *pdev)
{
	struct ar91xx_flash_platform_data *pdata;
	struct ar91xx_flash_info *info;
	struct resource *res;
	struct resource *region;
	const char **probe_type;
	int err = 0;

	pdata = pdev->dev.platform_data;
	if (pdata == NULL)
		return -EINVAL;

	info = devm_kzalloc(&pdev->dev, sizeof(struct ar91xx_flash_info),
			    GFP_KERNEL);
	if (info == NULL) {
		err = -ENOMEM;
		goto err_out;
	}

	platform_set_drvdata(pdev, info);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
		err = -ENOENT;
		goto err_out;
	}

	dev_info(&pdev->dev, "%.8llx at %.8llx\n",
		 (unsigned long long)(res->end - res->start + 1),
		 (unsigned long long)res->start);

	region = devm_request_mem_region(&pdev->dev,
					 res->start, res->end - res->start + 1,
					 dev_name(&pdev->dev));
	if (region == NULL) {
		dev_err(&pdev->dev, "could not reserve memory region\n");
		err = -ENOMEM;
		goto err_out;
	}

	info->map.name = dev_name(&pdev->dev);
	info->map.phys = res->start;
	info->map.size = res->end - res->start + 1;
	info->map.bankwidth = pdata->width;

	info->map.virt = devm_ioremap(&pdev->dev, info->map.phys,
				      info->map.size);
	if (info->map.virt == NULL) {
		dev_err(&pdev->dev, "failed to ioremap flash region\n");
		err = -EIO;
		goto err_out;
	}

	simple_map_init(&info->map);
	if (pdata->is_shared) {
		info->map.read = ar91xx_flash_read_lock;
		info->map.write = ar91xx_flash_write_lock;
		info->map.copy_from = ar91xx_flash_copy_from_lock;
		info->map.copy_to = ar91xx_flash_copy_to_lock;
	} else {
		info->map.read = ar91xx_flash_read;
		info->map.write = ar91xx_flash_write;
	}

	probe_type = rom_probe_types;
	for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
		info->mtd = do_map_probe(*probe_type, &info->map);

	if (info->mtd == NULL) {
		dev_err(&pdev->dev, "map_probe failed\n");
		err = -ENXIO;
		goto err_out;
	}

	info->mtd->owner = THIS_MODULE;

#ifdef CONFIG_MTD_PARTITIONS
	if (pdata->nr_parts) {
		dev_info(&pdev->dev, "using static partition mapping\n");
		add_mtd_partitions(info->mtd, pdata->parts, pdata->nr_parts);
		return 0;
	}

	err = parse_mtd_partitions(info->mtd, part_probe_types,
				   &info->parts, 0);
	if (err > 0) {
		add_mtd_partitions(info->mtd, info->parts, err);
		return 0;
	}
#endif

	add_mtd_device(info->mtd);
	return 0;

err_out:
	ar91xx_flash_remove(pdev);
	return err;
}

#ifdef CONFIG_PM
static int ar91xx_flash_suspend(struct platform_device *dev, pm_message_t state)
{
	struct ar91xx_flash_info *info = platform_get_drvdata(dev);
	int ret = 0;

	if (info->mtd->suspend)
		ret = info->mtd->suspend(info->mtd);

	if (ret)
		goto fail;

	return 0;

fail:
	if (info->mtd->suspend) {
		BUG_ON(!info->mtd->resume);
		info->mtd->resume(info->mtd);
	}

	return ret;
}

static int ar91xx_flash_resume(struct platform_device *pdev)
{
	struct ar91xx_flash_info *info = platform_get_drvdata(pdev);

	if (info->mtd->resume)
		info->mtd->resume(info->mtd);

	return 0;
}

static void ar91xx_flash_shutdown(struct platform_device *pdev)
{
	struct ar91xx_flash_info *info = platform_get_drvdata(pdev);

	if (info->mtd->suspend && info->mtd->resume)
		if (info->mtd->suspend(info->mtd) == 0)
			info->mtd->resume(info->mtd);
}
#else
#define ar91xx_flash_suspend	NULL
#define ar91xx_flash_resume	NULL
#define ar91xx_flash_shutdown	NULL
#endif

static struct platform_driver ar91xx_flash_driver = {
	.probe		= ar91xx_flash_probe,
	.remove		= ar91xx_flash_remove,
	.suspend	= ar91xx_flash_suspend,
	.resume		= ar91xx_flash_resume,
	.shutdown	= ar91xx_flash_shutdown,
	.driver		= {
		.name	= DRV_NAME,
		.owner	= THIS_MODULE,
	},
};

static int __init ar91xx_flash_init(void)
{
	return platform_driver_register(&ar91xx_flash_driver);
}

static void __exit ar91xx_flash_exit(void)
{
	platform_driver_unregister(&ar91xx_flash_driver);
}

module_init(ar91xx_flash_init);
module_exit(ar91xx_flash_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_DESCRIPTION("Parallel flash driver for the Atheros AR91xx SoC");
MODULE_ALIAS("platform:" DRV_NAME);