sram.c 4.3 KB

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