gen_probe.c 6.5 KB

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