vmax301.c 5.3 KB

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