blob: e3180d5aa06a0fb4fd09e842d01939164373520f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Jingoo Haned0b2722014-01-03 11:15:04 +090016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
20#include <linux/string.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021#include <linux/slab.h>
22#include <linux/ioport.h>
23#include <linux/device.h>
Linus Torvalds4fd5f822005-10-31 07:32:56 -080024#include <linux/platform_device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mtd/mtd.h>
27#include <linux/mtd/map.h>
28#include <linux/mtd/partitions.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/mach/flash.h>
32
33#include <linux/reboot.h>
34
John Bowler3c773542005-11-16 16:23:25 +000035/*
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 Torvalds1da177e2005-04-16 15:20:36 -070054#ifndef __ARMEB__
John Bowler3c773542005-11-16 16:23:25 +000055#ifndef CONFIG_MTD_CFI_BE_BYTE_SWAP
56# error CONFIG_MTD_CFI_BE_BYTE_SWAP required
57#endif
58
59static inline u16 flash_read16(void __iomem *addr)
60{
61 return be16_to_cpu(__raw_readw((void __iomem *)((unsigned long)addr ^ 0x2)));
62}
63
64static 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 Torvalds1da177e2005-04-16 15:20:36 -070069#define BYTE0(h) ((h) & 0xFF)
70#define BYTE1(h) (((h) >> 8) & 0xFF)
John Bowler3c773542005-11-16 16:23:25 +000071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#else
John Bowler3c773542005-11-16 16:23:25 +000073
74static inline u16 flash_read16(const void __iomem *addr)
75{
76 return __raw_readw(addr);
77}
78
79static inline void flash_write16(u16 d, void __iomem *addr)
80{
81 __raw_writew(d, addr);
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define BYTE0(h) (((h) >> 8) & 0xFF)
85#define BYTE1(h) ((h) & 0xFF)
86#endif
87
88static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
89{
90 map_word val;
John Bowler3c773542005-11-16 16:23:25 +000091 val.x[0] = flash_read16(map->virt + ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 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 */
100static void ixp4xx_copy_from(struct map_info *map, void *to,
101 unsigned long from, ssize_t len)
102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 u8 *dest = (u8 *) to;
David Vrabel9090ed02005-11-01 16:46:19 +0000104 void __iomem *src = map->virt + from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
John Bowler3c773542005-11-16 16:23:25 +0000106 if (len <= 0)
107 return;
108
109 if (from & 1) {
Jon Ringle53f2b1c2010-01-13 09:36:10 -0500110 *dest++ = BYTE1(flash_read16(src-1));
111 src++;
John Bowler3c773542005-11-16 16:23:25 +0000112 --len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
John Bowler3c773542005-11-16 16:23:25 +0000115 while (len >= 2) {
116 u16 data = flash_read16(src);
117 *dest++ = BYTE0(data);
118 *dest++ = BYTE1(data);
119 src += 2;
120 len -= 2;
Richard Cochran68640c22010-06-14 18:15:19 +0200121 }
John Bowler3c773542005-11-16 16:23:25 +0000122
123 if (len > 0)
124 *dest++ = BYTE0(flash_read16(src));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000127/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * Unaligned writes are ignored, causing the 8-bit
129 * probe to fail and proceed to the 16-bit probe (which succeeds).
130 */
131static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr)
132{
133 if (!(adr & 1))
John Bowler3c773542005-11-16 16:23:25 +0000134 flash_write16(d.x[0], map->virt + adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000137/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * Fast write16 function without the probing check above
139 */
140static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
141{
John Bowler3c773542005-11-16 16:23:25 +0000142 flash_write16(d.x[0], map->virt + adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145struct ixp4xx_flash_info {
146 struct mtd_info *mtd;
147 struct map_info map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct resource *res;
149};
150
Artem Bityutskiy0984c892013-03-12 10:46:37 +0200151static const char * const probes[] = { "RedBoot", "cmdlinepart", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Russell King3ae5eae2005-11-09 22:32:44 +0000153static int ixp4xx_flash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Jingoo Hand20d5a52013-07-30 17:18:06 +0900155 struct flash_platform_data *plat = dev_get_platdata(&dev->dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000156 struct ixp4xx_flash_info *info = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if(!info)
159 return 0;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (info->mtd) {
Jamie Iles6cbbf2f2011-05-23 10:22:49 +0100162 mtd_device_unregister(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 map_destroy(info->mtd);
164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (plat->exit)
167 plat->exit();
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170}
171
Russell King3ae5eae2005-11-09 22:32:44 +0000172static int ixp4xx_flash_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Jingoo Hand20d5a52013-07-30 17:18:06 +0900174 struct flash_platform_data *plat = dev_get_platdata(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct ixp4xx_flash_info *info;
Marc Kleine-Buddea3c1e3b2012-02-08 20:24:29 +0100176 struct mtd_part_parser_data ppdata = {
177 .origin = dev->resource->start,
178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int err = -1;
180
181 if (!plat)
182 return -ENODEV;
183
184 if (plat->init) {
185 err = plat->init();
186 if (err)
187 return err;
188 }
189
Jingoo Haned0b2722014-01-03 11:15:04 +0900190 info = devm_kzalloc(&dev->dev, sizeof(struct ixp4xx_flash_info),
191 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if(!info) {
193 err = -ENOMEM;
194 goto Error;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Russell King3ae5eae2005-11-09 22:32:44 +0000197 platform_set_drvdata(dev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /*
200 * Tell the MTD layer we're not 1:1 mapped so that it does
201 * not attempt to do a direct access on us.
202 */
203 info->map.phys = NO_XIP;
Tobias Klauserd6587fe2009-10-30 17:54:33 +0100204 info->map.size = resource_size(dev->resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /*
207 * We only support 16-bit accesses for now. If and when
208 * any board use 8-bit access, we'll fixup the driver to
209 * handle that.
210 */
211 info->map.bankwidth = 2;
Kay Sievers475b44c2009-01-06 10:44:38 -0800212 info->map.name = dev_name(&dev->dev);
Richard Cochran68640c22010-06-14 18:15:19 +0200213 info->map.read = ixp4xx_read16;
214 info->map.write = ixp4xx_probe_write16;
215 info->map.copy_from = ixp4xx_copy_from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jingoo Haned0b2722014-01-03 11:15:04 +0900217 info->map.virt = devm_ioremap_resource(&dev->dev, dev->resource);
218 if (IS_ERR(info->map.virt)) {
219 err = PTR_ERR(info->map.virt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto Error;
221 }
222
223 info->mtd = do_map_probe(plat->map_name, &info->map);
224 if (!info->mtd) {
225 printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
226 err = -ENXIO;
227 goto Error;
228 }
Frans Klavere4e07db2015-06-10 22:38:27 +0200229 info->mtd->dev.parent = &dev->dev;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Use the fast version */
Richard Cochran68640c22010-06-14 18:15:19 +0200232 info->map.write = ixp4xx_write16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Marc Kleine-Buddea3c1e3b2012-02-08 20:24:29 +0100234 err = mtd_device_parse_register(info->mtd, probes, &ppdata,
Dmitry Eremin-Solenikovbc413f12011-06-02 17:59:47 +0400235 plat->parts, plat->nr_parts);
236 if (err) {
Jamie Iles6cbbf2f2011-05-23 10:22:49 +0100237 printk(KERN_ERR "Could not parse partitions\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto Error;
Dmitry Eremin-Solenikovbc413f12011-06-02 17:59:47 +0400239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return 0;
242
243Error:
Russell King3ae5eae2005-11-09 22:32:44 +0000244 ixp4xx_flash_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return err;
246}
247
Russell King3ae5eae2005-11-09 22:32:44 +0000248static struct platform_driver ixp4xx_flash_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .probe = ixp4xx_flash_probe,
250 .remove = ixp4xx_flash_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000251 .driver = {
252 .name = "IXP4XX-Flash",
253 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
Axel Linf99640d2011-11-27 20:45:03 +0800256module_platform_driver(ixp4xx_flash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258MODULE_LICENSE("GPL");
Deepak Saxena82810a92005-09-28 16:42:54 -0700259MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260MODULE_AUTHOR("Deepak Saxena");
Kay Sievers41d867c2008-04-18 13:44:26 -0700261MODULE_ALIAS("platform:IXP4XX-Flash");