spi_flash.h 7.07 KB
Newer Older
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
1
/*
2
 * Common SPI flash Interface
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
3 4
 *
 * Copyright (C) 2008 Atmel Corporation
5
 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
6
 *
7
 * SPDX-License-Identifier:	GPL-2.0
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
8
 */
9

Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
10 11 12
#ifndef _SPI_FLASH_H_
#define _SPI_FLASH_H_

13
#include <dm.h>	/* Because we dereference struct udevice here */
14
#include <linux/types.h>
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
15

16 17 18 19 20 21 22 23 24 25 26 27 28
#ifndef CONFIG_SF_DEFAULT_SPEED
# define CONFIG_SF_DEFAULT_SPEED	1000000
#endif
#ifndef CONFIG_SF_DEFAULT_MODE
# define CONFIG_SF_DEFAULT_MODE		SPI_MODE_3
#endif
#ifndef CONFIG_SF_DEFAULT_CS
# define CONFIG_SF_DEFAULT_CS		0
#endif
#ifndef CONFIG_SF_DEFAULT_BUS
# define CONFIG_SF_DEFAULT_BUS		0
#endif

29
struct spi_slave;
30

31 32 33 34
/**
 * struct spi_flash - SPI flash structure
 *
 * @spi:		SPI slave
Jagan Teki's avatar
Jagan Teki committed
35
 * @dev:		SPI flash device
36
 * @name:		Name of SPI flash
Jagan Teki's avatar
Jagan Teki committed
37
 * @dual_flash:		Indicates dual flash memories - dual stacked, parallel
38
 * @shift:		Flash shift useful in dual parallel
39
 * @flags:		Indication of spi flash flags
40 41 42
 * @size:		Total flash size
 * @page_size:		Write (page) size
 * @sector_size:	Sector size
Jagan Teki's avatar
Jagan Teki committed
43
 * @erase_size:		Erase size
44 45 46 47
 * @bank_read_cmd:	Bank read cmd
 * @bank_write_cmd:	Bank write cmd
 * @bank_curr:		Current flash bank
 * @erase_cmd:		Erase cmd 4K, 32K, 64K
48 49
 * @read_cmd:		Read cmd - Array Fast, Extn read and quad read.
 * @write_cmd:		Write cmd - page and quad program.
Jagan Teki's avatar
Jagan Teki committed
50 51
 * @dummy_byte:		Dummy cycles for read operation.
 * @memory_map:		Address of read-only SPI flash access
52 53 54
 * @flash_lock:		lock a region of the SPI Flash
 * @flash_unlock:	unlock a region of the SPI Flash
 * @flash_is_locked:	check if a region of the SPI Flash is completely locked
55 56
 * @read:		Flash read ops: Read len bytes at offset into buf
 *			Supported cmds: Fast Array Read
Vasili Galka's avatar
Vasili Galka committed
57
 * @write:		Flash write ops: Write len bytes from buf into offset
58 59 60
 *			Supported cmds: Page Program
 * @erase:		Flash erase ops: Erase len bytes from offset
 *			Supported cmds: Sector erase 4K, 32K, 64K
Vasili Galka's avatar
Vasili Galka committed
61
 * return 0 - Success, 1 - Failure
62
 */
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
63 64
struct spi_flash {
	struct spi_slave *spi;
65
#ifdef CONFIG_DM_SPI_FLASH
66 67
	struct udevice *dev;
#endif
68
	const char *name;
69
	u8 dual_flash;
70
	u8 shift;
71
	u16 flags;
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
72

73 74 75 76
	u32 size;
	u32 page_size;
	u32 sector_size;
	u32 erase_size;
77
#ifdef CONFIG_SPI_FLASH_BAR
78 79 80
	u8 bank_read_cmd;
	u8 bank_write_cmd;
	u8 bank_curr;
81
#endif
82
	u8 erase_cmd;
83
	u8 read_cmd;
84
	u8 write_cmd;
85
	u8 dummy_byte;
86

87
	void *memory_map;
88 89 90 91

