blob: 39c86c0b0ec1623b8a190983e2d5e98437b5356e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Atsushi Nemoto610f75e72009-03-04 12:01:34 -08002/*
3 * rbtx4939-flash (based on physmap.c)
4 *
5 * This is a simplified physmap driver with map_init callback function.
6 *
Atsushi Nemoto610f75e72009-03-04 12:01:34 -08007 * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
8 */
9
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/kernel.h>
Atsushi Nemoto610f75e72009-03-04 12:01:34 -080013#include <linux/slab.h>
14#include <linux/device.h>
15#include <linux/platform_device.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mtd/map.h>
18#include <linux/mtd/partitions.h>
19#include <asm/txx9/rbtx4939.h>
20
21struct rbtx4939_flash_info {
22 struct mtd_info *mtd;
23 struct map_info map;
Atsushi Nemoto610f75e72009-03-04 12:01:34 -080024};
25
26static int rbtx4939_flash_remove(struct platform_device *dev)
27{
28 struct rbtx4939_flash_info *info;
29
30 info = platform_get_drvdata(dev);
31 if (!info)
32 return 0;
Atsushi Nemoto610f75e72009-03-04 12:01:34 -080033
34 if (info->mtd) {
Jamie Iles16b0eb12011-05-23 10:23:08 +010035 mtd_device_unregister(info->mtd);
Atsushi Nemoto610f75e72009-03-04 12:01:34 -080036 map_destroy(info->mtd);
37 }
38 return 0;
39}
40
Artem Bityutskiy0984c892013-03-12 10:46:37 +020041static const char * const rom_probe_types[] = {
42 "cfi_probe", "jedec_probe", NULL };
Atsushi Nemoto610f75e72009-03-04 12:01:34 -080043
44static int rbtx4939_flash_probe(struct platform_device *dev)
45{
46 struct rbtx4939_flash_data *pdata;
47 struct rbtx4939_flash_info *info;
48 struct resource *res;
Artem Bityutskiy0984c892013-03-12 10:46:37 +020049 const char * const *probe_type;
Atsushi Nemoto610f75e72009-03-04 12:01:34 -080050 int err = 0;
51 unsigned long size;
52
Jingoo Hand20d5a52013-07-30 17:18:06 +090053 pdata = dev_get_platdata(&dev->dev);
Atsushi Nemoto610f75e72009-03-04 12:01:34 -080054 if (!pdata)
55 return -ENODEV;
56
57 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
58 if (!res)
59 return -ENODEV;
60 info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info),
61 GFP_KERNEL);
62 if (!info)
63 return -ENOMEM;
64
65 platform_set_drvdata(dev, info);
66
67 size = resource_size(res);
68 pr_notice("rbtx4939 platform flash device: %pR\n", res);
69
70 if (!devm_request_mem_region(&dev->dev, res->start, size,
71 dev_name(&dev->dev)))
72 return -EBUSY;
73
74 info->map.name = dev_name(&dev->dev);
75 info->map.phys = res->start;
76 info->map.size = size;
77 info->map.bankwidth = pdata->width;
78
79 info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size);
80 if (!info->map.virt)
81 return -EBUSY;
82
83 if (pdata->map_init)
84 (*pdata->map_init)(&info->map);
85 else
86 simple_map_init(&info->map);
87
88 probe_type = rom_probe_types;
89 for (; !info->mtd && *probe_type; probe_type++)
90 info->mtd = do_map_probe(*probe_type, &info->map);
91 if (!info->mtd) {
92 dev_err(&dev->dev, "map_probe failed\n");
93 err = -ENXIO;
94 goto err_out;
95 }
Brian Norrisff84d2b2015-10-19 12:22:57 -070096 info->mtd->dev.parent = &dev->dev;
Rafał Miłecki48970152018-07-13 11:27:33 +020097 err = mtd_device_register(info->mtd, pdata->parts, pdata->nr_parts);
Atsushi Nemoto610f75e72009-03-04 12:01:34 -080098
Dmitry Eremin-Solenikovc77d8092011-06-02 18:00:04 +040099 if (err)
100 goto err_out;
Atsushi Nemoto610f75e72009-03-04 12:01:34 -0800101 return 0;
102
103err_out:
104 rbtx4939_flash_remove(dev);
105 return err;
106}
107
108#ifdef CONFIG_PM
Atsushi Nemoto610f75e72009-03-04 12:01:34 -0800109static void rbtx4939_flash_shutdown(struct platform_device *dev)
110{
111 struct rbtx4939_flash_info *info = platform_get_drvdata(dev);
112
Artem Bityutskiy079c9852011-12-30 17:15:59 +0200113 if (mtd_suspend(info->mtd) == 0)
114 mtd_resume(info->mtd);
Atsushi Nemoto610f75e72009-03-04 12:01:34 -0800115}
116#else
Atsushi Nemoto610f75e72009-03-04 12:01:34 -0800117#define rbtx4939_flash_shutdown NULL
118#endif
119
120static struct platform_driver rbtx4939_flash_driver = {
121 .probe = rbtx4939_flash_probe,
122 .remove = rbtx4939_flash_remove,
Atsushi Nemoto610f75e72009-03-04 12:01:34 -0800123 .shutdown = rbtx4939_flash_shutdown,
124 .driver = {
125 .name = "rbtx4939-flash",
Atsushi Nemoto610f75e72009-03-04 12:01:34 -0800126 },
127};
128
Axel Linf99640d2011-11-27 20:45:03 +0800129module_platform_driver(rbtx4939_flash_driver);
Atsushi Nemoto610f75e72009-03-04 12:01:34 -0800130
131MODULE_LICENSE("GPL");
132MODULE_DESCRIPTION("RBTX4939 MTD map driver");
133MODULE_ALIAS("platform:rbtx4939-flash");