1 From fb9ed0ef6f0ba6b6535c64dcfcf45c161723e56f Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Tue, 6 Nov 2012 19:31:38 +0100
4 Subject: sf: add generic support for 4-byte address mode
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
8 --- a/drivers/mtd/spi/spi_flash.c
9 +++ b/drivers/mtd/spi/spi_flash.c
11 static void spi_flash_addr(struct spi_flash *flash, u32 addr, u8 *cmd, u8 *cmd_len)
13 /* cmd[0] is actual command */
14 - cmd[1] = addr >> 16;
18 + if (spi_flash_use_4byte_mode(flash)) {
19 + cmd[1] = addr >> 24;
20 + cmd[2] = addr >> 16;
25 + cmd[1] = addr >> 16;
32 static void spi_flash_page_addr(struct spi_flash *flash, u32 page_addr, u32 byte_addr, u8 *cmd, u8 *cmd_len)
34 /* cmd[0] is actual command */
35 - cmd[1] = page_addr >> 8;
36 - cmd[2] = page_addr >> 0;
39 + if (spi_flash_use_4byte_mode(flash)) {
40 + cmd[1] = page_addr >> 16;
41 + cmd[2] = page_addr >> 8;
42 + cmd[3] = page_addr >> 0;
46 + cmd[1] = page_addr >> 8;
47 + cmd[2] = page_addr >> 0;
53 static int spi_flash_read_write(struct spi_slave *spi,
54 @@ -81,7 +97,7 @@ int spi_flash_cmd_write_multi(struct spi
55 unsigned long page_addr, byte_addr, page_size;
56 size_t chunk_len, actual;
61 page_size = flash->page_size;
62 page_addr = offset / page_size;
63 @@ -99,8 +115,8 @@ int spi_flash_cmd_write_multi(struct spi
65 spi_flash_page_addr(flash, page_addr, byte_addr, cmd, &cmd_len);
67 - debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
68 - buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
69 + debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x%02x } chunk_len = %zu\n",
70 + buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], chunk_len);
72 ret = spi_flash_cmd_write_enable(flash);
74 @@ -146,7 +162,7 @@ int spi_flash_read_common(struct spi_fla
75 int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
76 size_t len, void *data)
81 cmd[0] = CMD_READ_ARRAY_FAST;
82 spi_flash_addr(flash, offset, cmd, &cmd_len);
83 @@ -202,7 +218,7 @@ int spi_flash_cmd_erase(struct spi_flash
85 u32 start, end, erase_size;
90 erase_size = flash->sector_size;
91 if (offset % erase_size || len % erase_size) {
92 @@ -227,8 +243,8 @@ int spi_flash_cmd_erase(struct spi_flash
93 spi_flash_addr(flash, offset, cmd, &cmd_len);
96 - debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
97 - cmd[2], cmd[3], offset);
98 + debug("SF: erase %2x %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
99 + cmd[2], cmd[3], cmd[4], offset);
101 ret = spi_flash_cmd_write_enable(flash);
103 @@ -409,6 +425,12 @@ int spi_flash_probe_spl(struct spi_flash
104 goto err_manufacturer_probe;
107 + ret = spi_flash_set_4byte_mode(flash);
109 + debug("SF: Failed to enable 4 byte mode: %d\n", ret);
110 + goto err_manufacturer_probe;
113 spi_release_bus(spi);
116 --- a/drivers/mtd/spi/spi_flash_internal.h
117 +++ b/drivers/mtd/spi/spi_flash_internal.h
118 @@ -97,6 +97,31 @@ int spi_flash_cmd_wait_ready(struct spi_
120 int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len);
122 +#ifdef CONFIG_SPI_FLASH_4BYTE_MODE
123 +static inline int spi_flash_use_4byte_mode(struct spi_flash *flash)
125 + return NULL != flash->set_4byte_mode;
128 +static inline int spi_flash_set_4byte_mode(struct spi_flash *flash)
130 + if (spi_flash_use_4byte_mode(flash))
131 + return flash->set_4byte_mode(flash);
136 +static inline int spi_flash_use_4byte_mode(struct spi_flash *flash)
141 +static inline int spi_flash_set_4byte_mode(struct spi_flash *flash)
147 /* Manufacturer-specific probe functions */
148 int spi_flash_probe_spansion(struct spi_flash *flash, u8 *idcode);
149 int spi_flash_probe_atmel(struct spi_flash *flash, u8 *idcode);
150 --- a/include/spi_flash.h
151 +++ b/include/spi_flash.h
152 @@ -46,6 +46,9 @@ struct spi_flash {
153 size_t len, const void *buf);
154 int (*erase)(struct spi_flash *flash, u32 offset,
156 +#ifdef CONFIG_SPI_FLASH_4BYTE_MODE
157 + int (*set_4byte_mode)(struct spi_flash *flash);
161 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,