xref: /rk3399_rockchip-uboot/drivers/mtd/spi/spi_flash.c (revision 8ae86b76c648d8bef97241c78e2fddb1c4f164d3)
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 <malloc.h>
12 #include <spi.h>
13 #include <spi_flash.h>
14 
15 #include "spi_flash_internal.h"
16 
17 static void spi_flash_addr(u32 addr, u8 *cmd)
18 {
19 	/* cmd[0] is actual command */
20 	cmd[1] = addr >> 16;
21 	cmd[2] = addr >> 8;
22 	cmd[3] = addr >> 0;
23 }
24 
25 static int spi_flash_read_write(struct spi_slave *spi,
26 				const u8 *cmd, size_t cmd_len,
27 				const u8 *data_out, u8 *data_in,
28 				size_t data_len)
29 {
30 	unsigned long flags = SPI_XFER_BEGIN;
31 	int ret;
32 
33 	if (data_len == 0)
34 		flags |= SPI_XFER_END;
35 
36 	ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
37 	if (ret) {
38 		debug("SF: Failed to send command (%zu bytes): %d\n",
39 				cmd_len, ret);
40 	} else if (data_len != 0) {
41 		ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
42 		if (ret)
43 			debug("SF: Failed to transfer %zu bytes of data: %d\n",
44 					data_len, ret);
45 	}
46 
47 	return ret;
48 }
49 
50 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
51 {
52 	return spi_flash_cmd_read(spi, &cmd, 1, response, len);
53 }
54 
55 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
56 		size_t cmd_len, void *data, size_t data_len)
57 {
58 	return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
59 }
60 
61 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
62 		const void *data, size_t data_len)
63 {
64 	return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
65 }
66 
67 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
68 		size_t cmd_len, void *data, size_t data_len)
69 {
70 	struct spi_slave *spi = flash->spi;
71 	int ret;
72 
73 	spi_claim_bus(spi);
74 	ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
75 	spi_release_bus(spi);
76 
77 	return ret;
78 }
79 
80 int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
81 		size_t len, void *data)
82 {
83 	u8 cmd[5];
84 
85 	cmd[0] = CMD_READ_ARRAY_FAST;
86 	spi_flash_addr(offset, cmd);
87 	cmd[4] = 0x00;
88 
89 	return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
90 }
91 
92 int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
93 			   u8 cmd, u8 poll_bit)
94 {
95 	struct spi_slave *spi = flash->spi;
96 	unsigned long timebase;
97 	int ret;
98 	u8 status;
99 
100 	ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
101 	if (ret) {
102 		debug("SF: Failed to send command %02x: %d\n", cmd, ret);
103 		return ret;
104 	}
105 
106 	timebase = get_timer(0);
107 	do {
108 		ret = spi_xfer(spi, 8, NULL, &status, 0);
109 		if (ret)
110 			return -1;
111 
112 		if ((status & poll_bit) == 0)
113 			break;
114 
115 	} while (get_timer(timebase) < timeout);
116 
117 	spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
118 
119 	if ((status & poll_bit) == 0)
120 		return 0;
121 
122 	/* Timed out */
123 	debug("SF: time out!\n");
124 	return -1;
125 }
126 
127 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
128 {
129 	return spi_flash_cmd_poll_bit(flash, timeout,
130 		CMD_READ_STATUS, STATUS_WIP);
131 }
132 
133 int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
134 			u32 erase_size, u32 offset, size_t len)
135 {
136 	u32 start, end;
137 	int ret;
138 	u8 cmd[4];
139 
140 	if (offset % erase_size || len % erase_size) {
141 		debug("SF: Erase offset/length not multiple of erase size\n");
142 		return -1;
143 	}
144 
145 	ret = spi_claim_bus(flash->spi);
146 	if (ret) {
147 		debug("SF: Unable to claim SPI bus\n");
148 		return ret;
149 	}
150 
151 	cmd[0] = erase_cmd;
152 	start = offset;
153 	end = start + len;
154 
155 	while (offset < end) {
156 		spi_flash_addr(offset, cmd);
157 		offset += erase_size;
158 
159 		debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
160 		      cmd[2], cmd[3], offset);
161 
162 		ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
163 		if (ret)
164 			goto out;
165 
166 		ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
167 		if (ret)
168 			goto out;
169 
170 		ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
171 		if (ret)
172 			goto out;
173 	}
174 
175 	debug("SF: Successfully erased %lu bytes @ %#x\n",
176 	      len * erase_size, start);
177 
178  out:
179 	spi_release_bus(flash->spi);
180 	return ret;
181 }
182 
183 /*
184  * The following table holds all device probe functions
185  *
186  * shift:  number of continuation bytes before the ID
187  * idcode: the expected IDCODE or 0xff for non JEDEC devices
188  * probe:  the function to call
189  *
190  * Non JEDEC devices should be ordered in the table such that
191  * the probe functions with best detection algorithms come first.
192  *
193  * Several matching entries are permitted, they will be tried
194  * in sequence until a probe function returns non NULL.
195  *
196  * IDCODE_CONT_LEN may be redefined if a device needs to declare a
197  * larger "shift" value.  IDCODE_PART_LEN generally shouldn't be
198  * changed.  This is the max number of bytes probe functions may
199  * examine when looking up part-specific identification info.
200  *
201  * Probe functions will be given the idcode buffer starting at their
202  * manu id byte (the "idcode" in the table below).  In other words,
203  * all of the continuation bytes will be skipped (the "shift" below).
204  */
205 #define IDCODE_CONT_LEN 0
206 #define IDCODE_PART_LEN 5
207 static const struct {
208 	const u8 shift;
209 	const u8 idcode;
210 	struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
211 } flashes[] = {
212 	/* Keep it sorted by define name */
213 #ifdef CONFIG_SPI_FLASH_ATMEL
214 	{ 0, 0x1f, spi_flash_probe_atmel, },
215 #endif
216 #ifdef CONFIG_SPI_FLASH_EON
217 	{ 0, 0x1c, spi_flash_probe_eon, },
218 #endif
219 #ifdef CONFIG_SPI_FLASH_MACRONIX
220 	{ 0, 0xc2, spi_flash_probe_macronix, },
221 #endif
222 #ifdef CONFIG_SPI_FLASH_SPANSION
223 	{ 0, 0x01, spi_flash_probe_spansion, },
224 #endif
225 #ifdef CONFIG_SPI_FLASH_SST
226 	{ 0, 0xbf, spi_flash_probe_sst, },
227 #endif
228 #ifdef CONFIG_SPI_FLASH_STMICRO
229 	{ 0, 0x20, spi_flash_probe_stmicro, },
230 #endif
231 #ifdef CONFIG_SPI_FLASH_WINBOND
232 	{ 0, 0xef, spi_flash_probe_winbond, },
233 #endif
234 #ifdef CONFIG_SPI_FRAM_RAMTRON
235 	{ 6, 0xc2, spi_fram_probe_ramtron, },
236 # undef IDCODE_CONT_LEN
237 # define IDCODE_CONT_LEN 6
238 #endif
239 	/* Keep it sorted by best detection */
240 #ifdef CONFIG_SPI_FLASH_STMICRO
241 	{ 0, 0xff, spi_flash_probe_stmicro, },
242 #endif
243 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
244 	{ 0, 0xff, spi_fram_probe_ramtron, },
245 #endif
246 };
247 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
248 
249 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
250 		unsigned int max_hz, unsigned int spi_mode)
251 {
252 	struct spi_slave *spi;
253 	struct spi_flash *flash = NULL;
254 	int ret, i, shift;
255 	u8 idcode[IDCODE_LEN], *idp;
256 
257 	spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
258 	if (!spi) {
259 		printf("SF: Failed to set up slave\n");
260 		return NULL;
261 	}
262 
263 	ret = spi_claim_bus(spi);
264 	if (ret) {
265 		debug("SF: Failed to claim SPI bus: %d\n", ret);
266 		goto err_claim_bus;
267 	}
268 
269 	/* Read the ID codes */
270 	ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
271 	if (ret)
272 		goto err_read_id;
273 
274 #ifdef DEBUG
275 	printf("SF: Got idcodes\n");
276 	print_buffer(0, idcode, 1, sizeof(idcode), 0);
277 #endif
278 
279 	/* count the number of continuation bytes */
280 	for (shift = 0, idp = idcode;
281 	     shift < IDCODE_CONT_LEN && *idp == 0x7f;
282 	     ++shift, ++idp)
283 		continue;
284 
285 	/* search the table for matches in shift and id */
286 	for (i = 0; i < ARRAY_SIZE(flashes); ++i)
287 		if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
288 			/* we have a match, call probe */
289 			flash = flashes[i].probe(spi, idp);
290 			if (flash)
291 				break;
292 		}
293 
294 	if (!flash) {
295 		printf("SF: Unsupported manufacturer %02x\n", *idp);
296 		goto err_manufacturer_probe;
297 	}
298 
299 	spi_release_bus(spi);
300 
301 	return flash;
302 
303 err_manufacturer_probe:
304 err_read_id:
305 	spi_release_bus(spi);
306 err_claim_bus:
307 	spi_free_slave(spi);
308 	return NULL;
309 }
310 
311 void spi_flash_free(struct spi_flash *flash)
312 {
313 	spi_free_slave(flash->spi);
314 	free(flash);
315 }
316