Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 |
| 3 | * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com> |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <spi.h> |
| 26 | |
| 27 | #define SPI_EEPROM_WREN 0x06 |
| 28 | #define SPI_EEPROM_RDSR 0x05 |
| 29 | #define SPI_EEPROM_READ 0x03 |
| 30 | #define SPI_EEPROM_WRITE 0x02 |
| 31 | |
| 32 | #ifndef CONFIG_DEFAULT_SPI_BUS |
| 33 | #define CONFIG_DEFAULT_SPI_BUS 0 |
| 34 | #endif |
| 35 | |
| 36 | #ifndef CONFIG_DEFAULT_SPI_MODE |
| 37 | #define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0 |
| 38 | #endif |
| 39 | |
Mike Frysinger | 8b463da | 2012-01-19 22:31:01 -0500 | [diff] [blame] | 40 | #ifndef CONFIG_SYS_SPI_WRITE_TOUT |
| 41 | #define CONFIG_SYS_SPI_WRITE_TOUT (5 * CONFIG_SYS_HZ) |
| 42 | #endif |
| 43 | |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 44 | ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len) |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 45 | { |
| 46 | struct spi_slave *slave; |
| 47 | u8 cmd = SPI_EEPROM_READ; |
| 48 | |
| 49 | slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000, |
| 50 | CONFIG_DEFAULT_SPI_MODE); |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 51 | if (!slave) |
Albin Tonnerre | f852a0c | 2010-03-14 18:47:23 +0100 | [diff] [blame] | 52 | return 0; |
| 53 | |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 54 | spi_claim_bus(slave); |
| 55 | |
| 56 | /* command */ |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 57 | if (spi_xfer(slave, 8, &cmd, NULL, SPI_XFER_BEGIN)) |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 58 | return -1; |
| 59 | |
| 60 | /* |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 61 | * if alen == 3, addr[0] is the block number, we never use it here. |
| 62 | * All we need are the lower 16 bits. |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 63 | */ |
| 64 | if (alen == 3) |
| 65 | addr++; |
| 66 | |
| 67 | /* address, and data */ |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 68 | if (spi_xfer(slave, 16, addr, NULL, 0)) |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 69 | return -1; |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 70 | if (spi_xfer(slave, 8 * len, NULL, buffer, SPI_XFER_END)) |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 71 | return -1; |
| 72 | |
| 73 | spi_release_bus(slave); |
| 74 | spi_free_slave(slave); |
| 75 | return len; |
| 76 | } |
| 77 | |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 78 | ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len) |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 79 | { |
| 80 | struct spi_slave *slave; |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 81 | char buf[3]; |
Graeme Russ | a60d1e5 | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 82 | ulong start; |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 83 | |
| 84 | slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000, |
| 85 | CONFIG_DEFAULT_SPI_MODE); |
Albin Tonnerre | f852a0c | 2010-03-14 18:47:23 +0100 | [diff] [blame] | 86 | if (!slave) |
| 87 | return 0; |
| 88 | |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 89 | spi_claim_bus(slave); |
| 90 | |
| 91 | buf[0] = SPI_EEPROM_WREN; |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 92 | if (spi_xfer(slave, 8, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END)) |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 93 | return -1; |
| 94 | |
| 95 | buf[0] = SPI_EEPROM_WRITE; |
| 96 | |
| 97 | /* As for reading, drop addr[0] if alen is 3 */ |
| 98 | if (alen == 3) { |
| 99 | alen--; |
| 100 | addr++; |
| 101 | } |
| 102 | |
| 103 | memcpy(buf + 1, addr, alen); |
| 104 | /* command + addr, then data */ |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 105 | if (spi_xfer(slave, 24, buf, NULL, SPI_XFER_BEGIN)) |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 106 | return -1; |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 107 | if (spi_xfer(slave, len * 8, buffer, NULL, SPI_XFER_END)) |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 108 | return -1; |
| 109 | |
Graeme Russ | a60d1e5 | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 110 | start = get_timer(0); |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 111 | do { |
| 112 | buf[0] = SPI_EEPROM_RDSR; |
| 113 | buf[1] = 0; |
| 114 | spi_xfer(slave, 16, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END); |
| 115 | |
| 116 | if (!(buf[1] & 1)) |
| 117 | break; |
| 118 | |
Graeme Russ | a60d1e5 | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 119 | } while (get_timer(start) < CONFIG_SYS_SPI_WRITE_TOUT); |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 120 | |
| 121 | if (buf[1] & 1) |
Mike Frysinger | a1f77b6 | 2012-01-19 22:25:55 -0500 | [diff] [blame] | 122 | printf("*** spi_write: Timeout while writing!\n"); |
Albin Tonnerre | 3ac374c | 2009-08-07 12:37:36 +0200 | [diff] [blame] | 123 | |
| 124 | spi_release_bus(slave); |
| 125 | spi_free_slave(slave); |
| 126 | return len; |
| 127 | } |