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