]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mtd/maps/ixp4xx.c
drivers/mtd: Use kzalloc
[net-next-2.6.git] / drivers / mtd / maps / ixp4xx.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * drivers/mtd/maps/ixp4xx.c
3 *
4 * MTD Map file for IXP4XX based systems. Please do not make per-board
5 * changes in here. If your board needs special setup, do it in your
6 * platform level code in arch/arm/mach-ixp4xx/board-setup.c
7 *
8 * Original Author: Intel Corporation
9 * Maintainer: Deepak Saxena <dsaxena@mvista.com>
10 *
11 * Copyright (C) 2002 Intel Corporation
12 * Copyright (C) 2003-2004 MontaVista Software, Inc.
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/types.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/string.h>
4e57b681
TS
21#include <linux/slab.h>
22#include <linux/ioport.h>
23#include <linux/device.h>
4fd5f826 24#include <linux/platform_device.h>
4e57b681 25
1da177e4
LT
26#include <linux/mtd/mtd.h>
27#include <linux/mtd/map.h>
28#include <linux/mtd/partitions.h>
4e57b681 29
1da177e4 30#include <asm/io.h>
1da177e4
LT
31#include <asm/mach/flash.h>
32
33#include <linux/reboot.h>
34
3c773547
JB
35/*
36 * Read/write a 16 bit word from flash address 'addr'.
37 *
38 * When the cpu is in little-endian mode it swizzles the address lines
39 * ('address coherency') so we need to undo the swizzling to ensure commands
40 * and the like end up on the correct flash address.
41 *
42 * To further complicate matters, due to the way the expansion bus controller
43 * handles 32 bit reads, the byte stream ABCD is stored on the flash as:
44 * D15 D0
45 * +---+---+
46 * | A | B | 0
47 * +---+---+
48 * | C | D | 2
49 * +---+---+
50 * This means that on LE systems each 16 bit word must be swapped. Note that
51 * this requires CONFIG_MTD_CFI_BE_BYTE_SWAP to be enabled to 'unswap' the CFI
52 * data and other flash commands which are always in D7-D0.
53 */
1da177e4 54#ifndef __ARMEB__
3c773547
JB
55#ifndef CONFIG_MTD_CFI_BE_BYTE_SWAP
56# error CONFIG_MTD_CFI_BE_BYTE_SWAP required
57#endif
58
59static inline u16 flash_read16(void __iomem *addr)
60{
61 return be16_to_cpu(__raw_readw((void __iomem *)((unsigned long)addr ^ 0x2)));
62}
63
64static inline void flash_write16(u16 d, void __iomem *addr)
65{
66 __raw_writew(cpu_to_be16(d), (void __iomem *)((unsigned long)addr ^ 0x2));
67}
68
1da177e4
LT
69#define BYTE0(h) ((h) & 0xFF)
70#define BYTE1(h) (((h) >> 8) & 0xFF)
3c773547 71
1da177e4 72#else
3c773547
JB
73
74static inline u16 flash_read16(const void __iomem *addr)
75{
76 return __raw_readw(addr);
77}
78
79static inline void flash_write16(u16 d, void __iomem *addr)
80{
81 __raw_writew(d, addr);
82}
83
1da177e4
LT
84#define BYTE0(h) (((h) >> 8) & 0xFF)
85#define BYTE1(h) ((h) & 0xFF)
86#endif
87
88static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
89{
90 map_word val;
3c773547 91 val.x[0] = flash_read16(map->virt + ofs);
1da177e4
LT
92 return val;
93}
94
95/*
96 * The IXP4xx expansion bus only allows 16-bit wide acceses
97 * when attached to a 16-bit wide device (such as the 28F128J3A),
98 * so we can't just memcpy_fromio().
99 */
100static void ixp4xx_copy_from(struct map_info *map, void *to,
101 unsigned long from, ssize_t len)
102{
1da177e4 103 u8 *dest = (u8 *) to;
9090ed0b 104 void __iomem *src = map->virt + from;
1da177e4 105
3c773547
JB
106 if (len <= 0)
107 return;
108
109 if (from & 1) {
53f2b1c8
JR
110 *dest++ = BYTE1(flash_read16(src-1));
111 src++;
3c773547 112 --len;
1da177e4
LT
113 }
114
3c773547
JB
115 while (len >= 2) {
116 u16 data = flash_read16(src);
117 *dest++ = BYTE0(data);
118 *dest++ = BYTE1(data);
119 src += 2;
120 len -= 2;
121 }
122
123 if (len > 0)
124 *dest++ = BYTE0(flash_read16(src));
1da177e4
LT
125}
126
69f34c98 127/*
1da177e4
LT
128 * Unaligned writes are ignored, causing the 8-bit
129 * probe to fail and proceed to the 16-bit probe (which succeeds).
130 */
131static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr)
132{
133 if (!(adr & 1))
3c773547 134 flash_write16(d.x[0], map->virt + adr);
1da177e4
LT
135}
136
69f34c98 137/*
1da177e4
LT
138 * Fast write16 function without the probing check above
139 */
140static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
141{
3c773547 142 flash_write16(d.x[0], map->virt + adr);
1da177e4
LT
143}
144
145struct ixp4xx_flash_info {
146 struct mtd_info *mtd;
147 struct map_info map;
148 struct mtd_partition *partitions;
149 struct resource *res;
150};
151
152static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
153
3ae5eaec 154static int ixp4xx_flash_remove(struct platform_device *dev)
1da177e4 155{
1da177e4 156 struct flash_platform_data *plat = dev->dev.platform_data;
3ae5eaec 157 struct ixp4xx_flash_info *info = platform_get_drvdata(dev);
1da177e4 158
3ae5eaec 159 platform_set_drvdata(dev, NULL);
1da177e4
LT
160
161 if(!info)
162 return 0;
163
1da177e4
LT
164 if (info->mtd) {
165 del_mtd_partitions(info->mtd);
166 map_destroy(info->mtd);
167 }
9090ed0b
DV
168 if (info->map.virt)
169 iounmap(info->map.virt);
1da177e4 170
fa671646 171 kfree(info->partitions);
1da177e4
LT
172
173 if (info->res) {
174 release_resource(info->res);
175 kfree(info->res);
176 }
177
178 if (plat->exit)
179 plat->exit();
180
1da177e4
LT
181 return 0;
182}
183
3ae5eaec 184static int ixp4xx_flash_probe(struct platform_device *dev)
1da177e4 185{
1da177e4
LT
186 struct flash_platform_data *plat = dev->dev.platform_data;
187 struct ixp4xx_flash_info *info;
188 int err = -1;
189
190 if (!plat)
191 return -ENODEV;
192
193 if (plat->init) {
194 err = plat->init();
195 if (err)
196 return err;
197 }
198
2bfefa4c 199 info = kzalloc(sizeof(struct ixp4xx_flash_info), GFP_KERNEL);
1da177e4
LT
200 if(!info) {
201 err = -ENOMEM;
202 goto Error;
69f34c98 203 }
1da177e4 204
3ae5eaec 205 platform_set_drvdata(dev, info);
1da177e4 206
1da177e4
LT
207 /*
208 * Tell the MTD layer we're not 1:1 mapped so that it does
209 * not attempt to do a direct access on us.
210 */
211 info->map.phys = NO_XIP;
d6587fea 212 info->map.size = resource_size(dev->resource);
1da177e4
LT
213
214 /*
215 * We only support 16-bit accesses for now. If and when
216 * any board use 8-bit access, we'll fixup the driver to
217 * handle that.
218 */
219 info->map.bankwidth = 2;
475b44c1 220 info->map.name = dev_name(&dev->dev);
1da177e4
LT
221 info->map.read = ixp4xx_read16,
222 info->map.write = ixp4xx_probe_write16,
223 info->map.copy_from = ixp4xx_copy_from,
224
69f34c98 225 info->res = request_mem_region(dev->resource->start,
d6587fea 226 resource_size(dev->resource),
1da177e4
LT
227 "IXP4XXFlash");
228 if (!info->res) {
229 printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n");
230 err = -ENOMEM;
231 goto Error;
232 }
233
9090ed0b 234 info->map.virt = ioremap(dev->resource->start,
d6587fea 235 resource_size(dev->resource));
9090ed0b 236 if (!info->map.virt) {
1da177e4
LT
237 printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n");
238 err = -EIO;
239 goto Error;
240 }
241
242 info->mtd = do_map_probe(plat->map_name, &info->map);
243 if (!info->mtd) {
244 printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
245 err = -ENXIO;
246 goto Error;
247 }
248 info->mtd->owner = THIS_MODULE;
69f34c98 249
1da177e4
LT
250 /* Use the fast version */
251 info->map.write = ixp4xx_write16,
252
f40a6f1c 253 err = parse_mtd_partitions(info->mtd, probes, &info->partitions, dev->resource->start);
1da177e4
LT
254 if (err > 0) {
255 err = add_mtd_partitions(info->mtd, info->partitions, err);
256 if(err)
257 printk(KERN_ERR "Could not parse partitions\n");
258 }
259
260 if (err)
261 goto Error;
262
263 return 0;
264
265Error:
3ae5eaec 266 ixp4xx_flash_remove(dev);
1da177e4
LT
267 return err;
268}
269
3ae5eaec 270static struct platform_driver ixp4xx_flash_driver = {
1da177e4
LT
271 .probe = ixp4xx_flash_probe,
272 .remove = ixp4xx_flash_remove,
3ae5eaec
RK
273 .driver = {
274 .name = "IXP4XX-Flash",
41d867c9 275 .owner = THIS_MODULE,
3ae5eaec 276 },
1da177e4
LT
277};
278
279static int __init ixp4xx_flash_init(void)
280{
3ae5eaec 281 return platform_driver_register(&ixp4xx_flash_driver);
1da177e4
LT
282}
283
284static void __exit ixp4xx_flash_exit(void)
285{
3ae5eaec 286 platform_driver_unregister(&ixp4xx_flash_driver);
1da177e4
LT
287}
288
289
290module_init(ixp4xx_flash_init);
291module_exit(ixp4xx_flash_exit);
292
293MODULE_LICENSE("GPL");
82810a90 294MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
1da177e4 295MODULE_AUTHOR("Deepak Saxena");
41d867c9 296MODULE_ALIAS("platform:IXP4XX-Flash");