Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[pandora-kernel.git] / drivers / mtd / chips / cfi_util.c
1 /*
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.
9  *
10  * $Id: cfi_util.c,v 1.10 2005/11/07 11:14:23 gleixner Exp $
11  *
12  */
13
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <asm/io.h>
18 #include <asm/byteorder.h>
19
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/mtd/xip.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/map.h>
27 #include <linux/mtd/cfi.h>
28 #include <linux/mtd/compatmac.h>
29
30 struct cfi_extquery *
31 __xipram cfi_read_pri(struct map_info *map, __u16 adr, __u16 size, const char* name)
32 {
33         struct cfi_private *cfi = map->fldrv_priv;
34         __u32 base = 0; // cfi->chips[0].start;
35         int ofs_factor = cfi->interleave * cfi->device_type;
36         int i;
37         struct cfi_extquery *extp = NULL;
38
39         printk(" %s Extended Query Table at 0x%4.4X\n", name, adr);
40         if (!adr)
41                 goto out;
42
43         extp = kmalloc(size, GFP_KERNEL);
44         if (!extp) {
45                 printk(KERN_ERR "Failed to allocate memory\n");
46                 goto out;
47         }
48
49 #ifdef CONFIG_MTD_XIP
50         local_irq_disable();
51 #endif
52
53         /* Switch it into Query Mode */
54         cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
55
56         /* Read in the Extended Query Table */
57         for (i=0; i<size; i++) {
58                 ((unsigned char *)extp)[i] =
59                         cfi_read_query(map, base+((adr+i)*ofs_factor));
60         }
61
62         /* Make sure it returns to read mode */
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
66 #ifdef CONFIG_MTD_XIP
67         (void) map_read(map, base);
68         asm volatile (".rep 8; nop; .endr");
69         local_irq_enable();
70 #endif
71
72  out:   return extp;
73 }
74
75 EXPORT_SYMBOL(cfi_read_pri);
76
77 void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup *fixups)
78 {
79         struct map_info *map = mtd->priv;
80         struct cfi_private *cfi = map->fldrv_priv;
81         struct cfi_fixup *f;
82
83         for (f=fixups; f->fixup; f++) {
84                 if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi->mfr)) &&
85                     ((f->id  == CFI_ID_ANY)  || (f->id  == cfi->id))) {
86                         f->fixup(mtd, f->param);
87                 }
88         }
89 }
90
91 EXPORT_SYMBOL(cfi_fixup);
92
93 int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
94                                      loff_t ofs, size_t len, void *thunk)
95 {
96         struct map_info *map = mtd->priv;
97         struct cfi_private *cfi = map->fldrv_priv;
98         unsigned long adr;
99         int chipnum, ret = 0;
100         int i, first;
101         struct mtd_erase_region_info *regions = mtd->eraseregions;
102
103         if (ofs > mtd->size)
104                 return -EINVAL;
105
106         if ((len + ofs) > mtd->size)
107                 return -EINVAL;
108
109         /* Check that both start and end of the requested erase are
110          * aligned with the erasesize at the appropriate addresses.
111          */
112
113         i = 0;
114
115         /* Skip all erase regions which are ended before the start of
116            the requested erase. Actually, to save on the calculations,
117            we skip to the first erase region which starts after the
118            start of the requested erase, and then go back one.
119         */
120
121         while (i < mtd->numeraseregions && ofs >= regions[i].offset)
122                i++;
123         i--;
124
125         /* OK, now i is pointing at the erase region in which this
126            erase request starts. Check the start of the requested
127            erase range is aligned with the erase size which is in
128            effect here.
129         */
130
131         if (ofs & (regions[i].erasesize-1))
132                 return -EINVAL;
133
134         /* Remember the erase region we start on */
135         first = i;
136
137         /* Next, check that the end of the requested erase is aligned
138          * with the erase region at that address.
139          */
140
141         while (i<mtd->numeraseregions && (ofs + len) >= regions[i].offset)
142                 i++;
143
144         /* As before, drop back one to point at the region in which
145            the address actually falls
146         */
147         i--;
148
149         if ((ofs + len) & (regions[i].erasesize-1))
150                 return -EINVAL;
151
152         chipnum = ofs >> cfi->chipshift;
153         adr = ofs - (chipnum << cfi->chipshift);
154
155         i=first;
156
157         while(len) {
158                 int size = regions[i].erasesize;
159
160                 ret = (*frob)(map, &cfi->chips[chipnum], adr, size, thunk);
161
162                 if (ret)
163                         return ret;
164
165                 adr += size;
166                 ofs += size;
167                 len -= size;
168
169                 if (ofs == regions[i].offset + size * regions[i].numblocks)
170                         i++;
171
172                 if (adr >> cfi->chipshift) {
173                         adr = 0;
174                         chipnum++;
175
176                         if (chipnum >= cfi->numchips)
177                         break;
178                 }
179         }
180
181         return 0;
182 }
183
184 EXPORT_SYMBOL(cfi_varsize_frob);
185
186 MODULE_LICENSE("GPL");