Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 2 | /* |
| 3 | * SPI flash interface |
| 4 | * |
| 5 | * Copyright (C) 2008 Atmel Corporation |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 6 | * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 7 | */ |
Mike Frysinger | f773a1b | 2009-03-23 23:03:58 -0400 | [diff] [blame] | 8 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 9 | #include <common.h> |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 10 | #include <spi.h> |
Mike Frysinger | e7b44ed | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 11 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 12 | static int spi_flash_read_write(struct spi_slave *spi, |
| 13 | const u8 *cmd, size_t cmd_len, |
| 14 | const u8 *data_out, u8 *data_in, |
| 15 | size_t data_len) |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 16 | { |
| 17 | unsigned long flags = SPI_XFER_BEGIN; |
| 18 | int ret; |
| 19 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 20 | if (data_len == 0) |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 21 | flags |= SPI_XFER_END; |
| 22 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 23 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 24 | if (ret) { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 25 | debug("SF: Failed to send command (%zu bytes): %d\n", |
Jagannadha Sutradharudu Teki | fd35ca5 | 2013-07-29 22:16:41 +0530 | [diff] [blame] | 26 | cmd_len, ret); |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 27 | } else if (data_len != 0) { |
Jagannadha Sutradharudu Teki | fd35ca5 | 2013-07-29 22:16:41 +0530 | [diff] [blame] | 28 | ret = spi_xfer(spi, data_len * 8, data_out, data_in, |
| 29 | SPI_XFER_END); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 30 | if (ret) |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 31 | debug("SF: Failed to transfer %zu bytes of data: %d\n", |
Jagannadha Sutradharudu Teki | fd35ca5 | 2013-07-29 22:16:41 +0530 | [diff] [blame] | 32 | data_len, ret); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | return ret; |
| 36 | } |
| 37 | |
| 38 | int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd, |
| 39 | size_t cmd_len, void *data, size_t data_len) |
| 40 | { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 41 | return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 44 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
| 45 | { |
| 46 | return spi_flash_cmd_read(spi, &cmd, 1, response, len); |
| 47 | } |
| 48 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 49 | int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, |
| 50 | const void *data, size_t data_len) |
| 51 | { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 52 | return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 53 | } |