1 From 61cba34bd6c1bddfc38f94cc3f80bdfefcc3393b Mon Sep 17 00:00:00 2001
2 From: Ricardo Ribalda <ricardo.ribalda@gmail.com>
3 Date: Fri, 2 Dec 2016 12:31:44 +0100
4 Subject: [PATCH] mtd: spi-nor: Add support for S3AN spi-nor devices
6 Xilinx Spartan-3AN FPGAs contain an In-System Flash where they keep
7 their configuration data and (optionally) some user data.
9 The protocol of this flash follows most of the spi-nor standard. With
10 the following differences:
12 - Page size might not be a power of two.
13 - The address calculation (default addressing mode).
14 - The spi nor commands used.
16 Protocol is described on Xilinx User Guide UG333
18 Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
19 Cc: Boris Brezillon <boris.brezillon@free-electrons.com>
20 Cc: Brian Norris <computersforpeace@gmail.com>
21 Cc: Marek Vasut <marek.vasut@gmail.com>
22 Reviewed-by: Marek Vasut <marek.vasut@gmail.com>
23 Signed-off-by: Cyrille Pitchen <cyrille.pitchen@atmel.com>
25 drivers/mtd/spi-nor/spi-nor.c | 154 ++++++++++++++++++++++++++++++++++++++++--
26 include/linux/mtd/spi-nor.h | 12 ++++
27 2 files changed, 161 insertions(+), 5 deletions(-)
29 --- a/drivers/mtd/spi-nor/spi-nor.c
30 +++ b/drivers/mtd/spi-nor/spi-nor.c
31 @@ -75,6 +75,12 @@ struct flash_info {
32 * bit. Must be used with
35 +#define SPI_S3AN BIT(10) /*
36 + * Xilinx Spartan 3AN In-System Flash
37 + * (MFR cannot be used for probing
38 + * because it has the same value as
43 #define JEDEC_MFR(info) ((info)->id[0])
44 @@ -217,6 +223,21 @@ static inline int set_4byte(struct spi_n
45 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
49 +static int s3an_sr_ready(struct spi_nor *nor)
54 + ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
56 + dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
60 + return !!(val & XSR_RDY);
63 static inline int spi_nor_sr_ready(struct spi_nor *nor)
65 int sr = read_sr(nor);
66 @@ -238,7 +259,11 @@ static inline int spi_nor_fsr_ready(stru
67 static int spi_nor_ready(struct spi_nor *nor)
70 - sr = spi_nor_sr_ready(nor);
72 + if (nor->flags & SNOR_F_READY_XSR_RDY)
73 + sr = s3an_sr_ready(nor);
75 + sr = spi_nor_sr_ready(nor);
78 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
79 @@ -320,6 +345,24 @@ static void spi_nor_unlock_and_unprep(st
83 + * This code converts an address to the Default Address Mode, that has non
84 + * power of two page sizes. We must support this mode because it is the default
85 + * mode supported by Xilinx tools, it can access the whole flash area and
86 + * changing over to the Power-of-two mode is irreversible and corrupts the
88 + * Addr can safely be unsigned int, the biggest S3AN device is smaller than
91 +static loff_t spi_nor_s3an_addr_convert(struct spi_nor *nor, unsigned int addr)
93 + unsigned int offset = addr;
95 + offset %= nor->page_size;
97 + return ((addr - offset) << 1) | offset;
101 * Initiate the erasure of a single sector
103 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
104 @@ -327,6 +370,9 @@ static int spi_nor_erase_sector(struct s
105 u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
108 + if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
109 + addr = spi_nor_s3an_addr_convert(nor, addr);
112 return nor->erase(nor, addr);
114 @@ -368,7 +414,7 @@ static int spi_nor_erase(struct mtd_info
117 /* whole-chip erase? */
118 - if (len == mtd->size) {
119 + if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
120 unsigned long timeout;
123 @@ -782,6 +828,19 @@ static int spi_nor_is_locked(struct mtd_
124 .addr_width = (_addr_width), \
127 +#define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
129 + ((_jedec_id) >> 16) & 0xff, \
130 + ((_jedec_id) >> 8) & 0xff, \
131 + (_jedec_id) & 0xff \
134 + .sector_size = (8*_page_size), \
135 + .n_sectors = (_n_sectors), \
136 + .page_size = _page_size, \
138 + .flags = SPI_NOR_NO_FR | SPI_S3AN,
140 /* NOTE: double check command sets and memory organization when you add
141 * more nor chips. This current list focusses on newer chips, which
142 * have been converging on command sets which including JEDEC ID.
143 @@ -1020,6 +1079,13 @@ static const struct flash_info spi_nor_i
144 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
145 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
146 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
148 + /* Xilinx S3AN Internal Flash */
149 + { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
150 + { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
151 + { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
152 + { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
153 + { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
157 @@ -1060,7 +1126,12 @@ static int spi_nor_read(struct mtd_info
161 - ret = nor->read(nor, from, len, buf);
162 + loff_t addr = from;
164 + if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
165 + addr = spi_nor_s3an_addr_convert(nor, addr);
167 + ret = nor->read(nor, addr, len, buf);
169 /* We shouldn't see 0-length reads */
171 @@ -1181,8 +1252,23 @@ static int spi_nor_write(struct mtd_info
173 for (i = 0; i < len; ) {
175 + loff_t addr = to + i;
177 - page_offset = (to + i) & (nor->page_size - 1);
179 + * If page_size is a power of two, the offset can be quickly
180 + * calculated with an AND operation. On the other cases we
181 + * need to do a modulus operation (more expensive).
182 + * Power of two numbers have only one bit set and we can use
183 + * the instruction hweight32 to detect if we need to do a
184 + * modulus (do_div()) or not.
186 + if (hweight32(nor->page_size) == 1) {
187 + page_offset = addr & (nor->page_size - 1);
189 + uint64_t aux = addr;
191 + page_offset = do_div(aux, nor->page_size);
193 WARN_ONCE(page_offset,
194 "Writing at offset %zu into a NOR page. Writing partial pages may decrease reliability and increase wear of NOR flash.",
196 @@ -1190,8 +1276,11 @@ static int spi_nor_write(struct mtd_info
197 page_remain = min_t(size_t,
198 nor->page_size - page_offset, len - i);
200 + if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
201 + addr = spi_nor_s3an_addr_convert(nor, addr);
204 - ret = nor->write(nor, to + i, page_remain, buf + i);
205 + ret = nor->write(nor, addr, page_remain, buf + i);
209 @@ -1325,6 +1414,47 @@ static int spi_nor_check(struct spi_nor
213 +static int s3an_nor_scan(const struct flash_info *info, struct spi_nor *nor)
218 + ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
220 + dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
224 + nor->erase_opcode = SPINOR_OP_XSE;
225 + nor->program_opcode = SPINOR_OP_XPP;
226 + nor->read_opcode = SPINOR_OP_READ;
227 + nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
230 + * This flashes have a page size of 264 or 528 bytes (known as
231 + * Default addressing mode). It can be changed to a more standard
232 + * Power of two mode where the page size is 256/512. This comes
233 + * with a price: there is 3% less of space, the data is corrupted
234 + * and the page size cannot be changed back to default addressing
237 + * The current addressing mode can be read from the XRDSR register
238 + * and should not be changed, because is a destructive operation.
240 + if (val & XSR_PAGESIZE) {
241 + /* Flash in Power of 2 mode */
242 + nor->page_size = (nor->page_size == 264) ? 256 : 512;
243 + nor->mtd.writebufsize = nor->page_size;
244 + nor->mtd.size = 8 * nor->page_size * info->n_sectors;
245 + nor->mtd.erasesize = 8 * nor->page_size;
247 + /* Flash in Default addressing mode */
248 + nor->flags |= SNOR_F_S3AN_ADDR_DEFAULT;
254 int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
256 const struct flash_info *info = NULL;
257 @@ -1373,6 +1503,14 @@ int spi_nor_scan(struct spi_nor *nor, co
258 mutex_init(&nor->lock);
261 + * Make sure the XSR_RDY flag is set before calling
262 + * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
263 + * with Atmel spi-nor
265 + if (info->flags & SPI_S3AN)
266 + nor->flags |= SNOR_F_READY_XSR_RDY;
269 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
270 * with the software protection bits set
272 @@ -1530,6 +1668,12 @@ int spi_nor_scan(struct spi_nor *nor, co
274 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
276 + if (info->flags & SPI_S3AN) {
277 + ret = s3an_nor_scan(info, nor);
282 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
283 (long long)mtd->size >> 10);
285 --- a/include/linux/mtd/spi-nor.h
286 +++ b/include/linux/mtd/spi-nor.h
288 #define SPINOR_OP_WRDI 0x04 /* Write disable */
289 #define SPINOR_OP_AAI_WP 0xad /* Auto address increment word program */
291 +/* Used for S3AN flashes only */
292 +#define SPINOR_OP_XSE 0x50 /* Sector erase */
293 +#define SPINOR_OP_XPP 0x82 /* Page program */
294 +#define SPINOR_OP_XRDSR 0xd7 /* Read status register */
296 +#define XSR_PAGESIZE BIT(0) /* Page size in Po2 or Linear */
297 +#define XSR_RDY BIT(7) /* Ready */
300 /* Used for Macronix and Winbond flashes. */
301 #define SPINOR_OP_EN4B 0xb7 /* Enter 4-byte mode */
302 #define SPINOR_OP_EX4B 0xe9 /* Exit 4-byte mode */
303 @@ -119,6 +128,9 @@ enum spi_nor_ops {
304 enum spi_nor_option_flags {
305 SNOR_F_USE_FSR = BIT(0),
306 SNOR_F_HAS_SR_TB = BIT(1),
307 + SNOR_F_NO_OP_CHIP_ERASE = BIT(2),
308 + SNOR_F_S3AN_ADDR_DEFAULT = BIT(3),
309 + SNOR_F_READY_XSR_RDY = BIT(4),