blob: ef420d984d1acb81965d95bf14ba1925986ac609 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Thomas Gleixner69f34c92005-11-07 11:15:40 +00002 * NV-RAM memory access on autcpu12
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (C) 2002 Thomas Gleixner (gleixner@autronix.de)
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Alexander Shiyance557542012-08-15 20:28:06 +040019#include <linux/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/types.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
Alexander Shiyance557542012-08-15 20:28:06 +040024#include <linux/device.h>
25#include <linux/module.h>
26#include <linux/platform_device.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mtd/mtd.h>
29#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Alexander Shiyance557542012-08-15 20:28:06 +040031struct autcpu12_nvram_priv {
32 struct mtd_info *mtd;
33 struct map_info map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Alexander Shiyance557542012-08-15 20:28:06 +040036static int __devinit autcpu12_nvram_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Alexander Shiyand1f55c62012-08-15 20:28:05 +040038 map_word tmp, save0, save1;
Alexander Shiyance557542012-08-15 20:28:06 +040039 struct resource *res;
40 struct autcpu12_nvram_priv *priv;
Alexander Shiyand1f55c62012-08-15 20:28:05 +040041 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Alexander Shiyance557542012-08-15 20:28:06 +040043 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 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 goto out;
55 }
Alexander Shiyance557542012-08-15 20:28:06 +040056
57 priv->map.bankwidth = 4;
58 priv->map.phys = res->start;
59 priv->map.size = resource_size(res);
60 priv->map.virt = devm_request_and_ioremap(&pdev->dev, res);
61 strcpy((char *)priv->map.name, res->name);
62 if (!priv->map.virt) {
63 dev_err(&pdev->dev, "failed to remap mem resource\n");
64 err = -EBUSY;
65 goto out;
66 }
67
68 simple_map_init(&priv->map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Thomas Gleixner69f34c92005-11-07 11:15:40 +000070 /*
71 * Check for 32K/128K
72 * read ofs 0
73 * read ofs 0x10000
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * Write complement to ofs 0x100000
75 * Read and check result on ofs 0x0
76 * Restore contents
77 */
Alexander Shiyance557542012-08-15 20:28:06 +040078 save0 = map_read(&priv->map, 0);
79 save1 = map_read(&priv->map, 0x10000);
Alexander Shiyand1f55c62012-08-15 20:28:05 +040080 tmp.x[0] = ~save0.x[0];
Alexander Shiyance557542012-08-15 20:28:06 +040081 map_write(&priv->map, tmp, 0x10000);
82 tmp = map_read(&priv->map, 0);
83 /* if we find this pattern on 0x0, we have 32K size */
84 if (!map_word_equal(&priv->map, tmp, save0)) {
85 map_write(&priv->map, save0, 0x0);
86 priv->map.size = SZ_32K;
87 } else
88 map_write(&priv->map, save1, 0x10000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Alexander Shiyance557542012-08-15 20:28:06 +040090 priv->mtd = do_map_probe("map_ram", &priv->map);
91 if (!priv->mtd) {
92 dev_err(&pdev->dev, "probing failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 err = -ENXIO;
Alexander Shiyance557542012-08-15 20:28:06 +040094 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96
Alexander Shiyance557542012-08-15 20:28:06 +040097 priv->mtd->owner = THIS_MODULE;
98 priv->mtd->erasesize = 16;
99 priv->mtd->dev.parent = &pdev->dev;
100 if (!mtd_device_register(priv->mtd, NULL, 0)) {
101 dev_info(&pdev->dev,
102 "NV-RAM device size %ldKiB registered on AUTCPU12\n",
103 priv->map.size / SZ_1K);
104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Alexander Shiyance557542012-08-15 20:28:06 +0400107 map_destroy(priv->mtd);
108 dev_err(&pdev->dev, "NV-RAM device addition failed\n");
109 err = -ENOMEM;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111out:
Alexander Shiyance557542012-08-15 20:28:06 +0400112 devm_kfree(&pdev->dev, priv);
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return err;
115}
116
Alexander Shiyance557542012-08-15 20:28:06 +0400117static int __devexit autcpu12_nvram_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Alexander Shiyance557542012-08-15 20:28:06 +0400119 struct autcpu12_nvram_priv *priv = platform_get_drvdata(pdev);
120
121 mtd_device_unregister(priv->mtd);
122 map_destroy(priv->mtd);
123 devm_kfree(&pdev->dev, priv);
124
125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Alexander Shiyance557542012-08-15 20:28:06 +0400128static struct platform_driver autcpu12_nvram_driver = {
129 .driver = {
130 .name = "autcpu12_nvram",
131 .owner = THIS_MODULE,
132 },
133 .probe = autcpu12_nvram_probe,
134 .remove = __devexit_p(autcpu12_nvram_remove),
135};
136module_platform_driver(autcpu12_nvram_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138MODULE_AUTHOR("Thomas Gleixner");
Alexander Shiyance557542012-08-15 20:28:06 +0400139MODULE_DESCRIPTION("autcpu12 NVRAM map driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140MODULE_LICENSE("GPL");