ixp4xx.c 6.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * drivers/mtd/maps/ixp4xx.c
 *
 * MTD Map file for IXP4XX based systems. Please do not make per-board
 * changes in here. If your board needs special setup, do it in your
 * platform level code in arch/arm/mach-ixp4xx/board-setup.c
 *
 * Original Author: Intel Corporation
 * Maintainer: Deepak Saxena <dsaxena@mvista.com>
 *
 * Copyright (C) 2002 Intel Corporation
 * Copyright (C) 2003-2004 MontaVista Software, Inc.
 *
 */

J
Jingoo Han 已提交
16
#include <linux/err.h>
L
Linus Torvalds 已提交
17 18 19 20 21
#include <linux/module.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/string.h>
T
Tim Schmielau 已提交
22 23 24
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/device.h>
25
#include <linux/platform_device.h>
T
Tim Schmielau 已提交
26

L
Linus Torvalds 已提交
27 28 29
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
T
Tim Schmielau 已提交
30

L
Linus Torvalds 已提交
31 32 33 34 35
#include <asm/io.h>
#include <asm/mach/flash.h>

#include <linux/reboot.h>

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * Read/write a 16 bit word from flash address 'addr'.
 *
 * When the cpu is in little-endian mode it swizzles the address lines
 * ('address coherency') so we need to undo the swizzling to ensure commands
 * and the like end up on the correct flash address.
 *
 * To further complicate matters, due to the way the expansion bus controller
 * handles 32 bit reads, the byte stream ABCD is stored on the flash as:
 *     D15    D0
 *     +---+---+
 *     | A | B | 0
 *     +---+---+
 *     | C | D | 2
 *     +---+---+
 * This means that on LE systems each 16 bit word must be swapped. Note that
 * this requires CONFIG_MTD_CFI_BE_BYTE_SWAP to be enabled to 'unswap' the CFI
 * data and other flash commands which are always in D7-D0.
 */
L
Linus Torvalds 已提交
55
#ifndef __ARMEB__
56 57 58 59 60 61 62 63 64 65 66 67 68 69
#ifndef CONFIG_MTD_CFI_BE_BYTE_SWAP
#  error CONFIG_MTD_CFI_BE_BYTE_SWAP required
#endif

static inline u16 flash_read16(void __iomem *addr)
{
	return be16_to_cpu(__raw_readw((void __iomem *)((unsigned long)addr ^ 0x2)));
}

static inline void flash_write16(u16 d, void __iomem *addr)
{
	__raw_writew(cpu_to_be16(d), (void __iomem *)((unsigned long)addr ^ 0x2));
}

L
Linus Torvalds 已提交
70 71
#define	BYTE0(h)	((h) & 0xFF)
#define	BYTE1(h)	(((h) >> 8) & 0xFF)
72

L
Linus Torvalds 已提交
73
#else
74 75 76 77 78 79 80 81 82 83 84

static inline u16 flash_read16(const void __iomem *addr)
{
	return __raw_readw(addr);
}

static inline void flash_write16(u16 d, void __iomem *addr)
{
	__raw_writew(d, addr);
}

L
Linus Torvalds 已提交
85 86 87 88 89 90 91
#define	BYTE0(h)	(((h) >> 8) & 0xFF)
#define	BYTE1(h)	((h) & 0xFF)
#endif

static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
{
	map_word val;
92
	val.x[0] = flash_read16(map->virt + ofs);
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103 104
	return val;
}

/*
 * The IXP4xx expansion bus only allows 16-bit wide acceses
 * when attached to a 16-bit wide device (such as the 28F128J3A),
 * so we can't just memcpy_fromio().
 */
static void ixp4xx_copy_from(struct map_info *map, void *to,
			     unsigned long from, ssize_t len)
{
	u8 *dest = (u8 *) to;
105
	void __iomem *src = map->virt + from;
L
Linus Torvalds 已提交
106

107 108 109 110
	if (len <= 0)
		return;

	if (from & 1) {
111 112
		*dest++ = BYTE1(flash_read16(src-1));
		src++;
113
		--len;
L
Linus Torvalds 已提交
114 115
	}

116 117 118 119 120 121
	while (len >= 2) {
		u16 data = flash_read16(src);
		*dest++ = BYTE0(data);
		*dest++ = BYTE1(data);
		src += 2;
		len -= 2;
122
	}
123 124 125

	if (len > 0)
		*dest++ = BYTE0(flash_read16(src));
L
Linus Torvalds 已提交
126 127
}

128
/*
L
Linus Torvalds 已提交
129 130 131 132 133 134
 * Unaligned writes are ignored, causing the 8-bit
 * probe to fail and proceed to the 16-bit probe (which succeeds).
 */
