Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
167e1770 | 2 | * $Id: ixp2000.c,v 1.6 2005/03/18 14:07:46 gleixner Exp $ |
1da177e4 LT |
3 | * |
4 | * drivers/mtd/maps/ixp2000.c | |
5 | * | |
6 | * Mapping for the Intel XScale IXP2000 based systems | |
7 | * | |
8 | * Copyright (C) 2002 Intel Corp. | |
9 | * Copyright (C) 2003-2004 MontaVista Software, Inc. | |
10 | * | |
11 | * Original Author: Naeem M Afzal <naeem.m.afzal@intel.com> | |
12 | * Maintainer: Deepak Saxena <dsaxena@plexity.net> | |
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 version 2 as | |
16 | * published by the Free Software Foundation. | |
17 | * | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/types.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/string.h> | |
4e57b681 TS |
25 | #include <linux/slab.h> |
26 | #include <linux/ioport.h> | |
27 | #include <linux/device.h> | |
28 | ||
1da177e4 LT |
29 | #include <linux/mtd/mtd.h> |
30 | #include <linux/mtd/map.h> | |
31 | #include <linux/mtd/partitions.h> | |
1da177e4 LT |
32 | |
33 | #include <asm/io.h> | |
34 | #include <asm/hardware.h> | |
1da177e4 LT |
35 | #include <asm/mach/flash.h> |
36 | ||
37 | #include <linux/reboot.h> | |
38 | ||
39 | struct ixp2000_flash_info { | |
40 | struct mtd_info *mtd; | |
41 | struct map_info map; | |
42 | struct mtd_partition *partitions; | |
43 | struct resource *res; | |
44 | int nr_banks; | |
45 | }; | |
46 | ||
47 | static inline unsigned long flash_bank_setup(struct map_info *map, unsigned long ofs) | |
48 | { | |
49 | unsigned long (*set_bank)(unsigned long) = | |
50 | (unsigned long(*)(unsigned long))map->map_priv_2; | |
51 | ||
52 | return (set_bank ? set_bank(ofs) : ofs); | |
53 | } | |
54 | ||
55 | #ifdef __ARMEB__ | |
56 | /* | |
57 | * Rev A0 and A1 of IXP2400 silicon have a broken addressing unit which | |
58 | * causes the lower address bits to be XORed with 0x11 on 8 bit accesses | |
59 | * and XORed with 0x10 on 16 bit accesses. See the spec update, erratum 44. | |
60 | */ | |
61 | static int erratum44_workaround = 0; | |
62 | ||
63 | static inline unsigned long address_fix8_write(unsigned long addr) | |
64 | { | |
65 | if (erratum44_workaround) { | |
66 | return (addr ^ 3); | |
67 | } | |
68 | return addr; | |
69 | } | |
70 | #else | |
71 | ||
72 | #define address_fix8_write(x) (x) | |
73 | #endif | |
74 | ||
75 | static map_word ixp2000_flash_read8(struct map_info *map, unsigned long ofs) | |
76 | { | |
77 | map_word val; | |
78 | ||
79 | val.x[0] = *((u8 *)(map->map_priv_1 + flash_bank_setup(map, ofs))); | |
80 | return val; | |
81 | } | |
82 | ||
83 | /* | |
84 | * We can't use the standard memcpy due to the broken SlowPort | |
85 | * address translation on rev A0 and A1 silicon and the fact that | |
86 | * we have banked flash. | |
87 | */ | |
88 | static void ixp2000_flash_copy_from(struct map_info *map, void *to, | |
89 | unsigned long from, ssize_t len) | |
90 | { | |
91 | from = flash_bank_setup(map, from); | |
92 | while(len--) | |
93 | *(__u8 *) to++ = *(__u8 *)(map->map_priv_1 + from++); | |
94 | } | |
95 | ||
96 | static void ixp2000_flash_write8(struct map_info *map, map_word d, unsigned long ofs) | |
97 | { | |
98 | *(__u8 *) (address_fix8_write(map->map_priv_1 + | |
99 | flash_bank_setup(map, ofs))) = d.x[0]; | |
100 | } | |
101 | ||
102 | static void ixp2000_flash_copy_to(struct map_info *map, unsigned long to, | |
103 | const void *from, ssize_t len) | |
104 | { | |
105 | to = flash_bank_setup(map, to); | |
106 | while(len--) { | |
107 | unsigned long tmp = address_fix8_write(map->map_priv_1 + to++); | |
108 | *(__u8 *)(tmp) = *(__u8 *)(from++); | |
109 | } | |
110 | } | |
111 | ||
112 | ||
113 | static int ixp2000_flash_remove(struct device *_dev) | |
114 | { | |
115 | struct platform_device *dev = to_platform_device(_dev); | |
116 | struct flash_platform_data *plat = dev->dev.platform_data; | |
117 | struct ixp2000_flash_info *info = dev_get_drvdata(&dev->dev); | |
118 | ||
119 | dev_set_drvdata(&dev->dev, NULL); | |
120 | ||
121 | if(!info) | |
122 | return 0; | |
123 | ||
124 | if (info->mtd) { | |
125 | del_mtd_partitions(info->mtd); | |
126 | map_destroy(info->mtd); | |
127 | } | |
128 | if (info->map.map_priv_1) | |
129 | iounmap((void *) info->map.map_priv_1); | |
130 | ||
131 | if (info->partitions) { | |
132 | kfree(info->partitions); } | |
133 | ||
134 | if (info->res) { | |
135 | release_resource(info->res); | |
136 | kfree(info->res); | |
137 | } | |
138 | ||
139 | if (plat->exit) | |
140 | plat->exit(); | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | ||
146 | static int ixp2000_flash_probe(struct device *_dev) | |
147 | { | |
148 | static const char *probes[] = { "RedBoot", "cmdlinepart", NULL }; | |
149 | struct platform_device *dev = to_platform_device(_dev); | |
150 | struct ixp2000_flash_data *ixp_data = dev->dev.platform_data; | |
151 | struct flash_platform_data *plat; | |
152 | struct ixp2000_flash_info *info; | |
153 | unsigned long window_size; | |
154 | int err = -1; | |
155 | ||
156 | if (!ixp_data) | |
157 | return -ENODEV; | |
158 | ||
159 | plat = ixp_data->platform_data; | |
160 | if (!plat) | |
161 | return -ENODEV; | |
162 | ||
163 | window_size = dev->resource->end - dev->resource->start + 1; | |
164 | dev_info(_dev, "Probe of IXP2000 flash(%d banks x %dMiB)\n", | |
165 | ixp_data->nr_banks, ((u32)window_size >> 20)); | |
166 | ||
167 | if (plat->width != 1) { | |
168 | dev_err(_dev, "IXP2000 MTD map only supports 8-bit mode, asking for %d\n", | |
169 | plat->width * 8); | |
170 | return -EIO; | |
171 | } | |
172 | ||
173 | info = kmalloc(sizeof(struct ixp2000_flash_info), GFP_KERNEL); | |
174 | if(!info) { | |
175 | err = -ENOMEM; | |
176 | goto Error; | |
177 | } | |
178 | memzero(info, sizeof(struct ixp2000_flash_info)); | |
179 | ||
180 | dev_set_drvdata(&dev->dev, info); | |
181 | ||
182 | /* | |
183 | * Tell the MTD layer we're not 1:1 mapped so that it does | |
184 | * not attempt to do a direct access on us. | |
185 | */ | |
186 | info->map.phys = NO_XIP; | |
187 | ||
188 | info->nr_banks = ixp_data->nr_banks; | |
189 | info->map.size = ixp_data->nr_banks * window_size; | |
190 | info->map.bankwidth = 1; | |
191 | ||
192 | /* | |
193 | * map_priv_2 is used to store a ptr to to the bank_setup routine | |
194 | */ | |
195 | info->map.map_priv_2 = (void __iomem *) ixp_data->bank_setup; | |
196 | ||
197 | info->map.name = dev->dev.bus_id; | |
198 | info->map.read = ixp2000_flash_read8; | |
199 | info->map.write = ixp2000_flash_write8; | |
200 | info->map.copy_from = ixp2000_flash_copy_from; | |
201 | info->map.copy_to = ixp2000_flash_copy_to; | |
202 | ||
203 | info->res = request_mem_region(dev->resource->start, | |
204 | dev->resource->end - dev->resource->start + 1, | |
205 | dev->dev.bus_id); | |
206 | if (!info->res) { | |
207 | dev_err(_dev, "Could not reserve memory region\n"); | |
208 | err = -ENOMEM; | |
209 | goto Error; | |
210 | } | |
211 | ||
212 | info->map.map_priv_1 = ioremap(dev->resource->start, | |
213 | dev->resource->end - dev->resource->start + 1); | |
214 | if (!info->map.map_priv_1) { | |
215 | dev_err(_dev, "Failed to ioremap flash region\n"); | |
216 | err = -EIO; | |
217 | goto Error; | |
218 | } | |
219 | ||
1da177e4 LT |
220 | #if defined(__ARMEB__) |
221 | /* | |
222 | * Enable erratum 44 workaround for NPUs with broken slowport | |
223 | */ | |
224 | ||
225 | erratum44_workaround = ixp2000_has_broken_slowport(); | |
226 | dev_info(_dev, "Erratum 44 workaround %s\n", | |
227 | erratum44_workaround ? "enabled" : "disabled"); | |
228 | #endif | |
229 | ||
230 | info->mtd = do_map_probe(plat->map_name, &info->map); | |
231 | if (!info->mtd) { | |
232 | dev_err(_dev, "map_probe failed\n"); | |
233 | err = -ENXIO; | |
234 | goto Error; | |
235 | } | |
236 | info->mtd->owner = THIS_MODULE; | |
237 | ||
238 | err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0); | |
239 | if (err > 0) { | |
240 | err = add_mtd_partitions(info->mtd, info->partitions, err); | |
241 | if(err) | |
242 | dev_err(_dev, "Could not parse partitions\n"); | |
243 | } | |
244 | ||
245 | if (err) | |
246 | goto Error; | |
247 | ||
248 | return 0; | |
249 | ||
250 | Error: | |
251 | ixp2000_flash_remove(_dev); | |
252 | return err; | |
253 | } | |
254 | ||
255 | static struct device_driver ixp2000_flash_driver = { | |
256 | .name = "IXP2000-Flash", | |
257 | .bus = &platform_bus_type, | |
258 | .probe = &ixp2000_flash_probe, | |
259 | .remove = &ixp2000_flash_remove | |
260 | }; | |
261 | ||
262 | static int __init ixp2000_flash_init(void) | |
263 | { | |
264 | return driver_register(&ixp2000_flash_driver); | |
265 | } | |
266 | ||
267 | static void __exit ixp2000_flash_exit(void) | |
268 | { | |
269 | driver_unregister(&ixp2000_flash_driver); | |
270 | } | |
271 | ||
272 | module_init(ixp2000_flash_init); | |
273 | module_exit(ixp2000_flash_exit); | |
274 | MODULE_LICENSE("GPL"); | |
275 | MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>"); | |
276 |