Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* linux/drivers/mtd/maps/bast_flash.c |
2 | * | |
6fc93d8c BD |
3 | * Copyright (c) 2004-2005 Simtec Electronics |
4 | * Ben Dooks <ben@simtec.co.uk> | |
1da177e4 LT |
5 | * |
6 | * Simtec Bast (EB2410ITX) NOR MTD Mapping driver | |
7 | * | |
8 | * Changelog: | |
9 | * 20-Sep-2004 BJD Initial version | |
6fc93d8c | 10 | * 17-Jan-2005 BJD Add whole device if no partitions found |
1da177e4 | 11 | * |
6fc93d8c | 12 | * $Id: bast-flash.c,v 1.2 2005/01/18 11:13:47 bjd Exp $ |
1da177e4 LT |
13 | * |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
27 | */ | |
28 | ||
29 | #include <linux/module.h> | |
30 | #include <linux/types.h> | |
31 | #include <linux/init.h> | |
32 | #include <linux/kernel.h> | |
33 | #include <linux/string.h> | |
34 | #include <linux/ioport.h> | |
d052d1be | 35 | #include <linux/platform_device.h> |
1da177e4 LT |
36 | |
37 | #include <linux/mtd/mtd.h> | |
38 | #include <linux/mtd/map.h> | |
39 | #include <linux/mtd/partitions.h> | |
40 | ||
41 | #include <asm/io.h> | |
1da177e4 LT |
42 | #include <asm/mach/flash.h> |
43 | ||
44 | #include <asm/arch/map.h> | |
45 | #include <asm/arch/bast-map.h> | |
46 | #include <asm/arch/bast-cpld.h> | |
47 | ||
48 | #ifdef CONFIG_MTD_BAST_MAXSIZE | |
6fc93d8c | 49 | #define AREA_MAXSIZE (CONFIG_MTD_BAST_MAXSIZE * SZ_1M) |
1da177e4 | 50 | #else |
6fc93d8c | 51 | #define AREA_MAXSIZE (32 * SZ_1M) |
1da177e4 LT |
52 | #endif |
53 | ||
54 | #define PFX "bast-flash: " | |
55 | ||
56 | struct bast_flash_info { | |
57 | struct mtd_info *mtd; | |
58 | struct map_info map; | |
59 | struct mtd_partition *partitions; | |
60 | struct resource *area; | |
61 | }; | |
62 | ||
63 | static const char *probes[] = { "RedBoot", "cmdlinepart", NULL }; | |
64 | ||
65 | static struct bast_flash_info *to_bast_info(struct device *dev) | |
66 | { | |
67 | return (struct bast_flash_info *)dev_get_drvdata(dev); | |
68 | } | |
69 | ||
70 | static void bast_flash_setrw(int to) | |
71 | { | |
72 | unsigned int val; | |
73 | unsigned long flags; | |
74 | ||
75 | local_irq_save(flags); | |
76 | val = __raw_readb(BAST_VA_CTRL3); | |
77 | ||
78 | if (to) | |
79 | val |= BAST_CPLD_CTRL3_ROMWEN; | |
80 | else | |
81 | val &= ~BAST_CPLD_CTRL3_ROMWEN; | |
82 | ||
83 | pr_debug("new cpld ctrl3=%02x\n", val); | |
84 | ||
85 | __raw_writeb(val, BAST_VA_CTRL3); | |
86 | local_irq_restore(flags); | |
87 | } | |
88 | ||
89 | static int bast_flash_remove(struct device *dev) | |
90 | { | |
91 | struct bast_flash_info *info = to_bast_info(dev); | |
92 | ||
93 | dev_set_drvdata(dev, NULL); | |
94 | ||
95 | if (info == NULL) | |
96 | return 0; | |
97 | ||
98 | if (info->map.virt != NULL) | |
99 | iounmap(info->map.virt); | |
100 | ||
101 | if (info->mtd) { | |
102 | del_mtd_partitions(info->mtd); | |
103 | map_destroy(info->mtd); | |
104 | } | |
105 | ||
106 | if (info->partitions) | |
107 | kfree(info->partitions); | |
108 | ||
109 | if (info->area) { | |
110 | release_resource(info->area); | |
111 | kfree(info->area); | |
112 | } | |
113 | ||
114 | kfree(info); | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
119 | static int bast_flash_probe(struct device *dev) | |
120 | { | |
121 | struct platform_device *pdev = to_platform_device(dev); | |
122 | struct bast_flash_info *info; | |
123 | struct resource *res; | |
124 | int err = 0; | |
125 | ||
126 | info = kmalloc(sizeof(*info), GFP_KERNEL); | |
127 | if (info == NULL) { | |
128 | printk(KERN_ERR PFX "no memory for flash info\n"); | |
129 | err = -ENOMEM; | |
130 | goto exit_error; | |
131 | } | |
132 | ||
133 | memzero(info, sizeof(*info)); | |
134 | dev_set_drvdata(dev, info); | |
135 | ||
136 | res = pdev->resource; /* assume that the flash has one resource */ | |
137 | ||
138 | info->map.phys = res->start; | |
139 | info->map.size = res->end - res->start + 1; | |
140 | info->map.name = dev->bus_id; | |
141 | info->map.bankwidth = 2; | |
142 | ||
143 | if (info->map.size > AREA_MAXSIZE) | |
144 | info->map.size = AREA_MAXSIZE; | |
145 | ||
146 | pr_debug("%s: area %08lx, size %ld\n", __FUNCTION__, | |
147 | info->map.phys, info->map.size); | |
148 | ||
149 | info->area = request_mem_region(res->start, info->map.size, | |
150 | pdev->name); | |
151 | if (info->area == NULL) { | |
152 | printk(KERN_ERR PFX "cannot reserve flash memory region\n"); | |
153 | err = -ENOENT; | |
154 | goto exit_error; | |
155 | } | |
156 | ||
157 | info->map.virt = ioremap(res->start, info->map.size); | |
158 | pr_debug("%s: virt at %08x\n", __FUNCTION__, (int)info->map.virt); | |
159 | ||
160 | if (info->map.virt == 0) { | |
161 | printk(KERN_ERR PFX "failed to ioremap() region\n"); | |
162 | err = -EIO; | |
163 | goto exit_error; | |
164 | } | |
165 | ||
166 | simple_map_init(&info->map); | |
167 | ||
168 | /* enable the write to the flash area */ | |
169 | ||
170 | bast_flash_setrw(1); | |
171 | ||
172 | /* probe for the device(s) */ | |
173 | ||
174 | info->mtd = do_map_probe("jedec_probe", &info->map); | |
175 | if (info->mtd == NULL) | |
176 | info->mtd = do_map_probe("cfi_probe", &info->map); | |
177 | ||
178 | if (info->mtd == NULL) { | |
179 | printk(KERN_ERR PFX "map_probe() failed\n"); | |
180 | err = -ENXIO; | |
181 | goto exit_error; | |
182 | } | |
183 | ||
184 | /* mark ourselves as the owner */ | |
185 | info->mtd->owner = THIS_MODULE; | |
186 | ||
187 | err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0); | |
188 | if (err > 0) { | |
189 | err = add_mtd_partitions(info->mtd, info->partitions, err); | |
190 | if (err) | |
191 | printk(KERN_ERR PFX "cannot add/parse partitions\n"); | |
6fc93d8c BD |
192 | } else { |
193 | err = add_mtd_device(info->mtd); | |
1da177e4 LT |
194 | } |
195 | ||
196 | if (err == 0) | |
197 | return 0; | |
198 | ||
199 | /* fall through to exit error */ | |
200 | ||
201 | exit_error: | |
202 | bast_flash_remove(dev); | |
203 | return err; | |
204 | } | |
205 | ||
206 | static struct device_driver bast_flash_driver = { | |
207 | .name = "bast-nor", | |
208 | .bus = &platform_bus_type, | |
209 | .probe = bast_flash_probe, | |
210 | .remove = bast_flash_remove, | |
211 | }; | |
212 | ||
213 | static int __init bast_flash_init(void) | |
214 | { | |
215 | printk("BAST NOR-Flash Driver, (c) 2004 Simtec Electronics\n"); | |
216 | return driver_register(&bast_flash_driver); | |
217 | } | |
218 | ||
219 | static void __exit bast_flash_exit(void) | |
220 | { | |
221 | driver_unregister(&bast_flash_driver); | |
222 | } | |
223 | ||
224 | module_init(bast_flash_init); | |
225 | module_exit(bast_flash_exit); | |
226 | ||
227 | MODULE_LICENSE("GPL"); | |
228 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); | |
229 | MODULE_DESCRIPTION("BAST MTD Map driver"); |