1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Common SPI flash Interface 4 * 5 * Copyright (C) 2008 Atmel Corporation 6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. 7 */ 8 9 #ifndef _SPI_FLASH_H_ 10 #define _SPI_FLASH_H_ 11 12 #include <dm.h> /* Because we dereference struct udevice here */ 13 #include <linux/types.h> 14 #include <linux/mtd/spi-nor.h> 15 16 /* by default ENV use the same parameters than SF command */ 17 #ifndef CONFIG_ENV_SPI_BUS 18 # define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS 19 #endif 20 #ifndef CONFIG_ENV_SPI_CS 21 # define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS 22 #endif 23 #ifndef CONFIG_ENV_SPI_MAX_HZ 24 # define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED 25 #endif 26 #ifndef CONFIG_ENV_SPI_MODE 27 # define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE 28 #endif 29 30 struct spi_slave; 31 32 struct dm_spi_flash_ops { 33 int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf); 34 int (*write)(struct udevice *dev, u32 offset, size_t len, 35 const void *buf); 36 int (*erase)(struct udevice *dev, u32 offset, size_t len); 37 /** 38 * get_sw_write_prot() - Check state of software write-protect feature 39 * 40 * SPI flash chips can lock a region of the flash defined by a 41 * 'protected area'. This function checks if this protected area is 42 * defined. 43 * 44 * @dev: SPI flash device 45 * @return 0 if no region is write-protected, 1 if a region is 46 * write-protected, -ENOSYS if the driver does not implement this, 47 * other -ve value on error 48 */ 49 int (*get_sw_write_prot)(struct udevice *dev); 50 int (*flash_ctrl_wlock)(struct udevice *dev, u32 offset, size_t len); 51 int (*flash_ctrl_wunlock)(struct udevice *dev, u32 offset, size_t len); 52 }; 53 54 /* Access the serial operations for a device */ 55 #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops) 56 57 #ifdef CONFIG_DM_SPI_FLASH 58 /** 59 * spi_flash_read_dm() - Read data from SPI flash 60 * 61 * @dev: SPI flash device 62 * @offset: Offset into device in bytes to read from 63 * @len: Number of bytes to read 64 * @buf: Buffer to put the data that is read 65 * @return 0 if OK, -ve on error 66 */ 67 int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf); 68 69 /** 70 * spi_flash_write_dm() - Write data to SPI flash 71 * 72 * @dev: SPI flash device 73 * @offset: Offset into device in bytes to write to 74 * @len: Number of bytes to write 75 * @buf: Buffer containing bytes to write 76 * @return 0 if OK, -ve on error 77 */ 78 int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len, 79 const void *buf); 80 81 /** 82 * spi_flash_erase_dm() - Erase blocks of the SPI flash 83 * 84 * Note that @len must be a muiltiple of the flash sector size. 85 * 86 * @dev: SPI flash device 87 * @offset: Offset into device in bytes to start erasing 88 * @len: Number of bytes to erase 89 * @return 0 if OK, -ve on error 90 */ 91 int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len); 92 93 /** 94 * spl_flash_get_sw_write_prot() - Check state of software write-protect feature 95 * 96 * SPI flash chips can lock a region of the flash defined by a 97 * 'protected area'. This function checks if this protected area is 98 * defined. 99 * 100 * @dev: SPI flash device 101 * @return 0 if no region is write-protected, 1 if a region is 102 * write-protected, -ENOSYS if the driver does not implement this, 103 * other -ve value on error 104 */ 105 int spl_flash_get_sw_write_prot(struct udevice *dev); 106 int spi_flash_ctrl_wlock_dm(struct udevice *dev, u32 offset, size_t len); 107 int spi_flash_ctrl_wunlock_dm(struct udevice *dev, u32 offset, size_t len); 108 109 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs, 110 unsigned int max_hz, unsigned int spi_mode, 111 struct udevice **devp); 112 113 /* Compatibility function - this is the old U-Boot API */ 114 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, 115 unsigned int max_hz, unsigned int spi_mode); 116 117 /* Compatibility function - this is the old U-Boot API */ 118 void spi_flash_free(struct spi_flash *flash); 119 120 static inline int spi_flash_read(struct spi_flash *flash, u32 offset, 121 size_t len, void *buf) 122 { 123 return spi_flash_read_dm(flash->dev, offset, len, buf); 124 } 125 126 static inline int spi_flash_write(struct spi_flash *flash, u32 offset, 127 size_t len, const void *buf) 128 { 129 return spi_flash_write_dm(flash->dev, offset, len, buf); 130 } 131 132 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, 133 size_t len) 134 { 135 return spi_flash_erase_dm(flash->dev, offset, len); 136 } 137 138 static inline int spi_flash_ctrl_wlock(struct spi_flash *flash, 139 u32 offset, size_t len) 140 { 141 return spi_flash_ctrl_wlock_dm(flash->dev, offset, len); 142 } 143 144 static inline int spi_flash_ctrl_wunlock(struct spi_flash *flash, 145 u32 offset, size_t len) 146 { 147 return spi_flash_ctrl_wunlock_dm(flash->dev, offset, len); 148 } 149 150 struct sandbox_state; 151 152 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, 153 struct udevice *bus, ofnode node, const char *spec); 154 155 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs); 156 157 #else 158 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, 159 unsigned int max_hz, unsigned int spi_mode); 160 161 void spi_flash_free(struct spi_flash *flash); 162 163 static inline int spi_flash_read(struct spi_flash *flash, u32 offset, 164 size_t len, void *buf) 165 { 166 struct mtd_info *mtd = &flash->mtd; 167 size_t retlen; 168 169 return mtd->_read(mtd, offset, len, &retlen, buf); 170 } 171 172 static inline int spi_flash_write(struct spi_flash *flash, u32 offset, 173 size_t len, const void *buf) 174 { 175 struct mtd_info *mtd = &flash->mtd; 176 size_t retlen; 177 178 return mtd->_write(mtd, offset, len, &retlen, buf); 179 } 180 181 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, 182 size_t len) 183 { 184 struct mtd_info *mtd = &flash->mtd; 185 struct erase_info instr; 186 187 if (offset % mtd->erasesize || len % mtd->erasesize) { 188 printf("SF: Erase offset/length not multiple of erase size\n"); 189 return -EINVAL; 190 } 191 192 memset(&instr, 0, sizeof(instr)); 193 instr.addr = offset; 194 instr.len = len; 195 196 return mtd->_erase(mtd, &instr); 197 } 198 #endif 199 200 static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len, 201 bool prot) 202 { 203 if (!flash->flash_lock || !flash->flash_unlock) 204 return -EOPNOTSUPP; 205 206 if (prot) 207 return flash->flash_lock(flash, ofs, len); 208 else 209 return flash->flash_unlock(flash, ofs, len); 210 } 211 212 #endif /* _SPI_FLASH_H_ */ 213