sram.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/genalloc.h>
  15. #include <mach/common.h>
  16. #include <mach/memory.h>
  17. #include <mach/sram.h>
  18. static struct gen_pool *sram_pool;
  19. void *sram_alloc(size_t len, dma_addr_t *dma)
  20. {
  21. unsigned long vaddr;
  22. dma_addr_t dma_base = davinci_soc_info.sram_dma;
  23. if (dma)
  24. *dma = 0;
  25. if (!sram_pool || (dma && !dma_base))
  26. return NULL;
  27. vaddr = gen_pool_alloc(sram_pool, len);
  28. if (!vaddr)
  29. return NULL;
  30. if (dma)
  31. *dma = dma_base + (vaddr - SRAM_VIRT);
  32. return (void *)vaddr;
  33. }
  34. EXPORT_SYMBOL(sram_alloc);
  35. void sram_free(void *addr, size_t len)
  36. {
  37. gen_pool_free(sram_pool, (unsigned long) addr, len);
  38. }
  39. EXPORT_SYMBOL(sram_free);
  40. /*
  41. * REVISIT This supports CPU and DMA access to/from SRAM, but it
  42. * doesn't (yet?) support some other notable uses of SRAM: as TCM
  43. * for data and/or instructions; and holding code needed to enter
  44. * and exit suspend states (while DRAM can't be used).
  45. */
  46. static int __init sram_init(void)
  47. {
  48. unsigned len = davinci_soc_info.sram_len;
  49. int status = 0;
  50. if (len) {
  51. len = min_t(unsigned, len, SRAM_SIZE);
  52. sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1);
  53. if (!sram_pool)
  54. status = -ENOMEM;
  55. }
  56. if (sram_pool)
  57. status = gen_pool_add(sram_pool, SRAM_VIRT, len, -1);
  58. WARN_ON(status < 0);
  59. return status;
  60. }
  61. core_initcall(sram_init);