blob: d78587990e7e9e3a57b3f1230dd9f8b23a42cc40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Thomas Gleixner69f34c92005-11-07 11:15:40 +00002 * Handle mapping of the flash memory access routines
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * on TQM8xxL based devices.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * based on rpxlite.c
6 *
7 * Copyright(C) 2001 Kirk Lee <kirk@hpc.ee.ntu.edu.tw>
8 *
9 * This code is GPLed
Thomas Gleixner69f34c92005-11-07 11:15:40 +000010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
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
Thomas Gleixner69f34c92005-11-07 11:15:40 +000020 * Thus, we choose CONFIG_MTD_CFI_I2 & CONFIG_MTD_CFI_B4 at
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * kernel configuration.
22 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/map.h>
31#include <linux/mtd/partitions.h>
32
Tim Schmielau4e57b682005-10-30 15:03:48 -080033#include <asm/io.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define FLASH_ADDR 0x40000000
36#define FLASH_SIZE 0x00800000
37#define FLASH_BANK_MAX 4
38
39// trivial struct to describe partition information
40struct 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;
48static struct mtd_info* mtd_banks[FLASH_BANK_MAX];
49static struct map_info* map_banks[FLASH_BANK_MAX];
50static struct mtd_part_def part_banks[FLASH_BANK_MAX];
51static unsigned long num_banks;
52static 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.
Thomas Gleixner69f34c92005-11-07 11:15:40 +000058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * The *_max_flash_size is the maximum possible mapped flash size which
Thomas Gleixner69f34c92005-11-07 11:15:40 +000060 * is not necessarily the actual flash size. It must correspond to the
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * value specified in the mapping definition defined by the
62 * "struct map_desc *_io_desc" for the corresponding machine.
63 */
64
Lucas De Marchi25985ed2011-03-30 22:57:33 -030065/* Currently, TQM8xxL has up to 8MiB flash */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static unsigned long tqm8xxl_max_flash_size = 0x00800000;
67
68/* partition definition for first flash bank
69 * (cf. "drivers/char/flash_config.c")
70 */
71static 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 */
96static 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dmitri Vorobieve30bb9c2008-11-25 02:55:08 +0200110static int __init init_tqm_mtd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
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) {
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700123 printk(KERN_WARNING "%s:Failed to ioremap address:0x%x\n", __func__, flash_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -EIO;
125 }
126
127 for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
128 if(mtd_size >= flash_size)
129 break;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000130
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700131 printk(KERN_INFO "%s: chip probing count %d\n", __func__, idx);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000132
Burman Yan95b93a02006-11-15 21:10:29 +0200133 map_banks[idx] = kzalloc(sizeof(struct map_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if(map_banks[idx] == NULL) {
135 ret = -ENOMEM;
136 /* FIXME: What if some MTD devices were probed already? */
137 goto error_mem;
138 }
139
Jesper Juhlce4a37f2010-11-09 00:09:02 +0100140 map_banks[idx]->name = kmalloc(16, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
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
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700177 printk(KERN_INFO "%s: bank%d, name:%s, size:%dbytes \n", __func__, num_banks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /*
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++) {
Jamie Ilesb5c0a4e2011-05-23 10:23:11 +0100201 if (part_banks[idx].nums == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 printk(KERN_NOTICE "TQM flash%d: no partition info available, registering whole flash at once\n", idx);
Jamie Ilesb5c0a4e2011-05-23 10:23:11 +0100203 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 printk(KERN_NOTICE "TQM flash%d: Using %s partition definition\n",
205 idx, part_banks[idx].type);
Jamie Ilesb5c0a4e2011-05-23 10:23:11 +0100206 mtd_device_register(mtd_banks[idx], part_banks[idx].mtd_part,
207 part_banks[idx].nums);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210error_mem:
211 for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
212 if(map_banks[idx] != NULL) {
Jesper Juhlfa671642005-11-07 01:01:27 -0800213 kfree(map_banks[idx]->name);
214 map_banks[idx]->name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 kfree(map_banks[idx]);
216 map_banks[idx] = NULL;
217 }
218 }
219error:
220 iounmap(start_scan_addr);
221 return ret;
222}
223
224static 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]) {
Jamie Ilesb5c0a4e2011-05-23 10:23:11 +0100230 mtd_device_unregister(mtd_banks[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 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
244module_init(init_tqm_mtd);
245module_exit(cleanup_tqm_mtd);
246
247MODULE_LICENSE("GPL");
248MODULE_AUTHOR("Kirk Lee <kirk@hpc.ee.ntu.edu.tw>");
249MODULE_DESCRIPTION("MTD map driver for TQM8xxL boards");