fman_muram.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2008-2015 Freescale Semiconductor Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of Freescale Semiconductor nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. *
  16. * ALTERNATIVELY, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL") as published by the Free Software
  18. * Foundation, either version 2 of that License or (at your option) any
  19. * later version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  25. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "fman_muram.h"
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/genalloc.h>
  36. struct muram_info {
  37. struct gen_pool *pool;
  38. void __iomem *vbase;
  39. size_t size;
  40. phys_addr_t pbase;
  41. };
  42. static unsigned long fman_muram_vbase_to_offset(struct muram_info *muram,
  43. unsigned long vaddr)
  44. {
  45. return vaddr - (unsigned long)muram->vbase;
  46. }
  47. /**
  48. * fman_muram_init
  49. * @base: Pointer to base of memory mapped FM-MURAM.
  50. * @size: Size of the FM-MURAM partition.
  51. *
  52. * Creates partition in the MURAM.
  53. * The routine returns a pointer to the MURAM partition.
  54. * This pointer must be passed as to all other FM-MURAM function calls.
  55. * No actual initialization or configuration of FM_MURAM hardware is done by
  56. * this routine.
  57. *
  58. * Return: pointer to FM-MURAM object, or NULL for Failure.
  59. */
  60. struct muram_info *fman_muram_init(phys_addr_t base, size_t size)
  61. {
  62. struct muram_info *muram;
  63. void __iomem *vaddr;
  64. int ret;
  65. muram = kzalloc(sizeof(*muram), GFP_KERNEL);
  66. if (!muram)
  67. return NULL;
  68. muram->pool = gen_pool_create(ilog2(64), -1);
  69. if (!muram->pool) {
  70. pr_err("%s(): MURAM pool create failed\n", __func__);
  71. goto muram_free;
  72. }
  73. vaddr = ioremap(base, size);
  74. if (!vaddr) {
  75. pr_err("%s(): MURAM ioremap failed\n", __func__);
  76. goto pool_destroy;
  77. }
  78. ret = gen_pool_add_virt(muram->pool, (unsigned long)vaddr,
  79. base, size, -1);
  80. if (ret < 0) {
  81. pr_err("%s(): MURAM pool add failed\n", __func__);
  82. iounmap(vaddr);
  83. goto pool_destroy;
  84. }
  85. memset_io(vaddr, 0, (int)size);
  86. muram->vbase = vaddr;
  87. muram->pbase = base;
  88. return muram;
  89. pool_destroy:
  90. gen_pool_destroy(muram->pool);
  91. muram_free:
  92. kfree(muram);
  93. return NULL;
  94. }
  95. /**
  96. * fman_muram_offset_to_vbase
  97. * @muram: FM-MURAM module pointer.
  98. * @offset: the offset of the memory block
  99. *
  100. * Gives the address of the memory region from specific offset
  101. *
  102. * Return: The address of the memory block
  103. */
  104. unsigned long fman_muram_offset_to_vbase(struct muram_info *muram,
  105. unsigned long offset)
  106. {
  107. return offset + (unsigned long)muram->vbase;
  108. }
  109. /**
  110. * fman_muram_alloc
  111. * @muram: FM-MURAM module pointer.
  112. * @size: Size of the memory to be allocated.
  113. *
  114. * Allocate some memory from FM-MURAM partition.
  115. *
  116. * Return: address of the allocated memory; NULL otherwise.
  117. */
  118. unsigned long fman_muram_alloc(struct muram_info *muram, size_t size)
  119. {
  120. unsigned long vaddr;
  121. vaddr = gen_pool_alloc(muram->pool, size);
  122. if (!vaddr)
  123. return -ENOMEM;
  124. memset_io((void __iomem *)vaddr, 0, size);
  125. return fman_muram_vbase_to_offset(muram, vaddr);
  126. }
  127. /**
  128. * fman_muram_free_mem
  129. * muram: FM-MURAM module pointer.
  130. * offset: offset of the memory region to be freed.
  131. * size: size of the memory to be freed.
  132. *
  133. * Free an allocated memory from FM-MURAM partition.
  134. */
  135. void fman_muram_free_mem(struct muram_info *muram, unsigned long offset,
  136. size_t size)
  137. {
  138. unsigned long addr = fman_muram_offset_to_vbase(muram, offset);
  139. gen_pool_free(muram->pool, addr, size);
  140. }