vmax301.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* ######################################################################
  2. Tempustech VMAX SBC301 MTD Driver.
  3. The VMAx 301 is a SBC based on . It
  4. comes with three builtin AMD 29F016B flash chips and a socket for SRAM or
  5. more flash. Each unit has it's own 8k mapping into a settable region
  6. (0xD8000). There are two 8k mappings for each MTD, the first is always set
  7. to the lower 8k of the device the second is paged. Writing a 16 bit page
  8. value to anywhere in the first 8k will cause the second 8k to page around.
  9. To boot the device a bios extension must be installed into the first 8k
  10. of flash that is smart enough to copy itself down, page in the rest of
  11. itself and begin executing.
  12. ##################################################################### */
  13. #include <linux/module.h>
  14. #include <linux/ioport.h>
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include <asm/io.h>
  18. #include <linux/mtd/map.h>
  19. #include <linux/mtd/mtd.h>
  20. #define WINDOW_START 0xd8000
  21. #define WINDOW_LENGTH 0x2000
  22. #define WINDOW_SHIFT 25
  23. #define WINDOW_MASK 0x1FFF
  24. /* Actually we could use two spinlocks, but we'd have to have
  25. more private space in the struct map_info. We lose a little
  26. performance like this, but we'd probably lose more by having
  27. the extra indirection from having one of the map->map_priv
  28. fields pointing to yet another private struct.
  29. */
  30. static DEFINE_SPINLOCK(vmax301_spin);
  31. static void __vmax301_page(struct map_info *map, unsigned long page)
  32. {
  33. writew(page, map->map_priv_2 - WINDOW_LENGTH);
  34. map->map_priv_1 = page;
  35. }
  36. static inline void vmax301_page(struct map_info *map,
  37. unsigned long ofs)
  38. {
  39. unsigned long page = (ofs >> WINDOW_SHIFT);
  40. if (map->map_priv_1 != page)
  41. __vmax301_page(map, page);
  42. }
  43. static map_word vmax301_read8(struct map_info *map, unsigned long ofs)
  44. {
  45. map_word ret;
  46. spin_lock(&vmax301_spin);
  47. vmax301_page(map, ofs);
  48. ret.x[0] = readb(map->map_priv_2 + (ofs & WINDOW_MASK));
  49. spin_unlock(&vmax301_spin);
  50. return ret;
  51. }
  52. static void vmax301_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  53. {
  54. while(len) {
  55. unsigned long thislen = len;
  56. if (len > (WINDOW_LENGTH - (from & WINDOW_MASK)))
  57. thislen = WINDOW_LENGTH-(from & WINDOW_MASK);
  58. spin_lock(&vmax301_spin);
  59. vmax301_page(map, from);
  60. memcpy_fromio(to, map->map_priv_2 + from, thislen);
  61. spin_unlock(&vmax301_spin);
  62. to += thislen;
  63. from += thislen;
  64. len -= thislen;
  65. }
  66. }
  67. static void vmax301_write8(struct map_info *map, map_word d, unsigned long adr)
  68. {
  69. spin_lock(&vmax301_spin);
  70. vmax301_page(map, adr);
  71. writeb(d.x[0], map->map_priv_2 + (adr & WINDOW_MASK));
  72. spin_unlock(&vmax301_spin);
  73. }
  74. static void vmax301_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  75. {
  76. while(len) {
  77. unsigned long thislen = len;
  78. if (len > (WINDOW_LENGTH - (to & WINDOW_MASK)))
  79. thislen = WINDOW_LENGTH-(to & WINDOW_MASK);
  80. spin_lock(&vmax301_spin);
  81. vmax301_page(map, to);
  82. memcpy_toio(map->map_priv_2 + to, from, thislen);
  83. spin_unlock(&vmax301_spin);
  84. to += thislen;
  85. from += thislen;
  86. len -= thislen;
  87. }
  88. }
  89. static struct map_info vmax_map[2] = {
  90. {
  91. .name = "VMAX301 Internal Flash",
  92. .phys = NO_XIP,
  93. .size = 3*2*1024*1024,
  94. .bankwidth = 1,
  95. .read = vmax301_read8,
  96. .copy_from = vmax301_copy_from,
  97. .write = vmax301_write8,
  98. .copy_to = vmax301_copy_to,
  99. .map_priv_1 = WINDOW_START + WINDOW_LENGTH,
  100. .map_priv_2 = 0xFFFFFFFF
  101. },
  102. {
  103. .name = "VMAX301 Socket",
  104. .phys = NO_XIP,
  105. .size = 0,
  106. .bankwidth = 1,
  107. .read = vmax301_read8,
  108. .copy_from = vmax301_copy_from,
  109. .write = vmax301_write8,
  110. .copy_to = vmax301_copy_to,
  111. .map_priv_1 = WINDOW_START + (3*WINDOW_LENGTH),
  112. .map_priv_2 = 0xFFFFFFFF
  113. }
  114. };
  115. static struct mtd_info *vmax_mtd[2] = {NULL, NULL};
  116. static void __exit cleanup_vmax301(void)
  117. {
  118. int i;
  119. for (i=0; i<2; i++) {
  120. if (vmax_mtd[i]) {
  121. mtd_device_unregister(vmax_mtd[i]);
  122. map_destroy(vmax_mtd[i]);
  123. }
  124. }
  125. iounmap((void *)vmax_map[0].map_priv_1 - WINDOW_START);
  126. }
  127. static int __init init_vmax301(void)
  128. {
  129. int i;
  130. unsigned long iomapadr;
  131. // Print out our little header..
  132. printk("Tempustech VMAX 301 MEM:0x%x-0x%x\n",WINDOW_START,
  133. WINDOW_START+4*WINDOW_LENGTH);
  134. iomapadr = (unsigned long)ioremap(WINDOW_START, WINDOW_LENGTH*4);
  135. if (!iomapadr) {
  136. printk("Failed to ioremap memory region\n");
  137. return -EIO;
  138. }
  139. /* Put the address in the map's private data area.
  140. We store the actual MTD IO address rather than the
  141. address of the first half, because it's used more
  142. often.
  143. */
  144. vmax_map[0].map_priv_2 = iomapadr + WINDOW_START;
  145. vmax_map[1].map_priv_2 = iomapadr + (3*WINDOW_START);
  146. for (i=0; i<2; i++) {
  147. vmax_mtd[i] = do_map_probe("cfi_probe", &vmax_map[i]);
  148. if (!vmax_mtd[i])
  149. vmax_mtd[i] = do_map_probe("jedec", &vmax_map[i]);
  150. if (!vmax_mtd[i])
  151. vmax_mtd[i] = do_map_probe("map_ram", &vmax_map[i]);
  152. if (!vmax_mtd[i])
  153. vmax_mtd[i] = do_map_probe("map_rom", &vmax_map[i]);
  154. if (vmax_mtd[i]) {
  155. vmax_mtd[i]->owner = THIS_MODULE;
  156. mtd_device_register(vmax_mtd[i], NULL, 0);
  157. }
  158. }
  159. if (!vmax_mtd[0] && !vmax_mtd[1]) {
  160. iounmap((void *)iomapadr);
  161. return -ENXIO;
  162. }
  163. return 0;
  164. }
  165. module_init(init_vmax301);
  166. module_exit(cleanup_vmax301);
  167. MODULE_LICENSE("GPL");
  168. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  169. MODULE_DESCRIPTION("MTD map driver for Tempustech VMAX SBC301 board");