Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPI flash interface |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik |
| 6 | * |
Jagannadha Sutradharudu Teki | d145270 | 2013-10-10 22:32:55 +0530 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 8 | */ |
Mike Frysinger | c5431ac | 2009-03-23 23:03:58 -0400 | [diff] [blame] | 9 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 10 | #include <common.h> |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 11 | #include <spi.h> |
Mike Frysinger | 53421bb | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 12 | |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 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) |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 17 | { |
| 18 | unsigned long flags = SPI_XFER_BEGIN; |
| 19 | int ret; |
| 20 | |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 21 | #ifdef CONFIG_SF_DUAL_FLASH |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 22 | if (spi->flags & SPI_XFER_U_PAGE) |
| 23 | flags |= SPI_XFER_U_PAGE; |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 24 | #endif |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 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) { |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 30 | debug("SF: Failed to send command (%zu bytes): %d\n", |
Jagannadha Sutradharudu Teki | 1a83303 | 2013-07-29 22:16:41 +0530 | [diff] [blame] | 31 | cmd_len, ret); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 32 | } else if (data_len != 0) { |
Jagannadha Sutradharudu Teki | 1a83303 | 2013-07-29 22:16:41 +0530 | [diff] [blame] | 33 | ret = spi_xfer(spi, data_len * 8, data_out, data_in, |
| 34 | SPI_XFER_END); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 35 | if (ret) |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 36 | debug("SF: Failed to transfer %zu bytes of data: %d\n", |
Jagannadha Sutradharudu Teki | 1a83303 | 2013-07-29 22:16:41 +0530 | [diff] [blame] | 37 | data_len, ret); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | return ret; |
| 41 | } |
| 42 | |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 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); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 47 | } |
Mike Frysinger | 6fe6d0d | 2012-03-04 23:18:17 -0500 | [diff] [blame] | 48 | |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 49 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
Simon Glass | b162a7c | 2013-03-11 06:08:02 +0000 | [diff] [blame] | 50 | { |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 51 | return spi_flash_cmd_read(spi, &cmd, 1, response, len); |
Simon Glass | b162a7c | 2013-03-11 06:08:02 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 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) |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 56 | { |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 57 | return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 58 | } |