blob: 0ee457018016083743ca772a38d77325aa6911ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common Flash Interface support:
3 * Generic utility functions not dependant on command set
4 *
5 * Copyright (C) 2002 Red Hat
6 * Copyright (C) 2003 STMicroelectronics Limited
7 *
8 * This code is covered by the GPL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/io.h>
15#include <asm/byteorder.h>
16
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>
25#include <linux/mtd/compatmac.h>
26
27struct cfi_extquery *
28__xipram cfi_read_pri(struct map_info *map, __u16 adr, __u16 size, const char* name)
29{
30 struct cfi_private *cfi = map->fldrv_priv;
31 __u32 base = 0; // cfi->chips[0].start;
32 int ofs_factor = cfi->interleave * cfi->device_type;
33 int i;
34 struct cfi_extquery *extp = NULL;
35
36 printk(" %s Extended Query Table at 0x%4.4X\n", name, adr);
37 if (!adr)
38 goto out;
39
40 extp = kmalloc(size, GFP_KERNEL);
41 if (!extp) {
42 printk(KERN_ERR "Failed to allocate memory\n");
43 goto out;
44 }
45
46#ifdef CONFIG_MTD_XIP
47 local_irq_disable();
48#endif
49
50 /* Switch it into Query Mode */
51 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
52
53 /* Read in the Extended Query Table */
54 for (i=0; i<size; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +000055 ((unsigned char *)extp)[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 cfi_read_query(map, base+((adr+i)*ofs_factor));
57 }
58
59 /* Make sure it returns to read mode */
60 cfi_send_gen_cmd(0xf0, 0, base, map, cfi, cfi->device_type, NULL);
61 cfi_send_gen_cmd(0xff, 0, base, map, cfi, cfi->device_type, NULL);
62
63#ifdef CONFIG_MTD_XIP
64 (void) map_read(map, base);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +020065 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 local_irq_enable();
67#endif
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 out: return extp;
70}
71
72EXPORT_SYMBOL(cfi_read_pri);
73
74void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup *fixups)
75{
76 struct map_info *map = mtd->priv;
77 struct cfi_private *cfi = map->fldrv_priv;
78 struct cfi_fixup *f;
79
80 for (f=fixups; f->fixup; f++) {
81 if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi->mfr)) &&
82 ((f->id == CFI_ID_ANY) || (f->id == cfi->id))) {
83 f->fixup(mtd, f->param);
84 }
85 }
86}
87
88EXPORT_SYMBOL(cfi_fixup);
89
90int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
91 loff_t ofs, size_t len, void *thunk)
92{
93 struct map_info *map = mtd->priv;
94 struct cfi_private *cfi = map->fldrv_priv;
95 unsigned long adr;
96 int chipnum, ret = 0;
97 int i, first;
98 struct mtd_erase_region_info *regions = mtd->eraseregions;
99
100 if (ofs > mtd->size)
101 return -EINVAL;
102
103 if ((len + ofs) > mtd->size)
104 return -EINVAL;
105
106 /* Check that both start and end of the requested erase are
107 * aligned with the erasesize at the appropriate addresses.
108 */
109
110 i = 0;
111
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000112 /* Skip all erase regions which are ended before the start of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 the requested erase. Actually, to save on the calculations,
114 we skip to the first erase region which starts after the
115 start of the requested erase, and then go back one.
116 */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 while (i < mtd->numeraseregions && ofs >= regions[i].offset)
119 i++;
120 i--;
121
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000122 /* OK, now i is pointing at the erase region in which this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 erase request starts. Check the start of the requested
124 erase range is aligned with the erase size which is in
125 effect here.
126 */
127
128 if (ofs & (regions[i].erasesize-1))
129 return -EINVAL;
130
131 /* Remember the erase region we start on */
132 first = i;
133
134 /* Next, check that the end of the requested erase is aligned
135 * with the erase region at that address.
136 */
137
138 while (i<mtd->numeraseregions && (ofs + len) >= regions[i].offset)
139 i++;
140
141 /* As before, drop back one to point at the region in which
142 the address actually falls
143 */
144 i--;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if ((ofs + len) & (regions[i].erasesize-1))
147 return -EINVAL;
148
149 chipnum = ofs >> cfi->chipshift;
150 adr = ofs - (chipnum << cfi->chipshift);
151
152 i=first;
153
154 while(len) {
155 int size = regions[i].erasesize;
156
157 ret = (*frob)(map, &cfi->chips[chipnum], adr, size, thunk);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (ret)
160 return ret;
161
162 adr += size;
163 ofs += size;
164 len -= size;
165
166 if (ofs == regions[i].offset + size * regions[i].numblocks)
167 i++;
168
169 if (adr >> cfi->chipshift) {
170 adr = 0;
171 chipnum++;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (chipnum >= cfi->numchips)
174 break;
175 }
176 }
177
178 return 0;
179}
180
181EXPORT_SYMBOL(cfi_varsize_frob);
182
183MODULE_LICENSE("GPL");