blob: 8bbb09facc2ee766ca36ec350089888a96cc4232 [file] [log] [blame]
Sheetal Tigadoli6c4db5d2020-01-06 00:08:24 +05301/*
2 * Copyright (c) 2019-2020, Broadcom
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <common/debug.h>
8
9#include <spi.h>
10
11#define BITS_PER_BYTE 8
12#define CMD_LEN1 1
13
14static int spi_flash_read_write(const uint8_t *cmd,
15 size_t cmd_len,
16 const uint8_t *data_out,
17 uint8_t *data_in,
18 size_t data_len)
19{
20 unsigned long flags = SPI_XFER_BEGIN;
21 int ret;
22
23 if (data_len == 0)
24 flags |= SPI_XFER_END;
25
26 ret = spi_xfer(cmd_len * BITS_PER_BYTE, cmd, NULL, flags);
27 if (ret) {
28 ERROR("SF: Failed to send command (%zu bytes): %d\n",
29 cmd_len, ret);
30 } else if (data_len != 0) {
31 ret = spi_xfer(data_len * BITS_PER_BYTE, data_out,
32 data_in, SPI_XFER_END);
33 if (ret)
34 ERROR("SF: Failed to transfer %zu bytes of data: %d\n",
35 data_len, ret);
36 }
37
38 return ret;
39}
40
41int spi_flash_cmd_read(const uint8_t *cmd,
42 size_t cmd_len,
43 void *data,
44 size_t data_len)
45{
46 return spi_flash_read_write(cmd, cmd_len, NULL, data, data_len);
47}
48
49int spi_flash_cmd(uint8_t cmd, void *response, size_t len)
50{
51 return spi_flash_cmd_read(&cmd, CMD_LEN1, response, len);
52}
53
54int spi_flash_cmd_write(const uint8_t *cmd,
55 size_t cmd_len,
56 const void *data,
57 size_t data_len)
58{
59 return spi_flash_read_write(cmd, cmd_len, data, NULL, data_len);
60}