This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / mtd / maps / ixp4xx.c
1 /*
2  * $Id: ixp4xx.c,v 1.1 2004/05/13 22:21:26 dsaxena Exp $
3  *
4  * drivers/mtd/maps/ixp4xx.c
5  *
6  * MTD Map file for IXP4XX based systems. Please do not make per-board
7  * changes in here. If your board needs special setup, do it in your
8  * platform level code in arch/arm/mach-ixp4xx/board-setup.c
9  *
10  * Original Author: Intel Corporation
11  * Maintainer: Deepak Saxena <dsaxena@mvista.com>
12  *
13  * Copyright (C) 2002 Intel Corporation
14  * Copyright (C) 2003-2004 MontaVista Software, Inc.
15  *
16  */
17
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/map.h>
25 #include <linux/mtd/partitions.h>
26 #include <linux/ioport.h>
27 #include <linux/device.h>
28 #include <asm/io.h>
29 #include <asm/mach-types.h>
30 #include <asm/mach/flash.h>
31
32 #include <linux/reboot.h>
33
34 #ifndef __ARMEB__
35 #define BYTE0(h)        ((h) & 0xFF)
36 #define BYTE1(h)        (((h) >> 8) & 0xFF)
37 #else
38 #define BYTE0(h)        (((h) >> 8) & 0xFF)
39 #define BYTE1(h)        ((h) & 0xFF)
40 #endif
41
42 static __u16
43 ixp4xx_read16(struct map_info *map, unsigned long ofs)
44 {
45         return *(__u16 *) (map->map_priv_1 + ofs);
46 }
47
48 /*
49  * The IXP4xx expansion bus only allows 16-bit wide acceses
50  * when attached to a 16-bit wide device (such as the 28F128J3A),
51  * so we can't just memcpy_fromio().
52  */
53 static void
54 ixp4xx_copy_from(struct map_info *map, void *to,
55                  unsigned long from, ssize_t len)
56 {
57         int i;
58         u8 *dest = (u8 *) to;
59         u16 *src = (u16 *) (map->map_priv_1 + from);
60         u16 data;
61
62         for (i = 0; i < (len / 2); i++) {
63                 data = src[i];
64                 dest[i * 2] = BYTE0(data);
65                 dest[i * 2 + 1] = BYTE1(data);
66         }
67
68         if (len & 1)
69                 dest[len - 1] = BYTE0(src[i]);
70 }
71
72 static void
73 ixp4xx_write16(struct map_info *map, __u16 d, unsigned long adr)
74 {
75         *(__u16 *) (map->map_priv_1 + adr) = d;
76 }
77
78 struct ixp4xx_flash_info {
79         struct mtd_info *mtd;
80         struct map_info map;
81         struct mtd_partition *partitions;
82         struct resource *res;
83 };
84
85 static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
86
87 static int
88 ixp4xx_flash_remove(struct device *_dev)
89 {
90         struct platform_device *dev = to_platform_device(_dev);
91         struct flash_platform_data *plat = dev->dev.platform_data;
92         struct ixp4xx_flash_info *info = dev_get_drvdata(&dev->dev);
93
94         dev_set_drvdata(&dev->dev, NULL);
95
96         if(!info)
97                 return 0;
98
99         /*
100          * This is required for a soft reboot to work.
101          */
102         ixp4xx_write16(&info->map, 0xff, 0x55 * 0x2);
103
104         if (info->mtd) {
105                 del_mtd_partitions(info->mtd);
106                 map_destroy(info->mtd);
107         }
108         if (info->map.map_priv_1)
109                 iounmap((void *) info->map.map_priv_1);
110
111         if (info->partitions)
112                 kfree(info->partitions);
113
114         if (info->res) {
115                 release_resource(info->res);
116                 kfree(info->res);
117         }
118
119         if (plat->exit)
120                 plat->exit();
121
122         /* Disable flash write */
123         *IXP4XX_EXP_CS0 &= ~IXP4XX_FLASH_WRITABLE;
124
125         return 0;
126 }
127
128 static int ixp4xx_flash_probe(struct device *_dev)
129 {
130         struct platform_device *dev = to_platform_device(_dev);
131         struct flash_platform_data *plat = dev->dev.platform_data;
132         struct ixp4xx_flash_info *info;
133         int err = -1;
134
135         if (!plat)
136                 return -ENODEV;
137
138         if (plat->init) {
139                 err = plat->init();
140                 if (err)
141                         return err;
142         }
143
144         info = kmalloc(sizeof(struct ixp4xx_flash_info), GFP_KERNEL);
145         if(!info) {
146                 err = -ENOMEM;
147                 goto Error;
148         }       
149         memzero(info, sizeof(struct ixp4xx_flash_info));
150
151         dev_set_drvdata(&dev->dev, info);
152
153         /* 
154          * Enable flash write 
155          * TODO: Move this out to board specific code
156          */
157         *IXP4XX_EXP_CS0 |= IXP4XX_FLASH_WRITABLE;
158
159         /*
160          * Tell the MTD layer we're not 1:1 mapped so that it does
161          * not attempt to do a direct access on us.
162          */
163         info->map.phys = NO_XIP;
164         info->map.size = dev->resource->end - dev->resource->start + 1;
165
166         /*
167          * We only support 16-bit accesses for now. If and when
168          * any board use 8-bit access, we'll fixup the driver to
169          * handle that.
170          */
171         info->map.buswidth = 2;
172         info->map.name = dev->dev.bus_id;
173         info->map.read16 = ixp4xx_read16,
174         info->map.write16 = ixp4xx_write16,
175         info->map.copy_from = ixp4xx_copy_from,
176
177         info->res = request_mem_region(dev->resource->start, 
178                         dev->resource->end - dev->resource->start + 1, 
179                         "IXP4XXFlash");
180         if (!info->res) {
181                 printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n");
182                 err = -ENOMEM;
183                 goto Error;
184         }
185
186         info->map.map_priv_1 =
187             (unsigned long) ioremap(dev->resource->start, 
188                                     dev->resource->end - dev->resource->start + 1);
189         if (!info->map.map_priv_1) {
190                 printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n");
191                 err = -EIO;
192                 goto Error;
193         }
194
195         info->mtd = do_map_probe(plat->map_name, &info->map);
196         if (!info->mtd) {
197                 printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
198                 err = -ENXIO;
199                 goto Error;
200         }
201         info->mtd->owner = THIS_MODULE;
202
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");
208         }
209
210         if (err)
211                 goto Error;
212
213         return 0;
214
215 Error:
216         ixp4xx_flash_remove(_dev);
217         return err;
218 }
219
220 static struct device_driver ixp4xx_flash_driver = {
221         .name           = "IXP4XX-Flash",
222         .bus            = &platform_bus_type,
223         .probe          = ixp4xx_flash_probe,
224         .remove         = ixp4xx_flash_remove,
225 };
226
227 static int __init ixp4xx_flash_init(void)
228 {
229         return driver_register(&ixp4xx_flash_driver);
230 }
231
232 static void __exit ixp4xx_flash_exit(void)
233 {
234         driver_unregister(&ixp4xx_flash_driver);
235 }
236
237
238 module_init(ixp4xx_flash_init);
239 module_exit(ixp4xx_flash_exit);
240
241 MODULE_LICENSE("GPL");
242 MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems")
243 MODULE_AUTHOR("Deepak Saxena");
244