cfi_util.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Common Flash Interface support:
  3. * Generic utility functions not dependent on command set
  4. *
  5. * Copyright (C) 2002 Red Hat
  6. * Copyright (C) 2003 STMicroelectronics Limited
  7. *
  8. * This code is covered by the GPL.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <asm/io.h>
  14. #include <asm/byteorder.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/mtd/xip.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/map.h>
  22. #include <linux/mtd/cfi.h>
  23. int __xipram cfi_qry_present(struct map_info *map, __u32 base,
  24. struct cfi_private *cfi)
  25. {
  26. int osf = cfi->interleave * cfi->device_type; /* scale factor */
  27. map_word val[3];
  28. map_word qry[3];
  29. qry[0] = cfi_build_cmd('Q', map, cfi);
  30. qry[1] = cfi_build_cmd('R', map, cfi);
  31. qry[2] = cfi_build_cmd('Y', map, cfi);
  32. val[0] = map_read(map, base + osf*0x10);
  33. val[1] = map_read(map, base + osf*0x11);
  34. val[2] = map_read(map, base + osf*0x12);
  35. if (!map_word_equal(map, qry[0], val[0]))
  36. return 0;
  37. if (!map_word_equal(map, qry[1], val[1]))
  38. return 0;
  39. if (!map_word_equal(map, qry[2], val[2]))
  40. return 0;
  41. return 1; /* "QRY" found */
  42. }
  43. EXPORT_SYMBOL_GPL(cfi_qry_present);
  44. int __xipram cfi_qry_mode_on(uint32_t base, struct map_info *map,
  45. struct cfi_private *cfi)
  46. {
  47. cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
  48. cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
  49. if (cfi_qry_present(map, base, cfi))
  50. return 1;
  51. /* QRY not found probably we deal with some odd CFI chips */
  52. /* Some revisions of some old Intel chips? */
  53. cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
  54. cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
  55. cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
  56. if (cfi_qry_present(map, base, cfi))
  57. return 1;
  58. /* ST M29DW chips */
  59. cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
  60. cfi_send_gen_cmd(0x98, 0x555, base, map, cfi, cfi->device_type, NULL);
  61. if (cfi_qry_present(map, base, cfi))
  62. return 1;
  63. /* some old SST chips, e.g. 39VF160x/39VF320x */
  64. cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
  65. cfi_send_gen_cmd(0xAA, 0x5555, base, map, cfi, cfi->device_type, NULL);
  66. cfi_send_gen_cmd(0x55, 0x2AAA, base, map, cfi, cfi->device_type, NULL);
  67. cfi_send_gen_cmd(0x98, 0x5555, base, map, cfi, cfi->device_type, NULL);
  68. if (cfi_qry_present(map, base, cfi))
  69. return 1;
  70. /* SST 39VF640xB */
  71. cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
  72. cfi_send_gen_cmd(0xAA, 0x555, base, map, cfi, cfi->device_type, NULL);
  73. cfi_send_gen_cmd(0x55, 0x2AA, base, map, cfi, cfi->device_type, NULL);
  74. cfi_send_gen_cmd(0x98, 0x555, base, map, cfi, cfi->device_type, NULL);
  75. if (cfi_qry_present(map, base, cfi))
  76. return 1;
  77. /* QRY not found */
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_GPL(cfi_qry_mode_on);
  81. void __xipram cfi_qry_mode_off(uint32_t base, struct map_info *map,
  82. struct cfi_private *cfi)
  83. {
  84. cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
  85. cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
  86. /* M29W128G flashes require an additional reset command
  87. when exit qry mode */
  88. if ((cfi->mfr == CFI_MFR_ST) && (cfi->id == 0x227E || cfi->id == 0x7E))
  89. cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
  90. }
  91. EXPORT_SYMBOL_GPL(cfi_qry_mode_off);
  92. struct cfi_extquery *
  93. __xipram cfi_read_pri(struct map_info *map, __u16 adr, __u16 size, const char* name)
  94. {
  95. struct cfi_private *cfi = map->fldrv_priv;
  96. __u32 base = 0; // cfi->chips[0].start;
  97. int ofs_factor = cfi->interleave * cfi->device_type;
  98. int i;
  99. struct cfi_extquery *extp = NULL;
  100. if (!adr)
  101. goto out;
  102. printk(KERN_INFO "%s Extended Query Table at 0x%4.4X\n", name, adr);
  103. extp = kmalloc(size, GFP_KERNEL);
  104. if (!extp) {
  105. printk(KERN_ERR "Failed to allocate memory\n");
  106. goto out;
  107. }
  108. #ifdef CONFIG_MTD_XIP
  109. local_irq_disable();
  110. #endif
  111. /* Switch it into Query Mode */
  112. cfi_qry_mode_on(base, map, cfi);
  113. /* Read in the Extended Query Table */
  114. for (i=0; i<size; i++) {
  115. ((unsigned char *)extp)[i] =
  116. cfi_read_query(map, base+((adr+i)*ofs_factor));
  117. }
  118. /* Make sure it returns to read mode */
  119. cfi_qry_mode_off(base, map, cfi);
  120. #ifdef CONFIG_MTD_XIP
  121. (void) map_read(map, base);
  122. xip_iprefetch();
  123. local_irq_enable();
  124. #endif
  125. out: return extp;
  126. }
  127. EXPORT_SYMBOL(cfi_read_pri);
  128. void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup *fixups)
  129. {
  130. struct map_info *map = mtd->priv;
  131. struct cfi_private *cfi = map->fldrv_priv;
  132. struct cfi_fixup *f;
  133. for (f=fixups; f->fixup; f++) {
  134. if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi->mfr)) &&
  135. ((f->id == CFI_ID_ANY) || (f->id == cfi->id))) {
  136. f->fixup(mtd);
  137. }
  138. }
  139. }
  140. EXPORT_SYMBOL(cfi_fixup);
  141. int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
  142. loff_t ofs, size_t len, void *thunk)
  143. {
  144. struct map_info *map = mtd->priv;
  145. struct cfi_private *cfi = map->fldrv_priv;
  146. unsigned long adr;
  147. int chipnum, ret = 0;
  148. int i, first;
  149. struct mtd_erase_region_info *regions = mtd->eraseregions;
  150. /* Check that both start and end of the requested erase are
  151. * aligned with the erasesize at the appropriate addresses.
  152. */
  153. i = 0;
  154. /* Skip all erase regions which are ended before the start of
  155. the requested erase. Actually, to save on the calculations,
  156. we skip to the first erase region which starts after the
  157. start of the requested erase, and then go back one.
  158. */
  159. while (i < mtd->numeraseregions && ofs >= regions[i].offset)
  160. i++;
  161. i--;
  162. /* OK, now i is pointing at the erase region in which this
  163. erase request starts. Check the start of the requested
  164. erase range is aligned with the erase size which is in
  165. effect here.
  166. */
  167. if (ofs & (regions[i].erasesize-1))
  168. return -EINVAL;
  169. /* Remember the erase region we start on */
  170. first = i;
  171. /* Next, check that the end of the requested erase is aligned
  172. * with the erase region at that address.
  173. */
  174. while (i<mtd->numeraseregions && (ofs + len) >= regions[i].offset)
  175. i++;
  176. /* As before, drop back one to point at the region in which
  177. the address actually falls
  178. */
  179. i--;
  180. if ((ofs + len) & (regions[i].erasesize-1))
  181. return -EINVAL;
  182. chipnum = ofs >> cfi->chipshift;
  183. adr = ofs - (chipnum << cfi->chipshift);
  184. i=first;
  185. while(len) {
  186. int size = regions[i].erasesize;
  187. ret = (*frob)(map, &cfi->chips[chipnum], adr, size, thunk);
  188. if (ret)
  189. return ret;
  190. adr += size;
  191. ofs += size;
  192. len -= size;
  193. if (ofs == regions[i].offset + size * regions[i].numblocks)
  194. i++;
  195. if (adr >> cfi->chipshift) {
  196. adr = 0;
  197. chipnum++;
  198. if (chipnum >= cfi->numchips)
  199. break;
  200. }
  201. }
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(cfi_varsize_frob);
  205. MODULE_LICENSE("GPL");