sram.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * mach-davinci/sram.c - DaVinci simple SRAM allocator
  3. *
  4. * Copyright (C) 2009 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/genalloc.h>
  14. #include <mach/common.h>
  15. #include <mach/sram.h>
  16. static struct gen_pool *sram_pool;
  17. void *sram_alloc(size_t len, dma_addr_t *dma)
  18. {
  19. unsigned long vaddr;
  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. vaddr = gen_pool_alloc(sram_pool, len);
  26. if (!vaddr)
  27. return NULL;
  28. if (dma)
  29. *dma = dma_base + (vaddr - SRAM_VIRT);
  30. return (void *)vaddr;
  31. }
  32. EXPORT_SYMBOL(sram_alloc);
  33. void sram_free(void *addr, size_t len)
  34. {
  35. gen_pool_free(sram_pool, (unsigned long) addr, len);
  36. }
  37. EXPORT_SYMBOL(sram_free);
  38. /*
  39. * REVISIT This supports CPU and DMA access to/from SRAM, but it
  40. * doesn't (yet?) support some other notable uses of SRAM: as TCM
  41. * for data and/or instructions; and holding code needed to enter
  42. * and exit suspend states (while DRAM can't be used).
  43. */
  44. static int __init sram_init(void)
  45. {
  46. unsigned len = davinci_soc_info.sram_len;
  47. int status = 0;
  48. if (len) {
  49. len = min_t(unsigned, len, SRAM_SIZE);
  50. sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1);
  51. if (!sram_pool)
  52. status = -ENOMEM;
  53. }
  54. if (sram_pool)
  55. status = gen_pool_add(sram_pool, SRAM_VIRT, len, -1);
  56. WARN_ON(status < 0);
  57. return status;
  58. }
  59. core_initcall(sram_init);