Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / mtd / maps / rbtx4939-flash.c
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/slab.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/map.h>
21 #include <linux/mtd/partitions.h>
22 #include <asm/txx9/rbtx4939.h>
23
24 struct rbtx4939_flash_info {
25         struct mtd_info *mtd;
26         struct map_info map;
27 };
28
29 static int rbtx4939_flash_remove(struct platform_device *dev)
30 {
31         struct rbtx4939_flash_info *info;
32
33         info = platform_get_drvdata(dev);
34         if (!info)
35                 return 0;
36
37         if (info->mtd) {
38                 struct rbtx4939_flash_data *pdata = dev_get_platdata(&dev->dev);
39
40                 mtd_device_unregister(info->mtd);
41                 map_destroy(info->mtd);
42         }
43         return 0;
44 }
45
46 static const char * const rom_probe_types[] = {
47         "cfi_probe", "jedec_probe", NULL };
48
49 static int rbtx4939_flash_probe(struct platform_device *dev)
50 {
51         struct rbtx4939_flash_data *pdata;
52         struct rbtx4939_flash_info *info;
53         struct resource *res;
54         const char * const *probe_type;
55         int err = 0;
56         unsigned long size;
57
58         pdata = dev_get_platdata(&dev->dev);
59         if (!pdata)
60                 return -ENODEV;
61
62         res = platform_get_resource(dev, IORESOURCE_MEM, 0);
63         if (!res)
64                 return -ENODEV;
65         info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info),
66                             GFP_KERNEL);
67         if (!info)
68                 return -ENOMEM;
69
70         platform_set_drvdata(dev, info);
71
72         size = resource_size(res);
73         pr_notice("rbtx4939 platform flash device: %pR\n", res);
74
75         if (!devm_request_mem_region(&dev->dev, res->start, size,
76                                      dev_name(&dev->dev)))
77                 return -EBUSY;
78
79         info->map.name = dev_name(&dev->dev);
80         info->map.phys = res->start;
81         info->map.size = size;
82         info->map.bankwidth = pdata->width;
83
84         info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size);
85         if (!info->map.virt)
86                 return -EBUSY;
87
88         if (pdata->map_init)
89                 (*pdata->map_init)(&info->map);
90         else
91                 simple_map_init(&info->map);
92
93         probe_type = rom_probe_types;
94         for (; !info->mtd && *probe_type; probe_type++)
95                 info->mtd = do_map_probe(*probe_type, &info->map);
96         if (!info->mtd) {
97                 dev_err(&dev->dev, "map_probe failed\n");
98                 err = -ENXIO;
99                 goto err_out;
100         }
101         info->mtd->owner = THIS_MODULE;
102         err = mtd_device_parse_register(info->mtd, NULL, NULL, pdata->parts,
103                                         pdata->nr_parts);
104
105         if (err)
106                 goto err_out;
107         return 0;
108
109 err_out:
110         rbtx4939_flash_remove(dev);
111         return err;
112 }
113
114 #ifdef CONFIG_PM
115 static void rbtx4939_flash_shutdown(struct platform_device *dev)
116 {
117         struct rbtx4939_flash_info *info = platform_get_drvdata(dev);
118
119         if (mtd_suspend(info->mtd) == 0)
120                 mtd_resume(info->mtd);
121 }
122 #else
123 #define rbtx4939_flash_shutdown NULL
124 #endif
125
126 static struct platform_driver rbtx4939_flash_driver = {
127         .probe          = rbtx4939_flash_probe,
128         .remove         = rbtx4939_flash_remove,
129         .shutdown       = rbtx4939_flash_shutdown,
130         .driver         = {
131                 .name   = "rbtx4939-flash",
132                 .owner  = THIS_MODULE,
133         },
134 };
135
136 module_platform_driver(rbtx4939_flash_driver);
137
138 MODULE_LICENSE("GPL");
139 MODULE_DESCRIPTION("RBTX4939 MTD map driver");
140 MODULE_ALIAS("platform:rbtx4939-flash");