blob: d19138d9071d1365b080610bac46e649691aa84c [file] [log] [blame]
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05301/*
2 * SPI flash probing
3 *
4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
7 *
Jagannadha Sutradharudu Tekid1452702013-10-10 22:32:55 +05308 * SPDX-License-Identifier: GPL-2.0+
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05309 */
10
11#include <common.h>
Simon Glass6f7d2592014-10-13 23:42:07 -060012#include <dm.h>
Simon Glassc10c3d42014-10-13 23:42:05 -060013#include <errno.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053014#include <fdtdec.h>
15#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050016#include <mapmem.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053017#include <spi.h>
18#include <spi_flash.h>
Mike Frysingerb375ad92013-12-03 16:43:27 -070019#include <asm/io.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053020
Jagannadha Sutradharudu Tekiac8242d2013-09-26 16:00:15 +053021#include "sf_internal.h"
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053022
23DECLARE_GLOBAL_DATA_PTR;
24
Jagannadha Sutradharudu Teki02eee9a2014-01-11 15:10:28 +053025/* Read commands array */
26static u8 spi_read_cmds_array[] = {
27 CMD_READ_ARRAY_SLOW,
Jagannadha Sutradharudu Teki29e63912014-12-12 19:36:11 +053028 CMD_READ_ARRAY_FAST,
Jagannadha Sutradharudu Teki02eee9a2014-01-11 15:10:28 +053029 CMD_READ_DUAL_OUTPUT_FAST,
30 CMD_READ_DUAL_IO_FAST,
Jagannadha Sutradharudu Tekie0ebabc2014-01-11 15:13:11 +053031 CMD_READ_QUAD_OUTPUT_FAST,
Jagannadha Sutradharudu Teki45462302013-12-24 15:24:31 +053032 CMD_READ_QUAD_IO_FAST,
Jagannadha Sutradharudu Teki02eee9a2014-01-11 15:10:28 +053033};
34
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053035#ifdef CONFIG_SPI_FLASH_MACRONIX
36static int spi_flash_set_qeb_mxic(struct spi_flash *flash)
37{
38 u8 qeb_status;
39 int ret;
40
41 ret = spi_flash_cmd_read_status(flash, &qeb_status);
42 if (ret < 0)
43 return ret;
44
45 if (qeb_status & STATUS_QEB_MXIC) {
46 debug("SF: mxic: QEB is already set\n");
47 } else {
48 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC);
49 if (ret < 0)
50 return ret;
51 }
52
53 return ret;
54}
55#endif
56
57#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
58static int spi_flash_set_qeb_winspan(struct spi_flash *flash)
59{
60 u8 qeb_status;
61 int ret;
62
63 ret = spi_flash_cmd_read_config(flash, &qeb_status);
64 if (ret < 0)
65 return ret;
66
67 if (qeb_status & STATUS_QEB_WINSPAN) {
68 debug("SF: winspan: QEB is already set\n");
69 } else {
70 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
71 if (ret < 0)
72 return ret;
73 }
74
75 return ret;
76}
77#endif
78
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053079static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
80{
81 switch (idcode0) {
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053082#ifdef CONFIG_SPI_FLASH_MACRONIX
83 case SPI_FLASH_CFI_MFR_MACRONIX:
84 return spi_flash_set_qeb_mxic(flash);
85#endif
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053086#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
87 case SPI_FLASH_CFI_MFR_SPANSION:
88 case SPI_FLASH_CFI_MFR_WINBOND:
89 return spi_flash_set_qeb_winspan(flash);
90#endif
91#ifdef CONFIG_SPI_FLASH_STMICRO
92 case SPI_FLASH_CFI_MFR_STMICRO:
93 debug("SF: QEB is volatile for %02x flash\n", idcode0);
94 return 0;
95#endif
96 default:
97 printf("SF: Need set QEB func for %02x flash\n", idcode0);
98 return -1;
99 }
100}
101
Simon Glassc10c3d42014-10-13 23:42:05 -0600102static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
103 struct spi_flash *flash)
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530104{
105 const struct spi_flash_params *params;
Jagannadha Sutradharudu Teki02eee9a2014-01-11 15:10:28 +0530106 u8 cmd;
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530107 u16 jedec = idcode[1] << 8 | idcode[2];
Jagannadha Sutradharudu Teki25315532013-09-28 16:49:37 +0530108 u16 ext_jedec = idcode[3] << 8 | idcode[4];
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530109
Simon Glassc10c3d42014-10-13 23:42:05 -0600110 /* Validate params from spi_flash_params table */
Jagannadha Sutradharudu Teki20e45bc2013-12-23 23:34:42 +0530111 params = spi_flash_params_table;
112 for (; params->name != NULL; params++) {
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530113 if ((params->jedec >> 16) == idcode[0]) {
Jagannadha Sutradharudu Teki25315532013-09-28 16:49:37 +0530114 if ((params->jedec & 0xFFFF) == jedec) {
115 if (params->ext_jedec == 0)
116 break;
117 else if (params->ext_jedec == ext_jedec)
118 break;
119 }
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530120 }
121 }
122
Jagannadha Sutradharudu Teki20e45bc2013-12-23 23:34:42 +0530123 if (!params->name) {
Jagannadha Sutradharudu Teki25315532013-09-28 16:49:37 +0530124 printf("SF: Unsupported flash IDs: ");
125 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
126 idcode[0], jedec, ext_jedec);
Simon Glassc10c3d42014-10-13 23:42:05 -0600127 return -EPROTONOSUPPORT;
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530128 }
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530129
Jagannadha Sutradharudu Teki84fb8632013-10-10 22:14:09 +0530130 /* Assign spi data */
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530131 flash->spi = spi;
132 flash->name = params->name;
Jagannadha Sutradharudu Tekif3b2dd82013-10-07 19:34:56 +0530133 flash->memory_map = spi->memory_map;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530134 flash->dual_flash = flash->spi->option;
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530135
136 /* Assign spi_flash ops */
Simon Glass6f7d2592014-10-13 23:42:07 -0600137#ifndef CONFIG_DM_SPI_FLASH
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530138 flash->write = spi_flash_cmd_write_ops;
Simon Glass6f7d2592014-10-13 23:42:07 -0600139#if defined(CONFIG_SPI_FLASH_SST)
Jagannadha Sutradharudu Teki7f0fd702014-12-12 19:36:14 +0530140 if (params->flags & SST_WR) {
141 if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
142 flash->write = sst_write_bp;
143 else
144 flash->write = sst_write_wp;
145 }
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530146#endif
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530147 flash->erase = spi_flash_cmd_erase_ops;
148 flash->read = spi_flash_cmd_read_ops;
Simon Glass6f7d2592014-10-13 23:42:07 -0600149#endif
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530150
151 /* Compute the flash size */
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530152 flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
Marek Vasut52fa1ff2014-02-21 18:13:26 +0100153 /*
154 * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
155 * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
156 * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
157 * have 256b pages.
158 */
159 if (ext_jedec == 0x4d00) {
160 if ((jedec == 0x0215) || (jedec == 0x216))
161 flash->page_size = 256;
162 else
163 flash->page_size = 512;
164 } else {
165 flash->page_size = 256;
166 }
167 flash->page_size <<= flash->shift;
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530168 flash->sector_size = params->sector_size << flash->shift;
169 flash->size = flash->sector_size * params->nr_sectors << flash->shift;
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530170#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530171 if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
172 flash->size <<= 1;
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530173#endif
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530174
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530175 /* Compute erase sector and command */
176 if (params->flags & SECT_4K) {
177 flash->erase_cmd = CMD_ERASE_4K;
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530178 flash->erase_size = 4096 << flash->shift;
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530179 } else if (params->flags & SECT_32K) {
180 flash->erase_cmd = CMD_ERASE_32K;
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530181 flash->erase_size = 32768 << flash->shift;
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530182 } else {
183 flash->erase_cmd = CMD_ERASE_64K;
184 flash->erase_size = flash->sector_size;
185 }
186
Jagannadha Sutradharudu Teki02eee9a2014-01-11 15:10:28 +0530187 /* Look for the fastest read cmd */
188 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
189 if (cmd) {
190 cmd = spi_read_cmds_array[cmd - 1];
191 flash->read_cmd = cmd;
192 } else {
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +0530193 /* Go for default supported read cmd */
Jagannadha Sutradharudu Teki02eee9a2014-01-11 15:10:28 +0530194 flash->read_cmd = CMD_READ_ARRAY_FAST;
195 }
196
Jagannadha Sutradharudu Tekie0ebabc2014-01-11 15:13:11 +0530197 /* Not require to look for fastest only two write cmds yet */
198 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
199 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
200 else
201 /* Go for default supported write cmd */
202 flash->write_cmd = CMD_PAGE_PROGRAM;
203
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530204 /* Read dummy_byte: dummy byte is determined based on the
205 * dummy cycles of a particular command.
206 * Fast commands - dummy_byte = dummy_cycles/8
207 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
208 * For I/O commands except cmd[0] everything goes on no.of lines
209 * based on particular command but incase of fast commands except
210 * data all go on single line irrespective of command.
211 */
212 switch (flash->read_cmd) {
213 case CMD_READ_QUAD_IO_FAST:
214 flash->dummy_byte = 2;
215 break;
216 case CMD_READ_ARRAY_SLOW:
217 flash->dummy_byte = 0;
218 break;
219 default:
220 flash->dummy_byte = 1;
221 }
222
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +0530223 /* Poll cmd selection */
Jagannadha Sutradharudu Tekif41fcca2013-10-02 19:37:43 +0530224 flash->poll_cmd = CMD_READ_STATUS;
225#ifdef CONFIG_SPI_FLASH_STMICRO
226 if (params->flags & E_FSR)
227 flash->poll_cmd = CMD_FLAG_STATUS;
228#endif
229
Jagannadha Sutradharudu Tekif3b2dd82013-10-07 19:34:56 +0530230 /* Configure the BAR - discover bank cmds and read current bank */
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530231#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530232 u8 curr_bank = 0;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530233 if (flash->size > SPI_FLASH_16MB_BOUN) {
Simon Glassc10c3d42014-10-13 23:42:05 -0600234 int ret;
235
Jagannadha Sutradharudu Teki748bd4d2013-09-26 14:24:58 +0530236 flash->bank_read_cmd = (idcode[0] == 0x01) ?
237 CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
238 flash->bank_write_cmd = (idcode[0] == 0x01) ?
239 CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
240
Simon Glassc10c3d42014-10-13 23:42:05 -0600241 ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
242 &curr_bank, 1);
243 if (ret) {
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530244 debug("SF: fail to read bank addr register\n");
Simon Glassc10c3d42014-10-13 23:42:05 -0600245 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530246 }
247 flash->bank_curr = curr_bank;
248 } else {
249 flash->bank_curr = curr_bank;
250 }
Jagannadha Sutradharudu Teki748bd4d2013-09-26 14:24:58 +0530251#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530252
Jagannadha Sutradharudu Teki748bd4d2013-09-26 14:24:58 +0530253 /* Flash powers up read-only, so clear BP# bits */
254#if defined(CONFIG_SPI_FLASH_ATMEL) || \
255 defined(CONFIG_SPI_FLASH_MACRONIX) || \
256 defined(CONFIG_SPI_FLASH_SST)
257 spi_flash_cmd_write_status(flash, 0);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530258#endif
259
Simon Glassc10c3d42014-10-13 23:42:05 -0600260 return 0;
Jagannadha Sutradharudu Teki748bd4d2013-09-26 14:24:58 +0530261}
262
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530263#ifdef CONFIG_OF_CONTROL
264int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
265{
266 fdt_addr_t addr;
267 fdt_size_t size;
268 int node;
269
270 /* If there is no node, do nothing */
271 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
272 if (node < 0)
273 return 0;
274
275 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
276 if (addr == FDT_ADDR_T_NONE) {
277 debug("%s: Cannot decode address\n", __func__);
278 return 0;
279 }
280
281 if (flash->size != size) {
282 debug("%s: Memory map must cover entire device\n", __func__);
283 return -1;
284 }
Mike Frysingerb375ad92013-12-03 16:43:27 -0700285 flash->memory_map = map_sysmem(addr, size);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530286
287 return 0;
288}
289#endif /* CONFIG_OF_CONTROL */
290
Heiko Schocherd73b7ec2014-07-18 06:07:21 +0200291#ifdef CONFIG_SYS_SPI_ST_ENABLE_WP_PIN
292/* enable the W#/Vpp signal to disable writing to the status register */
293static int spi_enable_wp_pin(struct spi_flash *flash)
294{
295 u8 status;
296 int ret;
297
298 ret = spi_flash_cmd_read_status(flash, &status);
299 if (ret < 0)
300 return ret;
301
302 ret = spi_flash_cmd_write_status(flash, STATUS_SRWD);
303 if (ret < 0)
304 return ret;
305
306 ret = spi_flash_cmd_write_disable(flash);
307 if (ret < 0)
308 return ret;
309
310 return 0;
311}
312#else
313static int spi_enable_wp_pin(struct spi_flash *flash)
314{
315 return 0;
316}
317#endif
318
Simon Glassc10c3d42014-10-13 23:42:05 -0600319/**
320 * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
321 *
322 * @spi: Bus to probe
323 * @flashp: Pointer to place to put flash info, which may be NULL if the
324 * space should be allocated
325 */
326int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530327{
Jagannadha Sutradharudu Teki748bd4d2013-09-26 14:24:58 +0530328 u8 idcode[5];
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530329 int ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530330
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530331 /* Setup spi_slave */
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530332 if (!spi) {
333 printf("SF: Failed to set up slave\n");
Simon Glassc10c3d42014-10-13 23:42:05 -0600334 return -ENODEV;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530335 }
336
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530337 /* Claim spi bus */
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530338 ret = spi_claim_bus(spi);
339 if (ret) {
340 debug("SF: Failed to claim SPI bus: %d\n", ret);
Simon Glassc10c3d42014-10-13 23:42:05 -0600341 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530342 }
343
344 /* Read the ID codes */
345 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530346 if (ret) {
347 printf("SF: Failed to get idcodes\n");
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530348 goto err_read_id;
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530349 }
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530350
351#ifdef DEBUG
352 printf("SF: Got idcodes\n");
353 print_buffer(0, idcode, 1, sizeof(idcode), 0);
354#endif
355
Simon Glassc10c3d42014-10-13 23:42:05 -0600356 if (spi_flash_validate_params(spi, idcode, flash)) {
357 ret = -EINVAL;
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530358 goto err_read_id;
Simon Glassc10c3d42014-10-13 23:42:05 -0600359 }
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530360
Poddar, Sourav39a5ebc2014-04-23 18:57:03 +0530361 /* Set the quad enable bit - only for quad commands */
362 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
363 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
364 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
365 if (spi_flash_set_qeb(flash, idcode[0])) {
366 debug("SF: Fail to set QEB for %02x\n", idcode[0]);
Simon Glassc10c3d42014-10-13 23:42:05 -0600367 ret = -EINVAL;
368 goto err_read_id;
Poddar, Sourav39a5ebc2014-04-23 18:57:03 +0530369 }
370 }
371
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530372#ifdef CONFIG_OF_CONTROL
373 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
374 debug("SF: FDT decode error\n");
Simon Glassc10c3d42014-10-13 23:42:05 -0600375 ret = -EINVAL;
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530376 goto err_read_id;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530377 }
378#endif
379#ifndef CONFIG_SPL_BUILD
380 printf("SF: Detected %s with page size ", flash->name);
Jagannadha Sutradharudu Teki918d6ac2013-09-15 23:06:12 +0530381 print_size(flash->page_size, ", erase size ");
382 print_size(flash->erase_size, ", total ");
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530383 print_size(flash->size, "");
384 if (flash->memory_map)
385 printf(", mapped at %p", flash->memory_map);
386 puts("\n");
387#endif
388#ifndef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530389 if (((flash->dual_flash == SF_SINGLE_FLASH) &&
390 (flash->size > SPI_FLASH_16MB_BOUN)) ||
391 ((flash->dual_flash > SF_SINGLE_FLASH) &&
392 (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530393 puts("SF: Warning - Only lower 16MiB accessible,");
394 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
395 }
396#endif
Heiko Schocherd73b7ec2014-07-18 06:07:21 +0200397 if (spi_enable_wp_pin(flash))
398 puts("Enable WP pin failed\n");
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530399
Jagannadha Sutradharudu Tekibb302202013-09-24 16:01:23 +0530400 /* Release spi bus */
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530401 spi_release_bus(spi);
402
Simon Glassc10c3d42014-10-13 23:42:05 -0600403 return 0;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530404
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530405err_read_id:
406 spi_release_bus(spi);
Simon Glassc10c3d42014-10-13 23:42:05 -0600407 return ret;
408}
409
Simon Glass6f7d2592014-10-13 23:42:07 -0600410#ifndef CONFIG_DM_SPI_FLASH
411struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
Simon Glassc10c3d42014-10-13 23:42:05 -0600412{
413 struct spi_flash *flash;
414
415 /* Allocate space if needed (not used by sf-uclass */
416 flash = calloc(1, sizeof(*flash));
417 if (!flash) {
418 debug("SF: Failed to allocate spi_flash\n");
419 return NULL;
420 }
421
422 if (spi_flash_probe_slave(bus, flash)) {
423 spi_free_slave(bus);
424 free(flash);
425 return NULL;
426 }
427
428 return flash;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530429}
430
Simon Glassc10c3d42014-10-13 23:42:05 -0600431struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
Simon Glass49e9d2c2013-12-03 16:43:24 -0700432 unsigned int max_hz, unsigned int spi_mode)
433{
Simon Glassc10c3d42014-10-13 23:42:05 -0600434 struct spi_slave *bus;
Simon Glass49e9d2c2013-12-03 16:43:24 -0700435
Simon Glassc10c3d42014-10-13 23:42:05 -0600436 bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
437 return spi_flash_probe_tail(bus);
Simon Glass49e9d2c2013-12-03 16:43:24 -0700438}
439
440#ifdef CONFIG_OF_SPI_FLASH
441struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
442 int spi_node)
443{
Simon Glassc10c3d42014-10-13 23:42:05 -0600444 struct spi_slave *bus;
Simon Glass49e9d2c2013-12-03 16:43:24 -0700445
Simon Glassc10c3d42014-10-13 23:42:05 -0600446 bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
447 return spi_flash_probe_tail(bus);
Simon Glass49e9d2c2013-12-03 16:43:24 -0700448}
449#endif
450
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530451void spi_flash_free(struct spi_flash *flash)
452{
453 spi_free_slave(flash->spi);
454 free(flash);
455}
Simon Glass6f7d2592014-10-13 23:42:07 -0600456
457#else /* defined CONFIG_DM_SPI_FLASH */
458
459static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
460 void *buf)
461{
Simon Glassde0977b2015-03-05 12:25:20 -0700462 struct spi_flash *flash = dev_get_uclass_priv(dev);
Simon Glass6f7d2592014-10-13 23:42:07 -0600463
464 return spi_flash_cmd_read_ops(flash, offset, len, buf);
465}
466
467int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
468 const void *buf)
469{
Simon Glassde0977b2015-03-05 12:25:20 -0700470 struct spi_flash *flash = dev_get_uclass_priv(dev);
Simon Glass6f7d2592014-10-13 23:42:07 -0600471
472 return spi_flash_cmd_write_ops(flash, offset, len, buf);
473}
474
475int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
476{
Simon Glassde0977b2015-03-05 12:25:20 -0700477 struct spi_flash *flash = dev_get_uclass_priv(dev);
Simon Glass6f7d2592014-10-13 23:42:07 -0600478
479 return spi_flash_cmd_erase_ops(flash, offset, len);
480}
481
482int spi_flash_std_probe(struct udevice *dev)
483{
484 struct spi_slave *slave = dev_get_parentdata(dev);
Simon Glass5d2ee052015-01-25 08:27:12 -0700485 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glass6f7d2592014-10-13 23:42:07 -0600486 struct spi_flash *flash;
487
Simon Glassde0977b2015-03-05 12:25:20 -0700488 flash = dev_get_uclass_priv(dev);
Simon Glass6f7d2592014-10-13 23:42:07 -0600489 flash->dev = dev;
Simon Glass5d2ee052015-01-25 08:27:12 -0700490 debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
Simon Glass6f7d2592014-10-13 23:42:07 -0600491 return spi_flash_probe_slave(slave, flash);
492}
493
494static const struct dm_spi_flash_ops spi_flash_std_ops = {
495 .read = spi_flash_std_read,
496 .write = spi_flash_std_write,
497 .erase = spi_flash_std_erase,
498};
499
500static const struct udevice_id spi_flash_std_ids[] = {
501 { .compatible = "spi-flash" },
502 { }
503};
504
505U_BOOT_DRIVER(spi_flash_std) = {
506 .name = "spi_flash_std",
507 .id = UCLASS_SPI_FLASH,
508 .of_match = spi_flash_std_ids,
509 .probe = spi_flash_std_probe,
510 .priv_auto_alloc_size = sizeof(struct spi_flash),
511 .ops = &spi_flash_std_ops,
512};
513
514#endif /* CONFIG_DM_SPI_FLASH */