sram.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mach-mmp/sram.c
  4. *
  5. * based on mach-davinci/sram.c - DaVinci simple SRAM allocator
  6. *
  7. * Copyright (c) 2011 Marvell Semiconductors Inc.
  8. * All Rights Reserved
  9. *
  10. * Add for mmp sram support - Leo Yan <[email protected]>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/genalloc.h>
  20. #include <linux/platform_data/dma-mmp_tdma.h>
  21. struct sram_bank_info {
  22. char *pool_name;
  23. struct gen_pool *gpool;
  24. int granularity;
  25. phys_addr_t sram_phys;
  26. void __iomem *sram_virt;
  27. u32 sram_size;
  28. struct list_head node;
  29. };
  30. static DEFINE_MUTEX(sram_lock);
  31. static LIST_HEAD(sram_bank_list);
  32. struct gen_pool *sram_get_gpool(char *pool_name)
  33. {
  34. struct sram_bank_info *info = NULL;
  35. if (!pool_name)
  36. return NULL;
  37. mutex_lock(&sram_lock);
  38. list_for_each_entry(info, &sram_bank_list, node)
  39. if (!strcmp(pool_name, info->pool_name))
  40. break;
  41. mutex_unlock(&sram_lock);
  42. if (&info->node == &sram_bank_list)
  43. return NULL;
  44. return info->gpool;
  45. }
  46. EXPORT_SYMBOL(sram_get_gpool);
  47. static int sram_probe(struct platform_device *pdev)
  48. {
  49. struct sram_platdata *pdata = pdev->dev.platform_data;
  50. struct sram_bank_info *info;
  51. struct resource *res;
  52. int ret = 0;
  53. if (!pdata || !pdata->pool_name)
  54. return -ENODEV;
  55. info = kzalloc(sizeof(*info), GFP_KERNEL);
  56. if (!info)
  57. return -ENOMEM;
  58. platform_set_drvdata(pdev, info);
  59. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  60. if (res == NULL) {
  61. dev_err(&pdev->dev, "no memory resource defined\n");
  62. ret = -ENODEV;
  63. goto out;
  64. }
  65. if (!resource_size(res))
  66. return 0;
  67. info->sram_phys = (phys_addr_t)res->start;
  68. info->sram_size = resource_size(res);
  69. info->sram_virt = ioremap(info->sram_phys, info->sram_size);
  70. info->pool_name = kstrdup(pdata->pool_name, GFP_KERNEL);
  71. info->granularity = pdata->granularity;
  72. info->gpool = gen_pool_create(ilog2(info->granularity), -1);
  73. if (!info->gpool) {
  74. dev_err(&pdev->dev, "create pool failed\n");
  75. ret = -ENOMEM;
  76. goto create_pool_err;
  77. }
  78. ret = gen_pool_add_virt(info->gpool, (unsigned long)info->sram_virt,
  79. info->sram_phys, info->sram_size, -1);
  80. if (ret < 0) {
  81. dev_err(&pdev->dev, "add new chunk failed\n");
  82. ret = -ENOMEM;
  83. goto add_chunk_err;
  84. }
  85. mutex_lock(&sram_lock);
  86. list_add(&info->node, &sram_bank_list);
  87. mutex_unlock(&sram_lock);
  88. dev_info(&pdev->dev, "initialized\n");
  89. return 0;
  90. add_chunk_err:
  91. gen_pool_destroy(info->gpool);
  92. create_pool_err:
  93. iounmap(info->sram_virt);
  94. kfree(info->pool_name);
  95. out:
  96. kfree(info);
  97. return ret;
  98. }
  99. static int sram_remove(struct platform_device *pdev)
  100. {
  101. struct sram_bank_info *info;
  102. info = platform_get_drvdata(pdev);
  103. if (info->sram_size) {
  104. mutex_lock(&sram_lock);
  105. list_del(&info->node);
  106. mutex_unlock(&sram_lock);
  107. gen_pool_destroy(info->gpool);
  108. iounmap(info->sram_virt);
  109. kfree(info->pool_name);
  110. }
  111. kfree(info);
  112. return 0;
  113. }
  114. static const struct platform_device_id sram_id_table[] = {
  115. { "asram", MMP_ASRAM },
  116. { "isram", MMP_ISRAM },
  117. { }
  118. };
  119. static struct platform_driver sram_driver = {
  120. .probe = sram_probe,
  121. .remove = sram_remove,
  122. .driver = {
  123. .name = "mmp-sram",
  124. },
  125. .id_table = sram_id_table,
  126. };
  127. static int __init sram_init(void)
  128. {
  129. return platform_driver_register(&sram_driver);
  130. }
  131. core_initcall(sram_init);
  132. MODULE_LICENSE("GPL");