sram.c 3.9 KB

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