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 | */ |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 19 | #include <linux/sizes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/types.h> |
| 22 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 24 | #include <linux/device.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/mtd/mtd.h> |
| 29 | #include <linux/mtd/map.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 31 | struct autcpu12_nvram_priv { |
| 32 | struct mtd_info *mtd; |
| 33 | struct map_info map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 36 | static int __devinit autcpu12_nvram_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Alexander Shiyan | d1f55c6 | 2012-08-15 20:28:05 +0400 | [diff] [blame] | 38 | map_word tmp, save0, save1; |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 39 | struct resource *res; |
| 40 | struct autcpu12_nvram_priv *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 42 | priv = devm_kzalloc(&pdev->dev, |
| 43 | sizeof(struct autcpu12_nvram_priv), GFP_KERNEL); |
| 44 | if (!priv) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | platform_set_drvdata(pdev, priv); |
| 48 | |
| 49 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 50 | if (!res) { |
| 51 | dev_err(&pdev->dev, "failed to get memory resource\n"); |
Julia Lawall | 6f12f59 | 2012-09-01 18:33:11 +0200 | [diff] [blame] | 52 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 54 | |
| 55 | priv->map.bankwidth = 4; |
| 56 | priv->map.phys = res->start; |
| 57 | priv->map.size = resource_size(res); |
| 58 | priv->map.virt = devm_request_and_ioremap(&pdev->dev, res); |
| 59 | strcpy((char *)priv->map.name, res->name); |
| 60 | if (!priv->map.virt) { |
| 61 | dev_err(&pdev->dev, "failed to remap mem resource\n"); |
Julia Lawall | 6f12f59 | 2012-09-01 18:33:11 +0200 | [diff] [blame] | 62 | return -EBUSY; |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | simple_map_init(&priv->map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 67 | /* |
| 68 | * Check for 32K/128K |
| 69 | * read ofs 0 |
| 70 | * read ofs 0x10000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | * Write complement to ofs 0x100000 |
| 72 | * Read and check result on ofs 0x0 |
| 73 | * Restore contents |
| 74 | */ |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 75 | save0 = map_read(&priv->map, 0); |
| 76 | save1 = map_read(&priv->map, 0x10000); |
Alexander Shiyan | d1f55c6 | 2012-08-15 20:28:05 +0400 | [diff] [blame] | 77 | tmp.x[0] = ~save0.x[0]; |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 78 | map_write(&priv->map, tmp, 0x10000); |
| 79 | tmp = map_read(&priv->map, 0); |
| 80 | /* if we find this pattern on 0x0, we have 32K size */ |
| 81 | if (!map_word_equal(&priv->map, tmp, save0)) { |
| 82 | map_write(&priv->map, save0, 0x0); |
| 83 | priv->map.size = SZ_32K; |
| 84 | } else |
| 85 | map_write(&priv->map, save1, 0x10000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 87 | priv->mtd = do_map_probe("map_ram", &priv->map); |
| 88 | if (!priv->mtd) { |
| 89 | dev_err(&pdev->dev, "probing failed\n"); |
Julia Lawall | 6f12f59 | 2012-09-01 18:33:11 +0200 | [diff] [blame] | 90 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 93 | priv->mtd->owner = THIS_MODULE; |
| 94 | priv->mtd->erasesize = 16; |
| 95 | priv->mtd->dev.parent = &pdev->dev; |
| 96 | if (!mtd_device_register(priv->mtd, NULL, 0)) { |
| 97 | dev_info(&pdev->dev, |
| 98 | "NV-RAM device size %ldKiB registered on AUTCPU12\n", |
| 99 | priv->map.size / SZ_1K); |
| 100 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 103 | map_destroy(priv->mtd); |
| 104 | dev_err(&pdev->dev, "NV-RAM device addition failed\n"); |
Julia Lawall | 6f12f59 | 2012-09-01 18:33:11 +0200 | [diff] [blame] | 105 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 108 | static int __devexit autcpu12_nvram_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 110 | struct autcpu12_nvram_priv *priv = platform_get_drvdata(pdev); |
| 111 | |
| 112 | mtd_device_unregister(priv->mtd); |
| 113 | map_destroy(priv->mtd); |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 114 | |
| 115 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 118 | static struct platform_driver autcpu12_nvram_driver = { |
| 119 | .driver = { |
| 120 | .name = "autcpu12_nvram", |
| 121 | .owner = THIS_MODULE, |
| 122 | }, |
| 123 | .probe = autcpu12_nvram_probe, |
| 124 | .remove = __devexit_p(autcpu12_nvram_remove), |
| 125 | }; |
| 126 | module_platform_driver(autcpu12_nvram_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | MODULE_AUTHOR("Thomas Gleixner"); |
Alexander Shiyan | ce55754 | 2012-08-15 20:28:06 +0400 | [diff] [blame] | 129 | MODULE_DESCRIPTION("autcpu12 NVRAM map driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | MODULE_LICENSE("GPL"); |