Blame view

drivers/mtd/maps/ixp4xx.c 5.95 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
12
13
14
   * 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.
   *
   */
ed0b272ed   Jingoo Han   mtd: ixp4xx: Use ...
15
  #include <linux/err.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
17
  #include <linux/module.h>
  #include <linux/types.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
  #include <linux/kernel.h>
  #include <linux/string.h>
4e57b6817   Tim Schmielau   [PATCH] fix missi...
20
21
22
  #include <linux/slab.h>
  #include <linux/ioport.h>
  #include <linux/device.h>
4fd5f8267   Linus Torvalds   Merge master.kern...
23
  #include <linux/platform_device.h>
4e57b6817   Tim Schmielau   [PATCH] fix missi...
24

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
  #include <linux/mtd/mtd.h>
  #include <linux/mtd/map.h>
  #include <linux/mtd/partitions.h>
4e57b6817   Tim Schmielau   [PATCH] fix missi...
28

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  #include <asm/io.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
32
  #include <asm/mach/flash.h>
  
  #include <linux/reboot.h>
3c7735479   John Bowler   [MTD] maps/ixp4xx...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  /*
   * 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.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
  #ifndef __ARMEB__
3c7735479   John Bowler   [MTD] maps/ixp4xx...
53
54
55
56
57
58
59
60
61
62
63
64
65
  #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));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
67
  #define	BYTE0(h)	((h) & 0xFF)
  #define	BYTE1(h)	(((h) >> 8) & 0xFF)
3c7735479   John Bowler   [MTD] maps/ixp4xx...
68

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
  #else
3c7735479   John Bowler   [MTD] maps/ixp4xx...
70
71
72
73
74
75
76
77
78
79
  
  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);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
85
86
  #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;
3c7735479   John Bowler   [MTD] maps/ixp4xx...
87
  	val.x[0] = flash_read16(map->virt + ofs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
92
93
94
95
96
97
98
  	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)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
  	u8 *dest = (u8 *) to;
9090ed0b8   David Vrabel   [MTD] maps/ixp4xx...
100
  	void __iomem *src = map->virt + from;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101

3c7735479   John Bowler   [MTD] maps/ixp4xx...
102
103
104
105
  	if (len <= 0)
  		return;
  
  	if (from & 1) {
53f2b1c86   Jon Ringle   mtd: ixp4xx: fix ...
106
107
  		*dest++ = BYTE1(flash_read16(src-1));
  		src++;
3c7735479   John Bowler   [MTD] maps/ixp4xx...
108
  		--len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  	}
3c7735479   John Bowler   [MTD] maps/ixp4xx...
110
111
112
113
114
115
  	while (len >= 2) {
  		u16 data = flash_read16(src);
  		*dest++ = BYTE0(data);
  		*dest++ = BYTE1(data);
  		src += 2;
  		len -= 2;
68640c2a4   Richard Cochran   mtd: ixp4xx: add ...
116
  	}
3c7735479   John Bowler   [MTD] maps/ixp4xx...
117
118
119
  
  	if (len > 0)
  		*dest++ = BYTE0(flash_read16(src));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
  }
69f34c98c   Thomas Gleixner   [MTD] maps: Clean...
121
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
127
   * 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))
3c7735479   John Bowler   [MTD] maps/ixp4xx...
128
  		flash_write16(d.x[0], map->virt + adr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
  }
69f34c98c   Thomas Gleixner   [MTD] maps: Clean...
130
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
132
133
134
   * Fast write16 function without the probing check above
   */
  static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
  {
3c7735479   John Bowler   [MTD] maps/ixp4xx...
135
  	flash_write16(d.x[0], map->virt + adr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
139
140
  }
  
  struct ixp4xx_flash_info {
  	struct mtd_info *mtd;
  	struct map_info map;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
  	struct resource *res;
  };
0984c8910   Artem Bityutskiy   mtd: maps: add co...
143
  static const char * const probes[] = { "RedBoot", "cmdlinepart", NULL };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144

3ae5eaec1   Russell King   [DRIVER MODEL] Co...
145
  static int ixp4xx_flash_remove(struct platform_device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
  {
d20d5a578   Jingoo Han   mtd: maps: use de...
147
  	struct flash_platform_data *plat = dev_get_platdata(&dev->dev);
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
148
  	struct ixp4xx_flash_info *info = platform_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
  	if(!info)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
  	if (info->mtd) {
6cbbf2ffa   Jamie Iles   mtd: ixp: convert...
153
  		mtd_device_unregister(info->mtd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
  		map_destroy(info->mtd);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
  
  	if (plat->exit)
  		plat->exit();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
160
  	return 0;
  }
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
161
  static int ixp4xx_flash_probe(struct platform_device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  {
d20d5a578   Jingoo Han   mtd: maps: use de...
163
  	struct flash_platform_data *plat = dev_get_platdata(&dev->dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
  	struct ixp4xx_flash_info *info;
a3c1e3b73   Marc Kleine-Budde   mtd: ixp4xx: oops...
165
166
167
  	struct mtd_part_parser_data ppdata = {
  		.origin = dev->resource->start,
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
170
171
172
173
174
175
176
177
  	int err = -1;
  
  	if (!plat)
  		return -ENODEV;
  
  	if (plat->init) {
  		err = plat->init();
  		if (err)
  			return err;
  	}
ed0b272ed   Jingoo Han   mtd: ixp4xx: Use ...
178
179
  	info = devm_kzalloc(&dev->dev, sizeof(struct ixp4xx_flash_info),
  			    GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
182
  	if(!info) {
  		err = -ENOMEM;
  		goto Error;
69f34c98c   Thomas Gleixner   [MTD] maps: Clean...
183
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184

3ae5eaec1   Russell King   [DRIVER MODEL] Co...
185
  	platform_set_drvdata(dev, info);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
189
190
191
  	/*
  	 * 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;
d6587feaf   Tobias Klauser   mtd: ixp4xx map: ...
192
  	info->map.size = resource_size(dev->resource);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
194
195
196
197
198
199
  
  	/*
  	 * 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;
475b44c19   Kay Sievers   mtd: struct devic...
200
  	info->map.name = dev_name(&dev->dev);
68640c2a4   Richard Cochran   mtd: ixp4xx: add ...
201
202
203
  	info->map.read = ixp4xx_read16;
  	info->map.write = ixp4xx_probe_write16;
  	info->map.copy_from = ixp4xx_copy_from;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204

ed0b272ed   Jingoo Han   mtd: ixp4xx: Use ...
205
206
207
  	info->map.virt = devm_ioremap_resource(&dev->dev, dev->resource);
  	if (IS_ERR(info->map.virt)) {
  		err = PTR_ERR(info->map.virt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
214
215
216
217
  		goto Error;
  	}
  
  	info->mtd = do_map_probe(plat->map_name, &info->map);
  	if (!info->mtd) {
  		printk(KERN_ERR "IXP4XXFlash: map_probe failed
  ");
  		err = -ENXIO;
  		goto Error;
  	}
e4e07db4c   Frans Klaver   mtd: maps: ixp4xx...
218
  	info->mtd->dev.parent = &dev->dev;
69f34c98c   Thomas Gleixner   [MTD] maps: Clean...
219

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
  	/* Use the fast version */
68640c2a4   Richard Cochran   mtd: ixp4xx: add ...
221
  	info->map.write = ixp4xx_write16;
a3c1e3b73   Marc Kleine-Budde   mtd: ixp4xx: oops...
222
  	err = mtd_device_parse_register(info->mtd, probes, &ppdata,
bc413f11d   Dmitry Eremin-Solenikov   mtd: ixp4xx.c: us...
223
224
  			plat->parts, plat->nr_parts);
  	if (err) {
6cbbf2ffa   Jamie Iles   mtd: ixp: convert...
225
226
  		printk(KERN_ERR "Could not parse partitions
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  		goto Error;
bc413f11d   Dmitry Eremin-Solenikov   mtd: ixp4xx.c: us...
228
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
  
  	return 0;
  
  Error:
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
233
  	ixp4xx_flash_remove(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
235
  	return err;
  }
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
236
  static struct platform_driver ixp4xx_flash_driver = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
  	.probe		= ixp4xx_flash_probe,
  	.remove		= ixp4xx_flash_remove,
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
239
240
241
  	.driver		= {
  		.name	= "IXP4XX-Flash",
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
  };
f99640dee   Axel Lin   mtd: convert driv...
243
  module_platform_driver(ixp4xx_flash_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
  
  MODULE_LICENSE("GPL");
82810a906   Deepak Saxena   [PATCH] Fix ixp4x...
246
  MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
  MODULE_AUTHOR("Deepak Saxena");
41d867c9a   Kay Sievers   [MTD] [MAPS] fix ...
248
  MODULE_ALIAS("platform:IXP4XX-Flash");