3 * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 #define SPI_EEPROM_WREN 0x06
28 #define SPI_EEPROM_RDSR 0x05
29 #define SPI_EEPROM_READ 0x03
30 #define SPI_EEPROM_WRITE 0x02
32 #ifndef CONFIG_DEFAULT_SPI_BUS
33 #define CONFIG_DEFAULT_SPI_BUS 0
36 #ifndef CONFIG_DEFAULT_SPI_MODE
37 #define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
40 #ifndef CONFIG_SYS_SPI_WRITE_TOUT
41 #define CONFIG_SYS_SPI_WRITE_TOUT (5 * CONFIG_SYS_HZ)
44 ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
46 struct spi_slave *slave;
47 u8 cmd = SPI_EEPROM_READ;
49 slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
50 CONFIG_DEFAULT_SPI_MODE);
57 if (spi_xfer(slave, 8, &cmd, NULL, SPI_XFER_BEGIN))
61 * if alen == 3, addr[0] is the block number, we never use it here.
62 * All we need are the lower 16 bits.
67 /* address, and data */
68 if (spi_xfer(slave, 16, addr, NULL, 0))
70 if (spi_xfer(slave, 8 * len, NULL, buffer, SPI_XFER_END))
73 spi_release_bus(slave);
74 spi_free_slave(slave);
78 ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
80 struct spi_slave *slave;
84 slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
85 CONFIG_DEFAULT_SPI_MODE);
91 buf[0] = SPI_EEPROM_WREN;
92 if (spi_xfer(slave, 8, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END))
95 buf[0] = SPI_EEPROM_WRITE;
97 /* As for reading, drop addr[0] if alen is 3 */
103 memcpy(buf + 1, addr, alen);
104 /* command + addr, then data */
105 if (spi_xfer(slave, 24, buf, NULL, SPI_XFER_BEGIN))
107 if (spi_xfer(slave, len * 8, buffer, NULL, SPI_XFER_END))
110 start = get_timer(0);
112 buf[0] = SPI_EEPROM_RDSR;
114 spi_xfer(slave, 16, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
119 } while (get_timer(start) < CONFIG_SYS_SPI_WRITE_TOUT);
122 printf("*** spi_write: Timeout while writing!\n");
124 spi_release_bus(slave);
125 spi_free_slave(slave);