elan-104nc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* elan-104nc.c -- MTD map driver for Arcom Control Systems ELAN-104NC
  2. Copyright (C) 2000 Arcom Control System Ltd
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  14. $Id: elan-104nc.c,v 1.25 2004/11/28 09:40:39 dwmw2 Exp $
  15. The ELAN-104NC has up to 8 Mibyte of Intel StrataFlash (28F320/28F640) in x16
  16. mode. This drivers uses the CFI probe and Intel Extended Command Set drivers.
  17. The flash is accessed as follows:
  18. 32 kbyte memory window at 0xb0000-0xb7fff
  19. 16 bit I/O port (0x22) for some sort of paging.
  20. The single flash device is divided into 3 partition which appear as separate
  21. MTD devices.
  22. Linux thinks that the I/O port is used by the PIC and hence check_region() will
  23. always fail. So we don't do it. I just hope it doesn't break anything.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/ioport.h>
  28. #include <linux/init.h>
  29. #include <asm/io.h>
  30. #include <linux/mtd/map.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/partitions.h>
  33. #define WINDOW_START 0xb0000
  34. /* Number of bits in offset. */
  35. #define WINDOW_SHIFT 15
  36. #define WINDOW_LENGTH (1 << WINDOW_SHIFT)
  37. /* The bits for the offset into the window. */
  38. #define WINDOW_MASK (WINDOW_LENGTH-1)
  39. #define PAGE_IO 0x22
  40. #define PAGE_IO_SIZE 2
  41. static volatile int page_in_window = -1; // Current page in window.
  42. static void __iomem *iomapadr;
  43. static DEFINE_SPINLOCK(elan_104nc_spin);
  44. /* partition_info gives details on the logical partitions that the split the
  45. * single flash device into. If the size if zero we use up to the end of the
  46. * device. */
  47. static struct mtd_partition partition_info[]={
  48. { .name = "ELAN-104NC flash boot partition",
  49. .offset = 0,
  50. .size = 640*1024 },
  51. { .name = "ELAN-104NC flash partition 1",
  52. .offset = 640*1024,
  53. .size = 896*1024 },
  54. { .name = "ELAN-104NC flash partition 2",
  55. .offset = (640+896)*1024 }
  56. };
  57. #define NUM_PARTITIONS (sizeof(partition_info)/sizeof(partition_info[0]))
  58. /*
  59. * If no idea what is going on here. This is taken from the FlashFX stuff.
  60. */
  61. #define ROMCS 1
  62. static inline void elan_104nc_setup(void)
  63. {
  64. u16 t;
  65. outw( 0x0023 + ROMCS*2, PAGE_IO );
  66. t=inb( PAGE_IO+1 );
  67. t=(t & 0xf9) | 0x04;
  68. outw( ((0x0023 + ROMCS*2) | (t << 8)), PAGE_IO );
  69. }
  70. static inline void elan_104nc_page(struct map_info *map, unsigned long ofs)
  71. {
  72. unsigned long page = ofs >> WINDOW_SHIFT;
  73. if( page!=page_in_window ) {
  74. int cmd1;
  75. int cmd2;
  76. cmd1=(page & 0x700) + 0x0833 + ROMCS*0x4000;
  77. cmd2=((page & 0xff) << 8) + 0x0032;
  78. outw( cmd1, PAGE_IO );
  79. outw( cmd2, PAGE_IO );
  80. page_in_window = page;
  81. }
  82. }
  83. static map_word elan_104nc_read16(struct map_info *map, unsigned long ofs)
  84. {
  85. map_word ret;
  86. spin_lock(&elan_104nc_spin);
  87. elan_104nc_page(map, ofs);
  88. ret.x[0] = readw(iomapadr + (ofs & WINDOW_MASK));
  89. spin_unlock(&elan_104nc_spin);
  90. return ret;
  91. }
  92. static void elan_104nc_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  93. {
  94. while (len) {
  95. unsigned long thislen = len;
  96. if (len > (WINDOW_LENGTH - (from & WINDOW_MASK)))
  97. thislen = WINDOW_LENGTH-(from & WINDOW_MASK);
  98. spin_lock(&elan_104nc_spin);
  99. elan_104nc_page(map, from);
  100. memcpy_fromio(to, iomapadr + (from & WINDOW_MASK), thislen);
  101. spin_unlock(&elan_104nc_spin);
  102. to += thislen;
  103. from += thislen;
  104. len -= thislen;
  105. }
  106. }
  107. static void elan_104nc_write16(struct map_info *map, map_word d, unsigned long adr)
  108. {
  109. spin_lock(&elan_104nc_spin);
  110. elan_104nc_page(map, adr);
  111. writew(d.x[0], iomapadr + (adr & WINDOW_MASK));
  112. spin_unlock(&elan_104nc_spin);
  113. }
  114. static void elan_104nc_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  115. {
  116. while(len) {
  117. unsigned long thislen = len;
  118. if (len > (WINDOW_LENGTH - (to & WINDOW_MASK)))
  119. thislen = WINDOW_LENGTH-(to & WINDOW_MASK);
  120. spin_lock(&elan_104nc_spin);
  121. elan_104nc_page(map, to);
  122. memcpy_toio(iomapadr + (to & WINDOW_MASK), from, thislen);
  123. spin_unlock(&elan_104nc_spin);
  124. to += thislen;
  125. from += thislen;
  126. len -= thislen;
  127. }
  128. }
  129. static struct map_info elan_104nc_map = {
  130. .name = "ELAN-104NC flash",
  131. .phys = NO_XIP,
  132. .size = 8*1024*1024, /* this must be set to a maximum possible amount
  133. of flash so the cfi probe routines find all
  134. the chips */
  135. .bankwidth = 2,
  136. .read = elan_104nc_read16,
  137. .copy_from = elan_104nc_copy_from,
  138. .write = elan_104nc_write16,
  139. .copy_to = elan_104nc_copy_to
  140. };
  141. /* MTD device for all of the flash. */
  142. static struct mtd_info *all_mtd;
  143. static void cleanup_elan_104nc(void)
  144. {
  145. if( all_mtd ) {
  146. del_mtd_partitions( all_mtd );
  147. map_destroy( all_mtd );
  148. }
  149. iounmap(iomapadr);
  150. }
  151. static int __init init_elan_104nc(void)
  152. {
  153. /* Urg! We use I/O port 0x22 without request_region()ing it,
  154. because it's already allocated to the PIC. */
  155. iomapadr = ioremap(WINDOW_START, WINDOW_LENGTH);
  156. if (!iomapadr) {
  157. printk( KERN_ERR"%s: failed to ioremap memory region\n",
  158. elan_104nc_map.name );
  159. return -EIO;
  160. }
  161. printk( KERN_INFO"%s: IO:0x%x-0x%x MEM:0x%x-0x%x\n",
  162. elan_104nc_map.name,
  163. PAGE_IO, PAGE_IO+PAGE_IO_SIZE-1,
  164. WINDOW_START, WINDOW_START+WINDOW_LENGTH-1 );
  165. elan_104nc_setup();
  166. /* Probe for chip. */
  167. all_mtd = do_map_probe("cfi_probe", &elan_104nc_map );
  168. if( !all_mtd ) {
  169. cleanup_elan_104nc();
  170. return -ENXIO;
  171. }
  172. all_mtd->owner = THIS_MODULE;
  173. /* Create MTD devices for each partition. */
  174. add_mtd_partitions( all_mtd, partition_info, NUM_PARTITIONS );
  175. return 0;
  176. }
  177. module_init(init_elan_104nc);
  178. module_exit(cleanup_elan_104nc);
  179. MODULE_LICENSE("GPL");
  180. MODULE_AUTHOR("Arcom Control Systems Ltd.");
  181. MODULE_DESCRIPTION("MTD map driver for Arcom Control Systems ELAN-104NC");