phram.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) ???? Jochen Schäuble <[email protected]>
  4. * Copyright (c) 2003-2004 Joern Engel <[email protected]>
  5. *
  6. * Usage:
  7. *
  8. * one commend line parameter per device, each in the form:
  9. * phram=<name>,<start>,<len>[,<erasesize>]
  10. * <name> may be up to 63 characters.
  11. * <start>, <len>, and <erasesize> can be octal, decimal or hexadecimal. If followed
  12. * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
  13. * gigabytes. <erasesize> is optional and defaults to PAGE_SIZE.
  14. *
  15. * Example:
  16. * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi,64Ki
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/io.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/slab.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <asm/div64.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of.h>
  31. struct phram_mtd_list {
  32. struct mtd_info mtd;
  33. struct list_head list;
  34. bool cached;
  35. };
  36. static LIST_HEAD(phram_list);
  37. static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
  38. {
  39. u_char *start = mtd->priv;
  40. memset(start + instr->addr, 0xff, instr->len);
  41. return 0;
  42. }
  43. static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
  44. size_t *retlen, void **virt, resource_size_t *phys)
  45. {
  46. *virt = mtd->priv + from;
  47. *retlen = len;
  48. return 0;
  49. }
  50. static int phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  51. {
  52. return 0;
  53. }
  54. static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
  55. size_t *retlen, u_char *buf)
  56. {
  57. u_char *start = mtd->priv;
  58. memcpy(buf, start + from, len);
  59. *retlen = len;
  60. return 0;
  61. }
  62. static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
  63. size_t *retlen, const u_char *buf)
  64. {
  65. u_char *start = mtd->priv;
  66. memcpy(start + to, buf, len);
  67. *retlen = len;
  68. return 0;
  69. }
  70. static int phram_map(struct phram_mtd_list *phram, phys_addr_t start, size_t len)
  71. {
  72. void *addr = NULL;
  73. if (phram->cached)
  74. addr = memremap(start, len, MEMREMAP_WB);
  75. else
  76. addr = (void __force *)ioremap(start, len);
  77. if (!addr)
  78. return -EIO;
  79. phram->mtd.priv = addr;
  80. return 0;
  81. }
  82. static void phram_unmap(struct phram_mtd_list *phram)
  83. {
  84. void *addr = phram->mtd.priv;
  85. if (phram->cached) {
  86. memunmap(addr);
  87. return;
  88. }
  89. iounmap((void __iomem *)addr);
  90. }
  91. static void unregister_devices(void)
  92. {
  93. struct phram_mtd_list *this, *safe;
  94. list_for_each_entry_safe(this, safe, &phram_list, list) {
  95. mtd_device_unregister(&this->mtd);
  96. phram_unmap(this);
  97. kfree(this->mtd.name);
  98. kfree(this);
  99. }
  100. }
  101. static int register_device(struct platform_device *pdev, const char *name,
  102. phys_addr_t start, size_t len, uint32_t erasesize)
  103. {
  104. struct device_node *np = pdev ? pdev->dev.of_node : NULL;
  105. bool cached = np ? !of_property_read_bool(np, "no-map") : false;
  106. struct phram_mtd_list *new;
  107. int ret = -ENOMEM;
  108. new = kzalloc(sizeof(*new), GFP_KERNEL);
  109. if (!new)
  110. goto out0;
  111. new->cached = cached;
  112. ret = phram_map(new, start, len);
  113. if (ret) {
  114. pr_err("ioremap failed\n");
  115. goto out1;
  116. }
  117. new->mtd.name = name;
  118. new->mtd.size = len;
  119. new->mtd.flags = MTD_CAP_RAM;
  120. new->mtd._erase = phram_erase;
  121. new->mtd._point = phram_point;
  122. new->mtd._unpoint = phram_unpoint;
  123. new->mtd._read = phram_read;
  124. new->mtd._write = phram_write;
  125. new->mtd.owner = THIS_MODULE;
  126. new->mtd.type = MTD_RAM;
  127. new->mtd.erasesize = erasesize;
  128. new->mtd.writesize = 1;
  129. mtd_set_of_node(&new->mtd, np);
  130. ret = -EAGAIN;
  131. if (mtd_device_register(&new->mtd, NULL, 0)) {
  132. pr_err("Failed to register new device\n");
  133. goto out2;
  134. }
  135. if (pdev)
  136. platform_set_drvdata(pdev, new);
  137. else
  138. list_add_tail(&new->list, &phram_list);
  139. return 0;
  140. out2:
  141. phram_unmap(new);
  142. out1:
  143. kfree(new);
  144. out0:
  145. return ret;
  146. }
  147. static int parse_num64(uint64_t *num64, char *token)
  148. {
  149. size_t len;
  150. int shift = 0;
  151. int ret;
  152. len = strlen(token);
  153. /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
  154. if (len > 2) {
  155. if (token[len - 1] == 'i') {
  156. switch (token[len - 2]) {
  157. case 'G':
  158. shift += 10;
  159. fallthrough;
  160. case 'M':
  161. shift += 10;
  162. fallthrough;
  163. case 'k':
  164. shift += 10;
  165. token[len - 2] = 0;
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. }
  171. }
  172. ret = kstrtou64(token, 0, num64);
  173. *num64 <<= shift;
  174. return ret;
  175. }
  176. static int parse_name(char **pname, const char *token)
  177. {
  178. size_t len;
  179. char *name;
  180. len = strlen(token) + 1;
  181. if (len > 64)
  182. return -ENOSPC;
  183. name = kstrdup(token, GFP_KERNEL);
  184. if (!name)
  185. return -ENOMEM;
  186. *pname = name;
  187. return 0;
  188. }
  189. static inline void kill_final_newline(char *str)
  190. {
  191. char *newline = strrchr(str, '\n');
  192. if (newline && !newline[1])
  193. *newline = 0;
  194. }
  195. #define parse_err(fmt, args...) do { \
  196. pr_err(fmt , ## args); \
  197. return 1; \
  198. } while (0)
  199. #ifndef MODULE
  200. static int phram_init_called;
  201. /*
  202. * This shall contain the module parameter if any. It is of the form:
  203. * - phram=<device>,<address>,<size>[,<erasesize>] for module case
  204. * - phram.phram=<device>,<address>,<size>[,<erasesize>] for built-in case
  205. * We leave 64 bytes for the device name, 20 for the address , 20 for the
  206. * size and 20 for the erasesize.
  207. * Example: phram.phram=rootfs,0xa0000000,512Mi,65536
  208. */
  209. static char phram_paramline[64 + 20 + 20 + 20];
  210. #endif
  211. static int phram_setup(const char *val)
  212. {
  213. char buf[64 + 20 + 20 + 20], *str = buf;
  214. char *token[4];
  215. char *name;
  216. uint64_t start;
  217. uint64_t len;
  218. uint64_t erasesize = PAGE_SIZE;
  219. uint32_t rem;
  220. int i, ret;
  221. if (strnlen(val, sizeof(buf)) >= sizeof(buf))
  222. parse_err("parameter too long\n");
  223. strcpy(str, val);
  224. kill_final_newline(str);
  225. for (i = 0; i < 4; i++)
  226. token[i] = strsep(&str, ",");
  227. if (str)
  228. parse_err("too many arguments\n");
  229. if (!token[2])
  230. parse_err("not enough arguments\n");
  231. ret = parse_name(&name, token[0]);
  232. if (ret)
  233. return ret;
  234. ret = parse_num64(&start, token[1]);
  235. if (ret) {
  236. parse_err("illegal start address\n");
  237. goto error;
  238. }
  239. ret = parse_num64(&len, token[2]);
  240. if (ret) {
  241. parse_err("illegal device length\n");
  242. goto error;
  243. }
  244. if (token[3]) {
  245. ret = parse_num64(&erasesize, token[3]);
  246. if (ret) {
  247. parse_err("illegal erasesize\n");
  248. goto error;
  249. }
  250. }
  251. if (len == 0 || erasesize == 0 || erasesize > len
  252. || erasesize > UINT_MAX) {
  253. parse_err("illegal erasesize or len\n");
  254. ret = -EINVAL;
  255. goto error;
  256. }
  257. div_u64_rem(len, (uint32_t)erasesize, &rem);
  258. if (rem) {
  259. parse_err("len is not multiple of erasesize\n");
  260. ret = -EINVAL;
  261. goto error;
  262. }
  263. ret = register_device(NULL, name, start, len, (uint32_t)erasesize);
  264. if (ret)
  265. goto error;
  266. pr_info("%s device: %#llx at %#llx for erasesize %#llx\n", name, len, start, erasesize);
  267. return 0;
  268. error:
  269. kfree(name);
  270. return ret;
  271. }
  272. static int phram_param_call(const char *val, const struct kernel_param *kp)
  273. {
  274. #ifdef MODULE
  275. return phram_setup(val);
  276. #else
  277. /*
  278. * If more parameters are later passed in via
  279. * /sys/module/phram/parameters/phram
  280. * and init_phram() has already been called,
  281. * we can parse the argument now.
  282. */
  283. if (phram_init_called)
  284. return phram_setup(val);
  285. /*
  286. * During early boot stage, we only save the parameters
  287. * here. We must parse them later: if the param passed
  288. * from kernel boot command line, phram_param_call() is
  289. * called so early that it is not possible to resolve
  290. * the device (even kmalloc() fails). Defer that work to
  291. * phram_setup().
  292. */
  293. if (strlen(val) >= sizeof(phram_paramline))
  294. return -ENOSPC;
  295. strcpy(phram_paramline, val);
  296. return 0;
  297. #endif
  298. }
  299. module_param_call(phram, phram_param_call, NULL, NULL, 0200);
  300. MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>[,<erasesize>]\"");
  301. #ifdef CONFIG_OF
  302. static const struct of_device_id phram_of_match[] = {
  303. { .compatible = "phram" },
  304. {}
  305. };
  306. MODULE_DEVICE_TABLE(of, phram_of_match);
  307. #endif
  308. static int phram_probe(struct platform_device *pdev)
  309. {
  310. struct resource *res;
  311. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  312. if (!res)
  313. return -ENOMEM;
  314. /* mtd_set_of_node() reads name from "label" */
  315. return register_device(pdev, NULL, res->start, resource_size(res),
  316. PAGE_SIZE);
  317. }
  318. static int phram_remove(struct platform_device *pdev)
  319. {
  320. struct phram_mtd_list *phram = platform_get_drvdata(pdev);
  321. mtd_device_unregister(&phram->mtd);
  322. phram_unmap(phram);
  323. kfree(phram);
  324. return 0;
  325. }
  326. static struct platform_driver phram_driver = {
  327. .probe = phram_probe,
  328. .remove = phram_remove,
  329. .driver = {
  330. .name = "phram",
  331. .of_match_table = of_match_ptr(phram_of_match),
  332. },
  333. };
  334. static int __init init_phram(void)
  335. {
  336. int ret;
  337. ret = platform_driver_register(&phram_driver);
  338. if (ret)
  339. return ret;
  340. #ifndef MODULE
  341. if (phram_paramline[0])
  342. ret = phram_setup(phram_paramline);
  343. phram_init_called = 1;
  344. #endif
  345. if (ret)
  346. platform_driver_unregister(&phram_driver);
  347. return ret;
  348. }
  349. static void __exit cleanup_phram(void)
  350. {
  351. unregister_devices();
  352. platform_driver_unregister(&phram_driver);
  353. }
  354. module_init(init_phram);
  355. module_exit(cleanup_phram);
  356. MODULE_LICENSE("GPL");
  357. MODULE_AUTHOR("Joern Engel <[email protected]>");
  358. MODULE_DESCRIPTION("MTD driver for physical RAM");