2 * Interface to SPI flash
4 * Copyright (C) 2008 Atmel Corporation
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
20 #include <spi-generic.h>
21 #include <boot/coreboot_tables.h>
23 /* SPI Flash opcodes */
24 #define SPI_OPCODE_WREN 0x06
25 #define SPI_OPCODE_FAST_READ 0x0b
30 * Representation of SPI flash operations:
31 * read: Flash read operation.
32 * write: Flash write operation.
33 * erase: Flash erase operation.
34 * status: Read flash status register.
36 struct spi_flash_ops
{
37 int (*read
)(const struct spi_flash
*flash
, u32 offset
, size_t len
,
39 int (*write
)(const struct spi_flash
*flash
, u32 offset
, size_t len
,
41 int (*erase
)(const struct spi_flash
*flash
, u32 offset
, size_t len
);
42 int (*status
)(const struct spi_flash
*flash
, u8
*reg
);
44 * Returns 1 if the whole region is software write protected.
45 * Hardware write protection mechanism aren't accounted.
46 * If the write protection could be changed, due to unlocked status
47 * register for example, 0 should be returned.
48 * Returns -1 on error.
50 int (*get_write_protection
)(const struct spi_flash
*flash
,
51 const struct region
*region
);
63 const struct spi_flash_ops
*ops
;
64 const void *driver_private
;
67 void lb_spi_flash(struct lb_header
*header
);
69 /* SPI Flash Driver Public API */
72 * Probe for SPI flash chip on given SPI bus and chip select and fill info in
73 * spi_flash structure.
76 * bus = SPI Bus # for the flash chip
77 * cs = Chip select # for the flash chip
78 * flash = Pointer to spi flash structure that needs to be filled
84 int spi_flash_probe(unsigned int bus
, unsigned int cs
, struct spi_flash
*flash
);
87 * Generic probing for SPI flash chip based on the different flashes provided.
90 * spi = Pointer to spi_slave structure
91 * flash = Pointer to spi_flash structure that needs to be filled.
97 int spi_flash_generic_probe(const struct spi_slave
*slave
,
98 struct spi_flash
*flash
);
100 /* All the following functions return 0 on success and non-zero on error. */
101 int spi_flash_read(const struct spi_flash
*flash
, u32 offset
, size_t len
,
103 int spi_flash_write(const struct spi_flash
*flash
, u32 offset
, size_t len
,
105 int spi_flash_erase(const struct spi_flash
*flash
, u32 offset
, size_t len
);
106 int spi_flash_status(const struct spi_flash
*flash
, u8
*reg
);
109 * Return the vendor dependent SPI flash write protection state.
110 * @param flash : A SPI flash device
111 * @param region: A subregion of the device's region
115 * 0 if the device doesn't support block protection
116 * 0 if the device doesn't enable block protection
117 * 0 if given range isn't covered by block protection
118 * 1 if given range is covered by block protection
120 int spi_flash_is_write_protected(const struct spi_flash
*flash
,
121 const struct region
*region
);
123 * Some SPI controllers require exclusive access to SPI flash when volatile
124 * operations like erase or write are being performed. In such cases,
125 * volatile_group_begin will gain exclusive access to SPI flash if not already
126 * acquired and volatile_group_end will end exclusive access if this was the
127 * last request in the group. spi_flash_{write,erase} operations call
128 * volatile_group_begin at the start of function and volatile_group_end after
129 * erase/write operation is performed. These functions can also be used by any
130 * components that wish to club multiple volatile operations into a single
133 int spi_flash_volatile_group_begin(const struct spi_flash
*flash
);
134 int spi_flash_volatile_group_end(const struct spi_flash
*flash
);
137 * These are callbacks for marking the start and end of volatile group as
138 * handled by the chipset. Not every chipset requires this special handling. So,
139 * these functions are expected to be implemented in Kconfig option for volatile
140 * group is enabled (SPI_FLASH_HAS_VOLATILE_GROUP).
142 int chipset_volatile_group_begin(const struct spi_flash
*flash
);
143 int chipset_volatile_group_end(const struct spi_flash
*flash
);
145 /* Return spi_flash object reference for the boot device. This is only valid
146 * if CONFIG_BOOT_DEVICE_SPI_FLASH is enabled. */
147 const struct spi_flash
*boot_device_spi_flash(void);
149 /* Protect a region of spi flash using its controller, if available. Returns
150 * < 0 on error, else 0 on success. */
151 int spi_flash_ctrlr_protect_region(const struct spi_flash
*flash
,
152 const struct region
*region
);
155 * This function is provided to support spi flash command-response transactions.
156 * Only 2 vectors are supported and the 'func' is called with appropriate
157 * write and read buffers together. This can be used for chipsets that
158 * have specific spi flash controllers that don't conform to the normal
159 * spi xfer API because they are specialized controllers and not generic.
161 * Returns 0 on success and non-zero on failure.
163 int spi_flash_vector_helper(const struct spi_slave
*slave
,
164 struct spi_op vectors
[], size_t count
,
165 int (*func
)(const struct spi_slave
*slave
, const void *dout
,
166 size_t bytesout
, void *din
, size_t bytesin
));
168 #endif /* _SPI_FLASH_H_ */