sram.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Simple memory allocator for on-board SRAM
  4. *
  5. * Maintainer : Sylvain Munaut <[email protected]>
  6. *
  7. * Copyright (C) 2005 Sylvain Munaut <[email protected]>
  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. u64 regaddr64, size64;
  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. regaddr_p = of_get_address(sram_node, 0, &size64, NULL);
  49. if (!regaddr_p) {
  50. printk(KERN_ERR "%s: bcom_sram_init: "
  51. "Invalid device node !\n", owner);
  52. rv = -EINVAL;
  53. goto error_free;
  54. }
  55. regaddr64 = of_translate_address(sram_node, regaddr_p);
  56. bcom_sram->base_phys = (phys_addr_t) regaddr64;
  57. bcom_sram->size = (unsigned int) size64;
  58. /* Request region */
  59. if (!request_mem_region(bcom_sram->base_phys, bcom_sram->size, owner)) {
  60. printk(KERN_ERR "%s: bcom_sram_init: "
  61. "Couldn't request region !\n", owner);
  62. rv = -EBUSY;
  63. goto error_free;
  64. }
  65. /* Map SRAM */
  66. /* sram is not really __iomem */
  67. bcom_sram->base_virt = (void*) ioremap(bcom_sram->base_phys, bcom_sram->size);
  68. if (!bcom_sram->base_virt) {
  69. printk(KERN_ERR "%s: bcom_sram_init: "
  70. "Map error SRAM zone 0x%08lx (0x%0x)!\n",
  71. owner, (long)bcom_sram->base_phys, bcom_sram->size );
  72. rv = -ENOMEM;
  73. goto error_release;
  74. }
  75. /* Create an rheap (defaults to 32 bits word alignment) */
  76. bcom_sram->rh = rh_create(4);
  77. /* Attach the free zones */
  78. #if 0
  79. /* Currently disabled ... for future use only */
  80. reg_addr_p = of_get_property(sram_node, "available", &psize);
  81. #else
  82. regaddr_p = NULL;
  83. psize = 0;
  84. #endif
  85. if (!regaddr_p || !psize) {
  86. /* Attach the whole zone */
  87. rh_attach_region(bcom_sram->rh, 0, bcom_sram->size);
  88. } else {
  89. /* Attach each zone independently */
  90. while (psize >= 2 * sizeof(u32)) {
  91. phys_addr_t zbase = of_translate_address(sram_node, regaddr_p);
  92. rh_attach_region(bcom_sram->rh, zbase - bcom_sram->base_phys, regaddr_p[1]);
  93. regaddr_p += 2;
  94. psize -= 2 * sizeof(u32);
  95. }
  96. }
  97. /* Init our spinlock */
  98. spin_lock_init(&bcom_sram->lock);
  99. return 0;
  100. error_release:
  101. release_mem_region(bcom_sram->base_phys, bcom_sram->size);
  102. error_free:
  103. kfree(bcom_sram);
  104. bcom_sram = NULL;
  105. return rv;
  106. }
  107. EXPORT_SYMBOL_GPL(bcom_sram_init);
  108. void bcom_sram_cleanup(void)
  109. {
  110. /* Free resources */
  111. if (bcom_sram) {
  112. rh_destroy(bcom_sram->rh);
  113. iounmap((void __iomem *)bcom_sram->base_virt);
  114. release_mem_region(bcom_sram->base_phys, bcom_sram->size);
  115. kfree(bcom_sram);
  116. bcom_sram = NULL;
  117. }
  118. }
  119. EXPORT_SYMBOL_GPL(bcom_sram_cleanup);
  120. void* bcom_sram_alloc(int size, int align, phys_addr_t *phys)
  121. {
  122. unsigned long offset;
  123. spin_lock(&bcom_sram->lock);
  124. offset = rh_alloc_align(bcom_sram->rh, size, align, NULL);
  125. spin_unlock(&bcom_sram->lock);
  126. if (IS_ERR_VALUE(offset))
  127. return NULL;
  128. *phys = bcom_sram->base_phys + offset;
  129. return bcom_sram->base_virt + offset;
  130. }
  131. EXPORT_SYMBOL_GPL(bcom_sram_alloc);
  132. void bcom_sram_free(void *ptr)
  133. {
  134. unsigned long offset;
  135. if (!ptr)
  136. return;
  137. offset = ptr - bcom_sram->base_virt;
  138. spin_lock(&bcom_sram->lock);
  139. rh_free(bcom_sram->rh, offset);
  140. spin_unlock(&bcom_sram->lock);
  141. }
  142. EXPORT_SYMBOL_GPL(bcom_sram_free);