blob: d5e175ca0000f571ed3cfa290cbd12653cd10f76 [file] [log] [blame]
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02001/*
2 * SPI flash interface
3 *
4 * Copyright (C) 2008 Atmel Corporation
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +02005 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 *
Jagannadha Sutradharudu Teki0c88a842013-10-10 22:32:55 +05307 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02008 */
Mike Frysingerf773a1b2009-03-23 23:03:58 -04009
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020010#include <common.h>
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020011#include <spi.h>
Mike Frysingere7b44ed2011-01-10 02:20:13 -050012
Mike Frysinger000044d2011-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 Skinnemoend25ce7d2008-05-16 11:10:33 +020017{
18 unsigned long flags = SPI_XFER_BEGIN;
19 int ret;
20
Mike Frysinger000044d2011-01-10 02:20:11 -050021 if (data_len == 0)
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020022 flags |= SPI_XFER_END;
23
Mike Frysinger000044d2011-01-10 02:20:11 -050024 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020025 if (ret) {
Mike Frysinger000044d2011-01-10 02:20:11 -050026 debug("SF: Failed to send command (%zu bytes): %d\n",
Jagannadha Sutradharudu Tekifd35ca52013-07-29 22:16:41 +053027 cmd_len, ret);
Mike Frysinger000044d2011-01-10 02:20:11 -050028 } else if (data_len != 0) {
Jagannadha Sutradharudu Tekifd35ca52013-07-29 22:16:41 +053029 ret = spi_xfer(spi, data_len * 8, data_out, data_in,
30 SPI_XFER_END);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020031 if (ret)
Mike Frysinger000044d2011-01-10 02:20:11 -050032 debug("SF: Failed to transfer %zu bytes of data: %d\n",
Jagannadha Sutradharudu Tekifd35ca52013-07-29 22:16:41 +053033 data_len, ret);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020034 }
35
36 return ret;
37}
38
39int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
40 size_t cmd_len, void *data, size_t data_len)
41{
Mike Frysinger000044d2011-01-10 02:20:11 -050042 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020043}
44
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053045int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
46{
47 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
48}
49
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020050int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
51 const void *data, size_t data_len)
52{
Mike Frysinger000044d2011-01-10 02:20:11 -050053 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020054}