blob: 7cde4c052ab71d912e91a77b132b9b6a7b6df910 [file] [log] [blame]
Jagannadha Sutradharudu Teki4d5e29a2013-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 Teki0c88a842013-10-10 22:32:55 +05308 * SPDX-License-Identifier: GPL-2.0+
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +05309 */
10
11#include <common.h>
Simon Glassfbb09912014-10-13 23:42:07 -060012#include <dm.h>
Simon Glassae242cb2014-10-13 23:42:05 -060013#include <errno.h>
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053014#include <fdtdec.h>
15#include <malloc.h>
16#include <spi.h>
17#include <spi_flash.h>
Mike Frysingerffdb20b2013-12-03 16:43:27 -070018#include <asm/io.h>
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053019
Jagannadha Sutradharudu Teki898e76c2013-09-26 16:00:15 +053020#include "sf_internal.h"
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053021
22DECLARE_GLOBAL_DATA_PTR;
23
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053024/* Read commands array */
25static u8 spi_read_cmds_array[] = {
26 CMD_READ_ARRAY_SLOW,
Jagannadha Sutradharudu Teki6dd6e902014-12-12 19:36:11 +053027 CMD_READ_ARRAY_FAST,
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053028 CMD_READ_DUAL_OUTPUT_FAST,
29 CMD_READ_DUAL_IO_FAST,
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053030 CMD_READ_QUAD_OUTPUT_FAST,
Jagannadha Sutradharudu Tekic4ba0d82013-12-24 15:24:31 +053031 CMD_READ_QUAD_IO_FAST,
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053032};
33
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053034#ifdef CONFIG_SPI_FLASH_MACRONIX
35static int spi_flash_set_qeb_mxic(struct spi_flash *flash)
36{
37 u8 qeb_status;
38 int ret;
39
40 ret = spi_flash_cmd_read_status(flash, &qeb_status);
41 if (ret < 0)
42 return ret;
43
44 if (qeb_status & STATUS_QEB_MXIC) {
45 debug("SF: mxic: QEB is already set\n");
46 } else {
47 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC);
48 if (ret < 0)
49 return ret;
50 }
51
52 return ret;
53}
54#endif
55
56#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
57static int spi_flash_set_qeb_winspan(struct spi_flash *flash)
58{
59 u8 qeb_status;
60 int ret;
61
62 ret = spi_flash_cmd_read_config(flash, &qeb_status);
63 if (ret < 0)
64 return ret;
65
66 if (qeb_status & STATUS_QEB_WINSPAN) {
67 debug("SF: winspan: QEB is already set\n");
68 } else {
69 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
70 if (ret < 0)
71 return ret;
72 }
73
74 return ret;
75}
76#endif
77
Jagannadha Sutradharudu Tekid08a1ba2013-12-26 13:54:57 +053078static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
79{
80 switch (idcode0) {
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053081#ifdef CONFIG_SPI_FLASH_MACRONIX
82 case SPI_FLASH_CFI_MFR_MACRONIX:
83 return spi_flash_set_qeb_mxic(flash);
84#endif
Jagannadha Sutradharudu Tekid08a1ba2013-12-26 13:54:57 +053085#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
86 case SPI_FLASH_CFI_MFR_SPANSION:
87 case SPI_FLASH_CFI_MFR_WINBOND:
88 return spi_flash_set_qeb_winspan(flash);
89#endif
90#ifdef CONFIG_SPI_FLASH_STMICRO
91 case SPI_FLASH_CFI_MFR_STMICRO:
92 debug("SF: QEB is volatile for %02x flash\n", idcode0);
93 return 0;
94#endif
95 default:
96 printf("SF: Need set QEB func for %02x flash\n", idcode0);
97 return -1;
98 }
99}
100
Simon Glassae242cb2014-10-13 23:42:05 -0600101static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
102 struct spi_flash *flash)
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530103{
104 const struct spi_flash_params *params;
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530105 u8 cmd;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530106 u16 jedec = idcode[1] << 8 | idcode[2];
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530107 u16 ext_jedec = idcode[3] << 8 | idcode[4];
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530108
Simon Glassae242cb2014-10-13 23:42:05 -0600109 /* Validate params from spi_flash_params table */
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +0530110 params = spi_flash_params_table;
111 for (; params->name != NULL; params++) {
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530112 if ((params->jedec >> 16) == idcode[0]) {
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530113 if ((params->jedec & 0xFFFF) == jedec) {
114 if (params->ext_jedec == 0)
115 break;
116 else if (params->ext_jedec == ext_jedec)
117 break;
118 }
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530119 }
120 }
121
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +0530122 if (!params->name) {
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530123 printf("SF: Unsupported flash IDs: ");
124 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
125 idcode[0], jedec, ext_jedec);
Simon Glassae242cb2014-10-13 23:42:05 -0600126 return -EPROTONOSUPPORT;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530127 }
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530128
Jagannadha Sutradharudu Teki469146c2013-10-10 22:14:09 +0530129 /* Assign spi data */
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530130 flash->spi = spi;
131 flash->name = params->name;
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530132 flash->memory_map = spi->memory_map;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530133 flash->dual_flash = flash->spi->option;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530134
135 /* Assign spi_flash ops */
Simon Glassfbb09912014-10-13 23:42:07 -0600136#ifndef CONFIG_DM_SPI_FLASH
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530137 flash->write = spi_flash_cmd_write_ops;
Simon Glassfbb09912014-10-13 23:42:07 -0600138#if defined(CONFIG_SPI_FLASH_SST)
Jagannadha Sutradharudu Teki10ca45d2013-10-02 19:34:53 +0530139 if (params->flags & SST_WP)
140 flash->write = sst_write_wp;
141#endif
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530142 flash->erase = spi_flash_cmd_erase_ops;
143 flash->read = spi_flash_cmd_read_ops;
Simon Glassfbb09912014-10-13 23:42:07 -0600144#endif
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530145
146 /* Compute the flash size */
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530147 flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
Marek Vasutbf640352014-02-21 18:13:26 +0100148 /*
149 * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
150 * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
151 * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
152 * have 256b pages.
153 */
154 if (ext_jedec == 0x4d00) {
155 if ((jedec == 0x0215) || (jedec == 0x216))
156 flash->page_size = 256;
157 else
158 flash->page_size = 512;
159 } else {
160 flash->page_size = 256;
161 }
162 flash->page_size <<= flash->shift;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530163 flash->sector_size = params->sector_size << flash->shift;
164 flash->size = flash->sector_size * params->nr_sectors << flash->shift;
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530165#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530166 if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
167 flash->size <<= 1;
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530168#endif
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530169
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530170 /* Compute erase sector and command */
171 if (params->flags & SECT_4K) {
172 flash->erase_cmd = CMD_ERASE_4K;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530173 flash->erase_size = 4096 << flash->shift;
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530174 } else if (params->flags & SECT_32K) {
175 flash->erase_cmd = CMD_ERASE_32K;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530176 flash->erase_size = 32768 << flash->shift;
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530177 } else {
178 flash->erase_cmd = CMD_ERASE_64K;
179 flash->erase_size = flash->sector_size;
180 }
181
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530182 /* Look for the fastest read cmd */
183 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
184 if (cmd) {
185 cmd = spi_read_cmds_array[cmd - 1];
186 flash->read_cmd = cmd;
187 } else {
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +0530188 /* Go for default supported read cmd */
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530189 flash->read_cmd = CMD_READ_ARRAY_FAST;
190 }
191
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +0530192 /* Not require to look for fastest only two write cmds yet */
193 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
194 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
195 else
196 /* Go for default supported write cmd */
197 flash->write_cmd = CMD_PAGE_PROGRAM;
198
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530199 /* Read dummy_byte: dummy byte is determined based on the
200 * dummy cycles of a particular command.
201 * Fast commands - dummy_byte = dummy_cycles/8
202 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
203 * For I/O commands except cmd[0] everything goes on no.of lines
204 * based on particular command but incase of fast commands except
205 * data all go on single line irrespective of command.
206 */
207 switch (flash->read_cmd) {
208 case CMD_READ_QUAD_IO_FAST:
209 flash->dummy_byte = 2;
210 break;
211 case CMD_READ_ARRAY_SLOW:
212 flash->dummy_byte = 0;
213 break;
214 default:
215 flash->dummy_byte = 1;
216 }
217
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +0530218 /* Poll cmd selection */
Jagannadha Sutradharudu Teki0f623282013-10-02 19:37:43 +0530219 flash->poll_cmd = CMD_READ_STATUS;
220#ifdef CONFIG_SPI_FLASH_STMICRO
221 if (params->flags & E_FSR)
222 flash->poll_cmd = CMD_FLAG_STATUS;
223#endif
224
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530225 /* Configure the BAR - discover bank cmds and read current bank */
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530226#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530227 u8 curr_bank = 0;
228 if (flash->size > SPI_FLASH_16MB_BOUN) {
Simon Glassae242cb2014-10-13 23:42:05 -0600229 int ret;
230
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530231 flash->bank_read_cmd = (idcode[0] == 0x01) ?
232 CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
233 flash->bank_write_cmd = (idcode[0] == 0x01) ?
234 CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
235
Simon Glassae242cb2014-10-13 23:42:05 -0600236 ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
237 &curr_bank, 1);
238 if (ret) {
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530239 debug("SF: fail to read bank addr register\n");
Simon Glassae242cb2014-10-13 23:42:05 -0600240 return ret;
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530241 }
242 flash->bank_curr = curr_bank;
243 } else {
244 flash->bank_curr = curr_bank;
245 }
246#endif
247
Jagannadha Sutradharudu Teki54024c12013-09-26 13:16:45 +0530248 /* Flash powers up read-only, so clear BP# bits */
249#if defined(CONFIG_SPI_FLASH_ATMEL) || \
250 defined(CONFIG_SPI_FLASH_MACRONIX) || \
251 defined(CONFIG_SPI_FLASH_SST)
252 spi_flash_cmd_write_status(flash, 0);
253#endif
254
Simon Glassae242cb2014-10-13 23:42:05 -0600255 return 0;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530256}
257
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530258#ifdef CONFIG_OF_CONTROL
259int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
260{
261 fdt_addr_t addr;
262 fdt_size_t size;
263 int node;
264
265 /* If there is no node, do nothing */
266 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
267 if (node < 0)
268 return 0;
269
270 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
271 if (addr == FDT_ADDR_T_NONE) {
272 debug("%s: Cannot decode address\n", __func__);
273 return 0;
274 }
275
276 if (flash->size != size) {
277 debug("%s: Memory map must cover entire device\n", __func__);
278 return -1;
279 }
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700280 flash->memory_map = map_sysmem(addr, size);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530281
282 return 0;
283}
284#endif /* CONFIG_OF_CONTROL */
285
Heiko Schocher562f8df2014-07-18 06:07:21 +0200286#ifdef CONFIG_SYS_SPI_ST_ENABLE_WP_PIN
287/* enable the W#/Vpp signal to disable writing to the status register */
288static int spi_enable_wp_pin(struct spi_flash *flash)
289{
290 u8 status;
291 int ret;
292
293 ret = spi_flash_cmd_read_status(flash, &status);
294 if (ret < 0)
295 return ret;
296
297 ret = spi_flash_cmd_write_status(flash, STATUS_SRWD);
298 if (ret < 0)
299 return ret;
300
301 ret = spi_flash_cmd_write_disable(flash);
302 if (ret < 0)
303 return ret;
304
305 return 0;
306}
307#else
308static int spi_enable_wp_pin(struct spi_flash *flash)
309{
310 return 0;
311}
312#endif
313
Simon Glassae242cb2014-10-13 23:42:05 -0600314/**
315 * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
316 *
317 * @spi: Bus to probe
318 * @flashp: Pointer to place to put flash info, which may be NULL if the
319 * space should be allocated
320 */
321int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530322{
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530323 u8 idcode[5];
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530324 int ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530325
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530326 /* Setup spi_slave */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530327 if (!spi) {
328 printf("SF: Failed to set up slave\n");
Simon Glassae242cb2014-10-13 23:42:05 -0600329 return -ENODEV;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530330 }
331
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530332 /* Claim spi bus */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530333 ret = spi_claim_bus(spi);
334 if (ret) {
335 debug("SF: Failed to claim SPI bus: %d\n", ret);
Simon Glassae242cb2014-10-13 23:42:05 -0600336 return ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530337 }
338
339 /* Read the ID codes */
340 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530341 if (ret) {
342 printf("SF: Failed to get idcodes\n");
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530343 goto err_read_id;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530344 }
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530345
346#ifdef DEBUG
347 printf("SF: Got idcodes\n");
348 print_buffer(0, idcode, 1, sizeof(idcode), 0);
349#endif
350
Simon Glassae242cb2014-10-13 23:42:05 -0600351 if (spi_flash_validate_params(spi, idcode, flash)) {
352 ret = -EINVAL;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530353 goto err_read_id;
Simon Glassae242cb2014-10-13 23:42:05 -0600354 }
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530355
Poddar, Sourav1f436a62014-04-23 18:57:03 +0530356 /* Set the quad enable bit - only for quad commands */
357 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
358 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
359 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
360 if (spi_flash_set_qeb(flash, idcode[0])) {
361 debug("SF: Fail to set QEB for %02x\n", idcode[0]);
Simon Glassae242cb2014-10-13 23:42:05 -0600362 ret = -EINVAL;
363 goto err_read_id;
Poddar, Sourav1f436a62014-04-23 18:57:03 +0530364 }
365 }
366
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530367#ifdef CONFIG_OF_CONTROL
368 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
369 debug("SF: FDT decode error\n");
Simon Glassae242cb2014-10-13 23:42:05 -0600370 ret = -EINVAL;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530371 goto err_read_id;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530372 }
373#endif
374#ifndef CONFIG_SPL_BUILD
375 printf("SF: Detected %s with page size ", flash->name);
Jagannadha Sutradharudu Teki3ea708f2013-09-15 23:06:12 +0530376 print_size(flash->page_size, ", erase size ");
377 print_size(flash->erase_size, ", total ");
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530378 print_size(flash->size, "");
379 if (flash->memory_map)
380 printf(", mapped at %p", flash->memory_map);
381 puts("\n");
382#endif
383#ifndef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530384 if (((flash->dual_flash == SF_SINGLE_FLASH) &&
385 (flash->size > SPI_FLASH_16MB_BOUN)) ||
386 ((flash->dual_flash > SF_SINGLE_FLASH) &&
387 (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530388 puts("SF: Warning - Only lower 16MiB accessible,");
389 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
390 }
391#endif
Heiko Schocher562f8df2014-07-18 06:07:21 +0200392 if (spi_enable_wp_pin(flash))
393 puts("Enable WP pin failed\n");
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530394
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530395 /* Release spi bus */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530396 spi_release_bus(spi);
397
Simon Glassae242cb2014-10-13 23:42:05 -0600398 return 0;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530399
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530400err_read_id:
401 spi_release_bus(spi);
Simon Glassae242cb2014-10-13 23:42:05 -0600402 return ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530403}
404
Simon Glassfbb09912014-10-13 23:42:07 -0600405#ifndef CONFIG_DM_SPI_FLASH
406struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
Simon Glassae242cb2014-10-13 23:42:05 -0600407{
408 struct spi_flash *flash;
409
410 /* Allocate space if needed (not used by sf-uclass */
411 flash = calloc(1, sizeof(*flash));
412 if (!flash) {
413 debug("SF: Failed to allocate spi_flash\n");
414 return NULL;
415 }
416
417 if (spi_flash_probe_slave(bus, flash)) {
418 spi_free_slave(bus);
419 free(flash);
420 return NULL;
421 }
422
423 return flash;
424}
425
426struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
Simon Glass0efc0242013-12-03 16:43:24 -0700427 unsigned int max_hz, unsigned int spi_mode)
428{
Simon Glassae242cb2014-10-13 23:42:05 -0600429 struct spi_slave *bus;
Simon Glass0efc0242013-12-03 16:43:24 -0700430
Simon Glassae242cb2014-10-13 23:42:05 -0600431 bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
432 return spi_flash_probe_tail(bus);
Simon Glass0efc0242013-12-03 16:43:24 -0700433}
434
435#ifdef CONFIG_OF_SPI_FLASH
436struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
437 int spi_node)
438{
Simon Glassae242cb2014-10-13 23:42:05 -0600439 struct spi_slave *bus;
Simon Glass0efc0242013-12-03 16:43:24 -0700440
Simon Glassae242cb2014-10-13 23:42:05 -0600441 bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
442 return spi_flash_probe_tail(bus);
Simon Glass0efc0242013-12-03 16:43:24 -0700443}
444#endif
445
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530446void spi_flash_free(struct spi_flash *flash)
447{
448 spi_free_slave(flash->spi);
449 free(flash);
450}
Simon Glassfbb09912014-10-13 23:42:07 -0600451
452#else /* defined CONFIG_DM_SPI_FLASH */
453
454static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
455 void *buf)
456{
457 struct spi_flash *flash = dev->uclass_priv;
458
459 return spi_flash_cmd_read_ops(flash, offset, len, buf);
460}
461
462int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
463 const void *buf)
464{
465 struct spi_flash *flash = dev->uclass_priv;
466
467 return spi_flash_cmd_write_ops(flash, offset, len, buf);
468}
469
470int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
471{
472 struct spi_flash *flash = dev->uclass_priv;
473
474 return spi_flash_cmd_erase_ops(flash, offset, len);
475}
476
477int spi_flash_std_probe(struct udevice *dev)
478{
479 struct spi_slave *slave = dev_get_parentdata(dev);
480 struct spi_flash *flash;
481
482 flash = dev->uclass_priv;
483 flash->dev = dev;
484 debug("%s: slave=%p, cs=%d\n", __func__, slave, slave->cs);
485 return spi_flash_probe_slave(slave, flash);
486}
487
488static const struct dm_spi_flash_ops spi_flash_std_ops = {
489 .read = spi_flash_std_read,
490 .write = spi_flash_std_write,
491 .erase = spi_flash_std_erase,
492};
493
494static const struct udevice_id spi_flash_std_ids[] = {
495 { .compatible = "spi-flash" },
496 { }
497};
498
499U_BOOT_DRIVER(spi_flash_std) = {
500 .name = "spi_flash_std",
501 .id = UCLASS_SPI_FLASH,
502 .of_match = spi_flash_std_ids,
503 .probe = spi_flash_std_probe,
504 .priv_auto_alloc_size = sizeof(struct spi_flash),
505 .ops = &spi_flash_std_ops,
506};
507
508#endif /* CONFIG_DM_SPI_FLASH */