Blame view
include/spi_flash.h
6.61 KB
d25ce7d24
|
1 |
/* |
a5e8199a1
|
2 |
* Common SPI flash Interface |
d25ce7d24
|
3 4 |
* * Copyright (C) 2008 Atmel Corporation |
a5e8199a1
|
5 |
* Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. |
d25ce7d24
|
6 |
* |
5b8031ccb
|
7 |
* SPDX-License-Identifier: GPL-2.0 |
d25ce7d24
|
8 |
*/ |
a5e8199a1
|
9 |
|
d25ce7d24
|
10 11 |
#ifndef _SPI_FLASH_H_ #define _SPI_FLASH_H_ |
4c2dbefde
|
12 |
#include <dm.h> /* Because we dereference struct udevice here */ |
e06ab6546
|
13 |
#include <linux/types.h> |
d25ce7d24
|
14 |
|
88e34e5ff
|
15 16 17 18 19 20 21 22 23 24 25 26 |
#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 |
ff0960f9a
|
27 |
struct spi_slave; |
33adfb5f9
|
28 29 |
/** |
7ab35d922
|
30 31 32 |
* struct spi_flash - SPI flash structure * * @spi: SPI slave |
5d69df354
|
33 |
* @dev: SPI flash device |
7ab35d922
|
34 |
* @name: Name of SPI flash |
5d69df354
|
35 |
* @dual_flash: Indicates dual flash memories - dual stacked, parallel |
056fbc73d
|
36 |
* @shift: Flash shift useful in dual parallel |
1fabefddf
|
37 |
* @flags: Indication of spi flash flags |
7ab35d922
|
38 39 40 |
* @size: Total flash size * @page_size: Write (page) size * @sector_size: Sector size |
5d69df354
|
41 |
* @erase_size: Erase size |
7ab35d922
|
42 43 44 |
* @bank_read_cmd: Bank read cmd * @bank_write_cmd: Bank write cmd * @bank_curr: Current flash bank |
7ab35d922
|
45 |
* @erase_cmd: Erase cmd 4K, 32K, 64K |
3163aaa63
|
46 47 |
* @read_cmd: Read cmd - Array Fast, Extn read and quad read. * @write_cmd: Write cmd - page and quad program. |
5d69df354
|
48 49 |
* @dummy_byte: Dummy cycles for read operation. * @memory_map: Address of read-only SPI flash access |
c3c016cf7
|
50 51 52 |
* @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 |
7ab35d922
|
53 54 |
* @read: Flash read ops: Read len bytes at offset into buf * Supported cmds: Fast Array Read |
801cec590
|
55 |
* @write: Flash write ops: Write len bytes from buf into offset |
7ab35d922
|
56 57 58 |
* Supported cmds: Page Program * @erase: Flash erase ops: Erase len bytes from offset * Supported cmds: Sector erase 4K, 32K, 64K |
801cec590
|
59 |
* return 0 - Success, 1 - Failure |
7ab35d922
|
60 |
*/ |
d25ce7d24
|
61 62 |
struct spi_flash { struct spi_slave *spi; |
d15e74f16
|
63 |
#ifdef CONFIG_DM_SPI_FLASH |
4c2dbefde
|
64 |
struct udevice *dev; |
4c2dbefde
|
65 |
#endif |
7ab35d922
|
66 |
const char *name; |
f77f46911
|
67 |
u8 dual_flash; |
056fbc73d
|
68 |
u8 shift; |
1fabefddf
|
69 |
u16 flags; |
d25ce7d24
|
70 |
|
7ab35d922
|
71 72 73 74 |
u32 size; u32 page_size; u32 sector_size; u32 erase_size; |
1dcd6d038
|
75 |
#ifdef CONFIG_SPI_FLASH_BAR |
7ab35d922
|
76 77 78 |
u8 bank_read_cmd; u8 bank_write_cmd; u8 bank_curr; |
1dcd6d038
|
79 |
#endif |
7ab35d922
|
80 |
u8 erase_cmd; |
4e09cc1e2
|
81 |
u8 read_cmd; |
3163aaa63
|
82 |
u8 write_cmd; |
ff063ed48
|
83 |
u8 dummy_byte; |
615a15616
|
84 |
|
7ab35d922
|
85 |
void *memory_map; |
c3c016cf7
|
86 87 88 89 |
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); |
4c2dbefde
|
90 91 92 |
#ifndef CONFIG_DM_SPI_FLASH /* * These are not strictly needed for driver model, but keep them here |
d15e74f16
|
93 |
* while the transition is in progress. |
4c2dbefde
|
94 95 96 97 98 |
* * 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 |
d15e74f16
|
99 |
* if required, perhaps with a way of scanning through the list to |
4c2dbefde
|
100 101 |
* find the driver that matches the device. */ |
7ab35d922
|
102 103 104 105 |
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); |
4c2dbefde
|
106 107 108 109 110 111 112 113 |
#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); |
d25ce7d24
|
114 |
}; |
4c2dbefde
|
115 116 117 118 |
/* 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 |
8d987abc6
|
119 120 121 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 |
/** * 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); |
4c2dbefde
|
153 154 155 156 157 158 159 160 161 162 |
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); |
4c2dbefde
|
163 |
static inline int spi_flash_read(struct spi_flash *flash, u32 offset, |
8d987abc6
|
164 |
size_t len, void *buf) |
4c2dbefde
|
165 |
{ |
8d987abc6
|
166 |
return spi_flash_read_dm(flash->dev, offset, len, buf); |
4c2dbefde
|
167 168 169 |
} static inline int spi_flash_write(struct spi_flash *flash, u32 offset, |
8d987abc6
|
170 |
size_t len, const void *buf) |
4c2dbefde
|
171 |
{ |
8d987abc6
|
172 |
return spi_flash_write_dm(flash->dev, offset, len, buf); |
4c2dbefde
|
173 174 175 |
} static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, |
8d987abc6
|
176 |
size_t len) |
4c2dbefde
|
177 |
{ |
8d987abc6
|
178 |
return spi_flash_erase_dm(flash->dev, offset, len); |
4c2dbefde
|
179 180 181 182 183 184 185 186 187 188 |
} 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 |
d25ce7d24
|
189 190 |
struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, unsigned int max_hz, unsigned int spi_mode); |
0efc02499
|
191 |
|
d25ce7d24
|
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
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); } |
4c2dbefde
|
211 |
#endif |
d25ce7d24
|
212 |
|
c3c016cf7
|
213 214 215 |
static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len, bool prot) { |
439fcb9b4
|
216 |
if (!flash->flash_lock || !flash->flash_unlock) |
c3c016cf7
|
217 218 219 220 221 222 223 |
return -EOPNOTSUPP; if (prot) return flash->flash_lock(flash, ofs, len); else return flash->flash_unlock(flash, ofs, len); } |
d25ce7d24
|
224 |
#endif /* _SPI_FLASH_H_ */ |