summaryrefslogtreecommitdiff
path: root/drivers/mtd/maps/rbtx4939-flash.c
blob: 39c86c0b0ec1623b8a190983e2d5e98437b5356e (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * rbtx4939-flash (based on physmap.c)
 *
 * This is a simplified physmap driver with map_init callback function.
 *
 * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.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 <asm/txx9/rbtx4939.h>

struct rbtx4939_flash_info {
	struct mtd_info *mtd;
	struct map_info map;
};

static int rbtx4939_flash_remove(struct platform_device *dev)
{
	struct rbtx4939_flash_info *info;

	info = platform_get_drvdata(dev);
	if (!info)
		return 0;

	if (info->mtd) {
		mtd_device_unregister(info->mtd);
		map_destroy(info->mtd);
	}
	return 0;
}

static const char * const rom_probe_types[] = {
	"cfi_probe", "jedec_probe", NULL };

static int rbtx4939_flash_probe(struct platform_device *dev)
{
	struct rbtx4939_flash_data *pdata;
	struct rbtx4939_flash_info *info;
	struct resource *res;
	const char * const *probe_type;
	int err = 0;
	unsigned long size;

	pdata = dev_get_platdata(&dev->dev);
	if (!pdata)
		return -ENODEV;

	res = platform_get_resource(dev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENODEV;
	info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info),
			    GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	platform_set_drvdata(dev, info);

	size = resource_size(res);
	pr_notice("rbtx4939 platform flash device: %pR\n", res);

	if (!devm_request_mem_region(&dev->dev, res->start, size,
				     dev_name(&dev->dev)))
		return -EBUSY;

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

	info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size);
	if (!info->map.virt)
		return -EBUSY;

	if (pdata->map_init)
		(*pdata->map_init)(&info->map);
	else
		simple_map_init(&info->map);

	probe_type = rom_probe_types;
	for (; !info->mtd && *probe_type; probe_type++)
		info->mtd = do_map_probe(*probe_type, &info->map);
	if (!info->mtd) {
		dev_err(&dev->dev, "map_probe failed\n");
		err = -ENXIO;
		goto err_out;
	}
	info->mtd->dev.parent = &dev->dev;
	err = mtd_device_register(info->mtd, pdata->parts, pdata->nr_parts);

	if (err)
		goto err_out;
	return 0;

err_out:
	rbtx4939_flash_remove(dev);
	return err;
}

#ifdef CONFIG_PM
static void rbtx4939_flash_shutdown(struct platform_device *dev)
{
	struct rbtx4939_flash_info *info = platform_get_drvdata(dev);

	if (mtd_suspend(info->mtd) == 0)
		mtd_resume(info->mtd);
}
#else
#define rbtx4939_flash_shutdown NULL
#endif

static struct platform_driver rbtx4939_flash_driver = {
	.probe		= rbtx4939_flash_probe,
	.remove		= rbtx4939_flash_remove,
	.shutdown	= rbtx4939_flash_shutdown,
	.driver		= {
		.name	= "rbtx4939-flash",
	},
};

module_platform_driver(rbtx4939_flash_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("RBTX4939 MTD map driver");
MODULE_ALIAS("platform:rbtx4939-flash");