xref: /openbmc/u-boot/drivers/mtd/spi/sf.c (revision 0a1a1575)
1 /*
2  * SPI flash interface
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <spi.h>
12 
13 static 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)
17 {
18 	unsigned long flags = SPI_XFER_BEGIN;
19 	int ret;
20 
21 	if (data_len == 0)
22 		flags |= SPI_XFER_END;
23 
24 	ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
25 	if (ret) {
26 		debug("SF: Failed to send command (%zu bytes): %d\n",
27 		      cmd_len, ret);
28 	} else if (data_len != 0) {
29 		ret = spi_xfer(spi, data_len * 8, data_out, data_in,
30 					SPI_XFER_END);
31 		if (ret)
32 			debug("SF: Failed to transfer %zu bytes of data: %d\n",
33 			      data_len, ret);
34 	}
35 
36 	return ret;
37 }
38 
39 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
40 		size_t cmd_len, void *data, size_t data_len)
41 {
42 	return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
43 }
44 
45 int 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 
50 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
51 		const void *data, size_t data_len)
52 {
53 	return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
54 }
55