sram.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Simple memory allocator for on-board SRAM
  3. *
  4. *
  5. * Maintainer : Sylvain Munaut <tnt@246tNt.com>
  6. *
  7. * Copyright (C) 2005 Sylvain Munaut <tnt@246tNt.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public License
  10. * version 2. This program is licensed "as is" without any warranty of any
  11. * kind, whether express or implied.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/export.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/string.h>
  19. #include <linux/ioport.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <asm/io.h>
  23. #include <asm/mmu.h>
  24. #include <linux/fsl/bestcomm/sram.h>
  25. /* Struct keeping our 'state' */
  26. struct bcom_sram *bcom_sram = NULL;
  27. EXPORT_SYMBOL_GPL(bcom_sram); /* needed for inline functions */
  28. /* ======================================================================== */
  29. /* Public API */
  30. /* ======================================================================== */
  31. /* DO NOT USE in interrupts, if needed in irq handler, we should use the
  32. _irqsave version of the spin_locks */
  33. int bcom_sram_init(struct device_node *sram_node, char *owner)
  34. {
  35. int rv;
  36. const u32 *regaddr_p;
  37. u64 regaddr64, size64;
  38. unsigned int psize;
  39. /* Create our state struct */
  40. if (bcom_sram) {
  41. printk(KERN_ERR "%s: bcom_sram_init: "
  42. "Already initialized !\n", owner);
  43. return -EBUSY;
  44. }
  45. bcom_sram = kmalloc(sizeof(struct bcom_sram), GFP_KERNEL);
  46. if (!bcom_sram) {
  47. printk(KERN_ERR "%s: bcom_sram_init: "
  48. "Couldn't allocate internal state !\n", owner);
  49. return -ENOMEM;
  50. }
  51. /* Get address and size of the sram */
  52. regaddr_p = of_get_address(sram_node, 0, &size64, NULL);
  53. if (!regaddr_p) {
  54. printk(KERN_ERR "%s: bcom_sram_init: "
  55. "Invalid device node !\n", owner);
  56. rv = -EINVAL;
  57. goto error_free;
  58. }
  59. regaddr64 = of_translate_address(sram_node, regaddr_p);
  60. bcom_sram->base_phys = (phys_addr_t) regaddr64;
  61. bcom_sram->size = (unsigned int) size64;
  62. /* Request region */
  63. if (!request_mem_region(bcom_sram->base_phys, bcom_sram->size, owner)) {
  64. printk(KERN_ERR "%s: bcom_sram_init: "
  65. "Couldn't request region !\n", owner);
  66. rv = -EBUSY;
  67. goto error_free;
  68. }
  69. /* Map SRAM */
  70. /* sram is not really __iomem */
  71. bcom_sram->base_virt = (void*) ioremap(bcom_sram->base_phys, bcom_sram->size);
  72. if (!bcom_sram->base_virt) {
  73. printk(KERN_ERR "%s: bcom_sram_init: "
  74. "Map error SRAM zone 0x%08lx (0x%0x)!\n",
  75. owner, (long)bcom_sram->base_phys, bcom_sram->size );
  76. rv = -ENOMEM;
  77. goto error_release;
  78. }
  79. /* Create an rheap (defaults to 32 bits word alignment) */
  80. bcom_sram->rh = rh_create(4);
  81. /* Attach the free zones */
  82. #if 0
  83. /* Currently disabled ... for future use only */
  84. reg_addr_p = of_get_property(sram_node, "available", &psize);
  85. #else
  86. regaddr_p = NULL;
  87. psize = 0;
  88. #endif
  89. if (!regaddr_p || !psize) {
  90. /* Attach the whole zone */
  91. rh_attach_region(bcom_sram->rh, 0, bcom_sram->size);
  92. } else {
  93. /* Attach each zone independently */
  94. while (psize >= 2 * sizeof(u32)) {
  95. phys_addr_t zbase = of_translate_address(sram_node, regaddr_p);
  96. rh_attach_region(bcom_sram->rh, zbase - bcom_sram->base_phys, regaddr_p[1]);
  97. regaddr_p += 2;
  98. psize -= 2 * sizeof(u32);
  99. }
  100. }
  101. /* Init our spinlock */
  102. spin_lock_init(&bcom_sram->lock);
  103. return 0;
  104. error_release:
  105. release_mem_region(bcom_sram->base_phys, bcom_sram->size);
  106. error_free:
  107. kfree(bcom_sram);
  108. bcom_sram = NULL;
  109. return rv;
  110. }
  111. EXPORT_SYMBOL_GPL(bcom_sram_init);
  112. void bcom_sram_cleanup(void)
  113. {
  114. /* Free resources */
  115. if (bcom_sram) {
  116. rh_destroy(bcom_sram->rh);
  117. iounmap((void __iomem *)bcom_sram->base_virt);
  118. release_mem_region(bcom_sram->base_phys, bcom_sram->size);
  119. kfree(bcom_sram);
  120. bcom_sram = NULL;
  121. }
  122. }
  123. EXPORT_SYMBOL_GPL(bcom_sram_cleanup);
  124. void* bcom_sram_alloc(int size, int align, phys_addr_t *phys)
  125. {
  126. unsigned long offset;
  127. spin_lock(&bcom_sram->lock);
  128. offset = rh_alloc_align(bcom_sram->rh, size, align, NULL);
  129. spin_unlock(&bcom_sram->lock);
  130. if (IS_ERR_VALUE(offset))
  131. return NULL;
  132. *phys = bcom_sram->base_phys + offset;
  133. return bcom_sram->base_virt + offset;
  134. }
  135. EXPORT_SYMBOL_GPL(bcom_sram_alloc);
  136. void bcom_sram_free(void *ptr)
  137. {
  138. unsigned long offset;
  139. if (!ptr)
  140. return;
  141. offset = ptr - bcom_sram->base_virt;
  142. spin_lock(&bcom_sram->lock);
  143. rh_free(bcom_sram->rh, offset);
  144. spin_unlock(&bcom_sram->lock);
  145. }
  146. EXPORT_SYMBOL_GPL(bcom_sram_free);