sram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/genalloc.h>
  23. #include <linux/io.h>
  24. #include <linux/list_sort.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regmap.h>
  29. #include <linux/slab.h>
  30. #include <linux/mfd/syscon.h>
  31. #include <soc/at91/atmel-secumod.h>
  32. #define SRAM_GRANULARITY 32
  33. struct sram_partition {
  34. void __iomem *base;
  35. struct gen_pool *pool;
  36. struct bin_attribute battr;
  37. struct mutex lock;
  38. };
  39. struct sram_dev {
  40. struct device *dev;
  41. void __iomem *virt_base;
  42. struct gen_pool *pool;
  43. struct clk *clk;
  44. struct sram_partition *partition;
  45. u32 partitions;
  46. };
  47. struct sram_reserve {
  48. struct list_head list;
  49. u32 start;
  50. u32 size;
  51. bool export;
  52. bool pool;
  53. const char *label;
  54. };
  55. static ssize_t sram_read(struct file *filp, struct kobject *kobj,
  56. struct bin_attribute *attr,
  57. char *buf, loff_t pos, size_t count)
  58. {
  59. struct sram_partition *part;
  60. part = container_of(attr, struct sram_partition, battr);
  61. mutex_lock(&part->lock);
  62. memcpy_fromio(buf, part->base + pos, count);
  63. mutex_unlock(&part->lock);
  64. return count;
  65. }
  66. static ssize_t sram_write(struct file *filp, struct kobject *kobj,
  67. struct bin_attribute *attr,
  68. char *buf, loff_t pos, size_t count)
  69. {
  70. struct sram_partition *part;
  71. part = container_of(attr, struct sram_partition, battr);
  72. mutex_lock(&part->lock);
  73. memcpy_toio(part->base + pos, buf, count);
  74. mutex_unlock(&part->lock);
  75. return count;
  76. }
  77. static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block,
  78. phys_addr_t start, struct sram_partition *part)
  79. {
  80. int ret;
  81. part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  82. NUMA_NO_NODE, block->label);
  83. if (IS_ERR(part->pool))
  84. return PTR_ERR(part->pool);
  85. ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start,
  86. block->size, NUMA_NO_NODE);
  87. if (ret < 0) {
  88. dev_err(sram->dev, "failed to register subpool: %d\n", ret);
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block,
  94. phys_addr_t start, struct sram_partition *part)
  95. {
  96. sysfs_bin_attr_init(&part->battr);
  97. part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL,
  98. "%llx.sram",
  99. (unsigned long long)start);
  100. if (!part->battr.attr.name)
  101. return -ENOMEM;
  102. part->battr.attr.mode = S_IRUSR | S_IWUSR;
  103. part->battr.read = sram_read;
  104. part->battr.write = sram_write;
  105. part->battr.size = block->size;
  106. return device_create_bin_file(sram->dev, &part->battr);
  107. }
  108. static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block,
  109. phys_addr_t start)
  110. {
  111. int ret;
  112. struct sram_partition *part = &sram->partition[sram->partitions];
  113. mutex_init(&part->lock);
  114. part->base = sram->virt_base + block->start;
  115. if (block->pool) {
  116. ret = sram_add_pool(sram, block, start, part);
  117. if (ret)
  118. return ret;
  119. }
  120. if (block->export) {
  121. ret = sram_add_export(sram, block, start, part);
  122. if (ret)
  123. return ret;
  124. }
  125. sram->partitions++;
  126. return 0;
  127. }
  128. static void sram_free_partitions(struct sram_dev *sram)
  129. {
  130. struct sram_partition *part;
  131. if (!sram->partitions)
  132. return;
  133. part = &sram->partition[sram->partitions - 1];
  134. for (; sram->partitions; sram->partitions--, part--) {
  135. if (part->battr.size)
  136. device_remove_bin_file(sram->dev, &part->battr);
  137. if (part->pool &&
  138. gen_pool_avail(part->pool) < gen_pool_size(part->pool))
  139. dev_err(sram->dev, "removed pool while SRAM allocated\n");
  140. }
  141. }
  142. static int sram_reserve_cmp(void *priv, struct list_head *a,
  143. struct list_head *b)
  144. {
  145. struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
  146. struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
  147. return ra->start - rb->start;
  148. }
  149. static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
  150. {
  151. struct device_node *np = sram->dev->of_node, *child;
  152. unsigned long size, cur_start, cur_size;
  153. struct sram_reserve *rblocks, *block;
  154. struct list_head reserve_list;
  155. unsigned int nblocks, exports = 0;
  156. const char *label;
  157. int ret = 0;
  158. INIT_LIST_HEAD(&reserve_list);
  159. size = resource_size(res);
  160. /*
  161. * We need an additional block to mark the end of the memory region
  162. * after the reserved blocks from the dt are processed.
  163. */
  164. nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
  165. rblocks = kzalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
  166. if (!rblocks)
  167. return -ENOMEM;
  168. block = &rblocks[0];
  169. for_each_available_child_of_node(np, child) {
  170. struct resource child_res;
  171. ret = of_address_to_resource(child, 0, &child_res);
  172. if (ret < 0) {
  173. dev_err(sram->dev,
  174. "could not get address for node %s\n",
  175. child->full_name);
  176. goto err_chunks;
  177. }
  178. if (child_res.start < res->start || child_res.end > res->end) {
  179. dev_err(sram->dev,
  180. "reserved block %s outside the sram area\n",
  181. child->full_name);
  182. ret = -EINVAL;
  183. goto err_chunks;
  184. }
  185. block->start = child_res.start - res->start;
  186. block->size = resource_size(&child_res);
  187. list_add_tail(&block->list, &reserve_list);
  188. if (of_find_property(child, "export", NULL))
  189. block->export = true;
  190. if (of_find_property(child, "pool", NULL))
  191. block->pool = true;
  192. if ((block->export || block->pool) && block->size) {
  193. exports++;
  194. label = NULL;
  195. ret = of_property_read_string(child, "label", &label);
  196. if (ret && ret != -EINVAL) {
  197. dev_err(sram->dev,
  198. "%s has invalid label name\n",
  199. child->full_name);
  200. goto err_chunks;
  201. }
  202. if (!label)
  203. label = child->name;
  204. block->label = devm_kstrdup(sram->dev,
  205. label, GFP_KERNEL);
  206. if (!block->label)
  207. goto err_chunks;
  208. dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
  209. block->export ? "exported " : "", block->label,
  210. block->start, block->start + block->size);
  211. } else {
  212. dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
  213. block->start, block->start + block->size);
  214. }
  215. block++;
  216. }
  217. child = NULL;
  218. /* the last chunk marks the end of the region */
  219. rblocks[nblocks - 1].start = size;
  220. rblocks[nblocks - 1].size = 0;
  221. list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
  222. list_sort(NULL, &reserve_list, sram_reserve_cmp);
  223. if (exports) {
  224. sram->partition = devm_kzalloc(sram->dev,
  225. exports * sizeof(*sram->partition),
  226. GFP_KERNEL);
  227. if (!sram->partition) {
  228. ret = -ENOMEM;
  229. goto err_chunks;
  230. }
  231. }
  232. cur_start = 0;
  233. list_for_each_entry(block, &reserve_list, list) {
  234. /* can only happen if sections overlap */
  235. if (block->start < cur_start) {
  236. dev_err(sram->dev,
  237. "block at 0x%x starts after current offset 0x%lx\n",
  238. block->start, cur_start);
  239. ret = -EINVAL;
  240. sram_free_partitions(sram);
  241. goto err_chunks;
  242. }
  243. if ((block->export || block->pool) && block->size) {
  244. ret = sram_add_partition(sram, block,
  245. res->start + block->start);
  246. if (ret) {
  247. sram_free_partitions(sram);
  248. goto err_chunks;
  249. }
  250. }
  251. /* current start is in a reserved block, so continue after it */
  252. if (block->start == cur_start) {
  253. cur_start = block->start + block->size;
  254. continue;
  255. }
  256. /*
  257. * allocate the space between the current starting
  258. * address and the following reserved block, or the
  259. * end of the region.
  260. */
  261. cur_size = block->start - cur_start;
  262. dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
  263. cur_start, cur_start + cur_size);
  264. ret = gen_pool_add_virt(sram->pool,
  265. (unsigned long)sram->virt_base + cur_start,
  266. res->start + cur_start, cur_size, -1);
  267. if (ret < 0) {
  268. sram_free_partitions(sram);
  269. goto err_chunks;
  270. }
  271. /* next allocation after this reserved block */
  272. cur_start = block->start + block->size;
  273. }
  274. err_chunks:
  275. if (child)
  276. of_node_put(child);
  277. kfree(rblocks);
  278. return ret;
  279. }
  280. static int atmel_securam_wait(void)
  281. {
  282. struct regmap *regmap;
  283. u32 val;
  284. regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
  285. if (IS_ERR(regmap))
  286. return -ENODEV;
  287. return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
  288. val & AT91_SECUMOD_RAMRDY_READY,
  289. 10000, 500000);
  290. }
  291. static const struct of_device_id sram_dt_ids[] = {
  292. { .compatible = "mmio-sram" },
  293. { .compatible = "atmel,sama5d2-securam", .data = atmel_securam_wait },
  294. {}
  295. };
  296. static int sram_probe(struct platform_device *pdev)
  297. {
  298. struct sram_dev *sram;
  299. struct resource *res;
  300. size_t size;
  301. int ret;
  302. int (*init_func)(void);
  303. sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
  304. if (!sram)
  305. return -ENOMEM;
  306. sram->dev = &pdev->dev;
  307. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  308. if (!res) {
  309. dev_err(sram->dev, "found no memory resource\n");
  310. return -EINVAL;
  311. }
  312. size = resource_size(res);
  313. if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
  314. dev_err(sram->dev, "could not request region for resource\n");
  315. return -EBUSY;
  316. }
  317. if (of_property_read_bool(pdev->dev.of_node, "no-memory-wc"))
  318. sram->virt_base = devm_ioremap(sram->dev, res->start, size);
  319. else
  320. sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
  321. if (!sram->virt_base)
  322. return -ENOMEM;
  323. sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
  324. NUMA_NO_NODE, NULL);
  325. if (IS_ERR(sram->pool))
  326. return PTR_ERR(sram->pool);
  327. ret = sram_reserve_regions(sram, res);
  328. if (ret)
  329. return ret;
  330. sram->clk = devm_clk_get(sram->dev, NULL);
  331. if (IS_ERR(sram->clk))
  332. sram->clk = NULL;
  333. else
  334. clk_prepare_enable(sram->clk);
  335. platform_set_drvdata(pdev, sram);
  336. init_func = of_device_get_match_data(&pdev->dev);
  337. if (init_func) {
  338. ret = init_func();
  339. if (ret)
  340. return ret;
  341. }
  342. dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
  343. gen_pool_size(sram->pool) / 1024, sram->virt_base);
  344. return 0;
  345. }
  346. static int sram_remove(struct platform_device *pdev)
  347. {
  348. struct sram_dev *sram = platform_get_drvdata(pdev);
  349. sram_free_partitions(sram);
  350. if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
  351. dev_err(sram->dev, "removed while SRAM allocated\n");
  352. if (sram->clk)
  353. clk_disable_unprepare(sram->clk);
  354. return 0;
  355. }
  356. static struct platform_driver sram_driver = {
  357. .driver = {
  358. .name = "sram",
  359. .of_match_table = sram_dt_ids,
  360. },
  361. .probe = sram_probe,
  362. .remove = sram_remove,
  363. };
  364. static int __init sram_init(void)
  365. {
  366. return platform_driver_register(&sram_driver);
  367. }
  368. postcore_initcall(sram_init);