sram.c 3.5 KB

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