blob: a59f8027903c5d632975bbb28e23ad91f369b35f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Thomas Gleixner69f34c92005-11-07 11:15:40 +00002 * $Id: ixp4xx.c,v 1.12 2005/11/07 11:14:27 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <linux/slab.h>
24#include <linux/ioport.h>
25#include <linux/device.h>
Linus Torvalds4fd5f822005-10-31 07:32:56 -080026#include <linux/platform_device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mtd/mtd.h>
29#include <linux/mtd/map.h>
30#include <linux/mtd/partitions.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/mach/flash.h>
34
35#include <linux/reboot.h>
36
37#ifndef __ARMEB__
38#define BYTE0(h) ((h) & 0xFF)
39#define BYTE1(h) (((h) >> 8) & 0xFF)
40#else
41#define BYTE0(h) (((h) >> 8) & 0xFF)
42#define BYTE1(h) ((h) & 0xFF)
43#endif
44
45static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
46{
47 map_word val;
David Vrabel9090ed02005-11-01 16:46:19 +000048 val.x[0] = le16_to_cpu(readw(map->virt + ofs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return val;
50}
51
52/*
53 * The IXP4xx expansion bus only allows 16-bit wide acceses
54 * when attached to a 16-bit wide device (such as the 28F128J3A),
55 * so we can't just memcpy_fromio().
56 */
57static void ixp4xx_copy_from(struct map_info *map, void *to,
58 unsigned long from, ssize_t len)
59{
60 int i;
61 u8 *dest = (u8 *) to;
David Vrabel9090ed02005-11-01 16:46:19 +000062 void __iomem *src = map->virt + from;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 u16 data;
64
65 for (i = 0; i < (len / 2); i++) {
David Vrabel9090ed02005-11-01 16:46:19 +000066 data = le16_to_cpu(readw(src + 2*i));
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 dest[i * 2] = BYTE0(data);
68 dest[i * 2 + 1] = BYTE1(data);
69 }
70
71 if (len & 1)
David Vrabel9090ed02005-11-01 16:46:19 +000072 dest[len - 1] = BYTE0(le16_to_cpu(readw(src + 2*i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Thomas Gleixner69f34c92005-11-07 11:15:40 +000075/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * Unaligned writes are ignored, causing the 8-bit
77 * probe to fail and proceed to the 16-bit probe (which succeeds).
78 */
79static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr)
80{
81 if (!(adr & 1))
David Vrabel9090ed02005-11-01 16:46:19 +000082 writew(cpu_to_le16(d.x[0]), map->virt + adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Thomas Gleixner69f34c92005-11-07 11:15:40 +000085/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * Fast write16 function without the probing check above
87 */
88static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
89{
David Vrabel9090ed02005-11-01 16:46:19 +000090 writew(cpu_to_le16(d.x[0]), map->virt + adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93struct ixp4xx_flash_info {
94 struct mtd_info *mtd;
95 struct map_info map;
96 struct mtd_partition *partitions;
97 struct resource *res;
98};
99
100static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
101
Russell King3ae5eae2005-11-09 22:32:44 +0000102static int ixp4xx_flash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct flash_platform_data *plat = dev->dev.platform_data;
Russell King3ae5eae2005-11-09 22:32:44 +0000105 struct ixp4xx_flash_info *info = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Russell King3ae5eae2005-11-09 22:32:44 +0000107 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if(!info)
110 return 0;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (info->mtd) {
113 del_mtd_partitions(info->mtd);
114 map_destroy(info->mtd);
115 }
David Vrabel9090ed02005-11-01 16:46:19 +0000116 if (info->map.virt)
117 iounmap(info->map.virt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jesper Juhlfa671642005-11-07 01:01:27 -0800119 kfree(info->partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (info->res) {
122 release_resource(info->res);
123 kfree(info->res);
124 }
125
126 if (plat->exit)
127 plat->exit();
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130}
131
Russell King3ae5eae2005-11-09 22:32:44 +0000132static int ixp4xx_flash_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct flash_platform_data *plat = dev->dev.platform_data;
135 struct ixp4xx_flash_info *info;
136 int err = -1;
137
138 if (!plat)
139 return -ENODEV;
140
141 if (plat->init) {
142 err = plat->init();
143 if (err)
144 return err;
145 }
146
147 info = kmalloc(sizeof(struct ixp4xx_flash_info), GFP_KERNEL);
148 if(!info) {
149 err = -ENOMEM;
150 goto Error;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 memzero(info, sizeof(struct ixp4xx_flash_info));
153
Russell King3ae5eae2005-11-09 22:32:44 +0000154 platform_set_drvdata(dev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /*
157 * Tell the MTD layer we're not 1:1 mapped so that it does
158 * not attempt to do a direct access on us.
159 */
160 info->map.phys = NO_XIP;
161 info->map.size = dev->resource->end - dev->resource->start + 1;
162
163 /*
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.
167 */
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,
173
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000174 info->res = request_mem_region(dev->resource->start,
175 dev->resource->end - dev->resource->start + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 "IXP4XXFlash");
177 if (!info->res) {
178 printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n");
179 err = -ENOMEM;
180 goto Error;
181 }
182
David Vrabel9090ed02005-11-01 16:46:19 +0000183 info->map.virt = ioremap(dev->resource->start,
184 dev->resource->end - dev->resource->start + 1);
185 if (!info->map.virt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n");
187 err = -EIO;
188 goto Error;
189 }
190
191 info->mtd = do_map_probe(plat->map_name, &info->map);
192 if (!info->mtd) {
193 printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
194 err = -ENXIO;
195 goto Error;
196 }
197 info->mtd->owner = THIS_MODULE;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Use the fast version */
200 info->map.write = ixp4xx_write16,
201
202 err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
203 if (err > 0) {
204 err = add_mtd_partitions(info->mtd, info->partitions, err);
205 if(err)
206 printk(KERN_ERR "Could not parse partitions\n");
207 }
208
209 if (err)
210 goto Error;
211
212 return 0;
213
214Error:
Russell King3ae5eae2005-11-09 22:32:44 +0000215 ixp4xx_flash_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return err;
217}
218
Russell King3ae5eae2005-11-09 22:32:44 +0000219static struct platform_driver ixp4xx_flash_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .probe = ixp4xx_flash_probe,
221 .remove = ixp4xx_flash_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000222 .driver = {
223 .name = "IXP4XX-Flash",
224 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
227static int __init ixp4xx_flash_init(void)
228{
Russell King3ae5eae2005-11-09 22:32:44 +0000229 return platform_driver_register(&ixp4xx_flash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232static void __exit ixp4xx_flash_exit(void)
233{
Russell King3ae5eae2005-11-09 22:32:44 +0000234 platform_driver_unregister(&ixp4xx_flash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237
238module_init(ixp4xx_flash_init);
239module_exit(ixp4xx_flash_exit);
240
241MODULE_LICENSE("GPL");
Deepak Saxena82810a92005-09-28 16:42:54 -0700242MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243MODULE_AUTHOR("Deepak Saxena");