sram.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Generic on-chip SRAM allocation driver
  3. *
  4. * Copyright (C) 2012 Philipp Zabel, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  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., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/clk.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/genalloc.h>
  30. #define SRAM_GRANULARITY 32
  31. struct sram_dev {
  32. struct gen_pool *pool;
  33. struct clk *clk;
  34. };
  35. static int sram_probe(struct platform_device *pdev)
  36. {
  37. void __iomem *virt_base;
  38. struct sram_dev *sram;
  39. struct resource *res;
  40. unsigned long size;
  41. int ret;
  42. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  43. if (!res)
  44. return -EINVAL;
  45. size = resource_size(res);
  46. virt_base = devm_request_and_ioremap(&pdev->dev, res);
  47. if (!virt_base)
  48. return -EADDRNOTAVAIL;
  49. sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
  50. if (!sram)
  51. return -ENOMEM;
  52. sram->clk = devm_clk_get(&pdev->dev, NULL);
  53. if (IS_ERR(sram->clk))
  54. sram->clk = NULL;
  55. else
  56. clk_prepare_enable(sram->clk);
  57. sram->pool = devm_gen_pool_create(&pdev->dev, ilog2(SRAM_GRANULARITY), -1);
  58. if (!sram->pool)
  59. return -ENOMEM;
  60. ret = gen_pool_add_virt(sram->pool, (unsigned long)virt_base,
  61. res->start, size, -1);
  62. if (ret < 0) {
  63. gen_pool_destroy(sram->pool);
  64. return ret;
  65. }
  66. platform_set_drvdata(pdev, sram);
  67. dev_dbg(&pdev->dev, "SRAM pool: %ld KiB @ 0x%p\n", size / 1024, virt_base);
  68. return 0;
  69. }
  70. static int sram_remove(struct platform_device *pdev)
  71. {
  72. struct sram_dev *sram = platform_get_drvdata(pdev);
  73. if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
  74. dev_dbg(&pdev->dev, "removed while SRAM allocated\n");
  75. gen_pool_destroy(sram->pool);
  76. if (sram->clk)
  77. clk_disable_unprepare(sram->clk);
  78. return 0;
  79. }
  80. #ifdef CONFIG_OF
  81. static struct of_device_id sram_dt_ids[] = {
  82. { .compatible = "mmio-sram" },
  83. {}
  84. };
  85. #endif
  86. static struct platform_driver sram_driver = {
  87. .driver = {
  88. .name = "sram",
  89. .of_match_table = of_match_ptr(sram_dt_ids),
  90. },
  91. .probe = sram_probe,
  92. .remove = sram_remove,
  93. };
  94. static int __init sram_init(void)
  95. {
  96. return platform_driver_register(&sram_driver);
  97. }
  98. postcore_initcall(sram_init);