Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 2 | * NV-RAM memory access on autcpu12 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * (C) 2002 Thomas Gleixner (gleixner@autronix.de) |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame^] | 19 | #include <linux/err.h> |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 20 | #include <linux/sizes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/types.h> |
| 23 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 25 | #include <linux/device.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/mtd/mtd.h> |
| 30 | #include <linux/mtd/map.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 32 | struct autcpu12_nvram_priv { |
| 33 | struct mtd_info *mtd; |
| 34 | struct map_info map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 37 | static int autcpu12_nvram_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Alexander Shiyan | d1f55c6 | 2012-08-15 20:28:05 +0400 | [diff] [blame] | 39 | map_word tmp, save0, save1; |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 40 | struct resource *res; |
| 41 | struct autcpu12_nvram_priv *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 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"); |
Julia Lawall | 6f12f59 | 2012-09-01 18:33:11 +0200 | [diff] [blame] | 53 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 55 | |
| 56 | priv->map.bankwidth = 4; |
| 57 | priv->map.phys = res->start; |
| 58 | priv->map.size = resource_size(res); |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame^] | 59 | priv->map.virt = devm_ioremap_resource(&pdev->dev, res); |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 60 | strcpy((char *)priv->map.name, res->name); |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame^] | 61 | if (IS_ERR(priv->map.virt)) |
| 62 | return PTR_ERR(priv->map.virt); |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 63 | |
| 64 | simple_map_init(&priv->map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 66 | /* |
| 67 | * Check for 32K/128K |
| 68 | * read ofs 0 |
| 69 | * read ofs 0x10000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * Write complement to ofs 0x100000 |
| 71 | * Read and check result on ofs 0x0 |
| 72 | * Restore contents |
| 73 | */ |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 74 | save0 = map_read(&priv->map, 0); |
| 75 | save1 = map_read(&priv->map, 0x10000); |
Alexander Shiyan | d1f55c6 | 2012-08-15 20:28:05 +0400 | [diff] [blame] | 76 | tmp.x[0] = ~save0.x[0]; |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 77 | map_write(&priv->map, tmp, 0x10000); |
| 78 | tmp = map_read(&priv->map, 0); |
| 79 | /* if we find this pattern on 0x0, we have 32K size */ |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 86 | priv->mtd = do_map_probe("map_ram", &priv->map); |
| 87 | if (!priv->mtd) { |
| 88 | dev_err(&pdev->dev, "probing failed\n"); |
Julia Lawall | 6f12f59 | 2012-09-01 18:33:11 +0200 | [diff] [blame] | 89 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 102 | map_destroy(priv->mtd); |
| 103 | dev_err(&pdev->dev, "NV-RAM device addition failed\n"); |
Julia Lawall | 6f12f59 | 2012-09-01 18:33:11 +0200 | [diff] [blame] | 104 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 107 | static int autcpu12_nvram_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 109 | struct autcpu12_nvram_priv *priv = platform_get_drvdata(pdev); |
| 110 | |
| 111 | mtd_device_unregister(priv->mtd); |
| 112 | map_destroy(priv->mtd); |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 113 | |
| 114 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 117 | static struct platform_driver autcpu12_nvram_driver = { |
| 118 | .driver = { |
| 119 | .name = "autcpu12_nvram", |
| 120 | .owner = THIS_MODULE, |
| 121 | }, |
| 122 | .probe = autcpu12_nvram_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 123 | .remove = autcpu12_nvram_remove, |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 124 | }; |
| 125 | module_platform_driver(autcpu12_nvram_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | MODULE_AUTHOR("Thomas Gleixner"); |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 128 | MODULE_DESCRIPTION("autcpu12 NVRAM map driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | MODULE_LICENSE("GPL"); |