gen_probe.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Routines common to all CFI-type probes.
  3. * (C) 2001-2003 Red Hat, Inc.
  4. * GPL'd
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/mtd/mtd.h>
  10. #include <linux/mtd/map.h>
  11. #include <linux/mtd/cfi.h>
  12. #include <linux/mtd/gen_probe.h>
  13. static struct mtd_info *check_cmd_set(struct map_info *, int);
  14. static struct cfi_private *genprobe_ident_chips(struct map_info *map,
  15. struct chip_probe *cp);
  16. static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
  17. struct cfi_private *cfi);
  18. struct mtd_info *mtd_do_chip_probe(struct map_info *map, struct chip_probe *cp)
  19. {
  20. struct mtd_info *mtd;
  21. struct cfi_private *cfi;
  22. /* First probe the map to see if we have CFI stuff there. */
  23. cfi = genprobe_ident_chips(map, cp);
  24. if (!cfi)
  25. return NULL;
  26. map->fldrv_priv = cfi;
  27. /* OK we liked it. Now find a driver for the command set it talks */
  28. mtd = check_cmd_set(map, 1); /* First the primary cmdset */
  29. if (!mtd)
  30. mtd = check_cmd_set(map, 0); /* Then the secondary */
  31. if (mtd) {
  32. if (mtd->size > map->size) {
  33. printk(KERN_WARNING "Reducing visibility of %ldKiB chip to %ldKiB\n",
  34. (unsigned long)mtd->size >> 10,
  35. (unsigned long)map->size >> 10);
  36. mtd->size = map->size;
  37. }
  38. return mtd;
  39. }
  40. printk(KERN_WARNING"gen_probe: No supported Vendor Command Set found\n");
  41. kfree(cfi->cfiq);
  42. kfree(cfi);
  43. map->fldrv_priv = NULL;
  44. return NULL;
  45. }
  46. EXPORT_SYMBOL(mtd_do_chip_probe);
  47. static struct cfi_private *genprobe_ident_chips(struct map_info *map, struct chip_probe *cp)
  48. {
  49. struct cfi_private cfi;
  50. struct cfi_private *retcfi;
  51. unsigned long *chip_map;
  52. int max_chips;
  53. int i, j;
  54. memset(&cfi, 0, sizeof(cfi));
  55. /* Call the probetype-specific code with all permutations of
  56. interleave and device type, etc. */
  57. if (!genprobe_new_chip(map, cp, &cfi)) {
  58. /* The probe didn't like it */
  59. pr_debug("%s: Found no %s device at location zero\n",
  60. cp->name, map->name);
  61. return NULL;
  62. }
  63. #if 0 /* Let the CFI probe routine do this sanity check. The Intel and AMD
  64. probe routines won't ever return a broken CFI structure anyway,
  65. because they make them up themselves.
  66. */
  67. if (cfi.cfiq->NumEraseRegions == 0) {
  68. printk(KERN_WARNING "Number of erase regions is zero\n");
  69. kfree(cfi.cfiq);
  70. return NULL;
  71. }
  72. #endif
  73. cfi.chipshift = cfi.cfiq->DevSize;
  74. if (cfi_interleave_is_1(&cfi)) {
  75. ;
  76. } else if (cfi_interleave_is_2(&cfi)) {
  77. cfi.chipshift++;
  78. } else if (cfi_interleave_is_4((&cfi))) {
  79. cfi.chipshift += 2;
  80. } else if (cfi_interleave_is_8(&cfi)) {
  81. cfi.chipshift += 3;
  82. } else {
  83. BUG();
  84. }
  85. cfi.numchips = 1;
  86. /*
  87. * Allocate memory for bitmap of valid chips.
  88. * Align bitmap storage size to full byte.
  89. */
  90. max_chips = map->size >> cfi.chipshift;
  91. if (!max_chips) {
  92. printk(KERN_WARNING "NOR chip too large to fit in mapping. Attempting to cope...\n");
  93. max_chips = 1;
  94. }
  95. chip_map = bitmap_zalloc(max_chips, GFP_KERNEL);
  96. if (!chip_map) {
  97. kfree(cfi.cfiq);
  98. return NULL;
  99. }
  100. set_bit(0, chip_map); /* Mark first chip valid */
  101. /*
  102. * Now probe for other chips, checking sensibly for aliases while
  103. * we're at it. The new_chip probe above should have let the first
  104. * chip in read mode.
  105. */
  106. for (i = 1; i < max_chips; i++) {
  107. cp->probe_chip(map, i << cfi.chipshift, chip_map, &cfi);
  108. }
  109. /*
  110. * Now allocate the space for the structures we need to return to
  111. * our caller, and copy the appropriate data into them.
  112. */
  113. retcfi = kmalloc(struct_size(retcfi, chips, cfi.numchips), GFP_KERNEL);
  114. if (!retcfi) {
  115. kfree(cfi.cfiq);
  116. bitmap_free(chip_map);
  117. return NULL;
  118. }
  119. memcpy(retcfi, &cfi, sizeof(cfi));
  120. memset(&retcfi->chips[0], 0, sizeof(struct flchip) * cfi.numchips);
  121. for (i = 0, j = 0; (j < cfi.numchips) && (i < max_chips); i++) {
  122. if(test_bit(i, chip_map)) {
  123. struct flchip *pchip = &retcfi->chips[j++];
  124. pchip->start = (i << cfi.chipshift);
  125. pchip->state = FL_READY;
  126. init_waitqueue_head(&pchip->wq);
  127. mutex_init(&pchip->mutex);
  128. }
  129. }
  130. bitmap_free(chip_map);
  131. return retcfi;
  132. }
  133. static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
  134. struct cfi_private *cfi)
  135. {
  136. int min_chips = (map_bankwidth(map)/4?:1); /* At most 4-bytes wide. */
  137. int max_chips = map_bankwidth(map); /* And minimum 1 */
  138. int nr_chips, type;
  139. for (nr_chips = max_chips; nr_chips >= min_chips; nr_chips >>= 1) {
  140. if (!cfi_interleave_supported(nr_chips))
  141. continue;
  142. cfi->interleave = nr_chips;
  143. /* Minimum device size. Don't look for one 8-bit device
  144. in a 16-bit bus, etc. */
  145. type = map_bankwidth(map) / nr_chips;
  146. for (; type <= CFI_DEVICETYPE_X32; type<<=1) {
  147. cfi->device_type = type;
  148. if (cp->probe_chip(map, 0, NULL, cfi))
  149. return 1;
  150. }
  151. }
  152. return 0;
  153. }
  154. typedef struct mtd_info *cfi_cmdset_fn_t(struct map_info *, int);
  155. extern cfi_cmdset_fn_t cfi_cmdset_0001;
  156. extern cfi_cmdset_fn_t cfi_cmdset_0002;
  157. extern cfi_cmdset_fn_t cfi_cmdset_0020;
  158. static inline struct mtd_info *cfi_cmdset_unknown(struct map_info *map,
  159. int primary)
  160. {
  161. struct cfi_private *cfi = map->fldrv_priv;
  162. __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
  163. #ifdef CONFIG_MODULES
  164. cfi_cmdset_fn_t *probe_function;
  165. char *probename;
  166. probename = kasprintf(GFP_KERNEL, "cfi_cmdset_%4.4X", type);
  167. if (!probename)
  168. return NULL;
  169. probe_function = __symbol_get(probename);
  170. if (!probe_function) {
  171. request_module("cfi_cmdset_%4.4X", type);
  172. probe_function = __symbol_get(probename);
  173. }
  174. kfree(probename);
  175. if (probe_function) {
  176. struct mtd_info *mtd;
  177. mtd = (*probe_function)(map, primary);
  178. /* If it was happy, it'll have increased its own use count */
  179. symbol_put_addr(probe_function);
  180. return mtd;
  181. }
  182. #endif
  183. printk(KERN_NOTICE "Support for command set %04X not present\n", type);
  184. return NULL;
  185. }
  186. static struct mtd_info *check_cmd_set(struct map_info *map, int primary)
  187. {
  188. struct cfi_private *cfi = map->fldrv_priv;
  189. __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
  190. if (type == P_ID_NONE || type == P_ID_RESERVED)
  191. return NULL;
  192. switch(type){
  193. /* We need these for the !CONFIG_MODULES case,
  194. because symbol_get() doesn't work there */
  195. #ifdef CONFIG_MTD_CFI_INTELEXT
  196. case P_ID_INTEL_EXT:
  197. case P_ID_INTEL_STD:
  198. case P_ID_INTEL_PERFORMANCE:
  199. return cfi_cmdset_0001(map, primary);
  200. #endif
  201. #ifdef CONFIG_MTD_CFI_AMDSTD
  202. case P_ID_AMD_STD:
  203. case P_ID_SST_OLD:
  204. case P_ID_WINBOND:
  205. return cfi_cmdset_0002(map, primary);
  206. #endif
  207. #ifdef CONFIG_MTD_CFI_STAA
  208. case P_ID_ST_ADV:
  209. return cfi_cmdset_0020(map, primary);
  210. #endif
  211. default:
  212. return cfi_cmdset_unknown(map, primary);
  213. }
  214. }
  215. MODULE_LICENSE("GPL");
  216. MODULE_AUTHOR("David Woodhouse <[email protected]>");
  217. MODULE_DESCRIPTION("Helper routines for flash chip probe code");