Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 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 Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 15 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/device.h> |
Linus Torvalds | 4fd5f82 | 2005-10-31 07:32:56 -0800 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/mtd/mtd.h> |
| 29 | #include <linux/mtd/map.h> |
| 30 | #include <linux/mtd/partitions.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <asm/io.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 33 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/mach/flash.h> |
| 35 | |
| 36 | #include <linux/reboot.h> |
| 37 | |
| 38 | struct ixp2000_flash_info { |
| 39 | struct mtd_info *mtd; |
| 40 | struct map_info map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static inline unsigned long flash_bank_setup(struct map_info *map, unsigned long ofs) |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 45 | { |
| 46 | unsigned long (*set_bank)(unsigned long) = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | (unsigned long(*)(unsigned long))map->map_priv_2; |
| 48 | |
| 49 | return (set_bank ? set_bank(ofs) : ofs); |
| 50 | } |
| 51 | |
| 52 | #ifdef __ARMEB__ |
| 53 | /* |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 54 | * Rev A0 and A1 of IXP2400 silicon have a broken addressing unit which |
| 55 | * causes the lower address bits to be XORed with 0x11 on 8 bit accesses |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * and XORed with 0x10 on 16 bit accesses. See the spec update, erratum 44. |
| 57 | */ |
| 58 | static int erratum44_workaround = 0; |
| 59 | |
| 60 | static inline unsigned long address_fix8_write(unsigned long addr) |
| 61 | { |
| 62 | if (erratum44_workaround) { |
| 63 | return (addr ^ 3); |
| 64 | } |
| 65 | return addr; |
| 66 | } |
| 67 | #else |
| 68 | |
| 69 | #define address_fix8_write(x) (x) |
| 70 | #endif |
| 71 | |
| 72 | static map_word ixp2000_flash_read8(struct map_info *map, unsigned long ofs) |
| 73 | { |
| 74 | map_word val; |
| 75 | |
| 76 | val.x[0] = *((u8 *)(map->map_priv_1 + flash_bank_setup(map, ofs))); |
| 77 | return val; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * We can't use the standard memcpy due to the broken SlowPort |
| 82 | * address translation on rev A0 and A1 silicon and the fact that |
| 83 | * we have banked flash. |
| 84 | */ |
| 85 | static void ixp2000_flash_copy_from(struct map_info *map, void *to, |
| 86 | unsigned long from, ssize_t len) |
| 87 | { |
| 88 | from = flash_bank_setup(map, from); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 89 | while(len--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | *(__u8 *) to++ = *(__u8 *)(map->map_priv_1 + from++); |
| 91 | } |
| 92 | |
| 93 | static void ixp2000_flash_write8(struct map_info *map, map_word d, unsigned long ofs) |
| 94 | { |
| 95 | *(__u8 *) (address_fix8_write(map->map_priv_1 + |
| 96 | flash_bank_setup(map, ofs))) = d.x[0]; |
| 97 | } |
| 98 | |
| 99 | static void ixp2000_flash_copy_to(struct map_info *map, unsigned long to, |
| 100 | const void *from, ssize_t len) |
| 101 | { |
| 102 | to = flash_bank_setup(map, to); |
| 103 | while(len--) { |
| 104 | unsigned long tmp = address_fix8_write(map->map_priv_1 + to++); |
| 105 | *(__u8 *)(tmp) = *(__u8 *)(from++); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 110 | static int ixp2000_flash_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | struct flash_platform_data *plat = dev->dev.platform_data; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 113 | struct ixp2000_flash_info *info = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 115 | platform_set_drvdata(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | if(!info) |
| 118 | return 0; |
| 119 | |
| 120 | if (info->mtd) { |
Jamie Iles | 6cbbf2f | 2011-05-23 10:22:49 +0100 | [diff] [blame] | 121 | mtd_device_unregister(info->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | map_destroy(info->mtd); |
| 123 | } |
| 124 | if (info->map.map_priv_1) |
| 125 | iounmap((void *) info->map.map_priv_1); |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (info->res) { |
| 128 | release_resource(info->res); |
| 129 | kfree(info->res); |
| 130 | } |
| 131 | |
| 132 | if (plat->exit) |
| 133 | plat->exit(); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 139 | static int ixp2000_flash_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | static const char *probes[] = { "RedBoot", "cmdlinepart", NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | struct ixp2000_flash_data *ixp_data = dev->dev.platform_data; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 143 | struct flash_platform_data *plat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | struct ixp2000_flash_info *info; |
| 145 | unsigned long window_size; |
| 146 | int err = -1; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (!ixp_data) |
| 149 | return -ENODEV; |
| 150 | |
| 151 | plat = ixp_data->platform_data; |
| 152 | if (!plat) |
| 153 | return -ENODEV; |
| 154 | |
Joe Perches | 28f65c1 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 155 | window_size = resource_size(dev->resource); |
Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 156 | dev_info(&dev->dev, "Probe of IXP2000 flash(%d banks x %dMiB)\n", |
| 157 | ixp_data->nr_banks, ((u32)window_size >> 20)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | if (plat->width != 1) { |
Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 160 | dev_err(&dev->dev, "IXP2000 MTD map only supports 8-bit mode, asking for %d\n", |
| 161 | plat->width * 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return -EIO; |
| 163 | } |
| 164 | |
Julia Lawall | 2bfefa4 | 2010-05-13 22:03:15 +0200 | [diff] [blame] | 165 | info = kzalloc(sizeof(struct ixp2000_flash_info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if(!info) { |
| 167 | err = -ENOMEM; |
| 168 | goto Error; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 171 | platform_set_drvdata(dev, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Tell the MTD layer we're not 1:1 mapped so that it does |
| 175 | * not attempt to do a direct access on us. |
| 176 | */ |
| 177 | info->map.phys = NO_XIP; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | info->map.size = ixp_data->nr_banks * window_size; |
| 180 | info->map.bankwidth = 1; |
| 181 | |
| 182 | /* |
Anand Gadiyar | fd589a8 | 2009-07-16 17:13:03 +0200 | [diff] [blame] | 183 | * map_priv_2 is used to store a ptr to the bank_setup routine |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | */ |
Lennert Buytenhek | ea17629 | 2005-11-07 08:09:05 +0000 | [diff] [blame] | 185 | info->map.map_priv_2 = (unsigned long) ixp_data->bank_setup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 187 | info->map.name = dev_name(&dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | info->map.read = ixp2000_flash_read8; |
| 189 | info->map.write = ixp2000_flash_write8; |
| 190 | info->map.copy_from = ixp2000_flash_copy_from; |
| 191 | info->map.copy_to = ixp2000_flash_copy_to; |
| 192 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 193 | info->res = request_mem_region(dev->resource->start, |
Joe Perches | 28f65c1 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 194 | resource_size(dev->resource), |
| 195 | dev_name(&dev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (!info->res) { |
Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 197 | dev_err(&dev->dev, "Could not reserve memory region\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | err = -ENOMEM; |
| 199 | goto Error; |
| 200 | } |
| 201 | |
Joe Perches | 28f65c1 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 202 | info->map.map_priv_1 = |
| 203 | (unsigned long)ioremap(dev->resource->start, |
| 204 | resource_size(dev->resource)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if (!info->map.map_priv_1) { |
Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 206 | dev_err(&dev->dev, "Failed to ioremap flash region\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | err = -EIO; |
| 208 | goto Error; |
| 209 | } |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | #if defined(__ARMEB__) |
| 212 | /* |
| 213 | * Enable erratum 44 workaround for NPUs with broken slowport |
| 214 | */ |
| 215 | |
| 216 | erratum44_workaround = ixp2000_has_broken_slowport(); |
Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 217 | dev_info(&dev->dev, "Erratum 44 workaround %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | erratum44_workaround ? "enabled" : "disabled"); |
| 219 | #endif |
| 220 | |
| 221 | info->mtd = do_map_probe(plat->map_name, &info->map); |
| 222 | if (!info->mtd) { |
Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 223 | dev_err(&dev->dev, "map_probe failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | err = -ENXIO; |
| 225 | goto Error; |
| 226 | } |
| 227 | info->mtd->owner = THIS_MODULE; |
| 228 | |
Dmitry Eremin-Solenikov | 200b877 | 2011-06-02 17:59:46 +0400 | [diff] [blame] | 229 | err = mtd_device_parse_register(info->mtd, probes, 0, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | if (err) |
| 231 | goto Error; |
| 232 | |
| 233 | return 0; |
| 234 | |
| 235 | Error: |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 236 | ixp2000_flash_remove(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return err; |
| 238 | } |
| 239 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 240 | static struct platform_driver ixp2000_flash_driver = { |
Russell King | 7d78c88 | 2005-11-17 15:47:30 +0000 | [diff] [blame] | 241 | .probe = ixp2000_flash_probe, |
| 242 | .remove = ixp2000_flash_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 243 | .driver = { |
| 244 | .name = "IXP2000-Flash", |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 245 | .owner = THIS_MODULE, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 246 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 249 | module_platform_driver(ixp2000_flash_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | MODULE_LICENSE("GPL"); |
| 252 | MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>"); |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 253 | MODULE_ALIAS("platform:IXP2000-Flash"); |