autcpu12-nvram.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * NV-RAM memory access on autcpu12
  3. * (C) 2002 Thomas Gleixner (gleixner@autronix.de)
  4. *
  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
  18. */
  19. #include <linux/err.h>
  20. #include <linux/sizes.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/device.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/map.h>
  29. struct autcpu12_nvram_priv {
  30. struct mtd_info *mtd;
  31. struct map_info map;
  32. };
  33. static int autcpu12_nvram_probe(struct platform_device *pdev)
  34. {
  35. map_word tmp, save0, save1;
  36. struct resource *res;
  37. struct autcpu12_nvram_priv *priv;
  38. priv = devm_kzalloc(&pdev->dev,
  39. sizeof(struct autcpu12_nvram_priv), GFP_KERNEL);
  40. if (!priv)
  41. return -ENOMEM;
  42. platform_set_drvdata(pdev, priv);
  43. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  44. if (!res) {
  45. dev_err(&pdev->dev, "failed to get memory resource\n");
  46. return -ENOENT;
  47. }
  48. priv->map.bankwidth = 4;
  49. priv->map.phys = res->start;
  50. priv->map.size = resource_size(res);
  51. priv->map.virt = devm_ioremap_resource(&pdev->dev, res);
  52. strcpy((char *)priv->map.name, res->name);
  53. if (IS_ERR(priv->map.virt))
  54. return PTR_ERR(priv->map.virt);
  55. simple_map_init(&priv->map);
  56. /*
  57. * Check for 32K/128K
  58. * read ofs 0
  59. * read ofs 0x10000
  60. * Write complement to ofs 0x100000
  61. * Read and check result on ofs 0x0
  62. * Restore contents
  63. */
  64. save0 = map_read(&priv->map, 0);
  65. save1 = map_read(&priv->map, 0x10000);
  66. tmp.x[0] = ~save0.x[0];
  67. map_write(&priv->map, tmp, 0x10000);
  68. tmp = map_read(&priv->map, 0);
  69. /* if we find this pattern on 0x0, we have 32K size */
  70. if (!map_word_equal(&priv->map, tmp, save0)) {
  71. map_write(&priv->map, save0, 0x0);
  72. priv->map.size = SZ_32K;
  73. } else
  74. map_write(&priv->map, save1, 0x10000);
  75. priv->mtd = do_map_probe("map_ram", &priv->map);
  76. if (!priv->mtd) {
  77. dev_err(&pdev->dev, "probing failed\n");
  78. return -ENXIO;
  79. }
  80. priv->mtd->owner = THIS_MODULE;
  81. priv->mtd->erasesize = 16;
  82. priv->mtd->dev.parent = &pdev->dev;
  83. if (!mtd_device_register(priv->mtd, NULL, 0)) {
  84. dev_info(&pdev->dev,
  85. "NV-RAM device size %ldKiB registered on AUTCPU12\n",
  86. priv->map.size / SZ_1K);
  87. return 0;
  88. }
  89. map_destroy(priv->mtd);
  90. dev_err(&pdev->dev, "NV-RAM device addition failed\n");
  91. return -ENOMEM;
  92. }
  93. static int autcpu12_nvram_remove(struct platform_device *pdev)
  94. {
  95. struct autcpu12_nvram_priv *priv = platform_get_drvdata(pdev);
  96. mtd_device_unregister(priv->mtd);
  97. map_destroy(priv->mtd);
  98. return 0;
  99. }
  100. static struct platform_driver autcpu12_nvram_driver = {
  101. .driver = {
  102. .name = "autcpu12_nvram",
  103. .owner = THIS_MODULE,
  104. },
  105. .probe = autcpu12_nvram_probe,
  106. .remove = autcpu12_nvram_remove,
  107. };
  108. module_platform_driver(autcpu12_nvram_driver);
  109. MODULE_AUTHOR("Thomas Gleixner");
  110. MODULE_DESCRIPTION("autcpu12 NVRAM map driver");
  111. MODULE_LICENSE("GPL");