1 /* 2 * Common SPI flash Interface 3 * 4 * Copyright (C) 2008 Atmel Corporation 5 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. 6 * 7 * See file CREDITS for list of people who contributed to this 8 * project. 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * version 2 as published by the Free Software Foundation. 13 */ 14 15 #ifndef _SPI_FLASH_H_ 16 #define _SPI_FLASH_H_ 17 18 #include <spi.h> 19 #include <linux/types.h> 20 #include <linux/compiler.h> 21 22 /* sf param flags */ 23 #define SECT_4K 1 << 1 24 #define SECT_32K 1 << 2 25 #define E_FSR 1 << 3 26 #define WR_QPP 1 << 4 27 28 /* Enum list - Full read commands */ 29 enum spi_read_cmds { 30 ARRAY_SLOW = 1 << 0, 31 DUAL_OUTPUT_FAST = 1 << 1, 32 DUAL_IO_FAST = 1 << 2, 33 QUAD_OUTPUT_FAST = 1 << 3, 34 QUAD_IO_FAST = 1 << 4, 35 }; 36 #define RD_EXTN ARRAY_SLOW | DUAL_OUTPUT_FAST | DUAL_IO_FAST 37 #define RD_FULL RD_EXTN | QUAD_OUTPUT_FAST | QUAD_IO_FAST 38 39 /* Dual SPI flash memories */ 40 enum spi_dual_flash { 41 SF_SINGLE_FLASH = 0, 42 SF_DUAL_STACKED_FLASH = 1 << 0, 43 }; 44 45 /** 46 * struct spi_flash_params - SPI/QSPI flash device params structure 47 * 48 * @name: Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO]) 49 * @jedec: Device jedec ID (0x[1byte_manuf_id][2byte_dev_id]) 50 * @ext_jedec: Device ext_jedec ID 51 * @sector_size: Sector size of this device 52 * @nr_sectors: No.of sectors on this device 53 * @e_rd_cmd: Enum list for read commands 54 * @flags: Importent param, for flash specific behaviour 55 */ 56 struct spi_flash_params { 57 const char *name; 58 u32 jedec; 59 u16 ext_jedec; 60 u32 sector_size; 61 u32 nr_sectors; 62 u8 e_rd_cmd; 63 u16 flags; 64 }; 65 66 extern const struct spi_flash_params spi_flash_params_table[]; 67 68 /** 69 * struct spi_flash - SPI flash structure 70 * 71 * @spi: SPI slave 72 * @name: Name of SPI flash 73 * @dual_flash: Indicates dual flash memories - dual stacked 74 * @size: Total flash size 75 * @page_size: Write (page) size 76 * @sector_size: Sector size 77 * @erase_size: Erase size 78 * @bank_read_cmd: Bank read cmd 79 * @bank_write_cmd: Bank write cmd 80 * @bank_curr: Current flash bank 81 * @poll_cmd: Poll cmd - for flash erase/program 82 * @erase_cmd: Erase cmd 4K, 32K, 64K 83 * @read_cmd: Read cmd - Array Fast, Extn read and quad read. 84 * @write_cmd: Write cmd - page and quad program. 85 * @dummy_byte: Dummy cycles for read operation. 86 * @memory_map: Address of read-only SPI flash access 87 * @read: Flash read ops: Read len bytes at offset into buf 88 * Supported cmds: Fast Array Read 89 * @write: Flash write ops: Write len bytes from buf into offeset 90 * Supported cmds: Page Program 91 * @erase: Flash erase ops: Erase len bytes from offset 92 * Supported cmds: Sector erase 4K, 32K, 64K 93 * return 0 - Sucess, 1 - Failure 94 */ 95 struct spi_flash { 96 struct spi_slave *spi; 97 const char *name; 98 u8 dual_flash; 99 100 u32 size; 101 u32 page_size; 102 u32 sector_size; 103 u32 erase_size; 104 #ifdef CONFIG_SPI_FLASH_BAR 105 u8 bank_read_cmd; 106 u8 bank_write_cmd; 107 u8 bank_curr; 108 #endif 109 u8 poll_cmd; 110 u8 erase_cmd; 111 u8 read_cmd; 112 u8 write_cmd; 113 u8 dummy_byte; 114 115 void *memory_map; 116 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf); 117 int (*write)(struct spi_flash *flash, u32 offset, size_t len, 118 const void *buf); 119 int (*erase)(struct spi_flash *flash, u32 offset, size_t len); 120 }; 121 122 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, 123 unsigned int max_hz, unsigned int spi_mode); 124 125 /** 126 * Set up a new SPI flash from an fdt node 127 * 128 * @param blob Device tree blob 129 * @param slave_node Pointer to this SPI slave node in the device tree 130 * @param spi_node Cached pointer to the SPI interface this node belongs 131 * to 132 * @return 0 if ok, -1 on error 133 */ 134 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, 135 int spi_node); 136 137 void spi_flash_free(struct spi_flash *flash); 138 139 static inline int spi_flash_read(struct spi_flash *flash, u32 offset, 140 size_t len, void *buf) 141 { 142 return flash->read(flash, offset, len, buf); 143 } 144 145 static inline int spi_flash_write(struct spi_flash *flash, u32 offset, 146 size_t len, const void *buf) 147 { 148 return flash->write(flash, offset, len, buf); 149 } 150 151 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset, 152 size_t len) 153 { 154 return flash->erase(flash, offset, len); 155 } 156 157 void spi_boot(void) __noreturn; 158 159 #endif /* _SPI_FLASH_H_ */ 160