2009-08-07 10:37:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009
|
|
|
|
* Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <spi.h>
|
|
|
|
|
|
|
|
#define SPI_EEPROM_WREN 0x06
|
|
|
|
#define SPI_EEPROM_RDSR 0x05
|
|
|
|
#define SPI_EEPROM_READ 0x03
|
|
|
|
#define SPI_EEPROM_WRITE 0x02
|
|
|
|
|
|
|
|
#ifndef CONFIG_DEFAULT_SPI_BUS
|
|
|
|
#define CONFIG_DEFAULT_SPI_BUS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_DEFAULT_SPI_MODE
|
|
|
|
#define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
|
|
|
|
#endif
|
|
|
|
|
2012-01-20 03:31:01 +00:00
|
|
|
#ifndef CONFIG_SYS_SPI_WRITE_TOUT
|
|
|
|
#define CONFIG_SYS_SPI_WRITE_TOUT (5 * CONFIG_SYS_HZ)
|
|
|
|
#endif
|
|
|
|
|
2012-01-20 03:25:55 +00:00
|
|
|
ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
|
2009-08-07 10:37:36 +00:00
|
|
|
{
|
|
|
|
struct spi_slave *slave;
|
|
|
|
u8 cmd = SPI_EEPROM_READ;
|
|
|
|
|
|
|
|
slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
|
|
|
|
CONFIG_DEFAULT_SPI_MODE);
|
2012-01-20 03:25:55 +00:00
|
|
|
if (!slave)
|
2010-03-14 17:47:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-08-07 10:37:36 +00:00
|
|
|
spi_claim_bus(slave);
|
|
|
|
|
|
|
|
/* command */
|
2012-01-20 03:25:55 +00:00
|
|
|
if (spi_xfer(slave, 8, &cmd, NULL, SPI_XFER_BEGIN))
|
2009-08-07 10:37:36 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
2012-01-20 03:25:55 +00:00
|
|
|
* if alen == 3, addr[0] is the block number, we never use it here.
|
|
|
|
* All we need are the lower 16 bits.
|
2009-08-07 10:37:36 +00:00
|
|
|
*/
|
|
|
|
if (alen == 3)
|
|
|
|
addr++;
|
|
|
|
|
|
|
|
/* address, and data */
|
2012-01-20 03:25:55 +00:00
|
|
|
if (spi_xfer(slave, 16, addr, NULL, 0))
|
2009-08-07 10:37:36 +00:00
|
|
|
return -1;
|
2012-01-20 03:25:55 +00:00
|
|
|
if (spi_xfer(slave, 8 * len, NULL, buffer, SPI_XFER_END))
|
2009-08-07 10:37:36 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
spi_release_bus(slave);
|
|
|
|
spi_free_slave(slave);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-01-20 03:25:55 +00:00
|
|
|
ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
|
2009-08-07 10:37:36 +00:00
|
|
|
{
|
|
|
|
struct spi_slave *slave;
|
|
|
|
char buf[3];
|
2011-07-15 23:31:37 +00:00
|
|
|
ulong start;
|
2009-08-07 10:37:36 +00:00
|
|
|
|
|
|
|
slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
|
|
|
|
CONFIG_DEFAULT_SPI_MODE);
|
2010-03-14 17:47:23 +00:00
|
|
|
if (!slave)
|
|
|
|
return 0;
|
|
|
|
|
2009-08-07 10:37:36 +00:00
|
|
|
spi_claim_bus(slave);
|
|
|
|
|
|
|
|
buf[0] = SPI_EEPROM_WREN;
|
2012-01-20 03:25:55 +00:00
|
|
|
if (spi_xfer(slave, 8, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END))
|
2009-08-07 10:37:36 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
buf[0] = SPI_EEPROM_WRITE;
|
|
|
|
|
|
|
|
/* As for reading, drop addr[0] if alen is 3 */
|
|
|
|
if (alen == 3) {
|
|
|
|
alen--;
|
|
|
|
addr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf + 1, addr, alen);
|
|
|
|
/* command + addr, then data */
|
2012-01-20 03:25:55 +00:00
|
|
|
if (spi_xfer(slave, 24, buf, NULL, SPI_XFER_BEGIN))
|
2009-08-07 10:37:36 +00:00
|
|
|
return -1;
|
2012-01-20 03:25:55 +00:00
|
|
|
if (spi_xfer(slave, len * 8, buffer, NULL, SPI_XFER_END))
|
2009-08-07 10:37:36 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-07-15 23:31:37 +00:00
|
|
|
start = get_timer(0);
|
2009-08-07 10:37:36 +00:00
|
|
|
do {
|
|
|
|
buf[0] = SPI_EEPROM_RDSR;
|
|
|
|
buf[1] = 0;
|
|
|
|
spi_xfer(slave, 16, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
|
|
|
|
|
|
|
|
if (!(buf[1] & 1))
|
|
|
|
break;
|
|
|
|
|
2011-07-15 23:31:37 +00:00
|
|
|
} while (get_timer(start) < CONFIG_SYS_SPI_WRITE_TOUT);
|
2009-08-07 10:37:36 +00:00
|
|
|
|
|
|
|
if (buf[1] & 1)
|
2012-01-20 03:25:55 +00:00
|
|
|
printf("*** spi_write: Timeout while writing!\n");
|
2009-08-07 10:37:36 +00:00
|
|
|
|
|
|
|
spi_release_bus(slave);
|
|
|
|
spi_free_slave(slave);
|
|
|
|
return len;
|
|
|
|
}
|