mtdram.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * mtdram - a test mtd device
  3. * Author: Alexander Larsson <alex@cendio.se>
  4. *
  5. * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
  6. * Copyright (c) 2005 Joern Engel <joern@wh.fh-wedel.de>
  7. *
  8. * This code is GPL
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/ioport.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/init.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/mtdram.h>
  18. static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
  19. static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
  20. #define MTDRAM_TOTAL_SIZE (total_size * 1024)
  21. #define MTDRAM_ERASE_SIZE (erase_size * 1024)
  22. #ifdef MODULE
  23. module_param(total_size, ulong, 0);
  24. MODULE_PARM_DESC(total_size, "Total device size in KiB");
  25. module_param(erase_size, ulong, 0);
  26. MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
  27. #endif
  28. // We could store these in the mtd structure, but we only support 1 device..
  29. static struct mtd_info *mtd_info;
  30. static int check_offs_len(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  31. {
  32. int ret = 0;
  33. /* Start address must align on block boundary */
  34. if (mtd_mod_by_eb(ofs, mtd)) {
  35. pr_debug("%s: unaligned address\n", __func__);
  36. ret = -EINVAL;
  37. }
  38. /* Length must align on block boundary */
  39. if (mtd_mod_by_eb(len, mtd)) {
  40. pr_debug("%s: length not block aligned\n", __func__);
  41. ret = -EINVAL;
  42. }
  43. return ret;
  44. }
  45. static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
  46. {
  47. if (check_offs_len(mtd, instr->addr, instr->len))
  48. return -EINVAL;
  49. memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
  50. instr->state = MTD_ERASE_DONE;
  51. mtd_erase_callback(instr);
  52. return 0;
  53. }
  54. static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
  55. size_t *retlen, void **virt, resource_size_t *phys)
  56. {
  57. *virt = mtd->priv + from;
  58. *retlen = len;
  59. return 0;
  60. }
  61. static int ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  62. {
  63. return 0;
  64. }
  65. /*
  66. * Allow NOMMU mmap() to directly map the device (if not NULL)
  67. * - return the address to which the offset maps
  68. * - return -ENOSYS to indicate refusal to do the mapping
  69. */
  70. static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
  71. unsigned long len,
  72. unsigned long offset,
  73. unsigned long flags)
  74. {
  75. return (unsigned long) mtd->priv + offset;
  76. }
  77. static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
  78. size_t *retlen, u_char *buf)
  79. {
  80. memcpy(buf, mtd->priv + from, len);
  81. *retlen = len;
  82. return 0;
  83. }
  84. static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
  85. size_t *retlen, const u_char *buf)
  86. {
  87. memcpy((char *)mtd->priv + to, buf, len);
  88. *retlen = len;
  89. return 0;
  90. }
  91. static void __exit cleanup_mtdram(void)
  92. {
  93. if (mtd_info) {
  94. mtd_device_unregister(mtd_info);
  95. vfree(mtd_info->priv);
  96. kfree(mtd_info);
  97. }
  98. }
  99. int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
  100. unsigned long size, const char *name)
  101. {
  102. memset(mtd, 0, sizeof(*mtd));
  103. /* Setup the MTD structure */
  104. mtd->name = name;
  105. mtd->type = MTD_RAM;
  106. mtd->flags = MTD_CAP_RAM;
  107. mtd->size = size;
  108. mtd->writesize = 1;
  109. mtd->writebufsize = 64; /* Mimic CFI NOR flashes */
  110. mtd->erasesize = MTDRAM_ERASE_SIZE;
  111. mtd->priv = mapped_address;
  112. mtd->owner = THIS_MODULE;
  113. mtd->_erase = ram_erase;
  114. mtd->_point = ram_point;
  115. mtd->_unpoint = ram_unpoint;
  116. mtd->_get_unmapped_area = ram_get_unmapped_area;
  117. mtd->_read = ram_read;
  118. mtd->_write = ram_write;
  119. if (mtd_device_register(mtd, NULL, 0))
  120. return -EIO;
  121. return 0;
  122. }
  123. static int __init init_mtdram(void)
  124. {
  125. void *addr;
  126. int err;
  127. if (!total_size)
  128. return -EINVAL;
  129. /* Allocate some memory */
  130. mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  131. if (!mtd_info)
  132. return -ENOMEM;
  133. addr = vmalloc(MTDRAM_TOTAL_SIZE);
  134. if (!addr) {
  135. kfree(mtd_info);
  136. mtd_info = NULL;
  137. return -ENOMEM;
  138. }
  139. err = mtdram_init_device(mtd_info, addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
  140. if (err) {
  141. vfree(addr);
  142. kfree(mtd_info);
  143. mtd_info = NULL;
  144. return err;
  145. }
  146. memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
  147. return err;
  148. }
  149. module_init(init_mtdram);
  150. module_exit(cleanup_mtdram);
  151. MODULE_LICENSE("GPL");
  152. MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
  153. MODULE_DESCRIPTION("Simulated MTD driver for testing");