1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/err.h>
20#include <linux/sizes.h>
21
22#include <linux/types.h>
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/device.h>
26#include <linux/module.h>
27#include <linux/platform_device.h>
28
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/map.h>
31
32struct autcpu12_nvram_priv {
33 struct mtd_info *mtd;
34 struct map_info map;
35};
36
37static int autcpu12_nvram_probe(struct platform_device *pdev)
38{
39 map_word tmp, save0, save1;
40 struct resource *res;
41 struct autcpu12_nvram_priv *priv;
42
43 priv = devm_kzalloc(&pdev->dev,
44 sizeof(struct autcpu12_nvram_priv), GFP_KERNEL);
45 if (!priv)
46 return -ENOMEM;
47
48 platform_set_drvdata(pdev, priv);
49
50 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
51 if (!res) {
52 dev_err(&pdev->dev, "failed to get memory resource\n");
53 return -ENOENT;
54 }
55
56 priv->map.bankwidth = 4;
57 priv->map.phys = res->start;
58 priv->map.size = resource_size(res);
59 priv->map.virt = devm_ioremap_resource(&pdev->dev, res);
60 strcpy((char *)priv->map.name, res->name);
61 if (IS_ERR(priv->map.virt))
62 return PTR_ERR(priv->map.virt);
63
64 simple_map_init(&priv->map);
65
66
67
68
69
70
71
72
73
74 save0 = map_read(&priv->map, 0);
75 save1 = map_read(&priv->map, 0x10000);
76 tmp.x[0] = ~save0.x[0];
77 map_write(&priv->map, tmp, 0x10000);
78 tmp = map_read(&priv->map, 0);
79
80 if (!map_word_equal(&priv->map, tmp, save0)) {
81 map_write(&priv->map, save0, 0x0);
82 priv->map.size = SZ_32K;
83 } else
84 map_write(&priv->map, save1, 0x10000);
85
86 priv->mtd = do_map_probe("map_ram", &priv->map);
87 if (!priv->mtd) {
88 dev_err(&pdev->dev, "probing failed\n");
89 return -ENXIO;
90 }
91
92 priv->mtd->owner = THIS_MODULE;
93 priv->mtd->erasesize = 16;
94 priv->mtd->dev.parent = &pdev->dev;
95 if (!mtd_device_register(priv->mtd, NULL, 0)) {
96 dev_info(&pdev->dev,
97 "NV-RAM device size %ldKiB registered on AUTCPU12\n",
98 priv->map.size / SZ_1K);
99 return 0;
100 }
101
102 map_destroy(priv->mtd);
103 dev_err(&pdev->dev, "NV-RAM device addition failed\n");
104 return -ENOMEM;
105}
106
107static int autcpu12_nvram_remove(struct platform_device *pdev)
108{
109 struct autcpu12_nvram_priv *priv = platform_get_drvdata(pdev);
110
111 mtd_device_unregister(priv->mtd);
112 map_destroy(priv->mtd);
113
114 return 0;
115}
116
117static struct platform_driver autcpu12_nvram_driver = {
118 .driver = {
119 .name = "autcpu12_nvram",
120 .owner = THIS_MODULE,
121 },
122 .probe = autcpu12_nvram_probe,
123 .remove = autcpu12_nvram_remove,
124};
125module_platform_driver(autcpu12_nvram_driver);
126
127MODULE_AUTHOR("Thomas Gleixner");
128MODULE_DESCRIPTION("autcpu12 NVRAM map driver");
129MODULE_LICENSE("GPL");
130