spi_sf.c 1.3 KB

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