phram.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * Copyright (c) ???? Jochen Schäuble <psionic@psionic.de>
  3. * Copyright (c) 2003-2004 Joern Engel <joern@wh.fh-wedel.de>
  4. *
  5. * Usage:
  6. *
  7. * one commend line parameter per device, each in the form:
  8. * phram=<name>,<start>,<len>
  9. * <name> may be up to 63 characters.
  10. * <start> and <len> can be octal, decimal or hexadecimal. If followed
  11. * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
  12. * gigabytes.
  13. *
  14. * Example:
  15. * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/io.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/slab.h>
  25. #include <linux/mtd/mtd.h>
  26. struct phram_mtd_list {
  27. struct mtd_info mtd;
  28. struct list_head list;
  29. };
  30. static LIST_HEAD(phram_list);
  31. static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
  32. {
  33. u_char *start = mtd->priv;
  34. memset(start + instr->addr, 0xff, instr->len);
  35. /*
  36. * This'll catch a few races. Free the thing before returning :)
  37. * I don't feel at all ashamed. This kind of thing is possible anyway
  38. * with flash, but unlikely.
  39. */
  40. instr->state = MTD_ERASE_DONE;
  41. mtd_erase_callback(instr);
  42. return 0;
  43. }
  44. static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
  45. size_t *retlen, void **virt, resource_size_t *phys)
  46. {
  47. *virt = mtd->priv + from;
  48. *retlen = len;
  49. return 0;
  50. }
  51. static int phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  52. {
  53. return 0;
  54. }
  55. static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
  56. size_t *retlen, u_char *buf)
  57. {
  58. u_char *start = mtd->priv;
  59. memcpy(buf, start + from, len);
  60. *retlen = len;
  61. return 0;
  62. }
  63. static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
  64. size_t *retlen, const u_char *buf)
  65. {
  66. u_char *start = mtd->priv;
  67. memcpy(start + to, buf, len);
  68. *retlen = len;
  69. return 0;
  70. }
  71. static void unregister_devices(void)
  72. {
  73. struct phram_mtd_list *this, *safe;
  74. list_for_each_entry_safe(this, safe, &phram_list, list) {
  75. mtd_device_unregister(&this->mtd);
  76. iounmap(this->mtd.priv);
  77. kfree(this->mtd.name);
  78. kfree(this);
  79. }
  80. }
  81. static int register_device(char *name, phys_addr_t start, size_t len)
  82. {
  83. struct phram_mtd_list *new;
  84. int ret = -ENOMEM;
  85. new = kzalloc(sizeof(*new), GFP_KERNEL);
  86. if (!new)
  87. goto out0;
  88. ret = -EIO;
  89. new->mtd.priv = ioremap(start, len);
  90. if (!new->mtd.priv) {
  91. pr_err("ioremap failed\n");
  92. goto out1;
  93. }
  94. new->mtd.name = name;
  95. new->mtd.size = len;
  96. new->mtd.flags = MTD_CAP_RAM;
  97. new->mtd._erase = phram_erase;
  98. new->mtd._point = phram_point;
  99. new->mtd._unpoint = phram_unpoint;
  100. new->mtd._read = phram_read;
  101. new->mtd._write = phram_write;
  102. new->mtd.owner = THIS_MODULE;
  103. new->mtd.type = MTD_RAM;
  104. new->mtd.erasesize = PAGE_SIZE;
  105. new->mtd.writesize = 1;
  106. ret = -EAGAIN;
  107. if (mtd_device_register(&new->mtd, NULL, 0)) {
  108. pr_err("Failed to register new device\n");
  109. goto out2;
  110. }
  111. list_add_tail(&new->list, &phram_list);
  112. return 0;
  113. out2:
  114. iounmap(new->mtd.priv);
  115. out1:
  116. kfree(new);
  117. out0:
  118. return ret;
  119. }
  120. static int parse_num64(uint64_t *num64, char *token)
  121. {
  122. size_t len;
  123. int shift = 0;
  124. int ret;
  125. len = strlen(token);
  126. /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
  127. if (len > 2) {
  128. if (token[len - 1] == 'i') {
  129. switch (token[len - 2]) {
  130. case 'G':
  131. shift += 10;
  132. case 'M':
  133. shift += 10;
  134. case 'k':
  135. shift += 10;
  136. token[len - 2] = 0;
  137. break;
  138. default:
  139. return -EINVAL;
  140. }
  141. }
  142. }
  143. ret = kstrtou64(token, 0, num64);
  144. *num64 <<= shift;
  145. return ret;
  146. }
  147. static int parse_name(char **pname, const char *token)
  148. {
  149. size_t len;
  150. char *name;
  151. len = strlen(token) + 1;
  152. if (len > 64)
  153. return -ENOSPC;
  154. name = kstrdup(token, GFP_KERNEL);
  155. if (!name)
  156. return -ENOMEM;
  157. *pname = name;
  158. return 0;
  159. }
  160. static inline void kill_final_newline(char *str)
  161. {
  162. char *newline = strrchr(str, '\n');
  163. if (newline && !newline[1])
  164. *newline = 0;
  165. }
  166. #define parse_err(fmt, args...) do { \
  167. pr_err(fmt , ## args); \
  168. return 1; \
  169. } while (0)
  170. #ifndef MODULE
  171. static int phram_init_called;
  172. /*
  173. * This shall contain the module parameter if any. It is of the form:
  174. * - phram=<device>,<address>,<size> for module case
  175. * - phram.phram=<device>,<address>,<size> for built-in case
  176. * We leave 64 bytes for the device name, 20 for the address and 20 for the
  177. * size.
  178. * Example: phram.phram=rootfs,0xa0000000,512Mi
  179. */
  180. static char phram_paramline[64 + 20 + 20];
  181. #endif
  182. static int phram_setup(const char *val)
  183. {
  184. char buf[64 + 20 + 20], *str = buf;
  185. char *token[3];
  186. char *name;
  187. uint64_t start;
  188. uint64_t len;
  189. int i, ret;
  190. if (strnlen(val, sizeof(buf)) >= sizeof(buf))
  191. parse_err("parameter too long\n");
  192. strcpy(str, val);
  193. kill_final_newline(str);
  194. for (i = 0; i < 3; i++)
  195. token[i] = strsep(&str, ",");
  196. if (str)
  197. parse_err("too many arguments\n");
  198. if (!token[2])
  199. parse_err("not enough arguments\n");
  200. ret = parse_name(&name, token[0]);
  201. if (ret)
  202. return ret;
  203. ret = parse_num64(&start, token[1]);
  204. if (ret) {
  205. kfree(name);
  206. parse_err("illegal start address\n");
  207. }
  208. ret = parse_num64(&len, token[2]);
  209. if (ret) {
  210. kfree(name);
  211. parse_err("illegal device length\n");
  212. }
  213. ret = register_device(name, start, len);
  214. if (!ret)
  215. pr_info("%s device: %#llx at %#llx\n", name, len, start);
  216. else
  217. kfree(name);
  218. return ret;
  219. }
  220. static int phram_param_call(const char *val, struct kernel_param *kp)
  221. {
  222. #ifdef MODULE
  223. return phram_setup(val);
  224. #else
  225. /*
  226. * If more parameters are later passed in via
  227. * /sys/module/phram/parameters/phram
  228. * and init_phram() has already been called,
  229. * we can parse the argument now.
  230. */
  231. if (phram_init_called)
  232. return phram_setup(val);
  233. /*
  234. * During early boot stage, we only save the parameters
  235. * here. We must parse them later: if the param passed
  236. * from kernel boot command line, phram_param_call() is
  237. * called so early that it is not possible to resolve
  238. * the device (even kmalloc() fails). Defer that work to
  239. * phram_setup().
  240. */
  241. if (strlen(val) >= sizeof(phram_paramline))
  242. return -ENOSPC;
  243. strcpy(phram_paramline, val);
  244. return 0;
  245. #endif
  246. }
  247. module_param_call(phram, phram_param_call, NULL, NULL, 000);
  248. MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
  249. static int __init init_phram(void)
  250. {
  251. int ret = 0;
  252. #ifndef MODULE
  253. if (phram_paramline[0])
  254. ret = phram_setup(phram_paramline);
  255. phram_init_called = 1;
  256. #endif
  257. return ret;
  258. }
  259. static void __exit cleanup_phram(void)
  260. {
  261. unregister_devices();
  262. }
  263. module_init(init_phram);
  264. module_exit(cleanup_phram);
  265. MODULE_LICENSE("GPL");
  266. MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
  267. MODULE_DESCRIPTION("MTD driver for physical RAM");