[PATCH] fix missing includes
[pandora-kernel.git] / drivers / mtd / maps / ixp4xx.c
1 /*
2  * $Id: ixp4xx.c,v 1.7 2004/11/04 13:24:15 gleixner 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/slab.h>
24 #include <linux/ioport.h>
25 #include <linux/device.h>
26
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/map.h>
29 #include <linux/mtd/partitions.h>
30
31 #include <asm/io.h>
32 #include <asm/mach/flash.h>
33
34 #include <linux/reboot.h>
35
36 #ifndef __ARMEB__
37 #define BYTE0(h)        ((h) & 0xFF)
38 #define BYTE1(h)        (((h) >> 8) & 0xFF)
39 #else
40 #define BYTE0(h)        (((h) >> 8) & 0xFF)
41 #define BYTE1(h)        ((h) & 0xFF)
42 #endif
43
44 static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
45 {
46         map_word val;
47         val.x[0] = *(__u16 *) (map->map_priv_1 + ofs);
48         return val;
49 }
50
51 /*
52  * The IXP4xx expansion bus only allows 16-bit wide acceses
53  * when attached to a 16-bit wide device (such as the 28F128J3A),
54  * so we can't just memcpy_fromio().
55  */
56 static void ixp4xx_copy_from(struct map_info *map, void *to,
57                              unsigned long from, ssize_t len)
58 {
59         int i;
60         u8 *dest = (u8 *) to;
61         u16 *src = (u16 *) (map->map_priv_1 + from);
62         u16 data;
63
64         for (i = 0; i < (len / 2); i++) {
65                 data = src[i];
66                 dest[i * 2] = BYTE0(data);
67                 dest[i * 2 + 1] = BYTE1(data);
68         }
69
70         if (len & 1)
71                 dest[len - 1] = BYTE0(src[i]);
72 }
73
74 /* 
75  * Unaligned writes are ignored, causing the 8-bit
76  * probe to fail and proceed to the 16-bit probe (which succeeds).
77  */
78 static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr)
79 {
80         if (!(adr & 1))
81                *(__u16 *) (map->map_priv_1 + adr) = d.x[0];
82 }
83
84 /* 
85  * Fast write16 function without the probing check above
86  */
87 static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
88 {
89        *(__u16 *) (map->map_priv_1 + adr) = d.x[0];
90 }
91
92 struct ixp4xx_flash_info {
93         struct mtd_info *mtd;
94         struct map_info map;
95         struct mtd_partition *partitions;
96         struct resource *res;
97 };
98
99 static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
100
101 static int ixp4xx_flash_remove(struct device *_dev)
102 {
103         struct platform_device *dev = to_platform_device(_dev);
104         struct flash_platform_data *plat = dev->dev.platform_data;
105         struct ixp4xx_flash_info *info = dev_get_drvdata(&dev->dev);
106         map_word d;
107
108         dev_set_drvdata(&dev->dev, NULL);
109
110         if(!info)
111                 return 0;
112
113         /*
114          * This is required for a soft reboot to work.
115          */
116         d.x[0] = 0xff;
117         ixp4xx_write16(&info->map, d, 0x55 * 0x2);
118
119         if (info->mtd) {
120                 del_mtd_partitions(info->mtd);
121                 map_destroy(info->mtd);
122         }
123         if (info->map.map_priv_1)
124                 iounmap((void *) info->map.map_priv_1);
125
126         if (info->partitions)
127                 kfree(info->partitions);
128
129         if (info->res) {
130                 release_resource(info->res);
131                 kfree(info->res);
132         }
133
134         if (plat->exit)
135                 plat->exit();
136
137         /* Disable flash write */
138         *IXP4XX_EXP_CS0 &= ~IXP4XX_FLASH_WRITABLE;
139
140         return 0;
141 }
142
143 static int ixp4xx_flash_probe(struct device *_dev)
144 {
145         struct platform_device *dev = to_platform_device(_dev);
146         struct flash_platform_data *plat = dev->dev.platform_data;
147         struct ixp4xx_flash_info *info;
148         int err = -1;
149
150         if (!plat)
151                 return -ENODEV;
152
153         if (plat->init) {
154                 err = plat->init();
155                 if (err)
156                         return err;
157         }
158
159         info = kmalloc(sizeof(struct ixp4xx_flash_info), GFP_KERNEL);
160         if(!info) {
161                 err = -ENOMEM;
162                 goto Error;
163         }       
164         memzero(info, sizeof(struct ixp4xx_flash_info));
165
166         dev_set_drvdata(&dev->dev, info);
167
168         /* 
169          * Enable flash write 
170          * TODO: Move this out to board specific code
171          */
172         *IXP4XX_EXP_CS0 |= IXP4XX_FLASH_WRITABLE;
173
174         /*
175          * Tell the MTD layer we're not 1:1 mapped so that it does
176          * not attempt to do a direct access on us.
177          */
178         info->map.phys = NO_XIP;
179         info->map.size = dev->resource->end - dev->resource->start + 1;
180
181         /*
182          * We only support 16-bit accesses for now. If and when
183          * any board use 8-bit access, we'll fixup the driver to
184          * handle that.
185          */
186         info->map.bankwidth = 2;
187         info->map.name = dev->dev.bus_id;
188         info->map.read = ixp4xx_read16,
189         info->map.write = ixp4xx_probe_write16,
190         info->map.copy_from = ixp4xx_copy_from,
191
192         info->res = request_mem_region(dev->resource->start, 
193                         dev->resource->end - dev->resource->start + 1, 
194                         "IXP4XXFlash");
195         if (!info->res) {
196                 printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n");
197                 err = -ENOMEM;
198                 goto Error;
199         }
200
201         info->map.map_priv_1 = ioremap(dev->resource->start,
202                             dev->resource->end - dev->resource->start + 1);
203         if (!info->map.map_priv_1) {
204                 printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n");
205                 err = -EIO;
206                 goto Error;
207         }
208
209         info->mtd = do_map_probe(plat->map_name, &info->map);
210         if (!info->mtd) {
211                 printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
212                 err = -ENXIO;
213                 goto Error;
214         }
215         info->mtd->owner = THIS_MODULE;
216         
217         /* Use the fast version */
218         info->map.write = ixp4xx_write16,
219
220         err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
221         if (err > 0) {
222                 err = add_mtd_partitions(info->mtd, info->partitions, err);
223                 if(err)
224                         printk(KERN_ERR "Could not parse partitions\n");
225         }
226
227         if (err)
228                 goto Error;
229
230         return 0;
231
232 Error:
233         ixp4xx_flash_remove(_dev);
234         return err;
235 }
236
237 static struct device_driver ixp4xx_flash_driver = {
238         .name           = "IXP4XX-Flash",
239         .bus            = &platform_bus_type,
240         .probe          = ixp4xx_flash_probe,
241         .remove         = ixp4xx_flash_remove,
242 };
243
244 static int __init ixp4xx_flash_init(void)
245 {
246         return driver_register(&ixp4xx_flash_driver);
247 }
248
249 static void __exit ixp4xx_flash_exit(void)
250 {
251         driver_unregister(&ixp4xx_flash_driver);
252 }
253
254
255 module_init(ixp4xx_flash_init);
256 module_exit(ixp4xx_flash_exit);
257
258 MODULE_LICENSE("GPL");
259 MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
260 MODULE_AUTHOR("Deepak Saxena");
261