autcpu12-nvram.c 3.5 KB

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