gen_probe.c 6.8 KB

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