1 // SPDX-License-Identifier: GPL-2.0
3 * Common Flash Interface support:
4 * Generic utility functions not dependent on command set
6 * Copyright (C) 2002 Red Hat
7 * Copyright (C) 2003 STMicroelectronics Limited
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
14 #include <asm/byteorder.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/mtd/xip.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/map.h>
23 #include <linux/mtd/cfi.h>
25 void cfi_udelay(int us)
28 msleep(DIV_ROUND_UP(us, 1000));
34 EXPORT_SYMBOL(cfi_udelay);
37 * Returns the command address according to the given geometry.
39 uint32_t cfi_build_cmd_addr(uint32_t cmd_ofs,
40 struct map_info *map, struct cfi_private *cfi)
42 unsigned bankwidth = map_bankwidth(map);
43 unsigned interleave = cfi_interleave(cfi);
44 unsigned type = cfi->device_type;
47 addr = (cmd_ofs * type) * interleave;
49 /* Modify the unlock address if we are in compatibility mode.
50 * For 16bit devices on 8 bit busses
51 * and 32bit devices on 16 bit busses
52 * set the low bit of the alternating bit sequence of the address.
54 if (((type * interleave) > bankwidth) && ((cmd_ofs & 0xff) == 0xaa))
55 addr |= (type >> 1)*interleave;
59 EXPORT_SYMBOL(cfi_build_cmd_addr);
62 * Transforms the CFI command for the given geometry (bus width & interleave).
63 * It looks too long to be inline, but in the common case it should almost all
66 map_word cfi_build_cmd(u_long cmd, struct map_info *map, struct cfi_private *cfi)
68 map_word val = { {0} };
69 int wordwidth, words_per_bus, chip_mode, chips_per_word;
73 /* We do it this way to give the compiler a fighting chance
74 of optimising away all the crap for 'bankwidth' larger than
75 an unsigned long, in the common case where that support is
77 if (map_bankwidth_is_large(map)) {
78 wordwidth = sizeof(unsigned long);
79 words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
81 wordwidth = map_bankwidth(map);
85 chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
86 chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
88 /* First, determine what the bit-pattern should be for a single
89 device, according to chip mode and endianness... */
96 onecmd = cpu_to_cfi16(map, cmd);
99 onecmd = cpu_to_cfi32(map, cmd);
103 /* Now replicate it across the size of an unsigned long, or
104 just to the bus width as appropriate */
105 switch (chips_per_word) {
107 #if BITS_PER_LONG >= 64
109 onecmd |= (onecmd << (chip_mode * 32));
113 onecmd |= (onecmd << (chip_mode * 16));
116 onecmd |= (onecmd << (chip_mode * 8));
122 /* And finally, for the multi-word case, replicate it
123 in all words in the structure */
124 for (i=0; i < words_per_bus; i++) {
130 EXPORT_SYMBOL(cfi_build_cmd);
132 unsigned long cfi_merge_status(map_word val, struct map_info *map,
133 struct cfi_private *cfi)
135 int wordwidth, words_per_bus, chip_mode, chips_per_word;
136 unsigned long onestat, res = 0;
139 /* We do it this way to give the compiler a fighting chance
140 of optimising away all the crap for 'bankwidth' larger than
141 an unsigned long, in the common case where that support is
143 if (map_bankwidth_is_large(map)) {
144 wordwidth = sizeof(unsigned long);
145 words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
147 wordwidth = map_bankwidth(map);
151 chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
152 chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
155 /* Or all status words together */
156 for (i=1; i < words_per_bus; i++) {
161 switch(chips_per_word) {
163 #if BITS_PER_LONG >= 64
165 res |= (onestat >> (chip_mode * 32));
169 res |= (onestat >> (chip_mode * 16));
172 res |= (onestat >> (chip_mode * 8));
178 /* Last, determine what the bit-pattern should be for a single
179 device, according to chip mode and endianness... */
184 res = cfi16_to_cpu(map, res);
187 res = cfi32_to_cpu(map, res);
193 EXPORT_SYMBOL(cfi_merge_status);
196 * Sends a CFI command to a bank of flash for the given geometry.
198 * Returns the offset in flash where the command was written.
199 * If prev_val is non-null, it will be set to the value at the command address,
200 * before the command was written.
202 uint32_t cfi_send_gen_cmd(u_char cmd, uint32_t cmd_addr, uint32_t base,
203 struct map_info *map, struct cfi_private *cfi,
204 int type, map_word *prev_val)
207 uint32_t addr = base + cfi_build_cmd_addr(cmd_addr, map, cfi);
208 val = cfi_build_cmd(cmd, map, cfi);
211 *prev_val = map_read(map, addr);
213 map_write(map, val, addr);
217 EXPORT_SYMBOL(cfi_send_gen_cmd);
219 int __xipram cfi_qry_present(struct map_info *map, __u32 base,
220 struct cfi_private *cfi)
222 int osf = cfi->interleave * cfi->device_type; /* scale factor */
226 qry[0] = cfi_build_cmd('Q', map, cfi);
227 qry[1] = cfi_build_cmd('R', map, cfi);
228 qry[2] = cfi_build_cmd('Y', map, cfi);
230 val[0] = map_read(map, base + osf*0x10);
231 val[1] = map_read(map, base + osf*0x11);
232 val[2] = map_read(map, base + osf*0x12);
234 if (!map_word_equal(map, qry[0], val[0]))
237 if (!map_word_equal(map, qry[1], val[1]))
240 if (!map_word_equal(map, qry[2], val[2]))
243 return 1; /* "QRY" found */
245 EXPORT_SYMBOL_GPL(cfi_qry_present);
247 int __xipram cfi_qry_mode_on(uint32_t base, struct map_info *map,
248 struct cfi_private *cfi)
250 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
251 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
252 if (cfi_qry_present(map, base, cfi))
254 /* QRY not found probably we deal with some odd CFI chips */
255 /* Some revisions of some old Intel chips? */
256 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
257 cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
258 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
259 if (cfi_qry_present(map, base, cfi))
262 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
263 cfi_send_gen_cmd(0x98, 0x555, base, map, cfi, cfi->device_type, NULL);
264 if (cfi_qry_present(map, base, cfi))
266 /* some old SST chips, e.g. 39VF160x/39VF320x */
267 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
268 cfi_send_gen_cmd(0xAA, 0x5555, base, map, cfi, cfi->device_type, NULL);
269 cfi_send_gen_cmd(0x55, 0x2AAA, base, map, cfi, cfi->device_type, NULL);
270 cfi_send_gen_cmd(0x98, 0x5555, base, map, cfi, cfi->device_type, NULL);
271 if (cfi_qry_present(map, base, cfi))
274 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
275 cfi_send_gen_cmd(0xAA, 0x555, base, map, cfi, cfi->device_type, NULL);
276 cfi_send_gen_cmd(0x55, 0x2AA, base, map, cfi, cfi->device_type, NULL);
277 cfi_send_gen_cmd(0x98, 0x555, base, map, cfi, cfi->device_type, NULL);
278 if (cfi_qry_present(map, base, cfi))
283 EXPORT_SYMBOL_GPL(cfi_qry_mode_on);
285 void __xipram cfi_qry_mode_off(uint32_t base, struct map_info *map,
286 struct cfi_private *cfi)
288 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
289 cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
290 /* M29W128G flashes require an additional reset command
291 when exit qry mode */
292 if ((cfi->mfr == CFI_MFR_ST) && (cfi->id == 0x227E || cfi->id == 0x7E))
293 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
295 EXPORT_SYMBOL_GPL(cfi_qry_mode_off);
297 struct cfi_extquery *
298 __xipram cfi_read_pri(struct map_info *map, __u16 adr, __u16 size, const char* name)
300 struct cfi_private *cfi = map->fldrv_priv;
301 __u32 base = 0; // cfi->chips[0].start;
302 int ofs_factor = cfi->interleave * cfi->device_type;
304 struct cfi_extquery *extp = NULL;
309 printk(KERN_INFO "%s Extended Query Table at 0x%4.4X\n", name, adr);
311 extp = kmalloc(size, GFP_KERNEL);
315 #ifdef CONFIG_MTD_XIP
319 /* Switch it into Query Mode */
320 cfi_qry_mode_on(base, map, cfi);
321 /* Read in the Extended Query Table */
322 for (i=0; i<size; i++) {
323 ((unsigned char *)extp)[i] =
324 cfi_read_query(map, base+((adr+i)*ofs_factor));
327 /* Make sure it returns to read mode */
328 cfi_qry_mode_off(base, map, cfi);
330 #ifdef CONFIG_MTD_XIP
331 (void) map_read(map, base);
339 EXPORT_SYMBOL(cfi_read_pri);
341 void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup *fixups)
343 struct map_info *map = mtd->priv;
344 struct cfi_private *cfi = map->fldrv_priv;
347 for (f=fixups; f->fixup; f++) {
348 if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi->mfr)) &&
349 ((f->id == CFI_ID_ANY) || (f->id == cfi->id))) {
355 EXPORT_SYMBOL(cfi_fixup);
357 int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
358 loff_t ofs, size_t len, void *thunk)
360 struct map_info *map = mtd->priv;
361 struct cfi_private *cfi = map->fldrv_priv;
363 int chipnum, ret = 0;
365 struct mtd_erase_region_info *regions = mtd->eraseregions;
367 /* Check that both start and end of the requested erase are
368 * aligned with the erasesize at the appropriate addresses.
373 /* Skip all erase regions which are ended before the start of
374 the requested erase. Actually, to save on the calculations,
375 we skip to the first erase region which starts after the
376 start of the requested erase, and then go back one.
379 while (i < mtd->numeraseregions && ofs >= regions[i].offset)
383 /* OK, now i is pointing at the erase region in which this
384 erase request starts. Check the start of the requested
385 erase range is aligned with the erase size which is in
389 if (ofs & (regions[i].erasesize-1))
392 /* Remember the erase region we start on */
395 /* Next, check that the end of the requested erase is aligned
396 * with the erase region at that address.
399 while (i<mtd->numeraseregions && (ofs + len) >= regions[i].offset)
402 /* As before, drop back one to point at the region in which
403 the address actually falls
407 if ((ofs + len) & (regions[i].erasesize-1))
410 chipnum = ofs >> cfi->chipshift;
411 adr = ofs - (chipnum << cfi->chipshift);
416 int size = regions[i].erasesize;
418 ret = (*frob)(map, &cfi->chips[chipnum], adr, size, thunk);
427 if (ofs == regions[i].offset + size * regions[i].numblocks)
430 if (adr >> cfi->chipshift) {
434 if (chipnum >= cfi->numchips)
442 EXPORT_SYMBOL(cfi_varsize_frob);
444 MODULE_LICENSE("GPL");