blob: 1594a802631d439f008c58b601e5c29ab6d14a21 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * drivers/mtd/maps/ixp2000.c
3 *
4 * Mapping for the Intel XScale IXP2000 based systems
5 *
6 * Copyright (C) 2002 Intel Corp.
7 * Copyright (C) 2003-2004 MontaVista Software, Inc.
8 *
9 * Original Author: Naeem M Afzal <naeem.m.afzal@intel.com>
10 * Maintainer: Deepak Saxena <dsaxena@plexity.net>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
Thomas Gleixner69f34c92005-11-07 11:15:40 +000015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/mach/flash.h>
35
36#include <linux/reboot.h>
37
38struct ixp2000_flash_info {
39 struct mtd_info *mtd;
40 struct map_info map;
41 struct mtd_partition *partitions;
42 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
45static inline unsigned long flash_bank_setup(struct map_info *map, unsigned long ofs)
Thomas Gleixner69f34c92005-11-07 11:15:40 +000046{
47 unsigned long (*set_bank)(unsigned long) =
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 (unsigned long(*)(unsigned long))map->map_priv_2;
49
50 return (set_bank ? set_bank(ofs) : ofs);
51}
52
53#ifdef __ARMEB__
54/*
Thomas Gleixner69f34c92005-11-07 11:15:40 +000055 * Rev A0 and A1 of IXP2400 silicon have a broken addressing unit which
56 * causes the lower address bits to be XORed with 0x11 on 8 bit accesses
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * and XORed with 0x10 on 16 bit accesses. See the spec update, erratum 44.
58 */
59static int erratum44_workaround = 0;
60
61static inline unsigned long address_fix8_write(unsigned long addr)
62{
63 if (erratum44_workaround) {
64 return (addr ^ 3);
65 }
66 return addr;
67}
68#else
69
70#define address_fix8_write(x) (x)
71#endif
72
73static map_word ixp2000_flash_read8(struct map_info *map, unsigned long ofs)
74{
75 map_word val;
76
77 val.x[0] = *((u8 *)(map->map_priv_1 + flash_bank_setup(map, ofs)));
78 return val;
79}
80
81/*
82 * We can't use the standard memcpy due to the broken SlowPort
83 * address translation on rev A0 and A1 silicon and the fact that
84 * we have banked flash.
85 */
86static void ixp2000_flash_copy_from(struct map_info *map, void *to,
87 unsigned long from, ssize_t len)
88{
89 from = flash_bank_setup(map, from);
Thomas Gleixner69f34c92005-11-07 11:15:40 +000090 while(len--)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *(__u8 *) to++ = *(__u8 *)(map->map_priv_1 + from++);
92}
93
94static void ixp2000_flash_write8(struct map_info *map, map_word d, unsigned long ofs)
95{
96 *(__u8 *) (address_fix8_write(map->map_priv_1 +
97 flash_bank_setup(map, ofs))) = d.x[0];
98}
99
100static void ixp2000_flash_copy_to(struct map_info *map, unsigned long to,
101 const void *from, ssize_t len)
102{
103 to = flash_bank_setup(map, to);
104 while(len--) {
105 unsigned long tmp = address_fix8_write(map->map_priv_1 + to++);
106 *(__u8 *)(tmp) = *(__u8 *)(from++);
107 }
108}
109
110
Russell King3ae5eae2005-11-09 22:32:44 +0000111static int ixp2000_flash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct flash_platform_data *plat = dev->dev.platform_data;
Russell King3ae5eae2005-11-09 22:32:44 +0000114 struct ixp2000_flash_info *info = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Russell King3ae5eae2005-11-09 22:32:44 +0000116 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if(!info)
119 return 0;
120
121 if (info->mtd) {
Jamie Iles6cbbf2f2011-05-23 10:22:49 +0100122 mtd_device_unregister(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 map_destroy(info->mtd);
124 }
125 if (info->map.map_priv_1)
126 iounmap((void *) info->map.map_priv_1);
127
Jesper Juhlfa671642005-11-07 01:01:27 -0800128 kfree(info->partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 if (info->res) {
131 release_resource(info->res);
132 kfree(info->res);
133 }
134
135 if (plat->exit)
136 plat->exit();
137
138 return 0;
139}
140
141
Russell King3ae5eae2005-11-09 22:32:44 +0000142static int ixp2000_flash_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct ixp2000_flash_data *ixp_data = dev->dev.platform_data;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000146 struct flash_platform_data *plat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct ixp2000_flash_info *info;
148 unsigned long window_size;
149 int err = -1;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!ixp_data)
152 return -ENODEV;
153
154 plat = ixp_data->platform_data;
155 if (!plat)
156 return -ENODEV;
157
Joe Perches28f65c112011-06-09 09:13:32 -0700158 window_size = resource_size(dev->resource);
Russell King7d78c882005-11-17 15:47:30 +0000159 dev_info(&dev->dev, "Probe of IXP2000 flash(%d banks x %dMiB)\n",
160 ixp_data->nr_banks, ((u32)window_size >> 20));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (plat->width != 1) {
Russell King7d78c882005-11-17 15:47:30 +0000163 dev_err(&dev->dev, "IXP2000 MTD map only supports 8-bit mode, asking for %d\n",
164 plat->width * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -EIO;
166 }
167
Julia Lawall2bfefa42010-05-13 22:03:15 +0200168 info = kzalloc(sizeof(struct ixp2000_flash_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if(!info) {
170 err = -ENOMEM;
171 goto Error;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Russell King3ae5eae2005-11-09 22:32:44 +0000174 platform_set_drvdata(dev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /*
177 * Tell the MTD layer we're not 1:1 mapped so that it does
178 * not attempt to do a direct access on us.
179 */
180 info->map.phys = NO_XIP;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 info->map.size = ixp_data->nr_banks * window_size;
183 info->map.bankwidth = 1;
184
185 /*
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200186 * map_priv_2 is used to store a ptr to the bank_setup routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Lennert Buytenhekea176292005-11-07 08:09:05 +0000188 info->map.map_priv_2 = (unsigned long) ixp_data->bank_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Kay Sievers475b44c2009-01-06 10:44:38 -0800190 info->map.name = dev_name(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 info->map.read = ixp2000_flash_read8;
192 info->map.write = ixp2000_flash_write8;
193 info->map.copy_from = ixp2000_flash_copy_from;
194 info->map.copy_to = ixp2000_flash_copy_to;
195
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000196 info->res = request_mem_region(dev->resource->start,
Joe Perches28f65c112011-06-09 09:13:32 -0700197 resource_size(dev->resource),
198 dev_name(&dev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!info->res) {
Russell King7d78c882005-11-17 15:47:30 +0000200 dev_err(&dev->dev, "Could not reserve memory region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 err = -ENOMEM;
202 goto Error;
203 }
204
Joe Perches28f65c112011-06-09 09:13:32 -0700205 info->map.map_priv_1 =
206 (unsigned long)ioremap(dev->resource->start,
207 resource_size(dev->resource));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!info->map.map_priv_1) {
Russell King7d78c882005-11-17 15:47:30 +0000209 dev_err(&dev->dev, "Failed to ioremap flash region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 err = -EIO;
211 goto Error;
212 }
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#if defined(__ARMEB__)
215 /*
216 * Enable erratum 44 workaround for NPUs with broken slowport
217 */
218
219 erratum44_workaround = ixp2000_has_broken_slowport();
Russell King7d78c882005-11-17 15:47:30 +0000220 dev_info(&dev->dev, "Erratum 44 workaround %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 erratum44_workaround ? "enabled" : "disabled");
222#endif
223
224 info->mtd = do_map_probe(plat->map_name, &info->map);
225 if (!info->mtd) {
Russell King7d78c882005-11-17 15:47:30 +0000226 dev_err(&dev->dev, "map_probe failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 err = -ENXIO;
228 goto Error;
229 }
230 info->mtd->owner = THIS_MODULE;
231
232 err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
233 if (err > 0) {
Jamie Iles6cbbf2f2011-05-23 10:22:49 +0100234 err = mtd_device_register(info->mtd, info->partitions, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if(err)
Russell King7d78c882005-11-17 15:47:30 +0000236 dev_err(&dev->dev, "Could not parse partitions\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
239 if (err)
240 goto Error;
241
242 return 0;
243
244Error:
Russell King3ae5eae2005-11-09 22:32:44 +0000245 ixp2000_flash_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return err;
247}
248
Russell King3ae5eae2005-11-09 22:32:44 +0000249static struct platform_driver ixp2000_flash_driver = {
Russell King7d78c882005-11-17 15:47:30 +0000250 .probe = ixp2000_flash_probe,
251 .remove = ixp2000_flash_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000252 .driver = {
253 .name = "IXP2000-Flash",
Kay Sievers41d867c2008-04-18 13:44:26 -0700254 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +0000255 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
258static int __init ixp2000_flash_init(void)
259{
Russell King3ae5eae2005-11-09 22:32:44 +0000260 return platform_driver_register(&ixp2000_flash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263static void __exit ixp2000_flash_exit(void)
264{
Russell King3ae5eae2005-11-09 22:32:44 +0000265 platform_driver_unregister(&ixp2000_flash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268module_init(ixp2000_flash_init);
269module_exit(ixp2000_flash_exit);
270MODULE_LICENSE("GPL");
271MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
Kay Sievers41d867c2008-04-18 13:44:26 -0700272MODULE_ALIAS("platform:IXP2000-Flash");