sram.c 4.3 KB

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