elan-104nc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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