RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mtd / chips / cfi_util.c
blobe503b2ca894de98687490d8b1806739dc9d61021
1 /*
2 * Common Flash Interface support:
3 * Generic utility functions not dependant on command set
5 * Copyright (C) 2002 Red Hat
6 * Copyright (C) 2003 STMicroelectronics Limited
8 * This code is covered by the GPL.
9 */
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <asm/io.h>
15 #include <asm/byteorder.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/mtd/xip.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/map.h>
24 #include <linux/mtd/cfi.h>
26 int __xipram cfi_qry_present(struct map_info *map, __u32 base,
27 struct cfi_private *cfi)
29 int osf = cfi->interleave * cfi->device_type; /* scale factor */
30 map_word val[3];
31 map_word qry[3];
33 qry[0] = cfi_build_cmd('Q', map, cfi);
34 qry[1] = cfi_build_cmd('R', map, cfi);
35 qry[2] = cfi_build_cmd('Y', map, cfi);
37 val[0] = map_read(map, base + osf*0x10);
38 val[1] = map_read(map, base + osf*0x11);
39 val[2] = map_read(map, base + osf*0x12);
41 if (!map_word_equal(map, qry[0], val[0]))
42 return 0;
44 if (!map_word_equal(map, qry[1], val[1]))
45 return 0;
47 if (!map_word_equal(map, qry[2], val[2]))
48 return 0;
50 return 1; /* "QRY" found */
52 EXPORT_SYMBOL_GPL(cfi_qry_present);
54 int __xipram cfi_qry_mode_on(uint32_t base, struct map_info *map,
55 struct cfi_private *cfi)
57 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
58 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
59 if (cfi_qry_present(map, base, cfi))
60 return 1;
61 /* QRY not found probably we deal with some odd CFI chips */
62 /* Some revisions of some old Intel chips? */
63 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
64 cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
65 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
66 if (cfi_qry_present(map, base, cfi))
67 return 1;
68 /* ST M29DW chips */
69 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
70 cfi_send_gen_cmd(0x98, 0x555, base, map, cfi, cfi->device_type, NULL);
71 if (cfi_qry_present(map, base, cfi))
72 return 1;
73 /* some old SST chips, e.g. 39VF160x/39VF320x */
74 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
75 cfi_send_gen_cmd(0xAA, 0x5555, base, map, cfi, cfi->device_type, NULL);
76 cfi_send_gen_cmd(0x55, 0x2AAA, base, map, cfi, cfi->device_type, NULL);
77 cfi_send_gen_cmd(0x98, 0x5555, base, map, cfi, cfi->device_type, NULL);
78 if (cfi_qry_present(map, base, cfi))
79 return 1;
80 /* QRY not found */
81 return 0;
83 EXPORT_SYMBOL_GPL(cfi_qry_mode_on);
85 void __xipram cfi_qry_mode_off(uint32_t base, struct map_info *map,
86 struct cfi_private *cfi)
88 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
89 cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
90 /* M29W128G flashes require an additional reset command
91 when exit qry mode */
92 if ((cfi->mfr == CFI_MFR_ST) && (cfi->id == 0x227E || cfi->id == 0x7E))
93 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
95 EXPORT_SYMBOL_GPL(cfi_qry_mode_off);
97 struct cfi_extquery *
98 __xipram cfi_read_pri(struct map_info *map, __u16 adr, __u16 size, const char* name)
100 struct cfi_private *cfi = map->fldrv_priv;
101 __u32 base = 0; // cfi->chips[0].start;
102 int ofs_factor = cfi->interleave * cfi->device_type;
103 int i;
104 struct cfi_extquery *extp = NULL;
106 if (!adr)
107 goto out;
109 printk(KERN_INFO "%s Extended Query Table at 0x%4.4X\n", name, adr);
111 extp = kmalloc(size, GFP_KERNEL);
112 if (!extp) {
113 printk(KERN_ERR "Failed to allocate memory\n");
114 goto out;
117 #ifdef CONFIG_MTD_XIP
118 local_irq_disable();
119 #endif
121 /* Switch it into Query Mode */
122 cfi_qry_mode_on(base, map, cfi);
123 /* Read in the Extended Query Table */
124 for (i=0; i<size; i++) {
125 ((unsigned char *)extp)[i] =
126 cfi_read_query(map, base+((adr+i)*ofs_factor));
129 /* Make sure it returns to read mode */
130 cfi_qry_mode_off(base, map, cfi);
132 #ifdef CONFIG_MTD_XIP
133 (void) map_read(map, base);
134 xip_iprefetch();
135 local_irq_enable();
136 #endif
138 out: return extp;
141 EXPORT_SYMBOL(cfi_read_pri);
143 void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup *fixups)
145 struct map_info *map = mtd->priv;
146 struct cfi_private *cfi = map->fldrv_priv;
147 struct cfi_fixup *f;
149 for (f=fixups; f->fixup; f++) {
150 if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi->mfr)) &&
151 ((f->id == CFI_ID_ANY) || (f->id == cfi->id))) {
152 f->fixup(mtd, f->param);
157 EXPORT_SYMBOL(cfi_fixup);
159 int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
160 loff_t ofs, size_t len, void *thunk)
162 struct map_info *map = mtd->priv;
163 struct cfi_private *cfi = map->fldrv_priv;
164 unsigned long adr;
165 int chipnum, ret = 0;
166 int i, first;
167 struct mtd_erase_region_info *regions = mtd->eraseregions;
169 if (ofs > mtd->size)
170 return -EINVAL;
172 if ((len + ofs) > mtd->size)
173 return -EINVAL;
175 /* Check that both start and end of the requested erase are
176 * aligned with the erasesize at the appropriate addresses.
179 i = 0;
181 /* Skip all erase regions which are ended before the start of
182 the requested erase. Actually, to save on the calculations,
183 we skip to the first erase region which starts after the
184 start of the requested erase, and then go back one.
187 while (i < mtd->numeraseregions && ofs >= regions[i].offset)
188 i++;
189 i--;
191 /* OK, now i is pointing at the erase region in which this
192 erase request starts. Check the start of the requested
193 erase range is aligned with the erase size which is in
194 effect here.
197 if (ofs & (regions[i].erasesize-1))
198 return -EINVAL;
200 /* Remember the erase region we start on */
201 first = i;
203 /* Next, check that the end of the requested erase is aligned
204 * with the erase region at that address.
207 while (i<mtd->numeraseregions && (ofs + len) >= regions[i].offset)
208 i++;
210 /* As before, drop back one to point at the region in which
211 the address actually falls
213 i--;
215 if ((ofs + len) & (regions[i].erasesize-1))
216 return -EINVAL;
218 chipnum = ofs >> cfi->chipshift;
219 adr = ofs - (chipnum << cfi->chipshift);
221 i=first;
223 while(len) {
224 int size = regions[i].erasesize;
226 ret = (*frob)(map, &cfi->chips[chipnum], adr, size, thunk);
228 if (ret)
229 return ret;
231 adr += size;
232 ofs += size;
233 len -= size;
235 if (ofs == regions[i].offset + size * regions[i].numblocks)
236 i++;
238 if (adr >> cfi->chipshift) {
239 adr = 0;
240 chipnum++;
242 if (chipnum >= cfi->numchips)
243 break;
247 return 0;
250 EXPORT_SYMBOL(cfi_varsize_frob);
252 MODULE_LICENSE("GPL");