phram.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <asm/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, unsigned long start, unsigned long 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 ustrtoul(const char *cp, char **endp, unsigned int base)
  121. {
  122. unsigned long result = simple_strtoul(cp, endp, base);
  123. switch (**endp) {
  124. case 'G':
  125. result *= 1024;
  126. case 'M':
  127. result *= 1024;
  128. case 'k':
  129. result *= 1024;
  130. /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
  131. if ((*endp)[1] == 'i')
  132. (*endp) += 2;
  133. }
  134. return result;
  135. }
  136. static int parse_num32(uint32_t *num32, const char *token)
  137. {
  138. char *endp;
  139. unsigned long n;
  140. n = ustrtoul(token, &endp, 0);
  141. if (*endp)
  142. return -EINVAL;
  143. *num32 = n;
  144. return 0;
  145. }
  146. static int parse_name(char **pname, const char *token)
  147. {
  148. size_t len;
  149. char *name;
  150. len = strlen(token) + 1;
  151. if (len > 64)
  152. return -ENOSPC;
  153. name = kmalloc(len, GFP_KERNEL);
  154. if (!name)
  155. return -ENOMEM;
  156. strcpy(name, token);
  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. /*
  171. * This shall contain the module parameter if any. It is of the form:
  172. * - phram=<device>,<address>,<size> for module case
  173. * - phram.phram=<device>,<address>,<size> for built-in case
  174. * We leave 64 bytes for the device name, 12 for the address and 12 for the
  175. * size.
  176. * Example: phram.phram=rootfs,0xa0000000,512Mi
  177. */
  178. static __initdata char phram_paramline[64+12+12];
  179. static int __init phram_setup(const char *val)
  180. {
  181. char buf[64+12+12], *str = buf;
  182. char *token[3];
  183. char *name;
  184. uint32_t start;
  185. uint32_t len;
  186. int i, ret;
  187. if (strnlen(val, sizeof(buf)) >= sizeof(buf))
  188. parse_err("parameter too long\n");
  189. strcpy(str, val);
  190. kill_final_newline(str);
  191. for (i=0; i<3; i++)
  192. token[i] = strsep(&str, ",");
  193. if (str)
  194. parse_err("too many arguments\n");
  195. if (!token[2])
  196. parse_err("not enough arguments\n");
  197. ret = parse_name(&name, token[0]);
  198. if (ret)
  199. return ret;
  200. ret = parse_num32(&start, token[1]);
  201. if (ret) {
  202. kfree(name);
  203. parse_err("illegal start address\n");
  204. }
  205. ret = parse_num32(&len, token[2]);
  206. if (ret) {
  207. kfree(name);
  208. parse_err("illegal device length\n");
  209. }
  210. ret = register_device(name, start, len);
  211. if (!ret)
  212. pr_info("%s device: %#x at %#x\n", name, len, start);
  213. else
  214. kfree(name);
  215. return ret;
  216. }
  217. static int __init phram_param_call(const char *val, struct kernel_param *kp)
  218. {
  219. /*
  220. * This function is always called before 'init_phram()', whether
  221. * built-in or module.
  222. */
  223. if (strlen(val) >= sizeof(phram_paramline))
  224. return -ENOSPC;
  225. strcpy(phram_paramline, val);
  226. return 0;
  227. }
  228. module_param_call(phram, phram_param_call, NULL, NULL, 000);
  229. MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
  230. static int __init init_phram(void)
  231. {
  232. if (phram_paramline[0])
  233. return phram_setup(phram_paramline);
  234. return 0;
  235. }
  236. static void __exit cleanup_phram(void)
  237. {
  238. unregister_devices();
  239. }
  240. module_init(init_phram);
  241. module_exit(cleanup_phram);
  242. MODULE_LICENSE("GPL");
  243. MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
  244. MODULE_DESCRIPTION("MTD driver for physical RAM");