nora.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:4k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * $Id: nora.c,v 1.21 2001/10/02 15:05:14 dwmw2 Exp $
  3.  *
  4.  * This is so simple I love it.
  5.  */
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mtd/mtd.h>
  10. #include <linux/mtd/map.h>
  11. #define WINDOW_ADDR 0xd0000000
  12. #define WINDOW_SIZE 0x04000000
  13. static struct mtd_info *mymtd;
  14. __u8 nora_read8(struct map_info *map, unsigned long ofs)
  15. {
  16.   return *(__u8 *)(WINDOW_ADDR + ofs);
  17. }
  18. __u16 nora_read16(struct map_info *map, unsigned long ofs)
  19. {
  20.   return *(__u16 *)(WINDOW_ADDR + ofs);
  21. }
  22. __u32 nora_read32(struct map_info *map, unsigned long ofs)
  23. {
  24.   return *(__u32 *)(WINDOW_ADDR + ofs);
  25. }
  26. void nora_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  27. {
  28.   memcpy(to, (void *)(WINDOW_ADDR + from), len);
  29. }
  30. void nora_write8(struct map_info *map, __u8 d, unsigned long adr)
  31. {
  32.   *(__u8 *)(WINDOW_ADDR + adr) = d;
  33. }
  34. void nora_write16(struct map_info *map, __u16 d, unsigned long adr)
  35. {
  36.   *(__u16 *)(WINDOW_ADDR + adr) = d;
  37. }
  38. void nora_write32(struct map_info *map, __u32 d, unsigned long adr)
  39. {
  40.   *(__u32 *)(WINDOW_ADDR + adr) = d;
  41. }
  42. void nora_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  43. {
  44.   memcpy((void *)(WINDOW_ADDR + to), from, len);
  45. }
  46. struct map_info nora_map = {
  47. name: "NORA",
  48. size: WINDOW_SIZE,
  49. buswidth: 2,
  50. read8: nora_read8,
  51. read16: nora_read16,
  52. read32: nora_read32,
  53. copy_from: nora_copy_from,
  54. write8: nora_write8,
  55. write16: nora_write16,
  56. write32: nora_write32,
  57. copy_to: nora_copy_to
  58. };
  59. static int nora_mtd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  60. {
  61. return mymtd->read(mymtd, from + (unsigned long)mtd->priv, len, retlen, buf);
  62. }
  63. static int nora_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  64. {
  65. return mymtd->write(mymtd, to + (unsigned long)mtd->priv, len, retlen, buf);
  66. }
  67. static int nora_mtd_erase (struct mtd_info *mtd, struct erase_info *instr)
  68. {
  69. instr->addr += (unsigned long)mtd->priv;
  70. return mymtd->erase(mymtd, instr);
  71. }
  72. static void nora_mtd_sync (struct mtd_info *mtd)
  73. {
  74. mymtd->sync(mymtd);
  75. }
  76. static int nora_mtd_suspend (struct mtd_info *mtd)
  77. {
  78. return mymtd->suspend(mymtd);
  79. }
  80. static void nora_mtd_resume (struct mtd_info *mtd)
  81. {
  82. mymtd->resume(mymtd);
  83. }
  84. static struct mtd_info nora_mtds[4] = {  /* boot, kernel, ramdisk, fs */
  85. {
  86. type: MTD_NORFLASH,
  87. flags: MTD_CAP_NORFLASH,
  88. size: 0x60000,
  89. erasesize: 0x20000,
  90. name: "NORA boot firmware",
  91. module: THIS_MODULE,
  92. erase: nora_mtd_erase,
  93. read: nora_mtd_read,
  94. write: nora_mtd_write,
  95. suspend: nora_mtd_suspend,
  96. resume: nora_mtd_resume,
  97. sync: nora_mtd_sync,
  98. priv: (void *)0
  99. },
  100. {
  101. type: MTD_NORFLASH,
  102. flags: MTD_CAP_NORFLASH,
  103. size: 0x0a0000,
  104. erasesize: 0x20000,
  105. name: "NORA kernel",
  106. module: THIS_MODULE,
  107. erase: nora_mtd_erase,
  108. read: nora_mtd_read,
  109. write: nora_mtd_write,
  110. suspend: nora_mtd_suspend,
  111. resume: nora_mtd_resume,
  112. sync: nora_mtd_sync,
  113. priv: (void *)0x60000
  114. },
  115. {
  116. type: MTD_NORFLASH,
  117. flags: MTD_CAP_NORFLASH,
  118. size: 0x900000,
  119. erasesize: 0x20000,
  120. name: "NORA root filesystem",
  121. module: THIS_MODULE,
  122. erase: nora_mtd_erase,
  123. read: nora_mtd_read,
  124. write: nora_mtd_write,
  125. suspend: nora_mtd_suspend,
  126. resume: nora_mtd_resume,
  127. sync: nora_mtd_sync,
  128. priv: (void *)0x100000
  129. },
  130. {
  131. type: MTD_NORFLASH,
  132. flags: MTD_CAP_NORFLASH,
  133. size: 0x1600000,
  134. erasesize: 0x20000,
  135. name: "NORA second filesystem",
  136. module: THIS_MODULE,
  137. erase: nora_mtd_erase,
  138. read: nora_mtd_read,
  139. write: nora_mtd_write,
  140. suspend: nora_mtd_suspend,
  141. resume: nora_mtd_resume,
  142. sync: nora_mtd_sync,
  143. priv: (void *)0xa00000
  144. }
  145. };
  146. int __init init_nora(void)
  147. {
  148.         printk(KERN_NOTICE "nora flash device: %x at %xn", WINDOW_SIZE, WINDOW_ADDR);
  149. mymtd = do_map_probe("cfi_probe", &nora_map);
  150. if (mymtd) {
  151. mymtd->module = THIS_MODULE;
  152. add_mtd_device(&nora_mtds[2]);
  153. add_mtd_device(&nora_mtds[0]);
  154. add_mtd_device(&nora_mtds[1]);
  155. add_mtd_device(&nora_mtds[3]);
  156. return 0;
  157. }
  158. return -ENXIO;
  159. }
  160. static void __exit cleanup_nora(void)
  161. {
  162. if (mymtd) {
  163. del_mtd_device(&nora_mtds[3]);
  164. del_mtd_device(&nora_mtds[1]);
  165. del_mtd_device(&nora_mtds[0]);
  166. del_mtd_device(&nora_mtds[2]);
  167. map_destroy(mymtd);
  168. }
  169. }
  170. module_init(init_nora);
  171. module_exit(cleanup_nora);
  172. MODULE_LICENSE("GPL");
  173. MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
  174. MODULE_DESCRIPTION("MTD map driver for Nora board");