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