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