Commit 056fbc73d54df64adcb93c513cba708acb2683bd

Authored by Jagannadha Sutradharudu Teki
1 parent f77f469117

sf: Add dual memories support - DUAL_PARALLEL

This patch added support for accessing dual memories in
parallel connection with single chipselect line from controller.

For more info - see doc/SPI/README.dual-flash

Signed-off-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>

Showing 5 changed files with 45 additions and 9 deletions Inline Diff

doc/SPI/README.dual-flash
1 SPI/QSPI Dual flash connection modes: 1 SPI/QSPI Dual flash connection modes:
2 ===================================== 2 =====================================
3 3
4 This describes how SPI/QSPI flash memories are connected to a given 4 This describes how SPI/QSPI flash memories are connected to a given
5 controller in a single chip select line. 5 controller in a single chip select line.
6 6
7 Current spi_flash framework supports, single flash memory connected 7 Current spi_flash framework supports, single flash memory connected
8 to a given controller with single chip select line, but there are some 8 to a given controller with single chip select line, but there are some
9 hw logics(ex: xilinx zynq qspi) that describes two/dual memories are 9 hw logics(ex: xilinx zynq qspi) that describes two/dual memories are
10 connected with a single chip select line from a controller. 10 connected with a single chip select line from a controller.
11 11
12 "dual_flash" from include/spi.h describes these types of connection mode 12 "dual_flash" from include/spi.h describes these types of connection mode
13 13
14 Possible connections: 14 Possible connections:
15 -------------------- 15 --------------------
16 SF_SINGLE_FLASH: 16 SF_SINGLE_FLASH:
17 - single spi flash memory connected with single chip select line. 17 - single spi flash memory connected with single chip select line.
18 18
19 +------------+ CS +---------------+ 19 +------------+ CS +---------------+
20 | |----------------------->| | 20 | |----------------------->| |
21 | Controller | I0[3:0] | Flash memory | 21 | Controller | I0[3:0] | Flash memory |
22 | SPI/QSPI |<======================>| (SPI/QSPI) | 22 | SPI/QSPI |<======================>| (SPI/QSPI) |
23 | | CLK | | 23 | | CLK | |
24 | |----------------------->| | 24 | |----------------------->| |
25 +------------+ +---------------+ 25 +------------+ +---------------+
26 26
27 SF_DUAL_STACKED_FLASH: 27 SF_DUAL_STACKED_FLASH:
28 - dual spi/qspi flash memories are connected with a single chipselect 28 - dual spi/qspi flash memories are connected with a single chipselect
29 line and these two memories are operating stacked fasion with shared buses. 29 line and these two memories are operating stacked fasion with shared buses.
30 - xilinx zynq qspi controller has implemented this feature [1] 30 - xilinx zynq qspi controller has implemented this feature [1]
31 31
32 +------------+ CS +---------------+ 32 +------------+ CS +---------------+
33 | |---------------------->| | 33 | |---------------------->| |
34 | | I0[3:0] | Upper Flash | 34 | | I0[3:0] | Upper Flash |
35 | | +=========>| memory | 35 | | +=========>| memory |
36 | | | CLK | (SPI/QSPI) | 36 | | | CLK | (SPI/QSPI) |
37 | | | +---->| | 37 | | | +---->| |
38 | Controller | CS | | +---------------+ 38 | Controller | CS | | +---------------+
39 | SPI/QSPI |------------|----|---->| | 39 | SPI/QSPI |------------|----|---->| |
40 | | I0[3:0] | | | Lower Flash | 40 | | I0[3:0] | | | Lower Flash |
41 | |<===========+====|====>| memory | 41 | |<===========+====|====>| memory |
42 | | CLK | | (SPI/QSPI) | 42 | | CLK | | (SPI/QSPI) |
43 | |-----------------+---->| | 43 | |-----------------+---->| |
44 +------------+ +---------------+ 44 +------------+ +---------------+
45 45
46 - two memory flash devices should has same hw part attributes (like size, 46 - two memory flash devices should has same hw part attributes (like size,
47 vendor..etc) 47 vendor..etc)
48 - Configurations: 48 - Configurations:
49 on LQSPI_CFG register, Enable TWO_MEM[BIT:30] on LQSPI_CFG 49 on LQSPI_CFG register, Enable TWO_MEM[BIT:30] on LQSPI_CFG
50 Enable U_PAGE[BIT:28] if U_PAGE flag set - upper memory 50 Enable U_PAGE[BIT:28] if U_PAGE flag set - upper memory
51 Disable U_PAGE[BIT:28] if U_PAGE flag unset - lower memory 51 Disable U_PAGE[BIT:28] if U_PAGE flag unset - lower memory
52 - Operation: 52 - Operation:
53 accessing memories serially like one after another. 53 accessing memories serially like one after another.
54 by default, if U_PAGE is unset lower memory should accessible, 54 by default, if U_PAGE is unset lower memory should accessible,
55 once user wants to access upper memory need to set U_PAGE. 55 once user wants to access upper memory need to set U_PAGE.
56 56
57 SPI_FLASH_CONN_DUALPARALLEL:
58 - dual spi/qspi flash memories are connected with a single chipselect
59 line and these two memories are operating parallel with separate buses.
60 - xilinx zynq qspi controller has implemented this feature [1]
61
62 +-------------+ CS +---------------+
63 | |---------------------->| |
64 | | I0[3:0] | Upper Flash |
65 | |<=====================>| memory |
66 | | CLK | (SPI/QSPI) |
67 | |---------------------->| |
68 | Controller | CS +---------------+
69 | SPI/QSPI |---------------------->| |
70 | | I0[3:0] | Lower Flash |
71 | |<=====================>| memory |
72 | | CLK | (SPI/QSPI) |
73 | |---------------------->| |
74 +-------------+ +---------------+
75
76 - two memory flash devices should has same hw part attributes (like size,
77 vendor..etc)
78 - Configurations:
79 Need to enable SEP_BUS[BIT:29],TWO_MEM[BIT:30] on LQSPI_CFG register.
80 - Operation:
81 Even bits, i.e. bit 0, 2, 4 ., of a data word is located in the lower memory
82 and odd bits, i.e. bit 1, 3, 5, ., of a data word is located in the upper memory.
83
57 Note: Technically there is only one CS line from the controller, but 84 Note: Technically there is only one CS line from the controller, but
58 zynq qspi controller has an internal hw logic to enable additional CS 85 zynq qspi controller has an internal hw logic to enable additional CS
59 when controller is configured for dual memories. 86 when controller is configured for dual memories.
60 87
61 [1] http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf 88 [1] http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf
62 89
63 -- 90 --
64 Jagannadha Sutradharudu Teki <jaganna@xilinx.com> 91 Jagannadha Sutradharudu Teki <jaganna@xilinx.com>
65 05-01-2014. 92 05-01-2014.
66 93
drivers/mtd/spi/sf_ops.c
1 /* 1 /*
2 * SPI flash operations 2 * SPI flash operations
3 * 3 *
4 * Copyright (C) 2008 Atmel Corporation 4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik 5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. 6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
7 * 7 *
8 * SPDX-License-Identifier: GPL-2.0+ 8 * SPDX-License-Identifier: GPL-2.0+
9 */ 9 */
10 10
11 #include <common.h> 11 #include <common.h>
12 #include <malloc.h> 12 #include <malloc.h>
13 #include <spi.h> 13 #include <spi.h>
14 #include <spi_flash.h> 14 #include <spi_flash.h>
15 #include <watchdog.h> 15 #include <watchdog.h>
16 16
17 #include "sf_internal.h" 17 #include "sf_internal.h"
18 18
19 static void spi_flash_addr(u32 addr, u8 *cmd) 19 static void spi_flash_addr(u32 addr, u8 *cmd)
20 { 20 {
21 /* cmd[0] is actual command */ 21 /* cmd[0] is actual command */
22 cmd[1] = addr >> 16; 22 cmd[1] = addr >> 16;
23 cmd[2] = addr >> 8; 23 cmd[2] = addr >> 8;
24 cmd[3] = addr >> 0; 24 cmd[3] = addr >> 0;
25 } 25 }
26 26
27 int spi_flash_cmd_read_status(struct spi_flash *flash, u8 *rs) 27 int spi_flash_cmd_read_status(struct spi_flash *flash, u8 *rs)
28 { 28 {
29 int ret; 29 int ret;
30 u8 cmd; 30 u8 cmd;
31 31
32 cmd = CMD_READ_STATUS; 32 cmd = CMD_READ_STATUS;
33 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1); 33 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
34 if (ret < 0) { 34 if (ret < 0) {
35 debug("SF: fail to read status register\n"); 35 debug("SF: fail to read status register\n");
36 return ret; 36 return ret;
37 } 37 }
38 38
39 return 0; 39 return 0;
40 } 40 }
41 41
42 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 ws) 42 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 ws)
43 { 43 {
44 u8 cmd; 44 u8 cmd;
45 int ret; 45 int ret;
46 46
47 cmd = CMD_WRITE_STATUS; 47 cmd = CMD_WRITE_STATUS;
48 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1); 48 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
49 if (ret < 0) { 49 if (ret < 0) {
50 debug("SF: fail to write status register\n"); 50 debug("SF: fail to write status register\n");
51 return ret; 51 return ret;
52 } 52 }
53 53
54 return 0; 54 return 0;
55 } 55 }
56 56
57 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 57 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
58 int spi_flash_cmd_read_config(struct spi_flash *flash, u8 *rc) 58 int spi_flash_cmd_read_config(struct spi_flash *flash, u8 *rc)
59 { 59 {
60 int ret; 60 int ret;
61 u8 cmd; 61 u8 cmd;
62 62
63 cmd = CMD_READ_CONFIG; 63 cmd = CMD_READ_CONFIG;
64 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1); 64 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
65 if (ret < 0) { 65 if (ret < 0) {
66 debug("SF: fail to read config register\n"); 66 debug("SF: fail to read config register\n");
67 return ret; 67 return ret;
68 } 68 }
69 69
70 return 0; 70 return 0;
71 } 71 }
72 72
73 int spi_flash_cmd_write_config(struct spi_flash *flash, u8 wc) 73 int spi_flash_cmd_write_config(struct spi_flash *flash, u8 wc)
74 { 74 {
75 u8 data[2]; 75 u8 data[2];
76 u8 cmd; 76 u8 cmd;
77 int ret; 77 int ret;
78 78
79 ret = spi_flash_cmd_read_status(flash, &data[0]); 79 ret = spi_flash_cmd_read_status(flash, &data[0]);
80 if (ret < 0) 80 if (ret < 0)
81 return ret; 81 return ret;
82 82
83 cmd = CMD_WRITE_STATUS; 83 cmd = CMD_WRITE_STATUS;
84 data[1] = wc; 84 data[1] = wc;
85 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2); 85 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
86 if (ret) { 86 if (ret) {
87 debug("SF: fail to write config register\n"); 87 debug("SF: fail to write config register\n");
88 return ret; 88 return ret;
89 } 89 }
90 90
91 return 0; 91 return 0;
92 } 92 }
93 #endif 93 #endif
94 94
95 #ifdef CONFIG_SPI_FLASH_BAR 95 #ifdef CONFIG_SPI_FLASH_BAR
96 static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel) 96 static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
97 { 97 {
98 u8 cmd; 98 u8 cmd;
99 int ret; 99 int ret;
100 100
101 if (flash->bank_curr == bank_sel) { 101 if (flash->bank_curr == bank_sel) {
102 debug("SF: not require to enable bank%d\n", bank_sel); 102 debug("SF: not require to enable bank%d\n", bank_sel);
103 return 0; 103 return 0;
104 } 104 }
105 105
106 cmd = flash->bank_write_cmd; 106 cmd = flash->bank_write_cmd;
107 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1); 107 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
108 if (ret < 0) { 108 if (ret < 0) {
109 debug("SF: fail to write bank register\n"); 109 debug("SF: fail to write bank register\n");
110 return ret; 110 return ret;
111 } 111 }
112 flash->bank_curr = bank_sel; 112 flash->bank_curr = bank_sel;
113 113
114 return 0; 114 return 0;
115 } 115 }
116 116
117 static int spi_flash_bank(struct spi_flash *flash, u32 offset) 117 static int spi_flash_bank(struct spi_flash *flash, u32 offset)
118 { 118 {
119 u8 bank_sel; 119 u8 bank_sel;
120 int ret; 120 int ret;
121 121
122 bank_sel = offset / SPI_FLASH_16MB_BOUN; 122 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
123 123
124 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel); 124 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
125 if (ret) { 125 if (ret) {
126 debug("SF: fail to set bank%d\n", bank_sel); 126 debug("SF: fail to set bank%d\n", bank_sel);
127 return ret; 127 return ret;
128 } 128 }
129 129
130 return bank_sel; 130 return bank_sel;
131 } 131 }
132 #endif 132 #endif
133 133
134 static void spi_flash_dual_flash(struct spi_flash *flash, u32 *addr) 134 static void spi_flash_dual_flash(struct spi_flash *flash, u32 *addr)
135 { 135 {
136 switch (flash->dual_flash) { 136 switch (flash->dual_flash) {
137 case SF_DUAL_STACKED_FLASH: 137 case SF_DUAL_STACKED_FLASH:
138 if (*addr >= (flash->size >> 1)) { 138 if (*addr >= (flash->size >> 1)) {
139 *addr -= flash->size >> 1; 139 *addr -= flash->size >> 1;
140 flash->spi->flags |= SPI_XFER_U_PAGE; 140 flash->spi->flags |= SPI_XFER_U_PAGE;
141 } else { 141 } else {
142 flash->spi->flags &= ~SPI_XFER_U_PAGE; 142 flash->spi->flags &= ~SPI_XFER_U_PAGE;
143 } 143 }
144 break; 144 break;
145 case SF_DUAL_PARALLEL_FLASH:
146 *addr >>= flash->shift;
147 break;
145 default: 148 default:
146 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash); 149 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
147 break; 150 break;
148 } 151 }
149 } 152 }
150 153
151 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout) 154 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
152 { 155 {
153 struct spi_slave *spi = flash->spi; 156 struct spi_slave *spi = flash->spi;
154 unsigned long timebase; 157 unsigned long timebase;
155 unsigned long flags = SPI_XFER_BEGIN; 158 unsigned long flags = SPI_XFER_BEGIN;
156 int ret; 159 int ret;
157 u8 status; 160 u8 status;
158 u8 check_status = 0x0; 161 u8 check_status = 0x0;
159 u8 poll_bit = STATUS_WIP; 162 u8 poll_bit = STATUS_WIP;
160 u8 cmd = flash->poll_cmd; 163 u8 cmd = flash->poll_cmd;
161 164
162 if (cmd == CMD_FLAG_STATUS) { 165 if (cmd == CMD_FLAG_STATUS) {
163 poll_bit = STATUS_PEC; 166 poll_bit = STATUS_PEC;
164 check_status = poll_bit; 167 check_status = poll_bit;
165 } 168 }
166 169
167 if (spi->flags & SPI_XFER_U_PAGE) 170 if (spi->flags & SPI_XFER_U_PAGE)
168 flags |= SPI_XFER_U_PAGE; 171 flags |= SPI_XFER_U_PAGE;
169 172
170 ret = spi_xfer(spi, 8, &cmd, NULL, flags); 173 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
171 if (ret) { 174 if (ret) {
172 debug("SF: fail to read %s status register\n", 175 debug("SF: fail to read %s status register\n",
173 cmd == CMD_READ_STATUS ? "read" : "flag"); 176 cmd == CMD_READ_STATUS ? "read" : "flag");
174 return ret; 177 return ret;
175 } 178 }
176 179
177 timebase = get_timer(0); 180 timebase = get_timer(0);
178 do { 181 do {
179 WATCHDOG_RESET(); 182 WATCHDOG_RESET();
180 183
181 ret = spi_xfer(spi, 8, NULL, &status, 0); 184 ret = spi_xfer(spi, 8, NULL, &status, 0);
182 if (ret) 185 if (ret)
183 return -1; 186 return -1;
184 187
185 if ((status & poll_bit) == check_status) 188 if ((status & poll_bit) == check_status)
186 break; 189 break;
187 190
188 } while (get_timer(timebase) < timeout); 191 } while (get_timer(timebase) < timeout);
189 192
190 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); 193 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
191 194
192 if ((status & poll_bit) == check_status) 195 if ((status & poll_bit) == check_status)
193 return 0; 196 return 0;
194 197
195 /* Timed out */ 198 /* Timed out */
196 debug("SF: time out!\n"); 199 debug("SF: time out!\n");
197 return -1; 200 return -1;
198 } 201 }
199 202
200 int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd, 203 int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
201 size_t cmd_len, const void *buf, size_t buf_len) 204 size_t cmd_len, const void *buf, size_t buf_len)
202 { 205 {
203 struct spi_slave *spi = flash->spi; 206 struct spi_slave *spi = flash->spi;
204 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT; 207 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
205 int ret; 208 int ret;
206 209
207 if (buf == NULL) 210 if (buf == NULL)
208 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT; 211 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
209 212
210 ret = spi_claim_bus(flash->spi); 213 ret = spi_claim_bus(flash->spi);
211 if (ret) { 214 if (ret) {
212 debug("SF: unable to claim SPI bus\n"); 215 debug("SF: unable to claim SPI bus\n");
213 return ret; 216 return ret;
214 } 217 }
215 218
216 ret = spi_flash_cmd_write_enable(flash); 219 ret = spi_flash_cmd_write_enable(flash);
217 if (ret < 0) { 220 if (ret < 0) {
218 debug("SF: enabling write failed\n"); 221 debug("SF: enabling write failed\n");
219 return ret; 222 return ret;
220 } 223 }
221 224
222 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len); 225 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
223 if (ret < 0) { 226 if (ret < 0) {
224 debug("SF: write cmd failed\n"); 227 debug("SF: write cmd failed\n");
225 return ret; 228 return ret;
226 } 229 }
227 230
228 ret = spi_flash_cmd_wait_ready(flash, timeout); 231 ret = spi_flash_cmd_wait_ready(flash, timeout);
229 if (ret < 0) { 232 if (ret < 0) {
230 debug("SF: write %s timed out\n", 233 debug("SF: write %s timed out\n",
231 timeout == SPI_FLASH_PROG_TIMEOUT ? 234 timeout == SPI_FLASH_PROG_TIMEOUT ?
232 "program" : "page erase"); 235 "program" : "page erase");
233 return ret; 236 return ret;
234 } 237 }
235 238
236 spi_release_bus(spi); 239 spi_release_bus(spi);
237 240
238 return ret; 241 return ret;
239 } 242 }
240 243
241 int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len) 244 int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
242 { 245 {
243 u32 erase_size, erase_addr; 246 u32 erase_size, erase_addr;
244 u8 cmd[SPI_FLASH_CMD_LEN]; 247 u8 cmd[SPI_FLASH_CMD_LEN];
245 int ret = -1; 248 int ret = -1;
246 249
247 erase_size = flash->erase_size; 250 erase_size = flash->erase_size;
248 if (offset % erase_size || len % erase_size) { 251 if (offset % erase_size || len % erase_size) {
249 debug("SF: Erase offset/length not multiple of erase size\n"); 252 debug("SF: Erase offset/length not multiple of erase size\n");
250 return -1; 253 return -1;
251 } 254 }
252 255
253 cmd[0] = flash->erase_cmd; 256 cmd[0] = flash->erase_cmd;
254 while (len) { 257 while (len) {
255 erase_addr = offset; 258 erase_addr = offset;
256 259
257 if (flash->dual_flash > SF_SINGLE_FLASH) 260 if (flash->dual_flash > SF_SINGLE_FLASH)
258 spi_flash_dual_flash(flash, &erase_addr); 261 spi_flash_dual_flash(flash, &erase_addr);
259 262
260 #ifdef CONFIG_SPI_FLASH_BAR 263 #ifdef CONFIG_SPI_FLASH_BAR
261 ret = spi_flash_bank(flash, erase_addr); 264 ret = spi_flash_bank(flash, erase_addr);
262 if (ret < 0) 265 if (ret < 0)
263 return ret; 266 return ret;
264 #endif 267 #endif
265 spi_flash_addr(erase_addr, cmd); 268 spi_flash_addr(erase_addr, cmd);
266 269
267 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], 270 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
268 cmd[2], cmd[3], erase_addr); 271 cmd[2], cmd[3], erase_addr);
269 272
270 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0); 273 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
271 if (ret < 0) { 274 if (ret < 0) {
272 debug("SF: erase failed\n"); 275 debug("SF: erase failed\n");
273 break; 276 break;
274 } 277 }
275 278
276 offset += erase_size; 279 offset += erase_size;
277 len -= erase_size; 280 len -= erase_size;
278 } 281 }
279 282
280 return ret; 283 return ret;
281 } 284 }
282 285
283 int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset, 286 int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
284 size_t len, const void *buf) 287 size_t len, const void *buf)
285 { 288 {
286 unsigned long byte_addr, page_size; 289 unsigned long byte_addr, page_size;
287 u32 write_addr; 290 u32 write_addr;
288 size_t chunk_len, actual; 291 size_t chunk_len, actual;
289 u8 cmd[SPI_FLASH_CMD_LEN]; 292 u8 cmd[SPI_FLASH_CMD_LEN];
290 int ret = -1; 293 int ret = -1;
291 294
292 page_size = flash->page_size; 295 page_size = flash->page_size;
293 296
294 cmd[0] = flash->write_cmd; 297 cmd[0] = flash->write_cmd;
295 for (actual = 0; actual < len; actual += chunk_len) { 298 for (actual = 0; actual < len; actual += chunk_len) {
296 write_addr = offset; 299 write_addr = offset;
297 300
298 if (flash->dual_flash > SF_SINGLE_FLASH) 301 if (flash->dual_flash > SF_SINGLE_FLASH)
299 spi_flash_dual_flash(flash, &write_addr); 302 spi_flash_dual_flash(flash, &write_addr);
300 303
301 #ifdef CONFIG_SPI_FLASH_BAR 304 #ifdef CONFIG_SPI_FLASH_BAR
302 ret = spi_flash_bank(flash, write_addr); 305 ret = spi_flash_bank(flash, write_addr);
303 if (ret < 0) 306 if (ret < 0)
304 return ret; 307 return ret;
305 #endif 308 #endif
306 byte_addr = offset % page_size; 309 byte_addr = offset % page_size;
307 chunk_len = min(len - actual, page_size - byte_addr); 310 chunk_len = min(len - actual, page_size - byte_addr);
308 311
309 if (flash->spi->max_write_size) 312 if (flash->spi->max_write_size)
310 chunk_len = min(chunk_len, flash->spi->max_write_size); 313 chunk_len = min(chunk_len, flash->spi->max_write_size);
311 314
312 spi_flash_addr(write_addr, cmd); 315 spi_flash_addr(write_addr, cmd);
313 316
314 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n", 317 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
315 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); 318 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
316 319
317 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), 320 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
318 buf + actual, chunk_len); 321 buf + actual, chunk_len);
319 if (ret < 0) { 322 if (ret < 0) {
320 debug("SF: write failed\n"); 323 debug("SF: write failed\n");
321 break; 324 break;
322 } 325 }
323 326
324 offset += chunk_len; 327 offset += chunk_len;
325 } 328 }
326 329
327 return ret; 330 return ret;
328 } 331 }
329 332
330 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, 333 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
331 size_t cmd_len, void *data, size_t data_len) 334 size_t cmd_len, void *data, size_t data_len)
332 { 335 {
333 struct spi_slave *spi = flash->spi; 336 struct spi_slave *spi = flash->spi;
334 int ret; 337 int ret;
335 338
336 ret = spi_claim_bus(flash->spi); 339 ret = spi_claim_bus(flash->spi);
337 if (ret) { 340 if (ret) {
338 debug("SF: unable to claim SPI bus\n"); 341 debug("SF: unable to claim SPI bus\n");
339 return ret; 342 return ret;
340 } 343 }
341 344
342 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); 345 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
343 if (ret < 0) { 346 if (ret < 0) {
344 debug("SF: read cmd failed\n"); 347 debug("SF: read cmd failed\n");
345 return ret; 348 return ret;
346 } 349 }
347 350
348 spi_release_bus(spi); 351 spi_release_bus(spi);
349 352
350 return ret; 353 return ret;
351 } 354 }
352 355
353 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset, 356 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
354 size_t len, void *data) 357 size_t len, void *data)
355 { 358 {
356 u8 *cmd, cmdsz; 359 u8 *cmd, cmdsz;
357 u32 remain_len, read_len, read_addr; 360 u32 remain_len, read_len, read_addr;
358 int bank_sel = 0; 361 int bank_sel = 0;
359 int ret = -1; 362 int ret = -1;
360 363
361 /* Handle memory-mapped SPI */ 364 /* Handle memory-mapped SPI */
362 if (flash->memory_map) { 365 if (flash->memory_map) {
363 ret = spi_claim_bus(flash->spi); 366 ret = spi_claim_bus(flash->spi);
364 if (ret) { 367 if (ret) {
365 debug("SF: unable to claim SPI bus\n"); 368 debug("SF: unable to claim SPI bus\n");
366 return ret; 369 return ret;
367 } 370 }
368 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP); 371 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
369 memcpy(data, flash->memory_map + offset, len); 372 memcpy(data, flash->memory_map + offset, len);
370 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END); 373 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
371 spi_release_bus(flash->spi); 374 spi_release_bus(flash->spi);
372 return 0; 375 return 0;
373 } 376 }
374 377
375 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte; 378 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
376 cmd = malloc(cmdsz); 379 cmd = malloc(cmdsz);
377 memset(cmd, 0, cmdsz); 380 memset(cmd, 0, cmdsz);
378 381
379 cmd[0] = flash->read_cmd; 382 cmd[0] = flash->read_cmd;
380 while (len) { 383 while (len) {
381 read_addr = offset; 384 read_addr = offset;
382 385
383 if (flash->dual_flash > SF_SINGLE_FLASH) 386 if (flash->dual_flash > SF_SINGLE_FLASH)
384 spi_flash_dual_flash(flash, &read_addr); 387 spi_flash_dual_flash(flash, &read_addr);
385 388
386 #ifdef CONFIG_SPI_FLASH_BAR 389 #ifdef CONFIG_SPI_FLASH_BAR
387 bank_sel = spi_flash_bank(flash, read_addr); 390 bank_sel = spi_flash_bank(flash, read_addr);
388 if (bank_sel < 0) 391 if (bank_sel < 0)
389 return ret; 392 return ret;
390 #endif 393 #endif
391 remain_len = (SPI_FLASH_16MB_BOUN * (bank_sel + 1)) - offset; 394 remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
395 (bank_sel + 1)) - offset;
392 if (len < remain_len) 396 if (len < remain_len)
393 read_len = len; 397 read_len = len;
394 else 398 else
395 read_len = remain_len; 399 read_len = remain_len;
396 400
397 spi_flash_addr(read_addr, cmd); 401 spi_flash_addr(read_addr, cmd);
398 402
399 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len); 403 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
400 if (ret < 0) { 404 if (ret < 0) {
401 debug("SF: read failed\n"); 405 debug("SF: read failed\n");
402 break; 406 break;
403 } 407 }
404 408
405 offset += read_len; 409 offset += read_len;
406 len -= read_len; 410 len -= read_len;
407 data += read_len; 411 data += read_len;
408 } 412 }
409 413
410 return ret; 414 return ret;
411 } 415 }
412 416
413 #ifdef CONFIG_SPI_FLASH_SST 417 #ifdef CONFIG_SPI_FLASH_SST
414 static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf) 418 static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
415 { 419 {
416 int ret; 420 int ret;
417 u8 cmd[4] = { 421 u8 cmd[4] = {
418 CMD_SST_BP, 422 CMD_SST_BP,
419 offset >> 16, 423 offset >> 16,
420 offset >> 8, 424 offset >> 8,
421 offset, 425 offset,
422 }; 426 };
423 427
424 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n", 428 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
425 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset); 429 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
426 430
427 ret = spi_flash_cmd_write_enable(flash); 431 ret = spi_flash_cmd_write_enable(flash);
428 if (ret) 432 if (ret)
429 return ret; 433 return ret;
430 434
431 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1); 435 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
432 if (ret) 436 if (ret)
433 return ret; 437 return ret;
434 438
435 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); 439 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
436 } 440 }
437 441
438 int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, 442 int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
439 const void *buf) 443 const void *buf)
440 { 444 {
441 size_t actual, cmd_len; 445 size_t actual, cmd_len;
442 int ret; 446 int ret;
443 u8 cmd[4]; 447 u8 cmd[4];
444 448
445 ret = spi_claim_bus(flash->spi); 449 ret = spi_claim_bus(flash->spi);
446 if (ret) { 450 if (ret) {
447 debug("SF: Unable to claim SPI bus\n"); 451 debug("SF: Unable to claim SPI bus\n");
448 return ret; 452 return ret;
449 } 453 }
450 454
451 /* If the data is not word aligned, write out leading single byte */ 455 /* If the data is not word aligned, write out leading single byte */
452 actual = offset % 2; 456 actual = offset % 2;
453 if (actual) { 457 if (actual) {
454 ret = sst_byte_write(flash, offset, buf); 458 ret = sst_byte_write(flash, offset, buf);
455 if (ret) 459 if (ret)
456 goto done; 460 goto done;
457 } 461 }
458 offset += actual; 462 offset += actual;
459 463
460 ret = spi_flash_cmd_write_enable(flash); 464 ret = spi_flash_cmd_write_enable(flash);
461 if (ret) 465 if (ret)
462 goto done; 466 goto done;
463 467
464 cmd_len = 4; 468 cmd_len = 4;
465 cmd[0] = CMD_SST_AAI_WP; 469 cmd[0] = CMD_SST_AAI_WP;
466 cmd[1] = offset >> 16; 470 cmd[1] = offset >> 16;
467 cmd[2] = offset >> 8; 471 cmd[2] = offset >> 8;
468 cmd[3] = offset; 472 cmd[3] = offset;
469 473
470 for (; actual < len - 1; actual += 2) { 474 for (; actual < len - 1; actual += 2) {
471 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n", 475 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
472 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual, 476 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
473 cmd[0], offset); 477 cmd[0], offset);
474 478
475 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len, 479 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
476 buf + actual, 2); 480 buf + actual, 2);
477 if (ret) { 481 if (ret) {
478 debug("SF: sst word program failed\n"); 482 debug("SF: sst word program failed\n");
479 break; 483 break;
480 } 484 }
481 485
482 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); 486 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
483 if (ret) 487 if (ret)
484 break; 488 break;
485 489
486 cmd_len = 1; 490 cmd_len = 1;
487 offset += 2; 491 offset += 2;
488 } 492 }
489 493
490 if (!ret) 494 if (!ret)
491 ret = spi_flash_cmd_write_disable(flash); 495 ret = spi_flash_cmd_write_disable(flash);
492 496
493 /* If there is a single trailing byte, write it out */ 497 /* If there is a single trailing byte, write it out */
494 if (!ret && actual != len) 498 if (!ret && actual != len)
495 ret = sst_byte_write(flash, offset, buf + actual); 499 ret = sst_byte_write(flash, offset, buf + actual);
496 500
497 done: 501 done:
498 debug("SF: sst: program %s %zu bytes @ 0x%zx\n", 502 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
499 ret ? "failure" : "success", len, offset - actual); 503 ret ? "failure" : "success", len, offset - actual);
500 504
501 spi_release_bus(flash->spi); 505 spi_release_bus(flash->spi);
502 return ret; 506 return ret;
503 } 507 }
504 #endif 508 #endif
505 509
drivers/mtd/spi/sf_probe.c
1 /* 1 /*
2 * SPI flash probing 2 * SPI flash probing
3 * 3 *
4 * Copyright (C) 2008 Atmel Corporation 4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik 5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. 6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
7 * 7 *
8 * SPDX-License-Identifier: GPL-2.0+ 8 * SPDX-License-Identifier: GPL-2.0+
9 */ 9 */
10 10
11 #include <common.h> 11 #include <common.h>
12 #include <fdtdec.h> 12 #include <fdtdec.h>
13 #include <malloc.h> 13 #include <malloc.h>
14 #include <spi.h> 14 #include <spi.h>
15 #include <spi_flash.h> 15 #include <spi_flash.h>
16 #include <asm/io.h> 16 #include <asm/io.h>
17 17
18 #include "sf_internal.h" 18 #include "sf_internal.h"
19 19
20 DECLARE_GLOBAL_DATA_PTR; 20 DECLARE_GLOBAL_DATA_PTR;
21 21
22 /* Read commands array */ 22 /* Read commands array */
23 static u8 spi_read_cmds_array[] = { 23 static u8 spi_read_cmds_array[] = {
24 CMD_READ_ARRAY_SLOW, 24 CMD_READ_ARRAY_SLOW,
25 CMD_READ_DUAL_OUTPUT_FAST, 25 CMD_READ_DUAL_OUTPUT_FAST,
26 CMD_READ_DUAL_IO_FAST, 26 CMD_READ_DUAL_IO_FAST,
27 CMD_READ_QUAD_OUTPUT_FAST, 27 CMD_READ_QUAD_OUTPUT_FAST,
28 CMD_READ_QUAD_IO_FAST, 28 CMD_READ_QUAD_IO_FAST,
29 }; 29 };
30 30
31 #ifdef CONFIG_SPI_FLASH_MACRONIX 31 #ifdef CONFIG_SPI_FLASH_MACRONIX
32 static int spi_flash_set_qeb_mxic(struct spi_flash *flash) 32 static int spi_flash_set_qeb_mxic(struct spi_flash *flash)
33 { 33 {
34 u8 qeb_status; 34 u8 qeb_status;
35 int ret; 35 int ret;
36 36
37 ret = spi_flash_cmd_read_status(flash, &qeb_status); 37 ret = spi_flash_cmd_read_status(flash, &qeb_status);
38 if (ret < 0) 38 if (ret < 0)
39 return ret; 39 return ret;
40 40
41 if (qeb_status & STATUS_QEB_MXIC) { 41 if (qeb_status & STATUS_QEB_MXIC) {
42 debug("SF: mxic: QEB is already set\n"); 42 debug("SF: mxic: QEB is already set\n");
43 } else { 43 } else {
44 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC); 44 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC);
45 if (ret < 0) 45 if (ret < 0)
46 return ret; 46 return ret;
47 } 47 }
48 48
49 return ret; 49 return ret;
50 } 50 }
51 #endif 51 #endif
52 52
53 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 53 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
54 static int spi_flash_set_qeb_winspan(struct spi_flash *flash) 54 static int spi_flash_set_qeb_winspan(struct spi_flash *flash)
55 { 55 {
56 u8 qeb_status; 56 u8 qeb_status;
57 int ret; 57 int ret;
58 58
59 ret = spi_flash_cmd_read_config(flash, &qeb_status); 59 ret = spi_flash_cmd_read_config(flash, &qeb_status);
60 if (ret < 0) 60 if (ret < 0)
61 return ret; 61 return ret;
62 62
63 if (qeb_status & STATUS_QEB_WINSPAN) { 63 if (qeb_status & STATUS_QEB_WINSPAN) {
64 debug("SF: winspan: QEB is already set\n"); 64 debug("SF: winspan: QEB is already set\n");
65 } else { 65 } else {
66 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN); 66 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
67 if (ret < 0) 67 if (ret < 0)
68 return ret; 68 return ret;
69 } 69 }
70 70
71 return ret; 71 return ret;
72 } 72 }
73 #endif 73 #endif
74 74
75 static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0) 75 static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
76 { 76 {
77 switch (idcode0) { 77 switch (idcode0) {
78 #ifdef CONFIG_SPI_FLASH_MACRONIX 78 #ifdef CONFIG_SPI_FLASH_MACRONIX
79 case SPI_FLASH_CFI_MFR_MACRONIX: 79 case SPI_FLASH_CFI_MFR_MACRONIX:
80 return spi_flash_set_qeb_mxic(flash); 80 return spi_flash_set_qeb_mxic(flash);
81 #endif 81 #endif
82 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 82 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
83 case SPI_FLASH_CFI_MFR_SPANSION: 83 case SPI_FLASH_CFI_MFR_SPANSION:
84 case SPI_FLASH_CFI_MFR_WINBOND: 84 case SPI_FLASH_CFI_MFR_WINBOND:
85 return spi_flash_set_qeb_winspan(flash); 85 return spi_flash_set_qeb_winspan(flash);
86 #endif 86 #endif
87 #ifdef CONFIG_SPI_FLASH_STMICRO 87 #ifdef CONFIG_SPI_FLASH_STMICRO
88 case SPI_FLASH_CFI_MFR_STMICRO: 88 case SPI_FLASH_CFI_MFR_STMICRO:
89 debug("SF: QEB is volatile for %02x flash\n", idcode0); 89 debug("SF: QEB is volatile for %02x flash\n", idcode0);
90 return 0; 90 return 0;
91 #endif 91 #endif
92 default: 92 default:
93 printf("SF: Need set QEB func for %02x flash\n", idcode0); 93 printf("SF: Need set QEB func for %02x flash\n", idcode0);
94 return -1; 94 return -1;
95 } 95 }
96 } 96 }
97 97
98 static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi, 98 static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
99 u8 *idcode) 99 u8 *idcode)
100 { 100 {
101 const struct spi_flash_params *params; 101 const struct spi_flash_params *params;
102 struct spi_flash *flash; 102 struct spi_flash *flash;
103 u8 cmd; 103 u8 cmd;
104 u16 jedec = idcode[1] << 8 | idcode[2]; 104 u16 jedec = idcode[1] << 8 | idcode[2];
105 u16 ext_jedec = idcode[3] << 8 | idcode[4]; 105 u16 ext_jedec = idcode[3] << 8 | idcode[4];
106 106
107 params = spi_flash_params_table; 107 params = spi_flash_params_table;
108 for (; params->name != NULL; params++) { 108 for (; params->name != NULL; params++) {
109 if ((params->jedec >> 16) == idcode[0]) { 109 if ((params->jedec >> 16) == idcode[0]) {
110 if ((params->jedec & 0xFFFF) == jedec) { 110 if ((params->jedec & 0xFFFF) == jedec) {
111 if (params->ext_jedec == 0) 111 if (params->ext_jedec == 0)
112 break; 112 break;
113 else if (params->ext_jedec == ext_jedec) 113 else if (params->ext_jedec == ext_jedec)
114 break; 114 break;
115 } 115 }
116 } 116 }
117 } 117 }
118 118
119 if (!params->name) { 119 if (!params->name) {
120 printf("SF: Unsupported flash IDs: "); 120 printf("SF: Unsupported flash IDs: ");
121 printf("manuf %02x, jedec %04x, ext_jedec %04x\n", 121 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
122 idcode[0], jedec, ext_jedec); 122 idcode[0], jedec, ext_jedec);
123 return NULL; 123 return NULL;
124 } 124 }
125 125
126 flash = malloc(sizeof(*flash)); 126 flash = malloc(sizeof(*flash));
127 if (!flash) { 127 if (!flash) {
128 debug("SF: Failed to allocate spi_flash\n"); 128 debug("SF: Failed to allocate spi_flash\n");
129 return NULL; 129 return NULL;
130 } 130 }
131 memset(flash, '\0', sizeof(*flash)); 131 memset(flash, '\0', sizeof(*flash));
132 132
133 /* Assign spi data */ 133 /* Assign spi data */
134 flash->spi = spi; 134 flash->spi = spi;
135 flash->name = params->name; 135 flash->name = params->name;
136 flash->memory_map = spi->memory_map; 136 flash->memory_map = spi->memory_map;
137 flash->dual_flash = flash->spi->option; 137 flash->dual_flash = flash->spi->option;
138 138
139 /* Assign spi_flash ops */ 139 /* Assign spi_flash ops */
140 flash->write = spi_flash_cmd_write_ops; 140 flash->write = spi_flash_cmd_write_ops;
141 #ifdef CONFIG_SPI_FLASH_SST 141 #ifdef CONFIG_SPI_FLASH_SST
142 if (params->flags & SST_WP) 142 if (params->flags & SST_WP)
143 flash->write = sst_write_wp; 143 flash->write = sst_write_wp;
144 #endif 144 #endif
145 flash->erase = spi_flash_cmd_erase_ops; 145 flash->erase = spi_flash_cmd_erase_ops;
146 flash->read = spi_flash_cmd_read_ops; 146 flash->read = spi_flash_cmd_read_ops;
147 147
148 /* Compute the flash size */ 148 /* Compute the flash size */
149 flash->page_size = (ext_jedec == 0x4d00) ? 512 : 256; 149 flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
150 flash->sector_size = params->sector_size; 150 flash->page_size = ((ext_jedec == 0x4d00) ? 512 : 256) << flash->shift;
151 flash->size = flash->sector_size * params->nr_sectors; 151 flash->sector_size = params->sector_size << flash->shift;
152 flash->size = flash->sector_size * params->nr_sectors << flash->shift;
152 if (flash->dual_flash & SF_DUAL_STACKED_FLASH) 153 if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
153 flash->size <<= 1; 154 flash->size <<= 1;
154 155
155 /* Compute erase sector and command */ 156 /* Compute erase sector and command */
156 if (params->flags & SECT_4K) { 157 if (params->flags & SECT_4K) {
157 flash->erase_cmd = CMD_ERASE_4K; 158 flash->erase_cmd = CMD_ERASE_4K;
158 flash->erase_size = 4096; 159 flash->erase_size = 4096 << flash->shift;
159 } else if (params->flags & SECT_32K) { 160 } else if (params->flags & SECT_32K) {
160 flash->erase_cmd = CMD_ERASE_32K; 161 flash->erase_cmd = CMD_ERASE_32K;
161 flash->erase_size = 32768; 162 flash->erase_size = 32768 << flash->shift;
162 } else { 163 } else {
163 flash->erase_cmd = CMD_ERASE_64K; 164 flash->erase_cmd = CMD_ERASE_64K;
164 flash->erase_size = flash->sector_size; 165 flash->erase_size = flash->sector_size;
165 } 166 }
166 167
167 /* Look for the fastest read cmd */ 168 /* Look for the fastest read cmd */
168 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx); 169 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
169 if (cmd) { 170 if (cmd) {
170 cmd = spi_read_cmds_array[cmd - 1]; 171 cmd = spi_read_cmds_array[cmd - 1];
171 flash->read_cmd = cmd; 172 flash->read_cmd = cmd;
172 } else { 173 } else {
173 /* Go for default supported read cmd */ 174 /* Go for default supported read cmd */
174 flash->read_cmd = CMD_READ_ARRAY_FAST; 175 flash->read_cmd = CMD_READ_ARRAY_FAST;
175 } 176 }
176 177
177 /* Not require to look for fastest only two write cmds yet */ 178 /* Not require to look for fastest only two write cmds yet */
178 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP) 179 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
179 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM; 180 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
180 else 181 else
181 /* Go for default supported write cmd */ 182 /* Go for default supported write cmd */
182 flash->write_cmd = CMD_PAGE_PROGRAM; 183 flash->write_cmd = CMD_PAGE_PROGRAM;
183 184
184 /* Set the quad enable bit - only for quad commands */ 185 /* Set the quad enable bit - only for quad commands */
185 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) || 186 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
186 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) || 187 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
187 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) { 188 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
188 if (spi_flash_set_qeb(flash, idcode[0])) { 189 if (spi_flash_set_qeb(flash, idcode[0])) {
189 debug("SF: Fail to set QEB for %02x\n", idcode[0]); 190 debug("SF: Fail to set QEB for %02x\n", idcode[0]);
190 return NULL; 191 return NULL;
191 } 192 }
192 } 193 }
193 194
194 /* Read dummy_byte: dummy byte is determined based on the 195 /* Read dummy_byte: dummy byte is determined based on the
195 * dummy cycles of a particular command. 196 * dummy cycles of a particular command.
196 * Fast commands - dummy_byte = dummy_cycles/8 197 * Fast commands - dummy_byte = dummy_cycles/8
197 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8 198 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
198 * For I/O commands except cmd[0] everything goes on no.of lines 199 * For I/O commands except cmd[0] everything goes on no.of lines
199 * based on particular command but incase of fast commands except 200 * based on particular command but incase of fast commands except
200 * data all go on single line irrespective of command. 201 * data all go on single line irrespective of command.
201 */ 202 */
202 switch (flash->read_cmd) { 203 switch (flash->read_cmd) {
203 case CMD_READ_QUAD_IO_FAST: 204 case CMD_READ_QUAD_IO_FAST:
204 flash->dummy_byte = 2; 205 flash->dummy_byte = 2;
205 break; 206 break;
206 case CMD_READ_ARRAY_SLOW: 207 case CMD_READ_ARRAY_SLOW:
207 flash->dummy_byte = 0; 208 flash->dummy_byte = 0;
208 break; 209 break;
209 default: 210 default:
210 flash->dummy_byte = 1; 211 flash->dummy_byte = 1;
211 } 212 }
212 213
213 /* Poll cmd selection */ 214 /* Poll cmd selection */
214 flash->poll_cmd = CMD_READ_STATUS; 215 flash->poll_cmd = CMD_READ_STATUS;
215 #ifdef CONFIG_SPI_FLASH_STMICRO 216 #ifdef CONFIG_SPI_FLASH_STMICRO
216 if (params->flags & E_FSR) 217 if (params->flags & E_FSR)
217 flash->poll_cmd = CMD_FLAG_STATUS; 218 flash->poll_cmd = CMD_FLAG_STATUS;
218 #endif 219 #endif
219 220
220 /* Configure the BAR - discover bank cmds and read current bank */ 221 /* Configure the BAR - discover bank cmds and read current bank */
221 #ifdef CONFIG_SPI_FLASH_BAR 222 #ifdef CONFIG_SPI_FLASH_BAR
222 u8 curr_bank = 0; 223 u8 curr_bank = 0;
223 if (flash->size > SPI_FLASH_16MB_BOUN) { 224 if (flash->size > SPI_FLASH_16MB_BOUN) {
224 flash->bank_read_cmd = (idcode[0] == 0x01) ? 225 flash->bank_read_cmd = (idcode[0] == 0x01) ?
225 CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR; 226 CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
226 flash->bank_write_cmd = (idcode[0] == 0x01) ? 227 flash->bank_write_cmd = (idcode[0] == 0x01) ?
227 CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR; 228 CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
228 229
229 if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1, 230 if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
230 &curr_bank, 1)) { 231 &curr_bank, 1)) {
231 debug("SF: fail to read bank addr register\n"); 232 debug("SF: fail to read bank addr register\n");
232 return NULL; 233 return NULL;
233 } 234 }
234 flash->bank_curr = curr_bank; 235 flash->bank_curr = curr_bank;
235 } else { 236 } else {
236 flash->bank_curr = curr_bank; 237 flash->bank_curr = curr_bank;
237 } 238 }
238 #endif 239 #endif
239 240
240 /* Flash powers up read-only, so clear BP# bits */ 241 /* Flash powers up read-only, so clear BP# bits */
241 #if defined(CONFIG_SPI_FLASH_ATMEL) || \ 242 #if defined(CONFIG_SPI_FLASH_ATMEL) || \
242 defined(CONFIG_SPI_FLASH_MACRONIX) || \ 243 defined(CONFIG_SPI_FLASH_MACRONIX) || \
243 defined(CONFIG_SPI_FLASH_SST) 244 defined(CONFIG_SPI_FLASH_SST)
244 spi_flash_cmd_write_status(flash, 0); 245 spi_flash_cmd_write_status(flash, 0);
245 #endif 246 #endif
246 247
247 return flash; 248 return flash;
248 } 249 }
249 250
250 #ifdef CONFIG_OF_CONTROL 251 #ifdef CONFIG_OF_CONTROL
251 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash) 252 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
252 { 253 {
253 fdt_addr_t addr; 254 fdt_addr_t addr;
254 fdt_size_t size; 255 fdt_size_t size;
255 int node; 256 int node;
256 257
257 /* If there is no node, do nothing */ 258 /* If there is no node, do nothing */
258 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH); 259 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
259 if (node < 0) 260 if (node < 0)
260 return 0; 261 return 0;
261 262
262 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size); 263 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
263 if (addr == FDT_ADDR_T_NONE) { 264 if (addr == FDT_ADDR_T_NONE) {
264 debug("%s: Cannot decode address\n", __func__); 265 debug("%s: Cannot decode address\n", __func__);
265 return 0; 266 return 0;
266 } 267 }
267 268
268 if (flash->size != size) { 269 if (flash->size != size) {
269 debug("%s: Memory map must cover entire device\n", __func__); 270 debug("%s: Memory map must cover entire device\n", __func__);
270 return -1; 271 return -1;
271 } 272 }
272 flash->memory_map = map_sysmem(addr, size); 273 flash->memory_map = map_sysmem(addr, size);
273 274
274 return 0; 275 return 0;
275 } 276 }
276 #endif /* CONFIG_OF_CONTROL */ 277 #endif /* CONFIG_OF_CONTROL */
277 278
278 static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi) 279 static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
279 { 280 {
280 struct spi_flash *flash = NULL; 281 struct spi_flash *flash = NULL;
281 u8 idcode[5]; 282 u8 idcode[5];
282 int ret; 283 int ret;
283 284
284 /* Setup spi_slave */ 285 /* Setup spi_slave */
285 if (!spi) { 286 if (!spi) {
286 printf("SF: Failed to set up slave\n"); 287 printf("SF: Failed to set up slave\n");
287 return NULL; 288 return NULL;
288 } 289 }
289 290
290 /* Claim spi bus */ 291 /* Claim spi bus */
291 ret = spi_claim_bus(spi); 292 ret = spi_claim_bus(spi);
292 if (ret) { 293 if (ret) {
293 debug("SF: Failed to claim SPI bus: %d\n", ret); 294 debug("SF: Failed to claim SPI bus: %d\n", ret);
294 goto err_claim_bus; 295 goto err_claim_bus;
295 } 296 }
296 297
297 /* Read the ID codes */ 298 /* Read the ID codes */
298 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); 299 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
299 if (ret) { 300 if (ret) {
300 printf("SF: Failed to get idcodes\n"); 301 printf("SF: Failed to get idcodes\n");
301 goto err_read_id; 302 goto err_read_id;
302 } 303 }
303 304
304 #ifdef DEBUG 305 #ifdef DEBUG
305 printf("SF: Got idcodes\n"); 306 printf("SF: Got idcodes\n");
306 print_buffer(0, idcode, 1, sizeof(idcode), 0); 307 print_buffer(0, idcode, 1, sizeof(idcode), 0);
307 #endif 308 #endif
308 309
309 /* Validate params from spi_flash_params table */ 310 /* Validate params from spi_flash_params table */
310 flash = spi_flash_validate_params(spi, idcode); 311 flash = spi_flash_validate_params(spi, idcode);
311 if (!flash) 312 if (!flash)
312 goto err_read_id; 313 goto err_read_id;
313 314
314 #ifdef CONFIG_OF_CONTROL 315 #ifdef CONFIG_OF_CONTROL
315 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) { 316 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
316 debug("SF: FDT decode error\n"); 317 debug("SF: FDT decode error\n");
317 goto err_read_id; 318 goto err_read_id;
318 } 319 }
319 #endif 320 #endif
320 #ifndef CONFIG_SPL_BUILD 321 #ifndef CONFIG_SPL_BUILD
321 printf("SF: Detected %s with page size ", flash->name); 322 printf("SF: Detected %s with page size ", flash->name);
322 print_size(flash->page_size, ", erase size "); 323 print_size(flash->page_size, ", erase size ");
323 print_size(flash->erase_size, ", total "); 324 print_size(flash->erase_size, ", total ");
324 print_size(flash->size, ""); 325 print_size(flash->size, "");
325 if (flash->memory_map) 326 if (flash->memory_map)
326 printf(", mapped at %p", flash->memory_map); 327 printf(", mapped at %p", flash->memory_map);
327 puts("\n"); 328 puts("\n");
328 #endif 329 #endif
329 #ifndef CONFIG_SPI_FLASH_BAR 330 #ifndef CONFIG_SPI_FLASH_BAR
330 if (((flash->dual_flash == SF_SINGLE_FLASH) && 331 if (((flash->dual_flash == SF_SINGLE_FLASH) &&
331 (flash->size > SPI_FLASH_16MB_BOUN)) || 332 (flash->size > SPI_FLASH_16MB_BOUN)) ||
332 ((flash->dual_flash > SF_SINGLE_FLASH) && 333 ((flash->dual_flash > SF_SINGLE_FLASH) &&
333 (flash->size > SPI_FLASH_16MB_BOUN << 1))) { 334 (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
334 puts("SF: Warning - Only lower 16MiB accessible,"); 335 puts("SF: Warning - Only lower 16MiB accessible,");
335 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n"); 336 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
336 } 337 }
337 #endif 338 #endif
338 339
339 /* Release spi bus */ 340 /* Release spi bus */
340 spi_release_bus(spi); 341 spi_release_bus(spi);
341 342
342 return flash; 343 return flash;
343 344
344 err_read_id: 345 err_read_id:
345 spi_release_bus(spi); 346 spi_release_bus(spi);
346 err_claim_bus: 347 err_claim_bus:
347 spi_free_slave(spi); 348 spi_free_slave(spi);
348 return NULL; 349 return NULL;
349 } 350 }
350 351
351 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, 352 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
352 unsigned int max_hz, unsigned int spi_mode) 353 unsigned int max_hz, unsigned int spi_mode)
353 { 354 {
354 struct spi_slave *spi; 355 struct spi_slave *spi;
355 356
356 spi = spi_setup_slave(bus, cs, max_hz, spi_mode); 357 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
357 return spi_flash_probe_slave(spi); 358 return spi_flash_probe_slave(spi);
358 } 359 }
359 360
360 #ifdef CONFIG_OF_SPI_FLASH 361 #ifdef CONFIG_OF_SPI_FLASH
361 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, 362 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
362 int spi_node) 363 int spi_node)
363 { 364 {
364 struct spi_slave *spi; 365 struct spi_slave *spi;
365 366
366 spi = spi_setup_slave_fdt(blob, slave_node, spi_node); 367 spi = spi_setup_slave_fdt(blob, slave_node, spi_node);
367 return spi_flash_probe_slave(spi); 368 return spi_flash_probe_slave(spi);
368 } 369 }
369 #endif 370 #endif
370 371
371 void spi_flash_free(struct spi_flash *flash) 372 void spi_flash_free(struct spi_flash *flash)
372 { 373 {
373 spi_free_slave(flash->spi); 374 spi_free_slave(flash->spi);
374 free(flash); 375 free(flash);
375 } 376 }
376 377
1 /* 1 /*
2 * Common SPI Interface: Controller-specific definitions 2 * Common SPI Interface: Controller-specific definitions
3 * 3 *
4 * (C) Copyright 2001 4 * (C) Copyright 2001
5 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. 5 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
6 * 6 *
7 * SPDX-License-Identifier: GPL-2.0+ 7 * SPDX-License-Identifier: GPL-2.0+
8 */ 8 */
9 9
10 #ifndef _SPI_H_ 10 #ifndef _SPI_H_
11 #define _SPI_H_ 11 #define _SPI_H_
12 12
13 /* SPI mode flags */ 13 /* SPI mode flags */
14 #define SPI_CPHA 0x01 /* clock phase */ 14 #define SPI_CPHA 0x01 /* clock phase */
15 #define SPI_CPOL 0x02 /* clock polarity */ 15 #define SPI_CPOL 0x02 /* clock polarity */
16 #define SPI_MODE_0 (0|0) /* (original MicroWire) */ 16 #define SPI_MODE_0 (0|0) /* (original MicroWire) */
17 #define SPI_MODE_1 (0|SPI_CPHA) 17 #define SPI_MODE_1 (0|SPI_CPHA)
18 #define SPI_MODE_2 (SPI_CPOL|0) 18 #define SPI_MODE_2 (SPI_CPOL|0)
19 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 19 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
20 #define SPI_CS_HIGH 0x04 /* CS active high */ 20 #define SPI_CS_HIGH 0x04 /* CS active high */
21 #define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */ 21 #define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */
22 #define SPI_3WIRE 0x10 /* SI/SO signals shared */ 22 #define SPI_3WIRE 0x10 /* SI/SO signals shared */
23 #define SPI_LOOP 0x20 /* loopback mode */ 23 #define SPI_LOOP 0x20 /* loopback mode */
24 #define SPI_SLAVE 0x40 /* slave mode */ 24 #define SPI_SLAVE 0x40 /* slave mode */
25 #define SPI_PREAMBLE 0x80 /* Skip preamble bytes */ 25 #define SPI_PREAMBLE 0x80 /* Skip preamble bytes */
26 26
27 /* SPI transfer flags */ 27 /* SPI transfer flags */
28 #define SPI_XFER_BEGIN 0x01 /* Assert CS before transfer */ 28 #define SPI_XFER_BEGIN 0x01 /* Assert CS before transfer */
29 #define SPI_XFER_END 0x02 /* Deassert CS after transfer */ 29 #define SPI_XFER_END 0x02 /* Deassert CS after transfer */
30 #define SPI_XFER_MMAP 0x08 /* Memory Mapped start */ 30 #define SPI_XFER_MMAP 0x08 /* Memory Mapped start */
31 #define SPI_XFER_MMAP_END 0x10 /* Memory Mapped End */ 31 #define SPI_XFER_MMAP_END 0x10 /* Memory Mapped End */
32 #define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END) 32 #define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END)
33 #define SPI_XFER_U_PAGE (1 << 5) 33 #define SPI_XFER_U_PAGE (1 << 5)
34 34
35 /* SPI TX operation modes */ 35 /* SPI TX operation modes */
36 #define SPI_OPM_TX_QPP 1 << 0 36 #define SPI_OPM_TX_QPP 1 << 0
37 37
38 /* SPI RX operation modes */ 38 /* SPI RX operation modes */
39 #define SPI_OPM_RX_AS 1 << 0 39 #define SPI_OPM_RX_AS 1 << 0
40 #define SPI_OPM_RX_DOUT 1 << 1 40 #define SPI_OPM_RX_DOUT 1 << 1
41 #define SPI_OPM_RX_DIO 1 << 2 41 #define SPI_OPM_RX_DIO 1 << 2
42 #define SPI_OPM_RX_QOF 1 << 3 42 #define SPI_OPM_RX_QOF 1 << 3
43 #define SPI_OPM_RX_QIOF 1 << 4 43 #define SPI_OPM_RX_QIOF 1 << 4
44 #define SPI_OPM_RX_EXTN SPI_OPM_RX_AS | SPI_OPM_RX_DOUT | \ 44 #define SPI_OPM_RX_EXTN SPI_OPM_RX_AS | SPI_OPM_RX_DOUT | \
45 SPI_OPM_RX_DIO | SPI_OPM_RX_QOF | \ 45 SPI_OPM_RX_DIO | SPI_OPM_RX_QOF | \
46 SPI_OPM_RX_QIOF 46 SPI_OPM_RX_QIOF
47 47
48 /* SPI bus connection options */ 48 /* SPI bus connection options */
49 #define SPI_CONN_DUAL_SHARED 1 << 0 49 #define SPI_CONN_DUAL_SHARED 1 << 0
50 #define SPI_CONN_DUAL_SEPARATED 1 << 1
50 51
51 /* Header byte that marks the start of the message */ 52 /* Header byte that marks the start of the message */
52 #define SPI_PREAMBLE_END_BYTE 0xec 53 #define SPI_PREAMBLE_END_BYTE 0xec
53 54
54 #define SPI_DEFAULT_WORDLEN 8 55 #define SPI_DEFAULT_WORDLEN 8
55 56
56 /** 57 /**
57 * struct spi_slave - Representation of a SPI slave 58 * struct spi_slave - Representation of a SPI slave
58 * 59 *
59 * Drivers are expected to extend this with controller-specific data. 60 * Drivers are expected to extend this with controller-specific data.
60 * 61 *
61 * @bus: ID of the bus that the slave is attached to. 62 * @bus: ID of the bus that the slave is attached to.
62 * @cs: ID of the chip select connected to the slave. 63 * @cs: ID of the chip select connected to the slave.
63 * @op_mode_rx: SPI RX operation mode. 64 * @op_mode_rx: SPI RX operation mode.
64 * @op_mode_tx: SPI TX operation mode. 65 * @op_mode_tx: SPI TX operation mode.
65 * @wordlen: Size of SPI word in number of bits 66 * @wordlen: Size of SPI word in number of bits
66 * @max_write_size: If non-zero, the maximum number of bytes which can 67 * @max_write_size: If non-zero, the maximum number of bytes which can
67 * be written at once, excluding command bytes. 68 * be written at once, excluding command bytes.
68 * @memory_map: Address of read-only SPI flash access. 69 * @memory_map: Address of read-only SPI flash access.
69 * @option: Varies SPI bus options - separate bus. 70 * @option: Varies SPI bus options - separate, shared bus.
70 * @flags: Indication of SPI flags. 71 * @flags: Indication of SPI flags.
71 */ 72 */
72 struct spi_slave { 73 struct spi_slave {
73 unsigned int bus; 74 unsigned int bus;
74 unsigned int cs; 75 unsigned int cs;
75 u8 op_mode_rx; 76 u8 op_mode_rx;
76 u8 op_mode_tx; 77 u8 op_mode_tx;
77 unsigned int wordlen; 78 unsigned int wordlen;
78 unsigned int max_write_size; 79 unsigned int max_write_size;
79 void *memory_map; 80 void *memory_map;
80 u8 option; 81 u8 option;
81 u8 flags; 82 u8 flags;
82 }; 83 };
83 84
84 /** 85 /**
85 * Initialization, must be called once on start up. 86 * Initialization, must be called once on start up.
86 * 87 *
87 * TODO: I don't think we really need this. 88 * TODO: I don't think we really need this.
88 */ 89 */
89 void spi_init(void); 90 void spi_init(void);
90 91
91 /** 92 /**
92 * spi_do_alloc_slave - Allocate a new SPI slave (internal) 93 * spi_do_alloc_slave - Allocate a new SPI slave (internal)
93 * 94 *
94 * Allocate and zero all fields in the spi slave, and set the bus/chip 95 * Allocate and zero all fields in the spi slave, and set the bus/chip
95 * select. Use the helper macro spi_alloc_slave() to call this. 96 * select. Use the helper macro spi_alloc_slave() to call this.
96 * 97 *
97 * @offset: Offset of struct spi_slave within slave structure. 98 * @offset: Offset of struct spi_slave within slave structure.
98 * @size: Size of slave structure. 99 * @size: Size of slave structure.
99 * @bus: Bus ID of the slave chip. 100 * @bus: Bus ID of the slave chip.
100 * @cs: Chip select ID of the slave chip on the specified bus. 101 * @cs: Chip select ID of the slave chip on the specified bus.
101 */ 102 */
102 void *spi_do_alloc_slave(int offset, int size, unsigned int bus, 103 void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
103 unsigned int cs); 104 unsigned int cs);
104 105
105 /** 106 /**
106 * spi_alloc_slave - Allocate a new SPI slave 107 * spi_alloc_slave - Allocate a new SPI slave
107 * 108 *
108 * Allocate and zero all fields in the spi slave, and set the bus/chip 109 * Allocate and zero all fields in the spi slave, and set the bus/chip
109 * select. 110 * select.
110 * 111 *
111 * @_struct: Name of structure to allocate (e.g. struct tegra_spi). 112 * @_struct: Name of structure to allocate (e.g. struct tegra_spi).
112 * This structure must contain a member 'struct spi_slave *slave'. 113 * This structure must contain a member 'struct spi_slave *slave'.
113 * @bus: Bus ID of the slave chip. 114 * @bus: Bus ID of the slave chip.
114 * @cs: Chip select ID of the slave chip on the specified bus. 115 * @cs: Chip select ID of the slave chip on the specified bus.
115 */ 116 */
116 #define spi_alloc_slave(_struct, bus, cs) \ 117 #define spi_alloc_slave(_struct, bus, cs) \
117 spi_do_alloc_slave(offsetof(_struct, slave), \ 118 spi_do_alloc_slave(offsetof(_struct, slave), \
118 sizeof(_struct), bus, cs) 119 sizeof(_struct), bus, cs)
119 120
120 /** 121 /**
121 * spi_alloc_slave_base - Allocate a new SPI slave with no private data 122 * spi_alloc_slave_base - Allocate a new SPI slave with no private data
122 * 123 *
123 * Allocate and zero all fields in the spi slave, and set the bus/chip 124 * Allocate and zero all fields in the spi slave, and set the bus/chip
124 * select. 125 * select.
125 * 126 *
126 * @bus: Bus ID of the slave chip. 127 * @bus: Bus ID of the slave chip.
127 * @cs: Chip select ID of the slave chip on the specified bus. 128 * @cs: Chip select ID of the slave chip on the specified bus.
128 */ 129 */
129 #define spi_alloc_slave_base(bus, cs) \ 130 #define spi_alloc_slave_base(bus, cs) \
130 spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs) 131 spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
131 132
132 /** 133 /**
133 * Set up communications parameters for a SPI slave. 134 * Set up communications parameters for a SPI slave.
134 * 135 *
135 * This must be called once for each slave. Note that this function 136 * This must be called once for each slave. Note that this function
136 * usually doesn't touch any actual hardware, it only initializes the 137 * usually doesn't touch any actual hardware, it only initializes the
137 * contents of spi_slave so that the hardware can be easily 138 * contents of spi_slave so that the hardware can be easily
138 * initialized later. 139 * initialized later.
139 * 140 *
140 * @bus: Bus ID of the slave chip. 141 * @bus: Bus ID of the slave chip.
141 * @cs: Chip select ID of the slave chip on the specified bus. 142 * @cs: Chip select ID of the slave chip on the specified bus.
142 * @max_hz: Maximum SCK rate in Hz. 143 * @max_hz: Maximum SCK rate in Hz.
143 * @mode: Clock polarity, clock phase and other parameters. 144 * @mode: Clock polarity, clock phase and other parameters.
144 * 145 *
145 * Returns: A spi_slave reference that can be used in subsequent SPI 146 * Returns: A spi_slave reference that can be used in subsequent SPI
146 * calls, or NULL if one or more of the parameters are not supported. 147 * calls, or NULL if one or more of the parameters are not supported.
147 */ 148 */
148 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, 149 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
149 unsigned int max_hz, unsigned int mode); 150 unsigned int max_hz, unsigned int mode);
150 151
151 /** 152 /**
152 * Free any memory associated with a SPI slave. 153 * Free any memory associated with a SPI slave.
153 * 154 *
154 * @slave: The SPI slave 155 * @slave: The SPI slave
155 */ 156 */
156 void spi_free_slave(struct spi_slave *slave); 157 void spi_free_slave(struct spi_slave *slave);
157 158
158 /** 159 /**
159 * Claim the bus and prepare it for communication with a given slave. 160 * Claim the bus and prepare it for communication with a given slave.
160 * 161 *
161 * This must be called before doing any transfers with a SPI slave. It 162 * This must be called before doing any transfers with a SPI slave. It
162 * will enable and initialize any SPI hardware as necessary, and make 163 * will enable and initialize any SPI hardware as necessary, and make
163 * sure that the SCK line is in the correct idle state. It is not 164 * sure that the SCK line is in the correct idle state. It is not
164 * allowed to claim the same bus for several slaves without releasing 165 * allowed to claim the same bus for several slaves without releasing
165 * the bus in between. 166 * the bus in between.
166 * 167 *
167 * @slave: The SPI slave 168 * @slave: The SPI slave
168 * 169 *
169 * Returns: 0 if the bus was claimed successfully, or a negative value 170 * Returns: 0 if the bus was claimed successfully, or a negative value
170 * if it wasn't. 171 * if it wasn't.
171 */ 172 */
172 int spi_claim_bus(struct spi_slave *slave); 173 int spi_claim_bus(struct spi_slave *slave);
173 174
174 /** 175 /**
175 * Release the SPI bus 176 * Release the SPI bus
176 * 177 *
177 * This must be called once for every call to spi_claim_bus() after 178 * This must be called once for every call to spi_claim_bus() after
178 * all transfers have finished. It may disable any SPI hardware as 179 * all transfers have finished. It may disable any SPI hardware as
179 * appropriate. 180 * appropriate.
180 * 181 *
181 * @slave: The SPI slave 182 * @slave: The SPI slave
182 */ 183 */
183 void spi_release_bus(struct spi_slave *slave); 184 void spi_release_bus(struct spi_slave *slave);
184 185
185 /** 186 /**
186 * Set the word length for SPI transactions 187 * Set the word length for SPI transactions
187 * 188 *
188 * Set the word length (number of bits per word) for SPI transactions. 189 * Set the word length (number of bits per word) for SPI transactions.
189 * 190 *
190 * @slave: The SPI slave 191 * @slave: The SPI slave
191 * @wordlen: The number of bits in a word 192 * @wordlen: The number of bits in a word
192 * 193 *
193 * Returns: 0 on success, -1 on failure. 194 * Returns: 0 on success, -1 on failure.
194 */ 195 */
195 int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen); 196 int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
196 197
197 /** 198 /**
198 * SPI transfer 199 * SPI transfer
199 * 200 *
200 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks 201 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
201 * "bitlen" bits in the SPI MISO port. That's just the way SPI works. 202 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
202 * 203 *
203 * The source of the outgoing bits is the "dout" parameter and the 204 * The source of the outgoing bits is the "dout" parameter and the
204 * destination of the input bits is the "din" parameter. Note that "dout" 205 * destination of the input bits is the "din" parameter. Note that "dout"
205 * and "din" can point to the same memory location, in which case the 206 * and "din" can point to the same memory location, in which case the
206 * input data overwrites the output data (since both are buffered by 207 * input data overwrites the output data (since both are buffered by
207 * temporary variables, this is OK). 208 * temporary variables, this is OK).
208 * 209 *
209 * spi_xfer() interface: 210 * spi_xfer() interface:
210 * @slave: The SPI slave which will be sending/receiving the data. 211 * @slave: The SPI slave which will be sending/receiving the data.
211 * @bitlen: How many bits to write and read. 212 * @bitlen: How many bits to write and read.
212 * @dout: Pointer to a string of bits to send out. The bits are 213 * @dout: Pointer to a string of bits to send out. The bits are
213 * held in a byte array and are sent MSB first. 214 * held in a byte array and are sent MSB first.
214 * @din: Pointer to a string of bits that will be filled in. 215 * @din: Pointer to a string of bits that will be filled in.
215 * @flags: A bitwise combination of SPI_XFER_* flags. 216 * @flags: A bitwise combination of SPI_XFER_* flags.
216 * 217 *
217 * Returns: 0 on success, not 0 on failure 218 * Returns: 0 on success, not 0 on failure
218 */ 219 */
219 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, 220 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
220 void *din, unsigned long flags); 221 void *din, unsigned long flags);
221 222
222 /** 223 /**
223 * Determine if a SPI chipselect is valid. 224 * Determine if a SPI chipselect is valid.
224 * This function is provided by the board if the low-level SPI driver 225 * This function is provided by the board if the low-level SPI driver
225 * needs it to determine if a given chipselect is actually valid. 226 * needs it to determine if a given chipselect is actually valid.
226 * 227 *
227 * Returns: 1 if bus:cs identifies a valid chip on this board, 0 228 * Returns: 1 if bus:cs identifies a valid chip on this board, 0
228 * otherwise. 229 * otherwise.
229 */ 230 */
230 int spi_cs_is_valid(unsigned int bus, unsigned int cs); 231 int spi_cs_is_valid(unsigned int bus, unsigned int cs);
231 232
232 /** 233 /**
233 * Activate a SPI chipselect. 234 * Activate a SPI chipselect.
234 * This function is provided by the board code when using a driver 235 * This function is provided by the board code when using a driver
235 * that can't control its chipselects automatically (e.g. 236 * that can't control its chipselects automatically (e.g.
236 * common/soft_spi.c). When called, it should activate the chip select 237 * common/soft_spi.c). When called, it should activate the chip select
237 * to the device identified by "slave". 238 * to the device identified by "slave".
238 */ 239 */
239 void spi_cs_activate(struct spi_slave *slave); 240 void spi_cs_activate(struct spi_slave *slave);
240 241
241 /** 242 /**
242 * Deactivate a SPI chipselect. 243 * Deactivate a SPI chipselect.
243 * This function is provided by the board code when using a driver 244 * This function is provided by the board code when using a driver
244 * that can't control its chipselects automatically (e.g. 245 * that can't control its chipselects automatically (e.g.
245 * common/soft_spi.c). When called, it should deactivate the chip 246 * common/soft_spi.c). When called, it should deactivate the chip
246 * select to the device identified by "slave". 247 * select to the device identified by "slave".
247 */ 248 */
248 void spi_cs_deactivate(struct spi_slave *slave); 249 void spi_cs_deactivate(struct spi_slave *slave);
249 250
250 /** 251 /**
251 * Set transfer speed. 252 * Set transfer speed.
252 * This sets a new speed to be applied for next spi_xfer(). 253 * This sets a new speed to be applied for next spi_xfer().
253 * @slave: The SPI slave 254 * @slave: The SPI slave
254 * @hz: The transfer speed 255 * @hz: The transfer speed
255 */ 256 */
256 void spi_set_speed(struct spi_slave *slave, uint hz); 257 void spi_set_speed(struct spi_slave *slave, uint hz);
257 258
258 /** 259 /**
259 * Write 8 bits, then read 8 bits. 260 * Write 8 bits, then read 8 bits.
260 * @slave: The SPI slave we're communicating with 261 * @slave: The SPI slave we're communicating with
261 * @byte: Byte to be written 262 * @byte: Byte to be written
262 * 263 *
263 * Returns: The value that was read, or a negative value on error. 264 * Returns: The value that was read, or a negative value on error.
264 * 265 *
265 * TODO: This function probably shouldn't be inlined. 266 * TODO: This function probably shouldn't be inlined.
266 */ 267 */
267 static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte) 268 static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
268 { 269 {
269 unsigned char dout[2]; 270 unsigned char dout[2];
270 unsigned char din[2]; 271 unsigned char din[2];
271 int ret; 272 int ret;
272 273
273 dout[0] = byte; 274 dout[0] = byte;
274 dout[1] = 0; 275 dout[1] = 0;
275 276
276 ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END); 277 ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
277 return ret < 0 ? ret : din[1]; 278 return ret < 0 ? ret : din[1];
278 } 279 }
279 280
280 /** 281 /**
281 * Set up a SPI slave for a particular device tree node 282 * Set up a SPI slave for a particular device tree node
282 * 283 *
283 * This calls spi_setup_slave() with the correct bus number. Call 284 * This calls spi_setup_slave() with the correct bus number. Call
284 * spi_free_slave() to free it later. 285 * spi_free_slave() to free it later.
285 * 286 *
286 * @param blob: Device tree blob 287 * @param blob: Device tree blob
287 * @param slave_node: Slave node to use 288 * @param slave_node: Slave node to use
288 * @param spi_node: SPI peripheral node to use 289 * @param spi_node: SPI peripheral node to use
289 * @return pointer to new spi_slave structure 290 * @return pointer to new spi_slave structure
290 */ 291 */
291 struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node, 292 struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
292 int spi_node); 293 int spi_node);
293 294
294 /** 295 /**
295 * spi_base_setup_slave_fdt() - helper function to set up a SPI slace 296 * spi_base_setup_slave_fdt() - helper function to set up a SPI slace
296 * 297 *
297 * This decodes SPI properties from the slave node to determine the 298 * This decodes SPI properties from the slave node to determine the
298 * chip select and SPI parameters. 299 * chip select and SPI parameters.
299 * 300 *
300 * @blob: Device tree blob 301 * @blob: Device tree blob
301 * @busnum: Bus number to use 302 * @busnum: Bus number to use
302 * @node: Device tree node for the SPI bus 303 * @node: Device tree node for the SPI bus
303 */ 304 */
304 struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum, 305 struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
305 int node); 306 int node);
306 307
307 #endif /* _SPI_H_ */ 308 #endif /* _SPI_H_ */
308 309
1 /* 1 /*
2 * Common SPI flash Interface 2 * Common SPI flash Interface
3 * 3 *
4 * Copyright (C) 2008 Atmel Corporation 4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. 5 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
6 * 6 *
7 * See file CREDITS for list of people who contributed to this 7 * See file CREDITS for list of people who contributed to this
8 * project. 8 * project.
9 * 9 *
10 * This program is free software; you can redistribute it and/or 10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License 11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation. 12 * version 2 as published by the Free Software Foundation.
13 */ 13 */
14 14
15 #ifndef _SPI_FLASH_H_ 15 #ifndef _SPI_FLASH_H_
16 #define _SPI_FLASH_H_ 16 #define _SPI_FLASH_H_
17 17
18 #include <spi.h> 18 #include <spi.h>
19 #include <linux/types.h> 19 #include <linux/types.h>
20 #include <linux/compiler.h> 20 #include <linux/compiler.h>
21 21
22 /* sf param flags */ 22 /* sf param flags */
23 #define SECT_4K 1 << 1 23 #define SECT_4K 1 << 1
24 #define SECT_32K 1 << 2 24 #define SECT_32K 1 << 2
25 #define E_FSR 1 << 3 25 #define E_FSR 1 << 3
26 #define WR_QPP 1 << 4 26 #define WR_QPP 1 << 4
27 27
28 /* Enum list - Full read commands */ 28 /* Enum list - Full read commands */
29 enum spi_read_cmds { 29 enum spi_read_cmds {
30 ARRAY_SLOW = 1 << 0, 30 ARRAY_SLOW = 1 << 0,
31 DUAL_OUTPUT_FAST = 1 << 1, 31 DUAL_OUTPUT_FAST = 1 << 1,
32 DUAL_IO_FAST = 1 << 2, 32 DUAL_IO_FAST = 1 << 2,
33 QUAD_OUTPUT_FAST = 1 << 3, 33 QUAD_OUTPUT_FAST = 1 << 3,
34 QUAD_IO_FAST = 1 << 4, 34 QUAD_IO_FAST = 1 << 4,
35 }; 35 };
36 #define RD_EXTN ARRAY_SLOW | DUAL_OUTPUT_FAST | DUAL_IO_FAST 36 #define RD_EXTN ARRAY_SLOW | DUAL_OUTPUT_FAST | DUAL_IO_FAST
37 #define RD_FULL RD_EXTN | QUAD_OUTPUT_FAST | QUAD_IO_FAST 37 #define RD_FULL RD_EXTN | QUAD_OUTPUT_FAST | QUAD_IO_FAST
38 38
39 /* Dual SPI flash memories */ 39 /* Dual SPI flash memories */
40 enum spi_dual_flash { 40 enum spi_dual_flash {
41 SF_SINGLE_FLASH = 0, 41 SF_SINGLE_FLASH = 0,
42 SF_DUAL_STACKED_FLASH = 1 << 0, 42 SF_DUAL_STACKED_FLASH = 1 << 0,
43 SF_DUAL_PARALLEL_FLASH = 1 << 1,
43 }; 44 };
44 45
45 /** 46 /**
46 * struct spi_flash_params - SPI/QSPI flash device params structure 47 * struct spi_flash_params - SPI/QSPI flash device params structure
47 * 48 *
48 * @name: Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO]) 49 * @name: Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO])
49 * @jedec: Device jedec ID (0x[1byte_manuf_id][2byte_dev_id]) 50 * @jedec: Device jedec ID (0x[1byte_manuf_id][2byte_dev_id])
50 * @ext_jedec: Device ext_jedec ID 51 * @ext_jedec: Device ext_jedec ID
51 * @sector_size: Sector size of this device 52 * @sector_size: Sector size of this device
52 * @nr_sectors: No.of sectors on this device 53 * @nr_sectors: No.of sectors on this device
53 * @e_rd_cmd: Enum list for read commands 54 * @e_rd_cmd: Enum list for read commands
54 * @flags: Importent param, for flash specific behaviour 55 * @flags: Importent param, for flash specific behaviour
55 */ 56 */
56 struct spi_flash_params { 57 struct spi_flash_params {
57 const char *name; 58 const char *name;
58 u32 jedec; 59 u32 jedec;
59 u16 ext_jedec; 60 u16 ext_jedec;
60 u32 sector_size; 61 u32 sector_size;
61 u32 nr_sectors; 62 u32 nr_sectors;
62 u8 e_rd_cmd; 63 u8 e_rd_cmd;
63 u16 flags; 64 u16 flags;
64 }; 65 };
65 66
66 extern const struct spi_flash_params spi_flash_params_table[]; 67 extern const struct spi_flash_params spi_flash_params_table[];
67 68
68 /** 69 /**
69 * struct spi_flash - SPI flash structure 70 * struct spi_flash - SPI flash structure
70 * 71 *
71 * @spi: SPI slave 72 * @spi: SPI slave
72 * @name: Name of SPI flash 73 * @name: Name of SPI flash
73 * @dual_flash: Indicates dual flash memories - dual stacked 74 * @dual_flash: Indicates dual flash memories - dual stacked, parallel
75 * @shift: Flash shift useful in dual parallel
74 * @size: Total flash size 76 * @size: Total flash size
75 * @page_size: Write (page) size 77 * @page_size: Write (page) size
76 * @sector_size: Sector size 78 * @sector_size: Sector size
77 * @erase_size: Erase size 79 * @erase_size: Erase size
78 * @bank_read_cmd: Bank read cmd 80 * @bank_read_cmd: Bank read cmd
79 * @bank_write_cmd: Bank write cmd 81 * @bank_write_cmd: Bank write cmd
80 * @bank_curr: Current flash bank 82 * @bank_curr: Current flash bank
81 * @poll_cmd: Poll cmd - for flash erase/program 83 * @poll_cmd: Poll cmd - for flash erase/program
82 * @erase_cmd: Erase cmd 4K, 32K, 64K 84 * @erase_cmd: Erase cmd 4K, 32K, 64K
83 * @read_cmd: Read cmd - Array Fast, Extn read and quad read. 85 * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
84 * @write_cmd: Write cmd - page and quad program. 86 * @write_cmd: Write cmd - page and quad program.
85 * @dummy_byte: Dummy cycles for read operation. 87 * @dummy_byte: Dummy cycles for read operation.
86 * @memory_map: Address of read-only SPI flash access 88 * @memory_map: Address of read-only SPI flash access
87 * @read: Flash read ops: Read len bytes at offset into buf 89 * @read: Flash read ops: Read len bytes at offset into buf
88 * Supported cmds: Fast Array Read 90 * Supported cmds: Fast Array Read
89 * @write: Flash write ops: Write len bytes from buf into offeset 91 * @write: Flash write ops: Write len bytes from buf into offeset
90 * Supported cmds: Page Program 92 * Supported cmds: Page Program
91 * @erase: Flash erase ops: Erase len bytes from offset 93 * @erase: Flash erase ops: Erase len bytes from offset
92 * Supported cmds: Sector erase 4K, 32K, 64K 94 * Supported cmds: Sector erase 4K, 32K, 64K
93 * return 0 - Sucess, 1 - Failure 95 * return 0 - Sucess, 1 - Failure
94 */ 96 */
95 struct spi_flash { 97 struct spi_flash {
96 struct spi_slave *spi; 98 struct spi_slave *spi;
97 const char *name; 99 const char *name;
98 u8 dual_flash; 100 u8 dual_flash;
101 u8 shift;
99 102
100 u32 size; 103 u32 size;
101 u32 page_size; 104 u32 page_size;
102 u32 sector_size; 105 u32 sector_size;
103 u32 erase_size; 106 u32 erase_size;
104 #ifdef CONFIG_SPI_FLASH_BAR 107 #ifdef CONFIG_SPI_FLASH_BAR
105 u8 bank_read_cmd; 108 u8 bank_read_cmd;
106 u8 bank_write_cmd; 109 u8 bank_write_cmd;
107 u8 bank_curr; 110 u8 bank_curr;
108 #endif 111 #endif
109 u8 poll_cmd; 112 u8 poll_cmd;
110 u8 erase_cmd; 113 u8 erase_cmd;
111 u8 read_cmd; 114 u8 read_cmd;
112 u8 write_cmd; 115 u8 write_cmd;
113 u8 dummy_byte; 116 u8 dummy_byte;
114 117
115 void *memory_map; 118 void *memory_map;
116 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf); 119 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
117 int (*write)(struct spi_flash *flash, u32 offset, size_t len, 120 int (*write)(struct spi_flash *flash, u32 offset, size_t len,
118 const void *buf); 121 const void *buf);
119 int (*erase)(struct spi_flash *flash, u32 offset, size_t len); 122 int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
120 }; 123 };
121 124
122 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, 125 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
123 unsigned int max_hz, unsigned int spi_mode); 126 unsigned int max_hz, unsigned int spi_mode);
124 127
125 /** 128 /**
126 * Set up a new SPI flash from an fdt node 129 * Set up a new SPI flash from an fdt node
127 * 130 *
128 * @param blob Device tree blob 131 * @param blob Device tree blob
129 * @param slave_node Pointer to this SPI slave node in the device tree 132 * @param slave_node Pointer to this SPI slave node in the device tree
130 * @param spi_node Cached pointer to the SPI interface this node belongs 133 * @param spi_node Cached pointer to the SPI interface this node belongs
131 * to 134 * to
132 * @return 0 if ok, -1 on error 135 * @return 0 if ok, -1 on error
133 */ 136 */
134 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, 137 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
135 int spi_node); 138 int spi_node);
136 139
137 void spi_flash_free(struct spi_flash *flash); 140 void spi_flash_free(struct spi_flash *flash);
138 141
139 static inline int spi_flash_read(struct spi_flash *flash, u32 offset, 142 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
140 size_t len, void *buf) 143 size_t len, void *buf)
141 { 144 {
142 return flash->read(flash, offset, len, buf); 145 return flash->read(flash, offset, len, buf);
143 } 146 }
144 147
145 static inline int spi_flash_write(struct spi_flash *flash, u32 offset, 148 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
146 size_t len, const void *buf) 149 size_t len, const void *buf)
147 { 150 {
148 return flash->write(flash, offset, len, buf); 151 return flash->write(flash, offset, len, buf);
149 } 152 }
150 153
151 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, 154 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
152 size_t len) 155 size_t len)
153 { 156 {
154 return flash->erase(flash, offset, len); 157 return flash->erase(flash, offset, len);
155 } 158 }
156 159
157 void spi_boot(void) __noreturn; 160 void spi_boot(void) __noreturn;
158 161
159 #endif /* _SPI_FLASH_H_ */ 162 #endif /* _SPI_FLASH_H_ */
160 163