Branch data Line data Source code
1 : : /*
2 : : * Routines common to all CFI-type probes.
3 : : * (C) 2001-2003 Red Hat, Inc.
4 : : * GPL'd
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 : 0 : struct mtd_info *mtd_do_chip_probe(struct map_info *map, struct chip_probe *cp)
22 : : {
23 : : struct mtd_info *mtd = NULL;
24 : : struct cfi_private *cfi;
25 : :
26 : : /* First probe the map to see if we have CFI stuff there. */
27 : 0 : cfi = genprobe_ident_chips(map, cp);
28 : :
29 [ # # ]: 0 : if (!cfi)
30 : : return NULL;
31 : :
32 : 0 : map->fldrv_priv = cfi;
33 : : /* OK we liked it. Now find a driver for the command set it talks */
34 : :
35 : 0 : mtd = check_cmd_set(map, 1); /* First the primary cmdset */
36 [ # # ]: 0 : if (!mtd)
37 : 0 : mtd = check_cmd_set(map, 0); /* Then the secondary */
38 : :
39 [ # # ]: 0 : if (mtd) {
40 [ # # ]: 0 : if (mtd->size > map->size) {
41 : 0 : printk(KERN_WARNING "Reducing visibility of %ldKiB chip to %ldKiB\n",
42 : 0 : (unsigned long)mtd->size >> 10,
43 : : (unsigned long)map->size >> 10);
44 : 0 : mtd->size = map->size;
45 : : }
46 : 0 : return mtd;
47 : : }
48 : :
49 : 0 : printk(KERN_WARNING"gen_probe: No supported Vendor Command Set found\n");
50 : :
51 : 0 : kfree(cfi->cfiq);
52 : 0 : kfree(cfi);
53 : 0 : map->fldrv_priv = NULL;
54 : 0 : return NULL;
55 : : }
56 : : EXPORT_SYMBOL(mtd_do_chip_probe);
57 : :
58 : :
59 : 0 : 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;
64 : : int i, j, mapsize;
65 : : int max_chips;
66 : :
67 : 0 : memset(&cfi, 0, sizeof(cfi));
68 : :
69 : : /* Call the probetype-specific code with all permutations of
70 : : interleave and device type, etc. */
71 [ # # ]: 0 : 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",
74 : : cp->name, map->name);
75 : : return NULL;
76 : : }
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 : 0 : cfi.chipshift = cfi.cfiq->DevSize;
89 : :
90 [ # # ]: 0 : if (cfi_interleave_is_1(&cfi)) {
91 : : ;
92 [ # # ]: 0 : } else if (cfi_interleave_is_2(&cfi)) {
93 : 0 : 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 : 0 : BUG();
100 : : }
101 : :
102 : 0 : cfi.numchips = 1;
103 : :
104 : : /*
105 : : * Allocate memory for bitmap of valid chips.
106 : : * Align bitmap storage size to full byte.
107 : : */
108 : 0 : max_chips = map->size >> cfi.chipshift;
109 [ # # ]: 0 : if (!max_chips) {
110 : 0 : printk(KERN_WARNING "NOR chip too large to fit in mapping. Attempting to cope...\n");
111 : : max_chips = 1;
112 : : }
113 : :
114 : 0 : mapsize = sizeof(long) * DIV_ROUND_UP(max_chips, BITS_PER_LONG);
115 : : chip_map = kzalloc(mapsize, GFP_KERNEL);
116 [ # # ]: 0 : if (!chip_map) {
117 : 0 : printk(KERN_WARNING "%s: kmalloc failed for CFI chip map\n", map->name);
118 : 0 : kfree(cfi.cfiq);
119 : 0 : return NULL;
120 : : }
121 : :
122 : 0 : set_bit(0, chip_map); /* Mark first chip valid */
123 : :
124 : : /*
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
127 : : * chip in read mode.
128 : : */
129 : :
130 [ # # ]: 0 : for (i = 1; i < max_chips; i++) {
131 : 0 : cp->probe_chip(map, i << cfi.chipshift, chip_map, &cfi);
132 : : }
133 : :
134 : : /*
135 : : * Now allocate the space for the structures we need to return to
136 : : * our caller, and copy the appropriate data into them.
137 : : */
138 : :
139 : 0 : retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL);
140 : :
141 [ # # ]: 0 : if (!retcfi) {
142 : 0 : printk(KERN_WARNING "%s: kmalloc failed for CFI private structure\n", map->name);
143 : 0 : kfree(cfi.cfiq);
144 : 0 : kfree(chip_map);
145 : 0 : return NULL;
146 : : }
147 : :
148 : 0 : memcpy(retcfi, &cfi, sizeof(cfi));
149 [ # # ]: 0 : memset(&retcfi->chips[0], 0, sizeof(struct flchip) * cfi.numchips);
150 : :
151 [ # # ][ # # ]: 0 : for (i = 0, j = 0; (j < cfi.numchips) && (i < max_chips); i++) {
152 [ # # ]: 0 : if(test_bit(i, chip_map)) {
153 : 0 : struct flchip *pchip = &retcfi->chips[j++];
154 : :
155 : 0 : pchip->start = (i << cfi.chipshift);
156 : 0 : pchip->state = FL_READY;
157 : 0 : init_waitqueue_head(&pchip->wq);
158 : 0 : mutex_init(&pchip->mutex);
159 : : }
160 : : }
161 : :
162 : 0 : kfree(chip_map);
163 : 0 : return retcfi;
164 : : }
165 : :
166 : :
167 : 0 : static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
168 : : struct cfi_private *cfi)
169 : : {
170 [ # # ]: 0 : int min_chips = (map_bankwidth(map)/4?:1); /* At most 4-bytes wide. */
171 : : int max_chips = map_bankwidth(map); /* And minimum 1 */
172 : : int nr_chips, type;
173 : :
174 [ # # ]: 0 : for (nr_chips = max_chips; nr_chips >= min_chips; nr_chips >>= 1) {
175 : :
176 [ # # ]: 0 : if (!cfi_interleave_supported(nr_chips))
177 : 0 : continue;
178 : :
179 : 0 : cfi->interleave = nr_chips;
180 : :
181 : : /* Minimum device size. Don't look for one 8-bit device
182 : : in a 16-bit bus, etc. */
183 : 0 : type = map_bankwidth(map) / nr_chips;
184 : :
185 [ # # ]: 0 : for (; type <= CFI_DEVICETYPE_X32; type<<=1) {
186 : 0 : cfi->device_type = type;
187 : :
188 [ # # ]: 0 : if (cp->probe_chip(map, 0, NULL, cfi))
189 : : return 1;
190 : : }
191 : : }
192 : : return 0;
193 : : }
194 : :
195 : : typedef struct mtd_info *cfi_cmdset_fn_t(struct map_info *, int);
196 : :
197 : : extern cfi_cmdset_fn_t cfi_cmdset_0001;
198 : : extern cfi_cmdset_fn_t cfi_cmdset_0002;
199 : : extern cfi_cmdset_fn_t cfi_cmdset_0020;
200 : :
201 : : static inline struct mtd_info *cfi_cmdset_unknown(struct map_info *map,
202 : : int primary)
203 : : {
204 : : struct cfi_private *cfi = map->fldrv_priv;
205 [ # # ]: 0 : __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
206 : : #ifdef CONFIG_MODULES
207 : : char probename[sizeof(VMLINUX_SYMBOL_STR(cfi_cmdset_%4.4X))];
208 : : cfi_cmdset_fn_t *probe_function;
209 : :
210 : 0 : sprintf(probename, VMLINUX_SYMBOL_STR(cfi_cmdset_%4.4X), type);
211 : :
212 : 0 : probe_function = __symbol_get(probename);
213 [ # # ]: 0 : if (!probe_function) {
214 : 0 : request_module("cfi_cmdset_%4.4X", type);
215 : 0 : probe_function = __symbol_get(probename);
216 : : }
217 : :
218 [ # # ]: 0 : if (probe_function) {
219 : : struct mtd_info *mtd;
220 : :
221 : 0 : mtd = (*probe_function)(map, primary);
222 : : /* If it was happy, it'll have increased its own use count */
223 : 0 : symbol_put_addr(probe_function);
224 : : return mtd;
225 : : }
226 : : #endif
227 : 0 : printk(KERN_NOTICE "Support for command set %04X not present\n", type);
228 : :
229 : : return NULL;
230 : : }
231 : :
232 : 0 : static struct mtd_info *check_cmd_set(struct map_info *map, int primary)
233 : : {
234 : 0 : struct cfi_private *cfi = map->fldrv_priv;
235 [ # # ]: 0 : __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
236 : :
237 [ # # ]: 0 : if (type == P_ID_NONE || type == P_ID_RESERVED)
238 : : return NULL;
239 : :
240 [ # # ]: 0 : switch(type){
241 : : /* We need these for the !CONFIG_MODULES case,
242 : : because symbol_get() doesn't work there */
243 : : #ifdef CONFIG_MTD_CFI_INTELEXT
244 : : case P_ID_INTEL_EXT:
245 : : case P_ID_INTEL_STD:
246 : : case P_ID_INTEL_PERFORMANCE:
247 : 0 : return cfi_cmdset_0001(map, primary);
248 : : #endif
249 : : #ifdef CONFIG_MTD_CFI_AMDSTD
250 : : case P_ID_AMD_STD:
251 : : case P_ID_SST_OLD:
252 : : case P_ID_WINBOND:
253 : : return cfi_cmdset_0002(map, primary);
254 : : #endif
255 : : #ifdef CONFIG_MTD_CFI_STAA
256 : : case P_ID_ST_ADV:
257 : : return cfi_cmdset_0020(map, primary);
258 : : #endif
259 : : default:
260 : 0 : return cfi_cmdset_unknown(map, primary);
261 : : }
262 : : }
263 : :
264 : : MODULE_LICENSE("GPL");
265 : : MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
266 : : MODULE_DESCRIPTION("Helper routines for flash chip probe code");
|