blob: c780a81c599076056cb10c1edb2cd582c29576fc [file] [log] [blame]
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02001/*
2 * SPI flash interface
3 *
4 * Copyright (C) 2008 Atmel Corporation
Reinhard Meyercfe1c672010-10-05 16:56:39 +02005 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 *
Jagannadha Sutradharudu Tekid1452702013-10-10 22:32:55 +05307 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02008 */
Mike Frysingerc5431ac2009-03-23 23:03:58 -04009
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020010#include <common.h>
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020011#include <spi.h>
Mike Frysinger53421bb2011-01-10 02:20:13 -050012
Mike Frysinger5efdb042011-01-10 02:20:11 -050013static int spi_flash_read_write(struct spi_slave *spi,
14 const u8 *cmd, size_t cmd_len,
15 const u8 *data_out, u8 *data_in,
16 size_t data_len)
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020017{
18 unsigned long flags = SPI_XFER_BEGIN;
19 int ret;
20
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +053021 if (spi->flags & SPI_XFER_U_PAGE)
22 flags |= SPI_XFER_U_PAGE;
23
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020024 if (data_len == 0)
25 flags |= SPI_XFER_END;
26
27 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
28 if (ret) {
Mike Frysinger5efdb042011-01-10 02:20:11 -050029 debug("SF: Failed to send command (%zu bytes): %d\n",
Jagannadha Sutradharudu Teki1a833032013-07-29 22:16:41 +053030 cmd_len, ret);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020031 } else if (data_len != 0) {
Jagannadha Sutradharudu Teki1a833032013-07-29 22:16:41 +053032 ret = spi_xfer(spi, data_len * 8, data_out, data_in,
33 SPI_XFER_END);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020034 if (ret)
Mike Frysinger5efdb042011-01-10 02:20:11 -050035 debug("SF: Failed to transfer %zu bytes of data: %d\n",
Jagannadha Sutradharudu Teki1a833032013-07-29 22:16:41 +053036 data_len, ret);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020037 }
38
39 return ret;
40}
41
Mike Frysinger5efdb042011-01-10 02:20:11 -050042int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
43 size_t cmd_len, void *data, size_t data_len)
44{
45 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020046}
Mike Frysinger6fe6d0d2012-03-04 23:18:17 -050047
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053048int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
Simon Glassb162a7c2013-03-11 06:08:02 +000049{
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053050 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
Simon Glassb162a7c2013-03-11 06:08:02 +000051}
52
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053053int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
54 const void *data, size_t data_len)
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020055{
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053056 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020057}