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

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * Common Flash Interface support:
  3.  *   Intel Extended Vendor Command Set (ID 0x0001)
  4.  *
  5.  * (C) 2000 Red Hat. GPL'd
  6.  *
  7.  * $Id: cfi_cmdset_0001.c,v 1.96 2002/03/06 10:14:54 ch Exp $
  8.  *
  9.  * 
  10.  * 10/10/2000 Nicolas Pitre <nico@cam.org>
  11.  *  - completely revamped method functions so they are aware and
  12.  *    independent of the flash geometry (buswidth, interleave, etc.)
  13.  *  - scalability vs code size is completely set at compile-time
  14.  *    (see include/linux/mtd/cfi.h for selection)
  15.  * - optimized write buffer method
  16.  * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
  17.  * - reworked lock/unlock/erase support for var size flash
  18.  */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <asm/io.h>
  24. #include <asm/byteorder.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/mtd/map.h>
  30. #include <linux/mtd/cfi.h>
  31. #include <linux/mtd/compatmac.h>
  32. // debugging, turns off buffer write mode #define FORCE_WORD_WRITE
  33. static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  34. static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  35. static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  36. static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  37. static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  38. static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
  39. static void cfi_intelext_sync (struct mtd_info *);
  40. static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
  41. static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
  42. static int cfi_intelext_suspend (struct mtd_info *);
  43. static void cfi_intelext_resume (struct mtd_info *);
  44. static void cfi_intelext_destroy(struct mtd_info *);
  45. struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
  46. static struct mtd_info *cfi_intelext_setup (struct map_info *);
  47. static struct mtd_chip_driver cfi_intelext_chipdrv = {
  48. probe: NULL, /* Not usable directly */
  49. destroy: cfi_intelext_destroy,
  50. name: "cfi_cmdset_0001",
  51. module: THIS_MODULE
  52. };
  53. /* #define DEBUG_LOCK_BITS */
  54. /* #define DEBUG_CFI_FEATURES */
  55. #ifdef DEBUG_CFI_FEATURES
  56. static void cfi_tell_features(struct cfi_pri_intelext *extp)
  57. {
  58. int i;
  59. printk("  Feature/Command Support: %4.4Xn", extp->FeatureSupport);
  60. printk("     - Chip Erase:         %sn", extp->FeatureSupport&1?"supported":"unsupported");
  61. printk("     - Suspend Erase:      %sn", extp->FeatureSupport&2?"supported":"unsupported");
  62. printk("     - Suspend Program:    %sn", extp->FeatureSupport&4?"supported":"unsupported");
  63. printk("     - Legacy Lock/Unlock: %sn", extp->FeatureSupport&8?"supported":"unsupported");
  64. printk("     - Queued Erase:       %sn", extp->FeatureSupport&16?"supported":"unsupported");
  65. printk("     - Instant block lock: %sn", extp->FeatureSupport&32?"supported":"unsupported");
  66. printk("     - Protection Bits:    %sn", extp->FeatureSupport&64?"supported":"unsupported");
  67. printk("     - Page-mode read:     %sn", extp->FeatureSupport&128?"supported":"unsupported");
  68. printk("     - Synchronous read:   %sn", extp->FeatureSupport&256?"supported":"unsupported");
  69. for (i=9; i<32; i++) {
  70. if (extp->FeatureSupport & (1<<i)) 
  71. printk("     - Unknown Bit %X:      supportedn", i);
  72. }
  73. printk("  Supported functions after Suspend: %2.2Xn", extp->SuspendCmdSupport);
  74. printk("     - Program after Erase Suspend: %sn", extp->SuspendCmdSupport&1?"supported":"unsupported");
  75. for (i=1; i<8; i++) {
  76. if (extp->SuspendCmdSupport & (1<<i))
  77. printk("     - Unknown Bit %X:               supportedn", i);
  78. }
  79. printk("  Block Status Register Mask: %4.4Xn", extp->BlkStatusRegMask);
  80. printk("     - Lock Bit Active:      %sn", extp->BlkStatusRegMask&1?"yes":"no");
  81. printk("     - Valid Bit Active:     %sn", extp->BlkStatusRegMask&2?"yes":"no");
  82. for (i=2; i<16; i++) {
  83. if (extp->BlkStatusRegMask & (1<<i))
  84. printk("     - Unknown Bit %X Active: yesn",i);
  85. }
  86. printk("  Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d Vn", 
  87.        extp->VccOptimal >> 8, extp->VccOptimal & 0xf);
  88. if (extp->VppOptimal)
  89. printk("  Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d Vn", 
  90.        extp->VppOptimal >> 8, extp->VppOptimal & 0xf);
  91. }
  92. #endif
  93. /* This routine is made available to other mtd code via
  94.  * inter_module_register.  It must only be accessed through
  95.  * inter_module_get which will bump the use count of this module.  The
  96.  * addresses passed back in cfi are valid as long as the use count of
  97.  * this module is non-zero, i.e. between inter_module_get and
  98.  * inter_module_put.  Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
  99.  */
  100. struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
  101. {
  102. struct cfi_private *cfi = map->fldrv_priv;
  103. int i;
  104. __u32 base = cfi->chips[0].start;
  105. if (cfi->cfi_mode == CFI_MODE_CFI) {
  106. /* 
  107.  * It's a real CFI chip, not one for which the probe
  108.  * routine faked a CFI structure. So we read the feature
  109.  * table from it.
  110.  */
  111. __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
  112. struct cfi_pri_intelext *extp;
  113. int ofs_factor = cfi->interleave * cfi->device_type;
  114. //printk(" Intel/Sharp Extended Query Table at 0x%4.4Xn", adr);
  115. if (!adr)
  116. return NULL;
  117. /* Switch it into Query Mode */
  118. cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
  119. extp = kmalloc(sizeof(*extp), GFP_KERNEL);
  120. if (!extp) {
  121. printk(KERN_ERR "Failed to allocate memoryn");
  122. return NULL;
  123. }
  124. /* Read in the Extended Query Table */
  125. for (i=0; i<sizeof(*extp); i++) {
  126. ((unsigned char *)extp)[i] = 
  127. cfi_read_query(map, (base+((adr+i)*ofs_factor)));
  128. }
  129. if (extp->MajorVersion != '1' || 
  130.     (extp->MinorVersion < '0' || extp->MinorVersion > '2')) {
  131. printk(KERN_WARNING "  Unknown IntelExt Extended Query "
  132.        "version %c.%c.n",  extp->MajorVersion,
  133.        extp->MinorVersion);
  134. kfree(extp);
  135. return NULL;
  136. }
  137. /* Do some byteswapping if necessary */
  138. extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
  139. extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
  140. extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
  141. #ifdef DEBUG_CFI_FEATURES
  142. /* Tell the user about it in lots of lovely detail */
  143. cfi_tell_features(extp);
  144. #endif
  145. /* Install our own private info structure */
  146. cfi->cmdset_priv = extp;
  147. }
  148. for (i=0; i< cfi->numchips; i++) {
  149. cfi->chips[i].word_write_time = 128;
  150. cfi->chips[i].buffer_write_time = 128;
  151. cfi->chips[i].erase_time = 1024;
  152. }
  153. map->fldrv = &cfi_intelext_chipdrv;
  154. MOD_INC_USE_COUNT;
  155. /* Make sure it's in read mode */
  156. cfi_send_gen_cmd(0xff, 0x55, base, map, cfi, cfi->device_type, NULL);
  157. return cfi_intelext_setup(map);
  158. }
  159. static struct mtd_info *cfi_intelext_setup(struct map_info *map)
  160. {
  161. struct cfi_private *cfi = map->fldrv_priv;
  162. struct mtd_info *mtd;
  163. unsigned long offset = 0;
  164. int i,j;
  165. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  166. mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
  167. //printk(KERN_DEBUG "number of CFI chips: %dn", cfi->numchips);
  168. if (!mtd) {
  169. printk(KERN_ERR "Failed to allocate memory for MTD devicen");
  170. kfree(cfi->cmdset_priv);
  171. return NULL;
  172. }
  173. memset(mtd, 0, sizeof(*mtd));
  174. mtd->priv = map;
  175. mtd->type = MTD_NORFLASH;
  176. mtd->size = devsize * cfi->numchips;
  177. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  178. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info) 
  179. * mtd->numeraseregions, GFP_KERNEL);
  180. if (!mtd->eraseregions) { 
  181. printk(KERN_ERR "Failed to allocate memory for MTD erase region infon");
  182. kfree(cfi->cmdset_priv);
  183. return NULL;
  184. }
  185. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  186. unsigned long ernum, ersize;
  187. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  188. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  189. if (mtd->erasesize < ersize) {
  190. mtd->erasesize = ersize;
  191. }
  192. for (j=0; j<cfi->numchips; j++) {
  193. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  194. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  195. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  196. }
  197. offset += (ersize * ernum);
  198. }
  199. if (offset != devsize) {
  200. /* Argh */
  201. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)n", offset, devsize);
  202. kfree(mtd->eraseregions);
  203. kfree(cfi->cmdset_priv);
  204. return NULL;
  205. }
  206. for (i=0; i<mtd->numeraseregions;i++){
  207. printk(KERN_DEBUG "%d: offset=0x%x,size=0x%x,blocks=%dn",
  208.        i,mtd->eraseregions[i].offset,
  209.        mtd->eraseregions[i].erasesize,
  210.        mtd->eraseregions[i].numblocks);
  211. }
  212. /* Also select the correct geometry setup too */ 
  213. mtd->erase = cfi_intelext_erase_varsize;
  214. mtd->read = cfi_intelext_read;
  215. #ifndef FORCE_WORD_WRITE
  216. if ( cfi->cfiq->BufWriteTimeoutTyp ) {
  217. printk("Using buffer write methodn" );
  218. mtd->write = cfi_intelext_write_buffers;
  219. } else {
  220. #else
  221. {
  222. #endif
  223. printk("Using word write methodn" );
  224. mtd->write = cfi_intelext_write_words;
  225. }
  226. mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
  227. mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
  228. mtd->sync = cfi_intelext_sync;
  229. mtd->lock = cfi_intelext_lock;
  230. mtd->unlock = cfi_intelext_unlock;
  231. mtd->suspend = cfi_intelext_suspend;
  232. mtd->resume = cfi_intelext_resume;
  233. mtd->flags = MTD_CAP_NORFLASH;
  234. map->fldrv = &cfi_intelext_chipdrv;
  235. MOD_INC_USE_COUNT;
  236. mtd->name = map->name;
  237. return mtd;
  238. }
  239. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  240. {
  241. cfi_word status, status_OK;
  242. unsigned long timeo;
  243. DECLARE_WAITQUEUE(wait, current);
  244. int suspended = 0;
  245. unsigned long cmd_addr;
  246. struct cfi_private *cfi = map->fldrv_priv;
  247. adr += chip->start;
  248. /* Ensure cmd read/writes are aligned. */ 
  249. cmd_addr = adr & ~(CFIDEV_BUSWIDTH-1); 
  250. /* Let's determine this according to the interleave only once */
  251. status_OK = CMD(0x80);
  252. timeo = jiffies + HZ;
  253.  retry:
  254. spin_lock_bh(chip->mutex);
  255. /* Check that the chip's ready to talk to us.
  256.  * If it's in FL_ERASING state, suspend it and make it talk now.
  257.  */
  258. switch (chip->state) {
  259. case FL_ERASING:
  260. if (!(((struct cfi_pri_intelext *)cfi->cmdset_priv)->FeatureSupport & 2))
  261. goto sleep; /* We don't support erase suspend */
  262. cfi_write (map, CMD(0xb0), cmd_addr);
  263. /* If the flash has finished erasing, then 'erase suspend'
  264.  * appears to make some (28F320) flash devices switch to
  265.  * 'read' mode.  Make sure that we switch to 'read status'
  266.  * mode so we get the right data. --rmk
  267.  */
  268. cfi_write(map, CMD(0x70), cmd_addr);
  269. chip->oldstate = FL_ERASING;
  270. chip->state = FL_ERASE_SUSPENDING;
  271. // printk("Erase suspending at 0x%lxn", cmd_addr);
  272. for (;;) {
  273. status = cfi_read(map, cmd_addr);
  274. if ((status & status_OK) == status_OK)
  275. break;
  276. if (time_after(jiffies, timeo)) {
  277. /* Urgh */
  278. cfi_write(map, CMD(0xd0), cmd_addr);
  279. /* make sure we're in 'read status' mode */
  280. cfi_write(map, CMD(0x70), cmd_addr);
  281. chip->state = FL_ERASING;
  282. spin_unlock_bh(chip->mutex);
  283. printk(KERN_ERR "Chip not ready after erase "
  284.        "suspended: status = 0x%llxn", (__u64)status);
  285. return -EIO;
  286. }
  287. spin_unlock_bh(chip->mutex);
  288. cfi_udelay(1);
  289. spin_lock_bh(chip->mutex);
  290. }
  291. suspended = 1;
  292. cfi_write(map, CMD(0xff), cmd_addr);
  293. chip->state = FL_READY;
  294. break;
  295. #if 0
  296. case FL_WRITING:
  297. /* Not quite yet */
  298. #endif
  299. case FL_READY:
  300. break;
  301. case FL_CFI_QUERY:
  302. case FL_JEDEC_QUERY:
  303. cfi_write(map, CMD(0x70), cmd_addr);
  304. chip->state = FL_STATUS;
  305. case FL_STATUS:
  306. status = cfi_read(map, cmd_addr);
  307. if ((status & status_OK) == status_OK) {
  308. cfi_write(map, CMD(0xff), cmd_addr);
  309. chip->state = FL_READY;
  310. break;
  311. }
  312. /* Urgh. Chip not yet ready to talk to us. */
  313. if (time_after(jiffies, timeo)) {
  314. spin_unlock_bh(chip->mutex);
  315. printk(KERN_ERR "waiting for chip to be ready timed out in read. WSM status = %llxn", (__u64)status);
  316. return -EIO;
  317. }
  318. /* Latency issues. Drop the lock, wait a while and retry */
  319. spin_unlock_bh(chip->mutex);
  320. cfi_udelay(1);
  321. goto retry;
  322. default:
  323. sleep:
  324. /* Stick ourselves on a wait queue to be woken when
  325.    someone changes the status */
  326. set_current_state(TASK_UNINTERRUPTIBLE);
  327. add_wait_queue(&chip->wq, &wait);
  328. spin_unlock_bh(chip->mutex);
  329. schedule();
  330. remove_wait_queue(&chip->wq, &wait);
  331. timeo = jiffies + HZ;
  332. goto retry;
  333. }
  334. map->copy_from(map, buf, adr, len);
  335. if (suspended) {
  336. chip->state = chip->oldstate;
  337. /* What if one interleaved chip has finished and the 
  338.    other hasn't? The old code would leave the finished
  339.    one in READY mode. That's bad, and caused -EROFS 
  340.    errors to be returned from do_erase_oneblock because
  341.    that's the only bit it checked for at the time.
  342.    As the state machine appears to explicitly allow 
  343.    sending the 0x70 (Read Status) command to an erasing
  344.    chip and expecting it to be ignored, that's what we 
  345.    do. */
  346. cfi_write(map, CMD(0xd0), cmd_addr);
  347. cfi_write(map, CMD(0x70), cmd_addr);
  348. }
  349. wake_up(&chip->wq);
  350. spin_unlock_bh(chip->mutex);
  351. return 0;
  352. }
  353. static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  354. {
  355. struct map_info *map = mtd->priv;
  356. struct cfi_private *cfi = map->fldrv_priv;
  357. unsigned long ofs;
  358. int chipnum;
  359. int ret = 0;
  360. /* ofs: offset within the first chip that the first read should start */
  361. chipnum = (from >> cfi->chipshift);
  362. ofs = from - (chipnum <<  cfi->chipshift);
  363. *retlen = 0;
  364. while (len) {
  365. unsigned long thislen;
  366. if (chipnum >= cfi->numchips)
  367. break;
  368. if ((len + ofs -1) >> cfi->chipshift)
  369. thislen = (1<<cfi->chipshift) - ofs;
  370. else
  371. thislen = len;
  372. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  373. if (ret)
  374. break;
  375. *retlen += thislen;
  376. len -= thislen;
  377. buf += thislen;
  378. ofs = 0;
  379. chipnum++;
  380. }
  381. return ret;
  382. }
  383. static int cfi_intelext_read_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf, int base_offst, int reg_sz)
  384. {
  385. struct map_info *map = mtd->priv;
  386. struct cfi_private *cfi = map->fldrv_priv;
  387. struct cfi_pri_intelext *extp=cfi->cmdset_priv;
  388. int ofs_factor = cfi->interleave * cfi->device_type;
  389. int   count=len;
  390. struct flchip *chip;
  391. int chip_num,offst;
  392. unsigned long timeo;
  393. DECLARE_WAITQUEUE(wait, current);
  394. chip=0;
  395. /* Calculate which chip & protection register offset we need */
  396. chip_num=((unsigned int)from/reg_sz);
  397. offst=from-(reg_sz*chip_num)+base_offst;
  398. while(count){
  399. if(chip_num>=cfi->numchips)
  400. goto out;
  401. /* Make sure that the chip is in the right state */
  402. timeo = jiffies + HZ;
  403. chip=&cfi->chips[chip_num];
  404. retry:
  405. spin_lock_bh(chip->mutex);
  406. switch (chip->state) {
  407. case FL_READY:
  408. case FL_STATUS:
  409. case FL_CFI_QUERY:
  410. case FL_JEDEC_QUERY:
  411. break;
  412. default:
  413. /* Stick ourselves on a wait queue to be woken when
  414.    someone changes the status */
  415. set_current_state(TASK_UNINTERRUPTIBLE);
  416. add_wait_queue(&chip->wq, &wait);
  417. spin_unlock_bh(chip->mutex);
  418. schedule();
  419. remove_wait_queue(&chip->wq, &wait);
  420. timeo = jiffies + HZ;
  421. goto retry;
  422. }
  423. /* Now read the data required from this flash */
  424.        
  425. cfi_send_gen_cmd(0x90, 0x55,chip->start, map, cfi, cfi->device_type, NULL);
  426. while(count && ((offst-base_offst)<reg_sz)){
  427. *buf=map->read8(map,(chip->start+(extp->ProtRegAddr*ofs_factor)+offst));
  428. buf++;
  429. offst++;
  430. count--;
  431. }
  432.        
  433. chip->state=FL_CFI_QUERY;
  434. spin_unlock_bh(chip->mutex);
  435. /* Move on to the next chip */
  436. chip_num++;
  437. offst=base_offst;
  438. }
  439.  out:
  440. wake_up(&chip->wq);
  441. return len-count;
  442. }
  443. static int cfi_intelext_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  444. {
  445. struct map_info *map = mtd->priv;
  446. struct cfi_private *cfi = map->fldrv_priv;
  447. struct cfi_pri_intelext *extp=cfi->cmdset_priv;
  448. int base_offst,reg_sz;
  449. /* Check that we actually have some protection registers */
  450. if(!(extp->FeatureSupport&64)){
  451. printk(KERN_WARNING "%s: This flash device has no protection data to read!n",map->name);
  452. return 0;
  453. }
  454. base_offst=(1<<extp->FactProtRegSize);
  455. reg_sz=(1<<extp->UserProtRegSize);
  456. return cfi_intelext_read_prot_reg(mtd, from, len, retlen, buf, base_offst, reg_sz);
  457. }
  458. static int cfi_intelext_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  459. {
  460. struct map_info *map = mtd->priv;
  461. struct cfi_private *cfi = map->fldrv_priv;
  462. struct cfi_pri_intelext *extp=cfi->cmdset_priv;
  463. int base_offst,reg_sz;
  464. /* Check that we actually have some protection registers */
  465. if(!(extp->FeatureSupport&64)){
  466. printk(KERN_WARNING "%s: This flash device has no protection data to read!n",map->name);
  467. return 0;
  468. }
  469. base_offst=0;
  470. reg_sz=(1<<extp->FactProtRegSize);
  471. return cfi_intelext_read_prot_reg(mtd, from, len, retlen, buf, base_offst, reg_sz);
  472. }
  473. static int do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, cfi_word datum)
  474. {
  475. struct cfi_private *cfi = map->fldrv_priv;
  476. cfi_word status, status_OK;
  477. unsigned long timeo;
  478. DECLARE_WAITQUEUE(wait, current);
  479. int z;
  480. adr += chip->start;
  481. /* Let's determine this according to the interleave only once */
  482. status_OK = CMD(0x80);
  483. timeo = jiffies + HZ;
  484.  retry:
  485. spin_lock_bh(chip->mutex);
  486. /* Check that the chip's ready to talk to us.
  487.  * Later, we can actually think about interrupting it
  488.  * if it's in FL_ERASING state.
  489.  * Not just yet, though.
  490.  */
  491. switch (chip->state) {
  492. case FL_READY:
  493. break;
  494. case FL_CFI_QUERY:
  495. case FL_JEDEC_QUERY:
  496. cfi_write(map, CMD(0x70), adr);
  497. chip->state = FL_STATUS;
  498. case FL_STATUS:
  499. status = cfi_read(map, adr);
  500. if ((status & status_OK) == status_OK)
  501. break;
  502. /* Urgh. Chip not yet ready to talk to us. */
  503. if (time_after(jiffies, timeo)) {
  504. spin_unlock_bh(chip->mutex);
  505. printk(KERN_ERR "waiting for chip to be ready timed out in readn");
  506. return -EIO;
  507. }
  508. /* Latency issues. Drop the lock, wait a while and retry */
  509. spin_unlock_bh(chip->mutex);
  510. cfi_udelay(1);
  511. goto retry;
  512. default:
  513. /* Stick ourselves on a wait queue to be woken when
  514.    someone changes the status */
  515. set_current_state(TASK_UNINTERRUPTIBLE);
  516. add_wait_queue(&chip->wq, &wait);
  517. spin_unlock_bh(chip->mutex);
  518. schedule();
  519. remove_wait_queue(&chip->wq, &wait);
  520. timeo = jiffies + HZ;
  521. goto retry;
  522. }
  523. ENABLE_VPP(map);
  524. cfi_write(map, CMD(0x40), adr);
  525. cfi_write(map, datum, adr);
  526. chip->state = FL_WRITING;
  527. spin_unlock_bh(chip->mutex);
  528. cfi_udelay(chip->word_write_time);
  529. spin_lock_bh(chip->mutex);
  530. timeo = jiffies + (HZ/2);
  531. z = 0;
  532. for (;;) {
  533. if (chip->state != FL_WRITING) {
  534. /* Someone's suspended the write. Sleep */
  535. set_current_state(TASK_UNINTERRUPTIBLE);
  536. add_wait_queue(&chip->wq, &wait);
  537. spin_unlock_bh(chip->mutex);
  538. schedule();
  539. remove_wait_queue(&chip->wq, &wait);
  540. timeo = jiffies + (HZ / 2); /* FIXME */
  541. spin_lock_bh(chip->mutex);
  542. continue;
  543. }
  544. status = cfi_read(map, adr);
  545. if ((status & status_OK) == status_OK)
  546. break;
  547. /* OK Still waiting */
  548. if (time_after(jiffies, timeo)) {
  549. chip->state = FL_STATUS;
  550. DISABLE_VPP(map);
  551. spin_unlock_bh(chip->mutex);
  552. printk(KERN_ERR "waiting for chip to be ready timed out in word writen");
  553. return -EIO;
  554. }
  555. /* Latency issues. Drop the lock, wait a while and retry */
  556. spin_unlock_bh(chip->mutex);
  557. z++;
  558. cfi_udelay(1);
  559. spin_lock_bh(chip->mutex);
  560. }
  561. if (!z) {
  562. chip->word_write_time--;
  563. if (!chip->word_write_time)
  564. chip->word_write_time++;
  565. }
  566. if (z > 1) 
  567. chip->word_write_time++;
  568. /* Done and happy. */
  569. DISABLE_VPP(map);
  570. chip->state = FL_STATUS;
  571. /* check for lock bit */
  572. if (status & CMD(0x02)) {
  573. /* clear status */
  574. cfi_write(map, CMD(0x50), adr);
  575. /* put back into read status register mode */
  576. cfi_write(map, CMD(0x70), adr);
  577. wake_up(&chip->wq);
  578. spin_unlock_bh(chip->mutex);
  579. return -EROFS;
  580. }
  581. wake_up(&chip->wq);
  582. spin_unlock_bh(chip->mutex);
  583. return 0;
  584. }
  585. static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
  586. {
  587. struct map_info *map = mtd->priv;
  588. struct cfi_private *cfi = map->fldrv_priv;
  589. int ret = 0;
  590. int chipnum;
  591. unsigned long ofs;
  592. *retlen = 0;
  593. if (!len)
  594. return 0;
  595. chipnum = to >> cfi->chipshift;
  596. ofs = to  - (chipnum << cfi->chipshift);
  597. /* If it's not bus-aligned, do the first byte write */
  598. if (ofs & (CFIDEV_BUSWIDTH-1)) {
  599. unsigned long bus_ofs = ofs & ~(CFIDEV_BUSWIDTH-1);
  600. int gap = ofs - bus_ofs;
  601. int i = 0, n = 0;
  602. u_char tmp_buf[8];
  603. cfi_word datum;
  604. while (gap--)
  605. tmp_buf[i++] = 0xff;
  606. while (len && i < CFIDEV_BUSWIDTH)
  607. tmp_buf[i++] = buf[n++], len--;
  608. while (i < CFIDEV_BUSWIDTH)
  609. tmp_buf[i++] = 0xff;
  610. if (cfi_buswidth_is_2()) {
  611. datum = *(__u16*)tmp_buf;
  612. } else if (cfi_buswidth_is_4()) {
  613. datum = *(__u32*)tmp_buf;
  614. } else if (cfi_buswidth_is_8()) {
  615. datum = *(__u64*)tmp_buf;
  616. } else {
  617. return -EINVAL;  /* should never happen, but be safe */
  618. }
  619. ret = do_write_oneword(map, &cfi->chips[chipnum],
  620.        bus_ofs, datum);
  621. if (ret) 
  622. return ret;
  623. ofs += n;
  624. buf += n;
  625. (*retlen) += n;
  626. if (ofs >> cfi->chipshift) {
  627. chipnum ++; 
  628. ofs = 0;
  629. if (chipnum == cfi->numchips)
  630. return 0;
  631. }
  632. }
  633. while(len >= CFIDEV_BUSWIDTH) {
  634. cfi_word datum;
  635. if (cfi_buswidth_is_1()) {
  636. datum = *(__u8*)buf;
  637. } else if (cfi_buswidth_is_2()) {
  638. datum = *(__u16*)buf;
  639. } else if (cfi_buswidth_is_4()) {
  640. datum = *(__u32*)buf;
  641. } else if (cfi_buswidth_is_8()) {
  642. datum = *(__u64*)buf;
  643. } else {
  644. return -EINVAL;
  645. }
  646. ret = do_write_oneword(map, &cfi->chips[chipnum],
  647. ofs, datum);
  648. if (ret)
  649. return ret;
  650. ofs += CFIDEV_BUSWIDTH;
  651. buf += CFIDEV_BUSWIDTH;
  652. (*retlen) += CFIDEV_BUSWIDTH;
  653. len -= CFIDEV_BUSWIDTH;
  654. if (ofs >> cfi->chipshift) {
  655. chipnum ++; 
  656. ofs = 0;
  657. if (chipnum == cfi->numchips)
  658. return 0;
  659. }
  660. }
  661. if (len & (CFIDEV_BUSWIDTH-1)) {
  662. int i = 0, n = 0;
  663. u_char tmp_buf[8];
  664. cfi_word datum;
  665. while (len--)
  666. tmp_buf[i++] = buf[n++];
  667. while (i < CFIDEV_BUSWIDTH)
  668. tmp_buf[i++] = 0xff;
  669. if (cfi_buswidth_is_2()) {
  670. datum = *(__u16*)tmp_buf;
  671. } else if (cfi_buswidth_is_4()) {
  672. datum = *(__u32*)tmp_buf;
  673. } else if (cfi_buswidth_is_8()) {
  674. datum = *(__u64*)tmp_buf;
  675. } else {
  676. return -EINVAL;  /* should never happen, but be safe */
  677. }
  678. ret = do_write_oneword(map, &cfi->chips[chipnum],
  679.        ofs, datum);
  680. if (ret) 
  681. return ret;
  682. (*retlen) += n;
  683. }
  684. return 0;
  685. }
  686. static inline int do_write_buffer(struct map_info *map, struct flchip *chip, 
  687.   unsigned long adr, const u_char *buf, int len)
  688. {
  689. struct cfi_private *cfi = map->fldrv_priv;
  690. cfi_word status, status_OK;
  691. unsigned long cmd_adr, timeo;
  692. DECLARE_WAITQUEUE(wait, current);
  693. int wbufsize, z;
  694. wbufsize = CFIDEV_INTERLEAVE << cfi->cfiq->MaxBufWriteSize;
  695. adr += chip->start;
  696. cmd_adr = adr & ~(wbufsize-1);
  697. /* Let's determine this according to the interleave only once */
  698. status_OK = CMD(0x80);
  699. timeo = jiffies + HZ;
  700.  retry:
  701. spin_lock_bh(chip->mutex);
  702. /* Check that the chip's ready to talk to us.
  703.  * Later, we can actually think about interrupting it
  704.  * if it's in FL_ERASING state.
  705.  * Not just yet, though.
  706.  */
  707. switch (chip->state) {
  708. case FL_READY:
  709. case FL_CFI_QUERY:
  710. case FL_JEDEC_QUERY:
  711. cfi_write(map, CMD(0x70), cmd_adr);
  712. chip->state = FL_STATUS;
  713. case FL_STATUS:
  714. status = cfi_read(map, cmd_adr);
  715. if ((status & status_OK) == status_OK)
  716. break;
  717. /* Urgh. Chip not yet ready to talk to us. */
  718. if (time_after(jiffies, timeo)) {
  719. spin_unlock_bh(chip->mutex);
  720. printk(KERN_ERR "waiting for chip to be ready timed out in buffer writen");
  721. return -EIO;
  722. }
  723. /* Latency issues. Drop the lock, wait a while and retry */
  724. spin_unlock_bh(chip->mutex);
  725. cfi_udelay(1);
  726. goto retry;
  727. default:
  728. /* Stick ourselves on a wait queue to be woken when
  729.    someone changes the status */
  730. set_current_state(TASK_UNINTERRUPTIBLE);
  731. add_wait_queue(&chip->wq, &wait);
  732. spin_unlock_bh(chip->mutex);
  733. schedule();
  734. remove_wait_queue(&chip->wq, &wait);
  735. timeo = jiffies + HZ;
  736. goto retry;
  737. }
  738. /* We know we're now in FL_STATUS mode, and 'status' is current */
  739. /*