037-mtd-add-SMEM-parser-for-QCOM-platforms.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. From 0501f76b138cf1dc11a313bb7a094da524b79337 Mon Sep 17 00:00:00 2001
  2. From: Mathieu Olivari <mathieu@codeaurora.org>
  3. Date: Thu, 13 Aug 2015 09:53:14 -0700
  4. Subject: [PATCH 3/3] mtd: add SMEM parser for QCOM platforms
  5. On QCOM platforms using MTD devices storage (such as IPQ806x), SMEM is
  6. used to store partition layout. This new parser can now be used to read
  7. SMEM and use it to register an MTD layout according to its content.
  8. Signed-off-by: Mathieu Olivari <mathieu@codeaurora.org>
  9. ---
  10. drivers/mtd/Kconfig | 7 ++
  11. drivers/mtd/Makefile | 1 +
  12. drivers/mtd/qcom_smem_part.c | 231 +++++++++++++++++++++++++++++++++++++++++++
  13. 3 files changed, 239 insertions(+)
  14. create mode 100644 drivers/mtd/qcom_smem_part.c
  15. --- a/drivers/mtd/Kconfig
  16. +++ b/drivers/mtd/Kconfig
  17. @@ -200,6 +200,13 @@ config MTD_MYLOADER_PARTS
  18. You will still need the parsing functions to be called by the driver
  19. for your particular device. It won't happen automatically.
  20. +config MTD_QCOM_SMEM_PARTS
  21. + tristate "QCOM SMEM partitioning support"
  22. + depends on QCOM_SMEM
  23. + help
  24. + This provides partitions parser for QCOM devices using SMEM
  25. + such as IPQ806x.
  26. +
  27. comment "User Modules And Translation Layers"
  28. #
  29. --- /dev/null
  30. +++ b/drivers/mtd/qcom_smem_part.c
  31. @@ -0,0 +1,231 @@
  32. +/*
  33. + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  34. + *
  35. + * This program is free software; you can redistribute it and/or modify
  36. + * it under the terms of the GNU General Public License version 2 and
  37. + * only version 2 as published by the Free Software Foundation.
  38. + *
  39. + * This program is distributed in the hope that it will be useful,
  40. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. + * GNU General Public License for more details.
  43. + */
  44. +
  45. +#include <linux/kernel.h>
  46. +#include <linux/device.h>
  47. +#include <linux/slab.h>
  48. +
  49. +#include <linux/mtd/mtd.h>
  50. +#include <linux/mtd/partitions.h>
  51. +#include <linux/spi/spi.h>
  52. +#include <linux/module.h>
  53. +
  54. +#include <linux/soc/qcom/smem.h>
  55. +
  56. +/* Processor/host identifier for the application processor */
  57. +#define SMEM_HOST_APPS 0
  58. +
  59. +/* SMEM items index */
  60. +#define SMEM_AARM_PARTITION_TABLE 9
  61. +#define SMEM_BOOT_FLASH_TYPE 421
  62. +#define SMEM_BOOT_FLASH_BLOCK_SIZE 424
  63. +
  64. +/* SMEM Flash types */
  65. +#define SMEM_FLASH_NAND 2
  66. +#define SMEM_FLASH_SPI 6
  67. +
  68. +#define SMEM_PART_NAME_SZ 16
  69. +#define SMEM_PARTS_MAX 32
  70. +
  71. +struct smem_partition {
  72. + char name[SMEM_PART_NAME_SZ];
  73. + __le32 start;
  74. + __le32 size;
  75. + __le32 attr;
  76. +};
  77. +
  78. +struct smem_partition_table {
  79. + u8 magic[8];
  80. + __le32 version;
  81. + __le32 len;
  82. + struct smem_partition parts[SMEM_PARTS_MAX];
  83. +};
  84. +
  85. +/* SMEM Magic values in partition table */
  86. +static const u8 SMEM_PTABLE_MAGIC[] = {
  87. + 0xaa, 0x73, 0xee, 0x55,
  88. + 0xdb, 0xbd, 0x5e, 0xe3,
  89. +};
  90. +
  91. +static int qcom_smem_get_flash_blksz(u64 **smem_blksz)
  92. +{
  93. + int ret;
  94. + size_t size;
  95. +
  96. + ret = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_BLOCK_SIZE,
  97. + (void **) smem_blksz, &size);
  98. +
  99. + if (ret < 0) {
  100. + pr_err("Unable to read flash blksz from SMEM\n");
  101. + return -ENOENT;
  102. + }
  103. +
  104. + if (size != sizeof(**smem_blksz)) {
  105. + pr_err("Invalid flash blksz size in SMEM\n");
  106. + return -EINVAL;
  107. + }
  108. +
  109. + return 0;
  110. +}
  111. +
  112. +static int qcom_smem_get_flash_type(u64 **smem_flash_type)
  113. +{
  114. + int ret;
  115. + size_t size;
  116. +
  117. + ret = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_TYPE,
  118. + (void **) smem_flash_type, &size);
  119. +
  120. + if (ret < 0) {
  121. + pr_err("Unable to read flash type from SMEM\n");
  122. + return -ENOENT;
  123. + }
  124. +
  125. + if (size != sizeof(**smem_flash_type)) {
  126. + pr_err("Invalid flash type size in SMEM\n");
  127. + return -EINVAL;
  128. + }
  129. +
  130. + return 0;
  131. +}
  132. +
  133. +static int qcom_smem_get_flash_partitions(struct smem_partition_table **pparts)
  134. +{
  135. + int ret;
  136. + size_t size;
  137. +
  138. + ret = qcom_smem_get(SMEM_HOST_APPS, SMEM_AARM_PARTITION_TABLE,
  139. + (void **) pparts, &size);
  140. +
  141. + if (ret < 0) {
  142. + pr_err("Unable to read partition table from SMEM\n");
  143. + return -ENOENT;
  144. + }
  145. +
  146. + return 0;
  147. +}
  148. +
  149. +static int of_dev_node_match(struct device *dev, void *data)
  150. +{
  151. + return dev->of_node == data;
  152. +}
  153. +
  154. +static bool is_spi_device(struct device_node *np)
  155. +{
  156. + struct device *dev;
  157. +
  158. + dev = bus_find_device(&spi_bus_type, NULL, np, of_dev_node_match);
  159. + if (!dev)
  160. + return false;
  161. +
  162. + put_device(dev);
  163. + return true;
  164. +}
  165. +
  166. +static int parse_qcom_smem_partitions(struct mtd_info *master,
  167. + struct mtd_partition **pparts,
  168. + struct mtd_part_parser_data *data)
  169. +{
  170. + struct smem_partition_table *smem_parts;
  171. + u64 *smem_flash_type, *smem_blksz;
  172. + struct mtd_partition *mtd_parts;
  173. + struct device_node *of_node = data->of_node;
  174. + int i, ret;
  175. +
  176. + /*
  177. + * SMEM will only store the partition table of the boot device.
  178. + * If this is not the boot device, do not return any partition.
  179. + */
  180. + ret = qcom_smem_get_flash_type(&smem_flash_type);
  181. + if (ret < 0)
  182. + return ret;
  183. +
  184. + if ((*smem_flash_type == SMEM_FLASH_NAND && !mtd_type_is_nand(master))
  185. + || (*smem_flash_type == SMEM_FLASH_SPI && !is_spi_device(of_node)))
  186. + return 0;
  187. +
  188. + /*
  189. + * Just for sanity purpose, make sure the block size in SMEM matches the
  190. + * block size of the MTD device
  191. + */
  192. + ret = qcom_smem_get_flash_blksz(&smem_blksz);
  193. + if (ret < 0)
  194. + return ret;
  195. +
  196. + if (*smem_blksz != master->erasesize) {
  197. + pr_err("SMEM block size differs from MTD block size\n");
  198. + return -EINVAL;
  199. + }
  200. +
  201. + /* Get partition pointer from SMEM */
  202. + ret = qcom_smem_get_flash_partitions(&smem_parts);
  203. + if (ret < 0)
  204. + return ret;
  205. +
  206. + if (memcmp(SMEM_PTABLE_MAGIC, smem_parts->magic,
  207. + sizeof(SMEM_PTABLE_MAGIC))) {
  208. + pr_err("SMEM partition magic invalid\n");
  209. + return -EINVAL;
  210. + }
  211. +
  212. + /* Allocate and populate the mtd structures */
  213. + mtd_parts = kcalloc(le32_to_cpu(smem_parts->len), sizeof(*mtd_parts),
  214. + GFP_KERNEL);
  215. + if (!mtd_parts)
  216. + return -ENOMEM;
  217. +
  218. + for (i = 0; i < smem_parts->len; i++) {
  219. + struct smem_partition *s_part = &smem_parts->parts[i];
  220. + struct mtd_partition *m_part = &mtd_parts[i];
  221. +
  222. + m_part->name = s_part->name;
  223. + m_part->size = le32_to_cpu(s_part->size) * (*smem_blksz);
  224. + m_part->offset = le32_to_cpu(s_part->start) * (*smem_blksz);
  225. +
  226. + /*
  227. + * The last SMEM partition may have its size marked as
  228. + * something like 0xffffffff, which means "until the end of the
  229. + * flash device". In this case, truncate it.
  230. + */
  231. + if (m_part->offset + m_part->size > master->size)
  232. + m_part->size = master->size - m_part->offset;
  233. + }
  234. +
  235. + *pparts = mtd_parts;
  236. +
  237. + return smem_parts->len;
  238. +}
  239. +
  240. +static struct mtd_part_parser qcom_smem_parser = {
  241. + .owner = THIS_MODULE,
  242. + .parse_fn = parse_qcom_smem_partitions,
  243. + .name = "qcom-smem",
  244. +};
  245. +
  246. +static int __init qcom_smem_parser_init(void)
  247. +{
  248. + register_mtd_parser(&qcom_smem_parser);
  249. + return 0;
  250. +}
  251. +
  252. +static void __exit qcom_smem_parser_exit(void)
  253. +{
  254. + deregister_mtd_parser(&qcom_smem_parser);
  255. +}
  256. +
  257. +module_init(qcom_smem_parser_init);
  258. +module_exit(qcom_smem_parser_exit);
  259. +
  260. +MODULE_LICENSE("GPL");
  261. +MODULE_AUTHOR("Mathieu Olivari <mathieu@codeaurora.org>");
  262. +MODULE_DESCRIPTION("Parsing code for SMEM based partition tables");
  263. --- a/drivers/mtd/Makefile
  264. +++ b/drivers/mtd/Makefile
  265. @@ -16,6 +16,7 @@ obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o
  266. obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o
  267. obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o
  268. obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
  269. +obj-$(CONFIG_MTD_QCOM_SMEM_PARTS) += qcom_smem_part.o
  270. # 'Users' - code which presents functionality to userspace.
  271. obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o