spi_flash.h 2.47 KB
Newer Older
Alexander Yurtsev's avatar
Alexander Yurtsev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*
 * Interface to SPI flash
 *
 * Copyright (C) 2008 Atmel Corporation
 *
 * 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
 * version 2 as published by the Free Software Foundation.
 *
 * 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
 */
#ifndef _SPI_FLASH_H_
#define _SPI_FLASH_H_

#include <spi.h>
#include <linux/types.h>

struct spi_flash_region {
	unsigned int	count;
	unsigned int	size;
};

struct spi_flash {
	struct spi_slave *spi;

	const char	*name;

	u32		size;

	int		(*read)(struct spi_flash *flash, u32 offset,
				size_t len, void *buf);
	int		(*write)(struct spi_flash *flash, u32 offset,
				size_t len, const void *buf);
	int		(*erase)(struct spi_flash *flash, u32 offset,
				size_t len);
};

struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
		unsigned int max_hz, unsigned int spi_mode);
void spi_flash_free(struct spi_flash *flash);

static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
		size_t len, void *buf)
{
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#if defined(CONFIG_SPI_MAX_XF_LEN)
	u32	adr = offset, pos = 0, sz;
	int	rv;

	do {
		sz = len < CONFIG_SPI_MAX_XF_LEN ? len : CONFIG_SPI_MAX_XF_LEN;
		rv = flash->read(flash, adr, sz, &((u8 *)buf)[pos]);
		if (rv != 0)
			break;
		len -= sz;
		pos += sz;
		adr += sz;
	} while (len);

	return rv;
#else
Alexander Yurtsev's avatar
Alexander Yurtsev committed
72
	return flash->read(flash, offset, len, buf);
73
#endif
Alexander Yurtsev's avatar
Alexander Yurtsev committed
74 75 76 77 78
}

static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
		size_t len, const void *buf)
{
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#if defined(CONFIG_SPI_MAX_XF_LEN)
	u32	adr = offset, pos = 0, sz;
	int	rv;

	do {
		sz = len < CONFIG_SPI_MAX_XF_LEN ? len : CONFIG_SPI_MAX_XF_LEN;
		rv = flash->write(flash, adr, sz, &((const u8 *)buf)[pos]);
		if (rv != 0)
			break;
		len -= sz;
		pos += sz;
		adr += sz;
	} while (len);

	return rv;
#else
Alexander Yurtsev's avatar
Alexander Yurtsev committed
95
	return flash->write(flash, offset, len, buf);
96
#endif
Alexander Yurtsev's avatar
Alexander Yurtsev committed
97 98 99 100 101 102 103 104 105
}

static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
		size_t len)
{
	return flash->erase(flash, offset, len);
}

#endif /* _SPI_FLASH_H_ */