sram.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Generic on-chip SRAM allocation driver
  3. *
  4. * Copyright (C) 2012 Philipp Zabel, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/clk.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/of.h>
  26. #include <linux/of_address.h>
  27. #include <linux/list.h>
  28. #include <linux/list_sort.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/genalloc.h>
  33. #define SRAM_GRANULARITY 32
  34. struct sram_dev {
  35. struct gen_pool *pool;
  36. struct clk *clk;
  37. };
  38. struct sram_reserve {
  39. struct list_head list;
  40. u32 start;
  41. u32 size;
  42. };
  43. static int sram_reserve_cmp(void *priv, struct list_head *a,
  44. struct list_head *b)
  45. {
  46. struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
  47. struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
  48. return ra->start - rb->start;
  49. }
  50. static int sram_probe(struct platform_device *pdev)
  51. {
  52. void __iomem *virt_base;
  53. struct sram_dev *sram;
  54. struct resource *res;
  55. struct device_node *np = pdev->dev.of_node, *child;
  56. unsigned long size, cur_start, cur_size;
  57. struct sram_reserve *rblocks, *block;
  58. struct list_head reserve_list;
  59. unsigned int nblocks;
  60. int ret;
  61. INIT_LIST_HEAD(&reserve_list);
  62. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  63. virt_base = devm_ioremap_resource(&pdev->dev, res);
  64. if (IS_ERR(virt_base))
  65. return PTR_ERR(virt_base);
  66. size = resource_size(res);
  67. sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
  68. if (!sram)
  69. return -ENOMEM;
  70. sram->clk = devm_clk_get(&pdev->dev, NULL);
  71. if (IS_ERR(sram->clk))
  72. sram->clk = NULL;
  73. else
  74. clk_prepare_enable(sram->clk);
  75. sram->pool = devm_gen_pool_create(&pdev->dev, ilog2(SRAM_GRANULARITY), -1);
  76. if (!sram->pool)
  77. return -ENOMEM;
  78. /*
  79. * We need an additional block to mark the end of the memory region
  80. * after the reserved blocks from the dt are processed.
  81. */
  82. nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
  83. rblocks = kmalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
  84. if (!rblocks) {
  85. ret = -ENOMEM;
  86. goto err_alloc;
  87. }
  88. block = &rblocks[0];
  89. for_each_available_child_of_node(np, child) {
  90. struct resource child_res;
  91. ret = of_address_to_resource(child, 0, &child_res);
  92. if (ret < 0) {
  93. dev_err(&pdev->dev,
  94. "could not get address for node %s\n",
  95. child->full_name);
  96. goto err_chunks;
  97. }
  98. if (child_res.start < res->start || child_res.end > res->end) {
  99. dev_err(&pdev->dev,
  100. "reserved block %s outside the sram area\n",
  101. child->full_name);
  102. ret = -EINVAL;
  103. goto err_chunks;
  104. }
  105. block->start = child_res.start - res->start;
  106. block->size = resource_size(&child_res);
  107. list_add_tail(&block->list, &reserve_list);
  108. dev_dbg(&pdev->dev, "found reserved block 0x%x-0x%x\n",
  109. block->start,
  110. block->start + block->size);
  111. block++;
  112. }
  113. /* the last chunk marks the end of the region */
  114. rblocks[nblocks - 1].start = size;
  115. rblocks[nblocks - 1].size = 0;
  116. list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
  117. list_sort(NULL, &reserve_list, sram_reserve_cmp);
  118. cur_start = 0;
  119. list_for_each_entry(block, &reserve_list, list) {
  120. /* can only happen if sections overlap */
  121. if (block->start < cur_start) {
  122. dev_err(&pdev->dev,
  123. "block at 0x%x starts after current offset 0x%lx\n",
  124. block->start, cur_start);
  125. ret = -EINVAL;
  126. goto err_chunks;
  127. }
  128. /* current start is in a reserved block, so continue after it */
  129. if (block->start == cur_start) {
  130. cur_start = block->start + block->size;
  131. continue;
  132. }
  133. /*
  134. * allocate the space between the current starting
  135. * address and the following reserved block, or the
  136. * end of the region.
  137. */
  138. cur_size = block->start - cur_start;
  139. dev_dbg(&pdev->dev, "adding chunk 0x%lx-0x%lx\n",
  140. cur_start, cur_start + cur_size);
  141. ret = gen_pool_add_virt(sram->pool,
  142. (unsigned long)virt_base + cur_start,
  143. res->start + cur_start, cur_size, -1);
  144. if (ret < 0)
  145. goto err_chunks;
  146. /* next allocation after this reserved block */
  147. cur_start = block->start + block->size;
  148. }
  149. kfree(rblocks);
  150. platform_set_drvdata(pdev, sram);
  151. dev_dbg(&pdev->dev, "SRAM pool: %ld KiB @ 0x%p\n", size / 1024, virt_base);
  152. return 0;
  153. err_chunks:
  154. kfree(rblocks);
  155. err_alloc:
  156. if (sram->clk)
  157. clk_disable_unprepare(sram->clk);
  158. return ret;
  159. }
  160. static int sram_remove(struct platform_device *pdev)
  161. {
  162. struct sram_dev *sram = platform_get_drvdata(pdev);
  163. if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
  164. dev_dbg(&pdev->dev, "removed while SRAM allocated\n");
  165. if (sram->clk)
  166. clk_disable_unprepare(sram->clk);
  167. return 0;
  168. }
  169. #ifdef CONFIG_OF
  170. static struct of_device_id sram_dt_ids[] = {
  171. { .compatible = "mmio-sram" },
  172. {}
  173. };
  174. #endif
  175. static struct platform_driver sram_driver = {
  176. .driver = {
  177. .name = "sram",
  178. .of_match_table = of_match_ptr(sram_dt_ids),
  179. },
  180. .probe = sram_probe,
  181. .remove = sram_remove,
  182. };
  183. static int __init sram_init(void)
  184. {
  185. return platform_driver_register(&sram_driver);
  186. }
  187. postcore_initcall(sram_init);