MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mtd / maps / ixp4xx.c
blob36b3ed046a7eff8d0e5a7d9f94050b52423bb9e0
1 /*
2 * $Id: ixp4xx.c,v 1.4 2004/10/19 06:41:34 gerg Exp $
4 * drivers/mtd/maps/ixp425.c
6 * MTD Map file for IXP4XX based systems. Please do not make per-board
7 * map driver as the code will be 90% identical. For now just add
8 * if(machine_is_XXX()) checks to the code. I'll clean this stuff to
9 * use platform_data in the the future so we can get rid of that too.
11 * Original Author: Intel Corporation
12 * Maintainer: Deepak Saxena <dsaxena@mvista.com>
14 * Copyright (C) 2002 Intel Corporation
15 * Copyright (C) 2003 MontaVista Software, Inc.
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/map.h>
26 #include <linux/mtd/partitions.h>
27 #include <linux/ioport.h>
28 #include <linux/device.h>
29 #include <asm/io.h>
30 #include <asm/mach-types.h>
31 #include <asm/mach/flash.h>
33 #include <linux/reboot.h>
35 #ifndef __ARMEB__
36 #define BYTE0(h) ((h) & 0xFF)
37 #define BYTE1(h) (((h) >> 8) & 0xFF)
38 #else
39 #define BYTE0(h) (((h) >> 8) & 0xFF)
40 #define BYTE1(h) ((h) & 0xFF)
41 #endif
43 static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
45 map_word val;
46 val.x[0] = *(__u16 *) (map->map_priv_1 + ofs);
47 return val;
51 * The IXP4XX expansion bus only allows 16-bit wide acceses
52 * when attached to a 16-bit wide device (such as the 28F128J3A),
53 * so we can't just memcpy_fromio().
55 static void ixp4xx_copy_from(struct map_info *map, void *to,
56 unsigned long from, ssize_t len)
58 int i;
59 u8 *dest = (u8 *) to;
60 u16 *src = (u16 *) (map->map_priv_1 + from);
61 u16 data;
63 for (i = 0; i < (len / 2); i++) {
64 #ifdef __ARMEB__
65 data = src[i];
66 #else
67 data = src[i ^ 1];
68 #endif
69 dest[i * 2] = BYTE0(data);
70 dest[i * 2 + 1] = BYTE1(data);
73 if (len & 1)
74 #ifdef __ARMEB__
75 dest[len - 1] = BYTE0(src[i]);
76 #else
77 dest[len - 1] = BYTE0(src[i ^ 1]);
78 #endif
82 * Unaligned writes are ignored, causing the 8-bit
83 * probe to fail and proceed to the 16-bit probe (which succeeds).
85 static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr)
87 if (!(adr & 1))
88 *(__u16 *) (map->map_priv_1 + adr) = d.x[0];
92 * Fast write16 function without the probing check above
94 static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
96 *(__u16 *) (map->map_priv_1 + adr) = d.x[0];
99 struct ixp425_flash_info {
100 struct mtd_info *mtd;
101 struct map_info map;
102 struct mtd_partition *partitions;
103 struct resource *res;
106 static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
108 static int ixp4xx_flash_remove(struct device *_dev)
110 struct platform_device *dev = to_platform_device(_dev);
111 struct flash_platform_data *plat = dev->dev.platform_data;
112 struct ixp4xx_flash_info *info = dev_get_drvdata(&dev->dev);
113 map_word d;
115 dev_set_drvdata(&dev->dev, NULL);
118 * Reboot hack...
120 d.x[0] = 0xff;
121 ixp4xx_write16(&info->map, d, 0x55 * 0x2);
123 if (info->mtd) {
124 del_mtd_partitions(info->mtd);
125 map_destroy(info->mtd);
127 if (info->map.map_priv_1)
128 iounmap((void *) info->map.map_priv_1);
130 if (info->partitions)
131 kfree(info->partitions);
133 if (info->res) {
134 release_resource(info->res);
135 kfree(info->res);
138 /* Disable flash write */
139 *IXP4XX_EXP_CS0 &= ~IXP4XX_FLASH_WRITABLE;
141 return 0;
144 static int ixp425_flash_probe(struct device *_dev)
146 struct platform_device *dev = to_platform_device(_dev);
147 struct flash_platform_data *plat = dev->dev.platform_data;
148 struct ixp425_flash_info *info;
149 int err = -1;
151 info = kmalloc(sizeof(struct ixp425_flash_info), GFP_KERNEL);
152 if(!info) {
153 err = -ENOMEM;
154 goto Error;
156 memzero(info, sizeof(struct ixp425_flash_info));
158 dev_set_drvdata(&dev->dev, info);
160 /* Enable flash write */
161 *IXP4XX_EXP_CS0 |= IXP4XX_FLASH_WRITABLE;
164 * We only support 16-bit accesses for now. If and when
165 * any board use 8-bit access, we'll fixup the driver to
166 * handle that.
168 info->map.bankwidth = 2;
169 info->map.name = dev->dev.bus_id;
170 info->map.read = ixp4xx_read16,
171 info->map.write = ixp4xx_probe_write16,
172 info->map.copy_from = ixp4xx_copy_from,
174 info->res = request_mem_region(dev->resource->start,
175 dev->resource->end - dev->resource->start + 1,
176 "IXP4XXFlash");
177 if (!info->res) {
178 printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n");
179 err = -ENOMEM;
180 goto Error;
183 info->map.map_priv_1 =
184 (void __iomem *) ioremap(dev->resource->start,
185 dev->resource->end - dev->resource->start + 1);
186 if (!info->map.map_priv_1) {
187 printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n");
188 err = -EIO;
189 goto Error;
192 info->mtd = do_map_probe(plat->map_name, &info->map);
193 if (!info->mtd) {
194 printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
195 err = -ENXIO;
196 goto Error;
198 info->mtd->owner = THIS_MODULE;
200 /* Use the fast version */
201 info->map.write = ixp4xx_write16,
203 err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
204 if (err > 0) {
205 err = add_mtd_partitions(info->mtd, info->partitions, err);
206 if(err)
207 printk(KERN_ERR "Could not parse partitions\n");
210 if (err)
211 goto Error;
213 return 0;
215 Error:
216 ixp425_flash_remove(_dev);
217 return err;
220 static struct device_driver ixp425_flash_driver = {
221 .name = "IXP4XX-Flash",
222 .bus = &platform_bus_type,
223 .probe = ixp425_flash_probe,
224 .remove = ixp425_flash_remove,
227 static int __init ixp425_flash_init(void)
229 return driver_register(&ixp425_flash_driver);
232 static void __exit ixp425_flash_exit(void)
234 driver_unregister(&ixp425_flash_driver);
238 module_init(ixp425_flash_init);
239 module_exit(ixp425_flash_exit);
241 MODULE_LICENSE("GPL");
242 MODULE_DESCRIPTION("MTD map driver for ixp425 evaluation board");
243 MODULE_AUTHOR("Deepak Saxena");