Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / drivers / mtd / maps / tqm8xxl.c
1 /*
2  * Handle mapping of the flash memory access routines 
3  * on TQM8xxL based devices.
4  *
5  * $Id: tqm8xxl.c,v 1.13 2004/10/20 22:21:53 dwmw2 Exp $
6  *
7  * based on rpxlite.c
8  *
9  * Copyright(C) 2001 Kirk Lee <kirk@hpc.ee.ntu.edu.tw>
10  *
11  * This code is GPLed
12  * 
13  */
14
15 /*
16  * According to TQM8xxL hardware manual, TQM8xxL series have
17  * following flash memory organisations:
18  *      | capacity |    | chip type |   | bank0 |       | bank1 |
19  *          2MiB           512Kx16        2MiB             0
20  *          4MiB           1Mx16          4MiB             0
21  *          8MiB           1Mx16          4MiB             4MiB
22  * Thus, we choose CONFIG_MTD_CFI_I2 & CONFIG_MTD_CFI_B4 at 
23  * kernel configuration.
24  */
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <asm/io.h>
31
32 #include <linux/mtd/mtd.h>
33 #include <linux/mtd/map.h>
34 #include <linux/mtd/partitions.h>
35
36 #define FLASH_ADDR 0x40000000
37 #define FLASH_SIZE 0x00800000
38 #define FLASH_BANK_MAX 4
39
40 // trivial struct to describe partition information
41 struct mtd_part_def
42 {
43         int nums;
44         unsigned char *type;
45         struct mtd_partition* mtd_part;
46 };
47
48 //static struct mtd_info *mymtd;
49 static struct mtd_info* mtd_banks[FLASH_BANK_MAX];
50 static struct map_info* map_banks[FLASH_BANK_MAX];
51 static struct mtd_part_def part_banks[FLASH_BANK_MAX];
52 static unsigned long num_banks;
53 static void __iomem *start_scan_addr;
54
55 /*
56  * Here are partition information for all known TQM8xxL series devices.
57  * See include/linux/mtd/partitions.h for definition of the mtd_partition
58  * structure.
59  * 
60  * The *_max_flash_size is the maximum possible mapped flash size which
61  * is not necessarily the actual flash size.  It must correspond to the 
62  * value specified in the mapping definition defined by the
63  * "struct map_desc *_io_desc" for the corresponding machine.
64  */
65
66 #ifdef CONFIG_MTD_PARTITIONS
67 /* Currently, TQM8xxL has upto 8MiB flash */
68 static unsigned long tqm8xxl_max_flash_size = 0x00800000;
69
70 /* partition definition for first flash bank
71  * (cf. "drivers/char/flash_config.c")
72  */
73 static struct mtd_partition tqm8xxl_partitions[] = {
74         {
75           .name = "ppcboot",
76           .offset = 0x00000000,
77           .size = 0x00020000,           /* 128KB           */
78           .mask_flags = MTD_WRITEABLE,  /* force read-only */
79         },
80         {
81           .name = "kernel",             /* default kernel image */
82           .offset = 0x00020000,
83           .size = 0x000e0000,
84           .mask_flags = MTD_WRITEABLE,  /* force read-only */
85         },
86         {
87           .name = "user",
88           .offset = 0x00100000,
89           .size = 0x00100000,
90         },
91         {
92           .name = "initrd",
93           .offset = 0x00200000,
94           .size = 0x00200000,
95         }
96 };
97 /* partition definition for second flash bank */
98 static struct mtd_partition tqm8xxl_fs_partitions[] = {
99         {
100           .name = "cramfs",
101           .offset = 0x00000000,
102           .size = 0x00200000,
103         },
104         {
105           .name = "jffs",
106           .offset = 0x00200000,
107           .size = 0x00200000,
108           //.size = MTDPART_SIZ_FULL,
109         }
110 };
111 #endif
112
113 int __init init_tqm_mtd(void)
114 {
115         int idx = 0, ret = 0;
116         unsigned long flash_addr, flash_size, mtd_size = 0;
117         /* pointer to TQM8xxL board info data */
118         bd_t *bd = (bd_t *)__res;
119
120         flash_addr = bd->bi_flashstart;
121         flash_size = bd->bi_flashsize;
122
123         //request maximum flash size address space
124         start_scan_addr = ioremap(flash_addr, flash_size);
125         if (!start_scan_addr) {
126                 printk(KERN_WARNING "%s:Failed to ioremap address:0x%x\n", __FUNCTION__, flash_addr);
127                 return -EIO;
128         }
129
130         for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
131                 if(mtd_size >= flash_size)
132                         break;
133                 
134                 printk(KERN_INFO "%s: chip probing count %d\n", __FUNCTION__, idx);
135                 
136                 map_banks[idx] = (struct map_info *)kmalloc(sizeof(struct map_info), GFP_KERNEL);
137                 if(map_banks[idx] == NULL) {
138                         ret = -ENOMEM;
139                         /* FIXME: What if some MTD devices were probed already? */
140                         goto error_mem;
141                 }
142
143                 memset((void *)map_banks[idx], 0, sizeof(struct map_info));
144                 map_banks[idx]->name = (char *)kmalloc(16, GFP_KERNEL);
145
146                 if (!map_banks[idx]->name) {
147                         ret = -ENOMEM;
148                         /* FIXME: What if some MTD devices were probed already? */
149                         goto error_mem;
150                 }
151                 sprintf(map_banks[idx]->name, "TQM8xxL%d", idx);
152
153                 map_banks[idx]->size = flash_size;
154                 map_banks[idx]->bankwidth = 4;
155
156                 simple_map_init(map_banks[idx]);
157
158                 map_banks[idx]->virt = start_scan_addr;
159                 map_banks[idx]->phys = flash_addr;
160                 /* FIXME: This looks utterly bogus, but I'm trying to
161                    preserve the behaviour of the original (shown here)...
162
163                 map_banks[idx]->map_priv_1 =
164                 start_scan_addr + ((idx > 0) ?
165                 (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0);
166                 */
167
168                 if (idx && mtd_banks[idx-1]) {
169                         map_banks[idx]->virt += mtd_banks[idx-1]->size;
170                         map_banks[idx]->phys += mtd_banks[idx-1]->size;
171                 }
172
173                 //start to probe flash chips
174                 mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]);
175
176                 if (mtd_banks[idx]) {
177                         mtd_banks[idx]->owner = THIS_MODULE;
178                         mtd_size += mtd_banks[idx]->size;
179                         num_banks++;
180
181                         printk(KERN_INFO "%s: bank%d, name:%s, size:%dbytes \n", __FUNCTION__, num_banks, 
182                         mtd_banks[idx]->name, mtd_banks[idx]->size);
183                 }
184         }
185
186         /* no supported flash chips found */
187         if (!num_banks) {
188                 printk(KERN_NOTICE "TQM8xxL: No support flash chips found!\n");
189                 ret = -ENXIO;
190                 goto error_mem;
191         }
192
193 #ifdef CONFIG_MTD_PARTITIONS
194         /*
195          * Select Static partition definitions
196          */
197         part_banks[0].mtd_part = tqm8xxl_partitions;
198         part_banks[0].type = "Static image";
199         part_banks[0].nums = ARRAY_SIZE(tqm8xxl_partitions);
200
201         part_banks[1].mtd_part = tqm8xxl_fs_partitions;
202         part_banks[1].type = "Static file system";
203         part_banks[1].nums = ARRAY_SIZE(tqm8xxl_fs_partitions);
204
205         for(idx = 0; idx < num_banks ; idx++) {
206                 if (part_banks[idx].nums == 0) {
207                         printk(KERN_NOTICE "TQM flash%d: no partition info available, registering whole flash at once\n", idx);
208                         add_mtd_device(mtd_banks[idx]);
209                 } else {
210                         printk(KERN_NOTICE "TQM flash%d: Using %s partition definition\n",
211                                         idx, part_banks[idx].type);
212                         add_mtd_partitions(mtd_banks[idx], part_banks[idx].mtd_part, 
213                                                                 part_banks[idx].nums);
214                 }
215         }
216 #else
217         printk(KERN_NOTICE "TQM flash: registering %d whole flash banks at once\n", num_banks);
218         for(idx = 0 ; idx < num_banks ; idx++)
219                 add_mtd_device(mtd_banks[idx]);
220 #endif
221         return 0;
222 error_mem:
223         for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
224                 if(map_banks[idx] != NULL) {
225                         if(map_banks[idx]->name != NULL) {
226                                 kfree(map_banks[idx]->name);
227                                 map_banks[idx]->name = NULL;
228                         }
229                         kfree(map_banks[idx]);
230                         map_banks[idx] = NULL;
231                 }
232         }
233 error:
234         iounmap(start_scan_addr);
235         return ret;
236 }
237
238 static void __exit cleanup_tqm_mtd(void)
239 {
240         unsigned int idx = 0;
241         for(idx = 0 ; idx < num_banks ; idx++) {
242                 /* destroy mtd_info previously allocated */
243                 if (mtd_banks[idx]) {
244                         del_mtd_partitions(mtd_banks[idx]);
245                         map_destroy(mtd_banks[idx]);
246                 }
247                 /* release map_info not used anymore */
248                 kfree(map_banks[idx]->name);
249                 kfree(map_banks[idx]);
250         }
251
252         if (start_scan_addr) {
253                 iounmap(start_scan_addr);
254                 start_scan_addr = 0;
255         }
256 }
257
258 module_init(init_tqm_mtd);
259 module_exit(cleanup_tqm_mtd);
260
261 MODULE_LICENSE("GPL");
262 MODULE_AUTHOR("Kirk Lee <kirk@hpc.ee.ntu.edu.tw>");
263 MODULE_DESCRIPTION("MTD map driver for TQM8xxL boards");