static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr)
{
	if (!(adr & 1))
135
		flash_write16(d.x[0], map->virt + adr);
L
Linus Torvalds 已提交
136 137
}

138
/*
L
Linus Torvalds 已提交
139 140 141 142
 * Fast write16 function without the probing check above
 */
static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
{
143
	flash_write16(d.x[0], map->virt + adr);
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151
}

struct ixp4xx_flash_info {
	struct mtd_info *mtd;
	struct map_info map;
	struct resource *res;
};

152
static const char * const probes[] = { "RedBoot", "cmdlinepart", NULL };
L
Linus Torvalds 已提交
153

154
static int ixp4xx_flash_remove(struct platform_device *dev)
L
Linus Torvalds 已提交
155
{
J
Jingoo Han 已提交
156
	struct flash_platform_data *plat = dev_get_platdata(&dev->dev);
157
	struct ixp4xx_flash_info *info = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
158 159 160 161 162

	if(!info)
		return 0;

	if (info->mtd) {
163
		mtd_device_unregister(info->mtd);
L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172
		map_destroy(info->mtd);
	}

	if (plat->exit)
		plat->exit();

	return 0;
}

173
static int ixp4xx_flash_probe(struct platform_device *dev)
L
Linus Torvalds 已提交
174
{
J
Jingoo Han 已提交
175
	struct flash_platform_data *plat = dev_get_platdata(&dev->dev);
L
Linus Torvalds 已提交
176
	struct ixp4xx_flash_info *info;
177 178 179
	struct mtd_part_parser_data ppdata = {
		.origin = dev->resource->start,
	};
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190
	int err = -1;

	if (!plat)
		return -ENODEV;

	if (plat->init) {
		err = plat->init();
		if (err)
			return err;
	}

J
Jingoo Han 已提交
191 192
	info = devm_kzalloc(&dev->dev, sizeof(struct ixp4xx_flash_info),
			    GFP_KERNEL);
L
Linus Torvalds 已提交
193 194 195
	if(!info) {
		err = -ENOMEM;
		goto Error;
196
	}
L
Linus Torvalds 已提交
197

198
	platform_set_drvdata(dev, info);
L
Linus Torvalds 已提交
199 200 201 202 203 204

	/*
	 * Tell the MTD layer we're not 1:1 mapped so that it does
	 * not attempt to do a direct access on us.
	 */
	info->map.phys = NO_XIP;
205
	info->map.size = resource_size(dev->resource);
L
Linus Torvalds 已提交
206 207 208 209 210 211 212

	/*
	 * We only support 16-bit accesses for now. If and when
	 * any board use 8-bit access, we'll fixup the driver to
	 * handle that.
	 */
	info->map.bankwidth = 2;
213
	info->map.name = dev_name(&dev->dev);
214 215 216
	info->map.read = ixp4xx_read16;
	info->map.write = ixp4xx_probe_write16;
	info->map.copy_from = ixp4xx_copy_from;
L
Linus Torvalds 已提交
217

J
Jingoo Han 已提交
218 219 220
	info->map.virt = devm_ioremap_resource(&dev->dev, dev->resource);
	if (IS_ERR(info->map.virt)) {
		err = PTR_ERR(info->map.virt);
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229 230
		goto Error;
	}

	info->mtd = do_map_probe(plat->map_name, &info->map);
	if (!info->mtd) {
		printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
		err = -ENXIO;
		goto Error;
	}
	info->mtd->owner = THIS_MODULE;
231

L
Linus Torvalds 已提交
232
	/* Use the fast version */
233 234
	info->map.write = ixp4xx_write16;

235
	err = mtd_device_parse_register(info->mtd, probes, &ppdata,
236 237
			plat->parts, plat->nr_parts);
	if (err) {
238
		printk(KERN_ERR "Could not parse partitions\n");
L
Linus Torvalds 已提交
239
		goto Error;
240
	}
L
Linus Torvalds 已提交
241 242 243 244

	return 0;

Error:
245
	ixp4xx_flash_remove(dev);
L
Linus Torvalds 已提交
246 247 248
	return err;
}

249
static struct platform_driver ixp4xx_flash_driver = {
L
Linus Torvalds 已提交
250 251
	.probe		= ixp4xx_flash_probe,
	.remove		= ixp4xx_flash_remove,
252 253
	.driver		= {
		.name	= "IXP4XX-Flash",
254
		.owner	= THIS_MODULE,
255
	},
L
Linus Torvalds 已提交
256 257
};

258
module_platform_driver(ixp4xx_flash_driver);
L
Linus Torvalds 已提交
259 260

MODULE_LICENSE("GPL");
261
MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
L
Linus Torvalds 已提交
262
MODULE_AUTHOR("Deepak Saxena");
263
MODULE_ALIAS("platform:IXP4XX-Flash");