Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 2 | * $Id: ixp4xx.c,v 1.13 2005/11/16 16:23:21 dvrabel Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
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> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/mach/flash.h> |
| 34 | |
| 35 | #include <linux/reboot.h> |
| 36 | |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 37 | /* |
| 38 | * Read/write a 16 bit word from flash address 'addr'. |
| 39 | * |
| 40 | * When the cpu is in little-endian mode it swizzles the address lines |
| 41 | * ('address coherency') so we need to undo the swizzling to ensure commands |
| 42 | * and the like end up on the correct flash address. |
| 43 | * |
| 44 | * To further complicate matters, due to the way the expansion bus controller |
| 45 | * handles 32 bit reads, the byte stream ABCD is stored on the flash as: |
| 46 | * D15 D0 |
| 47 | * +---+---+ |
| 48 | * | A | B | 0 |
| 49 | * +---+---+ |
| 50 | * | C | D | 2 |
| 51 | * +---+---+ |
| 52 | * This means that on LE systems each 16 bit word must be swapped. Note that |
| 53 | * this requires CONFIG_MTD_CFI_BE_BYTE_SWAP to be enabled to 'unswap' the CFI |
| 54 | * data and other flash commands which are always in D7-D0. |
| 55 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #ifndef __ARMEB__ |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 57 | #ifndef CONFIG_MTD_CFI_BE_BYTE_SWAP |
| 58 | # error CONFIG_MTD_CFI_BE_BYTE_SWAP required |
| 59 | #endif |
| 60 | |
| 61 | static inline u16 flash_read16(void __iomem *addr) |
| 62 | { |
| 63 | return be16_to_cpu(__raw_readw((void __iomem *)((unsigned long)addr ^ 0x2))); |
| 64 | } |
| 65 | |
| 66 | static inline void flash_write16(u16 d, void __iomem *addr) |
| 67 | { |
| 68 | __raw_writew(cpu_to_be16(d), (void __iomem *)((unsigned long)addr ^ 0x2)); |
| 69 | } |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #define BYTE0(h) ((h) & 0xFF) |
| 72 | #define BYTE1(h) (((h) >> 8) & 0xFF) |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #else |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 75 | |
| 76 | static inline u16 flash_read16(const void __iomem *addr) |
| 77 | { |
| 78 | return __raw_readw(addr); |
| 79 | } |
| 80 | |
| 81 | static inline void flash_write16(u16 d, void __iomem *addr) |
| 82 | { |
| 83 | __raw_writew(d, addr); |
| 84 | } |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #define BYTE0(h) (((h) >> 8) & 0xFF) |
| 87 | #define BYTE1(h) ((h) & 0xFF) |
| 88 | #endif |
| 89 | |
| 90 | static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs) |
| 91 | { |
| 92 | map_word val; |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 93 | val.x[0] = flash_read16(map->virt + ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return val; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * The IXP4xx expansion bus only allows 16-bit wide acceses |
| 99 | * when attached to a 16-bit wide device (such as the 28F128J3A), |
| 100 | * so we can't just memcpy_fromio(). |
| 101 | */ |
| 102 | static void ixp4xx_copy_from(struct map_info *map, void *to, |
| 103 | unsigned long from, ssize_t len) |
| 104 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | u8 *dest = (u8 *) to; |
David Vrabel | 9090ed0 | 2005-11-01 16:46:19 +0000 | [diff] [blame] | 106 | void __iomem *src = map->virt + from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 108 | if (len <= 0) |
| 109 | return; |
| 110 | |
| 111 | if (from & 1) { |
| 112 | *dest++ = BYTE1(flash_read16(src)); |
| 113 | src++; |
| 114 | --len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 117 | while (len >= 2) { |
| 118 | u16 data = flash_read16(src); |
| 119 | *dest++ = BYTE0(data); |
| 120 | *dest++ = BYTE1(data); |
| 121 | src += 2; |
| 122 | len -= 2; |
| 123 | } |
| 124 | |
| 125 | if (len > 0) |
| 126 | *dest++ = BYTE0(flash_read16(src)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 129 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | * Unaligned writes are ignored, causing the 8-bit |
| 131 | * probe to fail and proceed to the 16-bit probe (which succeeds). |
| 132 | */ |
| 133 | static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr) |
| 134 | { |
| 135 | if (!(adr & 1)) |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 136 | flash_write16(d.x[0], map->virt + adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 139 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | * Fast write16 function without the probing check above |
| 141 | */ |
| 142 | static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr) |
| 143 | { |
John Bowler | 3c77354 | 2005-11-16 16:23:25 +0000 | [diff] [blame] | 144 | flash_write16(d.x[0], map->virt + adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | struct ixp4xx_flash_info { |
| 148 | struct mtd_info *mtd; |
| 149 | struct map_info map; |
| 150 | struct mtd_partition *partitions; |
| 151 | struct resource *res; |
| 152 | }; |
| 153 | |
| 154 | static const char *probes[] = { "RedBoot", "cmdlinepart", NULL }; |
| 155 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 156 | static int ixp4xx_flash_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | struct flash_platform_data *plat = dev->dev.platform_data; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 159 | struct ixp4xx_flash_info *info = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 161 | platform_set_drvdata(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | if(!info) |
| 164 | return 0; |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (info->mtd) { |
| 167 | del_mtd_partitions(info->mtd); |
| 168 | map_destroy(info->mtd); |
| 169 | } |
David Vrabel | 9090ed0 | 2005-11-01 16:46:19 +0000 | [diff] [blame] | 170 | if (info->map.virt) |
| 171 | iounmap(info->map.virt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 173 | kfree(info->partitions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | if (info->res) { |
| 176 | release_resource(info->res); |
| 177 | kfree(info->res); |
| 178 | } |
| 179 | |
| 180 | if (plat->exit) |
| 181 | plat->exit(); |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 186 | static int ixp4xx_flash_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | struct flash_platform_data *plat = dev->dev.platform_data; |
| 189 | struct ixp4xx_flash_info *info; |
| 190 | int err = -1; |
| 191 | |
| 192 | if (!plat) |
| 193 | return -ENODEV; |
| 194 | |
| 195 | if (plat->init) { |
| 196 | err = plat->init(); |
| 197 | if (err) |
| 198 | return err; |
| 199 | } |
| 200 | |
| 201 | info = kmalloc(sizeof(struct ixp4xx_flash_info), GFP_KERNEL); |
| 202 | if(!info) { |
| 203 | err = -ENOMEM; |
| 204 | goto Error; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | memzero(info, sizeof(struct ixp4xx_flash_info)); |
| 207 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 208 | platform_set_drvdata(dev, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* |
| 211 | * Tell the MTD layer we're not 1:1 mapped so that it does |
| 212 | * not attempt to do a direct access on us. |
| 213 | */ |
| 214 | info->map.phys = NO_XIP; |
| 215 | info->map.size = dev->resource->end - dev->resource->start + 1; |
| 216 | |
| 217 | /* |
| 218 | * We only support 16-bit accesses for now. If and when |
| 219 | * any board use 8-bit access, we'll fixup the driver to |
| 220 | * handle that. |
| 221 | */ |
| 222 | info->map.bankwidth = 2; |
| 223 | info->map.name = dev->dev.bus_id; |
| 224 | info->map.read = ixp4xx_read16, |
| 225 | info->map.write = ixp4xx_probe_write16, |
| 226 | info->map.copy_from = ixp4xx_copy_from, |
| 227 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 228 | info->res = request_mem_region(dev->resource->start, |
| 229 | dev->resource->end - dev->resource->start + 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | "IXP4XXFlash"); |
| 231 | if (!info->res) { |
| 232 | printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n"); |
| 233 | err = -ENOMEM; |
| 234 | goto Error; |
| 235 | } |
| 236 | |
David Vrabel | 9090ed0 | 2005-11-01 16:46:19 +0000 | [diff] [blame] | 237 | info->map.virt = ioremap(dev->resource->start, |
| 238 | dev->resource->end - dev->resource->start + 1); |
| 239 | if (!info->map.virt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n"); |
| 241 | err = -EIO; |
| 242 | goto Error; |
| 243 | } |
| 244 | |
| 245 | info->mtd = do_map_probe(plat->map_name, &info->map); |
| 246 | if (!info->mtd) { |
| 247 | printk(KERN_ERR "IXP4XXFlash: map_probe failed\n"); |
| 248 | err = -ENXIO; |
| 249 | goto Error; |
| 250 | } |
| 251 | info->mtd->owner = THIS_MODULE; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | /* Use the fast version */ |
| 254 | info->map.write = ixp4xx_write16, |
| 255 | |
Brian Walsh | f40a6f1 | 2006-09-22 10:16:16 +0100 | [diff] [blame] | 256 | err = parse_mtd_partitions(info->mtd, probes, &info->partitions, dev->resource->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (err > 0) { |
| 258 | err = add_mtd_partitions(info->mtd, info->partitions, err); |
| 259 | if(err) |
| 260 | printk(KERN_ERR "Could not parse partitions\n"); |
| 261 | } |
| 262 | |
| 263 | if (err) |
| 264 | goto Error; |
| 265 | |
| 266 | return 0; |
| 267 | |
| 268 | Error: |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 269 | ixp4xx_flash_remove(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return err; |
| 271 | } |
| 272 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 273 | static struct platform_driver ixp4xx_flash_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | .probe = ixp4xx_flash_probe, |
| 275 | .remove = ixp4xx_flash_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 276 | .driver = { |
| 277 | .name = "IXP4XX-Flash", |
| 278 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | static int __init ixp4xx_flash_init(void) |
| 282 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 283 | return platform_driver_register(&ixp4xx_flash_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static void __exit ixp4xx_flash_exit(void) |
| 287 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 288 | platform_driver_unregister(&ixp4xx_flash_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | |
| 292 | module_init(ixp4xx_flash_init); |
| 293 | module_exit(ixp4xx_flash_exit); |
| 294 | |
| 295 | MODULE_LICENSE("GPL"); |
Deepak Saxena | 82810a9 | 2005-09-28 16:42:54 -0700 | [diff] [blame] | 296 | MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | MODULE_AUTHOR("Deepak Saxena"); |