blob: f17ec173bf16d116806a70781e3735281c626bad [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>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050016#include <mapmem.h>
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053017#include <spi.h>
18#include <spi_flash.h>
Mike Frysingerffdb20b2013-12-03 16:43:27 -070019#include <asm/io.h>
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053020
Jagannadha Sutradharudu Teki898e76c2013-09-26 16:00:15 +053021#include "sf_internal.h"
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053022
23DECLARE_GLOBAL_DATA_PTR;
24
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053025/* Read commands array */
26static u8 spi_read_cmds_array[] = {
27 CMD_READ_ARRAY_SLOW,
Jagannadha Sutradharudu Teki6dd6e902014-12-12 19:36:11 +053028 CMD_READ_ARRAY_FAST,
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053029 CMD_READ_DUAL_OUTPUT_FAST,
30 CMD_READ_DUAL_IO_FAST,
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053031 CMD_READ_QUAD_OUTPUT_FAST,
Jagannadha Sutradharudu Tekic4ba0d82013-12-24 15:24:31 +053032 CMD_READ_QUAD_IO_FAST,
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053033};
34
Jagannadha Sutradharudu Teki9f4322f2013-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 Tekid08a1ba2013-12-26 13:54:57 +053079static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
80{
81 switch (idcode0) {
Jagannadha Sutradharudu Teki06795122013-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 Tekid08a1ba2013-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
Jagan Teki234a9e12015-09-02 11:39:47 +0530102#ifdef CONFIG_SPI_FLASH_BAR
103static int spi_flash_read_bank(struct spi_flash *flash, u8 idcode0)
104{
105 u8 curr_bank = 0;
106 int ret;
107
108 if (flash->size <= SPI_FLASH_16MB_BOUN)
109 goto bank_end;
110
111 switch (idcode0) {
112 case SPI_FLASH_CFI_MFR_SPANSION:
113 flash->bank_read_cmd = CMD_BANKADDR_BRRD;
114 flash->bank_write_cmd = CMD_BANKADDR_BRWR;
115 default:
116 flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
117 flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
118 }
119
120 ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
121 &curr_bank, 1);
122 if (ret) {
123 debug("SF: fail to read bank addr register\n");
124 return ret;
125 }
126
127bank_end:
128 flash->bank_curr = curr_bank;
129 return 0;
130}
131#endif
132
Simon Glassae242cb2014-10-13 23:42:05 -0600133static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
134 struct spi_flash *flash)
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530135{
136 const struct spi_flash_params *params;
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530137 u8 cmd;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530138 u16 jedec = idcode[1] << 8 | idcode[2];
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530139 u16 ext_jedec = idcode[3] << 8 | idcode[4];
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530140
Simon Glassae242cb2014-10-13 23:42:05 -0600141 /* Validate params from spi_flash_params table */
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +0530142 params = spi_flash_params_table;
143 for (; params->name != NULL; params++) {
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530144 if ((params->jedec >> 16) == idcode[0]) {
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530145 if ((params->jedec & 0xFFFF) == jedec) {
146 if (params->ext_jedec == 0)
147 break;
148 else if (params->ext_jedec == ext_jedec)
149 break;
150 }
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530151 }
152 }
153
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +0530154 if (!params->name) {
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530155 printf("SF: Unsupported flash IDs: ");
156 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
157 idcode[0], jedec, ext_jedec);
Simon Glassae242cb2014-10-13 23:42:05 -0600158 return -EPROTONOSUPPORT;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530159 }
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530160
Jagannadha Sutradharudu Teki469146c2013-10-10 22:14:09 +0530161 /* Assign spi data */
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530162 flash->spi = spi;
163 flash->name = params->name;
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530164 flash->memory_map = spi->memory_map;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530165 flash->dual_flash = flash->spi->option;
Bin Mengbe7be782015-04-28 13:29:54 +0530166#ifdef CONFIG_DM_SPI_FLASH
167 flash->flags = params->flags;
168#endif
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530169
170 /* Assign spi_flash ops */
Simon Glassfbb09912014-10-13 23:42:07 -0600171#ifndef CONFIG_DM_SPI_FLASH
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530172 flash->write = spi_flash_cmd_write_ops;
Simon Glassfbb09912014-10-13 23:42:07 -0600173#if defined(CONFIG_SPI_FLASH_SST)
Jagannadha Sutradharudu Teki54ba6532014-12-12 19:36:14 +0530174 if (params->flags & SST_WR) {
175 if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
176 flash->write = sst_write_bp;
177 else
178 flash->write = sst_write_wp;
179 }
Jagannadha Sutradharudu Teki10ca45d2013-10-02 19:34:53 +0530180#endif
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530181 flash->erase = spi_flash_cmd_erase_ops;
182 flash->read = spi_flash_cmd_read_ops;
Simon Glassfbb09912014-10-13 23:42:07 -0600183#endif
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530184
185 /* Compute the flash size */
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530186 flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
Marek Vasutbf640352014-02-21 18:13:26 +0100187 /*
188 * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
189 * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
190 * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
191 * have 256b pages.
192 */
193 if (ext_jedec == 0x4d00) {
194 if ((jedec == 0x0215) || (jedec == 0x216))
195 flash->page_size = 256;
196 else
197 flash->page_size = 512;
198 } else {
199 flash->page_size = 256;
200 }
201 flash->page_size <<= flash->shift;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530202 flash->sector_size = params->sector_size << flash->shift;
203 flash->size = flash->sector_size * params->nr_sectors << flash->shift;
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530204#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530205 if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
206 flash->size <<= 1;
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530207#endif
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530208
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530209 /* Compute erase sector and command */
210 if (params->flags & SECT_4K) {
211 flash->erase_cmd = CMD_ERASE_4K;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530212 flash->erase_size = 4096 << flash->shift;
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530213 } else if (params->flags & SECT_32K) {
214 flash->erase_cmd = CMD_ERASE_32K;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530215 flash->erase_size = 32768 << flash->shift;
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530216 } else {
217 flash->erase_cmd = CMD_ERASE_64K;
218 flash->erase_size = flash->sector_size;
219 }
220
Jagannadha Sutradharudu Tekic650ca72015-04-27 21:04:15 +0530221 /* Now erase size becomes valid sector size */
222 flash->sector_size = flash->erase_size;
223
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530224 /* Look for the fastest read cmd */
225 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
226 if (cmd) {
227 cmd = spi_read_cmds_array[cmd - 1];
228 flash->read_cmd = cmd;
229 } else {
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +0530230 /* Go for default supported read cmd */
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530231 flash->read_cmd = CMD_READ_ARRAY_FAST;
232 }
233
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +0530234 /* Not require to look for fastest only two write cmds yet */
235 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
236 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
237 else
238 /* Go for default supported write cmd */
239 flash->write_cmd = CMD_PAGE_PROGRAM;
240
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530241 /* Read dummy_byte: dummy byte is determined based on the
242 * dummy cycles of a particular command.
243 * Fast commands - dummy_byte = dummy_cycles/8
244 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
245 * For I/O commands except cmd[0] everything goes on no.of lines
246 * based on particular command but incase of fast commands except
247 * data all go on single line irrespective of command.
248 */
249 switch (flash->read_cmd) {
250 case CMD_READ_QUAD_IO_FAST:
251 flash->dummy_byte = 2;
252 break;
253 case CMD_READ_ARRAY_SLOW:
254 flash->dummy_byte = 0;
255 break;
256 default:
257 flash->dummy_byte = 1;
258 }
259
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +0530260 /* Poll cmd selection */
Jagannadha Sutradharudu Teki0f623282013-10-02 19:37:43 +0530261 flash->poll_cmd = CMD_READ_STATUS;
262#ifdef CONFIG_SPI_FLASH_STMICRO
263 if (params->flags & E_FSR)
264 flash->poll_cmd = CMD_FLAG_STATUS;
265#endif
266
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530267 /* Configure the BAR - discover bank cmds and read current bank */
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530268#ifdef CONFIG_SPI_FLASH_BAR
Jagan Teki234a9e12015-09-02 11:39:47 +0530269 int ret = spi_flash_read_bank(flash, idcode[0]);
270 if (ret < 0)
271 return ret;
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530272#endif
273
Jagannadha Sutradharudu Teki54024c12013-09-26 13:16:45 +0530274 /* Flash powers up read-only, so clear BP# bits */
275#if defined(CONFIG_SPI_FLASH_ATMEL) || \
276 defined(CONFIG_SPI_FLASH_MACRONIX) || \
277 defined(CONFIG_SPI_FLASH_SST)
278 spi_flash_cmd_write_status(flash, 0);
279#endif
280
Simon Glassae242cb2014-10-13 23:42:05 -0600281 return 0;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530282}
283
Masahiro Yamada0f925822015-08-12 07:31:55 +0900284#if CONFIG_IS_ENABLED(OF_CONTROL)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530285int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
286{
287 fdt_addr_t addr;
288 fdt_size_t size;
289 int node;
290
291 /* If there is no node, do nothing */
292 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
293 if (node < 0)
294 return 0;
295
296 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
297 if (addr == FDT_ADDR_T_NONE) {
298 debug("%s: Cannot decode address\n", __func__);
299 return 0;
300 }
301
302 if (flash->size != size) {
303 debug("%s: Memory map must cover entire device\n", __func__);
304 return -1;
305 }
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700306 flash->memory_map = map_sysmem(addr, size);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530307
308 return 0;
309}
Masahiro Yamada0f925822015-08-12 07:31:55 +0900310#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530311
Simon Glassae242cb2014-10-13 23:42:05 -0600312/**
313 * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
314 *
315 * @spi: Bus to probe
316 * @flashp: Pointer to place to put flash info, which may be NULL if the
317 * space should be allocated
318 */
319int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530320{
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530321 u8 idcode[5];
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530322 int ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530323
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530324 /* Setup spi_slave */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530325 if (!spi) {
326 printf("SF: Failed to set up slave\n");
Simon Glassae242cb2014-10-13 23:42:05 -0600327 return -ENODEV;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530328 }
329
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530330 /* Claim spi bus */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530331 ret = spi_claim_bus(spi);
332 if (ret) {
333 debug("SF: Failed to claim SPI bus: %d\n", ret);
Simon Glassae242cb2014-10-13 23:42:05 -0600334 return ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530335 }
336
337 /* Read the ID codes */
338 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530339 if (ret) {
340 printf("SF: Failed to get idcodes\n");
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530341 goto err_read_id;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530342 }
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530343
344#ifdef DEBUG
345 printf("SF: Got idcodes\n");
346 print_buffer(0, idcode, 1, sizeof(idcode), 0);
347#endif
348
Simon Glassae242cb2014-10-13 23:42:05 -0600349 if (spi_flash_validate_params(spi, idcode, flash)) {
350 ret = -EINVAL;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530351 goto err_read_id;
Simon Glassae242cb2014-10-13 23:42:05 -0600352 }
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530353
Poddar, Sourav1f436a62014-04-23 18:57:03 +0530354 /* Set the quad enable bit - only for quad commands */
355 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
356 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
357 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
358 if (spi_flash_set_qeb(flash, idcode[0])) {
359 debug("SF: Fail to set QEB for %02x\n", idcode[0]);
Simon Glassae242cb2014-10-13 23:42:05 -0600360 ret = -EINVAL;
361 goto err_read_id;
Poddar, Sourav1f436a62014-04-23 18:57:03 +0530362 }
363 }
364
Masahiro Yamada0f925822015-08-12 07:31:55 +0900365#if CONFIG_IS_ENABLED(OF_CONTROL)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530366 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
367 debug("SF: FDT decode error\n");
Simon Glassae242cb2014-10-13 23:42:05 -0600368 ret = -EINVAL;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530369 goto err_read_id;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530370 }
371#endif
372#ifndef CONFIG_SPL_BUILD
373 printf("SF: Detected %s with page size ", flash->name);
Jagannadha Sutradharudu Teki3ea708f2013-09-15 23:06:12 +0530374 print_size(flash->page_size, ", erase size ");
375 print_size(flash->erase_size, ", total ");
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530376 print_size(flash->size, "");
377 if (flash->memory_map)
378 printf(", mapped at %p", flash->memory_map);
379 puts("\n");
380#endif
381#ifndef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530382 if (((flash->dual_flash == SF_SINGLE_FLASH) &&
383 (flash->size > SPI_FLASH_16MB_BOUN)) ||
384 ((flash->dual_flash > SF_SINGLE_FLASH) &&
385 (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530386 puts("SF: Warning - Only lower 16MiB accessible,");
387 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
388 }
389#endif
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +0200390#ifdef CONFIG_SPI_FLASH_MTD
391 ret = spi_flash_mtd_register(flash);
392#endif
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530393
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530394err_read_id:
395 spi_release_bus(spi);
Simon Glassae242cb2014-10-13 23:42:05 -0600396 return ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530397}
398
Simon Glassfbb09912014-10-13 23:42:07 -0600399#ifndef CONFIG_DM_SPI_FLASH
400struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
Simon Glassae242cb2014-10-13 23:42:05 -0600401{
402 struct spi_flash *flash;
403
404 /* Allocate space if needed (not used by sf-uclass */
405 flash = calloc(1, sizeof(*flash));
406 if (!flash) {
407 debug("SF: Failed to allocate spi_flash\n");
408 return NULL;
409 }
410
411 if (spi_flash_probe_slave(bus, flash)) {
412 spi_free_slave(bus);
413 free(flash);
414 return NULL;
415 }
416
417 return flash;
418}
419
420struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
Simon Glass0efc0242013-12-03 16:43:24 -0700421 unsigned int max_hz, unsigned int spi_mode)
422{
Simon Glassae242cb2014-10-13 23:42:05 -0600423 struct spi_slave *bus;
Simon Glass0efc0242013-12-03 16:43:24 -0700424
Simon Glassae242cb2014-10-13 23:42:05 -0600425 bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
Peng Fan4fbad922015-03-20 13:19:16 +0800426 if (!bus)
427 return NULL;
Simon Glassae242cb2014-10-13 23:42:05 -0600428 return spi_flash_probe_tail(bus);
Simon Glass0efc0242013-12-03 16:43:24 -0700429}
430
431#ifdef CONFIG_OF_SPI_FLASH
432struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
433 int spi_node)
434{
Simon Glassae242cb2014-10-13 23:42:05 -0600435 struct spi_slave *bus;
Simon Glass0efc0242013-12-03 16:43:24 -0700436
Simon Glassae242cb2014-10-13 23:42:05 -0600437 bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
Peng Fan4fbad922015-03-20 13:19:16 +0800438 if (!bus)
439 return NULL;
Simon Glassae242cb2014-10-13 23:42:05 -0600440 return spi_flash_probe_tail(bus);
Simon Glass0efc0242013-12-03 16:43:24 -0700441}
442#endif
443
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530444void spi_flash_free(struct spi_flash *flash)
445{
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +0200446#ifdef CONFIG_SPI_FLASH_MTD
447 spi_flash_mtd_unregister();
448#endif
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530449 spi_free_slave(flash->spi);
450 free(flash);
451}
Simon Glassfbb09912014-10-13 23:42:07 -0600452
453#else /* defined CONFIG_DM_SPI_FLASH */
454
455static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
456 void *buf)
457{
Simon Glasse564f052015-03-05 12:25:20 -0700458 struct spi_flash *flash = dev_get_uclass_priv(dev);
Simon Glassfbb09912014-10-13 23:42:07 -0600459
460 return spi_flash_cmd_read_ops(flash, offset, len, buf);
461}
462
463int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
464 const void *buf)
465{
Simon Glasse564f052015-03-05 12:25:20 -0700466 struct spi_flash *flash = dev_get_uclass_priv(dev);
Simon Glassfbb09912014-10-13 23:42:07 -0600467
Bin Meng074eed52015-04-24 19:51:10 +0800468#if defined(CONFIG_SPI_FLASH_SST)
469 if (flash->flags & SST_WR) {
470 if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
471 return sst_write_bp(flash, offset, len, buf);
472 else
473 return sst_write_wp(flash, offset, len, buf);
474 }
475#endif
476
Simon Glassfbb09912014-10-13 23:42:07 -0600477 return spi_flash_cmd_write_ops(flash, offset, len, buf);
478}
479
480int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
481{
Simon Glasse564f052015-03-05 12:25:20 -0700482 struct spi_flash *flash = dev_get_uclass_priv(dev);
Simon Glassfbb09912014-10-13 23:42:07 -0600483
484 return spi_flash_cmd_erase_ops(flash, offset, len);
485}
486
487int spi_flash_std_probe(struct udevice *dev)
488{
489 struct spi_slave *slave = dev_get_parentdata(dev);
Simon Glassd0cff032015-01-25 08:27:12 -0700490 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassfbb09912014-10-13 23:42:07 -0600491 struct spi_flash *flash;
492
Simon Glasse564f052015-03-05 12:25:20 -0700493 flash = dev_get_uclass_priv(dev);
Simon Glassfbb09912014-10-13 23:42:07 -0600494 flash->dev = dev;
Simon Glassd0cff032015-01-25 08:27:12 -0700495 debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
Simon Glassfbb09912014-10-13 23:42:07 -0600496 return spi_flash_probe_slave(slave, flash);
497}
498
499static const struct dm_spi_flash_ops spi_flash_std_ops = {
500 .read = spi_flash_std_read,
501 .write = spi_flash_std_write,
502 .erase = spi_flash_std_erase,
503};
504
505static const struct udevice_id spi_flash_std_ids[] = {
506 { .compatible = "spi-flash" },
507 { }
508};
509
510U_BOOT_DRIVER(spi_flash_std) = {
511 .name = "spi_flash_std",
512 .id = UCLASS_SPI_FLASH,
513 .of_match = spi_flash_std_ids,
514 .probe = spi_flash_std_probe,
515 .priv_auto_alloc_size = sizeof(struct spi_flash),
516 .ops = &spi_flash_std_ops,
517};
518
519#endif /* CONFIG_DM_SPI_FLASH */