mtdram.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/mm.h>
  16. #include <linux/init.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/mtdram.h>
  19. static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
  20. static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
  21. static unsigned long writebuf_size = 64;
  22. #define MTDRAM_TOTAL_SIZE (total_size * 1024)
  23. #define MTDRAM_ERASE_SIZE (erase_size * 1024)
  24. #ifdef MODULE
  25. module_param(total_size, ulong, 0);
  26. MODULE_PARM_DESC(total_size, "Total device size in KiB");
  27. module_param(erase_size, ulong, 0);
  28. MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
  29. module_param(writebuf_size, ulong, 0);
  30. MODULE_PARM_DESC(writebuf_size, "Device write buf size in Bytes (Default: 64)");
  31. #endif
  32. // We could store these in the mtd structure, but we only support 1 device..
  33. static struct mtd_info *mtd_info;
  34. static int check_offs_len(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  35. {
  36. int ret = 0;
  37. /* Start address must align on block boundary */
  38. if (mtd_mod_by_eb(ofs, mtd)) {
  39. pr_debug("%s: unaligned address\n", __func__);
  40. ret = -EINVAL;
  41. }
  42. /* Length must align on block boundary */
  43. if (mtd_mod_by_eb(len, mtd)) {
  44. pr_debug("%s: length not block aligned\n", __func__);
  45. ret = -EINVAL;
  46. }
  47. return ret;
  48. }
  49. static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
  50. {
  51. if (check_offs_len(mtd, instr->addr, instr->len))
  52. return -EINVAL;
  53. memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
  54. return 0;
  55. }
  56. static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
  57. size_t *retlen, void **virt, resource_size_t *phys)
  58. {
  59. *virt = mtd->priv + from;
  60. *retlen = len;
  61. if (phys) {
  62. /* limit retlen to the number of contiguous physical pages */
  63. unsigned long page_ofs = offset_in_page(*virt);
  64. void *addr = *virt - page_ofs;
  65. unsigned long pfn1, pfn0 = vmalloc_to_pfn(addr);
  66. *phys = __pfn_to_phys(pfn0) + page_ofs;
  67. len += page_ofs;
  68. while (len > PAGE_SIZE) {
  69. len -= PAGE_SIZE;
  70. addr += PAGE_SIZE;
  71. pfn0++;
  72. pfn1 = vmalloc_to_pfn(addr);
  73. if (pfn1 != pfn0) {
  74. *retlen = addr - *virt;
  75. break;
  76. }
  77. }
  78. }
  79. return 0;
  80. }
  81. static int ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  82. {
  83. return 0;
  84. }
  85. static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
  86. size_t *retlen, u_char *buf)
  87. {
  88. memcpy(buf, mtd->priv + from, len);
  89. *retlen = len;
  90. return 0;
  91. }
  92. static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
  93. size_t *retlen, const u_char *buf)
  94. {
  95. memcpy((char *)mtd->priv + to, buf, len);
  96. *retlen = len;
  97. return 0;
  98. }
  99. static void __exit cleanup_mtdram(void)
  100. {
  101. if (mtd_info) {
  102. mtd_device_unregister(mtd_info);
  103. vfree(mtd_info->priv);
  104. kfree(mtd_info);
  105. }
  106. }
  107. int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
  108. unsigned long size, const char *name)
  109. {
  110. memset(mtd, 0, sizeof(*mtd));
  111. /* Setup the MTD structure */
  112. mtd->name = name;
  113. mtd->type = MTD_RAM;
  114. mtd->flags = MTD_CAP_RAM;
  115. mtd->size = size;
  116. mtd->writesize = 1;
  117. mtd->writebufsize = writebuf_size;
  118. mtd->erasesize = MTDRAM_ERASE_SIZE;
  119. mtd->priv = mapped_address;
  120. mtd->owner = THIS_MODULE;
  121. mtd->_erase = ram_erase;
  122. mtd->_point = ram_point;
  123. mtd->_unpoint = ram_unpoint;
  124. mtd->_read = ram_read;
  125. mtd->_write = ram_write;
  126. if (mtd_device_register(mtd, NULL, 0))
  127. return -EIO;
  128. return 0;
  129. }
  130. static int __init init_mtdram(void)
  131. {
  132. void *addr;
  133. int err;
  134. if (!total_size)
  135. return -EINVAL;
  136. /* Allocate some memory */
  137. mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  138. if (!mtd_info)
  139. return -ENOMEM;
  140. addr = vmalloc(MTDRAM_TOTAL_SIZE);
  141. if (!addr) {
  142. kfree(mtd_info);
  143. mtd_info = NULL;
  144. return -ENOMEM;
  145. }
  146. err = mtdram_init_device(mtd_info, addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
  147. if (err) {
  148. vfree(addr);
  149. kfree(mtd_info);
  150. mtd_info = NULL;
  151. return err;
  152. }
  153. memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
  154. return err;
  155. }
  156. module_init(init_mtdram);
  157. module_exit(cleanup_mtdram);
  158. MODULE_LICENSE("GPL");
  159. MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
  160. MODULE_DESCRIPTION("Simulated MTD driver for testing");