sram.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * mach-davinci/sram.c - DaVinci simple SRAM allocator
  4. *
  5. * Copyright (C) 2009 David Brownell
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/io.h>
  10. #include <linux/genalloc.h>
  11. #include <mach/common.h>
  12. #include "sram.h"
  13. static struct gen_pool *sram_pool;
  14. struct gen_pool *sram_get_gen_pool(void)
  15. {
  16. return sram_pool;
  17. }
  18. void *sram_alloc(size_t len, dma_addr_t *dma)
  19. {
  20. dma_addr_t dma_base = davinci_soc_info.sram_dma;
  21. if (dma)
  22. *dma = 0;
  23. if (!sram_pool || (dma && !dma_base))
  24. return NULL;
  25. return gen_pool_dma_alloc(sram_pool, len, dma);
  26. }
  27. EXPORT_SYMBOL(sram_alloc);
  28. void sram_free(void *addr, size_t len)
  29. {
  30. gen_pool_free(sram_pool, (unsigned long) addr, len);
  31. }
  32. EXPORT_SYMBOL(sram_free);
  33. /*
  34. * REVISIT This supports CPU and DMA access to/from SRAM, but it
  35. * doesn't (yet?) support some other notable uses of SRAM: as TCM
  36. * for data and/or instructions; and holding code needed to enter
  37. * and exit suspend states (while DRAM can't be used).
  38. */
  39. static int __init sram_init(void)
  40. {
  41. phys_addr_t phys = davinci_soc_info.sram_dma;
  42. unsigned len = davinci_soc_info.sram_len;
  43. int status = 0;
  44. void __iomem *addr;
  45. if (len) {
  46. len = min_t(unsigned, len, SRAM_SIZE);
  47. sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1);
  48. if (!sram_pool)
  49. status = -ENOMEM;
  50. }
  51. if (sram_pool) {
  52. addr = ioremap(phys, len);
  53. if (!addr)
  54. return -ENOMEM;
  55. status = gen_pool_add_virt(sram_pool, (unsigned long) addr,
  56. phys, len, -1);
  57. if (status < 0)
  58. iounmap(addr);
  59. }
  60. WARN_ON(status < 0);
  61. return status;
  62. }
  63. core_initcall(sram_init);