iram_alloc.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/io.h>
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/genalloc.h>
  24. #include <mach/iram.h>
  25. static unsigned long iram_phys_base;
  26. static void __iomem *iram_virt_base;
  27. static struct gen_pool *iram_pool;
  28. static inline void __iomem *iram_phys_to_virt(unsigned long p)
  29. {
  30. return iram_virt_base + (p - iram_phys_base);
  31. }
  32. void __iomem *iram_alloc(unsigned int size, unsigned long *dma_addr)
  33. {
  34. if (!iram_pool)
  35. return NULL;
  36. *dma_addr = gen_pool_alloc(iram_pool, size);
  37. pr_debug("iram alloc - %dB@0x%lX\n", size, *dma_addr);
  38. if (!*dma_addr)
  39. return NULL;
  40. return iram_phys_to_virt(*dma_addr);
  41. }
  42. EXPORT_SYMBOL(iram_alloc);
  43. void iram_free(unsigned long addr, unsigned int size)
  44. {
  45. if (!iram_pool)
  46. return;
  47. gen_pool_free(iram_pool, addr, size);
  48. }
  49. EXPORT_SYMBOL(iram_free);
  50. int __init iram_init(unsigned long base, unsigned long size)
  51. {
  52. iram_phys_base = base;
  53. iram_pool = gen_pool_create(PAGE_SHIFT, -1);
  54. if (!iram_pool)
  55. return -ENOMEM;
  56. gen_pool_add(iram_pool, base, size, -1);
  57. iram_virt_base = ioremap(iram_phys_base, size);
  58. if (!iram_virt_base)
  59. return -EIO;
  60. pr_debug("i.MX IRAM pool: %ld KB@0x%p\n", size / 1024, iram_virt_base);
  61. return 0;
  62. }