slram.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*======================================================================
  2. $Id: slram.c,v 1.33 2005/01/05 18:05:13 dwmw2 Exp $
  3. This driver provides a method to access memory not used by the kernel
  4. itself (i.e. if the kernel commandline mem=xxx is used). To actually
  5. use slram at least mtdblock or mtdchar is required (for block or
  6. character device access).
  7. Usage:
  8. if compiled as loadable module:
  9. modprobe slram map=<name>,<start>,<end/offset>
  10. if statically linked into the kernel use the following kernel cmd.line
  11. slram=<name>,<start>,<end/offset>
  12. <name>: name of the device that will be listed in /proc/mtd
  13. <start>: start of the memory region, decimal or hex (0xabcdef)
  14. <end/offset>: end of the memory region. It's possible to use +0x1234
  15. to specify the offset instead of the absolute address
  16. NOTE:
  17. With slram it's only possible to map a contigous memory region. Therfore
  18. if there's a device mapped somewhere in the region specified slram will
  19. fail to load (see kernel log if modprobe fails).
  20. -
  21. Jochen Schaeuble <psionic@psionic.de>
  22. ======================================================================*/
  23. #include <linux/module.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <linux/timer.h>
  32. #include <linux/major.h>
  33. #include <linux/fs.h>
  34. #include <linux/ioctl.h>
  35. #include <linux/init.h>
  36. #include <asm/io.h>
  37. #include <asm/system.h>
  38. #include <linux/mtd/mtd.h>
  39. #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
  40. #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
  41. #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
  42. typedef struct slram_priv {
  43. u_char *start;
  44. u_char *end;
  45. } slram_priv_t;
  46. typedef struct slram_mtd_list {
  47. struct mtd_info *mtdinfo;
  48. struct slram_mtd_list *next;
  49. } slram_mtd_list_t;
  50. #ifdef MODULE
  51. static char *map[SLRAM_MAX_DEVICES_PARAMS];
  52. module_param_array(map, charp, NULL, 0);
  53. MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
  54. #else
  55. static char *map;
  56. #endif
  57. static slram_mtd_list_t *slram_mtdlist = NULL;
  58. static int slram_erase(struct mtd_info *, struct erase_info *);
  59. static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, u_char **);
  60. static void slram_unpoint(struct mtd_info *, u_char *, loff_t, size_t);
  61. static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  62. static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  63. static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
  64. {
  65. slram_priv_t *priv = mtd->priv;
  66. if (instr->addr + instr->len > mtd->size) {
  67. return(-EINVAL);
  68. }
  69. memset(priv->start + instr->addr, 0xff, instr->len);
  70. /* This'll catch a few races. Free the thing before returning :)
  71. * I don't feel at all ashamed. This kind of thing is possible anyway
  72. * with flash, but unlikely.
  73. */
  74. instr->state = MTD_ERASE_DONE;
  75. mtd_erase_callback(instr);
  76. return(0);
  77. }
  78. static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
  79. size_t *retlen, u_char **mtdbuf)
  80. {
  81. slram_priv_t *priv = mtd->priv;
  82. *mtdbuf = priv->start + from;
  83. *retlen = len;
  84. return(0);
  85. }
  86. static void slram_unpoint(struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
  87. {
  88. }
  89. static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
  90. size_t *retlen, u_char *buf)
  91. {
  92. slram_priv_t *priv = mtd->priv;
  93. memcpy(buf, priv->start + from, len);
  94. *retlen = len;
  95. return(0);
  96. }
  97. static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
  98. size_t *retlen, const u_char *buf)
  99. {
  100. slram_priv_t *priv = mtd->priv;
  101. memcpy(priv->start + to, buf, len);
  102. *retlen = len;
  103. return(0);
  104. }
  105. /*====================================================================*/
  106. static int register_device(char *name, unsigned long start, unsigned long length)
  107. {
  108. slram_mtd_list_t **curmtd;
  109. curmtd = &slram_mtdlist;
  110. while (*curmtd) {
  111. curmtd = &(*curmtd)->next;
  112. }
  113. *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL);
  114. if (!(*curmtd)) {
  115. E("slram: Cannot allocate new MTD device.\n");
  116. return(-ENOMEM);
  117. }
  118. (*curmtd)->mtdinfo = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  119. (*curmtd)->next = NULL;
  120. if ((*curmtd)->mtdinfo) {
  121. memset((char *)(*curmtd)->mtdinfo, 0, sizeof(struct mtd_info));
  122. (*curmtd)->mtdinfo->priv =
  123. kmalloc(sizeof(slram_priv_t), GFP_KERNEL);
  124. if (!(*curmtd)->mtdinfo->priv) {
  125. kfree((*curmtd)->mtdinfo);
  126. (*curmtd)->mtdinfo = NULL;
  127. } else {
  128. memset((*curmtd)->mtdinfo->priv,0,sizeof(slram_priv_t));
  129. }
  130. }
  131. if (!(*curmtd)->mtdinfo) {
  132. E("slram: Cannot allocate new MTD device.\n");
  133. return(-ENOMEM);
  134. }
  135. if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
  136. ioremap(start, length))) {
  137. E("slram: ioremap failed\n");
  138. return -EIO;
  139. }
  140. ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
  141. ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
  142. (*curmtd)->mtdinfo->name = name;
  143. (*curmtd)->mtdinfo->size = length;
  144. (*curmtd)->mtdinfo->flags = MTD_CLEAR_BITS | MTD_SET_BITS |
  145. MTD_WRITEB_WRITEABLE | MTD_VOLATILE;
  146. (*curmtd)->mtdinfo->erase = slram_erase;
  147. (*curmtd)->mtdinfo->point = slram_point;
  148. (*curmtd)->mtdinfo->unpoint = slram_unpoint;
  149. (*curmtd)->mtdinfo->read = slram_read;
  150. (*curmtd)->mtdinfo->write = slram_write;
  151. (*curmtd)->mtdinfo->owner = THIS_MODULE;
  152. (*curmtd)->mtdinfo->type = MTD_RAM;
  153. (*curmtd)->mtdinfo->erasesize = 0x0;
  154. if (add_mtd_device((*curmtd)->mtdinfo)) {
  155. E("slram: Failed to register new device\n");
  156. iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
  157. kfree((*curmtd)->mtdinfo->priv);
  158. kfree((*curmtd)->mtdinfo);
  159. return(-EAGAIN);
  160. }
  161. T("slram: Registered device %s from %luKiB to %luKiB\n", name,
  162. (start / 1024), ((start + length) / 1024));
  163. T("slram: Mapped from 0x%p to 0x%p\n",
  164. ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
  165. ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
  166. return(0);
  167. }
  168. static void unregister_devices(void)
  169. {
  170. slram_mtd_list_t *nextitem;
  171. while (slram_mtdlist) {
  172. nextitem = slram_mtdlist->next;
  173. del_mtd_device(slram_mtdlist->mtdinfo);
  174. iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
  175. kfree(slram_mtdlist->mtdinfo->priv);
  176. kfree(slram_mtdlist->mtdinfo);
  177. kfree(slram_mtdlist);
  178. slram_mtdlist = nextitem;
  179. }
  180. }
  181. static unsigned long handle_unit(unsigned long value, char *unit)
  182. {
  183. if ((*unit == 'M') || (*unit == 'm')) {
  184. return(value * 1024 * 1024);
  185. } else if ((*unit == 'K') || (*unit == 'k')) {
  186. return(value * 1024);
  187. }
  188. return(value);
  189. }
  190. static int parse_cmdline(char *devname, char *szstart, char *szlength)
  191. {
  192. char *buffer;
  193. unsigned long devstart;
  194. unsigned long devlength;
  195. if ((!devname) || (!szstart) || (!szlength)) {
  196. unregister_devices();
  197. return(-EINVAL);
  198. }
  199. devstart = simple_strtoul(szstart, &buffer, 0);
  200. devstart = handle_unit(devstart, buffer);
  201. if (*(szlength) != '+') {
  202. devlength = simple_strtoul(szlength, &buffer, 0);
  203. devlength = handle_unit(devlength, buffer) - devstart;
  204. } else {
  205. devlength = simple_strtoul(szlength + 1, &buffer, 0);
  206. devlength = handle_unit(devlength, buffer);
  207. }
  208. T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
  209. devname, devstart, devlength);
  210. if ((devstart < 0) || (devlength < 0)) {
  211. E("slram: Illegal start / length parameter.\n");
  212. return(-EINVAL);
  213. }
  214. if ((devstart = register_device(devname, devstart, devlength))){
  215. unregister_devices();
  216. return((int)devstart);
  217. }
  218. return(0);
  219. }
  220. #ifndef MODULE
  221. static int __init mtd_slram_setup(char *str)
  222. {
  223. map = str;
  224. return(1);
  225. }
  226. __setup("slram=", mtd_slram_setup);
  227. #endif
  228. static int init_slram(void)
  229. {
  230. char *devname;
  231. int i;
  232. #ifndef MODULE
  233. char *devstart;
  234. char *devlength;
  235. i = 0;
  236. if (!map) {
  237. E("slram: not enough parameters.\n");
  238. return(-EINVAL);
  239. }
  240. while (map) {
  241. devname = devstart = devlength = NULL;
  242. if (!(devname = strsep(&map, ","))) {
  243. E("slram: No devicename specified.\n");
  244. break;
  245. }
  246. T("slram: devname = %s\n", devname);
  247. if ((!map) || (!(devstart = strsep(&map, ",")))) {
  248. E("slram: No devicestart specified.\n");
  249. }
  250. T("slram: devstart = %s\n", devstart);
  251. if ((!map) || (!(devlength = strsep(&map, ",")))) {
  252. E("slram: No devicelength / -end specified.\n");
  253. }
  254. T("slram: devlength = %s\n", devlength);
  255. if (parse_cmdline(devname, devstart, devlength) != 0) {
  256. return(-EINVAL);
  257. }
  258. }
  259. #else
  260. int count;
  261. for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS);
  262. count++) {
  263. }
  264. if ((count % 3 != 0) || (count == 0)) {
  265. E("slram: not enough parameters.\n");
  266. return(-EINVAL);
  267. }
  268. for (i = 0; i < (count / 3); i++) {
  269. devname = map[i * 3];
  270. if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
  271. return(-EINVAL);
  272. }
  273. }
  274. #endif /* !MODULE */
  275. return(0);
  276. }
  277. static void __exit cleanup_slram(void)
  278. {
  279. unregister_devices();
  280. }
  281. module_init(init_slram);
  282. module_exit(cleanup_slram);
  283. MODULE_LICENSE("GPL");
  284. MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
  285. MODULE_DESCRIPTION("MTD driver for uncached system RAM");