Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-2.6] / arch / cris / arch-v32 / drivers / axisflashmap.c
1 /*
2  * Physical mapping layer for MTD using the Axis partitiontable format
3  *
4  * Copyright (c) 2001, 2002, 2003 Axis Communications AB
5  *
6  * This file is under the GPL.
7  *
8  * First partition is always sector 0 regardless of if we find a partitiontable
9  * or not. In the start of the next sector, there can be a partitiontable that
10  * tells us what other partitions to define. If there isn't, we use a default
11  * partition split defined below.
12  *
13  * Copy of os/lx25/arch/cris/arch-v10/drivers/axisflashmap.c 1.5
14  * with minor changes.
15  *
16  */
17
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23
24 #include <linux/mtd/concat.h>
25 #include <linux/mtd/map.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/mtdram.h>
28 #include <linux/mtd/partitions.h>
29
30 #include <asm/arch/hwregs/config_defs.h>
31 #include <asm/axisflashmap.h>
32 #include <asm/mmu.h>
33
34 #define MEM_CSE0_SIZE (0x04000000)
35 #define MEM_CSE1_SIZE (0x04000000)
36
37 #define FLASH_UNCACHED_ADDR  KSEG_E
38 #define FLASH_CACHED_ADDR    KSEG_F
39
40 #if CONFIG_ETRAX_FLASH_BUSWIDTH==1
41 #define flash_data __u8
42 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
43 #define flash_data __u16
44 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
45 #define flash_data __u16
46 #endif
47
48 /* From head.S */
49 extern unsigned long romfs_start, romfs_length, romfs_in_flash;
50
51 /* The master mtd for the entire flash. */
52 struct mtd_info* axisflash_mtd = NULL;
53
54 /* Map driver functions. */
55
56 static map_word flash_read(struct map_info *map, unsigned long ofs)
57 {
58         map_word tmp;
59         tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);
60         return tmp;
61 }
62
63 static void flash_copy_from(struct map_info *map, void *to,
64                             unsigned long from, ssize_t len)
65 {
66         memcpy(to, (void *)(map->map_priv_1 + from), len);
67 }
68
69 static void flash_write(struct map_info *map, map_word d, unsigned long adr)
70 {
71         *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];
72 }
73
74 /*
75  * The map for chip select e0.
76  *
77  * We run into tricky coherence situations if we mix cached with uncached
78  * accesses to we only use the uncached version here.
79  *
80  * The size field is the total size where the flash chips may be mapped on the
81  * chip select. MTD probes should find all devices there and it does not matter
82  * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
83  * probes will ignore them.
84  *
85  * The start address in map_priv_1 is in virtual memory so we cannot use
86  * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
87  * address of cse0.
88  */
89 static struct map_info map_cse0 = {
90         .name = "cse0",
91         .size = MEM_CSE0_SIZE,
92         .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
93         .read = flash_read,
94         .copy_from = flash_copy_from,
95         .write = flash_write,
96         .map_priv_1 = FLASH_UNCACHED_ADDR
97 };
98
99 /*
100  * The map for chip select e1.
101  *
102  * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
103  * address, but there isn't.
104  */
105 static struct map_info map_cse1 = {
106         .name = "cse1",
107         .size = MEM_CSE1_SIZE,
108         .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
109         .read = flash_read,
110         .copy_from = flash_copy_from,
111         .write = flash_write,
112         .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE
113 };
114
115 /* If no partition-table was found, we use this default-set. */
116 #define MAX_PARTITIONS         7
117 #define NUM_DEFAULT_PARTITIONS 3
118
119 /*
120  * Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the
121  * size of one flash block and "filesystem"-partition needs 5 blocks to be able
122  * to use JFFS.
123  */
124 static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {
125         {
126                 .name = "boot firmware",
127                 .size = CONFIG_ETRAX_PTABLE_SECTOR,
128                 .offset = 0
129         },
130         {
131                 .name = "kernel",
132                 .size = 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR),
133                 .offset = CONFIG_ETRAX_PTABLE_SECTOR
134         },
135         {
136                 .name = "filesystem",
137                 .size = 5 * CONFIG_ETRAX_PTABLE_SECTOR,
138                 .offset = 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR)
139         }
140 };
141
142 /* Initialize the ones normally used. */
143 static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {
144         {
145                 .name = "part0",
146                 .size = CONFIG_ETRAX_PTABLE_SECTOR,
147                 .offset = 0
148         },
149         {
150                 .name = "part1",
151                 .size = 0,
152                 .offset = 0
153         },
154         {
155                 .name = "part2",
156                 .size = 0,
157                 .offset = 0
158         },
159         {
160                 .name = "part3",
161                 .size = 0,
162                 .offset = 0
163         },
164         {
165                 .name = "part4",
166                 .size = 0,
167                 .offset = 0
168         },
169         {
170                 .name = "part5",
171                 .size = 0,
172                 .offset = 0
173         },
174         {
175                 .name = "part6",
176                 .size = 0,
177                 .offset = 0
178         },
179 };
180
181 /*
182  * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
183  * chips in that order (because the amd_flash-driver is faster).
184  */
185 static struct mtd_info *probe_cs(struct map_info *map_cs)
186 {
187         struct mtd_info *mtd_cs = NULL;
188
189         printk(KERN_INFO
190                "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
191                map_cs->name, map_cs->size, map_cs->map_priv_1);
192
193 #ifdef CONFIG_MTD_CFI
194                 mtd_cs = do_map_probe("cfi_probe", map_cs);
195 #endif
196 #ifdef CONFIG_MTD_JEDECPROBE
197         if (!mtd_cs)
198                 mtd_cs = do_map_probe("jedec_probe", map_cs);
199 #endif
200
201         return mtd_cs;
202 }
203
204 /*
205  * Probe each chip select individually for flash chips. If there are chips on
206  * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
207  * so that MTD partitions can cross chip boundaries.
208  *
209  * The only known restriction to how you can mount your chips is that each
210  * chip select must hold similar flash chips. But you need external hardware
211  * to do that anyway and you can put totally different chips on cse0 and cse1
212  * so it isn't really much of a restriction.
213  */
214 extern struct mtd_info* __init crisv32_nand_flash_probe (void);
215 static struct mtd_info *flash_probe(void)
216 {
217         struct mtd_info *mtd_cse0;
218         struct mtd_info *mtd_cse1;
219         struct mtd_info *mtd_nand = NULL;
220         struct mtd_info *mtd_total;
221         struct mtd_info *mtds[3];
222         int count = 0;
223
224         if ((mtd_cse0 = probe_cs(&map_cse0)) != NULL)
225                 mtds[count++] = mtd_cse0;
226         if ((mtd_cse1 = probe_cs(&map_cse1)) != NULL)
227                 mtds[count++] = mtd_cse1;
228
229 #ifdef CONFIG_ETRAX_NANDFLASH
230         if ((mtd_nand = crisv32_nand_flash_probe()) != NULL)
231                 mtds[count++] = mtd_nand;
232 #endif
233
234         if (!mtd_cse0 && !mtd_cse1 && !mtd_nand) {
235                 /* No chip found. */
236                 return NULL;
237         }
238
239         if (count > 1) {
240 #ifdef CONFIG_MTD_CONCAT
241                 /* Since the concatenation layer adds a small overhead we
242                  * could try to figure out if the chips in cse0 and cse1 are
243                  * identical and reprobe the whole cse0+cse1 window. But since
244                  * flash chips are slow, the overhead is relatively small.
245                  * So we use the MTD concatenation layer instead of further
246                  * complicating the probing procedure.
247                  */
248                 mtd_total = mtd_concat_create(mtds,
249                                               count,
250                                               "cse0+cse1+nand");
251 #else
252                 printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel "
253                        "(mis)configuration!\n", map_cse0.name, map_cse1.name);
254                 mtd_toal = NULL;
255 #endif
256                 if (!mtd_total) {
257                         printk(KERN_ERR "%s and %s: Concatenation failed!\n",
258                                map_cse0.name, map_cse1.name);
259
260                         /* The best we can do now is to only use what we found
261                          * at cse0.
262                          */
263                         mtd_total = mtd_cse0;
264                         map_destroy(mtd_cse1);
265                 }
266         } else {
267                 mtd_total = mtd_cse0? mtd_cse0 : mtd_cse1 ? mtd_cse1 : mtd_nand;
268
269         }
270
271         return mtd_total;
272 }
273
274 extern unsigned long crisv32_nand_boot;
275 extern unsigned long crisv32_nand_cramfs_offset;
276
277 /*
278  * Probe the flash chip(s) and, if it succeeds, read the partition-table
279  * and register the partitions with MTD.
280  */
281 static int __init init_axis_flash(void)
282 {
283         struct mtd_info *mymtd;
284         int err = 0;
285         int pidx = 0;
286         struct partitiontable_head *ptable_head = NULL;
287         struct partitiontable_entry *ptable;
288         int use_default_ptable = 1; /* Until proven otherwise. */
289         const char *pmsg = KERN_INFO "  /dev/flash%d at 0x%08x, size 0x%08x\n";
290         static char page[512];
291         size_t len;
292
293 #ifndef CONFIG_ETRAXFS_SIM
294         mymtd = flash_probe();
295         mymtd->read(mymtd, CONFIG_ETRAX_PTABLE_SECTOR, 512, &len, page);
296         ptable_head = (struct partitiontable_head *)(page + PARTITION_TABLE_OFFSET);
297
298         if (!mymtd) {
299                 /* There's no reason to use this module if no flash chip can
300                  * be identified. Make sure that's understood.
301                  */
302                 printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
303         } else {
304                 printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
305                        mymtd->name, mymtd->size);
306                 axisflash_mtd = mymtd;
307         }
308
309         if (mymtd) {
310                 mymtd->owner = THIS_MODULE;
311         }
312         pidx++;  /* First partition is always set to the default. */
313
314         if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
315             && (ptable_head->size <
316                 (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
317                 PARTITIONTABLE_END_MARKER_SIZE))
318             && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
319                                   ptable_head->size -
320                                   PARTITIONTABLE_END_MARKER_SIZE)
321                 == PARTITIONTABLE_END_MARKER)) {
322                 /* Looks like a start, sane length and end of a
323                  * partition table, lets check csum etc.
324                  */
325                 int ptable_ok = 0;
326                 struct partitiontable_entry *max_addr =
327                         (struct partitiontable_entry *)
328                         ((unsigned long)ptable_head + sizeof(*ptable_head) +
329                          ptable_head->size);
330                 unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
331                 unsigned char *p;
332                 unsigned long csum = 0;
333
334                 ptable = (struct partitiontable_entry *)
335                         ((unsigned long)ptable_head + sizeof(*ptable_head));
336
337                 /* Lets be PARANOID, and check the checksum. */
338                 p = (unsigned char*) ptable;
339
340                 while (p <= (unsigned char*)max_addr) {
341                         csum += *p++;
342                         csum += *p++;
343                         csum += *p++;
344                         csum += *p++;
345                 }
346                 ptable_ok = (csum == ptable_head->checksum);
347
348                 /* Read the entries and use/show the info.  */
349                 printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
350                        (ptable_ok ? " valid" : "n invalid"), ptable_head,
351                        max_addr);
352
353                 /* We have found a working bootblock.  Now read the
354                  * partition table.  Scan the table.  It ends when
355                  * there is 0xffffffff, that is, empty flash.
356                  */
357                 while (ptable_ok
358                        && ptable->offset != 0xffffffff
359                        && ptable < max_addr
360                        && pidx < MAX_PARTITIONS) {
361
362                         axis_partitions[pidx].offset = offset + ptable->offset + (crisv32_nand_boot ? 16384 : 0);
363                         axis_partitions[pidx].size = ptable->size;
364
365                         printk(pmsg, pidx, axis_partitions[pidx].offset,
366                                axis_partitions[pidx].size);
367                         pidx++;
368                         ptable++;
369                 }
370                 use_default_ptable = !ptable_ok;
371         }
372
373         if (romfs_in_flash) {
374                 /* Add an overlapping device for the root partition (romfs). */
375
376                 axis_partitions[pidx].name = "romfs";
377                 if (crisv32_nand_boot) {
378                         char* data = kmalloc(1024, GFP_KERNEL);
379                         int len;
380                         int offset = crisv32_nand_cramfs_offset & ~(1024-1);
381                         char* tmp;
382
383                         mymtd->read(mymtd, offset, 1024, &len, data);
384                         tmp = &data[crisv32_nand_cramfs_offset % 512];
385                         axis_partitions[pidx].size = *(unsigned*)(tmp + 4);
386                         axis_partitions[pidx].offset = crisv32_nand_cramfs_offset;
387                         kfree(data);
388                 } else {
389                         axis_partitions[pidx].size = romfs_length;
390                         axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
391                 }
392
393                 axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
394
395                 printk(KERN_INFO
396                        " Adding readonly flash partition for romfs image:\n");
397                 printk(pmsg, pidx, axis_partitions[pidx].offset,
398                        axis_partitions[pidx].size);
399                 pidx++;
400         }
401
402         if (mymtd) {
403                 if (use_default_ptable) {
404                         printk(KERN_INFO " Using default partition table.\n");
405                         err = add_mtd_partitions(mymtd, axis_default_partitions,
406                                                  NUM_DEFAULT_PARTITIONS);
407                 } else {
408                         err = add_mtd_partitions(mymtd, axis_partitions, pidx);
409                 }
410
411                 if (err) {
412                         panic("axisflashmap could not add MTD partitions!\n");
413                 }
414         }
415 /* CONFIG_EXTRAXFS_SIM */
416 #endif
417
418         if (!romfs_in_flash) {
419                 /* Create an RAM device for the root partition (romfs). */
420
421 #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
422                 /* No use trying to boot this kernel from RAM. Panic! */
423                 printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
424                        "device due to kernel (mis)configuration!\n");
425                 panic("This kernel cannot boot from RAM!\n");
426 #else
427                 struct mtd_info *mtd_ram;
428
429                 mtd_ram = kmalloc(sizeof(struct mtd_info),
430                                                      GFP_KERNEL);
431                 if (!mtd_ram) {
432                         panic("axisflashmap couldn't allocate memory for "
433                               "mtd_info!\n");
434                 }
435
436                 printk(KERN_INFO " Adding RAM partition for romfs image:\n");
437                 printk(pmsg, pidx, romfs_start, romfs_length);
438
439                 err = mtdram_init_device(mtd_ram, (void*)romfs_start,
440                                          romfs_length, "romfs");
441                 if (err) {
442                         panic("axisflashmap could not initialize MTD RAM "
443                               "device!\n");
444                 }
445 #endif
446         }
447
448         return err;
449 }
450
451 /* This adds the above to the kernels init-call chain. */
452 module_init(init_axis_flash);
453
454 EXPORT_SYMBOL(axisflash_mtd);