2 * Routines common to all CFI-type probes.
3 * (C) 2001-2003 Red Hat, Inc.
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>
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
);
21 struct mtd_info
*mtd_do_chip_probe(struct map_info
*map
, struct chip_probe
*cp
)
23 struct mtd_info
*mtd
= NULL
;
24 struct cfi_private
*cfi
;
26 /* First probe the map to see if we have CFI stuff there. */
27 cfi
= genprobe_ident_chips(map
, cp
);
32 map
->fldrv_priv
= cfi
;
33 /* OK we liked it. Now find a driver for the command set it talks */
35 mtd
= check_cmd_set(map
, 1); /* First the primary cmdset */
37 mtd
= check_cmd_set(map
, 0); /* Then the secondary */
40 if (mtd
->size
> map
->size
) {
41 printk(KERN_WARNING
"Reducing visibility of %ldKiB chip to %ldKiB\n",
42 (unsigned long)mtd
->size
>> 10,
43 (unsigned long)map
->size
>> 10);
44 mtd
->size
= map
->size
;
49 printk(KERN_WARNING
"gen_probe: No supported Vendor Command Set found\n");
53 map
->fldrv_priv
= NULL
;
56 EXPORT_SYMBOL(mtd_do_chip_probe
);
59 static struct cfi_private
*genprobe_ident_chips(struct map_info
*map
, struct chip_probe
*cp
)
61 struct cfi_private cfi
;
62 struct cfi_private
*retcfi
;
63 unsigned long *chip_map
;
67 memset(&cfi
, 0, sizeof(cfi
));
69 /* Call the probetype-specific code with all permutations of
70 interleave and device type, etc. */
71 if (!genprobe_new_chip(map
, cp
, &cfi
)) {
72 /* The probe didn't like it */
73 pr_debug("%s: Found no %s device at location zero\n",
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.
82 if (cfi
.cfiq
->NumEraseRegions
== 0) {
83 printk(KERN_WARNING
"Number of erase regions is zero\n");
88 cfi
.chipshift
= cfi
.cfiq
->DevSize
;
90 if (cfi_interleave_is_1(&cfi
)) {
92 } else if (cfi_interleave_is_2(&cfi
)) {
94 } else if (cfi_interleave_is_4((&cfi
))) {
96 } else if (cfi_interleave_is_8(&cfi
)) {
105 * Allocate memory for bitmap of valid chips.
106 * Align bitmap storage size to full byte.
108 max_chips
= map
->size
>> cfi
.chipshift
;
110 printk(KERN_WARNING
"NOR chip too large to fit in mapping. Attempting to cope...\n");
114 mapsize
= sizeof(long) * DIV_ROUND_UP(max_chips
, BITS_PER_LONG
);
115 chip_map
= kzalloc(mapsize
, GFP_KERNEL
);
117 printk(KERN_WARNING
"%s: kmalloc failed for CFI chip map\n", map
->name
);
122 set_bit(0, chip_map
); /* Mark first chip valid */
125 * Now probe for other chips, checking sensibly for aliases while
126 * we're at it. The new_chip probe above should have let the first
130 for (i
= 1; i
< max_chips
; i
++) {
131 cp
->probe_chip(map
, i
<< cfi
.chipshift
, chip_map
, &cfi
);
135 * Now allocate the space for the structures we need to return to
136 * our caller, and copy the appropriate data into them.
139 retcfi
= kmalloc(sizeof(struct cfi_private
) + cfi
.numchips
* sizeof(struct flchip
), GFP_KERNEL
);
142 printk(KERN_WARNING
"%s: kmalloc failed for CFI private structure\n", map
->name
);
148 memcpy(retcfi
, &cfi
, sizeof(cfi
));
149 memset(&retcfi
->chips
[0], 0, sizeof(struct flchip
) * cfi
.numchips
);
151 for (i
= 0, j
= 0; (j
< cfi
.numchips
) && (i
< max_chips
); i
++) {
152 if(test_bit(i
, chip_map
)) {
153 struct flchip
*pchip
= &retcfi
->chips
[j
++];
155 pchip
->start
= (i
<< cfi
.chipshift
);
156 pchip
->state
= FL_READY
;
157 init_waitqueue_head(&pchip
->wq
);
158 spin_lock_init(&pchip
->_spinlock
);
159 pchip
->mutex
= &pchip
->_spinlock
;
168 static int genprobe_new_chip(struct map_info
*map
, struct chip_probe
*cp
,
169 struct cfi_private
*cfi
)
171 int min_chips
= (map_bankwidth(map
)/4?:1); /* At most 4-bytes wide. */
172 int max_chips
= map_bankwidth(map
); /* And minimum 1 */
175 for (nr_chips
= max_chips
; nr_chips
>= min_chips
; nr_chips
>>= 1) {
177 if (!cfi_interleave_supported(nr_chips
))
180 cfi
->interleave
= nr_chips
;
182 /* Minimum device size. Don't look for one 8-bit device
183 in a 16-bit bus, etc. */
184 type
= map_bankwidth(map
) / nr_chips
;
186 for (; type
<= CFI_DEVICETYPE_X32
; type
<<=1) {
187 cfi
->device_type
= type
;
189 if (cp
->probe_chip(map
, 0, NULL
, cfi
))
196 typedef struct mtd_info
*cfi_cmdset_fn_t(struct map_info
*, int);
198 extern cfi_cmdset_fn_t cfi_cmdset_0001
;
199 extern cfi_cmdset_fn_t cfi_cmdset_0002
;
200 extern cfi_cmdset_fn_t cfi_cmdset_0020
;
202 static inline struct mtd_info
*cfi_cmdset_unknown(struct map_info
*map
,
205 struct cfi_private
*cfi
= map
->fldrv_priv
;
206 __u16 type
= primary
?cfi
->cfiq
->P_ID
:cfi
->cfiq
->A_ID
;
207 #ifdef CONFIG_MODULES
208 char probename
[16+sizeof(MODULE_SYMBOL_PREFIX
)];
209 cfi_cmdset_fn_t
*probe_function
;
211 sprintf(probename
, MODULE_SYMBOL_PREFIX
"cfi_cmdset_%4.4X", type
);
213 probe_function
= __symbol_get(probename
);
214 if (!probe_function
) {
215 request_module(probename
+ sizeof(MODULE_SYMBOL_PREFIX
) - 1);
216 probe_function
= __symbol_get(probename
);
219 if (probe_function
) {
220 struct mtd_info
*mtd
;
222 mtd
= (*probe_function
)(map
, primary
);
223 /* If it was happy, it'll have increased its own use count */
224 symbol_put_addr(probe_function
);
228 printk(KERN_NOTICE
"Support for command set %04X not present\n", type
);
233 static struct mtd_info
*check_cmd_set(struct map_info
*map
, int primary
)
235 struct cfi_private
*cfi
= map
->fldrv_priv
;
236 __u16 type
= primary
?cfi
->cfiq
->P_ID
:cfi
->cfiq
->A_ID
;
238 if (type
== P_ID_NONE
|| type
== P_ID_RESERVED
)
242 /* We need these for the !CONFIG_MODULES case,
243 because symbol_get() doesn't work there */
244 #ifdef CONFIG_MTD_CFI_INTELEXT
248 return cfi_cmdset_0001(map
, primary
);
250 #ifdef CONFIG_MTD_CFI_AMDSTD
252 return cfi_cmdset_0002(map
, primary
);
254 #ifdef CONFIG_MTD_CFI_STAA
256 return cfi_cmdset_0020(map
, primary
);
259 return cfi_cmdset_unknown(map
, primary
);
263 MODULE_LICENSE("GPL");
264 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
265 MODULE_DESCRIPTION("Helper routines for flash chip probe code");