blob: e2dc96441e05e0a6affa6c5fa2611fef0f545d9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines common to all CFI-type probes.
3 * (C) 2001-2003 Red Hat, Inc.
4 * GPL'd
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
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
15static struct mtd_info *check_cmd_set(struct map_info *, int);
16static struct cfi_private *genprobe_ident_chips(struct map_info *map,
17 struct chip_probe *cp);
18static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
19 struct cfi_private *cfi);
20
21struct 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 cfi = genprobe_ident_chips(map, cp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +000028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 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 Gleixner1f948b42005-11-07 11:15:37 +000038
David Woodhouse0f5ae3d2006-05-14 01:40:50 +010039 if (mtd) {
40 if (mtd->size > map->size) {
David Woodhousef6a673b2006-05-17 22:03:10 +010041 printk(KERN_WARNING "Reducing visibility of %ldKiB chip to %ldKiB\n",
David Woodhousec9ac5972006-11-30 08:17:38 +000042 (unsigned long)mtd->size >> 10,
David Woodhouse0f5ae3d2006-05-14 01:40:50 +010043 (unsigned long)map->size >> 10);
44 mtd->size = map->size;
45 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return mtd;
David Woodhouse0f5ae3d2006-05-14 01:40:50 +010047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 printk(KERN_WARNING"gen_probe: No supported Vendor Command Set found\n");
Thomas Gleixner1f948b42005-11-07 11:15:37 +000050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 kfree(cfi->cfiq);
52 kfree(cfi);
53 map->fldrv_priv = NULL;
54 return NULL;
55}
56EXPORT_SYMBOL(mtd_do_chip_probe);
57
58
59static 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 memset(&cfi, 0, sizeof(cfi));
68
Thomas Gleixner1f948b42005-11-07 11:15:37 +000069 /* Call the probetype-specific code with all permutations of
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 interleave and device type, etc. */
71 if (!genprobe_new_chip(map, cp, &cfi)) {
72 /* The probe didn't like it */
Jean Delvare3a3688b2008-07-10 13:37:08 +020073 pr_debug("%s: Found no %s device at location zero\n",
74 cp->name, map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return NULL;
Thomas Gleixner1f948b42005-11-07 11:15:37 +000076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
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 Gleixner1f948b42005-11-07 11:15:37 +0000101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 cfi.numchips = 1;
103
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000104 /*
105 * Allocate memory for bitmap of valid chips.
106 * Align bitmap storage size to full byte.
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 max_chips = map->size >> cfi.chipshift;
David Woodhouse0f5ae3d2006-05-14 01:40:50 +0100109 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
Julia Lawallc8872b02008-08-02 17:14:21 +0200114 mapsize = sizeof(long) * DIV_ROUND_UP(max_chips, BITS_PER_LONG);
Burman Yan95b93a02006-11-15 21:10:29 +0200115 chip_map = kzalloc(mapsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!chip_map) {
117 printk(KERN_WARNING "%s: kmalloc failed for CFI chip map\n", map->name);
118 kfree(cfi.cfiq);
119 return NULL;
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 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 for (i = 1; i < max_chips; i++) {
131 cp->probe_chip(map, i << cfi.chipshift, chip_map, &cfi);
132 }
133
134 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000135 * Now allocate the space for the structures we need to return to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * our caller, and copy the appropriate data into them.
137 */
138
139 retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL);
140
141 if (!retcfi) {
142 printk(KERN_WARNING "%s: kmalloc failed for CFI private structure\n", map->name);
143 kfree(cfi.cfiq);
144 kfree(chip_map);
145 return NULL;
146 }
147
148 memcpy(retcfi, &cfi, sizeof(cfi));
149 memset(&retcfi->chips[0], 0, sizeof(struct flchip) * cfi.numchips);
150
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++];
154
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;
160 }
161 }
162
163 kfree(chip_map);
164 return retcfi;
165}
166
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
169 struct cfi_private *cfi)
170{
171 int min_chips = (map_bankwidth(map)/4?:1); /* At most 4-bytes wide. */
172 int max_chips = map_bankwidth(map); /* And minimum 1 */
173 int nr_chips, type;
174
Russell King6170b432005-01-24 23:49:54 +0000175 for (nr_chips = max_chips; nr_chips >= min_chips; nr_chips >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 if (!cfi_interleave_supported(nr_chips))
178 continue;
179
180 cfi->interleave = nr_chips;
181
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;
185
186 for (; type <= CFI_DEVICETYPE_X32; type<<=1) {
187 cfi->device_type = type;
188
189 if (cp->probe_chip(map, 0, NULL, cfi))
190 return 1;
191 }
192 }
193 return 0;
194}
195
196typedef struct mtd_info *cfi_cmdset_fn_t(struct map_info *, int);
197
198extern cfi_cmdset_fn_t cfi_cmdset_0001;
199extern cfi_cmdset_fn_t cfi_cmdset_0002;
200extern cfi_cmdset_fn_t cfi_cmdset_0020;
201
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000202static inline struct mtd_info *cfi_cmdset_unknown(struct map_info *map,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int primary)
204{
205 struct cfi_private *cfi = map->fldrv_priv;
206 __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
David Woodhousea15bdee2006-05-08 22:35:05 +0100207#ifdef CONFIG_MODULES
David Woodhouse615191b2006-05-21 19:03:21 +0100208 char probename[16+sizeof(MODULE_SYMBOL_PREFIX)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 cfi_cmdset_fn_t *probe_function;
210
David Woodhouse615191b2006-05-21 19:03:21 +0100211 sprintf(probename, MODULE_SYMBOL_PREFIX "cfi_cmdset_%4.4X", type);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000212
David Woodhouse5fc3dbc2006-05-20 02:41:34 +0100213 probe_function = __symbol_get(probename);
David Woodhousea15bdee2006-05-08 22:35:05 +0100214 if (!probe_function) {
David Woodhouse615191b2006-05-21 19:03:21 +0100215 request_module(probename + sizeof(MODULE_SYMBOL_PREFIX) - 1);
David Woodhouse5fc3dbc2006-05-20 02:41:34 +0100216 probe_function = __symbol_get(probename);
David Woodhousea15bdee2006-05-08 22:35:05 +0100217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (probe_function) {
220 struct mtd_info *mtd;
221
222 mtd = (*probe_function)(map, primary);
223 /* If it was happy, it'll have increased its own use count */
David Woodhousea15bdee2006-05-08 22:35:05 +0100224 symbol_put_addr(probe_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return mtd;
226 }
227#endif
David Woodhousea15bdee2006-05-08 22:35:05 +0100228 printk(KERN_NOTICE "Support for command set %04X not present\n", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 return NULL;
231}
232
233static struct mtd_info *check_cmd_set(struct map_info *map, int primary)
234{
235 struct cfi_private *cfi = map->fldrv_priv;
236 __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (type == P_ID_NONE || type == P_ID_RESERVED)
239 return NULL;
240
241 switch(type){
David Woodhousea15bdee2006-05-08 22:35:05 +0100242 /* We need these for the !CONFIG_MODULES case,
243 because symbol_get() doesn't work there */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#ifdef CONFIG_MTD_CFI_INTELEXT
245 case 0x0001:
246 case 0x0003:
Nicolas Pitre638d9832005-08-06 05:40:46 +0100247 case 0x0200:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return cfi_cmdset_0001(map, primary);
249#endif
250#ifdef CONFIG_MTD_CFI_AMDSTD
251 case 0x0002:
252 return cfi_cmdset_0002(map, primary);
253#endif
254#ifdef CONFIG_MTD_CFI_STAA
255 case 0x0020:
256 return cfi_cmdset_0020(map, primary);
257#endif
David Woodhousea15bdee2006-05-08 22:35:05 +0100258 default:
259 return cfi_cmdset_unknown(map, primary);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263MODULE_LICENSE("GPL");
264MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
265MODULE_DESCRIPTION("Helper routines for flash chip probe code");