Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 2 | * $Id: ixp2000.c,v 1.9 2005/11/07 11:14:27 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * drivers/mtd/maps/ixp2000.c |
| 5 | * |
| 6 | * Mapping for the Intel XScale IXP2000 based systems |
| 7 | * |
| 8 | * Copyright (C) 2002 Intel Corp. |
| 9 | * Copyright (C) 2003-2004 MontaVista Software, Inc. |
| 10 | * |
| 11 | * Original Author: Naeem M Afzal <naeem.m.afzal@intel.com> |
| 12 | * Maintainer: Deepak Saxena <dsaxena@plexity.net> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/string.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/ioport.h> |
| 27 | #include <linux/device.h> |
Linus Torvalds | 4fd5f82 | 2005-10-31 07:32:56 -0800 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/mtd/mtd.h> |
| 31 | #include <linux/mtd/map.h> |
| 32 | #include <linux/mtd/partitions.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <asm/io.h> |
| 35 | #include <asm/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/mach/flash.h> |
| 37 | |
| 38 | #include <linux/reboot.h> |
| 39 | |
| 40 | struct ixp2000_flash_info { |
| 41 | struct mtd_info *mtd; |
| 42 | struct map_info map; |
| 43 | struct mtd_partition *partitions; |
| 44 | struct resource *res; |
| 45 | int nr_banks; |
| 46 | }; |
| 47 | |
| 48 | 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^] | 49 | { |
| 50 | unsigned long (*set_bank)(unsigned long) = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | (unsigned long(*)(unsigned long))map->map_priv_2; |
| 52 | |
| 53 | return (set_bank ? set_bank(ofs) : ofs); |
| 54 | } |
| 55 | |
| 56 | #ifdef __ARMEB__ |
| 57 | /* |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 58 | * Rev A0 and A1 of IXP2400 silicon have a broken addressing unit which |
| 59 | * 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] | 60 | * and XORed with 0x10 on 16 bit accesses. See the spec update, erratum 44. |
| 61 | */ |
| 62 | static int erratum44_workaround = 0; |
| 63 | |
| 64 | static inline unsigned long address_fix8_write(unsigned long addr) |
| 65 | { |
| 66 | if (erratum44_workaround) { |
| 67 | return (addr ^ 3); |
| 68 | } |
| 69 | return addr; |
| 70 | } |
| 71 | #else |
| 72 | |
| 73 | #define address_fix8_write(x) (x) |
| 74 | #endif |
| 75 | |
| 76 | static map_word ixp2000_flash_read8(struct map_info *map, unsigned long ofs) |
| 77 | { |
| 78 | map_word val; |
| 79 | |
| 80 | val.x[0] = *((u8 *)(map->map_priv_1 + flash_bank_setup(map, ofs))); |
| 81 | return val; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * We can't use the standard memcpy due to the broken SlowPort |
| 86 | * address translation on rev A0 and A1 silicon and the fact that |
| 87 | * we have banked flash. |
| 88 | */ |
| 89 | static void ixp2000_flash_copy_from(struct map_info *map, void *to, |
| 90 | unsigned long from, ssize_t len) |
| 91 | { |
| 92 | from = flash_bank_setup(map, from); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 93 | while(len--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | *(__u8 *) to++ = *(__u8 *)(map->map_priv_1 + from++); |
| 95 | } |
| 96 | |
| 97 | static void ixp2000_flash_write8(struct map_info *map, map_word d, unsigned long ofs) |
| 98 | { |
| 99 | *(__u8 *) (address_fix8_write(map->map_priv_1 + |
| 100 | flash_bank_setup(map, ofs))) = d.x[0]; |
| 101 | } |
| 102 | |
| 103 | static void ixp2000_flash_copy_to(struct map_info *map, unsigned long to, |
| 104 | const void *from, ssize_t len) |
| 105 | { |
| 106 | to = flash_bank_setup(map, to); |
| 107 | while(len--) { |
| 108 | unsigned long tmp = address_fix8_write(map->map_priv_1 + to++); |
| 109 | *(__u8 *)(tmp) = *(__u8 *)(from++); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static int ixp2000_flash_remove(struct device *_dev) |
| 115 | { |
| 116 | struct platform_device *dev = to_platform_device(_dev); |
| 117 | struct flash_platform_data *plat = dev->dev.platform_data; |
| 118 | struct ixp2000_flash_info *info = dev_get_drvdata(&dev->dev); |
| 119 | |
| 120 | dev_set_drvdata(&dev->dev, NULL); |
| 121 | |
| 122 | if(!info) |
| 123 | return 0; |
| 124 | |
| 125 | if (info->mtd) { |
| 126 | del_mtd_partitions(info->mtd); |
| 127 | map_destroy(info->mtd); |
| 128 | } |
| 129 | if (info->map.map_priv_1) |
| 130 | iounmap((void *) info->map.map_priv_1); |
| 131 | |
| 132 | if (info->partitions) { |
| 133 | kfree(info->partitions); } |
| 134 | |
| 135 | if (info->res) { |
| 136 | release_resource(info->res); |
| 137 | kfree(info->res); |
| 138 | } |
| 139 | |
| 140 | if (plat->exit) |
| 141 | plat->exit(); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | static int ixp2000_flash_probe(struct device *_dev) |
| 148 | { |
| 149 | static const char *probes[] = { "RedBoot", "cmdlinepart", NULL }; |
| 150 | struct platform_device *dev = to_platform_device(_dev); |
| 151 | struct ixp2000_flash_data *ixp_data = dev->dev.platform_data; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 152 | struct flash_platform_data *plat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | struct ixp2000_flash_info *info; |
| 154 | unsigned long window_size; |
| 155 | int err = -1; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (!ixp_data) |
| 158 | return -ENODEV; |
| 159 | |
| 160 | plat = ixp_data->platform_data; |
| 161 | if (!plat) |
| 162 | return -ENODEV; |
| 163 | |
| 164 | window_size = dev->resource->end - dev->resource->start + 1; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 165 | dev_info(_dev, "Probe of IXP2000 flash(%d banks x %dMiB)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | ixp_data->nr_banks, ((u32)window_size >> 20)); |
| 167 | |
| 168 | if (plat->width != 1) { |
| 169 | dev_err(_dev, "IXP2000 MTD map only supports 8-bit mode, asking for %d\n", |
| 170 | plat->width * 8); |
| 171 | return -EIO; |
| 172 | } |
| 173 | |
| 174 | info = kmalloc(sizeof(struct ixp2000_flash_info), GFP_KERNEL); |
| 175 | if(!info) { |
| 176 | err = -ENOMEM; |
| 177 | goto Error; |
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 | memzero(info, sizeof(struct ixp2000_flash_info)); |
| 180 | |
| 181 | dev_set_drvdata(&dev->dev, info); |
| 182 | |
| 183 | /* |
| 184 | * Tell the MTD layer we're not 1:1 mapped so that it does |
| 185 | * not attempt to do a direct access on us. |
| 186 | */ |
| 187 | info->map.phys = NO_XIP; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | info->nr_banks = ixp_data->nr_banks; |
| 190 | info->map.size = ixp_data->nr_banks * window_size; |
| 191 | info->map.bankwidth = 1; |
| 192 | |
| 193 | /* |
| 194 | * map_priv_2 is used to store a ptr to to the bank_setup routine |
| 195 | */ |
Lennert Buytenhek | ea17629 | 2005-11-07 08:09:05 +0000 | [diff] [blame] | 196 | info->map.map_priv_2 = (unsigned long) ixp_data->bank_setup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | info->map.name = dev->dev.bus_id; |
| 199 | info->map.read = ixp2000_flash_read8; |
| 200 | info->map.write = ixp2000_flash_write8; |
| 201 | info->map.copy_from = ixp2000_flash_copy_from; |
| 202 | info->map.copy_to = ixp2000_flash_copy_to; |
| 203 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 204 | info->res = request_mem_region(dev->resource->start, |
| 205 | dev->resource->end - dev->resource->start + 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | dev->dev.bus_id); |
| 207 | if (!info->res) { |
| 208 | dev_err(_dev, "Could not reserve memory region\n"); |
| 209 | err = -ENOMEM; |
| 210 | goto Error; |
| 211 | } |
| 212 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame^] | 213 | info->map.map_priv_1 = (unsigned long) ioremap(dev->resource->start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | dev->resource->end - dev->resource->start + 1); |
| 215 | if (!info->map.map_priv_1) { |
| 216 | dev_err(_dev, "Failed to ioremap flash region\n"); |
| 217 | err = -EIO; |
| 218 | goto Error; |
| 219 | } |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | #if defined(__ARMEB__) |
| 222 | /* |
| 223 | * Enable erratum 44 workaround for NPUs with broken slowport |
| 224 | */ |
| 225 | |
| 226 | erratum44_workaround = ixp2000_has_broken_slowport(); |
| 227 | dev_info(_dev, "Erratum 44 workaround %s\n", |
| 228 | erratum44_workaround ? "enabled" : "disabled"); |
| 229 | #endif |
| 230 | |
| 231 | info->mtd = do_map_probe(plat->map_name, &info->map); |
| 232 | if (!info->mtd) { |
| 233 | dev_err(_dev, "map_probe failed\n"); |
| 234 | err = -ENXIO; |
| 235 | goto Error; |
| 236 | } |
| 237 | info->mtd->owner = THIS_MODULE; |
| 238 | |
| 239 | err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0); |
| 240 | if (err > 0) { |
| 241 | err = add_mtd_partitions(info->mtd, info->partitions, err); |
| 242 | if(err) |
| 243 | dev_err(_dev, "Could not parse partitions\n"); |
| 244 | } |
| 245 | |
| 246 | if (err) |
| 247 | goto Error; |
| 248 | |
| 249 | return 0; |
| 250 | |
| 251 | Error: |
| 252 | ixp2000_flash_remove(_dev); |
| 253 | return err; |
| 254 | } |
| 255 | |
| 256 | static struct device_driver ixp2000_flash_driver = { |
| 257 | .name = "IXP2000-Flash", |
| 258 | .bus = &platform_bus_type, |
| 259 | .probe = &ixp2000_flash_probe, |
| 260 | .remove = &ixp2000_flash_remove |
| 261 | }; |
| 262 | |
| 263 | static int __init ixp2000_flash_init(void) |
| 264 | { |
| 265 | return driver_register(&ixp2000_flash_driver); |
| 266 | } |
| 267 | |
| 268 | static void __exit ixp2000_flash_exit(void) |
| 269 | { |
| 270 | driver_unregister(&ixp2000_flash_driver); |
| 271 | } |
| 272 | |
| 273 | module_init(ixp2000_flash_init); |
| 274 | module_exit(ixp2000_flash_exit); |
| 275 | MODULE_LICENSE("GPL"); |
| 276 | MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>"); |
| 277 | |