xref: /rk3399_rockchip-uboot/drivers/mtd/spi/spi_flash.c (revision d2eae43ba803cff75b44a07d08d718ecdecdee94)
1 /*
2  * SPI flash interface
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6  *
7  * Licensed under the GPL-2 or later.
8  */
9 
10 #include <common.h>
11 #include <fdtdec.h>
12 #include <malloc.h>
13 #include <spi.h>
14 #include <spi_flash.h>
15 #include <watchdog.h>
16 
17 #include "spi_flash_internal.h"
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 static void spi_flash_addr(u32 addr, u8 *cmd)
22 {
23 	/* cmd[0] is actual command */
24 	cmd[1] = addr >> 16;
25 	cmd[2] = addr >> 8;
26 	cmd[3] = addr >> 0;
27 }
28 
29 static int spi_flash_read_write(struct spi_slave *spi,
30 				const u8 *cmd, size_t cmd_len,
31 				const u8 *data_out, u8 *data_in,
32 				size_t data_len)
33 {
34 	unsigned long flags = SPI_XFER_BEGIN;
35 	int ret;
36 
37 	if (data_len == 0)
38 		flags |= SPI_XFER_END;
39 
40 	ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
41 	if (ret) {
42 		debug("SF: Failed to send command (%zu bytes): %d\n",
43 				cmd_len, ret);
44 	} else if (data_len != 0) {
45 		ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
46 		if (ret)
47 			debug("SF: Failed to transfer %zu bytes of data: %d\n",
48 					data_len, ret);
49 	}
50 
51 	return ret;
52 }
53 
54 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
55 {
56 	return spi_flash_cmd_read(spi, &cmd, 1, response, len);
57 }
58 
59 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
60 		size_t cmd_len, void *data, size_t data_len)
61 {
62 	return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
63 }
64 
65 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
66 		const void *data, size_t data_len)
67 {
68 	return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
69 }
70 
71 int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
72 		size_t len, const void *buf)
73 {
74 	unsigned long page_addr, byte_addr, page_size;
75 	size_t chunk_len, actual;
76 	int ret;
77 	u8 cmd[4];
78 
79 	page_size = flash->page_size;
80 	page_addr = offset / page_size;
81 	byte_addr = offset % page_size;
82 
83 	ret = spi_claim_bus(flash->spi);
84 	if (ret) {
85 		debug("SF: unable to claim SPI bus\n");
86 		return ret;
87 	}
88 
89 	cmd[0] = CMD_PAGE_PROGRAM;
90 	for (actual = 0; actual < len; actual += chunk_len) {
91 		chunk_len = min(len - actual, page_size - byte_addr);
92 
93 		if (flash->spi->max_write_size)
94 			chunk_len = min(chunk_len, flash->spi->max_write_size);
95 
96 		cmd[1] = page_addr >> 8;
97 		cmd[2] = page_addr;
98 		cmd[3] = byte_addr;
99 
100 		debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
101 		      buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
102 
103 		ret = spi_flash_cmd_write_enable(flash);
104 		if (ret < 0) {
105 			debug("SF: enabling write failed\n");
106 			break;
107 		}
108 
109 		ret = spi_flash_cmd_write(flash->spi, cmd, 4,
110 					  buf + actual, chunk_len);
111 		if (ret < 0) {
112 			debug("SF: write failed\n");
113 			break;
114 		}
115 
116 		ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
117 		if (ret)
118 			break;
119 
120 		byte_addr += chunk_len;
121 		if (byte_addr == page_size) {
122 			page_addr++;
123 			byte_addr = 0;
124 		}
125 	}
126 
127 	debug("SF: program %s %zu bytes @ %#x\n",
128 	      ret ? "failure" : "success", len, offset);
129 
130 	spi_release_bus(flash->spi);
131 	return ret;
132 }
133 
134 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
135 		size_t cmd_len, void *data, size_t data_len)
136 {
137 	struct spi_slave *spi = flash->spi;
138 	int ret;
139 
140 	spi_claim_bus(spi);
141 	ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
142 	spi_release_bus(spi);
143 
144 	return ret;
145 }
146 
147 int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
148 		size_t len, void *data)
149 {
150 	u8 cmd[5];
151 
152 	/* Handle memory-mapped SPI */
153 	if (flash->memory_map)
154 		memcpy(data, flash->memory_map + offset, len);
155 
156 	cmd[0] = CMD_READ_ARRAY_FAST;
157 	spi_flash_addr(offset, cmd);
158 	cmd[4] = 0x00;
159 
160 	return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
161 }
162 
163 int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
164 			   u8 cmd, u8 poll_bit)
165 {
166 	struct spi_slave *spi = flash->spi;
167 	unsigned long timebase;
168 	int ret;
169 	u8 status;
170 
171 	ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
172 	if (ret) {
173 		debug("SF: Failed to send command %02x: %d\n", cmd, ret);
174 		return ret;
175 	}
176 
177 	timebase = get_timer(0);
178 	do {
179 		WATCHDOG_RESET();
180 
181 		ret = spi_xfer(spi, 8, NULL, &status, 0);
182 		if (ret)
183 			return -1;
184 
185 		if ((status & poll_bit) == 0)
186 			break;
187 
188 	} while (get_timer(timebase) < timeout);
189 
190 	spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
191 
192 	if ((status & poll_bit) == 0)
193 		return 0;
194 
195 	/* Timed out */
196 	debug("SF: time out!\n");
197 	return -1;
198 }
199 
200 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
201 {
202 	return spi_flash_cmd_poll_bit(flash, timeout,
203 		CMD_READ_STATUS, STATUS_WIP);
204 }
205 
206 int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
207 {
208 	u32 start, end, erase_size;
209 	int ret;
210 	u8 cmd[4];
211 
212 	erase_size = flash->sector_size;
213 	if (offset % erase_size || len % erase_size) {
214 		debug("SF: Erase offset/length not multiple of erase size\n");
215 		return -1;
216 	}
217 
218 	ret = spi_claim_bus(flash->spi);
219 	if (ret) {
220 		debug("SF: Unable to claim SPI bus\n");
221 		return ret;
222 	}
223 
224 	if (erase_size == 4096)
225 		cmd[0] = CMD_ERASE_4K;
226 	else
227 		cmd[0] = CMD_ERASE_64K;
228 	start = offset;
229 	end = start + len;
230 
231 	while (offset < end) {
232 		spi_flash_addr(offset, cmd);
233 		offset += erase_size;
234 
235 		debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
236 		      cmd[2], cmd[3], offset);
237 
238 		ret = spi_flash_cmd_write_enable(flash);
239 		if (ret)
240 			goto out;
241 
242 		ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
243 		if (ret)
244 			goto out;
245 
246 		ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
247 		if (ret)
248 			goto out;
249 	}
250 
251 	debug("SF: Successfully erased %zu bytes @ %#x\n", len, start);
252 
253  out:
254 	spi_release_bus(flash->spi);
255 	return ret;
256 }
257 
258 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
259 {
260 	u8 cmd;
261 	int ret;
262 
263 	ret = spi_flash_cmd_write_enable(flash);
264 	if (ret < 0) {
265 		debug("SF: enabling write failed\n");
266 		return ret;
267 	}
268 
269 	cmd = CMD_WRITE_STATUS;
270 	ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
271 	if (ret) {
272 		debug("SF: fail to write status register\n");
273 		return ret;
274 	}
275 
276 	ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
277 	if (ret < 0) {
278 		debug("SF: write status register timed out\n");
279 		return ret;
280 	}
281 
282 	return 0;
283 }
284 
285 #ifdef CONFIG_OF_CONTROL
286 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
287 {
288 	fdt_addr_t addr;
289 	fdt_size_t size;
290 	int node;
291 
292 	/* If there is no node, do nothing */
293 	node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
294 	if (node < 0)
295 		return 0;
296 
297 	addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
298 	if (addr == FDT_ADDR_T_NONE) {
299 		debug("%s: Cannot decode address\n", __func__);
300 		return 0;
301 	}
302 
303 	if (flash->size != size) {
304 		debug("%s: Memory map must cover entire device\n", __func__);
305 		return -1;
306 	}
307 	flash->memory_map = (void *)addr;
308 
309 	return 0;
310 }
311 #endif /* CONFIG_OF_CONTROL */
312 
313 /*
314  * The following table holds all device probe functions
315  *
316  * shift:  number of continuation bytes before the ID
317  * idcode: the expected IDCODE or 0xff for non JEDEC devices
318  * probe:  the function to call
319  *
320  * Non JEDEC devices should be ordered in the table such that
321  * the probe functions with best detection algorithms come first.
322  *
323  * Several matching entries are permitted, they will be tried
324  * in sequence until a probe function returns non NULL.
325  *
326  * IDCODE_CONT_LEN may be redefined if a device needs to declare a
327  * larger "shift" value.  IDCODE_PART_LEN generally shouldn't be
328  * changed.  This is the max number of bytes probe functions may
329  * examine when looking up part-specific identification info.
330  *
331  * Probe functions will be given the idcode buffer starting at their
332  * manu id byte (the "idcode" in the table below).  In other words,
333  * all of the continuation bytes will be skipped (the "shift" below).
334  */
335 #define IDCODE_CONT_LEN 0
336 #define IDCODE_PART_LEN 5
337 static const struct {
338 	const u8 shift;
339 	const u8 idcode;
340 	struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
341 } flashes[] = {
342 	/* Keep it sorted by define name */
343 #ifdef CONFIG_SPI_FLASH_ATMEL
344 	{ 0, 0x1f, spi_flash_probe_atmel, },
345 #endif
346 #ifdef CONFIG_SPI_FLASH_EON
347 	{ 0, 0x1c, spi_flash_probe_eon, },
348 #endif
349 #ifdef CONFIG_SPI_FLASH_MACRONIX
350 	{ 0, 0xc2, spi_flash_probe_macronix, },
351 #endif
352 #ifdef CONFIG_SPI_FLASH_SPANSION
353 	{ 0, 0x01, spi_flash_probe_spansion, },
354 #endif
355 #ifdef CONFIG_SPI_FLASH_SST
356 	{ 0, 0xbf, spi_flash_probe_sst, },
357 #endif
358 #ifdef CONFIG_SPI_FLASH_STMICRO
359 	{ 0, 0x20, spi_flash_probe_stmicro, },
360 #endif
361 #ifdef CONFIG_SPI_FLASH_WINBOND
362 	{ 0, 0xef, spi_flash_probe_winbond, },
363 #endif
364 #ifdef CONFIG_SPI_FRAM_RAMTRON
365 	{ 6, 0xc2, spi_fram_probe_ramtron, },
366 # undef IDCODE_CONT_LEN
367 # define IDCODE_CONT_LEN 6
368 #endif
369 	/* Keep it sorted by best detection */
370 #ifdef CONFIG_SPI_FLASH_STMICRO
371 	{ 0, 0xff, spi_flash_probe_stmicro, },
372 #endif
373 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
374 	{ 0, 0xff, spi_fram_probe_ramtron, },
375 #endif
376 };
377 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
378 
379 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
380 		unsigned int max_hz, unsigned int spi_mode)
381 {
382 	struct spi_slave *spi;
383 	struct spi_flash *flash = NULL;
384 	int ret, i, shift;
385 	u8 idcode[IDCODE_LEN], *idp;
386 
387 	spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
388 	if (!spi) {
389 		printf("SF: Failed to set up slave\n");
390 		return NULL;
391 	}
392 
393 	ret = spi_claim_bus(spi);
394 	if (ret) {
395 		debug("SF: Failed to claim SPI bus: %d\n", ret);
396 		goto err_claim_bus;
397 	}
398 
399 	/* Read the ID codes */
400 	ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
401 	if (ret)
402 		goto err_read_id;
403 
404 #ifdef DEBUG
405 	printf("SF: Got idcodes\n");
406 	print_buffer(0, idcode, 1, sizeof(idcode), 0);
407 #endif
408 
409 	/* count the number of continuation bytes */
410 	for (shift = 0, idp = idcode;
411 	     shift < IDCODE_CONT_LEN && *idp == 0x7f;
412 	     ++shift, ++idp)
413 		continue;
414 
415 	/* search the table for matches in shift and id */
416 	for (i = 0; i < ARRAY_SIZE(flashes); ++i)
417 		if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
418 			/* we have a match, call probe */
419 			flash = flashes[i].probe(spi, idp);
420 			if (flash)
421 				break;
422 		}
423 
424 	if (!flash) {
425 		printf("SF: Unsupported manufacturer %02x\n", *idp);
426 		goto err_manufacturer_probe;
427 	}
428 
429 #ifdef CONFIG_OF_CONTROL
430 	if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
431 		debug("SF: FDT decode error\n");
432 		goto err_manufacturer_probe;
433 	}
434 #endif
435 	printf("SF: Detected %s with page size ", flash->name);
436 	print_size(flash->sector_size, ", total ");
437 	print_size(flash->size, "");
438 	if (flash->memory_map)
439 		printf(", mapped at %p", flash->memory_map);
440 	puts("\n");
441 
442 	spi_release_bus(spi);
443 
444 	return flash;
445 
446 err_manufacturer_probe:
447 err_read_id:
448 	spi_release_bus(spi);
449 err_claim_bus:
450 	spi_free_slave(spi);
451 	return NULL;
452 }
453 
454 void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
455 			 const char *name)
456 {
457 	struct spi_flash *flash;
458 	void *ptr;
459 
460 	ptr = malloc(size);
461 	if (!ptr) {
462 		debug("SF: Failed to allocate memory\n");
463 		return NULL;
464 	}
465 	memset(ptr, '\0', size);
466 	flash = (struct spi_flash *)(ptr + offset);
467 
468 	/* Set up some basic fields - caller will sort out sizes */
469 	flash->spi = spi;
470 	flash->name = name;
471 
472 	flash->read = spi_flash_cmd_read_fast;
473 	flash->write = spi_flash_cmd_write_multi;
474 	flash->erase = spi_flash_cmd_erase;
475 
476 	return flash;
477 }
478 
479 void spi_flash_free(struct spi_flash *flash)
480 {
481 	spi_free_slave(flash->spi);
482 	free(flash);
483 }
484