	int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
	int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
	int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
92 93 94
#ifndef CONFIG_DM_SPI_FLASH
	/*
	 * These are not strictly needed for driver model, but keep them here
95
	 * while the transition is in progress.
96 97 98 99 100
	 *
	 * Normally each driver would provide its own operations, but for
	 * SPI flash most chips use the same algorithms. One approach is
	 * to create a 'common' SPI flash device which knows how to talk
	 * to most devices, and then allow other drivers to be used instead
101
	 * if required, perhaps with a way of scanning through the list to
102 103
	 * find the driver that matches the device.
	 */
104 105 106 107
	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);
108 109 110 111 112 113 114 115
#endif
};

struct dm_spi_flash_ops {
	int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
	int (*write)(struct udevice *dev, u32 offset, size_t len,
		     const void *buf);
	int (*erase)(struct udevice *dev, u32 offset, size_t len);
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
116 117
};

118 119 120 121
/* Access the serial operations for a device */
#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)

#ifdef CONFIG_DM_SPI_FLASH
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/**
 * spi_flash_read_dm() - Read data from SPI flash
 *
 * @dev:	SPI flash device
 * @offset:	Offset into device in bytes to read from
 * @len:	Number of bytes to read
 * @buf:	Buffer to put the data that is read
 * @return 0 if OK, -ve on error
 */
int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);

/**
 * spi_flash_write_dm() - Write data to SPI flash
 *
 * @dev:	SPI flash device
 * @offset:	Offset into device in bytes to write to
 * @len:	Number of bytes to write
 * @buf:	Buffer containing bytes to write
 * @return 0 if OK, -ve on error
 */
int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
		       const void *buf);

/**
 * spi_flash_erase_dm() - Erase blocks of the SPI flash
 *
 * Note that @len must be a muiltiple of the flash sector size.
 *
 * @dev:	SPI flash device
 * @offset:	Offset into device in bytes to start erasing
 * @len:	Number of bytes to erase
 * @return 0 if OK, -ve on error
 */
int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);

157 158 159 160 161 162 163 164 165 166 167 168
int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
			   unsigned int max_hz, unsigned int spi_mode,
			   struct udevice **devp);

/* Compatibility function - this is the old U-Boot API */
struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
				  unsigned int max_hz, unsigned int spi_mode);

/* Compatibility function - this is the old U-Boot API */
void spi_flash_free(struct spi_flash *flash);

static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
169
				 size_t len, void *buf)
170
{
171
	return spi_flash_read_dm(flash->dev, offset, len, buf);
172 173 174
}

static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
175
				  size_t len, const void *buf)
176
{
177
	return spi_flash_write_dm(flash->dev, offset, len, buf);
178 179 180
}

static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
181
				  size_t len)
182
{
183
	return spi_flash_erase_dm(flash->dev, offset, len);
184 185 186 187 188 189 190 191 192 193
}

struct sandbox_state;

int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
			 struct udevice *bus, int of_offset, const char *spec);

void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);

#else
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
194 195
struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
		unsigned int max_hz, unsigned int spi_mode);
196 197 198 199 200 201 202 203 204 205 206 207 208

/**
 * Set up a new SPI flash from an fdt node
 *
 * @param blob		Device tree blob
 * @param slave_node	Pointer to this SPI slave node in the device tree
 * @param spi_node	Cached pointer to the SPI interface this node belongs
 *			to
 * @return 0 if ok, -1 on error
 */
struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
				      int spi_node);

Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
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)
{
	return flash->read(flash, offset, len, buf);
}

static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
		size_t len, const void *buf)
{
	return flash->write(flash, offset, len, buf);
}

static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
		size_t len)
{
	return flash->erase(flash, offset, len);
}
228
#endif
Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
229

230 231 232
static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
					bool prot)
{
233
	if (!flash->flash_lock || !flash->flash_unlock)
234 235 236 237 238 239 240 241
		return -EOPNOTSUPP;

	if (prot)
		return flash->flash_lock(flash, ofs, len);
	else
		return flash->flash_unlock(flash, ofs, len);
}

242
void spi_boot(void) __noreturn;
243
void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
244

Haavard Skinnemoen's avatar
Haavard Skinnemoen committed
245
#endif /* _SPI_FLASH_H_ */