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