mtdram.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * mtdram - a test mtd device
  3. * $Id: mtdram.c,v 1.35 2005/01/05 18:05:12 dwmw2 Exp $
  4. * Author: Alexander Larsson <alex@cendio.se>
  5. *
  6. * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
  7. *
  8. * This code is GPL
  9. *
  10. */
  11. #include <linux/config.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/ioport.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/init.h>
  17. #include <linux/mtd/compatmac.h>
  18. #include <linux/mtd/mtd.h>
  19. #ifndef CONFIG_MTDRAM_ABS_POS
  20. #define CONFIG_MTDRAM_ABS_POS 0
  21. #endif
  22. #if CONFIG_MTDRAM_ABS_POS > 0
  23. #include <asm/io.h>
  24. #endif
  25. #ifdef MODULE
  26. static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
  27. static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
  28. module_param(total_size,ulong,0);
  29. MODULE_PARM_DESC(total_size, "Total device size in KiB");
  30. module_param(erase_size,ulong,0);
  31. MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
  32. #define MTDRAM_TOTAL_SIZE (total_size * 1024)
  33. #define MTDRAM_ERASE_SIZE (erase_size * 1024)
  34. #else
  35. #define MTDRAM_TOTAL_SIZE (CONFIG_MTDRAM_TOTAL_SIZE * 1024)
  36. #define MTDRAM_ERASE_SIZE (CONFIG_MTDRAM_ERASE_SIZE * 1024)
  37. #endif
  38. // We could store these in the mtd structure, but we only support 1 device..
  39. static struct mtd_info *mtd_info;
  40. static int
  41. ram_erase(struct mtd_info *mtd, struct erase_info *instr)
  42. {
  43. DEBUG(MTD_DEBUG_LEVEL2, "ram_erase(pos:%ld, len:%ld)\n", (long)instr->addr, (long)instr->len);
  44. if (instr->addr + instr->len > mtd->size) {
  45. DEBUG(MTD_DEBUG_LEVEL1, "ram_erase() out of bounds (%ld > %ld)\n", (long)(instr->addr + instr->len), (long)mtd->size);
  46. return -EINVAL;
  47. }
  48. memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
  49. instr->state = MTD_ERASE_DONE;
  50. mtd_erase_callback(instr);
  51. return 0;
  52. }
  53. static int ram_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
  54. {
  55. if (from + len > mtd->size)
  56. return -EINVAL;
  57. *mtdbuf = mtd->priv + from;
  58. *retlen = len;
  59. return 0;
  60. }
  61. static void ram_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from,
  62. size_t len)
  63. {
  64. DEBUG(MTD_DEBUG_LEVEL2, "ram_unpoint\n");
  65. }
  66. static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
  67. size_t *retlen, u_char *buf)
  68. {
  69. DEBUG(MTD_DEBUG_LEVEL2, "ram_read(pos:%ld, len:%ld)\n", (long)from, (long)len);
  70. if (from + len > mtd->size) {
  71. DEBUG(MTD_DEBUG_LEVEL1, "ram_read() out of bounds (%ld > %ld)\n", (long)(from + len), (long)mtd->size);
  72. return -EINVAL;
  73. }
  74. memcpy(buf, mtd->priv + from, len);
  75. *retlen=len;
  76. return 0;
  77. }
  78. static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
  79. size_t *retlen, const u_char *buf)
  80. {
  81. DEBUG(MTD_DEBUG_LEVEL2, "ram_write(pos:%ld, len:%ld)\n", (long)to, (long)len);
  82. if (to + len > mtd->size) {
  83. DEBUG(MTD_DEBUG_LEVEL1, "ram_write() out of bounds (%ld > %ld)\n", (long)(to + len), (long)mtd->size);
  84. return -EINVAL;
  85. }
  86. memcpy ((char *)mtd->priv + to, buf, len);
  87. *retlen=len;
  88. return 0;
  89. }
  90. static void __exit cleanup_mtdram(void)
  91. {
  92. if (mtd_info) {
  93. del_mtd_device(mtd_info);
  94. #if CONFIG_MTDRAM_TOTAL_SIZE > 0
  95. if (mtd_info->priv)
  96. #if CONFIG_MTDRAM_ABS_POS > 0
  97. iounmap(mtd_info->priv);
  98. #else
  99. vfree(mtd_info->priv);
  100. #endif
  101. #endif
  102. kfree(mtd_info);
  103. }
  104. }
  105. int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
  106. unsigned long size, char *name)
  107. {
  108. memset(mtd, 0, sizeof(*mtd));
  109. /* Setup the MTD structure */
  110. mtd->name = name;
  111. mtd->type = MTD_RAM;
  112. mtd->flags = MTD_CAP_RAM;
  113. mtd->size = size;
  114. mtd->erasesize = MTDRAM_ERASE_SIZE;
  115. mtd->priv = mapped_address;
  116. mtd->owner = THIS_MODULE;
  117. mtd->erase = ram_erase;
  118. mtd->point = ram_point;
  119. mtd->unpoint = ram_unpoint;
  120. mtd->read = ram_read;
  121. mtd->write = ram_write;
  122. if (add_mtd_device(mtd)) {
  123. return -EIO;
  124. }
  125. return 0;
  126. }
  127. #if CONFIG_MTDRAM_TOTAL_SIZE > 0
  128. #if CONFIG_MTDRAM_ABS_POS > 0
  129. static int __init init_mtdram(void)
  130. {
  131. void *addr;
  132. int err;
  133. /* Allocate some memory */
  134. mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  135. if (!mtd_info)
  136. return -ENOMEM;
  137. addr = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE);
  138. if (!addr) {
  139. DEBUG(MTD_DEBUG_LEVEL1,
  140. "Failed to ioremap) memory region of size %ld at ABS_POS:%ld\n",
  141. (long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS);
  142. kfree(mtd_info);
  143. mtd_info = NULL;
  144. return -ENOMEM;
  145. }
  146. err = mtdram_init_device(mtd_info, addr,
  147. MTDRAM_TOTAL_SIZE, "mtdram test device");
  148. if (err)
  149. {
  150. iounmap(addr);
  151. kfree(mtd_info);
  152. mtd_info = NULL;
  153. return err;
  154. }
  155. memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
  156. return err;
  157. }
  158. #else /* CONFIG_MTDRAM_ABS_POS > 0 */
  159. static int __init init_mtdram(void)
  160. {
  161. void *addr;
  162. int err;
  163. /* Allocate some memory */
  164. mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  165. if (!mtd_info)
  166. return -ENOMEM;
  167. addr = vmalloc(MTDRAM_TOTAL_SIZE);
  168. if (!addr) {
  169. DEBUG(MTD_DEBUG_LEVEL1,
  170. "Failed to vmalloc memory region of size %ld\n",
  171. (long)MTDRAM_TOTAL_SIZE);
  172. kfree(mtd_info);
  173. mtd_info = NULL;
  174. return -ENOMEM;
  175. }
  176. err = mtdram_init_device(mtd_info, addr,
  177. MTDRAM_TOTAL_SIZE, "mtdram test device");
  178. if (err)
  179. {
  180. vfree(addr);
  181. kfree(mtd_info);
  182. mtd_info = NULL;
  183. return err;
  184. }
  185. memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
  186. return err;
  187. }
  188. #endif /* !(CONFIG_MTDRAM_ABS_POS > 0) */
  189. #else /* CONFIG_MTDRAM_TOTAL_SIZE > 0 */
  190. static int __init init_mtdram(void)
  191. {
  192. return 0;
  193. }
  194. #endif /* !(CONFIG_MTDRAM_TOTAL_SIZE > 0) */
  195. module_init(init_mtdram);
  196. module_exit(cleanup_mtdram);
  197. MODULE_LICENSE("GPL");
  198. MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
  199. MODULE_DESCRIPTION("Simulated MTD driver for testing");