xref: /openbmc/u-boot/drivers/mtd/spi/sf.c (revision b2e02d28)
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 #ifdef CONFIG_SF_DUAL_FLASH
22 	if (spi->flags & SPI_XFER_U_PAGE)
23 		flags |= SPI_XFER_U_PAGE;
24 #endif
25 	if (data_len == 0)
26 		flags |= SPI_XFER_END;
27 
28 	ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
29 	if (ret) {
30 		debug("SF: Failed to send command (%zu bytes): %d\n",
31 		      cmd_len, ret);
32 	} else if (data_len != 0) {
33 		ret = spi_xfer(spi, data_len * 8, data_out, data_in,
34 					SPI_XFER_END);
35 		if (ret)
36 			debug("SF: Failed to transfer %zu bytes of data: %d\n",
37 			      data_len, ret);
38 	}
39 
40 	return ret;
41 }
42 
43 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
44 		size_t cmd_len, void *data, size_t data_len)
45 {
46 	return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
47 }
48 
49 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
50 {
51 	return spi_flash_cmd_read(spi, &cmd, 1, response, len);
52 }
53 
54 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
55 		const void *data, size_t data_len)
56 {
57 	return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
58 }
59